Git Product home page Git Product logo

cpp-wasm-loader's Introduction

CPP to WASM Webpack loader

Ever wanted to include C/C++ files directly from Javascript? Well now you can.

This Webpack loader allows you to import C or C++ files, which are compiled to WASM using Emscripten.

The C/C++ files have to know about Emscripten, so you can export functions. For simple things it's enough to include emscripten and add EMSCRIPTEN_KEEPALIVE to your functions (+ extern "C" for C++).

You should be able to run simple C/C++ files in any modern browser that supports WASM (Chrome, Firefox etc.).

Usage

Webpack settings

module.exports = {
  module: {
    rules: [
      {
        test: /\.(c|cpp)$/,
        use: {
          loader: 'cpp-wasm-loader',
          options: {
            emccPath: 'path-to-emcc',
            emccFlags: ['-O3']
          }
        }
      }
    ]
  },
  externals: {
    'fs': true
  }
}

C/C++ file

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <emscripten/emscripten.h>

int main(int argc, char ** argv)
{
    printf("WASM loaded\n");
}

// Simple C function that returns a number between 1 and 6.
int EMSCRIPTEN_KEEPALIVE roll_dice() {
    srand ( time(NULL) );
    return rand() % 6 + 1;
}

Usage in Javascript

import wasm from './source.c';

wasm.initialize().then(module => {
  const result = module._roll_dice();
  console.log(result);
});

Configuration

The following options can be added to the Webpack loader query:

Name Description Required Default
emccPath Path to your Emscripten binary (emcc or em++) false emcc
emccFlags Array of compilation flags passed to Esmcripten false ['-O3']

Requirements

Emscripten

License

MIT

Ideas

  • generate Typescript definitions (using Clang)
  • automatically export functions (using Clang)

Inspiration

Inspired by rust-wasm-loader and this article. WASM alternative of cpp-loader.

cpp-wasm-loader's People

Contributors

kobzol avatar

Watchers

 avatar  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.