Skip to content

Commit

Permalink
Wasm support (#351)
Browse files Browse the repository at this point in the history
* Allow WASM compatibility

* Add wasm-test

* Fix

* fmt

* Fix build

* Update version

* Add WASM test harness

* fmt

* Add CI step

* typo

* Wrong block
  • Loading branch information
tjsharp1 authored Jun 7, 2022
1 parent 20f964a commit ed239e0
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 1 deletion.
18 changes: 18 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,24 @@ jobs:
with:
key: "${{ matrix.command }} ${{ matrix.args }} ${{ matrix.package }}"

- name: Add WASM target
if: ${{ matrix.command == 'test' }}
run: rustup target add wasm32-unknown-unknown

- name: Install WebAssembly Test harness
if: ${{ matrix.command == 'test' }}
uses: actions-rs/cargo@v1
with:
command: install
args: webassembly-test-runner

- name: Test WASM package
if: ${{ matrix.command == 'test' }}
run: |
cd packages/wasm-tests
cargo test --target wasm32-unknown-unknown --all-targets --all-features
cargo test --target wasm32-unknown-unknown --all-targets --no-default-features
- name: Install rustfmt
if: ${{ matrix.command == 'fmt' }}
run: rustup component add rustfmt
Expand Down
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ members = [
"packages/fuels-signers",
"packages/fuels-test-helpers",
"packages/fuels-types",
"packages/wasm-tests",
"tools/fuels-abi-cli",
"scripts/build-test-projects",
"examples/contracts",
Expand Down
3 changes: 2 additions & 1 deletion packages/fuels-core/src/code_gen/abigen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,16 +109,17 @@ impl Abigen {
(
quote! {
use alloc::{vec, vec::Vec};
use fuels_core::{Detokenize, EnumSelector, InvalidOutputType, ParamType, Tokenizable, Token};
},
quote! {},
)
} else {
(
quote! {
use fuels::contract::contract::{Contract, ContractCall};
use fuels::prelude::InvalidOutputType;
use fuels::signers::LocalWallet;
use fuels::tx::{ContractId, Address};
use fuels::core::{Detokenize, EnumSelector, InvalidOutputType, ParamType, Tokenizable, Token};
use std::str::FromStr;
use fuels::core::{Detokenize, EnumSelector, ParamType, Tokenizable, Token, Parameterize};
},
Expand Down
5 changes: 5 additions & 0 deletions packages/wasm-tests/.cargo/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[build]
target = "wasm32-unknown-unknown"

[target.wasm32-unknown-unknown]
runner = "webassembly-test-runner"
16 changes: 16 additions & 0 deletions packages/wasm-tests/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[package]
name = "wasm-tests"
version = "0.0.0"
edition = "2021"
publish = false

[lib]
crate-type = ['cdylib']

[dependencies]
fuels-abigen-macro = { path = "../fuels-abigen-macro" }
fuels-core = { path = "../fuels-core" }
getrandom = { version = "0.2", features = ["js"] }

[dev-dependencies]
webassembly-test = "0.1"
78 changes: 78 additions & 0 deletions packages/wasm-tests/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
extern crate alloc;
use fuels_abigen_macro::wasm_abigen;

wasm_abigen!(
no_name,
r#"[
{
"type":"contract",
"inputs":[
{
"name":"SomeEvent",
"type":"struct SomeEvent",
"components": [
{
"name": "id",
"type": "u64"
},
{
"name": "account",
"type": "b256"
}
]
},
{
"name":"AnotherEvent",
"type":"struct AnotherEvent",
"components": [
{
"name": "id",
"type": "u64"
},
{
"name": "hash",
"type": "b256"
},
{
"name": "bar",
"type": "bool"
}
]
}
],
"name":"takes_struct",
"outputs":[]
}
]
"#
);

pub fn the_fn() {
use fuels_core::{abi_decoder::ABIDecoder, ParamType};
let data = vec![
0, 0, 0, 0, 0, 0, 3, 252, 175, 175, 175, 175, 175, 175, 175, 175, 175, 175, 175, 175, 175,
175, 175, 175, 175, 175, 175, 175, 175, 175, 175, 175, 175, 175, 175, 175, 175, 175, 175,
175,
];

let mut decoder = ABIDecoder::new();

let obj = decoder
.decode(&[ParamType::U64, ParamType::B256], &data)
.expect("Failed to decode");

let a_struct = SomeEvent::new_from_tokens(&obj);

assert_eq!(1020, a_struct.id);
}

#[cfg(test)]
mod tests {
use super::*;
use webassembly_test::webassembly_test;

#[webassembly_test]
fn test() {
the_fn();
}
}

0 comments on commit ed239e0

Please sign in to comment.