@@ -199,32 +199,31 @@ impl WASMVirtualMachine {
199
199
fn error ( ) -> JsValue {
200
200
TypeError :: new ( "Unknown stdout option, please pass a function or 'console'" ) . into ( )
201
201
}
202
- let print_fn: Box < Fn ( & VirtualMachine , PyFuncArgs ) -> PyResult > =
203
- if let Some ( s ) = stdout . as_string ( ) {
204
- match s . as_str ( ) {
205
- "console" => Box :: new ( wasm_builtins :: builtin_print_console ) ,
206
- _ => return Err ( error ( ) ) ,
207
- }
208
- } else if stdout . is_function ( ) {
209
- let func = js_sys :: Function :: from ( stdout ) ;
210
- Box :: new ( move |vm : & VirtualMachine , args : PyFuncArgs | -> PyResult {
202
+ let print_fn: PyObjectRef = if let Some ( s ) = stdout . as_string ( ) {
203
+ match s . as_str ( ) {
204
+ "console" => vm . ctx . new_rustfunc ( wasm_builtins :: builtin_print_console ) ,
205
+ _ => return Err ( error ( ) ) ,
206
+ }
207
+ } else if stdout . is_function ( ) {
208
+ let func = js_sys :: Function :: from ( stdout ) ;
209
+ vm . ctx
210
+ . new_rustfunc ( move |vm : & VirtualMachine , args : PyFuncArgs | -> PyResult {
211
211
func. call1 (
212
212
& JsValue :: UNDEFINED ,
213
213
& wasm_builtins:: format_print_args ( vm, args) ?. into ( ) ,
214
214
)
215
215
. map_err ( |err| convert:: js_to_py ( vm, err) ) ?;
216
216
Ok ( vm. get_none ( ) )
217
217
} )
218
- } else if stdout. is_undefined ( ) || stdout. is_null ( ) {
219
- fn noop ( vm : & VirtualMachine , _args : PyFuncArgs ) -> PyResult {
220
- Ok ( vm. get_none ( ) )
221
- }
222
- Box :: new ( noop)
223
- } else {
224
- return Err ( error ( ) ) ;
225
- } ;
226
- vm. set_attr ( & vm. builtins , "print" , vm. ctx . new_rustfunc ( print_fn) )
227
- . unwrap ( ) ;
218
+ } else if stdout. is_undefined ( ) || stdout. is_null ( ) {
219
+ fn noop ( vm : & VirtualMachine , _args : PyFuncArgs ) -> PyResult {
220
+ Ok ( vm. get_none ( ) )
221
+ }
222
+ vm. ctx . new_rustfunc ( noop)
223
+ } else {
224
+ return Err ( error ( ) ) ;
225
+ } ;
226
+ vm. set_attr ( & vm. builtins , "print" , print_fn) . unwrap ( ) ;
228
227
Ok ( ( ) )
229
228
} ) ?
230
229
}
0 commit comments