struct member fixes
[m6w6/ext-psi] / src / module.c
index 53079d0a1293771ea8b74001595224a7f6ef1350..5a44593af95a64401236d4a4766171ccd1e913ca 100644 (file)
@@ -11,8 +11,6 @@
 #include "zend_operators.h"
 
 #include "php_psi.h"
-#include "parser.h"
-#include "context.h"
 
 #if HAVE_LIBJIT
 # include "libjit.h"
@@ -25,6 +23,8 @@
 # define PSI_ENGINE "ffi"
 #endif
 
+#include <ndbm.h>
+
 ZEND_DECLARE_MODULE_GLOBALS(psi);
 
 PHP_INI_BEGIN()
@@ -35,16 +35,25 @@ PHP_INI_END();
 static zend_object_handlers psi_object_handlers;
 static zend_class_entry *psi_class_entry;
 
-void psi_error(int type, const char *msg, ...)
+void psi_error_wrapper(PSI_Token *t, int type, const char *msg, ...)
 {
-       char buf[0x1000];
        va_list argv;
 
        va_start(argv, msg);
-       vslprintf(buf, 0x1000, msg, argv);
+       psi_verror(type, t?t->file:"Unknown", t?*t->line:0, msg, argv);
        va_end(argv);
+}
+void psi_error(int type, const char *fn, unsigned ln, const char *msg, ...)
+{
+       va_list argv;
 
-       php_error(type, buf);
+       va_start(argv, msg);
+       psi_verror(type, fn, ln, msg, argv);
+       va_end(argv);
+}
+void psi_verror(int type, const char *fn, unsigned ln, const char *msg, va_list argv)
+{
+       zend_error_cb(type, fn, ln, msg, argv);
 }
 
 size_t psi_t_alignment(token_t t)
@@ -99,6 +108,15 @@ size_t psi_t_align(token_t t, size_t s)
        return ((s - 1) | (a - 1)) + 1;
 }
 
+size_t psi_offset_padding(size_t diff, size_t alignment)
+{
+       if (diff && diff <= ((diff - 1) | (alignment -1)) + 1) {
+               diff = 0;
+       }
+
+       return diff;
+}
+
 int psi_internal_type(impl_type *type)
 {
        switch (type->type) {
@@ -124,13 +142,27 @@ zend_internal_arg_info *psi_internal_arginfo(impl *impl)
        zend_internal_arg_info *aip;
        zend_internal_function_info *fi;
 
-       aip = calloc(impl->func->args->count + 1, sizeof(*aip));
+       aip = calloc(impl->func->args->count + 1 + !!impl->func->args->vararg.name, sizeof(*aip));
 
        fi = (zend_internal_function_info *) &aip[0];
+       fi->allow_null = 1;
        fi->required_num_args = psi_num_min_args(impl);
        fi->return_reference = impl->func->return_reference;
        fi->type_hint = psi_internal_type(impl->func->return_type);
 
+       if (impl->func->args->vararg.name) {
+               impl_arg *vararg = impl->func->args->vararg.name;
+               zend_internal_arg_info *ai = &aip[impl->func->args->count];
+
+               ai->name = vararg->var->name;
+               ai->allow_null = 1;
+               ai->type_hint = psi_internal_type(vararg->type);
+               if (vararg->var->reference) {
+                       ai->pass_by_reference = 1;
+               }
+               ai->is_variadic = 1;
+       }
+
        for (i = 0; i < impl->func->args->count; ++i) {
                impl_arg *iarg = impl->func->args->args[i];
                zend_internal_arg_info *ai = &aip[i+1];
@@ -140,9 +172,9 @@ zend_internal_arg_info *psi_internal_arginfo(impl *impl)
                if (iarg->var->reference) {
                        ai->pass_by_reference = 1;
                }
-               if (iarg->var->reference || (iarg->def && iarg->def->type == PSI_T_NULL)) {
+               //if (iarg->var->reference || (iarg->def && iarg->def->type == PSI_T_NULL)) {
                        ai->allow_null = 1;
-               }
+               //}
        }
 
        return aip;
@@ -273,37 +305,39 @@ void psi_to_double(zval *return_value, set_value *set, impl_val *ret_val)
 
 void psi_to_string(zval *return_value, set_value *set, impl_val *ret_val)
 {
+       char *str;
        decl_var *var = set->vars->vars[0];
        token_t t = real_decl_type(var->arg->type)->type;
 
        switch (t) {
-       case PSI_T_VOID:
-       case PSI_T_INT8:
-       case PSI_T_UINT8:
+       case PSI_T_FLOAT:
+               RETVAL_DOUBLE((double) deref_impl_val(ret_val, var)->fval);
+               break;
+       case PSI_T_DOUBLE:
+               RETVAL_DOUBLE(deref_impl_val(ret_val, var)->dval);
+               break;
+       default:
                if (!var->arg->var->pointer_level) {
                        RETVAL_STRINGL(&ret_val->cval, 1);
                } else {
                        ret_val = deref_impl_val(ret_val, var);
-                       if (ret_val && ret_val->ptr) {
+                       if (var->arg->var->array_size) {
+                               str = (char *) ret_val;
+                       } else {
+                               str = ret_val->ptr;
+                       }
+                       if (str) {
                                if (set->num) {
-                                       RETVAL_STRINGL(ret_val->ptr, psi_long_num_exp(set->num, NULL));
+                                       zend_long n = psi_long_num_exp(set->num, set->outer.val);
+                                       RETVAL_STRINGL(str, n);
                                } else {
-                                       RETVAL_STRING(ret_val->ptr);
+                                       RETVAL_STRING(str);
                                }
                        } else {
                                RETVAL_EMPTY_STRING();
                        }
                }
                return;
-       case PSI_T_FLOAT:
-               RETVAL_DOUBLE((double) deref_impl_val(ret_val, var)->fval);
-               break;
-       case PSI_T_DOUBLE:
-               RETVAL_DOUBLE(deref_impl_val(ret_val, var)->dval);
-               break;
-       default:
-               psi_to_int(return_value, set, ret_val);
-               break;
        }
        convert_to_string(return_value);
 }
@@ -312,7 +346,7 @@ void psi_to_string(zval *return_value, set_value *set, impl_val *ret_val)
 static impl_val *iterate(impl_val *val, size_t size, unsigned i, impl_val *tmp)
 {
        memset(tmp, 0, sizeof(*tmp));
-       memcpy(tmp, ((void*) val->ptr) + size * i, size);
+       memcpy(tmp, ((void*) val) + size * i, size);
        return tmp;
 }
 
@@ -327,6 +361,7 @@ void psi_from_zval(impl_val *mem, decl_arg *spec, zval *zv, void **tmp)
        case PSI_T_DOUBLE:
                mem->dval = zval_get_double(zv);
                break;
+       case PSI_T_VOID:
        case PSI_T_INT8:
        case PSI_T_UINT8:
                if (spec->var->pointer_level) {
@@ -366,6 +401,11 @@ void *psi_array_to_struct(decl_struct *s, HashTable *arr)
        return mem;
 }
 
+void psi_to_recursive(zval *return_value, set_value *set, impl_val *r_val)
+{
+       set->outer.set->func->handler(return_value, set, r_val);
+}
+
 void psi_to_array(zval *return_value, set_value *set, impl_val *r_val)
 {
        size_t i;
@@ -373,6 +413,10 @@ void psi_to_array(zval *return_value, set_value *set, impl_val *r_val)
        token_t t = real_decl_type(var->arg->type)->type;
        impl_val tmp, *ret_val = deref_impl_val(r_val, var);
 
+       if ((intptr_t) ret_val <= (intptr_t) 0) {
+               RETURN_NULL();
+       }
+
        array_init(return_value);
 
        if (t == PSI_T_STRUCT) {
@@ -384,13 +428,13 @@ void psi_to_array(zval *return_value, set_value *set, impl_val *r_val)
                                set_value *sub_set = set->inner[i];
                                decl_var *sub_var = sub_set->vars->vars[0];
 
-                               sub_set->outer.val = r_val;
+                               sub_set->outer.val = ret_val;
 
                                if (sub_var->arg) {
                                        impl_val *tmp = NULL, *val;
                                        zval ztmp;
 
-                                       val = deref_impl_val(struct_member_ref(sub_var->arg, ret_val, &tmp), sub_var);
+                                       val = struct_member_ref(sub_var->arg, ret_val, &tmp);
                                        sub_set->func->handler(&ztmp, sub_set, val);
                                        add_assoc_zval(return_value, sub_var->name, &ztmp);
 
@@ -425,53 +469,33 @@ void psi_to_array(zval *return_value, set_value *set, impl_val *r_val)
                        add_next_index_zval(return_value, &ele);
                }
                return;
-       } else if (set->vars->count > 1) {
-               /* to_array(arr_var, cnt_var[, cnt_var...], to_int(*arr_var))
-                * check for length in second var
-                * /
-               size_t count = 0;
-               zval ele;
-
-               if (set->outer.set) {
-                       / * struct *//*
-                       for (i = 1; i < set->vars->count; ++i) {
-                               impl_val *tmp = NULL, *cnt_val;
-                               decl_var *cnt_var = set->vars->vars[i];
-
-                               cnt_val = struct_member_ref(cnt_var->arg, set->outer.val, &tmp);
-                               count += deref_impl_val(cnt_val, cnt_var)->lval;
-
-                               if (tmp) {
-                                       free(tmp);
-                               }
-                       }
-               } else {
-                       ZEND_ASSERT(0);
-               }
-
-               for (i = 0; i < count; ++i) {
-                       size_t size = psi_t_size(var->arg->var->pointer_level ? PSI_T_POINTER : t);
-                       impl_val *ptr = iterate(ret_val, size, i, &tmp);
-
-                       set->inner[0]->func->handler(&ele, set->inner[0], ptr);
-                       add_next_index_zval(return_value, &ele);
-               }
-               */
        } else if (set->num) {
-               /* to_array(arr_var, num_expr, to_int(*arr_var))
-                */
+               /* to_array(arr_var, num_expr, to_int(*arr_var)) */
                zval ele;
+               char *ptr;
                zend_long i, n = psi_long_num_exp(set->num, set->outer.val);
+               size_t size = psi_t_size(var->arg->var->pointer_level ? PSI_T_POINTER : t);
+               set_value *sub_set = set->inner[0];
 
+               sub_set->outer.val = set->outer.val;
                for (i = 0; i < n; ++i) {
-                       size_t size = psi_t_size(var->arg->var->pointer_level ? PSI_T_POINTER : t);
-                       impl_val *ptr = iterate(ret_val, size, i, &tmp);
-
-                       set->inner[0]->func->handler(&ele, set->inner[0], ptr);
+                       ptr = (char *) ret_val->ptr + i * size;
+                       sub_set->func->handler(&ele, sub_set, (void *) ptr);
                        add_next_index_zval(return_value, &ele);
                }
        } else {
-               ZEND_ASSERT(0);
+               /* to_array(arr_var, to_int(*arr_var)) */
+               zval ele;
+               char *ptr = ret_val->ptr;
+               size_t size = psi_t_size(var->arg->var->pointer_level ? PSI_T_POINTER : t);
+               set_value *sub_set = set->inner[0];
+
+               sub_set->outer.val = set->outer.val;
+               while (*(void **) ptr) {
+                       sub_set->func->handler(&ele, sub_set, (void *) ptr);
+                       add_next_index_zval(return_value, &ele);
+                       ptr += size;
+               }
        }
 }
 
@@ -481,7 +505,7 @@ void psi_to_object(zval *return_value, set_value *set, impl_val *r_val)
        impl_val *ret_val = deref_impl_val(r_val, var);
        psi_object *obj;
 
-       if (ret_val->ptr) {
+       if ((intptr_t) ret_val->ptr > (intptr_t) 0) {
                object_init_ex(return_value, psi_class_entry);
                obj = PSI_OBJ(return_value, NULL);
                obj->data = ret_val->ptr;
@@ -493,7 +517,6 @@ void psi_to_object(zval *return_value, set_value *set, impl_val *r_val)
 static inline ZEND_RESULT_CODE psi_parse_args(zend_execute_data *execute_data, impl *impl)
 {
        size_t i;
-       zval *zarg = ZEND_CALL_ARG(execute_data, 0);
        impl_arg *iarg;
        zend_error_handling zeh;
 
@@ -507,74 +530,50 @@ static inline ZEND_RESULT_CODE psi_parse_args(zend_execute_data *execute_data, i
                return rv;
        }
 
-       /* set up defaults */
-       for (i = 0; i < impl->func->args->count; ++i) {
-               iarg = impl->func->args->args[i];
-
-               if (i < EX_NUM_ARGS()) {
-                       iarg->_zv = ++zarg;
-                       ZVAL_DEREF(iarg->_zv);
-                       if (iarg->var->reference) {
-                               zval_dtor(iarg->_zv);
-                               ZVAL_NULL(iarg->_zv);
-                       }
-               }
-
-               if (iarg->def) {
-                       switch (iarg->type->type) {
-                       case PSI_T_BOOL:
-                               iarg->val.zend.bval = iarg->def->type == PSI_T_TRUE ? 1 : 0;
-                               break;
-                       case PSI_T_INT:
-                               iarg->val.zend.lval = zend_atol(iarg->def->text, strlen(iarg->def->text));
-                               break;
-                       case PSI_T_FLOAT:
-                       case PSI_T_DOUBLE:
-                               iarg->val.dval = zend_strtod(iarg->def->text, NULL);
-                               break;
-                       case PSI_T_STRING:
-                               /* FIXME */
-                               iarg->val.zend.str = zend_string_init(&iarg->def->text[1], strlen(iarg->def->text) - 2, 0);
-                               break;
-                       }
-               }
-       }
-
-       ZEND_PARSE_PARAMETERS_START(psi_num_min_args(impl), impl->func->args->count)
+       ZEND_PARSE_PARAMETERS_START(psi_num_min_args(impl), impl->func->args->vararg.name ? -1 : impl->func->args->count)
        nextarg:
-               iarg = impl->func->args->args[_i];
-               if (iarg->def) {
+               if (impl->func->args->vararg.name && _i >= impl->func->args->count) {
+                       impl_arg *varg = impl->func->args->vararg.name;
+                       iarg = init_impl_arg(
+                                       init_impl_type(varg->type->type, varg->type->name),
+                                       init_impl_var(varg->var->name, varg->var->reference),
+                                       NULL);
+
                        Z_PARAM_OPTIONAL;
-               }
-               if (PSI_T_BOOL == iarg->type->type) {
+                       if (_i == impl->func->args->count) {
+                               impl->func->args->vararg.args = init_impl_args(iarg);
+                       } else {
+                               add_impl_arg(impl->func->args->vararg.args, iarg);
+                       }
+               } else {
+                       iarg = impl->func->args->args[_i];
                        if (iarg->def) {
-                               iarg->val.zend.bval = iarg->def->type == PSI_T_TRUE ? 1 : 0;
+                               Z_PARAM_OPTIONAL;
                        }
+               }
+               if (PSI_T_BOOL == iarg->type->type) {
                        Z_PARAM_BOOL(iarg->val.zend.bval);
                } else if (PSI_T_INT == iarg->type->type) {
-                       if (iarg->def) {
-                               iarg->val.zend.lval = zend_atol(iarg->def->text, strlen(iarg->def->text));
-                       }
                        Z_PARAM_LONG(iarg->val.zend.lval);
                } else if (PSI_T_FLOAT == iarg->type->type || PSI_T_DOUBLE == iarg->type->type) {
-                       if (iarg->def) {
-                               iarg->val.dval = zend_strtod(iarg->def->text, NULL);
-                       }
                        Z_PARAM_DOUBLE(iarg->val.dval);
                } else if (PSI_T_STRING == iarg->type->type) {
-                       zend_string *str = iarg->val.zend.str;
                        Z_PARAM_STR_EX(iarg->val.zend.str, 1, iarg->var->reference);
-                       if (str && str != iarg->val.zend.str) {
-                               zend_string_release(str);
+                       if (iarg->val.zend.str) {
+                               zend_string_addref(iarg->val.zend.str);
                        }
                } else if (PSI_T_ARRAY == iarg->type->type) {
                        Z_PARAM_PROLOGUE(0);
                } else if (PSI_T_OBJECT == iarg->type->type) {
                        Z_PARAM_PROLOGUE(0);
+               } else if (PSI_T_MIXED == iarg->type->type) {
+                       Z_PARAM_PROLOGUE(0);
                } else {
                        error_code = ZPP_ERROR_FAILURE;
                        break;
                }
+               iarg->_zv = _arg;
+               ZVAL_DEREF(iarg->_zv);
                if (_i < _num_args) {
                        goto nextarg;
                }
@@ -583,6 +582,30 @@ static inline ZEND_RESULT_CODE psi_parse_args(zend_execute_data *execute_data, i
                return FAILURE
        );
 
+       /* set up defaults */
+       for (i = 0; i < impl->func->args->count; ++i) {
+               if (i >= EX_NUM_ARGS() && iarg->def) {
+                       iarg = impl->func->args->args[i];
+
+                       switch (iarg->type->type) {
+                       case PSI_T_BOOL:
+                               iarg->val.zend.bval = iarg->def->type == PSI_T_TRUE ? 1 : 0;
+                               break;
+                       case PSI_T_INT:
+                               iarg->val.zend.lval = zend_atol(iarg->def->text, strlen(iarg->def->text));
+                               break;
+                       case PSI_T_FLOAT:
+                       case PSI_T_DOUBLE:
+                               iarg->val.dval = zend_strtod(iarg->def->text, NULL);
+                               break;
+                       case PSI_T_STRING:
+                               /* FIXME */
+                               iarg->val.zend.str = zend_string_init(&iarg->def->text[1], strlen(iarg->def->text) - 2, 0);
+                               break;
+                       }
+               }
+       }
+
        zend_restore_error_handling(&zeh);
        return SUCCESS;
 }
@@ -592,113 +615,145 @@ static inline void *psi_do_calloc(let_calloc *alloc)
        zend_long n = psi_long_num_exp(alloc->nmemb, NULL), s = psi_long_num_exp(alloc->size, NULL);
        void *mem = safe_emalloc(n, s, sizeof(void *));
        memset(mem, 0, n * s + sizeof(void *));
+#if 0
+       fprintf(stderr, "calloc: %p\n", mem);
+#endif
        return mem;
 }
 
-static inline void *psi_do_let(let_stmt *let)
+static inline impl_val *psi_let_val(token_t let_func, impl_arg *iarg, impl_val *arg_val, decl_struct *strct, void **to_free)
 {
-       decl_arg *darg = let->var->arg;
-       impl_arg *iarg = darg->let->arg;
-       impl_val *arg_val = darg->ptr;
-
-       if (!iarg) {
-               /* let foo = calloc(1, long);
-                * let foo = NULL;
-                * let foo;
-                */
-               if (darg->let->val->func && darg->let->val->func->type == PSI_T_CALLOC) {
-                       arg_val->ptr = psi_do_calloc(darg->let->val->func->alloc);
-                       darg->mem = arg_val->ptr;
-               } else if (darg->var->array_size) {
-                       arg_val->ptr = ecalloc(darg->var->array_size, sizeof(*arg_val));
-                       darg->mem = arg_val->ptr;
+       switch (let_func) {
+       case PSI_T_BOOLVAL:
+               if (iarg->type->type == PSI_T_BOOL) {
+                       arg_val->cval = iarg->val.zend.bval;
                } else {
-                       memset(arg_val, 0, sizeof(*arg_val));
+                       arg_val->cval = zend_is_true(iarg->_zv);
                }
-       } else {
-
-               switch (darg->let->val->func->type) {
-               case PSI_T_BOOLVAL:
-                       if (iarg->type->type == PSI_T_BOOL) {
-                               arg_val->cval = iarg->val.zend.bval;
-                       } else {
-                               arg_val->cval = zend_is_true(iarg->_zv);
-                       }
-                       break;
-               case PSI_T_INTVAL:
-                       if (iarg->type->type == PSI_T_INT) {
-                               arg_val->lval = iarg->val.zend.lval;
-                       } else {
-                               arg_val->lval = zval_get_long(iarg->_zv);
-                       }
-                       break;
-               case PSI_T_PATHVAL:
-               case PSI_T_STRVAL:
-                       if (iarg->type->type == PSI_T_STRING) {
-                               arg_val->ptr = estrdup(iarg->val.zend.str->val);
-                               darg->mem = arg_val->ptr;
-                               zend_string_release(iarg->val.zend.str);
+               break;
+       case PSI_T_INTVAL:
+               if (iarg->type->type == PSI_T_INT) {
+                       arg_val->lval = iarg->val.zend.lval;
+               } else {
+                       arg_val->lval = zval_get_long(iarg->_zv);
+               }
+               break;
+       case PSI_T_FLOATVAL:
+               if (iarg->type->type == PSI_T_FLOAT || iarg->type->type == PSI_T_DOUBLE) {
+                       arg_val->dval = iarg->val.dval;
+               } else {
+                       arg_val->dval = zval_get_double(iarg->_zv);
+               }
+               break;
+       case PSI_T_PATHVAL:
+       case PSI_T_STRVAL:
+               if (iarg->type->type == PSI_T_STRING) {
+                       if (iarg->val.zend.str) {
+                               arg_val->ptr = estrndup(iarg->val.zend.str->val, iarg->val.zend.str->len);
+                               *to_free = arg_val->ptr;
                        } else {
-                               zend_string *zs = zval_get_string(iarg->_zv);
-                               arg_val->ptr = estrdup(zs->val);
-                               darg->mem = arg_val->ptr;
-                               zend_string_release(zs);
+                               arg_val->ptr = "";
                        }
-                       if (PSI_T_PATHVAL == darg->let->val->func->type) {
-                               if (SUCCESS != php_check_open_basedir(arg_val->ptr)) {
-                                       efree(arg_val->ptr);
-                                       return NULL;
-                               }
+               } else {
+                       zend_string *zs = zval_get_string(iarg->_zv);
+                       arg_val->ptr = estrdup(zs->val);
+                       *to_free = arg_val->ptr;
+                       zend_string_release(zs);
+               }
+               if (PSI_T_PATHVAL == let_func) {
+                       if (SUCCESS != php_check_open_basedir(arg_val->ptr)) {
+                               efree(arg_val->ptr);
+                               return NULL;
                        }
-                       break;
-               case PSI_T_STRLEN:
-                       if (iarg->type->type == PSI_T_STRING) {
+               }
+               break;
+       case PSI_T_STRLEN:
+               if (iarg->type->type == PSI_T_STRING) {
+                       if (iarg->val.zend.str) {
                                arg_val->lval = iarg->val.zend.str->len;
-                               zend_string_release(iarg->val.zend.str);
                        } else {
-                               zend_string *zs = zval_get_string(iarg->_zv);
-                               arg_val->lval = zs->len;
-                               zend_string_release(zs);
+                               arg_val->lval = 0;
                        }
-                       break;
-               case PSI_T_ARRVAL:
-                       if (iarg->type->type == PSI_T_ARRAY) {
-                               decl_type *type = real_decl_type(darg->type);
-
-                               switch (type->type) {
-                               case PSI_T_STRUCT:
-                                       arg_val->ptr = psi_array_to_struct(type->strct, HASH_OF(iarg->_zv));
-                                       darg->mem = arg_val->ptr;
-                                       break;
-                               }
+               } else {
+                       zend_string *zs = zval_get_string(iarg->_zv);
+                       arg_val->lval = zs->len;
+                       zend_string_release(zs);
+               }
+               break;
+       case PSI_T_ARRVAL:
+               if (iarg->type->type == PSI_T_ARRAY) {
+                       arg_val = psi_array_to_struct(strct, HASH_OF(iarg->_zv));
+                       *to_free = arg_val;
+               }
+               break;
+       case PSI_T_OBJVAL:
+               if (iarg->type->type == PSI_T_OBJECT) {
+                       psi_object *obj;
+
+                       if (!instanceof_function(Z_OBJCE_P(iarg->_zv), psi_class_entry)) {
+                               return NULL;
                        }
-                       break;
-               case PSI_T_OBJVAL:
-                       if (iarg->type->type == PSI_T_OBJECT) {
-                               psi_object *obj;
 
-                               if (!instanceof_function(Z_OBJCE_P(iarg->_zv), psi_class_entry)) {
-                                       return NULL;
-                               }
+                       obj = PSI_OBJ(iarg->_zv, NULL);
+                       arg_val->ptr = obj->data;
+               }
+               break;
+       EMPTY_SWITCH_DEFAULT_CASE();
+       }
+       return arg_val;
+}
 
-                               obj = PSI_OBJ(iarg->_zv, NULL);
-                               arg_val->ptr = obj->data;
-                       }
-                       break;
-               EMPTY_SWITCH_DEFAULT_CASE();
+static inline void *psi_do_let(let_stmt *let)
+{
+       decl_arg *darg = let->var->arg;
+       impl_val *arg_val = darg->ptr;
+       impl_arg *iarg;
+
+       switch (let->val ? let->val->kind : PSI_LET_NULL) {
+       case PSI_LET_TMP:
+               memcpy(arg_val, deref_impl_val(let->val->data.var->arg->let->ptr, let->val->data.var), sizeof(*arg_val));
+#if 0
+               fprintf(stderr, "LET TMP: %p -> %p\n",
+                               let->val->data.var->arg->let->ptr,
+                               arg_val->ptr);
+#endif
+               break;
+       case PSI_LET_NULL:
+               if (darg->var->array_size) {
+                       arg_val->ptr = ecalloc(darg->var->array_size, sizeof(*arg_val));
+                       darg->mem = arg_val->ptr;
+               } else {
+                       memset(arg_val, 0, sizeof(*arg_val));
+               }
+               break;
+       case PSI_LET_CALLOC:
+               arg_val->ptr = psi_do_calloc(let->val->data.alloc);
+               darg->mem = arg_val->ptr;
+               break;
+       case PSI_LET_NUMEXP:
+               arg_val->zend.lval = psi_long_num_exp(let->val->data.num, NULL);
+               break;
+       case PSI_LET_FUNC:
+               iarg = let->val->data.func->arg;
+
+               if (!(darg->ptr = psi_let_val(let->val->data.func->type, iarg, darg->ptr, real_decl_type(darg->type)->strct, &darg->mem))) {
+                       return NULL;
                }
        }
 
-       if (darg->let->val && darg->let->val->is_reference) {
-               return darg->let->ptr = &darg->ptr;
+       if (let->val && let->val->flags.one.is_reference) {
+               return let->ptr = &darg->ptr;
        } else {
-               return darg->let->ptr = darg->ptr;
+               return let->ptr = darg->ptr;
        }
 }
 
 static inline void psi_do_set(zval *return_value, set_value *set)
 {
-       set->func->handler(return_value, set, set->vars->vars[0]->arg->ptr);
+       decl_arg *set_arg = set->vars->vars[0]->arg;
+
+       zval_dtor(return_value);
+       set->func->handler(return_value, set, set_arg->let ? set_arg->let->ptr : set_arg->ptr);
 }
 
 static inline void psi_do_return(zval *return_value, return_stmt *ret)
@@ -706,15 +761,9 @@ static inline void psi_do_return(zval *return_value, return_stmt *ret)
        ret->set->func->handler(return_value, ret->set, ret->set->vars->vars[0]->arg->ptr);
 }
 
-static inline void psi_do_return2(zval *return_value, return_stmt *ret, impl_val *ret_val)
-{
-       ret->set->func->handler(return_value, ret->set, ret_val);
-}
-
 static inline void psi_do_free(free_stmt *fre)
 {
        size_t i, j;
-       impl_val dummy;
 
        for (i = 0; i < fre->calls->count; ++i) {
                free_call *f = fre->calls->list[i];
@@ -722,12 +771,29 @@ static inline void psi_do_free(free_stmt *fre)
                for (j = 0; j < f->vars->count; ++j) {
                        decl_var *dvar = f->vars->vars[j];
                        decl_arg *darg = dvar->arg;
+                       impl_val *fval = darg->let ? darg->let->ptr : darg->ptr;
 
-                       f->decl->call.args[j] = &darg->val;
+                       f->decl->call.args[j] = deref_impl_val(fval, dvar);
                }
 
                /* FIXME: check in validate_* that free functions return scalar */
-               PSI_ContextCall(&PSI_G(context), &dummy, f->decl);
+               PSI_ContextCall(&PSI_G(context), &f->decl->call, NULL);
+       }
+}
+
+static inline void psi_clean_array_struct(decl_arg *darg) {
+       if (darg->let
+       &&      darg->let->val->kind == PSI_LET_FUNC
+       &&      darg->let->val->data.func->type == PSI_T_ARRVAL) {
+               decl_type *type = real_decl_type(darg->type);
+
+               if (type->type == PSI_T_STRUCT) {
+                       void **ptr = (void **) ((char *) darg->mem + type->strct->size);
+
+                       while (*ptr) {
+                               efree(*ptr++);
+                       }
+               }
        }
 }
 
@@ -735,6 +801,10 @@ static inline void psi_do_clean(impl *impl)
 {
        size_t i;
 
+       if (impl->decl->func->ptr != &impl->decl->func->val) {
+               efree(impl->decl->func->ptr);
+               impl->decl->func->ptr = &impl->decl->func->val;
+       }
        for (i = 0; i < impl->func->args->count; ++i ) {
                impl_arg *iarg = impl->func->args->args[i];
 
@@ -751,23 +821,39 @@ static inline void psi_do_clean(impl *impl)
                decl_arg *darg = impl->decl->args->args[i];
 
                if (darg->mem) {
-                       decl_type *type = real_decl_type(darg->type);
-
-                       if (type->type == PSI_T_STRUCT) {
-                               void **ptr = (void **) ((char *) darg->mem + type->strct->size);
-
-                               while (*ptr) {
-                                       efree(*ptr++);
-                               }
-                       }
+                       psi_clean_array_struct(darg);
                        efree(darg->mem);
                        darg->mem = NULL;
                }
+               darg->ptr = &darg->val;
+       }
+
+       if (impl->func->args->vararg.args) {
+               free_impl_args(impl->func->args->vararg.args);
+               impl->func->args->vararg.args = NULL;
+       }
+       if (impl->func->args->vararg.types) {
+               efree(impl->func->args->vararg.types);
+               impl->func->args->vararg.types = NULL;
+       }
+       if (impl->func->args->vararg.values) {
+               efree(impl->func->args->vararg.values);
+               impl->func->args->vararg.values = NULL;
+       }
+       if (impl->func->args->vararg.free_list) {
+               void **list = impl->func->args->vararg.free_list;
+
+               while (*list) {
+                       efree(*list++);
+               }
+
+               efree(impl->func->args->vararg.free_list);
+               impl->func->args->vararg.free_list = NULL;
        }
 }
 
-static inline int psi_calc_num_exp_value(num_exp *exp, impl_val *ref, impl_val *res) {
-       impl_val *tmp = NULL;
+static inline int psi_calc_num_exp_value(num_exp *exp, impl_val *strct, impl_val *res) {
+       impl_val *ref, *tmp = NULL;
 
        switch (exp->t) {
        case PSI_T_NUMBER:
@@ -793,10 +879,12 @@ static inline int psi_calc_num_exp_value(num_exp *exp, impl_val *ref, impl_val *
                break;
 
        case PSI_T_NAME:
-               if (1) {
-                       ref = exp->u.dvar->arg->ptr;
+               if (strct) {
+                       ref = struct_member_ref(exp->u.dvar->arg, strct, &tmp);
+               } else if (exp->u.dvar->arg->let) {
+                       ref = exp->u.dvar->arg->let->ptr;
                } else {
-                       ref = struct_member_ref(exp->u.dvar->arg, ref, &tmp);
+                       ref = exp->u.dvar->arg->ptr;
                }
                switch (real_decl_type(exp->u.dvar->arg->type)->type) {
                case PSI_T_INT8:
@@ -830,13 +918,13 @@ static inline int psi_calc_num_exp_value(num_exp *exp, impl_val *ref, impl_val *
        return  0;
 }
 
-int psi_calc_num_exp(num_exp *exp, impl_val *ref, impl_val *res) {
+int psi_calc_num_exp(num_exp *exp, impl_val *strct, impl_val *res) {
        impl_val num = {0};
-       int num_type = psi_calc_num_exp_value(exp, ref, &num);
+       int num_type = psi_calc_num_exp_value(exp, strct, &num);
 
        if (exp->operand) {
                impl_val tmp = {0};
-               int tmp_type = psi_calc_num_exp(exp->operand, ref, &tmp);
+               int tmp_type = psi_calc_num_exp(exp->operand, strct, &tmp);
 
                return exp->calculator(num_type, &num, tmp_type, &tmp, res);
        }
@@ -992,13 +1080,85 @@ PSI_CALC_FN(sub)
 #define PSI_CALC(var1, var2) (var1) / (var2)
 PSI_CALC_FN(div)
 
+static inline void psi_do_args(impl *impl) {
+       size_t i;
+
+       for (i = 0; i < impl->decl->args->count; ++i) {
+               impl->decl->call.args[i] = impl->decl->args->args[i]->let->ptr;
+       }
+
+       if (!impl->decl->func->var->pointer_level) {
+               decl_type *real = real_decl_type(impl->decl->func->type);
+
+               switch (real->type) {
+               case PSI_T_STRUCT:
+                       impl->decl->func->ptr = psi_array_to_struct(real->strct, NULL);
+                       break;
+               }
+       }
+}
+
+static inline impl_vararg *psi_do_varargs(impl *impl) {
+       size_t i, j;
+       impl_vararg *va = &impl->func->args->vararg;
+       size_t vacount = va->args->count;
+
+
+       if (!vacount) {
+               return NULL;
+       }
+
+       va->types = ecalloc(vacount, sizeof(*va->types));
+       va->values = ecalloc(vacount, sizeof(*va->values));
+
+       for (i = 0, j = 0; i < vacount; ++i) {
+               impl_arg *vaarg = va->args->args[i];
+               void *to_free = NULL;
+               token_t let_fn, vatype = va->name->type->type;
+
+               if (vatype == PSI_T_MIXED) {
+                       switch (Z_TYPE_P(vaarg->_zv)) {
+                       case IS_TRUE:
+                       case IS_FALSE:  vatype = PSI_T_BOOL;    break;
+                       case IS_LONG:   vatype = PSI_T_INT;             break;
+                       case IS_DOUBLE: vatype = PSI_T_FLOAT;   break;
+                       default:                vatype = PSI_T_STRING;  break;
+                       }
+               }
+
+
+               switch (vatype) {
+               case PSI_T_BOOL:        let_fn = PSI_T_BOOLVAL; break;
+               case PSI_T_INT:         let_fn = PSI_T_INTVAL;  break;
+               case PSI_T_FLOAT:
+               case PSI_T_DOUBLE:      let_fn = PSI_T_FLOATVAL;break;
+               case PSI_T_STRING:      let_fn = PSI_T_STRVAL;  break;
+               EMPTY_SWITCH_DEFAULT_CASE();
+               }
+
+               va->types[i] = vatype;
+               /* FIXME: varargs with struct-by-value :) */
+               if (!psi_let_val(let_fn, vaarg, &va->values[i], NULL, &to_free)) {
+                       return NULL;
+               }
+
+               if (to_free) {
+                       if (!va->free_list) {
+                               va->free_list = ecalloc(vacount - i + 1, sizeof(*va->free_list));
+                       }
+                       va->free_list[j++] = to_free;
+               }
+       }
+
+       return va;
+}
+
 void psi_call(zend_execute_data *execute_data, zval *return_value, impl *impl)
 {
        size_t i;
-       return_stmt *ret = impl->stmts->ret.list[0];
-       decl_var *var = ret->set->vars->vars[0];
+       impl_vararg *va = NULL;
 
-       memset(var->arg->ptr, 0, sizeof(impl_val));
+       memset(impl->decl->func->ptr, 0, sizeof(impl_val));
 
        if (SUCCESS != psi_parse_args(execute_data, impl)) {
                return;
@@ -1008,18 +1168,22 @@ void psi_call(zend_execute_data *execute_data, zval *return_value, impl *impl)
                let_stmt *let = impl->stmts->let.list[i];
 
                if (!psi_do_let(let)) {
-                       psi_do_return(return_value, ret);
+                       psi_do_return(return_value, impl->stmts->ret.list[0]);
                        psi_do_clean(impl);
                        return;
                }
        }
 
-       if (impl->decl->args) for (i = 0; i < impl->decl->args->count; ++i) {
-               impl->decl->call.args[i] = impl->decl->args->args[i]->let->ptr;
+       if (impl->decl->args) {
+               psi_do_args(impl);
+
+               if (impl->func->args->vararg.args) {
+                       va = psi_do_varargs(impl);
+               }
        }
 
-       PSI_ContextCall(&PSI_G(context), var->arg->ptr, impl->decl);
-       psi_do_return(return_value, ret);
+       PSI_ContextCall(&PSI_G(context), &impl->decl->call, va);
+       psi_do_return(return_value, impl->stmts->ret.list[0]);
 
        for (i = 0; i < impl->stmts->set.count; ++i) {
                set_stmt *set = impl->stmts->set.list[i];
@@ -1088,7 +1252,7 @@ PHP_MINIT_FUNCTION(psi)
                return FAILURE;
        }
 
-       PSI_ContextInit(&PSI_G(context), ops, psi_error);
+       PSI_ContextInit(&PSI_G(context), ops, psi_error_wrapper);
        PSI_ContextBuild(&PSI_G(context), PSI_G(directory));
 
        if (psi_check_env("PSI_DUMP")) {