Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add deno example with wasm-gc #183

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions examples/deno/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Deno with MoonBit wasm gc

deno version of wasm-gc example

## Deps

This example works with the following versions of the environment.

```bash
moon 0.1.0 (ee9885e 2024-04-15)
moonc 3b02f89 2024-04-15
deno 1.42.4
```

## How to run

```bash
moon build --target wasm-gc
deno run --allow-read run.ts
```
3 changes: 3 additions & 0 deletions examples/deno/main/main.mbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main {
println("hello")
}
4 changes: 4 additions & 0 deletions examples/deno/main/moon.pkg.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"is_main": true,
"import": {}
}
3 changes: 3 additions & 0 deletions examples/deno/moon.mod.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "hello"
}
41 changes: 41 additions & 0 deletions examples/deno/run.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
let memory: WebAssembly.Memory;
const [log, flush] = (() => {
let buffer: number[] = [];
function flush() {
if (buffer.length > 0) {
console.log(new TextDecoder("utf-16").decode(new Uint16Array(buffer).valueOf()));
buffer = [];
}
}
function log(ch: number) {
if (ch == '\n'.charCodeAt(0)) { flush(); }
else if (ch == '\r'.charCodeAt(0)) { /* noop */ }
else { buffer.push(ch); }
}
return [log, flush]
})();

const importObject = {
spectest: {
print_char: log
},
js_string: {
new: (offset: number, length: number) => {
const bytes = new Uint16Array(memory.buffer, offset, length);
const string = new TextDecoder("utf-16").decode(bytes);
return string
},
empty: () => { return "" },
log: (string: string) => { console.log(string) },
append: (s1: string, s2: string) => { return (s1 + s2) },
}
};

const { instance } = await WebAssembly.instantiateStreaming(
fetch(new URL("./target/wasm-gc/release/build/main/main.wasm", import.meta.url)),
importObject
);
memory = instance.exports["moonbit.memory"] as WebAssembly.Memory;
// @ts-expect-error
instance.exports._start();
flush();
14 changes: 14 additions & 0 deletions examples/deno/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
// just skip the type checking
"compilerOptions": {
"target": "esnext",
"module": "esnext",
"moduleResolution": "Bundler",
"strict": true,
"noEmit": true,
"allowImportingTsExtensions": true,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true
}
}