Bindgen support (#2)

This commit is contained in:
Dima 2020-09-30 11:42:18 +03:00 committed by GitHub
parent 0956f1ab0c
commit f6541aae32
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 156 additions and 36 deletions

76
Cargo.lock generated
View File

@ -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"

View File

@ -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"

30
src/execution.rs Normal file
View File

@ -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::<Vec<Instruction>>(&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
}

View File

@ -39,7 +39,7 @@ pub(crate) enum Instruction {
impl ExecutableInstruction for Instruction {
fn execute(self, data: &mut HashMap<String, Vec<u8>, RandomState>) {
match self {
Instruction::Null => {},
Instruction::Null => {}
Instruction::Call(call) => call.execute(data),
}
}

40
src/lib.rs Normal file
View File

@ -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;
}

View File

@ -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::<Vec<Instruction>>(&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]

View File

@ -14,8 +14,7 @@
* limitations under the License.
*/
use crate::Instruction;
use crate::instructions::Instruction;
use std::collections::HashMap;
pub(crate) trait ExecutableInstruction {

View File

@ -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<String>,