{"id":13720897,"url":"https://github.com/twiddlingbits/twr-wasm","last_synced_at":"2025-04-19T20:41:39.738Z","repository":{"id":222044852,"uuid":"752260007","full_name":"twiddlingbits/twr-wasm","owner":"twiddlingbits","description":"twr-wasm: easily run C/C++ code in a web browser using Web Assembly, with examples","archived":false,"fork":false,"pushed_at":"2025-03-01T19:01:37.000Z","size":39313,"stargazers_count":33,"open_issues_count":23,"forks_count":3,"subscribers_count":4,"default_branch":"main","last_synced_at":"2025-03-29T13:11:18.215Z","etag":null,"topics":["c","cpp","crt","emscripten","libcxx","wasm","web-development","webassembly","webassembly-cpp","webassembly-demo","webassembly-examples","webassembly-tutorial"],"latest_commit_sha":null,"homepage":"https://twiddlingbits.dev/","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/twiddlingbits.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-02-03T14:07:36.000Z","updated_at":"2025-03-24T07:18:39.000Z","dependencies_parsed_at":"2024-05-06T04:30:01.331Z","dependency_job_id":"a44c732e-bc4b-46ea-8f7d-dbe85f010eaf","html_url":"https://github.com/twiddlingbits/twr-wasm","commit_stats":null,"previous_names":["twiddlingbits/tiny-wasm-runtime","twiddlingbits/twr-wasm"],"tags_count":22,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/twiddlingbits%2Ftwr-wasm","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/twiddlingbits%2Ftwr-wasm/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/twiddlingbits%2Ftwr-wasm/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/twiddlingbits%2Ftwr-wasm/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/twiddlingbits","download_url":"https://codeload.github.com/twiddlingbits/twr-wasm/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249795203,"owners_count":21326777,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["c","cpp","crt","emscripten","libcxx","wasm","web-development","webassembly","webassembly-cpp","webassembly-demo","webassembly-examples","webassembly-tutorial"],"created_at":"2024-08-03T01:01:09.552Z","updated_at":"2025-04-19T20:41:39.703Z","avatar_url":"https://github.com/twiddlingbits.png","language":"C++","readme":"# Easier C/C++ WebAssembly\n**Version 2.5.0**\n\ntwr-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. \n\n**Key Features:**\n\n- build `.wasm` modules using C/C++ using clang directly (no wrapper)\n- from JavaScript load `.wasm` modules, call C/C++ functions, and access wasm memory\n- comprehensive console support for `stdin`, `stdio`, and `stderr`.\n  - in C/C++, print and get characters to/from `\u003cdiv\u003e` tags in your HTML page\n  - in C/C++, print and get characters to/from a `\u003ccanvas\u003e` based \"terminal\"\n  - localization support, UTF-8, and windows-1252 support\n- from JavaScript, use `class twrWasmModuleAsync` to:\n  - integrate a C/C++ Read-Eval-Print Loop (REPL) with JavaScript\n  - integrate a C/C++ CLI or Shell with JavaScript\n  - In JavaScript `await` on blocking/synchronous C/C++ functions. \n- 2D drawing API for C/C++ compatible with JavaScript Canvas\n- audio playback APIs for C/C++\n- create your own C/C++ APIs using TypeScript by extending `class twrLibrary`\n- standard C library optimized for WebAssembly\n- libc++ built for WebAssembly\n- comprehensive examples and documentation\n- TypeScript and JavaScript support\n\n## Live WebAssembly Examples and Source\n\n| Name | View Live Link | Source Link |\n| --------- | ------------ | ----------- |\n| 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) |\n| 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) |\n| Input/Output with `\u003cdiv\u003e` | [View square demo](https://twiddlingbits.dev/examples/dist/divcon/index.html) | [Source](https://github.com/twiddlingbits/twr-wasm/tree/main/examples/divcon) |\n|I/O to terminal with `\u003ccanvas\u003e`|[View demo](https://twiddlingbits.dev/examples/dist/terminal/index.html) |[Source](https://github.com/twiddlingbits/twr-wasm/tree/main/examples/terminal) |\n|CLI using libc++ and `\u003ccanvas\u003e`)| [View console](https://twiddlingbits.dev/examples/dist/tests-user/index.html) | [Source](https://github.com/twiddlingbits/twr-wasm/tree/main/examples/tests-user) |\n\n## Full Documentation\nThe full documentation can be [found here](https://twiddlingbits.dev/docsite/)\n\n## Installation\n`npm install twr-wasm`. \n\nFor details see https://twiddlingbits.dev/docsite/gettingstarted/installation/\n\n## Hello World\nHere is the simplest twr-wasm example.\n\nC code:\n\n~~~\n#include \u003cstdio.h\u003e\n\nvoid hello() {\n   printf(\"hello world\\n\");\n}\n~~~\n\nindex.html:\n~~~\n\u003chead\u003e\n   \u003ctitle\u003eHello World\u003c/title\u003e\n\u003c/head\u003e\n\u003cbody\u003e\n   \u003cdiv id=\"twr_iodiv\"\u003e\u003c/div\u003e\n\n   \u003cscript type=\"module\"\u003e\n      import {twrWasmModule} from \"twr-wasm\";\n      \n      const mod = new twrWasmModule();\n      await mod.loadWasm(\"./helloworld.wasm\");\n      await mod.callC([\"hello\"]);\n   \u003c/script\u003e\n\u003c/body\u003e\n~~~\n\n\n\n## Simple \\\u003cdiv\u003e i/o\nI/O can be directed to or from a \\\u003cdiv\u003e or a \\\u003ccanvas\u003e tag.  Here is a simple example using a \\\u003cdiv\u003e for stdio input and output.\n\n \u003cbr\u003e\n\n~~~\n#include \u003cstdio.h\u003e\n#include \u003cstdlib.h\u003e\n#include \"twr-crt.h\"\n\nvoid stdio_div() {\n   char inbuf[64];\n   char *r;\n   int i;\n\n   printf(\"Square Calculator\\n\");\n\n   while (1) {\n      printf(\"Enter an integer: \");\n      r=twr_mbgets(inbuf);  // r is NULL if esc entered.  Otherwise r == inbuf\n      if (r) {  \n         i=atoi(inbuf);\n         printf(\"%d squared is %d\\n\\n\",i,i*i);\n      }\n      else {\n         printf(\"\\n\");\n      }\n   }\n}\n~~~\n\nWith 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 \\\u003cdiv\u003e or \\\u003ccanvas\u003e tag. \n\n~~~\n\u003cbody\u003e\n   \u003cdiv id=\"stdioDiv\" \n        tabindex=\"0\" \n        style=\"color: DarkGreen; background-color: LightGray; font-size: 18px;font-family: Arial, sans-serif;\" \u003e\n        Loading... \u003cbr\u003e\n   \u003c/div\u003e\n\n   \u003cscript type=\"module\"\u003e\n      import {twrWasmModuleAsync, twrConsoleDiv} from \"twr-wasm\";\n\n      const con = new twrConsoleDiv(document.getElementById(\"stdioDiv\"));\n      const amod = new twrWasmModuleAsync({stdio: con});\n\n      // remove 'Loading...'\n      document.getElementById(\"stdioDiv\").innerHTML =\"\u003cbr\u003e\"; \n      // send key events to twrConsoleDiv\n      document.getElementById(\"stdioDiv\").addEventListener(\"keydown\",(ev)=\u003e{con.keyDown(ev)});\n\n      await amod.loadWasm(\"./divcon.wasm\");\n      await amod.callC([\"stdio_div\"]);\n\n   \u003c/script\u003e\n\u003c/body\u003e\n~~~\n\n## Full Documentation\nThe full documentation can be [found here](https://twiddlingbits.dev/)\n","funding_links":[],"categories":["C++","Inside the browser"],"sub_categories":["Frontend Web App"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftwiddlingbits%2Ftwr-wasm","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftwiddlingbits%2Ftwr-wasm","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftwiddlingbits%2Ftwr-wasm/lists"}