Remove BaseObj and fix Object::equals

* BaseObj felt a bit redundant. For everything that BaseObj did, we use
  Obj instead.
* Object::equals was a little weird. It was used for giving back
  equality, except when it wasn't. It's a little better defined now,
  here's what I'm shooting for:
    * *In general*, Object::equals will return true when two objects
      refer to the same object.
    * The exception to this rule is for "constant" objects, or "copy on
      write" objects. These include, but are not limited to: Int, Float,
      Bool, Nil, Str. Their base values are immutable and are the heart
      of object equality.

Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
This commit is contained in:
2024-10-10 20:30:24 -07:00
parent 6143437626
commit 3fd0ba3a0b
9 changed files with 114 additions and 209 deletions

View File

@@ -1,11 +1,10 @@
use std::fmt::{self, Debug, Display};
use std::ptr;
use std::rc::Rc;
use gc::{Finalize, Trace};
use crate::obj::macros::*;
use crate::obj::{make_ptr, BaseObj, ObjP, Object};
use crate::obj::prelude::*;
use crate::vm::{Argc, Chunk, Frame, Function, Vm};
////////////////////////////////////////////////////////////////////////////////
@@ -56,7 +55,7 @@ pub type BuiltinFunctionPtr = fn(vm: &mut Vm, function_state: FunctionState) ->
#[derive(Trace, Finalize)]
pub struct BuiltinFunction {
base: BaseObj,
base: Obj,
#[unsafe_ignore_trace]
name: Rc<String>,
#[unsafe_ignore_trace]
@@ -117,16 +116,6 @@ impl Object for BuiltinFunction {
vm.push_frame(new_frame);
}
fn equals(&self, other: &dyn Object) -> bool {
// TODO BuiltinFunction::equals : need something more robust than checking addr_eq,
// maybe check the self_binding pointer too?
if let Some(other) = other.as_any().downcast_ref::<BuiltinFunction>() {
ptr::addr_eq(self, other)
} else {
false
}
}
impl_base_obj!(BuiltinFunction);
}
@@ -136,7 +125,7 @@ impl Object for BuiltinFunction {
#[derive(Clone, Trace, Finalize)]
pub struct UserFunction {
base: BaseObj,
base: Obj,
#[unsafe_ignore_trace]
path: Rc<String>,
#[unsafe_ignore_trace]
@@ -218,15 +207,6 @@ impl Object for UserFunction {
}
}
fn equals(&self, other: &dyn Object) -> bool {
if let Some(other) = other.as_any().downcast_ref::<UserFunction>() {
// TODO UserFunction::equals : need something more robust than checking addr_eq.
ptr::addr_eq(self, other)
} else {
false
}
}
impl_base_obj!(UserFunction);
}
@@ -236,7 +216,7 @@ impl Object for UserFunction {
#[derive(Trace, Finalize)]
pub struct Method {
base: BaseObj,
base: Obj,
self_binding: ObjP,
function: ObjP,
}
@@ -312,14 +292,5 @@ impl Object for Method {
self.function.borrow().call(vm, argc)
}
fn equals(&self, other: &dyn Object) -> bool {
if let Some(other) = other.as_any().downcast_ref::<Method>() {
ptr::addr_eq(&*self.self_binding(), &*other.self_binding())
&& ptr::addr_eq(&*self.function, &*other.function)
} else {
false
}
}
impl_base_obj!(Method);
}