summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorEmiel Bruijntjes <emiel.bruijntjes@copernica.com>2013-08-25 23:10:15 +0200
committerEmiel Bruijntjes <emiel.bruijntjes@copernica.com>2013-08-25 23:10:15 +0200
commit9ba4fb9ae4233a656bbfca1b169b0a608202b717 (patch)
tree0ccad56abc640ace7c97847552ff99702e16c577 /src
parente14055694478d70e58b5fc653b08a9a514bbc455 (diff)
By hiding a pointer to the callable object in front of the function name we have managed to retrieve back the callable object, so we can pass all callbacks to one single function, which will further deliver them to the appropriate function object
Diffstat (limited to 'src')
-rw-r--r--src/callable.cpp17
-rw-r--r--src/callable.h26
-rw-r--r--src/extension.cpp2
-rw-r--r--src/functions.h11
4 files changed, 46 insertions, 10 deletions
diff --git a/src/callable.cpp b/src/callable.cpp
index 1ad4ed2..05545f1 100644
--- a/src/callable.cpp
+++ b/src/callable.cpp
@@ -19,8 +19,17 @@ namespace PhpCpp {
*/
void invoke_callable(INTERNAL_FUNCTION_PARAMETERS)
{
- std::cout << "invoke_callable" << std::endl;
+ // find the function name
+ const char *function = get_active_function_name(TSRMLS_C);
+ // when we registered the function name, we have hidden the pointer to the
+ // callable right in front of the function name - we retrieve it back
+ Callable *callable = *((Callable **)(function - sizeof(Callable *)));
+
+ std::cout << "callable: " << callable << std::endl;
+
+
+ return;
}
/**
@@ -30,7 +39,7 @@ void invoke_callable(INTERNAL_FUNCTION_PARAMETERS)
void Callable::fill(zend_function_entry *entry)
{
// fill the members of the entity
- entry->fname = _function.c_str();
+ entry->fname = _name;
entry->handler = invoke_callable;
entry->arg_info = _argv;
entry->num_args = _argc;
@@ -44,8 +53,8 @@ void Callable::fill(zend_function_entry *entry)
void Callable::fill(zend_internal_function_info *info)
{
// fill in all the members, not that the returning by reference is not used
- info->_name = _function.c_str();
- info->_name_len = _function.size();
+ info->_name = _name;
+ info->_name_len = _data.size() - sizeof(this);
info->_class_name = _classname.size() ? _classname.c_str() : NULL;
info->required_num_args = _required;
info->_type_hint = _type;
diff --git a/src/callable.h b/src/callable.h
index 3903ac7..2af7d41 100644
--- a/src/callable.h
+++ b/src/callable.h
@@ -31,8 +31,22 @@ public:
* @param flags Optional flags to be passed to the function
*/
Callable(const std::string &classname, const std::string &function, Type type = nullType, const std::initializer_list<Argument> &arguments = {}, int flags = 0) :
- _classname(classname), _function(function), _type(type), _flags(flags)
+ _classname(classname), _type(type), _flags(flags)
{
+ // somehow "&this" is not accepted by the compiler, so we make a copy
+ Callable *callable = this;
+
+ // append function name to the data (the data contains a pointer
+ // to this object, appended with the function name. this is a trick
+ // so that we have the pointer to this function available in the
+ // function name by going back a number of bytes)
+ _data.reserve(function.size() + sizeof(this));
+ _data.assign(std::string((const char *)&callable, sizeof(callable)));
+ _data.append(function);
+
+ // find the name
+ _name = _data.c_str() + sizeof(this);
+
// process the arguments
process(arguments);
}
@@ -91,10 +105,16 @@ private:
std::string _classname;
/**
- * Function name
+ * Pointer to current object, appended with function name
* @var string
*/
- std::string _function;
+ std::string _data;
+
+ /**
+ * Pointer to the function name
+ * @var char*
+ */
+ const char *_name;
/**
* The return type
diff --git a/src/extension.cpp b/src/extension.cpp
index 94b5082..c995f0b 100644
--- a/src/extension.cpp
+++ b/src/extension.cpp
@@ -63,6 +63,8 @@ static int extension_startup(INIT_FUNC_ARGS)
*/
static int extension_shutdown(SHUTDOWN_FUNC_ARGS)
{
+ std::cout << "extension_shutdown" << std::endl;
+
// finalize the extension
return BOOL2SUCCESS(extension->finalize());
}
diff --git a/src/functions.h b/src/functions.h
index 9b28ca2..bc08b8a 100644
--- a/src/functions.h
+++ b/src/functions.h
@@ -23,7 +23,7 @@ public:
* Constructor
* @param functions The functions to parse
*/
- Functions(const std::initializer_list<Function> &functions)
+ Functions(const std::initializer_list<Function> &functions) : _functions(functions)
{
// allocate the function entries
_entries = new zend_function_entry[functions.size() + 1];
@@ -69,6 +69,13 @@ private:
* @var zend_function_entry*
*/
zend_function_entry *_entries;
+
+ /**
+ * Vector of functions (we need this because the function objects must
+ * remain in memory)
+ * @var vector
+ */
+ std::vector<Function> _functions;
};
/**
@@ -76,5 +83,3 @@ private:
*/
}
-
- \ No newline at end of file