Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit4acea45

Browse files
committed
Add code and frame python objects.
1 parentdf0400d commit4acea45

File tree

9 files changed

+139
-42
lines changed

9 files changed

+139
-42
lines changed

‎vm/src/compile.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ pub fn compile(
5454
trace!("Compilation completed: {:?}", code);
5555
Ok(PyObject::new(
5656
PyObjectKind::Code{code: code},
57-
vm.get_type(),
57+
vm.ctx.code_type(),
5858
))
5959
}
6060

‎vm/src/frame.rs‎

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use super::builtins;
99
usesuper::bytecode;
1010
usesuper::import::import;
1111
usesuper::obj::objbool;
12+
usesuper::obj::objcode;
1213
usesuper::obj::objdict;
1314
usesuper::obj::objiter;
1415
usesuper::obj::objlist;
@@ -46,15 +47,6 @@ pub struct Frame {
4647
publasti:usize,// index of last instruction ran
4748
}
4849

49-
pubfncopy_code(code_obj:PyObjectRef) -> bytecode::CodeObject{
50-
let code_obj = code_obj.borrow();
51-
ifletPyObjectKind::Code{ref code} = code_obj.kind{
52-
code.clone()
53-
}else{
54-
panic!("Must be code obj");
55-
}
56-
}
57-
5850
// Running a frame can result in one of the below:
5951
pubenumExecutionResult{
6052
Return(PyObjectRef),
@@ -78,7 +70,7 @@ impl Frame {
7870
// locals.extend(callargs);
7971

8072
Frame{
81-
code:copy_code(code),
73+
code:objcode::copy_code(&code),
8274
stack:vec![],
8375
blocks:vec![],
8476
// save the callargs as locals
@@ -302,8 +294,13 @@ impl Frame {
302294
bytecode::Instruction::ListAppend{ i} =>{
303295
let list_obj =self.nth_value(*i);
304296
let item =self.pop_value();
305-
// TODO: objlist::list_append()
306-
vm.call_method(&list_obj,"append",vec![item])?;
297+
objlist::list_append(
298+
vm,
299+
PyFuncArgs{
300+
args:vec![list_obj.clone(), item],
301+
kwargs:vec![],
302+
},
303+
)?;
307304
Ok(None)
308305
}
309306
bytecode::Instruction::SetAdd{ i} =>{

‎vm/src/obj/mod.rs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
pubmod objbool;
44
pubmod objbytearray;
55
pubmod objbytes;
6+
pubmod objcode;
67
pubmod objcomplex;
78
pubmod objdict;
89
pubmod objfloat;
10+
pubmod objframe;
911
pubmod objfunction;
1012
pubmod objgenerator;
1113
pubmod objint;

‎vm/src/obj/objcode.rs‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*! Infamous code object. The python class `code`
2+
3+
*/
4+
5+
usesuper::super::bytecode;
6+
usesuper::super::pyobject::{
7+
AttributeProtocol,PyContext,PyFuncArgs,PyObjectKind,PyObjectRef,PyResult,TypeProtocol,
8+
};
9+
usesuper::super::vm::VirtualMachine;
10+
usesuper::objtype;
11+
12+
pubfninit(context:&PyContext){
13+
letref code_type = context.code_type;
14+
code_type.set_attr("__repr__", context.new_rustfunc(code_repr));
15+
}
16+
17+
/// Extract rust bytecode object from a python code object.
18+
pubfncopy_code(code_obj:&PyObjectRef) -> bytecode::CodeObject{
19+
let code_obj = code_obj.borrow();
20+
ifletPyObjectKind::Code{ref code} = code_obj.kind{
21+
code.clone()
22+
}else{
23+
panic!("Must be code obj");
24+
}
25+
}
26+
27+
fncode_repr(vm:&mutVirtualMachine,args:PyFuncArgs) ->PyResult{
28+
arg_check!(vm, args, required =[(o,Some(vm.ctx.code_type()))]);
29+
30+
// Fetch actual code:
31+
let code =copy_code(o);
32+
33+
let file =ifletSome(source_path) = code.source_path{
34+
format!(", file {}", source_path)
35+
}else{
36+
String::new()
37+
};
38+
39+
// TODO: fetch proper line info from code object
40+
let line =format!(", line 1");
41+
42+
let repr =format!("<code object at .. {}{}>", file, line);
43+
Ok(vm.new_str(repr))
44+
}

‎vm/src/obj/objframe.rs‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*! The python `frame` type.
2+
3+
*/
4+
5+
usesuper::super::pyobject::{
6+
AttributeProtocol,PyContext,PyFuncArgs,PyObjectRef,PyResult,TypeProtocol,
7+
};
8+
usesuper::super::vm::VirtualMachine;
9+
usesuper::objtype;
10+
11+
pubfninit(context:&PyContext){
12+
letref frame_type = context.frame_type;
13+
frame_type.set_attr("__repr__", context.new_rustfunc(frame_repr));
14+
}
15+
16+
fnframe_repr(vm:&mutVirtualMachine,args:PyFuncArgs) ->PyResult{
17+
arg_check!(vm, args, required =[(_frame,Some(vm.ctx.frame_type()))]);
18+
let repr =format!("<frame object at .. >");
19+
Ok(vm.new_str(repr))
20+
}

‎vm/src/obj/objsuper.rs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ fn super_init(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
3232
}else{
3333
// TODO: implement complex logic here....
3434
unimplemented!("TODO: get frame and determine instance and class?");
35+
// let frame = vm.get_current_frame();
36+
//
3537
// vm.get_none()
3638
};
3739

‎vm/src/pyobject.rs‎

Lines changed: 56 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@ use super::frame::Frame;
44
usesuper::obj::objbool;
55
usesuper::obj::objbytearray;
66
usesuper::obj::objbytes;
7+
usesuper::obj::objcode;
78
usesuper::obj::objcomplex;
89
usesuper::obj::objdict;
910
usesuper::obj::objfloat;
11+
usesuper::obj::objframe;
1012
usesuper::obj::objfunction;
1113
usesuper::obj::objgenerator;
1214
usesuper::obj::objint;
@@ -70,31 +72,44 @@ impl fmt::Display for PyObjectRef {
7072
}
7173
}*/
7274

75+
/*
76+
// Idea: implement the iterator trait upon PyObjectRef
77+
impl Iterator for (VirtualMachine, PyObjectRef) {
78+
type Item = char;
79+
80+
fn next(&mut self) -> Option<Self::Item> {
81+
// call method ("_next__")
82+
}
83+
}
84+
*/
85+
7386
#[derive(Debug)]
7487
pubstructPyContext{
75-
pubtype_type:PyObjectRef,
76-
pubnone:PyObjectRef,
88+
pubbytes_type:PyObjectRef,
89+
pubbytearray_type:PyObjectRef,
90+
pubbool_type:PyObjectRef,
7791
pubclassmethod_type:PyObjectRef,
78-
pubstaticmethod_type:PyObjectRef,
92+
pubcode_type:PyObjectRef,
7993
pubdict_type:PyObjectRef,
80-
pubint_type:PyObjectRef,
8194
pubfloat_type:PyObjectRef,
95+
pubframe_type:PyObjectRef,
96+
pubfrozenset_type:PyObjectRef,
97+
pubgenerator_type:PyObjectRef,
98+
pubint_type:PyObjectRef,
99+
pubiter_type:PyObjectRef,
82100
pubcomplex_type:PyObjectRef,
83-
pubbytes_type:PyObjectRef,
84-
pubbytearray_type:PyObjectRef,
85-
pubbool_type:PyObjectRef,
86101
pubtrue_value:PyObjectRef,
87102
pubfalse_value:PyObjectRef,
88103
publist_type:PyObjectRef,
104+
pubnone:PyObjectRef,
89105
pubtuple_type:PyObjectRef,
90106
pubset_type:PyObjectRef,
91-
pubfrozenset_type:PyObjectRef,
92-
pubiter_type:PyObjectRef,
107+
pubstaticmethod_type:PyObjectRef,
93108
pubsuper_type:PyObjectRef,
94109
pubstr_type:PyObjectRef,
110+
pubtype_type:PyObjectRef,
95111
pubfunction_type:PyObjectRef,
96112
pubproperty_type:PyObjectRef,
97-
pubgenerator_type:PyObjectRef,
98113
pubmodule_type:PyObjectRef,
99114
pubbound_method_type:PyObjectRef,
100115
pubmember_descriptor_type:PyObjectRef,
@@ -162,12 +177,14 @@ impl PyContext {
162177
let frozenset_type =create_type("frozenset",&type_type,&object_type,&dict_type);
163178
let int_type =create_type("int",&type_type,&object_type,&dict_type);
164179
let float_type =create_type("float",&type_type,&object_type,&dict_type);
180+
let frame_type =create_type("frame",&type_type,&object_type,&dict_type);
165181
let complex_type =create_type("complex",&type_type,&object_type,&dict_type);
166182
let bytes_type =create_type("bytes",&type_type,&object_type,&dict_type);
167183
let bytearray_type =create_type("bytearray",&type_type,&object_type,&dict_type);
168184
let tuple_type =create_type("tuple",&type_type,&object_type,&dict_type);
169185
let iter_type =create_type("iter",&type_type,&object_type,&dict_type);
170186
let bool_type =create_type("bool",&type_type,&int_type,&dict_type);
187+
let code_type =create_type("code",&type_type,&int_type,&dict_type);
171188
let exceptions = exceptions::ExceptionZoo::new(&type_type,&object_type,&dict_type);
172189

173190
let none =PyObject::new(
@@ -186,17 +203,19 @@ impl PyContext {
186203
bool_type.clone(),
187204
);
188205
let context =PyContext{
189-
int_type: int_type,
190-
float_type: float_type,
206+
bool_type: bool_type,
207+
bytearray_type: bytearray_type,
208+
bytes_type: bytes_type,
209+
code_type: code_type,
191210
complex_type: complex_type,
192211
classmethod_type: classmethod_type,
212+
int_type: int_type,
213+
float_type: float_type,
214+
frame_type: frame_type,
193215
staticmethod_type: staticmethod_type,
194-
bytes_type: bytes_type,
195-
bytearray_type: bytearray_type,
196216
list_type: list_type,
197217
set_type: set_type,
198218
frozenset_type: frozenset_type,
199-
bool_type: bool_type,
200219
true_value: true_value,
201220
false_value: false_value,
202221
tuple_type: tuple_type,
@@ -234,28 +253,42 @@ impl PyContext {
234253
objtuple::init(&context);
235254
objiter::init(&context);
236255
objbool::init(&context);
256+
objcode::init(&context);
257+
objframe::init(&context);
237258
exceptions::init(&context);
238259
context
239260
}
240261

241-
pubfnint_type(&self) ->PyObjectRef{
242-
self.int_type.clone()
262+
pubfnbytearray_type(&self) ->PyObjectRef{
263+
self.bytearray_type.clone()
243264
}
244265

245-
pubfnfloat_type(&self) ->PyObjectRef{
246-
self.float_type.clone()
266+
pubfnbytes_type(&self) ->PyObjectRef{
267+
self.bytes_type.clone()
268+
}
269+
270+
pubfncode_type(&self) ->PyObjectRef{
271+
self.code_type.clone()
247272
}
248273

249274
pubfncomplex_type(&self) ->PyObjectRef{
250275
self.complex_type.clone()
251276
}
252277

253-
pubfnbytes_type(&self) ->PyObjectRef{
254-
self.bytes_type.clone()
278+
pubfndict_type(&self) ->PyObjectRef{
279+
self.dict_type.clone()
255280
}
256281

257-
pubfnbytearray_type(&self) ->PyObjectRef{
258-
self.bytearray_type.clone()
282+
pubfnfloat_type(&self) ->PyObjectRef{
283+
self.float_type.clone()
284+
}
285+
286+
pubfnframe_type(&self) ->PyObjectRef{
287+
self.frame_type.clone()
288+
}
289+
290+
pubfnint_type(&self) ->PyObjectRef{
291+
self.int_type.clone()
259292
}
260293

261294
pubfnlist_type(&self) ->PyObjectRef{
@@ -282,10 +315,6 @@ impl PyContext {
282315
self.iter_type.clone()
283316
}
284317

285-
pubfndict_type(&self) ->PyObjectRef{
286-
self.dict_type.clone()
287-
}
288-
289318
pubfnstr_type(&self) ->PyObjectRef{
290319
self.str_type.clone()
291320
}

‎vm/src/stdlib/types.rs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ pub fn mk_module(ctx: &PyContext) -> PyObjectRef {
4040
py_mod.set_item("new_class", ctx.new_rustfunc(types_new_class));
4141
py_mod.set_item("FunctionType", ctx.function_type());
4242
py_mod.set_item("LambdaType", ctx.function_type());
43+
py_mod.set_item("CodeType", ctx.code_type());
44+
py_mod.set_item("FrameType", ctx.frame_type());
4345

4446
py_mod
4547
}

‎vm/src/vm.rs‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ use std::collections::hash_map::HashMap;
1010

1111
usesuper::builtins;
1212
usesuper::bytecode;
13-
usesuper::frame::{copy_code,Frame};
13+
usesuper::frame::Frame;
14+
usesuper::obj::objcode::copy_code;
1415
usesuper::obj::objgenerator;
1516
usesuper::obj::objiter;
1617
usesuper::obj::objsequence;
@@ -252,7 +253,7 @@ impl VirtualMachine {
252253
defaults:&PyObjectRef,
253254
args:PyFuncArgs,
254255
) ->PyResult{
255-
let code_object =copy_code(code.clone());
256+
let code_object =copy_code(code);
256257
let scope =self.ctx.new_scope(Some(scope.clone()));
257258
self.fill_scope_from_args(&code_object,&scope, args, defaults)?;
258259

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp