From 4e0c0e139dc59cf02807b0947fe435f2a2f198b2 Mon Sep 17 00:00:00 2001 From: Vlad Chernenko Date: Fri, 10 Mar 2023 01:52:24 +0200 Subject: [PATCH] Added tests with async KLY-VM --- .../test-1-simple-object/.appveyor.yml | 11 ++ AsyncVMTests/test-1-simple-object/.cargo-ok | 0 AsyncVMTests/test-1-simple-object/.gitignore | 8 ++ AsyncVMTests/test-1-simple-object/.travis.yml | 69 +++++++++++ AsyncVMTests/test-1-simple-object/Cargo.toml | 35 ++++++ AsyncVMTests/test-1-simple-object/README.md | 86 ++++++++++++++ AsyncVMTests/test-1-simple-object/src/lib.rs | 55 +++++++++ AsyncVMTests/test-2-async-func/.appveyor.yml | 11 ++ AsyncVMTests/test-2-async-func/.cargo-ok | 0 AsyncVMTests/test-2-async-func/.gitignore | 8 ++ AsyncVMTests/test-2-async-func/.travis.yml | 69 +++++++++++ AsyncVMTests/test-2-async-func/Cargo.toml | 39 +++++++ AsyncVMTests/test-2-async-func/README.md | 86 ++++++++++++++ AsyncVMTests/test-2-async-func/js/foo.js | 9 ++ AsyncVMTests/test-2-async-func/src/lib.rs | 64 +++++++++++ AsyncVMTests/test-3-to-get-base/.appveyor.yml | 11 ++ AsyncVMTests/test-3-to-get-base/.cargo-ok | 0 AsyncVMTests/test-3-to-get-base/.gitignore | 8 ++ AsyncVMTests/test-3-to-get-base/.travis.yml | 69 +++++++++++ AsyncVMTests/test-3-to-get-base/Cargo.toml | 39 +++++++ AsyncVMTests/test-3-to-get-base/README.md | 86 ++++++++++++++ AsyncVMTests/test-3-to-get-base/js/foo.js | 9 ++ AsyncVMTests/test-3-to-get-base/src/lib.rs | 79 +++++++++++++ .../test-4-no-async-imports/.appveyor.yml | 11 ++ .../test-4-no-async-imports/.cargo-ok | 0 .../test-4-no-async-imports/.gitignore | 8 ++ .../test-4-no-async-imports/.travis.yml | 69 +++++++++++ .../test-4-no-async-imports/Cargo.toml | 39 +++++++ .../test-4-no-async-imports/README.md | 86 ++++++++++++++ .../test-4-no-async-imports/src/lib.rs | 75 ++++++++++++ .../test-4-no-async-imports/src/utils.rs | 10 ++ .../test-5-async-without-await/.appveyor.yml | 11 ++ .../test-5-async-without-await/.cargo-ok | 0 .../test-5-async-without-await/.gitignore | 8 ++ .../test-5-async-without-await/.travis.yml | 69 +++++++++++ .../test-5-async-without-await/Cargo.toml | 39 +++++++ .../test-5-async-without-await/README.md | 86 ++++++++++++++ .../test-5-async-without-await/src/lib.rs | 98 ++++++++++++++++ rustBase.js | 108 ++++++++++++++++++ tests/package.json | 13 +++ tests/use_1.js | 7 ++ tests/use_2.js | 9 ++ tests/use_5.js | 17 +++ tests/use_cusom.js | 24 ++++ 44 files changed, 1638 insertions(+) create mode 100644 AsyncVMTests/test-1-simple-object/.appveyor.yml create mode 100644 AsyncVMTests/test-1-simple-object/.cargo-ok create mode 100644 AsyncVMTests/test-1-simple-object/.gitignore create mode 100644 AsyncVMTests/test-1-simple-object/.travis.yml create mode 100644 AsyncVMTests/test-1-simple-object/Cargo.toml create mode 100644 AsyncVMTests/test-1-simple-object/README.md create mode 100644 AsyncVMTests/test-1-simple-object/src/lib.rs create mode 100644 AsyncVMTests/test-2-async-func/.appveyor.yml create mode 100644 AsyncVMTests/test-2-async-func/.cargo-ok create mode 100644 AsyncVMTests/test-2-async-func/.gitignore create mode 100644 AsyncVMTests/test-2-async-func/.travis.yml create mode 100644 AsyncVMTests/test-2-async-func/Cargo.toml create mode 100644 AsyncVMTests/test-2-async-func/README.md create mode 100644 AsyncVMTests/test-2-async-func/js/foo.js create mode 100644 AsyncVMTests/test-2-async-func/src/lib.rs create mode 100644 AsyncVMTests/test-3-to-get-base/.appveyor.yml create mode 100644 AsyncVMTests/test-3-to-get-base/.cargo-ok create mode 100644 AsyncVMTests/test-3-to-get-base/.gitignore create mode 100644 AsyncVMTests/test-3-to-get-base/.travis.yml create mode 100644 AsyncVMTests/test-3-to-get-base/Cargo.toml create mode 100644 AsyncVMTests/test-3-to-get-base/README.md create mode 100644 AsyncVMTests/test-3-to-get-base/js/foo.js create mode 100644 AsyncVMTests/test-3-to-get-base/src/lib.rs create mode 100644 AsyncVMTests/test-4-no-async-imports/.appveyor.yml create mode 100644 AsyncVMTests/test-4-no-async-imports/.cargo-ok create mode 100644 AsyncVMTests/test-4-no-async-imports/.gitignore create mode 100644 AsyncVMTests/test-4-no-async-imports/.travis.yml create mode 100644 AsyncVMTests/test-4-no-async-imports/Cargo.toml create mode 100644 AsyncVMTests/test-4-no-async-imports/README.md create mode 100644 AsyncVMTests/test-4-no-async-imports/src/lib.rs create mode 100644 AsyncVMTests/test-4-no-async-imports/src/utils.rs create mode 100644 AsyncVMTests/test-5-async-without-await/.appveyor.yml create mode 100644 AsyncVMTests/test-5-async-without-await/.cargo-ok create mode 100644 AsyncVMTests/test-5-async-without-await/.gitignore create mode 100644 AsyncVMTests/test-5-async-without-await/.travis.yml create mode 100644 AsyncVMTests/test-5-async-without-await/Cargo.toml create mode 100644 AsyncVMTests/test-5-async-without-await/README.md create mode 100644 AsyncVMTests/test-5-async-without-await/src/lib.rs create mode 100644 rustBase.js create mode 100644 tests/package.json create mode 100644 tests/use_1.js create mode 100644 tests/use_2.js create mode 100644 tests/use_5.js create mode 100644 tests/use_cusom.js diff --git a/AsyncVMTests/test-1-simple-object/.appveyor.yml b/AsyncVMTests/test-1-simple-object/.appveyor.yml new file mode 100644 index 0000000..50910bd --- /dev/null +++ b/AsyncVMTests/test-1-simple-object/.appveyor.yml @@ -0,0 +1,11 @@ +install: + - appveyor-retry appveyor DownloadFile https://win.rustup.rs/ -FileName rustup-init.exe + - if not defined RUSTFLAGS rustup-init.exe -y --default-host x86_64-pc-windows-msvc --default-toolchain nightly + - set PATH=%PATH%;C:\Users\appveyor\.cargo\bin + - rustc -V + - cargo -V + +build: false + +test_script: + - cargo test --locked diff --git a/AsyncVMTests/test-1-simple-object/.cargo-ok b/AsyncVMTests/test-1-simple-object/.cargo-ok new file mode 100644 index 0000000..e69de29 diff --git a/AsyncVMTests/test-1-simple-object/.gitignore b/AsyncVMTests/test-1-simple-object/.gitignore new file mode 100644 index 0000000..d558542 --- /dev/null +++ b/AsyncVMTests/test-1-simple-object/.gitignore @@ -0,0 +1,8 @@ +/target +**/*.rs.bk +Cargo.lock +bin/ +pkg/ +wasm-pack.log +tests +LICENSE* \ No newline at end of file diff --git a/AsyncVMTests/test-1-simple-object/.travis.yml b/AsyncVMTests/test-1-simple-object/.travis.yml new file mode 100644 index 0000000..7a91325 --- /dev/null +++ b/AsyncVMTests/test-1-simple-object/.travis.yml @@ -0,0 +1,69 @@ +language: rust +sudo: false + +cache: cargo + +matrix: + include: + + # Builds with wasm-pack. + - rust: beta + env: RUST_BACKTRACE=1 + addons: + firefox: latest + chrome: stable + before_script: + - (test -x $HOME/.cargo/bin/cargo-install-update || cargo install cargo-update) + - (test -x $HOME/.cargo/bin/cargo-generate || cargo install --vers "^0.2" cargo-generate) + - cargo install-update -a + - curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh -s -- -f + script: + - cargo generate --git . --name testing + # Having a broken Cargo.toml (in that it has curlies in fields) anywhere + # in any of our parent dirs is problematic. + - mv Cargo.toml Cargo.toml.tmpl + - cd testing + - wasm-pack build + - wasm-pack test --chrome --firefox --headless + + # Builds on nightly. + - rust: nightly + env: RUST_BACKTRACE=1 + before_script: + - (test -x $HOME/.cargo/bin/cargo-install-update || cargo install cargo-update) + - (test -x $HOME/.cargo/bin/cargo-generate || cargo install --vers "^0.2" cargo-generate) + - cargo install-update -a + - rustup target add wasm32-unknown-unknown + script: + - cargo generate --git . --name testing + - mv Cargo.toml Cargo.toml.tmpl + - cd testing + - cargo check + - cargo check --target wasm32-unknown-unknown + - cargo check --no-default-features + - cargo check --target wasm32-unknown-unknown --no-default-features + - cargo check --no-default-features --features console_error_panic_hook + - cargo check --target wasm32-unknown-unknown --no-default-features --features console_error_panic_hook + - cargo check --no-default-features --features "console_error_panic_hook wee_alloc" + - cargo check --target wasm32-unknown-unknown --no-default-features --features "console_error_panic_hook wee_alloc" + + # Builds on beta. + - rust: beta + env: RUST_BACKTRACE=1 + before_script: + - (test -x $HOME/.cargo/bin/cargo-install-update || cargo install cargo-update) + - (test -x $HOME/.cargo/bin/cargo-generate || cargo install --vers "^0.2" cargo-generate) + - cargo install-update -a + - rustup target add wasm32-unknown-unknown + script: + - cargo generate --git . --name testing + - mv Cargo.toml Cargo.toml.tmpl + - cd testing + - cargo check + - cargo check --target wasm32-unknown-unknown + - cargo check --no-default-features + - cargo check --target wasm32-unknown-unknown --no-default-features + - cargo check --no-default-features --features console_error_panic_hook + - cargo check --target wasm32-unknown-unknown --no-default-features --features console_error_panic_hook + # Note: no enabling the `wee_alloc` feature here because it requires + # nightly for now. diff --git a/AsyncVMTests/test-1-simple-object/Cargo.toml b/AsyncVMTests/test-1-simple-object/Cargo.toml new file mode 100644 index 0000000..f4f7618 --- /dev/null +++ b/AsyncVMTests/test-1-simple-object/Cargo.toml @@ -0,0 +1,35 @@ +[package] +name = "test-1-simple-object" +version = "0.1.0" +authors = ["Vlad Chernenko "] +edition = "2018" + +[lib] +crate-type = ["cdylib"] + +[features] +default = ["console_error_panic_hook"] + +[dependencies] +gloo-utils = { version = "0.1", features = ["serde"] } +serde = { version = "1.0", features = ["derive"] } +wasm-bindgen = "0.2.84" + +# The `console_error_panic_hook` crate provides better debugging of panics by +# logging them with `console.error`. This is great for development, but requires +# all the `std::fmt` and `std::panicking` infrastructure, so isn't great for +# code size when deploying. +console_error_panic_hook = { version = "0.1.6", optional = true } + +# `wee_alloc` is a tiny allocator for wasm that is only ~1K in code size +# compared to the default allocator's ~10K. It is slower than the default +# allocator, however. +wee_alloc = { version = "0.4.5", optional = true } + +[dev-dependencies] +wasm-bindgen-test = "0.3.13" + +[profile.release] +# Tell `rustc` to optimize for small code size. +opt-level = "s" +lto = true \ No newline at end of file diff --git a/AsyncVMTests/test-1-simple-object/README.md b/AsyncVMTests/test-1-simple-object/README.md new file mode 100644 index 0000000..be29ebd --- /dev/null +++ b/AsyncVMTests/test-1-simple-object/README.md @@ -0,0 +1,86 @@ +
+ +

wasm-pack-template

+ + A template for kick starting a Rust and WebAssembly project using wasm-pack. + +

+ Build Status +

+ +

+ Tutorial + | + Chat +

+ + Built with 🦀🕸 by The Rust and WebAssembly Working Group +
+ +## About + +[**📚 Read this template tutorial! 📚**][template-docs] + +This template is designed for compiling Rust libraries into WebAssembly and +publishing the resulting package to NPM. + +Be sure to check out [other `wasm-pack` tutorials online][tutorials] for other +templates and usages of `wasm-pack`. + +[tutorials]: https://rustwasm.github.io/docs/wasm-pack/tutorials/index.html +[template-docs]: https://rustwasm.github.io/docs/wasm-pack/tutorials/npm-browser-packages/index.html + +## 🚴 Usage + +### 🐑 Use `cargo generate` to Clone this Template + +[Learn more about `cargo generate` here.](https://github.com/ashleygwilliams/cargo-generate) + +``` +cargo generate --git https://github.com/rustwasm/wasm-pack-template.git --name my-project +cd my-project +``` + +### 🛠️ Build with `wasm-pack build` + +``` +wasm-pack build +``` + +### 🔬 Test in Headless Browsers with `wasm-pack test` + +``` +wasm-pack test --headless --firefox +``` + +### 🎁 Publish to NPM with `wasm-pack publish` + +``` +wasm-pack publish +``` + +## 🔋 Batteries Included + +* [`wasm-bindgen`](https://github.com/rustwasm/wasm-bindgen) for communicating + between WebAssembly and JavaScript. +* [`console_error_panic_hook`](https://github.com/rustwasm/console_error_panic_hook) + for logging panic messages to the developer console. +* [`wee_alloc`](https://github.com/rustwasm/wee_alloc), an allocator optimized + for small code size. +* `LICENSE-APACHE` and `LICENSE-MIT`: most Rust projects are licensed this way, so these are included for you + +## License + +Licensed under either of + +* Apache License, Version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0) +* MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT) + +at your option. + +### Contribution + +Unless you explicitly state otherwise, any contribution intentionally +submitted for inclusion in the work by you, as defined in the Apache-2.0 +license, shall be dual licensed as above, without any additional terms or +conditions. \ No newline at end of file diff --git a/AsyncVMTests/test-1-simple-object/src/lib.rs b/AsyncVMTests/test-1-simple-object/src/lib.rs new file mode 100644 index 0000000..2a2eef1 --- /dev/null +++ b/AsyncVMTests/test-1-simple-object/src/lib.rs @@ -0,0 +1,55 @@ +use gloo_utils::format::JsValueSerdeExt; +use serde::{Serialize, Deserialize}; +use std::collections::HashMap; +use wasm_bindgen::prelude::*; + + + +//-------------------- IMPORTS ---------------------- + + +#[wasm_bindgen] +extern "C" { + + #[wasm_bindgen(js_namespace = console)] + fn log(s: &str); + +} + + + +#[derive(Serialize, Deserialize,Debug)] +pub struct Example { + pub field1: HashMap, + pub field2: Vec>, + pub field3: [f32; 4], +} + + + +#[wasm_bindgen] +pub fn send_example_to_js() -> JsValue { + + let mut field1 = HashMap::new(); + + field1.insert(0, String::from("ex")); + + let example = Example { + field1, + field2: vec![vec![1., 2.], vec![3., 4.]], + field3: [1., 2., 3., 4.] + }; + + ::from_serde(&example).unwrap() + +} + +#[wasm_bindgen] +pub fn receive_example_from_js(val: JsValue) { + + let example: Example = val.into_serde().unwrap(); + + log("Object accepted inside WASM"); + +} + diff --git a/AsyncVMTests/test-2-async-func/.appveyor.yml b/AsyncVMTests/test-2-async-func/.appveyor.yml new file mode 100644 index 0000000..50910bd --- /dev/null +++ b/AsyncVMTests/test-2-async-func/.appveyor.yml @@ -0,0 +1,11 @@ +install: + - appveyor-retry appveyor DownloadFile https://win.rustup.rs/ -FileName rustup-init.exe + - if not defined RUSTFLAGS rustup-init.exe -y --default-host x86_64-pc-windows-msvc --default-toolchain nightly + - set PATH=%PATH%;C:\Users\appveyor\.cargo\bin + - rustc -V + - cargo -V + +build: false + +test_script: + - cargo test --locked diff --git a/AsyncVMTests/test-2-async-func/.cargo-ok b/AsyncVMTests/test-2-async-func/.cargo-ok new file mode 100644 index 0000000..e69de29 diff --git a/AsyncVMTests/test-2-async-func/.gitignore b/AsyncVMTests/test-2-async-func/.gitignore new file mode 100644 index 0000000..d558542 --- /dev/null +++ b/AsyncVMTests/test-2-async-func/.gitignore @@ -0,0 +1,8 @@ +/target +**/*.rs.bk +Cargo.lock +bin/ +pkg/ +wasm-pack.log +tests +LICENSE* \ No newline at end of file diff --git a/AsyncVMTests/test-2-async-func/.travis.yml b/AsyncVMTests/test-2-async-func/.travis.yml new file mode 100644 index 0000000..7a91325 --- /dev/null +++ b/AsyncVMTests/test-2-async-func/.travis.yml @@ -0,0 +1,69 @@ +language: rust +sudo: false + +cache: cargo + +matrix: + include: + + # Builds with wasm-pack. + - rust: beta + env: RUST_BACKTRACE=1 + addons: + firefox: latest + chrome: stable + before_script: + - (test -x $HOME/.cargo/bin/cargo-install-update || cargo install cargo-update) + - (test -x $HOME/.cargo/bin/cargo-generate || cargo install --vers "^0.2" cargo-generate) + - cargo install-update -a + - curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh -s -- -f + script: + - cargo generate --git . --name testing + # Having a broken Cargo.toml (in that it has curlies in fields) anywhere + # in any of our parent dirs is problematic. + - mv Cargo.toml Cargo.toml.tmpl + - cd testing + - wasm-pack build + - wasm-pack test --chrome --firefox --headless + + # Builds on nightly. + - rust: nightly + env: RUST_BACKTRACE=1 + before_script: + - (test -x $HOME/.cargo/bin/cargo-install-update || cargo install cargo-update) + - (test -x $HOME/.cargo/bin/cargo-generate || cargo install --vers "^0.2" cargo-generate) + - cargo install-update -a + - rustup target add wasm32-unknown-unknown + script: + - cargo generate --git . --name testing + - mv Cargo.toml Cargo.toml.tmpl + - cd testing + - cargo check + - cargo check --target wasm32-unknown-unknown + - cargo check --no-default-features + - cargo check --target wasm32-unknown-unknown --no-default-features + - cargo check --no-default-features --features console_error_panic_hook + - cargo check --target wasm32-unknown-unknown --no-default-features --features console_error_panic_hook + - cargo check --no-default-features --features "console_error_panic_hook wee_alloc" + - cargo check --target wasm32-unknown-unknown --no-default-features --features "console_error_panic_hook wee_alloc" + + # Builds on beta. + - rust: beta + env: RUST_BACKTRACE=1 + before_script: + - (test -x $HOME/.cargo/bin/cargo-install-update || cargo install cargo-update) + - (test -x $HOME/.cargo/bin/cargo-generate || cargo install --vers "^0.2" cargo-generate) + - cargo install-update -a + - rustup target add wasm32-unknown-unknown + script: + - cargo generate --git . --name testing + - mv Cargo.toml Cargo.toml.tmpl + - cd testing + - cargo check + - cargo check --target wasm32-unknown-unknown + - cargo check --no-default-features + - cargo check --target wasm32-unknown-unknown --no-default-features + - cargo check --no-default-features --features console_error_panic_hook + - cargo check --target wasm32-unknown-unknown --no-default-features --features console_error_panic_hook + # Note: no enabling the `wee_alloc` feature here because it requires + # nightly for now. diff --git a/AsyncVMTests/test-2-async-func/Cargo.toml b/AsyncVMTests/test-2-async-func/Cargo.toml new file mode 100644 index 0000000..996f7a6 --- /dev/null +++ b/AsyncVMTests/test-2-async-func/Cargo.toml @@ -0,0 +1,39 @@ +[package] +name = "test-1-simple-object" +version = "0.1.0" +authors = ["Vlad Chernenko "] +edition = "2018" + +[lib] +crate-type = ["cdylib"] + +[features] +default = ["console_error_panic_hook"] + +[dependencies] +gloo-utils = { version = "0.1", features = ["serde"] } +serde = { version = "1.0", features = ["derive"] } +wasm-bindgen = "0.2.84" +wasm-bindgen-futures = "0.4.34" +js-sys = "0.3.61" + + + +# The `console_error_panic_hook` crate provides better debugging of panics by +# logging them with `console.error`. This is great for development, but requires +# all the `std::fmt` and `std::panicking` infrastructure, so isn't great for +# code size when deploying. +console_error_panic_hook = { version = "0.1.6", optional = true } + +# `wee_alloc` is a tiny allocator for wasm that is only ~1K in code size +# compared to the default allocator's ~10K. It is slower than the default +# allocator, however. +wee_alloc = { version = "0.4.5", optional = true } + +[dev-dependencies] +wasm-bindgen-test = "0.3.13" + +[profile.release] +# Tell `rustc` to optimize for small code size. +opt-level = "s" +lto = true \ No newline at end of file diff --git a/AsyncVMTests/test-2-async-func/README.md b/AsyncVMTests/test-2-async-func/README.md new file mode 100644 index 0000000..be29ebd --- /dev/null +++ b/AsyncVMTests/test-2-async-func/README.md @@ -0,0 +1,86 @@ +
+ +

wasm-pack-template

+ + A template for kick starting a Rust and WebAssembly project using wasm-pack. + +

+ Build Status +

+ +

+ Tutorial + | + Chat +

+ + Built with 🦀🕸 by The Rust and WebAssembly Working Group +
+ +## About + +[**📚 Read this template tutorial! 📚**][template-docs] + +This template is designed for compiling Rust libraries into WebAssembly and +publishing the resulting package to NPM. + +Be sure to check out [other `wasm-pack` tutorials online][tutorials] for other +templates and usages of `wasm-pack`. + +[tutorials]: https://rustwasm.github.io/docs/wasm-pack/tutorials/index.html +[template-docs]: https://rustwasm.github.io/docs/wasm-pack/tutorials/npm-browser-packages/index.html + +## 🚴 Usage + +### 🐑 Use `cargo generate` to Clone this Template + +[Learn more about `cargo generate` here.](https://github.com/ashleygwilliams/cargo-generate) + +``` +cargo generate --git https://github.com/rustwasm/wasm-pack-template.git --name my-project +cd my-project +``` + +### 🛠️ Build with `wasm-pack build` + +``` +wasm-pack build +``` + +### 🔬 Test in Headless Browsers with `wasm-pack test` + +``` +wasm-pack test --headless --firefox +``` + +### 🎁 Publish to NPM with `wasm-pack publish` + +``` +wasm-pack publish +``` + +## 🔋 Batteries Included + +* [`wasm-bindgen`](https://github.com/rustwasm/wasm-bindgen) for communicating + between WebAssembly and JavaScript. +* [`console_error_panic_hook`](https://github.com/rustwasm/console_error_panic_hook) + for logging panic messages to the developer console. +* [`wee_alloc`](https://github.com/rustwasm/wee_alloc), an allocator optimized + for small code size. +* `LICENSE-APACHE` and `LICENSE-MIT`: most Rust projects are licensed this way, so these are included for you + +## License + +Licensed under either of + +* Apache License, Version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0) +* MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT) + +at your option. + +### Contribution + +Unless you explicitly state otherwise, any contribution intentionally +submitted for inclusion in the work by you, as defined in the Apache-2.0 +license, shall be dual licensed as above, without any additional terms or +conditions. \ No newline at end of file diff --git a/AsyncVMTests/test-2-async-func/js/foo.js b/AsyncVMTests/test-2-async-func/js/foo.js new file mode 100644 index 0000000..031d8fe --- /dev/null +++ b/AsyncVMTests/test-2-async-func/js/foo.js @@ -0,0 +1,9 @@ +export let async_func_1 = async() => { + + return new Promise(resolve => { + + setTimeout(()=>resolve('Hello VladArtem!'),3000) + + }) + +} \ No newline at end of file diff --git a/AsyncVMTests/test-2-async-func/src/lib.rs b/AsyncVMTests/test-2-async-func/src/lib.rs new file mode 100644 index 0000000..eacb74d --- /dev/null +++ b/AsyncVMTests/test-2-async-func/src/lib.rs @@ -0,0 +1,64 @@ +use serde::{Serialize, Deserialize}; +use std::collections::HashMap; +use wasm_bindgen::prelude::*; + + + +//-------------------- IMPORTS ---------------------- + + +#[wasm_bindgen] +extern "C" { + + #[wasm_bindgen(js_namespace = console)] + fn log(s: &str); + + #[wasm_bindgen(module = "/js/foo.js")] + async fn async_func_1() -> JsValue; + +} + + + +#[derive(Serialize,Deserialize,Debug)] +pub struct Example { + pub field1: HashMap, + pub field2: Vec>, + pub field3: [f32; 4], +} + + + +#[wasm_bindgen] +pub fn send_example_to_js() -> JsValue { + + let mut field1 = HashMap::new(); + + field1.insert(0, String::from("ex")); + + let example = Example { + field1, + field2: vec![vec![1., 2.], vec![3., 4.]], + field3: [1., 2., 3., 4.] + }; + + JsValue::from_serde(&example).unwrap() + +} + + +#[wasm_bindgen] +pub async fn receive_example_from_js(val: JsValue) -> Result { + + let example: Example = val.into_serde().unwrap(); + + log("Object accepted inside WASM"); + + let promise = js_sys::Promise::resolve(&(async_func_1().await)); + + let result = wasm_bindgen_futures::JsFuture::from(promise).await?; + + Ok(result) + +} + diff --git a/AsyncVMTests/test-3-to-get-base/.appveyor.yml b/AsyncVMTests/test-3-to-get-base/.appveyor.yml new file mode 100644 index 0000000..50910bd --- /dev/null +++ b/AsyncVMTests/test-3-to-get-base/.appveyor.yml @@ -0,0 +1,11 @@ +install: + - appveyor-retry appveyor DownloadFile https://win.rustup.rs/ -FileName rustup-init.exe + - if not defined RUSTFLAGS rustup-init.exe -y --default-host x86_64-pc-windows-msvc --default-toolchain nightly + - set PATH=%PATH%;C:\Users\appveyor\.cargo\bin + - rustc -V + - cargo -V + +build: false + +test_script: + - cargo test --locked diff --git a/AsyncVMTests/test-3-to-get-base/.cargo-ok b/AsyncVMTests/test-3-to-get-base/.cargo-ok new file mode 100644 index 0000000..e69de29 diff --git a/AsyncVMTests/test-3-to-get-base/.gitignore b/AsyncVMTests/test-3-to-get-base/.gitignore new file mode 100644 index 0000000..d558542 --- /dev/null +++ b/AsyncVMTests/test-3-to-get-base/.gitignore @@ -0,0 +1,8 @@ +/target +**/*.rs.bk +Cargo.lock +bin/ +pkg/ +wasm-pack.log +tests +LICENSE* \ No newline at end of file diff --git a/AsyncVMTests/test-3-to-get-base/.travis.yml b/AsyncVMTests/test-3-to-get-base/.travis.yml new file mode 100644 index 0000000..7a91325 --- /dev/null +++ b/AsyncVMTests/test-3-to-get-base/.travis.yml @@ -0,0 +1,69 @@ +language: rust +sudo: false + +cache: cargo + +matrix: + include: + + # Builds with wasm-pack. + - rust: beta + env: RUST_BACKTRACE=1 + addons: + firefox: latest + chrome: stable + before_script: + - (test -x $HOME/.cargo/bin/cargo-install-update || cargo install cargo-update) + - (test -x $HOME/.cargo/bin/cargo-generate || cargo install --vers "^0.2" cargo-generate) + - cargo install-update -a + - curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh -s -- -f + script: + - cargo generate --git . --name testing + # Having a broken Cargo.toml (in that it has curlies in fields) anywhere + # in any of our parent dirs is problematic. + - mv Cargo.toml Cargo.toml.tmpl + - cd testing + - wasm-pack build + - wasm-pack test --chrome --firefox --headless + + # Builds on nightly. + - rust: nightly + env: RUST_BACKTRACE=1 + before_script: + - (test -x $HOME/.cargo/bin/cargo-install-update || cargo install cargo-update) + - (test -x $HOME/.cargo/bin/cargo-generate || cargo install --vers "^0.2" cargo-generate) + - cargo install-update -a + - rustup target add wasm32-unknown-unknown + script: + - cargo generate --git . --name testing + - mv Cargo.toml Cargo.toml.tmpl + - cd testing + - cargo check + - cargo check --target wasm32-unknown-unknown + - cargo check --no-default-features + - cargo check --target wasm32-unknown-unknown --no-default-features + - cargo check --no-default-features --features console_error_panic_hook + - cargo check --target wasm32-unknown-unknown --no-default-features --features console_error_panic_hook + - cargo check --no-default-features --features "console_error_panic_hook wee_alloc" + - cargo check --target wasm32-unknown-unknown --no-default-features --features "console_error_panic_hook wee_alloc" + + # Builds on beta. + - rust: beta + env: RUST_BACKTRACE=1 + before_script: + - (test -x $HOME/.cargo/bin/cargo-install-update || cargo install cargo-update) + - (test -x $HOME/.cargo/bin/cargo-generate || cargo install --vers "^0.2" cargo-generate) + - cargo install-update -a + - rustup target add wasm32-unknown-unknown + script: + - cargo generate --git . --name testing + - mv Cargo.toml Cargo.toml.tmpl + - cd testing + - cargo check + - cargo check --target wasm32-unknown-unknown + - cargo check --no-default-features + - cargo check --target wasm32-unknown-unknown --no-default-features + - cargo check --no-default-features --features console_error_panic_hook + - cargo check --target wasm32-unknown-unknown --no-default-features --features console_error_panic_hook + # Note: no enabling the `wee_alloc` feature here because it requires + # nightly for now. diff --git a/AsyncVMTests/test-3-to-get-base/Cargo.toml b/AsyncVMTests/test-3-to-get-base/Cargo.toml new file mode 100644 index 0000000..996f7a6 --- /dev/null +++ b/AsyncVMTests/test-3-to-get-base/Cargo.toml @@ -0,0 +1,39 @@ +[package] +name = "test-1-simple-object" +version = "0.1.0" +authors = ["Vlad Chernenko "] +edition = "2018" + +[lib] +crate-type = ["cdylib"] + +[features] +default = ["console_error_panic_hook"] + +[dependencies] +gloo-utils = { version = "0.1", features = ["serde"] } +serde = { version = "1.0", features = ["derive"] } +wasm-bindgen = "0.2.84" +wasm-bindgen-futures = "0.4.34" +js-sys = "0.3.61" + + + +# The `console_error_panic_hook` crate provides better debugging of panics by +# logging them with `console.error`. This is great for development, but requires +# all the `std::fmt` and `std::panicking` infrastructure, so isn't great for +# code size when deploying. +console_error_panic_hook = { version = "0.1.6", optional = true } + +# `wee_alloc` is a tiny allocator for wasm that is only ~1K in code size +# compared to the default allocator's ~10K. It is slower than the default +# allocator, however. +wee_alloc = { version = "0.4.5", optional = true } + +[dev-dependencies] +wasm-bindgen-test = "0.3.13" + +[profile.release] +# Tell `rustc` to optimize for small code size. +opt-level = "s" +lto = true \ No newline at end of file diff --git a/AsyncVMTests/test-3-to-get-base/README.md b/AsyncVMTests/test-3-to-get-base/README.md new file mode 100644 index 0000000..be29ebd --- /dev/null +++ b/AsyncVMTests/test-3-to-get-base/README.md @@ -0,0 +1,86 @@ +
+ +

wasm-pack-template

+ + A template for kick starting a Rust and WebAssembly project using wasm-pack. + +

+ Build Status +

+ +

+ Tutorial + | + Chat +

+ + Built with 🦀🕸 by The Rust and WebAssembly Working Group +
+ +## About + +[**📚 Read this template tutorial! 📚**][template-docs] + +This template is designed for compiling Rust libraries into WebAssembly and +publishing the resulting package to NPM. + +Be sure to check out [other `wasm-pack` tutorials online][tutorials] for other +templates and usages of `wasm-pack`. + +[tutorials]: https://rustwasm.github.io/docs/wasm-pack/tutorials/index.html +[template-docs]: https://rustwasm.github.io/docs/wasm-pack/tutorials/npm-browser-packages/index.html + +## 🚴 Usage + +### 🐑 Use `cargo generate` to Clone this Template + +[Learn more about `cargo generate` here.](https://github.com/ashleygwilliams/cargo-generate) + +``` +cargo generate --git https://github.com/rustwasm/wasm-pack-template.git --name my-project +cd my-project +``` + +### 🛠️ Build with `wasm-pack build` + +``` +wasm-pack build +``` + +### 🔬 Test in Headless Browsers with `wasm-pack test` + +``` +wasm-pack test --headless --firefox +``` + +### 🎁 Publish to NPM with `wasm-pack publish` + +``` +wasm-pack publish +``` + +## 🔋 Batteries Included + +* [`wasm-bindgen`](https://github.com/rustwasm/wasm-bindgen) for communicating + between WebAssembly and JavaScript. +* [`console_error_panic_hook`](https://github.com/rustwasm/console_error_panic_hook) + for logging panic messages to the developer console. +* [`wee_alloc`](https://github.com/rustwasm/wee_alloc), an allocator optimized + for small code size. +* `LICENSE-APACHE` and `LICENSE-MIT`: most Rust projects are licensed this way, so these are included for you + +## License + +Licensed under either of + +* Apache License, Version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0) +* MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT) + +at your option. + +### Contribution + +Unless you explicitly state otherwise, any contribution intentionally +submitted for inclusion in the work by you, as defined in the Apache-2.0 +license, shall be dual licensed as above, without any additional terms or +conditions. \ No newline at end of file diff --git a/AsyncVMTests/test-3-to-get-base/js/foo.js b/AsyncVMTests/test-3-to-get-base/js/foo.js new file mode 100644 index 0000000..031d8fe --- /dev/null +++ b/AsyncVMTests/test-3-to-get-base/js/foo.js @@ -0,0 +1,9 @@ +export let async_func_1 = async() => { + + return new Promise(resolve => { + + setTimeout(()=>resolve('Hello VladArtem!'),3000) + + }) + +} \ No newline at end of file diff --git a/AsyncVMTests/test-3-to-get-base/src/lib.rs b/AsyncVMTests/test-3-to-get-base/src/lib.rs new file mode 100644 index 0000000..4459d5d --- /dev/null +++ b/AsyncVMTests/test-3-to-get-base/src/lib.rs @@ -0,0 +1,79 @@ +use gloo_utils::format::JsValueSerdeExt; +use serde::{Serialize, Deserialize}; +use std::collections::HashMap; +use wasm_bindgen::prelude::*; + + + +//-------------------- IMPORTS ---------------------- + + +#[wasm_bindgen] +extern "C" { + + #[wasm_bindgen(js_namespace = console)] + #[no_mangle] + fn log(s: &str); + + #[wasm_bindgen(js_namespace = async_module)] + #[no_mangle] + async fn async_func_1() -> JsValue; + +} + + + +#[derive(Serialize,Deserialize,Debug)] +pub struct Example { + pub field1: HashMap, + pub field2: Vec>, + pub field3: [f32; 4], +} + + + + + +#[derive(Serialize,Deserialize,Debug)] +pub struct Square { + pub field1: HashMap, + pub field2: Vec>, + pub field3: [f32; 4], + pub data: i32 +} + + + + +#[wasm_bindgen] +pub fn send_example_to_js() -> JsValue { + + let mut field1 = HashMap::new(); + + field1.insert(0, String::from("ex")); + + let example = Example { + field1, + field2: vec![vec![1., 2.], vec![3., 4.]], + field3: [1., 2., 3., 4.] + }; + + ::from_serde(&example).unwrap() + +} + +#[wasm_bindgen] +pub async fn receive_example_from_js(val: JsValue) -> Result { + + let example: Example = val.into_serde().unwrap(); + + log("Object accepted inside WASM"); + + let promise = js_sys::Promise::resolve(&(async_func_1().await)); + + let result = wasm_bindgen_futures::JsFuture::from(promise).await?; + + Ok(result) + +} + diff --git a/AsyncVMTests/test-4-no-async-imports/.appveyor.yml b/AsyncVMTests/test-4-no-async-imports/.appveyor.yml new file mode 100644 index 0000000..50910bd --- /dev/null +++ b/AsyncVMTests/test-4-no-async-imports/.appveyor.yml @@ -0,0 +1,11 @@ +install: + - appveyor-retry appveyor DownloadFile https://win.rustup.rs/ -FileName rustup-init.exe + - if not defined RUSTFLAGS rustup-init.exe -y --default-host x86_64-pc-windows-msvc --default-toolchain nightly + - set PATH=%PATH%;C:\Users\appveyor\.cargo\bin + - rustc -V + - cargo -V + +build: false + +test_script: + - cargo test --locked diff --git a/AsyncVMTests/test-4-no-async-imports/.cargo-ok b/AsyncVMTests/test-4-no-async-imports/.cargo-ok new file mode 100644 index 0000000..e69de29 diff --git a/AsyncVMTests/test-4-no-async-imports/.gitignore b/AsyncVMTests/test-4-no-async-imports/.gitignore new file mode 100644 index 0000000..d558542 --- /dev/null +++ b/AsyncVMTests/test-4-no-async-imports/.gitignore @@ -0,0 +1,8 @@ +/target +**/*.rs.bk +Cargo.lock +bin/ +pkg/ +wasm-pack.log +tests +LICENSE* \ No newline at end of file diff --git a/AsyncVMTests/test-4-no-async-imports/.travis.yml b/AsyncVMTests/test-4-no-async-imports/.travis.yml new file mode 100644 index 0000000..7a91325 --- /dev/null +++ b/AsyncVMTests/test-4-no-async-imports/.travis.yml @@ -0,0 +1,69 @@ +language: rust +sudo: false + +cache: cargo + +matrix: + include: + + # Builds with wasm-pack. + - rust: beta + env: RUST_BACKTRACE=1 + addons: + firefox: latest + chrome: stable + before_script: + - (test -x $HOME/.cargo/bin/cargo-install-update || cargo install cargo-update) + - (test -x $HOME/.cargo/bin/cargo-generate || cargo install --vers "^0.2" cargo-generate) + - cargo install-update -a + - curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh -s -- -f + script: + - cargo generate --git . --name testing + # Having a broken Cargo.toml (in that it has curlies in fields) anywhere + # in any of our parent dirs is problematic. + - mv Cargo.toml Cargo.toml.tmpl + - cd testing + - wasm-pack build + - wasm-pack test --chrome --firefox --headless + + # Builds on nightly. + - rust: nightly + env: RUST_BACKTRACE=1 + before_script: + - (test -x $HOME/.cargo/bin/cargo-install-update || cargo install cargo-update) + - (test -x $HOME/.cargo/bin/cargo-generate || cargo install --vers "^0.2" cargo-generate) + - cargo install-update -a + - rustup target add wasm32-unknown-unknown + script: + - cargo generate --git . --name testing + - mv Cargo.toml Cargo.toml.tmpl + - cd testing + - cargo check + - cargo check --target wasm32-unknown-unknown + - cargo check --no-default-features + - cargo check --target wasm32-unknown-unknown --no-default-features + - cargo check --no-default-features --features console_error_panic_hook + - cargo check --target wasm32-unknown-unknown --no-default-features --features console_error_panic_hook + - cargo check --no-default-features --features "console_error_panic_hook wee_alloc" + - cargo check --target wasm32-unknown-unknown --no-default-features --features "console_error_panic_hook wee_alloc" + + # Builds on beta. + - rust: beta + env: RUST_BACKTRACE=1 + before_script: + - (test -x $HOME/.cargo/bin/cargo-install-update || cargo install cargo-update) + - (test -x $HOME/.cargo/bin/cargo-generate || cargo install --vers "^0.2" cargo-generate) + - cargo install-update -a + - rustup target add wasm32-unknown-unknown + script: + - cargo generate --git . --name testing + - mv Cargo.toml Cargo.toml.tmpl + - cd testing + - cargo check + - cargo check --target wasm32-unknown-unknown + - cargo check --no-default-features + - cargo check --target wasm32-unknown-unknown --no-default-features + - cargo check --no-default-features --features console_error_panic_hook + - cargo check --target wasm32-unknown-unknown --no-default-features --features console_error_panic_hook + # Note: no enabling the `wee_alloc` feature here because it requires + # nightly for now. diff --git a/AsyncVMTests/test-4-no-async-imports/Cargo.toml b/AsyncVMTests/test-4-no-async-imports/Cargo.toml new file mode 100644 index 0000000..b45cabe --- /dev/null +++ b/AsyncVMTests/test-4-no-async-imports/Cargo.toml @@ -0,0 +1,39 @@ +[package] +name = "test-1-simple-object" +version = "0.1.0" +authors = ["Vlad Chernenko "] +edition = "2018" + +[lib] +crate-type = ["cdylib"] + +[features] +default = ["console_error_panic_hook"] + +[dependencies] +gloo-utils = { version = "0.1", features = ["serde"] } +serde = { version = "1.0", features = ["derive"] } +wasm-bindgen-futures = "0.4.34" +wasm-bindgen = "0.2.84" +js-sys = "0.3.61" + + + +# The `console_error_panic_hook` crate provides better debugging of panics by +# logging them with `console.error`. This is great for development, but requires +# all the `std::fmt` and `std::panicking` infrastructure, so isn't great for +# code size when deploying. +console_error_panic_hook = { version = "0.1.6", optional = true } + +# `wee_alloc` is a tiny allocator for wasm that is only ~1K in code size +# compared to the default allocator's ~10K. It is slower than the default +# allocator, however. +wee_alloc = { version = "0.4.5", optional = true } + +[dev-dependencies] +wasm-bindgen-test = "0.3.13" + +[profile.release] +# Tell `rustc` to optimize for small code size. +opt-level = "s" +lto = true \ No newline at end of file diff --git a/AsyncVMTests/test-4-no-async-imports/README.md b/AsyncVMTests/test-4-no-async-imports/README.md new file mode 100644 index 0000000..be29ebd --- /dev/null +++ b/AsyncVMTests/test-4-no-async-imports/README.md @@ -0,0 +1,86 @@ +
+ +

wasm-pack-template

+ + A template for kick starting a Rust and WebAssembly project using wasm-pack. + +

+ Build Status +

+ +

+ Tutorial + | + Chat +

+ + Built with 🦀🕸 by The Rust and WebAssembly Working Group +
+ +## About + +[**📚 Read this template tutorial! 📚**][template-docs] + +This template is designed for compiling Rust libraries into WebAssembly and +publishing the resulting package to NPM. + +Be sure to check out [other `wasm-pack` tutorials online][tutorials] for other +templates and usages of `wasm-pack`. + +[tutorials]: https://rustwasm.github.io/docs/wasm-pack/tutorials/index.html +[template-docs]: https://rustwasm.github.io/docs/wasm-pack/tutorials/npm-browser-packages/index.html + +## 🚴 Usage + +### 🐑 Use `cargo generate` to Clone this Template + +[Learn more about `cargo generate` here.](https://github.com/ashleygwilliams/cargo-generate) + +``` +cargo generate --git https://github.com/rustwasm/wasm-pack-template.git --name my-project +cd my-project +``` + +### 🛠️ Build with `wasm-pack build` + +``` +wasm-pack build +``` + +### 🔬 Test in Headless Browsers with `wasm-pack test` + +``` +wasm-pack test --headless --firefox +``` + +### 🎁 Publish to NPM with `wasm-pack publish` + +``` +wasm-pack publish +``` + +## 🔋 Batteries Included + +* [`wasm-bindgen`](https://github.com/rustwasm/wasm-bindgen) for communicating + between WebAssembly and JavaScript. +* [`console_error_panic_hook`](https://github.com/rustwasm/console_error_panic_hook) + for logging panic messages to the developer console. +* [`wee_alloc`](https://github.com/rustwasm/wee_alloc), an allocator optimized + for small code size. +* `LICENSE-APACHE` and `LICENSE-MIT`: most Rust projects are licensed this way, so these are included for you + +## License + +Licensed under either of + +* Apache License, Version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0) +* MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT) + +at your option. + +### Contribution + +Unless you explicitly state otherwise, any contribution intentionally +submitted for inclusion in the work by you, as defined in the Apache-2.0 +license, shall be dual licensed as above, without any additional terms or +conditions. \ No newline at end of file diff --git a/AsyncVMTests/test-4-no-async-imports/src/lib.rs b/AsyncVMTests/test-4-no-async-imports/src/lib.rs new file mode 100644 index 0000000..eb9fbff --- /dev/null +++ b/AsyncVMTests/test-4-no-async-imports/src/lib.rs @@ -0,0 +1,75 @@ +use serde::{Serialize, Deserialize}; +use std::collections::HashMap; +use wasm_bindgen::prelude::*; +use gloo_utils::format::JsValueSerdeExt; + + + +#[derive(Serialize,Deserialize)] +pub struct Example { + pub field1: HashMap, + pub field2: Vec>, + pub field3: [f32; 4], +} + + + + + +#[derive(Serialize,Deserialize)] +pub struct Square { + pub field1: HashMap, + pub field2: Vec>, + pub field3: [f32; 4], + pub data: i32 +} + + + + +#[wasm_bindgen] +pub fn send_example_to_js() -> JsValue { + + let mut field1 = HashMap::new(); + + field1.insert(0, String::from("ex")); + + let example = Example { + field1, + field2: vec![vec![1., 2.], vec![3., 4.]], + field3: [1., 2., 3., 4.] + }; + + JsValue::from_serde(&example).unwrap() + +} + + + +#[wasm_bindgen] +pub async fn receive_example_from_js(val: JsValue) -> Result { + + let example: Example = val.into_serde().unwrap(); + + let promise = js_sys::Promise::resolve(&JsValue::NULL); + + let result = wasm_bindgen_futures::JsFuture::from(promise).await?; + + Ok(result) + +} + + +#[wasm_bindgen] +pub async fn another_async_func(val: JsValue) -> Result { + + let example: Example = val.into_serde().unwrap(); + + let promise = js_sys::Promise::resolve(&JsValue::NULL); + + let result = wasm_bindgen_futures::JsFuture::from(promise).await?; + + Ok(result) + +} + diff --git a/AsyncVMTests/test-4-no-async-imports/src/utils.rs b/AsyncVMTests/test-4-no-async-imports/src/utils.rs new file mode 100644 index 0000000..b1d7929 --- /dev/null +++ b/AsyncVMTests/test-4-no-async-imports/src/utils.rs @@ -0,0 +1,10 @@ +pub fn set_panic_hook() { + // When the `console_error_panic_hook` feature is enabled, we can call the + // `set_panic_hook` function at least once during initialization, and then + // we will get better error messages if our code ever panics. + // + // For more details see + // https://github.com/rustwasm/console_error_panic_hook#readme + #[cfg(feature = "console_error_panic_hook")] + console_error_panic_hook::set_once(); +} diff --git a/AsyncVMTests/test-5-async-without-await/.appveyor.yml b/AsyncVMTests/test-5-async-without-await/.appveyor.yml new file mode 100644 index 0000000..50910bd --- /dev/null +++ b/AsyncVMTests/test-5-async-without-await/.appveyor.yml @@ -0,0 +1,11 @@ +install: + - appveyor-retry appveyor DownloadFile https://win.rustup.rs/ -FileName rustup-init.exe + - if not defined RUSTFLAGS rustup-init.exe -y --default-host x86_64-pc-windows-msvc --default-toolchain nightly + - set PATH=%PATH%;C:\Users\appveyor\.cargo\bin + - rustc -V + - cargo -V + +build: false + +test_script: + - cargo test --locked diff --git a/AsyncVMTests/test-5-async-without-await/.cargo-ok b/AsyncVMTests/test-5-async-without-await/.cargo-ok new file mode 100644 index 0000000..e69de29 diff --git a/AsyncVMTests/test-5-async-without-await/.gitignore b/AsyncVMTests/test-5-async-without-await/.gitignore new file mode 100644 index 0000000..d558542 --- /dev/null +++ b/AsyncVMTests/test-5-async-without-await/.gitignore @@ -0,0 +1,8 @@ +/target +**/*.rs.bk +Cargo.lock +bin/ +pkg/ +wasm-pack.log +tests +LICENSE* \ No newline at end of file diff --git a/AsyncVMTests/test-5-async-without-await/.travis.yml b/AsyncVMTests/test-5-async-without-await/.travis.yml new file mode 100644 index 0000000..7a91325 --- /dev/null +++ b/AsyncVMTests/test-5-async-without-await/.travis.yml @@ -0,0 +1,69 @@ +language: rust +sudo: false + +cache: cargo + +matrix: + include: + + # Builds with wasm-pack. + - rust: beta + env: RUST_BACKTRACE=1 + addons: + firefox: latest + chrome: stable + before_script: + - (test -x $HOME/.cargo/bin/cargo-install-update || cargo install cargo-update) + - (test -x $HOME/.cargo/bin/cargo-generate || cargo install --vers "^0.2" cargo-generate) + - cargo install-update -a + - curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh -s -- -f + script: + - cargo generate --git . --name testing + # Having a broken Cargo.toml (in that it has curlies in fields) anywhere + # in any of our parent dirs is problematic. + - mv Cargo.toml Cargo.toml.tmpl + - cd testing + - wasm-pack build + - wasm-pack test --chrome --firefox --headless + + # Builds on nightly. + - rust: nightly + env: RUST_BACKTRACE=1 + before_script: + - (test -x $HOME/.cargo/bin/cargo-install-update || cargo install cargo-update) + - (test -x $HOME/.cargo/bin/cargo-generate || cargo install --vers "^0.2" cargo-generate) + - cargo install-update -a + - rustup target add wasm32-unknown-unknown + script: + - cargo generate --git . --name testing + - mv Cargo.toml Cargo.toml.tmpl + - cd testing + - cargo check + - cargo check --target wasm32-unknown-unknown + - cargo check --no-default-features + - cargo check --target wasm32-unknown-unknown --no-default-features + - cargo check --no-default-features --features console_error_panic_hook + - cargo check --target wasm32-unknown-unknown --no-default-features --features console_error_panic_hook + - cargo check --no-default-features --features "console_error_panic_hook wee_alloc" + - cargo check --target wasm32-unknown-unknown --no-default-features --features "console_error_panic_hook wee_alloc" + + # Builds on beta. + - rust: beta + env: RUST_BACKTRACE=1 + before_script: + - (test -x $HOME/.cargo/bin/cargo-install-update || cargo install cargo-update) + - (test -x $HOME/.cargo/bin/cargo-generate || cargo install --vers "^0.2" cargo-generate) + - cargo install-update -a + - rustup target add wasm32-unknown-unknown + script: + - cargo generate --git . --name testing + - mv Cargo.toml Cargo.toml.tmpl + - cd testing + - cargo check + - cargo check --target wasm32-unknown-unknown + - cargo check --no-default-features + - cargo check --target wasm32-unknown-unknown --no-default-features + - cargo check --no-default-features --features console_error_panic_hook + - cargo check --target wasm32-unknown-unknown --no-default-features --features console_error_panic_hook + # Note: no enabling the `wee_alloc` feature here because it requires + # nightly for now. diff --git a/AsyncVMTests/test-5-async-without-await/Cargo.toml b/AsyncVMTests/test-5-async-without-await/Cargo.toml new file mode 100644 index 0000000..c3a6ed1 --- /dev/null +++ b/AsyncVMTests/test-5-async-without-await/Cargo.toml @@ -0,0 +1,39 @@ +[package] +name = "test-1-simple-object" +version = "0.1.0" +authors = ["Vlad Chernenko "] +edition = "2018" + +[lib] +crate-type = ["cdylib"] + +[features] +default = ["console_error_panic_hook"] + +[dependencies] +gloo-utils = { version = "0.1", features = ["serde"] } +serde = { version = "1.0", features = ["derive"] } +wasm-bindgen-futures = "0.4.34" +wasm-bindgen = "0.2.84" +js-sys = "0.3.61" +futures = "0.3" + + +# The `console_error_panic_hook` crate provides better debugging of panics by +# logging them with `console.error`. This is great for development, but requires +# all the `std::fmt` and `std::panicking` infrastructure, so isn't great for +# code size when deploying. +console_error_panic_hook = { version = "0.1.6", optional = true } + +# `wee_alloc` is a tiny allocator for wasm that is only ~1K in code size +# compared to the default allocator's ~10K. It is slower than the default +# allocator, however. +wee_alloc = { version = "0.4.5", optional = true } + +[dev-dependencies] +wasm-bindgen-test = "0.3.13" + +[profile.release] +# Tell `rustc` to optimize for small code size. +opt-level = "s" +lto = true \ No newline at end of file diff --git a/AsyncVMTests/test-5-async-without-await/README.md b/AsyncVMTests/test-5-async-without-await/README.md new file mode 100644 index 0000000..be29ebd --- /dev/null +++ b/AsyncVMTests/test-5-async-without-await/README.md @@ -0,0 +1,86 @@ +
+ +

wasm-pack-template

+ + A template for kick starting a Rust and WebAssembly project using wasm-pack. + +

+ Build Status +

+ +

+ Tutorial + | + Chat +

+ + Built with 🦀🕸 by The Rust and WebAssembly Working Group +
+ +## About + +[**📚 Read this template tutorial! 📚**][template-docs] + +This template is designed for compiling Rust libraries into WebAssembly and +publishing the resulting package to NPM. + +Be sure to check out [other `wasm-pack` tutorials online][tutorials] for other +templates and usages of `wasm-pack`. + +[tutorials]: https://rustwasm.github.io/docs/wasm-pack/tutorials/index.html +[template-docs]: https://rustwasm.github.io/docs/wasm-pack/tutorials/npm-browser-packages/index.html + +## 🚴 Usage + +### 🐑 Use `cargo generate` to Clone this Template + +[Learn more about `cargo generate` here.](https://github.com/ashleygwilliams/cargo-generate) + +``` +cargo generate --git https://github.com/rustwasm/wasm-pack-template.git --name my-project +cd my-project +``` + +### 🛠️ Build with `wasm-pack build` + +``` +wasm-pack build +``` + +### 🔬 Test in Headless Browsers with `wasm-pack test` + +``` +wasm-pack test --headless --firefox +``` + +### 🎁 Publish to NPM with `wasm-pack publish` + +``` +wasm-pack publish +``` + +## 🔋 Batteries Included + +* [`wasm-bindgen`](https://github.com/rustwasm/wasm-bindgen) for communicating + between WebAssembly and JavaScript. +* [`console_error_panic_hook`](https://github.com/rustwasm/console_error_panic_hook) + for logging panic messages to the developer console. +* [`wee_alloc`](https://github.com/rustwasm/wee_alloc), an allocator optimized + for small code size. +* `LICENSE-APACHE` and `LICENSE-MIT`: most Rust projects are licensed this way, so these are included for you + +## License + +Licensed under either of + +* Apache License, Version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0) +* MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT) + +at your option. + +### Contribution + +Unless you explicitly state otherwise, any contribution intentionally +submitted for inclusion in the work by you, as defined in the Apache-2.0 +license, shall be dual licensed as above, without any additional terms or +conditions. \ No newline at end of file diff --git a/AsyncVMTests/test-5-async-without-await/src/lib.rs b/AsyncVMTests/test-5-async-without-await/src/lib.rs new file mode 100644 index 0000000..b23b70e --- /dev/null +++ b/AsyncVMTests/test-5-async-without-await/src/lib.rs @@ -0,0 +1,98 @@ +use serde::{Serialize, Deserialize}; +use std::collections::HashMap; +use wasm_bindgen::prelude::*; +use gloo_utils::format::JsValueSerdeExt; + + + + +#[wasm_bindgen] +extern "C" { + + #[wasm_bindgen(js_namespace = console)] + fn log(s: &str); + + #[wasm_bindgen(js_namespace = network_module)] + async fn make_http_call(s: &str); + +} + + +#[derive(Serialize,Deserialize)] +pub struct Example { + pub field1: HashMap, + pub field2: Vec>, + pub field3: [f32; 4], +} + + +#[derive(Serialize,Deserialize)] +pub struct Square { + pub field1: HashMap, + pub field2: Vec>, + pub field3: [f32; 4], + pub data: i32 +} + + + +async fn hello_world(x:&mut f32) { + + *x=123f32; + + make_http_call("FromHelloWorld").await; + + // log("Called Hello World"); + +} + +async fn hello_vlad(x:&mut f32){ + + *x=321f32; + + make_http_call("FromHelloVlad").await; + + // log("Called Hello Vlad"); + +} + + + +#[wasm_bindgen] +pub async fn send_example_to_js(extra_val:f32) -> JsValue { + + let mut x_value:f32 = 1337f32; + + let f1 = hello_world(&mut x_value); + let f2 = hello_vlad(&mut x_value); + + futures::join!(f1,f2); + + let mut field1 = HashMap::new(); + + field1.insert(0, String::from("ex")); + + let example = Example { + field1, + field2: vec![vec![1., 2.], vec![3., 4.]], + field3: [x_value, 2., 3., 4.] + }; + + JsValue::from_serde(&example).unwrap() + +} + + + +#[wasm_bindgen] +pub async fn receive_example_from_js(val: JsValue) -> Result { + + let example: Example = val.into_serde().unwrap(); + + let promise = js_sys::Promise::resolve(&JsValue::NULL); + + let result = wasm_bindgen_futures::JsFuture::from(promise).await?; + + Ok(result) + +} \ No newline at end of file diff --git a/rustBase.js b/rustBase.js new file mode 100644 index 0000000..912f281 --- /dev/null +++ b/rustBase.js @@ -0,0 +1,108 @@ +import {TextEncoder} from 'util' + + +let wasm, WASM_VECTOR_LEN = 0, cachedUint8Memory0 = new Uint8Array(), cachedTextEncoder = new TextEncoder('utf-8') + + + +let getUint8Memory0 = () => { + + if (cachedUint8Memory0.byteLength === 0) cachedUint8Memory0 = new Uint8Array(wasm.memory.buffer) + + return cachedUint8Memory0 + +}, + +encodeString = () => { + + if(typeof cachedTextEncoder.encodeInto === 'function'){ + + return (arg,view) => cachedTextEncoder.encodeInto(arg, view) + + }else{ + + return (arg,view) => { + + const buf = cachedTextEncoder.encode(arg) + + view.set(buf) + + return { + + read: arg.length, + + written: buf.length + + } + + } + + } + +}, + +passStringToWasm0=(arg, malloc, realloc) => { + + if (realloc === undefined) { + + let buf = cachedTextEncoder.encode(arg), + + ptr = malloc(buf.length) + + getUint8Memory0().subarray(ptr, ptr + buf.length).set(buf) + + WASM_VECTOR_LEN = buf.length + + return ptr + + } + + let len = arg.length, + + ptr = malloc(len), + + mem = getUint8Memory0(), + + offset = 0 + + + for (; offset < len; offset++) { + + const code = arg.charCodeAt(offset) + + if (code > 0x7F) break + + mem[ptr + offset] = code + + } + + if (offset !== len) { + + if (offset !== 0) arg = arg.slice(offset) + + ptr = realloc(ptr, len, len = offset + arg.length * 3) + + let view = getUint8Memory0().subarray(ptr + offset, ptr + len), ret = encodeString(arg, view) + + offset += ret.written + } + + WASM_VECTOR_LEN = offset + + return ptr + +} + + + +export default (contractInstance,serializedContractStateChunk,functionName) => { + + wasm=contractInstance + + const ptr0 = passStringToWasm0(serializedContractStateChunk, contractInstance.__wbindgen_malloc, contractInstance.__wbindgen_realloc) + + const len0 = WASM_VECTOR_LEN + + return contractInstance[functionName](ptr0, len0) + +} \ No newline at end of file diff --git a/tests/package.json b/tests/package.json new file mode 100644 index 0000000..4dd5cbf --- /dev/null +++ b/tests/package.json @@ -0,0 +1,13 @@ +{ + "name": "klyntarvmtests", + "version": "1.0.0", + "description": "", + "main": "index.js", + "type":"module", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "keywords": [], + "author": "", + "license": "ISC" +} diff --git a/tests/use_1.js b/tests/use_1.js new file mode 100644 index 0000000..09ef105 --- /dev/null +++ b/tests/use_1.js @@ -0,0 +1,7 @@ +import * as MOD from '../test-1-simple-object/pkg/test_1_simple_object.js' + +let myObject = MOD.send_example_to_js() + +console.log(myObject) + +MOD.receive_example_from_js(myObject) \ No newline at end of file diff --git a/tests/use_2.js b/tests/use_2.js new file mode 100644 index 0000000..1e8445c --- /dev/null +++ b/tests/use_2.js @@ -0,0 +1,9 @@ +import * as MOD from '../test-4-no-async-imports/pkg/test_1_simple_object.js' + +let myObject = MOD.send_example_to_js() + +console.log(myObject) + +// let data = await MOD.receive_example_from_js(myObject) + +// console.log(data) \ No newline at end of file diff --git a/tests/use_5.js b/tests/use_5.js new file mode 100644 index 0000000..772c219 --- /dev/null +++ b/tests/use_5.js @@ -0,0 +1,17 @@ +import * as MOD from '../test-5-async-without-await/pkg/test_1_simple_object.js' + +// let myObject = await MOD.send_example_to_js(331) + + +for(let i=0;i<100;i++){ + + await MOD.send_example_to_js(331) + + console.log('\n======================\n') + +} + + +// let data = await MOD.receive_example_from_js(myObject) + +// console.log(data) \ No newline at end of file diff --git a/tests/use_cusom.js b/tests/use_cusom.js new file mode 100644 index 0000000..5538f33 --- /dev/null +++ b/tests/use_cusom.js @@ -0,0 +1,24 @@ +import ContractInstance from '../test-4-no-async-imports/pkg/test_custom.js' +import fs from 'fs' + + +const bytes = fs.readFileSync('C:/Users/Acer/MyProjects/Klyntar/KlyntarVMTests/test-4-no-async-imports/pkg/test_1_simple_object_bg.wasm'); + + +let contract = new ContractInstance({},bytes) + +contract.setUpContract() + +//console.log(contract) + +let myObject = contract.takeObject(contract.wasm.send_example_to_js()) + +console.log(myObject) + +let data = await contract.takeObject(contract.wasm.receive_example_from_js(contract.addHeapObject(myObject))) + +console.log(data) + + +console.log(contract.takeObject(contract.wasm.send_example_to_js())) +console.log(contract.takeObject(contract.wasm.send_example_to_js())) \ No newline at end of file