Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
bokuweb committed Sep 23, 2024
1 parent dfdaa40 commit c8ab836
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 24 deletions.
8 changes: 4 additions & 4 deletions js/entry.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import { readFile } from 'node:fs/promises';
import fs from 'node:fs';
import { WASI } from '@tybys/wasm-util';
import { WASI, type IFs } from '@tybys/wasm-util';
import { env } from 'node:process';
import { join } from 'node:path';
import { parentPort, workerData } from 'node:worker_threads';

const wasi = new WASI({
version: 'preview1',
args: workerData.argv,
env,
env: env as Record<string, string>,
returnOnExit: true,
preopens: { './': './' },
fs,
fs: fs as IFs,
});

const imports = wasi.getImportObject();
Expand All @@ -25,7 +25,7 @@ const file = readFile(join(__dirname, './reg.wasm'));
let instance = await WebAssembly.instantiate(wasm, {
...imports,
wasi: {
'thread-spawn': (startArg) => {
'thread-spawn': (startArg: number) => {
const threadIdBuffer = new SharedArrayBuffer(4);
const id = new Int32Array(threadIdBuffer);
Atomics.store(id, 0, -1);
Expand Down
26 changes: 6 additions & 20 deletions js/worker.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
import { parentPort } from 'node:worker_threads';
import { readFile } from 'node:fs/promises';
import fs from 'node:fs';
import { WASI } from '@tybys/wasm-util';
import { WASI, type IFs } from '@tybys/wasm-util';
import { argv, env } from 'node:process';
import { join } from 'node:path';

const wasi = new WASI({
version: 'preview1',
args: argv,
env,
env: env as Record<string, string>,
returnOnExit: true,
preopens: { './': './' },
fs,
fs: fs as IFs,
});

const imports = wasi.getImportObject();
const file = readFile(join(__dirname, './reg.wasm'));

const handler = async ({ startArg, tid, memory }) => {
const handler = async ({ startArg, tid, memory }: { startArg: number, tid: number, memory: number }) => {
try {
const wasm = await WebAssembly.compile(await file);
let instance = await WebAssembly.instantiate(wasm, {
...imports,
wasi: {
'thread-spawn': (startArg) => {
'thread-spawn': (startArg: number) => {
const threadIdBuffer = new SharedArrayBuffer(4);
const id = new Int32Array(threadIdBuffer);
Atomics.store(id, 0, -1);
Expand All @@ -39,21 +39,7 @@ const handler = async ({ startArg, tid, memory }) => {
const { createInstanceProxy } = require('./proxy.js');
instance = createInstanceProxy(instance, memory);
wasi.start(instance);
try {
const symbols = Object.getOwnPropertySymbols(wasi);
const selectDescription = (description) => (s) => {
if (s.description) {
return s.description === description;
}
return s.toString() === `Symbol(${description})`;
};
if (Array.isArray(description)) {
return description.map((d) => symbols.filter(selectDescription(d))[0]);
}
const kStarted = symbols.filter(selectDescription('kStarted'))[0];
wasi[kStarted] = false;
} catch (_) {}
console.log(tid);
// @ts-expect-error wasi_thread_start not defined
instance.exports.wasi_thread_start(tid, startArg);
} catch (e) {
throw e;
Expand Down

0 comments on commit c8ab836

Please sign in to comment.