File tree 3 files changed +31
-19
lines changed 3 files changed +31
-19
lines changed Original file line number Diff line number Diff line change 1
1
from _browser import *
2
+
2
3
from _js import JsValue
3
4
from _window import window
4
5
@@ -10,3 +11,27 @@ def alert(msg):
10
11
if type (msg ) != str :
11
12
raise TypeError ("msg must be a string" )
12
13
_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
+
Original file line number Diff line number Diff line change @@ -312,22 +312,6 @@ impl Element {
312
312
}
313
313
}
314
314
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
-
331
315
fn browser_prompt (
332
316
message : PyStringRef ,
333
317
default : OptionalArg < PyStringRef > ,
@@ -408,9 +392,6 @@ pub fn make_module(vm: &VirtualMachine) -> PyObjectRef {
408
392
"Document" => document_class,
409
393
"document" => document,
410
394
"Element" => element,
411
- "alert" => ctx. new_rustfunc( browser_alert) ,
412
- "confirm" => ctx. new_rustfunc( browser_confirm) ,
413
- "prompt" => ctx. new_rustfunc( browser_prompt) ,
414
395
"load_module" => ctx. new_rustfunc( browser_load_module) ,
415
396
} )
416
397
}
Original file line number Diff line number Diff line change @@ -130,6 +130,11 @@ impl PyJsValue {
130
130
fn as_float ( & self , _vm : & VirtualMachine ) -> Option < f64 > {
131
131
self . value . as_f64 ( )
132
132
}
133
+
134
+ #[ pymethod]
135
+ fn as_bool ( & self , _vm : & VirtualMachine ) -> Option < bool > {
136
+ self . value . as_bool ( )
137
+ }
133
138
134
139
#[ pymethod]
135
140
/// Checks that `typeof self == "object" && self !== null`. Use instead
@@ -186,6 +191,7 @@ impl PyJsValue {
186
191
} else {
187
192
Reflect :: construct ( ctor, & js_args)
188
193
} ;
194
+
189
195
constructed_result
190
196
. map ( PyJsValue :: new)
191
197
. map_err ( |err| convert:: js_to_py ( vm, err) )
You can’t perform that action at this time.
0 commit comments