menu

Wasmer

Wasmer is a organization dedicated to the advancement and development of WebAssembly through open-source software.

Channels
Team

Emscripten Hello World

February 13, 2019 at 12:17am

Emscripten Hello World

February 13, 2019 at 12:17am

Hi everyone!

So first of all, I've run the wasm-sample-app and it went well, no problems there. Very cool stuff. Now I'm trying to run an Emscripten "Hello World" type of program. Should that be expected to work? It looks something like this:

// emcc -s WASM=1 -Os test.c -o a.out.wasm
extern int printf(const char *, ...);
int main() {
printf("Hello World!\n");
return 0;
}

From looking at the wast, it looks like this relies on a couple of syscall imports. (I'm sort of new to WASM, so I'm not sure if these syscalls are Emscripten specific. I'm guessing they are.)

By the way, here are the imports I'm seeing:

(import "env" "___syscall146" (func $9 (param i32 i32) (result i32)))
(import "env" "abort" (func $10 (param i32)))
(import "env" "_emscripten_memcpy_big" (func $11 (param i32 i32 i32) (result i32)))
(import "env" "___syscall6" (func $12 (param i32 i32) (result i32)))
(import "env" "___syscall54" (func $13 (param i32 i32) (result i32)))
(import "env" "___syscall140" (func $14 (param i32 i32) (result i32)))
(import "env" "table" (table $5 6 6 funcref))
(import "env" "memory" (memory $6 256 256))
(import "env" "__table_base" (global $7 i32))

Trying to run this fails with something like:

$ ./wasmer/target/release/wasmer run c_test/a.out.wasm
thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: ExportNotFound { name: "_malloc" }', src/libcore/result.rs:1009:5
note: Run with `RUST_BACKTRACE=1` for a backtrace.

February 13, 2019 at 12:22am

I can see that there are some examples, like Lua, that were apparently compiled with Emscripten. It would be helpful if there were some example instructions of how Emscripten was called to create those files. (I think I read somewhere that it's being worked on.)

  • reply
  • like

February 14, 2019 at 7:44pm

Hi @tprk77!

We just merged the emscripten changes into the lib, so compatibility should be good to go. We will publish a new package wasmer-emscripten soon, that you can use also if you want to execute Emscripten WebAssembly files in the Wasmer embedded runtime 🙂

  • reply
  • like

February 17, 2019 at 6:21pm

It works! Thank you!

I did realize I was missing a couple flags from my Emscripten command. I was apparently missing some exports. The final command ended up being: emcc -s WASM=1 -s EXPORTED_FUNCTIONS='["_main", "_malloc", "_free", "_memset"]' -Os test.c -o a.out.wasm. (It seems that I could get away with EXPORTED_FUNCTIONS='["_main"]' if I left out the -Os flag.)

  • reply
  • like

@syrusakbary I have another question you might be able to answer. Is there a way to combine two ImportObject? I think I want to combine the result of generate_emscripten_env with my own imports! but I'm not seeing a way to do that.

  • reply
  • like

February 18, 2019 at 4:43am

So anyway, I tried implementing it for fun. The code is probably a bit rough. It's up as PR #190.

  • reply
  • like

I've also successfully got the wasmer-rust-example to run an Emscripten module, which was kind of neat.

  • reply
  • like

But it was very hacky, I basically had to edit the WAST imports and exports to remove the underscores and then recompile.

  • reply
  • like

Not sure if there's a better way to do that currently.

  • reply
  • like