Skip to content

Commit eeb7551

Browse files
Merge pull request RustPython#1039 from RustPython/coolreader18/wasm-_js-module
[WASM] Add a JsValue class
2 parents fee9c99 + 658b6ca commit eeb7551

File tree

11 files changed

+360
-47
lines changed

11 files changed

+360
-47
lines changed

derive/src/compile_bytecode.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ pub fn impl_py_compile_bytecode(input: TokenStream2) -> Result<TokenStream2, Dia
163163

164164
let output = quote! {
165165
({
166-
use ::bincode;
166+
use ::rustpython_vm::__exports::bincode;
167167
bincode::deserialize::<::rustpython_vm::bytecode::CodeObject>(#bytes)
168168
.expect("Deserializing CodeObject failed")
169169
})

vm/src/lib.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,8 @@ mod vm;
6666
pub use self::exceptions::print_exception;
6767
pub use self::vm::VirtualMachine;
6868
pub use rustpython_compiler::*;
69+
70+
#[doc(hidden)]
71+
pub mod __exports {
72+
pub use bincode;
73+
}

vm/src/pyobject.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1379,7 +1379,11 @@ pub trait PyClassImpl: PyClassDef {
13791379
}
13801380

13811381
fn make_class(ctx: &PyContext) -> PyClassRef {
1382-
let py_class = ctx.new_class(Self::NAME, ctx.object());
1382+
Self::make_class_with_base(ctx, ctx.object())
1383+
}
1384+
1385+
fn make_class_with_base(ctx: &PyContext, base: PyClassRef) -> PyClassRef {
1386+
let py_class = ctx.new_class(Self::NAME, base);
13831387
Self::extend_class(ctx, &py_class);
13841388
py_class
13851389
}

vm/src/stdlib/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ use crate::pyobject::PyObjectRef;
3434
pub type StdlibInitFunc = Box<dyn Fn(&VirtualMachine) -> PyObjectRef>;
3535

3636
pub fn get_module_inits() -> HashMap<String, StdlibInitFunc> {
37+
#[allow(unused_mut)]
3738
let mut modules = hashmap! {
3839
"ast".to_string() => Box::new(ast::make_module) as StdlibInitFunc,
3940
"binascii".to_string() => Box::new(binascii::make_module),

wasm/lib/src/browser.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
from _browser import *
2+
3+
from _js import JsValue
4+
from _window import window
5+
6+
7+
jsstr = window.new_from_str
8+
9+
10+
_alert = window.get_prop("alert")
11+
12+
13+
def alert(msg):
14+
if type(msg) != str:
15+
raise TypeError("msg must be a string")
16+
_alert.call(jsstr(msg))
17+
18+
19+
_confirm = window.get_prop("confirm")
20+
21+
22+
def confirm(msg):
23+
if type(msg) != str:
24+
raise TypeError("msg must be a string")
25+
return _confirm.call(jsstr(msg)).as_bool()
26+
27+
28+
_prompt = window.get_prop("prompt")
29+
30+
31+
def prompt(msg, default_val=None):
32+
if type(msg) != str:
33+
raise TypeError("msg must be a string")
34+
if default_val is not None and type(default_val) != str:
35+
raise TypeError("default_val must be a string")
36+
37+
return _prompt.call(*(jsstr(arg) for arg in [msg, default_val] if arg)).as_str()

wasm/lib/src/browser_module.rs

Lines changed: 5 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -312,41 +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-
331-
fn browser_prompt(
332-
message: PyStringRef,
333-
default: OptionalArg<PyStringRef>,
334-
vm: &VirtualMachine,
335-
) -> PyResult {
336-
let result = if let OptionalArg::Present(default) = default {
337-
window().prompt_with_message_and_default(message.as_str(), default.as_str())
338-
} else {
339-
window().prompt_with_message(message.as_str())
340-
};
341-
342-
let result = match result.expect("prompt() not to fail") {
343-
Some(result) => vm.new_str(result),
344-
None => vm.get_none(),
345-
};
346-
347-
Ok(result)
348-
}
349-
350315
fn browser_load_module(module: PyStringRef, path: PyStringRef, vm: &VirtualMachine) -> PyResult {
351316
let weak_vm = weak_vm(vm);
352317

@@ -408,15 +373,16 @@ pub fn make_module(vm: &VirtualMachine) -> PyObjectRef {
408373
"Document" => document_class,
409374
"document" => document,
410375
"Element" => element,
411-
"alert" => ctx.new_rustfunc(browser_alert),
412-
"confirm" => ctx.new_rustfunc(browser_confirm),
413-
"prompt" => ctx.new_rustfunc(browser_prompt),
414376
"load_module" => ctx.new_rustfunc(browser_load_module),
415377
})
416378
}
417379

418380
pub fn setup_browser_module(vm: &VirtualMachine) {
419381
vm.stdlib_inits
420382
.borrow_mut()
421-
.insert("browser".to_string(), Box::new(make_module));
383+
.insert("_browser".to_string(), Box::new(make_module));
384+
vm.frozen.borrow_mut().insert(
385+
"browser".to_string(),
386+
py_compile_bytecode!(file = "src/browser.py"),
387+
);
422388
}

wasm/lib/src/convert.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ pub fn py_err_to_js_err(vm: &VirtualMachine, py_err: &PyObjectRef) -> JsValue {
4545
let elements = objsequence::get_elements_list(&tb).to_vec();
4646
if let Some(top) = elements.get(0) {
4747
if objtype::isinstance(&top, &vm.ctx.tuple_type()) {
48-
let element = objsequence::get_elements_list(&top);
48+
let element = objsequence::get_elements_tuple(&top);
4949

5050
if let Some(lineno) = objint::to_int(vm, &element[1], 10)
5151
.ok()

0 commit comments

Comments
 (0)