flush
[m6w6/ext-psi] / src / module.c
index 53079d0a1293771ea8b74001595224a7f6ef1350..c820f4f1bd0ed47b6b2a873790a78fd3d6f8dbbd 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"
@@ -124,13 +122,26 @@ 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, 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) {
+               impl_arg *vararg = impl->func->args->vararg;
+               zend_internal_arg_info *ai = &aip[impl->func->args->count];
+
+               ai->name = vararg->var->name;
+               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];
@@ -286,7 +297,7 @@ void psi_to_string(zval *return_value, set_value *set, impl_val *ret_val)
                        ret_val = deref_impl_val(ret_val, var);
                        if (ret_val && ret_val->ptr) {
                                if (set->num) {
-                                       RETVAL_STRINGL(ret_val->ptr, psi_long_num_exp(set->num, NULL));
+                                       RETVAL_STRINGL(ret_val->ptr, psi_long_num_exp(set->num, set->outer.val));
                                } else {
                                        RETVAL_STRING(ret_val->ptr);
                                }
@@ -373,6 +384,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->ptr <= (intptr_t) 0) {
+               RETURN_NULL();
+       }
+
        array_init(return_value);
 
        if (t == PSI_T_STRUCT) {
@@ -425,38 +440,6 @@ 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))
                 */
@@ -481,7 +464,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;
@@ -507,65 +490,22 @@ 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 ? -1 : impl->func->args->count)
        nextarg:
                iarg = impl->func->args->args[_i];
                if (iarg->def) {
                        Z_PARAM_OPTIONAL;
                }
                if (PSI_T_BOOL == iarg->type->type) {
-                       if (iarg->def) {
-                               iarg->val.zend.bval = iarg->def->type == PSI_T_TRUE ? 1 : 0;
-                       }
                        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);
@@ -575,6 +515,20 @@ static inline ZEND_RESULT_CODE psi_parse_args(zend_execute_data *execute_data, i
                        error_code = ZPP_ERROR_FAILURE;
                        break;
                }
+               if (impl->func->args->vararg) {
+                       iarg = impl->func->args->vararg;
+
+                       if (_i == impl->func->args->count) {
+                               zval *ptr = iarg->_zv = calloc(_num_args - _i + 1, sizeof(zval));
+
+                               _optional = 1;
+                               while (_i < _num_args) {
+                                       Z_PARAM_PROLOGUE(0);
+                                       ZVAL_COPY_VALUE(ptr++, _arg);
+                               }
+                               break;
+                       }
+               }
                if (_i < _num_args) {
                        goto nextarg;
                }
@@ -583,6 +537,33 @@ 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) {
+               iarg = impl->func->args->args[i];
+
+               if (i < EX_NUM_ARGS()) {
+                       iarg->_zv = ++zarg;
+                       ZVAL_DEREF(iarg->_zv);
+               } else 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_restore_error_handling(&zeh);
        return SUCCESS;
 }
@@ -598,26 +579,32 @@ static inline void *psi_do_calloc(let_calloc *alloc)
 static inline void *psi_do_let(let_stmt *let)
 {
        decl_arg *darg = let->var->arg;
-       impl_arg *iarg = darg->let->arg;
        impl_val *arg_val = darg->ptr;
+       impl_arg *iarg;
 
-       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) {
+       switch (let->val ? let->val->kind : PSI_LET_NULL) {
+       case PSI_LET_TMP:
+               arg_val->ptr = deref_impl_val(let->val->data.var->arg->val.ptr, let->var);
+               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));
                }
-       } else {
+               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;
 
-               switch (darg->let->val->func->type) {
+               switch (let->val->data.func->type) {
                case PSI_T_BOOLVAL:
                        if (iarg->type->type == PSI_T_BOOL) {
                                arg_val->cval = iarg->val.zend.bval;
@@ -635,16 +622,19 @@ static inline void *psi_do_let(let_stmt *let)
                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);
+                               if (iarg->val.zend.str) {
+                                       arg_val->ptr = estrdup(iarg->val.zend.str->val);
+                                       darg->mem = arg_val->ptr;
+                               } else {
+                                       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);
                        }
-                       if (PSI_T_PATHVAL == darg->let->val->func->type) {
+                       if (PSI_T_PATHVAL == darg->let->val->data.func->type) {
                                if (SUCCESS != php_check_open_basedir(arg_val->ptr)) {
                                        efree(arg_val->ptr);
                                        return NULL;
@@ -653,8 +643,11 @@ static inline void *psi_do_let(let_stmt *let)
                        break;
                case PSI_T_STRLEN:
                        if (iarg->type->type == PSI_T_STRING) {
-                               arg_val->lval = iarg->val.zend.str->len;
-                               zend_string_release(iarg->val.zend.str);
+                               if (iarg->val.zend.str) {
+                                       arg_val->lval = iarg->val.zend.str->len;
+                               } else {
+                                       arg_val->lval = 0;
+                               }
                        } else {
                                zend_string *zs = zval_get_string(iarg->_zv);
                                arg_val->lval = zs->len;
@@ -689,15 +682,16 @@ static inline void *psi_do_let(let_stmt *let)
                }
        }
 
-       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)
 {
+       zval_dtor(return_value);
        set->func->handler(return_value, set, set->vars->vars[0]->arg->ptr);
 }
 
@@ -706,11 +700,6 @@ 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;
@@ -766,8 +755,8 @@ static inline void psi_do_clean(impl *impl)
        }
 }
 
-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 +782,10 @@ 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 {
-                       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 +819,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);
        }