Fix decode bug in varying.

This commit is contained in:
yukirij 2023-08-19 00:40:03 -07:00
parent 24ce88cb3f
commit 5d82c7659b
5 changed files with 27 additions and 33 deletions

View File

@ -147,7 +147,7 @@ let refer = acquire(list(integer()));
`release(refer:Reference)` `release(refer:Reference)`
Destruct and deallocate a type. Destruct and deallocate an object.
``` ```
release(refer); release(refer);
``` ```
@ -173,7 +173,7 @@ println!("{}", target.get()); // prints "Hello, world!"
`copy(dst:Reference, src:Reference) -> Result<(),()>` `copy(dst:Reference, src:Reference) -> Result<(),()>`
Copy the contents of an objcet to another location, keeping the original. Copy the contents of an object to another location, keeping the original.
``` ```
let original = Sequence::with("Hello, world!"); let original = Sequence::with("Hello, world!");

View File

@ -10,6 +10,7 @@ use crate::{
}; };
use crate::kind; use crate::kind;
use crate::{ use crate::{
Varying,
Boolean, Boolean,
Natural, Integer, Natural, Integer,
Block, Sequence, Block, Sequence,
@ -39,6 +40,10 @@ pub fn encode_data(addr:Reference) -> Vec<u8>
{ {
let mut result = Vec::<u8>::new(); let mut result = Vec::<u8>::new();
match unsafe {type_key(addr.class)} { match unsafe {type_key(addr.class)} {
tag::VARYING => {
let data = Varying::from(addr).unwrap();
result.append(&mut encode(data.get()));
}
tag::BOOLEAN => { tag::BOOLEAN => {
result.append(&mut util::pack_natural(Boolean::from(addr).unwrap().get() as u64)); result.append(&mut util::pack_natural(Boolean::from(addr).unwrap().get() as u64));
} }
@ -128,7 +133,12 @@ pub fn decode_data(data:&Vec<u8>, type_id:usize, index:&mut usize) -> Result<Typ
{ {
match unsafe {type_key(type_id)} { match unsafe {type_key(type_id)} {
tag::VARYING => { tag::VARYING => {
return decode(data, index); return match decode(data, index) {
Ok(value) => {
Ok(Type::Varying(Varying::with(value.get())))
}
Err(_) => Err(()),
}
} }
tag::BOOLEAN => { tag::BOOLEAN => {
return Ok(Type::Boolean(Boolean::with(unpack_natural(data, index) == 1))); return Ok(Type::Boolean(Boolean::with(unpack_natural(data, index) == 1)));

View File

@ -4,8 +4,8 @@ use crate::runtime;
pub fn inner(type_id:usize) -> usize { unsafe {runtime::type_inner(type_id)} } pub fn inner(type_id:usize) -> usize { unsafe {runtime::type_inner(type_id)} }
pub fn kind(type_id:usize) -> usize { unsafe {runtime::type_key(type_id)} } pub fn kind(type_id:usize) -> usize { unsafe {runtime::type_key(type_id)} }
pub fn varying() -> usize { 0 } pub fn null() -> usize { 0 }
pub fn null() -> usize { unsafe { runtime::type_outer(0, tag::NULL) } } pub fn varying() -> usize { unsafe { runtime::type_outer(0, tag::VARYING) } }
pub fn boolean() -> usize { unsafe { runtime::type_outer(0, tag::BOOLEAN) } } pub fn boolean() -> usize { unsafe { runtime::type_outer(0, tag::BOOLEAN) } }
pub fn natural() -> usize { unsafe { runtime::type_outer(0, tag::NATURAL) } } pub fn natural() -> usize { unsafe { runtime::type_outer(0, tag::NATURAL) } }
pub fn integer() -> usize { unsafe { runtime::type_outer(0, tag::INTEGER) } } pub fn integer() -> usize { unsafe { runtime::type_outer(0, tag::INTEGER) } }

View File

@ -31,6 +31,13 @@ impl Varying {
} }
} }
pub fn with(addr:Reference) -> Self
{
let mut obj = Varying::new();
obj.set(addr);
return obj;
}
pub fn detach(&mut self) pub fn detach(&mut self)
{ {
self.managed = false; self.managed = false;

View File

@ -7,28 +7,10 @@ mod interface; pub use interface::*;
mod encoding; pub use encoding::*; mod encoding; pub use encoding::*;
pub fn test() { pub fn test() {
Schema::with(vec![ let mut data = Varying::new();
("nat", natural()), data.set(*Natural::with(79));
("pass", block(6)),
]).bind(9);
Schema::with(vec![
("str", sequence()),
("int", integer()),
("bool", boolean()),
("rec", record(9)),
]).bind(10);
let rec = Record::with(10, vec![ let out = encode(*data);
("str", *Sequence::with("hello!")),
("int", *Integer::with(-70)),
("bool", *Boolean::with(true)),
("rec", *Record::with(9, vec![
("nat", *Natural::with(8)),
("pass", *Block::with(6, vec![1, 2, 3, 4, 5, 6])),
]).unwrap()),
]).unwrap();
let out = encode(*rec);
print!("[{}]: ", out.len()); print!("[{}]: ", out.len());
for byte in &out { for byte in &out {
print!("{:02x} ", *byte); print!("{:02x} ", *byte);
@ -36,13 +18,8 @@ pub fn test() {
match decode(&out, &mut 0) { match decode(&out, &mut 0) {
Ok(ty) => match ty { Ok(ty) => match ty {
Type::Record(rec) => { Type::Varying(data) => {
println!("{}", Sequence::from(rec.get("str")).unwrap().get()); println!("Ok: {}", Natural::from(data.get()).unwrap().get());
println!("{}", Integer::from(rec.get("int")).unwrap().get());
println!("{}", Boolean::from(rec.get("bool")).unwrap().get());
let sub = Record::from(rec.get("rec")).unwrap();
println!("{}", Natural::from(sub.get("nat")).unwrap().get());
println!("{:?}", Block::from(sub.get("pass")).unwrap().get());
} }
_ => { println!("Other"); } _ => { println!("Other"); }
}, },