hhvm icon indicating copy to clipboard operation
hhvm copied to clipboard

object arguments of call_user_func_array are being iterated

Open elgenie opened this issue 12 years ago • 5 comments

Example code to compare on hhvm and php5

<?php

function f() { var_dump(func_get_args()); }

class C implements IteratorAggregate {
    private $arr;
    function __construct(array $arr) {$this->arr = $arr;} 
    function getIterator() { return new ArrayIterator($this->arr); } 
}


$a = array('a' => 1, 'b' => 2, 3, 4, 5);
$c = new C($a);

call_user_func_array('f', new stdClass()); // non-Traversable
call_user_func_array('f', $c); // Traversable via IteratorAggregate
call_user_func_array('f', new ArrayIterator($a)); // Traversable via Iterator

http://3v4l.org/ktsfL

PHP 5.5 complains that "call_user_func_array() expects parameter 2 to be array, object given" whereas hhvm iterates the properties of the object

elgenie avatar Jun 23 '14 12:06 elgenie

This is intended, no? It would defeat the purpose of maps/vectors if they couldn't be passed around where arrays are used.

milesj avatar Jul 08 '14 06:07 milesj

Looks like this will be fixed in #3942 (see type annotation).

paulbiss avatar Oct 21 '14 16:10 paulbiss

#3942 won't fix this, it's being casted explicitly in ExecutionContext::doFCallArrayTC / ExecutionContext::iopFCallArray.

Majkl578 avatar Oct 27 '14 00:10 Majkl578

This is still an issue, the function is typed as taking an array but the FCallArray bytecode still doesn't require an array. So anywhere we can't statically infer that call_user_func_array is being invoked we fallback to the builtin which will fail on non-arrays. That being said allowing an iterable doesn't seem unreasonable. The right answer here may be to just allow call_user_func_array to take iterables.

https://3v4l.org/r6nM3

paulbiss avatar Sep 30 '16 04:09 paulbiss

I am going over old issues on this repository, to see which ones apply to the current versions of hhvm.

Hhvm now matches the PHP 7 behavior: Warning: call_user_func_array() expects parameter 2 to be an array or collection, object given in /file.php on line 21

This could theoretically throw a TypeError, but it nolonger iterates over the object properties. There could be a new issue created for this.

lexidor avatar May 14 '20 20:05 lexidor