Code Monkey home page Code Monkey logo

emscripten-webpack-cpp-example's Introduction

emscripten-webpack-cpp-example

Complete example of C++ to JS library with Emscripten + Webpack for Browser and Node

There are many technical challenges, that's why I try to present this as simple as possible, but also covering complete aspects of this translation of C++ to JS.

So, there are two main examples intended to be quite simple.

First Example (basic)

A function that computes x+y

int mylibrary_add(int x, int y) {
   return x+y;
}

Second Example (advanced)

A welcome function that receives a number, deals with memory pointers and invokes an external c++ function (implemented in JS):

int mylibrary_welcome(int x) {
   std::string str("hello");
   std::string str_out("world");
   unsigned char ustr[10];
   //
   const char* bn1 = str.c_str();
   const char* bn_out = str_out.c_str();
   unsigned char* u_out = ustr;
   int y = mylibrary_external_welcome(bn1, bn_out, u_out);
   return x+y+u_out[0];
}

How to build

This works currently on Webpack 4.

cd packages/example-webpack4-js
make start

Then, open browser on http://localhost:8080. The file index.html (see online) will consume library from dist/mylibrary.js (and corresponding .wasm).

How does it work

This will compile file src/mylibrary.cpp using docker image emscripten/emsdk and generate outputs on build/ and dist/. The build/ are intermediate files from emscripten, that are useful for usage in Node. The dist/ are post-processed files (after build/), that re useful for browser (in UMD format).

See README.md inside folder packages/example-webpack4-js for detailed information.

How to test

Basic test

This uses prebuilt package files inside build/ and dist/.

Just run command on root (requires npm and node): ./node-test.sh

Expected output:

test with nodejs .js
js mylibrary_external_welcome: ptr_str1=hello
requires some bytes: 5
out space is '5'
out value is 'world'
out value is 'hello'
should compute input+5+3
18

test with nodejs modules .mjs
WARNING: this is disabled! because --bind is breaking things...

Tests on Browser with Puppeteer (inside package)

cd packages/example-webpack4-js
make test

Tests with Node (inside package)

cd packages/example-webpack4-js
make node_test

License

MIT License - Igor Machado Coelho - 2021

Feel free to learn and pass it on!

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.