ceifa / wasmoon

A real lua 5.4 VM with JS bindings made with webassembly
MIT License
462 stars 27 forks source link

Expose a method to set environment variables #20

Closed timstableford closed 1 year ago

timstableford commented 3 years ago

Lua can access the env through os.getenv(name). Expose a way of setting that environment variable map.

timstableford commented 3 years ago

There currently seems to be two approaches to this:

  1. Set the environment variables in the Emscripten module. The downside to this is all created engine's will share the same environment.
  2. Override the os.getenv function on a per-engine basis allowing per-engine env vars.

I'm currently most leaning towards the latter because it's more flexible. Along with this it would be nice to provide some utility functions in thread such as createTable and getTable which would both accept a callback where you do actions on the table and then throw an error if the stack wasn't properly balanced. Maybe an upsertTable too to allow overriding pre-loaded tables.

timstableford commented 3 years ago

31 makes it easier to overwrite pre-existing library functions

ceifa commented 3 years ago

I added a way to set in the module, shared by the factory in https://github.com/ceifa/wasmoon/commit/675216a2c61b228e2e31b18e54cf8266a29063f2

Maybe it's not the best solution, but now you can share the node process environment variables with the module easily:

const factory = new LuaFactory(undefined, process.env)

NOTE: It will not share a reference to process.env, the entire object will be copied and will not watch for modifications.