Skip to content

Commit

Permalink
feat: hash_code
Browse files Browse the repository at this point in the history
  • Loading branch information
nullishamy committed Oct 28, 2023
1 parent 041dfba commit d697490
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
2 changes: 1 addition & 1 deletion sources/interpreter_two/src/bytecode/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pub fn decode_instruction(_vm: &VM, bytes: &mut BytesMut) -> Result<Box<dyn Inst
Ok(match instruction {
0x00 => b(ops::Nop),

// Constants Loads Stores
// Constants / Loads / Stores
0x01 => b(ops::PushConst {
value: RuntimeValue::Null,
}),
Expand Down
29 changes: 24 additions & 5 deletions sources/interpreter_two/src/object/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ pub trait Object: fmt::Debug {
}
}


#[derive(Debug)]
pub struct RuntimeObject {
pub class_object: WrappedClassObject,
Expand All @@ -49,9 +48,9 @@ impl RuntimeObject {
pub fn new(class_object: WrappedClassObject) -> Self {
Self {
class_object,
instance_fields: HashMap::new()
instance_fields: HashMap::new(),
}
}
}
}

impl Object for RuntimeObject {
Expand Down Expand Up @@ -227,8 +226,10 @@ impl StringObject {
})));

s.set_instance_field(("value".to_string(), "[B".to_string()), arr)?;
s.set_instance_field(("coder".to_string(), "B".to_string()), RuntimeValue::Integral((1_i32).into()))?;

s.set_instance_field(
("coder".to_string(), "B".to_string()),
RuntimeValue::Integral((1_i32).into()),
)?;

Ok(s)
}
Expand All @@ -255,6 +256,24 @@ pub enum RuntimeValue {
Null,
}

impl RuntimeValue {
pub fn hash_code(&self) -> i32 {
match self {
RuntimeValue::Object(data) => {
let pt = Rc::as_ptr(data);
pt as *const () as i32
},
RuntimeValue::Array(data) => {
let pt = Rc::as_ptr(data);
pt as *const () as i32
},
RuntimeValue::Integral(data) => data.value as i32,
RuntimeValue::Floating(data) => data.value as i32,
RuntimeValue::Null => 0,
}
}
}

const UPPER_SCIENCE_BOUND: f64 = 1_000_000.0;
const LOWER_SCIENCE_BOUND: f64 = 0.000_000_1;

Expand Down

0 comments on commit d697490

Please sign in to comment.