-
Notifications
You must be signed in to change notification settings - Fork 3
Multithreading
Luau is an embeddable programming language, this fork rewrites the WASM execution API and actually implements interop allowing you to provide a custom environment for the script that is executed, as well as interop allowing JS to call functions from Lua, and Lua to call functions from JS.
Note
As of Luau Web v1.4, the runtime supports a subset of multithreading to run multiple states concurrently parallel.
Caution
This does not mean you should run multiple Luau functions from the same state at once, even if it looks like it works. It is not supported and may blow up in your face later on, but you are free to experiment.
Warning
Multithreading requires JSPI which is currently only massively supported in Chrome and Chromium-based browsers. In unsupported environments like Node.js, Bun, Safari, Firefox, etc, execution is automatically serialized with a warning. Serialized means that two tasks that each wait 1 second will take 2 seconds total instead of 1
Also remember that every time Luau calls a asynchronous JS function, it will automatically await it.
Taking this into account and awaiting every Luau call, this is safe to use:
state.env.set('wait', async function(s: number) {
return new Promise(r => setTimeout(r, s * 1000))
}, true)