Skip to content

Commit 9cb3264

Browse files
committed
Clean up constructor handling in com_dotnet
We substitute the construction magic with standard constructors, move the ZPP checks to the beginning of the ctors, and also let the function entries be generated from the stubs.
1 parent 5076507 commit 9cb3264

File tree

9 files changed

+149
-123
lines changed

9 files changed

+149
-123
lines changed

ext/com_dotnet/com_com.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
#include "Zend/zend_exceptions.h"
2727

2828
/* {{{ com_create_instance - ctor for COM class */
29-
PHP_FUNCTION(com_create_instance)
29+
PHP_METHOD(com, __construct)
3030
{
3131
zval *object = getThis();
3232
zval *server_params = NULL;
@@ -51,9 +51,6 @@ PHP_FUNCTION(com_create_instance)
5151
zend_long cp = GetACP();
5252
const struct php_win32_cp *cp_it;
5353

54-
php_com_initialize();
55-
obj = CDNO_FETCH(object);
56-
5754
if (FAILURE == zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET,
5855
ZEND_NUM_ARGS(), "s|s!ls",
5956
&module_name, &module_name_len, &server_name, &server_name_len,
@@ -65,6 +62,9 @@ PHP_FUNCTION(com_create_instance)
6562
RETURN_THROWS();
6663
}
6764

65+
php_com_initialize();
66+
obj = CDNO_FETCH(object);
67+
6868
cp_it = php_win32_cp_get_by_id((DWORD)cp);
6969
if (!cp_it) {
7070
php_com_throw_exception(E_INVALIDARG, "Could not create COM object - invalid codepage!");

ext/com_dotnet/com_dotnet.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ static HRESULT dotnet_init(char **p_where)
179179
}
180180

181181
/* {{{ com_dotnet_create_instance - ctor for DOTNET class */
182-
PHP_FUNCTION(com_dotnet_create_instance)
182+
PHP_METHOD(dotnet, __construct)
183183
{
184184
zval *object = getThis();
185185
php_com_dotnet_object *obj;
@@ -195,6 +195,13 @@ PHP_FUNCTION(com_dotnet_create_instance)
195195
zend_long cp = GetACP();
196196
const struct php_win32_cp *cp_it;
197197

198+
if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS(), "ss|l",
199+
&assembly_name, &assembly_name_len,
200+
&datatype_name, &datatype_name_len,
201+
&cp)) {
202+
RETURN_THROWS();
203+
}
204+
198205
php_com_initialize();
199206
stuff = (struct dotnet_runtime_stuff*)COMG(dotnet_runtime_stuff);
200207
if (stuff == NULL) {
@@ -237,13 +244,6 @@ PHP_FUNCTION(com_dotnet_create_instance)
237244

238245
obj = CDNO_FETCH(object);
239246

240-
if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS(), "ss|l",
241-
&assembly_name, &assembly_name_len,
242-
&datatype_name, &datatype_name_len,
243-
&cp)) {
244-
RETURN_THROWS();
245-
}
246-
247247
cp_it = php_win32_cp_get_by_id((DWORD)cp);
248248
if (!cp_it) {
249249
php_com_throw_exception(E_INVALIDARG, "Could not create .Net object - invalid codepage!");

ext/com_dotnet/com_extension.c

Lines changed: 16 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -37,40 +37,18 @@ zend_class_entry
3737
*php_com_exception_class_entry,
3838
*php_com_saproxy_class_entry;
3939

40-
static const zend_function_entry com_dotnet_functions[] = {
41-
PHP_FE(variant_set, arginfo_variant_set)
42-
PHP_FE(variant_add, arginfo_variant_add)
43-
PHP_FE(variant_cat, arginfo_variant_add)
44-
PHP_FE(variant_sub, arginfo_variant_add)
45-
PHP_FE(variant_mul, arginfo_variant_add)
46-
PHP_FE(variant_and, arginfo_variant_add)
47-
PHP_FE(variant_div, arginfo_variant_add)
48-
PHP_FE(variant_eqv, arginfo_variant_add)
49-
PHP_FE(variant_idiv, arginfo_variant_add)
50-
PHP_FE(variant_imp, arginfo_variant_add)
51-
PHP_FE(variant_mod, arginfo_variant_add)
52-
PHP_FE(variant_or, arginfo_variant_add)
53-
PHP_FE(variant_pow, arginfo_variant_add)
54-
PHP_FE(variant_xor, arginfo_variant_add)
55-
PHP_FE(variant_abs, arginfo_variant_abs)
56-
PHP_FE(variant_fix, arginfo_variant_fix)
57-
PHP_FE(variant_int, arginfo_variant_int)
58-
PHP_FE(variant_neg, arginfo_variant_neg)
59-
PHP_FE(variant_not, arginfo_variant_not)
60-
PHP_FE(variant_round, arginfo_variant_round)
61-
PHP_FE(variant_cmp, arginfo_variant_cmp)
62-
PHP_FE(variant_date_to_timestamp, arginfo_variant_date_to_timestamp)
63-
PHP_FE(variant_date_from_timestamp, arginfo_variant_date_from_timestamp)
64-
PHP_FE(variant_get_type, arginfo_variant_get_type)
65-
PHP_FE(variant_set_type, arginfo_variant_set_type)
66-
PHP_FE(variant_cast, arginfo_variant_cast)
67-
/* com_com.c */
68-
PHP_FE(com_create_guid, arginfo_com_create_guid)
69-
PHP_FE(com_event_sink, arginfo_com_event_sink)
70-
PHP_FE(com_print_typeinfo, arginfo_com_print_typeinfo)
71-
PHP_FE(com_message_pump, arginfo_com_message_pump)
72-
PHP_FE(com_load_typelib, arginfo_com_load_typelib)
73-
PHP_FE(com_get_active_object, arginfo_com_get_active_object)
40+
static const zend_function_entry com_variant_funcs[] = {
41+
PHP_ME(variant, __construct, arginfo_class_variant___construct, ZEND_ACC_PUBLIC)
42+
PHP_FE_END
43+
};
44+
45+
static const zend_function_entry com_com_funcs[] = {
46+
PHP_ME(com, __construct, arginfo_class_com___construct, ZEND_ACC_PUBLIC)
47+
PHP_FE_END
48+
};
49+
50+
static const zend_function_entry com_dotnet_funcs[] = {
51+
PHP_ME(dotnet, __construct, arginfo_class_dotnet___construct, ZEND_ACC_PUBLIC)
7452
PHP_FE_END
7553
};
7654

@@ -79,7 +57,7 @@ static const zend_function_entry com_dotnet_functions[] = {
7957
zend_module_entry com_dotnet_module_entry = {
8058
STANDARD_MODULE_HEADER,
8159
"com_dotnet",
82-
com_dotnet_functions,
60+
ext_functions,
8361
PHP_MINIT(com_dotnet),
8462
PHP_MSHUTDOWN(com_dotnet),
8563
PHP_RINIT(com_dotnet),
@@ -218,22 +196,22 @@ PHP_MINIT_FUNCTION(com_dotnet)
218196
/* php_com_saproxy_class_entry->constructor->common.fn_flags |= ZEND_ACC_PROTECTED; */
219197
php_com_saproxy_class_entry->get_iterator = php_com_saproxy_iter_get;
220198

221-
INIT_CLASS_ENTRY(ce, "variant", NULL);
199+
INIT_CLASS_ENTRY(ce, "variant", com_variant_funcs);
222200
ce.create_object = php_com_object_new;
223201
php_com_variant_class_entry = zend_register_internal_class(&ce);
224202
php_com_variant_class_entry->get_iterator = php_com_iter_get;
225203
php_com_variant_class_entry->serialize = zend_class_serialize_deny;
226204
php_com_variant_class_entry->unserialize = zend_class_unserialize_deny;
227205

228-
INIT_CLASS_ENTRY(ce, "com", NULL);
206+
INIT_CLASS_ENTRY(ce, "com", com_com_funcs);
229207
ce.create_object = php_com_object_new;
230208
tmp = zend_register_internal_class_ex(&ce, php_com_variant_class_entry);
231209
tmp->get_iterator = php_com_iter_get;
232210
tmp->serialize = zend_class_serialize_deny;
233211
tmp->unserialize = zend_class_unserialize_deny;
234212

235213
#if HAVE_MSCOREE_H
236-
INIT_CLASS_ENTRY(ce, "dotnet", NULL);
214+
INIT_CLASS_ENTRY(ce, "dotnet", com_dotnet_funcs);
237215
ce.create_object = php_com_object_new;
238216
tmp = zend_register_internal_class_ex(&ce, php_com_variant_class_entry);
239217
tmp->get_iterator = php_com_iter_get;

ext/com_dotnet/com_extension.stub.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
/** @generate-function-entries */
4+
35
function variant_set(variant $variant, $value): void {}
46

57
function variant_add($left, $right): variant {}
@@ -64,3 +66,21 @@ function com_print_typeinfo($comobject, ?string $dispinterface = null, bool $wan
6466
function com_message_pump(int $timeoutms = 0): bool {}
6567

6668
function com_load_typelib(string $typelib_name, bool $case_insensitive = true): bool {}
69+
70+
class variant
71+
{
72+
public function __construct($value = null, int $type = VT_EMPTY, int $codepage = CP_ACP) {}
73+
}
74+
75+
class com
76+
{
77+
/** @param string|array|null $server_name */
78+
public function __construct(string $module_name, $server_name = UNKOWN, int $codepage = CP_ACP, string $typelib = "") {}
79+
}
80+
81+
#if HAVE_MSCOREE_H
82+
class dotnet
83+
{
84+
public function __construct(string $assembly_name, string $datatype_name, int $codepage = CP_ACP) {}
85+
}
86+
#endif

ext/com_dotnet/com_extension_arginfo.h

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,3 +108,95 @@ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_com_load_typelib, 0, 1, _IS_BOOL
108108
ZEND_ARG_TYPE_INFO(0, typelib_name, IS_STRING, 0)
109109
ZEND_ARG_TYPE_INFO(0, case_insensitive, _IS_BOOL, 0)
110110
ZEND_END_ARG_INFO()
111+
112+
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_variant___construct, 0, 0, 0)
113+
ZEND_ARG_INFO(0, value)
114+
ZEND_ARG_TYPE_INFO(0, type, IS_LONG, 0)
115+
ZEND_ARG_TYPE_INFO(0, codepage, IS_LONG, 0)
116+
ZEND_END_ARG_INFO()
117+
118+
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_com___construct, 0, 0, 1)
119+
ZEND_ARG_TYPE_INFO(0, module_name, IS_STRING, 0)
120+
ZEND_ARG_INFO(0, server_name)
121+
ZEND_ARG_TYPE_INFO(0, codepage, IS_LONG, 0)
122+
ZEND_ARG_TYPE_INFO(0, typelib, IS_STRING, 0)
123+
ZEND_END_ARG_INFO()
124+
125+
#if HAVE_MSCOREE_H
126+
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_dotnet___construct, 0, 0, 2)
127+
ZEND_ARG_TYPE_INFO(0, assembly_name, IS_STRING, 0)
128+
ZEND_ARG_TYPE_INFO(0, datatype_name, IS_STRING, 0)
129+
ZEND_ARG_TYPE_INFO(0, codepage, IS_LONG, 0)
130+
ZEND_END_ARG_INFO()
131+
#endif
132+
133+
134+
ZEND_FUNCTION(variant_set);
135+
ZEND_FUNCTION(variant_add);
136+
ZEND_FUNCTION(variant_cat);
137+
ZEND_FUNCTION(variant_sub);
138+
ZEND_FUNCTION(variant_mul);
139+
ZEND_FUNCTION(variant_and);
140+
ZEND_FUNCTION(variant_div);
141+
ZEND_FUNCTION(variant_eqv);
142+
ZEND_FUNCTION(variant_idiv);
143+
ZEND_FUNCTION(variant_imp);
144+
ZEND_FUNCTION(variant_mod);
145+
ZEND_FUNCTION(variant_or);
146+
ZEND_FUNCTION(variant_pow);
147+
ZEND_FUNCTION(variant_xor);
148+
ZEND_FUNCTION(variant_abs);
149+
ZEND_FUNCTION(variant_fix);
150+
ZEND_FUNCTION(variant_int);
151+
ZEND_FUNCTION(variant_neg);
152+
ZEND_FUNCTION(variant_not);
153+
ZEND_FUNCTION(variant_round);
154+
ZEND_FUNCTION(variant_cmp);
155+
ZEND_FUNCTION(variant_date_to_timestamp);
156+
ZEND_FUNCTION(variant_date_from_timestamp);
157+
ZEND_FUNCTION(variant_get_type);
158+
ZEND_FUNCTION(variant_set_type);
159+
ZEND_FUNCTION(variant_cast);
160+
ZEND_FUNCTION(com_get_active_object);
161+
ZEND_FUNCTION(com_create_guid);
162+
ZEND_FUNCTION(com_event_sink);
163+
ZEND_FUNCTION(com_print_typeinfo);
164+
ZEND_FUNCTION(com_message_pump);
165+
ZEND_FUNCTION(com_load_typelib);
166+
167+
168+
static const zend_function_entry ext_functions[] = {
169+
ZEND_FE(variant_set, arginfo_variant_set)
170+
ZEND_FE(variant_add, arginfo_variant_add)
171+
ZEND_FE(variant_cat, arginfo_variant_cat)
172+
ZEND_FE(variant_sub, arginfo_variant_sub)
173+
ZEND_FE(variant_mul, arginfo_variant_mul)
174+
ZEND_FE(variant_and, arginfo_variant_and)
175+
ZEND_FE(variant_div, arginfo_variant_div)
176+
ZEND_FE(variant_eqv, arginfo_variant_eqv)
177+
ZEND_FE(variant_idiv, arginfo_variant_idiv)
178+
ZEND_FE(variant_imp, arginfo_variant_imp)
179+
ZEND_FE(variant_mod, arginfo_variant_mod)
180+
ZEND_FE(variant_or, arginfo_variant_or)
181+
ZEND_FE(variant_pow, arginfo_variant_pow)
182+
ZEND_FE(variant_xor, arginfo_variant_xor)
183+
ZEND_FE(variant_abs, arginfo_variant_abs)
184+
ZEND_FE(variant_fix, arginfo_variant_fix)
185+
ZEND_FE(variant_int, arginfo_variant_int)
186+
ZEND_FE(variant_neg, arginfo_variant_neg)
187+
ZEND_FE(variant_not, arginfo_variant_not)
188+
ZEND_FE(variant_round, arginfo_variant_round)
189+
ZEND_FE(variant_cmp, arginfo_variant_cmp)
190+
ZEND_FE(variant_date_to_timestamp, arginfo_variant_date_to_timestamp)
191+
ZEND_FE(variant_date_from_timestamp, arginfo_variant_date_from_timestamp)
192+
ZEND_FE(variant_get_type, arginfo_variant_get_type)
193+
ZEND_FE(variant_set_type, arginfo_variant_set_type)
194+
ZEND_FE(variant_cast, arginfo_variant_cast)
195+
ZEND_FE(com_get_active_object, arginfo_com_get_active_object)
196+
ZEND_FE(com_create_guid, arginfo_com_create_guid)
197+
ZEND_FE(com_event_sink, arginfo_com_event_sink)
198+
ZEND_FE(com_print_typeinfo, arginfo_com_print_typeinfo)
199+
ZEND_FE(com_message_pump, arginfo_com_message_pump)
200+
ZEND_FE(com_load_typelib, arginfo_com_load_typelib)
201+
ZEND_FE_END
202+
};

ext/com_dotnet/com_handlers.c

Lines changed: 1 addition & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -387,38 +387,6 @@ static zend_function *com_method_get(zend_object **object_ptr, zend_string *name
387387
return NULL;
388388
}
389389

390-
static zend_function *com_constructor_get(zend_object *object)
391-
{
392-
php_com_dotnet_object *obj = (php_com_dotnet_object *) object;
393-
static zend_internal_function c, d, v;
394-
395-
#define POPULATE_CTOR(f, fn) \
396-
f.type = ZEND_INTERNAL_FUNCTION; \
397-
f.function_name = obj->ce->name; \
398-
f.scope = obj->ce; \
399-
f.arg_info = NULL; \
400-
f.num_args = 0; \
401-
f.fn_flags = 0; \
402-
f.handler = ZEND_FN(fn); \
403-
return (zend_function*)&f;
404-
405-
switch (obj->ce->name->val[0]) {
406-
#if HAVE_MSCOREE_H
407-
case 'd':
408-
POPULATE_CTOR(d, com_dotnet_create_instance);
409-
#endif
410-
411-
case 'c':
412-
POPULATE_CTOR(c, com_create_instance);
413-
414-
case 'v':
415-
POPULATE_CTOR(v, com_variant_create_instance);
416-
417-
default:
418-
return NULL;
419-
}
420-
}
421-
422390
static zend_string* com_class_name_get(const zend_object *object)
423391
{
424392
php_com_dotnet_object *obj = (php_com_dotnet_object *)object;
@@ -553,7 +521,7 @@ zend_object_handlers php_com_object_handlers = {
553521
com_dimension_delete,
554522
com_properties_get,
555523
com_method_get,
556-
com_constructor_get,
524+
zend_std_get_constructor,
557525
com_class_name_get,
558526
com_object_cast,
559527
com_object_count,

ext/com_dotnet/com_variant.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -425,14 +425,14 @@ PHP_COM_DOTNET_API int php_com_copy_variant(VARIANT *dstvar, VARIANT *srcvar)
425425
return php_com_copy_variant(V_VARIANTREF(dstvar), srcvar);
426426

427427
default:
428-
php_error_docref(NULL, E_WARNING, "variant->variant: failed to copy from 0x%x to 0x%x", V_VT(dstvar), V_VT(srcvar));
428+
php_error_docref(NULL, E_WARNING, "variant->__construct: failed to copy from 0x%x to 0x%x", V_VT(dstvar), V_VT(srcvar));
429429
ret = FAILURE;
430430
}
431431
return ret;
432432
}
433433

434434
/* {{{ com_variant_create_instance - ctor for new VARIANT() */
435-
PHP_FUNCTION(com_variant_create_instance)
435+
PHP_METHOD(variant, __construct)
436436
{
437437
/* VARTYPE == unsigned short */ zend_long vt = VT_EMPTY;
438438
zend_long codepage = CP_ACP;
@@ -446,14 +446,14 @@ PHP_FUNCTION(com_variant_create_instance)
446446
return;
447447
}
448448

449-
obj = CDNO_FETCH(object);
450-
451449
if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS(),
452450
"z!|ll", &zvalue, &vt, &codepage)) {
453451
RETURN_THROWS();
454452
}
455453

456454
php_com_initialize();
455+
obj = CDNO_FETCH(object);
456+
457457
if (ZEND_NUM_ARGS() == 3) {
458458
obj->code_page = (int)codepage;
459459
}

0 commit comments

Comments
 (0)