Skip to content

Commit b15ade1

Browse files
Merge pull request #267 from coolreader18/wasm-vm-class
Add a VirtualMachine class to the WASM library
2 parents 3f8f0e2 + e0f222c commit b15ade1

File tree

11 files changed

+623
-206
lines changed

11 files changed

+623
-206
lines changed

Cargo.lock

Lines changed: 37 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vm/src/builtins.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ use crate::pyobject::{
1818
AttributeProtocol, IdProtocol, PyContext, PyFuncArgs, PyObject, PyObjectPayload, PyObjectRef,
1919
PyResult, Scope, TypeProtocol,
2020
};
21+
22+
#[cfg(not(target_arch = "wasm32"))]
2123
use crate::stdlib::io::io_open;
2224

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

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

@@ -747,7 +749,6 @@ pub fn make_module(ctx: &PyContext) -> PyObjectRef {
747749
"min" => ctx.new_rustfunc(builtin_min),
748750
"object" => ctx.object(),
749751
"oct" => ctx.new_rustfunc(builtin_oct),
750-
"open" => ctx.new_rustfunc(io_open),
751752
"ord" => ctx.new_rustfunc(builtin_ord),
752753
"next" => ctx.new_rustfunc(builtin_next),
753754
"pow" => ctx.new_rustfunc(builtin_pow),
@@ -789,7 +790,12 @@ pub fn make_module(ctx: &PyContext) -> PyObjectRef {
789790
"StopIteration" => ctx.exceptions.stop_iteration.clone(),
790791
"ZeroDivisionError" => ctx.exceptions.zero_division_error.clone(),
791792
"KeyError" => ctx.exceptions.key_error.clone(),
792-
})
793+
});
794+
795+
#[cfg(not(target_arch = "wasm32"))]
796+
ctx.set_attr(&py_mod, "open", ctx.new_rustfunc(io_open));
797+
798+
py_mod
793799
}
794800

795801
pub fn builtin_build_class_(vm: &mut VirtualMachine, mut args: PyFuncArgs) -> PyResult {

vm/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ extern crate rustpython_parser;
2828

2929
// This is above everything else so that the defined macros are available everywhere
3030
#[macro_use]
31-
mod macros;
31+
pub mod macros;
3232

3333
mod builtins;
3434
pub mod bytecode;

vm/src/macros.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
// count number of tokens given as arguments.
22
// see: https://danielkeep.github.io/tlborm/book/blk-counting.html
3+
#[macro_export]
34
macro_rules! replace_expr {
45
($_t:tt $sub:expr) => {
56
$sub
67
};
78
}
89

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

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

37+
#[macro_export]
3438
macro_rules! arg_check {
3539
( $vm: ident, $args:ident ) => {
3640
// Zero-arg case
@@ -94,6 +98,7 @@ macro_rules! arg_check {
9498
};
9599
}
96100

101+
#[macro_export]
97102
macro_rules! no_kwargs {
98103
( $vm: ident, $args:ident ) => {
99104
// Zero-arg case

vm/src/stdlib/mod.rs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
mod ast;
22
mod dis;
3-
pub mod io;
43
mod json;
54
mod keyword;
65
mod math;
7-
mod os;
86
mod pystruct;
97
mod random;
108
mod re;
@@ -15,6 +13,11 @@ mod types;
1513
mod weakref;
1614
use std::collections::HashMap;
1715

16+
#[cfg(not(target_arch = "wasm32"))]
17+
pub mod io;
18+
#[cfg(not(target_arch = "wasm32"))]
19+
mod os;
20+
1821
use crate::pyobject::{PyContext, PyObjectRef};
1922

2023
pub type StdlibInitFunc = fn(&PyContext) -> PyObjectRef;
@@ -23,11 +26,9 @@ pub fn get_module_inits() -> HashMap<String, StdlibInitFunc> {
2326
let mut modules = HashMap::new();
2427
modules.insert("ast".to_string(), ast::mk_module as StdlibInitFunc);
2528
modules.insert("dis".to_string(), dis::mk_module as StdlibInitFunc);
26-
modules.insert("io".to_string(), io::mk_module as StdlibInitFunc);
2729
modules.insert("json".to_string(), json::mk_module as StdlibInitFunc);
2830
modules.insert("keyword".to_string(), keyword::mk_module as StdlibInitFunc);
2931
modules.insert("math".to_string(), math::mk_module as StdlibInitFunc);
30-
modules.insert("os".to_string(), os::mk_module as StdlibInitFunc);
3132
modules.insert("re".to_string(), re::mk_module as StdlibInitFunc);
3233
modules.insert("random".to_string(), random::mk_module as StdlibInitFunc);
3334
modules.insert("string".to_string(), string::mk_module as StdlibInitFunc);
@@ -39,5 +40,13 @@ pub fn get_module_inits() -> HashMap<String, StdlibInitFunc> {
3940
);
4041
modules.insert("types".to_string(), types::mk_module as StdlibInitFunc);
4142
modules.insert("_weakref".to_string(), weakref::mk_module as StdlibInitFunc);
43+
44+
// disable some modules on WASM
45+
#[cfg(not(target_arch = "wasm32"))]
46+
{
47+
modules.insert("io".to_string(), io::mk_module as StdlibInitFunc);
48+
modules.insert("os".to_string(), os::mk_module as StdlibInitFunc);
49+
}
50+
4251
modules
4352
}

vm/src/vm.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ pub struct VirtualMachine {
3939
pub stdlib_inits: HashMap<String, stdlib::StdlibInitFunc>,
4040
pub ctx: PyContext,
4141
pub current_frame: Option<PyObjectRef>,
42+
pub wasm_id: Option<String>,
4243
}
4344

4445
impl VirtualMachine {
@@ -61,6 +62,7 @@ impl VirtualMachine {
6162
stdlib_inits,
6263
ctx,
6364
current_frame: None,
65+
wasm_id: None,
6466
}
6567
}
6668

wasm/lib/Cargo.toml

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,25 @@ edition = "2018"
1111
crate-type = ["cdylib", "rlib"]
1212

1313
[dependencies]
14-
rustpython_parser = {path = "../../parser"}
15-
rustpython_vm = {path = "../../vm"}
14+
rustpython_parser = { path = "../../parser" }
15+
rustpython_vm = { path = "../../vm" }
1616
cfg-if = "0.1.2"
1717
wasm-bindgen = "0.2"
18+
wasm-bindgen-futures = "0.3"
1819
js-sys = "0.3"
20+
futures = "0.1"
21+
console_error_panic_hook = "0.1"
1922

2023
[dependencies.web-sys]
2124
version = "0.3"
22-
features = [ "console", "Document", "Element", "HtmlTextAreaElement", "Window" ]
25+
features = [
26+
"console",
27+
"Document",
28+
"Element",
29+
"HtmlTextAreaElement",
30+
"Window",
31+
"Headers",
32+
"Request",
33+
"RequestInit",
34+
"Response"
35+
]

0 commit comments

Comments
 (0)