From 1421ba5a9b99e456ee79dbafdf631dfb2a12e661 Mon Sep 17 00:00:00 2001 From: Ashwin Phatak Date: Mon, 20 Sep 2021 16:16:29 +0530 Subject: [PATCH] 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 --- packages/graph-node/.eslintrc.json | 5 ++++- packages/graph-node/.mocharc.yml | 1 + packages/graph-node/asconfig.json | 20 ++++++++++++++++++++ packages/graph-node/assembly/index.ts | 5 +++++ packages/graph-node/assembly/tsconfig.json | 6 ++++++ packages/graph-node/package.json | 14 ++++++++++++-- packages/graph-node/src/index.test.ts | 17 +++++++++++++++++ packages/graph-node/src/index.ts | 10 ++++++++++ yarn.lock | 18 ++++++++++++++++++ 9 files changed, 93 insertions(+), 3 deletions(-) create mode 100644 packages/graph-node/.mocharc.yml create mode 100644 packages/graph-node/asconfig.json create mode 100644 packages/graph-node/assembly/index.ts create mode 100644 packages/graph-node/assembly/tsconfig.json create mode 100644 packages/graph-node/src/index.test.ts diff --git a/packages/graph-node/.eslintrc.json b/packages/graph-node/.eslintrc.json index acccd0d2..aa470196 100644 --- a/packages/graph-node/.eslintrc.json +++ b/packages/graph-node/.eslintrc.json @@ -14,5 +14,8 @@ }, "plugins": [ "@typescript-eslint" - ] + ], + "rules": { + "@typescript-eslint/no-explicit-any": "off" + } } diff --git a/packages/graph-node/.mocharc.yml b/packages/graph-node/.mocharc.yml new file mode 100644 index 00000000..45bd4bdc --- /dev/null +++ b/packages/graph-node/.mocharc.yml @@ -0,0 +1 @@ +require: 'ts-node/register' diff --git a/packages/graph-node/asconfig.json b/packages/graph-node/asconfig.json new file mode 100644 index 00000000..30171789 --- /dev/null +++ b/packages/graph-node/asconfig.json @@ -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": {} +} diff --git a/packages/graph-node/assembly/index.ts b/packages/graph-node/assembly/index.ts new file mode 100644 index 00000000..a866864c --- /dev/null +++ b/packages/graph-node/assembly/index.ts @@ -0,0 +1,5 @@ +// The entry file of your WebAssembly module. + +export function add (a: i32, b: i32): i32 { + return a + b; +} diff --git a/packages/graph-node/assembly/tsconfig.json b/packages/graph-node/assembly/tsconfig.json new file mode 100644 index 00000000..891cc238 --- /dev/null +++ b/packages/graph-node/assembly/tsconfig.json @@ -0,0 +1,6 @@ +{ + "extends": "assemblyscript/std/assembly.json", + "include": [ + "./**/*.ts" + ] +} diff --git a/packages/graph-node/package.json b/packages/graph-node/package.json index c4131aab..3ea694f6 100644 --- a/packages/graph-node/package.json +++ b/packages/graph-node/package.json @@ -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" } } diff --git a/packages/graph-node/src/index.test.ts b/packages/graph-node/src/index.test.ts new file mode 100644 index 00000000..5f6ecd75 --- /dev/null +++ b/packages/graph-node/src/index.test.ts @@ -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); +}); diff --git a/packages/graph-node/src/index.ts b/packages/graph-node/src/index.ts index c4562172..86f2ae4d 100644 --- a/packages/graph-node/src/index.ts +++ b/packages/graph-node/src/index.ts @@ -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 => { + const buffer = await fs.readFile(filePath); + return loader.instantiate(buffer, imports); +}; diff --git a/yarn.lock b/yarn.lock index 41b486ea..5ef294db 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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"