Skip to content

Commit

Permalink
Setup wat compile
Browse files Browse the repository at this point in the history
  • Loading branch information
lahmatiy committed Mar 20, 2024
1 parent ac59c25 commit 27e8be5
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 5 deletions.
20 changes: 19 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@
"scripts": {
"lint": "eslint src",
"start": "discovery",
"build": "npm run build-app && npm run build-report-template",
"build-wasm": "wat2wasm app/prepare/build-trees.wat -o app/prepare/build-trees.wasm",
"build": "npm run build-wasm && npm run build-app && npm run build-report-template",
"build-wasm": "node scripts/wat-compile.js",
"build-app": "discovery-build --clean --config .discoveryrc.app.js --single-file --no-data --entry-names \"app\"",
"build-report-template": "discovery-build --single-file --no-data --no-model-data-upload --entry-names \"report\"",
"build-gh-pages": "discovery-build --config .discoveryrc.app.js --single-file --no-data -o .gh-pages",
"build-gh-pages": "npm run build-wasm && discovery-build --config .discoveryrc.app.js --single-file --no-data -o .gh-pages",
"prepublishOnly": "npm run build"
},
"dependencies": {
Expand All @@ -51,6 +51,7 @@
"@discoveryjs/discovery": "^1.0.0-beta.81",
"@typescript-eslint/eslint-plugin": "^5.21.0",
"@typescript-eslint/parser": "^5.21.0",
"eslint": "^8.7.0"
"eslint": "^8.7.0",
"wabt": "^1.0.32"
}
}
37 changes: 37 additions & 0 deletions scripts/wat-compile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/* eslint-env node */
const path = require('path');
const fs = require('fs');
const wabtPromise = require('wabt')();

const files = [
'../app/prepare/build-trees.wat'
].map(relpath => path.join(__dirname, relpath));

async function compileWatToWasm() {
console.log('Compile WAT into WASM');
console.log();

const wabt = await wabtPromise;

for (let filepath of files) {
process.stdout.write(path.relative(process.cwd(), filepath) + '...');

const destPath = filepath.replace(/\.wat$/, '.wasm');
const watFileContent = fs.readFileSync(filepath, 'utf8');
const wasmModule = wabt.parseWat(filepath, watFileContent);
const { buffer } = wasmModule.toBinary({});

fs.writeFileSync(destPath, buffer);

console.log('OK');
console.log(' Written to', path.relative(process.cwd(), destPath));
}
}

if (module === require.main) {
compileWatToWasm();
}

module.exports = {
compileWatToWasm
};

0 comments on commit 27e8be5

Please sign in to comment.