Stream: cranelift

Topic: Running lua 5.1 wasi


view this post on Zulip Brian G. Merrell (Aug 20 2025 at 17:05):

Hi all,

I'm quite new to WASM. My goal is to run Lua 5.1 using wasmtime so that I can support some legacy plugins. I've manage to get lua 5.1 to build to wasi by 1) using the new -wasm-enable-sjlj flag from wasi-sdk, and 2) stubbing out some lua things I don't need (like tmp files and system()).

When I attempt to run the built wasi binary with wasmtime it fails (expectedly?):

% wasmtime run -W exceptions=yes -W legacy-exceptions=yes ./lua
Error: failed to compile: wasm[0]::function[123]::luaD_rawrunprotected

Caused by:
    0: WebAssembly translation error
    1: Unsupported feature: proposed exception handling operator Try { blockty: Empty }

From what I can glean from googling this work is currently going on in cranelift, so I'm not sure if it should be working or not at this point. Any info appreciated and sorry if I'm posting in the wrong channel.

view this post on Zulip Chris Fallin (Aug 20 2025 at 17:13):

@Brian G. Merrell exception handling is under review and should land soon -- I've just gotten all spec-tests to pass in my branch (https://github.com/bytecodealliance/wasmtime/pull/11326). If you wait until the next Wasmtime release, it should be present there, assuming nothing goes wrong

This PR introduces support for the Wasm exception-handling proposal, which introduces a conventional try/catch mechanism to WebAssembly. The PR supports modules that use try_table to register handl...

view this post on Zulip Chris Fallin (Aug 20 2025 at 17:13):

You're correct that current Wasmtime (latest release, and main) do not have Wasm exception-handling support

view this post on Zulip Brian G. Merrell (Aug 20 2025 at 17:14):

Awesome! thank you! (and yes, I built wasmtime from main since I knew that was all in the works)

view this post on Zulip Alex Crichton (Aug 20 2025 at 17:28):

Note though @Brian G. Merrell you'll need to build lua with the standard wasm exceptions proposal, the try instruction that this is failing on is the "legacy wasm exceptions" which Wasmtime has no plans to implement at this time. This is probably going to be figuring out the right flags to pass to the compile (I'm not sure what such flags are myself)

view this post on Zulip Chris Fallin (Aug 20 2025 at 17:34):

I believe LLVM doesn't have support for it directly at the moment; the expected path is to use Binaryen (it has a pass to translate legacy exceptions to standard exceptions; I don't remember off top of head)

view this post on Zulip Chris Fallin (Aug 20 2025 at 17:35):

I don't know whether the wasi-libc release binaries are already translated as such or not though?

view this post on Zulip Brian G. Merrell (Aug 20 2025 at 17:38):

Hmm.. would this be a problem for me since -wasm-enable-sjlj is an llvm flag? (sjlj is needed for Lua setjump/longjump support afaik.)

view this post on Zulip Chris Fallin (Aug 20 2025 at 17:39):

shouldn't be a problem as long as you're able to run the Binaryen pass IIRC

view this post on Zulip Chris Fallin (Aug 20 2025 at 17:40):

LLVM got exception support in Wasm early, before the proposal was overhauled, and then they patched up the difference with Binaryen; and I believe the maintainers of the LLVM backend haven't prioritized an update in LLVM proper because the patch is "good enough" (and they expect ~everyone to run Binaryen on production binaries anyway). It's a little annoying and hopefully one day the extra step won't be necessary; but at least the compatibility path is there

view this post on Zulip Chris Fallin (Aug 20 2025 at 17:42):

(and for the record we are explicitly avoiding implementing legacy exceptions because it's not a standard (and by policy we don't ship nonstandard extensions), and also because the exceptions-proposal folks in the Wasm CG have discussed how to "unship" it eventually and we don't want to make that problem worse)

view this post on Zulip Brian G. Merrell (Aug 20 2025 at 17:51):

Ah, OK. I ran the original wasi binary through binaryen (which I'm hearing about for the first time :sweat_smile:). I suspect this is more what I should be doing atm:

$ wasmtime -W exceptions=yes ./lua.exnref.wasm
Error: failed to compile: wasm[0]::function[96]

Caused by:
    0: WebAssembly translation error
    1: Unsupported feature: exception operators are not yet implemented

view this post on Zulip Chris Fallin (Aug 20 2025 at 17:53):

If you're feeling adventurous, you could pull down my branch (git clone -b wasm-exceptions https://github.com/cfallin/wasmtime), build that and try it out with that command line; we pass spec tests now but a real-world validation would be really cool too

view this post on Zulip Brian G. Merrell (Aug 20 2025 at 17:54):

Great, I will

view this post on Zulip Brian G. Merrell (Aug 20 2025 at 18:20):

I'll be darned! It works:

$ ~/code/github.com/cfallin/wasmtime/target/release/wasmtime -W exceptions=yes ./lua.exnref.wasm
Lua 5.1.5  Copyright (C) 1994-2012 Lua.org, PUC-Rio
> do
  print("About to test error handling...")

  local function boom()
    error("this is a test error")
  end

  local ok, msg = pcall(boom)
  print("pcall returned:", ok, msg)

  local co = coroutine.create(function()
    print("Inside coroutine")
    coroutine.yield("yielded once")
    return "done"
  end)

  local ok1, val1 = coroutine.resume(co)
  print("resume 1:", ok1, val1)

  local ok2, val2 = coroutine.resume(co)
  print("resume 2:", ok2, val2)
end
>> >> >> >> >> >> >> >> >> >> >> >> >> >> >> >> >> >> >> >> >>
About to test error handling...
pcall returned: false   stdin:5: this is a test error
Inside coroutine
resume 1:   true    yielded once
resume 2:   true    done

view this post on Zulip Chris Fallin (Aug 20 2025 at 18:21):

nice! thanks for testing this!

view this post on Zulip Brian G. Merrell (Aug 20 2025 at 18:22):

Thanks for doing the work.. being able to run lua in wasmtime is going to be big for us I think.

view this post on Zulip Chris Fallin (Aug 21 2025 at 03:28):

and now this is merged, so you should be able to build from main (and assuming it doesn't have to be backed out, will be in Wasmtime 37 released in a month, but off by default still)


Last updated: Dec 06 2025 at 06:05 UTC