Skip to content

Commit 4e857ef

Browse files
author
Andrei Zmievski
committed
Changed ext_skel to use zend_parse_parameters() for argument parsing in
the generated functions. This cuts down on code a lot. The point before this change was tagged BEFORE_PARAM_PARSING_CHANGE.
1 parent dc2e122 commit 4e857ef

File tree

2 files changed

+41
-123
lines changed

2 files changed

+41
-123
lines changed

ext/ext_skel

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ givup() {
77

88
usage() {
99
echo "$0 --extname=module [--proto=file] [--stubs=file] [--xml[=file]]"
10-
echo " [--full-xml] [--no-help] [--assign-params [--string-lens]]"
10+
echo " [--full-xml] [--no-help]"
1111
echo ""
1212
echo " --extname=module module is the name of your extension"
1313
echo " --proto=file file contains prototypes of functions to create"
@@ -17,8 +17,6 @@ echo " --full-xml generate xml documentation for a self-contained exten
1717
echo " (not yet implemented)"
1818
echo " --no-help don't try to be nice and create comments in the code"
1919
echo " and helper functions to test if the module compiled"
20-
echo " --assign-params"
21-
echo " --string-lens"
2220
exit 1
2321
}
2422

@@ -56,23 +54,13 @@ while test $# -gt 0; do
5654
--no-help)
5755
no_help="yes"
5856
;;
59-
--assign-params)
60-
assign_params="yes"
61-
;;
62-
--string-lens)
63-
string_lens="yes"
64-
;;
6557
*)
6658
usage
6759
;;
6860
esac
6961
shift
7062
done
7163

72-
if test -z "$assign_params" && test -n "$string_lens"; then
73-
usage
74-
fi
75-
7664
if test -d "$extname" ; then
7765
givup "Directory $extname already exists."
7866
fi
@@ -96,7 +84,7 @@ if test -z "$stubs"; then
9684
fi
9785

9886
if test -n "$proto"; then
99-
cat $proto | awk -v extname=$extname -v stubs=$stubs -v stubfile=$stubfile -v xml=$xml -v full_xml=$full_xml -v i_know_what_to_do_shut_up_i_dont_need_your_help_mode=$no_help -v assign_params=$assign_params -v string_lens=$string_lens -f ./skeleton/create_stubs
87+
cat $proto | awk -v extname=$extname -v stubs=$stubs -v stubfile=$stubfile -v xml=$xml -v full_xml=$full_xml -v i_know_what_to_do_shut_up_i_dont_need_your_help_mode=$no_help -f ./skeleton/create_stubs
10088
fi
10189

10290
if test -z "$stubs"; then

ext/skeleton/create_stubs

Lines changed: 39 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -18,46 +18,23 @@ function convert(i, j, t)
1818

1919
for (i = 0; i < t; i++) { tabs = tabs "\t" }
2020

21-
if (type == "int") {
22-
x = tabs "convert_to_long_ex(" name ext ");\n" \
23-
(ext? tabs name " = Z_LVAL_PP(" name ext ");\n": "")
24-
ints = ints "\tint " name ";\n"
21+
if (type == "int" || type == "long") {
22+
longs = longs "\tlong " name ";\n"
2523
} else if (type == "bool") {
26-
x = tabs "convert_to_long_ex(" name ext ");\n" \
27-
(ext? tabs name " = Z_LVAL_PP(" name ext ");\n": "")
28-
ints = ints "\tint " name ";\n"
29-
} else if (type == "double") {
30-
x = tabs "convert_to_double_ex(" name ext ");\n" \
31-
(ext? tabs name " = Z_DVAL_PP(" name ext ");\n": "")
24+
bools = bools "\tzend_bool " name ";\n"
25+
} else if (type == "double" || type == "float") {
3226
doubles = doubles "\tdouble " name ";\n"
33-
} else if (type == "float") {
34-
x = tabs "convert_to_double_ex(" name ext ");\n" \
35-
(ext? tabs name " = (float) Z_DVAL_PP(" name ext ");\n": "")
36-
floats = floats "\tfloat " name ";\n"
3727
} else if (type == "string") {
38-
x = tabs "convert_to_string_ex(" name ext ");\n" \
39-
(ext? tabs name " = Z_STRVAL_PP(" name ext ");\n": "")
40-
(ext ? strings = strings "\tchar *" name " = NULL;\n" : 0)
41-
if (string_lens) {
42-
x = x tabs name "_len = Z_STRLEN_PP(" name ext ");\n"
43-
ints = ints "\tint " name "_len;\n"
44-
}
45-
} else if (type == "array") {
46-
x = "convert_to_array_ex(" name ext ");\n"
28+
strings = strings "\tchar *" name " = NULL;\n"
29+
ints = ints "\tint " name "_len;\n"
30+
} else if (type == "array" || type == "object" || type == "mixed") {
31+
zvals = zvals "\tzval *" name " = NULL;\n"
4732
} else if (type == "resource") {
48-
if (opt && i > -1) {
49-
resources = resources "\tif (argc < " j+1 ") {\n" \
50-
comment("\t\t/* Argument not given, do something before\n\t\t trying to fetch resource " name ". */\n") \
51-
"\t}\n\tZEND_FETCH_RESOURCE(???, ???, " name ext ", " name "_id, \"???\", ???_rsrc_id);\n"
52-
} else {
53-
resources = resources "\tZEND_FETCH_RESOURCE(???, ???, " name ext ", " name "_id, \"???\", ???_rsrc_id);\n"
54-
}
33+
zvals = zvals "\tzval *" name " = NULL;\n"
34+
resources = resources "\tif (" name ") {\n" \
35+
"\t\tZEND_FETCH_RESOURCE(???, ???, " name ", " name "_id, \"???\", ???_rsrc_id);\n\t}\n"
5536
ints = ints "\tint " name "_id = -1;\n"
56-
} else {
57-
x = comment(tabs "/* Write your own code here to handle argument " name ". */\n")
5837
}
59-
60-
if (x) return x
6138
}
6239

6340
function comment(s)
@@ -71,10 +48,16 @@ function comment(s)
7148

7249
BEGIN {
7350
name = "[_A-Za-z][_A-Za-z0-9]*"
74-
type = "int|double|float|string|bool|array|object|resource|mixed|void"
51+
type = "int|long|double|float|string|bool|array|object|resource|mixed|void"
52+
spec = "l|l|d|d|s|b|a|o|r|z|"
7553
num_funcs = 0
7654

77-
if (assign_params) ext = "_arg"
55+
# create a map from type name to the spec
56+
split(type, type_array, "\|")
57+
split(spec, spec_array, "\|")
58+
for (i in type_array) {
59+
spec_map[type_array[i]] = spec_array[i]
60+
}
7861

7962
if (xml && xml != "yes") {
8063
xmldoc = xml
@@ -111,8 +94,9 @@ BEGIN {
11194
}
11295

11396
{
114-
args_max = args_min = optional = i = 0
97+
args_max = args_min = optional = i = spec_opt = 0
11598
line = $0
99+
spec_str = "\""
116100

117101
func_type = gobble(type);
118102
func_name = gobble(name);
@@ -130,6 +114,7 @@ BEGIN {
130114
argnames[num_funcs,args_max] = arg_name
131115

132116
args_max++
117+
spec_str = spec_str spec_map[arg_type]
133118
if (optional) {
134119
optionals[num_funcs,i] = optional
135120
if (arg_type != "resource") {
@@ -140,12 +125,15 @@ BEGIN {
140125
}
141126

142127
if (x = gobble("\\[")) {
128+
if (!spec_opt) {
129+
spec_str = spec_str "|"
130+
spec_opt = 1
131+
}
143132
optional++
144133
}
145134

146135
y = gobble(",")
147136
if (!x && y && optional) {
148-
check_argc_in_switch[num_funcs] = 1
149137
grouped_optional_param[num_funcs,i] = 1
150138
}
151139
i++
@@ -159,10 +147,13 @@ BEGIN {
159147
fcomments[num_funcs] = line
160148
# }
161149

150+
spec_str = spec_str "\""
151+
162152
funcs[num_funcs] = func_name
163153
types[num_funcs] = func_type
164154
maxargs[num_funcs] = args_max
165155
minargs[num_funcs] = args_min
156+
specs[num_funcs] = spec_str
166157

167158
num_funcs++
168159
}
@@ -172,7 +163,7 @@ END {
172163
for (i = 0; i < num_funcs; i++) {
173164
compareargc = maxargs[i] - minargs[i]
174165
closefetch = fetchargs = zvals = xmlparams = funcvals = resources = handleargs = closeopts = ""
175-
ints = doubles = floats = strings = arrays = ""
166+
ints = longs = doubles = strings = bools = zvals = ""
176167

177168
proto = "/* {{{ proto " types[i] " " funcs[i] "("
178169

@@ -189,33 +180,18 @@ END {
189180
" <funcprototype>\n" \
190181
" <funcdef>" types[i] " <function>" funcs[i] "</function></funcdef>\n"
191182

192-
if (maxargs[i]>0) {
193-
zvals = "\tzval "
194-
if (compareargc) {
195-
if (minargs[i]) {
196-
fetchargs = "\tint argc = ZEND_NUM_ARGS();\n\tif (argc < " \
197-
minargs[i] " || argc > " maxargs[i] \
198-
" || zend_get_parameters_ex(argc, "
199-
} else {
200-
fetchargs = "\tint argc = ZEND_NUM_ARGS();\n\tif (argc > " \
201-
maxargs[i] " || (argc && zend_get_parameters_ex(argc, "
202-
closefetch = ")"
203-
}
204-
} else {
205-
fetchargs = "\tif (ZEND_NUM_ARGS() != " maxargs[i] \
206-
" || zend_get_parameters_ex(" maxargs[i] ", "
207-
}
208-
}
183+
fetchargs = "\tif (zend_parse_parameters(ZEND_NUM_ARGS, " specs[i] ", "
209184

210185
for (j = 0; j < maxargs[i]; j++) {
211186

212187
if (j) {
213-
zvals = zvals ", "
214188
fetchargs = fetchargs ", "
215189
}
216190

217-
zvals = zvals "**" argnames[i,j] ext
218-
fetchargs = fetchargs "&" argnames[i,j] ext
191+
fetchargs = fetchargs "&" argnames[i,j]
192+
if (argtypes[i,j] == "string") {
193+
fetchargs = fetchargs ", &" argnames[i,j] "_len"
194+
}
219195

220196
xmlparams = xmlparams " <paramdef>" argtypes[i,j]
221197
if (j > minargs[i]-1) {
@@ -237,56 +213,12 @@ END {
237213
if (j > 0) proto = proto ", "
238214
proto = proto argtypes[i,j] " " argnames[i,j]
239215

240-
# Clean up this mess...
241-
242-
if (useswitch[i]) {
243-
if (grouped_optional_param[i,j] && code) {
244-
handleargs = convert(i, j, 3) \
245-
((grouped_optional_param[i,j-1]) ? "" : comment("\t\t\t/* Fall-through. */\n")) \
246-
handleargs
247-
} else {
248-
if (j > minargs[i]-1) {
249-
if (code = convert(i, j, 3)) {
250-
handleargs = "\t\tcase " j+1 ":\n" code \
251-
((grouped_optional_param[i,j-1]) ? "" : comment("\t\t\t/* Fall-through. */\n")) \
252-
handleargs
253-
} else {
254-
handleargs = "\t\tcase " j+1 ":" \
255-
comment("\t/* Fall-through. */") \
256-
"\n" handleargs
257-
}
258-
} else if (j >= minargs[i]-1) {
259-
if (code = convert(i, j, 3)) {
260-
handleargs = "\t\tcase " j+1 ":\n" code handleargs
261-
} else {
262-
handleargs = "\t\tcase " j+1 ":\n" handleargs
263-
}
264-
} else {
265-
if (code = convert(i, j, 3)) handleargs = code handleargs
266-
}
267-
}
268-
} else {
269-
if (code = convert(i, j, 1)) handleargs = handleargs code
270-
}
216+
convert(i, j, 1)
271217
}
272218

273219
proto = proto closeopts ")\n " fcomments[i] " */\nPHP_FUNCTION(" funcs[i] ")\n{"
274-
if (maxargs[i]) {
275-
zvals = zvals ";"
276-
fetchargs = fetchargs ") == FAILURE)" closefetch " {\n\t\tZEND_WRONG_PARAM_COUNT();\n\t}\n"
277-
}
278-
if (assign_params) funcvals = ints doubles floats strings
279-
if (useswitch[i]) {
280-
if (check_argc_in_switch[i]) {
281-
check_argc = "\t\tdefault:\n\t\t\tZEND_WRONG_PARAM_COUNT();\n"
282-
} else {
283-
check_argc = ""
284-
}
285-
handleargs = "\tswitch (argc) {\n" \
286-
handleargs \
287-
(minargs[i] ? "" : "\t\tcase 0:\n") \
288-
"\t\t\tbreak;\n" check_argc "\t}"
289-
}
220+
fetchargs = fetchargs ") == FAILURE)" closefetch " \n\t\treturn;\n"
221+
funcvals = strings ints longs doubles bools zvals
290222
xmlstr = xmlstr xmlparams \
291223
" </funcprototype>\n" \
292224
" </funcsynopsis>\n" \
@@ -296,16 +228,14 @@ END {
296228
" </refentry>\n"
297229

298230
print proto > stubfile
299-
if (zvals) print zvals > stubfile
300231
if (funcvals) print funcvals > stubfile
301232
if (fetchargs) print fetchargs > stubfile
302233
if (resources) {
303234
print resources > stubfile
304235
if (!stubs) print "" > extname "/function_warning"
305236
}
306-
if (handleargs) print handleargs > stubfile
307237
if (!i_know_what_to_do_shut_up_i_dont_need_your_help_mode) {
308-
print "\n\tphp_error(E_WARNING, \"" funcs[i] ": not yet implemented\");" > stubfile
238+
print "\tphp_error(E_WARNING, \"" funcs[i] ": not yet implemented\");" > stubfile
309239
}
310240
print "}\n/* }}} */\n" > stubfile
311241

0 commit comments

Comments
 (0)