From 7d08867419cb8ed5d7dad5a15ad288fee15c6e00 Mon Sep 17 00:00:00 2001 From: Adam Kelly Date: Fri, 8 Feb 2019 17:59:27 +0000 Subject: [PATCH] Record first line number in code object. --- vm/src/bytecode.rs | 3 +++ vm/src/compile.rs | 12 ++++++++++++ 2 files changed, 15 insertions(+) diff --git a/vm/src/bytecode.rs b/vm/src/bytecode.rs index 77b12917a..55c339c95 100644 --- a/vm/src/bytecode.rs +++ b/vm/src/bytecode.rs @@ -23,6 +23,7 @@ pub struct CodeObject { pub kwonlyarg_names: Vec, pub varkeywords: Option>, // **kwargs or ** pub source_path: Option, + pub first_line_number: usize, pub obj_name: String, // Name of the object that created this code object pub is_generator: bool, } @@ -34,6 +35,7 @@ impl CodeObject { kwonlyarg_names: Vec, varkeywords: Option>, source_path: Option, + first_line_number: usize, obj_name: String, ) -> CodeObject { CodeObject { @@ -45,6 +47,7 @@ impl CodeObject { kwonlyarg_names, varkeywords, source_path, + first_line_number, obj_name, is_generator: false, } diff --git a/vm/src/compile.rs b/vm/src/compile.rs index 9050e59ce..a8e3a4623 100644 --- a/vm/src/compile.rs +++ b/vm/src/compile.rs @@ -79,12 +79,14 @@ impl Compiler { } fn push_new_code_object(&mut self, source_path: Option, obj_name: String) { + let line_number = self.get_source_line_number(); self.code_object_stack.push(CodeObject::new( Vec::new(), None, Vec::new(), None, source_path.clone(), + line_number, obj_name, )); } @@ -453,12 +455,14 @@ impl Compiler { } => { self.prepare_decorators(decorator_list)?; self.emit(Instruction::LoadBuildClass); + let line_number = self.get_source_line_number(); self.code_object_stack.push(CodeObject::new( vec![String::from("__locals__")], None, vec![], None, self.source_path.clone(), + line_number, name.clone(), )); self.emit(Instruction::LoadName { @@ -653,12 +657,14 @@ impl Compiler { }); } + let line_number = self.get_source_line_number(); self.code_object_stack.push(CodeObject::new( args.args.clone(), args.vararg.clone(), args.kwonlyargs.clone(), args.kwarg.clone(), self.source_path.clone(), + line_number, name.to_string(), )); @@ -1162,6 +1168,7 @@ impl Compiler { } .to_string(); + let line_number = self.get_source_line_number(); // Create magnificent function : self.code_object_stack.push(CodeObject::new( vec![".0".to_string()], @@ -1169,6 +1176,7 @@ impl Compiler { vec![], None, self.source_path.clone(), + line_number, name.clone(), )); @@ -1338,6 +1346,10 @@ impl Compiler { self.current_source_location = location.clone(); } + fn get_source_line_number(&mut self) -> usize { + self.current_source_location.get_row() + } + fn mark_generator(&mut self) { self.current_code_object().is_generator = true; }