Skip to content

Commit

Permalink
Adding a way to load the runtime synchronously
Browse files Browse the repository at this point in the history
  • Loading branch information
ghadeeras committed Dec 2, 2024
1 parent 62b197a commit ae103ef
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"--require", "source-map-support/register",
"--timeout", "120000000",
// "--fgrep", ">>>",
"\"${workspaceFolder}/lib/test/**/*.test.js\"",
"\"${workspaceFolder}/out/test/**/*.test.js\"",
],
"console": "integratedTerminal"
}
Expand Down
7 changes: 7 additions & 0 deletions src/prod/js/rt-node.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as rt from "./rt.js"
import * as wa from "./wa.js"
import * as waNode from "./wa-node.js"

import binaryen from 'binaryen'
Expand Down Expand Up @@ -124,3 +125,9 @@ export function addDelayImportsToModule(module: binaryen.Module) {
module.addFunctionImport("item_ref", "delay", "item_ref", binaryen.createType([binaryen.i32, binaryen.i32]), binaryen.i32)
module.addFunctionImport("rotate", "delay", "rotate", binaryen.createType([binaryen.i32]), binaryen.i32)
}

export function runtime(modulesLoader: wa.SyncModulesLoader = waNode.syncFsModulesLoader, rawMem: ArrayBuffer | null = null): rt.Runtime {
const modules = modulesLoader(import.meta.dirname + "/../wa", rt.runtimeModulePaths());
return rt.createRuntime(rawMem, modules);
}

10 changes: 7 additions & 3 deletions src/prod/js/rt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,12 +146,16 @@ export function runtimeModulePaths(): Record<RuntimeModuleNames, string> {
}
}

export async function runtime(waPath: string, modulesLoader: wa.ModulesLoader = wa.webModulesLoader, rawMem: ArrayBuffer | null = null): Promise<Runtime> {
const modules = await loadRuntimeModules(waPath, modulesLoader);
export async function runtime(waPath: string = import.meta.url + "/../wa", modulesLoader: wa.ModulesLoader = wa.webModulesLoader, rawMem: ArrayBuffer | null = null): Promise<Runtime> {
const modules = await loadRuntimeModules(waPath, modulesLoader)
return createRuntime(rawMem, modules)
}

export function createRuntime(rawMem: ArrayBuffer | null, modules: wa.WebAssemblyModules<keyof RuntimeExports>) {
if (rawMem) {
modules.rawMem = new WebAssembly.Module(rawMem)
}
return linkRuntime(modules);
return linkRuntime(modules)
}

export async function loadRuntimeModules(waPath: string, modulesLoader: wa.ModulesLoader = wa.webModulesLoader): Promise<wa.WebAssemblyModules<RuntimeModuleNames>> {
Expand Down
1 change: 1 addition & 0 deletions src/prod/js/wa-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import fs from 'fs'
export * from "./wa.js"

export const fsModulesLoader: wa.ModulesLoader = async (waPath, modulePaths) => fsLoadModules(waPath, modulePaths)
export const syncFsModulesLoader: wa.SyncModulesLoader = (waPath, modulePaths) => fsLoadModules(waPath, modulePaths)

export function fsLoadModules<N extends string>(waPath: string, modulePaths: wa.WebAssemblyModulePaths<N>): wa.WebAssemblyModules<N> {
const result: wa.WebAssemblyModules<string> = {}
Expand Down
1 change: 1 addition & 0 deletions src/prod/js/wa.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export type WebAssemblyInstance = Pick<WebAssembly.Instance, any>
export type WebAssemblyInstances<N extends string> = Record<N, WebAssemblyInstance>

export type ModulesLoader = <N extends string>(waPath: string, modulePaths: WebAssemblyModulePaths<N>) => Promise<WebAssemblyModules<N>>
export type SyncModulesLoader = <N extends string>(waPath: string, modulePaths: WebAssemblyModulePaths<N>) => WebAssemblyModules<N>

export const webModulesLoader: ModulesLoader = webLoadModules

Expand Down

0 comments on commit ae103ef

Please sign in to comment.