From ec9bceeabe1b4a3a8e25487721d0f4ab6e31ea14 Mon Sep 17 00:00:00 2001 From: Peter Salomonsen Date: Mon, 16 Oct 2023 18:35:55 +0200 Subject: [PATCH] purejs test --- .github/workflows/main.yml | 16 +++++++++++++++- examples/purejs/build.sh | 5 ++++- examples/purejs/callpurejswasm.js | 6 +++--- examples/purejs/localjstest/purejs.spec.js | 13 +++++++++++++ examples/purejs/meta-dce.json | 12 ++++++++++++ examples/purejs/test.sh | 2 -- package.json | 1 + 7 files changed, 48 insertions(+), 7 deletions(-) create mode 100644 examples/purejs/localjstest/purejs.spec.js create mode 100644 examples/purejs/meta-dce.json delete mode 100755 examples/purejs/test.sh diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 927f07b..f7b3fcb 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -61,7 +61,7 @@ jobs: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - - name: NFT example End to End tests + - name: NFT example local JS test run: | set -e curl -L https://github.com/WebAssembly/binaryen/releases/download/version_111/binaryen-version_111-x86_64-linux.tar.gz | tar xzf - @@ -71,6 +71,20 @@ jobs: cd examples/nft mkdir out yarn test-examples-nft-localjs + localjs-test-example-purejs: + name: PureJS example local JS test + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - name: PureJS example local JS test + run: | + set -e + curl -L https://github.com/WebAssembly/binaryen/releases/download/version_111/binaryen-version_111-x86_64-linux.tar.gz | tar xzf - + export PATH="`pwd`/binaryen-version_111/bin:$PATH" + rustup target add wasm32-unknown-unknown + yarn install + cd examples/purejs + yarn test-purejs webapptests: name: Web app tests runs-on: ubuntu-latest diff --git a/examples/purejs/build.sh b/examples/purejs/build.sh index f9befa2..677973f 100755 --- a/examples/purejs/build.sh +++ b/examples/purejs/build.sh @@ -7,4 +7,7 @@ WASM_FILENAME=../../target/wasm32-unknown-unknown/release/quickjs_rust_near_pure wasm2wat $WASM_FILENAME > purejs.wat OBJDUMP_DATA_SECTION=`wasm-objdump -h $WASM_FILENAME | grep "Data start"` node ./manipulatepurejswat.js "$OBJDUMP_DATA_SECTION" -wat2wasm purejs.wat \ No newline at end of file +wat2wasm purejs.wat +wasm-metadce -f meta-dce.json purejs.wasm -o purejs.wasm +# Optimize the Wasm binary +wasm-opt -Oz --signext-lowering purejs.wasm -o purejs.wasm \ No newline at end of file diff --git a/examples/purejs/callpurejswasm.js b/examples/purejs/callpurejswasm.js index 091c2a4..4475507 100644 --- a/examples/purejs/callpurejswasm.js +++ b/examples/purejs/callpurejswasm.js @@ -1,11 +1,11 @@ import { readFile } from 'fs/promises'; import { getContractInstanceExports } from '../../localjstestenv/contract-runner.js'; - + const { exports, nearenv } = await getContractInstanceExports(await readFile('./purejs.wasm')); -nearenv.set_args({ name: 'peter' }); +nearenv.set_args({ name: 'peter' }); exports.hello(); console.log(nearenv.latest_return_value); -nearenv.set_args({ a: 22,b: 23 }); +nearenv.set_args({ a: 22, b: 23 }); exports.add(); console.log(nearenv.latest_return_value); \ No newline at end of file diff --git a/examples/purejs/localjstest/purejs.spec.js b/examples/purejs/localjstest/purejs.spec.js new file mode 100644 index 0000000..908aba0 --- /dev/null +++ b/examples/purejs/localjstest/purejs.spec.js @@ -0,0 +1,13 @@ +import { readFile } from "fs/promises"; +import { getContractInstanceExports } from "../../../localjstestenv/contract-runner.js"; + +test('should find exported javascript methods as contract methods and run them', async () => { + const { exports, nearenv } = await getContractInstanceExports(await readFile('./purejs.wasm')); + nearenv.set_args({ name: 'peter' }); + exports.hello(); + expect(nearenv.latest_return_value).toEqual('hello peter'); + + nearenv.set_args({ a: 22, b: 23 }); + exports.add(); + expect(JSON.parse(nearenv.latest_return_value).result).toEqual(45); +}); \ No newline at end of file diff --git a/examples/purejs/meta-dce.json b/examples/purejs/meta-dce.json new file mode 100644 index 0000000..48543b7 --- /dev/null +++ b/examples/purejs/meta-dce.json @@ -0,0 +1,12 @@ +[ + { + "name": "hello", + "export": "hello", + "root": true + }, + { + "name": "add", + "export": "add", + "root": true + } +] \ No newline at end of file diff --git a/examples/purejs/test.sh b/examples/purejs/test.sh deleted file mode 100755 index a0e1a7c..0000000 --- a/examples/purejs/test.sh +++ /dev/null @@ -1,2 +0,0 @@ -#!/bin/bash -WASMTIME_BACKTRACE_DETAILS=1 RUSTFLAGS='-C link-args=--initial-memory=67108864' cargo wasi test -- --show-output --nocapture \ No newline at end of file diff --git a/package.json b/package.json index f1e384f..e911ffe 100644 --- a/package.json +++ b/package.json @@ -10,6 +10,7 @@ "test-web": "NODE_OPTIONS=--experimental-vm-modules jest web4", "test-examples-nft-e2e": "yarn examples-nft-web4bundle && cd examples/nft && NODE_OPTIONS=--experimental-vm-modules jest examples/nft/e2e", "test-examples-nft-localjs": "cd examples/nft && ./buildanddeploy.sh --only-build && NODE_OPTIONS=--experimental-vm-modules jest examples/nft/localjstest", + "test-purejs": "cd examples/purejs && ./build.sh && NODE_OPTIONS=--experimental-vm-modules jest examples/purejs/localjstest", "examples-nft-web4bundle": "cd examples/nft/web4 && rollup -c rollup.config.js", "serve-examples-nft": "http-server -p 8085 examples/nft/web4" },