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

Commitc5b6b61

Browse files
committed
Construct PyObjectRef earlier to avoid the need for Clone on Frame.
1 parentee86229 commitc5b6b61

File tree

6 files changed

+42
-31
lines changed

6 files changed

+42
-31
lines changed

‎vm/src/frame.rs‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ enum Block {
3838
},
3939
}
4040

41-
#[derive(Clone)]
4241
pubstructFrame{
4342
pubcode: bytecode::CodeObject,
4443
// We need 1 stack per frame

‎vm/src/obj/objframe.rs‎

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
33
*/
44

5-
usesuper::super::frame::Frame;
65
usesuper::super::pyobject::{
76
AttributeProtocol,PyContext,PyFuncArgs,PyObjectKind,PyObjectRef,PyResult,TypeProtocol,
87
};
@@ -30,26 +29,25 @@ fn frame_repr(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
3029

3130
fnframe_flocals(vm:&mutVirtualMachine,args:PyFuncArgs) ->PyResult{
3231
arg_check!(vm, args, required =[(frame,Some(vm.ctx.frame_type()))]);
33-
let frame =get_value(frame);
34-
let py_scope = frame.locals.clone();
35-
let py_scope = py_scope.borrow();
36-
37-
ifletPyObjectKind::Scope{ scope} =&py_scope.kind{
38-
Ok(scope.locals.clone())
32+
ifletPyObjectKind::Frame{ref frame} = frame.borrow().kind{
33+
let py_scope = frame.locals.clone();
34+
let py_scope = py_scope.borrow();
35+
36+
ifletPyObjectKind::Scope{ scope} =&py_scope.kind{
37+
Ok(scope.locals.clone())
38+
}else{
39+
panic!("The scope isn't a scope!");
40+
}
3941
}else{
40-
panic!("The scope isn'ta scope!");
42+
panic!("Frame doesn'tcontain a frame: {:?}", frame);
4143
}
4244
}
4345

4446
fnframe_fcode(vm:&mutVirtualMachine,args:PyFuncArgs) ->PyResult{
4547
arg_check!(vm, args, required =[(frame,Some(vm.ctx.frame_type()))]);
46-
Ok(vm.ctx.new_code_object(get_value(frame).code))
47-
}
48-
49-
pubfnget_value(obj:&PyObjectRef) ->Frame{
50-
ifletPyObjectKind::Frame{ frame} =&obj.borrow().kind{
51-
frame.clone()
48+
ifletPyObjectKind::Frame{ref frame} = frame.borrow().kind{
49+
Ok(vm.ctx.new_code_object(frame.code.clone()))
5250
}else{
53-
panic!("Inner error getting int{:?}",obj);
51+
panic!("Frame doesn't contain a frame:{:?}",frame);
5452
}
5553
}

‎vm/src/obj/objgenerator.rs‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* The mythical generator.
33
*/
44

5-
usesuper::super::frame::{ExecutionResult,Frame};
5+
usesuper::super::frame::ExecutionResult;
66
usesuper::super::pyobject::{
77
AttributeProtocol,PyContext,PyFuncArgs,PyObject,PyObjectKind,PyObjectRef,PyResult,
88
TypeProtocol,
@@ -17,7 +17,7 @@ pub fn init(context: &PyContext) {
1717
generator_type.set_attr("send", context.new_rustfunc(generator_send));
1818
}
1919

20-
pubfnnew_generator(vm:&mutVirtualMachine,frame:Frame) ->PyResult{
20+
pubfnnew_generator(vm:&mutVirtualMachine,frame:PyObjectRef) ->PyResult{
2121
let g =PyObject::new(
2222
PyObjectKind::Generator{frame: frame},
2323
vm.ctx.generator_type.clone(),
@@ -47,7 +47,12 @@ fn generator_send(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
4747

4848
fnsend(vm:&mutVirtualMachine,gen:&PyObjectRef,value:&PyObjectRef) ->PyResult{
4949
ifletPyObjectKind::Generator{refmut frame} = gen.borrow_mut().kind{
50-
frame.push_value(value.clone());
50+
ifletPyObjectKind::Frame{refmut frame} = frame.borrow_mut().kind{
51+
frame.push_value(value.clone());
52+
}else{
53+
panic!("Generator frame isn't a frame.");
54+
}
55+
5156
match vm.run_frame(frame.clone())?{
5257
ExecutionResult::Yield(value) =>Ok(value),
5358
ExecutionResult::Return(_value) =>{

‎vm/src/pyobject.rs‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -459,8 +459,13 @@ impl PyContext {
459459
)
460460
}
461461

462-
pubfnnew_frame(&self,frame:Frame) ->PyObjectRef{
463-
PyObject::new(PyObjectKind::Frame{frame: frame},self.frame_type())
462+
pubfnnew_frame(&self,code:PyObjectRef,scope:PyObjectRef) ->PyObjectRef{
463+
PyObject::new(
464+
PyObjectKind::Frame{
465+
frame:Frame::new(code, scope),
466+
},
467+
self.frame_type(),
468+
)
464469
}
465470

466471
pubfnnew_property(&self,function:RustPyFunc) ->PyObjectRef{
@@ -811,7 +816,7 @@ pub enum PyObjectKind {
811816
defaults:PyObjectRef,
812817
},
813818
Generator{
814-
frame:Frame,
819+
frame:PyObjectRef,
815820
},
816821
BoundMethod{
817822
function:PyObjectRef,

‎vm/src/sysmodule.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use super::vm::VirtualMachine;
55
use num_bigint::ToBigInt;
66
use num_traits::ToPrimitive;
77
use std::env;
8+
use std::mem;
89
use std::rc::Rc;
910

1011
/*

‎vm/src/vm.rs‎

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

1111
usesuper::builtins;
1212
usesuper::bytecode;
13-
usesuper::frame::{ExecutionResult,Frame};
13+
usesuper::frame::ExecutionResult;
1414
usesuper::obj::objcode::copy_code;
15-
usesuper::obj::objframe;
1615
usesuper::obj::objgenerator;
1716
usesuper::obj::objiter;
1817
usesuper::obj::objsequence;
@@ -58,21 +57,25 @@ impl VirtualMachine {
5857
}
5958

6059
pubfnrun_code_obj(&mutself,code:PyObjectRef,scope:PyObjectRef) ->PyResult{
61-
self.run_frame_full(Frame::new(code, scope))
60+
let frame =self.ctx.new_frame(code, scope);
61+
self.run_frame_full(frame)
6262
}
6363

64-
pubfnrun_frame_full(&mutself,frame:Frame) ->PyResult{
64+
pubfnrun_frame_full(&mutself,frame:PyObjectRef) ->PyResult{
6565
matchself.run_frame(frame)?{
6666
ExecutionResult::Return(value) =>Ok(value),
6767
_ =>panic!("Got unexpected result from function"),
6868
}
6969
}
7070

71-
pubfnrun_frame(&mutself,frame:Frame) ->Result<ExecutionResult,PyObjectRef>{
72-
letframe =self.ctx.new_frame(frame);
71+
pubfnrun_frame(&mutself,frame:PyObjectRef) ->Result<ExecutionResult,PyObjectRef>{
72+
letresult;
7373
self.frames.push(frame.clone());
74-
letmut frame = objframe::get_value(&frame);
75-
let result = frame.run(self);
74+
ifletPyObjectKind::Frame{refmut frame} = frame.borrow_mut().kind{
75+
result = frame.run(self);
76+
}else{
77+
panic!("Frame doesn't contain a frame: {:?}", frame);
78+
}
7679
self.frames.pop();
7780
result
7881
}
@@ -276,7 +279,7 @@ impl VirtualMachine {
276279
self.fill_scope_from_args(&code_object,&scope, args, defaults)?;
277280

278281
// Construct frame:
279-
let frame =Frame::new(code.clone(), scope);
282+
let frame =self.ctx.new_frame(code.clone(), scope);
280283

281284
// If we have a generator, create a new generator
282285
if code_object.is_generator{

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp