flush
[m6w6/ext-psi] / src / compiler.c
1 #include <stdlib.h>
2
3 #include <jit/jit.h>
4
5 #include <php.h>
6 #include <Zend/Zend_API.h>
7
8 #include "compiler.h"
9
10 PSI_Compiler *PSI_CompilerInit(PSI_Compiler *C, PSI_Validator *V)
11 {
12 if (!C) {
13 C = malloc(sizeof(*C));
14 }
15 memset(C, 0, sizeof(*C));
16
17 PSI_DataExchange((PSI_Data *) C, (PSI_Data *) V);
18 return C;
19 }
20
21 typedef struct PSI_ClosureData {
22 impl *impl;
23 } PSI_ClosureData;
24
25 static inline size_t impl_num_min_args(impl *impl) {
26 size_t i, n = impl->func->args->count;
27
28 for (i = 0; i < impl->func->args->count; ++i) {
29 if (impl->func->args->args[i]->def) {
30 --n;
31 }
32 }
33 return n;
34 }
35 void PSI_Closure(jit_type_t signature, void *result, void **args, void *user_data)
36 {
37 zend_execute_data *execute_data = args[0];
38 zval *return_value = args[1];
39 PSI_ClosureData *data = user_data;
40 impl_arg *iarg;
41
42 if (!data->impl->func->args->count) {
43 if (SUCCESS != zend_parse_parameters_none()) {
44 return;
45 }
46 } else
47 ZEND_PARSE_PARAMETERS_START(impl_num_min_args(data->impl), data->impl->func->args->count)
48 nextarg:
49 iarg = data->impl->func->args->args[_i];
50 if (iarg->def) {
51 Z_PARAM_OPTIONAL;
52 }
53 if (PSI_T_BOOL == iarg->type->type) {
54 if (iarg->def) {
55 iarg->val.bval = iarg->def->type == PSI_T_TRUE ? 1 : 0;
56 }
57 Z_PARAM_BOOL(iarg->val.bval);
58 } else if (PSI_T_INT == iarg->type->type) {
59 if (iarg->def) {
60 iarg->val.lval = atol(iarg->def->text);
61 }
62 Z_PARAM_LONG(iarg->val.lval);
63 } else if (PSI_T_FLOAT == iarg->type->type) {
64 if (iarg->def) {
65 iarg->val.dval = strtod(iarg->def->text, NULL);
66 }
67 Z_PARAM_DOUBLE(iarg->val.dval);
68 } else if (PSI_T_STRING == iarg->type->type) {
69 if (iarg->def) {
70 /* FIXME */
71 iarg->val.str.len = strlen(iarg->def->text) - 2;
72 iarg->val.str.val = &iarg->def->text[1];
73 }
74 Z_PARAM_STRING(iarg->val.str.val, iarg->val.str.len);
75 } else {
76 error_code = ZPP_ERROR_FAILURE;
77 break;
78 }
79 goto nextarg;
80 ZEND_PARSE_PARAMETERS_END();
81 }