diff options
author | Emiel Bruijntjes <emiel.bruijntjes@copernica.com> | 2014-03-08 14:15:15 +0100 |
---|---|---|
committer | Emiel Bruijntjes <emiel.bruijntjes@copernica.com> | 2014-03-08 14:15:15 +0100 |
commit | 78e7865b8dac7229bdc128af870a4cad11cf1cd2 (patch) | |
tree | ff0a660ca7916e7c7d5e75d3f82a09033d514a39 | |
parent | 2c484adbab9255498953739cb4d9751353cc804d (diff) |
exception is now thrown when a function is called that does not exist
-rw-r--r-- | documentation/calling-functions-and-methods.html | 15 | ||||
-rw-r--r-- | include/classbase.h | 2 | ||||
-rw-r--r-- | src/value.cpp | 58 |
3 files changed, 50 insertions, 25 deletions
diff --git a/documentation/calling-functions-and-methods.html b/documentation/calling-functions-and-methods.html index da3b57c..47a3bf9 100644 --- a/documentation/calling-functions-and-methods.html +++ b/documentation/calling-functions-and-methods.html @@ -57,7 +57,7 @@ void example_function(Php::Parameters &params) // in PHP it is possible to create an array with two parameters, the first // parameter being an object, and the second parameter should be the name // of the method, we can do that in PHP-CPP too - Php::Array time_format(time, "format"); + Php::Array time_format({time, "format"}); // call the method that is stored in the array std::cout << time_format("Y-m-d H:i:s") << std::endl; @@ -143,3 +143,16 @@ extern "C" { }); ?></code></pre> </p> +<p> + This above script will generate output similar to this: +<p> +<pre> +key: x +key: y +key: z +userspace function called with some_parameter +2014-03-08 13:55:54 +lambda function called with param some parameter +2014-03-08 13:55:54 +</pre> +</p>
\ No newline at end of file diff --git a/include/classbase.h b/include/classbase.h index 25d081f..f15e879 100644 --- a/include/classbase.h +++ b/include/classbase.h @@ -240,7 +240,7 @@ private: * @param count * @return int */ - static int countElements(struct _zval *object, long *count); + static int countElements(struct _zval_struct *object, long *count); /** * Retrieve pointer to our own object handlers diff --git a/src/value.cpp b/src/value.cpp index b775892..7b1a931 100644 --- a/src/value.cpp +++ b/src/value.cpp @@ -1142,12 +1142,14 @@ Value Value::call(const char *name, Value p0, Value p1, Value p2, Value p3, Valu } /** - * Call function with a number of parameters - * @param argc Number of parameters - * @param argv The parameters + * Helper function that runs the actual call + * @param object The object to call it on + * @param method The function or method to call + * @param args Number of arguments + * @param params The parameters * @return Value */ -Value Value::exec(int argc, zval ***params) const +static Value do_exec(zval **object, zval *method, int argc, zval ***params) { // the return zval zval *retval = nullptr; @@ -1156,13 +1158,35 @@ Value Value::exec(int argc, zval ***params) const zval *oldException = EG(exception); // call the function - if (call_user_function_ex(CG(function_table), NULL, _val, &retval, argc, params, 1, NULL) != SUCCESS) return nullptr; + if (call_user_function_ex(CG(function_table), object, method, &retval, argc, params, 1, NULL) != SUCCESS) + { + // throw an exception, the function does not exist + throw Exception("Invalid call to "+Value(method).stringValue()); + + // unreachable, but let's return at least something to prevent compiler warnings + return nullptr; + } + else + { + // was an exception thrown inside the function? In that case we throw a C++ new exception + // to give the C++ code the chance to catch it + if (oldException != EG(exception) && EG(exception)) throw OrigException(EG(exception)); - // was an exception thrown? we throw a C++ new exception to give the C++ the chance to catch it - if (oldException != EG(exception) && EG(exception)) throw OrigException(EG(exception)); + // no (additional) exception was thrown + return retval ? Value(retval) : nullptr; + } +} - // no (additional) exception was thrown - return retval ? Value(retval) : nullptr; +/** + * Call function with a number of parameters + * @param argc Number of parameters + * @param argv The parameters + * @return Value + */ +Value Value::exec(int argc, zval ***params) const +{ + // call helper function + return do_exec(nullptr, _val, argc, params); } /** @@ -1177,20 +1201,8 @@ Value Value::exec(const char *name, int argc, struct _zval_struct ***params) // wrap the name in a Php::Value object to get a zval Value method(name); - // the method to call and the return value - zval *retval; - - // the current exception - zval *oldException = EG(exception); - - // call the function - if (call_user_function_ex(CG(function_table), &_val, method._val, &retval, argc, params, 1, NULL) != SUCCESS) return nullptr; - - // was an exception thrown? - if (oldException != EG(exception)) throw OrigException(EG(exception)); - - // no (additional) exception was thrown - return retval ? Value(retval) : nullptr; + // call helper function + return do_exec(&_val, method._val, argc, params); } /** |