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

Need basic example #1129

Open
blakemcbride opened this issue Aug 23, 2017 · 5 comments
Open

Need basic example #1129

blakemcbride opened this issue Aug 23, 2017 · 5 comments

Comments

@blakemcbride
Copy link

Your "Hello World" example at http://webassembly.org/getting-started/developers-guide/ creates a 1,301 line HTML file, a 2,688 line JavaScript file to make the generated "Hello World" wasm file work. Presuming it is possible, could you please create a demo with the absolute minimal HTML and JavaScript code? What you have provided is useless to me.

@RyanLamansky
Copy link

For a truly simple WASM file, the HTML and JavaScript can be reduced to maybe 5 lines each, and online tools like http://mbebenita.github.io/WasmExplorer/ make it relatively easy to generate a WASM file, but I agree, a sample putting it all together would be very helpful.

It's on my agenda for the .NET-based WebAssembly implementation I'm making, but I'm not aware of any "standard" way to generate a WASM file that would be appropriate for the official site, other than the complex Emscripten-based approach already there...

@binji
Copy link
Member

binji commented Sep 6, 2017

Here's an example that's about as simple as it gets:

<!DOCTYPE html>
<body>
  <pre></pre>
  <script>
    /*
      (module
        (func (export "add") (param i32 i32) (result i32)
          get_local 0
          get_local 1
          i32.add))
    */
    let moduleBytes = new Uint8Array([
      0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x01, 0x07, 0x01, 0x60,
      0x02, 0x7f, 0x7f, 0x01, 0x7f, 0x03, 0x02, 0x01, 0x00, 0x07, 0x07, 0x01,
      0x03, 0x61, 0x64, 0x64, 0x00, 0x00, 0x0a, 0x09, 0x01, 0x07, 0x00, 0x20,
      0x00, 0x20, 0x01, 0x6a, 0x0b
    ]);
    WebAssembly.instantiate(moduleBytes).then(
    ({instance}) => {
      let preEl = document.querySelector('pre');
      preEl.textContent += instance.exports.add(10, 20);
    });
  </script>
</body>

If you load this in your browser it should display 30.

@jfbastien
Copy link
Member

@binji's example, live: https://jsfiddle.net/b1cmfjzy/

@blakemcbride
Copy link
Author

blakemcbride commented Sep 10, 2017

Thanks a lot @binji! Your example has got to be "as simple as it gets". If you could make it just slightly more complex, it would be very helpful. Your example has the bytecode inline. The way I imagine using WebAssembly is to write WA code, compile it in it's own file, and then use it in a standard html/js situation.

Could you extend your example to have the WebAssembly in a separate file rather than inline? Also, could you give the WebAssembly source for the "display 30" example?

Thanks!
Blake

@binji
Copy link
Member

binji commented Sep 11, 2017

OK, here you go: https://gist.github.com/binji/09e61f11f7b9307bcbcc120b95cf4162

The way I imagine using WebAssembly is to write WA code...

You can write WebAssembly text by hand, but we expect most users will compile from a higher-level language.

Also, could you give the WebAssembly source for the "display 30" example?

The source is in the comment before moduleBytes, and I've also included it in the gist.

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

No branches or pull requests

5 participants