use crate::bytecode;
use crate::obj::objbytes::{PyBytes, PyBytesRef};
use crate::obj::objcode::{PyCode, PyCodeRef};
use crate::pyobject::{PyObjectRef, PyResult};
use crate::vm::VirtualMachine;

fn marshal_dumps(co: PyCodeRef, _vm: &VirtualMachine) -> PyBytes {
    PyBytes::new(bincode::serialize(&co.code).unwrap())
}

fn marshal_loads(code_bytes: PyBytesRef, vm: &VirtualMachine) -> PyResult<PyCode> {
    let code = bincode::deserialize::<bytecode::CodeObject>(&code_bytes)
        .map_err(|_| vm.new_value_error("Couldn't deserialize python bytecode".to_owned()))?;
    Ok(PyCode { code })
}

pub fn make_module(vm: &VirtualMachine) -> PyObjectRef {
    let ctx = &vm.ctx;

    py_module!(vm, "marshal", {
        "loads" => ctx.new_rustfunc(marshal_loads),
        "dumps" => ctx.new_rustfunc(marshal_dumps),
    })
}
