Skip to content

Commit da705a1

Browse files
committed
Implement Random class in ext/standard.
1 parent 95a9e55 commit da705a1

27 files changed

+1566
-3
lines changed

ext/reflection/tests/ReflectionExtension_getClassNames_basic.phpt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ $standard = new ReflectionExtension('standard');
88
var_dump($standard->getClassNames());
99
?>
1010
--EXPECT--
11-
array(4) {
11+
array(5) {
1212
[0]=>
1313
string(22) "__PHP_Incomplete_Class"
1414
[1]=>
@@ -17,4 +17,6 @@ array(4) {
1717
string(15) "php_user_filter"
1818
[3]=>
1919
string(9) "Directory"
20+
[4]=>
21+
string(6) "Random"
2022
}

ext/standard/basic_functions.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include "ext/standard/php_uuencode.h"
3535
#include "ext/standard/php_mt_rand.h"
3636
#include "ext/standard/crc32_x86.h"
37+
#include "ext/standard/php_random_class.h"
3738

3839
#ifdef PHP_WIN32
3940
#include "win32/php_win32_globals.h"
@@ -412,6 +413,8 @@ PHP_MINIT_FUNCTION(basic) /* {{{ */
412413

413414
BASIC_MINIT_SUBMODULE(hrtime)
414415

416+
BASIC_MINIT_SUBMODULE(random_class)
417+
415418
return SUCCESS;
416419
}
417420
/* }}} */
@@ -449,6 +452,7 @@ PHP_MSHUTDOWN_FUNCTION(basic) /* {{{ */
449452
BASIC_MSHUTDOWN_SUBMODULE(crypt)
450453
BASIC_MSHUTDOWN_SUBMODULE(random)
451454
BASIC_MSHUTDOWN_SUBMODULE(password)
455+
BASIC_MSHUTDOWN_SUBMODULE(random_class)
452456

453457
return SUCCESS;
454458
}
@@ -564,6 +568,7 @@ PHP_MINFO_FUNCTION(basic) /* {{{ */
564568
BASIC_MINFO_SUBMODULE(mail)
565569
php_info_print_table_end();
566570
BASIC_MINFO_SUBMODULE(assert)
571+
BASIC_MINFO_SUBMODULE(random_class)
567572
}
568573
/* }}} */
569574

ext/standard/config.m4

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -450,7 +450,7 @@ PHP_NEW_EXTENSION(standard, array.c base64.c basic_functions.c browscap.c crc32.
450450
http_fopen_wrapper.c php_fopen_wrapper.c credits.c css.c \
451451
var_unserializer.c ftok.c sha1.c user_filters.c uuencode.c \
452452
filters.c proc_open.c streamsfuncs.c http.c password.c \
453-
random.c net.c hrtime.c crc32_x86.c,,,
453+
random.c net.c hrtime.c crc32_x86.c random_class.c,,,
454454
-DZEND_ENABLE_STATIC_TSRMLS_CACHE=1)
455455

456456
PHP_ADD_MAKEFILE_FRAGMENT

ext/standard/config.w32

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ EXTENSION("standard", "array.c base64.c basic_functions.c browscap.c \
3535
url_scanner_ex.c ftp_fopen_wrapper.c http_fopen_wrapper.c \
3636
php_fopen_wrapper.c credits.c css.c var_unserializer.c ftok.c sha1.c \
3737
user_filters.c uuencode.c filters.c proc_open.c password.c \
38-
streamsfuncs.c http.c flock_compat.c random.c hrtime.c", false /* never shared */,
38+
streamsfuncs.c http.c flock_compat.c random.c hrtime.c random_class.c", false /* never shared */,
3939
'/DZEND_ENABLE_STATIC_TSRMLS_CACHE=1');
4040
PHP_STANDARD = "yes";
4141
ADD_MAKEFILE_FRAGMENT();

ext/standard/php_random_class.h

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
+----------------------------------------------------------------------+
3+
| Copyright (c) The PHP Group |
4+
+----------------------------------------------------------------------+
5+
| This source file is subject to version 3.01 of the PHP license, |
6+
| that is bundled with this package in the file LICENSE, and is |
7+
| available through the world-wide-web at the following url: |
8+
| https://www.php.net/license/3_01.txt |
9+
| If you did not receive a copy of the PHP license and are unable to |
10+
| obtain it through the world-wide-web, please send a note to |
11+
| [email protected] so we can mail you a copy immediately. |
12+
+----------------------------------------------------------------------+
13+
| Author: Go Kudo <[email protected]> |
14+
+----------------------------------------------------------------------+
15+
*/
16+
17+
#ifndef PHP_RANDOM_CLASS_H
18+
#define PHP_RANDOM_CLASS_H
19+
20+
PHP_MINIT_FUNCTION(random_class);
21+
PHP_MSHUTDOWN_FUNCTION(random_class);
22+
PHP_MINFO_FUNCTION(random_class);
23+
24+
extern PHPAPI zend_class_entry *php_ce_random;
25+
26+
typedef struct _php_random_class_algo {
27+
const char* ident;
28+
const unsigned char bytes;
29+
const size_t state_size;
30+
uint64_t (*next)(void *state);
31+
void* (*init)(void);
32+
void (*seed)(void *state, const zend_long seed);
33+
int (*serialize)(void *state, zval *return_value);
34+
int (*unserialize)(void *state, HashTable *data);
35+
} php_random_class_algo;
36+
37+
typedef struct _php_random_class {
38+
const php_random_class_algo *algo;
39+
void *state;
40+
zend_object std;
41+
} php_random_class;
42+
43+
static inline php_random_class *random_class_from_obj(zend_object *obj) {
44+
return (php_random_class *)((char *)(obj) - XtOffsetOf(php_random_class, std));
45+
}
46+
47+
#define Z_RANDOM_CLASS_P(zval) random_class_from_obj(Z_OBJ_P(zval))
48+
49+
#define PHP_RANDOM_CLASS_UNSERIALIZE_PADDING 2
50+
51+
PHPAPI int php_random_class_algo_register(const php_random_class_algo *algo);
52+
PHPAPI void php_random_class_algo_unregister(const char *ident);
53+
PHPAPI const php_random_class_algo* php_random_class_algo_find(const zend_string *ident);
54+
PHPAPI uint64_t php_random_class_next(php_random_class *random_class, zend_object *obj);
55+
PHPAPI zend_long php_random_class_range(php_random_class *random_class, zend_object *obj, zend_long min, zend_long max);
56+
PHPAPI void php_random_class_array_data_shuffle(php_random_class *random_class, zend_object *obj, zval *array);
57+
PHPAPI void php_random_class_string_shuffle(php_random_class *random_class, zend_object *obj, char *str, zend_long len);
58+
59+
#endif

0 commit comments

Comments
 (0)