Skip to content

Commit 1d5df41

Browse files
committed
Add doc comments and typescript custom section
1 parent b277280 commit 1d5df41

File tree

2 files changed

+29
-5
lines changed

2 files changed

+29
-5
lines changed

wasm/lib/README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,17 @@ A Python-3 (CPython >= 3.5.0) Interpreter written in Rust.
1919
## Quick Documentation
2020

2121
```js
22-
pyEval(code, options);
22+
pyEval(code, options?);
2323
```
2424
2525
`code`: `string`: The Python code to run
2626
2727
`options`:
2828
29-
- `vars`: `{ [key: string]: any }`: Variables passed to the VM that can be
29+
- `vars?`: `{ [key: string]: any }`: Variables passed to the VM that can be
3030
accessed in Python with the variable `js_vars`. Functions do work, and
3131
recieve the Python kwargs as the `this` argument.
32-
- `print`: `(out: string) => void`: A function to replace the native print
32+
- `stdout?`: `(out: string) => void`: A function to replace the native print
3333
function, by default `console.log`.
3434
3535
## License

wasm/lib/src/lib.rs

+26-2
Original file line numberDiff line numberDiff line change
@@ -125,15 +125,29 @@ fn eval(vm: &mut VirtualMachine, source: &str, vars: PyObjectRef) -> PyResult {
125125
}
126126

127127
#[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`.
128143
pub fn eval_py(source: &str, options: Option<Object>) -> Result<JsValue, JsValue> {
129144
let options = options.unwrap_or_else(|| Object::new());
130145
let js_vars = {
131146
let prop = Reflect::get(&options, &"vars".into())?;
132-
let prop = Object::from(prop);
133147
if prop.is_undefined() {
134148
None
135149
} else if prop.is_object() {
136-
Some(prop)
150+
Some(Object::from(prop))
137151
} else {
138152
return Err(TypeError::new("vars must be an object").into());
139153
}
@@ -205,3 +219,13 @@ pub fn eval_py(source: &str, options: Option<Object>) -> Result<JsValue, JsValue
205219
.map(|value| py_to_js(&mut vm, value))
206220
.map_err(|err| py_str_err(&mut vm, &err).into())
207221
}
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

Comments
 (0)