Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion derive/src/compile_bytecode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ pub fn impl_py_compile_bytecode(input: TokenStream2) -> Result<TokenStream2, Dia

let output = quote! {
({
use ::bincode;
use ::rustpython_vm::__exports::bincode;
bincode::deserialize::<::rustpython_vm::bytecode::CodeObject>(#bytes)
.expect("Deserializing CodeObject failed")
})
Expand Down
5 changes: 5 additions & 0 deletions vm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,8 @@ mod vm;
pub use self::exceptions::print_exception;
pub use self::vm::VirtualMachine;
pub use rustpython_compiler::*;

#[doc(hidden)]
pub mod __exports {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the same can be achieved by using extern crate bincode. Cargo will make sure that both the vm and the wasm crate use the same version of the bincode crate, so no need to import the bincode crate via the rustpython_vm.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pub use bincode;
}
6 changes: 5 additions & 1 deletion vm/src/pyobject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1379,7 +1379,11 @@ pub trait PyClassImpl: PyClassDef {
}

fn make_class(ctx: &PyContext) -> PyClassRef {
let py_class = ctx.new_class(Self::NAME, ctx.object());
Self::make_class_with_base(ctx, ctx.object())
}

fn make_class_with_base(ctx: &PyContext, base: PyClassRef) -> PyClassRef {
let py_class = ctx.new_class(Self::NAME, base);
Self::extend_class(ctx, &py_class);
py_class
}
Expand Down
1 change: 1 addition & 0 deletions vm/src/stdlib/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ use crate::pyobject::PyObjectRef;
pub type StdlibInitFunc = Box<dyn Fn(&VirtualMachine) -> PyObjectRef>;

pub fn get_module_inits() -> HashMap<String, StdlibInitFunc> {
#[allow(unused_mut)]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why was this added? We're returning an immutable HashMap here right?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A little bit downwards there are some #[cfg]s that conditionally add modules to the map, and if your target matches neither of those cfgs, you get an unused mut warning.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I understand now. Thanks!

let mut modules = hashmap! {
"ast".to_string() => Box::new(ast::make_module) as StdlibInitFunc,
"binascii".to_string() => Box::new(binascii::make_module),
Expand Down
37 changes: 37 additions & 0 deletions wasm/lib/src/browser.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
from _browser import *

from _js import JsValue
from _window import window


jsstr = window.new_from_str


_alert = window.get_prop("alert")


def alert(msg):
if type(msg) != str:
raise TypeError("msg must be a string")
_alert.call(jsstr(msg))


_confirm = window.get_prop("confirm")


def confirm(msg):
if type(msg) != str:
raise TypeError("msg must be a string")
return _confirm.call(jsstr(msg)).as_bool()


_prompt = window.get_prop("prompt")


def prompt(msg, default_val=None):
if type(msg) != str:
raise TypeError("msg must be a string")
if default_val is not None and type(default_val) != str:
raise TypeError("default_val must be a string")

return _prompt.call(*(jsstr(arg) for arg in [msg, default_val] if arg)).as_str()
44 changes: 5 additions & 39 deletions wasm/lib/src/browser_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -312,41 +312,6 @@ impl Element {
}
}

fn browser_alert(message: PyStringRef, vm: &VirtualMachine) -> PyResult {
window()
.alert_with_message(message.as_str())
.expect("alert() not to fail");

Ok(vm.get_none())
}

fn browser_confirm(message: PyStringRef, vm: &VirtualMachine) -> PyResult {
let result = window()
.confirm_with_message(message.as_str())
.expect("confirm() not to fail");

Ok(vm.new_bool(result))
}

fn browser_prompt(
message: PyStringRef,
default: OptionalArg<PyStringRef>,
vm: &VirtualMachine,
) -> PyResult {
let result = if let OptionalArg::Present(default) = default {
window().prompt_with_message_and_default(message.as_str(), default.as_str())
} else {
window().prompt_with_message(message.as_str())
};

let result = match result.expect("prompt() not to fail") {
Some(result) => vm.new_str(result),
None => vm.get_none(),
};

Ok(result)
}

fn browser_load_module(module: PyStringRef, path: PyStringRef, vm: &VirtualMachine) -> PyResult {
let weak_vm = weak_vm(vm);

Expand Down Expand Up @@ -408,15 +373,16 @@ pub fn make_module(vm: &VirtualMachine) -> PyObjectRef {
"Document" => document_class,
"document" => document,
"Element" => element,
"alert" => ctx.new_rustfunc(browser_alert),
"confirm" => ctx.new_rustfunc(browser_confirm),
"prompt" => ctx.new_rustfunc(browser_prompt),
"load_module" => ctx.new_rustfunc(browser_load_module),
})
}

pub fn setup_browser_module(vm: &VirtualMachine) {
vm.stdlib_inits
.borrow_mut()
.insert("browser".to_string(), Box::new(make_module));
.insert("_browser".to_string(), Box::new(make_module));
vm.frozen.borrow_mut().insert(
"browser".to_string(),
py_compile_bytecode!(file = "src/browser.py"),
);
}
2 changes: 1 addition & 1 deletion wasm/lib/src/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ pub fn py_err_to_js_err(vm: &VirtualMachine, py_err: &PyObjectRef) -> JsValue {
let elements = objsequence::get_elements_list(&tb).to_vec();
if let Some(top) = elements.get(0) {
if objtype::isinstance(&top, &vm.ctx.tuple_type()) {
let element = objsequence::get_elements_list(&top);
let element = objsequence::get_elements_tuple(&top);

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