diff --git a/bytecode/src/bytecode.rs b/bytecode/src/bytecode.rs index 427df199c..7fe640c1c 100644 --- a/bytecode/src/bytecode.rs +++ b/bytecode/src/bytecode.rs @@ -36,6 +36,9 @@ pub trait Constant: Sized { fn into_data(self) -> ConstantData { self.borrow_constant().into_data() } + fn map_constant(self, bag: &Bag) -> Bag::Constant { + bag.make_constant(self.into_data()) + } } impl Constant for ConstantData { fn borrow_constant(&self) -> BorrowedConstant { @@ -591,9 +594,13 @@ impl CodeObject { Display(self) } - fn _map_inner(self, map: impl Fn(C) -> U) -> CodeObject { + pub fn map_bag(self, bag: &Bag) -> CodeObject { CodeObject { - constants: self.constants.into_iter().map(map).collect(), + constants: self + .constants + .into_iter() + .map(|x| x.map_constant(bag)) + .collect(), instructions: self.instructions, label_map: self.label_map, @@ -610,10 +617,6 @@ impl CodeObject { } } - pub fn map_bag(self, bag: &Bag) -> CodeObject { - self._map_inner(|x| bag.make_constant_borrowed(x.borrow_constant())) - } - pub fn map_clone_bag(&self, bag: &Bag) -> CodeObject { CodeObject { constants: self @@ -651,10 +654,6 @@ impl CodeObject { let data = bincode::serialize(&self).expect("Code object must be serializable"); lz4_compression::compress::compress(&data) } - - pub fn map_basic(self, bag: &Bag) -> CodeObject { - self._map_inner(|x| bag.make_constant(x)) - } } impl fmt::Display for CodeObject { diff --git a/vm/src/builtins/code.rs b/vm/src/builtins/code.rs index 6e251cda0..708df4040 100644 --- a/vm/src/builtins/code.rs +++ b/vm/src/builtins/code.rs @@ -70,6 +70,9 @@ impl Constant for PyConstant { fn borrow_constant(&self) -> BorrowedConstant { borrow_obj_constant(&self.0) } + fn map_constant(self, bag: &Bag) -> Bag::Constant { + bag.make_constant_borrowed(self.borrow_constant()) + } } pub(crate) struct PyObjBag<'a>(pub &'a PyContext); diff --git a/vm/src/pyobject.rs b/vm/src/pyobject.rs index 48b77cb26..2bb62bf1e 100644 --- a/vm/src/pyobject.rs +++ b/vm/src/pyobject.rs @@ -407,7 +407,7 @@ impl PyContext { } pub fn map_codeobj(&self, code: bytecode::CodeObject) -> code::CodeObject { - code.map_basic(&code::PyObjBag(self)) + code.map_bag(&code::PyObjBag(self)) } pub fn add_tp_new_wrapper(&self, ty: &PyTypeRef) {