Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(wrap/rust): test harness fixes #17

Merged
merged 9 commits into from
Jul 18, 2023
2 changes: 1 addition & 1 deletion implementations/wrap-rust/URI.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
wrap://ipfs/QmNenpmWMFM5DUAp4G2NpDRc3WMFo2R3BrBYcgMNCU8Vwp
wrap://ipfs/QmS77xZ1j3i9jsNMkwm1HCYHrDzztZwSVkgXFd1bxzHMnp
Binary file modified implementations/wrap-rust/build/wrap.wasm
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,11 @@ impl CustomType {
u_opt_array_opt_array: vec![],
u_array_opt_array_array: vec![],
crazy_array: None,
object: Option<AnotherType>::new(),
object: AnotherType::new(),
opt_object: None,
object_array: vec![],
opt_object_array: None,
en: Option<CustomEnum>::_MAX_,
en: CustomEnum::_MAX_,
opt_enum: None,
enum_array: vec![],
opt_enum_array: None,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ impl TestImportEnv {

pub fn new() -> TestImportEnv {
TestImportEnv {
object: Option<TestImportAnotherObject>::new(),
object: TestImportAnotherObject::new(),
opt_object: None,
object_array: vec![],
opt_object_array: None,
en: Option<TestImportEnum>::_MAX_,
en: TestImportEnum::_MAX_,
opt_enum: None,
enum_array: vec![],
opt_enum_array: None,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ impl TestImportObject {

pub fn new() -> TestImportObject {
TestImportObject {
object: Option<TestImportAnotherObject>::new(),
object: TestImportAnotherObject::new(),
opt_object: None,
object_array: vec![],
opt_object_array: None,
en: Option<TestImportEnum>::_MAX_,
en: TestImportEnum::_MAX_,
opt_enum: None,
enum_array: vec![],
opt_enum_array: None,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,24 @@ type CustomMapValue {
type else {
else: String!
}


type Module {
method(
arg: Arg!
): Output!
}

type Arg {
prop: String!
nested: Nested!
}

type Nested {
prop: String!
}

type Output {
prop: String!
nested: Nested!
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
use serde::{Serialize, Deserialize};
use polywrap_msgpack_serde::{
wrappers::polywrap_json::JSONString,
wrappers::polywrap_bigint::BigIntWrapper
};
use polywrap_wasm_rs::{
BigInt,
BigNumber,
Map,
JSON
};
use crate::Nested;

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct Arg {
pub prop: String,
pub nested: Nested,
}

impl Arg {
pub fn new() -> Arg {
Arg {
prop: String::new(),
nested: Nested::new(),
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ impl CustomType {
u_opt_array_opt_array: vec![],
u_array_opt_array_array: vec![],
crazy_array: None,
object: Option<AnotherType>::new(),
object: AnotherType::new(),
opt_object: None,
object_array: vec![],
opt_object_array: None,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
use crate::{
method_wrapped
};
use polywrap_wasm_rs::{
abort,
invoke,
Expand All @@ -13,6 +16,9 @@ pub extern "C" fn _wrap_invoke(method_size: u32, args_size: u32, env_size: u32)
let result: Vec<u8>;

match args.method.as_str() {
"method" => {
result = method_wrapped(args.args.as_slice(), env_size);
}
_ => {
invoke::wrap_invoke_error(format!("Could not find invoke function {}", args.method));
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,20 @@ pub mod custom_map_value;
pub use custom_map_value::CustomMapValue;
pub mod _else;
pub use _else::Else;
pub mod arg;
pub use arg::Arg;
pub mod nested;
pub use nested::Nested;
pub mod output;
pub use output::Output;

pub mod module;
pub use module::{
Module,
ModuleTrait,
method_wrapped,
ArgsMethod
};

// Override print!(...) & println!(...) macros
#[macro_export]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
pub mod wrapped;
pub use wrapped::{
method_wrapped,
ArgsMethod
};

pub mod module;
pub use module::*;
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
use polywrap_msgpack_serde::{
wrappers::polywrap_json::JSONString,
wrappers::polywrap_bigint::BigIntWrapper
};
use polywrap_wasm_rs::{
BigInt,
BigNumber,
Map,
JSON
};
use crate::{
ArgsMethod,
};
use crate::Arg;
use crate::Output;

pub struct Module;

pub trait ModuleTrait {
fn method(args: ArgsMethod) -> Result<Output, String>;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
use serde::{Deserialize, Serialize};
use polywrap_msgpack_serde::{
from_slice,
to_vec,
wrappers::polywrap_json::JSONString,
wrappers::polywrap_bigint::BigIntWrapper
};
use polywrap_wasm_rs::{
BigInt,
BigNumber,
Map,
JSON,
wrap_load_env
};
use crate::module::{ModuleTrait, Module};
use crate::Arg;
use crate::Output;

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct ArgsMethod {
pub arg: Arg,
}

pub fn method_wrapped(args: &[u8], env_size: u32) -> Vec<u8> {
match from_slice::<ArgsMethod>(args) {
Ok(args) => {
let result = Module::method(ArgsMethod {
arg: args.arg,
});
match result {
Ok(res) => {
to_vec(&res).unwrap()
}
Err(e) => {
panic!("{}", e.to_string())
}
}
}
Err(e) => {
panic!("{}", e.to_string())
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use serde::{Serialize, Deserialize};
use polywrap_msgpack_serde::{
wrappers::polywrap_json::JSONString,
wrappers::polywrap_bigint::BigIntWrapper
};
use polywrap_wasm_rs::{
BigInt,
BigNumber,
Map,
JSON
};

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct Nested {
pub prop: String,
}

impl Nested {
pub fn new() -> Nested {
Nested {
prop: String::new(),
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
use serde::{Serialize, Deserialize};
use polywrap_msgpack_serde::{
wrappers::polywrap_json::JSONString,
wrappers::polywrap_bigint::BigIntWrapper
};
use polywrap_wasm_rs::{
BigInt,
BigNumber,
Map,
JSON
};
use crate::Nested;

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct Output {
pub prop: String,
pub nested: Nested,
}

impl Output {
pub fn new() -> Output {
Output {
prop: String::new(),
nested: Nested::new(),
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
scalar UInt
scalar UInt8
scalar UInt16
scalar UInt32
scalar Int
scalar Int8
scalar Int16
scalar Int32
scalar Bytes
scalar BigInt
scalar BigNumber
scalar JSON
scalar Map

type EnvObject {
prop: String!
}

enum EnvEnum {
FIRST
SECOND
}

type Env {
str: String!
optStr: String
optFilledStr: String
number: Int8!
optNumber: Int8
bool: Boolean!
optBool: Boolean
en: EnvEnum!
optEnum: EnvEnum
object: EnvObject!
optObject: EnvObject
array: [UInt32!]!
}

type Module {
methodNoEnv(
arg: String!
): String!

methodRequireEnv: Env! @env(required: true)

methodOptionalEnv: Env @env(required: false)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
use crate::{
method_no_env_wrapped,
method_require_env_wrapped,
method_optional_env_wrapped
};
use polywrap_wasm_rs::{
abort,
invoke,
InvokeArgs,
};

#[no_mangle]
pub extern "C" fn _wrap_invoke(method_size: u32, args_size: u32, env_size: u32) -> bool {
// Ensure the abort handler is properly setup
abort::wrap_abort_setup();

let args: InvokeArgs = invoke::wrap_invoke_args(method_size, args_size);
let result: Vec<u8>;

match args.method.as_str() {
"methodNoEnv" => {
result = method_no_env_wrapped(args.args.as_slice(), env_size);
}
"methodRequireEnv" => {
result = method_require_env_wrapped(args.args.as_slice(), env_size);
}
"methodOptionalEnv" => {
result = method_optional_env_wrapped(args.args.as_slice(), env_size);
}
_ => {
invoke::wrap_invoke_error(format!("Could not find invoke function {}", args.method));
return false;
}
};
invoke::wrap_invoke_result(result);
return true;
}
Loading
Loading