diff options
-rw-r--r-- | include/fastcall.h | 13 | ||||
-rw-r--r-- | phpcpp.h | 3 | ||||
-rw-r--r-- | zend/fastcall.cpp | 40 | ||||
-rw-r--r-- | zend/includes.h | 1 |
4 files changed, 56 insertions, 1 deletions
diff --git a/include/fastcall.h b/include/fastcall.h new file mode 100644 index 0000000..29f274d --- /dev/null +++ b/include/fastcall.h @@ -0,0 +1,13 @@ +/** + * fastcall.h + * + * This file holds some PHP functions implementation in C directly. + * + */ + +namespace Php { + + Value eval(const std::string &phpCode); + +} + @@ -2,7 +2,7 @@ * phpcpp.h * * Library to build PHP extensions with CPP - * + * * @copyright 2013 CopernicA BV * @author Emiel Bruijntjes <emiel.bruijntjes@copernica.com> */ @@ -60,6 +60,7 @@ #include <phpcpp/namespace.h> #include <phpcpp/extension.h> #include <phpcpp/call.h> +#include <phpcpp/fastcall.h> /** * Macro to export a function diff --git a/zend/fastcall.cpp b/zend/fastcall.cpp new file mode 100644 index 0000000..3ecd598 --- /dev/null +++ b/zend/fastcall.cpp @@ -0,0 +1,40 @@ +/** + * fastcall.cpp + * + * This file holds some PHP functions implementation in C directly. + * + */ + +#include "includes.h" + +namespace Php { + + Value eval(const std::string &phpCode) { + // we need the tsrm_ls variable + TSRMLS_FETCH(); + + // the current exception + zval* oldException = EG(exception); + + // the return zval + zval* retval = nullptr; + if (zend_eval_stringl_ex((char *)phpCode.c_str(), (int32_t)phpCode.length(), retval, (char *)"", 1 TSRMLS_CC) != SUCCESS) + { + // throw an exception, php couldn't evaluate code + throw Exception("PHP couldn't evaluate: " + phpCode); + + // 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) TSRMLS_CC); + + // no (additional) exception was thrown + return retval ? Value(retval) : nullptr; + } + } + +}
\ No newline at end of file diff --git a/zend/includes.h b/zend/includes.h index 63b435e..8af557c 100644 --- a/zend/includes.h +++ b/zend/includes.h @@ -79,6 +79,7 @@ #include "../include/namespace.h" #include "../include/extension.h" #include "../include/call.h" +#include "../include/fastcall.h" /** * Common header files for internal use only |