summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/argument.h10
-rw-r--r--include/arguments.h39
-rw-r--r--include/value.h5
-rw-r--r--phpcpp.h1
-rw-r--r--src/arginfo.h10
-rw-r--r--src/argument.cpp14
-rw-r--r--src/arguments.cpp41
-rw-r--r--src/callable.cpp42
-rw-r--r--src/includes.h1
-rw-r--r--src/value.cpp8
-rw-r--r--tests/simple/simple.cpp4
-rw-r--r--tests/simple/simple.php18
12 files changed, 184 insertions, 9 deletions
diff --git a/include/argument.h b/include/argument.h
index 862b677..5c0c23f 100644
--- a/include/argument.h
+++ b/include/argument.h
@@ -35,11 +35,19 @@ public:
/**
* Constructor if the argument can be anything
+ * Note that only arrayType and callableType are supported type-hints
* @param name Name of the argument
- * @param type Type hint
+ * @param type Type hint (arrayType or callableType)
* @param ref Is this a pass-by-reference argument?
*/
Argument(const std::string &name, Type type = nullType, bool ref = false);
+
+ /**
+ * Constructor if the argument can be anything
+ * @param name Name of the argument
+ * @param ref Is this a pass-by-reference argument?
+ */
+ Argument(const std::string &name, bool ref = false);
/**
* Copy constructor
diff --git a/include/arguments.h b/include/arguments.h
new file mode 100644
index 0000000..65476c3
--- /dev/null
+++ b/include/arguments.h
@@ -0,0 +1,39 @@
+/**
+ * Arguments.h
+ *
+ * When a function is invoked, it is passed a vector of arguments. This
+ * arguments class, that overrides from vector, takes care of that.
+ *
+ * @author Emiel Bruijntjes <emiel.bruijntjes@copernica.com>
+ * @copyright 2013 Copernica BV
+ */
+
+/**
+ * Set up namespace
+ */
+namespace PhpCpp {
+
+/**
+ * Class definition
+ */
+class Arguments : public std::vector<Value>
+{
+public:
+ /**
+ * Constructor
+ * @param argc The number of arguments
+ */
+ Arguments(int argc);
+
+ /**
+ * Destructor
+ */
+ virtual ~Arguments() {}
+
+};
+
+/**
+ * End of namespace
+ */
+}
+
diff --git a/include/value.h b/include/value.h
index fe370dc..157a456 100644
--- a/include/value.h
+++ b/include/value.h
@@ -65,9 +65,10 @@ public:
/**
* Wrap object around zval
- * @param zval
+ * @param zval Zval to wrap
+ * @param ref Force this to be a reference
*/
- Value(struct _zval_struct *zval);
+ Value(struct _zval_struct *zval, bool ref = false);
/**
* Copy constructor
diff --git a/phpcpp.h b/phpcpp.h
index 5884213..301f802 100644
--- a/phpcpp.h
+++ b/phpcpp.h
@@ -21,6 +21,7 @@
#include <phpcpp/request.h>
#include <phpcpp/argument.h>
#include <phpcpp/value.h>
+#include <phpcpp/arguments.h>
#include <phpcpp/function.h>
#include <phpcpp/extension.h>
diff --git a/src/arginfo.h b/src/arginfo.h
index 8ed00c8..0080667 100644
--- a/src/arginfo.h
+++ b/src/arginfo.h
@@ -31,11 +31,19 @@ public:
/**
* Constructor if this argument can be anything
* @param name Name of the argument
- * @param type Type hint
+ * @param type Type hint (arrayType or callableType)
* @param ref Is this a pass-by-reference argument?
*/
ArgInfo(const std::string &name, Type type = nullType, bool ref = false) :
_name(name), _type(type), _null(false), _ref(ref) {}
+
+ /**
+ * Constructor if this argument can be anything
+ * @param name Name of the argument
+ * @param ref Is this a pass-by-reference argument?
+ */
+ ArgInfo(const std::string &name, bool ref = false) :
+ _name(name), _type(nullType), _null(false), _ref(ref) {}
/**
* Destructor
diff --git a/src/argument.cpp b/src/argument.cpp
index 153da9c..fc9d720 100644
--- a/src/argument.cpp
+++ b/src/argument.cpp
@@ -28,8 +28,9 @@ Argument::Argument(const std::string &name, const std::string &classname, bool n
/**
* Constructor if the argument can be anything
+ * Note that only arrayType and callableType are supported type-hints
* @param name Name of the argument
- * @param type Type hint
+ * @param type Type hint (arrayType or callableType)
* @param ref Is this a pass-by-reference argument?
*/
Argument::Argument(const std::string &name, Type type, bool ref)
@@ -39,6 +40,17 @@ Argument::Argument(const std::string &name, Type type, bool ref)
}
/**
+ * Constructor if the argument can be anything
+ * @param name Name of the argument
+ * @param ref Is this a pass-by-reference argument?
+ */
+Argument::Argument(const std::string &name, bool ref)
+{
+ _refcount = new int[1];
+ _info = new ArgInfo(name, ref);
+}
+
+/**
* Clean up the object
*/
void Argument::cleanup()
diff --git a/src/arguments.cpp b/src/arguments.cpp
new file mode 100644
index 0000000..e5e7ae5
--- /dev/null
+++ b/src/arguments.cpp
@@ -0,0 +1,41 @@
+/**
+ * Arguments.cpp
+ *
+ * Implementation of the arguments class
+ *
+ * @author Emiel Bruijntjes <emiel.bruijntjes@copernica.com>
+ * @copyright 2013 Copernica BV
+ */
+#include "includes.h"
+
+/**
+ * Set up namespace
+ */
+namespace PhpCpp {
+
+/**
+ * Constructor
+ * @param argc Number of arguments
+ * @param tsrm_ls
+ */
+Arguments::Arguments(int argc TSRMLS_DC)
+{
+ // reserve plenty of space
+ reserve(argc);
+
+ // loop through the arguments
+ for (int i=0; i<argc; i++)
+ {
+ // get the argument
+ zval **arg = (zval **) (zend_vm_stack_top(TSRMLS_C) - 1 - (argc-i));
+
+ // append value
+ push_back(Value(*arg));
+ }
+}
+
+/**
+ * End of namespace
+ */
+}
+
diff --git a/src/callable.cpp b/src/callable.cpp
index 494e9ed..b4b1a6e 100644
--- a/src/callable.cpp
+++ b/src/callable.cpp
@@ -104,6 +104,13 @@ void Callable::process(const std::initializer_list<Argument> &arguments)
}
}
+int do_test(int a, int b)
+{
+ std::cout << "do_test: " << a << " " << b << std::endl;
+
+ return 77;
+}
+
/**
* Invoke the method
* @param ht
@@ -116,11 +123,46 @@ void Callable::process(const std::initializer_list<Argument> &arguments)
*/
int Callable::invoke(INTERNAL_FUNCTION_PARAMETERS)
{
+ std::cout << "args: " << ZEND_NUM_ARGS() << std::endl;
+ std::cout << "required: " << _required << std::endl;
+ std::cout << "argc: " << _argc << std::endl;
+
+ // number of arguments should be sufficient // @todo show error message
+// if (ZEND_NUM_ARGS() < _required) return FAILURE;
+ // and not be too much // @todo show error message
+// if (ZEND_NUM_ARGS() > _argc) return FAILURE;
+ // number of arguments on the stack
+ int arg_count = (int)(zend_uintptr_t) *(zend_vm_stack_top(TSRMLS_C) - 1);
+ // loop through the arguments
+ Arguments args(ZEND_NUM_ARGS());
+
+ for (auto iter = args.begin(); iter != args.end(); iter++)
+ {
+ Value val = *iter;
+
+ val = 1234;
+
+ std::cout << "argument: " << iter->stringValue() << std::endl;
+ }
+
+ int result = do_test(args[1], args[2]);
+
+ return SUCCESS;
+
+ Value ret(return_value, true);
+
+ ret = result;
+
+ // done
+ return SUCCESS;
}
+
+
+
/**
* End of namespace
*/
diff --git a/src/includes.h b/src/includes.h
index ae98c43..37b8b1b 100644
--- a/src/includes.h
+++ b/src/includes.h
@@ -35,6 +35,7 @@
#include "../include/request.h"
#include "../include/argument.h"
#include "../include/value.h"
+#include "../include/arguments.h"
#include "../include/function.h"
#include "../include/extension.h"
diff --git a/src/value.cpp b/src/value.cpp
index 6ba67a4..3c84d9c 100644
--- a/src/value.cpp
+++ b/src/value.cpp
@@ -87,15 +87,19 @@ Value::Value(double value)
/**
* Wrap object around zval
- * @param zval
+ * @param zval Value to wrap
+ * @param ref Force this to be a reference
*/
-Value::Value(struct _zval_struct *zval)
+Value::Value(struct _zval_struct *zval, bool ref)
{
// just copy the zval into this object
_val = zval;
// we see ourselves as reference too
Z_ADDREF_P(_val);
+
+ // should this be a forced reference
+ if (ref) Z_SET_ISREF_P(zval);
}
/**
diff --git a/tests/simple/simple.cpp b/tests/simple/simple.cpp
index 06d4431..db314ca 100644
--- a/tests/simple/simple.cpp
+++ b/tests/simple/simple.cpp
@@ -48,7 +48,9 @@ public:
* Constructor
*/
SimpleExtension() : Extension("simple", "1.0", {
- PhpCpp::Function("hallo")
+ PhpCpp::Function("hallo", {
+ PhpCpp::Argument("arg1", true)
+ })
})
{
}
diff --git a/tests/simple/simple.php b/tests/simple/simple.php
index 0fb3e32..c8614da 100644
--- a/tests/simple/simple.php
+++ b/tests/simple/simple.php
@@ -1,3 +1,19 @@
<?php
-hallo();
+
+class XXX
+{
+ public function __toString()
+ {
+ return "MyClass";
+ }
+}
+
+$myvar = "hoi";
+
+$result = hallo($myvar, 1, 2, 3, "blabla", new XXX());
+
+echo("myvar = $myvar\n");
+
+echo("resultaat: $result\n");
+
?> \ No newline at end of file