Giter Site home page Giter Site logo

thelartians / modern-wasm-starter Goto Github PK

View Code? Open in Web Editor NEW
231.0 9.0 14.0 250 KB

๐Ÿ›ธ Run C++ code on web and create blazingly fast websites! A starter template to easily create WebAssembly packages using type-safe C++ bindings with automatic TypeScript declarations.

Home Page: https://runkit.com/embed/wdcjombu4wnt

License: The Unlicense

TypeScript 47.19% CMake 29.91% C++ 22.29% C 0.61%
c-plus-plus cpp webassembly wasm npm package modern cmake emscripten embind

modern-wasm-starter's Introduction

Check npm version

Modern WASM Starter

A starter template to easily create WebAssembly packages for npm using type-safe C++ code with automatic declarations. This project should take care of most of the boilerplate code required to create a modern and type-safe WebAssembly project.

Features

Usage

Get started

Use this repo as a template to quickly start your own projects!

Build WebAssembly code

To be able to build WebAssembly code from C++ using Emscripten, you must first install and activate the emsdk. To compile the C++ code to WebAssembly, run the following command from the project's root directory.

npm install

This will create the files source/WasmModule.js and source/WasmModule.d.ts from the C++ code in the wasm directory and transpile everything into a JavaScript module in the dist directory. To build your code as wasm, add it as a CPM.cmake dependency in the CMakeLists.txt file and define the bindings in the wasmGlue.cpp source file. To update the wasm and TypeScript declarations, you can run npm run build:wasm.

Run tests

The following command will build and run the test suite.

npm test

For rapid developing, tests can also be started in watch mode, which will automatically run on any code change to the TypeScript or JavaScript sources.

npm start

Fix code style

The following command will run prettier on the TypeScript and clang-format on the C++ source code.

npm run fix:style

Writing bindings

This starter uses the Glue project to create bindings and declarations. Update the wasmGlue.cpp source files to expose new classes or functions. See the Glue or EmGlue projects for documentation and examples.

Memory management

As JavaScript has no destructors, any created C++ objects must be deleted manually, or they will be leaked. To simplify this, the project introduces memory scopes that semi-automatically take care of memory management. The usage is illustrated below.

import { withGreeter } from "modern-wasm-starter";

// `withGreeter()` will run the callback asynchronously in a memory scope and return the result in a `Promise`
withGreeter(greeterModule => {
  // construct a new C++ `Greeter` instance
  const greeter = new greeterModule.Greeter("Wasm");

  // call a member function
  console.log(greeter.greet(greeterModule.LanguageCode.EN));
  
  // any created C++ objects will be destroyed after the function exits, unless they are persisted
});

To see additional techniques, such as synchronous scopes or persisting and removing values outside of the scope, check out the tests or API.

modern-wasm-starter's People

Contributors

dependabot[bot] avatar thelartians avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

modern-wasm-starter's Issues

Issues with nesting classes

I have created a library that parses a tsv file. Each line parsed is added to an object of another class that is used as a collections.

When I used the template to compile I had some initial issues accessing the filesystem but that has been sorted by modifying the CMAKELists and adding some extra arguments.

Now all compiles ok and I have exposed the functions and members using Glue; I am faced a new issue that I can access a member but when I try to console log the data in that member it does not recognise it.

The code for the library is https://github.com/Constology/XERNative

Any suggesting on what is the best practice to write the Glue code for this library?

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.