Skip to content

Commit

Permalink
purejs test
Browse files Browse the repository at this point in the history
  • Loading branch information
petersalomonsen committed Oct 16, 2023
1 parent db7cb18 commit ec9bcee
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 7 deletions.
16 changes: 15 additions & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 -
Expand All @@ -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
Expand Down
5 changes: 4 additions & 1 deletion examples/purejs/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
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
6 changes: 3 additions & 3 deletions examples/purejs/callpurejswasm.js
Original file line number Diff line number Diff line change
@@ -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);
13 changes: 13 additions & 0 deletions examples/purejs/localjstest/purejs.spec.js
Original file line number Diff line number Diff line change
@@ -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);
});
12 changes: 12 additions & 0 deletions examples/purejs/meta-dce.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[
{
"name": "hello",
"export": "hello",
"root": true
},
{
"name": "add",
"export": "add",
"root": true
}
]
2 changes: 0 additions & 2 deletions examples/purejs/test.sh

This file was deleted.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down

0 comments on commit ec9bcee

Please sign in to comment.