@@ -115,52 +115,54 @@ pub fn generate(js: &JS, exported_functions: Vec<String>) -> Result<Vec<u8>> {
115115
116116 module. exports . add ( "_start" , main) ;
117117
118- let invoke_type = module. types . add (
119- & [ ValType :: I32 , ValType :: I32 , ValType :: I32 , ValType :: I32 ] ,
120- & [ ] ,
121- ) ;
122- let ( invoke_fn, _) = module. add_import_func ( IMPORT_NAMESPACE , "invoke" , invoke_type) ;
123-
124- let fn_name_ptr_local = module. locals . add ( ValType :: I32 ) ;
125- for js_export in exported_functions {
126- // For each JS function export, add an export that copies the name of the function into memory and invokes it.
127- let js_export_bytes = js_export. as_bytes ( ) ;
128- let js_export_len: i32 = js_export_bytes. len ( ) . try_into ( ) . unwrap ( ) ;
129- let fn_name_data = module. data . add ( DataKind :: Passive , js_export_bytes. to_vec ( ) ) ;
130-
131- let mut export_fn = FunctionBuilder :: new ( & mut module. types , & [ ] , & [ ] ) ;
132- export_fn
133- . func_body ( )
134- // Copy bytecode.
135- . i32_const ( 0 ) // orig ptr
136- . i32_const ( 0 ) // orig len
137- . i32_const ( 1 ) // alignment
138- . i32_const ( bytecode_len) // size to copy
139- . call ( canonical_abi_realloc_fn)
140- . local_tee ( bytecode_ptr_local)
141- . i32_const ( 0 ) // offset into data segment
142- . i32_const ( bytecode_len) // size to copy
143- . memory_init ( memory, bytecode_data) // copy bytecode into allocated memory
144- . data_drop ( bytecode_data)
145- // Copy function name.
146- . i32_const ( 0 ) // orig ptr
147- . i32_const ( 0 ) // orig len
148- . i32_const ( 1 ) // alignment
149- . i32_const ( js_export_len) // new size
150- . call ( canonical_abi_realloc_fn)
151- . local_tee ( fn_name_ptr_local)
152- . i32_const ( 0 ) // offset into data segment
153- . i32_const ( js_export_len) // size to copy
154- . memory_init ( memory, fn_name_data) // copy fn name into allocated memory
155- . data_drop ( fn_name_data)
156- // Call invoke.
157- . local_get ( bytecode_ptr_local)
158- . i32_const ( bytecode_len)
159- . local_get ( fn_name_ptr_local)
160- . i32_const ( js_export_len)
161- . call ( invoke_fn) ;
162- let export_fn = export_fn. finish ( vec ! [ ] , & mut module. funcs ) ;
163- module. exports . add ( & js_export, export_fn) ;
118+ if !exported_functions. is_empty ( ) {
119+ let invoke_type = module. types . add (
120+ & [ ValType :: I32 , ValType :: I32 , ValType :: I32 , ValType :: I32 ] ,
121+ & [ ] ,
122+ ) ;
123+ let ( invoke_fn, _) = module. add_import_func ( IMPORT_NAMESPACE , "invoke" , invoke_type) ;
124+
125+ let fn_name_ptr_local = module. locals . add ( ValType :: I32 ) ;
126+ for js_export in exported_functions {
127+ // For each JS function export, add an export that copies the name of the function into memory and invokes it.
128+ let js_export_bytes = js_export. as_bytes ( ) ;
129+ let js_export_len: i32 = js_export_bytes. len ( ) . try_into ( ) . unwrap ( ) ;
130+ let fn_name_data = module. data . add ( DataKind :: Passive , js_export_bytes. to_vec ( ) ) ;
131+
132+ let mut export_fn = FunctionBuilder :: new ( & mut module. types , & [ ] , & [ ] ) ;
133+ export_fn
134+ . func_body ( )
135+ // Copy bytecode.
136+ . i32_const ( 0 ) // orig ptr
137+ . i32_const ( 0 ) // orig len
138+ . i32_const ( 1 ) // alignment
139+ . i32_const ( bytecode_len) // size to copy
140+ . call ( canonical_abi_realloc_fn)
141+ . local_tee ( bytecode_ptr_local)
142+ . i32_const ( 0 ) // offset into data segment
143+ . i32_const ( bytecode_len) // size to copy
144+ . memory_init ( memory, bytecode_data) // copy bytecode into allocated memory
145+ . data_drop ( bytecode_data)
146+ // Copy function name.
147+ . i32_const ( 0 ) // orig ptr
148+ . i32_const ( 0 ) // orig len
149+ . i32_const ( 1 ) // alignment
150+ . i32_const ( js_export_len) // new size
151+ . call ( canonical_abi_realloc_fn)
152+ . local_tee ( fn_name_ptr_local)
153+ . i32_const ( 0 ) // offset into data segment
154+ . i32_const ( js_export_len) // size to copy
155+ . memory_init ( memory, fn_name_data) // copy fn name into allocated memory
156+ . data_drop ( fn_name_data)
157+ // Call invoke.
158+ . local_get ( bytecode_ptr_local)
159+ . i32_const ( bytecode_len)
160+ . local_get ( fn_name_ptr_local)
161+ . i32_const ( js_export_len)
162+ . call ( invoke_fn) ;
163+ let export_fn = export_fn. finish ( vec ! [ ] , & mut module. funcs ) ;
164+ module. exports . add ( & js_export, export_fn) ;
165+ }
164166 }
165167
166168 let wasm = module. emit_wasm ( ) ;
0 commit comments