Skip to content

Commit bea832c

Browse files
committed
Don't check type of simple parameter default values
After fixing the int->double coercion case, this is already verified at compile-time, so there is no need to redo this type check on every call. Only perform the type check every time for the case of AST default values.
1 parent 2114867 commit bea832c

File tree

3 files changed

+25
-18
lines changed

3 files changed

+25
-18
lines changed

Zend/zend_compile.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5392,10 +5392,6 @@ void zend_compile_params(zend_ast *ast, zend_ast *return_type_ast) /* {{{ */
53925392
op_array->required_num_args = i + 1;
53935393
}
53945394

5395-
opline = zend_emit_op(NULL, opcode, NULL, &default_node);
5396-
SET_NODE(opline->result, &var_node);
5397-
opline->op1.num = i + 1;
5398-
53995395
arg_info = &arg_infos[i];
54005396
arg_info->name = zend_string_copy(name);
54015397
arg_info->pass_by_reference = is_ref;
@@ -5443,6 +5439,7 @@ void zend_compile_params(zend_ast *ast, zend_ast *return_type_ast) /* {{{ */
54435439
zend_error_noreturn(E_COMPILE_ERROR, "Default value for parameters "
54445440
"with a float type can only be float, integer, or NULL");
54455441
}
5442+
convert_to_double(&default_node.u.constant);
54465443
break;
54475444

54485445
case IS_ITERABLE:
@@ -5467,7 +5464,13 @@ void zend_compile_params(zend_ast *ast, zend_ast *return_type_ast) /* {{{ */
54675464
}
54685465
}
54695466
}
5467+
}
54705468

5469+
opline = zend_emit_op(NULL, opcode, NULL, &default_node);
5470+
SET_NODE(opline->result, &var_node);
5471+
opline->op1.num = i + 1;
5472+
5473+
if (type_ast) {
54715474
/* Allocate cache slot to speed-up run-time class resolution */
54725475
if (opline->opcode == ZEND_RECV_INIT) {
54735476
if (ZEND_TYPE_IS_CLASS(arg_info->type)) {

Zend/zend_vm_def.h

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5248,17 +5248,19 @@ ZEND_VM_HOT_HANDLER(64, ZEND_RECV_INIT, NUM, CONST, CACHE_SLOT)
52485248
ZVAL_COPY_VALUE(cache_val, param);
52495249
}
52505250
}
5251+
ZEND_VM_C_GOTO(recv_init_check_type);
52515252
} else {
52525253
ZVAL_COPY(param, default_value);
52535254
}
5254-
}
5255-
5256-
if (UNEXPECTED((EX(func)->op_array.fn_flags & ZEND_ACC_HAS_TYPE_HINTS) != 0)) {
5257-
zval *default_value = RT_CONSTANT(opline, opline->op2);
5255+
} else {
5256+
ZEND_VM_C_LABEL(recv_init_check_type):
5257+
if (UNEXPECTED((EX(func)->op_array.fn_flags & ZEND_ACC_HAS_TYPE_HINTS) != 0)) {
5258+
zval *default_value = RT_CONSTANT(opline, opline->op2);
52585259

5259-
SAVE_OPLINE();
5260-
if (UNEXPECTED(!zend_verify_recv_arg_type(EX(func), arg_num, param, default_value, CACHE_ADDR(opline->extended_value)))) {
5261-
HANDLE_EXCEPTION();
5260+
SAVE_OPLINE();
5261+
if (UNEXPECTED(!zend_verify_recv_arg_type(EX(func), arg_num, param, default_value, CACHE_ADDR(opline->extended_value)))) {
5262+
HANDLE_EXCEPTION();
5263+
}
52625264
}
52635265
}
52645266

Zend/zend_vm_execute.h

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3051,17 +3051,19 @@ static ZEND_VM_HOT ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL ZEND_RECV_INIT_SPEC_CON
30513051
ZVAL_COPY_VALUE(cache_val, param);
30523052
}
30533053
}
3054+
goto recv_init_check_type;
30543055
} else {
30553056
ZVAL_COPY(param, default_value);
30563057
}
3057-
}
3058-
3059-
if (UNEXPECTED((EX(func)->op_array.fn_flags & ZEND_ACC_HAS_TYPE_HINTS) != 0)) {
3060-
zval *default_value = RT_CONSTANT(opline, opline->op2);
3058+
} else {
3059+
recv_init_check_type:
3060+
if (UNEXPECTED((EX(func)->op_array.fn_flags & ZEND_ACC_HAS_TYPE_HINTS) != 0)) {
3061+
zval *default_value = RT_CONSTANT(opline, opline->op2);
30613062

3062-
SAVE_OPLINE();
3063-
if (UNEXPECTED(!zend_verify_recv_arg_type(EX(func), arg_num, param, default_value, CACHE_ADDR(opline->extended_value)))) {
3064-
HANDLE_EXCEPTION();
3063+
SAVE_OPLINE();
3064+
if (UNEXPECTED(!zend_verify_recv_arg_type(EX(func), arg_num, param, default_value, CACHE_ADDR(opline->extended_value)))) {
3065+
HANDLE_EXCEPTION();
3066+
}
30653067
}
30663068
}
30673069

0 commit comments

Comments
 (0)