WASM instance setup (#11)

* Code for running wasm file.

* Add test for wasm exported function.

* Use target names for build files.

Co-authored-by: nabarun <nabarun@deepstacksoft.com>
This commit is contained in:
Ashwin Phatak
2021-12-28 16:08:04 +05:30
committed by nabarun
co-authored by nabarun
parent 987457992a
commit 1421ba5a9b
9 changed files with 93 additions and 3 deletions
+4 -1
View File
@@ -14,5 +14,8 @@
},
"plugins": [
"@typescript-eslint"
]
],
"rules": {
"@typescript-eslint/no-explicit-any": "off"
}
}
+1
View File
@@ -0,0 +1 @@
require: 'ts-node/register'
+20
View File
@@ -0,0 +1,20 @@
{
"targets": {
"debug": {
"binaryFile": "build/debug.wasm",
"textFile": "build/debug.wat",
"sourceMap": true,
"debug": true
},
"release": {
"binaryFile": "build/release.wasm",
"textFile": "build/release.wat",
"sourceMap": true,
"optimizeLevel": 3,
"shrinkLevel": 0,
"converge": false,
"noAssert": false
}
},
"options": {}
}
+5
View File
@@ -0,0 +1,5 @@
// The entry file of your WebAssembly module.
export function add (a: i32, b: i32): i32 {
return a + b;
}
@@ -0,0 +1,6 @@
{
"extends": "assemblyscript/std/assembly.json",
"include": [
"./**/*.ts"
]
}
+12 -2
View File
@@ -6,6 +6,7 @@
"devDependencies": {
"@typescript-eslint/eslint-plugin": "^4.25.0",
"@typescript-eslint/parser": "^4.25.0",
"assemblyscript": "^0.19.16",
"eslint": "^7.27.0",
"eslint-config-semistandard": "^15.0.1",
"eslint-config-standard": "^16.0.3",
@@ -13,10 +14,19 @@
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-promise": "^5.1.0",
"eslint-plugin-standard": "^5.0.0",
"typescript": "^4.3.2"
"typescript": "^4.3.2",
"nodemon": "^2.0.7",
"ts-node": "^10.0.0"
},
"scripts": {
"lint": "eslint .",
"build": "tsc"
"build": "tsc",
"asbuild:debug": "asc assembly/index.ts --target debug",
"asbuild:release": "asc assembly/index.ts --target release",
"asbuild": "yarn asbuild:debug && yarn asbuild:release",
"test": "mocha src/**/*.test.ts"
},
"dependencies": {
"@assemblyscript/loader": "^0.19.16"
}
}
+17
View File
@@ -0,0 +1,17 @@
//
// Copyright 2021 Vulcanize, Inc.
//
import { expect } from 'chai';
import path from 'path';
import 'mocha';
import { getExports } from './index';
const WASM_FILE_PATH = '../build/debug.wasm';
it('should execute exported function', async () => {
const filePath = path.resolve(__dirname, WASM_FILE_PATH);
const { exports } = await getExports(filePath);
expect(exports.add(1, 2)).to.equal(3);
});
+10
View File
@@ -1,3 +1,13 @@
//
// Copyright 2021 Vulcanize, Inc.
//
import fs from 'fs/promises';
import loader from '@assemblyscript/loader';
const imports = { /* imports go here */ };
export const getExports = async (filePath: string): Promise<loader.ResultObject & { exports: any }> => {
const buffer = await fs.readFile(filePath);
return loader.instantiate(buffer, imports);
};