fix retval
[m6w6/ext-psi] / src / module.c
index b8d23008ffcaa4f73139b92224c29f9631234636..b7a09c3d5b81f9b6f51e976b12374cfecedd89be 100644 (file)
@@ -360,17 +360,20 @@ void psi_to_array(zval *return_value, token_t t, impl_val *ret_val, set_value *s
                                set_value *sub_set = set->inner[i];
                                decl_var *sub_var = sub_set->vars->vars[0];
                                decl_arg *sub_arg = sub_var->arg;
-                               token_t t = real_decl_type(sub_arg->type)->type;
-                               void *ptr = malloc(sub_arg->layout->len);
-
-                               memcpy(ptr, (char *) ret_val->ptr + sub_arg->layout->pos,
-                                               sub_arg->layout->len);
-                               tmp_ptr = enref_impl_val(ptr, sub_arg->var);
-                               sub_set->func->handler(&ztmp, t, tmp_ptr, sub_set, sub_var);
-                               add_assoc_zval(return_value, sub_var->name, &ztmp);
-                               free(tmp_ptr);
-                               if (tmp_ptr != ptr) {
-                                       free(ptr);
+
+                               if (sub_arg) {
+                                       token_t t = real_decl_type(sub_arg->type)->type;
+                                       void *ptr = malloc(sub_arg->layout->len);
+
+                                       memcpy(ptr, (char *) ret_val->ptr + sub_arg->layout->pos,
+                                                       sub_arg->layout->len);
+                                       tmp_ptr = enref_impl_val(ptr, sub_arg->var);
+                                       sub_set->func->handler(&ztmp, t, tmp_ptr, sub_set, sub_var);
+                                       add_assoc_zval(return_value, sub_var->name, &ztmp);
+                                       free(tmp_ptr);
+                                       if (tmp_ptr != ptr) {
+                                               free(ptr);
+                                       }
                                }
                        }
                }
@@ -436,7 +439,7 @@ void psi_to_array(zval *return_value, token_t t, impl_val *ret_val, set_value *s
        }
 }
 
-ZEND_RESULT_CODE psi_parse_args(zend_execute_data *execute_data, impl *impl)
+static inline ZEND_RESULT_CODE psi_parse_args(zend_execute_data *execute_data, impl *impl)
 {
        impl_arg *iarg;
 
@@ -494,7 +497,7 @@ ZEND_RESULT_CODE psi_parse_args(zend_execute_data *execute_data, impl *impl)
        return SUCCESS;
 }
 
-void *psi_do_calloc(let_calloc *alloc)
+static inline void *psi_do_calloc(let_calloc *alloc)
 {
        decl_type *type = real_decl_type(alloc->type);
        size_t size;
@@ -509,7 +512,7 @@ void *psi_do_calloc(let_calloc *alloc)
        return ecalloc(alloc->n, size);
 }
 
-void *psi_do_let(decl_arg *darg)
+static inline void *psi_do_let(decl_arg *darg)
 {
        impl_arg *iarg = darg->let->arg;
        impl_val *arg_val;
@@ -593,7 +596,7 @@ void *psi_do_let(decl_arg *darg)
        }
 }
 
-void psi_do_set(zval *return_value, set_value *set)
+static inline void psi_do_set(zval *return_value, set_value *set)
 {
        impl_val *val = (impl_val *) &set->vars->vars[0]->arg->let->ptr;
        token_t t = real_decl_type(set->vars->vars[0]->arg->type)->type;
@@ -604,28 +607,33 @@ void psi_do_set(zval *return_value, set_value *set)
        set->func->handler(return_value, t, val, set, set->vars->vars[0]);
 }
 
-void psi_do_return(zval *return_value, return_stmt *ret, impl_val *ret_val)
+static inline void psi_do_return(zval *return_value, return_stmt *ret, impl_val *ret_val)
 {
        token_t t = real_decl_type(ret->decl->type)->type;
 
        ret->set->func->handler(return_value, t, ret_val, ret->set, ret->decl->var);
 }
 
-void psi_do_free(free_stmt *fre)
+static inline void psi_do_free(free_stmt *fre)
 {
-       size_t i;
+       size_t i, j;
+       impl_val dummy;
 
-       for (i = 0; i < fre->vars->count; ++i) {
-               decl_var *dvar = fre->vars->vars[i];
+       for (i = 0; i < fre->calls->count; ++i) {
+               free_call *f = fre->calls->list[i];
 
-               if (dvar->arg && dvar->arg->let->out.ptr) {
-                       free(dvar->arg->let->out.ptr);
-                       dvar->arg->let->out.ptr = NULL;
+               for (j = 0; j < f->vars->count; ++j) {
+                       decl_var *dvar = f->vars->vars[j];
+                       decl_arg *darg = dvar->arg;
+
+                       f->decl->call.args[j] = &darg->let->out;
                }
+
+               PSI_ContextCall(&PSI_G(context), &dummy, f->decl);
        }
 }
 
-void psi_do_clean(impl *impl)
+static inline void psi_do_clean(impl *impl)
 {
        size_t i;
 
@@ -660,6 +668,45 @@ void psi_do_clean(impl *impl)
        }
 }
 
+void psi_call(zend_execute_data *execute_data, zval *return_value, impl *impl)
+{
+       impl_val ret_val;
+       size_t i;
+
+       if (SUCCESS != psi_parse_args(execute_data, impl)) {
+               return;
+       }
+
+       if (impl->decl->args) {
+               for (i = 0; i < impl->decl->args->count; ++i) {
+                       decl_arg *darg = impl->decl->args->args[i];
+
+                       impl->decl->call.args[i] = psi_do_let(darg);
+               }
+       }
+
+       memset(&ret_val, 0, sizeof(ret_val));
+       PSI_ContextCall(&PSI_G(context), &ret_val, impl->decl);
+
+       psi_do_return(return_value, impl->stmts->ret.list[0], &ret_val);
+
+       for (i = 0; i < impl->stmts->set.count; ++i) {
+               set_stmt *set = impl->stmts->set.list[i];
+
+               if (set->arg->_zv) {
+                       psi_do_set(set->arg->_zv, set->val);
+               }
+       }
+
+       for (i = 0; i < impl->stmts->fre.count; ++i) {
+               free_stmt *fre = impl->stmts->fre.list[i];
+
+               psi_do_free(fre);
+       }
+
+       psi_do_clean(impl);
+}
+
 PHP_MINIT_FUNCTION(psi)
 {
        PSI_ContextOps *ops;