11extern crate eel_wasm;
2+ mod common;
23
3- use std:: { collections:: HashMap , io} ;
4-
4+ use common:: GlobalPool ;
55use eel_wasm:: compile;
6- use wasmi:: nan_preserving_float:: F64 ;
7- use wasmi:: RuntimeValue ;
8- use wasmi:: {
9- Error as WasmiError , Externals , FuncInstance , FuncRef , GlobalDescriptor , GlobalInstance ,
10- GlobalRef , ImportsBuilder , ModuleImportResolver , ModuleInstance , NopExternals , RuntimeArgs ,
11- Signature , Trap , ValueType ,
12- } ;
6+ use wasmi:: { ImportsBuilder , ModuleInstance , RuntimeValue } ;
137
148fn run ( body : & [ u8 ] ) -> Result < f64 , String > {
159 let wasm_binary = body;
1610 let module = wasmi:: Module :: from_buffer ( & wasm_binary) . expect ( "failed to load wasm" ) ;
17- let instance = ModuleInstance :: new ( & module, & ImportsBuilder :: default ( ) )
11+
12+ let mut global_imports = GlobalPool { } ;
13+ let mut imports = ImportsBuilder :: default ( ) ;
14+ imports. push_resolver ( "pool" , & global_imports) ;
15+ imports. push_resolver ( "shims" , & global_imports) ;
16+ let instance = ModuleInstance :: new ( & module, & imports)
1817 . expect ( "failed to instantiate wasm module" )
1918 . assert_no_start ( ) ;
2019
21- // Finally, invoke the exported function "test" with no parameters
22- // and empty external function executor.
2320 match instance
24- . invoke_export ( "test" , & [ ] , & mut NopExternals )
21+ . invoke_export ( "test" , & [ ] , & mut global_imports )
2522 . expect ( "failed to execute export" )
2623 {
2724 Some ( RuntimeValue :: F64 ( val) ) => Ok ( val. into ( ) ) ,
@@ -42,18 +39,6 @@ fn test_run(program: &str, expected_output: f64) {
4239 ) ;
4340}
4441
45- #[ test]
46- fn build_one ( ) -> io:: Result < ( ) > {
47- assert_eq ! (
48- & compile( vec![ ( "test" . to_string( ) , "1" , "pool" . to_string( ) ) ] , vec![ ] ) . unwrap( ) ,
49- & [
50- 0 , 97 , 115 , 109 , 1 , 0 , 0 , 0 , 1 , 5 , 1 , 96 , 0 , 1 , 124 , 3 , 2 , 1 , 0 , 7 , 8 , 1 , 4 , 116 , 101 ,
51- 115 , 116 , 0 , 0 , 10 , 13 , 1 , 11 , 0 , 68 , 0 , 0 , 0 , 0 , 0 , 0 , 240 , 63 , 11 ,
52- ]
53- ) ;
54- Ok ( ( ) )
55- }
56-
5742#[ test]
5843fn execute_one ( ) {
5944 test_run ( "1" , 1.0 ) ;
@@ -65,142 +50,14 @@ fn execute_one() {
6550 test_run ( "1+1*2" , 3.0 ) ;
6651}
6752
68- struct GlobalPool {
69- globals : HashMap < String , GlobalRef > ,
70- }
71-
72- impl ModuleImportResolver for GlobalPool {
73- fn resolve_global (
74- & self ,
75- field_name : & str ,
76- _global_type : & GlobalDescriptor ,
77- ) -> Result < GlobalRef , WasmiError > {
78- let global = GlobalInstance :: alloc ( RuntimeValue :: F64 ( F64 :: from_float ( 0.0 ) ) , true ) ;
79- Ok ( global)
80- }
81- fn resolve_func ( & self , field_name : & str , signature : & Signature ) -> Result < FuncRef , WasmiError > {
82- println ! ( "Calling function: {}" , field_name) ;
83- let index = match field_name {
84- "sin" => SIN_FUNC_INDEX ,
85- _ => {
86- return Err ( WasmiError :: Instantiation ( format ! (
87- "Export {} not found" ,
88- field_name
89- ) ) )
90- }
91- } ;
92-
93- if !self . check_signature ( index, signature) {
94- return Err ( WasmiError :: Instantiation ( format ! (
95- "Export {} has a bad signature" ,
96- field_name
97- ) ) ) ;
98- }
99-
100- println ! ( "PAssed sig check: {}" , field_name) ;
101-
102- Ok ( FuncInstance :: alloc_host (
103- Signature :: new ( & [ ValueType :: F64 ] [ ..] , Some ( ValueType :: F64 ) ) ,
104- index,
105- ) )
106- }
107- }
108-
109- const SIN_FUNC_INDEX : usize = 0 ;
110-
111- impl Externals for GlobalPool {
112- fn invoke_index (
113- & mut self ,
114- index : usize ,
115- args : RuntimeArgs ,
116- ) -> Result < Option < RuntimeValue > , Trap > {
117- println ! ( "Calling index {}" , index) ;
118- match index {
119- SIN_FUNC_INDEX => {
120- let a: F64 = args. nth_checked ( 0 ) ?;
121- println ! ( "First arg was {:?}" , a) ;
122-
123- let result = a * 2 ;
124-
125- Ok ( Some ( RuntimeValue :: F64 ( F64 :: from ( result) ) ) )
126- }
127- _ => panic ! ( "Unimplemented function at {}" , index) ,
128- }
129- }
130- }
131-
132- impl GlobalPool {
133- fn check_signature ( & self , index : usize , signature : & Signature ) -> bool {
134- println ! ( "Checking sig of {}" , index) ;
135- let ( params, ret_ty) : ( & [ ValueType ] , Option < ValueType > ) = match index {
136- SIN_FUNC_INDEX => ( & [ ValueType :: F64 ] , Some ( ValueType :: F64 ) ) ,
137- _ => return false ,
138- } ;
139- signature. params ( ) == params && signature. return_type ( ) == ret_ty
140- }
141- }
142-
14353#[ test]
144- #[ ignore]
14554fn with_global ( ) {
146- let global_imports = GlobalPool {
147- globals : HashMap :: default ( ) ,
148- } ;
149- let wasm_binary = compile (
150- vec ! [ ( "test" . to_string( ) , "g=1" , "pool" . to_string( ) ) ] ,
151- vec ! [ ( "g" . to_string( ) , "pool" . to_string( ) ) ] ,
152- )
153- . expect ( "Expect to compile" ) ;
154- // TODO: This will fail becuase wasmi 0.8.0 depends upon wasmi-validaiton
155- // 0.3.0 which does not include https://github.com/paritytech/wasmi/pull/228
156- // which allows mutable globals.
157- // 0.3.1 has the PR, but wasmi has not shipped a new version that includes it.
158- // parity-wasm already depends upon 0.3.1 (I _think_)
159- let module = wasmi:: Module :: from_buffer ( & wasm_binary) . expect ( "No validation errors" ) ;
160- let mut imports = ImportsBuilder :: default ( ) ;
161- imports. push_resolver ( "pool" , & global_imports) ;
162- let instance = ModuleInstance :: new ( & module, & imports)
163- . expect ( "failed to instantiate wasm module" )
164- . assert_no_start ( ) ;
165-
166- // Finally, invoke the exported function "test" with no parameters
167- // and empty external function executor.
168- instance
169- . invoke_export ( "test" , & [ ] , & mut NopExternals )
170- . expect ( "failed to execute export" )
171- . expect ( "Ran" ) ;
55+ test_run ( "g=1" , 1.0 ) ;
17256}
17357
17458#[ test]
175- #[ ignore]
17659fn with_shims ( ) {
177- let global_imports = GlobalPool {
178- globals : HashMap :: default ( ) ,
179- } ;
180- let wasm_binary = compile (
181- vec ! [ ( "test" . to_string( ) , "sin(10)" , "pool" . to_string( ) ) ] ,
182- vec ! [ ] ,
183- )
184- . expect ( "Expect to compile" ) ;
185- // TODO: This will fail becuase wasmi 0.8.0 depends upon wasmi-validaiton
186- // 0.3.0 which does not include https://github.com/paritytech/wasmi/pull/228
187- // which allows mutable globals.
188- // 0.3.1 has the PR, but wasmi has not shipped a new version that includes it.
189- // parity-wasm already depends upon 0.3.1 (I _think_)
190- let module = wasmi:: Module :: from_buffer ( & wasm_binary) . expect ( "No validation errors" ) ;
191- let mut imports = ImportsBuilder :: default ( ) ;
192- imports. push_resolver ( "pool" , & global_imports) ;
193- imports. push_resolver ( "shims" , & global_imports) ;
194- let instance = ModuleInstance :: new ( & module, & imports)
195- . expect ( "failed to instantiate wasm module" )
196- . assert_no_start ( ) ;
197-
198- // Finally, invoke the exported function "test" with no parameters
199- // and empty external function executor.
200- instance
201- . invoke_export ( "test" , & [ ] , & mut NopExternals )
202- . expect ( "failed to execute export" )
203- . expect ( "Ran" ) ;
60+ test_run ( "sin(10)" , 10.0_f64 . sin ( ) ) ;
20461}
20562
20663#[ test]
@@ -219,17 +76,22 @@ fn multiple_functions() {
21976 // 0.3.1 has the PR, but wasmi has not shipped a new version that includes it.
22077 // parity-wasm already depends upon 0.3.1 (I _think_)
22178 let module = wasmi:: Module :: from_buffer ( & wasm_binary) . expect ( "No validation errors" ) ;
222- let instance = ModuleInstance :: new ( & module, & ImportsBuilder :: default ( ) )
79+ let mut global_imports = GlobalPool { } ;
80+ let mut imports = ImportsBuilder :: default ( ) ;
81+ imports. push_resolver ( "pool" , & global_imports) ;
82+ imports. push_resolver ( "shims" , & global_imports) ;
83+
84+ let instance = ModuleInstance :: new ( & module, & imports)
22385 . expect ( "failed to instantiate wasm module" )
22486 . assert_no_start ( ) ;
22587
22688 instance
227- . invoke_export ( "one" , & [ ] , & mut NopExternals )
89+ . invoke_export ( "one" , & [ ] , & mut global_imports )
22890 . expect ( "failed to execute export" )
22991 . expect ( "Ran" ) ;
23092
23193 instance
232- . invoke_export ( "two" , & [ ] , & mut NopExternals )
94+ . invoke_export ( "two" , & [ ] , & mut global_imports )
23395 . expect ( "failed to execute export" )
23496 . expect ( "Ran" ) ;
23597}
0 commit comments