Repository navigation
[WASM] Add a JsValue class #1039
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
61b2687
56bcb02
93cdd94
5627ab2
a3735da
886f77b
072c872
8cf38f5
298b05d
658b6ca
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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)] | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why was this added? We're returning an immutable HashMap here right?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A little bit downwards there are some
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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), | ||
|
|
||
| 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() |
There was a problem hiding this comment.
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 therustpython_vm.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did this here because this pattern is used by other macros in the ecosystem, here are a few examples: