Open aekobear opened 8 months ago
According to the Lua manual:
When you interact with the Lua API, you are responsible for ensuring consistency. In particular, you are responsible for controlling stack overflow. You can use the function lua_checkstack to ensure that the stack has extra slots when pushing new elements.
Running return
statements pushes values onto the stack. Without lua.global.pop()
/ lua.global.lua.lua_checkstack(lua.global.address)
, the stack grows and eventually overflows.
I am not sure whether this library should call lua_checkstack
for the user though. But remembering to pop values off the stack should free you from random overflows.
According to the Lua manual:
When you interact with the Lua API, you are responsible for ensuring consistency. In particular, you are responsible for controlling stack overflow. You can use the function lua_checkstack to ensure that the stack has extra slots when pushing new elements.
Running
return
statements pushes values onto the stack. Withoutlua.global.pop()
/lua.global.lua.lua_checkstack(lua.global.address)
, the stack grows and eventually overflows.I am not sure whether this library should call
lua_checkstack
for the user though. But remembering to pop values off the stack should free you from random overflows.
This helps me move forward, thank you!
I do think this is something to fix. When using the API directly, it makes sense to manage the stack because that's how you communicate with lua:
lua_call(L, 1, 1) // call a function
result = lua_pop(L, 1) // get its result
However in wasmoon, not only does doString
already copy the value out of the stack, but the wasmoon wrapper for popping the stack doesn't actually return a value (which may also be a bug):
// i already have my result, so no incentive to access stack
const result = await lua.doString(`return ${a} + ${b};`);
// stackResult is always undefined. This is just busy-work
const stackResult = lua.global.pop();
@aekobear thanks for the suggestion, I think it does make sense for wasmoon to pop the values returned, I can't think on a use case for returning the value and it continue on the stack
@tims-bsquare do you have any opinion on this?
I think the lua_xmove is the memory leak in callByteCode. I can't remember why it's that way though. I have a suspicion something about returning a function from doString and calling it later but in theory a reference to that should continue to exist through lua_ref. Could try and remove it and see if things break?
I wrote a small script to benchmark wasmoon's runtime overhead with multiple calls. However calling
doString()
more than a few dozen times causes a memory errorThis script should print 1000 random numbers then return. It always works for roughly the first 60, the crashes with:
I tested on
node@19.1.0
anddeno@1.41.1
and got the same result