From d058b3debfa791abade2d4fd69fc74a3b4dca7ec Mon Sep 17 00:00:00 2001 From: yanganto Date: Mon, 25 Nov 2019 14:24:39 +0800 Subject: [PATCH] Set default stdout of wasm to `console.log` The print function of wasm will be - `console.log` by default or when giving undefined or "console" - a dumb function when giving `null` - the function when giving a function Co-Authored-By: Noah <33094578+coolreader18@users.noreply.github.com> --- wasm/lib/README.md | 5 +++-- wasm/lib/src/lib.rs | 17 +++++------------ wasm/lib/src/vm_class.rs | 4 +++- 3 files changed, 11 insertions(+), 15 deletions(-) diff --git a/wasm/lib/README.md b/wasm/lib/README.md index fc40074a44..012351abb6 100644 --- a/wasm/lib/README.md +++ b/wasm/lib/README.md @@ -33,8 +33,9 @@ pyEval(code, options?); - `vars?`: `{ [key: string]: any }`: Variables passed to the VM that can be accessed in Python with the variable `js_vars`. Functions do work, and receive the Python kwargs as the `this` argument. -- `stdout?`: `(out: string) => void`: A function to replace the native print - function, by default `console.log`. +- `stdout?`: `"console" | ((out: string) => void) | null`: A function to replace the + native print function, and it will be `console.log` when giving `undefined` + or "console", and it will be a dumb function when giving null. ## License diff --git a/wasm/lib/src/lib.rs b/wasm/lib/src/lib.rs index e750ef977a..b1f55d36bc 100644 --- a/wasm/lib/src/lib.rs +++ b/wasm/lib/src/lib.rs @@ -61,8 +61,10 @@ const TS_CMT_START: &'static str = "/*"; /// - `vars?`: `{ [key: string]: any }`: Variables passed to the VM that can be /// accessed in Python with the variable `js_vars`. Functions do work, and /// receive the Python kwargs as the `this` argument. -/// - `stdout?`: `(out: string) => void`: A function to replace the native print -/// function, by default `console.log`. +/// - `stdout?`: `"console" | ((out: string) => void) | null`: A function to replace the +/// native print native print function, and it will be `console.log` when giving +/// `undefined` or "console", and it will be a dumb function when giving null. + pub fn eval_py(source: &str, options: Option) -> Result { let options = options.unwrap_or_else(Object::new); let js_vars = { @@ -75,18 +77,9 @@ pub fn eval_py(source: &str, options: Option) -> Result PyResult { Ok(vm.get_none()) } vm.ctx.new_rustfunc(noop) + } else if stdout.is_undefined() { + vm.ctx.new_rustfunc(wasm_builtins::builtin_print_console) } else { return Err(error()); };