@@ -125,15 +125,29 @@ fn eval(vm: &mut VirtualMachine, source: &str, vars: PyObjectRef) -> PyResult {
125
125
}
126
126
127
127
#[ wasm_bindgen( js_name = pyEval) ]
128
+ /// Evaluate Python code
129
+ ///
130
+ /// ```js
131
+ /// pyEval(code, options?);
132
+ /// ```
133
+ ///
134
+ /// `code`: `string`: The Python code to run
135
+ ///
136
+ /// `options`:
137
+ ///
138
+ /// - `vars?`: `{ [key: string]: any }`: Variables passed to the VM that can be
139
+ /// accessed in Python with the variable `js_vars`. Functions do work, and
140
+ /// recieve the Python kwargs as the `this` argument.
141
+ /// - `stdout?`: `(out: string) => void`: A function to replace the native print
142
+ /// function, by default `console.log`.
128
143
pub fn eval_py ( source : & str , options : Option < Object > ) -> Result < JsValue , JsValue > {
129
144
let options = options. unwrap_or_else ( || Object :: new ( ) ) ;
130
145
let js_vars = {
131
146
let prop = Reflect :: get ( & options, & "vars" . into ( ) ) ?;
132
- let prop = Object :: from ( prop) ;
133
147
if prop. is_undefined ( ) {
134
148
None
135
149
} else if prop. is_object ( ) {
136
- Some ( prop)
150
+ Some ( Object :: from ( prop) )
137
151
} else {
138
152
return Err ( TypeError :: new ( "vars must be an object" ) . into ( ) ) ;
139
153
}
@@ -205,3 +219,13 @@ pub fn eval_py(source: &str, options: Option<Object>) -> Result<JsValue, JsValue
205
219
. map ( |value| py_to_js ( & mut vm, value) )
206
220
. map_err ( |err| py_str_err ( & mut vm, & err) . into ( ) )
207
221
}
222
+
223
+ #[ wasm_bindgen( typescript_custom_section) ]
224
+ const TYPESCRIPT_APPEND : & ' static str = r#"
225
+ interface PyEvalOptions {
226
+ stdout: (out: string) => void;
227
+ vars: { [key: string]: any };
228
+ }
229
+
230
+ export function pyEval(code: string, options?: PyEvalOptions): any;
231
+ "# ;
0 commit comments