Skip to content

Commit

Permalink
Merge pull request #513 from robocoder/patch-1
Browse files Browse the repository at this point in the history
reorder PHP_VERSION_ID conditionals for consistency
  • Loading branch information
EmielBruijntjes authored Nov 20, 2023
2 parents 72e1960 + b1b7432 commit e64e3ac
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 91 deletions.
18 changes: 9 additions & 9 deletions common/modifiers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,22 @@ namespace Php {
/**
* The modifiers are constants
*/
#if PHP_VERSION_ID >= 70400
const int Static = 0x10;
const int Abstract = 0x40;
const int Final = 0x20;
const int Public = 0x01;
const int Protected = 0x02;
const int Private = 0x04;
const int Const = 0;
#else
#if PHP_VERSION_ID < 70400
const int Static = 0x01;
const int Abstract = 0x02;
const int Final = 0x04;
const int Public = 0x100;
const int Protected = 0x200;
const int Private = 0x400;
const int Const = 0;
#else
const int Static = 0x10;
const int Abstract = 0x40;
const int Final = 0x20;
const int Public = 0x01;
const int Protected = 0x02;
const int Private = 0x04;
const int Const = 0;
#endif

/**
Expand Down
8 changes: 4 additions & 4 deletions zend/callable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ void Callable::invoke(INTERNAL_FUNCTION_PARAMETERS)
// Sanity check
assert(ZEND_TYPE_IS_SET(info[argc].type) && info[argc].name == nullptr);
// the callable we are retrieving
#if PHP_VERSION_ID < 80000
# if PHP_VERSION_ID < 80000
Callable *callable = reinterpret_cast<Callable*>(info[argc].type);
#else
# else
Callable *callable = reinterpret_cast<Callable*>(info[argc].type.ptr);
#endif
# endif
#endif

// check if sufficient parameters were passed (for some reason this check
Expand All @@ -64,7 +64,7 @@ void Callable::invoke(INTERNAL_FUNCTION_PARAMETERS)
{
// get the result
Value result(callable->invoke(params));

// return a full copy of the zval, and do not destruct it
RETVAL_ZVAL(result._val, 1, 0);
}
Expand Down
34 changes: 17 additions & 17 deletions zend/callable.h
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,23 @@ class Callable
case Type::Object: info->type_hint = IS_OBJECT; break; // must be an object of the given classname
case Type::Callable: info->type_hint = IS_CALLABLE; break; // anything that can be invoked
default: info->type_hint = IS_UNDEF; break; // if not specified we allow anything
#elif PHP_VERSION_ID >= 80000
#elif PHP_VERSION_ID < 80000
case Type::Undefined: info->type = ZEND_TYPE_ENCODE(IS_UNDEF, arg.allowNull()); break; // undefined means we'll accept any type
case Type::Null: info->type = ZEND_TYPE_ENCODE(IS_UNDEF, arg.allowNull()); break; // this is likely an error, what good would accepting NULL be? accept anything
case Type::False: info->type = ZEND_TYPE_ENCODE(_IS_BOOL, arg.allowNull()); break; // accept true as well ;)
case Type::True: info->type = ZEND_TYPE_ENCODE(_IS_BOOL, arg.allowNull()); break; // accept false as well
case Type::Bool: info->type = ZEND_TYPE_ENCODE(_IS_BOOL, arg.allowNull()); break; // any bool will do, true, false, the options are limitless
case Type::Numeric: info->type = ZEND_TYPE_ENCODE(IS_LONG, arg.allowNull()); break; // accept integers here
case Type::Float: info->type = ZEND_TYPE_ENCODE(IS_DOUBLE, arg.allowNull()); break; // floating-point values welcome too
case Type::String: info->type = ZEND_TYPE_ENCODE(IS_STRING, arg.allowNull()); break; // accept strings, should auto-cast objects with __toString as well
case Type::Array: info->type = ZEND_TYPE_ENCODE(IS_ARRAY, arg.allowNull()); break; // array of anything (individual members cannot be restricted)
case Type::Object: // if there is a classname and the argument is not nullable, it's simply the classname
if (!arg.classname()) info->type = ZEND_TYPE_ENCODE(IS_OBJECT, arg.allowNull());
else info->type = (zend_type)arg.encoded();
break;
case Type::Callable: info->type = ZEND_TYPE_ENCODE(IS_CALLABLE, arg.allowNull()); break; // anything that can be invoked
default: info->type = ZEND_TYPE_ENCODE(IS_UNDEF, arg.allowNull()); break; // if not specified we allow anything
#else
case Type::Undefined: info->type = (zend_type) ZEND_TYPE_INIT_NONE(_ZEND_ARG_INFO_FLAGS((unsigned int)arg.byReference(), 0, 0)); break; // undefined means we'll accept any type
case Type::Null: info->type = (zend_type) ZEND_TYPE_INIT_NONE(_ZEND_ARG_INFO_FLAGS((unsigned int)arg.byReference(), 0, 0)); break; // this is likely an error, what good would accepting NULL be? accept anything
case Type::False: info->type = (zend_type) ZEND_TYPE_INIT_CODE(_IS_BOOL, arg.allowNull(), _ZEND_ARG_INFO_FLAGS(arg.byReference(), 0, 0)); break; // accept true as well ;)
Expand All @@ -224,22 +240,6 @@ class Callable
case Type::Callable: info->type = (zend_type) ZEND_TYPE_INIT_CODE(IS_CALLABLE, arg.allowNull(), 0); break; // anything that can be invoke

default: info->type = ZEND_TYPE_INIT_CODE(IS_UNDEF, 0, _ZEND_ARG_INFO_FLAGS(arg.byReference(), 0, 0)); break; // if not specified we allow anything
#else
case Type::Undefined: info->type = ZEND_TYPE_ENCODE(IS_UNDEF, arg.allowNull()); break; // undefined means we'll accept any type
case Type::Null: info->type = ZEND_TYPE_ENCODE(IS_UNDEF, arg.allowNull()); break; // this is likely an error, what good would accepting NULL be? accept anything
case Type::False: info->type = ZEND_TYPE_ENCODE(_IS_BOOL, arg.allowNull()); break; // accept true as well ;)
case Type::True: info->type = ZEND_TYPE_ENCODE(_IS_BOOL, arg.allowNull()); break; // accept false as well
case Type::Bool: info->type = ZEND_TYPE_ENCODE(_IS_BOOL, arg.allowNull()); break; // any bool will do, true, false, the options are limitless
case Type::Numeric: info->type = ZEND_TYPE_ENCODE(IS_LONG, arg.allowNull()); break; // accept integers here
case Type::Float: info->type = ZEND_TYPE_ENCODE(IS_DOUBLE, arg.allowNull()); break; // floating-point values welcome too
case Type::String: info->type = ZEND_TYPE_ENCODE(IS_STRING, arg.allowNull()); break; // accept strings, should auto-cast objects with __toString as well
case Type::Array: info->type = ZEND_TYPE_ENCODE(IS_ARRAY, arg.allowNull()); break; // array of anything (individual members cannot be restricted)
case Type::Object: // if there is a classname and the argument is not nullable, it's simply the classname
if (!arg.classname()) info->type = ZEND_TYPE_ENCODE(IS_OBJECT, arg.allowNull());
else info->type = (zend_type)arg.encoded();
break;
case Type::Callable: info->type = ZEND_TYPE_ENCODE(IS_CALLABLE, arg.allowNull()); break; // anything that can be invoked
default: info->type = ZEND_TYPE_ENCODE(IS_UNDEF, arg.allowNull()); break; // if not specified we allow anything
#endif
}
#if PHP_VERSION_ID < 80000
Expand Down
60 changes: 31 additions & 29 deletions zend/classimpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -287,10 +287,10 @@ zend_function *ClassImpl::getStaticMethod(zend_class_entry *entry, zend_string *
*/
#if PHP_VERSION_ID < 80000
int ClassImpl::getClosure(ZEND_OBJECT_OR_ZVAL object, zend_class_entry **entry_ptr, zend_function **func, zend_object **object_ptr)
#elif PHP_VERSION_ID > 80200
zend_result ClassImpl::getClosure(ZEND_OBJECT_OR_ZVAL object, zend_class_entry **entry_ptr, zend_function **func, zend_object **object_ptr, zend_bool check_only)
#else
#elif PHP_VERSION_ID < 80200
int ClassImpl::getClosure(ZEND_OBJECT_OR_ZVAL object, zend_class_entry **entry_ptr, zend_function **func, zend_object **object_ptr, zend_bool check_only)
#else
zend_result ClassImpl::getClosure(ZEND_OBJECT_OR_ZVAL object, zend_class_entry **entry_ptr, zend_function **func, zend_object **object_ptr, zend_bool check_only)
#endif
{
// it is really unbelievable how the Zend engine manages to implement every feature
Expand Down Expand Up @@ -473,10 +473,10 @@ int ClassImpl::compare(zval *val1, zval *val2)
* @param type
* @return int
*/
#if PHP_VERSION_ID >= 80200
zend_result ClassImpl::cast(ZEND_OBJECT_OR_ZVAL val, zval *retval, int type)
#else
#if PHP_VERSION_ID < 80200
int ClassImpl::cast(ZEND_OBJECT_OR_ZVAL val, zval *retval, int type)
#else
zend_result ClassImpl::cast(ZEND_OBJECT_OR_ZVAL val, zval *retval, int type)
#endif
{
// get the base c++ object
Expand Down Expand Up @@ -589,10 +589,10 @@ zend_object *ClassImpl::cloneObject(ZEND_OBJECT_OR_ZVAL val)
* @param count
* @return int
*/
#if PHP_VERSION_ID >= 80200
zend_result ClassImpl::countElements(ZEND_OBJECT_OR_ZVAL object, zend_long *count)
#else
#if PHP_VERSION_ID < 80200
int ClassImpl::countElements(ZEND_OBJECT_OR_ZVAL object, zend_long *count)
#else
zend_result ClassImpl::countElements(ZEND_OBJECT_OR_ZVAL object, zend_long *count)
#endif
{
// does it implement the countable interface?
Expand Down Expand Up @@ -995,12 +995,12 @@ PHP_WRITE_PROP_HANDLER_TYPE ClassImpl::writeProperty(ZEND_OBJECT_OR_ZVAL object,
{
// check if it could be set
if (iter->second->set(base, value)) {
#if PHP_VERSION_ID >= 70400
return value;
#else
#if PHP_VERSION_ID < 70400
return;
#else
return value;
#endif
}
}

// read-only property
zend_error(E_ERROR, "Unable to write to read-only property %s", (const char *)key);
Expand All @@ -1010,27 +1010,29 @@ PHP_WRITE_PROP_HANDLER_TYPE ClassImpl::writeProperty(ZEND_OBJECT_OR_ZVAL object,
{
// __set() function was not overridden by user, check if there is a default
if (!std_object_handlers.write_property) {
#if PHP_VERSION_ID >= 70400
return value;
#if PHP_VERSION_ID < 70400
return;
#else
return;
return value;
#endif
}

// call the default
std_object_handlers.write_property(object, name, value, cache_slot);
#if PHP_VERSION_ID >= 70400
return value;
#if PHP_VERSION_ID < 70400
return;
#else
return;
return value;
#endif
}
catch (Throwable &throwable)
{
// object was not caught by the extension, let it end up in user space
throwable.rethrow();
}
#if PHP_VERSION_ID >= 70400
#if PHP_VERSION_ID < 70400
return;
#else
return value;
#endif
}
Expand Down Expand Up @@ -1252,7 +1254,7 @@ zend_object_iterator *ClassImpl::getIterator(zend_class_entry *entry, zval *obje

// retrieve the traversable object
Traversable *traversable = dynamic_cast<Traversable*>(ObjectImpl::find(object)->object());

// use might throw an exception in the getIterator() function
try
{
Expand All @@ -1263,10 +1265,10 @@ zend_object_iterator *ClassImpl::getIterator(zend_class_entry *entry, zval *obje
// the iteraters itself, we can no longer let c++ allocate the buffer + object
// directly, so we first allocate the buffer, which is going to be cleaned up by php)
auto *buffer = emalloc(sizeof(IteratorImpl));

// and then we use placement-new to allocate the implementation
auto *wrapper = new(buffer)IteratorImpl(object, userspace);

// done
return wrapper->implementation();
}
Expand Down Expand Up @@ -1473,7 +1475,7 @@ zend_class_entry *ClassImpl::initialize(ClassBase *base, const std::string &pref
INIT_CLASS_ENTRY_EX(entry, _name.c_str(), _name.size(), entries());

// we need a special constructor, but only for real classes, not for interfaces.
// (in fact: from php 7.4 onwards the create_object method is part of union
// (in fact: from php 7.4 onwards the create_object method is part of union
// together with the interface_gets_implemented method, which causes a crash
// when the create_object property is set for an interface)
if (_type != ClassType::Interface) entry.create_object = &ClassImpl::createObject;
Expand All @@ -1482,7 +1484,7 @@ zend_class_entry *ClassImpl::initialize(ClassBase *base, const std::string &pref
entry.get_static_method = &ClassImpl::getStaticMethod;

// for traversable classes we install a special method to get the iterator
if (_base->traversable())
if (_base->traversable())
{
// install iterator functions
entry.get_iterator = &ClassImpl::getIterator;
Expand Down Expand Up @@ -1527,7 +1529,7 @@ zend_class_entry *ClassImpl::initialize(ClassBase *base, const std::string &pref
// register the class
_entry = zend_register_internal_class(&entry);
}

// register the interfaces
for (auto &interface : _interfaces)
{
Expand All @@ -1537,9 +1539,9 @@ zend_class_entry *ClassImpl::initialize(ClassBase *base, const std::string &pref
// otherwise report an error
else std::cerr << "Derived class " << name() << " is initialized before base class " << interface->name() << ": interface is ignored" << std::endl;
}

// we may have to expose the Traversable
if (_base->traversable())
if (_base->traversable())
{
#if PHP_VERSION_ID < 80000
// up to php 7.x we can implement just the Traversable method (strictly speaking, it does not seem
Expand All @@ -1551,7 +1553,7 @@ zend_class_entry *ClassImpl::initialize(ClassBase *base, const std::string &pref
zend_class_implements(_entry, 1, zend_ce_aggregate);
#endif
}

// check if the Serializable interface
if (_base->serializable())
{
Expand Down
26 changes: 13 additions & 13 deletions zend/classimpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,17 @@
*/
namespace Php {

#if PHP_VERSION_ID >= 70400
# define PHP_WRITE_PROP_HANDLER_TYPE zval *
#if PHP_VERSION_ID < 70400
# define PHP_WRITE_PROP_HANDLER_TYPE void
#else
# define PHP_WRITE_PROP_HANDLER_TYPE void
# define PHP_WRITE_PROP_HANDLER_TYPE zval *
#endif
#if PHP_VERSION_ID < 80000
#define ZEND_OBJECT_OR_ZVAL zval *
#define ZEND_STRING_OR_ZVAL zval *
# define ZEND_OBJECT_OR_ZVAL zval *
# define ZEND_STRING_OR_ZVAL zval *
#else
#define ZEND_OBJECT_OR_ZVAL zend_object *
#define ZEND_STRING_OR_ZVAL zend_string *
# define ZEND_OBJECT_OR_ZVAL zend_object *
# define ZEND_STRING_OR_ZVAL zend_string *
#endif
/**
* Class definition
Expand Down Expand Up @@ -218,10 +218,10 @@ class ClassImpl
* @param count
* @return int
*/
#if PHP_VERSION_ID >= 80200
static zend_result countElements(ZEND_OBJECT_OR_ZVAL object, zend_long *count);
#else
#if PHP_VERSION_ID < 80200
static int countElements(ZEND_OBJECT_OR_ZVAL object, zend_long *count);
#else
static zend_result countElements(ZEND_OBJECT_OR_ZVAL object, zend_long *count);
#endif
/**
* Function that is called when the object is used as an array in PHP
Expand Down Expand Up @@ -346,10 +346,10 @@ class ClassImpl
* @param type
* @return int
*/
#if PHP_VERSION_ID >= 80200
static zend_result cast(ZEND_OBJECT_OR_ZVAL object, zval *retval, int type);
#else
#if PHP_VERSION_ID < 80200
static int cast(ZEND_OBJECT_OR_ZVAL object, zval *retval, int type);
#else
static zend_result cast(ZEND_OBJECT_OR_ZVAL object, zval *retval, int type);
#endif

/**
Expand Down
14 changes: 7 additions & 7 deletions zend/file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ namespace Php {
*/
File::File(const char *name, size_t size) : _original(zend_string_init(name, size, 0))
{
#if PHP_VERSION_ID < 80000
#if PHP_VERSION_ID < 80100
// resolve the path
_path = zend_resolve_path(name, size);
_path = zend_resolve_path(name, size);
#else
// first convert the path, then read it
_path = zend_resolve_path(_original);
Expand All @@ -44,7 +44,7 @@ File::~File()
{
// clean up path name
if (_path) zend_string_release(_path);

// clean up original path
if (_original) zend_string_release(_original);
}
Expand All @@ -64,7 +64,10 @@ bool File::compile()
// we are going to open the file
zend_file_handle fileHandle;

#if PHP_VERSION_ID > 80000
#if PHP_VERSION_ID < 80100
// open the file
if (zend_stream_open(ZSTR_VAL(_path), &fileHandle) == FAILURE) return false;
#else
/**
* zend_stream_open now only accepts the fileHandle object
* Filename must now be set through the object path.
Expand All @@ -73,9 +76,6 @@ bool File::compile()

// open the file
if (zend_stream_open(&fileHandle) == FAILURE) return false;
#else
// open the file
if (zend_stream_open(ZSTR_VAL(_path), &fileHandle) == FAILURE) return false;
#endif

// make sure the path name is stored in the handle (@todo: is this necessary? do we need the copy?)
Expand Down
10 changes: 5 additions & 5 deletions zend/script.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@ zend_op_array *Script::compile(const char *name, const char *phpcode, size_t siz

// remember the old compiler options, and set new compiler options
CompilerOptions options(ZEND_COMPILE_DEFAULT_FOR_EVAL);

// compile the string
#if PHP_VERSION_ID < 80000
return zend_compile_string(source._val, (char *)name);
#elif PHP_VERSION_ID >= 80200
return zend_compile_string(zval_get_string(source._val), (char*)name, ZEND_COMPILE_POSITION_AT_OPEN_TAG);
#else
#elif PHP_VERSION_ID < 80200
return zend_compile_string(zval_get_string(source._val), (char*)name);
#else
return zend_compile_string(zval_get_string(source._val), (char*)name, ZEND_COMPILE_POSITION_AT_OPEN_TAG);
#endif
}

Expand Down Expand Up @@ -86,7 +86,7 @@ Value Script::execute() const
{
// pass on to opcodes
if (!_opcodes) return nullptr;

// execute opcodes
return _opcodes->execute();
}
Expand Down
Loading

0 comments on commit e64e3ac

Please sign in to comment.