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

cant u do like

var input = "local log = print\\ " + editor.Text
luau.exec(input)

make up ur freaking mind what ur are using

This is the only way I can use it

Bro what

I am in Javascript
I want to use Luau in Javascript
Oh look there is already https://www.luau.org/demo, let me just copy the script from there to use Luau
Ok now, let me try and add window to my Luau environment
Wait a minute, I can only change the print function in the Luau environment?
Time to find another alternative
No Luau in Javascript alternatives?
Oh well there are some but they are WASM and they don’t work
What do I do now?


idk what ur even talking at this point

also did you even see how big of an object window in javascript even is? luau is supposed to be lightweight language and not have this b*

BLUDS ACTUALLY INSANE

I want to use Luau in my OWN Website
Let me use Luau.Web.js (THE SCRIPT THAT https://www.luau.org/demo USES TO RUN THEIR LUAU SCRIPTS)
Oh, I found out to use Luau.Web.js like this
How would I simulate Luau scripts in the browser? - #20 by ChatGGPT

Ok, let me use it now
Wow it is working so good

Hmm, can I add my own stuff like

print(window) -- Prints the window object I gave the Luau environment FROM JAVASCRIPT

Ok let me try
Oh how do I do that
Let me use this guy’s answer

Doesn’t work? Wait let me try and change the print function

var Module = { print: console.warn };

Now let me try print in my Luau environment

print("A")

Wow this warns in my console

But I can’t do this for any other function! And I can’t even add my own stuff like

var Module = { a: 1 };

Then

print(a) -- I expect it to print "1" because I set it as "1" but it prints nil

What do I do now?

Wait I should find a new way to run Luau in the browser that lets me set my own variables from javascript

Wow I found GitHub - fengari-lua/fengari: 🌙 φεγγάρι - The Lua VM written in JS ES6 for Node and the browser which is EXACTLY what I want, but… It’s not luau?

Module is only an override lookup, your only way is to fork the wasm build and add your stuff into the environment

Bro what wasm build?

Fork the Luau repo → modify the environment → compile to your own WASM → use that locally or on your own page.

Bro how do I compile to Wasm :skull: :pray:

jesus christ just go ask chatgpt…

Lil bro I did and I still don’t know how

IDK if this is good I will try it a little bit more to see if it is solution

import * as cart_luau from "https://cdn.jsdelivr.net/npm/cart-luau@0.1.6/dist/src/index.min.js";

const memory = new cart_luau.Memory();
const cart = new cart_luau.Cart(new cart_luau.CartOptions({
  memory,
  args: [],
  env: [],
  fds: cart_luau.stdIo("", new Map())
}));

await cart.load("https://github.com/vinterbell/cart/releases/download/v0.1.6/cart.wasm");

const thread = cart.loadThreadFromString("test", `print("hi")`);

await thread.execute();

Here are the files if they ever get deleted
Archive.zip (2.0 MB)

Can’t seem to find out how to edit the environment

So AGAIN the short answer is: yes you can import your own globals into the Luau runtime, but you have to be careful about where and how you register them as i said. The reason why only print worked for you is because print is already defined in the Luau global table (_G), so when you overrode it in your Module it just replaced the existing reference! For new globals like test or window you need to explicitly assign them into the Luau state before execution.


Why your attempt didnt work

When you did:

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

that only created a JS object in your webpage… it dosent automatically push those properties into the Luau runtimes global scope. Luau dosent magically pick up from JS objects unless you tell it to.

Overriding print worked because the engine already maps that global to JS, so replacing it in your Module worked. But new symbols like test and window dont exist yet in Luau, so you need to register them explicitly.


How to properly inject globals

When using Luau.Web.js, the API provides hooks to set up custom environments. The most direct way is to set globals manually on the Luau state before running your script.

  1. Create your Luau runtime
  2. Push your custom JS functions/objects into the global table
  3. Run the Luau code, which can now see those globals

Example:

import { Luau } from "luau-web";

// create runtime
const runtime = new Luau.Runtime();

// define your custom JS functions
function jsTest(msg) {
  console.warn("From Luau:", msg);
}

function jsEval(code) {
  return eval(code);
}

// import into globals
runtime.globals.set("test", jsTest);
runtime.globals.set("windowEval", jsEval);

// now you can run Luau code that calls them
runtime.exec(`
    test("Hello from Luau!");
    windowEval("console.log('Eval works!')");
`);

Whats happening here is that runtime.globals is the API to expose JS functions into Luaus global namespace. After you call set("name", fn), your Luau code can just call name(...).


Passing objects instead of just functions

You can also expose an entire JS object (like your window) if you wrap it properly:

runtime.globals.set("window", {
  eval: (code) => eval(code),
  alert: (msg) => alert(msg)
});

Now in Luau you can call:

window.eval("console.log('hi')")
window.alert("Hello from Luau!")

  • exposing window.eval to Luau means Luau scripts can execute arbitrary JS on your page. Be careful if the Luau code comes from untrusted sources!!!
  • print override works because it was already mapped by default. For new globals you must explicitly set
  • only primitives and functions can pass smoothly. Passing complex objects might require wrapping.
  • you can return values from Luau to JS with runtime.exec() or by reading from runtime.globals.get("varName")

Last Example:

import { Luau } from "luau-web";

const runtime = new Luau.Runtime();

runtime.globals.set("test", (msg) => console.warn("Lua says:", msg));
runtime.globals.set("window", {
  eval: (code) => eval(code),
});

runtime.exec(`
    test("hi")                -- Logs "Lua says: hi"
    window.eval("console.log('JS eval works!')")
`);

These links MIGHT help you!


That should give you what you were aiming for: predefined globals that act like native functions inside Luau!!!

1 Like

Clone the Luau repo, enable the LUAU_WASM target in CMake, then build with Emscripten (emcmake cmake .. && emmake make). Thatll give you a .wasm + JS glue you can run locally or in a webpage!

1 Like

Could I use this for client Javascript and where can I download the module locally

Also I can’t seem to find this “luau-web” module

Thanks :pray: :pray:

Just to clear things up… there isnt a module called luau-web you can import! I showed it as an example, but it dosent exist as an npm package.

If you specifically want Luau running in the browser alongside JS, youd need to go down the WASM route (compile the Luau VM) like @ChiDj123 said or fall back on a Lua-to-JS project thats already packaged for the web!! (for example lua2js)… But im pretty sure theres some kind of lua web module out there!!


There isnt an official Luau-in-the-browser runtime. If your goal is “Luau + JS in the browser” youd either have to compile the Luau VM to WebAssembly yourself or use an existing Lua → JS project (like Fengari). Those arent Luau specifically, but they let you bridge Lua and client-side JavaScript pretty easily.

1 Like