-
Notifications
You must be signed in to change notification settings - Fork 3
API
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.
The main class that you will use is LuauState. The first argument is a one-time-use JS object of the globals you wish to add. It will create a Luau State internally for you. All global objects will be immutable by default. Wrap them with the Mutable transformer to allow the Luau runtime to modify the objects. You can also use the .set function of the .env property to change them at runtime.
Caution
By default, certain values that are risky will be blocked from being sent to the Luau runtime. If you are sure you won't be running any unsanitized user input and need to use things like process or Function constructors, you can remove the blocks by clearing the security transmit list:
(async () => {
const state = await LuauState.createAsync({});
InternalLuauWasmModule.securityTransmitList.clear();
//...Note
You can access the environment using the env property of the state. This includes globals such as table and buffer and can not be modified by the Luau runtime.
You can access the global environment containing the globals of the code that is run using the global property of the env property. This contains only Luau runtime controlled global variables and functions.
Mutable(object: object: object | Map<any, any>);
class LuauState(initialEnv?: LuauEnv);
LuauState.loadstring(source: string, chunkName?: string, throwOnCompilationError?: boolean);
LuauTable.get(key: any): any;
LuauTable.set(key: any, value: any, bypassReadonly?: boolean): boolean;loadstring will return a function that runs the code if it compiled correctly. The arguments of the function will be passed on to the functions varargs operator ....
loadstring will return a string if it failed to compile, if throwOnCompilationError is set to true, it will throw an error you can catch instead.
This means you can print it to console for debugging or feedback when loading code.
If readonly exceptions are turned off, LuauTable.set will return true if it has written successfully, otherwise it will return false to indicate the table is readonly.
Important
Remember that Luau functions can return tuples, for glue reasons you must return a single value or if you need to return multiple values you must pass an array. Calling Luau functions will yield an array of values (0-indexed).
Typescript Example
import { LuauState, LuauTable, Mutable } from "luau-web"
(async () => {
const luadata = Mutable({});
const state = await LuauState.createAsync({
example: function (luaTable: LuauTable) {
return [luaTable.somethingelse, luaTable.get('something')]
},
luadata: luadata
});
const func = state.loadstring(`
local one, two = example({something = 5, somethingelse = 10})
print(one, two)
luadata.output = "hi"
`, "index.ts", true);
func();
console.log(luadata.get("output"));
})();