From cf213d07c50dd667d298464b7c013cf49e504e65 Mon Sep 17 00:00:00 2001 From: CismonX Date: Tue, 23 Apr 2019 17:26:39 +0800 Subject: [PATCH] Add helper function `j()`. Fix bug. Refactor code. --- config.m4 | 1 + src/complex.cc | 18 ++++----- src/functions.cc | 35 ++++++++++++++++++ src/functions.hh | 17 +++++++++ src/mapval.cc | 2 +- src/php_arma.cc | 63 ++++++++++++++++++-------------- src/php_arma.hh | 11 +++++- stubs/functions.php | 13 +++++++ tests/002-complex-operators.phpt | 6 ++- 9 files changed, 124 insertions(+), 42 deletions(-) create mode 100644 src/functions.cc create mode 100644 src/functions.hh create mode 100644 stubs/functions.php diff --git a/config.m4 b/config.m4 index 1e790aa..f477876 100644 --- a/config.m4 +++ b/config.m4 @@ -9,6 +9,7 @@ if test "$PHP_ARMA" != "no"; then ARMA_SRC=" \ src/php_arma.cc \ + src/functions.cc \ src/constants.cc \ src/base.cc \ src/complex.cc \ diff --git a/src/complex.cc b/src/complex.cc index fb98b91..1ff3b85 100644 --- a/src/complex.cc +++ b/src/complex.cc @@ -23,8 +23,8 @@ namespace php_arma zval *real, *imag; ZEND_PARSE_PARAMETERS_START(0, 2) Z_PARAM_OPTIONAL - Z_PARAM_ZVAL(real) - Z_PARAM_ZVAL(imag) + Z_PARAM_ZVAL_DEREF(real) + Z_PARAM_ZVAL_DEREF(imag) ZEND_PARSE_PARAMETERS_END(); auto num_args = EX_NUM_ARGS(); @@ -54,8 +54,8 @@ namespace php_arma { zval *r, *theta; ZEND_PARSE_PARAMETERS_START(2, 2) - Z_PARAM_ZVAL(r) - Z_PARAM_ZVAL(theta) + Z_PARAM_ZVAL_DEREF(r) + Z_PARAM_ZVAL_DEREF(theta) ZEND_PARSE_PARAMETERS_END(); if (!zval_check_scalar(r)) { @@ -74,7 +74,7 @@ namespace php_arma { zval *other; ZEND_PARSE_PARAMETERS_START(1, 1) - Z_PARAM_ZVAL(other) + Z_PARAM_ZVAL_DEREF(other) ZEND_PARSE_PARAMETERS_END(); if (!operators::add(getThis(), other, return_value)) { @@ -87,7 +87,7 @@ namespace php_arma { zval *other; ZEND_PARSE_PARAMETERS_START(1, 1) - Z_PARAM_ZVAL(other) + Z_PARAM_ZVAL_DEREF(other) ZEND_PARSE_PARAMETERS_END(); if (!operators::sub(getThis(), other, return_value)) { @@ -106,7 +106,7 @@ namespace php_arma { zval *other; ZEND_PARSE_PARAMETERS_START(1, 1) - Z_PARAM_ZVAL(other) + Z_PARAM_ZVAL_DEREF(other) ZEND_PARSE_PARAMETERS_END(); if (!operators::mul(getThis(), other, return_value)) { @@ -119,7 +119,7 @@ namespace php_arma { zval *other; ZEND_PARSE_PARAMETERS_START(1, 1) - Z_PARAM_ZVAL(other) + Z_PARAM_ZVAL_DEREF(other) ZEND_PARSE_PARAMETERS_END(); if (!operators::div(getThis(), other, return_value)) { @@ -186,7 +186,7 @@ namespace php_arma { zval *other; ZEND_PARSE_PARAMETERS_START(1, 1) - Z_PARAM_ZVAL(other) + Z_PARAM_ZVAL_DEREF(other) ZEND_PARSE_PARAMETERS_END(); if (!operators::pow(getThis(), other, return_value)) { diff --git a/src/functions.cc b/src/functions.cc new file mode 100644 index 0000000..f9de887 --- /dev/null +++ b/src/functions.cc @@ -0,0 +1,35 @@ +// +// php-armadillo/functions.cc +// +// @Author CismonX +// + +#include "functions.hh" +#include "complex.hh" + +#define PHP_ARMA_FE(func) \ + ZEND_RAW_FENTRY("Arma\\" #func, ZEND_FN(func), nullptr, 0) + +namespace php_arma +{ + +#ifdef PHP_ARMA_OPERATORS + PHP_FUNCTION(j) + { + double imag = 0.0; + ZEND_PARSE_PARAMETERS_START(0, 1) + Z_PARAM_OPTIONAL + Z_PARAM_DOUBLE_DEREF(imag) + ZEND_PARSE_PARAMETERS_END(); + + zval_set_scalar(return_value, std::complex(0.0, imag)); + } +#endif // PHP_ARMA_OPERATORS + + const zend_function_entry functions[] = { +#ifdef PHP_ARMA_OPERATORS + PHP_ARMA_FE(j) +#endif // PHP_ARMA_OPERATORS + PHP_FE_END + }; +} diff --git a/src/functions.hh b/src/functions.hh new file mode 100644 index 0000000..f4ff010 --- /dev/null +++ b/src/functions.hh @@ -0,0 +1,17 @@ +// +// php-armadillo/functions.hh +// +// @Author CismonX +// + +#ifndef PHP_ARMA_FUNCTIONS_HH +#define PHP_ARMA_FUNCTIONS_HH + +#include "php_arma.hh" + +namespace php_arma +{ + extern const zend_function_entry functions[]; +} + +#endif // !PHP_ARMA_FUNCTIONS_HH \ No newline at end of file diff --git a/src/mapval.cc b/src/mapval.cc index d3f939a..55b722c 100644 --- a/src/mapval.cc +++ b/src/mapval.cc @@ -34,7 +34,7 @@ namespace php_arma { zval *val; ZEND_PARSE_PARAMETERS_START(1, 1) - Z_PARAM_ZVAL(val) + Z_PARAM_ZVAL_DEREF(val) ZEND_PARSE_PARAMETERS_END(); if (!zval_check_scalar(val)) { diff --git a/src/php_arma.cc b/src/php_arma.cc index 2faa730..3259a1d 100644 --- a/src/php_arma.cc +++ b/src/php_arma.cc @@ -9,6 +9,7 @@ #include "complex.hh" #include "base.hh" #include "mapval.hh" +#include "functions.hh" #ifdef PHP_ARMA_OPERATORS #include "operators.hh" @@ -16,39 +17,45 @@ #include -PHP_MINIT_FUNCTION(arma) +namespace php_arma { - php_arma::constants_init(); - php_arma::complex_init(); - php_arma::base_init(); - php_arma::mapval_init(); + int module_init(INIT_FUNC_ARGS) + { + constants_init(); + complex_init(); + base_init(); + mapval_init(); #ifdef PHP_ARMA_OPERATORS - php_arma::operators_init(); + operators_init(); #endif // PHP_ARMA_OPERATORS - return SUCCESS; -} + return SUCCESS; + } -PHP_MINFO_FUNCTION(arma) -{ - php_info_print_table_start(); - php_info_print_table_header(2, "armadillo support", "enabled"); - php_info_print_table_end(); -} + void module_info(ZEND_MODULE_INFO_FUNC_ARGS) + { + php_info_print_table_start(); + php_info_print_table_header(2, "armadillo support", "enabled"); + php_info_print_table_end(); + } -zend_module_entry arma_module_entry = { - STANDARD_MODULE_HEADER, - "arma", - nullptr, - PHP_MINIT(arma), - nullptr, - nullptr, - nullptr, - PHP_MINFO(arma), - PHP_ARMA_VERSION, - STANDARD_MODULE_PROPERTIES -}; + zend_module_entry module_entry = { + STANDARD_MODULE_HEADER, + "arma", + functions, + module_init, + nullptr, + nullptr, + nullptr, + module_info, + PHP_ARMA_VERSION, + STANDARD_MODULE_PROPERTIES + }; +} #ifdef COMPILE_DL_ARMA -ZEND_GET_MODULE(arma) -#endif +extern "C" ZEND_DLEXPORT zend_module_entry *get_module() +{ + return &php_arma::module_entry; +} +#endif // COMPILE_DL_ARMA diff --git a/src/php_arma.hh b/src/php_arma.hh index f28b04a..17b09e0 100644 --- a/src/php_arma.hh +++ b/src/php_arma.hh @@ -22,8 +22,6 @@ #define ZEND_ACC_CTOR 0 #endif -extern zend_module_entry arma_module_entry; - /// Helper macros for method entry. #define PHP_ARMA_START_ME(cls, ...) \ @@ -65,6 +63,13 @@ extern zend_module_entry arma_module_entry; static constexpr const char val[] = name; \ } +/// Helper macros for parameter parsing. + +#ifndef Z_PARAM_DOUBLE_DEREF +#define Z_PARAM_DOUBLE_DEREF(dest) \ + Z_PARAM_DOUBLE_EX2(dest, _dummy, 0, 1, 0) +#endif // !Z_PARAM_DOUBLE_DEREF + #ifdef PHP_ARMA_OPERATORS /// Helper macros for handling operator overloading. @@ -82,6 +87,8 @@ extern zend_module_entry arma_module_entry; namespace php_arma { + extern zend_module_entry module_entry; + /// Helpers for compile-time string concatenation for better module startup performance. namespace str { diff --git a/stubs/functions.php b/stubs/functions.php new file mode 100644 index 0000000..2b2e4e7 --- /dev/null +++ b/stubs/functions.php @@ -0,0 +1,13 @@ +add($bar), $foo + $bar],