mirror of
https://github.com/fluencelabs/aquavm
synced 2025-06-24 12:11:33 +00:00
New data format (#12)
This commit is contained in:
.circleci
CHANGELOG.mdCargo.lockCargo.tomlcrates/test-utils/src
stepper-lib
.rustfmt.tomlCargo.toml
src
tests
stepper
@ -33,8 +33,11 @@ use aquamarine_vm::HostImportDescriptor;
|
||||
use aquamarine_vm::IType;
|
||||
use aquamarine_vm::IValue;
|
||||
|
||||
use std::collections::HashMap;
|
||||
use std::path::PathBuf;
|
||||
|
||||
type JValue = serde_json::Value;
|
||||
|
||||
pub fn create_aqua_vm(
|
||||
call_service: HostExportedFunc,
|
||||
current_peer_id: impl Into<String>,
|
||||
@ -46,10 +49,14 @@ pub fn create_aqua_vm(
|
||||
error_handler: None,
|
||||
};
|
||||
|
||||
let tmp_dir = std::env::temp_dir();
|
||||
|
||||
let config = AquamarineVMConfig {
|
||||
aquamarine_wasm_path: PathBuf::from("../target/wasm32-wasi/debug/aquamarine.wasm"),
|
||||
call_service: call_service_descriptor,
|
||||
current_peer_id: current_peer_id.into(),
|
||||
particle_data_store: tmp_dir,
|
||||
logging_mask: i64::max_value(),
|
||||
};
|
||||
|
||||
AquamarineVM::new(config).expect("vm should be created")
|
||||
@ -100,3 +107,47 @@ pub fn echo_number_call_service() -> HostExportedFunc {
|
||||
))
|
||||
})
|
||||
}
|
||||
|
||||
pub fn set_variable_call_service(json: impl Into<String>) -> HostExportedFunc {
|
||||
let json = json.into();
|
||||
Box::new(move |_, _| -> Option<IValue> {
|
||||
Some(IValue::Record(
|
||||
Vec1::new(vec![IValue::S32(0), IValue::String(json.clone())]).unwrap(),
|
||||
))
|
||||
})
|
||||
}
|
||||
|
||||
pub fn set_variables_call_service(ret_mapping: HashMap<String, String>) -> HostExportedFunc {
|
||||
Box::new(move |_, args| -> Option<IValue> {
|
||||
let arg_name = match &args[2] {
|
||||
IValue::String(json_str) => {
|
||||
let json = serde_json::from_str(json_str).expect("a valid json");
|
||||
match json {
|
||||
JValue::Array(array) => match array.first() {
|
||||
Some(JValue::String(str)) => str.to_string(),
|
||||
_ => String::from("default"),
|
||||
},
|
||||
_ => String::from("default"),
|
||||
}
|
||||
}
|
||||
_ => String::from("default"),
|
||||
};
|
||||
|
||||
let result = ret_mapping
|
||||
.get(&arg_name)
|
||||
.cloned()
|
||||
.unwrap_or(String::from(r#""test""#));
|
||||
|
||||
Some(IValue::Record(
|
||||
Vec1::new(vec![IValue::S32(0), IValue::String(result.clone())]).unwrap(),
|
||||
))
|
||||
})
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! call_vm {
|
||||
($vm:expr, $init_user_id:expr, $script:expr, $prev_data:expr, $data:expr) => {
|
||||
$vm.call_with_prev_data($init_user_id, $script, $prev_data, $data)
|
||||
.expect("call should be successful");
|
||||
};
|
||||
}
|
||||
|
Reference in New Issue
Block a user