Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/twiddlingbits/twr-wasm
twr-wasm: easily run C/C++ code in a web browser using Web Assembly, with examples
https://github.com/twiddlingbits/twr-wasm
c cpp crt emscripten libcxx wasm web-development webassembly webassembly-cpp webassembly-demo webassembly-examples webassembly-tutorial
Last synced: about 11 hours ago
JSON representation
twr-wasm: easily run C/C++ code in a web browser using Web Assembly, with examples
- Host: GitHub
- URL: https://github.com/twiddlingbits/twr-wasm
- Owner: twiddlingbits
- License: mit
- Created: 2024-02-03T14:07:36.000Z (10 months ago)
- Default Branch: main
- Last Pushed: 2024-09-17T15:39:29.000Z (about 2 months ago)
- Last Synced: 2024-09-18T07:31:15.821Z (about 2 months ago)
- Topics: c, cpp, crt, emscripten, libcxx, wasm, web-development, webassembly, webassembly-cpp, webassembly-demo, webassembly-examples, webassembly-tutorial
- Language: C++
- Homepage: https://twiddlingbits.dev/
- Size: 36.6 MB
- Stars: 13
- Watchers: 4
- Forks: 1
- Open Issues: 8
-
Metadata Files:
- Readme: readme.md
- License: LICENSE
Awesome Lists containing this project
- Awesome-WebAssembly-Applications - [twr-wasm
README
# Easier C/C++ WebAssembly
**Version 2.5.0**twr-wasm is a simple, lightweight and easy to use library for building C/C++ WebAssembly code directly with clang. Run C/C++ code in a web browser. Legacy code, libraries, full applications, or single functions can be integrated with JavaScript and TypeScript. twr-wam solves some common use cases with less work than the more feature rich emscripten.
**Key Features:**
- build `.wasm` modules using C/C++ using clang directly (no wrapper)
- from JavaScript load `.wasm` modules, call C/C++ functions, and access wasm memory
- comprehensive console support for `stdin`, `stdio`, and `stderr`.
- in C/C++, print and get characters to/from `` tags in your HTML page
- in C/C++, print and get characters to/from a `` based "terminal"
- localization support, UTF-8, and windows-1252 support
- from JavaScript, use `class twrWasmModuleAsync` to:
- integrate a C/C++ Read-Eval-Print Loop (REPL) with JavaScript
- integrate a C/C++ CLI or Shell with JavaScript
- In JavaScript `await` on blocking/synchronous C/C++ functions.
- 2D drawing API for C/C++ compatible with JavaScript Canvas
- audio playback APIs for C/C++
- create your own C/C++ APIs using TypeScript by extending `class twrLibrary`
- standard C library optimized for WebAssembly
- libc++ built for WebAssembly
- comprehensive examples and documentation
- TypeScript and JavaScript support## Live WebAssembly Examples and Source
| Name | View Live Link | Source Link |
| --------- | ------------ | ----------- |
| Bouncing Balls (C++) | [View bouncing balls](https://twiddlingbits.dev/examples/dist/balls/index.html) | [Source for balls](https://github.com/twiddlingbits/twr-wasm/tree/main/examples/balls) |
| Maze Gen/Solve (Win32 C Port) | [View live maze](https://twiddlingbits.dev/examples/dist/maze/index.html) | [Source for maze](https://github.com/twiddlingbits/twr-wasm/tree/main/examples/maze) |
| Input/Output with `` | [View square demo](https://twiddlingbits.dev/examples/dist/divcon/index.html) | [Source](https://github.com/twiddlingbits/twr-wasm/tree/main/examples/divcon) |
|I/O to terminal with ``|[View demo](https://twiddlingbits.dev/examples/dist/terminal/index.html) |[Source](https://github.com/twiddlingbits/twr-wasm/tree/main/examples/terminal) |
|CLI using libc++ and ``)| [View console](https://twiddlingbits.dev/examples/dist/tests-user/index.html) | [Source](https://github.com/twiddlingbits/twr-wasm/tree/main/examples/tests-user) |## Full Documentation
The full documentation can be [found here](https://twiddlingbits.dev/docsite/)## Installation
`npm install twr-wasm`.For details see https://twiddlingbits.dev/docsite/gettingstarted/installation/
## Hello World
Here is the simplest twr-wasm example.C code:
~~~
#includevoid hello() {
printf("hello world\n");
}
~~~index.html:
~~~Hello World
import {twrWasmModule} from "twr-wasm";
const mod = new twrWasmModule();
await mod.loadWasm("./helloworld.wasm");
await mod.callC(["hello"]);
~~~
## Simple \
i/o
I/O can be directed to or from a \or a \ tag. Here is a simple example using a \for stdio input and output.
~~~
#include
#include
#include "twr-crt.h"void stdio_div() {
char inbuf[64];
char *r;
int i;printf("Square Calculator\n");
while (1) {
printf("Enter an integer: ");
r=twr_mbgets(inbuf); // r is NULL if esc entered. Otherwise r == inbuf
if (r) {
i=atoi(inbuf);
printf("%d squared is %d\n\n",i,i*i);
}
else {
printf("\n");
}
}
}
~~~With an index.html like the following. This time we are using twrWasmModuleAsync which integrates blocking C code into Javascript. twrWasmModuleAsync can also be used to receive key input from a \
or \ tag.~~~
Loading...
import {twrWasmModuleAsync, twrConsoleDiv} from "twr-wasm";const con = new twrConsoleDiv(document.getElementById("stdioDiv"));
const amod = new twrWasmModuleAsync({stdio: con});// remove 'Loading...'
document.getElementById("stdioDiv").innerHTML ="<br>";
// send key events to twrConsoleDiv
document.getElementById("stdioDiv").addEventListener("keydown",(ev)=>{con.keyDown(ev)});await amod.loadWasm("./divcon.wasm");
await amod.callC(["stdio_div"]);
~~~
## Full Documentation
The full documentation can be [found here](https://twiddlingbits.dev/)