Giter VIP home page Giter VIP logo

webassembly-sample's Introduction

WebAssembly-Sample

1. Using Emscripten

Compiler setup

[I am using ubuntu 18.04 vm (prefered) for compiling]

Set up the WebAssembly compiler (emscripten) using https://webassembly.org/getting-started/developers-guide/

$ git clone https://github.com/juj/emsdk.git
$ cd emsdk
$ ./emsdk install --build=Release sdk-incoming-64bit binaryen-master-64bit
$ ./emsdk activate --build=Release sdk-incoming-64bit binaryen-master-64bit

start the terminal

Environment setup after installation (non Windows 7 installs)

admin@admin-Virtual-Machine:~/$ cd emsdk                 
admin@admin-Virtual-Machine:~/emsdk$ source ./emsdk_env.sh --build=Release

Setting environment variables:
EMSDK = /home/admin/emsdk

admin@admin-Virtual-Machine:~/emsdk$ cd ..
admin@admin-Virtual-Machine:~/emsdk$ mkdir example
admin@admin-Virtual-Machine:~/$ cd example [this directory has the c/c++ code to be compiled into WebAssembly]
admin@admin-Virtual-Machine:~/example$ gedit test.c [take test.c from this repo]
admin@admin-Virtual-Machine:~/example$ ls
test.c

Compile to WASM

admin@admin-Virtual-Machine:~/example$ emcc test.c -o test.js -s WASM=1 -s NO_EXIT_RUNTIME=1 -s EXTRA_EXPORTED_RUNTIME_METHODS='["ccall"]'

input: test.c

output: test.js, test.wasm

admin@admin-Virtual-Machine:~/example$ ls

test.c 
test.js [generated]
test.wasm [generated]

Using WASM files in Javascript
-------------------------------

admin@admin-Virtual-Machine:~/example$ gedit index.html [take index.html from this repo]

Refer https://developer.mozilla.org/en-US/docs/WebAssembly/C_to_wasm for after installation steps.

Just run index.html and see the result. [firefox, edge prefered to run locally. chrome CORS prevents local system file access]

int square(a) { 
  return a*a;
}
  • Build

  • Download wasm

  • create an html file 'square_usage.html'

<script>
WebAssembly.instantiateStreaming(fetch('square.wasm'))
      .then(obj => {
         console.log(obj.instance.exports.square(5));
      });
</script>
  • Run with a local web server (Required due to fetch API-CORS)

For Refer: https://stackoverflow.com/a/58256067/12167785

3. Using WASM in Node.js

  • Run
node --experimental-modules --experimental-wasm-modules simplemath.mjs

webassembly-sample's People

Contributors

sudhakar3697 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.