diff --git a/.npm/.scripts/template/README.md b/.npm/.scripts/template/README.md index c27c0ecc44..bbbe7cb404 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 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 69496c741c..a8e4b9f296 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 a87083bbda..8ad5498f41 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 540a544644..e7857bfc0c 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 9ae9936900..6629f018cb 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) diff --git a/CHANGELOG.md b/CHANGELOG.md index 447af40d1b..452fbb3625 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,7 +20,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