From 9e926d1bfb83c781bc43dbed889465602dda1beb Mon Sep 17 00:00:00 2001 From: CismonX Date: Tue, 23 Apr 2019 21:38:51 +0800 Subject: [PATCH] update --- src/complex.cc | 27 ++++++++++++++++++++++++++- src/complex.hh | 1 + tests/001-complex.phpt | 5 ++++- 3 files changed, 31 insertions(+), 2 deletions(-) diff --git a/src/complex.cc b/src/complex.cc index 1ff3b85..a216057 100644 --- a/src/complex.cc +++ b/src/complex.cc @@ -299,7 +299,31 @@ namespace php_arma } template - void complex::write_property(zval *obj, zval *member, zval *value, void **unused) + zval *complex::read_dimension(zval *obj, zval *offset, int type, zval*) + { + if (UNEXPECTED(Z_TYPE_P(offset) != IS_LONG)) { + ex_bad_type("int", zval_get_type_name(offset)); + return nullptr; + } + if (UNEXPECTED(type != BP_VAR_R)) { + zend_throw_exception_ex(zend_ce_exception, -2, + "Numeric offset of class %s is read-only", Z_OBJNAME_P(obj)); + return nullptr; + } + + auto zobj = Z_OBJ_P(obj); + auto idx = Z_LVAL_P(offset); + if (EXPECTED(idx == 0 || idx == 1)) { + return OBJ_PROP_NUM(zobj, idx); + } + + zend_throw_exception_ex(zend_ce_exception, -2, + "Bad offset for %s, expected 0 or 1, %ld given.", Z_OBJNAME_P(obj), idx); + return nullptr; + } + + template + void complex::write_property(zval *obj, zval *member, zval *value, void**) { // Theoretically we won't get non-string values here, add type check just in case. if (UNEXPECTED(Z_TYPE_P(member) != IS_STRING)) { @@ -382,6 +406,7 @@ namespace php_arma property_declare(ce, "real"); property_declare(ce, "imag"); object_handlers_init(&handlers); + handlers.read_dimension = read_dimension; handlers.write_property = write_property; handlers.compare_objects = compare_objects; } diff --git a/src/complex.hh b/src/complex.hh index 4894185..bbb78d7 100644 --- a/src/complex.hh +++ b/src/complex.hh @@ -103,6 +103,7 @@ namespace php_arma static PHP_FUNCTION(acosh); static PHP_FUNCTION(atanh); + static zval *read_dimension(zval*, zval*, int, zval*); static void write_property(zval*, zval*, zval*, void**); static int compare_objects(zval*, zval*); diff --git a/tests/001-complex.phpt b/tests/001-complex.phpt index ca032e9..220b4f0 100644 --- a/tests/001-complex.phpt +++ b/tests/001-complex.phpt @@ -11,10 +11,13 @@ is_php_arma_loaded(); require_once 'includes/assert.php'; $cx_double = new Arma\CxDouble(1.2, 2.4); +[$real, $imag] = $cx_double; batch_assert('Arma\\Complex', [1.2, $cx_double->real], - [2.4, $cx_double->imag] + [2.4, $cx_double->imag], + [$cx_double->real, $real], + [$cx_double->imag, $imag] ); $cx_double->real *= 1.1;