From 4b2f9560362f1cafd3e5ff37628e483fa185dd31 Mon Sep 17 00:00:00 2001 From: Catherine Date: Sat, 20 Jan 2024 00:49:49 +0000 Subject: [PATCH] Change resource file import syntax to be visible to bundlers. --- lib/api.d.ts | 2 +- lib/api.js | 16 ++++++++-------- package-in.json | 2 +- test/yowasp_runtime_test/index.js | 2 +- 4 files changed, 11 insertions(+), 11 deletions(-) diff --git a/lib/api.d.ts b/lib/api.d.ts index 10682f9..9973b95 100644 --- a/lib/api.d.ts +++ b/lib/api.d.ts @@ -11,7 +11,7 @@ export class Exit extends Error { } export class Application { - constructor(resourceFileURL: URL | string, instantiate: any, argv0: string); + constructor(resources: () => Promise, instantiate: any, argv0: string); run(args?: string[], files?: Tree, options?: { stdout?: OutputStream | null, diff --git a/lib/api.js b/lib/api.js index a2e4177..b12ece1 100644 --- a/lib/api.js +++ b/lib/api.js @@ -40,17 +40,17 @@ function fetchResources({ modules, filesystem }) { } export class Application { - constructor(resourceFileURL, instantiate, argv0) { - this.resourceFileURL = resourceFileURL; - this.resources = null; + constructor(resources, instantiate, argv0) { + this.resources = resources; + this.resourceData = null; this.instantiate = instantiate; this.argv0 = argv0; } // The `printLine` option is deprecated and not documented but still accepted for compatibility. async run(args = null, files = {}, { stdout, stderr, decodeASCII = true, printLine } = {}) { - if (this.resources === null) - this.resources = await import(this.resourceFileURL).then(fetchResources); + if (this.resourceData === null) + this.resourceData = await this.resources().then(fetchResources); if (args === null) return; // prefetch resources, but do not run @@ -58,14 +58,14 @@ export class Application { const environment = new Environment(); environment.args = [this.argv0].concat(args); environment.root = directoryFromTree(files); - for (const [dirName, dirContents] of Object.entries(this.resources.filesystem)) + for (const [dirName, dirContents] of Object.entries(this.resourceData.filesystem)) environment.root.files[dirName] = directoryFromTree(dirContents); const lineBufferedConsole = lineBuffered(printLine ?? console.log); environment.stdout = stdout === undefined ? lineBufferedConsole : stdout; environment.stderr = stderr === undefined ? lineBufferedConsole : stderr; const wasmCommand = await this.instantiate( - (filename) => this.resources.modules[filename], + (filename) => this.resourceData.modules[filename], { runtime: environment.exports }); let error = null; try { @@ -77,7 +77,7 @@ export class Application { error = e; } - for (const dirName of Object.keys(this.resources.filesystem)) + for (const dirName of Object.keys(this.resourceData.filesystem)) delete environment.root.files[dirName]; files = directoryIntoTree(environment.root, { decodeASCII }); if (error !== null) { diff --git a/package-in.json b/package-in.json index 9d78340..48555be 100644 --- a/package-in.json +++ b/package-in.json @@ -1,6 +1,6 @@ { "name": "@yowasp/runtime", - "version": "6.0", + "version": "7.0", "description": "Common runtime for YoWASP packages", "author": "Catherine ", "license": "ISC", diff --git a/test/yowasp_runtime_test/index.js b/test/yowasp_runtime_test/index.js index 7dce1ab..89af668 100644 --- a/test/yowasp_runtime_test/index.js +++ b/test/yowasp_runtime_test/index.js @@ -3,7 +3,7 @@ import { lineBuffered } from '@yowasp/runtime/util'; import { instantiate } from './gen/copy.js'; -const yowaspRuntimeTest = new Application(new URL('./gen/resources.js', import.meta.url), instantiate, 'copy'); +const yowaspRuntimeTest = new Application(() => import('./gen/resources.js'), instantiate, 'copy'); if ((await yowaspRuntimeTest.run(['share/foo.txt', 'bar.txt'], {}))['bar.txt'] !== 'contents of foo')