Skip to content

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

@icylace

Description

@icylace

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugObserved behavior contradicts documented or intended behavior

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions