Skip to content

Commit

Permalink
runtime: Add arity to invoke_export test function name
Browse files Browse the repository at this point in the history
  • Loading branch information
evaporei committed Sep 8, 2021
1 parent 090d6d7 commit 911dcc3
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 40 deletions.
30 changes: 15 additions & 15 deletions runtime/test/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ fn test_module(
trait WasmInstanceExt {
fn invoke_export0_void(&self, f: &str);
fn invoke_export0<R>(&self, f: &str) -> AscPtr<R>;
fn invoke_export<C, R>(&self, f: &str, arg: AscPtr<C>) -> AscPtr<R>;
fn invoke_export1<C, R>(&self, f: &str, arg: AscPtr<C>) -> AscPtr<R>;
fn invoke_export2<C, D, R>(&self, f: &str, arg0: AscPtr<C>, arg1: AscPtr<D>) -> AscPtr<R>;
fn invoke_export2_void<C, D>(
&self,
Expand All @@ -145,7 +145,7 @@ impl WasmInstanceExt for WasmInstance<Chain> {
ptr.into()
}

fn invoke_export<C, R>(&self, f: &str, arg: AscPtr<C>) -> AscPtr<R> {
fn invoke_export1<C, R>(&self, f: &str, arg: AscPtr<C>) -> AscPtr<R> {
let func = self.get_func(f).typed().unwrap().clone();
let ptr: u32 = func.call(arg.wasm_ptr()).unwrap();
ptr.into()
Expand Down Expand Up @@ -210,7 +210,7 @@ fn test_json_conversions(api_version: Version) {
// test BigInt conversion
let number = "-922337203685077092345034";
let number_ptr = asc_new(&mut module, number).unwrap();
let big_int_obj: AscPtr<AscBigInt> = module.invoke_export("testToBigInt", number_ptr);
let big_int_obj: AscPtr<AscBigInt> = module.invoke_export1("testToBigInt", number_ptr);
let bytes: Vec<u8> = asc_get(&module, big_int_obj).unwrap();
assert_eq!(
scalar::BigInt::from_str(number).unwrap(),
Expand Down Expand Up @@ -242,15 +242,15 @@ fn test_json_parsing(api_version: Version) {
let s = "foo"; // Invalid because there are no quotes around `foo`
let bytes: &[u8] = s.as_ref();
let bytes_ptr = asc_new(&mut module, bytes).unwrap();
let return_value: AscPtr<AscString> = module.invoke_export("handleJsonError", bytes_ptr);
let return_value: AscPtr<AscString> = module.invoke_export1("handleJsonError", bytes_ptr);
let output: String = asc_get(&module, return_value).unwrap();
assert_eq!(output, "ERROR: true");

// Parse valid JSON and get it back
let s = "\"foo\""; // Valid because there are quotes around `foo`
let bytes: &[u8] = s.as_ref();
let bytes_ptr = asc_new(&mut module, bytes).unwrap();
let return_value: AscPtr<AscString> = module.invoke_export("handleJsonError", bytes_ptr);
let return_value: AscPtr<AscString> = module.invoke_export1("handleJsonError", bytes_ptr);
let output: String = asc_get(&module, return_value).unwrap();
assert_eq!(output, "OK: foo, ERROR: false");
}
Expand Down Expand Up @@ -284,7 +284,7 @@ async fn test_ipfs_cat(api_version: Version) {
api_version,
);
let arg = asc_new(&mut module, &hash).unwrap();
let converted: AscPtr<AscString> = module.invoke_export("ipfsCatString", arg);
let converted: AscPtr<AscString> = module.invoke_export1("ipfsCatString", arg);
let data: String = asc_get(&module, converted).unwrap();
assert_eq!(data, "42");
})
Expand Down Expand Up @@ -495,7 +495,7 @@ async fn test_ipfs_fail(api_version: Version) {

let hash = asc_new(&mut module, "invalid hash").unwrap();
assert!(module
.invoke_export::<_, AscString>("ipfsCat", hash)
.invoke_export1::<_, AscString>("ipfsCat", hash)
.is_null());
})
.join()
Expand Down Expand Up @@ -524,7 +524,7 @@ fn test_crypto_keccak256(api_version: Version) {
let input: &[u8] = "eth".as_ref();
let input: AscPtr<Uint8Array> = asc_new(&mut module, input).unwrap();

let hash: AscPtr<Uint8Array> = module.invoke_export("hash", input);
let hash: AscPtr<Uint8Array> = module.invoke_export1("hash", input);
let hash: Vec<u8> = asc_get(&module, hash).unwrap();
assert_eq!(
hex::encode(hash),
Expand Down Expand Up @@ -555,21 +555,21 @@ fn test_big_int_to_hex(api_version: Version) {
// Convert zero to hex
let zero = BigInt::from_unsigned_u256(&U256::zero());
let zero: AscPtr<AscBigInt> = asc_new(&mut module, &zero).unwrap();
let zero_hex_ptr: AscPtr<AscString> = module.invoke_export("big_int_to_hex", zero);
let zero_hex_ptr: AscPtr<AscString> = module.invoke_export1("big_int_to_hex", zero);
let zero_hex_str: String = asc_get(&module, zero_hex_ptr).unwrap();
assert_eq!(zero_hex_str, "0x0");

// Convert 1 to hex
let one = BigInt::from_unsigned_u256(&U256::one());
let one: AscPtr<AscBigInt> = asc_new(&mut module, &one).unwrap();
let one_hex_ptr: AscPtr<AscString> = module.invoke_export("big_int_to_hex", one);
let one_hex_ptr: AscPtr<AscString> = module.invoke_export1("big_int_to_hex", one);
let one_hex_str: String = asc_get(&module, one_hex_ptr).unwrap();
assert_eq!(one_hex_str, "0x1");

// Convert U256::max_value() to hex
let u256_max = BigInt::from_unsigned_u256(&U256::max_value());
let u256_max: AscPtr<AscBigInt> = asc_new(&mut module, &u256_max).unwrap();
let u256_max_hex_ptr: AscPtr<AscString> = module.invoke_export("big_int_to_hex", u256_max);
let u256_max_hex_ptr: AscPtr<AscString> = module.invoke_export1("big_int_to_hex", u256_max);
let u256_max_hex_str: String = asc_get(&module, u256_max_hex_ptr).unwrap();
assert_eq!(
u256_max_hex_str,
Expand Down Expand Up @@ -703,7 +703,7 @@ fn test_bytes_to_base58(api_version: Version) {
let bytes = hex::decode("12207D5A99F603F231D53A4F39D1521F98D2E8BB279CF29BEBFD0687DC98458E7F89")
.unwrap();
let bytes_ptr = asc_new(&mut module, bytes.as_slice()).unwrap();
let result_ptr: AscPtr<AscString> = module.invoke_export("bytes_to_base58", bytes_ptr);
let result_ptr: AscPtr<AscString> = module.invoke_export1("bytes_to_base58", bytes_ptr);
let base58: String = asc_get(&module, result_ptr).unwrap();
assert_eq!(base58, "QmWmyoMoctfbAaiEs2G46gpeUmhqFRDW6KWo64y5r581Vz");
}
Expand Down Expand Up @@ -785,13 +785,13 @@ fn test_ens_name_by_hash(api_version: Version) {
let name = "dealdrafts";
test_store::insert_ens_name(hash, name);
let val = asc_new(&mut module, hash).unwrap();
let converted: AscPtr<AscString> = module.invoke_export("nameByHash", val);
let converted: AscPtr<AscString> = module.invoke_export1("nameByHash", val);
let data: String = asc_get(&module, converted).unwrap();
assert_eq!(data, name);

let hash = asc_new(&mut module, "impossible keccak hash").unwrap();
assert!(module
.invoke_export::<_, AscString>("nameByHash", hash)
.invoke_export1::<_, AscString>("nameByHash", hash)
.is_null());
}

Expand Down Expand Up @@ -830,7 +830,7 @@ fn test_entity_store(api_version: Version) {

let get_user = move |module: &mut WasmInstance<Chain>, id: &str| -> Option<Entity> {
let id = asc_new(module, id).unwrap();
let entity_ptr: AscPtr<AscEntity> = module.invoke_export("getUser", id);
let entity_ptr: AscPtr<AscEntity> = module.invoke_export1("getUser", id);
if entity_ptr.is_null() {
None
} else {
Expand Down
51 changes: 26 additions & 25 deletions runtime/test/src/test/abi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ fn test_abi_array(api_version: Version) {
];
let vec_obj: AscPtr<Array<AscPtr<AscString>>> = asc_new(&mut module, &*vec).unwrap();

let new_vec_obj: AscPtr<Array<AscPtr<AscString>>> = module.invoke_export("test_array", vec_obj);
let new_vec_obj: AscPtr<Array<AscPtr<AscString>>> =
module.invoke_export1("test_array", vec_obj);
let new_vec: Vec<String> = asc_get(&module, new_vec_obj).unwrap();

assert_eq!(
Expand Down Expand Up @@ -116,7 +117,7 @@ fn test_abi_subarray(api_version: Version) {
let vec_obj: AscPtr<TypedArray<u8>> = asc_new(&mut module, &*vec).unwrap();

let new_vec_obj: AscPtr<TypedArray<u8>> =
module.invoke_export("byte_array_third_quarter", vec_obj);
module.invoke_export1("byte_array_third_quarter", vec_obj);
let new_vec: Vec<u8> = asc_get(&module, new_vec_obj).unwrap();

assert_eq!(new_vec, vec![3]);
Expand Down Expand Up @@ -182,9 +183,9 @@ fn test_abi_ethabi_token_identity(api_version: Version) {

let token_address_ptr = asc_new(&mut module, &token_address).unwrap();
let new_address_obj: AscPtr<AscAddress> =
module.invoke_export("token_to_address", token_address_ptr);
module.invoke_export1("token_to_address", token_address_ptr);

let new_token_ptr = module.invoke_export("token_from_address", new_address_obj);
let new_token_ptr = module.invoke_export1("token_from_address", new_address_obj);
let new_token = asc_get(&module, new_token_ptr).unwrap();

assert_eq!(token_address, new_token);
Expand All @@ -194,9 +195,9 @@ fn test_abi_ethabi_token_identity(api_version: Version) {

let token_bytes_ptr = asc_new(&mut module, &token_bytes).unwrap();
let new_bytes_obj: AscPtr<ArrayBuffer> =
module.invoke_export("token_to_bytes", token_bytes_ptr);
module.invoke_export1("token_to_bytes", token_bytes_ptr);

let new_token_ptr = module.invoke_export("token_from_bytes", new_bytes_obj);
let new_token_ptr = module.invoke_export1("token_from_bytes", new_bytes_obj);
let new_token = asc_get(&module, new_token_ptr).unwrap();

assert_eq!(token_bytes, new_token);
Expand All @@ -205,9 +206,9 @@ fn test_abi_ethabi_token_identity(api_version: Version) {
let int_token = Token::Int(U256([256, 453452345, 0, 42]));

let int_token_ptr = asc_new(&mut module, &int_token).unwrap();
let new_int_obj: AscPtr<ArrayBuffer> = module.invoke_export("token_to_int", int_token_ptr);
let new_int_obj: AscPtr<ArrayBuffer> = module.invoke_export1("token_to_int", int_token_ptr);

let new_token_ptr = module.invoke_export("token_from_int", new_int_obj);
let new_token_ptr = module.invoke_export1("token_from_int", new_int_obj);
let new_token = asc_get(&module, new_token_ptr).unwrap();

assert_eq!(int_token, new_token);
Expand All @@ -216,9 +217,9 @@ fn test_abi_ethabi_token_identity(api_version: Version) {
let uint_token = Token::Uint(U256([256, 453452345, 0, 42]));

let uint_token_ptr = asc_new(&mut module, &uint_token).unwrap();
let new_uint_obj: AscPtr<ArrayBuffer> = module.invoke_export("token_to_uint", uint_token_ptr);
let new_uint_obj: AscPtr<ArrayBuffer> = module.invoke_export1("token_to_uint", uint_token_ptr);

let new_token_ptr = module.invoke_export("token_from_uint", new_uint_obj);
let new_token_ptr = module.invoke_export1("token_from_uint", new_uint_obj);
let new_token = asc_get(&module, new_token_ptr).unwrap();

assert_eq!(uint_token, new_token);
Expand All @@ -241,9 +242,9 @@ fn test_abi_ethabi_token_identity(api_version: Version) {

let token_string_ptr = asc_new(&mut module, &token_string).unwrap();
let new_string_obj: AscPtr<AscString> =
module.invoke_export("token_to_string", token_string_ptr);
module.invoke_export1("token_to_string", token_string_ptr);

let new_token_ptr = module.invoke_export("token_from_string", new_string_obj);
let new_token_ptr = module.invoke_export1("token_from_string", new_string_obj);
let new_token = asc_get(&module, new_token_ptr).unwrap();

assert_eq!(token_string, new_token);
Expand All @@ -254,9 +255,9 @@ fn test_abi_ethabi_token_identity(api_version: Version) {

let new_array_ptr = asc_new(&mut module, &token_array_nested).unwrap();
let new_array_obj: AscEnumArray<EthereumValueKind> =
module.invoke_export("token_to_array", new_array_ptr);
module.invoke_export1("token_to_array", new_array_ptr);

let new_token_ptr = module.invoke_export("token_from_array", new_array_obj);
let new_token_ptr = module.invoke_export1("token_from_array", new_array_obj);
let new_token: Token = asc_get(&module, new_token_ptr).unwrap();

assert_eq!(new_token, token_array_nested);
Expand Down Expand Up @@ -296,7 +297,7 @@ fn test_abi_store_value(api_version: Version) {
// Value::String
let string = "some string";
let string_ptr = asc_new(&mut module, string).unwrap();
let new_value_ptr = module.invoke_export("value_from_string", string_ptr);
let new_value_ptr = module.invoke_export1("value_from_string", string_ptr);
let new_value: Value = try_asc_get(&module, new_value_ptr).unwrap();
assert_eq!(new_value, Value::from(string));

Expand All @@ -309,13 +310,13 @@ fn test_abi_store_value(api_version: Version) {
// Value::BigDecimal
let big_decimal = BigDecimal::from_str("3.14159001").unwrap();
let big_decimal_ptr = asc_new(&mut module, &big_decimal).unwrap();
let new_value_ptr = module.invoke_export("value_from_big_decimal", big_decimal_ptr);
let new_value_ptr = module.invoke_export1("value_from_big_decimal", big_decimal_ptr);
let new_value: Value = try_asc_get(&module, new_value_ptr).unwrap();
assert_eq!(new_value, Value::BigDecimal(big_decimal));

let big_decimal = BigDecimal::new(10.into(), 5);
let big_decimal_ptr = asc_new(&mut module, &big_decimal).unwrap();
let new_value_ptr = module.invoke_export("value_from_big_decimal", big_decimal_ptr);
let new_value_ptr = module.invoke_export1("value_from_big_decimal", big_decimal_ptr);
let new_value: Value = try_asc_get(&module, new_value_ptr).unwrap();
assert_eq!(new_value, Value::BigDecimal(1_000_000.into()));

Expand Down Expand Up @@ -347,7 +348,7 @@ fn test_abi_store_value(api_version: Version) {
Value::String("bar".to_owned()),
];
let array_ptr = asc_new(&mut module, array).unwrap();
let new_value_ptr = module.invoke_export("value_from_array", array_ptr);
let new_value_ptr = module.invoke_export1("value_from_array", array_ptr);
let new_value: Value = try_asc_get(&module, new_value_ptr).unwrap();
assert_eq!(
new_value,
Expand All @@ -360,14 +361,14 @@ fn test_abi_store_value(api_version: Version) {
// Value::Bytes
let bytes: &[u8] = &[0, 2, 5];
let bytes_ptr: AscPtr<Uint8Array> = asc_new(&mut module, bytes).unwrap();
let new_value_ptr = module.invoke_export("value_from_bytes", bytes_ptr);
let new_value_ptr = module.invoke_export1("value_from_bytes", bytes_ptr);
let new_value: Value = try_asc_get(&module, new_value_ptr).unwrap();
assert_eq!(new_value, Value::Bytes(bytes.into()));

// Value::BigInt
let bytes: &[u8] = &[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1];
let bytes_ptr: AscPtr<Uint8Array> = asc_new(&mut module, bytes).unwrap();
let new_value_ptr = module.invoke_export("value_from_bigint", bytes_ptr);
let new_value_ptr = module.invoke_export1("value_from_bigint", bytes_ptr);
let new_value: Value = try_asc_get(&module, new_value_ptr).unwrap();
assert_eq!(
new_value,
Expand Down Expand Up @@ -398,7 +399,7 @@ fn test_abi_h160(api_version: Version) {

// As an `Uint8Array`
let array_buffer: AscPtr<Uint8Array> = asc_new(&mut module, &address).unwrap();
let new_address_obj: AscPtr<Uint8Array> = module.invoke_export("test_address", array_buffer);
let new_address_obj: AscPtr<Uint8Array> = module.invoke_export1("test_address", array_buffer);

// This should have 1 added to the first and last byte.
let new_address: H160 = asc_get(&module, new_address_obj).unwrap();
Expand Down Expand Up @@ -431,7 +432,7 @@ fn test_string(api_version: Version) {
let string = " 漢字Double_Me🇧🇷 ";
let trimmed_string_ptr = asc_new(&mut module, string).unwrap();
let trimmed_string_obj: AscPtr<AscString> =
module.invoke_export("repeat_twice", trimmed_string_ptr);
module.invoke_export1("repeat_twice", trimmed_string_ptr);
let doubled_string: String = asc_get(&module, trimmed_string_obj).unwrap();
assert_eq!(doubled_string, string.repeat(2));
}
Expand Down Expand Up @@ -460,7 +461,7 @@ fn test_abi_big_int(api_version: Version) {
let old_uint = U256::zero();
let array_buffer: AscPtr<AscBigInt> =
asc_new(&mut module, &BigInt::from_unsigned_u256(&old_uint)).unwrap();
let new_uint_obj: AscPtr<AscBigInt> = module.invoke_export("test_uint", array_buffer);
let new_uint_obj: AscPtr<AscBigInt> = module.invoke_export1("test_uint", array_buffer);
let new_uint: BigInt = asc_get(&module, new_uint_obj).unwrap();
assert_eq!(new_uint, BigInt::from(1 as i32));
let new_uint = new_uint.to_unsigned_u256();
Expand All @@ -469,7 +470,7 @@ fn test_abi_big_int(api_version: Version) {
// Test passing in -50 and increment it by 1
let old_uint = BigInt::from(-50);
let array_buffer: AscPtr<AscBigInt> = asc_new(&mut module, &old_uint).unwrap();
let new_uint_obj: AscPtr<AscBigInt> = module.invoke_export("test_uint", array_buffer);
let new_uint_obj: AscPtr<AscBigInt> = module.invoke_export1("test_uint", array_buffer);
let new_uint: BigInt = asc_get(&module, new_uint_obj).unwrap();
assert_eq!(new_uint, BigInt::from(-49 as i32));
let new_uint_from_u256 = BigInt::from_signed_u256(&new_uint.to_signed_u256());
Expand Down Expand Up @@ -499,7 +500,7 @@ fn test_big_int_to_string(api_version: Version) {
let big_int_str = "30145144166666665000000000000000000";
let big_int = BigInt::from_str(big_int_str).unwrap();
let ptr: AscPtr<AscBigInt> = asc_new(&mut module, &big_int).unwrap();
let string_obj: AscPtr<AscString> = module.invoke_export("big_int_to_string", ptr);
let string_obj: AscPtr<AscString> = module.invoke_export1("big_int_to_string", ptr);
let string: String = asc_get(&module, string_obj).unwrap();
assert_eq!(string, big_int_str);
}
Expand Down

0 comments on commit 911dcc3

Please sign in to comment.