From c765752be6f402f1d67c09026d7728dd9c670523 Mon Sep 17 00:00:00 2001 From: Anderson Arboleya Date: Wed, 26 Jul 2023 07:55:23 -0300 Subject: [PATCH 1/6] Initializing WASM synchronously --- .npm/.scripts/template/rollup.config.mjs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/.npm/.scripts/template/rollup.config.mjs b/.npm/.scripts/template/rollup.config.mjs index 972038619..abf9e8091 100644 --- a/.npm/.scripts/template/rollup.config.mjs +++ b/.npm/.scripts/template/rollup.config.mjs @@ -1,15 +1,17 @@ import { wasm } from '@rollup/plugin-wasm' import dts from 'rollup-plugin-dts' +const sync = ['src/{{NAME_UNDERSCORED}}_bg.wasm']; + export default [ { - plugins: [wasm({ targetEnv: 'auto-inline' })], + plugins: [wasm({ sync, targetEnv: 'auto-inline' })], input: 'src/index.js', output: [ { file: 'dist/node/index.cjs', format: 'cjs' }, ] }, { - plugins: [wasm({ targetEnv: 'auto-inline' })], + plugins: [wasm({ sync, targetEnv: 'auto-inline' })], input: 'src/index.js', output: [ { file: 'dist/web/index.mjs', format: 'es' }, From 930b0f7044620d0c65de675858fe75c928730d78 Mon Sep 17 00:00:00 2001 From: Anderson Arboleya Date: Wed, 26 Jul 2023 11:50:04 -0300 Subject: [PATCH 2/6] Adding changelog entry --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 92f136f8d..58d8aa596 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ### Fixed +- [#529](https://github.com/FuelLabs/fuel-vm/pull/529): Fix WASM initialization for NPM wrapper packages. + #### Breaking - [#527](https://github.com/FuelLabs/fuel-vm/pull/527): The balances are empty during predicate estimation/verification. From e2fb8b5df150abc3988a81a7487c9ea127704d72 Mon Sep 17 00:00:00 2001 From: Anderson Arboleya Date: Thu, 27 Jul 2023 14:40:24 -0300 Subject: [PATCH 3/6] Forcing async WASM initialization, updating all docs around it --- .npm/.scripts/template/README.md | 17 +++++++++++++++++ .npm/.scripts/template/src/index.js | 10 +++++++++- .npm/README.md | 27 +++++++++++++++++++++++++++ .npm/packages/fuel-asm/index.test.cjs | 14 +++++++++++++- .npm/packages/fuel-asm/index.test.mjs | 14 +++++++++++++- 5 files changed, 79 insertions(+), 3 deletions(-) diff --git a/.npm/.scripts/template/README.md b/.npm/.scripts/template/README.md index c27c0ecc4..81793cdf6 100644 --- a/.npm/.scripts/template/README.md +++ b/.npm/.scripts/template/README.md @@ -4,3 +4,20 @@ WASM version of `{{NAME_DASHED}}` Rust crate: - https://crates.io/crates/{{NAME_DASHED}} - https://github.com/FuelLabs/fuel-vm/tree/master/{{NAME_DASHED}} + + +# Getting Started + +Be sure to `await` the the WASM async initialization: + +```ts +import * as {{NAME_UNDERSCORED}} from '@fuels/vm-{{PKG_NAME}}' + +(async function() { + await {{NAME_UNDERSCORED}}.initWasm(); + + // {{NAME_UNDERSCORED}}.(); + // ... +})(); + +``` diff --git a/.npm/.scripts/template/src/index.js b/.npm/.scripts/template/src/index.js index 69496c741..a8e4b9f29 100644 --- a/.npm/.scripts/template/src/index.js +++ b/.npm/.scripts/template/src/index.js @@ -1,6 +1,14 @@ import init from './{{NAME_UNDERSCORED}}.js' import wasm from './{{NAME_UNDERSCORED}}_bg.wasm' -init(wasm()) +export async function initWasm () { + return await init(wasm()); +} + +/** + * calling it right away for pre-caching + * the wasm async initialization at startup + */ +initWasm(); export * from './{{NAME_UNDERSCORED}}.js' diff --git a/.npm/README.md b/.npm/README.md index a87083bbd..8ad5498f4 100644 --- a/.npm/README.md +++ b/.npm/README.md @@ -2,6 +2,33 @@ You'll find all the routines to publish selected Rust crates as NPM packages here. +# Usage + +The external usage of WASM packages requires them to be async. + +Don't forget to await for the WASM initialization: + +```ts +import * as asm from '@fuels/vm-asm' + +// alternative 1 +(async function() { + await asm.initWasm(); + + asm.movi(0x10, 0); + +})(); + +// alternative 2 +import * as asm from '@fuels/vm-asm' + +asm.initWasm().then(() => { + asm.movi(0x10, 0); +}) + +``` + + # Testing Locally To get started and test things locally, you'll need to: diff --git a/.npm/packages/fuel-asm/index.test.cjs b/.npm/packages/fuel-asm/index.test.cjs index 540a54464..e7857bfc0 100644 --- a/.npm/packages/fuel-asm/index.test.cjs +++ b/.npm/packages/fuel-asm/index.test.cjs @@ -1,9 +1,21 @@ const { expect } = require('chai') const asm = require('.') +/* +Top-level usage: + + asm.initWasm().then(() => { + const gtf = asm.gtf(0x10, 0x00, asm.GTFArgs.ScriptData) + // ... + }); + +*/ + describe('fuel-asm [cjs]', () => { - it('should compose simple script', () => { + it('should compose simple script', async () => { + + await asm.initWasm(); const gtf = asm.gtf(0x10, 0x00, asm.GTFArgs.ScriptData) const addi = asm.addi(0x11, 0x10, 0x20) diff --git a/.npm/packages/fuel-asm/index.test.mjs b/.npm/packages/fuel-asm/index.test.mjs index 9ae993690..6629f018c 100644 --- a/.npm/packages/fuel-asm/index.test.mjs +++ b/.npm/packages/fuel-asm/index.test.mjs @@ -1,9 +1,21 @@ import { expect } from 'chai' import * as asm from './dist/web/index.mjs' +/* +Top-level usage: + + asm.initWasm().then(() => { + const gtf = asm.gtf(0x10, 0x00, asm.GTFArgs.ScriptData) + // ... + }); + +*/ + describe('fuel-asm [esm]', () => { - it('should compose simple script', () => { + it('should compose simple script', async () => { + + await asm.initWasm(); const gtf = asm.gtf(0x10, 0x00, asm.GTFArgs.ScriptData) const addi = asm.addi(0x11, 0x10, 0x20) From f945362764bbb565358d11056f6d5f8560d9b430 Mon Sep 17 00:00:00 2001 From: Anderson Arboleya Date: Thu, 27 Jul 2023 14:44:15 -0300 Subject: [PATCH 4/6] Updating changelog --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 58d8aa596..23752c913 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,7 +16,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ### Fixed -- [#529](https://github.com/FuelLabs/fuel-vm/pull/529): Fix WASM initialization for NPM wrapper packages. +- [#529](https://github.com/FuelLabs/fuel-vm/pull/529) [#534](https://github.com/FuelLabs/fuel-vm/pull/534): Enforcing async WASM initialization for all NPM wrapper packages. #### Breaking From 30d18d7bdf5260bc25ecef846a510bfd21344e11 Mon Sep 17 00:00:00 2001 From: Anderson Arboleya Date: Thu, 27 Jul 2023 14:44:15 -0300 Subject: [PATCH 5/6] Updating changelog --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f7fd2c742..db8af2e7e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,7 +19,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ### Fixed -- [#529](https://github.com/FuelLabs/fuel-vm/pull/529): Fix WASM initialization for NPM wrapper packages. +- [#529](https://github.com/FuelLabs/fuel-vm/pull/529) [#534](https://github.com/FuelLabs/fuel-vm/pull/534): Enforcing async WASM initialization for all NPM wrapper packages. #### Breaking From 15c802fd5054973fde930d476ce647ce81d6a04a Mon Sep 17 00:00:00 2001 From: Anderson Arboleya Date: Fri, 28 Jul 2023 07:18:45 -0300 Subject: [PATCH 6/6] Tyop --- .npm/.scripts/template/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.npm/.scripts/template/README.md b/.npm/.scripts/template/README.md index 81793cdf6..bbbe7cb40 100644 --- a/.npm/.scripts/template/README.md +++ b/.npm/.scripts/template/README.md @@ -8,7 +8,7 @@ WASM version of `{{NAME_DASHED}}` Rust crate: # Getting Started -Be sure to `await` the the WASM async initialization: +Be sure to `await` the WASM async initialization: ```ts import * as {{NAME_UNDERSCORED}} from '@fuels/vm-{{PKG_NAME}}'