1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
// Copyright 2017-2020 Parity Technologies (UK) Ltd.
// This file is part of Substrate.

// Substrate is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// Substrate is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with Substrate.  If not, see <http://www.gnu.org/licenses/>.

//! Utilities for defining the wasm host environment.

/// Converts arguments into respective WASM types.
#[macro_export]
macro_rules! convert_args {
	() => ([]);
	( $( $t:ty ),* ) => ( [ $( <$t as $crate::sp_wasm_interface::IntoValue>::VALUE_TYPE, )* ] );
}

/// Generates a WASM signature for given list of parameters.
#[macro_export]
macro_rules! gen_signature {
	( ( $( $params: ty ),* ) ) => (
		$crate::sp_wasm_interface::Signature {
			args: std::borrow::Cow::Borrowed(&convert_args!( $( $params ),* )[..]),
			return_value: None,
		}
	);
	( ( $( $params: ty ),* ) -> $returns:ty ) => (
		$crate::sp_wasm_interface::Signature {
			args: std::borrow::Cow::Borrowed(&convert_args!( $( $params ),* )[..]),
			return_value: Some(<$returns as $crate::sp_wasm_interface::IntoValue>::VALUE_TYPE),
		}
	);
}

macro_rules! gen_functions {
	(@INTERNAL
		{ $( $generated:tt )* }
		$context:ident,
	) => (
		vec![ $( $generated )* ]
	);
	(@INTERNAL
		{ $( $generated:tt )* }
		$context:ident,
		$name:ident ( $( $names:ident: $params:ty ),* ) $( -> $returns:ty )? { $( $body:tt )* }
		$( $tail:tt )*
	) => (
		gen_functions! {
			@INTERNAL
			{
				$( $generated )*
				{
					struct $name;

					#[allow(unused)]
					impl $crate::sp_wasm_interface::Function for $name {
						fn name(&self) -> &str {
							stringify!($name)
						}
						fn signature(&self) -> $crate::sp_wasm_interface::Signature {
							gen_signature!( ( $( $params ),* ) $( -> $returns )? )
						}
						fn execute(
							&self,
							context: &mut dyn $crate::sp_wasm_interface::FunctionContext,
							args: &mut dyn Iterator<Item=$crate::sp_wasm_interface::Value>,
						) -> ::std::result::Result<Option<$crate::sp_wasm_interface::Value>, String> {
							let mut $context = context;
							marshall! {
								args,
								( $( $names : $params ),* ) $( -> $returns )? => { $( $body )* }
							}
						}
					}

					&$name as &dyn $crate::sp_wasm_interface::Function
				},
			}
			$context,
			$( $tail )*
		}
	);

	( $context:ident, $( $tail:tt )* ) => (
		gen_functions!(@INTERNAL {} $context, $($tail)*);
	);
}

/// Converts the list of arguments coming from WASM into their native types.
#[macro_export]
macro_rules! unmarshall_args {
	( $body:tt, $args_iter:ident, $( $names:ident : $params:ty ),*) => ({
		$(
			let $names : $params =
				$args_iter.next()
					.and_then(|val| <$params as $crate::sp_wasm_interface::TryFromValue>::try_from_value(val))
					.expect(
						"`$args_iter` comes from an argument of Externals::execute_function;
						args to an external call always matches the signature of the external;
						external signatures are built with count and types and in order defined by `$params`;
						here, we iterating on `$params`;
						qed;
						"
					);
		)*
		$body
	})
}

/// Since we can't specify the type of closure directly at binding site:
///
/// ```nocompile
/// let f: FnOnce() -> Result<<u32 as ConvertibleToWasm>::NativeType, _> = || { /* ... */ };
/// ```
///
/// we use this function to constrain the type of the closure.
#[inline(always)]
pub fn constrain_closure<R, F>(f: F) -> F
where
	F: FnOnce() -> Result<R, String>
{
	f
}

/// Pass the list of parameters by converting them to respective WASM types.
#[macro_export]
macro_rules! marshall {
	( $args_iter:ident, ( $( $names:ident : $params:ty ),* ) -> $returns:ty => $body:tt ) => ({
		let body = $crate::wasm_utils::constrain_closure::<$returns, _>(|| {
			unmarshall_args!($body, $args_iter, $( $names : $params ),*)
		});
		let r = body()?;
		return Ok(Some($crate::sp_wasm_interface::IntoValue::into_value(r)))
	});
	( $args_iter:ident, ( $( $names:ident : $params:ty ),* ) => $body:tt ) => ({
		let body = $crate::wasm_utils::constrain_closure::<(), _>(|| {
			unmarshall_args!($body, $args_iter, $( $names : $params ),*)
		});
		body()?;
		return Ok(None)
	})
}

/// Implements the wasm host interface for the given type.
#[macro_export]
macro_rules! impl_wasm_host_interface {
	(
		impl $interface_name:ident where $context:ident {
			$(
				$name:ident($( $names:ident : $params:ty ),* $(,)? ) $( -> $returns:ty )?
				{ $( $body:tt )* }
			)*
		}
	) => (
		impl $crate::sp_wasm_interface::HostFunctions for $interface_name {
			#[allow(non_camel_case_types)]
			fn host_functions() -> Vec<&'static dyn $crate::sp_wasm_interface::Function> {
				gen_functions!(
					$context,
					$( $name( $( $names: $params ),* ) $( -> $returns )? { $( $body )* } )*
				)
			}
		}
	);
}