Last active
November 21, 2025 00:10
-
-
Save mochienya/1e7f3b67e22690cb3cc45320017e21c1 to your computer and use it in GitHub Desktop.
js string from wasm
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { readFile } from 'node:fs/promises' | |
| const wasm = await WebAssembly.instantiate(await readFile('main.wasm'), {}, { builtins: [ "js-string" ] }) | |
| .then(n => n.instance.exports) | |
| console.log(wasm.hello()) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (module | |
| ;; type definition needed, incorrect signature error if you do `arrayref` inline | |
| (type $str_arr_t (array (mut i16))) | |
| (import "wasm:js-string" "fromCharCodeArray" ( | |
| ;; `externref` gets a signature error because it's an alias for `(ref null extern)` | |
| func $js_str (param (ref null $str_arr_t) i32 i32) (result (ref extern)) | |
| )) | |
| ;; "hello from wasm!" in utf-16 | |
| (data $str "h\00e\00l\00l\00o\00 \00f\00r\00o\00m\00 \00w\00a\00s\00m\00!\00\00\00") | |
| (func (export "hello") (result (ref extern)) | |
| ;; push a new array onto the stack of type `str_arr_t` with the data `$str`, offset of 0, length of 16 (bytes get calculated automatically) | |
| (array.new_data $str_arr_t $str (i32.const 0) (i32.const 16)) | |
| (call $js_str (i32.const 0) (i32.const 16)) | |
| ) | |
| ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment