Skip to content

Commit

Permalink
Rework direct API for object comparison
Browse files Browse the repository at this point in the history
Add sq_direct_is_equal() - this is not the same as invoking ObjCmp()
on objects. Logic of this comparison is the same as for <=> operator
and it can fail in certain scenarios.
Also for safety such comparison of non-numeric types (or classes
implementing _cmp metamethod) will be disabled soon.
In practice sq_direct_cmp() function was never needed - in all the
cases it was used for the equality check.
For this, implement sq_direct_is_equal() function.

Also simplify sq_direct_cmp() implementation - make it a thin wrapper
around SQVM::ObjCmp(). No need for extra logic that differs from VM
internals.
  • Loading branch information
VasiliyRyabtsev committed Oct 30, 2024
1 parent 7934d9b commit 76078d9
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 12 deletions.
1 change: 1 addition & 0 deletions include/sqdirect.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ SQUIRREL_API SQRESULT sq_direct_get(HSQUIRRELVM v, const HSQOBJECT *obj, const H
SQUIRREL_API SQBool sq_direct_tobool(const HSQOBJECT *o);

SQUIRREL_API SQBool sq_direct_cmp(HSQUIRRELVM v, const HSQOBJECT *a, const HSQOBJECT *b, SQInteger *res);
SQUIRREL_API bool sq_direct_is_equal(HSQUIRRELVM v, const HSQOBJECT *a, const HSQOBJECT *b);

SQUIRREL_API SQRESULT sq_direct_getuserdata(const HSQOBJECT *obj, SQUserPointer *p, SQUserPointer *typetag=NULL);

Expand Down
3 changes: 1 addition & 2 deletions sqrat/include/sqrat/sqratFunction.h
Original file line number Diff line number Diff line change
Expand Up @@ -284,8 +284,7 @@ class Function {
if (GetVM() != so.GetVM())
return false;

SQInteger res = 0;
return sq_direct_cmp(vm, &obj, &so.obj, &res) && (res==0);
return sq_direct_is_equal(vm, &obj, &so.obj);
}

private:
Expand Down
3 changes: 1 addition & 2 deletions sqrat/include/sqrat/sqratObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,7 @@ class Object {
if (GetVM() != so.GetVM())
return false;

SQInteger res = 0;
return sq_direct_cmp(vm, &obj, &so.obj, &res) && (res==0);
return sq_direct_is_equal(vm, &obj, &so.obj);
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Expand Down
23 changes: 15 additions & 8 deletions squirrel/sqdirect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,23 @@ SQBool sq_direct_cmp(HSQUIRRELVM v, const HSQOBJECT *a, const HSQOBJECT *b, SQIn
const SQObjectPtr &aPtr = static_cast<const SQObjectPtr &>(*a);
const SQObjectPtr &bPtr = static_cast<const SQObjectPtr &>(*b);

SQObjectType t1 = sq_type(*a), t2 = sq_type(*b);
if (t1 == t2 || t1==OT_NULL || t2==OT_NULL || (sq_isnumeric(*a) && sq_isnumeric(*b)))
{
SQBool status = v->ObjCmp(aPtr, bPtr, *res);
assert(status);
return status;
}
return false;
return v->ObjCmp(aPtr, bPtr, *res);
}


bool sq_direct_is_equal(HSQUIRRELVM v, const HSQOBJECT *a, const HSQOBJECT *b)
{
const SQObjectPtr &aPtr = static_cast<const SQObjectPtr &>(*a);
const SQObjectPtr &bPtr = static_cast<const SQObjectPtr &>(*b);

bool res = false;
bool status = v->IsEqual(aPtr, bPtr, res);
(void)status;
assert(status); // cannot fail
return res;
}


SQRESULT sq_direct_getuserdata(const HSQOBJECT *obj, SQUserPointer *p, SQUserPointer *typetag)
{
if (obj->_type != OT_USERDATA)
Expand Down

0 comments on commit 76078d9

Please sign in to comment.