Skip to content

Commit ea93fb8

Browse files
committed
Add alert, confirm, and prompt to the browser module
1 parent d6242ac commit ea93fb8

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

wasm/lib/src/browser_module.rs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,13 +180,61 @@ fn browser_cancel_animation_frame(vm: &mut VirtualMachine, args: PyFuncArgs) ->
180180
Ok(vm.get_none())
181181
}
182182

183+
fn browser_alert(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
184+
arg_check!(vm, args, required = [(message, Some(vm.ctx.str_type()))]);
185+
186+
window()
187+
.alert_with_message(&objstr::get_value(message))
188+
.expect("alert() not to fail");
189+
190+
Ok(vm.get_none())
191+
}
192+
193+
fn browser_confirm(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
194+
arg_check!(vm, args, required = [(message, Some(vm.ctx.str_type()))]);
195+
196+
let result = window()
197+
.confirm_with_message(&objstr::get_value(message))
198+
.expect("confirm() not to fail");
199+
200+
Ok(vm.new_bool(result))
201+
}
202+
203+
fn browser_prompt(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
204+
arg_check!(
205+
vm,
206+
args,
207+
required = [(message, Some(vm.ctx.str_type()))],
208+
optional = [(default, Some(vm.ctx.str_type()))]
209+
);
210+
211+
let result = if let Some(default) = default {
212+
window().prompt_with_message_and_default(
213+
&objstr::get_value(message),
214+
&objstr::get_value(default),
215+
)
216+
} else {
217+
window().prompt_with_message(&objstr::get_value(message))
218+
};
219+
220+
let result = match result.expect("prompt() not to fail") {
221+
Some(result) => vm.new_str(result),
222+
None => vm.get_none(),
223+
};
224+
225+
Ok(result)
226+
}
227+
183228
const BROWSER_NAME: &str = "browser";
184229

185230
pub fn mk_module(ctx: &PyContext) -> PyObjectRef {
186231
py_module!(ctx, BROWSER_NAME, {
187232
"fetch" => ctx.new_rustfunc(browser_fetch),
188233
"request_animation_frame" => ctx.new_rustfunc(browser_request_animation_frame),
189234
"cancel_animation_frame" => ctx.new_rustfunc(browser_cancel_animation_frame),
235+
"alert" => ctx.new_rustfunc(browser_alert),
236+
"confirm" => ctx.new_rustfunc(browser_confirm),
237+
"prompt" => ctx.new_rustfunc(browser_prompt),
190238
})
191239
}
192240

0 commit comments

Comments
 (0)