summaryrefslogtreecommitdiff
path: root/zend
diff options
context:
space:
mode:
authorEmiel Bruijntjes <emiel.bruijntjes@copernica.com>2015-04-13 08:54:55 +0200
committerEmiel Bruijntjes <emiel.bruijntjes@copernica.com>2015-04-13 08:54:55 +0200
commit6dcadfb26de2ce9117f58986d543c521143df1c2 (patch)
tree92dd8efd6de0a4d9d837908caa4232ccd59bff80 /zend
parent6328204053b26585cc0a590815f912fb8b153f41 (diff)
parentfaa7df504380295296e3349e9360732d750554c3 (diff)
Merge branch 'master' of https://github.com/CopernicaMarketingSoftware/PHP-CPP
Diffstat (limited to 'zend')
-rw-r--r--zend/eval.cpp9
-rw-r--r--zend/executestate.h18
-rw-r--r--zend/hiddenpointer.h10
-rw-r--r--zend/module.cpp30
-rw-r--r--zend/module.h78
-rw-r--r--zend/opcodes.h2
6 files changed, 138 insertions, 9 deletions
diff --git a/zend/eval.cpp b/zend/eval.cpp
index 7ab4957..6d043b1 100644
--- a/zend/eval.cpp
+++ b/zend/eval.cpp
@@ -93,12 +93,17 @@ Value require_once(const char *filename)
/**
* Implementation of the dl() function - activate a different PHP extension
+ *
+ * If you open an extension persistently, the static variables inside it are
+ * kept open for as long as apache runs.
+ *
* @param filename
+ * @param persistent
*/
-bool dl(const char *filename)
+bool dl(const char *filename, bool persistent)
{
// create the module
- Module module(filename);
+ Module module(filename, persistent);
// start the module
return module.start();
diff --git a/zend/executestate.h b/zend/executestate.h
index 8e33b16..d71a252 100644
--- a/zend/executestate.h
+++ b/zend/executestate.h
@@ -26,11 +26,18 @@ class ExecuteState
private:
/**
* All the original settings
+ * @var mixed
*/
zend_op_array *_active_op_array;
zval **_return_value_ptr_ptr;
zend_op **_opline_ptr;
int _interactive;
+
+ /**
+ * The new value for 'no-extensions'
+ * @var int
+ */
+ int _no_extensions;
#ifdef ZTS
/**
@@ -42,15 +49,22 @@ private:
public:
/**
+ * No trivial constructor
+ */
+ ExecuteState() = delete;
+
+ /**
* Constructor
+ * @param no_extensions
*/
- ExecuteState(TSRMLS_D)
+ ExecuteState(int no_extensions TSRMLS_DC)
{
// store all the original stuff
_active_op_array = EG(active_op_array);
_return_value_ptr_ptr = EG(return_value_ptr_ptr);
_opline_ptr = EG(opline_ptr);
_interactive = CG(interactive);
+ _no_extensions = no_extensions;
#ifdef ZTS
// copy tsrm_ls param
@@ -65,7 +79,7 @@ public:
{
// restore all settings
CG(interactive) = _interactive;
- EG(no_extensions) = 0;
+ EG(no_extensions) = _no_extensions;
EG(opline_ptr) = _opline_ptr;
EG(active_op_array) = _active_op_array;
EG(return_value_ptr_ptr) = _return_value_ptr_ptr;
diff --git a/zend/hiddenpointer.h b/zend/hiddenpointer.h
index d2636e3..147886e 100644
--- a/zend/hiddenpointer.h
+++ b/zend/hiddenpointer.h
@@ -38,8 +38,14 @@ public:
// copy pointer into the buffer
memcpy(buffer, &pointer, sizeof(Type *));
- // copy the name into the buffer
- memcpy(buffer + sizeof(Type *), text, size + 1);
+ // copy the name into the buffer, making it lower case at the same time
+ // (php function names are case insensitive, and must even be lowercase
+ // because all the lookups are done in lowercase)
+ for (int i = 0; i < size + 1; i++)
+ {
+ // convert char the lower case
+ buffer[sizeof(Type *) + i] = tolower(text[i]);
+ }
// store in member
_buffer = buffer;
diff --git a/zend/module.cpp b/zend/module.cpp
new file mode 100644
index 0000000..46d03d8
--- /dev/null
+++ b/zend/module.cpp
@@ -0,0 +1,30 @@
+/**
+ * Module.cpp
+ *
+ * Module implementation file
+ *
+ * @author Emiel Bruijntjes <emiel.bruijntjes@copernica.com>
+ * @copyright 2015 Copernica BV
+ */
+
+/**
+ * Dependencies
+ */
+#include "includes.h"
+
+/**
+ * Set up namespace
+ */
+namespace Php {
+
+/**
+ * The persistent handles
+ * @var Module::Persistent
+ */
+Module::Persistent Module::_persistent;
+
+/**
+ * End of namespace
+ */
+}
+
diff --git a/zend/module.h b/zend/module.h
index bb36810..4431355 100644
--- a/zend/module.h
+++ b/zend/module.h
@@ -43,7 +43,7 @@ private:
* @var zend_module_entry
*/
zend_module_entry *_entry = nullptr;
-
+
#ifdef ZTS
/**
* When in thread safety mode, we also keep track of the TSRM_LS var
@@ -52,12 +52,82 @@ private:
void ***tsrm_ls;
#endif
+ /**
+ * Internal helper class with persistent modules
+ */
+ class Persistent
+ {
+ private:
+ /**
+ * The set of handles
+ * @var std::set
+ */
+ std::set<void*> _handles;
+
+ public:
+ /**
+ * Constructor
+ */
+ Persistent() {}
+
+ /**
+ * Destructor
+ */
+ virtual ~Persistent()
+ {
+ // remove all handles
+ while (!_handles.empty())
+ {
+ // get first handle
+ auto iter = _handles.begin();
+
+ // remove the handle
+ DL_UNLOAD(*iter);
+
+ // remove from set
+ _handles.erase(iter);
+ }
+ }
+
+ /**
+ * Check whether a handle is already persistently opened
+ * @param handle
+ * @return bool
+ */
+ bool contains(void *handle) const
+ {
+ return _handles.find(handle) != _handles.end();
+ }
+
+ /**
+ * Add a library persistently
+ * @param module
+ */
+ void add(const char *module)
+ {
+ // insert the handle
+ _handles.insert(DL_LOAD(module));
+ }
+ };
+
+ /**
+ * All persistent modules
+ * @var Persistent
+ */
+ static Persistent _persistent;
+
public:
/**
* Constructor
+ *
+ * A module can be loaded persistently. This means that the variables in
+ * the module will keep in scope for as long as Apache runs, even though
+ * the extension is not active in other page views
+ *
* @param module Name of the module
+ * @param persistent Should it be loaded persistently
*/
- Module(const char *module)
+ Module(const char *module, bool persistent)
{
#ifdef ZTS
// fetch multi-threading thing
@@ -76,6 +146,10 @@ public:
// handle should be valid
if (!_handle) return;
+ // if we have to open it persistently, we open it for a second time so that
+ // the refcounter always stays 1 or higher
+ if (persistent && !_persistent.contains(_handle)) _persistent.add(module);
+
// we have to call the get_module() function
Symbol<zend_module_entry*()> get_module(_handle, "get_module");
diff --git a/zend/opcodes.h b/zend/opcodes.h
index 7e6d1ec..4e78083 100644
--- a/zend/opcodes.h
+++ b/zend/opcodes.h
@@ -74,7 +74,7 @@ public:
// the zend engine is probably already busy processing opcodes, so we store
// the current execute state before we're going to switch the runtime to
// our own set of opcodes
- ExecuteState oldstate(TSRMLS_C);
+ ExecuteState execState(0 TSRMLS_CC);
// old execute state has been saved (and will automatically be restured when
// the oldstate is destructed), so we can now safely overwrite all the settings