@@ -30,7 +30,7 @@ use sp_runtime::{
3030 traits:: { BlakeTwo256 , Hash } ,
3131} ;
3232#[ cfg( not( feature = "std" ) ) ]
33- use sp_sandbox:: Value ;
33+ use sp_sandbox:: { SandboxEnvironmentBuilder , SandboxInstance , SandboxMemory , Value } ;
3434
3535extern "C" {
3636 #[ allow( dead_code) ]
@@ -183,61 +183,6 @@ sp_core::wasm_export_functions! {
183183 ) . as_ref( ) . to_vec( )
184184 }
185185
186- fn test_sandbox( code: Vec <u8 >) -> bool {
187- execute_sandboxed( & code, & [ ] ) . is_ok( )
188- }
189-
190- fn test_sandbox_args( code: Vec <u8 >) -> bool {
191- execute_sandboxed(
192- & code,
193- & [
194- Value :: I32 ( 0x12345678 ) ,
195- Value :: I64 ( 0x1234567887654321 ) ,
196- ] ,
197- ) . is_ok( )
198- }
199-
200- fn test_sandbox_return_val( code: Vec <u8 >) -> bool {
201- let ok = match execute_sandboxed(
202- & code,
203- & [
204- Value :: I32 ( 0x1336 ) ,
205- ]
206- ) {
207- Ok ( sp_sandbox:: ReturnValue :: Value ( Value :: I32 ( 0x1337 ) ) ) => true ,
208- _ => false ,
209- } ;
210-
211- ok
212- }
213-
214- fn test_sandbox_instantiate( code: Vec <u8 >) -> u8 {
215- let env_builder = sp_sandbox:: EnvironmentDefinitionBuilder :: new( ) ;
216- let code = match sp_sandbox:: Instance :: new( & code, & env_builder, & mut ( ) ) {
217- Ok ( _) => 0 ,
218- Err ( sp_sandbox:: Error :: Module ) => 1 ,
219- Err ( sp_sandbox:: Error :: Execution ) => 2 ,
220- Err ( sp_sandbox:: Error :: OutOfBounds ) => 3 ,
221- } ;
222-
223- code
224- }
225-
226- fn test_sandbox_get_global_val( code: Vec <u8 >) -> i64 {
227- let env_builder = sp_sandbox:: EnvironmentDefinitionBuilder :: new( ) ;
228- let instance = if let Ok ( i) = sp_sandbox:: Instance :: new( & code, & env_builder, & mut ( ) ) {
229- i
230- } else {
231- return 20 ;
232- } ;
233-
234- match instance. get_global_val( "test_global" ) {
235- Some ( sp_sandbox:: Value :: I64 ( val) ) => val,
236- None => 30 ,
237- _ => 40 ,
238- }
239- }
240-
241186 fn test_offchain_index_set( ) {
242187 sp_io:: offchain_index:: set( b"k" , b"v" ) ;
243188 }
@@ -408,15 +353,112 @@ mod tasks {
408353 }
409354}
410355
356+ /// A macro to define a test entrypoint for each available sandbox executor.
357+ macro_rules! wasm_export_sandbox_test_functions {
358+ (
359+ $(
360+ fn $name: ident<T >(
361+ $( $arg_name: ident: $arg_ty: ty ) ,* $( , ) ?
362+ ) $( -> $ret_ty: ty ) ? where T : SandboxInstance <$state: ty> $( , ) ?
363+ { $( $fn_impl: tt ) * }
364+ ) *
365+ ) => {
366+ $(
367+ #[ cfg( not( feature = "std" ) ) ]
368+ fn $name<T >( $( $arg_name: $arg_ty) ,* ) $( -> $ret_ty ) ? where T : SandboxInstance <$state> {
369+ $( $fn_impl ) *
370+ }
371+
372+ paste:: paste! {
373+ sp_core:: wasm_export_functions! {
374+ fn [ <$name _host>] ( $( $arg_name: $arg_ty) ,* ) $( -> $ret_ty ) ? {
375+ $name:: <sp_sandbox:: host_executor:: Instance <$state>>( $( $arg_name ) ,* )
376+ }
377+
378+ fn [ <$name _embedded>] ( $( $arg_name: $arg_ty) ,* ) $( -> $ret_ty ) ? {
379+ $name:: <sp_sandbox:: embedded_executor:: Instance <$state>>( $( $arg_name ) ,* )
380+ }
381+ }
382+ }
383+ ) *
384+ } ;
385+ }
386+
387+ wasm_export_sandbox_test_functions ! {
388+ fn test_sandbox<T >( code: Vec <u8 >) -> bool
389+ where
390+ T : SandboxInstance <State >,
391+ {
392+ execute_sandboxed:: <T >( & code, & [ ] ) . is_ok( )
393+ }
394+
395+ fn test_sandbox_args<T >( code: Vec <u8 >) -> bool
396+ where
397+ T : SandboxInstance <State >,
398+ {
399+ execute_sandboxed:: <T >( & code, & [ Value :: I32 ( 0x12345678 ) , Value :: I64 ( 0x1234567887654321 ) ] )
400+ . is_ok( )
401+ }
402+
403+ fn test_sandbox_return_val<T >( code: Vec <u8 >) -> bool
404+ where
405+ T : SandboxInstance <State >,
406+ {
407+ let ok = match execute_sandboxed:: <T >( & code, & [ Value :: I32 ( 0x1336 ) ] ) {
408+ Ok ( sp_sandbox:: ReturnValue :: Value ( Value :: I32 ( 0x1337 ) ) ) => true ,
409+ _ => false ,
410+ } ;
411+
412+ ok
413+ }
414+
415+ fn test_sandbox_instantiate<T >( code: Vec <u8 >) -> u8
416+ where
417+ T : SandboxInstance <( ) >,
418+ {
419+ let env_builder = T :: EnvironmentBuilder :: new( ) ;
420+ let code = match T :: new( & code, & env_builder, & mut ( ) ) {
421+ Ok ( _) => 0 ,
422+ Err ( sp_sandbox:: Error :: Module ) => 1 ,
423+ Err ( sp_sandbox:: Error :: Execution ) => 2 ,
424+ Err ( sp_sandbox:: Error :: OutOfBounds ) => 3 ,
425+ } ;
426+
427+ code
428+ }
429+
430+ fn test_sandbox_get_global_val<T >( code: Vec <u8 >) -> i64
431+ where
432+ T : SandboxInstance <( ) >,
433+ {
434+ let env_builder = T :: EnvironmentBuilder :: new( ) ;
435+ let instance = if let Ok ( i) = T :: new( & code, & env_builder, & mut ( ) ) {
436+ i
437+ } else {
438+ return 20
439+ } ;
440+
441+ match instance. get_global_val( "test_global" ) {
442+ Some ( sp_sandbox:: Value :: I64 ( val) ) => val,
443+ None => 30 ,
444+ _ => 40 ,
445+ }
446+ }
447+ }
448+
411449#[ cfg( not( feature = "std" ) ) ]
412- fn execute_sandboxed (
450+ struct State {
451+ counter : u32 ,
452+ }
453+
454+ #[ cfg( not( feature = "std" ) ) ]
455+ fn execute_sandboxed < T > (
413456 code : & [ u8 ] ,
414457 args : & [ Value ] ,
415- ) -> Result < sp_sandbox:: ReturnValue , sp_sandbox:: HostError > {
416- struct State {
417- counter : u32 ,
418- }
419-
458+ ) -> Result < sp_sandbox:: ReturnValue , sp_sandbox:: HostError >
459+ where
460+ T : sp_sandbox:: SandboxInstance < State > ,
461+ {
420462 fn env_assert (
421463 _e : & mut State ,
422464 args : & [ Value ] ,
@@ -446,10 +488,10 @@ fn execute_sandboxed(
446488 let mut state = State { counter : 0 } ;
447489
448490 let env_builder = {
449- let mut env_builder = sp_sandbox :: EnvironmentDefinitionBuilder :: new ( ) ;
491+ let mut env_builder = T :: EnvironmentBuilder :: new ( ) ;
450492 env_builder. add_host_func ( "env" , "assert" , env_assert) ;
451493 env_builder. add_host_func ( "env" , "inc_counter" , env_inc_counter) ;
452- let memory = match sp_sandbox :: Memory :: new ( 1 , Some ( 16 ) ) {
494+ let memory = match T :: Memory :: new ( 1 , Some ( 16 ) ) {
453495 Ok ( m) => m,
454496 Err ( _) => unreachable ! (
455497 "
@@ -462,7 +504,7 @@ fn execute_sandboxed(
462504 env_builder
463505 } ;
464506
465- let mut instance = sp_sandbox :: Instance :: new ( code, & env_builder, & mut state) ?;
507+ let mut instance = T :: new ( code, & env_builder, & mut state) ?;
466508 let result = instance. invoke ( "call" , args, & mut state) ;
467509
468510 result. map_err ( |_| sp_sandbox:: HostError )
0 commit comments