8 #include "zend_exceptions.h"
14 size_t psi_t_alignment(token_t t
)
16 #define PSI_ALIGNOF(T) case PSI_T_## T: return ALIGNOF_## T ##_T;
29 return ALIGNOF_DOUBLE
;
32 return ALIGNOF_VOID_P
;
35 EMPTY_SWITCH_DEFAULT_CASE();
40 size_t psi_t_size(token_t t
)
42 #define PSI_SIZEOF(T) case PSI_T_## T : return SIZEOF_## T ##_T;
61 EMPTY_SWITCH_DEFAULT_CASE();
66 int psi_internal_type(impl_type
*type
)
85 zend_internal_arg_info
*psi_internal_arginfo(impl
*impl
)
88 zend_internal_arg_info
*aip
;
89 zend_internal_function_info
*fi
;
91 aip
= calloc(impl
->func
->args
->count
+ 1 + !!impl
->func
->args
->vararg
.name
, sizeof(*aip
));
93 fi
= (zend_internal_function_info
*) &aip
[0];
95 fi
->required_num_args
= psi_num_min_args(impl
);
96 fi
->return_reference
= impl
->func
->return_reference
;
97 fi
->type_hint
= psi_internal_type(impl
->func
->return_type
);
99 if (impl
->func
->args
->vararg
.name
) {
100 impl_arg
*vararg
= impl
->func
->args
->vararg
.name
;
101 zend_internal_arg_info
*ai
= &aip
[impl
->func
->args
->count
];
103 ai
->name
= vararg
->var
->name
;
105 ai
->type_hint
= psi_internal_type(vararg
->type
);
106 if (vararg
->var
->reference
) {
107 ai
->pass_by_reference
= 1;
112 for (i
= 0; i
< impl
->func
->args
->count
; ++i
) {
113 impl_arg
*iarg
= impl
->func
->args
->args
[i
];
114 zend_internal_arg_info
*ai
= &aip
[i
+1];
116 ai
->name
= iarg
->var
->name
;
117 ai
->type_hint
= psi_internal_type(iarg
->type
);
118 if (iarg
->var
->reference
) {
119 ai
->pass_by_reference
= 1;
121 //if (iarg->var->reference || (iarg->def && iarg->def->type == PSI_T_NULL)) {
129 size_t psi_num_min_args(impl
*impl
)
131 size_t i
, n
= impl
->func
->args
->count
;
133 for (i
= 0; i
< impl
->func
->args
->count
; ++i
) {
134 if (impl
->func
->args
->args
[i
]->def
) {
142 static inline ZEND_RESULT_CODE
psi_parse_args(zend_execute_data
*execute_data
, impl
*impl
)
146 zend_error_handling zeh
;
148 zend_replace_error_handling(EH_THROW
, zend_exception_get_default(), &zeh
);
150 if (!impl
->func
->args
->count
) {
153 rv
= zend_parse_parameters_none();
154 zend_restore_error_handling(&zeh
);
158 ZEND_PARSE_PARAMETERS_START(psi_num_min_args(impl
), impl
->func
->args
->vararg
.name
? -1 : impl
->func
->args
->count
)
160 if (impl
->func
->args
->vararg
.name
&& _i
>= impl
->func
->args
->count
) {
161 impl_arg
*varg
= impl
->func
->args
->vararg
.name
;
162 iarg
= init_impl_arg(
163 init_impl_type(varg
->type
->type
, varg
->type
->name
),
164 init_impl_var(varg
->var
->name
, varg
->var
->reference
),
168 if (_i
== impl
->func
->args
->count
) {
169 impl
->func
->args
->vararg
.args
= init_impl_args(iarg
);
171 add_impl_arg(impl
->func
->args
->vararg
.args
, iarg
);
174 iarg
= impl
->func
->args
->args
[_i
];
179 if (PSI_T_BOOL
== iarg
->type
->type
) {
180 Z_PARAM_BOOL(iarg
->val
.zend
.bval
);
181 } else if (PSI_T_INT
== iarg
->type
->type
) {
182 Z_PARAM_LONG(iarg
->val
.zend
.lval
);
183 } else if (PSI_T_FLOAT
== iarg
->type
->type
|| PSI_T_DOUBLE
== iarg
->type
->type
) {
184 Z_PARAM_DOUBLE(iarg
->val
.dval
);
185 } else if (PSI_T_STRING
== iarg
->type
->type
) {
186 Z_PARAM_STR_EX(iarg
->val
.zend
.str
, 1, iarg
->var
->reference
);
187 if (iarg
->val
.zend
.str
) {
188 zend_string_addref(iarg
->val
.zend
.str
);
190 } else if (PSI_T_ARRAY
== iarg
->type
->type
) {
192 } else if (PSI_T_OBJECT
== iarg
->type
->type
) {
194 } else if (PSI_T_MIXED
== iarg
->type
->type
) {
197 error_code
= ZPP_ERROR_FAILURE
;
201 ZVAL_DEREF(iarg
->_zv
);
202 if (_i
< _num_args
) {
205 ZEND_PARSE_PARAMETERS_END_EX(
206 zend_restore_error_handling(&zeh
);
210 /* set up defaults */
211 for (i
= 0; i
< impl
->func
->args
->count
; ++i
) {
212 if (i
>= EX_NUM_ARGS() && iarg
->def
) {
213 iarg
= impl
->func
->args
->args
[i
];
215 switch (iarg
->type
->type
) {
217 iarg
->val
.zend
.bval
= iarg
->def
->type
== PSI_T_TRUE
? 1 : 0;
220 iarg
->val
.zend
.lval
= zend_atol(iarg
->def
->text
, strlen(iarg
->def
->text
));
224 iarg
->val
.dval
= zend_strtod(iarg
->def
->text
, NULL
);
228 iarg
->val
.zend
.str
= zend_string_init(&iarg
->def
->text
[1], strlen(iarg
->def
->text
) - 2, 0);
234 zend_restore_error_handling(&zeh
);
238 static inline void *psi_do_calloc(let_calloc
*alloc
)
240 zend_long n
= psi_long_num_exp(alloc
->nmemb
, NULL
), s
= psi_long_num_exp(alloc
->size
, NULL
);
241 void *mem
= safe_emalloc(n
, s
, sizeof(void *));
242 memset(mem
, 0, n
* s
+ sizeof(void *));
244 fprintf(stderr
, "calloc: %p\n", mem
);
249 static inline impl_val
*psi_let_val(token_t let_func
, impl_arg
*iarg
, impl_val
*arg_val
, decl_struct
*strct
, void **to_free
)
253 if (iarg
->type
->type
== PSI_T_BOOL
) {
254 arg_val
->cval
= iarg
->val
.zend
.bval
;
256 arg_val
->cval
= zend_is_true(iarg
->_zv
);
260 if (iarg
->type
->type
== PSI_T_INT
) {
261 arg_val
->lval
= iarg
->val
.zend
.lval
;
263 arg_val
->lval
= zval_get_long(iarg
->_zv
);
267 if (iarg
->type
->type
== PSI_T_FLOAT
|| iarg
->type
->type
== PSI_T_DOUBLE
) {
268 arg_val
->dval
= iarg
->val
.dval
;
270 arg_val
->dval
= zval_get_double(iarg
->_zv
);
275 if (iarg
->type
->type
== PSI_T_STRING
) {
276 if (iarg
->val
.zend
.str
) {
277 arg_val
->ptr
= estrndup(iarg
->val
.zend
.str
->val
, iarg
->val
.zend
.str
->len
);
278 *to_free
= arg_val
->ptr
;
283 zend_string
*zs
= zval_get_string(iarg
->_zv
);
284 arg_val
->ptr
= estrdup(zs
->val
);
285 *to_free
= arg_val
->ptr
;
286 zend_string_release(zs
);
288 if (PSI_T_PATHVAL
== let_func
) {
289 if (SUCCESS
!= php_check_open_basedir(arg_val
->ptr
)) {
296 if (iarg
->type
->type
== PSI_T_STRING
) {
297 if (iarg
->val
.zend
.str
) {
298 arg_val
->lval
= iarg
->val
.zend
.str
->len
;
303 zend_string
*zs
= zval_get_string(iarg
->_zv
);
304 arg_val
->lval
= zs
->len
;
305 zend_string_release(zs
);
309 if (iarg
->type
->type
== PSI_T_ARRAY
) {
310 arg_val
= psi_array_to_struct(strct
, HASH_OF(iarg
->_zv
));
315 if (iarg
->type
->type
== PSI_T_OBJECT
) {
318 if (!instanceof_function(Z_OBJCE_P(iarg
->_zv
), psi_object_get_class_entry())) {
322 obj
= PSI_OBJ(iarg
->_zv
, NULL
);
323 arg_val
->ptr
= obj
->data
;
326 EMPTY_SWITCH_DEFAULT_CASE();
331 static inline void *psi_do_let(let_stmt
*let
)
333 decl_arg
*darg
= let
->var
->arg
;
334 impl_val
*arg_val
= darg
->ptr
;
337 switch (let
->val
? let
->val
->kind
: PSI_LET_NULL
) {
339 memcpy(arg_val
, deref_impl_val(let
->val
->data
.var
->arg
->let
->ptr
, let
->val
->data
.var
), sizeof(*arg_val
));
341 fprintf(stderr
, "LET TMP: %p -> %p\n",
342 let
->val
->data
.var
->arg
->let
->ptr
,
347 if (darg
->var
->array_size
) {
348 arg_val
->ptr
= ecalloc(darg
->var
->array_size
, sizeof(*arg_val
));
349 darg
->mem
= arg_val
->ptr
;
351 memset(arg_val
, 0, sizeof(*arg_val
));
355 arg_val
->ptr
= psi_do_calloc(let
->val
->data
.alloc
);
356 darg
->mem
= arg_val
->ptr
;
359 arg_val
->zend
.lval
= psi_long_num_exp(let
->val
->data
.num
, NULL
);
362 iarg
= let
->val
->data
.func
->arg
;
364 if (!(darg
->ptr
= psi_let_val(let
->val
->data
.func
->type
, iarg
, darg
->ptr
, real_decl_type(darg
->type
)->strct
, &darg
->mem
))) {
369 if (let
->val
&& let
->val
->flags
.one
.is_reference
) {
370 return let
->ptr
= &darg
->ptr
;
372 return let
->ptr
= darg
->ptr
;
376 static inline void psi_do_set(zval
*return_value
, set_value
*set
)
378 decl_arg
*set_arg
= set
->vars
->vars
[0]->arg
;
380 zval_dtor(return_value
);
381 set
->func
->handler(return_value
, set
, set_arg
->let
? set_arg
->let
->ptr
: set_arg
->ptr
);
384 static inline void psi_do_return(zval
*return_value
, return_stmt
*ret
)
386 ret
->set
->func
->handler(return_value
, ret
->set
, ret
->set
->vars
->vars
[0]->arg
->ptr
);
389 static inline void psi_do_free(free_stmt
*fre
)
393 for (i
= 0; i
< fre
->calls
->count
; ++i
) {
394 free_call
*f
= fre
->calls
->list
[i
];
396 for (j
= 0; j
< f
->vars
->count
; ++j
) {
397 decl_var
*dvar
= f
->vars
->vars
[j
];
398 decl_arg
*darg
= dvar
->arg
;
399 impl_val
*fval
= darg
->let
? darg
->let
->ptr
: darg
->ptr
;
401 f
->decl
->call
.args
[j
] = deref_impl_val(fval
, dvar
);
404 /* FIXME: check in validate_* that free functions return scalar */
405 PSI_ContextCall(&PSI_G(context
), &f
->decl
->call
, NULL
);
409 static inline void psi_clean_array_struct(decl_arg
*darg
) {
411 && darg
->let
->val
->kind
== PSI_LET_FUNC
412 && darg
->let
->val
->data
.func
->type
== PSI_T_ARRVAL
) {
413 decl_type
*type
= real_decl_type(darg
->type
);
415 if (type
->type
== PSI_T_STRUCT
) {
416 void **ptr
= (void **) ((char *) darg
->mem
+ type
->strct
->size
);
425 static inline void psi_do_clean(impl
*impl
)
429 if (impl
->decl
->func
->ptr
!= &impl
->decl
->func
->val
) {
430 efree(impl
->decl
->func
->ptr
);
431 impl
->decl
->func
->ptr
= &impl
->decl
->func
->val
;
433 for (i
= 0; i
< impl
->func
->args
->count
; ++i
) {
434 impl_arg
*iarg
= impl
->func
->args
->args
[i
];
436 switch (iarg
->type
->type
) {
438 if (iarg
->val
.zend
.str
) {
439 zend_string_release(iarg
->val
.zend
.str
);
445 if (impl
->decl
->args
) for (i
= 0; i
< impl
->decl
->args
->count
; ++i
) {
446 decl_arg
*darg
= impl
->decl
->args
->args
[i
];
449 psi_clean_array_struct(darg
);
453 darg
->ptr
= &darg
->val
;
456 if (impl
->func
->args
->vararg
.args
) {
457 free_impl_args(impl
->func
->args
->vararg
.args
);
458 impl
->func
->args
->vararg
.args
= NULL
;
460 if (impl
->func
->args
->vararg
.types
) {
461 efree(impl
->func
->args
->vararg
.types
);
462 impl
->func
->args
->vararg
.types
= NULL
;
464 if (impl
->func
->args
->vararg
.values
) {
465 efree(impl
->func
->args
->vararg
.values
);
466 impl
->func
->args
->vararg
.values
= NULL
;
468 if (impl
->func
->args
->vararg
.free_list
) {
469 void **list
= impl
->func
->args
->vararg
.free_list
;
475 efree(impl
->func
->args
->vararg
.free_list
);
476 impl
->func
->args
->vararg
.free_list
= NULL
;
481 static inline void psi_do_args(impl
*impl
) {
484 for (i
= 0; i
< impl
->decl
->args
->count
; ++i
) {
485 impl
->decl
->call
.args
[i
] = impl
->decl
->args
->args
[i
]->let
->ptr
;
488 if (!impl
->decl
->func
->var
->pointer_level
) {
489 decl_type
*real
= real_decl_type(impl
->decl
->func
->type
);
491 switch (real
->type
) {
493 impl
->decl
->func
->ptr
= psi_array_to_struct(real
->strct
, NULL
);
499 static inline impl_vararg
*psi_do_varargs(impl
*impl
) {
501 impl_vararg
*va
= &impl
->func
->args
->vararg
;
502 size_t vacount
= va
->args
->count
;
509 va
->types
= ecalloc(vacount
, sizeof(*va
->types
));
510 va
->values
= ecalloc(vacount
, sizeof(*va
->values
));
512 for (i
= 0, j
= 0; i
< vacount
; ++i
) {
513 impl_arg
*vaarg
= va
->args
->args
[i
];
514 void *to_free
= NULL
;
515 token_t let_fn
, vatype
= va
->name
->type
->type
;
517 if (vatype
== PSI_T_MIXED
) {
518 switch (Z_TYPE_P(vaarg
->_zv
)) {
520 case IS_FALSE
: vatype
= PSI_T_BOOL
; break;
521 case IS_LONG
: vatype
= PSI_T_INT
; break;
522 case IS_DOUBLE
: vatype
= PSI_T_FLOAT
; break;
523 default: vatype
= PSI_T_STRING
; break;
529 case PSI_T_BOOL
: let_fn
= PSI_T_BOOLVAL
; break;
530 case PSI_T_INT
: let_fn
= PSI_T_INTVAL
; break;
532 case PSI_T_DOUBLE
: let_fn
= PSI_T_FLOATVAL
;break;
533 case PSI_T_STRING
: let_fn
= PSI_T_STRVAL
; break;
534 EMPTY_SWITCH_DEFAULT_CASE();
537 va
->types
[i
] = vatype
;
538 /* FIXME: varargs with struct-by-value :) */
539 if (!psi_let_val(let_fn
, vaarg
, &va
->values
[i
], NULL
, &to_free
)) {
544 if (!va
->free_list
) {
545 va
->free_list
= ecalloc(vacount
- i
+ 1, sizeof(*va
->free_list
));
547 va
->free_list
[j
++] = to_free
;
554 void psi_call(zend_execute_data
*execute_data
, zval
*return_value
, impl
*impl
)
557 impl_vararg
*va
= NULL
;
559 memset(impl
->decl
->func
->ptr
, 0, sizeof(impl_val
));
561 if (SUCCESS
!= psi_parse_args(execute_data
, impl
)) {
565 for (i
= 0; i
< impl
->stmts
->let
.count
; ++i
) {
566 let_stmt
*let
= impl
->stmts
->let
.list
[i
];
568 if (!psi_do_let(let
)) {
569 psi_do_return(return_value
, impl
->stmts
->ret
.list
[0]);
575 if (impl
->decl
->args
) {
578 if (impl
->func
->args
->vararg
.args
) {
579 va
= psi_do_varargs(impl
);
583 PSI_ContextCall(&PSI_G(context
), &impl
->decl
->call
, va
);
584 psi_do_return(return_value
, impl
->stmts
->ret
.list
[0]);
586 for (i
= 0; i
< impl
->stmts
->set
.count
; ++i
) {
587 set_stmt
*set
= impl
->stmts
->set
.list
[i
];
590 psi_do_set(set
->arg
->_zv
, set
->val
);
594 for (i
= 0; i
< impl
->stmts
->fre
.count
; ++i
) {
595 free_stmt
*fre
= impl
->stmts
->fre
.list
[i
];