Skip to content

Commit

Permalink
implementation of isset(), unset() for objects inside mixed
Browse files Browse the repository at this point in the history
  • Loading branch information
mkornaukhov03 committed Nov 1, 2024
1 parent dc2cc92 commit c9790f9
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
18 changes: 18 additions & 0 deletions runtime-common/core/core-types/definition/mixed.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1428,6 +1428,11 @@ const mixed mixed::push_back_return(const mixed &v) {

bool mixed::isset(int64_t int_key) const {
if (unlikely (get_type() != type::ARRAY)) {
if (get_type() == type::OBJECT) {
// TODO think about numeric-like string
auto xxx = from_mixed<class_instance<C$ArrayAccess>>(*this, string());
return f$ArrayAccess$$offsetExists(xxx, int_key);
}
if (get_type() == type::STRING) {
int_key = as_string().get_correct_index(int_key);
return as_string().isset(int_key);
Expand Down Expand Up @@ -1472,6 +1477,13 @@ bool mixed::isset(double double_key) const {

void mixed::unset(int64_t int_key) {
if (unlikely (get_type() != type::ARRAY)) {
// TODO f$is_a
if (get_type() == type::OBJECT) {
auto xxx = from_mixed<class_instance<C$ArrayAccess>>(*this, string());
f$ArrayAccess$$offsetUnset(xxx, int_key);
return;
}

if (get_type() != type::NUL && (get_type() != type::BOOLEAN || as_bool())) {
php_warning("Cannot use variable of type %s as array in unset", get_type_or_class_name());
}
Expand All @@ -1484,6 +1496,12 @@ void mixed::unset(int64_t int_key) {

void mixed::unset(const mixed &v) {
if (unlikely (get_type() != type::ARRAY)) {
if (get_type() == type::OBJECT) {
auto xxx = from_mixed<class_instance<C$ArrayAccess>>(*this, string());
f$ArrayAccess$$offsetUnset(xxx, v);
return;
}

if (get_type() != type::NUL && (get_type() != type::BOOLEAN || as_bool())) {
php_warning("Cannot use variable of type %s as array in unset", get_type_or_class_name());
}
Expand Down
12 changes: 12 additions & 0 deletions runtime-common/core/core-types/definition/mixed.inl
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,12 @@ int64_t spaceship(const T1 &lhs, const T2 &rhs);
template <class ...MaybeHash>
bool mixed::isset(const string &string_key, MaybeHash ...maybe_hash) const {
if (unlikely (get_type() != type::ARRAY)) {
if (get_type() == type::OBJECT) {
// TODO think about numeric-like string
auto xxx = from_mixed<class_instance<C$ArrayAccess>>(*this, string());
return f$ArrayAccess$$offsetExists(xxx, string_key);
}

int64_t int_key{std::numeric_limits<int64_t>::max()};
if (get_type() == type::STRING) {
if (!string_key.try_to_int(&int_key)) {
Expand All @@ -110,6 +116,12 @@ bool mixed::isset(const string &string_key, MaybeHash ...maybe_hash) const {
template <class ...MaybeHash>
void mixed::unset(const string &string_key, MaybeHash ...maybe_hash) {
if (unlikely (get_type() != type::ARRAY)) {
if (get_type() == type::OBJECT) {
auto xxx = from_mixed<class_instance<C$ArrayAccess>>(*this, string());
f$ArrayAccess$$offsetUnset(xxx, string_key);
return;
}

if (get_type() != type::NUL && (get_type() != type::BOOLEAN || as_bool())) {
php_warning("Cannot use variable of type %s as array in unset", get_type_or_class_name());
}
Expand Down

0 comments on commit c9790f9

Please sign in to comment.