forked from mgdm/MFFI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mffi_function.c
157 lines (126 loc) · 3.81 KB
/
mffi_function.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <dlfcn.h>
#include <ffi.h>
#include "php.h"
#include "zend_exceptions.h"
#include "php_mffi.h"
#include "mffi_internal.h"
zend_class_entry *mffi_ce_function;
static zend_object_handlers mffi_function_object_handlers;
/* {{{ PHP_METHOD(MFFI_Func, __construct) */
PHP_METHOD(MFFI_Func, __construct)
{
zend_throw_exception(mffi_ce_exception, "MFFI\\Func cannot be constructed directly", 1);
}
/* }}} */
static void php_mffi_free_argument(php_mffi_value *arg, long type) {
if (type == PHP_MFFI_TYPE_STRING) {
efree(arg->s);
}
}
/* {{{ */
PHP_METHOD(MFFI_Func, __invoke)
{
zval *args = NULL;
long arg_count = 0, i = 0;
php_mffi_function_object *intern = NULL;
php_mffi_value ret_value, *values = NULL;
php_mffi_struct_object *obj = NULL;
void **arguments;
PHP_MFFI_ERROR_HANDLING();
if (zend_parse_parameters(ZEND_NUM_ARGS(), "*", &args, &arg_count) == FAILURE) {
PHP_MFFI_RESTORE_ERRORS();
return;
}
PHP_MFFI_RESTORE_ERRORS();
intern = php_mffi_function_fetch_object(Z_OBJ_P(getThis()));
if (arg_count != intern->arg_count) {
zend_throw_exception(mffi_ce_exception, "Incorrect argument count", 1);
return;
}
values = (php_mffi_value *) ecalloc(arg_count, sizeof(php_mffi_value));
arguments = (void **) ecalloc(1 + arg_count, sizeof(void *));
for (i = 0; i < arg_count; i++) {
switch (Z_TYPE(args[i])) {
case IS_OBJECT:
obj = php_mffi_struct_fetch_object(Z_OBJ(args[i]));
arguments[i] = &obj->data;
break;
default:
php_mffi_set_argument(&args[i], &values[i], intern->php_arg_types[i]);
arguments[i] = &values[i];
break;
}
}
ffi_call(&intern->cif, intern->function, &ret_value, arguments);
php_mffi_set_return_value(return_value, &ret_value, intern->php_return_type);
/* Free the string arguments */
for (i = 0; i < arg_count; i++) {
php_mffi_free_argument(&values[i], intern->php_arg_types[i]);
}
efree(values);
efree(arguments);
}
/* }}} */
/* {{{ */
const zend_function_entry mffi_function_methods[] = {
PHP_ME(MFFI_Func, __construct, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR|ZEND_ACC_FINAL)
PHP_ME(MFFI_Func, __invoke, NULL, ZEND_ACC_PUBLIC)
PHP_FE_END
};
/* }}} */
/* {{{ */
php_mffi_function_object *php_mffi_function_fetch_object(zend_object *obj) {
return (php_mffi_function_object *)((char*)(obj) - XtOffsetOf(php_mffi_function_object, std));
}
/* }}} */
/* {{{ */
static zend_object *mffi_function_object_new(zend_class_entry *ce)
{
php_mffi_function_object *object = ecalloc(1, sizeof(php_mffi_function_object) + zend_object_properties_size(ce));
zend_object_std_init(&object->std, ce);
object_properties_init(&object->std, ce);
object->std.handlers = &mffi_function_object_handlers;
return &object->std;
}
/* }}} */
/* {{{ */
static void mffi_function_object_free_storage(zend_object *object)
{
php_mffi_function_object *intern = php_mffi_function_fetch_object(object);
if (!intern) {
return;
}
if (intern->ffi_arg_types) {
efree(intern->ffi_arg_types);
}
if (intern->php_arg_types) {
efree(intern->php_arg_types);
}
zend_object_std_dtor(&intern->std);
}
/* }}} */
/* {{{ */
PHP_MINIT_FUNCTION(mffi_function)
{
zend_class_entry function_ce;
memcpy(&mffi_function_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
mffi_function_object_handlers.offset = XtOffsetOf(php_mffi_function_object, std);
mffi_function_object_handlers.free_obj = mffi_function_object_free_storage;
mffi_function_object_handlers.clone_obj = NULL;
INIT_NS_CLASS_ENTRY(function_ce, "MFFI", "Func", mffi_function_methods);
function_ce.create_object = mffi_function_object_new;
mffi_ce_function = zend_register_internal_class(&function_ce);
return SUCCESS;
}
/* }}} */
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* End:
* vim600: noet sw=4 ts=4 fdm=marker
* vim<600: noet sw=4 ts=4
*/