{"id":22384333,"url":"https://github.com/richardanaya/wasm-script","last_synced_at":"2025-07-31T04:32:12.865Z","repository":{"id":57671895,"uuid":"308775436","full_name":"richardanaya/wasm-script","owner":"richardanaya","description":"Compile WebAssembly in your HTML","archived":false,"fork":false,"pushed_at":"2020-12-08T03:26:11.000Z","size":399,"stargazers_count":30,"open_issues_count":1,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2024-11-24T23:12:30.678Z","etag":null,"topics":["compilers","webassembly"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","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/richardanaya.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}},"created_at":"2020-10-31T00:46:13.000Z","updated_at":"2023-06-13T18:22:05.000Z","dependencies_parsed_at":"2022-08-30T15:40:26.048Z","dependency_job_id":null,"html_url":"https://github.com/richardanaya/wasm-script","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/richardanaya%2Fwasm-script","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/richardanaya%2Fwasm-script/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/richardanaya%2Fwasm-script/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/richardanaya%2Fwasm-script/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/richardanaya","download_url":"https://codeload.github.com/richardanaya/wasm-script/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":228215014,"owners_count":17886348,"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":["compilers","webassembly"],"created_at":"2024-12-05T01:17:05.392Z","updated_at":"2024-12-05T01:17:06.008Z","avatar_url":"https://github.com/richardanaya.png","language":"JavaScript","readme":"# wasm-script\n\nThis is a library for bringing your WebAssembly compiler to the web.\n\n# Vision\n\n```html\n\u003cscript src=\"wasm-script.js\"\u003e\u003c/script\u003e\n\u003cwasm-script id=\"math\" lang=\"c\" compiler=\"http://yourcompiler.com/c.wasm\"\u003e\n     \n     extern int add(int a, int b){\n          return a + b;\n     }  \n\n\u003c/wasm-script\u003e\n\u003cscript type=\"module\"\u003e\n     // Compile as a module for accelerating javascript\n     const mathModule = await document.getElementById(\"math\").compile();\n     console.log(mathModule.add(2,2));\n\u003c/script\u003e\n```\n\nOr run stand alone\n\n```html\n\u003cscript src=\"wasm-script.min.js\"\u003e\u003c/script\u003e\n\u003cwasm-script lang=\"python\" src=\"tetris.python\" execute\u003e\u003c/wasm-script\u003e\n\u003c!-- use default compilers from the community for certain languages --\u003e\n```\n\n# Features\n\n- [x] script for loading a WebAssembly module from a compiler\n- [ ] ask to load dependent files by url\n- [ ] cacheing\n- [x] show compile errors in console\n\n# CDN \n\n```\nhttps://cdn.jsdelivr.net/gh/richardanaya/wasm-script@latest/wasm-script.js\n```\n\n# Reference Implementation\n\nA reference implementation compiler is in progress in Rust for the [`wasp`](https://github.com/wasplang/wasp) programming language ( a simple lisp-like syntax language ).\n\n```html\n\u003cscript src=\"wasm-script.js\"\u003e\u003c/script\u003e\n\u003cwasm-script id=\"math\" lang=\"wasp\" compiler=\"compilers/waspc/compiler.wasm\"\u003e\n     \n    pub fn secret(){\n        42\n    }\n\n\u003c/wasm-script\u003e\n\u003cscript\u003e\n     // top-level await doesn't exist yet, so we have to do it the lame way\n    (async function(){\n        const mathModule = await document.getElementById(\"math\").compile();\n        document.body.innerHTML = mathModule.secret(1);\n    })();\n\u003c/script\u003e\n```\n\nSee the demo [here](https://richardanaya.github.io/wasm-script/examples/demo.html)\n\nWhat the compiler is doing is fairly simple:\n\n```rust\nuse wasm_compiler::{process,log};\nuse wasp_core::{compiler, parser};\n\n#[no_mangle]\npub fn compile(code_ptr: usize) -\u003e usize {\n    process(code_ptr, |code| {\n        log(\"compiling the code below!!\");\n        log(\u0026code);\n        let app = parser::parse(code)?;\n        Ok(compiler::compile(app)?)\n    })\n}\n```\n\n# Exposing Browser Functionality\n\nUntil we have DOM exposure in WebAssembly, we still are in a position we need to provide our own exposures to the browser's functionality. This library has some helper functions\nfor common memory structures.\n\n\n```html\n\u003cscript src=\"../wasm-script.js\"\u003e\u003c/script\u003e\n\u003cwasm-script id=\"math\" lang=\"wasp\" compiler=\"../compilers/waspc/compiler.wasm\"\u003e\n     \n    extern console_log(message)\n    pub fn main(){\n        console_log(\"hello world!\")\n    }\n\n\u003c/wasm-script\u003e\n\u003cscript\u003e\n     // top-level await doesn't exist yet, so we have to do it the lame way\n    (async function(){\n        const mathModule = await document.getElementById(\"math\").compile({\n            console_log: function(message_start) {\n                const message = WasmScript.getCString(mathModule.memory,message_start)\n                document.body.innerHTML += message;\n              }\n        });\n        mathModule.main();\n    })();\n\u003c/script\u003e\n```\n\nSee the demo [here](https://richardanaya.github.io/wasm-script/examples/helloworld.html)\n\n## Memory Helper API\n\n* `WasmScript.getCString(mem,start)` - Returns a utf-8 string from position in memory that ends with a zero character.\n* `WasmScript.getUint8ArrayFromMemory(mem,start)` - Returns a Uint8Array from position in memory, starting with a uint32 count followed by the elements.\n\n# Building Your Own Compiler\n\nRight now the current contract for building a compiler is very simple and can be implemented in any language that compiles to WebAssembly:\n\n```rust\n// external function to print a long takes a zero-character ending C string\nextern void compiler_log(uint32 start_cstr)\n// external function to print a error log and throw an exception takes a zero-character ending\n// C string ( this function does not return )\nextern void compiler_error(uint32 start_cstr)\n\n// allocate some bytes of size\nuint32 malloc(uint32 size)\n// compile a code by passing in the start of the code in memory (created using malloc above). \n// and get back a list of bytes (the length as u32, followed by the data)\nuint32 compile(uint32 code_str)\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frichardanaya%2Fwasm-script","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frichardanaya%2Fwasm-script","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frichardanaya%2Fwasm-script/lists"}