Skip to content

Commit 231ab68

Browse files
committed
Convert CodeObject payload to AnyRustValue
1 parent 3495b04 commit 231ab68

File tree

3 files changed

+39
-9
lines changed

3 files changed

+39
-9
lines changed

vm/src/compile.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
88
use crate::bytecode::{self, CallType, CodeObject, Instruction};
99
use crate::error::CompileError;
10+
use crate::obj::objcode;
1011
use crate::pyobject::{PyObject, PyObjectPayload, PyObjectRef};
1112
use num_complex::Complex64;
1213
use rustpython_parser::{ast, parser};
@@ -48,7 +49,12 @@ pub fn compile(
4849

4950
let code = compiler.pop_code_object();
5051
trace!("Compilation completed: {:?}", code);
51-
Ok(PyObject::new(PyObjectPayload::Code { code }, code_type))
52+
Ok(PyObject::new(
53+
PyObjectPayload::AnyRustValue {
54+
value: Box::new(objcode::PyCode::new(code)),
55+
},
56+
code_type,
57+
))
5258
}
5359

5460
pub enum Mode {

vm/src/obj/objcode.rs

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,32 @@
44

55
use crate::bytecode;
66
use crate::pyobject::{
7-
IdProtocol, PyContext, PyFuncArgs, PyObjectPayload, PyObjectRef, PyResult, TypeProtocol,
7+
IdProtocol, PyContext, PyFuncArgs, PyObjectPayload2, PyObjectRef, PyResult, TypeProtocol,
88
};
99
use crate::vm::VirtualMachine;
10+
use std::fmt;
11+
12+
pub struct PyCode {
13+
code: bytecode::CodeObject,
14+
}
15+
16+
impl PyCode {
17+
pub fn new(code: bytecode::CodeObject) -> PyCode {
18+
PyCode { code }
19+
}
20+
}
21+
22+
impl fmt::Debug for PyCode {
23+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
24+
write!(f, "code: {:?}", self.code)
25+
}
26+
}
27+
28+
impl PyObjectPayload2 for PyCode {
29+
fn required_type(ctx: &PyContext) -> PyObjectRef {
30+
ctx.code_type()
31+
}
32+
}
1033

1134
pub fn init(context: &PyContext) {
1235
let code_type = &context.code_type;
@@ -29,8 +52,8 @@ pub fn init(context: &PyContext) {
2952
}
3053

3154
pub fn get_value(obj: &PyObjectRef) -> bytecode::CodeObject {
32-
if let PyObjectPayload::Code { code } = &obj.payload {
33-
code.clone()
55+
if let Some(code) = obj.payload::<PyCode>() {
56+
code.code.clone()
3457
} else {
3558
panic!("Inner error getting code {:?}", obj)
3659
}

vm/src/pyobject.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -636,7 +636,12 @@ impl PyContext {
636636
}
637637

638638
pub fn new_code_object(&self, code: bytecode::CodeObject) -> PyObjectRef {
639-
PyObject::new(PyObjectPayload::Code { code }, self.code_type())
639+
PyObject::new(
640+
PyObjectPayload::AnyRustValue {
641+
value: Box::new(objcode::PyCode::new(code)),
642+
},
643+
self.code_type(),
644+
)
640645
}
641646

642647
pub fn new_function(
@@ -1497,9 +1502,6 @@ pub enum PyObjectPayload {
14971502
MemoryView {
14981503
obj: PyObjectRef,
14991504
},
1500-
Code {
1501-
code: bytecode::CodeObject,
1502-
},
15031505
Frame {
15041506
frame: Frame,
15051507
},
@@ -1550,7 +1552,6 @@ impl fmt::Debug for PyObjectPayload {
15501552
PyObjectPayload::MapIterator { .. } => write!(f, "map"),
15511553
PyObjectPayload::ZipIterator { .. } => write!(f, "zip"),
15521554
PyObjectPayload::Slice { .. } => write!(f, "slice"),
1553-
PyObjectPayload::Code { ref code } => write!(f, "code: {:?}", code),
15541555
PyObjectPayload::Function { .. } => write!(f, "function"),
15551556
PyObjectPayload::Generator { .. } => write!(f, "generator"),
15561557
PyObjectPayload::BoundMethod {

0 commit comments

Comments
 (0)