prepare varargs calls
[m6w6/ext-psi] / src / module.c
index 9d97a3ec29b6de98463dbbb8ae12efe1bd8c2ec4..ba3e4d5a98d5af9478ae3fa172d5a482c7f72d58 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];
@@ -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;
 }
@@ -599,24 +580,31 @@ static inline void *psi_do_let(let_stmt *let)
 {
        decl_arg *darg = let->var->arg;
        impl_val *arg_val = darg->ptr;
+       impl_arg *iarg;
 
-       if (!let->val) {
+       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 if (let->val->num) {
-               arg_val->zend.lval = psi_long_num_exp(darg->let->val->num, NULL);
-       } else {
-               impl_arg *iarg = darg->let->arg;
+               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 (let->val->func->type) {
-               case PSI_T_CALLOC:
-                       arg_val->ptr = psi_do_calloc(let->val->func->alloc);
-                       darg->mem = arg_val->ptr;
-                       break;
+               switch (let->val->data.func->type) {
                case PSI_T_BOOLVAL:
                        if (iarg->type->type == PSI_T_BOOL) {
                                arg_val->cval = iarg->val.zend.bval;
@@ -634,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;
@@ -652,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;
@@ -688,10 +682,10 @@ 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;
        }
 }
 
@@ -709,7 +703,6 @@ static inline void psi_do_return(zval *return_value, return_stmt *ret)
 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,7 +715,7 @@ static inline void psi_do_free(free_stmt *fre)
                }
 
                /* 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);
        }
 }
 
@@ -1013,7 +1006,7 @@ void psi_call(zend_execute_data *execute_data, zval *return_value, impl *impl)
                impl->decl->call.args[i] = impl->decl->args->args[i]->let->ptr;
        }
 
-       PSI_ContextCall(&PSI_G(context), var->arg->ptr, impl->decl);
+       PSI_ContextCall(&PSI_G(context), &impl->decl->call);
        psi_do_return(return_value, ret);
 
        for (i = 0; i < impl->stmts->set.count; ++i) {