Skip to content

Commit

Permalink
fix: Program deserialization (#1543)
Browse files Browse the repository at this point in the history
  • Loading branch information
Fumuran authored Oct 22, 2024
1 parent b2294a1 commit 2a97d8d
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 4 deletions.
64 changes: 61 additions & 3 deletions assembly/src/tests.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
use alloc::string::ToString;
use alloc::{string::ToString, vec::Vec};

use vm_core::mast::{MastNode, MastNodeId};
use vm_core::{
mast::{MastNode, MastNodeId},
Program,
};

use crate::{
assert_diagnostic_lines,
ast::{Module, ModuleKind},
diagnostics::{IntoDiagnostic, Report},
regex, source_file,
testing::{Pattern, TestContext},
Assembler, LibraryPath, ModuleParser,
Assembler, Deserializable, LibraryPath, ModuleParser, Serializable,
};

type TestResult = Result<(), Report>;
Expand Down Expand Up @@ -2970,3 +2973,58 @@ fn test_reexported_proc_with_same_name_as_local_proc_diff_locals() {

let _program = assembler.assemble_program(program_source).unwrap();
}

// PROGRAM SERIALIZATION AND DESERIALIZATION
// ================================================================================================
#[test]
fn test_program_serde_simple() {
let source = "
begin
push.1.2
add
drop
end
";

let assembler = Assembler::default();
let original_program = assembler.assemble_program(source).unwrap();

let mut target = Vec::new();
original_program.write_into(&mut target);
let deserialized_program = Program::read_from_bytes(&target).unwrap();

assert_eq!(original_program, deserialized_program);
}

#[test]
fn test_program_serde_with_decorators() {
let source = "
const.DEFAULT_CONST=100
proc.foo
push.1.2 add
debug.stack.8
end
begin
emit.DEFAULT_CONST
exec.foo
debug.stack.4
drop
trace.DEFAULT_CONST
end
";

let assembler = Assembler::default().with_debug_mode(true);
let original_program = assembler.assemble_program(source).unwrap();

let mut target = Vec::new();
original_program.write_into(&mut target);
let deserialized_program = Program::read_from_bytes(&target).unwrap();

assert_eq!(original_program, deserialized_program);
}
2 changes: 1 addition & 1 deletion core/src/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ impl Deserializable for Program {
let kernel = source.read()?;
let entrypoint = MastNodeId::from_u32_safe(source.read_u32()?, &mast_forest)?;

if mast_forest.is_procedure_root(entrypoint) {
if !mast_forest.is_procedure_root(entrypoint) {
return Err(DeserializationError::InvalidValue(format!(
"entrypoint {entrypoint} is not a procedure"
)));
Expand Down

0 comments on commit 2a97d8d

Please sign in to comment.