Skip to content

Commit

Permalink
Initial commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
whitequark committed Dec 9, 2023
0 parents commit 1fc23db
Show file tree
Hide file tree
Showing 23 changed files with 729 additions and 0 deletions.
17 changes: 17 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"env": {
"browser": true,
"es2021": true
},
"extends": "eslint:recommended",
"parserOptions": {
"ecmaVersion": "latest",
"sourceType": "module"
},
"rules": {
"semi": "error",
"eqeqeq": "error",
"no-unused-vars": ["error", { "argsIgnorePattern": "^_", "varsIgnorePattern": "^_" }],
"no-constant-condition": ["error", { "checkLoops": false }]
}
}
1 change: 1 addition & 0 deletions .github/FUNDING
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
patreon: whitequark
64 changes: 64 additions & 0 deletions .github/workflows/package.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
on: [push, pull_request]
name: Test & publish
jobs:
test:
runs-on: ubuntu-latest
steps:
- name: Check out source code
uses: actions/checkout@v3
with:
fetch-depth: 0
- name: Run tests
run: |
./test/run.sh
- name: Run lints
run: |
npm run lint
check: # group all `test (*)` workflows into one for the required status check
needs: test
if: always() && !contains(needs.*.result, 'cancelled')
runs-on: ubuntu-latest
steps:
- run: ${{ contains(needs.*.result, 'failure') && 'false' || 'true' }}
# publish:
# needs: check
# runs-on: ubuntu-latest
# environment: publish
# permissions:
# id-token: write
# steps:
# - name: Check out source code
# uses: actions/checkout@v3
# with:
# fetch-depth: 0
# - name: Set up Python
# uses: actions/setup-python@v4
# with:
# python-version: '3.x'
# - name: Install dependencies
# run: |
# pip install build
# - name: Build wheel
# run: |
# python -m build -w
# - name: Publish wheel to Test PyPI
# if: github.event_name == 'push' && github.event.ref == 'refs/heads/develop'
# uses: pypa/gh-action-pypi-publish@release/v1
# with:
# repository-url: https://test.pypi.org/legacy/
# - name: Publish wheel to PyPI
# if: github.event_name == 'push' && github.event.ref == 'refs/heads/release'
# uses: pypa/gh-action-pypi-publish@release/v1
# release:
# needs: check
# runs-on: ubuntu-latest
# if: "contains(github.event.head_commit.message, 'autorelease') && github.event_name == 'push' && github.ref == 'refs/heads/develop'"
# steps:
# - name: Check out source code
# uses: actions/checkout@v3
# with:
# token: ${{ secrets.PUSH_TOKEN }}
# fetch-depth: 0 # fetch the release branch as well
# - name: Update release branch
# run: |
# git push origin develop:release
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/package-lock.json
/node_modules
/dist
/gen
15 changes: 15 additions & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
ISC License

Copyright (C) Catherine <whitequark@whitequark.org>

Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
YoWASP JavaScript runtime
=========================

This package is an internal support package for the [YoWASP project][yowasp]. It handles interfacing with the [WebAssembly][] runtime and the supported execution environments (Node.js and the browser). Do not depend on this package in your own code.

[webassembly]: https://webassembly.org/
[yowasp]: https://yowasp.github.io/


License
-------

This package is covered by the [ISC license](LICENSE.txt).
24 changes: 24 additions & 0 deletions bin/pack-resources.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/usr/bin/env node

import { readdir, readFile, writeFile } from 'fs/promises';

async function packDirectory(root) {
const files = await readdir(root, { withFileTypes: true });
const packedFiles = {};
for (const file of files) {
const filePath = `${root}/${file.name}`;
if (file.isDirectory())
packedFiles[file.name] = await packDirectory(filePath);
if (file.isFile())
packedFiles[file.name] = await readFile(filePath, {encoding: 'utf-8'});
}
return packedFiles;
}

const args = process.argv.slice(2);
if (args.length !== 2) {
console.error(`Usage: yowasp-pack-resources <directory> <file.json>`);
process.exit(1);
}

await writeFile(args[1], JSON.stringify(await packDirectory(args[0])));
38 changes: 38 additions & 0 deletions lib/api-base.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { Exit, Environment, directoryFromTree, directoryIntoTree } from './wasi-virt.js';

export { Exit } from './wasi-virt.js';

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

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

const environment = new Environment();
environment.args = args;
environment.root = directoryFromTree(files);
for (const [dirName, resourceFiles] of Object.entries(this._resources))
environment.root.files[dirName] = directoryFromTree(resourceFiles);
environment.printLine = printLine;

const wasmCommand = await this.instantiate(
(filename) => this.wasmModules[filename],
{ runtime: environment.exports });
try {
wasmCommand.run.run();
} catch (e) {
if (!(e instanceof Exit && e.code === 0))
throw e;
}

for (const dirName of Object.keys(this._resources))
delete environment.root[dirName];
return directoryIntoTree(environment.root);
}
}
20 changes: 20 additions & 0 deletions lib/api-browser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { BaseApplication } from './api-base.js';

export { Exit } from './api-base.js';

export class Application extends BaseApplication {
constructor(baseURL, resourceFilenames, wasmFilenames, instantiate) {
async function getResources() {
const resources = {};
for (const [dirName, jsonFilename] of Object.entries(resourceFilenames))
resources[dirName] = JSON.parse(await fetch(new URL(jsonFilename, baseURL)));
return resources;
}

const wasmModules = {};
for (const [modName, wasmFilename] of Object.entries(wasmFilenames))
wasmModules[modName] = fetch(new URL(wasmFilename, baseURL)).then(WebAssembly.compileStreaming);

super(getResources, wasmModules, instantiate);
}
}
21 changes: 21 additions & 0 deletions lib/api-node.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { readFile } from 'fs/promises';
import { BaseApplication } from './api-base.js';

export { Exit } from './api-base.js';

export class Application extends BaseApplication {
constructor(baseURL, resourceFilenames, wasmFilenames, instantiate) {
async function getResources() {
const resources = {};
for (const [dirName, jsonFilename] of Object.entries(resourceFilenames))
resources[dirName] = JSON.parse(await readFile(new URL(jsonFilename, baseURL)));
return resources;
}

const wasmModules = {};
for (const [modName, wasmFilename] of Object.entries(wasmFilenames))
wasmModules[modName] = readFile(new URL(wasmFilename, baseURL)).then(WebAssembly.compile);

super(getResources, wasmModules, instantiate);
}
}
Loading

0 comments on commit 1fc23db

Please sign in to comment.