daurnimator / lua.vm.js

The project is superceded by Fengari. See https://fengari.io/
MIT License
835 stars 101 forks source link

Can you clone a state? #64

Closed jfmmm closed 7 years ago

jfmmm commented 7 years ago

Hi,

Can you clone a state?

from reading the comment in the code I thought just passing the first state like this might work, but I get an error.

luaState2 = new LuaVM.Lua.State(luaState);

I'm still working on my loading of game prototype from #57 and would like to compare the change made by mod to the main game prototypes.

Basically I load the game lua files. Copy the current game prototypes to a variable. load the mod and then compare the change between the game + mod prototypes with the original one.

Since loading the game file is a good half second and I have to do it for every mod in the game. Possibly hundreds, I was wondering if it was possible to just clone the state after loading the game file and then destroy the clone once I'm done with one mod.

Also what's the best way to destroy a state? Since it would be asyncronous I need to free all the memory that was used.

Thanks!

daurnimator commented 7 years ago

Can you clone a state?

Not exactly: the lua C API has no way to clone a lua_State.

However you might be able to clone it via copying the emscripten heap. That may be tricky though: as emscripten has no fork() call as far as I know (see https://github.com/kripken/emscripten/issues/4182): you'd likely have to delve quite deep to figure this out.

from reading the comment in the code I thought just passing the first state like this might work, but I get an error.

This comment? https://github.com/daurnimator/lua.vm.js/blob/4e3fd0341205b6bdecf74de80ea94e5d5e53013a/src/lua.js#L341

That function either creates a new lua state, or wraps an existing one to add JS methods. It says nothing about cloning/copying a state.

I'm still working on my loading of game prototype from #57 and would like to compare the change made by mod to the main game prototypes.

Basically I load the game lua files. Copy the current game prototypes to a variable. load the mod and then compare the change between the game + mod prototypes with the original one.

How do you intend to compare states once you clone them?

Also what's the best way to destroy a state?

As in the normal lua C api: lua_close. There is currently no JS wrapper for this function: a patch would be accepted.

However keep in mind that this wouldn't invalidate all references to the state, nor would it automatically destroy the emscripten heap.

Since it would be asyncronous I need to free all the memory that was used.

How/why would it be asynchronous?

jfmmm commented 7 years ago

How do you intend to compare states once you clone them?

not copying the state, just the object/table in which the game prototype are stored in.

Anyway you made it pretty clear that I need to rethink my strategy here, thank for the enlightenment.