Created
May 4, 2022 05:08
-
-
Save yskszk63/9af56cd968f5faeea2c59c6075821009 to your computer and use it in GitHub Desktop.
Returns struct
This file contains 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
typedef struct { | |
unsigned char data1; | |
unsigned char data2; | |
} MyResult; | |
MyResult myfunc(unsigned char data1) { | |
MyResult result; | |
result.data1 = data1; | |
result.data2 = 22; | |
return result; | |
} |
This file contains 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
CC = /opt/wasi-sdk/bin/clang | |
CFLAGS = | |
LDFLAGS = -mexec-model=reactor | |
LDFLAGS += -Wl,--export-all | |
prog.wasm: lib.c Makefile | |
$(CC) $(LDFLAGS) -o $@ $< |
This file contains 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 fs from "fs/promises"; | |
const mod = await WebAssembly.compile(await fs.readFile(new URL("./prog.wasm", import.meta.url))); | |
const instance = await WebAssembly.instantiate(mod, {}); | |
const { myfunc, memory } = instance.exports; | |
console.log(new Uint8Array(memory.buffer, 65535, 2)); // Uint8Array(2) [ 0, 0 ] | |
myfunc(65535, 32); | |
console.log(new Uint8Array(memory.buffer, 65535, 2)); // Uint8Array(2) [ 32, 22 ] | |
console.log(new Uint8Array(memory.buffer, 66000, 2)); // Uint8Array(2) [ 0, 0 ] | |
myfunc(66000, 64); | |
console.log(new Uint8Array(memory.buffer, 66000, 2)); // Uint8Array(2) [ 64, 22 ] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment