Your trying to define custom global functions in a Luau env. running in the ?browser? (i asume) via Luau.Web.js, but only overriding print seems to work! Thats because of how Luau.Web.js handles the env. and globals
import * as luau from "luau-web";
async function runLuau() {
const runtime = await luau.createRuntime();
runtime.global.set("test", (...args) => {
console.warn(...args);
});
runtime.global.set("window", {
eval: (code) => {
eval(code); // Be careful with eval
},
});
const result = runtime.execute(`
test("Hello from Luau!") -- Calls JS console.warn
window.eval("console.log('Running JS eval')") -- Calls JS eval
`);
console.log(result); // Optional: capture return values
}
runLuau();
(Why wouldnt somthing like this work?)
Luau.Web.js uses a sandboxed environment which also mean…
by default only the print function is exposed to the JS side- Other JS objects like console, window, etc., are not automatically available…
AND You need to explicitly expose functions to the Luau env.!
Theres no global “window” object in Luau.Web.js automatically unlike in normal JS. You have to import functions or objects manually…
Glad I could help. Side note, you should resize big images like this to avoid cluttering the screen. In the markup, there is 500x500. You can change that to your desired size, like 200x200 or something.
Unlike regular JavaScript Luau dosent automatically have a global window object or access to your JS functions. Basically anything you want Luau to use from JavaScript has to be explicitly exposed to the Luau environment!
For example, say you have a simple logging function in JS
function logMessage(msg) {
console.log("From Luau:", msg);
}
You can expose it to Luau by attaching it to the environment:
env.set("log", logMessage);
Then, in your Luau code you can just call:
log("Hello from Luau!")
And itll print to your console like youd expect!
The same goes for objects… if you want to expose a utility object with multiple functions you can do something like:
env.set("utils", {
add: (a, b) => a + b,
greet: name => `Hello, ${name}`
});
Then in Luau:
print(utils.greet("Luau"))
print(utils.add(2, 3))
So yeah, the thing is that nothing from JS is automatically available in Luau. You have to manually pass functions or objects into the Luau environment (which is kinda anoying). Once you do that you can call JS from Luau pretty seamlessly.
I sent this because its a simple way to run Lua in JavaScript. I know its not Luau, but I thought it might still be useful as a lightweight alternative compared to the more “complex” Luau-WASM or Cart solutions! Sorry if its not as usefull as i might thought it was!