Handle case where write_bits is used without being applied

This commit is contained in:
microproofs
2025-01-11 17:14:10 +07:00
parent e437d70ae2
commit d559e384ec
3 changed files with 41 additions and 3 deletions

View File

@@ -408,6 +408,10 @@ where
pub fn serialise_data() -> Self {
Term::Builtin(DefaultFunction::SerialiseData)
}
pub fn write_bits() -> Self {
Term::Builtin(DefaultFunction::WriteBits)
}
}
impl<T> Term<T>

View File

@@ -1498,6 +1498,7 @@ impl Term<Name> {
}
arg => {
context.write_bits_convert = true;
*arg = Term::var(INDICES_CONVERTER)
.apply(std::mem::replace(arg, Term::Error.force()));
}
@@ -1506,10 +1507,24 @@ impl Term<Name> {
}
Term::Builtin(DefaultFunction::WriteBits) => {
// first arg not needed
arg_stack.pop();
if arg_stack.is_empty() {
context.write_bits_convert = true;
*self = Term::write_bits()
.apply(Term::var("__arg_1"))
.apply(Term::var(INDICES_CONVERTER).apply(Term::var("__arg_2")))
.apply(Term::var("__arg_3"))
.lambda("__arg_3")
.lambda("__arg_2")
.lambda("__arg_1")
} else {
// first arg not needed
arg_stack.pop();
let Some(Args::Apply(arg_id, _)) = arg_stack.pop() else {
return;
};
if let Some(Args::Apply(arg_id, _)) = arg_stack.pop() {
context.write_bits_indices_arg.push(arg_id);
}
}