How do I add custom globals to Luau.Web.js?

There is a question exactly like mine but there was only one answer and it doesn’t even work:

What I mean is that I want to predefine globals in the environment
E.g.

var Module = {
	test: console.warn,
	window,
};

Then in the Luau environment I would do

test("hi") -- Warns to the console
window.eval("const a = 1") -- Self explanatory

I tried this but it doesn’t work
Only overriding “print” works

var Module = {
	print: console.error
};

Please help
Thanks :pray:

2 Likes

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…

1 Like

Bit of a tangent but you can actually have JavaScript syntax highlighting here.

Instead of starting a code block with ```, start it with ```javascript

import * as luau from "luau-web";

async function runLuau() {
    const runtime = await luau.createRuntime();

    runtime.global.set("test", (...args) => {
        console.warn(...args);
    });

    ...
}
1 Like

Thank you so much!! I dont use devforums often and love to lern knew things like that!!!

1 Like

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.

1 Like

How would I be able to do this? Thank you for your help :pray:

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.

Hope that helps!

This is lua in js, not luau in js, which is the main problem for me
I mean I did find GitHub - leonnatale/luau-wasm: Run Luau with WebAssembly

And also GitHub - vinterbell/cart: a `cross application run time` for luau, designed to be easy to embed and sandbox

But it is too complex for me to use

cant you fork it and add everything you want?

What do you mean
You mean forking lua-in-js and turning it into luau-in-js by adding every singular luau feature?

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!

1 Like

just to clarify, you want it to be luau not lua yes?

1 Like

Yes
I’m looking for something like Fengari but for luau basically

Then ig you could do that, visit luau source code and see how they do metatables and such

So you’re telling me to recreate the entire Luau codebase but in JavaScript?
Bro

1 Like

Theres luau parsers online, all you have to do is get the AST from luau and turn it into js

How do I do that Pls bro :pray:

Or just do what luau.org/demo does

Bro I am using the script Luau demo is using but the problem is that I can’t edit the luau environment from Javascript

im confused, u said ur using lua in js not luau in js???