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> {
[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.
*
......@@ -76,30 +100,6 @@ export declare function wasmWorkerDispatchHostFn(
args: Array<number>,
memoryAccessor: object
): 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 {
line: number
column?: number
......
......@@ -206,6 +206,7 @@ export const wasmManager = {
instanceId,
module,
hostFnDescriptors,
runtimeId,
}).then(
() => callback(instanceId),
(err) => callback(String(err))
......
......@@ -23,7 +23,8 @@ if (!parentPort) {
// Load native addon directly in the worker thread.
// This gives us direct NAPI access for host function callbacks.
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,
......@@ -91,6 +92,11 @@ parentPort.on('message', (req: any) => {
switch (req.type) {
case 'instantiate': {
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
const envImports: Record<string, Function> = {}
......@@ -100,13 +106,14 @@ parentPort.on('message', (req: any) => {
,
index,
] of req.hostFnDescriptors as HostFnDescriptor[]) {
const capturedRuntimeID = runtimeId
envImports[name] = (...args: number[]) => {
const ops = instances.get(instanceId)
if (!ops) {
throw new Error(`Instance ${instanceId} not found for host fn`)
}
return bindings.wasmWorkerDispatchHostFn(
runtimeId,
capturedRuntimeID,
instanceId,
index,
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