Skip to content

Commit 428cd6a

Browse files
committed
Add frame.{f_back,f_lineno}
1 parent c703bb7 commit 428cd6a

File tree

1 file changed

+23
-6
lines changed

1 file changed

+23
-6
lines changed

vm/src/obj/objframe.rs

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use super::objcode::PyCodeRef;
66
use super::objdict::PyDictRef;
77
use crate::frame::FrameRef;
8-
use crate::pyobject::{PyClassImpl, PyContext, PyObjectRef, PyResult};
8+
use crate::pyobject::{IdProtocol, PyClassImpl, PyContext, PyResult};
99
use crate::vm::VirtualMachine;
1010

1111
pub fn init(context: &PyContext) {
@@ -45,13 +45,30 @@ impl FrameRef {
4545
}
4646

4747
#[pyproperty]
48-
fn f_back(self, vm: &VirtualMachine) -> PyObjectRef {
49-
// TODO: how to retrieve the upper stack frame??
50-
vm.ctx.none()
48+
fn f_back(self, vm: &VirtualMachine) -> Option<FrameRef> {
49+
// TODO: actually store f_back inside Frame struct
50+
51+
// get the frame in the frame stack that appears before this one.
52+
// won't work if this frame isn't in the frame stack, hence the todo above
53+
vm.frames
54+
.borrow()
55+
.iter()
56+
.rev()
57+
.skip_while(|p| !p.is(&self))
58+
.nth(1)
59+
.cloned()
60+
}
61+
62+
#[pyproperty]
63+
fn f_lasti(self) -> usize {
64+
self.lasti.get()
5165
}
5266

5367
#[pyproperty]
54-
fn f_lasti(self, vm: &VirtualMachine) -> PyObjectRef {
55-
vm.ctx.new_int(self.lasti.get())
68+
fn f_lineno(self) -> Option<usize> {
69+
self.code
70+
.locations
71+
.get(self.lasti.get())
72+
.map(|loc| loc.row())
5673
}
5774
}

0 commit comments

Comments
 (0)