@@ -8,11 +8,13 @@ extern crate futures;
88extern crate js_sys;
99#[ macro_use]
1010extern crate rustpython_vm;
11+ extern crate rustpython_compiler;
1112extern crate wasm_bindgen;
1213extern crate wasm_bindgen_futures;
1314extern crate web_sys;
1415
1516use js_sys:: { Object , Reflect , TypeError } ;
17+ use rustpython_compiler:: compile:: Mode ;
1618use std:: panic;
1719use wasm_bindgen:: prelude:: * ;
1820
@@ -47,14 +49,35 @@ pub fn setup_console_error() {
4749#[ wasm_bindgen( typescript_custom_section) ]
4850const TS_CMT_START : & ' static str = "/*" ;
4951
52+ fn run_py ( source : & str , options : Option < Object > , mode : Mode ) -> Result < JsValue , JsValue > {
53+ let vm = VMStore :: init ( PY_EVAL_VM_ID . into ( ) , Some ( true ) ) ;
54+ let options = options. unwrap_or_else ( Object :: new) ;
55+ let js_vars = {
56+ let prop = Reflect :: get ( & options, & "vars" . into ( ) ) ?;
57+ if prop. is_undefined ( ) {
58+ None
59+ } else if prop. is_object ( ) {
60+ Some ( Object :: from ( prop) )
61+ } else {
62+ return Err ( TypeError :: new ( "vars must be an object" ) . into ( ) ) ;
63+ }
64+ } ;
65+
66+ vm. set_stdout ( Reflect :: get ( & options, & "stdout" . into ( ) ) ?) ?;
67+
68+ if let Some ( js_vars) = js_vars {
69+ vm. add_to_scope ( "js_vars" . into ( ) , js_vars. into ( ) ) ?;
70+ }
71+ vm. run ( source, mode)
72+ }
5073#[ wasm_bindgen( js_name = pyEval) ]
5174/// Evaluate Python code
5275///
5376/// ```js
54- /// pyEval(code, options?);
77+ /// var result = pyEval(code, options?);
5578/// ```
5679///
57- /// `code`: `string`: The Python code to run
80+ /// `code`: `string`: The Python code to run in eval mode
5881///
5982/// `options`:
6083///
@@ -66,26 +89,35 @@ const TS_CMT_START: &'static str = "/*";
6689/// `undefined` or "console", and it will be a dumb function when giving null.
6790
6891pub fn eval_py ( source : & str , options : Option < Object > ) -> Result < JsValue , JsValue > {
69- let options = options. unwrap_or_else ( Object :: new) ;
70- let js_vars = {
71- let prop = Reflect :: get ( & options, & "vars" . into ( ) ) ?;
72- if prop. is_undefined ( ) {
73- None
74- } else if prop. is_object ( ) {
75- Some ( Object :: from ( prop) )
76- } else {
77- return Err ( TypeError :: new ( "vars must be an object" ) . into ( ) ) ;
78- }
79- } ;
80- let vm = VMStore :: init ( PY_EVAL_VM_ID . into ( ) , Some ( true ) ) ;
81-
82- vm. set_stdout ( Reflect :: get ( & options, & "stdout" . into ( ) ) ?) ?;
92+ run_py ( source, options, Mode :: Eval )
93+ }
8394
84- if let Some ( js_vars) = js_vars {
85- vm. add_to_scope ( "js_vars" . into ( ) , js_vars. into ( ) ) ?;
86- }
95+ #[ wasm_bindgen( js_name = pyExec) ]
96+ /// Evaluate Python code
97+ ///
98+ /// ```js
99+ /// pyExec(code, options?);
100+ /// ```
101+ ///
102+ /// `code`: `string`: The Python code to run in exec mode
103+ ///
104+ /// `options`: The options are the same as eval mode
105+ pub fn exec_py ( source : & str , options : Option < Object > ) {
106+ let _ = run_py ( source, options, Mode :: Exec ) ;
107+ }
87108
88- vm. exec ( source)
109+ #[ wasm_bindgen( js_name = pyExecSingle) ]
110+ /// Evaluate Python code
111+ ///
112+ /// ```js
113+ /// var result = pyExecSingle(code, options?);
114+ /// ```
115+ ///
116+ /// `code`: `string`: The Python code to run in exec single mode
117+ ///
118+ /// `options`: The options are the same as eval mode
119+ pub fn exec_single_py ( source : & str , options : Option < Object > ) -> Result < JsValue , JsValue > {
120+ run_py ( source, options, Mode :: Single )
89121}
90122
91123#[ wasm_bindgen( typescript_custom_section) ]
0 commit comments