forked from RustPython/RustPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobjcode.rs
More file actions
103 lines (88 loc) · 3.13 KB
/
objcode.rs
File metadata and controls
103 lines (88 loc) · 3.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/*! Infamous code object. The python class `code`
*/
use crate::bytecode;
use crate::pyobject::{
IdProtocol, PyContext, PyFuncArgs, PyObjectPayload, PyObjectRef, PyResult, TypeProtocol,
};
use crate::vm::VirtualMachine;
pub fn init(context: &PyContext) {
let code_type = &context.code_type;
context.set_attr(code_type, "__new__", context.new_rustfunc(code_new));
context.set_attr(code_type, "__repr__", context.new_rustfunc(code_repr));
for (name, f) in &[
(
"co_argcount",
code_co_argcount as fn(&mut VirtualMachine, PyFuncArgs) -> PyResult,
),
("co_consts", code_co_consts),
("co_filename", code_co_filename),
("co_firstlineno", code_co_firstlineno),
("co_kwonlyargcount", code_co_kwonlyargcount),
("co_name", code_co_name),
] {
context.set_attr(code_type, name, context.new_member_descriptor(f))
}
}
pub fn get_value(obj: &PyObjectRef) -> bytecode::CodeObject {
if let PyObjectPayload::Code { code } = &obj.payload {
code.clone()
} else {
panic!("Inner error getting code {:?}", obj)
}
}
fn code_new(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
arg_check!(vm, args, required = [(_cls, None)]);
Err(vm.new_type_error("Cannot directly create code object".to_string()))
}
fn code_repr(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
arg_check!(vm, args, required = [(o, Some(vm.ctx.code_type()))]);
let code = get_value(o);
let repr = format!(
"<code object {} at 0x{:x} file {:?}, line {}>",
code.obj_name,
o.get_id(),
code.source_path,
code.first_line_number
);
Ok(vm.new_str(repr))
}
fn member_code_obj(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult<bytecode::CodeObject> {
arg_check!(
vm,
args,
required = [
(zelf, Some(vm.ctx.code_type())),
(_cls, Some(vm.ctx.type_type()))
]
);
Ok(get_value(zelf))
}
fn code_co_argcount(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
let code_obj = member_code_obj(vm, args)?;
Ok(vm.ctx.new_int(code_obj.arg_names.len()))
}
fn code_co_filename(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
let code_obj = member_code_obj(vm, args)?;
let source_path = code_obj.source_path;
Ok(vm.new_str(source_path))
}
fn code_co_firstlineno(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
let code_obj = member_code_obj(vm, args)?;
Ok(vm.ctx.new_int(code_obj.first_line_number))
}
fn code_co_kwonlyargcount(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
let code_obj = member_code_obj(vm, args)?;
Ok(vm.ctx.new_int(code_obj.kwonlyarg_names.len()))
}
fn code_co_consts(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
let code_obj = member_code_obj(vm, args)?;
let consts = code_obj
.get_constants()
.map(|x| vm.ctx.unwrap_constant(x))
.collect();
Ok(vm.ctx.new_tuple(consts))
}
fn code_co_name(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
let code_obj = member_code_obj(vm, args)?;
Ok(vm.new_str(code_obj.obj_name))
}