Skip to content

Commit a3735da

Browse files
committed
Move confirm and prompt to browser.py
1 parent 5627ab2 commit a3735da

File tree

3 files changed

+31
-19
lines changed

3 files changed

+31
-19
lines changed

wasm/lib/src/browser.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from _browser import *
2+
23
from _js import JsValue
34
from _window import window
45

@@ -10,3 +11,27 @@ def alert(msg):
1011
if type(msg) != str:
1112
raise TypeError("msg must be a string")
1213
_alert.call(JsValue.fromstr(msg))
14+
15+
16+
_confirm = window.get_prop("confirm")
17+
18+
19+
def confirm(msg):
20+
if type(msg) != str:
21+
raise TypeError("msg must be a string")
22+
return _confirm.call(JsValue.fromstr(msg)).as_bool()
23+
24+
25+
_prompt = window.get_prop("prompt")
26+
27+
28+
def prompt(msg, default_val=None):
29+
if type(msg) != str:
30+
raise TypeError("msg must be a string")
31+
if default_val is not None and type(default_val) != str:
32+
raise TypeError("default_val must be a string")
33+
34+
return _prompt.call(
35+
JsValue.fromstr(arg) for arg in [msg, default_val] if arg
36+
).as_str()
37+

wasm/lib/src/browser_module.rs

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -312,22 +312,6 @@ impl Element {
312312
}
313313
}
314314

315-
fn browser_alert(message: PyStringRef, vm: &VirtualMachine) -> PyResult {
316-
window()
317-
.alert_with_message(message.as_str())
318-
.expect("alert() not to fail");
319-
320-
Ok(vm.get_none())
321-
}
322-
323-
fn browser_confirm(message: PyStringRef, vm: &VirtualMachine) -> PyResult {
324-
let result = window()
325-
.confirm_with_message(message.as_str())
326-
.expect("confirm() not to fail");
327-
328-
Ok(vm.new_bool(result))
329-
}
330-
331315
fn browser_prompt(
332316
message: PyStringRef,
333317
default: OptionalArg<PyStringRef>,
@@ -408,9 +392,6 @@ pub fn make_module(vm: &VirtualMachine) -> PyObjectRef {
408392
"Document" => document_class,
409393
"document" => document,
410394
"Element" => element,
411-
"alert" => ctx.new_rustfunc(browser_alert),
412-
"confirm" => ctx.new_rustfunc(browser_confirm),
413-
"prompt" => ctx.new_rustfunc(browser_prompt),
414395
"load_module" => ctx.new_rustfunc(browser_load_module),
415396
})
416397
}

wasm/lib/src/js_module.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,11 @@ impl PyJsValue {
130130
fn as_float(&self, _vm: &VirtualMachine) -> Option<f64> {
131131
self.value.as_f64()
132132
}
133+
134+
#[pymethod]
135+
fn as_bool(&self, _vm: &VirtualMachine) -> Option<bool> {
136+
self.value.as_bool()
137+
}
133138

134139
#[pymethod]
135140
/// Checks that `typeof self == "object" && self !== null`. Use instead
@@ -186,6 +191,7 @@ impl PyJsValue {
186191
} else {
187192
Reflect::construct(ctor, &js_args)
188193
};
194+
189195
constructed_result
190196
.map(PyJsValue::new)
191197
.map_err(|err| convert::js_to_py(vm, err))

0 commit comments

Comments
 (0)