Skip to content

Commit

Permalink
[autorelease] Add argv0 argument to Application().
Browse files Browse the repository at this point in the history
This allows writing downstream code like:

    export const run_yosys = yosys.run.bind(yosys);
  • Loading branch information
whitequark committed Dec 9, 2023
1 parent eff459a commit efab3a8
Show file tree
Hide file tree
Showing 9 changed files with 25 additions and 37 deletions.
5 changes: 3 additions & 2 deletions lib/api-base.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,20 @@ import { Exit, Environment, directoryFromTree, directoryIntoTree } from './wasi-
export { Exit } from './wasi-virt.js';

export class BaseApplication {
constructor(getResources, wasmModules, instantiate) {
constructor(getResources, wasmModules, instantiate, argv0) {
this._resources = null;
this.getResources = getResources;
this.wasmModules = wasmModules;
this.instantiate = instantiate;
this.argv0 = argv0;
}

async run(args, files = {}, printLine = console.log) {
if (this._resources === null)
this._resources = await this.getResources();

const environment = new Environment();
environment.args = args;
environment.args = [this.argv0].concat(args);
environment.root = directoryFromTree(files);
for (const [dirName, resourceFiles] of Object.entries(this._resources))
environment.root.files[dirName] = directoryFromTree(resourceFiles);
Expand Down
4 changes: 2 additions & 2 deletions lib/api-browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { BaseApplication } from './api-base.js';
export { Exit } from './api-base.js';

export class Application extends BaseApplication {
constructor(baseURL, resourceFilenames, wasmFilenames, instantiate) {
constructor(baseURL, resourceFilenames, wasmFilenames, instantiate, argv0) {
async function getResources() {
const resources = {};
for (const [dirName, jsonFilename] of Object.entries(resourceFilenames))
Expand All @@ -15,6 +15,6 @@ export class Application extends BaseApplication {
for (const [modName, wasmFilename] of Object.entries(wasmFilenames))
wasmModules[modName] = fetch(new URL(wasmFilename, baseURL)).then(WebAssembly.compileStreaming);

super(getResources, wasmModules, instantiate);
super(getResources, wasmModules, instantiate, argv0);
}
}
4 changes: 2 additions & 2 deletions lib/api-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { BaseApplication } from './api-base.js';
export { Exit } from './api-base.js';

export class Application extends BaseApplication {
constructor(baseURL, resourceFilenames, wasmFilenames, instantiate) {
constructor(baseURL, resourceFilenames, wasmFilenames, instantiate, argv0) {
async function getResources() {
const resources = {};
for (const [dirName, jsonFilename] of Object.entries(resourceFilenames))
Expand All @@ -16,6 +16,6 @@ export class Application extends BaseApplication {
for (const [modName, wasmFilename] of Object.entries(wasmFilenames))
wasmModules[modName] = readFile(new URL(wasmFilename, baseURL)).then(WebAssembly.compile);

super(getResources, wasmModules, instantiate);
super(getResources, wasmModules, instantiate, argv0);
}
}
13 changes: 8 additions & 5 deletions prepare.mjs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { execFileSync } from 'child_process';
import { readFileSync, writeFileSync } from 'fs';

const revList = execFileSync('git', ['rev-list', 'HEAD'], {encoding: 'utf-8'}).split('\n');
const packageJSON = JSON.parse(readFileSync('package-in.json', {encoding: 'utf-8'}));
packageJSON.version = `1.0.${revList.length - 2}`;
const revCount = execFileSync('git', ['rev-list', 'HEAD'], {encoding: 'utf-8'}).split('\n').length - 1;

let version = `2.0.${revCount - 1}`;
if (!['true', '1', 'yes'].includes(process.env['RELEASE_BRANCH']))
packageJSON.version += '-dev';
console.log(`version ${packageJSON.version}`);
version += '-dev';
console.log(`version ${version}`);

const packageJSON = JSON.parse(readFileSync('package-in.json', {encoding: 'utf-8'}));
packageJSON.version = version;
writeFileSync('package.json', JSON.stringify(packageJSON));
8 changes: 4 additions & 4 deletions test/run.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/bin/sh -e

cd $(dirname $0)

cd yowasp_runtime_test
./build.sh
cd $(dirname $0)/yowasp_runtime_test
npm install
npm run pack
npm run transpile
npm run test
18 changes: 0 additions & 18 deletions test/yowasp_runtime_test/build.sh

This file was deleted.

Empty file.
8 changes: 4 additions & 4 deletions test/yowasp_runtime_test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@ import { Application } from '@yowasp/runtime';
import { instantiate } from './gen/copy.js';


const yowasp_runtime_test = new Application(import.meta.url, {
const yowaspRuntimeTest = new Application(import.meta.url, {
'share': './gen/share.json'
}, {
'copy.core.wasm': './gen/copy.core.wasm',
'copy.core2.wasm': './gen/copy.core2.wasm',
'copy.core3.wasm': './gen/copy.core3.wasm',
'copy.core4.wasm': './gen/copy.core4.wasm',
}, instantiate);
}, instantiate, 'copy');


if ((await yowasp_runtime_test.run(['copy', 'share/foo.txt', 'bar.txt'], {}))['bar.txt'] !== 'contents of foo')
if ((await yowaspRuntimeTest.run(['share/foo.txt', 'bar.txt'], {}))['bar.txt'] !== 'contents of foo')
throw 'test 1 failed';

if ((await yowasp_runtime_test.run(['copy', 'baz.txt', 'bar.txt'], {'baz.txt': 'contents of baz'}))['bar.txt'] !== 'contents of baz')
if ((await yowaspRuntimeTest.run(['baz.txt', 'bar.txt'], {'baz.txt': 'contents of baz'}))['bar.txt'] !== 'contents of baz')
throw 'test 2 failed';
2 changes: 2 additions & 0 deletions test/yowasp_runtime_test/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
"@bytecodealliance/jco": "^0.14.1"
},
"scripts": {
"pack": "yowasp-pack-resources share gen/share.json",
"transpile": "jco new ../copy.wasm --wasi-command --output copy.wasm && jco transpile copy.wasm --instantiation async --no-typescript --no-namespaced-exports --map 'wasi:io/*=runtime#io' --map 'wasi:cli/*=runtime#cli' --map 'wasi:clocks/*=runtime#*' --map 'wasi:filesystem/*=runtime#fs' --out-dir gen/",
"build": "./build.sh",
"test": "node ./index.js"
}
Expand Down

0 comments on commit efab3a8

Please sign in to comment.