Commit 16fb5d23 authored by Luke Sandberg's avatar Luke Sandberg Committed by Luke Sandberg

simplify some of the bootstrapping

parent 2a6e4a19
...@@ -23,7 +23,7 @@ static WASM_TSFN_ENV: OnceLock<usize> = OnceLock::new(); ...@@ -23,7 +23,7 @@ static WASM_TSFN_ENV: OnceLock<usize> = OnceLock::new();
/// Global host function registry: maps instance_id → Vec<Func>. /// Global host function registry: maps instance_id → Vec<Func>.
/// Workers call back into these during WASM execution via NAPI. /// Workers call back into these during WASM execution via NAPI.
static HOST_FNS: LazyLock<Mutex<HashMap<u64, Arc<Vec<(String, runtime::Func)>>>>> = static HOST_FNS: LazyLock<Mutex<HashMap<u64, Arc<Vec<runtime::Func>>>>> =
LazyLock::new(|| Mutex::new(HashMap::new())); LazyLock::new(|| Mutex::new(HashMap::new()));
/// Global per-instance TSFN registry: maps instance_id → TSFN targeting the worker thread. /// Global per-instance TSFN registry: maps instance_id → TSFN targeting the worker thread.
...@@ -294,7 +294,7 @@ pub fn wasm_worker_dispatch_host_fn( ...@@ -294,7 +294,7 @@ pub fn wasm_worker_dispatch_host_fn(
))); )));
} }
let (ref _name, ref func) = host_functions[fn_index]; let func = &host_functions[fn_index];
let result_count = func.sign.1 as usize; let result_count = func.sign.1 as usize;
let mut caller = NapiDirectCaller { let mut caller = NapiDirectCaller {
...@@ -557,54 +557,46 @@ fn instantiate_js( ...@@ -557,54 +557,46 @@ fn instantiate_js(
host_fn_descriptors: Vec<HostFnDescriptor>, host_fn_descriptors: Vec<HostFnDescriptor>,
reply: mpsc::SyncSender<anyhow::Result<u64>>, reply: mpsc::SyncSender<anyhow::Result<u64>>,
) -> napi::Result<()> { ) -> napi::Result<()> {
// Each descriptor is a [name, paramCount, resultCount, index] tuple —
// arrays are faster to construct and serialize than objects.
let mut descriptors_arr = env.create_array_with_length(host_fn_descriptors.len())?; let mut descriptors_arr = env.create_array_with_length(host_fn_descriptors.len())?;
for (i, desc) in host_fn_descriptors.iter().enumerate() { for (i, desc) in host_fn_descriptors.iter().enumerate() {
let mut obj = env.create_object()?; let mut tuple = env.create_array_with_length(4)?;
obj.set_named_property("name", env.create_string(&desc.name)?)?; tuple.set_element(0, env.create_string(&desc.name)?)?;
obj.set_named_property("paramCount", env.create_int32(desc.param_count as i32)?)?; tuple.set_element(1, env.create_int32(desc.param_count as i32)?)?;
obj.set_named_property("resultCount", env.create_int32(desc.result_count as i32)?)?; tuple.set_element(2, env.create_int32(desc.result_count as i32)?)?;
obj.set_named_property("index", env.create_int32(desc.index as i32)?)?; tuple.set_element(3, env.create_int32(desc.index as i32)?)?;
descriptors_arr.set_element(i as u32, obj)?; descriptors_arr.set_element(i as u32, tuple)?;
} }
// Single callback: JS calls with a number (instanceId) on success,
// or a string (error message) on failure.
let callback = env.create_function_from_closure("callback", move |ctx| {
let result: JsUnknown = ctx.get(0)?;
match result.get_type()? {
napi::ValueType::Number => {
let n: JsNumber = result.try_into()?;
let _ = reply.send(Ok(n.get_double()? as u64));
}
_ => {
let s: napi::JsString = result.try_into()?;
let msg = s.into_utf8()?.as_str()?.to_owned();
let _ = reply.send(Err(anyhow::anyhow!("Instantiate failed: {}", msg)));
}
}
ctx.env.get_undefined()
})?;
let instantiate_fn: JsFunction = manager.get_named_property("instantiateOnWorker")?; let instantiate_fn: JsFunction = manager.get_named_property("instantiateOnWorker")?;
let result = instantiate_fn.call( instantiate_fn.call(
None, None,
&[ &[
env.create_double(module_id as f64)?.into_unknown(), env.create_double(module_id as f64)?.into_unknown(),
unsafe { JsUnknown::from_raw_unchecked(env.raw(), descriptors_arr.raw()) }, unsafe { JsUnknown::from_raw_unchecked(env.raw(), descriptors_arr.raw()) },
callback.into_unknown(),
], ],
)?; )?;
let result: JsObject = result.try_into()?;
let instance_id: JsNumber = result.get_named_property("instanceId")?;
let instance_id_val = instance_id.get_double()? as u64;
// The promise resolves when the worker finishes instantiation.
let promise: JsObject = result.get_named_property("promise")?;
let then_fn: JsFunction = promise.get_named_property("then")?;
let reply_ok = reply.clone();
let resolve_cb = env.create_function_from_closure("resolve", move |ctx| {
let _ = reply_ok.send(Ok(instance_id_val));
ctx.env.get_undefined()
})?;
let reject_cb = env.create_function_from_closure("reject", move |ctx| {
let err: JsUnknown = ctx.get(0)?;
let msg = err
.coerce_to_string()
.and_then(|s| s.into_utf8()?.as_str().map(|s| s.to_owned()))
.unwrap_or_else(|_| "unknown error".to_string());
let _ = reply.send(Err(anyhow::anyhow!("Instantiate failed: {}", msg)));
ctx.env.get_undefined()
})?;
then_fn.call(
Some(&promise),
&[resolve_cb.into_unknown(), reject_cb.into_unknown()],
)?;
Ok(()) Ok(())
} }
...@@ -650,18 +642,18 @@ impl runtime::Runtime for NapiRuntime { ...@@ -650,18 +642,18 @@ impl runtime::Runtime for NapiRuntime {
} }
}; };
let host_fn_descriptors: Vec<HostFnDescriptor> = imports let mut host_fn_descriptors = Vec::with_capacity(imports.len());
.iter() let mut host_functions = Vec::with_capacity(imports.len());
.enumerate() for (i, (name, func)) in imports.into_iter().enumerate() {
.map(|(i, (name, func))| HostFnDescriptor { host_fn_descriptors.push(HostFnDescriptor {
name: name.clone(), name,
param_count: func.sign.0, param_count: func.sign.0,
result_count: func.sign.1, result_count: func.sign.1,
index: i, index: i,
}) });
.collect(); host_functions.push(func);
}
let host_functions = Arc::new(imports); let host_functions = Arc::new(host_functions);
// Instantiate on a worker thread via TSFN → postMessage. // Instantiate on a worker thread via TSFN → postMessage.
// The worker will call wasmWorkerRegisterCallback to set up the per-instance TSFN. // The worker will call wasmWorkerRegisterCallback to set up the per-instance TSFN.
......
...@@ -68,8 +68,8 @@ export declare function sendTaskMessage(message: NapiTaskMessage): Promise<void> ...@@ -68,8 +68,8 @@ export declare function sendTaskMessage(message: NapiTaskMessage): Promise<void>
* The `ops` object must have these methods: * The `ops` object must have these methods:
* - transform(programPtr, programLen, unresolvedMark, commentsProxy) → number * - transform(programPtr, programLen, unresolvedMark, commentsProxy) → number
* - getDiag() → number * - getDiag() → number
* - readMemory(ptr, len) → Buffer * - readBuf(ptr, len) → Buffer
* - writeMemory(ptr, data: Buffer) → void * - writeBuf(ptr, data: Buffer) → void
* - alloc(size) → number * - alloc(size) → number
* - free(ptr, size) → number * - free(ptr, size) → number
* *
......
...@@ -170,19 +170,16 @@ export const wasmManager = { ...@@ -170,19 +170,16 @@ export const wasmManager = {
* Instantiate a WASM module on a worker. The module is transferred via * Instantiate a WASM module on a worker. The module is transferred via
* structured clone (zero-cost for WebAssembly.Module). * structured clone (zero-cost for WebAssembly.Module).
* *
* Returns { instanceId, promise } — Rust waits on the promise for completion. * Rust passes a single callback; JS calls it with the instanceId (number)
* on success or an error message (string) on failure.
* After instantiation, the worker registers ops with Rust via NAPI and * After instantiation, the worker registers ops with Rust via NAPI and
* its event loop remains free to receive TSFN callbacks. * its event loop remains free to receive TSFN callbacks.
*/ */
instantiateOnWorker( instantiateOnWorker(
moduleId: number, moduleId: number,
hostFnDescriptors: Array<{ hostFnDescriptors: Array<[string, number, number, number]>,
name: string callback: (result: number | string) => void
paramCount: number ): void {
resultCount: number
index: number
}>
): { instanceId: number; promise: Promise<number> } {
const module = compiledModules.get(moduleId) const module = compiledModules.get(moduleId)
if (!module) throw new Error(`Module ${moduleId} not found`) if (!module) throw new Error(`Module ${moduleId} not found`)
...@@ -193,14 +190,15 @@ export const wasmManager = { ...@@ -193,14 +190,15 @@ export const wasmManager = {
instanceWorkerMap.set(instanceId, workerIndex) instanceWorkerMap.set(instanceId, workerIndex)
workerEntry.instances.add(instanceId) workerEntry.instances.add(instanceId)
const promise = sendToWorker(workerEntry, { sendToWorker(workerEntry, {
type: 'instantiate', type: 'instantiate',
instanceId, instanceId,
module, module,
hostFnDescriptors, hostFnDescriptors,
}) }).then(
() => callback(instanceId),
return { instanceId, promise } (err) => callback(String(err))
)
}, },
dropModule(moduleId: number): void { dropModule(moduleId: number): void {
......
...@@ -25,25 +25,16 @@ if (!parentPort) { ...@@ -25,25 +25,16 @@ if (!parentPort) {
const bindings = require(workerData.nativeBindingsPath) const bindings = require(workerData.nativeBindingsPath)
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
// Instance storage // Instance storage — the ops object closes over the WASM instance/memory,
// so it serves as both the instance handle and the ops interface.
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
interface InstanceEntry { const instances = new Map<number, ReturnType<typeof createInstanceOps>>()
instance: WebAssembly.Instance
memory: WebAssembly.Memory
}
const instances = new Map<number, InstanceEntry>()
const instanceOps = new Map<number, ReturnType<typeof createInstanceOps>>()
// ---------------------------------------------------------------------------
// Create ops object for a WASM instance.
// These methods are called by Rust via the per-instance TSFN (transform,
// getDiag, memory ops) and also passed as the memory accessor during host
// function dispatch (readBuf, writeBuf, alloc, free).
// ---------------------------------------------------------------------------
function createInstanceOps(entry: InstanceEntry) { function createInstanceOps(
instance: WebAssembly.Instance,
memory: WebAssembly.Memory
) {
return { return {
transform( transform(
programPtr: number, programPtr: number,
...@@ -51,7 +42,7 @@ function createInstanceOps(entry: InstanceEntry) { ...@@ -51,7 +42,7 @@ function createInstanceOps(entry: InstanceEntry) {
unresolvedMark: number, unresolvedMark: number,
commentsProxy: number commentsProxy: number
): number { ): number {
const transformFn = entry.instance.exports const transformFn = instance.exports
.__transform_plugin_process_impl as Function .__transform_plugin_process_impl as Function
return transformFn( return transformFn(
programPtr, programPtr,
...@@ -62,25 +53,25 @@ function createInstanceOps(entry: InstanceEntry) { ...@@ -62,25 +53,25 @@ function createInstanceOps(entry: InstanceEntry) {
}, },
getDiag(): number { getDiag(): number {
const diagFn = entry.instance.exports const diagFn = instance.exports
.__get_transform_plugin_core_pkg_diag as Function .__get_transform_plugin_core_pkg_diag as Function
return diagFn() as number return diagFn() as number
}, },
readBuf(ptr: number, len: number): Buffer { readBuf(ptr: number, len: number): Buffer {
return Buffer.from(new Uint8Array(entry.memory.buffer, ptr, len).slice()) return Buffer.from(new Uint8Array(memory.buffer, ptr, len).slice())
}, },
writeBuf(ptr: number, data: Buffer): void { writeBuf(ptr: number, data: Buffer): void {
new Uint8Array(entry.memory.buffer, ptr, data.byteLength).set(data) new Uint8Array(memory.buffer, ptr, data.byteLength).set(data)
}, },
alloc(size: number): number { alloc(size: number): number {
return (entry.instance.exports.__alloc as Function)(size) as number return (instance.exports.__alloc as Function)(size) as number
}, },
free(ptr: number, size: number): number { free(ptr: number, size: number): number {
return (entry.instance.exports.__free as Function)(ptr, size) as number return (instance.exports.__free as Function)(ptr, size) as number
}, },
} }
} }
...@@ -89,12 +80,8 @@ function createInstanceOps(entry: InstanceEntry) { ...@@ -89,12 +80,8 @@ function createInstanceOps(entry: InstanceEntry) {
// postMessage handlers // postMessage handlers
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
interface HostFnDescriptor { // Each descriptor is a [name, paramCount, resultCount, index] tuple.
name: string type HostFnDescriptor = [string, number, number, number]
paramCount: number
resultCount: number
index: number
}
parentPort.on('message', (req: any) => { parentPort.on('message', (req: any) => {
try { try {
...@@ -106,10 +93,14 @@ parentPort.on('message', (req: any) => { ...@@ -106,10 +93,14 @@ parentPort.on('message', (req: any) => {
// 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> = {}
for (const desc of req.hostFnDescriptors as HostFnDescriptor[]) { for (const [
const { index, paramCount } = desc name,
envImports[desc.name] = (...args: number[]) => { paramCount,
const ops = instanceOps.get(instanceId) ,
index,
] of req.hostFnDescriptors as HostFnDescriptor[]) {
envImports[name] = (...args: number[]) => {
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`)
} }
...@@ -132,13 +123,11 @@ parentPort.on('message', (req: any) => { ...@@ -132,13 +123,11 @@ parentPort.on('message', (req: any) => {
}) })
wasi.initialize(instance) wasi.initialize(instance)
const memory = instance.exports.memory as WebAssembly.Memory const memory = instance.exports.memory as WebAssembly.Memory
const entry: InstanceEntry = { instance, memory }
instances.set(instanceId, entry)
// Register ops with Rust — this creates a TSFN targeting this worker's // The ops object closes over instance/memory and serves as both
// event loop. Rust will call these ops directly via the TSFN. // the instance handle and the ops interface for Rust.
const ops = createInstanceOps(entry) const ops = createInstanceOps(instance, memory)
instanceOps.set(instanceId, ops) instances.set(instanceId, ops)
bindings.wasmWorkerRegisterCallback(instanceId, ops) bindings.wasmWorkerRegisterCallback(instanceId, ops)
response = { result: instanceId } response = { result: instanceId }
...@@ -147,7 +136,6 @@ parentPort.on('message', (req: any) => { ...@@ -147,7 +136,6 @@ parentPort.on('message', (req: any) => {
case 'dropInstance': { case 'dropInstance': {
instances.delete(req.instanceId) instances.delete(req.instanceId)
instanceOps.delete(req.instanceId)
response = { result: null } response = { result: null }
break break
} }
......
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