Skip to content

Commit c7f5da6

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

File tree

4 files changed

+83
-108
lines changed

4 files changed

+83
-108
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: 46 additions & 60 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,62 +272,55 @@ 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

284-
match initialize_parameter {
285-
InitParameter::NoInitialize => {}
286-
_ => {
287-
if self.initialized {
288-
panic!("Double Initialize Error");
289-
}
290-
291-
builtins::make_module(self, self.builtins.clone());
292-
sysmodule::make_module(self, self.sys_module.clone(), self.builtins.clone());
293-
294-
let mut inner_init = || -> PyResult<()> {
295-
#[cfg(not(target_arch = "wasm32"))]
296-
import::import_builtin(self, "signal")?;
297-
298-
import::init_importlib(self, initialize_parameter)?;
299-
300-
#[cfg(any(not(target_arch = "wasm32"), target_os = "wasi"))]
301-
{
302-
// this isn't fully compatible with CPython; it imports "io" and sets
303-
// builtins.open to io.OpenWrapper, but this is easier, since it doesn't
304-
// require the Python stdlib to be present
305-
let io = self.import("_io", &[], 0)?;
306-
let io_open = self.get_attribute(io, "open")?;
307-
let set_stdio = |name, fd, mode: &str| {
308-
let stdio = self.invoke(
309-
&io_open,
310-
vec![self.ctx.new_int(fd), self.new_pyobj(mode)],
311-
)?;
312-
self.set_attr(
313-
&self.sys_module,
314-
format!("__{}__", name), // e.g. __stdin__
315-
stdio.clone(),
316-
)?;
317-
self.set_attr(&self.sys_module, name, stdio)?;
318-
Ok(())
319-
};
320-
set_stdio("stdin", 0, "r")?;
321-
set_stdio("stdout", 1, "w")?;
322-
set_stdio("stderr", 2, "w")?;
323-
324-
self.set_attr(&self.builtins, "open", io_open)?;
325-
}
278+
if self.initialized {
279+
panic!("Double Initialize Error");
280+
}
326281

282+
builtins::make_module(self, self.builtins.clone());
283+
sysmodule::make_module(self, self.sys_module.clone(), self.builtins.clone());
284+
285+
let mut inner_init = || -> PyResult<()> {
286+
#[cfg(not(target_arch = "wasm32"))]
287+
import::import_builtin(self, "signal")?;
288+
289+
import::init_importlib(self, initialize_parameter)?;
290+
291+
#[cfg(any(not(target_arch = "wasm32"), target_os = "wasi"))]
292+
{
293+
// this isn't fully compatible with CPython; it imports "io" and sets
294+
// builtins.open to io.OpenWrapper, but this is easier, since it doesn't
295+
// require the Python stdlib to be present
296+
let io = self.import("_io", &[], 0)?;
297+
let io_open = self.get_attribute(io, "open")?;
298+
let set_stdio = |name, fd, mode: &str| {
299+
let stdio =
300+
self.invoke(&io_open, vec![self.ctx.new_int(fd), self.new_pyobj(mode)])?;
301+
self.set_attr(
302+
&self.sys_module,
303+
format!("__{}__", name), // e.g. __stdin__
304+
stdio.clone(),
305+
)?;
306+
self.set_attr(&self.sys_module, name, stdio)?;
327307
Ok(())
328308
};
309+
set_stdio("stdin", 0, "r")?;
310+
set_stdio("stdout", 1, "w")?;
311+
set_stdio("stderr", 2, "w")?;
312+
313+
self.set_attr(&self.builtins, "open", io_open)?;
314+
}
315+
316+
Ok(())
317+
};
329318

330-
let res = inner_init();
319+
let res = inner_init();
331320

332-
self.expect_pyresult(res, "initializiation failed");
321+
self.expect_pyresult(res, "initializiation failed");
333322

334-
self.initialized = true;
335-
}
336-
}
323+
self.initialized = true;
337324
}
338325

339326
#[cfg(feature = "threading")]
@@ -1582,8 +1569,7 @@ pub struct Interpreter {
15821569
}
15831570

15841571
impl Interpreter {
1585-
pub fn new(settings: PySettings) -> Self {
1586-
let init = settings.initialization_parameter;
1572+
pub fn new(settings: PySettings, init: InitParameter) -> Self {
15871573
Self::new_with_init(settings, |_| init)
15881574
}
15891575

@@ -1618,7 +1604,7 @@ impl Interpreter {
16181604

16191605
impl Default for Interpreter {
16201606
fn default() -> Self {
1621-
Self::new(PySettings::default())
1607+
Self::new(PySettings::default(), InitParameter::External)
16221608
}
16231609
}
16241610

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)