Skip to content

Instantly share code, notes, and snippets.

@yskszk63
Created May 4, 2022 05:08
Show Gist options
  • Save yskszk63/9af56cd968f5faeea2c59c6075821009 to your computer and use it in GitHub Desktop.
Save yskszk63/9af56cd968f5faeea2c59c6075821009 to your computer and use it in GitHub Desktop.
Returns struct
typedef struct {
unsigned char data1;
unsigned char data2;
} MyResult;
MyResult myfunc(unsigned char data1) {
MyResult result;
result.data1 = data1;
result.data2 = 22;
return result;
}
CC = /opt/wasi-sdk/bin/clang
CFLAGS =
LDFLAGS = -mexec-model=reactor
LDFLAGS += -Wl,--export-all
prog.wasm: lib.c Makefile
$(CC) $(LDFLAGS) -o $@ $<
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