Skip to content

Commit

Permalink
Bug fix
Browse files Browse the repository at this point in the history
  • Loading branch information
HenryAWE committed Sep 9, 2024
1 parent 39b70fb commit 875ae8f
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 12 deletions.
4 changes: 2 additions & 2 deletions include/asbind20/bind.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ class value_class : private detail::class_register_base
if constexpr(has_member_op_eq)
{
method(
detail::concat(m_name, "& opEquals(const ", m_name, " &in) const").c_str(),
detail::concat("bool opEquals(const ", m_name, " &in) const").c_str(),
static_cast<bool (T::*)(const T&) const>(&T::operator==)
);
}
Expand All @@ -473,7 +473,7 @@ class value_class : private detail::class_register_base
return lhs == rhs;
};
method(
detail::concat(m_name, "& opEquals(const ", m_name, " &in) const").c_str(),
detail::concat("bool opEquals(const ", m_name, " &in) const").c_str(),
wrapper_obj_last,
call_conv<asCALL_CDECL_OBJLAST>
);
Expand Down
5 changes: 4 additions & 1 deletion include/asbind20/ext/array.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ class script_array
int opEquals_status;
};

bool equals_impl(const void* lhs, const void* rhs, asIScriptContext* ctx, const array_cache* cache) const;
bool operator_eq_impl(const void* lhs, const void* rhs, asIScriptContext* ctx, const array_cache* cache) const;

void cache_data();
static void cache_cleanup_callback(asITypeInfo* ti);
Expand All @@ -98,7 +98,10 @@ class script_array
void enum_refs(asIScriptEngine* engine);
void release_refs(asIScriptEngine* engine);

bool member_indirect() const;

void* ref_at(asUINT idx);
const void* ref_at(asUINT idx) const;

void* opIndex(asUINT idx);

Expand Down
28 changes: 20 additions & 8 deletions src/ext/array.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ void script_array::assign_impl(void* ptr, void* value)
}
}

bool script_array::equals_impl(const void* lhs, const void* rhs, asIScriptContext* ctx, const array_cache* cache) const
bool script_array::operator_eq_impl(const void* lhs, const void* rhs, asIScriptContext* ctx, const array_cache* cache) const
{
if(!(m_subtype_id & ~asTYPEID_MASK_SEQNBR))
{
Expand Down Expand Up @@ -315,18 +315,17 @@ bool script_array::equals_impl(const void* lhs, const void* rhs, asIScriptContex
{
auto result = script_invoke<bool>(
ctx,
static_cast<const asIScriptObject*>(*((void**)const_cast<void*>(lhs))),
static_cast<const asIScriptObject*>(*(void**)lhs),
cache->subtype_opEquals,
rhs
*(void**)rhs
);

if(!result.has_value())
return false;
return *result;
}

// Fallback to OpCmp() == 0
if(cache->subtype_opCmp)
else if(cache->subtype_opCmp)
{
auto result = script_invoke<int>(
ctx,
Expand Down Expand Up @@ -437,7 +436,7 @@ void script_array::cache_data()
cache->opEquals_status = asMULTIPLE_FUNCTIONS;
}
else
cache->subtype_opCmp = func;
cache->subtype_opEquals = func;
}
}
}
Expand Down Expand Up @@ -492,9 +491,22 @@ const void* script_array::operator[](asUINT idx) const
return m_data.ptr + offset;
}

bool script_array::member_indirect() const
{
return ((m_subtype_id & asTYPEID_MASK_OBJECT) && !(m_subtype_id & asTYPEID_OBJHANDLE));
}

void* script_array::ref_at(size_type idx)
{
if((m_subtype_id & asTYPEID_MASK_OBJECT) && !(m_subtype_id & asTYPEID_OBJHANDLE))
if(member_indirect())
return *(void**)(*this)[idx];
else
return (*this)[idx];
}

const void* script_array::ref_at(size_type idx) const
{
if(member_indirect())
return *(void**)(*this)[idx];
else
return (*this)[idx];
Expand Down Expand Up @@ -538,7 +550,7 @@ bool script_array::operator==(const script_array& other) const
array_cache* cache = reinterpret_cast<array_cache*>(m_ti->GetUserData(script_array_cache_id()));
for(size_type i = 0; i < size(); ++i)
{
if(!equals_impl((*this)[i], other[i], ctx, cache))
if(!operator_eq_impl((*this)[i], other[i], ctx, cache))
{
result = false;
break;
Expand Down
7 changes: 6 additions & 1 deletion test/script/test_array.as
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,10 @@ void string_array()
assert(strs[0] == "hello");
assert(strs[1] == "world");
assert(strs[2] == "array");
assert(strs == {"hello", "world", "array"});
}

void main()
void check_int_array()
{
array<int> ia = {1, 2, 3, 4};

Expand All @@ -102,7 +103,11 @@ void main()
assert(ia.size == 5);
assert(ia.back == 5);
assert(ia == {0, 2, 3, 4, 5});
}

void main()
{
check_int_array();
my_pair_array();
my_pair_ref_array();
string_array();
Expand Down

0 comments on commit 875ae8f

Please sign in to comment.