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-09-20 16:16:29 +05:30 committed by nabarun
parent 987457992a
commit 1421ba5a9b
9 changed files with 93 additions and 3 deletions

View File

@ -14,5 +14,8 @@
},
"plugins": [
"@typescript-eslint"
]
],
"rules": {
"@typescript-eslint/no-explicit-any": "off"
}
}

View File

@ -0,0 +1 @@
require: 'ts-node/register'

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": {}
}

View File

@ -0,0 +1,5 @@
// The entry file of your WebAssembly module.
export function add (a: i32, b: i32): i32 {
return a + b;
}

View File

@ -0,0 +1,6 @@
{
"extends": "assemblyscript/std/assembly.json",
"include": [
"./**/*.ts"
]
}

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"
}
}

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);
});

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);
};

View File

@ -65,6 +65,11 @@
http-errors "^1.7.3"
object-path "^0.11.4"
"@assemblyscript/loader@^0.19.16":
version "0.19.16"
resolved "https://registry.yarnpkg.com/@assemblyscript/loader/-/loader-0.19.16.tgz#481063138724deef314dc0930714eebf39117683"
integrity sha512-Skp0eLY3oP2YfAxaHq4IpsUZQOpllkBB0dNDrhck42mGQTimAJ6KegXMuFVa9PIP6gw3bDlCs2iIegfGnXiuFg==
"@babel/code-frame@7.12.11":
version "7.12.11"
resolved "https://registry.yarnpkg.com/@babel/code-frame/-/code-frame-7.12.11.tgz#f4ad435aa263db935b8f10f2c552d23fb716a63f"
@ -3460,6 +3465,14 @@ asn1@~0.2.3:
dependencies:
safer-buffer "~2.1.0"
assemblyscript@^0.19.16:
version "0.19.16"
resolved "https://registry.yarnpkg.com/assemblyscript/-/assemblyscript-0.19.16.tgz#fc06c9892755775e8e31a59249fbc361fd49e1d1"
integrity sha512-AMNdwcat+EEsxjkVQ5vOE/lDbXBvy1swQKAuMG2Ken+DZufZH7wKHIAVKR5liteW/jLL3T971l1MN+onP/bixA==
dependencies:
binaryen "101.0.0-nightly.20210904"
long "^4.0.0"
assert-plus@1.0.0, assert-plus@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/assert-plus/-/assert-plus-1.0.0.tgz#f12e0f3c5d77b0b1cdd9146942e4e96c1e4dd525"
@ -4148,6 +4161,11 @@ bindings@^1.2.1:
dependencies:
file-uri-to-path "1.0.0"
binaryen@101.0.0-nightly.20210904:
version "101.0.0-nightly.20210904"
resolved "https://registry.yarnpkg.com/binaryen/-/binaryen-101.0.0-nightly.20210904.tgz#58a7990d6d64b16567f376a1fe47d8aea6698b14"
integrity sha512-2AvJhErttuoMvgNcYPPpPy7C12PSvDdtZWtEeX/Otm/Vtf4ePvBpT3UIA00hGAh8HNaGr+dzFNstxTUvjNwZTg==
bip39@2.5.0:
version "2.5.0"
resolved "https://registry.yarnpkg.com/bip39/-/bip39-2.5.0.tgz#51cbd5179460504a63ea3c000db3f787ca051235"