Skip to content
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

Closed
wants to merge 15 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions ext/random/config.m4
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/])
4 changes: 4 additions & 0 deletions ext/random/config.w32
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");
67 changes: 67 additions & 0 deletions ext/random/php_random_module.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
Copy link
Contributor

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.awk

../php-src/ext/tokenizer/php_tokenizer.h
21:#define phpext_tokenizer_ptr &tokenizer_module_entry

../php-src/ext/xsl/php_xsl.h
21:#define phpext_xsl_ptr &xsl_module_entry

Copy link
Contributor Author

@zeriyoshi zeriyoshi Jun 27, 2021

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.

+----------------------------------------------------------------------+
| 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;
Copy link
Contributor

Choose a reason for hiding this comment

The 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 random_module_entry here and below.

(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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I think random_ce_ would be shorter and follow conventions, e.g. spl starts with spl.

The other names in this header are fine.

extern PHPAPI zend_class_entry *spl_ce_SplFixedArray;

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 */
Loading