emscripten-tutorial

How to build an increasingly complex C/C++ codebase to WebAssembly
git clone https://git.tronto.net/emscripten-tutorial
Download | Log | Files | Refs | README

commit 4ea98362c39d63bbfaef28aa25a1e630e01c5611
parent 18132bf9c9755a7de41a8c89dc6b02409a629cd5
Author: Sebastiano Tronto <sebastiano@tronto.net>
Date:   Tue, 10 Jun 2025 12:02:10 +0200

Added native example

Diffstat:
A03_threads/build-native.sh | 4++++
A03_threads/native.c | 10++++++++++
M03_threads/program.mjs | 6++++--
MREADME.md | 5+++++
4 files changed, 23 insertions(+), 2 deletions(-)

diff --git a/03_threads/build-native.sh b/03_threads/build-native.sh @@ -0,0 +1,4 @@ +#!/bin/sh + +mkdir -p build +cc -o build/native primes.c native.c diff --git a/03_threads/native.c b/03_threads/native.c @@ -0,0 +1,10 @@ +#include <stdio.h> + +int primes_in_range(int, int); + +int main() { + int low = 1; + int high = 10000000; + int result = primes_in_range(low, high); + printf("There are %d primes between %d and %d\n", result, low, high); +} diff --git a/03_threads/program.mjs b/03_threads/program.mjs @@ -2,5 +2,7 @@ import Primes from "./build/primes.mjs" var primes = await Primes(); -const count = primes._primes_in_range(1, 100); -console.log("There are " + count + " primes between 1 and 100"); +var low = 1; +var high = 10000000; +const count = primes._primes_in_range(low, high); +console.log("There are " + count + " primes between " + low + " and " + high); diff --git a/README.md b/README.md @@ -67,6 +67,11 @@ In `03_threads` we build a more complicated example based on example, the web server has to be configured to provide the correct `Cross-Origin-*` headers, see `03_threads/run-server.sh` for details. +There is also a piece of code meant to run this more complicated library +function natively (`native.c`). It can be used, for example, to compare +the performance of native code vs WASM running in Node.js. You can +build it with `./build-native.sh` and run it with `./build/native`. + ## 4. Don't block the main thread In `04_no_block` we avoid our calculations blocking the main browser