Skip to content

Commit f76ae0e

Browse files
committed
Make array() a function
1 parent 16a3fb1 commit f76ae0e

File tree

8 files changed

+140
-4
lines changed

8 files changed

+140
-4
lines changed

Zend/zend_ast.c

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2173,9 +2173,20 @@ static ZEND_COLD void zend_ast_export_ex(smart_str *str, zend_ast *ast, int prio
21732173
zend_ast_export_list(str, (zend_ast_list*)ast, 1, 20, indent);
21742174
break;
21752175
case ZEND_AST_ARRAY:
2176-
smart_str_appendc(str, '[');
2177-
zend_ast_export_list(str, (zend_ast_list*)ast, 1, 20, indent);
2178-
smart_str_appendc(str, ']');
2176+
switch (ast->attr) {
2177+
case ZEND_ARRAY_SYNTAX_LONG:
2178+
case ZEND_ARRAY_SYNTAX_FUNCTION:
2179+
smart_str_appends(str, "array(");
2180+
zend_ast_export_list(str, (zend_ast_list*)ast, 1, 20, indent);
2181+
smart_str_appendc(str, ')');
2182+
break;
2183+
case ZEND_ARRAY_SYNTAX_SHORT:
2184+
default:
2185+
smart_str_appendc(str, '[');
2186+
zend_ast_export_list(str, (zend_ast_list*)ast, 1, 20, indent);
2187+
smart_str_appendc(str, ']');
2188+
break;
2189+
}
21792190
break;
21802191
case ZEND_AST_ENCAPS_LIST:
21812192
smart_str_appendc(str, '"');

Zend/zend_builtin_functions.c

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,43 @@ zend_result zend_startup_builtin_functions(void) /* {{{ */
6969
}
7070
/* }}} */
7171

72+
ZEND_FUNCTION(array)
73+
{
74+
zval *args;
75+
uint32_t argc;
76+
HashTable *named_params;
77+
78+
ZEND_PARSE_PARAMETERS_START(0, -1)
79+
Z_PARAM_VARIADIC_WITH_NAMED(args, argc, named_params);
80+
ZEND_PARSE_PARAMETERS_END();
81+
82+
if (EXPECTED(argc == 0)) {
83+
if (EXPECTED(named_params != NULL)) {
84+
GC_ADDREF(named_params);
85+
RETURN_ARR(named_params);
86+
} else {
87+
RETURN_EMPTY_ARRAY();
88+
}
89+
} else {
90+
HashTable *entries;
91+
92+
ALLOC_HASHTABLE(entries);
93+
zend_hash_init(entries, argc + (named_params ? zend_hash_num_elements(named_params) : 0), NULL, NULL, false);
94+
for (uint32_t i = 0; i < argc; i++) {
95+
zend_hash_index_add_new(entries, i, &args[i]);
96+
}
97+
if (named_params != NULL) {
98+
zend_string *key;
99+
zval *val;
100+
ZEND_HASH_FOREACH_STR_KEY_VAL(named_params, key, val) {
101+
zend_hash_update(entries, key, val);
102+
} ZEND_HASH_FOREACH_END();
103+
}
104+
105+
RETURN_ARR(entries);
106+
}
107+
}
108+
72109
ZEND_FUNCTION(exit)
73110
{
74111
zend_string *str = NULL;

Zend/zend_builtin_functions.stub.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ class stdClass
77
{
88
}
99

10+
function _array(mixed ...$entries): array {}
11+
1012
function exit(string|int $status = 0): never {}
1113

1214
/** @alias exit */

Zend/zend_builtin_functions_arginfo.h

Lines changed: 7 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Zend/zend_compile.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1053,6 +1053,7 @@ ZEND_API zend_string *zend_type_to_string(zend_type type);
10531053
#define ZEND_ARRAY_SYNTAX_LIST 1 /* list() */
10541054
#define ZEND_ARRAY_SYNTAX_LONG 2 /* array() */
10551055
#define ZEND_ARRAY_SYNTAX_SHORT 3 /* [] */
1056+
#define ZEND_ARRAY_SYNTAX_FUNCTION 4 /* [] */
10561057

10571058
/* var status for backpatching */
10581059
#define BP_VAR_R 0

Zend/zend_language_parser.y

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,7 @@ static YYSIZE_T zend_yytnamerr(char*, const char*);
286286
%type <ast> function_name non_empty_member_modifiers
287287
%type <ast> property_hook property_hook_list optional_property_hook_list hooked_property property_hook_body
288288
%type <ast> optional_parameter_list
289+
%type <ast> non_empty_array_function_argument_list array_function_argument
289290

290291
%type <num> returns_ref function fn is_reference is_variadic property_modifiers property_hook_modifiers
291292
%type <num> method_modifiers class_const_modifiers member_modifier optional_cpp_modifiers
@@ -1447,11 +1448,30 @@ ctor_arguments:
14471448

14481449
dereferenceable_scalar:
14491450
T_ARRAY '(' array_pair_list ')' { $$ = $3; $$->attr = ZEND_ARRAY_SYNTAX_LONG; }
1451+
| T_ARRAY '(' non_empty_array_function_argument_list possible_comma ')'
1452+
{ $$ = $3; $$->attr = ZEND_ARRAY_SYNTAX_FUNCTION; }
1453+
| T_ARRAY '(' T_ELLIPSIS ')' {
1454+
zend_ast *name = zend_ast_create_zval_from_str(ZSTR_KNOWN(ZEND_STR_ARRAY));
1455+
name->attr = ZEND_NAME_FQ;
1456+
$$ = zend_ast_create(ZEND_AST_CALL, name, zend_ast_create_fcc());
1457+
}
14501458
| '[' array_pair_list ']' { $$ = $2; $$->attr = ZEND_ARRAY_SYNTAX_SHORT; }
14511459
| T_CONSTANT_ENCAPSED_STRING { $$ = $1; }
14521460
| '"' encaps_list '"' { $$ = $2; }
14531461
;
14541462

1463+
non_empty_array_function_argument_list:
1464+
array_function_argument
1465+
{ $$ = zend_ast_create_list(1, ZEND_AST_ARRAY, $1); }
1466+
| non_empty_array_function_argument_list ',' array_function_argument
1467+
{ $$ = zend_ast_list_add($1, $3); }
1468+
;
1469+
1470+
array_function_argument:
1471+
identifier ':' expr
1472+
{ $$ = zend_ast_create(ZEND_AST_NAMED_ARG, $1, $3); }
1473+
;
1474+
14551475
scalar:
14561476
T_LNUMBER { $$ = $1; }
14571477
| T_DNUMBER { $$ = $1; }

build/gen_stub.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1009,6 +1009,9 @@ class FunctionName implements FunctionOrMethodName {
10091009
private /* readonly */ Name $name;
10101010

10111011
public function __construct(Name $name) {
1012+
if ($name->name === '_array') {
1013+
$name = new Name('array', $name->getAttributes());
1014+
}
10121015
$this->name = $name;
10131016
}
10141017

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
--TEST--
2+
Test array() function
3+
--FILE--
4+
<?php
5+
var_dump(\array(1, 2, 3));
6+
var_dump(\array(foo: "bar", baz: "quux"));
7+
var_dump(array(foo: "bar", baz: "quux"));
8+
9+
try {
10+
assert(false && \array(foo: "bar", baz: "quux"));
11+
} catch (AssertionError $e) {
12+
echo $e->getMessage(), PHP_EOL;
13+
}
14+
15+
var_dump(array_map(array(...), [1, 2, 3]));
16+
17+
?>
18+
--EXPECT--
19+
array(3) {
20+
[0]=>
21+
int(1)
22+
[1]=>
23+
int(2)
24+
[2]=>
25+
int(3)
26+
}
27+
array(2) {
28+
["foo"]=>
29+
string(3) "bar"
30+
["baz"]=>
31+
string(4) "quux"
32+
}
33+
array(2) {
34+
["bar"]=>
35+
string(3) "foo"
36+
["quux"]=>
37+
string(3) "baz"
38+
}
39+
assert(false && \array(foo: 'bar', baz: 'quux'))
40+
array(3) {
41+
[0]=>
42+
array(1) {
43+
[0]=>
44+
int(1)
45+
}
46+
[1]=>
47+
array(1) {
48+
[0]=>
49+
int(2)
50+
}
51+
[2]=>
52+
array(1) {
53+
[0]=>
54+
int(3)
55+
}
56+
}

0 commit comments

Comments
 (0)