Merge branch 'v1.0.x'
authorMichael Wallner <mike@php.net>
Wed, 30 Sep 2015 09:24:31 +0000 (11:24 +0200)
committerMichael Wallner <mike@php.net>
Wed, 30 Sep 2015 09:24:31 +0000 (11:24 +0200)
17 files changed:
.editorconfig
.gitattributes
.gitignore
BUGS [new file with mode: 0644]
CREDITS
README.md
TODO [new file with mode: 0644]
config.m4
config.w32
config0.m4 [new file with mode: 0644]
gen_travis_yml.php [deleted file]
package.xml
php_propro.c [deleted file]
php_propro_api.h [deleted file]
scripts/gen_travis_yml.php [new file with mode: 0755]
src/php_propro_api.c [new file with mode: 0644]
src/php_propro_api.h [new file with mode: 0644]

index 84c99337f69133bbb1548f9cc8c8477bc6f4ac2e..9b444aed05bf1b5de1a0aa8f5ea6756669f6f48d 100644 (file)
@@ -14,3 +14,10 @@ trim_trailing_whitespace = false
 [*.json]
 indent_style = space
 indent_size = 4
+
+[package.xml]
+indent_style = space
+indent_size = 1
+
+[config.w32]
+end_of_line = crlf
index f6d1a5eff172abbbf2617957bbf0710a6f4df56f..9d51e13117c080b475b2f61709e1696bd390313c 100644 (file)
@@ -1,2 +1,3 @@
 package.xml            merge=touch
 php_propro.h   merge=touch
+config.w32             eol=crlf
index a59cb22c2f70b0573db6521133a6829bd384f793..82c850faef8ac5714daff8139332ae5cf44845d8 100644 (file)
@@ -3,8 +3,8 @@
 *~
 /*.tgz
 /.deps
-/*.lo
-/*.la
+*.lo
+*.la
 /config.[^wm]*
 /configure*
 /lib*
@@ -21,3 +21,5 @@
 /autom4te*
 /.dep.inc
 run-tests.php
+.libs/
+/php_propro_api.h
diff --git a/BUGS b/BUGS
new file mode 100644 (file)
index 0000000..75711d2
--- /dev/null
+++ b/BUGS
@@ -0,0 +1 @@
+Yay, now known and unresolved issues yet!
diff --git a/CREDITS b/CREDITS
index 28b53edeac922443f698902acb434d3e0256b485..920bbcbd04458c0db57b19ea346156bcbdea5c34 100644 (file)
--- a/CREDITS
+++ b/CREDITS
@@ -1 +1,2 @@
-propro
\ No newline at end of file
+propro
+Michael Wallner
index 333d2c1d5d77213b5d6fb25c674605980b673eac..4c3eb31951a19f18bb444830860f0a3c82b32115 100644 (file)
--- a/README.md
+++ b/README.md
@@ -2,12 +2,15 @@
 
 [![Build Status](https://travis-ci.org/m6w6/ext-propro.svg?branch=master)](https://travis-ci.org/m6w6/ext-propro)
 
-Property Proxy
+The "Property Proxy" extension provides a fairly transparent proxy for internal
+object properties hidden in custom non-zval implementations.
 
 ## Documentation
 
 See the [online markdown reference](https://mdref.m6w6.name/propro).
 
+Known issues are listed in [BUGS](./BUGS) and future ideas can be found in [TODO](./TODO).
+
 ## Installing
 
 ### PECL
diff --git a/TODO b/TODO
new file mode 100644 (file)
index 0000000..e69de29
index 1a4965edf4ee3aa65994c079f934f55edadb0ece..3b883c86a242ce14ea2ad46c39bf5353ab320d63 100644 (file)
--- a/config.m4
+++ b/config.m4
@@ -1,7 +1 @@
-PHP_ARG_ENABLE(propro, whether to enable property proxy support,
-[  --enable-propro           Enable property proxy support])
-
-if test "$PHP_PROPRO" != "no"; then
-       PHP_INSTALL_HEADERS(ext/propro, php_propro.h php_propro_api.h)
-       PHP_NEW_EXTENSION(propro, php_propro.c, $ext_shared)
-fi
+sinclude(config0.m4)
index 53ab56e485d9abb36e6bc3891385599c4846127d..ed086e88d07350b5afefa679da2f884f08cd2dec 100644 (file)
@@ -1,9 +1,16 @@
-\r
-ARG_ENABLE("propro", "for propro support", "no");\r
-\r
-if (PHP_PROPRO == "yes") {\r
-               EXTENSION("propro", "php_propro.c");\r
-\r
-               AC_DEFINE("HAVE_PROPRO", 1);\r
-               PHP_INSTALL_HEADERS("ext/propro", "php_propro.h");\r
-}\r
+
+ARG_ENABLE("propro", "for propro support", "no");
+
+if (PHP_PROPRO == "yes") {
+       var PHP_PROPRO_HEADERS=glob("src/*.h"), PHP_PROPRO_SOURCES=glob("src/*.c");
+
+       EXTENSION("propro", PHP_PROPRO_SOURCES);
+       PHP_INSTALL_HEADERS("ext/propro", "php_propro.h");
+       for (var i=0; i<PHP_PROPRO_HEADERS.length; ++i) {
+               var basename = FSO.GetFileName(PHP_PROPRO_HEADERS[i]);
+               copy_and_subst(PHP_PROPRO_HEADERS[i], basename, []);
+               PHP_INSTALL_HEADERS("ext/propro", basename);
+       }
+
+       AC_DEFINE("HAVE_PROPRO", 1);
+}
diff --git a/config0.m4 b/config0.m4
new file mode 100644 (file)
index 0000000..d744578
--- /dev/null
@@ -0,0 +1,24 @@
+PHP_ARG_ENABLE(propro, whether to enable property proxy support,
+[  --enable-propro         Enable property proxy support])
+
+if test "$PHP_PROPRO" != "no"; then
+       PHP_PROPRO_SRCDIR=PHP_EXT_SRCDIR(propro)
+       PHP_PROPRO_BUILDDIR=PHP_EXT_BUILDDIR(propro)
+
+       PHP_ADD_INCLUDE($PHP_PROPRO_SRCDIR/src)
+       PHP_ADD_BUILD_DIR($PHP_PROPRO_BUILDDIR/src)
+
+       PHP_PROPRO_HEADERS=`(cd $PHP_PROPRO_SRCDIR/src && echo *.h)`
+       PHP_PROPRO_SOURCES=`(cd $PHP_PROPRO_SRCDIR && echo src/*.c)`
+
+       PHP_NEW_EXTENSION(propro, $PHP_PROPRO_SOURCES, $ext_shared)
+       PHP_INSTALL_HEADERS(ext/propro, php_propro.h $PHP_PROPRO_HEADERS)
+
+       PHP_SUBST(PHP_PROPRO_HEADERS)
+       PHP_SUBST(PHP_PROPRO_SOURCES)
+
+       PHP_SUBST(PHP_PROPRO_SRCDIR)
+       PHP_SUBST(PHP_PROPRO_BUILDDIR)
+
+       PHP_ADD_MAKEFILE_FRAGMENT
+fi
diff --git a/gen_travis_yml.php b/gen_travis_yml.php
deleted file mode 100755 (executable)
index 0283920..0000000
+++ /dev/null
@@ -1,34 +0,0 @@
-#!/usr/bin/env php
-# autogenerated file; do not edit
-sudo: false
-language: c
-
-addons:
- apt:
-  packages:
-   - php5-cli
-   - php-pear
-
-env:
- matrix:
-<?php
-
-$gen = include "./travis/pecl/gen-matrix.php";
-$env = $gen([
-       "PHP" => ["master"],
-       "enable_debug",
-       "enable_maintainer_zts",
-]);
-foreach ($env as $e) {
-       printf("  - %s\n", $e);
-}
-
-?>
-
-before_script:
- - make -f travis/pecl/Makefile php
- - make -f travis/pecl/Makefile ext PECL=propro
-
-script:
- - make -f travis/pecl/Makefile test
-
index 2b0e14f6accfed40ddc326353da141825e844fde..2ed8460eafd5f3278dfd88732ea3bf06bc1f7d88 100644 (file)
@@ -35,18 +35,25 @@ http://pear.php.net/dtd/package-2.0.xsd">
  <contents>
   <dir name="/">
    <file role="doc" name="AUTHORS"/>
+   <file role="doc" name="BUGS"/>
    <file role="doc" name="CONTRIBUTING.md"/>
    <file role="doc" name="CREDITS"/>
    <file role="doc" name="LICENSE"/>
    <file role="doc" name="README.md"/>
    <file role="doc" name="THANKS"/>
+   <file role="doc" name="TODO"/>
    <file role="src" name="config.m4"/>
+   <file role="src" name="config0.m4"/>
    <file role="src" name="config.w32" />
    <file role="doc" name="Doxyfile" />
    <file role="src" name="php_propro.h" />
-   <file role="src" name="php_propro_api.h" />
-   <file role="src" name="php_propro.c" />
-
+   <dir name="src">
+    <file role="src" name="php_propro_api.h"/>
+    <file role="src" name="php_propro_api.c"/>
+   </dir>
+   <dir name="scripts">
+    <file role="src" name="gen_travis_yml.php"/>
+   </dir>
    <dir name="tests">
     <file role="test" name="001.phpt" />
     <file role="test" name="002.phpt" />
diff --git a/php_propro.c b/php_propro.c
deleted file mode 100644 (file)
index c92d6d4..0000000
+++ /dev/null
@@ -1,597 +0,0 @@
-/*
-    +--------------------------------------------------------------------+
-    | PECL :: propro                                                     |
-    +--------------------------------------------------------------------+
-    | Redistribution and use in source and binary forms, with or without |
-    | modification, are permitted provided that the conditions mentioned |
-    | in the accompanying LICENSE file are met.                          |
-    +--------------------------------------------------------------------+
-    | Copyright (c) 2013 Michael Wallner <mike@php.net>                  |
-    +--------------------------------------------------------------------+
-*/
-
-
-#ifdef HAVE_CONFIG_H
-#      include "config.h"
-#endif
-
-#include <php.h>
-#include <ext/standard/info.h>
-
-#include "php_propro_api.h"
-
-#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));
-
-       ZVAL_COPY(&proxy->container, get_referenced_zval(container));
-       proxy->member = zend_string_copy(member);
-
-       return proxy;
-}
-
-void php_property_proxy_free(php_property_proxy_t **proxy)
-{
-       if (*proxy) {
-               zval_ptr_dtor(&(*proxy)->container);
-               zend_string_release((*proxy)->member);
-               efree(*proxy);
-               *proxy = NULL;
-       }
-}
-
-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;
-}
-
-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));
-               }
-               if (obj->proxy) {
-                       fprintf(stderr, ".%s", obj->proxy->member->val);
-               }
-       }
-}
-
-static void debug_propro(int inout, const char *f,
-               php_property_proxy_object_t *obj, zval *offset, zval *value TSRMLS_DC)
-{
-       fprintf(stderr, "#PP %p %s %s %s ", obj, &space[sizeof(space)-level],
-                       inoutstr[inout+1], f);
-
-       level += inout;
-
-       _walk(obj);
-
-       if (*f++=='d'
-       &&      *f++=='i'
-       &&      *f++=='m'
-       ) {
-               char *offset_str = "[]";
-               zval *o = offset;
-
-               if (o) {
-                       convert_to_string_ex(o);
-                       offset_str = Z_STRVAL_P(o);
-               }
-
-               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);
-               }
-       }
-
-       fprintf(stderr, "\n");
-}
-#else
-#define debug_propro(l, f, obj, off, val)
-#endif
-
-php_property_proxy_object_t *php_property_proxy_object_new_ex(
-               zend_class_entry *ce, php_property_proxy_t *proxy)
-{
-       php_property_proxy_object_t *o;
-
-       if (!ce) {
-               ce = php_property_proxy_class_entry;
-       }
-
-       o = ecalloc(1, sizeof(*o) + sizeof(zval) * (ce->default_properties_count - 1));
-       zend_object_std_init(&o->zo, ce);
-       object_properties_init(&o->zo, ce);
-
-       o->proxy = proxy;
-       o->zo.handlers = &php_property_proxy_object_handlers;
-
-       debug_propro(0, "init", o, NULL, NULL);
-
-       return o;
-}
-
-zend_object *php_property_proxy_object_new(zend_class_entry *ce)
-{
-       return &php_property_proxy_object_new_ex(ce, NULL)->zo;
-}
-
-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);
-       }
-       if (!Z_ISUNDEF(o->parent)) {
-               zval_ptr_dtor(&o->parent);
-               ZVAL_UNDEF(&o->parent);
-       }
-       zend_object_std_dtor(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;
-
-       EMPTY_SWITCH_DEFAULT_CASE();
-       }
-       return PHP_PROPRO_PTR(Z_OBJ_P(object));
-}
-
-static inline zend_bool got_value(zval *container, zval *value)
-{
-       zval identical;
-
-       if (!Z_ISUNDEF_P(value)) {
-               if (SUCCESS == is_identical_function(&identical, value, container)) {
-                       if (Z_TYPE(identical) != IS_TRUE) {
-                               return 1;
-                       }
-               }
-       }
-
-       return 0;
-}
-
-static zval *get_parent_proxied_value(zval *object, zval *return_value)
-{
-       php_property_proxy_object_t *obj;
-
-       obj = get_propro(object);
-       debug_propro(1, "parent_get", obj, NULL, NULL);
-
-       if (obj->proxy) {
-               if (!Z_ISUNDEF(obj->parent)) {
-                       get_proxied_value(&obj->parent, return_value);
-               }
-       }
-
-       debug_propro(-1, "parent_get", obj, NULL, return_value);
-
-       return return_value;
-}
-
-static zval *get_proxied_value(zval *object, zval *return_value)
-{
-       zval *hash_value, *ref, prop_tmp;
-       php_property_proxy_object_t *obj;
-
-       obj = get_propro(object);
-       debug_propro(1, "get", obj, NULL, NULL);
-
-       if (obj->proxy) {
-               if (!Z_ISUNDEF(obj->parent)) {
-                       zval parent_value;
-
-                       ZVAL_UNDEF(&parent_value);
-                       get_parent_proxied_value(object, &parent_value);
-
-                       if (got_value(&obj->proxy->container, &parent_value)) {
-                               zval_ptr_dtor(&obj->proxy->container);
-                               ZVAL_COPY(&obj->proxy->container, &parent_value);
-                       }
-               }
-
-               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;
-
-               case IS_ARRAY:
-                       hash_value = zend_symtable_find(Z_ARRVAL_P(ref), obj->proxy->member);
-
-                       if (hash_value) {
-                               RETVAL_ZVAL(hash_value, 0, 0);
-                       }
-                       break;
-               }
-       }
-
-       debug_propro(-1, "get", obj, NULL, return_value);
-
-       return return_value;
-}
-
-static ZEND_RESULT_CODE cast_proxied_value(zval *object, zval *return_value,
-               int type)
-{
-       get_proxied_value(object, return_value);
-
-       debug_propro(0, "cast", get_propro(object), NULL, return_value);
-
-       if (!Z_ISUNDEF_P(return_value)) {
-               convert_to_explicit_type_ex(return_value, type);
-               return SUCCESS;
-       }
-
-       return FAILURE;
-}
-
-static void set_proxied_value(zval *object, zval *value)
-{
-       php_property_proxy_object_t *obj;
-       zval *ref;
-
-       obj = get_propro(object);
-       debug_propro(1, "set", obj, NULL, value TSRMLS_CC);
-
-       if (obj->proxy) {
-               if (!Z_ISUNDEF(obj->parent)) {
-                       zval parent_value;
-
-                       ZVAL_UNDEF(&parent_value);
-                       get_parent_proxied_value(object, &parent_value);
-
-                       if (got_value(&obj->proxy->container, &parent_value)) {
-                               zval_ptr_dtor(&obj->proxy->container);
-                               ZVAL_COPY(&obj->proxy->container, &parent_value);
-                       }
-               }
-
-               ref = get_referenced_zval(&obj->proxy->container);
-
-               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;
-
-               default:
-                       convert_to_array(ref);
-                       /* no break */
-
-               case IS_ARRAY:
-                       Z_TRY_ADDREF_P(value);
-                       zend_symtable_update(Z_ARRVAL_P(ref), obj->proxy->member, value);
-                       break;
-               }
-
-               if (!Z_ISUNDEF(obj->parent)) {
-                       set_proxied_value(&obj->parent, &obj->proxy->container);
-               }
-       }
-
-       debug_propro(-1, "set", obj, NULL, NULL);
-}
-
-static zval *read_dimension(zval *object, zval *offset, int type, zval *return_value)
-{
-       zval proxied_value;
-       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);
-
-       ZVAL_UNDEF(&proxied_value);
-       get_proxied_value(object, &proxied_value);
-
-       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 (hash_value) {
-                               RETVAL_ZVAL(hash_value, 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 (!member) {
-                       member = zend_long_to_str(zend_hash_next_free_element(
-                                       Z_ARRVAL(proxied_value)));
-               }
-
-               proxy = php_property_proxy_init(&proxied_value, member);
-               zval_ptr_dtor(&proxied_value);
-
-               proxy_obj = php_property_proxy_object_new_ex(NULL, proxy);
-               ZVAL_COPY(&proxy_obj->parent, object);
-               RETVAL_OBJ(&proxy_obj->zo);
-       }
-
-       if (member) {
-               zend_string_release(member);
-       }
-
-       debug_propro(-1, type == BP_VAR_R ? "dim_read" : "dim_read_ref",
-                       get_propro(object), offset, return_value);
-
-       return return_value;
-}
-
-static int has_dimension(zval *object, zval *offset, int check_empty)
-{
-       zval proxied_value;
-       int exists = 0;
-
-       debug_propro(1, "dim_exists", get_propro(object), offset, NULL);
-
-       ZVAL_UNDEF(&proxied_value);
-       get_proxied_value(object, &proxied_value);
-       if (Z_ISUNDEF(proxied_value)) {
-               exists = 0;
-       } else {
-               zend_string *zs = zval_get_string(offset);
-
-               if (Z_TYPE(proxied_value) == IS_ARRAY) {
-                       zval *zentry = zend_symtable_find(Z_ARRVAL(proxied_value), zs);
-
-                       if (!zentry) {
-                               exists = 0;
-                       } else {
-                               if (check_empty) {
-                                       exists = !Z_ISNULL_P(zentry);
-                               } else {
-                                       exists = 1;
-                               }
-                       }
-               }
-
-               zend_string_release(zs);
-       }
-
-       debug_propro(-1, "dim_exists", get_propro(object), offset, NULL);
-
-       return exists;
-}
-
-static void write_dimension(zval *object, zval *offset, zval *value)
-{
-       zval proxied_value;
-
-       debug_propro(1, "dim_write", get_propro(object), offset, 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);
-       }
-
-       SEPARATE_ZVAL(value);
-       Z_TRY_ADDREF_P(value);
-
-       if (offset) {
-               zend_string *zs = zval_get_string(offset);
-               zend_symtable_update(Z_ARRVAL(proxied_value), zs, value);
-               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);
-}
-
-static void unset_dimension(zval *object, zval *offset)
-{
-       zval proxied_value;
-
-       debug_propro(1, "dim_unset", get_propro(object), offset, NULL);
-
-       ZVAL_UNDEF(&proxied_value);
-       get_proxied_value(object, &proxied_value);
-
-       if (Z_TYPE(proxied_value) == IS_ARRAY) {
-               zval *o = offset;
-               ZEND_RESULT_CODE rv;
-
-               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);
-               }
-
-               if (o != offset) {
-                       zval_ptr_dtor(o);
-               }
-       }
-
-       debug_propro(-1, "dim_unset", get_propro(object), offset, &proxied_value);
-}
-
-ZEND_BEGIN_ARG_INFO_EX(ai_propro_construct, 0, 0, 2)
-       ZEND_ARG_INFO(1, 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;
-       zend_string *member;
-
-       zend_replace_error_handling(EH_THROW, NULL, &zeh);
-       if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "zS|O!",
-                       &container, &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);
-               }
-       }
-       zend_restore_error_handling(&zeh);
-}
-
-static const zend_function_entry php_property_proxy_method_entry[] = {
-       PHP_ME(propro, __construct, ai_propro_construct, ZEND_ACC_PUBLIC)
-       {0}
-};
-
-static PHP_MINIT_FUNCTION(propro)
-{
-       zend_class_entry ce = {0};
-
-       INIT_NS_CLASS_ENTRY(ce, "php", "PropertyProxy",
-                       php_property_proxy_method_entry);
-       php_property_proxy_class_entry = zend_register_internal_class(&ce);
-       php_property_proxy_class_entry->create_object = php_property_proxy_object_new;
-       php_property_proxy_class_entry->ce_flags |= ZEND_ACC_FINAL;
-
-       memcpy(&php_property_proxy_object_handlers, zend_get_std_object_handlers(),
-                       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.read_dimension = read_dimension;
-       php_property_proxy_object_handlers.write_dimension = write_dimension;
-       php_property_proxy_object_handlers.has_dimension = has_dimension;
-       php_property_proxy_object_handlers.unset_dimension = unset_dimension;
-
-       return SUCCESS;
-}
-
-PHP_MINFO_FUNCTION(propro)
-{
-       php_info_print_table_start();
-       php_info_print_table_header(2, "Property proxy support", "enabled");
-       php_info_print_table_row(2, "Extension version", PHP_PROPRO_VERSION);
-       php_info_print_table_end();
-}
-
-static const zend_function_entry propro_functions[] = {
-       {0}
-};
-
-zend_module_entry propro_module_entry = {
-       STANDARD_MODULE_HEADER,
-       "propro",
-       propro_functions,
-       PHP_MINIT(propro),
-       NULL,
-       NULL,
-       NULL,
-       PHP_MINFO(propro),
-       PHP_PROPRO_VERSION,
-       STANDARD_MODULE_PROPERTIES
-};
-
-#ifdef COMPILE_DL_PROPRO
-ZEND_GET_MODULE(propro)
-#endif
-
-
-/*
- * Local variables:
- * tab-width: 4
- * c-basic-offset: 4
- * End:
- * vim600: noet sw=4 ts=4 fdm=marker
- * vim<600: noet sw=4 ts=4
- */
diff --git a/php_propro_api.h b/php_propro_api.h
deleted file mode 100644 (file)
index d82055f..0000000
+++ /dev/null
@@ -1,128 +0,0 @@
-/*
-    +--------------------------------------------------------------------+
-    | PECL :: propro                                                     |
-    +--------------------------------------------------------------------+
-    | Redistribution and use in source and binary forms, with or without |
-    | modification, are permitted provided that the conditions mentioned |
-    | in the accompanying LICENSE file are met.                          |
-    +--------------------------------------------------------------------+
-    | Copyright (c) 2013 Michael Wallner <mike@php.net>                  |
-    +--------------------------------------------------------------------+
-*/
-
-#ifndef PHP_PROPRO_API_H
-#define PHP_PROPRO_API_H
-
-#include "php_propro.h"
-
-/**
- * The internal property proxy.
- *
- * Container for the object/array holding the proxied property.
- */
-struct php_property_proxy {
-       /** The container holding the property */
-       zval container;
-       /** The name of the proxied property */
-       zend_string *member;
-};
-typedef struct php_property_proxy php_property_proxy_t;
-
-/**
- * The userland object.
- *
- * Return an object instance of php\\PropertyProxy to make your C-struct
- * member accessible by reference from PHP userland.
- *
- * Example:
- * ~~~~~~~~~~{.c}
- * static zval *my_read_prop(zval *object, zval *member, int type, void **cache_slot, zval *tmp)
- * {
- *    zval *return_value;
- *    zend_string *member_name = zval_get_string(member);
- *    my_prophandler_t *handler = my_get_prophandler(member_name);
- *
- *    if (!handler || type == BP_VAR_R || type == BP_VAR_IS) {
- *     return_value = zend_get_std_object_handlers()->read_property(object, member, type, cache_slot, tmp);
- *
- *     if (handler) {
- *             handler->read(object, tmp);
- *
- *             zval_ptr_dtor(return_value);
- *             ZVAL_COPY_VALUE(return_value, tmp);
- *     }
- *    } else {
- *     return_value = php_property_proxy_zval(object, member_name);
- *    }
- *
- *    zend_string_release(member_name);
- *
- *    return return_value;
- * }
- * ~~~~~~~~~~
- */
-struct php_property_proxy_object {
-       /** The actual property proxy */
-       php_property_proxy_t *proxy;
-       /** Any parent property proxy object */
-       zval parent;
-       /** The std zend_object */
-       zend_object zo;
-};
-typedef struct php_property_proxy_object php_property_proxy_object_t;
-
-/**
- * Create a property proxy
- *
- * The property proxy will forward reads and writes to itself to the
- * proxied property with name \a member_str of \a container.
- *
- * @param container the container holding the property
- * @param member the name of the proxied property
- * @return a new property proxy
- */
-PHP_PROPRO_API php_property_proxy_t *php_property_proxy_init(zval *container,
-               zend_string *member);
-
-/**
- * Destroy and free a property proxy.
- *
- * The destruction of the property proxy object calls this.
- *
- * @param proxy a pointer to the allocated property proxy
- */
-PHP_PROPRO_API void php_property_proxy_free(php_property_proxy_t **proxy);
-
-/**
- * Get the zend_class_entry of php\\PropertyProxy
- * @return the class entry pointer
- */
-PHP_PROPRO_API zend_class_entry *php_property_proxy_get_class_entry(void);
-
-/**
- * Instantiate a new php\\PropertyProxy
- * @param ce the property proxy or derived class entry
- * @return the zend object
- */
-PHP_PROPRO_API zend_object *php_property_proxy_object_new(zend_class_entry *ce);
-
-/**
- * Instantiate a new php\\PropertyProxy with \a proxy
- * @param ce the property proxy or derived class entry
- * @param proxy the internal property proxy
- * @return the property proxy
- */
-PHP_PROPRO_API php_property_proxy_object_t *php_property_proxy_object_new_ex(
-               zend_class_entry *ce, php_property_proxy_t *proxy);
-
-#endif /* PHP_PROPRO_API_H */
-
-
-/*
- * Local variables:
- * tab-width: 4
- * c-basic-offset: 4
- * End:
- * vim600: noet sw=4 ts=4 fdm=marker
- * vim<600: noet sw=4 ts=4
- */
diff --git a/scripts/gen_travis_yml.php b/scripts/gen_travis_yml.php
new file mode 100755 (executable)
index 0000000..0283920
--- /dev/null
@@ -0,0 +1,34 @@
+#!/usr/bin/env php
+# autogenerated file; do not edit
+sudo: false
+language: c
+
+addons:
+ apt:
+  packages:
+   - php5-cli
+   - php-pear
+
+env:
+ matrix:
+<?php
+
+$gen = include "./travis/pecl/gen-matrix.php";
+$env = $gen([
+       "PHP" => ["master"],
+       "enable_debug",
+       "enable_maintainer_zts",
+]);
+foreach ($env as $e) {
+       printf("  - %s\n", $e);
+}
+
+?>
+
+before_script:
+ - make -f travis/pecl/Makefile php
+ - make -f travis/pecl/Makefile ext PECL=propro
+
+script:
+ - make -f travis/pecl/Makefile test
+
diff --git a/src/php_propro_api.c b/src/php_propro_api.c
new file mode 100644 (file)
index 0000000..c92d6d4
--- /dev/null
@@ -0,0 +1,597 @@
+/*
+    +--------------------------------------------------------------------+
+    | PECL :: propro                                                     |
+    +--------------------------------------------------------------------+
+    | Redistribution and use in source and binary forms, with or without |
+    | modification, are permitted provided that the conditions mentioned |
+    | in the accompanying LICENSE file are met.                          |
+    +--------------------------------------------------------------------+
+    | Copyright (c) 2013 Michael Wallner <mike@php.net>                  |
+    +--------------------------------------------------------------------+
+*/
+
+
+#ifdef HAVE_CONFIG_H
+#      include "config.h"
+#endif
+
+#include <php.h>
+#include <ext/standard/info.h>
+
+#include "php_propro_api.h"
+
+#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));
+
+       ZVAL_COPY(&proxy->container, get_referenced_zval(container));
+       proxy->member = zend_string_copy(member);
+
+       return proxy;
+}
+
+void php_property_proxy_free(php_property_proxy_t **proxy)
+{
+       if (*proxy) {
+               zval_ptr_dtor(&(*proxy)->container);
+               zend_string_release((*proxy)->member);
+               efree(*proxy);
+               *proxy = NULL;
+       }
+}
+
+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;
+}
+
+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));
+               }
+               if (obj->proxy) {
+                       fprintf(stderr, ".%s", obj->proxy->member->val);
+               }
+       }
+}
+
+static void debug_propro(int inout, const char *f,
+               php_property_proxy_object_t *obj, zval *offset, zval *value TSRMLS_DC)
+{
+       fprintf(stderr, "#PP %p %s %s %s ", obj, &space[sizeof(space)-level],
+                       inoutstr[inout+1], f);
+
+       level += inout;
+
+       _walk(obj);
+
+       if (*f++=='d'
+       &&      *f++=='i'
+       &&      *f++=='m'
+       ) {
+               char *offset_str = "[]";
+               zval *o = offset;
+
+               if (o) {
+                       convert_to_string_ex(o);
+                       offset_str = Z_STRVAL_P(o);
+               }
+
+               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);
+               }
+       }
+
+       fprintf(stderr, "\n");
+}
+#else
+#define debug_propro(l, f, obj, off, val)
+#endif
+
+php_property_proxy_object_t *php_property_proxy_object_new_ex(
+               zend_class_entry *ce, php_property_proxy_t *proxy)
+{
+       php_property_proxy_object_t *o;
+
+       if (!ce) {
+               ce = php_property_proxy_class_entry;
+       }
+
+       o = ecalloc(1, sizeof(*o) + sizeof(zval) * (ce->default_properties_count - 1));
+       zend_object_std_init(&o->zo, ce);
+       object_properties_init(&o->zo, ce);
+
+       o->proxy = proxy;
+       o->zo.handlers = &php_property_proxy_object_handlers;
+
+       debug_propro(0, "init", o, NULL, NULL);
+
+       return o;
+}
+
+zend_object *php_property_proxy_object_new(zend_class_entry *ce)
+{
+       return &php_property_proxy_object_new_ex(ce, NULL)->zo;
+}
+
+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);
+       }
+       if (!Z_ISUNDEF(o->parent)) {
+               zval_ptr_dtor(&o->parent);
+               ZVAL_UNDEF(&o->parent);
+       }
+       zend_object_std_dtor(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;
+
+       EMPTY_SWITCH_DEFAULT_CASE();
+       }
+       return PHP_PROPRO_PTR(Z_OBJ_P(object));
+}
+
+static inline zend_bool got_value(zval *container, zval *value)
+{
+       zval identical;
+
+       if (!Z_ISUNDEF_P(value)) {
+               if (SUCCESS == is_identical_function(&identical, value, container)) {
+                       if (Z_TYPE(identical) != IS_TRUE) {
+                               return 1;
+                       }
+               }
+       }
+
+       return 0;
+}
+
+static zval *get_parent_proxied_value(zval *object, zval *return_value)
+{
+       php_property_proxy_object_t *obj;
+
+       obj = get_propro(object);
+       debug_propro(1, "parent_get", obj, NULL, NULL);
+
+       if (obj->proxy) {
+               if (!Z_ISUNDEF(obj->parent)) {
+                       get_proxied_value(&obj->parent, return_value);
+               }
+       }
+
+       debug_propro(-1, "parent_get", obj, NULL, return_value);
+
+       return return_value;
+}
+
+static zval *get_proxied_value(zval *object, zval *return_value)
+{
+       zval *hash_value, *ref, prop_tmp;
+       php_property_proxy_object_t *obj;
+
+       obj = get_propro(object);
+       debug_propro(1, "get", obj, NULL, NULL);
+
+       if (obj->proxy) {
+               if (!Z_ISUNDEF(obj->parent)) {
+                       zval parent_value;
+
+                       ZVAL_UNDEF(&parent_value);
+                       get_parent_proxied_value(object, &parent_value);
+
+                       if (got_value(&obj->proxy->container, &parent_value)) {
+                               zval_ptr_dtor(&obj->proxy->container);
+                               ZVAL_COPY(&obj->proxy->container, &parent_value);
+                       }
+               }
+
+               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;
+
+               case IS_ARRAY:
+                       hash_value = zend_symtable_find(Z_ARRVAL_P(ref), obj->proxy->member);
+
+                       if (hash_value) {
+                               RETVAL_ZVAL(hash_value, 0, 0);
+                       }
+                       break;
+               }
+       }
+
+       debug_propro(-1, "get", obj, NULL, return_value);
+
+       return return_value;
+}
+
+static ZEND_RESULT_CODE cast_proxied_value(zval *object, zval *return_value,
+               int type)
+{
+       get_proxied_value(object, return_value);
+
+       debug_propro(0, "cast", get_propro(object), NULL, return_value);
+
+       if (!Z_ISUNDEF_P(return_value)) {
+               convert_to_explicit_type_ex(return_value, type);
+               return SUCCESS;
+       }
+
+       return FAILURE;
+}
+
+static void set_proxied_value(zval *object, zval *value)
+{
+       php_property_proxy_object_t *obj;
+       zval *ref;
+
+       obj = get_propro(object);
+       debug_propro(1, "set", obj, NULL, value TSRMLS_CC);
+
+       if (obj->proxy) {
+               if (!Z_ISUNDEF(obj->parent)) {
+                       zval parent_value;
+
+                       ZVAL_UNDEF(&parent_value);
+                       get_parent_proxied_value(object, &parent_value);
+
+                       if (got_value(&obj->proxy->container, &parent_value)) {
+                               zval_ptr_dtor(&obj->proxy->container);
+                               ZVAL_COPY(&obj->proxy->container, &parent_value);
+                       }
+               }
+
+               ref = get_referenced_zval(&obj->proxy->container);
+
+               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;
+
+               default:
+                       convert_to_array(ref);
+                       /* no break */
+
+               case IS_ARRAY:
+                       Z_TRY_ADDREF_P(value);
+                       zend_symtable_update(Z_ARRVAL_P(ref), obj->proxy->member, value);
+                       break;
+               }
+
+               if (!Z_ISUNDEF(obj->parent)) {
+                       set_proxied_value(&obj->parent, &obj->proxy->container);
+               }
+       }
+
+       debug_propro(-1, "set", obj, NULL, NULL);
+}
+
+static zval *read_dimension(zval *object, zval *offset, int type, zval *return_value)
+{
+       zval proxied_value;
+       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);
+
+       ZVAL_UNDEF(&proxied_value);
+       get_proxied_value(object, &proxied_value);
+
+       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 (hash_value) {
+                               RETVAL_ZVAL(hash_value, 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 (!member) {
+                       member = zend_long_to_str(zend_hash_next_free_element(
+                                       Z_ARRVAL(proxied_value)));
+               }
+
+               proxy = php_property_proxy_init(&proxied_value, member);
+               zval_ptr_dtor(&proxied_value);
+
+               proxy_obj = php_property_proxy_object_new_ex(NULL, proxy);
+               ZVAL_COPY(&proxy_obj->parent, object);
+               RETVAL_OBJ(&proxy_obj->zo);
+       }
+
+       if (member) {
+               zend_string_release(member);
+       }
+
+       debug_propro(-1, type == BP_VAR_R ? "dim_read" : "dim_read_ref",
+                       get_propro(object), offset, return_value);
+
+       return return_value;
+}
+
+static int has_dimension(zval *object, zval *offset, int check_empty)
+{
+       zval proxied_value;
+       int exists = 0;
+
+       debug_propro(1, "dim_exists", get_propro(object), offset, NULL);
+
+       ZVAL_UNDEF(&proxied_value);
+       get_proxied_value(object, &proxied_value);
+       if (Z_ISUNDEF(proxied_value)) {
+               exists = 0;
+       } else {
+               zend_string *zs = zval_get_string(offset);
+
+               if (Z_TYPE(proxied_value) == IS_ARRAY) {
+                       zval *zentry = zend_symtable_find(Z_ARRVAL(proxied_value), zs);
+
+                       if (!zentry) {
+                               exists = 0;
+                       } else {
+                               if (check_empty) {
+                                       exists = !Z_ISNULL_P(zentry);
+                               } else {
+                                       exists = 1;
+                               }
+                       }
+               }
+
+               zend_string_release(zs);
+       }
+
+       debug_propro(-1, "dim_exists", get_propro(object), offset, NULL);
+
+       return exists;
+}
+
+static void write_dimension(zval *object, zval *offset, zval *value)
+{
+       zval proxied_value;
+
+       debug_propro(1, "dim_write", get_propro(object), offset, 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);
+       }
+
+       SEPARATE_ZVAL(value);
+       Z_TRY_ADDREF_P(value);
+
+       if (offset) {
+               zend_string *zs = zval_get_string(offset);
+               zend_symtable_update(Z_ARRVAL(proxied_value), zs, value);
+               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);
+}
+
+static void unset_dimension(zval *object, zval *offset)
+{
+       zval proxied_value;
+
+       debug_propro(1, "dim_unset", get_propro(object), offset, NULL);
+
+       ZVAL_UNDEF(&proxied_value);
+       get_proxied_value(object, &proxied_value);
+
+       if (Z_TYPE(proxied_value) == IS_ARRAY) {
+               zval *o = offset;
+               ZEND_RESULT_CODE rv;
+
+               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);
+               }
+
+               if (o != offset) {
+                       zval_ptr_dtor(o);
+               }
+       }
+
+       debug_propro(-1, "dim_unset", get_propro(object), offset, &proxied_value);
+}
+
+ZEND_BEGIN_ARG_INFO_EX(ai_propro_construct, 0, 0, 2)
+       ZEND_ARG_INFO(1, 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;
+       zend_string *member;
+
+       zend_replace_error_handling(EH_THROW, NULL, &zeh);
+       if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "zS|O!",
+                       &container, &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);
+               }
+       }
+       zend_restore_error_handling(&zeh);
+}
+
+static const zend_function_entry php_property_proxy_method_entry[] = {
+       PHP_ME(propro, __construct, ai_propro_construct, ZEND_ACC_PUBLIC)
+       {0}
+};
+
+static PHP_MINIT_FUNCTION(propro)
+{
+       zend_class_entry ce = {0};
+
+       INIT_NS_CLASS_ENTRY(ce, "php", "PropertyProxy",
+                       php_property_proxy_method_entry);
+       php_property_proxy_class_entry = zend_register_internal_class(&ce);
+       php_property_proxy_class_entry->create_object = php_property_proxy_object_new;
+       php_property_proxy_class_entry->ce_flags |= ZEND_ACC_FINAL;
+
+       memcpy(&php_property_proxy_object_handlers, zend_get_std_object_handlers(),
+                       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.read_dimension = read_dimension;
+       php_property_proxy_object_handlers.write_dimension = write_dimension;
+       php_property_proxy_object_handlers.has_dimension = has_dimension;
+       php_property_proxy_object_handlers.unset_dimension = unset_dimension;
+
+       return SUCCESS;
+}
+
+PHP_MINFO_FUNCTION(propro)
+{
+       php_info_print_table_start();
+       php_info_print_table_header(2, "Property proxy support", "enabled");
+       php_info_print_table_row(2, "Extension version", PHP_PROPRO_VERSION);
+       php_info_print_table_end();
+}
+
+static const zend_function_entry propro_functions[] = {
+       {0}
+};
+
+zend_module_entry propro_module_entry = {
+       STANDARD_MODULE_HEADER,
+       "propro",
+       propro_functions,
+       PHP_MINIT(propro),
+       NULL,
+       NULL,
+       NULL,
+       PHP_MINFO(propro),
+       PHP_PROPRO_VERSION,
+       STANDARD_MODULE_PROPERTIES
+};
+
+#ifdef COMPILE_DL_PROPRO
+ZEND_GET_MODULE(propro)
+#endif
+
+
+/*
+ * Local variables:
+ * tab-width: 4
+ * c-basic-offset: 4
+ * End:
+ * vim600: noet sw=4 ts=4 fdm=marker
+ * vim<600: noet sw=4 ts=4
+ */
diff --git a/src/php_propro_api.h b/src/php_propro_api.h
new file mode 100644 (file)
index 0000000..d82055f
--- /dev/null
@@ -0,0 +1,128 @@
+/*
+    +--------------------------------------------------------------------+
+    | PECL :: propro                                                     |
+    +--------------------------------------------------------------------+
+    | Redistribution and use in source and binary forms, with or without |
+    | modification, are permitted provided that the conditions mentioned |
+    | in the accompanying LICENSE file are met.                          |
+    +--------------------------------------------------------------------+
+    | Copyright (c) 2013 Michael Wallner <mike@php.net>                  |
+    +--------------------------------------------------------------------+
+*/
+
+#ifndef PHP_PROPRO_API_H
+#define PHP_PROPRO_API_H
+
+#include "php_propro.h"
+
+/**
+ * The internal property proxy.
+ *
+ * Container for the object/array holding the proxied property.
+ */
+struct php_property_proxy {
+       /** The container holding the property */
+       zval container;
+       /** The name of the proxied property */
+       zend_string *member;
+};
+typedef struct php_property_proxy php_property_proxy_t;
+
+/**
+ * The userland object.
+ *
+ * Return an object instance of php\\PropertyProxy to make your C-struct
+ * member accessible by reference from PHP userland.
+ *
+ * Example:
+ * ~~~~~~~~~~{.c}
+ * static zval *my_read_prop(zval *object, zval *member, int type, void **cache_slot, zval *tmp)
+ * {
+ *    zval *return_value;
+ *    zend_string *member_name = zval_get_string(member);
+ *    my_prophandler_t *handler = my_get_prophandler(member_name);
+ *
+ *    if (!handler || type == BP_VAR_R || type == BP_VAR_IS) {
+ *     return_value = zend_get_std_object_handlers()->read_property(object, member, type, cache_slot, tmp);
+ *
+ *     if (handler) {
+ *             handler->read(object, tmp);
+ *
+ *             zval_ptr_dtor(return_value);
+ *             ZVAL_COPY_VALUE(return_value, tmp);
+ *     }
+ *    } else {
+ *     return_value = php_property_proxy_zval(object, member_name);
+ *    }
+ *
+ *    zend_string_release(member_name);
+ *
+ *    return return_value;
+ * }
+ * ~~~~~~~~~~
+ */
+struct php_property_proxy_object {
+       /** The actual property proxy */
+       php_property_proxy_t *proxy;
+       /** Any parent property proxy object */
+       zval parent;
+       /** The std zend_object */
+       zend_object zo;
+};
+typedef struct php_property_proxy_object php_property_proxy_object_t;
+
+/**
+ * Create a property proxy
+ *
+ * The property proxy will forward reads and writes to itself to the
+ * proxied property with name \a member_str of \a container.
+ *
+ * @param container the container holding the property
+ * @param member the name of the proxied property
+ * @return a new property proxy
+ */
+PHP_PROPRO_API php_property_proxy_t *php_property_proxy_init(zval *container,
+               zend_string *member);
+
+/**
+ * Destroy and free a property proxy.
+ *
+ * The destruction of the property proxy object calls this.
+ *
+ * @param proxy a pointer to the allocated property proxy
+ */
+PHP_PROPRO_API void php_property_proxy_free(php_property_proxy_t **proxy);
+
+/**
+ * Get the zend_class_entry of php\\PropertyProxy
+ * @return the class entry pointer
+ */
+PHP_PROPRO_API zend_class_entry *php_property_proxy_get_class_entry(void);
+
+/**
+ * Instantiate a new php\\PropertyProxy
+ * @param ce the property proxy or derived class entry
+ * @return the zend object
+ */
+PHP_PROPRO_API zend_object *php_property_proxy_object_new(zend_class_entry *ce);
+
+/**
+ * Instantiate a new php\\PropertyProxy with \a proxy
+ * @param ce the property proxy or derived class entry
+ * @param proxy the internal property proxy
+ * @return the property proxy
+ */
+PHP_PROPRO_API php_property_proxy_object_t *php_property_proxy_object_new_ex(
+               zend_class_entry *ce, php_property_proxy_t *proxy);
+
+#endif /* PHP_PROPRO_API_H */
+
+
+/*
+ * Local variables:
+ * tab-width: 4
+ * c-basic-offset: 4
+ * End:
+ * vim600: noet sw=4 ts=4 fdm=marker
+ * vim<600: noet sw=4 ts=4
+ */