Skip to content

Commit 654ab56

Browse files
committed
Rework VM initialization some more; make more stuff private
1 parent 4b9a015 commit 654ab56

File tree

4 files changed

+43
-62
lines changed

4 files changed

+43
-62
lines changed

src/main.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,7 @@ fn main() {
2929
env_logger::init();
3030
let app = App::new("RustPython");
3131
let matches = parse_arguments(app);
32-
let mut settings = create_settings(&matches);
33-
34-
// We only include the standard library bytecode in WASI when initializing
35-
if cfg!(target_os = "wasi") {
36-
settings.initialization_parameter = InitParameter::InitializeInternal;
37-
}
32+
let settings = create_settings(&matches);
3833

3934
// don't translate newlines (\r\n <=> \n)
4035
#[cfg(windows)]
@@ -49,7 +44,14 @@ fn main() {
4944
}
5045
}
5146

52-
let interp = Interpreter::new(settings);
47+
// We only include the standard library bytecode in WASI when initializing
48+
let init = if cfg!(target_os = "wasi") {
49+
InitParameter::Internal
50+
} else {
51+
InitParameter::External
52+
};
53+
54+
let interp = Interpreter::new(settings, init);
5355

5456
interp.enter(move |vm| {
5557
let res = run_rustpython(vm, &matches);

vm/src/import.rs

Lines changed: 26 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use crate::vm::{InitParameter, VirtualMachine};
1414
#[cfg(feature = "rustpython-compiler")]
1515
use rustpython_compiler::compile;
1616

17-
pub fn init_importlib(
17+
pub(crate) fn init_importlib(
1818
vm: &mut VirtualMachine,
1919
initialize_parameter: InitParameter,
2020
) -> PyResult<()> {
@@ -30,39 +30,32 @@ pub fn init_importlib(
3030
})?;
3131
vm.import_func = vm.get_attribute(importlib.clone(), "__import__")?;
3232

33-
match initialize_parameter {
34-
InitParameter::InitializeExternal if cfg!(feature = "rustpython-compiler") => {
35-
enter_vm(vm, || {
36-
flame_guard!("install_external");
37-
let install_external =
38-
vm.get_attribute(importlib, "_install_external_importers")?;
39-
vm.invoke(&install_external, vec![])?;
40-
// Set pyc magic number to commit hash. Should be changed when bytecode will be more stable.
41-
let importlib_external = vm.import("_frozen_importlib_external", &[], 0)?;
42-
let mut magic = get_git_revision().into_bytes();
43-
magic.truncate(4);
44-
if magic.len() != 4 {
45-
magic = rand::thread_rng().gen::<[u8; 4]>().to_vec();
46-
}
47-
vm.set_attr(&importlib_external, "MAGIC_NUMBER", vm.ctx.new_bytes(magic))?;
48-
let zipimport_res = (|| -> PyResult<()> {
49-
let zipimport = vm.import("zipimport", &[], 0)?;
50-
let zipimporter = vm.get_attribute(zipimport, "zipimporter")?;
51-
let path_hooks = vm.get_attribute(vm.sys_module.clone(), "path_hooks")?;
52-
let path_hooks = objlist::PyListRef::try_from_object(vm, path_hooks)?;
53-
path_hooks.insert(0, zipimporter);
54-
Ok(())
55-
})();
56-
if zipimport_res.is_err() {
57-
warn!("couldn't init zipimport")
58-
}
33+
if initialize_parameter == InitParameter::External && cfg!(feature = "rustpython-compiler") {
34+
enter_vm(vm, || {
35+
flame_guard!("install_external");
36+
let install_external = vm.get_attribute(importlib, "_install_external_importers")?;
37+
vm.invoke(&install_external, vec![])?;
38+
// Set pyc magic number to commit hash. Should be changed when bytecode will be more stable.
39+
let importlib_external = vm.import("_frozen_importlib_external", &[], 0)?;
40+
let mut magic = get_git_revision().into_bytes();
41+
magic.truncate(4);
42+
if magic.len() != 4 {
43+
magic = rand::thread_rng().gen::<[u8; 4]>().to_vec();
44+
}
45+
vm.set_attr(&importlib_external, "MAGIC_NUMBER", vm.ctx.new_bytes(magic))?;
46+
let zipimport_res = (|| -> PyResult<()> {
47+
let zipimport = vm.import("zipimport", &[], 0)?;
48+
let zipimporter = vm.get_attribute(zipimport, "zipimporter")?;
49+
let path_hooks = vm.get_attribute(vm.sys_module.clone(), "path_hooks")?;
50+
let path_hooks = objlist::PyListRef::try_from_object(vm, path_hooks)?;
51+
path_hooks.insert(0, zipimporter);
5952
Ok(())
60-
})?
61-
}
62-
InitParameter::NoInitialize => {
63-
panic!("Import library initialize should be InitializeInternal or InitializeExternal");
64-
}
65-
_ => {}
53+
})();
54+
if zipimport_res.is_err() {
55+
warn!("couldn't init zipimport")
56+
}
57+
Ok(())
58+
})?
6659
}
6760
Ok(())
6861
}

vm/src/vm.rs

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -126,11 +126,10 @@ pub struct PyGlobalState {
126126

127127
pub const NSIG: usize = 64;
128128

129-
#[derive(Copy, Clone)]
129+
#[derive(Copy, Clone, PartialEq, Eq)]
130130
pub enum InitParameter {
131-
NoInitialize,
132-
InitializeInternal,
133-
InitializeExternal,
131+
Internal,
132+
External,
134133
}
135134

136135
/// Struct containing all kind of settings for the python vm.
@@ -168,10 +167,6 @@ pub struct PySettings {
168167
/// sys.argv
169168
pub argv: Vec<String>,
170169

171-
/// Initialization parameter to decide to initialize or not,
172-
/// and to decide the importer required external filesystem access or not
173-
pub initialization_parameter: InitParameter,
174-
175170
/// PYTHONHASHSEED=x
176171
pub hash_seed: Option<u32>,
177172
}
@@ -207,7 +202,6 @@ impl Default for PySettings {
207202
dont_write_bytecode: false,
208203
path_list: vec![],
209204
argv: vec![],
210-
initialization_parameter: InitParameter::InitializeExternal,
211205
hash_seed: None,
212206
}
213207
}
@@ -278,11 +272,10 @@ impl VirtualMachine {
278272
vm
279273
}
280274

281-
pub fn initialize(&mut self, initialize_parameter: InitParameter) {
275+
fn initialize(&mut self, initialize_parameter: InitParameter) {
282276
flame_guard!("init VirtualMachine");
283277

284278
match initialize_parameter {
285-
InitParameter::NoInitialize => {}
286279
_ => {
287280
if self.initialized {
288281
panic!("Double Initialize Error");
@@ -1582,8 +1575,7 @@ pub struct Interpreter {
15821575
}
15831576

15841577
impl Interpreter {
1585-
pub fn new(settings: PySettings) -> Self {
1586-
let init = settings.initialization_parameter;
1578+
pub fn new(settings: PySettings, init: InitParameter) -> Self {
15871579
Self::new_with_init(settings, |_| init)
15881580
}
15891581

@@ -1618,7 +1610,7 @@ impl Interpreter {
16181610

16191611
impl Default for Interpreter {
16201612
fn default() -> Self {
1621-
Self::new(PySettings::default())
1613+
Self::new(PySettings::default(), InitParameter::External)
16221614
}
16231615
}
16241616

wasm/lib/src/vm_class.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,8 @@ pub(crate) struct StoredVirtualMachine {
2727

2828
impl StoredVirtualMachine {
2929
fn new(id: String, inject_browser_module: bool) -> StoredVirtualMachine {
30-
let settings = PySettings {
31-
// After js, browser modules injected, the VM will not be initialized.
32-
initialization_parameter: InitParameter::NoInitialize,
33-
..Default::default()
34-
};
35-
3630
let mut scope = None;
37-
let interp = Interpreter::new_with_init(settings, |vm| {
31+
let interp = Interpreter::new_with_init(PySettings::default(), |vm| {
3832
vm.wasm_id = Some(id);
3933

4034
js_module::setup_js_module(vm);
@@ -52,7 +46,7 @@ impl StoredVirtualMachine {
5246

5347
scope = Some(vm.new_scope_with_builtins());
5448

55-
InitParameter::InitializeInternal
49+
InitParameter::Internal
5650
});
5751

5852
StoredVirtualMachine {

0 commit comments

Comments
 (0)