Skip to content

Commit cca19cf

Browse files
Merge pull request RustPython#569 from coolreader18/wasm-alert
Add alert, confirm, and prompt to the browser module
2 parents 6c8584c + ea93fb8 commit cca19cf

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
@@ -182,13 +182,61 @@ fn browser_cancel_animation_frame(vm: &mut VirtualMachine, args: PyFuncArgs) ->
182182
Ok(vm.get_none())
183183
}
184184

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

187232
pub fn mk_module(ctx: &PyContext) -> PyObjectRef {
188233
py_module!(ctx, BROWSER_NAME, {
189234
"fetch" => ctx.new_rustfunc(browser_fetch),
190235
"request_animation_frame" => ctx.new_rustfunc(browser_request_animation_frame),
191236
"cancel_animation_frame" => ctx.new_rustfunc(browser_cancel_animation_frame),
237+
"alert" => ctx.new_rustfunc(browser_alert),
238+
"confirm" => ctx.new_rustfunc(browser_confirm),
239+
"prompt" => ctx.new_rustfunc(browser_prompt),
192240
})
193241
}
194242

0 commit comments

Comments
 (0)