Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions Zend/tests/partial_application/default_arg_scope.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
--TEST--
PFA default argument value scope
--ENV--
A=1
--FILE--
<?php

if (getenv('A')) {
/* Relative class references are never resolved at compile time on traits */

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, I was trying to resolve them at one point, but was hitting issues with the JIT. So I guess this might be revisited at one point.

trait T {
static function f($a, $b = self::VAL) {
var_dump($b);
}
}
trait U {
static function g($a, $b = parent::VAL) {
var_dump($b);
}
}
}

class C {
const VAL = 'C';
use T;
}

class D extends C {
const VAL = 'D';
use U;
}

C::f(0);
D::f(0);
C::f(?, ...)(0);
D::f(?, ...)(0);

D::g(0);
D::g(?, ...)(0);

?>
--EXPECT--
string(1) "C"
string(1) "C"
string(1) "C"
string(1) "C"
string(1) "C"
string(1) "C"
2 changes: 1 addition & 1 deletion Zend/tests/partial_application/magic_001.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ Closure [ <user> public method {closure:%s:%d} ] {
Parameter #0 [ <required> mixed $arguments0 ]
}
}
ArgumentCountError: Too few arguments to function Foo::{closure:%s:%d}(), 0 passed in %s on line %d and exactly 1 expected
ArgumentCountError: Too few arguments to function Closure::{closure:%s:%d}(), 0 passed in %s on line %d and exactly 1 expected
Foo::method
int(1)
Foo::method
Expand Down
6 changes: 3 additions & 3 deletions Zend/tests/partial_application/magic_002.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ echo (string) new ReflectionFunction($bar);
$bar(100);
?>
--EXPECTF--
Closure [ <user> static public method {closure:%s:%d} ] {
Closure [ <user> static function {closure:%s:%d} ] {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This matches more what:

<?php

class C {
    private static function priv($a) { echo "private priv\n"; }
    public static function __callStatic($name, $args) { echo "trampoline $name\n"; }
    public static function foo() {}
}

// Sees only C::__callStatic() 
$f = C::priv(...);
echo (string) new ReflectionFunction($f);
$g = C::foo(...);
echo (string) new ReflectionFunction($g);

produces (although that output is also a bit confusing):

Closure [ <internal> static public method priv ] {

  - Parameters [1] {
    Parameter #0 [ <optional> mixed ...$arguments ]
  }
}
Closure [ <user> static public method foo ] {
  @@ /in/AItpa 6 - 6
}

@@ %s 10 - 10

- Parameters [1] {
Expand All @@ -42,7 +42,7 @@ Foo::method
int(1)
Foo::method
int(1)
Closure [ <user> static public method {closure:%s:%d} ] {
Closure [ <user> static function {closure:%s:%d} ] {
@@ %s 17 - 17

- Parameters [2] {
Expand All @@ -55,7 +55,7 @@ int(10)
Foo::method
int(10)
int(20)
Closure [ <user> static public method {closure:%s:%d} ] {
Closure [ <user> static function {closure:%s:%d} ] {
@@ %s 24 - 24

- Bound Variables [1] {
Expand Down
49 changes: 49 additions & 0 deletions Zend/tests/partial_application/magic_scope.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
--TEST--
Magic method scope
--CREDITS--
Ryan @ Calif.io
--FILE--
<?php

class InstanceTarget
{
private function secret(string $value): void
{
echo "PRIVATE-INSTANCE:$value\n";
}

public function __call(string $name, array $arguments): void
{
echo "MAGIC-INSTANCE:$name:" . implode(',', $arguments) . "\n";
}
}

class StaticTarget
{
private static function secret(string $value): void
{
echo "PRIVATE-STATIC:$value\n";
}

public static function __callStatic(string $name, array $arguments): void
{
echo "MAGIC-STATIC:$name:" . implode(',', $arguments) . "\n";
}
}

$instance = new InstanceTarget();

$instance->secret('direct');
StaticTarget::secret('direct');

$instancePartial = $instance->secret(?);
$staticPartial = StaticTarget::secret(?);
$instancePartial('controlled');
$staticPartial('controlled');

?>
--EXPECT--
MAGIC-INSTANCE:secret:direct
MAGIC-STATIC:secret:direct
MAGIC-INSTANCE:secret:controlled
MAGIC-STATIC:secret:controlled
2 changes: 1 addition & 1 deletion Zend/zend_ast.c
Original file line number Diff line number Diff line change
Expand Up @@ -1349,7 +1349,7 @@ static zend_result ZEND_FASTCALL zend_ast_evaluate_inner(
if (uses_variadic_placeholder) {
flags |= ZEND_PARTIAL_USES_VARIADIC_PLACEHOLDER;
}
zend_partial_create(result, &frame->This, fptr,
zend_partial_create(result, scope, &frame->This, fptr,
ZEND_CALL_NUM_ARGS(frame), ZEND_CALL_ARG(frame, 1),
extra_named_params, named_positions,
fcc_ast->filename, &ast->lineno,
Expand Down
12 changes: 10 additions & 2 deletions Zend/zend_partial.c
Original file line number Diff line number Diff line change
Expand Up @@ -1103,7 +1103,7 @@ static void zp_bind(zval *result, zend_function *function, uint32_t argc, zval *
}
}

void zend_partial_create(zval *result, zval *this_ptr, zend_function *function,
void zend_partial_create(zval *result, zend_class_entry *scope, zval *this_ptr, zend_function *function,
uint32_t argc, zval *argv, zend_array *extra_named_params,
const zend_array *named_positions,
zend_string *declaring_filename,
Expand Down Expand Up @@ -1139,8 +1139,16 @@ void zend_partial_create(zval *result, zval *this_ptr, zend_function *function,
ZVAL_UNDEF(&object);
}


/* We conveniently use the function's scope for the scope of the generated closure as this allows const exprs
* referencing self:: or parent:: to behave normally without rewriting them.
* This affects method resolution for magic methods, so use the actual scope for them. */
if (!(function->common.fn_flags & ZEND_ACC_CALL_VIA_TRAMPOLINE)) {
scope = function->common.scope;
}

zend_create_partial_closure(result, (zend_function*)op_array,
function->common.scope, called_scope, &object,
scope, called_scope, &object,
(function->common.fn_flags & ZEND_ACC_CLOSURE) != 0);

zp_bind(result, function, argc, argv, extra_named_params, const_args);
Expand Down
2 changes: 1 addition & 1 deletion Zend/zend_partial.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ BEGIN_EXTERN_C()
* 'declaring_lineno_ptr' should be a pointer the zend_op.lineno or
* zend_ast.lineno that declares the PFA. The address is used to build a cache
* key. */
void zend_partial_create(zval *result, zval *this_ptr, zend_function *function,
void zend_partial_create(zval *result, zend_class_entry *scope, zval *this_ptr, zend_function *function,
uint32_t argc, zval *argv, zend_array *extra_named_params,
const zend_array *named_positions,
zend_string *declaring_filename,
Expand Down
2 changes: 1 addition & 1 deletion Zend/zend_vm_def.h
Original file line number Diff line number Diff line change
Expand Up @@ -9897,7 +9897,7 @@ ZEND_VM_HANDLER(212, ZEND_CALLABLE_CONVERT_PARTIAL, CONST, CONST|UNUSED, NUM)
}

zend_partial_create(EX_VAR(opline->result.var),
&call->This, call->func,
EX(func)->common.scope, &call->This, call->func,
ZEND_CALL_NUM_ARGS(call), ZEND_CALL_ARG(call, 1),
(ZEND_CALL_INFO(call) & ZEND_CALL_HAS_EXTRA_NAMED_PARAMS) ?
call->extra_named_params : NULL,
Expand Down
8 changes: 4 additions & 4 deletions Zend/zend_vm_execute.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading