Skip to content

Commit

Permalink
Merge pull request #11 from joshLong145/ref/instance-wrappers-consoli…
Browse files Browse the repository at this point in the history
…dation

Ref/instance wrappers consolidation
  • Loading branch information
joshLong145 authored Mar 13, 2024
2 parents 01f5d60 + 9b5224f commit 5c8de47
Show file tree
Hide file tree
Showing 17 changed files with 261 additions and 412 deletions.
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,11 +172,10 @@ Below we provide the go WASM runtime as an `addon` and give a callback for
loading the module at the given file path.

```javascript
import { WasmInstanceWrapper, WasmWorkerDefinition } from "./../../src/mod.ts";
class Example extends WasmWorkerDefinition {
class Example extends WorkerDefinition {

public constructor(modulePath: string) {
super(modulePath);
public constructor() {
super();
}

addOne = (buffer: SharedArrayBuffer, module: any) => {
Expand All @@ -188,12 +187,13 @@ class Example extends WasmWorkerDefinition {
}
}

const example: Example = new Example("./examples/wasm/primes-2.wasm");
const example: Example = new Example();

const wrapper: WasmInstanceWrapper<Example> = new WasmInstanceWrapper<Example>(example, {
const wrapper: InstanceWrapper<Example> = new InstanceWrapper<Example>(example, {
addons: [
"./lib/wasm_exec_tiny.js",
],
modulePath: "./examples/wasm/primes-2.wasm",
addonLoader: (path: string) => {
return Deno.readTextFileSync(path);
},
Expand All @@ -204,7 +204,7 @@ const wrapper: WasmInstanceWrapper<Example> = new WasmInstanceWrapper<Example>(e
});

wrapper.start();
//@ts-ignore

await example.execute("addOne").then((buf: SharedArrayBuffer) => {
console.log("buffer returned ", new Int32Array(buf))
});
Expand Down
23 changes: 9 additions & 14 deletions benchmark/wasm_instance_start_bench.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
//@ts-nocheck

import {
WasmInstanceWrapper,
WasmWorkerDefinition,
} from "../src/WasmInstanceWrapper.ts";
import { InstanceWrapper, WorkerDefinition } from "../src/mod.ts";

class TestExample extends WasmWorkerDefinition {
class TestExample extends WorkerDefinition {
public constructor(modulePath: string) {
super(modulePath);
}
Expand All @@ -23,11 +20,9 @@ class TestExample extends WasmWorkerDefinition {
}

Deno.bench("Wasm Worker Start Go Module loading", async (_b) => {
const example: WasmWorkerDefinition = new TestExample(
"./examples/wasm/tiny-go/primes-2.wasm",
);
const example: WorkerDefinition = new TestExample();

const wrapper: WasmInstanceWrapper<TestExample> = new WasmInstanceWrapper<
const wrapper: InstanceWrapper<TestExample> = new InstanceWrapper<
Example
>(
example,
Expand All @@ -37,12 +32,13 @@ Deno.bench("Wasm Worker Start Go Module loading", async (_b) => {
addons: [
"./lib/wasm_exec_tiny.js",
],
modulePath: "./examples/wasm/tiny-go/primes-2.wasm",
addonLoader: (path: string) => {
return Deno.readTextFileSync(path);
},
moduleLoader: (path: string) => {
const fd = Deno.openSync(path);
const mod = Deno.readFileSync(fd);
const mod = Deno.readAllSync(fd);
fd.close();
return mod;
},
Expand All @@ -55,11 +51,9 @@ Deno.bench("Wasm Worker Start Go Module loading", async (_b) => {
});

Deno.bench("Wasm Worker Start Rust Module loading", async (_b) => {
const example: WasmWorkerDefinition = new TestExample(
"./examples/wasm/rust/wasm_test_bg.wasm",
);
const example: WorkerDefinition = new TestExample();

const wrapper: WasmInstanceWrapper<TestExample> = new WasmInstanceWrapper<
const wrapper: InstanceWrapper<TestExample> = new InstanceWrapper<
Example
>(
example,
Expand All @@ -69,6 +63,7 @@ Deno.bench("Wasm Worker Start Rust Module loading", async (_b) => {
addons: [
"./lib/wasm_test.js",
],
modulePath: "./examples/wasm/rust/wasm_test_bg.wasm",
addonLoader: (path: string) => {
return Deno.readTextFileSync(path);
},
Expand Down
8 changes: 6 additions & 2 deletions deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
"gen-npm": "deno run -A --no-check scripts/gen-npm.ts"
},
"exclude": [
"./examples/*.ts"
]
"./examples/*.ts",
"./lib/*.js"
],
"name": "@joshlong145/span",
"version": "0.5.0",
"exports": "./src/mod.ts"
}
2 changes: 1 addition & 1 deletion examples/js/example.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ const wrapper: InstanceWrapper<Example> = new InstanceWrapper<Example>(

wrapper.start();

(example as any).worker.onerror = (event: any) => {
example.worker.onerror = (event: any) => {
console.log("an error occured ", event);
};

Expand Down
21 changes: 10 additions & 11 deletions examples/wasm/rust/example.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import {
WasmInstanceWrapper,
WasmWorkerDefinition,
} from "./../../../src/WasmInstanceWrapper.ts";
InstanceWrapper,
WorkerDefinition,
} from "../../../src/InstanceWrapper.ts";

class Example extends WasmWorkerDefinition {
public constructor(modulePath: string) {
super(modulePath);
class Example extends WorkerDefinition {
public constructor() {
super();
}

test2 = (buffer: SharedArrayBuffer, args: Record<string, any>) => {
test2 = (buffer: SharedArrayBuffer, _args: Record<string, any>) => {
let arr = new Int8Array(buffer);
arr[0] += 1;
//@ts-ignore
Expand All @@ -17,11 +17,9 @@ class Example extends WasmWorkerDefinition {
};
}

const example: Example = new Example(
"./examples/wasm/rust/wasm_test_bg.wasm",
);
const example: Example = new Example();

const wrapper: WasmInstanceWrapper<Example> = new WasmInstanceWrapper<Example>(
const wrapper: InstanceWrapper<Example> = new InstanceWrapper<Example>(
example,
{
addons: [
Expand All @@ -30,6 +28,7 @@ const wrapper: WasmInstanceWrapper<Example> = new WasmInstanceWrapper<Example>(
addonLoader: (path: string) => {
return Deno.readTextFileSync(path);
},
modulePath: "./examples/wasm/rust/wasm_test_bg.wasm",
moduleLoader: (path: string) => {
const fd = Deno.openSync(path);
return Deno.readAllSync(fd);
Expand Down
45 changes: 24 additions & 21 deletions examples/wasm/tiny-go/example.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,30 @@
import {
WasmInstanceWrapper,
WasmWorkerDefinition,
} from "./../../../src/WasmInstanceWrapper.ts";
InstanceWrapper,
WorkerDefinition,
} from "../../../src/InstanceWrapper.ts";

class Example extends WasmWorkerDefinition {
public constructor(modulePath: string) {
super(modulePath);
class Example extends WorkerDefinition {
public constructor() {
super();
}

public test2(buffer: SharedArrayBuffer, args: Record<string, any>) {
let arr = new Int8Array(buffer);
test2 = (
buffer: SharedArrayBuffer,
_args: Record<string, any>,
): SharedArrayBuffer => {
const arr = new Int8Array(buffer);
arr[0] += 1;
//@ts-ignore
self.primeGenerator();
return arr.buffer;
}
return arr.buffer as SharedArrayBuffer;
};

public test1(
test1 = (
buffer: SharedArrayBuffer,
args: Record<string, any>,
): SharedArrayBuffer {
let arr = new Int32Array(buffer);
let myString = "A rather long string of English text, an error message \
_args: Record<string, any>,
): SharedArrayBuffer => {
const arr = new Int32Array(buffer);
const myString = "A rather long string of English text, an error message \
actually that just keeps going and going -- an error \
message to make the Energizer bunny blush (right through \
those Schwarzenegger shades)! Where was I? Oh yes, \
Expand All @@ -37,21 +40,20 @@ class Example extends WasmWorkerDefinition {
arr[index] = hash;
}
return buffer;
}
};
}

const example: Example = new Example(
"./examples/wasm/tiny-go/primes-2.wasm",
);
const example: Example = new Example();

const wrapper: WasmInstanceWrapper<Example> = new WasmInstanceWrapper<Example>(
const wrapper: InstanceWrapper<Example> = new InstanceWrapper<Example>(
example,
{
outputPath: "output",
namespace: "asd",
addons: [
"./lib/wasm_exec_tiny.js",
],
modulePath: "./examples/wasm/tiny-go/primes-2.wasm",
addonLoader: (path: string) => {
return Deno.readTextFileSync(path);
},
Expand Down Expand Up @@ -81,7 +83,8 @@ await example.execute("test2").then((buf: SharedArrayBuffer) => {

example.terminateWorker();

wrapper.restart();
await wrapper.restart();
console.log("restarting web worker");

await example.execute("test1").then((buf: SharedArrayBuffer) => {
console.log("hello", new Int32Array(buf));
Expand Down
Loading

0 comments on commit 5c8de47

Please sign in to comment.