From ea93fb871c2c0c10207373abbde610176d742ddb Mon Sep 17 00:00:00 2001 From: coolreader18 <33094578+coolreader18@users.noreply.github.com> Date: Wed, 27 Feb 2019 17:38:24 -0600 Subject: [PATCH] Add alert, confirm, and prompt to the browser module --- wasm/lib/src/browser_module.rs | 48 ++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/wasm/lib/src/browser_module.rs b/wasm/lib/src/browser_module.rs index e3f697838..64c9cba3b 100644 --- a/wasm/lib/src/browser_module.rs +++ b/wasm/lib/src/browser_module.rs @@ -180,6 +180,51 @@ fn browser_cancel_animation_frame(vm: &mut VirtualMachine, args: PyFuncArgs) -> Ok(vm.get_none()) } +fn browser_alert(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { + arg_check!(vm, args, required = [(message, Some(vm.ctx.str_type()))]); + + window() + .alert_with_message(&objstr::get_value(message)) + .expect("alert() not to fail"); + + Ok(vm.get_none()) +} + +fn browser_confirm(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { + arg_check!(vm, args, required = [(message, Some(vm.ctx.str_type()))]); + + let result = window() + .confirm_with_message(&objstr::get_value(message)) + .expect("confirm() not to fail"); + + Ok(vm.new_bool(result)) +} + +fn browser_prompt(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { + arg_check!( + vm, + args, + required = [(message, Some(vm.ctx.str_type()))], + optional = [(default, Some(vm.ctx.str_type()))] + ); + + let result = if let Some(default) = default { + window().prompt_with_message_and_default( + &objstr::get_value(message), + &objstr::get_value(default), + ) + } else { + window().prompt_with_message(&objstr::get_value(message)) + }; + + let result = match result.expect("prompt() not to fail") { + Some(result) => vm.new_str(result), + None => vm.get_none(), + }; + + Ok(result) +} + const BROWSER_NAME: &str = "browser"; pub fn mk_module(ctx: &PyContext) -> PyObjectRef { @@ -187,6 +232,9 @@ pub fn mk_module(ctx: &PyContext) -> PyObjectRef { "fetch" => ctx.new_rustfunc(browser_fetch), "request_animation_frame" => ctx.new_rustfunc(browser_request_animation_frame), "cancel_animation_frame" => ctx.new_rustfunc(browser_cancel_animation_frame), + "alert" => ctx.new_rustfunc(browser_alert), + "confirm" => ctx.new_rustfunc(browser_confirm), + "prompt" => ctx.new_rustfunc(browser_prompt), }) }