Skip to content

Commit a807dd3

Browse files
author
Thies C. Arntzen
committed
new assert() module. (Not yet finished!)
1 parent 512c641 commit a807dd3

File tree

5 files changed

+273
-24
lines changed

5 files changed

+273
-24
lines changed

ext/standard/Makefile.am

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ libphpext_standard_la_SOURCES=\
66
formatted_print.c fsock.c head.c html.c image.c info.c iptc.c lcg.c \
77
link.c mail.c math.c md5.c metaphone.c microtime.c pack.c pageinfo.c \
88
parsedate.y post.c quot_print.c rand.c reg.c soundex.c string.c \
9-
syslog.c type.c uniqid.c url.c url_scanner.c var.c output.c
9+
syslog.c type.c uniqid.c url.c url_scanner.c var.c output.c assert.c
1010

1111
$(srcdir)/url_scanner.c: $(srcdir)/url_scanner.re
1212
-re2c $< > $@.new && mv $@.new $@

ext/standard/assert.c

Lines changed: 230 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,230 @@
1+
/*
2+
+----------------------------------------------------------------------+
3+
| PHP version 4.0 |
4+
+----------------------------------------------------------------------+
5+
| Copyright (c) 1997, 1998, 1999 The PHP Group |
6+
+----------------------------------------------------------------------+
7+
| This source file is subject to version 2.0 of the PHP license, |
8+
| that is bundled with this package in the file LICENSE, and is |
9+
| available at through the world-wide-web at |
10+
| http://www.php.net/license/2_0.txt. |
11+
| If you did not receive a copy of the PHP license and are unable to |
12+
| obtain it through the world-wide-web, please send a note to |
13+
| license@php.net so we can mail you a copy immediately. |
14+
+----------------------------------------------------------------------+
15+
| Author: Thies C. Arntzen (thies@digicol.de) |
16+
+----------------------------------------------------------------------+
17+
*/
18+
19+
/* $Id$ */
20+
21+
/* {{{ includes/startup/misc */
22+
23+
#include "php.h"
24+
#include "php_assert.h"
25+
26+
typedef struct {
27+
int active;
28+
int exit;
29+
char *callback;
30+
} php_assert_globals;
31+
32+
#ifdef ZTS
33+
#define ASSERT(v) (assert_globals->v)
34+
#define ASSERTLS_FETCH() php_assert_globals *assert_globals = ts_resource(assert_globals_id)
35+
int assert_globals_id;
36+
#else
37+
#define ASSERT(v) (assert_globals.v)
38+
#define ASSERTLS_FETCH()
39+
php_assert_globals assert_globals;
40+
#endif
41+
42+
PHP_MINIT_FUNCTION(assert);
43+
PHP_MSHUTDOWN_FUNCTION(assert);
44+
PHP_RINIT_FUNCTION(assert);
45+
PHP_RSHUTDOWN_FUNCTION(assert);
46+
PHP_MINFO_FUNCTION(assert);
47+
48+
PHP_FUNCTION(assert);
49+
PHP_FUNCTION(assert_options);
50+
51+
static zend_function_entry php_assert_functions[] = {
52+
PHP_FE(assert, NULL)
53+
PHP_FE(assert_options, NULL)
54+
{NULL, NULL, NULL}
55+
};
56+
57+
58+
zend_module_entry assert_module_entry = {
59+
"Assertion",
60+
php_assert_functions,
61+
PHP_MINIT(assert),
62+
PHP_MSHUTDOWN(assert),
63+
PHP_RINIT(assert),
64+
PHP_RSHUTDOWN(assert),
65+
PHP_MINFO(assert),
66+
STANDARD_MODULE_PROPERTIES
67+
};
68+
69+
#define ASSERT_ACTIVE 1
70+
#define ASSERT_CALLBACK 2
71+
#define ASSERT_EXIT 3
72+
73+
#ifdef ZTS
74+
static void php_assert_init_globals(php_assert_globals *assert_globals)
75+
{
76+
ASSERT(active) = 0;
77+
ASSERT(exit) = 0;
78+
ASSERT(callback) = 0;
79+
}
80+
#endif
81+
82+
PHP_MINIT_FUNCTION(assert)
83+
{
84+
85+
#ifdef ZTS
86+
assert_globals_id = ts_allocate_id(sizeof(php_assert_globals), (ts_allocate_ctor) php_assert_init_globals, NULL);
87+
#else
88+
ASSERT(active) = 0;
89+
ASSERT(exit) = 0;
90+
ASSERT(callback) = 0;
91+
#endif
92+
93+
REGISTER_LONG_CONSTANT("ASSERT_ACTIVE", ASSERT_ACTIVE, CONST_CS|CONST_PERSISTENT);
94+
REGISTER_LONG_CONSTANT("ASSERT_CALLBACK", ASSERT_CALLBACK, CONST_CS|CONST_PERSISTENT);
95+
REGISTER_LONG_CONSTANT("ASSERT_EXIT", ASSERT_EXIT, CONST_CS|CONST_PERSISTENT);
96+
97+
return SUCCESS;
98+
}
99+
100+
PHP_MSHUTDOWN_FUNCTION(assert)
101+
{
102+
return SUCCESS;
103+
}
104+
105+
PHP_RINIT_FUNCTION(assert)
106+
{
107+
return SUCCESS;
108+
}
109+
110+
PHP_RSHUTDOWN_FUNCTION(assert)
111+
{
112+
return SUCCESS;
113+
}
114+
115+
PHP_MINFO_FUNCTION(assert)
116+
{
117+
}
118+
119+
/* }}} */
120+
/* {{{ internal functions */
121+
/* }}} */
122+
/* {{{ proto int assert(string|int assertion)
123+
checks if assertion is false */
124+
125+
PHP_FUNCTION(assert)
126+
{
127+
pval **assertion;
128+
int val;
129+
char *myeval = empty_string;
130+
ASSERTLS_FETCH();
131+
132+
if (! ASSERT(active)) {
133+
RETURN_TRUE;
134+
}
135+
136+
if (ARG_COUNT(ht) != 1 || getParametersEx(1, &assertion) == FAILURE) {
137+
WRONG_PARAM_COUNT;
138+
}
139+
140+
if ((*assertion)->type == IS_STRING) {
141+
zval retval;
142+
CLS_FETCH();
143+
ELS_FETCH();
144+
145+
myeval = (*assertion)->value.str.val;
146+
zend_eval_string(myeval, &retval CLS_CC ELS_CC);
147+
convert_to_boolean(&retval);
148+
val = retval.value.lval;
149+
} else {
150+
convert_to_boolean_ex(assertion);
151+
val = (*assertion)->value.lval;
152+
}
153+
154+
if (val) {
155+
RETURN_TRUE;
156+
}
157+
php_error(E_WARNING,"Assertion \"%s\" failed",myeval);
158+
159+
if (ASSERT(exit)) {
160+
zend_bailout();
161+
}
162+
}
163+
164+
/* }}} */
165+
/* {{{ proto mixed assert_options(int what,mixed value)
166+
set/get the various assert flags. */
167+
168+
PHP_FUNCTION(assert_options)
169+
{
170+
pval **what,**value;
171+
int oldint;
172+
char *oldstr;
173+
int ac = ARG_COUNT(ht);
174+
ASSERTLS_FETCH();
175+
176+
if (ac < 1 || ac > 2 || getParametersEx(ac, &what, &value) == FAILURE) {
177+
WRONG_PARAM_COUNT;
178+
}
179+
180+
convert_to_long_ex(what);
181+
182+
switch ((*what)->value.lval) {
183+
case ASSERT_ACTIVE:
184+
oldint = ASSERT(active);
185+
if (ac == 2) {
186+
convert_to_long_ex(value);
187+
ASSERT(active) = (*value)->value.lval;
188+
}
189+
RETURN_LONG(oldint);
190+
break;
191+
192+
case ASSERT_EXIT:
193+
oldint = ASSERT(exit);
194+
if (ac == 2) {
195+
convert_to_long_ex(value);
196+
ASSERT(exit) = (*value)->value.lval;
197+
}
198+
RETURN_LONG(oldint);
199+
break;
200+
201+
case ASSERT_CALLBACK:
202+
oldstr = ASSERT(callback);
203+
RETVAL_STRING(oldstr,1);
204+
205+
if (ac == 2) {
206+
convert_to_string_ex(value);
207+
ASSERT(callback) = estrndup((*value)->value.str.val,(*value)->value.str.len);
208+
}
209+
if (oldstr) {
210+
efree(oldstr);
211+
}
212+
return;
213+
break;
214+
215+
default:
216+
php_error(E_WARNING,"Unknown value %d.",(*what)->value.lval);
217+
break;
218+
}
219+
220+
RETURN_FALSE;
221+
}
222+
223+
/* }}} */
224+
225+
/*
226+
* Local variables:
227+
* tab-width: 4
228+
* c-basic-offset: 4
229+
* End:
230+
*/

ext/standard/php_assert.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
+----------------------------------------------------------------------+
3+
| PHP version 4.0 |
4+
+----------------------------------------------------------------------+
5+
| Copyright (c) 1997, 1998, 1999 The PHP Group |
6+
+----------------------------------------------------------------------+
7+
| This source file is subject to version 2.0 of the PHP license, |
8+
| that is bundled with this package in the file LICENSE, and is |
9+
| available at through the world-wide-web at |
10+
| http://www.php.net/license/2_0.txt. |
11+
| If you did not receive a copy of the PHP license and are unable to |
12+
| obtain it through the world-wide-web, please send a note to |
13+
| license@php.net so we can mail you a copy immediately. |
14+
+----------------------------------------------------------------------+
15+
| Author: Thies C. Arntzen (thies@digicol.de) |
16+
+----------------------------------------------------------------------+
17+
*/
18+
19+
/* $Id$ */
20+
21+
#ifndef _PHP_ASSERT_H
22+
#define _PHP_ASSERT_H
23+
24+
extern zend_module_entry assert_module_entry;
25+
#define phpext_assert_ptr &assert_module_entry
26+
27+
28+
#endif /* _PHP_ASSERT_H */

main/internal_functions.c.in

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,16 @@
1-
/*
1+
/*
22
+----------------------------------------------------------------------+
3-
| PHP HTML Embedded Scripting Language Version 3.0 |
3+
| PHP version 4.0 |
44
+----------------------------------------------------------------------+
5-
| Copyright (c) 1997,1998 PHP Development Team (See Credits file) |
5+
| Copyright (c) 1997, 1998, 1999 The PHP Group |
66
+----------------------------------------------------------------------+
7-
| This program is free software; you can redistribute it and/or modify |
8-
| it under the terms of one of the following licenses: |
9-
| |
10-
| A) the GNU General Public License as published by the Free Software |
11-
| Foundation; either version 2 of the License, or (at your option) |
12-
| any later version. |
13-
| |
14-
| B) the PHP License as published by the PHP Development Team and |
15-
| included in the distribution in the file: LICENSE |
16-
| |
17-
| This program is distributed in the hope that it will be useful, |
18-
| but WITHOUT ANY WARRANTY; without even the implied warranty of |
19-
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
20-
| GNU General Public License for more details. |
21-
| |
22-
| You should have received a copy of both licenses referred to here. |
23-
| If you did not, or have any questions about PHP licensing, please |
24-
| contact core@php.net. |
7+
| This source file is subject to version 2.0 of the PHP license, |
8+
| that is bundled with this package in the file LICENSE, and is |
9+
| available at through the world-wide-web at |
10+
| http://www.php.net/license/2_0.txt. |
11+
| If you did not receive a copy of the PHP license and are unable to |
12+
| obtain it through the world-wide-web, please send a note to |
13+
| license@php.net so we can mail you a copy immediately. |
2514
+----------------------------------------------------------------------+
2615
| Authors: Andi Gutmans <andi@zend.com> |
2716
| Zeev Suraski <zeev@zend.com> |
@@ -31,7 +20,6 @@
3120

3221
/* $Id$ */
3322

34-
3523
#include "php.h"
3624
#include "modules.h"
3725
#include "internal_functions_registry.h"
@@ -67,6 +55,7 @@ zend_module_entry *php3_builtin_modules[] = {
6755
phpext_metaphone_ptr,
6856
phpext_output_ptr,
6957
phpext_array_ptr,
58+
phpext_assert_ptr,
7059
@EXT_MODULE_PTRS@
7160
};
7261

main/internal_functions_win32.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
#include "ext/standard/php_lcg.h"
4848
#include "ext/standard/php_output.h"
4949
#include "ext/standard/php_array.h"
50+
#include "ext/standard/php_assert.h"
5051
#include "ext/COM/php_COM.h"
5152
#include "ext/standard/reg.h"
5253
#include "ext/pcre/php_pcre.h"
@@ -84,7 +85,8 @@ zend_module_entry *php3_builtin_modules[] = {
8485
phpext_lcg_ptr,
8586
phpext_session_ptr,
8687
phpext_output_ptr,
87-
phpext_array_ptr
88+
phpext_array_ptr,
89+
phpext_assert_ptr
8890
};
8991

9092

0 commit comments

Comments
 (0)