-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e2dd165
commit 7b2c976
Showing
6 changed files
with
135 additions
and
145 deletions.
There are no files selected for viewing
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
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
This file was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/// <reference lib="deno.ns" /> | ||
import { assert, fail, assertEquals } from "https://deno.land/[email protected]/assert/mod.ts"; | ||
import { Module, Instruction, Type } from "../../source/wasm/index.ts"; | ||
|
||
const decoder = new TextDecoder(); | ||
|
||
const goalText = "Hello, World!" + Math.floor(Math.random() * 9); | ||
|
||
Deno.test(`Wasm module test: should print "${goalText}"`, async () => { | ||
let mod = new Module(); | ||
const mem = mod.addMemory(1); | ||
mod.exportMemory("memory", mem); | ||
|
||
const type0 = mod.makeType([Type.Intrinsic.i32], [Type.Intrinsic.i32]); | ||
const type1 = mod.makeType([Type.Intrinsic.i32, Type.Intrinsic.i32, Type.Intrinsic.i32, Type.Intrinsic.i32], [Type.Intrinsic.i32]); | ||
|
||
const fd_write = mod.importFunction("wasi_snapshot_preview1", "fd_write", type1); | ||
|
||
mod.setData(0, goalText); | ||
// The WASI iovec struct, which consists of a pointer to | ||
// the data and the length of the data. | ||
// This starts at address 100, to leave some room after the string data. | ||
mod.setData(16, "\x00\x00\x00\x00\x0e\x00\x00\x00"); // Set pointer to 0 and length to 14 | ||
|
||
const main = mod.makeFunction([], []); | ||
main.code.push(Instruction.const.i32(1)); // File descriptor for stdout | ||
main.code.push(Instruction.const.i32(16)); // iovec array | ||
main.code.push(Instruction.const.i32(1)); // number of iovec structs | ||
main.code.push(Instruction.const.i32(0)); // address to store number of bytes written (ignoring it here) | ||
main.code.push(Instruction.call(fd_write)); | ||
main.code.push(Instruction.drop()); | ||
|
||
const extra = mod.makeFunction([Type.Intrinsic.i32], [Type.Intrinsic.i32]); | ||
extra.code.push(Instruction.local.get(0)); | ||
extra.code.push(Instruction.return()); | ||
|
||
mod.exportFunction("_start", main.ref); | ||
|
||
let stdout = ""; | ||
|
||
let memory: WebAssembly.Memory; | ||
|
||
const imports = { | ||
wasi_snapshot_preview1: { | ||
fd_write: (fd: number, iovs: number, iovs_len: number, n_written: number) => { | ||
const memoryArray = new Int32Array(memory.buffer); | ||
const byteArray = new Uint8Array(memory.buffer); | ||
for (let iovIdx = 0; iovIdx < iovs_len; iovIdx++) { | ||
const bufPtr = memoryArray.at(iovs/4 + iovIdx*2) || 0; | ||
const bufLen = memoryArray.at(iovs/4 + iovIdx*2 + 1) || 0; | ||
const data = decoder.decode(byteArray.slice(bufPtr, bufPtr + bufLen)); | ||
stdout += data; | ||
} | ||
return 0; // Return 0 to indicate success | ||
} | ||
} | ||
}; | ||
|
||
// Load the wasm module | ||
const wasmModule = new WebAssembly.Module(mod.toBinary()); | ||
|
||
try { | ||
// Instantiate the wasm module | ||
const instance = await WebAssembly.instantiate(wasmModule, imports); | ||
|
||
const exports = instance.exports; | ||
memory = exports.memory as WebAssembly.Memory; | ||
|
||
// Call the _start function | ||
if (typeof exports._start === "function") { | ||
(exports._start as Function)(); | ||
} else { | ||
fail(`Expected _start to be a function`); | ||
} | ||
|
||
// Check stdout | ||
assertEquals(stdout, goalText); | ||
|
||
} catch (err) { | ||
// If there's an error, the test will fail | ||
fail(`Failed to run wasm module: ${err}`); | ||
} | ||
|
||
}); |
This file was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/// <reference lib="deno.ns" /> | ||
import { assertEquals, assertThrows } from "https://deno.land/[email protected]/assert/mod.ts"; | ||
import { EncodeSignedLEB, EncodeUnsignedLEB } from "../../source/wasm/type.ts"; | ||
|
||
function toHex(arr: number[]): string { | ||
return arr.map(x => x.toString(16).padStart(2, "0")).join(""); | ||
} | ||
|
||
Deno.test("EncodeSignedLEB: zero encoding", () => { | ||
assertEquals(toHex(EncodeSignedLEB(0)), "00"); | ||
}); | ||
|
||
Deno.test("EncodeSignedLEB: small positive integer", () => { | ||
assertEquals(toHex(EncodeSignedLEB(8)), "08"); | ||
assertEquals(toHex(EncodeSignedLEB(10)), "0a"); | ||
assertEquals(toHex(EncodeSignedLEB(100)), "e400"); | ||
assertEquals(toHex(EncodeSignedLEB(123456)), "c0c407"); | ||
assertEquals(toHex(EncodeSignedLEB(2141192192)), "808080fd07"); | ||
}); | ||
|
||
Deno.test("EncodeSignedLEB: small negative integer", () => { | ||
assertEquals(toHex(EncodeSignedLEB(-100)), "9c7f"); | ||
}); | ||
|
||
Deno.test("EncodeSignedLEB: should throw an error for non-integers", () => { | ||
assertThrows(() => EncodeSignedLEB(12.34)); | ||
}); | ||
|
||
Deno.test("EncodeUnsignedLEB: zero encoding", () => { | ||
assertEquals(toHex(EncodeUnsignedLEB(0)), "00"); | ||
}); | ||
|
||
Deno.test("EncodeUnsignedLEB: small integer", () => { | ||
assertEquals(toHex(EncodeUnsignedLEB(1)), "01"); | ||
assertEquals(toHex(EncodeUnsignedLEB(8)), "08"); | ||
assertEquals(toHex(EncodeUnsignedLEB(127)), "7f"); | ||
assertEquals(toHex(EncodeUnsignedLEB(128)), "8001"); | ||
assertEquals(toHex(EncodeUnsignedLEB(255)), "ff01"); | ||
assertEquals(toHex(EncodeUnsignedLEB(256)), "8002"); | ||
assertEquals(toHex(EncodeUnsignedLEB(624485)), "e58e26"); | ||
}); | ||
|
||
Deno.test("EncodeUnsignedLEB: should throw an error for non-integers", () => { | ||
assertThrows(() => EncodeUnsignedLEB(12.34)); | ||
}); | ||
|
||
Deno.test("EncodeUnsignedLEB: should throw an error for negative integers", () => { | ||
assertThrows(() => EncodeUnsignedLEB(-12)); | ||
}); |