Skip to content

Add a VirtualMachine class to the WASM library #267

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

Merged
merged 37 commits into from
Feb 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
4020ee7
Add a bare-bones VirtualMachine class to the WASM library
coolreader18 Jan 30, 2019
13b2f83
Fix vms.get()
coolreader18 Jan 30, 2019
f064bca
Add ids() function to VMStore and destroy() method to VirtualMachine
coolreader18 Jan 30, 2019
d0a4f9d
Make destroy() method public
coolreader18 Jan 30, 2019
b32b732
Run cargo fmt
coolreader18 Jan 30, 2019
136a476
Call assert_valid() in destroy()
coolreader18 Jan 31, 2019
f47864f
Return Ok(()) from destroy()
coolreader18 Jan 31, 2019
3bec226
Rename VMStore
coolreader18 Feb 5, 2019
f7c91c7
Add add_to_scope() method for WASM VM
coolreader18 Feb 5, 2019
de3762c
Add .run(); fix .addToScope(); add panic-catcher for debugging
coolreader18 Feb 5, 2019
a24cd83
Merge branch 'master' into wasm-vm-class
coolreader18 Feb 5, 2019
9b6516e
Allow closures to be passed from Python to JS if using a WASM VM
coolreader18 Feb 6, 2019
6851767
Fix RefCell borrowing errors
coolreader18 Feb 17, 2019
f55a8ea
Merge branch 'master' into wasm-vm-class
coolreader18 Feb 17, 2019
c83ff47
Fix error with new compile() and set_attr/item()
coolreader18 Feb 17, 2019
3e22c4f
Add print to default vm scope
coolreader18 Feb 17, 2019
a86069d
Store the WASM id in the VirtualMachine, add a (broken) fetch builtin
coolreader18 Feb 17, 2019
f00c3c8
Fix fetch builtin
coolreader18 Feb 17, 2019
00bc9e9
Convert fetch to use wasm_bindgen_futures
coolreader18 Feb 17, 2019
d0b4751
Remove the VM pointer from the map when it's dropped
coolreader18 Feb 17, 2019
7405c84
Add some more options to fetch()
coolreader18 Feb 17, 2019
b043f21
Fix js_py_typeerror
coolreader18 Feb 17, 2019
416f088
Convert `ArrayBuffer`s and `TypedArray`s to Python bytearrays
coolreader18 Feb 17, 2019
bead3f6
Fix conversion from ArrayBuffer, allow array_buffer in fetch
coolreader18 Feb 17, 2019
5244707
Add eval() method, rename run() to exec()
coolreader18 Feb 17, 2019
74e7131
Clean up some code
coolreader18 Feb 18, 2019
101ee77
Improve error messages
coolreader18 Feb 18, 2019
0d3d090
Convert pyEval to use a WASM VM, allowing closures and stuff
coolreader18 Feb 18, 2019
634571f
Remove WASM fetch builtin
coolreader18 Feb 18, 2019
8e5073e
Convert `bytes` and `bytearray`s to `Uint8Array`s
coolreader18 Feb 19, 2019
8c222af
Include details about thread_local! for WASM
coolreader18 Feb 21, 2019
9e17690
Use crate:: imports
coolreader18 Feb 22, 2019
65857e7
Merge
coolreader18 Feb 23, 2019
955d0b3
Merge branch 'master' into wasm-vm-class
coolreader18 Feb 23, 2019
7fa0a0c
Fix open in new make_module
coolreader18 Feb 23, 2019
09e2a7a
Add js_name for .set_stdout()
coolreader18 Feb 23, 2019
e0f222c
Don't hold on to a PyObjectRef from a python -> js closure
coolreader18 Feb 23, 2019
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
44 changes: 37 additions & 7 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 9 additions & 3 deletions vm/src/builtins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ use crate::pyobject::{
AttributeProtocol, IdProtocol, PyContext, PyFuncArgs, PyObject, PyObjectPayload, PyObjectRef,
PyResult, Scope, TypeProtocol,
};

#[cfg(not(target_arch = "wasm32"))]
use crate::stdlib::io::io_open;

use crate::vm::VirtualMachine;
Expand Down Expand Up @@ -702,7 +704,7 @@ fn builtin_sum(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
// builtin___import__

pub fn make_module(ctx: &PyContext) -> PyObjectRef {
py_module!(ctx, "__builtins__", {
let py_mod = py_module!(ctx, "__builtins__", {
//set __name__ fixes: https://github.com/RustPython/RustPython/issues/146
"__name__" => ctx.new_str(String::from("__main__")),

Expand Down Expand Up @@ -747,7 +749,6 @@ pub fn make_module(ctx: &PyContext) -> PyObjectRef {
"min" => ctx.new_rustfunc(builtin_min),
"object" => ctx.object(),
"oct" => ctx.new_rustfunc(builtin_oct),
"open" => ctx.new_rustfunc(io_open),
"ord" => ctx.new_rustfunc(builtin_ord),
"next" => ctx.new_rustfunc(builtin_next),
"pow" => ctx.new_rustfunc(builtin_pow),
Expand Down Expand Up @@ -789,7 +790,12 @@ pub fn make_module(ctx: &PyContext) -> PyObjectRef {
"StopIteration" => ctx.exceptions.stop_iteration.clone(),
"ZeroDivisionError" => ctx.exceptions.zero_division_error.clone(),
"KeyError" => ctx.exceptions.key_error.clone(),
})
});

#[cfg(not(target_arch = "wasm32"))]
ctx.set_attr(&py_mod, "open", ctx.new_rustfunc(io_open));

py_mod
}

pub fn builtin_build_class_(vm: &mut VirtualMachine, mut args: PyFuncArgs) -> PyResult {
Expand Down
2 changes: 1 addition & 1 deletion vm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ extern crate rustpython_parser;

// This is above everything else so that the defined macros are available everywhere
#[macro_use]
mod macros;
pub mod macros;

mod builtins;
pub mod bytecode;
Expand Down
5 changes: 5 additions & 0 deletions vm/src/macros.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
// count number of tokens given as arguments.
// see: https://danielkeep.github.io/tlborm/book/blk-counting.html
#[macro_export]
macro_rules! replace_expr {
($_t:tt $sub:expr) => {
$sub
};
}

#[macro_export]
macro_rules! count_tts {
($($tts:tt)*) => {0usize $(+ replace_expr!($tts 1usize))*};
}

#[macro_export]
macro_rules! type_check {
($vm:ident, $args:ident, $arg_count:ident, $arg_name:ident, $arg_type:expr) => {
// None indicates that we have no type requirement (i.e. we accept any type)
Expand All @@ -31,6 +34,7 @@ macro_rules! type_check {
};
}

#[macro_export]
macro_rules! arg_check {
( $vm: ident, $args:ident ) => {
// Zero-arg case
Expand Down Expand Up @@ -94,6 +98,7 @@ macro_rules! arg_check {
};
}

#[macro_export]
macro_rules! no_kwargs {
( $vm: ident, $args:ident ) => {
// Zero-arg case
Expand Down
17 changes: 13 additions & 4 deletions vm/src/stdlib/mod.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
mod ast;
mod dis;
pub mod io;
mod json;
mod keyword;
mod math;
mod os;
mod pystruct;
mod random;
mod re;
Expand All @@ -15,6 +13,11 @@ mod types;
mod weakref;
use std::collections::HashMap;

#[cfg(not(target_arch = "wasm32"))]
pub mod io;
#[cfg(not(target_arch = "wasm32"))]
mod os;

use crate::pyobject::{PyContext, PyObjectRef};

pub type StdlibInitFunc = fn(&PyContext) -> PyObjectRef;
Expand All @@ -23,11 +26,9 @@ pub fn get_module_inits() -> HashMap<String, StdlibInitFunc> {
let mut modules = HashMap::new();
modules.insert("ast".to_string(), ast::mk_module as StdlibInitFunc);
modules.insert("dis".to_string(), dis::mk_module as StdlibInitFunc);
modules.insert("io".to_string(), io::mk_module as StdlibInitFunc);
modules.insert("json".to_string(), json::mk_module as StdlibInitFunc);
modules.insert("keyword".to_string(), keyword::mk_module as StdlibInitFunc);
modules.insert("math".to_string(), math::mk_module as StdlibInitFunc);
modules.insert("os".to_string(), os::mk_module as StdlibInitFunc);
modules.insert("re".to_string(), re::mk_module as StdlibInitFunc);
modules.insert("random".to_string(), random::mk_module as StdlibInitFunc);
modules.insert("string".to_string(), string::mk_module as StdlibInitFunc);
Expand All @@ -39,5 +40,13 @@ pub fn get_module_inits() -> HashMap<String, StdlibInitFunc> {
);
modules.insert("types".to_string(), types::mk_module as StdlibInitFunc);
modules.insert("_weakref".to_string(), weakref::mk_module as StdlibInitFunc);

// disable some modules on WASM
#[cfg(not(target_arch = "wasm32"))]
{
modules.insert("io".to_string(), io::mk_module as StdlibInitFunc);
modules.insert("os".to_string(), os::mk_module as StdlibInitFunc);
}

modules
}
2 changes: 2 additions & 0 deletions vm/src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ pub struct VirtualMachine {
pub stdlib_inits: HashMap<String, stdlib::StdlibInitFunc>,
pub ctx: PyContext,
pub current_frame: Option<PyObjectRef>,
pub wasm_id: Option<String>,
}

impl VirtualMachine {
Expand All @@ -61,6 +62,7 @@ impl VirtualMachine {
stdlib_inits,
ctx,
current_frame: None,
wasm_id: None,
}
}

Expand Down
19 changes: 16 additions & 3 deletions wasm/lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,25 @@ edition = "2018"
crate-type = ["cdylib", "rlib"]

[dependencies]
rustpython_parser = {path = "../../parser"}
rustpython_vm = {path = "../../vm"}
rustpython_parser = { path = "../../parser" }
rustpython_vm = { path = "../../vm" }
cfg-if = "0.1.2"
wasm-bindgen = "0.2"
wasm-bindgen-futures = "0.3"
js-sys = "0.3"
futures = "0.1"
console_error_panic_hook = "0.1"

[dependencies.web-sys]
version = "0.3"
features = [ "console", "Document", "Element", "HtmlTextAreaElement", "Window" ]
features = [
"console",
"Document",
"Element",
"HtmlTextAreaElement",
"Window",
"Headers",
"Request",
"RequestInit",
"Response"
]
Loading