Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Linking fails with Emscripten #86

Open
Interrupt opened this issue Jun 11, 2024 · 6 comments
Open

Linking fails with Emscripten #86

Interrupt opened this issue Jun 11, 2024 · 6 comments

Comments

@Interrupt
Copy link

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
Copy link
Author

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
Copy link
Owner

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
Copy link
Author

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
Copy link
Owner

Is this the only thing you needed to get it working with emscripten? Interrupt@3d7532e

@Interrupt
Copy link
Author

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

@luchak
Copy link
Contributor

luchak commented Sep 16, 2024

@Interrupt In case it's useful: I got one of the examples running in a browser w/ Luau by lifting a bunch of build code from sokol-zig and switching both compilation (unlike sokol-zig) and linking (like sokol-zig) to invoke emcc directly. #95

edit: Lua 5.x seems to work too now

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants