Skip to content

Commit 19ce7e0

Browse files
author
Thies C. Arntzen
committed
getting there
1 parent c89b7e1 commit 19ce7e0

File tree

1 file changed

+79
-5
lines changed

1 file changed

+79
-5
lines changed

ext/standard/assert.c

Lines changed: 79 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
typedef struct {
2727
int active;
2828
int exit;
29+
int warning;
2930
char *callback;
3031
} php_assert_globals;
3132

@@ -39,6 +40,8 @@ int assert_globals_id;
3940
php_assert_globals assert_globals;
4041
#endif
4142

43+
#define SAFE_STRING(s) ((s)?(s):"")
44+
4245
PHP_MINIT_FUNCTION(assert);
4346
PHP_MSHUTDOWN_FUNCTION(assert);
4447
PHP_RINIT_FUNCTION(assert);
@@ -69,13 +72,15 @@ zend_module_entry assert_module_entry = {
6972
#define ASSERT_ACTIVE 1
7073
#define ASSERT_CALLBACK 2
7174
#define ASSERT_EXIT 3
75+
#define ASSERT_WARNING 4
7276

7377
#ifdef ZTS
7478
static void php_assert_init_globals(php_assert_globals *assert_globals)
7579
{
7680
ASSERT(active) = 0;
7781
ASSERT(exit) = 0;
7882
ASSERT(callback) = 0;
83+
ASSERT(warning) = 1;
7984
}
8085
#endif
8186

@@ -88,17 +93,24 @@ PHP_MINIT_FUNCTION(assert)
8893
ASSERT(active) = 0;
8994
ASSERT(exit) = 0;
9095
ASSERT(callback) = 0;
96+
ASSERT(warning) = 1;
9197
#endif
9298

9399
REGISTER_LONG_CONSTANT("ASSERT_ACTIVE", ASSERT_ACTIVE, CONST_CS|CONST_PERSISTENT);
94100
REGISTER_LONG_CONSTANT("ASSERT_CALLBACK", ASSERT_CALLBACK, CONST_CS|CONST_PERSISTENT);
95101
REGISTER_LONG_CONSTANT("ASSERT_EXIT", ASSERT_EXIT, CONST_CS|CONST_PERSISTENT);
102+
REGISTER_LONG_CONSTANT("ASSERT_WARNING", ASSERT_WARNING, CONST_CS|CONST_PERSISTENT);
96103

97104
return SUCCESS;
98105
}
99106

100107
PHP_MSHUTDOWN_FUNCTION(assert)
101108
{
109+
if (ASSERT(callback)) {
110+
efree(ASSERT(callback));
111+
ASSERT(callback) = NULL;
112+
}
113+
102114
return SUCCESS;
103115
}
104116

@@ -109,6 +121,13 @@ PHP_RINIT_FUNCTION(assert)
109121

110122
PHP_RSHUTDOWN_FUNCTION(assert)
111123
{
124+
ASSERTLS_FETCH();
125+
126+
if (ASSERT(callback)) {
127+
efree(ASSERT(callback));
128+
ASSERT(callback) = NULL;
129+
}
130+
112131
return SUCCESS;
113132
}
114133

@@ -126,7 +145,9 @@ PHP_FUNCTION(assert)
126145
{
127146
pval **assertion;
128147
int val;
129-
char *myeval = empty_string;
148+
char *myeval = NULL;
149+
CLS_FETCH();
150+
ELS_FETCH();
130151
ASSERTLS_FETCH();
131152

132153
if (! ASSERT(active)) {
@@ -139,8 +160,6 @@ PHP_FUNCTION(assert)
139160

140161
if ((*assertion)->type == IS_STRING) {
141162
zval retval;
142-
CLS_FETCH();
143-
ELS_FETCH();
144163

145164
myeval = (*assertion)->value.str.val;
146165
zend_eval_string(myeval, &retval CLS_CC ELS_CC);
@@ -154,7 +173,53 @@ PHP_FUNCTION(assert)
154173
if (val) {
155174
RETURN_TRUE;
156175
}
157-
php_error(E_WARNING,"Assertion \"%s\" failed",myeval);
176+
177+
if (ASSERT(callback)) {
178+
zval *args[5];
179+
zval *retval;
180+
int i;
181+
uint lineno = zend_get_executed_lineno(ELS_C);
182+
char *filename = zend_get_executed_filename(ELS_C);
183+
/*
184+
char *function = get_active_function_name();
185+
*/
186+
187+
MAKE_STD_ZVAL(args[0]);
188+
MAKE_STD_ZVAL(args[1]);
189+
MAKE_STD_ZVAL(args[2]);
190+
MAKE_STD_ZVAL(args[3]);
191+
/*
192+
MAKE_STD_ZVAL(args[4]);
193+
*/
194+
195+
args[0]->type = IS_STRING; args[0]->value.str.val = estrdup(SAFE_STRING(ASSERT(callback))); args[0]->value.str.len = strlen(args[0]->value.str.val);
196+
args[1]->type = IS_STRING; args[1]->value.str.val = estrdup(SAFE_STRING(filename)); args[1]->value.str.len = strlen(args[1]->value.str.val);
197+
args[2]->type = IS_LONG; args[2]->value.lval = lineno;
198+
args[3]->type = IS_STRING; args[3]->value.str.val = estrdup(SAFE_STRING(myeval)); args[3]->value.str.len = strlen(args[3]->value.str.val);
199+
/*
200+
this is always "assert" so it's useless
201+
args[4]->type = IS_STRING; args[4]->value.str.val = estrdup(SAFE_STRING(function)); args[4]->value.str.len = strlen(args[4]->value.str.val);
202+
*/
203+
204+
MAKE_STD_ZVAL(retval);
205+
retval->type = IS_BOOL;
206+
retval->value.lval = 0;
207+
208+
call_user_function(CG(function_table), NULL, args[0], retval, 3, args+1);
209+
210+
for (i = 0; i < 4; i++) {
211+
zval_del_ref(&(args[i]));
212+
}
213+
zval_del_ref(&retval);
214+
}
215+
216+
if (ASSERT(warning)) {
217+
if (myeval) {
218+
php_error(E_WARNING,"Assertion \"%s\" failed",myeval);
219+
} else {
220+
php_error(E_WARNING,"Assertion failed");
221+
}
222+
}
158223

159224
if (ASSERT(exit)) {
160225
zend_bailout();
@@ -198,9 +263,18 @@ PHP_FUNCTION(assert_options)
198263
RETURN_LONG(oldint);
199264
break;
200265

266+
case ASSERT_WARNING:
267+
oldint = ASSERT(warning);
268+
if (ac == 2) {
269+
convert_to_long_ex(value);
270+
ASSERT(warning) = (*value)->value.lval;
271+
}
272+
RETURN_LONG(oldint);
273+
break;
274+
201275
case ASSERT_CALLBACK:
202276
oldstr = ASSERT(callback);
203-
RETVAL_STRING(oldstr,1);
277+
RETVAL_STRING(SAFE_STRING(oldstr),1);
204278

205279
if (ac == 2) {
206280
convert_to_string_ex(value);

0 commit comments

Comments
 (0)