From be565d30ae90a44539063a2fb7af417aa60b77ee Mon Sep 17 00:00:00 2001 From: Noah <33094578+coolreader18@users.noreply.github.com> Date: Sat, 8 Feb 2020 23:23:03 -0600 Subject: [PATCH] Make incognito a CodeFlag bit --- bytecode/src/bytecode.rs | 9 ++++++--- compiler/src/compile.rs | 3 ++- vm/src/obj/objcode.rs | 2 +- vm/src/obj/objframe.rs | 4 ++-- vm/src/obj/objfunction.rs | 2 +- 5 files changed, 12 insertions(+), 8 deletions(-) diff --git a/bytecode/src/bytecode.rs b/bytecode/src/bytecode.rs index ae07a8b2e..cdd24221a 100644 --- a/bytecode/src/bytecode.rs +++ b/bytecode/src/bytecode.rs @@ -46,18 +46,18 @@ pub struct CodeObject { pub source_path: String, pub first_line_number: usize, pub obj_name: String, // Name of the object that created this code object - pub incognito: bool, } bitflags! { #[derive(Serialize, Deserialize)] - pub struct CodeFlags: u8 { + pub struct CodeFlags: u16 { const HAS_DEFAULTS = 0x01; const HAS_KW_ONLY_DEFAULTS = 0x02; const HAS_ANNOTATIONS = 0x04; const NEW_LOCALS = 0x08; const IS_GENERATOR = 0x10; const IS_COROUTINE = 0x20; + const INCOGNITO = 1 << 15; } } @@ -394,7 +394,6 @@ impl CodeObject { source_path, first_line_number, obj_name, - incognito: false, } } @@ -451,6 +450,10 @@ impl CodeObject { } Display(self) } + + pub fn incognito(&self) -> bool { + self.flags.contains(CodeFlags::INCOGNITO) + } } impl fmt::Display for CodeObject { diff --git a/compiler/src/compile.rs b/compiler/src/compile.rs index bceee87f5..e602159fb 100644 --- a/compiler/src/compile.rs +++ b/compiler/src/compile.rs @@ -189,7 +189,8 @@ impl Compiler { } fn push_output(&mut self, mut code: CodeObject) { - code.incognito = self.opts.incognito; + code.flags + .set(bytecode::CodeFlags::INCOGNITO, self.opts.incognito); self.output_stack.push(code.into()); } diff --git a/vm/src/obj/objcode.rs b/vm/src/obj/objcode.rs index 16410e9be..71053bdb9 100644 --- a/vm/src/obj/objcode.rs +++ b/vm/src/obj/objcode.rs @@ -103,7 +103,7 @@ impl PyCodeRef { } #[pyproperty] - fn co_flags(self) -> u8 { + fn co_flags(self) -> u16 { self.code.flags.bits() } } diff --git a/vm/src/obj/objframe.rs b/vm/src/obj/objframe.rs index 7ad7b8119..118223d40 100644 --- a/vm/src/obj/objframe.rs +++ b/vm/src/obj/objframe.rs @@ -31,7 +31,7 @@ impl FrameRef { #[pyproperty] fn f_globals(self, vm: &VirtualMachine) -> PyResult { - if self.code.incognito { + if self.code.incognito() { Err(vm.new_type_error("Can't get f_globals on an incognito frame".to_owned())) } else { Ok(self.scope.globals.clone()) @@ -40,7 +40,7 @@ impl FrameRef { #[pyproperty] fn f_locals(self, vm: &VirtualMachine) -> PyResult { - if self.code.incognito { + if self.code.incognito() { Err(vm.new_type_error("Can't get f_locals on an incognito frame".to_owned())) } else { Ok(self.scope.get_locals()) diff --git a/vm/src/obj/objfunction.rs b/vm/src/obj/objfunction.rs index e63abfdfb..8e536561d 100644 --- a/vm/src/obj/objfunction.rs +++ b/vm/src/obj/objfunction.rs @@ -281,7 +281,7 @@ impl PyFunction { #[pyproperty(magic)] fn globals(&self, vm: &VirtualMachine) -> PyResult { - if self.code.incognito { + if self.code.incognito() { Err(vm.new_type_error("Can't get __globals__ on an incognito function".to_owned())) } else { Ok(self.scope.globals.clone())