Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Prev Previous commit
Next Next commit
Fix macros
Specifically, make fixes that allows to use a function without arguments (apart from ctx).
For example:

```rust
ext_return_42(ctx) -> u32 => {
    Ok(42)
}
```

Also, add impl ConvertibleToWasm for u64.
  • Loading branch information
pepyakin committed Aug 30, 2018
commit c6e7c54ffe9d91851b5b452ae9e20b05cfff3194
10 changes: 5 additions & 5 deletions substrate/runtime/contract/src/vm/env_def/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

#[macro_export]
macro_rules! convert_args {
() => ([]);
() => (vec![]);
( $( $t:ty ),* ) => ( vec![ $( { use $crate::vm::env_def::ConvertibleToWasm; <$t>::VALUE_TYPE }, )* ] );
}

Expand Down Expand Up @@ -90,7 +90,7 @@ macro_rules! unmarshall_then_body_then_marshall {
unmarshall_then_body!($body, $ctx, $args_iter, $( $names : $params ),*)
});
let r = body()?;
return Ok(ReturnValue::Value({ use $crate::vm::env_def::ConvertibleToWasm; r.to_typed_value() }))
return Ok($crate::sandbox::ReturnValue::Value({ use $crate::vm::env_def::ConvertibleToWasm; r.to_typed_value() }))
});
( $args_iter:ident, $ctx:ident, ( $( $names:ident : $params:ty ),* ) => $body:tt ) => ({
let body = $crate::vm::env_def::macros::constrain_closure::<(), _>(|| {
Expand All @@ -103,7 +103,7 @@ macro_rules! unmarshall_then_body_then_marshall {

#[macro_export]
macro_rules! define_func {
( < E: $ext_ty:tt > $name:ident ( $ctx: ident, $($names:ident : $params:ty),*) $(-> $returns:ty)* => $body:tt ) => {
( < E: $ext_ty:tt > $name:ident ( $ctx: ident $(, $names:ident : $params:ty)*) $(-> $returns:ty)* => $body:tt ) => {
fn $name< E: $ext_ty >(
$ctx: &mut $crate::vm::Runtime<E>,
args: &[$crate::sandbox::TypedValue],
Expand All @@ -129,7 +129,7 @@ macro_rules! define_func {
/// and reject the code if any imported function has a mismached signature.
macro_rules! define_env {
( $init_name:ident , < E: $ext_ty:tt > ,
$( $name:ident ( $ctx:ident, $( $names:ident : $params:ty ),* )
$( $name:ident ( $ctx:ident $( , $names:ident : $params:ty )* )
$( -> $returns:ty )* => $body:tt , )*
) => {
pub(crate) fn $init_name<E: Ext>() -> HostFunctionSet<E> {
Expand All @@ -142,7 +142,7 @@ macro_rules! define_env {
gen_signature!( ( $( $params ),* ) $( -> $returns )* ),
{
define_func!(
< E: $ext_ty > $name ( $ctx, $( $names : $params ),* ) $( -> $returns )* => $body
< E: $ext_ty > $name ( $ctx $(, $names : $params )* ) $( -> $returns )* => $body
);
$name::<E>
},
Expand Down
18 changes: 17 additions & 1 deletion substrate/runtime/contract/src/vm/env_def/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,23 @@ impl ConvertibleToWasm for u32 {
TypedValue::I32(self as i32)
}
fn from_typed_value(v: TypedValue) -> Option<Self> {
v.as_i32().map(|v| v as u32)
match v {
TypedValue::I32(v) => Some(v as u32),
_ => None,
}
}
}
impl ConvertibleToWasm for u64 {
type NativeType = u64;
const VALUE_TYPE: ValueType = ValueType::I64;
fn to_typed_value(self) -> TypedValue {
TypedValue::I64(self as i64)
}
fn from_typed_value(v: TypedValue) -> Option<Self> {
match v {
TypedValue::I64(v) => Some(v as u64),
_ => None,
}
}
}

Expand Down