[config.w32] Warn that propro was discontinued
[m6w6/ext-propro] / src / php_propro_api.c
index c92d6d4a3f7754da92505947919ca129ee9d3073..631f707c3a9a77d9944c77240851a0df6ff4489e 100644 (file)
 
 #define DEBUG_PROPRO 0
 
-static inline zval *get_referenced_zval(zval *ref)
-{
-       while (Z_ISREF_P(ref)) {
-               ref = Z_REFVAL_P(ref);
-       }
-       return ref;
-}
-
-php_property_proxy_t *php_property_proxy_init(zval *container, zend_string *member)
-{
-       php_property_proxy_t *proxy = ecalloc(1, sizeof(*proxy));
+static inline php_property_proxy_object_t *get_propro(zval *object);
+static inline zval *get_proxied_value(zval *object, zval *return_value);
+static inline void set_proxied_value(zval *object, zval *value);
 
-       ZVAL_COPY(&proxy->container, get_referenced_zval(container));
-       proxy->member = zend_string_copy(member);
+static zval *read_dimension(zval *object, zval *offset, int type, zval *return_value);
+static ZEND_RESULT_CODE cast_obj(zval *object, zval *return_value, int type);
+static void write_dimension(zval *object, zval *offset, zval *input_value);
 
-       return proxy;
-}
+#if DEBUG_PROPRO
+/* we do not really care about TS when debugging */
+static int level = 1;
+static const char *inoutstr[] = {"< return","=       "," > enter "};
+static const char *types[] = {
+               "UNDEF",
+               "NULL",
+               "FALSE",
+               "TRUE",
+               "int",
+               "float",
+               "string",
+               "Array",
+               "Object",
+               "resource",
+               "reference",
+               "constant",
+               "const AST",
+               "_BOOL",
+               "callable",
+               "indirect",
+               "---",
+               "pointer"
+};
 
-void php_property_proxy_free(php_property_proxy_t **proxy)
+static const char *_type(zval *zv)
 {
-       if (*proxy) {
-               zval_ptr_dtor(&(*proxy)->container);
-               zend_string_release((*proxy)->member);
-               efree(*proxy);
-               *proxy = NULL;
+       switch (Z_TYPE_P(zv)) {
+       case IS_OBJECT:
+               if (zv->value.obj->ce == php_property_proxy_get_class_entry()) {
+                       return "PROPRO";
+               }
+               /* no break */
+       default:
+               return types[Z_TYPE_P(zv)];
        }
 }
 
-static zend_class_entry *php_property_proxy_class_entry;
-static zend_object_handlers php_property_proxy_object_handlers;
-
-zend_class_entry *php_property_proxy_get_class_entry(void)
+static int _walk(php_property_proxy_object_t *obj)
 {
-       return php_property_proxy_class_entry;
-}
+       int p = 0;
 
-static inline php_property_proxy_object_t *get_propro(zval *object);
-static zval *get_parent_proxied_value(zval *object, zval *return_value);
-static zval *get_proxied_value(zval *object, zval *return_value);
-static zval *read_dimension(zval *object, zval *offset, int type, zval *return_value);
-static ZEND_RESULT_CODE cast_proxied_value(zval *object, zval *return_value, int type);
-static void write_dimension(zval *object, zval *offset, zval *value);
-static void set_proxied_value(zval *object, zval *value);
-
-#if DEBUG_PROPRO
-/* we do not really care about TS when debugging */
-static int level = 1;
-static const char space[] = "                               ";
-static const char *inoutstr[] = {"< return","="," > enter"};
-
-static void _walk(php_property_proxy_object_t *obj)
-{
        if (obj) {
                if (!Z_ISUNDEF(obj->parent)) {
-                       _walk(get_propro(&obj->parent));
+                       p += _walk(get_propro(&obj->parent));
                }
                if (obj->proxy) {
-                       fprintf(stderr, ".%s", obj->proxy->member->val);
+                       p += fprintf(stderr, ".%s", obj->proxy->member->val);
                }
        }
+
+       return p;
 }
 
+static inline zval *get_container(zval *object, zval *tmp);
+static inline zval *get_container_value(zval *container, zend_string *member, zval *return_value);
+
 static void debug_propro(int inout, const char *f,
-               php_property_proxy_object_t *obj, zval *offset, zval *value TSRMLS_DC)
+               php_property_proxy_object_t *obj,
+               php_property_proxy_t *proxy,
+               zval *offset, zval *value)
 {
-       fprintf(stderr, "#PP %p %s %s %s ", obj, &space[sizeof(space)-level],
-                       inoutstr[inout+1], f);
+       int width;
+       zval tmp, *container = &tmp;
+
+       if (!proxy && obj) {
+               proxy = obj->proxy;
+       }
+
+       fprintf(stderr, "#PP %14p %*c %s %s\t", proxy, level, ' ', inoutstr[inout + 1], f);
 
        level += inout;
 
-       _walk(obj);
+       if (proxy) {
+               ZVAL_UNDEF(&tmp);
+               if (!obj) {
+                       container = &proxy->container;
+               } else if (Z_ISUNDEF(obj->parent)) {
+                       container = &obj->proxy->container;
+               }
+               fprintf(stderr, " container= %-14p < %-10s rc=%-2d%s> ",
+                               Z_REFCOUNTED_P(container) ? Z_COUNTED_P(container) : NULL,
+                               _type(container),
+                               Z_REFCOUNTED_P(container) ? Z_REFCOUNT_P(container) : 0,
+                               Z_ISREF_P(container) ? "(ref)  ":"(noref)");
+       }
+
+       width = _walk(obj);
 
        if (*f++=='d'
        &&      *f++=='i'
@@ -106,45 +131,90 @@ static void debug_propro(int inout, const char *f,
                        offset_str = Z_STRVAL_P(o);
                }
 
-               fprintf(stderr, ".%s", offset_str);
+               width += fprintf(stderr, ".%s", offset_str);
 
                if (o && o != offset) {
                        zval_ptr_dtor(o);
                }
        }
-       if (value && !Z_ISUNDEF_P(value)) {
-               const char *t[] = {
-                               "UNDEF",
-                               "NULL",
-                               "FALSE",
-                               "TRUE",
-                               "int",
-                               "float",
-                               "string",
-                               "Array",
-                               "Object",
-                               "resource",
-                               "reference",
-                               "constant",
-                               "constant AST",
-                               "_BOOL",
-                               "callable",
-                               "indirect",
-                               "---",
-                               "pointer"
-               };
-               fprintf(stderr, " = (%s) ", t[Z_TYPE_P(value)&0xf]);
-               if (!Z_ISUNDEF_P(value) && Z_TYPE_P(value) != IS_INDIRECT) {
-                       zend_print_flat_zval_r(value TSRMLS_CC);
+       if (value) {
+               fprintf(stderr, "%.*s", 32-width, "                                ");
+               if (Z_ISUNDEF_P(value)) {
+                       fprintf(stderr, " = UNDEF");
+               } else {
+                       fprintf(stderr, " = %-14p < %-10s rc=%-2d%s> ",
+                                       Z_REFCOUNTED_P(value) ? Z_COUNTED_P(value) : NULL,
+                                       _type(value),
+                                       Z_REFCOUNTED_P(value) ? Z_REFCOUNT_P(value) : 0,
+                                       Z_ISREF_P(value) ? "(ref)  " : "(noref)");
+                       if (!Z_ISUNDEF_P(value) && Z_TYPE_P(value) != IS_INDIRECT) {
+                               zend_print_flat_zval_r(value TSRMLS_CC);
+                       }
                }
        }
 
        fprintf(stderr, "\n");
 }
 #else
-#define debug_propro(l, f, obj, off, val)
+#define debug_propro(l, f, obj, proxy, off, val)
 #endif
 
+php_property_proxy_t *php_property_proxy_init(zval *container, zend_string *member)
+{
+       php_property_proxy_t *proxy = ecalloc(1, sizeof(*proxy));
+#if DEBUG_PROPRO
+       zval offset;
+
+       ZVAL_STR_COPY(&offset, member);
+#endif
+
+       if (container) {
+               ZVAL_COPY(&proxy->container, container);
+       }
+       proxy->member = zend_string_copy(member);
+
+       debug_propro(0, "init", NULL, proxy, &offset, NULL);
+
+#if DEBUG_PROPRO
+       zval_dtor(&offset);
+#endif
+
+       return proxy;
+}
+
+void php_property_proxy_free(php_property_proxy_t **proxy)
+{
+#if DEBUG_PROPRO
+       zval offset;
+
+       ZVAL_STR_COPY(&offset, (*proxy)->member);
+       debug_propro(0, "free", NULL, *proxy, &offset, NULL);
+#endif
+
+       if (*proxy) {
+               if (!Z_ISUNDEF((*proxy)->container)) {
+                       zval_ptr_dtor(&(*proxy)->container);
+                       ZVAL_UNDEF(&(*proxy)->container);
+               }
+               zend_string_release((*proxy)->member);
+               (*proxy)->member = NULL;
+               efree(*proxy);
+               *proxy = NULL;
+       }
+
+#if DEBUG_PROPRO
+       zval_dtor(&offset);
+#endif
+}
+
+static zend_class_entry *php_property_proxy_class_entry;
+static zend_object_handlers php_property_proxy_object_handlers;
+
+zend_class_entry *php_property_proxy_get_class_entry(void)
+{
+       return php_property_proxy_class_entry;
+}
+
 php_property_proxy_object_t *php_property_proxy_object_new_ex(
                zend_class_entry *ce, php_property_proxy_t *proxy)
 {
@@ -161,8 +231,6 @@ php_property_proxy_object_t *php_property_proxy_object_new_ex(
        o->proxy = proxy;
        o->zo.handlers = &php_property_proxy_object_handlers;
 
-       debug_propro(0, "init", o, NULL, NULL);
-
        return o;
 }
 
@@ -175,8 +243,6 @@ static void destroy_obj(zend_object *object)
 {
        php_property_proxy_object_t *o = PHP_PROPRO_PTR(object);
 
-       debug_propro(0, "dtor", o, NULL, NULL);
-
        if (o->proxy) {
                php_property_proxy_free(&o->proxy);
        }
@@ -189,231 +255,311 @@ static void destroy_obj(zend_object *object)
 
 static inline php_property_proxy_object_t *get_propro(zval *object)
 {
-       object = get_referenced_zval(object);
-       switch (Z_TYPE_P(object)) {
-       case IS_OBJECT:
-               break;
+       ZEND_ASSERT(Z_TYPE_P(object) == IS_OBJECT);
+       return PHP_PROPRO_PTR(Z_OBJ_P(object));
+}
 
-       EMPTY_SWITCH_DEFAULT_CASE();
+static HashTable *get_gc(zval *object, zval **table, int *n)
+{
+       php_property_proxy_object_t *o = get_propro(object);
+
+       if (Z_ISUNDEF(o->parent)) {
+               *table = &o->proxy->container;
+       } else {
+               *table = &o->parent;
        }
-       return PHP_PROPRO_PTR(Z_OBJ_P(object));
+       *n = 1;
+       return NULL;
 }
 
-static inline zend_bool got_value(zval *container, zval *value)
+static HashTable *get_debug_info(zval *object, int *is_temp)
 {
-       zval identical;
+       HashTable *ht;
+       zval *zmember;
+       php_property_proxy_object_t *obj = get_propro(object);
 
-       if (!Z_ISUNDEF_P(value)) {
-               if (SUCCESS == is_identical_function(&identical, value, container)) {
-                       if (Z_TYPE(identical) != IS_TRUE) {
-                               return 1;
-                       }
-               }
+       ALLOC_HASHTABLE(ht);
+       zend_hash_init(ht, 3, NULL, NULL, 0);
+
+       if (Z_ISUNDEF(obj->parent)) {
+               zend_hash_str_add_empty_element(ht, "parent", sizeof("parent")-1);
+               zend_hash_str_add(ht, "container", sizeof("container")-1, &obj->proxy->container);
+       } else {
+               zend_hash_str_add(ht, "parent", sizeof("parent")-1, &obj->parent);
+               zend_hash_str_add_empty_element(ht, "container", sizeof("container")-1);
        }
 
-       return 0;
+       zmember = zend_hash_str_add_empty_element(ht, "member", sizeof("member")-1);
+       ZVAL_STR(zmember, obj->proxy->member);
+
+       *is_temp = 1;
+       return ht;
 }
 
-static zval *get_parent_proxied_value(zval *object, zval *return_value)
+static ZEND_RESULT_CODE cast_obj(zval *object, zval *return_value, int type)
 {
-       php_property_proxy_object_t *obj;
+       zval tmp;
 
-       obj = get_propro(object);
-       debug_propro(1, "parent_get", obj, NULL, NULL);
+       ZVAL_UNDEF(&tmp);
+       RETVAL_ZVAL(get_proxied_value(object, &tmp), 1, 0);
 
-       if (obj->proxy) {
-               if (!Z_ISUNDEF(obj->parent)) {
-                       get_proxied_value(&obj->parent, return_value);
-               }
+       debug_propro(0, "cast", get_propro(object), NULL, NULL, return_value);
+
+       if (!Z_ISUNDEF_P(return_value)) {
+               ZVAL_DEREF(return_value);
+               convert_to_explicit_type_ex(return_value, type);
+               return SUCCESS;
        }
 
-       debug_propro(-1, "parent_get", obj, NULL, return_value);
+       return FAILURE;
+}
+
+static zval *get_obj(zval *object, zval *return_value)
+{
+       zval tmp;
 
+       ZVAL_UNDEF(&tmp);
+       RETVAL_ZVAL(get_proxied_value(object, &tmp), 1, 0);
        return return_value;
 }
 
-static zval *get_proxied_value(zval *object, zval *return_value)
+static void set_obj(zval *object, zval *value) {
+       set_proxied_value(object, value);
+}
+
+static inline zval *get_container(zval *object, zval *tmp)
 {
-       zval *hash_value, *ref, prop_tmp;
-       php_property_proxy_object_t *obj;
+       php_property_proxy_object_t *obj = get_propro(object);
+       zval *container;
 
-       obj = get_propro(object);
-       debug_propro(1, "get", obj, NULL, NULL);
+       if (Z_ISUNDEF(obj->parent)) {
+               container = &obj->proxy->container;
+       } else {
+               container = get_proxied_value(&obj->parent, tmp);
+       }
+       return container;
+}
 
-       if (obj->proxy) {
-               if (!Z_ISUNDEF(obj->parent)) {
-                       zval parent_value;
+static inline void set_container(zval *object, zval *container)
+{
+       php_property_proxy_object_t *obj = get_propro(object);
 
-                       ZVAL_UNDEF(&parent_value);
-                       get_parent_proxied_value(object, &parent_value);
+       if (Z_ISUNDEF(obj->parent)) {
+               if (container != &obj->proxy->container) {
+                       zval tmp;
 
-                       if (got_value(&obj->proxy->container, &parent_value)) {
-                               zval_ptr_dtor(&obj->proxy->container);
-                               ZVAL_COPY(&obj->proxy->container, &parent_value);
-                       }
+                       ZVAL_COPY_VALUE(&tmp, &obj->proxy->container);
+                       ZVAL_COPY(&obj->proxy->container, container);
+                       zval_ptr_dtor(&tmp);
                }
+       } else {
+               set_proxied_value(&obj->parent, container);
+       }
+}
 
-               ref = get_referenced_zval(&obj->proxy->container);
-
-               switch (Z_TYPE_P(ref)) {
-               case IS_OBJECT:
-                       RETVAL_ZVAL(zend_read_property(Z_OBJCE_P(ref), ref,
-                                       obj->proxy->member->val, obj->proxy->member->len, 0, &prop_tmp),
-                                       0, 0);
-                       break;
+static inline zval *get_container_value(zval *container, zend_string *member, zval *return_value)
+{
+       zval *found_value = NULL, prop_tmp;
 
-               case IS_ARRAY:
-                       hash_value = zend_symtable_find(Z_ARRVAL_P(ref), obj->proxy->member);
+       ZVAL_DEREF(container);
+       switch (Z_TYPE_P(container)) {
+       case IS_OBJECT:
+               ZVAL_UNDEF(&prop_tmp);
+               found_value = zend_read_property(Z_OBJCE_P(container), container,
+                               member->val, member->len, 0, &prop_tmp);
+               break;
 
-                       if (hash_value) {
-                               RETVAL_ZVAL(hash_value, 0, 0);
-                       }
-                       break;
-               }
+       case IS_ARRAY:
+               found_value = zend_symtable_find(Z_ARRVAL_P(container), member);
+               break;
        }
 
-       debug_propro(-1, "get", obj, NULL, return_value);
+       if (found_value) {
+               RETVAL_ZVAL(found_value, 0, 0);
+       }
 
        return return_value;
 }
 
-static ZEND_RESULT_CODE cast_proxied_value(zval *object, zval *return_value,
-               int type)
+static inline void cleanup_container(zval *object, zval *container, zend_bool separated)
 {
-       get_proxied_value(object, return_value);
+       if (separated) {
+               zval_ptr_dtor(container);
+       }
+}
 
-       debug_propro(0, "cast", get_propro(object), NULL, return_value);
+static inline zend_bool separate_container(zval *object, zval *container)
+{
+       switch (Z_TYPE_P(container)) {
+       case IS_OBJECT:
+               return 0;
 
-       if (!Z_ISUNDEF_P(return_value)) {
-               convert_to_explicit_type_ex(return_value, type);
-               return SUCCESS;
+       case IS_ARRAY:
+               /* always duplicate for PHP-7.0 and 7.1 on travis */
+               ZVAL_ARR(container, zend_array_dup(Z_ARRVAL_P(container)));
+               break;
+
+       case IS_UNDEF:
+               array_init(container);
+               break;
+
+       default:
+               SEPARATE_ZVAL(container);
+               Z_TRY_ADDREF_P(container);
+               convert_to_array(container);
+               break;
        }
 
-       return FAILURE;
+       return 1;
 }
 
-static void set_proxied_value(zval *object, zval *value)
+static inline zval *set_container_value(zval *container, zend_string *member, zval *value)
 {
-       php_property_proxy_object_t *obj;
-       zval *ref;
+       ZVAL_DEREF(container);
+       switch (Z_TYPE_P(container)) {
+       case IS_OBJECT:
+               zend_update_property(Z_OBJCE_P(container), container,
+                               member->val, member->len, value);
+               break;
 
-       obj = get_propro(object);
-       debug_propro(1, "set", obj, NULL, value TSRMLS_CC);
+       case IS_ARRAY:
+               Z_TRY_ADDREF_P(value);
+               if (member) {
+                       value = zend_symtable_update(Z_ARRVAL_P(container), member, value);
+               } else {
+                       value = zend_hash_next_index_insert(Z_ARRVAL_P(container), value);
+               }
+               break;
+
+       default:
+               ZEND_ASSERT(0);
+               break;
+       }
+
+       return value;
+}
+
+
+static zval *get_proxied_value(zval *object, zval *return_value)
+{
+       php_property_proxy_object_t *obj = get_propro(object);
+
+       debug_propro(1, "get", obj, NULL, NULL, NULL);
 
        if (obj->proxy) {
-               if (!Z_ISUNDEF(obj->parent)) {
-                       zval parent_value;
+               zval tmp, *container;
 
-                       ZVAL_UNDEF(&parent_value);
-                       get_parent_proxied_value(object, &parent_value);
+               ZVAL_UNDEF(&tmp);
+               container = get_container(object, &tmp);
 
-                       if (got_value(&obj->proxy->container, &parent_value)) {
-                               zval_ptr_dtor(&obj->proxy->container);
-                               ZVAL_COPY(&obj->proxy->container, &parent_value);
-                       }
-               }
+               return_value = get_container_value(container, obj->proxy->member, return_value);
+       }
+
+       debug_propro(-1, "get", obj, NULL, NULL, return_value);
 
-               ref = get_referenced_zval(&obj->proxy->container);
+       return return_value;
+}
 
-               switch (Z_TYPE_P(ref)) {
-               case IS_OBJECT:
-                       zend_update_property(Z_OBJCE_P(ref), ref, obj->proxy->member->val,
-                                       obj->proxy->member->len, value);
-                       break;
+static void set_proxied_value(zval *object, zval *value)
+{
+       php_property_proxy_object_t *obj = get_propro(object);
 
-               default:
-                       convert_to_array(ref);
-                       /* no break */
+       debug_propro(1, "set", obj, NULL, NULL, value);
 
-               case IS_ARRAY:
-                       Z_TRY_ADDREF_P(value);
-                       zend_symtable_update(Z_ARRVAL_P(ref), obj->proxy->member, value);
-                       break;
-               }
+       if (obj->proxy) {
+               zval tmp, *container;
+               zend_bool separated;
 
-               if (!Z_ISUNDEF(obj->parent)) {
-                       set_proxied_value(&obj->parent, &obj->proxy->container);
-               }
+               Z_TRY_ADDREF_P(value);
+
+               ZVAL_UNDEF(&tmp);
+               container = get_container(object, &tmp);
+               separated = separate_container(object, container);
+               set_container_value(container, obj->proxy->member, value);
+               set_container(object, container);
+               cleanup_container(object, container, separated);
+
+               Z_TRY_DELREF_P(value);
+
+               debug_propro(0, "set", obj, NULL, NULL, value);
        }
 
-       debug_propro(-1, "set", obj, NULL, NULL);
+       debug_propro(-1, "set", obj, NULL, NULL, value);
 }
 
 static zval *read_dimension(zval *object, zval *offset, int type, zval *return_value)
 {
-       zval proxied_value;
+       zval *value, tmp;
        zend_string *member = offset ? zval_get_string(offset) : NULL;
 
-       debug_propro(1, type == BP_VAR_R ? "dim_read" : "dim_read_ref",
-                       get_propro(object), offset, NULL);
+       debug_propro(1, type == BP_VAR_R ? "dim_r" : "dim_R",
+                       get_propro(object), NULL, offset, NULL);
 
-       ZVAL_UNDEF(&proxied_value);
-       get_proxied_value(object, &proxied_value);
+       ZVAL_UNDEF(&tmp);
+       value = get_proxied_value(object, &tmp);
 
-       if (BP_VAR_R == type && member && !Z_ISUNDEF(proxied_value)) {
-               if (Z_TYPE(proxied_value) == IS_ARRAY) {
-                       zval *hash_value = zend_symtable_find(Z_ARRVAL(proxied_value),
-                                       member);
+       if (type == BP_VAR_R || type == BP_VAR_IS) {
+               ZEND_ASSERT(member);
 
-                       if (hash_value) {
-                               RETVAL_ZVAL(hash_value, 1, 0);
-                       }
+               if (!Z_ISUNDEF_P(value)) {
+                       zval tmp;
+
+                       ZVAL_UNDEF(&tmp);
+                       RETVAL_ZVAL(get_container_value(value, member, &tmp), 1, 0);
                }
        } else {
                php_property_proxy_t *proxy;
                php_property_proxy_object_t *proxy_obj;
 
-               if (!Z_ISUNDEF(proxied_value)) {
-                       convert_to_array(&proxied_value);
-                       Z_ADDREF(proxied_value);
-               } else {
-                       array_init(&proxied_value);
-                       set_proxied_value(object, &proxied_value);
+               if (Z_ISUNDEF_P(value)) {
+                       ZVAL_NULL(value);
                }
 
                if (!member) {
-                       member = zend_long_to_str(zend_hash_next_free_element(
-                                       Z_ARRVAL(proxied_value)));
+                       if (Z_TYPE_P(value) == IS_ARRAY) {
+                               member = zend_long_to_str(zend_hash_next_free_element(
+                                       Z_ARRVAL_P(value)));
+                       } else if (Z_TYPE_P(value) != IS_OBJECT){
+                               member = zend_long_to_str(0);
+                       }
                }
 
-               proxy = php_property_proxy_init(&proxied_value, member);
-               zval_ptr_dtor(&proxied_value);
-
+               proxy = php_property_proxy_init(NULL, member);
                proxy_obj = php_property_proxy_object_new_ex(NULL, proxy);
                ZVAL_COPY(&proxy_obj->parent, object);
                RETVAL_OBJ(&proxy_obj->zo);
+
+               debug_propro(0, "dim_R pp", get_propro(object), NULL, offset, return_value);
        }
 
        if (member) {
                zend_string_release(member);
        }
 
-       debug_propro(-1, type == BP_VAR_R ? "dim_read" : "dim_read_ref",
-                       get_propro(object), offset, return_value);
+       debug_propro(-1, type == BP_VAR_R ? "dim_r" : "dim_R",
+                       get_propro(object), NULL, offset, return_value);
 
        return return_value;
 }
 
 static int has_dimension(zval *object, zval *offset, int check_empty)
 {
-       zval proxied_value;
+       zval *value, tmp;
        int exists = 0;
 
-       debug_propro(1, "dim_exists", get_propro(object), offset, NULL);
+       debug_propro(1, "dim_e", get_propro(object), NULL, offset, NULL);
 
-       ZVAL_UNDEF(&proxied_value);
-       get_proxied_value(object, &proxied_value);
-       if (Z_ISUNDEF(proxied_value)) {
-               exists = 0;
-       } else {
+       ZVAL_UNDEF(&tmp);
+       value = get_proxied_value(object, &tmp);
+
+       if (!Z_ISUNDEF_P(value)) {
                zend_string *zs = zval_get_string(offset);
 
-               if (Z_TYPE(proxied_value) == IS_ARRAY) {
-                       zval *zentry = zend_symtable_find(Z_ARRVAL(proxied_value), zs);
+               ZVAL_DEREF(value);
+               if (Z_TYPE_P(value) == IS_ARRAY) {
+                       zval *zentry = zend_symtable_find(Z_ARRVAL_P(value), zs);
 
-                       if (!zentry) {
-                               exists = 0;
-                       } else {
+                       if (zentry) {
                                if (check_empty) {
                                        exists = !Z_ISNULL_P(zentry);
                                } else {
@@ -425,103 +571,88 @@ static int has_dimension(zval *object, zval *offset, int check_empty)
                zend_string_release(zs);
        }
 
-       debug_propro(-1, "dim_exists", get_propro(object), offset, NULL);
+       debug_propro(-1, "dim_e", get_propro(object), NULL, offset, NULL);
 
        return exists;
 }
 
-static void write_dimension(zval *object, zval *offset, zval *value)
+static void write_dimension(zval *object, zval *offset, zval *input_value)
 {
-       zval proxied_value;
+       zval *array, tmp;
+       zend_string *zs = NULL;
+       zend_bool separated;
 
-       debug_propro(1, "dim_write", get_propro(object), offset, value);
+       debug_propro(1, "dim_w", get_propro(object), NULL, offset, input_value);
 
-       ZVAL_UNDEF(&proxied_value);
-       get_proxied_value(object, &proxied_value);
-
-       if (!Z_ISUNDEF(proxied_value)) {
-               if (Z_TYPE(proxied_value) == IS_ARRAY) {
-                       Z_ADDREF(proxied_value);
-               } else {
-                       convert_to_array(&proxied_value);
-               }
-       } else {
-               array_init(&proxied_value);
+       if (offset) {
+               zs = zval_get_string(offset);
        }
 
-       SEPARATE_ZVAL(value);
-       Z_TRY_ADDREF_P(value);
+       ZVAL_UNDEF(&tmp);
+       array = get_proxied_value(object, &tmp);
+       separated = separate_container(object, array);
+       set_container_value(array, zs, input_value);
+       set_proxied_value(object, array);
+       cleanup_container(object, array, separated);
 
-       if (offset) {
-               zend_string *zs = zval_get_string(offset);
-               zend_symtable_update(Z_ARRVAL(proxied_value), zs, value);
+       if (zs) {
                zend_string_release(zs);
-       } else {
-               zend_hash_next_index_insert(Z_ARRVAL(proxied_value), value);
        }
 
-       set_proxied_value(object, &proxied_value);
-
-       debug_propro(-1, "dim_write", get_propro(object), offset, &proxied_value);
-
-       zval_ptr_dtor(&proxied_value);
+       debug_propro(-1, "dim_w", get_propro(object), NULL, offset, input_value);
 }
 
 static void unset_dimension(zval *object, zval *offset)
 {
-       zval proxied_value;
+       zval *array, *value, tmp;
 
-       debug_propro(1, "dim_unset", get_propro(object), offset, NULL);
+       debug_propro(1, "dim_u", get_propro(object), NULL, offset, NULL);
 
-       ZVAL_UNDEF(&proxied_value);
-       get_proxied_value(object, &proxied_value);
+       ZVAL_UNDEF(&tmp);
+       value = get_proxied_value(object, &tmp);
+       array = value;
+       ZVAL_DEREF(array);
 
-       if (Z_TYPE(proxied_value) == IS_ARRAY) {
-               zval *o = offset;
-               ZEND_RESULT_CODE rv;
+       if (Z_TYPE_P(array) == IS_ARRAY) {
+               zend_string *o = zval_get_string(offset);
 
-               convert_to_string_ex(o);
-               rv = zend_symtable_del(Z_ARRVAL(proxied_value), Z_STR_P(o));
-               if (SUCCESS == rv) {
-                       set_proxied_value(object, &proxied_value);
-               }
+               SEPARATE_ARRAY(array);
+               zend_symtable_del(Z_ARRVAL_P(array), o);
 
-               if (o != offset) {
-                       zval_ptr_dtor(o);
-               }
+               set_proxied_value(object, value);
+
+               zend_string_release(o);
        }
 
-       debug_propro(-1, "dim_unset", get_propro(object), offset, &proxied_value);
+       debug_propro(-1, "dim_u", get_propro(object), NULL, offset, NULL);
 }
 
 ZEND_BEGIN_ARG_INFO_EX(ai_propro_construct, 0, 0, 2)
-       ZEND_ARG_INFO(1, object)
+       ZEND_ARG_INFO(0, object)
        ZEND_ARG_INFO(0, member)
        ZEND_ARG_OBJ_INFO(0, parent, php\\PropertyProxy, 1)
 ZEND_END_ARG_INFO();
 static PHP_METHOD(propro, __construct) {
        zend_error_handling zeh;
-       zval *container, *parent = NULL;
+       zval *reference, *parent = NULL;
        zend_string *member;
 
        zend_replace_error_handling(EH_THROW, NULL, &zeh);
-       if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "zS|O!",
-                       &container, &member, &parent,
+       if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "o!S|O!",
+                       &reference, &member, &parent,
                        php_property_proxy_class_entry)) {
                php_property_proxy_object_t *obj;
-               zval *ref = get_referenced_zval(container);
-
-               switch (Z_TYPE_P(ref)) {
-               case IS_OBJECT:
-               case IS_ARRAY:
-                       break;
-               default:
-                       convert_to_array(ref);
-               }
+
                obj = get_propro(getThis());
-               obj->proxy = php_property_proxy_init(container, member);
+
                if (parent) {
                        ZVAL_COPY(&obj->parent, parent);
+                       obj->proxy = php_property_proxy_init(NULL, member);
+               } else if (reference) {
+                       zval *container = reference;
+                       obj->proxy = php_property_proxy_init(container, member);
+               } else {
+                       php_error(E_WARNING, "Either object or parent must be set");
                }
        }
        zend_restore_error_handling(&zeh);
@@ -546,9 +677,11 @@ static PHP_MINIT_FUNCTION(propro)
                        sizeof(zend_object_handlers));
        php_property_proxy_object_handlers.offset = XtOffsetOf(php_property_proxy_object_t, zo);
        php_property_proxy_object_handlers.free_obj = destroy_obj;
-       php_property_proxy_object_handlers.set = set_proxied_value;
-       php_property_proxy_object_handlers.get = get_proxied_value;
-       php_property_proxy_object_handlers.cast_object = cast_proxied_value;
+       php_property_proxy_object_handlers.get_gc = get_gc;
+       php_property_proxy_object_handlers.get_debug_info = get_debug_info;
+       php_property_proxy_object_handlers.set = set_obj;
+       php_property_proxy_object_handlers.get = get_obj;
+       php_property_proxy_object_handlers.cast_object = cast_obj;
        php_property_proxy_object_handlers.read_dimension = read_dimension;
        php_property_proxy_object_handlers.write_dimension = write_dimension;
        php_property_proxy_object_handlers.has_dimension = has_dimension;