Skip to content

Commit 1d3ec86

Browse files
committed
vm::compile
1 parent 0294473 commit 1d3ec86

2 files changed

Lines changed: 43 additions & 40 deletions

File tree

vm/src/vm/compile.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
use crate::{
2+
builtins::PyCode,
3+
compile::{self, CompileError, CompileOpts},
4+
PyPayload, PyRef, VirtualMachine,
5+
};
6+
7+
impl VirtualMachine {
8+
/// Returns a basic CompileOpts instance with options accurate to the vm. Used
9+
/// as the CompileOpts for `vm.compile()`.
10+
pub fn compile_opts(&self) -> CompileOpts {
11+
CompileOpts {
12+
optimize: self.state.settings.optimize,
13+
}
14+
}
15+
16+
pub fn compile(
17+
&self,
18+
source: &str,
19+
mode: compile::Mode,
20+
source_path: String,
21+
) -> Result<PyRef<PyCode>, CompileError> {
22+
self.compile_with_opts(source, mode, source_path, self.compile_opts())
23+
}
24+
25+
pub fn compile_with_opts(
26+
&self,
27+
source: &str,
28+
mode: compile::Mode,
29+
source_path: String,
30+
opts: CompileOpts,
31+
) -> Result<PyRef<PyCode>, CompileError> {
32+
compile::compile(source, mode, source_path, opts)
33+
.map(|code| PyCode::new(self.map_codeobj(code)).into_ref(self))
34+
}
35+
}

vm/src/vm/mod.rs

Lines changed: 8 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,14 @@
44
//! <https://github.com/ProgVal/pythonvm-rust/blob/master/src/processor/mod.rs>
55
66
#[cfg(feature = "rustpython-compiler")]
7-
use crate::compile::{self, CompileError, CompileOpts};
7+
mod compile;
8+
mod interpreter;
9+
mod setting;
10+
pub mod thread;
11+
mod vm_new;
12+
mod vm_object;
13+
mod vm_ops;
14+
815
use crate::{
916
builtins::{
1017
code::{self, PyCode},
@@ -32,16 +39,8 @@ use std::{
3239
collections::{HashMap, HashSet},
3340
};
3441

35-
mod interpreter;
36-
mod setting;
37-
pub mod thread;
38-
mod vm_new;
39-
mod vm_object;
40-
mod vm_ops;
41-
4242
pub use interpreter::Interpreter;
4343
pub use setting::PySettings;
44-
pub use thread::ThreadedVirtualMachine;
4544

4645
// Objects are live when they are on stack, or referenced by a name (for now)
4746

@@ -719,34 +718,3 @@ impl VirtualMachine {
719718
object::generic_setattr(module, attr_name.into_pystr_ref(self), Some(val), self)
720719
}
721720
}
722-
723-
#[cfg(feature = "rustpython-compiler")]
724-
impl VirtualMachine {
725-
/// Returns a basic CompileOpts instance with options accurate to the vm. Used
726-
/// as the CompileOpts for `vm.compile()`.
727-
pub fn compile_opts(&self) -> CompileOpts {
728-
CompileOpts {
729-
optimize: self.state.settings.optimize,
730-
}
731-
}
732-
733-
pub fn compile(
734-
&self,
735-
source: &str,
736-
mode: compile::Mode,
737-
source_path: String,
738-
) -> Result<PyRef<PyCode>, CompileError> {
739-
self.compile_with_opts(source, mode, source_path, self.compile_opts())
740-
}
741-
742-
pub fn compile_with_opts(
743-
&self,
744-
source: &str,
745-
mode: compile::Mode,
746-
source_path: String,
747-
opts: CompileOpts,
748-
) -> Result<PyRef<PyCode>, CompileError> {
749-
compile::compile(source, mode, source_path, opts)
750-
.map(|code| PyCode::new(self.map_codeobj(code)).into_ref(self))
751-
}
752-
}

0 commit comments

Comments
 (0)