@@ -266,46 +266,40 @@ impl WASMVirtualMachine {
266
266
267
267
#[ wasm_bindgen( js_name = setStdout) ]
268
268
pub fn set_stdout ( & self , stdout : JsValue ) -> Result < ( ) , JsValue > {
269
- self . with (
270
- move |StoredVirtualMachine {
271
- ref mut vm,
272
- ref mut scope,
273
- ..
274
- } | {
275
- fn error ( ) -> JsValue {
276
- TypeError :: new ( "Unknown stdout option, please pass a function or 'console'" )
277
- . into ( )
278
- }
279
- let print_fn: Box < Fn ( & mut VirtualMachine , PyFuncArgs ) -> PyResult > =
280
- if let Some ( s) = stdout. as_string ( ) {
281
- match s. as_str ( ) {
282
- "console" => Box :: new ( wasm_builtins:: builtin_print_console) ,
283
- _ => return Err ( error ( ) ) ,
284
- }
285
- } else if stdout. is_function ( ) {
286
- let func = js_sys:: Function :: from ( stdout) ;
287
- Box :: new (
288
- move |vm : & mut VirtualMachine , args : PyFuncArgs | -> PyResult {
289
- func. call1 (
290
- & JsValue :: UNDEFINED ,
291
- & wasm_builtins:: format_print_args ( vm, args) ?. into ( ) ,
292
- )
293
- . map_err ( |err| convert:: js_to_py ( vm, err) ) ?;
294
- Ok ( vm. get_none ( ) )
295
- } ,
296
- )
297
- } else if stdout. is_undefined ( ) || stdout. is_null ( ) {
298
- fn noop ( vm : & mut VirtualMachine , _args : PyFuncArgs ) -> PyResult {
269
+ self . with ( move |StoredVirtualMachine { ref mut vm, .. } | {
270
+ fn error ( ) -> JsValue {
271
+ TypeError :: new ( "Unknown stdout option, please pass a function or 'console'" ) . into ( )
272
+ }
273
+ let print_fn: Box < Fn ( & mut VirtualMachine , PyFuncArgs ) -> PyResult > =
274
+ if let Some ( s) = stdout. as_string ( ) {
275
+ match s. as_str ( ) {
276
+ "console" => Box :: new ( wasm_builtins:: builtin_print_console) ,
277
+ _ => return Err ( error ( ) ) ,
278
+ }
279
+ } else if stdout. is_function ( ) {
280
+ let func = js_sys:: Function :: from ( stdout) ;
281
+ Box :: new (
282
+ move |vm : & mut VirtualMachine , args : PyFuncArgs | -> PyResult {
283
+ func. call1 (
284
+ & JsValue :: UNDEFINED ,
285
+ & wasm_builtins:: format_print_args ( vm, args) ?. into ( ) ,
286
+ )
287
+ . map_err ( |err| convert:: js_to_py ( vm, err) ) ?;
299
288
Ok ( vm. get_none ( ) )
300
- }
301
- Box :: new ( noop)
302
- } else {
303
- return Err ( error ( ) ) ;
304
- } ;
305
- scope. store_name ( & vm, "print" , vm. ctx . new_rustfunc ( print_fn) ) ;
306
- Ok ( ( ) )
307
- } ,
308
- ) ?
289
+ } ,
290
+ )
291
+ } else if stdout. is_undefined ( ) || stdout. is_null ( ) {
292
+ fn noop ( vm : & mut VirtualMachine , _args : PyFuncArgs ) -> PyResult {
293
+ Ok ( vm. get_none ( ) )
294
+ }
295
+ Box :: new ( noop)
296
+ } else {
297
+ return Err ( error ( ) ) ;
298
+ } ;
299
+ let rustfunc = vm. ctx . new_rustfunc ( print_fn) ;
300
+ vm. ctx . set_attr ( & vm. builtins , "print" , rustfunc) ;
301
+ Ok ( ( ) )
302
+ } ) ?
309
303
}
310
304
311
305
#[ wasm_bindgen( js_name = injectModule) ]
@@ -395,4 +389,9 @@ impl WASMVirtualMachine {
395
389
pub fn eval ( & self , source : String ) -> Result < JsValue , JsValue > {
396
390
self . run ( source, compile:: Mode :: Eval )
397
391
}
392
+
393
+ #[ wasm_bindgen( js_name = execSingle) ]
394
+ pub fn exec_single ( & self , source : String ) -> Result < JsValue , JsValue > {
395
+ self . run ( source, compile:: Mode :: Single )
396
+ }
398
397
}
0 commit comments