From f6541aae32383ab72d64bc131982463cc668e049 Mon Sep 17 00:00:00 2001 From: Dima Date: Wed, 30 Sep 2020 11:42:18 +0300 Subject: [PATCH] Bindgen support (#2) --- Cargo.lock | 76 ++++++++++++++++++++++++++++++++++++++--- Cargo.toml | 5 +++ src/execution.rs | 30 ++++++++++++++++ src/instructions/mod.rs | 4 +-- src/lib.rs | 40 ++++++++++++++++++++++ src/main.rs | 32 +++-------------- src/stepper.rs | 3 +- src/stepper_outcome.rs | 2 ++ 8 files changed, 156 insertions(+), 36 deletions(-) create mode 100644 src/execution.rs create mode 100644 src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index ff1a6999..5c1fe17f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -8,9 +8,17 @@ dependencies = [ "log", "serde", "serde_derive", + "serde_json", "serde_sexpr", + "wasm-bindgen", ] +[[package]] +name = "bumpalo" +version = "3.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2e8c087f005730276d1096a652e92a8bacee2e2472bcc9715a74d2bec38b5820" + [[package]] name = "cfg-if" version = "0.1.10" @@ -20,7 +28,7 @@ checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822" [[package]] name = "fluence" version = "0.2.7" -source = "git+https://github.com/fluencelabs/rust-sdk#d63c6e87404348331629ddd4c6fa67d56ddc5924" +source = "git+https://github.com/fluencelabs/rust-sdk#e5d564d4a61e203798383b01505ba23e54e71912" dependencies = [ "fluence-sdk-macro", "fluence-sdk-main", @@ -29,7 +37,7 @@ dependencies = [ [[package]] name = "fluence-sdk-macro" version = "0.2.7" -source = "git+https://github.com/fluencelabs/rust-sdk#d63c6e87404348331629ddd4c6fa67d56ddc5924" +source = "git+https://github.com/fluencelabs/rust-sdk#e5d564d4a61e203798383b01505ba23e54e71912" dependencies = [ "fluence-sdk-wit", ] @@ -37,7 +45,7 @@ dependencies = [ [[package]] name = "fluence-sdk-main" version = "0.2.7" -source = "git+https://github.com/fluencelabs/rust-sdk#d63c6e87404348331629ddd4c6fa67d56ddc5924" +source = "git+https://github.com/fluencelabs/rust-sdk#e5d564d4a61e203798383b01505ba23e54e71912" dependencies = [ "fluence-sdk-macro", "log", @@ -47,7 +55,7 @@ dependencies = [ [[package]] name = "fluence-sdk-wit" version = "0.2.7" -source = "git+https://github.com/fluencelabs/rust-sdk#d63c6e87404348331629ddd4c6fa67d56ddc5924" +source = "git+https://github.com/fluencelabs/rust-sdk#e5d564d4a61e203798383b01505ba23e54e71912" dependencies = [ "proc-macro2", "quote", @@ -74,6 +82,12 @@ version = "0.4.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dc6f3ad7b9d11a0c00842ff8de1b60ee58661048eb8049ed33c73594f359d7e6" +[[package]] +name = "lazy_static" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" + [[package]] name = "libc" version = "0.2.77" @@ -254,3 +268,57 @@ name = "wasi" version = "0.9.0+wasi-snapshot-preview1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cccddf32554fecc6acb585f82a32a72e28b48f8c4c1883ddfeeeaa96f7d8e519" + +[[package]] +name = "wasm-bindgen" +version = "0.2.68" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1ac64ead5ea5f05873d7c12b545865ca2b8d28adfc50a49b84770a3a97265d42" +dependencies = [ + "cfg-if", + "wasm-bindgen-macro", +] + +[[package]] +name = "wasm-bindgen-backend" +version = "0.2.68" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f22b422e2a757c35a73774860af8e112bff612ce6cb604224e8e47641a9e4f68" +dependencies = [ + "bumpalo", + "lazy_static", + "log", + "proc-macro2", + "quote", + "syn", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-macro" +version = "0.2.68" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6b13312a745c08c469f0b292dd2fcd6411dba5f7160f593da6ef69b64e407038" +dependencies = [ + "quote", + "wasm-bindgen-macro-support", +] + +[[package]] +name = "wasm-bindgen-macro-support" +version = "0.2.68" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f249f06ef7ee334cc3b8ff031bfc11ec99d00f34d86da7498396dc1e3b1498fe" +dependencies = [ + "proc-macro2", + "quote", + "syn", + "wasm-bindgen-backend", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-shared" +version = "0.2.68" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1d649a3145108d7d3fbcde896a468d1bd636791823c9921135218ad89be08307" diff --git a/Cargo.toml b/Cargo.toml index ae925c16..001c87f8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,6 +4,9 @@ version = "0.1.0" authors = ["Fluence Labs"] edition = "2018" +[lib] +crate-type = ["cdylib"] + [[bin]] name = "aquamarine" path = "src/main.rs" @@ -18,3 +21,5 @@ serde = { version = "1.0.116", features = ["derive"] } serde_derive = "1.0.116" serde_sexpr = "0.1.0" log = "0.4.11" +serde_json = "1.0" +wasm-bindgen = "0.2.68" diff --git a/src/execution.rs b/src/execution.rs new file mode 100644 index 00000000..b25d5cd5 --- /dev/null +++ b/src/execution.rs @@ -0,0 +1,30 @@ +use crate::instructions::Instruction; +use crate::stepper_outcome::StepperOutcome; + +pub fn exec(init_user_id: String, aqua: String, data: String) -> StepperOutcome { + log::info!( + "stepper invoked with user_id = {}, aqua = {:?}, data = {:?}", + init_user_id, + aqua, + data + ); + + let outcome = StepperOutcome { + data, + next_peer_pks: vec![init_user_id], + }; + + let parsed_aqua = match serde_sexpr::from_str::>(&aqua) { + Ok(parsed) => parsed, + Err(e) => { + log::error!("supplied aqua script can't be parsed: {:?}", e); + + return outcome; + } + }; + log::info!("parsed_aqua: {:?}", parsed_aqua); + + crate::stepper::execute(parsed_aqua); + + outcome +} diff --git a/src/instructions/mod.rs b/src/instructions/mod.rs index ed29894f..0a5faab2 100644 --- a/src/instructions/mod.rs +++ b/src/instructions/mod.rs @@ -39,8 +39,8 @@ pub(crate) enum Instruction { impl ExecutableInstruction for Instruction { fn execute(self, data: &mut HashMap, RandomState>) { match self { - Instruction::Null => {}, + Instruction::Null => {} Instruction::Call(call) => call.execute(data), } } -} \ No newline at end of file +} diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 00000000..d9f50320 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,40 @@ +/* + * Copyright 2020 Fluence Labs Limited + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +mod air; +mod execution; +mod instructions; +mod stepper; +mod stepper_outcome; + +use crate::execution::exec; +use wasm_bindgen::prelude::*; + +#[wasm_bindgen] +pub fn invoke(init_user_id: String, aqua: String, data: String) -> String { + let outcome = exec(init_user_id, aqua, data); + serde_json::to_string(&outcome).unwrap() +} + +#[wasm_bindgen] +extern "C" { + #[wasm_bindgen(js_namespace = console)] + fn log(s: &str); +} + +#[wasm_bindgen(raw_module = "../src/call_service.ts")] +extern "C" { + pub fn call_service(service_id: String, fn_name: String, args: String) -> String; +} diff --git a/src/main.rs b/src/main.rs index 0926782b..55e106e4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -14,13 +14,13 @@ * limitations under the License. */ mod air; +mod execution; mod instructions; mod stepper; mod stepper_outcome; -use instructions::Instruction; -use stepper_outcome::StepperOutcome; - +use crate::execution::exec; +use crate::stepper_outcome::StepperOutcome; use fluence::fce; pub fn main() { @@ -29,31 +29,7 @@ pub fn main() { #[fce] pub fn invoke(init_user_id: String, aqua: String, data: String) -> StepperOutcome { - log::info!( - "stepper invoked with user_id = {}, aqua = {:?}, data = {:?}", - init_user_id, - aqua, - data - ); - - let outcome = StepperOutcome { - data, - next_peer_pks: vec![init_user_id], - }; - - let parsed_aqua = match serde_sexpr::from_str::>(&aqua) { - Ok(parsed) => parsed, - Err(e) => { - log::error!("supplied aqua script can't be parsed: {:?}", e); - - return outcome; - } - }; - log::info!("parsed_aqua: {:?}", parsed_aqua); - - crate::stepper::execute(parsed_aqua); - - outcome + exec(init_user_id, aqua, data) } #[fce] diff --git a/src/stepper.rs b/src/stepper.rs index a1bdfc4a..fbb91673 100644 --- a/src/stepper.rs +++ b/src/stepper.rs @@ -14,8 +14,7 @@ * limitations under the License. */ -use crate::Instruction; - +use crate::instructions::Instruction; use std::collections::HashMap; pub(crate) trait ExecutableInstruction { diff --git a/src/stepper_outcome.rs b/src/stepper_outcome.rs index fa4f85d2..15c5ad8f 100644 --- a/src/stepper_outcome.rs +++ b/src/stepper_outcome.rs @@ -15,8 +15,10 @@ */ use fluence::fce; +use serde::{Deserialize, Serialize}; #[fce] +#[derive(Serialize, Deserialize)] pub struct StepperOutcome { pub data: String, pub next_peer_pks: Vec,