Skip to content

Commit

Permalink
Move some array functions into runtime-common (#1171)
Browse files Browse the repository at this point in the history
  • Loading branch information
apolyakov authored Dec 3, 2024
1 parent ceed6fc commit 58ffb27
Show file tree
Hide file tree
Showing 40 changed files with 835 additions and 1,000 deletions.
51 changes: 51 additions & 0 deletions builtin-functions/kphp-light/array.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,55 @@
<?php

define('SORT_REGULAR', 0);
define('SORT_NUMERIC', 1);
define('SORT_STRING', 2);

function array_intersect_key ($a1 ::: array, $a2 ::: array) ::: ^1;

function array_intersect ($a1 ::: array, $a2 ::: array) ::: ^1;

/** @kphp-extern-func-info cpp_template_call */
function array_merge ($a1 ::: array, $a2 ::: array = array(), $a3 ::: array = array(),
$a4 ::: array = array(), $a5 ::: array = array(), $a6 ::: array = array(),
$a7 ::: array = array(), $a8 ::: array = array(), $a9 ::: array = array(),
$a10 ::: array = array(), $a11 ::: array = array(), $a12 ::: array = array())
::: ^1 | ^2 | ^3 | ^4 | ^5 | ^6 | ^7 | ^8 | ^9 | ^10 | ^11 | ^12;

function array_merge_into (&$a ::: array, $another_array ::: array) ::: void;

function array_shift (&$a ::: array) ::: ^1[*];

function array_unshift (&$a ::: array, $val ::: any) ::: int;

function array_key_exists ($v ::: any, $a ::: array) ::: bool;

function array_search ($val ::: any, $a ::: array, $strict ::: bool = false) ::: mixed;

function array_keys ($a ::: array) ::: mixed[];

function array_keys_as_strings ($a ::: array) ::: string[];

function array_keys_as_ints ($a ::: array) ::: int[];

function array_values ($a ::: array) ::: ^1;

function array_unique ($a ::: array, int $flags = SORT_STRING) ::: ^1;

function array_reserve (&$a ::: array, $int_size ::: int, $string_size ::: int, $make_vector_if_possible ::: bool) ::: void;

function array_reserve_vector (&$a ::: array, $size ::: int) ::: void;

function array_reserve_map_int_keys (&$a ::: array, $size ::: int) ::: void;

function array_reserve_map_string_keys (&$a ::: array, $size ::: int) ::: void;

function array_reserve_from (&$a ::: array, $base ::: array) ::: void;

function shuffle (&$a ::: array) ::: void;

function implode ($s ::: string, $v ::: array) ::: string;

function explode ($delimiter ::: string, $str ::: string, $limit ::: int = PHP_INT_MAX) ::: string[];

/** @kphp-extern-func-info interruptible */
function array_map (callable(^2[*] $x):any $callback, $a ::: array) ::: ^1() [];
1 change: 1 addition & 0 deletions builtin-functions/kphp-light/functions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ require_once __DIR__ . '/string.txt';
require_once __DIR__ . '/server.txt';
require_once __DIR__ . '/kphp-toggles.txt';
require_once __DIR__ . '/kphp_internal.txt';
require_once __DIR__ . '/math.txt';
require_once __DIR__ . '/time.txt';
require_once __DIR__ . '/regex.txt';

Expand Down
35 changes: 14 additions & 21 deletions builtin-functions/kphp-light/kphp_internal.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,50 +13,43 @@ function _hrtime_int(): int;
// hrtime(false) specialization
function _hrtime_array(): int[];


// ===== UNSUPPORTED =====


function _exception_set_location($e, string $filename, int $line): ^1;

// `new $c(...)` => `_by_name_construct($c, ...)`
function _by_name_construct(string $class_name, ...$args) ::: instance<^1>;
// `$c::method()` => `_by_name_call_method($c, 'method')`
function _by_name_call_method(string $class_name, string $method_name, ...$args);
// `$c::CONST` => `_by_name_get_const($c, 'CONST')`
function _by_name_get_const(string $class_name, string $constant);
// `$c::$field` => `_by_name_get_field($c, 'field')`
function _by_name_get_field(string $class_name, string $field);


/**
* @kphp-extern-func-info generate-stub
* @kphp-internal-result-indexing
*/
function _explode_nth($delimiter ::: string, $str ::: string, int $index): string;

/**
* @kphp-extern-func-info generate-stub
* @kphp-internal-result-indexing
*/
function _explode_1($delimiter ::: string, $str ::: string): string;

/**
* @kphp-extern-func-info generate-stub
* @kphp-internal-result-array2tuple
*/
function _explode_tuple2($delimiter ::: string, $str ::: string, int $mask, $limit ::: int = 2+1): tuple(string, string);

/**
* @kphp-extern-func-info generate-stub
* @kphp-internal-result-array2tuple
*/
function _explode_tuple3($delimiter ::: string, $str ::: string, int $mask, $limit ::: int = 3+1): tuple(string, string, string);

/**
* @kphp-extern-func-info generate-stub
* @kphp-internal-result-array2tuple
*/
function _explode_tuple4($delimiter ::: string, $str ::: string, int $mask, $limit ::: int = 4+1): tuple(string, string, string, string);

// ===== UNSUPPORTED =====

function _exception_set_location($e, string $filename, int $line): ^1;

// `new $c(...)` => `_by_name_construct($c, ...)`
function _by_name_construct(string $class_name, ...$args) ::: instance<^1>;
// `$c::method()` => `_by_name_call_method($c, 'method')`
function _by_name_call_method(string $class_name, string $method_name, ...$args);
// `$c::CONST` => `_by_name_get_const($c, 'CONST')`
function _by_name_get_const(string $class_name, string $constant);
// `$c::$field` => `_by_name_get_field($c, 'field')`
function _by_name_get_field(string $class_name, string $field);

/**
* @kphp-internal-param-readonly $str
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

// === SUPPORTED ===

define('M_PI', 3.1415926535897932384626433832795);
define('M_E', 2.7182818284590452354);
define('M_LOG2E', 1.4426950408889634074);
Expand All @@ -18,6 +20,14 @@ define('M_SQRT1_2', 0.70710678118654752440);
define('M_LNPI', 1.14472988584940017414);
define('M_EULER', 0.57721566490153286061);

function mt_rand ($l ::: int = TODO_OVERLOAD, $r ::: int = TODO_OVERLOAD) ::: int;

function mt_getrandmax() ::: int;

function mt_srand ($seed ::: int = PHP_INT_MIN) ::: void;

// === UNSUPPORTED ===

/**
* @kphp-pure-function
*/
Expand Down Expand Up @@ -99,12 +109,6 @@ function rad2deg ($v ::: float) ::: float;
function lcg_value() ::: float;
/** @kphp-extern-func-info generate-stub */
function levenshtein ($str1 ::: string, $str2 ::: string) ::: int;
/** @kphp-extern-func-info generate-stub */
function mt_srand ($seed ::: int = PHP_INT_MIN) ::: void;
/** @kphp-extern-func-info generate-stub */
function mt_rand ($l ::: int = TODO_OVERLOAD, $r ::: int = TODO_OVERLOAD) ::: int;
/** @kphp-extern-func-info generate-stub */
function mt_getrandmax() ::: int;

/** @kphp-extern-func-info generate-stub */
function uniqid ($prefix ::: string = '', $more_entropy ::: bool = false) ::: string;
Expand Down
1 change: 0 additions & 1 deletion builtin-functions/kphp-light/unsupported-functions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ require_once __DIR__ . '/unsupported/error.txt';
require_once __DIR__ . '/unsupported/fork.txt';
require_once __DIR__ . '/unsupported/kml.txt';
require_once __DIR__ . '/unsupported/kphp-tracing.txt';
require_once __DIR__ . '/unsupported/math.txt';
require_once __DIR__ . '/unsupported/memcache.txt';
require_once __DIR__ . '/unsupported/misc.txt';
require_once __DIR__ . '/unsupported/serialize.txt';
Expand Down
36 changes: 1 addition & 35 deletions builtin-functions/kphp-light/unsupported/arrays.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,12 @@ function array_swap_int_keys (&$a ::: array, $idx1 ::: int, $idx2 ::: int) ::: v
function array_chunk ($a ::: array, $chunk_size ::: int, $preserve_keys ::: bool = false) ::: ^1[];

function array_splice (&$a ::: array, $offset ::: int, $length ::: int, $replacement ::: array = array()) ::: ^1;
/** @kphp-extern-func-info cpp_template_call */
function array_merge ($a1 ::: array, $a2 ::: array = array(), $a3 ::: array = array(),
$a4 ::: array = array(), $a5 ::: array = array(), $a6 ::: array = array(),
$a7 ::: array = array(), $a8 ::: array = array(), $a9 ::: array = array(),
$a10 ::: array = array(), $a11 ::: array = array(), $a12 ::: array = array())
::: ^1 | ^2 | ^3 | ^4 | ^5 | ^6 | ^7 | ^8 | ^9 | ^10 | ^11 | ^12;

/**
* @kphp-extern-func-info cpp_template_call cpp_variadic_call
* @kphp-pure-function
*/
function array_merge_recursive (...$a) ::: mixed[];
function array_merge_into (&$a ::: array, $another_array ::: array) ::: void;
/** @kphp-extern-func-info cpp_template_call */
function array_merge_spread ($a1 ::: array, $a2 ::: array = array(), $a3 ::: array = array(),
$a4 ::: array = array(), $a5 ::: array = array(), $a6 ::: array = array(),
Expand All @@ -41,24 +35,13 @@ function array_replace ($base_array ::: array,
$replacements_7 ::: array = array(), $replacements_8 ::: array = array(), $replacements_9 ::: array = array(),
$replacements_10 ::: array = array(), $replacements_11 ::: array = array())
::: ^1 | ^2 | ^3 | ^4 | ^5 | ^6 | ^7 | ^8 | ^9 | ^10 | ^11 | ^12;
function array_intersect_key ($a1 ::: array, $a2 ::: array) ::: ^1;
function array_intersect ($a1 ::: array, $a2 ::: array) ::: ^1;
function array_intersect_assoc ($a1 ::: array, $a2 ::: array, $a3 ::: array = TODO) ::: ^1;
function array_diff_key ($a1 ::: array, $a2 ::: array) ::: ^1;
function array_diff ($a1 ::: array, $a2 ::: array, $a3 ::: array = TODO) ::: ^1;
function array_diff_assoc ($a1 ::: array, $a2 ::: array, $a3 ::: array = TODO) ::: ^1;
function array_reverse ($a ::: array, $preserve_keys ::: bool = false) ::: ^1;
function array_shift (&$a ::: array) ::: ^1[*];
function array_unshift (&$a ::: array, $val ::: any) ::: int;
function array_key_exists ($v ::: any, $a ::: array) ::: bool;
function array_search ($val ::: any, $a ::: array, $strict ::: bool = false) ::: mixed;
function array_find ($val ::: array, callable(^1[*] $x):bool $callback) ::: tuple(mixed, ^1[*]);
function array_rand ($a ::: array, $num ::: int = 1) ::: mixed;
function array_keys ($a ::: array) ::: mixed[];
function array_keys_as_strings ($a ::: array) ::: string[];
function array_keys_as_ints ($a ::: array) ::: int[];
function array_values ($a ::: array) ::: ^1;
function array_unique ($a ::: array, int $flags = SORT_STRING) ::: ^1;
function array_count_values ($a ::: array) ::: int[];
function array_flip ($a ::: array) ::: mixed[];
function in_array ($value ::: any, $a ::: array, $strict ::: bool = false) ::: bool;
Expand All @@ -80,39 +63,22 @@ function array_filter ($a ::: array, callable(^1[*] $x):bool $callback = TODO) :
function array_filter_by_key ($a ::: array, callable(mixed $key):bool $callback) ::: ^1;
/** @kphp-extern-func-info cpp_template_call */
function array_reduce ($a ::: array, callable(^3 | ^2() $carry, ^1[*] $item):any $callback, $initial ::: any) ::: ^2() | ^3;
function array_reserve (&$a ::: array, $int_size ::: int, $string_size ::: int, $make_vector_if_possible ::: bool) ::: void;
function array_reserve_vector (&$a ::: array, $size ::: int) ::: void;
function array_reserve_map_int_keys (&$a ::: array, $size ::: int) ::: void;
function array_reserve_map_string_keys (&$a ::: array, $size ::: int) ::: void;
function array_reserve_from (&$a ::: array, $base ::: array) ::: void;
function array_is_vector ($a ::: array) ::: bool;
function array_is_list ($a ::: array) ::: bool;

define('SORT_REGULAR', 0);
define('SORT_NUMERIC', 1);
define('SORT_STRING', 2);

function asort (&$a ::: array, $flag ::: int = SORT_REGULAR) ::: void;
function arsort (&$a ::: array, $flag ::: int = SORT_REGULAR) ::: void;
function ksort (&$a ::: array, $flag ::: int = SORT_REGULAR) ::: void;
function krsort (&$a ::: array, $flag ::: int = SORT_REGULAR) ::: void;
function natsort (&$a ::: array) ::: void;
function rsort (&$a ::: array, $flag ::: int = SORT_REGULAR) ::: void;
function shuffle (&$a ::: array) ::: void;
function sort (&$a ::: array, $flag ::: int = SORT_REGULAR) ::: void;
function uasort (&$a ::: array, callable(^1[*] $x, ^1[*] $y):int $callback) ::: void;
function uksort (&$a ::: array, callable(mixed $x, mixed $y):int $callback) ::: void;
function usort (&$a ::: array, callable(^1[*] $x, ^1[*] $y):int $callback) ::: void;


/** @kphp-extern-func-info cpp_template_call */
function vk_dot_product ($a ::: array, $b ::: array) ::: ^1[*] | ^2[*];


function to_array_debug(any $instance, bool $with_class_names = false) ::: mixed[];
function instance_to_array(object $instance, $with_class_names ::: bool = false) ::: mixed[];

function implode ($s ::: string, $v ::: array) ::: string;

/** @kphp-extern-func-info generate-stub */
function explode ($delimiter ::: string, $str ::: string, $limit ::: int = PHP_INT_MAX) ::: string[];
Loading

0 comments on commit 58ffb27

Please sign in to comment.