• Stars
    star
    239
  • Rank 168,763 (Top 4 %)
  • Language
    TypeScript
  • License
    The Unlicense
  • Created over 4 years ago
  • Updated almost 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

๐Ÿ›ธ 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.

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.

More Repositories

1

ModernCppStarter

๐Ÿš€ Kick-start your C++! A template for modern C++ projects using CMake, CI, code coverage, clang-format, reproducible dependency management and much more.
CMake
4,417
star
2

PEGParser

๐Ÿ’ก Build your own programming language! A C++17 PEG parser generator supporting parser combination, memoization, left-recursion and context-dependent grammars.
C++
240
star
3

Format.cmake

๐Ÿ’… Stylize your code! Automatic clang-format and cmake-format targets for CMake.
Python
159
star
4

EasyIterator

๐Ÿƒ Iterators made easy! Zero cost abstractions for designing and using C++ iterators.
C++
138
star
5

Observe

๐Ÿ“ฃ Hey listen! A simple general-purpose event-listener system for C++17.
CMake
126
star
6

PackageProject.cmake

๐Ÿ›๏ธ Help other developers use your project. A CMake script for packaging C/C++ projects for simple project installation while employing best-practices for maximum compatibility.
CMake
103
star
7

MiniCppStarter

๐Ÿงธ A tiny single-file version of the ModernCppStarter project for exploring libraries or playing with C++ code. Reproducible dependency management included, so the code will work the same everywhere.
CMake
75
star
8

StaticTypeInfo

๐Ÿ€ Up your type-game. A small C++ library for compile-time type names and type indices.
CMake
70
star
9

Ccache.cmake

๐Ÿš… Compile faster with Ccache! A Ccache integration for CMake with Xcode support.
CMake
58
star
10

Glue

โ›“๏ธ Bindings that stick. A simple and generic API for C++ to other language bindings supporting bidirectional communication, inheritance and automatic declarations.
C++
56
star
11

TypeScriptXX

๐Ÿงท Stay safe! Type-safe scripting for C++ using TypeScriptToLua and CMake with auto-generated declarations.
CMake
40
star
12

GroupSourcesByFolder.cmake

Automatically group sources by folder structure for Visual Studio/Xcode generators
CMake
32
star
13

BitLens

๐Ÿ”Ž Have your bits and eat them too! A C++17 bit lens container for vector types.
C++
21
star
14

EmGlue

๐Ÿ•ธ๏ธ Glue C++ to your browser! Universal bindings for JavaScript/Wasm using Glue and Embind.
C++
21
star
15

StaticHash

Constexpr hash functions for C++
C++
18
star
16

substitute

๐ŸฟEnjoy the the big screen! Watch and synchronise subtitles externally in any browser. On mobile and desktop.
TypeScript
16
star
17

TypeScript2Python

๐Ÿšƒ Transpile TypeScript types to Python! A TypeScript to Python type transpiler.
TypeScript
15
star
18

Revisited

๐Ÿง‘โ€๐Ÿคโ€๐Ÿง‘ The visitor pattern revisited. An inheritance-aware acyclic visitor template, any and any-function templates.
C++
15
star
19

react-native-simple-transition

๐ŸŒ  An easy to use transition component for React Native
Java
13
star
20

Generator

A generator class emulating coroutines' yield functionality through std::thread
C++
9
star
21

PyPropagate

A paraxial wave propagation framework for python
Jupyter Notebook
5
star
22

NDArray

[legacy project] A fast n-dimensional array template for C++
C++
5
star
23

LuaGlue

Lua bindings for the Glue library
C++
4
star
24

LHC

[legacy project] Some single-header helper libraries I use in some projects
C++
4
star
25

IndexSet

A class for manipulating large sets of indices with optimal performance and memory use
CMake
3
star
26

denon-cli

A simple command line interface for controlling Denon AVR receivers.
JavaScript
2
star
27

Presentations

https://thelartians.github.io/Presentations
JavaScript
2
star
28

Saphira

Our submission for the #WirVsVirus Hackathron: https://youtu.be/OWfDj2fOfVk | https://thelartians.github.io/Saphira/
TypeScript
2
star
29

NDArrayOld

A fast n-dimensional array template for c++
C++
2
star
30

Lars

A cmake enabled git-submodule collection of my C++ toolbox
C++
1
star
31

advent-of-code-2020

โ˜ƒ๏ธ๐ŸŽ„๐Ÿ‘จโ€๐Ÿ’ป Solving Advent of Code 2020 in Rust ๐Ÿฆ€
Rust
1
star