Skip to content

Commit e05a75c

Browse files
Merge pull request RustPython#1092 from RustPython/bytecode-crate
Make parser and compiler optional features for vm crate.
2 parents dfabd4a + 3954dbe commit e05a75c

File tree

12 files changed

+58
-24
lines changed

12 files changed

+58
-24
lines changed

Cargo.lock

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

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ path = "./benchmarks/bench.rs"
1616
log="0.4.1"
1717
env_logger="0.5.10"
1818
clap = "2.31.2"
19+
rustpython_compiler = {path = "compiler"}
1920
rustpython_parser = {path = "parser"}
2021
rustpython_vm = {path = "vm"}
2122
rustyline = "4.1.0"

src/main.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,17 @@ extern crate rustpython_vm;
88
extern crate rustyline;
99

1010
use clap::{App, Arg};
11+
use rustpython_compiler::{compile, error::CompileError, error::CompileErrorType};
1112
use rustpython_parser::error::ParseError;
1213
use rustpython_vm::{
13-
compile,
14-
error::CompileError,
15-
error::CompileErrorType,
1614
frame::Scope,
1715
import,
1816
obj::objstr,
1917
print_exception,
2018
pyobject::{ItemProtocol, PyResult},
2119
util, VirtualMachine,
2220
};
21+
2322
use rustyline::{error::ReadlineError, Editor};
2423
use std::path::PathBuf;
2524

vm/Cargo.toml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ version = "0.1.0"
44
authors = ["Shing Lyu <[email protected]>"]
55
edition = "2018"
66

7+
[features]
8+
default = ["rustpython_parser", "rustpython_compiler"]
9+
710
[dependencies]
811
# Crypto:
912
digest = "0.8.1"
@@ -21,8 +24,8 @@ num-rational = "0.2.1"
2124
rand = "0.5"
2225
log = "0.3"
2326
rustpython_derive = {path = "../derive"}
24-
rustpython_parser = {path = "../parser"}
25-
rustpython_compiler = {path = "../compiler"}
27+
rustpython_parser = {path = "../parser", optional = true}
28+
rustpython_compiler = {path = "../compiler", optional = true}
2629
rustpython_bytecode = { path = "../bytecode" }
2730
serde = { version = "1.0.66", features = ["derive"] }
2831
serde_json = "1.0.26"

vm/src/builtins.rs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ use std::str;
1010
use num_bigint::Sign;
1111
use num_traits::{Signed, ToPrimitive, Zero};
1212

13-
use crate::compile;
1413
use crate::obj::objbool;
1514
use crate::obj::objbytes::PyBytesRef;
1615
use crate::obj::objcode::PyCodeRef;
@@ -19,6 +18,8 @@ use crate::obj::objint::{self, PyIntRef};
1918
use crate::obj::objiter;
2019
use crate::obj::objstr::{self, PyString, PyStringRef};
2120
use crate::obj::objtype::{self, PyClassRef};
21+
#[cfg(feature = "rustpython_compiler")]
22+
use rustpython_compiler::compile;
2223

2324
use crate::frame::Scope;
2425
use crate::function::{single_or_tuple_any, Args, KwArgs, OptionalArg, PyFuncArgs};
@@ -98,6 +99,7 @@ struct CompileArgs {
9899
optimize: OptionalArg<PyIntRef>,
99100
}
100101

102+
#[cfg(feature = "rustpython_compiler")]
101103
fn builtin_compile(args: CompileArgs, vm: &VirtualMachine) -> PyResult<PyCodeRef> {
102104
// TODO: compile::compile should probably get bytes
103105
let source = match args.source {
@@ -153,6 +155,7 @@ fn builtin_divmod(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
153155

154156
/// Implements `eval`.
155157
/// See also: https://docs.python.org/3/library/functions.html#eval
158+
#[cfg(feature = "rustpython_compiler")]
156159
fn builtin_eval(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
157160
// TODO: support any mapping for `locals`
158161
arg_check!(
@@ -184,6 +187,7 @@ fn builtin_eval(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
184187

185188
/// Implements `exec`
186189
/// https://docs.python.org/3/library/functions.html#exec
190+
#[cfg(feature = "rustpython_compiler")]
187191
fn builtin_exec(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
188192
arg_check!(
189193
vm,
@@ -785,6 +789,15 @@ pub fn make_module(vm: &VirtualMachine, module: PyObjectRef) {
785789
#[cfg(not(target_arch = "wasm32"))]
786790
let open = vm.ctx.new_rustfunc(io_open);
787791

792+
#[cfg(feature = "rustpython_compiler")]
793+
{
794+
extend_module!(vm, module, {
795+
"compile" => ctx.new_rustfunc(builtin_compile),
796+
"eval" => ctx.new_rustfunc(builtin_eval),
797+
"exec" => ctx.new_rustfunc(builtin_exec),
798+
});
799+
}
800+
788801
extend_module!(vm, module, {
789802
//set __name__ fixes: https://github.com/RustPython/RustPython/issues/146
790803
"__name__" => ctx.new_str(String::from("__main__")),
@@ -799,15 +812,12 @@ pub fn make_module(vm: &VirtualMachine, module: PyObjectRef) {
799812
"callable" => ctx.new_rustfunc(builtin_callable),
800813
"chr" => ctx.new_rustfunc(builtin_chr),
801814
"classmethod" => ctx.classmethod_type(),
802-
"compile" => ctx.new_rustfunc(builtin_compile),
803815
"complex" => ctx.complex_type(),
804816
"delattr" => ctx.new_rustfunc(builtin_delattr),
805817
"dict" => ctx.dict_type(),
806818
"divmod" => ctx.new_rustfunc(builtin_divmod),
807819
"dir" => ctx.new_rustfunc(builtin_dir),
808820
"enumerate" => ctx.enumerate_type(),
809-
"eval" => ctx.new_rustfunc(builtin_eval),
810-
"exec" => ctx.new_rustfunc(builtin_exec),
811821
"float" => ctx.float_type(),
812822
"frozenset" => ctx.frozenset_type(),
813823
"filter" => ctx.filter_type(),

vm/src/eval.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
use crate::compile;
21
use crate::frame::Scope;
32
use crate::pyobject::PyResult;
43
use crate::vm::VirtualMachine;
4+
use rustpython_compiler::compile;
55

66
pub fn eval(vm: &VirtualMachine, source: &str, scope: Scope, source_path: &str) -> PyResult {
77
match vm.compile(source, &compile::Mode::Eval, source_path.to_string()) {

vm/src/import.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@
55
use std::path::PathBuf;
66

77
use crate::bytecode::CodeObject;
8-
use crate::compile;
98
use crate::frame::Scope;
109
use crate::obj::{objcode, objsequence, objstr};
1110
use crate::pyobject::{ItemProtocol, PyResult, PyValue};
1211
use crate::util;
1312
use crate::vm::VirtualMachine;
13+
#[cfg(feature = "rustpython_compiler")]
14+
use rustpython_compiler::compile;
1415

1516
pub fn init_importlib(vm: &VirtualMachine) -> PyResult {
1617
let importlib = import_frozen(vm, "_frozen_importlib")?;
@@ -56,7 +57,7 @@ pub fn import_module(vm: &VirtualMachine, current_path: PathBuf, module_name: &s
5657
import_frozen(vm, module_name)
5758
} else if vm.stdlib_inits.borrow().contains_key(module_name) {
5859
import_builtin(vm, module_name)
59-
} else {
60+
} else if cfg!(feature = "rustpython_compiler") {
6061
let notfound_error = &vm.ctx.exceptions.module_not_found_error;
6162
let import_error = &vm.ctx.exceptions.import_error;
6263

@@ -72,9 +73,13 @@ pub fn import_module(vm: &VirtualMachine, current_path: PathBuf, module_name: &s
7273
file_path.to_str().unwrap().to_string(),
7374
source,
7475
)
76+
} else {
77+
let notfound_error = &vm.ctx.exceptions.module_not_found_error;
78+
Err(vm.new_exception(notfound_error.clone(), module_name.to_string()))
7579
}
7680
}
7781

82+
#[cfg(feature = "rustpython_compiler")]
7883
pub fn import_file(
7984
vm: &VirtualMachine,
8085
module_name: &str,

vm/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ pub mod macros;
4444
mod builtins;
4545
pub mod cformat;
4646
mod dictdatatype;
47+
#[cfg(feature = "rustpython_compiler")]
4748
pub mod eval;
4849
mod exceptions;
4950
pub mod format;
@@ -65,7 +66,6 @@ mod vm;
6566
pub use self::exceptions::print_exception;
6667
pub use self::vm::VirtualMachine;
6768
pub use rustpython_bytecode::*;
68-
pub use rustpython_compiler::*;
6969

7070
#[doc(hidden)]
7171
pub mod __exports {

vm/src/stdlib/mod.rs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1+
#[cfg(feature = "rustpython_parser")]
12
mod ast;
23
mod binascii;
34
mod dis;
45
mod hashlib;
56
mod imp;
67
mod itertools;
78
mod json;
9+
#[cfg(feature = "rustpython_parser")]
810
mod keyword;
911
mod marshal;
1012
mod math;
@@ -16,6 +18,7 @@ pub mod socket;
1618
mod string;
1719
mod thread;
1820
mod time_module;
21+
#[cfg(feature = "rustpython_parser")]
1922
mod tokenize;
2023
mod warnings;
2124
mod weakref;
@@ -37,13 +40,11 @@ pub type StdlibInitFunc = Box<dyn Fn(&VirtualMachine) -> PyObjectRef>;
3740
pub fn get_module_inits() -> HashMap<String, StdlibInitFunc> {
3841
#[allow(unused_mut)]
3942
let mut modules = hashmap! {
40-
"ast".to_string() => Box::new(ast::make_module) as StdlibInitFunc,
41-
"binascii".to_string() => Box::new(binascii::make_module),
42-
"dis".to_string() => Box::new(dis::make_module),
43+
"binascii".to_string() => Box::new(binascii::make_module) as StdlibInitFunc,
44+
"dis".to_string() => Box::new(dis::make_module) as StdlibInitFunc,
4345
"hashlib".to_string() => Box::new(hashlib::make_module),
4446
"itertools".to_string() => Box::new(itertools::make_module),
4547
"json".to_string() => Box::new(json::make_module),
46-
"keyword".to_string() => Box::new(keyword::make_module),
4748
"marshal".to_string() => Box::new(marshal::make_module),
4849
"math".to_string() => Box::new(math::make_module),
4950
"platform".to_string() => Box::new(platform::make_module),
@@ -53,12 +54,22 @@ pub fn get_module_inits() -> HashMap<String, StdlibInitFunc> {
5354
"struct".to_string() => Box::new(pystruct::make_module),
5455
"_thread".to_string() => Box::new(thread::make_module),
5556
"time".to_string() => Box::new(time_module::make_module),
56-
"tokenize".to_string() => Box::new(tokenize::make_module),
5757
"_weakref".to_string() => Box::new(weakref::make_module),
5858
"_imp".to_string() => Box::new(imp::make_module),
5959
"_warnings".to_string() => Box::new(warnings::make_module),
6060
};
6161

62+
// Insert parser related modules:
63+
#[cfg(feature = "rustpython_parser")]
64+
{
65+
modules.insert(
66+
"ast".to_string(),
67+
Box::new(ast::make_module) as StdlibInitFunc,
68+
);
69+
modules.insert("keyword".to_string(), Box::new(keyword::make_module));
70+
modules.insert("tokenize".to_string(), Box::new(tokenize::make_module));
71+
}
72+
6273
// disable some modules on WASM
6374
#[cfg(not(target_arch = "wasm32"))]
6475
{

vm/src/vm.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ use std::sync::{Mutex, MutexGuard};
1212

1313
use crate::builtins;
1414
use crate::bytecode;
15-
use crate::compile;
16-
use crate::error::CompileError;
1715
use crate::frame::{ExecutionResult, Frame, FrameRef, Scope};
1816
use crate::frozen;
1917
use crate::function::PyFuncArgs;
@@ -38,6 +36,10 @@ use crate::pyobject::{
3836
use crate::stdlib;
3937
use crate::sysmodule;
4038
use num_bigint::BigInt;
39+
#[cfg(feature = "rustpython_compiler")]
40+
use rustpython_compiler::compile;
41+
#[cfg(feature = "rustpython_compiler")]
42+
use rustpython_compiler::error::CompileError;
4143

4244
// use objects::objects;
4345

@@ -249,6 +251,7 @@ impl VirtualMachine {
249251
self.new_exception(overflow_error, msg)
250252
}
251253

254+
#[cfg(feature = "rustpython_compiler")]
252255
pub fn new_syntax_error(&self, error: &CompileError) -> PyObjectRef {
253256
let syntax_error_type = self.ctx.exceptions.syntax_error.clone();
254257
let syntax_error = self.new_exception(syntax_error_type, error.to_string());
@@ -730,6 +733,7 @@ impl VirtualMachine {
730733
)
731734
}
732735

736+
#[cfg(feature = "rustpython_compiler")]
733737
pub fn compile(
734738
&self,
735739
source: &str,

wasm/lib/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ edition = "2018"
1111
crate-type = ["cdylib", "rlib"]
1212

1313
[dependencies]
14+
rustpython_compiler = { path = "../../compiler" }
1415
rustpython_parser = { path = "../../parser" }
1516
rustpython_vm = { path = "../../vm" }
1617
cfg-if = "0.1.2"

wasm/lib/src/vm_class.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::rc::{Rc, Weak};
55
use js_sys::{Object, Reflect, SyntaxError, TypeError};
66
use wasm_bindgen::prelude::*;
77

8-
use rustpython_vm::compile;
8+
use rustpython_compiler::{compile, error::CompileErrorType};
99
use rustpython_vm::frame::{NameProtocol, Scope};
1010
use rustpython_vm::function::PyFuncArgs;
1111
use rustpython_vm::pyobject::{PyObject, PyObjectPayload, PyObjectRef, PyResult, PyValue};
@@ -280,9 +280,7 @@ impl WASMVirtualMachine {
280280
let code = vm.compile(&source, &mode, "<wasm>".to_string());
281281
let code = code.map_err(|err| {
282282
let js_err = SyntaxError::new(&format!("Error parsing Python code: {}", err));
283-
if let rustpython_vm::error::CompileErrorType::Parse(ref parse_error) =
284-
err.error
285-
{
283+
if let CompileErrorType::Parse(ref parse_error) = err.error {
286284
use rustpython_parser::error::ParseError;
287285
if let ParseError::EOF(Some(ref loc))
288286
| ParseError::ExtraToken((ref loc, ..))

0 commit comments

Comments
 (0)