summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEmiel Bruijntjes <emiel.bruijntjes@copernica.com>2014-07-26 11:50:02 +0200
committerEmiel Bruijntjes <emiel.bruijntjes@copernica.com>2014-07-26 11:50:02 +0200
commitff83769a9d5fc2f0f88cd42bdb3355f509294ad9 (patch)
tree66275722fa134c72d9dd061d6bde749296901d91
parent7de92192f6791ea326f8acbee90c4fa7f5880eae (diff)
parentd24ce36eaa07e9e924e7084fafa4f504cde39c2f (diff)
Merge branch 'issue#98' of https://github.com/andot/PHP-CPP into andot-issue#98
-rw-r--r--include/fastcall.h13
-rw-r--r--phpcpp.h3
-rw-r--r--zend/fastcall.cpp40
-rw-r--r--zend/includes.h1
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);
+
+}
+
diff --git a/phpcpp.h b/phpcpp.h
index c3f3365..8be1a25 100644
--- a/phpcpp.h
+++ b/phpcpp.h
@@ -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