Skip to content

Commit

Permalink
return helper does not need to cleanup args
Browse files Browse the repository at this point in the history
hook/return do not need to copy args
  • Loading branch information
krakjoe committed Jan 25, 2017
1 parent 9634704 commit 1e81e56
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 49 deletions.
13 changes: 0 additions & 13 deletions src/handlers.c
Original file line number Diff line number Diff line change
Expand Up @@ -378,21 +378,8 @@ static inline void uopz_run_hook(zend_function *function, zend_execute_data *exe
/* {{{ */
static inline int php_uopz_leave_helper(zend_execute_data *execute_data) {
zend_execute_data *call = EX(call);
uint32_t info = ZEND_CALL_INFO(call);

if (info & ZEND_CALL_RELEASE_THIS) {
OBJ_RELEASE(Z_OBJ(call->This));
} else if (info & ZEND_CALL_CLOSURE) {
OBJ_RELEASE((zend_object*)call->func->op_array.prototype);
}

EX(call) = call->prev_execute_data;

if (!EG(exception)) {
zend_vm_stack_free_args(call);
zend_vm_stack_free_call_frame(call);
}

EX(opline) = EX(opline) + 1;

return ZEND_USER_OPCODE_LEAVE;
Expand Down
16 changes: 2 additions & 14 deletions src/hook.c
Original file line number Diff line number Diff line change
Expand Up @@ -180,23 +180,11 @@ void uopz_execute_hook(uopz_hook_t *uhook, zend_execute_data *execute_data) { /*
goto _exit_uopz_execute_hook;
}

if (zend_fcall_info_argp(&fci, EX_NUM_ARGS(), EX_VAR_NUM(0)) != SUCCESS) {
if (EX(func)->common.scope) {
uopz_exception("cannot set arguments for %s::%s hook",
ZSTR_VAL(EX(func)->common.scope->name),
ZSTR_VAL(EX(func)->common.function_name));
} else {
uopz_exception("cannot set arguments for %s hook",
ZSTR_VAL(EX(func)->common.function_name));
}

goto _exit_uopz_execute_hook;
}

fci.param_count = ZEND_CALL_NUM_ARGS(execute_data);
fci.params = ZEND_CALL_ARG(execute_data, 1);
fci.retval= &rv;

if (zend_call_function(&fci, &fcc) == SUCCESS) {
zend_fcall_info_args_clear(&fci, 1);
if (!Z_ISUNDEF(rv)) {
zval_ptr_dtor(&rv);
}
Expand Down
17 changes: 2 additions & 15 deletions src/return.c
Original file line number Diff line number Diff line change
Expand Up @@ -182,24 +182,11 @@ void uopz_execute_return(uopz_return_t *ureturn, zend_execute_data *execute_data
goto _exit_uopz_execute_return;
}

if (zend_fcall_info_argp(&fci, EX_NUM_ARGS(), EX_VAR_NUM(0)) != SUCCESS) {
if (EX(func)->common.scope) {
uopz_exception("cannot set arguments for return value function for %s::%s",
ZSTR_VAL(EX(func)->common.scope->name),
ZSTR_VAL(EX(func)->common.function_name));
} else {
uopz_exception("cannot set arguments for return value function for %s",
ZSTR_VAL(EX(func)->common.function_name));
}

goto _exit_uopz_execute_return;
}

fci.param_count = ZEND_CALL_NUM_ARGS(execute_data);
fci.params = ZEND_CALL_ARG(execute_data, 1);
fci.retval= result;

if (zend_call_function(&fci, &fcc) == SUCCESS) {
zend_fcall_info_args_clear(&fci, 1);

if (!return_value) {
if (!Z_ISUNDEF(rv)) {
zval_ptr_dtor(&rv);
Expand Down
14 changes: 7 additions & 7 deletions tests/001.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,22 @@ uopz_set_return
--FILE--
<?php
class Foo {
public function bar() {
return false;
public function bar(int $arg) : int {
return $arg;
}
}

var_dump(uopz_set_return(Foo::class, "bar", true));

$foo = new Foo();

var_dump($foo->bar());
var_dump($foo->bar(1));

uopz_set_return(Foo::class, "bar", function() {
return 2;
uopz_set_return(Foo::class, "bar", function(int $arg) : int {
return $arg * 2;
}, true);

var_dump($foo->bar());
var_dump($foo->bar(2));

try {
uopz_set_return(Foo::class, "nope", 1);
Expand All @@ -39,7 +39,7 @@ try {
--EXPECT--
bool(true)
bool(true)
int(2)
int(4)
string(61) "failed to set return for Foo::nope, the method does not exist"
string(63) "failed to set return for Bar::bar, the method is defined in Foo"

0 comments on commit 1e81e56

Please sign in to comment.