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

Compiling to Wasm doesn't include exports by default, at least while doing it on macOS. #14139

Closed
icylace opened this issue Dec 31, 2022 · 2 comments
Labels
bug Observed behavior contradicts documented or intended behavior

Comments

@icylace
Copy link

icylace commented Dec 31, 2022

Zig Version

0.11.0-dev.1017+d86685ac9

Steps to Reproduce and Observed Behavior

I'm running a MacBook Pro 16-inch from 2019 with macOS Ventura 13.1.

  1. Install the latest version of Zig for macOS using Homebrew:

    $ brew install zig --HEAD
  2. Create an add.zig file having this code:

    export fn add(x: i32, y: i32) i32 {
        return x + y;
    }
  3. Compile it to Wasm:

    $ zig build-lib add.zig -target wasm32-freestanding -dynamic
  4. Inspect the generated Wasm file with wasm2wat:

    $ wasm2wat add.wasm
    
    (module
      (memory (;0;) 16)
      (global $__stack_pointer (mut i32) (i32.const 1048576))
      (export "memory" (memory 0)))

    Notice the add() function is missing !

  5. However, if we recompile while forcing the export with --export=add it will work. For brevity of output, let's also use -O ReleaseSmall:

    $ zig build-lib add.zig -target wasm32-freestanding -dynamic -O ReleaseSmall --export=add
  6. Again, inspect the generated Wasm with wasm2wat:

    $ wasm2wat add.wasm
    
    (module
      (type (;0;) (func (param i32 i32) (result i32)))
      (func (;0;) (type 0) (param i32 i32) (result i32)
        local.get 1
        local.get 0
        i32.add)
      (memory (;0;) 16)
      (global (;0;) (mut i32) (i32.const 1048576))
      (export "memory" (memory 0))
      (export "add" (func 0)))

    Finally a good result but we were forced to export explicitly on the command line. This would be a pain if we had many things to export.

Expected Behavior

My expectation is that running:

$ zig build-lib add.zig -target wasm32-freestanding -dynamic

will produce a Wasm file that includes the add() function.

To put it in general terms, all exports should actually be exported by default.

@icylace icylace added the bug Observed behavior contradicts documented or intended behavior label Dec 31, 2022
@schmee
Copy link
Sponsor Contributor

schmee commented Dec 31, 2022

This is an intentional (breaking) change, see #14102.

@icylace
Copy link
Author

icylace commented Dec 31, 2022

I see, so the solution for my situation is to include -rdynamic:

$ zig build-lib add.zig -target wasm32-freestanding -dynamic -rdynamic -O ReleaseSmall

$ wasm2wat add.wasm

(module
  (type (;0;) (func (param i32 i32) (result i32)))
  (func (;0;) (type 0) (param i32 i32) (result i32)
    local.get 1
    local.get 0
    i32.add)
  (memory (;0;) 16)
  (global (;0;) (mut i32) (i32.const 1048576))
  (export "memory" (memory 0))
  (export "add" (func 0)))

That works for me. Thanks, @schmee !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Observed behavior contradicts documented or intended behavior
Projects
None yet
Development

No branches or pull requests

2 participants