Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Forcing async wasm initialization #534

Merged
merged 8 commits into from
Aug 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions .npm/.scripts/template/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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}}.<?>();
// ...
})();

```
10 changes: 9 additions & 1 deletion .npm/.scripts/template/src/index.js
Original file line number Diff line number Diff line change
@@ -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'
27 changes: 27 additions & 0 deletions .npm/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
14 changes: 13 additions & 1 deletion .npm/packages/fuel-asm/index.test.cjs
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
14 changes: 13 additions & 1 deletion .npm/packages/fuel-asm/index.test.mjs
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Loading