From abc3b4fbf996a647bcefb02e4ecf643b659577c9 Mon Sep 17 00:00:00 2001 From: Emiel Bruijntjes Date: Sat, 1 Mar 2014 20:04:19 +0100 Subject: array access operators can now be used to access array properties --- src/value.cpp | 57 ++++++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 42 insertions(+), 15 deletions(-) (limited to 'src/value.cpp') diff --git a/src/value.cpp b/src/value.cpp index c948dcd..8827e6e 100644 --- a/src/value.cpp +++ b/src/value.cpp @@ -1382,8 +1382,14 @@ bool Value::contains(const char *key, int size) const } else if (isObject()) { - // @todo implementation - return false; + // retrieve the class entry + auto *entry = zend_get_class_entry(_val); + + // read the property + zval *property = zend_read_property(entry, _val, key, size, 0); + + // check if valid + return property != nullptr; } else { @@ -1440,8 +1446,14 @@ Value Value::get(const char *key, int size) const } else { - // @todo implementation for objects - return Value(); + // retrieve the class entry + auto *entry = zend_get_class_entry(_val); + + // read the property + zval *property = zend_read_property(entry, _val, key, size, 1); + + // wrap in value + return Value(property); } } @@ -1500,22 +1512,37 @@ const Value &Value::set(const char *key, int size, const Value &value) // skip if nothing is going to change if (value._val == *current) return value; } + + // is this an object? + if (isObject()) + { + // if this is not a reference variable, we should detach it to implement copy on write + SEPARATE_ZVAL_IF_NOT_REF(&_val); - // must be an array - setType(arrayType); + // retrieve the class entry + auto *entry = zend_get_class_entry(_val); - // if this is not a reference variable, we should detach it to implement copy on write - SEPARATE_ZVAL_IF_NOT_REF(&_val); + // update the property + zend_update_property(entry, _val, key, size, value._val); + } + else + { + // must be an array + setType(arrayType); + + // if this is not a reference variable, we should detach it to implement copy on write + SEPARATE_ZVAL_IF_NOT_REF(&_val); + + // add the value (this will reduce the refcount of the current value) + add_assoc_zval_ex(_val, key, size+1, value._val); + + // the variable has one more reference (the array entry) + Z_ADDREF_P(value._val); + } - // add the value (this will reduce the refcount of the current value) - add_assoc_zval_ex(_val, key, size+1, value._val); - - // the variable has one more reference (the array entry) - Z_ADDREF_P(value._val); - // object should stay valid validate(); - + // done return value; } -- cgit v1.2.3