-
Notifications
You must be signed in to change notification settings - Fork 7.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[RFC] Add Random Extension #7079
Changes from 2 commits
14de122
e2497f4
406225d
8060452
e5d9e7f
cbd47ee
ee845fe
c423e79
1fb7658
0fcefe3
1d0af1d
59b5bbe
3ccc236
af5b15f
f382490
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
dnl | ||
dnl Check for strtoull | ||
dnl | ||
AC_CHECK_FUNCS(strtoull) | ||
|
||
dnl | ||
dnl Setup extension | ||
dnl | ||
PHP_NEW_EXTENSION(random, random.c , , , -DZEND_ENABLE_STATIC_TSRMLS_CACHE=1) | ||
PHP_INSTALL_HEADERS([ext/random/]) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
// vim:ft=javascript | ||
|
||
EXTENSION("random", "random.c", false /* never shared */, '/DZEND_ENABLE_STATIC_TSRMLS_CACHE=1'); | ||
PHP_INSTALL_HEADERS("", "ext/random"); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
+----------------------------------------------------------------------+ | ||
| Copyright (c) The PHP Group | | ||
+----------------------------------------------------------------------+ | ||
| This source file is subject to version 3.01 of the PHP license, | | ||
| that is bundled with this package in the file LICENSE, and is | | ||
| available through the world-wide-web at the following url: | | ||
| https://www.php.net/license/3_01.txt | | ||
| If you did not receive a copy of the PHP license and are unable to | | ||
| obtain it through the world-wide-web, please send a note to | | ||
| license@php.net so we can mail you a copy immediately. | | ||
+----------------------------------------------------------------------+ | ||
| Author: Go Kudo <zeriyoshi@gmail.com> | | ||
+----------------------------------------------------------------------+ | ||
*/ | ||
|
||
#ifndef PHP_RANDOM_MODULE_H | ||
#define PHP_RANDOM_MODULE_H | ||
|
||
extern zend_module_entry php_random_module_entry; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: I don't remember if this affects the build (it might or might not), but the conventional name would be (E.g. if php looks for a symbol with that name when loading a shared library - it very likely might) |
||
#define phpext_random_ptr &php_random_module_entry | ||
|
||
extern PHPAPI zend_class_entry *php_random_ce_Random_NumberGenerator_RandomNumberGenerator; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: I think The other names in this header are fine.
|
||
extern PHPAPI zend_class_entry *php_random_ce_Random_NumberGenerator_XorShift128Plus; | ||
extern PHPAPI zend_class_entry *php_random_ce_Random_NumberGenerator_MT19937; | ||
extern PHPAPI zend_class_entry *php_random_ce_Random_NumberGenerator_Secure; | ||
extern PHPAPI zend_class_entry *php_random_ce_RandomInterface; | ||
extern PHPAPI zend_class_entry *php_random_ce_Random; | ||
|
||
typedef struct _php_random_rng_algo { | ||
const size_t generate_size; | ||
const size_t state_size; | ||
uint64_t (*generate)(void *state); | ||
void (*seed)(void *state, const zend_long seed); /* nullable */ | ||
int (*serialize)(void *state, HashTable *data); /* nullable */ | ||
int (*unserialize)(void *state, HashTable *data); /* nullable */ | ||
} php_random_rng_algo; | ||
|
||
typedef struct _php_random_rng { | ||
const php_random_rng_algo *algo; | ||
void *state; | ||
zend_object std; | ||
} php_random_rng; | ||
|
||
typedef struct _php_random { | ||
php_random_rng *rng; | ||
zend_object std; | ||
} php_random; | ||
|
||
static inline php_random_rng *php_random_rng_from_obj(zend_object *obj) { | ||
return (php_random_rng *)((char *)(obj) - XtOffsetOf(php_random_rng, std)); | ||
} | ||
|
||
static inline php_random *php_random_from_obj(zend_object *obj) { | ||
return (php_random *)((char *)(obj) - XtOffsetOf(php_random, std)); | ||
} | ||
|
||
#define Z_RANDOM_RNG_P(zval) php_random_rng_from_obj(Z_OBJ_P(zval)) | ||
|
||
#define Z_RANDOM_P(zval) php_random_from_obj(Z_OBJ_P(zval)) | ||
|
||
PHPAPI uint64_t php_random_next(php_random *random, bool shift); | ||
PHPAPI zend_long php_random_range(php_random *random, zend_long min, zend_long max); | ||
PHPAPI void php_random_array_data_shuffle(php_random *random, zval *array); | ||
PHPAPI void php_random_string_shuffle(php_random *random, char *str, zend_long len); | ||
|
||
#endif /* PHP_RANDOM_MODULE_H */ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The file name is wrong and will cause build issues, I think. This should be named
php_random.h
instead. See build/print_include.awkThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If changed to php_random.h, the include guard conflicts with php_random.h in ext/standard.
Use PHP_RANDOM_H_MODULE for the include guard only.