Commit c897f445 authored by Luke Sandberg's avatar Luke Sandberg Committed by Luke Sandberg

more snaps

parent fb4332ff
...@@ -39,6 +39,30 @@ export declare class ExternalObject<T> { ...@@ -39,6 +39,30 @@ export declare class ExternalObject<T> {
[K: symbol]: T [K: symbol]: T
} }
} }
export declare function registerWorkerScheduler(
creator: (arg: NapiWorkerCreation) => any,
terminator: (arg: NapiWorkerTermination) => any
): void
export declare function workerCreated(workerId: number): void
export interface NapiWorkerCreation {
options: NapiWorkerOptions
}
export interface NapiWorkerOptions {
filename: RcStr
cwd: RcStr
}
export interface NapiWorkerTermination {
options: NapiWorkerOptions
workerId: number
}
export interface NapiTaskMessage {
taskId: number
data: Buffer
}
export declare function recvTaskMessageInWorker(
workerId: number
): Promise<NapiTaskMessage>
export declare function sendTaskMessage(message: NapiTaskMessage): Promise<void>
/** /**
* Called by worker after WASM instantiation to register a dispatch callback. * Called by worker after WASM instantiation to register a dispatch callback.
* *
...@@ -76,30 +100,6 @@ export declare function wasmWorkerDispatchHostFn( ...@@ -76,30 +100,6 @@ export declare function wasmWorkerDispatchHostFn(
args: Array<number>, args: Array<number>,
memoryAccessor: object memoryAccessor: object
): unknown ): unknown
export declare function registerWorkerScheduler(
creator: (arg: NapiWorkerCreation) => any,
terminator: (arg: NapiWorkerTermination) => any
): void
export declare function workerCreated(workerId: number): void
export interface NapiWorkerCreation {
options: NapiWorkerOptions
}
export interface NapiWorkerOptions {
filename: RcStr
cwd: RcStr
}
export interface NapiWorkerTermination {
options: NapiWorkerOptions
workerId: number
}
export interface NapiTaskMessage {
taskId: number
data: Buffer
}
export declare function recvTaskMessageInWorker(
workerId: number
): Promise<NapiTaskMessage>
export declare function sendTaskMessage(message: NapiTaskMessage): Promise<void>
export interface NapiLocation { export interface NapiLocation {
line: number line: number
column?: number column?: number
......
...@@ -206,6 +206,7 @@ export const wasmManager = { ...@@ -206,6 +206,7 @@ export const wasmManager = {
instanceId, instanceId,
module, module,
hostFnDescriptors, hostFnDescriptors,
runtimeId,
}).then( }).then(
() => callback(instanceId), () => callback(instanceId),
(err) => callback(String(err)) (err) => callback(String(err))
......
...@@ -23,7 +23,8 @@ if (!parentPort) { ...@@ -23,7 +23,8 @@ if (!parentPort) {
// Load native addon directly in the worker thread. // Load native addon directly in the worker thread.
// This gives us direct NAPI access for host function callbacks. // This gives us direct NAPI access for host function callbacks.
const bindings = require(workerData.nativeBindingsPath) const bindings = require(workerData.nativeBindingsPath)
const runtimeId: number = workerData.runtimeId // runtimeId may be 0 at worker creation time; it gets updated per-instantiate message.
let runtimeId: number = workerData.runtimeId
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
// Instance storage — the ops object closes over the WASM instance/memory, // Instance storage — the ops object closes over the WASM instance/memory,
...@@ -91,6 +92,11 @@ parentPort.on('message', (req: any) => { ...@@ -91,6 +92,11 @@ parentPort.on('message', (req: any) => {
switch (req.type) { switch (req.type) {
case 'instantiate': { case 'instantiate': {
const instanceId = req.instanceId as number const instanceId = req.instanceId as number
// Update runtimeId from the message (may differ from workerData if
// workers were created before runtime registration).
if (req.runtimeId != null) {
runtimeId = req.runtimeId as number
}
// Build env imports that call Rust host functions directly via NAPI // Build env imports that call Rust host functions directly via NAPI
const envImports: Record<string, Function> = {} const envImports: Record<string, Function> = {}
...@@ -100,13 +106,14 @@ parentPort.on('message', (req: any) => { ...@@ -100,13 +106,14 @@ parentPort.on('message', (req: any) => {
, ,
index, index,
] of req.hostFnDescriptors as HostFnDescriptor[]) { ] of req.hostFnDescriptors as HostFnDescriptor[]) {
const capturedRuntimeID = runtimeId
envImports[name] = (...args: number[]) => { envImports[name] = (...args: number[]) => {
const ops = instances.get(instanceId) const ops = instances.get(instanceId)
if (!ops) { if (!ops) {
throw new Error(`Instance ${instanceId} not found for host fn`) throw new Error(`Instance ${instanceId} not found for host fn`)
} }
return bindings.wasmWorkerDispatchHostFn( return bindings.wasmWorkerDispatchHostFn(
runtimeId, capturedRuntimeID,
instanceId, instanceId,
index, index,
args.slice(0, paramCount), args.slice(0, paramCount),
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment