Skip to content

Commit 2f76939

Browse files
author
Thies C. Arntzen
committed
@- Added support for a C-like assert() function. (Thies)
1 parent 19ce7e0 commit 2f76939

File tree

2 files changed

+32
-31
lines changed

2 files changed

+32
-31
lines changed

ext/standard/assert.c

Lines changed: 25 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,11 @@
2222

2323
#include "php.h"
2424
#include "php_assert.h"
25+
#include "php_ini.h"
2526

2627
typedef struct {
2728
int active;
28-
int exit;
29+
int bail;
2930
int warning;
3031
char *callback;
3132
} php_assert_globals;
@@ -71,68 +72,61 @@ zend_module_entry assert_module_entry = {
7172

7273
#define ASSERT_ACTIVE 1
7374
#define ASSERT_CALLBACK 2
74-
#define ASSERT_EXIT 3
75+
#define ASSERT_BAIL 3
7576
#define ASSERT_WARNING 4
7677

77-
#ifdef ZTS
78-
static void php_assert_init_globals(php_assert_globals *assert_globals)
79-
{
80-
ASSERT(active) = 0;
81-
ASSERT(exit) = 0;
82-
ASSERT(callback) = 0;
83-
ASSERT(warning) = 1;
84-
}
85-
#endif
78+
PHP_INI_BEGIN()
79+
STD_PHP_INI_BOOLEAN("assert.active", "0", PHP_INI_ALL, OnUpdateInt, active, php_assert_globals, assert_globals)
80+
STD_PHP_INI_BOOLEAN("assert.bail", "0", PHP_INI_ALL, OnUpdateInt, bail, php_assert_globals, assert_globals)
81+
STD_PHP_INI_BOOLEAN("assert.warning", "1", PHP_INI_ALL, OnUpdateInt, warning, php_assert_globals, assert_globals)
82+
PHP_INI_ENTRY("assert.callback", NULL, PHP_INI_ALL, NULL)
83+
PHP_INI_END()
8684

8785
PHP_MINIT_FUNCTION(assert)
8886
{
8987

9088
#ifdef ZTS
9189
assert_globals_id = ts_allocate_id(sizeof(php_assert_globals), (ts_allocate_ctor) php_assert_init_globals, NULL);
92-
#else
93-
ASSERT(active) = 0;
94-
ASSERT(exit) = 0;
95-
ASSERT(callback) = 0;
96-
ASSERT(warning) = 1;
9790
#endif
9891

92+
REGISTER_INI_ENTRIES();
93+
9994
REGISTER_LONG_CONSTANT("ASSERT_ACTIVE", ASSERT_ACTIVE, CONST_CS|CONST_PERSISTENT);
10095
REGISTER_LONG_CONSTANT("ASSERT_CALLBACK", ASSERT_CALLBACK, CONST_CS|CONST_PERSISTENT);
101-
REGISTER_LONG_CONSTANT("ASSERT_EXIT", ASSERT_EXIT, CONST_CS|CONST_PERSISTENT);
96+
REGISTER_LONG_CONSTANT("ASSERT_BAIL", ASSERT_BAIL, CONST_CS|CONST_PERSISTENT);
10297
REGISTER_LONG_CONSTANT("ASSERT_WARNING", ASSERT_WARNING, CONST_CS|CONST_PERSISTENT);
10398

10499
return SUCCESS;
105100
}
106101

107102
PHP_MSHUTDOWN_FUNCTION(assert)
108103
{
109-
if (ASSERT(callback)) {
110-
efree(ASSERT(callback));
111-
ASSERT(callback) = NULL;
112-
}
113-
114104
return SUCCESS;
115105
}
116106

117107
PHP_RINIT_FUNCTION(assert)
118108
{
109+
ASSERTLS_FETCH();
110+
111+
ASSERT(callback) = estrdup(INI_STR("assert.callback"));
112+
119113
return SUCCESS;
120114
}
121115

122116
PHP_RSHUTDOWN_FUNCTION(assert)
123117
{
124118
ASSERTLS_FETCH();
125-
126-
if (ASSERT(callback)) {
127-
efree(ASSERT(callback));
128-
ASSERT(callback) = NULL;
129-
}
119+
120+
if (ASSERT(callback)) efree(ASSERT(callback));
130121

131122
return SUCCESS;
132123
}
133124

134125
PHP_MINFO_FUNCTION(assert)
135126
{
127+
ASSERTLS_FETCH();
128+
129+
DISPLAY_INI_ENTRIES();
136130
}
137131

138132
/* }}} */
@@ -221,7 +215,7 @@ PHP_FUNCTION(assert)
221215
}
222216
}
223217

224-
if (ASSERT(exit)) {
218+
if (ASSERT(bail)) {
225219
zend_bailout();
226220
}
227221
}
@@ -254,11 +248,11 @@ PHP_FUNCTION(assert_options)
254248
RETURN_LONG(oldint);
255249
break;
256250

257-
case ASSERT_EXIT:
258-
oldint = ASSERT(exit);
251+
case ASSERT_BAIL:
252+
oldint = ASSERT(bail);
259253
if (ac == 2) {
260254
convert_to_long_ex(value);
261-
ASSERT(exit) = (*value)->value.lval;
255+
ASSERT(bail) = (*value)->value.lval;
262256
}
263257
RETURN_LONG(oldint);
264258
break;

php.ini-dist

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,3 +302,10 @@ mssql.max_links = -1 ; maximum number of links (persistent+non persistent).
302302
mssql.min_error_severity = 10 ; minimum error severity to display
303303
mssql.min_message_severity = 10 ; minimum message severity to display
304304
mssql.compatability_mode = Off ; compatability mode with old versions of PHP 3.0.
305+
306+
[Assertion]
307+
assert.active = Off ; assert(expr); does nothing by default
308+
assert.warning = On ; assert() will issue a PHP warning for each failed assertion.
309+
assert.bail = Off ; assert() will not bail out by default.
310+
assert.callback = 0 ; assert() will not call any user-defined PHP-Code by default.
311+

0 commit comments

Comments
 (0)