Skip to content

Commit

Permalink
Introduced PropertyPtrPtr interface to return pointer to Zval of prop…
Browse files Browse the repository at this point in the history
…erty object.
  • Loading branch information
kirmorozov committed Dec 24, 2020
1 parent 26cbf1e commit 687714e
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 0 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ SET(PHPCPP_HEADERS_INCLUDE
include/argument.h
include/array.h
include/arrayaccess.h
include/propertyptrptr.h
include/base.h
include/byref.h
include/byval.h
Expand Down
41 changes: 41 additions & 0 deletions include/propertyptrptr.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**
* PropertyPtrPtr.h
*
* "Interface" that can be "implemented" by your class. If you do, you
* create your class like this:
*
* class MyClass : public Php::Base, public Php::PropertyPtrPtr { ... }
*
* @author Kirill Morozov <[email protected]>
* @copyright 2020 Morozov
*/

/**
* Set up namespace
*/
namespace Php {

/**
* Class definition
*/
class PHPCPP_EXPORT PropertyPtrPtr
{
public:

/**
* Retrieve a member
* @param key
* @return value
*/
virtual Php::Value getPropertyPtrPtr(const Php::Value &member, int type) = 0;

/**
* Destructor
*/
virtual ~PropertyPtrPtr() = default;
};

/**
* End namespace
*/
}
1 change: 1 addition & 0 deletions phpcpp.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
#include <phpcpp/base.h>
#include <phpcpp/countable.h>
#include <phpcpp/arrayaccess.h>
#include <phpcpp/propertyptrptr.h>
#include <phpcpp/iterator.h>
#include <phpcpp/traversable.h>
#include <phpcpp/serializable.h>
Expand Down
42 changes: 42 additions & 0 deletions zend/classimpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,8 @@ zend_object_handlers *ClassImpl::objectHandlers()
_handlers.has_property = &ClassImpl::hasProperty;
_handlers.unset_property = &ClassImpl::unsetProperty;

_handlers.get_property_ptr_ptr = &ClassImpl::getPropertyPtrPtr;

// when a method is called (__call and __invoke)
_handlers.get_method = &ClassImpl::getMethod;
_handlers.get_closure = &ClassImpl::getClosure;
Expand Down Expand Up @@ -651,6 +653,46 @@ zval *ClassImpl::readDimension(zval *object, zval *offset, int type, zval *rv)
return std_object_handlers.read_dimension(object, offset, type, rv);
}
}
/**
* Function that is called when the object property is used for write operations in PHP
*
* This is the object->property[x]=y operation in PHP, and mapped to the getPropertyPtrPtr() method
* of the ArrayAccess PropertyPtrPtr
*
* @param object The object on which it is called
* @param member The name of the property
* @param type The type of operation 0 - read, 1 - write
* @return zval*
*/
zval *ClassImpl::getPropertyPtrPtr(zval *object, zval *member, int type, void **cache_slot)
{
PropertyPtrPtr *p_ptr_ptr = dynamic_cast<PropertyPtrPtr*>(ObjectImpl::find(object)->object());
if (p_ptr_ptr)
{
try
{
Php::Value res = p_ptr_ptr->getPropertyPtrPtr(member, type);
return res.detach(true);
}
catch (Throwable &throwable)
{
// object was not caught by the extension, let it end up in user space
throwable.rethrow();

// unreachable
return Value(nullptr).detach(false);
}

}
else
{
// ArrayAccess not implemented, check if there is a default handler
if (!std_object_handlers.get_property_ptr_ptr) return nullptr;

// call default
return std_object_handlers.get_property_ptr_ptr(object, member, type, cache_slot);
}
}

/**
* Function that is called when the object is used as an array in PHP
Expand Down
2 changes: 2 additions & 0 deletions zend/classimpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,8 @@ class ClassImpl
*/
static zend_object_handlers *objectHandlers(zend_class_entry *entry);

static zval *getPropertyPtrPtr(zval *object, zval *member, int type, void **cache_slot);

/**
* Function to create a new iterator to iterate over an object
* @param entry The class entry
Expand Down
1 change: 1 addition & 0 deletions zend/includes.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@
#include "../include/base.h"
#include "../include/countable.h"
#include "../include/arrayaccess.h"
#include "../include/propertyptrptr.h"
#include "../include/serializable.h"
#include "../include/iterator.h"
#include "../include/traversable.h"
Expand Down

0 comments on commit 687714e

Please sign in to comment.