mirror of
https://github.com/cerc-io/watcher-ts
synced 2026-09-07 16:34:06 +00:00
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:
@@ -14,5 +14,8 @@
|
||||
},
|
||||
"plugins": [
|
||||
"@typescript-eslint"
|
||||
]
|
||||
],
|
||||
"rules": {
|
||||
"@typescript-eslint/no-explicit-any": "off"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
require: 'ts-node/register'
|
||||
@@ -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": {}
|
||||
}
|
||||
@@ -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"
|
||||
]
|
||||
}
|
||||
@@ -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"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
});
|
||||
@@ -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);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user