hlbc icon indicating copy to clipboard operation
hlbc copied to clipboard

Mutable `as_fn`?

Open N3rdL0rd opened this issue 6 months ago • 0 comments

In my codebase for hlbc-python, I want to add functions to modify and reserialize the bytecode, but this code does not work - as_fn does not return a mutable Function. I assume this is intentional, but is there some way to get around it?

    fn copy_function_from(&self, other: &Self, source_idx: String, dest_idx: String) -> PyResult<()> {
        catch_panic(|| {
            let source = RefFun(source_idx.parse::<usize>().unwrap()).as_fn(&other.bytecode).ok_or_else(|| 
                PyErr::new::<pyo3::exceptions::PyException, _>(format!("Source function with index {} not found!", source_idx)))?;
            let mut dest = RefFun(dest_idx.parse::<usize>().unwrap()).as_fn(&self.bytecode).ok_or_else(|| 
                PyErr::new::<pyo3::exceptions::PyException, _>(format!("Destination function with index {} not found!", dest_idx)))?;
            dest.ops = source.ops.clone();
            dest.regs = source.regs.clone();
            dest.debug_info = source.debug_info.clone();
            dest.assigns = source.assigns.clone();
            Ok(())
        })
    }

PS: Forgive me for my lack of knowledge of Rust, I'm a bit of a beginner with the language...

N3rdL0rd avatar Aug 14 '24 13:08 N3rdL0rd