natecraddock / ziglua

Zig bindings for the Lua C API
MIT License
219 stars 31 forks source link

Linking fails with Emscripten #86

Open Interrupt opened 3 weeks ago

Interrupt commented 3 weeks ago

When making a web build for Emscripten, and linking ziglua with the Emscripten system headers, linking fails with an error like:

/Users/ccuddigan/.cache/zig/p/1220f93ada1fa077ab096bf88a5b159ad421dbf6a478edec78ddb186d0c21d3476d9/src/lauxlib.h:246:3: error: unknown type name 'FILE'
  FILE *f;  /* stream (NULL for incompletely created streams) */

This might be because -DLUA_USE_POSIX is passed in build.zig for all unknown targets, and the WASM case isn't handled.

Interrupt commented 2 weeks ago

Dug more into this, that unknown type name was due to the Zig library not having the correct headers - the Lua library did have the right ones but the Zig library was including some of Lua's C files directly and calling into them which makes sense.

The next issue I found though is harder to work around:

warning: undefined symbol: _longjmp (referenced by root reference (e.g. compiled C/C++ code))
warning: undefined symbol: _setjmp (referenced by root reference (e.g. compiled C/C++ code))

In POSIX mode it can't seem to find _longjmp / _setjmp, in non-POSIX it complains about a similar missing longjmp / setjmp.

It's possible to work around this by modifying ziglua's build.zig file to turn off Lua error handling, but that does not seem to be ideal.

Turning off the error handling can be done by adding the following to the Lua build flags:

-DLUAI_THROW(L,c)={return;}
-DLUAI_TRY(L,c,a)=a
-Dluai_jmpbuf=int
natecraddock commented 2 weeks ago

I pushed a 0.3.0 release to ziglua earlier today and then I was thinking about this issue. Thanks for digging into it a bit more!

Yeah I was thinking about how there are functions not available in wasm. We may need a new build option to enable/disable things :)

Interrupt commented 2 weeks ago

Thinking about this more, maybe what is needed is a way to modify the Lua build flags by the upstream builder pulling in the ziglua dependency. The sokol-zig project does something like this when linking against emscripten, there's an emLinkStep function that among other things takes an array of extra argument flags to use in the form of extra_args: []const []const u8 = &.{},

natecraddock commented 2 weeks ago

Is this the only thing you needed to get it working with emscripten? https://github.com/Interrupt/ziglua/commit/3d7532ec73202bddfbeba1933d43fe427ca6d80d

Interrupt commented 2 days ago

Yes, but that is a super hacky solution! It turns off all of the debug printing amongst other stuff.