case IS_ARRAY:
return 1;
case IS_OBJECT:
- return instanceof_function(Z_OBJCE_P(iterable), zend_ce_traversable);
+ return zend_class_implements_interface(Z_OBJCE_P(iterable), zend_ce_traversable);
default:
return 0;
}
return 1;
}
- return instanceof_function(Z_OBJCE_P(countable), zend_ce_countable);
+ return zend_class_implements_interface(Z_OBJCE_P(countable), zend_ce_countable);
default:
return 0;
}
{
if (class_type->parent
&& (class_type->parent->serialize || class_type->parent->unserialize)
- && !instanceof_function_ex(class_type->parent, zend_ce_serializable, 1)) {
+ && !zend_class_implements_interface(class_type->parent, zend_ce_serializable)) {
return FAILURE;
}
if (!class_type->serialize) {
zend_class_entry *ce = object->ce;
zval tmp_offset;
- if (EXPECTED(instanceof_function_ex(ce, zend_ce_arrayaccess, 1) != 0)) {
+ if (EXPECTED(zend_class_implements_interface(ce, zend_ce_arrayaccess) != 0)) {
if (offset == NULL) {
/* [] construct */
ZVAL_NULL(&tmp_offset);
zend_class_entry *ce = object->ce;
zval tmp_offset;
- if (EXPECTED(instanceof_function_ex(ce, zend_ce_arrayaccess, 1) != 0)) {
+ if (EXPECTED(zend_class_implements_interface(ce, zend_ce_arrayaccess) != 0)) {
if (!offset) {
ZVAL_NULL(&tmp_offset);
} else {
zval retval, tmp_offset;
int result;
- if (EXPECTED(instanceof_function_ex(ce, zend_ce_arrayaccess, 1) != 0)) {
+ if (EXPECTED(zend_class_implements_interface(ce, zend_ce_arrayaccess) != 0)) {
ZVAL_COPY_DEREF(&tmp_offset, offset);
GC_ADDREF(object);
zend_call_method_with_1_params(object, ce, NULL, "offsetexists", &retval, &tmp_offset);
zend_class_entry *ce = object->ce;
zval tmp_offset;
- if (instanceof_function_ex(ce, zend_ce_arrayaccess, 1)) {
+ if (zend_class_implements_interface(ce, zend_ce_arrayaccess)) {
ZVAL_COPY_DEREF(&tmp_offset, offset);
GC_ADDREF(object);
zend_call_method_with_1_params(object, ce, NULL, "offsetunset", NULL, &tmp_offset);
}
/* }}} */
-static zend_always_inline zend_bool instanceof_class(const zend_class_entry *instance_ce, const zend_class_entry *ce) /* {{{ */
-{
- do {
- if (instance_ce == ce) {
- return 1;
- }
- instance_ce = instance_ce->parent;
- } while (instance_ce);
- return 0;
-}
-/* }}} */
-
-static zend_bool ZEND_FASTCALL instanceof_interface(const zend_class_entry *instance_ce, const zend_class_entry *ce) /* {{{ */
+ZEND_API zend_bool ZEND_FASTCALL zend_class_implements_interface(const zend_class_entry *class_ce, const zend_class_entry *interface_ce) /* {{{ */
{
uint32_t i;
+ ZEND_ASSERT(!(class_ce->ce_flags & ZEND_ACC_INTERFACE));
+ ZEND_ASSERT(interface_ce->ce_flags & ZEND_ACC_INTERFACE);
- if (instance_ce->num_interfaces) {
- ZEND_ASSERT(instance_ce->ce_flags & ZEND_ACC_RESOLVED_INTERFACES);
- for (i = 0; i < instance_ce->num_interfaces; i++) {
- if (instance_ce->interfaces[i] == ce) {
+ if (class_ce->num_interfaces) {
+ ZEND_ASSERT(class_ce->ce_flags & ZEND_ACC_RESOLVED_INTERFACES);
+ for (i = 0; i < class_ce->num_interfaces; i++) {
+ if (class_ce->interfaces[i] == interface_ce) {
return 1;
}
}
}
- return instance_ce == ce;
-}
-/* }}} */
-
-// TODO: It would make more sense to expose instanceof_class + instanceof_interface instead
-ZEND_API zend_bool ZEND_FASTCALL instanceof_function_ex(const zend_class_entry *instance_ce, const zend_class_entry *ce, zend_bool is_interface) /* {{{ */
-{
- if (is_interface) {
- ZEND_ASSERT(ce->ce_flags & ZEND_ACC_INTERFACE);
- return instanceof_interface(instance_ce, ce);
- } else {
- ZEND_ASSERT(!(ce->ce_flags & ZEND_ACC_INTERFACE));
- return instanceof_class(instance_ce, ce);
- }
+ return 0;
}
/* }}} */
-ZEND_API zend_bool ZEND_FASTCALL instanceof_function(const zend_class_entry *instance_ce, const zend_class_entry *ce) /* {{{ */
+ZEND_API zend_bool ZEND_FASTCALL instanceof_function_slow(const zend_class_entry *instance_ce, const zend_class_entry *ce) /* {{{ */
{
+ ZEND_ASSERT(instance_ce != ce && "Should have been checked already");
if (ce->ce_flags & ZEND_ACC_INTERFACE) {
- return instanceof_interface(instance_ce, ce);
+ uint32_t i;
+
+ if (instance_ce->num_interfaces) {
+ ZEND_ASSERT(instance_ce->ce_flags & ZEND_ACC_RESOLVED_INTERFACES);
+ for (i = 0; i < instance_ce->num_interfaces; i++) {
+ if (instance_ce->interfaces[i] == ce) {
+ return 1;
+ }
+ }
+ }
+ return 0;
} else {
- return instanceof_class(instance_ce, ce);
+ while (1) {
+ instance_ce = instance_ce->parent;
+ if (instance_ce == ce) {
+ return 1;
+ }
+ if (instance_ce == NULL) {
+ return 0;
+ }
+ }
}
}
/* }}} */
ZEND_API int ZEND_FASTCALL is_smaller_function(zval *result, zval *op1, zval *op2);
ZEND_API int ZEND_FASTCALL is_smaller_or_equal_function(zval *result, zval *op1, zval *op2);
-ZEND_API zend_bool ZEND_FASTCALL instanceof_function_ex(const zend_class_entry *instance_ce, const zend_class_entry *ce, zend_bool is_interface);
-ZEND_API zend_bool ZEND_FASTCALL instanceof_function(const zend_class_entry *instance_ce, const zend_class_entry *ce);
+ZEND_API zend_bool ZEND_FASTCALL zend_class_implements_interface(const zend_class_entry *class_ce, const zend_class_entry *interface_ce);
+ZEND_API zend_bool ZEND_FASTCALL instanceof_function_slow(const zend_class_entry *instance_ce, const zend_class_entry *ce);
+
+static zend_always_inline zend_bool instanceof_function(
+ const zend_class_entry *instance_ce, const zend_class_entry *ce) {
+ return instance_ce == ce || instanceof_function_slow(instance_ce, ce);
+}
/**
* Checks whether the string "str" with length "length" is numeric. The value
}
/* if not and the object implements Countable we call its count() method */
- if (instanceof_function(zobj->ce, zend_ce_countable)) {
+ if (zend_class_implements_interface(zobj->ce, zend_ce_countable)) {
zval retval;
zend_call_method_with_0_params(zobj, NULL, NULL, "count", &retval);
}
/* if not and the object implements Countable we call its count() method */
- if (instanceof_function(zobj->ce, zend_ce_countable)) {
+ if (zend_class_implements_interface(zobj->ce, zend_ce_countable)) {
zval retval;
zend_call_method_with_0_params(zobj, NULL, NULL, "count", &retval);
}
/* if not and the object implements Countable we call its count() method */
- if (instanceof_function(zobj->ce, zend_ce_countable)) {
+ if (zend_class_implements_interface(zobj->ce, zend_ce_countable)) {
zval retval;
zend_call_method_with_0_params(zobj, NULL, NULL, "count", &retval);
}
/* if not and the object implements Countable we call its count() method */
- if (instanceof_function(zobj->ce, zend_ce_countable)) {
+ if (zend_class_implements_interface(zobj->ce, zend_ce_countable)) {
zval retval;
zend_call_method_with_0_params(zobj, NULL, NULL, "count", &retval);
cal_int_type = Z_LVAL_P(calendar_zv);
} else if (Z_TYPE_P(calendar_zv) == IS_OBJECT &&
- instanceof_function_ex(Z_OBJCE_P(calendar_zv),
- Calendar_ce_ptr, 0)) {
+ instanceof_function(Z_OBJCE_P(calendar_zv), Calendar_ce_ptr)) {
cal = calendar_fetch_native_calendar(calendar_zv);
if (cal == NULL) {
if (ce->iterator_funcs_ptr) {
memset(ce->iterator_funcs_ptr, 0, sizeof(zend_class_iterator_funcs));
- if (instanceof_function_ex(ce, zend_ce_aggregate, 1)) {
+ if (zend_class_implements_interface(ce, zend_ce_aggregate)) {
ce->iterator_funcs_ptr->zf_new_iterator = zend_hash_str_find_ptr(&ce->function_table, "getiterator", sizeof("getiterator") - 1);
}
- if (instanceof_function_ex(ce, zend_ce_iterator, 1)) {
+ if (zend_class_implements_interface(ce, zend_ce_iterator)) {
ce->iterator_funcs_ptr->zf_rewind = zend_hash_str_find_ptr(&ce->function_table, "rewind", sizeof("rewind") - 1);
ce->iterator_funcs_ptr->zf_valid = zend_hash_str_find_ptr(&ce->function_table, "valid", sizeof("valid") - 1);
ce->iterator_funcs_ptr->zf_key = zend_hash_str_find_ptr(&ce->function_table, "key", sizeof("key") - 1);