summaryrefslogtreecommitdiff
path: root/zend/callable.h
diff options
context:
space:
mode:
Diffstat (limited to 'zend/callable.h')
-rw-r--r--zend/callable.h97
1 files changed, 41 insertions, 56 deletions
diff --git a/zend/callable.h b/zend/callable.h
index bd74f27..f6a172f 100644
--- a/zend/callable.h
+++ b/zend/callable.h
@@ -1,7 +1,7 @@
/**
* Callable.h
*
- * Object represents a callable function or method that is defined with the CPP
+ * Object represents a callable function or method that is defined with the CPP
* API.
*
* @author Emiel Bruijntjes <emiel.bruijntjes@copernica.com>
@@ -9,11 +9,16 @@
*/
/**
+ * Dependencies
+ */
+#include <cstring>
+
+/**
* Set up namespace
*/
namespace Php {
-/**
+/**
* Class definition
*/
class Callable
@@ -24,68 +29,62 @@ public:
* @param name Function or method name
* @param arguments Information about the arguments
*/
- Callable(const char *name, const Arguments &arguments = {}) : _ptr(this, name)
+ Callable(const char *name, const Arguments &arguments = {}) :
+ _name(name),
+ _argc(arguments.size()),
+ _argv(new zend_internal_arg_info[_argc + 1])
{
- // construct vector for arguments
- _argc = arguments.size();
- _argv = new zend_arg_info[_argc+1];
-
// the first record is initialized with information about the function,
// so we skip that here
int i=1;
-
+
// loop through the arguments
- for (auto it = arguments.begin(); it != arguments.end(); it++)
+ for (auto &argument : arguments)
{
// increment counter with number of required parameters
- if (it->required()) _required++;
-
+ if (argument.required()) _required++;
+
// fill the arg info
- fill(&_argv[i++], *it);
+ fill(&_argv[i++], argument);
}
}
-
+
/**
* Copy constructor
* @param that
*/
Callable(const Callable &that) :
- _ptr(that._ptr),
+ _name(that._name),
_return(that._return),
_required(that._required),
_argc(that._argc),
_argv(nullptr) {}
-
+ // @todo: we have no arguments after copy? is this correct?
+ // we do have the argument count though...
+
/**
* Move constructor
* @param that
*/
Callable(Callable &&that) :
- _ptr(std::move(that._ptr)),
+ _name(std::move(that._name)),
_return(that._return),
_required(that._required),
_argc(that._argc),
- _argv(that._argv)
- {
- // invalidate other object
- that._argv = nullptr;
- }
+ _argv(std::move(that._argv)) {}
/**
* Destructor
*/
- virtual ~Callable()
- {
- if (_argv) delete[] _argv;
- }
-
+ virtual ~Callable() = default;
+
/**
* Method that gets called every time the function is executed
* @param params The parameters that were passed
* @return Variable Return value
*/
virtual Value invoke(Parameters &params) = 0;
-
+
/**
* Fill a function entry
* @param entry Entry to be filled
@@ -101,27 +100,27 @@ public:
* @param ns Active namespace
* @param classname Optional class name
*/
- void initialize(zend_arg_info *info, const char *classname = nullptr) const;
+ void initialize(zend_internal_function_info *info, const char *classname = nullptr) const;
protected:
/**
- * Hidden pointer to the name and the function
- * @var HiddenPointer
+ * Name of the function
+ * @var std::string
*/
- HiddenPointer<Callable> _ptr;
+ std::string _name;
/**
* Suggestion for the return type
* @var Type
*/
- Type _return = Type::Null;
+ Type _return = Type::Undefined;
/**
* Required number of arguments
- * @var integer
+ * @var unsigned integer
*/
- int _required = 0;
+ unsigned int _required = 0;
/**
* Total number of arguments
@@ -131,22 +130,23 @@ protected:
/**
* The arguments
- * @var zend_arg_info[]
+ * @var std::unique_ptr<zend_internal_arg_info[]>
*/
- zend_arg_info *_argv = nullptr;
-
+ std::unique_ptr<zend_internal_arg_info[]> _argv;
+
/**
* Private helper method to fill an argument object
* @param info object from the zend engine
* @param arg original object
*/
- void fill(zend_arg_info *info, const Argument &arg) const
+ void fill(zend_internal_arg_info *info, const Argument &arg) const
{
// fill members
info->name = arg.name();
- info->name_len = ::strlen(arg.name());
-#if PHP_VERSION_ID >= 50400
+ // are we filling an object
+ if (arg.type() == Type::Object) info->class_name = arg.classname();
+ else info->class_name = nullptr;
// since php 5.4 there is a type-hint, but we only support arrays, objects and callables
switch (arg.type()) {
@@ -155,29 +155,14 @@ protected:
case Type::Object: info->type_hint = IS_OBJECT; break;
default: info->type_hint = IS_NULL; break;
}
-
-# if PHP_VERSION_ID >= 50600
- // from PHP 5.6 and onwards, an is_variadic property can be set, this
+ // from PHP 5.6 and onwards, an is_variadic property can be set, this
// specifies whether this argument is the first argument that specifies
// the type for a variable length list of arguments. For now we only
// support methods and functions with a fixed number of arguments.
info->is_variadic = false;
-# endif
-
-#else
-
- // php 5.3 code
- info->array_type_hint = arg.type() == Type::Array;
- info->return_reference = false;
- info->required_num_args = 0; // @todo is this correct?
-
-#endif
-
// this parameter is a regular type
- info->class_name = arg.type() == Type::Object ? arg.classname() : nullptr;
- info->class_name_len = arg.type() == Type::Object && arg.classname() ? ::strlen(arg.classname()) : 0;
info->allow_null = arg.allowNull();
info->pass_by_reference = arg.byReference();
}