From eaff1217b5e5eaed51cf2903943eb5e1f8e0e378 Mon Sep 17 00:00:00 2001 From: bytedream Date: Fri, 28 Feb 2025 21:28:46 +0100 Subject: [PATCH] add more introduction sections --- src/introduction.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/introduction.md b/src/introduction.md index 9c521d8..01cc44c 100644 --- a/src/introduction.md +++ b/src/introduction.md @@ -12,3 +12,19 @@ This smol book describes how to use Lua in the Browser, powered by Rust WebAssem print("Hello from WebAssembly Lua!") ``` + +### How it works + +Unlike other Lua VMs in the browser, like [fengari](https://github.com/fengari-lua/fengari) that is completely written in JavaScript, the approach described here uses the official Lua VM to execute code. +To interact with the Lua VM, the [mlua](https://github.com/mlua-rs/mlua) crate is used which contains all relevant methods to interact with the VM. + +Because Lua relies on some libc functions that aren't available in WebAssembly (most notably `setjmp`, which is used for error handling), it can't be built with the `wasm32-unknown-unknown` toolchain. +This limitation is bypassed by using the `wasm32-unknown-emscripten` toolchain instead. + +### Why? + +In most cases using "web-native" VMs like [fengari](https://github.com/fengari-lua/fengari) is probably the better choice, especially if you just want your code to be run in the browser. +But if you have an existing Rust codebase that uses Lua or that is planned to use Lua, and want to run it in the browser, the describe approach might be the right choice. + +I personally had the case where user code got executed on a server, and I wanted the users to check their code for correctness in the browser before uploading/submitting it. +The Lua environment had custom defined Rust functions that got exposed to Lua and any user code could use, and I didn't want to rewrite everything in JavaScript.