Skip to content

Split out the TYPE_CHECK implementation for resources #4921

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion Zend/zend_compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -2118,6 +2118,7 @@ ZEND_API int zend_is_smart_branch(const zend_op *opline) /* {{{ */
case ZEND_ISSET_ISEMPTY_STATIC_PROP:
case ZEND_INSTANCEOF:
case ZEND_TYPE_CHECK:
case ZEND_IS_RESOURCE:
case ZEND_DEFINED:
case ZEND_IN_ARRAY:
case ZEND_ARRAY_KEY_EXISTS:
Expand Down Expand Up @@ -3461,6 +3462,20 @@ static int zend_compile_func_is_scalar(znode *result, zend_ast_list *args) /* {{
return SUCCESS;
}

int zend_compile_func_is_resource(znode *result, zend_ast_list *args) /* {{{ */
{
znode arg_node;

if (args->children != 1) {
return FAILURE;
}

zend_compile_expr(&arg_node, args->child[0]);
zend_emit_op_tmp(result, ZEND_IS_RESOURCE, &arg_node, NULL);
return SUCCESS;
}
/* }}} */

int zend_compile_func_cast(znode *result, zend_ast_list *args, uint32_t type) /* {{{ */
{
znode arg_node;
Expand Down Expand Up @@ -3974,7 +3989,7 @@ int zend_try_compile_special_func(znode *result, zend_string *lcname, zend_ast_l
} else if (zend_string_equals_literal(lcname, "is_object")) {
return zend_compile_func_typecheck(result, args, IS_OBJECT);
} else if (zend_string_equals_literal(lcname, "is_resource")) {
return zend_compile_func_typecheck(result, args, IS_RESOURCE);
return zend_compile_func_is_resource(result, args);
} else if (zend_string_equals_literal(lcname, "is_scalar")) {
return zend_compile_func_is_scalar(result, args);
} else if (zend_string_equals_literal(lcname, "boolval")) {
Expand Down
41 changes: 32 additions & 9 deletions Zend/zend_vm_def.h
Original file line number Diff line number Diff line change
Expand Up @@ -7932,16 +7932,9 @@ ZEND_VM_HOT_NOCONST_HANDLER(123, ZEND_TYPE_CHECK, CONST|TMPVAR|CV, ANY, TYPE_MAS

value = GET_OP1_ZVAL_PTR_UNDEF(BP_VAR_R);
if ((opline->extended_value >> (uint32_t)Z_TYPE_P(value)) & 1) {
ZEND_VM_C_LABEL(type_check_resource):
if (opline->extended_value != MAY_BE_RESOURCE
|| EXPECTED(NULL != zend_rsrc_list_get_rsrc_type(Z_RES_P(value)))) {
result = 1;
}
result = 1;
} else if ((OP1_TYPE & (IS_CV|IS_VAR)) && Z_ISREF_P(value)) {
value = Z_REFVAL_P(value);
if ((opline->extended_value >> (uint32_t)Z_TYPE_P(value)) & 1) {
ZEND_VM_C_GOTO(type_check_resource);
}
result = (opline->extended_value >> (uint32_t)Z_TYPE_P(Z_REFVAL_P(value))) & 1;
} else if (OP1_TYPE == IS_CV && UNEXPECTED(Z_TYPE_P(value) == IS_UNDEF)) {
result = ((1 << IS_NULL) & opline->extended_value) != 0;
SAVE_OPLINE();
Expand All @@ -7960,6 +7953,36 @@ ZEND_VM_C_LABEL(type_check_resource):
}
}

ZEND_VM_HOT_NOCONST_HANDLER(195, ZEND_IS_RESOURCE, CONST|TMPVAR|CV, ANY, TYPE_MASK)
{
USE_OPLINE
zval *value;

value = GET_OP1_ZVAL_PTR_UNDEF(BP_VAR_R);
ZEND_VM_C_LABEL(type_check_resource):
if (EXPECTED(Z_TYPE_P(value) == IS_RESOURCE)) {
int result = EXPECTED(NULL != zend_rsrc_list_get_rsrc_type(Z_RES_P(value)));
SAVE_OPLINE();
FREE_OP1();
ZEND_VM_SMART_BRANCH(result, 1);
} else if ((OP1_TYPE & (IS_CV|IS_VAR)) && Z_ISREF_P(value)) {
value = Z_REFVAL_P(value);
ZEND_VM_C_GOTO(type_check_resource);
} else if (OP1_TYPE == IS_CV && UNEXPECTED(Z_TYPE_P(value) == IS_UNDEF)) {
SAVE_OPLINE();
ZVAL_UNDEFINED_OP1();
if (UNEXPECTED(EG(exception))) {
/* I'm not sure why ZEND_TYPE_CHECK did this. */
ZVAL_UNDEF(EX_VAR(opline->result.var));
HANDLE_EXCEPTION();
}
ZEND_VM_SMART_BRANCH(0, 0);
}
SAVE_OPLINE();
FREE_OP1();
ZEND_VM_SMART_BRANCH(0, 1);
}

ZEND_VM_HOT_HANDLER(122, ZEND_DEFINED, CONST, ANY, CACHE_SLOT)
{
USE_OPLINE
Expand Down
Loading