Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
bokuweb committed Sep 29, 2024
1 parent c10d750 commit 15bbe8d
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 13 deletions.
5 changes: 2 additions & 3 deletions js/entry.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { readFile } from 'node:fs/promises';
import fs from 'node:fs';
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';
import { readWasm } from './utils';

export type CompareOutput = {
failedItems: string[],
Expand All @@ -28,7 +27,7 @@ const wasi = new WASI({
});

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

(async () => {
try {
Expand Down
5 changes: 3 additions & 2 deletions js/index.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import EventEmitter from 'node:events';
import { Worker } from 'node:worker_threads';
import { resolveExtention } from './utils';

export const run = (argv: string[]): EventEmitter => {
const emitter = new EventEmitter();
const worker = new Worker('./entry.js', { workerData: { argv } });
const worker = new Worker(`./entry.${resolveExtention()}`, { workerData: { argv } });

let nextTid = 1;
const workers = [worker];

const spawn = (startArg: number, threadId: Int32Array, memory: WebAssembly.Memory) => {
const worker = new Worker('./worker.js');
const worker = new Worker(`./worker.${resolveExtention()}`);

workers.push(worker);

Expand Down
4 changes: 2 additions & 2 deletions js/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "reg-cli",
"version": "0.19.0-wasm0",
"version": "0.0.0-experimental-wasm0",
"description": "",
"type": "module",
"exports": {
Expand All @@ -17,7 +17,7 @@
"dist"
],
"scripts": {
"build": "unbuild"
"build": "unbuild && cp reg.wasm ./dist/shared/reg.wasm"
},
"keywords": [],
"author": "bokuweb",
Expand Down
17 changes: 17 additions & 0 deletions js/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import path from 'path';
import { fileURLToPath } from 'url';
import { readFile } from 'node:fs/promises';
import { join } from 'node:path';

const isESM = typeof import.meta !== 'undefined';

export const readWasm = () => {
const dir = isESM ? path.dirname(fileURLToPath(import.meta.url)) : __dirname;

const file = readFile(join(dir, './reg.wasm'));
return file;
};

export const resolveExtention = (): string => {
return isESM ? 'mjs' : 'cjs';
};
11 changes: 5 additions & 6 deletions js/worker.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { parentPort } from 'node:worker_threads';
import { readFile } from 'node:fs/promises';
import fs from 'node:fs';
import { WASI, type IFs } from '@tybys/wasm-util';
import { argv, env } from 'node:process';
import { join } from 'node:path';
import { readWasm, resolveExtention } from './utils';
// https://github.com/toyobayashi/emnapi/blob/5ab92c706c7cd4a0a30759e58f26eedfb0ded591/packages/wasi-threads/src/wasi-threads.ts#L288-L335
import { createInstanceProxy } from './proxy';

const wasi = new WASI({
version: 'preview1',
Expand All @@ -15,9 +16,9 @@ const wasi = new WASI({
});

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

const handler = async ({ startArg, tid, memory }: { startArg: number, tid: number, memory: number }) => {
const handler = async ({ startArg, tid, memory }: { startArg: number, tid: number, memory: WebAssembly.Memory }) => {
try {
const wasm = await WebAssembly.compile(await file);
let instance = await WebAssembly.instantiate(wasm, {
Expand All @@ -35,8 +36,6 @@ const handler = async ({ startArg, tid, memory }: { startArg: number, tid: numbe
},
env: { memory },
});
// https://github.com/toyobayashi/emnapi/blob/5ab92c706c7cd4a0a30759e58f26eedfb0ded591/packages/wasi-threads/src/wasi-threads.ts#L288-L335
const { createInstanceProxy } = require('./proxy.js');
instance = createInstanceProxy(instance, memory);
wasi.start(instance);
// @ts-expect-error wasi_thread_start not defined
Expand Down

0 comments on commit 15bbe8d

Please sign in to comment.