Fixed assert() in namesapaces

This commit is contained in:
Dmitry Stogov 2015-02-18 00:28:39 +03:00
parent 5eba069c28
commit 96de5ffc8d
6 changed files with 119 additions and 6 deletions

View File

@ -0,0 +1,24 @@
--TEST--
test enable/disable assertions at runtime (assertions not completely disabled)
--INI--
zend.assertions=0
assert.exception=0
--FILE--
<?php
ini_set("zend.assertions", 0);
var_dump(assert(false));
var_dump(assert(true));
ini_set("zend.assertions", 1);
var_dump(assert(false));
var_dump(assert(true));
ini_set("zend.assertions", -1);
?>
--EXPECTF--
bool(true)
bool(true)
Warning: assert(): assert(false) failed in %sexpect_016.php on line 6
NULL
bool(true)
Warning: zend.assertions may be completely enabled or disabled only in php.ini in %sexpect_016.php on line 8

View File

@ -0,0 +1,19 @@
--TEST--
test enable/disable assertions at runtime (assertions completely disabled)
--INI--
zend.assertions=-1
assert.exception=0
--FILE--
<?php
var_dump(assert(false));
var_dump(assert(true));
ini_set("zend.assertions", 0);
ini_set("zend.assertions", 1);
?>
--EXPECTF--
bool(true)
bool(true)
Warning: zend.assertions may be completely enabled or disabled only in php.ini in %sexpect_017.php on line 4
Warning: zend.assertions may be completely enabled or disabled only in php.ini in %sexpect_017.php on line 5

View File

@ -0,0 +1,33 @@
--TEST--
test assertions in namespace
--INI--
zend.assertions=1
assert.exception=0
--FILE--
<?php
namespace Foo;
ini_set("zend.assertions", 0);
var_dump(\assert(false));
var_dump(\assert(true));
var_dump(assert(false));
var_dump(assert(true));
ini_set("zend.assertions", 1);
var_dump(\assert(false));
var_dump(\assert(true));
var_dump(assert(false));
var_dump(assert(true));
?>
--EXPECTF--
bool(true)
bool(true)
bool(true)
bool(true)
Warning: assert(): assert(false) failed in %sexpect_018.php on line 10
NULL
bool(true)
Warning: assert(): assert(false) failed in %sexpect_018.php on line 12
NULL
bool(true)

View File

@ -0,0 +1,19 @@
--TEST--
test assertions in namespace (assertions completely disabled)
--INI--
zend.assertions=-1
assert.exception=0
--FILE--
<?php
namespace Foo;
var_dump(\assert(false));
var_dump(\assert(true));
var_dump(assert(false));
var_dump(assert(true));
?>
--EXPECTF--
bool(true)
bool(true)
bool(true)
bool(true)

View File

@ -116,6 +116,7 @@ static ZEND_INI_MH(OnUpdateAssertions) /* {{{ */
if (stage != ZEND_INI_STAGE_STARTUP &&
stage != ZEND_INI_STAGE_SHUTDOWN &&
*p != val &&
(*p < 0 || val < 0)) {
zend_error(E_WARNING, "zend.assertions may be completely enabled or disabled only in php.ini");
return FAILURE;

View File

@ -2817,19 +2817,26 @@ int zend_compile_func_cuf(znode *result, zend_ast_list *args, zend_string *lcnam
static int zend_compile_assert(znode *result, zend_ast_list *args, zend_string *lcname, zend_function *fbc) /* {{{ */
static int zend_compile_assert(znode *result, zend_ast_list *args, zend_string *name, zend_function *fbc) /* {{{ */
{
if (EG(assertions) >= 0) {
znode name_node;
znode name_node;
zend_op *opline;
uint32_t check_op_number = get_next_op_number(CG(active_op_array));
zend_emit_op(NULL, ZEND_ASSERT_CHECK, NULL, NULL);
name_node.op_type = IS_CONST;
ZVAL_STR_COPY(&name_node.u.constant, lcname);
if (fbc) {
name_node.op_type = IS_CONST;
ZVAL_STR_COPY(&name_node.u.constant, name);
opline = zend_emit_op(NULL, ZEND_INIT_FCALL, NULL, &name_node);
opline = zend_emit_op(NULL, ZEND_INIT_FCALL, NULL, &name_node);
} else {
opline = zend_emit_op(NULL, ZEND_INIT_NS_FCALL_BY_NAME, NULL, NULL);
opline->op2_type = IS_CONST;
opline->op2.constant = zend_add_ns_func_name_literal(
CG(active_op_array), name);
}
zend_alloc_cache_slot(opline->op2.constant);
if (args->children == 1 &&
@ -2844,6 +2851,12 @@ static int zend_compile_assert(znode *result, zend_ast_list *args, zend_string *
zend_compile_call_common(result, (zend_ast*)args, fbc);
CG(active_op_array)->opcodes[check_op_number].op2.opline_num = get_next_op_number(CG(active_op_array));
} else {
if (!fbc) {
zend_string_release(name);
}
result->op_type = IS_CONST;
ZVAL_TRUE(&result->u.constant);
}
return SUCCESS;
@ -2906,7 +2919,11 @@ void zend_compile_call(znode *result, zend_ast *ast, uint32_t type) /* {{{ */
{
zend_bool runtime_resolution = zend_compile_function_name(&name_node, name_ast);
if (runtime_resolution) {
zend_compile_ns_call(result, &name_node, args_ast);
if (zend_string_equals_literal_ci(zend_ast_get_str(name_ast), "assert")) {
zend_compile_assert(result, args_ast, Z_STR(name_node.u.constant), NULL);
} else {
zend_compile_ns_call(result, &name_node, args_ast);
}
return;
}
}