diff options
author | Emiel Bruijntjes <emiel.bruijntjes@copernica.com> | 2013-09-25 09:41:17 -0700 |
---|---|---|
committer | Emiel Bruijntjes <emiel.bruijntjes@copernica.com> | 2013-09-25 09:41:17 -0700 |
commit | 753402a84b40ff4dc9697dea1d2d4aa037ea7624 (patch) | |
tree | 9ff5e95a534265d01ae732a1e39dd3aaf3127803 | |
parent | 49ac629257835426c311fb7b92b23a9138a9d77b (diff) |
Constructor gets almost called when object is constructed
-rw-r--r-- | include/classinfo.h | 44 | ||||
-rw-r--r-- | include/value.h | 8 | ||||
-rw-r--r-- | src/classinfo.cpp | 79 | ||||
-rw-r--r-- | src/includes.h | 1 | ||||
-rw-r--r-- | src/internalfunction.h | 80 | ||||
-rw-r--r-- | tests/simple/simple.cpp | 25 | ||||
-rw-r--r-- | tests/simple/simple.php | 8 |
7 files changed, 233 insertions, 12 deletions
diff --git a/include/classinfo.h b/include/classinfo.h index dfa480c..3941259 100644 --- a/include/classinfo.h +++ b/include/classinfo.h @@ -12,6 +12,11 @@ */ /** + * Forward declarations + */ +struct _zend_class_entry; + +/** * Namespace */ namespace Php { @@ -26,9 +31,34 @@ class _ClassInfo { public: /** + * Constructor + * @param name + */ + _ClassInfo(const char *name) : _name(name), _entry(NULL) {} + + /** + * Destructor + */ + virtual ~_ClassInfo() {} + + /** * Initialize the class */ - virtual void initialize() = 0; + void initialize(); + +private: + /** + * Class name + * @var string + */ + std::string _name; + + /** + * The class entry + * @var zend_class_entry + */ + struct _zend_class_entry *_entry; + }; /** @@ -43,7 +73,7 @@ public: * @param name Name of the class * @param type The class type */ - ClassInfo(const char *name, const Class<T> &type) : _name(name), _type(type) + ClassInfo(const char *name, const Class<T> &type) : _ClassInfo(name), _type(type) { } @@ -52,19 +82,9 @@ public: */ virtual ~ClassInfo() {} - /** - * Initialize the class - */ - virtual void initialize(); private: /** - * Class name - * @var string - */ - std::string _name; - - /** * The class object * @var Class */ diff --git a/include/value.h b/include/value.h index e29b8f2..f47304a 100644 --- a/include/value.h +++ b/include/value.h @@ -500,6 +500,14 @@ protected: }; /** + * Custom output stream operator + * @param stream + * @param value + * @return ostream + */ +std::ostream &operator<<(std::ostream &stream, const Value &value); + +/** * End of namespace */ } diff --git a/src/classinfo.cpp b/src/classinfo.cpp new file mode 100644 index 0000000..760df5c --- /dev/null +++ b/src/classinfo.cpp @@ -0,0 +1,79 @@ +/** + * ClassInfo.cpp + * + * Implementation for the class info + * + * @documentation private + */ +#include "includes.h" + +/** + * Set up namespace + */ +namespace Php { + +/** + * Function that is called by the Zend engine every time that a function gets called + * @param ht + * @param return_value + * @param return_value_ptr + * @param this_ptr + * @param return_value_used + * @param tsrm_ls + * @return integer + */ +void invoke_method(INTERNAL_FUNCTION_PARAMETERS) +{ + std::cout << "invoke method" << std::endl; + + return; + + // find the function name + const char *name = get_active_function_name(TSRMLS_C); + + // uncover the hidden pointer inside the function name + Function *function = HiddenPointer<Function>(name); + + // wrap the return value + Value result(return_value, true); + + // construct parameters + Parameters params(ZEND_NUM_ARGS()); + + // get the result + result = function->invoke(*PHPCPP_G(environment), params); +} + +/** + * Helper struct to create an internal method + */ + + +/** + * Initialize the class + * @param mixed Optional threading ID + */ +void _ClassInfo::initialize(TSRMLS_D) +{ + // the class entry + zend_class_entry entry; + + // initialize the class entry + INIT_CLASS_ENTRY_EX(entry, _name.c_str(), _name.size(), NULL); + + // functions we need + // @todo should not be static + static InternalFunction constructor(invoke_method, ZEND_ACC_CTOR|ZEND_ACC_PUBLIC); + + // we need a special constructor + entry.__call = constructor; + + // register the class + _entry = zend_register_internal_class(&entry TSRMLS_CC); +} + +/** + * End of namespace + */ +} + diff --git a/src/includes.h b/src/includes.h index 8eae5f7..20e7e95 100644 --- a/src/includes.h +++ b/src/includes.h @@ -55,4 +55,5 @@ * Interface files for internal use only */ #include "nativefunction.h" +#include "internalfunction.h" diff --git a/src/internalfunction.h b/src/internalfunction.h new file mode 100644 index 0000000..741ea5b --- /dev/null +++ b/src/internalfunction.h @@ -0,0 +1,80 @@ +/** + * InternalFunction.h + * + * Helper union to create an internal function + * + * @documentation private + */ + +/** + * Namespace + */ +namespace Php { + +/** + * An internal function + */ +class InternalFunction +{ +public: + /** + * Constructor + * @param handler + * @param flags + */ + InternalFunction(void (*handler)(INTERNAL_FUNCTION_PARAMETERS), int flags = 0) + { + // set everything to zero + memset(&_func, 0, sizeof(zend_internal_function)); + + // set the appropriate properties + _func.type = ZEND_INTERNAL_FUNCTION; + _func.handler = handler; + _func.fn_flags = flags; + +// _func.function_name = NULL; +// _func.scope = NULL; +// _func.prototype = NULL; +// _func.num_args = 0; +// _func.required_num_args = 0; +// _func.arg_info = NULL; +// _func.module = NULL; + } + + /** + * Destructor + */ + virtual ~InternalFunction() {} + + /** + * Cast to zend_internal_function pointer + * @return zend_internal_function + */ + operator zend_internal_function *() + { + return &_func; + } + + /** + * Cast to zend_function pointer + * @return zend_function + */ + operator zend_function *() + { + return (zend_function *)&_func; + } + +private: + /** + * The internal function object + * @var zend_internal_function + */ + zend_internal_function _func; +}; + + +/** + * End of namespace + */ +} + diff --git a/tests/simple/simple.cpp b/tests/simple/simple.cpp index f2a5207..39e615a 100644 --- a/tests/simple/simple.cpp +++ b/tests/simple/simple.cpp @@ -45,7 +45,32 @@ static Php::Value my_plus(Php::Environment &env, Php::Parameters ¶ms) */ class MyCustomClass : public Php::Base { +private: + int _x; + public: + MyCustomClass() + { + } + + virtual void __construct() + { + + } + + virtual void __destruct() + { + + + } + + void myMethod(Php::Parameters ¶ms) + { + + + } + + }; // symbols are exported according to the "C" language diff --git a/tests/simple/simple.php b/tests/simple/simple.php index af9a2a2..10cb603 100644 --- a/tests/simple/simple.php +++ b/tests/simple/simple.php @@ -31,3 +31,11 @@ echo("g1: $g1\n"); echo("g2: $g2\n"); echo("g3: $g3\n"); + +if (class_exists("my_class")) echo("Warempel, de class bestaat\n"); + +$x = new my_class(); + +$x->my_method(); + + |