Skip to content

Commit

Permalink
Change resource file import syntax to be visible to bundlers.
Browse files Browse the repository at this point in the history
  • Loading branch information
whitequark committed Jan 20, 2024
1 parent f42d61d commit 4b2f956
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 11 deletions.
2 changes: 1 addition & 1 deletion lib/api.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export class Exit extends Error {
}

export class Application {
constructor(resourceFileURL: URL | string, instantiate: any, argv0: string);
constructor(resources: () => Promise<any>, instantiate: any, argv0: string);

run(args?: string[], files?: Tree, options?: {
stdout?: OutputStream | null,
Expand Down
16 changes: 8 additions & 8 deletions lib/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,32 +40,32 @@ 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

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 {
Expand All @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion package-in.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@yowasp/runtime",
"version": "6.0",
"version": "7.0",
"description": "Common runtime for YoWASP packages",
"author": "Catherine <[email protected]>",
"license": "ISC",
Expand Down
2 changes: 1 addition & 1 deletion test/yowasp_runtime_test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down

0 comments on commit 4b2f956

Please sign in to comment.