forked from Rust-related/RustPython
PyObjBag refers Context instead of vm
This commit is contained in:
@@ -11,7 +11,7 @@ fn run(vm: &vm::VirtualMachine) -> vm::PyResult<()> {
|
|||||||
// https://doc.rust-lang.org/cargo/reference/environment-variables.html#environment-variables-cargo-sets-for-crates
|
// https://doc.rust-lang.org/cargo/reference/environment-variables.html#environment-variables-cargo-sets-for-crates
|
||||||
let module = vm::py_compile!(file = "examples/freeze/freeze.py");
|
let module = vm::py_compile!(file = "examples/freeze/freeze.py");
|
||||||
|
|
||||||
let res = vm.run_code_obj(vm.new_code_object(module), scope);
|
let res = vm.run_code_obj(vm.ctx.new_code(module), scope);
|
||||||
|
|
||||||
if let Err(exc) = res {
|
if let Err(exc) = res {
|
||||||
vm.print_exception(exc);
|
vm.print_exception(exc);
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ macro_rules! add_python_function {
|
|||||||
// compile the code to bytecode
|
// compile the code to bytecode
|
||||||
let code = vm::py_compile!(source = $src);
|
let code = vm::py_compile!(source = $src);
|
||||||
// convert the rustpython_bytecode::CodeObject to a PyRef<PyCode>
|
// convert the rustpython_bytecode::CodeObject to a PyRef<PyCode>
|
||||||
let code = $vm.new_code_object(code);
|
let code = $vm.ctx.new_code(code);
|
||||||
|
|
||||||
// run the python code in the scope to store the function
|
// run the python code in the scope to store the function
|
||||||
$vm.run_code_obj(code, $scope.clone())
|
$vm.run_code_obj(code, $scope.clone())
|
||||||
|
|||||||
@@ -506,7 +506,7 @@ __import__("io").TextIOWrapper(
|
|||||||
mode = "eval"
|
mode = "eval"
|
||||||
);
|
);
|
||||||
eprintln!("downloading get-pip.py...");
|
eprintln!("downloading get-pip.py...");
|
||||||
let getpip_code = vm.run_code_obj(vm.new_code_object(get_getpip), scope.clone())?;
|
let getpip_code = vm.run_code_obj(vm.ctx.new_code(get_getpip), scope.clone())?;
|
||||||
let getpip_code: rustpython_vm::builtins::PyStrRef = getpip_code
|
let getpip_code: rustpython_vm::builtins::PyStrRef = getpip_code
|
||||||
.downcast()
|
.downcast()
|
||||||
.expect("TextIOWrapper.read() should return str");
|
.expect("TextIOWrapper.read() should return str");
|
||||||
|
|||||||
@@ -71,24 +71,23 @@ impl Constant for PyConstant {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) struct PyObjBag<'a>(pub &'a VirtualMachine);
|
pub(crate) struct PyObjBag<'a>(pub &'a Context);
|
||||||
|
|
||||||
impl ConstantBag for PyObjBag<'_> {
|
impl ConstantBag for PyObjBag<'_> {
|
||||||
type Constant = PyConstant;
|
type Constant = PyConstant;
|
||||||
fn make_constant(&self, constant: bytecode::ConstantData) -> Self::Constant {
|
fn make_constant(&self, constant: bytecode::ConstantData) -> Self::Constant {
|
||||||
let vm = self.0;
|
let ctx = self.0;
|
||||||
let ctx = &vm.ctx;
|
|
||||||
let obj = match constant {
|
let obj = match constant {
|
||||||
bytecode::ConstantData::Integer { value } => ctx.new_int(value).into(),
|
bytecode::ConstantData::Integer { value } => ctx.new_int(value).into(),
|
||||||
bytecode::ConstantData::Float { value } => ctx.new_float(value).into(),
|
bytecode::ConstantData::Float { value } => ctx.new_float(value).into(),
|
||||||
bytecode::ConstantData::Complex { value } => vm.new_pyobj(value),
|
bytecode::ConstantData::Complex { value } => ctx.new_complex(value).into(),
|
||||||
bytecode::ConstantData::Str { value } if value.len() <= 20 => {
|
bytecode::ConstantData::Str { value } if value.len() <= 20 => {
|
||||||
vm.ctx.intern_string(value).into_pyref().into()
|
ctx.intern_string(value).into_pyref().into()
|
||||||
}
|
}
|
||||||
bytecode::ConstantData::Str { value } => vm.ctx.new_str(value).into(),
|
bytecode::ConstantData::Str { value } => ctx.new_str(value).into(),
|
||||||
bytecode::ConstantData::Bytes { value } => ctx.new_bytes(value.to_vec()).into(),
|
bytecode::ConstantData::Bytes { value } => ctx.new_bytes(value.to_vec()).into(),
|
||||||
bytecode::ConstantData::Boolean { value } => ctx.new_bool(value).into(),
|
bytecode::ConstantData::Boolean { value } => ctx.new_bool(value).into(),
|
||||||
bytecode::ConstantData::Code { code } => vm.new_code_object(code.map_bag(self)).into(),
|
bytecode::ConstantData::Code { code } => ctx.new_code(code.map_bag(self)).into(),
|
||||||
bytecode::ConstantData::Tuple { elements } => {
|
bytecode::ConstantData::Tuple { elements } => {
|
||||||
let elements = elements
|
let elements = elements
|
||||||
.into_iter()
|
.into_iter()
|
||||||
@@ -102,20 +101,19 @@ impl ConstantBag for PyObjBag<'_> {
|
|||||||
PyConstant(obj)
|
PyConstant(obj)
|
||||||
}
|
}
|
||||||
fn make_constant_borrowed<C: Constant>(&self, constant: BorrowedConstant<C>) -> Self::Constant {
|
fn make_constant_borrowed<C: Constant>(&self, constant: BorrowedConstant<C>) -> Self::Constant {
|
||||||
let vm = self.0;
|
let ctx = &self.0;
|
||||||
let ctx = &vm.ctx;
|
|
||||||
let obj = match constant {
|
let obj = match constant {
|
||||||
bytecode::BorrowedConstant::Integer { value } => ctx.new_bigint(value).into(),
|
bytecode::BorrowedConstant::Integer { value } => ctx.new_bigint(value).into(),
|
||||||
bytecode::BorrowedConstant::Float { value } => ctx.new_float(value).into(),
|
bytecode::BorrowedConstant::Float { value } => ctx.new_float(value).into(),
|
||||||
bytecode::BorrowedConstant::Complex { value } => vm.new_pyobj(value),
|
bytecode::BorrowedConstant::Complex { value } => ctx.new_complex(value).into(),
|
||||||
bytecode::BorrowedConstant::Str { value } if value.len() <= 20 => {
|
bytecode::BorrowedConstant::Str { value } if value.len() <= 20 => {
|
||||||
vm.ctx.intern_string(value).into_pyref().into()
|
ctx.intern_string(value).into_pyref().into()
|
||||||
}
|
}
|
||||||
bytecode::BorrowedConstant::Str { value } => vm.ctx.new_str(value).into(),
|
bytecode::BorrowedConstant::Str { value } => ctx.new_str(value).into(),
|
||||||
bytecode::BorrowedConstant::Bytes { value } => ctx.new_bytes(value.to_vec()).into(),
|
bytecode::BorrowedConstant::Bytes { value } => ctx.new_bytes(value.to_vec()).into(),
|
||||||
bytecode::BorrowedConstant::Boolean { value } => ctx.new_bool(value).into(),
|
bytecode::BorrowedConstant::Boolean { value } => ctx.new_bool(value).into(),
|
||||||
bytecode::BorrowedConstant::Code { code } => {
|
bytecode::BorrowedConstant::Code { code } => {
|
||||||
vm.new_code_object(code.map_clone_bag(self)).into()
|
ctx.new_code(code.map_clone_bag(self)).into()
|
||||||
}
|
}
|
||||||
bytecode::BorrowedConstant::Tuple { elements } => {
|
bytecode::BorrowedConstant::Tuple { elements } => {
|
||||||
let elements = elements
|
let elements = elements
|
||||||
@@ -130,10 +128,10 @@ impl ConstantBag for PyObjBag<'_> {
|
|||||||
PyConstant(obj)
|
PyConstant(obj)
|
||||||
}
|
}
|
||||||
fn make_name(&self, name: String) -> PyStrRef {
|
fn make_name(&self, name: String) -> PyStrRef {
|
||||||
self.0.ctx.intern_string(name).into_pyref()
|
self.0.intern_string(name).into_pyref()
|
||||||
}
|
}
|
||||||
fn make_name_ref(&self, name: &str) -> PyStrRef {
|
fn make_name_ref(&self, name: &str) -> PyStrRef {
|
||||||
self.0.ctx.intern_string(name).into_pyref()
|
self.0.intern_string(name).into_pyref()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -141,16 +139,17 @@ pub type CodeObject = bytecode::CodeObject<PyConstant>;
|
|||||||
pub type FrozenModule = bytecode::FrozenModule<PyConstant>;
|
pub type FrozenModule = bytecode::FrozenModule<PyConstant>;
|
||||||
|
|
||||||
pub trait IntoCodeObject {
|
pub trait IntoCodeObject {
|
||||||
fn into_codeobj(self, vm: &VirtualMachine) -> CodeObject;
|
fn into_codeobj(self, ctx: &Context) -> CodeObject;
|
||||||
}
|
}
|
||||||
impl IntoCodeObject for CodeObject {
|
impl IntoCodeObject for CodeObject {
|
||||||
fn into_codeobj(self, _vm: &VirtualMachine) -> CodeObject {
|
fn into_codeobj(self, _ctx: &Context) -> CodeObject {
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl IntoCodeObject for bytecode::CodeObject {
|
impl IntoCodeObject for bytecode::CodeObject {
|
||||||
fn into_codeobj(self, vm: &VirtualMachine) -> CodeObject {
|
fn into_codeobj(self, ctx: &Context) -> CodeObject {
|
||||||
vm.map_codeobj(self)
|
self.map_bag(&PyObjBag(ctx))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ pub fn map_frozen<'a>(
|
|||||||
(
|
(
|
||||||
k,
|
k,
|
||||||
code::FrozenModule {
|
code::FrozenModule {
|
||||||
code: vm.map_codeobj(code),
|
code: vm.ctx.new_code_object(code),
|
||||||
package,
|
package,
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -123,9 +123,9 @@ pub fn import_file(
|
|||||||
file_path: String,
|
file_path: String,
|
||||||
content: String,
|
content: String,
|
||||||
) -> PyResult {
|
) -> PyResult {
|
||||||
let code_obj = compile::compile(&content, compile::Mode::Exec, file_path, vm.compile_opts())
|
let code = compile::compile(&content, compile::Mode::Exec, file_path, vm.compile_opts())
|
||||||
.map_err(|err| vm.new_syntax_error(&err))?;
|
.map_err(|err| vm.new_syntax_error(&err))?;
|
||||||
import_codeobj(vm, module_name, vm.map_codeobj(code_obj), true)
|
import_codeobj(vm, module_name, vm.ctx.new_code_object(code), true)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn import_codeobj(
|
pub fn import_codeobj(
|
||||||
|
|||||||
@@ -276,7 +276,7 @@ pub(crate) fn compile(
|
|||||||
let code = rustpython_compiler_core::compile::compile_top(&ast, filename.to_owned(), opts)
|
let code = rustpython_compiler_core::compile::compile_top(&ast, filename.to_owned(), opts)
|
||||||
// TODO: use vm.new_syntax_error()
|
// TODO: use vm.new_syntax_error()
|
||||||
.map_err(|err| vm.new_value_error(err.to_string()))?;
|
.map_err(|err| vm.new_value_error(err.to_string()))?;
|
||||||
Ok(vm.new_code_object(code).into())
|
Ok(vm.ctx.new_code(code).into())
|
||||||
}
|
}
|
||||||
|
|
||||||
// Required crate visibility for inclusion by gen.rs
|
// Required crate visibility for inclusion by gen.rs
|
||||||
|
|||||||
@@ -309,10 +309,7 @@ mod decl {
|
|||||||
),
|
),
|
||||||
_ => vm.new_value_error("Couldn't deserialize python bytecode".to_owned()),
|
_ => vm.new_value_error("Couldn't deserialize python bytecode".to_owned()),
|
||||||
})?;
|
})?;
|
||||||
Ok(PyCode {
|
Ok(vm.ctx.new_code(code).into())
|
||||||
code: vm.map_codeobj(code),
|
|
||||||
}
|
|
||||||
.to_pyobject(vm))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -30,6 +30,6 @@ impl VirtualMachine {
|
|||||||
opts: CompileOpts,
|
opts: CompileOpts,
|
||||||
) -> Result<PyRef<PyCode>, CompileError> {
|
) -> Result<PyRef<PyCode>, CompileError> {
|
||||||
compile::compile(source, mode, source_path, opts)
|
compile::compile(source, mode, source_path, opts)
|
||||||
.map(|code| PyCode::new(self.map_codeobj(code)).into_ref(self))
|
.map(|code| PyCode::new(self.ctx.new_code_object(code)).into_ref(self))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,11 +2,13 @@ use crate::{
|
|||||||
builtins::{
|
builtins::{
|
||||||
builtinfunc::{PyBuiltinFunction, PyBuiltinMethod, PyNativeFuncDef},
|
builtinfunc::{PyBuiltinFunction, PyBuiltinMethod, PyNativeFuncDef},
|
||||||
bytes,
|
bytes,
|
||||||
|
code::{self, PyCode},
|
||||||
getset::{IntoPyGetterFunc, IntoPySetterFunc, PyGetSet},
|
getset::{IntoPyGetterFunc, IntoPySetterFunc, PyGetSet},
|
||||||
object, pystr,
|
object, pystr,
|
||||||
type_::PyAttributes,
|
type_::PyAttributes,
|
||||||
PyBaseException, PyDict, PyDictRef, PyEllipsis, PyFloat, PyFrozenSet, PyInt, PyIntRef,
|
PyBaseException, PyComplex, PyDict, PyDictRef, PyEllipsis, PyFloat, PyFrozenSet, PyInt,
|
||||||
PyList, PyListRef, PyNone, PyNotImplemented, PyStr, PyTuple, PyTupleRef, PyType, PyTypeRef,
|
PyIntRef, PyList, PyListRef, PyNone, PyNotImplemented, PyStr, PyTuple, PyTupleRef, PyType,
|
||||||
|
PyTypeRef,
|
||||||
},
|
},
|
||||||
class::{PyClassImpl, StaticType},
|
class::{PyClassImpl, StaticType},
|
||||||
exceptions,
|
exceptions,
|
||||||
@@ -16,6 +18,7 @@ use crate::{
|
|||||||
types::{PyTypeFlags, PyTypeSlots, TypeZoo},
|
types::{PyTypeFlags, PyTypeSlots, TypeZoo},
|
||||||
};
|
};
|
||||||
use num_bigint::BigInt;
|
use num_bigint::BigInt;
|
||||||
|
use num_complex::Complex64;
|
||||||
use num_traits::ToPrimitive;
|
use num_traits::ToPrimitive;
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
@@ -159,6 +162,15 @@ impl Context {
|
|||||||
PyRef::new_ref(PyFloat::from(value), self.types.float_type.clone(), None)
|
PyRef::new_ref(PyFloat::from(value), self.types.float_type.clone(), None)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
pub fn new_complex(&self, value: Complex64) -> PyRef<PyComplex> {
|
||||||
|
PyRef::new_ref(
|
||||||
|
PyComplex::from(value),
|
||||||
|
self.types.complex_type.clone(),
|
||||||
|
None,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn new_str(&self, s: impl Into<pystr::PyStr>) -> PyRef<PyStr> {
|
pub fn new_str(&self, s: impl Into<pystr::PyStr>) -> PyRef<PyStr> {
|
||||||
pystr::PyStr::new_ref(s, self)
|
pystr::PyStr::new_ref(s, self)
|
||||||
@@ -308,6 +320,14 @@ impl Context {
|
|||||||
);
|
);
|
||||||
PyRef::new_ref(object::PyBaseObject, class, dict).into()
|
PyRef::new_ref(object::PyBaseObject, class, dict).into()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn new_code(&self, code: impl code::IntoCodeObject) -> PyRef<PyCode> {
|
||||||
|
PyCode::new_ref(code.into_codeobj(self), self)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn new_code_object(&self, code: crate::bytecode::CodeObject) -> code::CodeObject {
|
||||||
|
code.map_bag(&code::PyObjBag(self))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for Context {
|
impl Default for Context {
|
||||||
|
|||||||
@@ -685,10 +685,6 @@ impl VirtualMachine {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn map_codeobj(&self, code: bytecode::CodeObject) -> code::CodeObject {
|
|
||||||
code.map_bag(&code::PyObjBag(self))
|
|
||||||
}
|
|
||||||
|
|
||||||
#[doc(hidden)]
|
#[doc(hidden)]
|
||||||
pub fn __module_set_attr(
|
pub fn __module_set_attr(
|
||||||
&self,
|
&self,
|
||||||
|
|||||||
@@ -2,7 +2,6 @@
|
|||||||
use crate::compile::{CompileError, CompileErrorType};
|
use crate::compile::{CompileError, CompileErrorType};
|
||||||
use crate::{
|
use crate::{
|
||||||
builtins::{
|
builtins::{
|
||||||
code::{self, PyCode},
|
|
||||||
pystr::IntoPyStrRef,
|
pystr::IntoPyStrRef,
|
||||||
tuple::{IntoPyTuple, PyTupleRef},
|
tuple::{IntoPyTuple, PyTupleRef},
|
||||||
PyBaseException, PyBaseExceptionRef, PyDictRef, PyModule, PyStrRef, PyTypeRef,
|
PyBaseException, PyBaseExceptionRef, PyDictRef, PyModule, PyStrRef, PyTypeRef,
|
||||||
@@ -32,10 +31,6 @@ impl VirtualMachine {
|
|||||||
value.into_pytuple(self)
|
value.into_pytuple(self)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn new_code_object(&self, code: impl code::IntoCodeObject) -> PyRef<PyCode> {
|
|
||||||
PyCode::new_ref(code.into_codeobj(self), &self.ctx)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn new_module(&self, name: &str, dict: PyDictRef, doc: Option<&str>) -> PyObjectRef {
|
pub fn new_module(&self, name: &str, dict: PyDictRef, doc: Option<&str>) -> PyObjectRef {
|
||||||
let module = PyRef::new_ref(PyModule {}, self.ctx.types.module_type.clone(), Some(dict));
|
let module = PyRef::new_ref(PyModule {}, self.ctx.types.module_type.clone(), Some(dict));
|
||||||
module.init_module_dict(
|
module.init_module_dict(
|
||||||
|
|||||||
Reference in New Issue
Block a user