Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
toyobayashi committed May 13, 2024
1 parent f33a75d commit c024ead
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 20 deletions.
1 change: 0 additions & 1 deletion packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ export type {
} from './load'

export type {
InstantiatePayload,
MessageHandlerOptions
} from './worker'

Expand Down
8 changes: 4 additions & 4 deletions packages/core/src/worker.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import {
ThreadMessageHandler,
type ThreadMessageHandlerOptions,
type InstantiatePayload
type LoadPayload
} from '@emnapi/wasi-threads'
import type { NapiModule } from './emnapi/index'
import type { InstantiatedSource } from './load'

export type { ThreadMessageHandlerOptions, InstantiatePayload }
export type { ThreadMessageHandlerOptions, LoadPayload }

/** @public */
export interface MessageHandlerOptions extends ThreadMessageHandlerOptions {
onLoad: (data: InstantiatePayload) => InstantiatedSource | PromiseLike<InstantiatedSource>
onLoad: (data: LoadPayload) => InstantiatedSource | PromiseLike<InstantiatedSource>
}

/** @public */
Expand All @@ -25,7 +25,7 @@ export class MessageHandler extends ThreadMessageHandler {
this.napiModule = undefined
}

public override instantiate (data: InstantiatePayload): InstantiatedSource | PromiseLike<InstantiatedSource> {
public override instantiate (data: LoadPayload): InstantiatedSource | PromiseLike<InstantiatedSource> {
const source = this.onLoad!(data) as InstantiatedSource | PromiseLike<InstantiatedSource>
const then = (source as PromiseLike<InstantiatedSource>).then
if (typeof then === 'function') {
Expand Down
5 changes: 4 additions & 1 deletion packages/wasi-threads/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export { ThreadManager } from './thread-manager'

export type {
WASIInstance,
StartResult,
WASIThreadsOptions,
MainThreadOptions,
ChildThreadOptions,
Expand All @@ -15,8 +16,10 @@ export type {
export { WASIThreads } from './wasi-threads'

export { ThreadMessageHandler } from './worker'
export type { InstantiatePayload, ThreadMessageHandlerOptions } from './worker'
export type { ThreadMessageHandlerOptions } from './worker'

export { createInstanceProxy } from './proxy'

export { isTrapError } from './util'

export type { LoadPayload } from './command'
7 changes: 7 additions & 0 deletions packages/wasi-threads/src/proxy.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
export const kIsProxy = Symbol('kIsProxy')

/** @public */
export function createInstanceProxy (
instance: WebAssembly.Instance,
memory?: WebAssembly.Memory | (() => WebAssembly.Memory)
): WebAssembly.Instance {
if ((instance as any)[kIsProxy]) return instance

// https://github.com/nodejs/help/issues/4102
const originalExports = instance.exports
const createHandler = function (target: WebAssembly.Exports): ProxyHandler<WebAssembly.Exports> {
Expand Down Expand Up @@ -57,6 +61,9 @@ export function createInstanceProxy (
if (p === 'exports') {
return exportsProxy
}
if (p === kIsProxy) {
return true
}
return Reflect.get(target, p, receiver)
}
})
Expand Down
19 changes: 18 additions & 1 deletion packages/wasi-threads/src/wasi-threads.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ export interface WASIThreadsImports {
'thread-spawn': (startArg: number, errorOrTid?: number) => number
}

/** @public */
export interface StartResult {
exitCode: number
instance: WebAssembly.Instance
}

const patchedWasiInstances = new WeakMap<WASIThreads, WeakSet<WASIInstance>>()

/** @public */
Expand Down Expand Up @@ -276,6 +282,17 @@ export class WASIThreads {
return instance
}

public start (instance: WebAssembly.Instance, module: WebAssembly.Module, memory?: WebAssembly.Memory): StartResult {
const exports = instance.exports
memory ??= exports.memory as WebAssembly.Memory
if (this.childThread) {
instance = createInstanceProxy(instance, memory)
}
this!.setup(instance, module, memory)
const exitCode = this.wasi.start(instance)
return { exitCode, instance }
}

public terminateAllThreads (): void {
if (!this.childThread) {
this.PThread?.terminateAllThreads()
Expand All @@ -290,7 +307,6 @@ function patchWasiInstance (wasiThreads: WASIThreads, wasi: WASIInstance): void
if (patched.has(wasi)) {
return
}
patched.add(wasi)

const _this = wasiThreads
const wasiImport = wasi.wasiImport
Expand All @@ -314,6 +330,7 @@ function patchWasiInstance (wasiThreads: WASIThreads, wasi: WASIInstance): void
}
}
}
patched.add(wasi)
}

function getWasiSymbol (wasi: WASIInstance, description: string): symbol | undefined
Expand Down
19 changes: 6 additions & 13 deletions packages/wasi-threads/src/worker.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
import { createMessage } from './command'
import { type LoadPayload, createMessage } from './command'
import type { WorkerMessageEvent } from './thread-manager'
import { getPostMessage, isTrapError, serizeErrorToBuffer } from './util'

/** @public */
export interface InstantiatePayload {
wasmModule: WebAssembly.Module
wasmMemory: WebAssembly.Memory
sab?: Int32Array
}

export interface OnStartData {
tid: number
arg: number
Expand All @@ -17,7 +10,7 @@ export interface OnStartData {

/** @public */
export interface ThreadMessageHandlerOptions {
onLoad?: (data: InstantiatePayload) => WebAssembly.WebAssemblyInstantiatedSource | PromiseLike<WebAssembly.WebAssemblyInstantiatedSource>
onLoad?: (data: LoadPayload) => WebAssembly.WebAssemblyInstantiatedSource | PromiseLike<WebAssembly.WebAssemblyInstantiatedSource>
postMessage?: (message: any) => void
}

Expand All @@ -26,7 +19,7 @@ export class ThreadMessageHandler {
protected instance: WebAssembly.Instance | undefined
private messagesBeforeLoad: any[]
protected postMessage: (message: any) => void
protected onLoad?: (data: InstantiatePayload) => WebAssembly.WebAssemblyInstantiatedSource | PromiseLike<WebAssembly.WebAssemblyInstantiatedSource>
protected onLoad?: (data: LoadPayload) => WebAssembly.WebAssemblyInstantiatedSource | PromiseLike<WebAssembly.WebAssemblyInstantiatedSource>

public constructor (options?: ThreadMessageHandlerOptions) {
const postMsg = getPostMessage(options)
Expand All @@ -41,7 +34,7 @@ export class ThreadMessageHandler {
}

/** @virtual */
public instantiate (data: InstantiatePayload): WebAssembly.WebAssemblyInstantiatedSource | PromiseLike<WebAssembly.WebAssemblyInstantiatedSource> {
public instantiate (data: LoadPayload): WebAssembly.WebAssemblyInstantiatedSource | PromiseLike<WebAssembly.WebAssemblyInstantiatedSource> {
if (typeof this.onLoad === 'function') {
return this.onLoad(data)
}
Expand All @@ -64,7 +57,7 @@ export class ThreadMessageHandler {
}
}

private _load (payload: InstantiatePayload): void {
private _load (payload: LoadPayload): void {
if (this.instance !== undefined) return
let source: WebAssembly.WebAssemblyInstantiatedSource | PromiseLike<WebAssembly.WebAssemblyInstantiatedSource>
try {
Expand Down Expand Up @@ -107,7 +100,7 @@ export class ThreadMessageHandler {
postMessage(createMessage('cleanup-thread', { tid }))
}

protected _loaded (err: Error | null, source: WebAssembly.WebAssemblyInstantiatedSource | null, payload: InstantiatePayload): void {
protected _loaded (err: Error | null, source: WebAssembly.WebAssemblyInstantiatedSource | null, payload: LoadPayload): void {
if (err) {
notifyPthreadCreateResult(payload.sab, 2, err)
throw err
Expand Down

0 comments on commit c024ead

Please sign in to comment.