Use NO_DYNAMIC_PROPERTIES for Closure

Instead of manually implementing this, use the standard mechanism.
This has minor behavior changes (e.g. doing an isset() will now
return false instead of throwing) which are more in line with
typical behavior.
This commit is contained in:
Nikita Popov 2021-05-14 14:47:40 +02:00
parent e7135cb817
commit 008bfcc7ba
7 changed files with 10 additions and 58 deletions

View File

@ -13,11 +13,7 @@ var_dump($ref->hasProperty('b'));
var_dump(isset($obj->a));
?>
--EXPECTF--
--EXPECT--
bool(false)
bool(false)
bool(false)
Fatal error: Uncaught Error: Closure object cannot have properties in %s:%d
Stack trace:
#0 {main}
thrown in %s on line %d

View File

@ -8,7 +8,7 @@ $foo = function() use ($a) {
$foo->a = 1;
?>
--EXPECTF--
Fatal error: Uncaught Error: Closure object cannot have properties in %sclosure_022.php:5
Fatal error: Uncaught Error: Cannot create dynamic property Closure::$a in %s:%d
Stack trace:
#0 {main}
thrown in %sclosure_022.php on line 5

View File

@ -3,7 +3,7 @@ Closure 031: Closure properties with custom error handlers
--FILE--
<?php
function foo($errno, $errstr, $errfile, $errline) {
echo "Error: $errstr\n";
echo "Warning: $errstr\n";
}
set_error_handler('foo');
$foo = function() {
@ -15,4 +15,5 @@ try {
}
?>
--EXPECT--
Error: Closure object cannot have properties
Warning: Undefined property: Closure::$a
NULL

View File

@ -19,4 +19,4 @@ try {
?>
--EXPECT--
Closure object cannot have properties
Cannot create dynamic property Closure::$b

View File

@ -28,11 +28,6 @@
#include "zend_globals.h"
#include "zend_closures_arginfo.h"
#define ZEND_CLOSURE_PRINT_NAME "Closure object"
#define ZEND_CLOSURE_PROPERTY_ERROR() \
zend_throw_error(NULL, "Closure object cannot have properties")
typedef struct _zend_closure {
zend_object std;
zend_function func;
@ -442,42 +437,6 @@ static zend_function *zend_closure_get_method(zend_object **object, zend_string
}
/* }}} */
static ZEND_COLD zval *zend_closure_read_property(zend_object *object, zend_string *member, int type, void **cache_slot, zval *rv) /* {{{ */
{
ZEND_CLOSURE_PROPERTY_ERROR();
return &EG(uninitialized_zval);
}
/* }}} */
static ZEND_COLD zval *zend_closure_write_property(zend_object *object, zend_string *member, zval *value, void **cache_slot) /* {{{ */
{
ZEND_CLOSURE_PROPERTY_ERROR();
return &EG(error_zval);
}
/* }}} */
static ZEND_COLD zval *zend_closure_get_property_ptr_ptr(zend_object *object, zend_string *member, int type, void **cache_slot) /* {{{ */
{
ZEND_CLOSURE_PROPERTY_ERROR();
return NULL;
}
/* }}} */
static ZEND_COLD int zend_closure_has_property(zend_object *object, zend_string *member, int has_set_exists, void **cache_slot) /* {{{ */
{
if (has_set_exists != ZEND_PROPERTY_EXISTS) {
ZEND_CLOSURE_PROPERTY_ERROR();
}
return 0;
}
/* }}} */
static ZEND_COLD void zend_closure_unset_property(zend_object *object, zend_string *member, void **cache_slot) /* {{{ */
{
ZEND_CLOSURE_PROPERTY_ERROR();
}
/* }}} */
static void zend_closure_free_storage(zend_object *object) /* {{{ */
{
zend_closure *closure = (zend_closure *)object;
@ -645,11 +604,6 @@ void zend_register_closure_ce(void) /* {{{ */
closure_handlers.free_obj = zend_closure_free_storage;
closure_handlers.get_constructor = zend_closure_get_constructor;
closure_handlers.get_method = zend_closure_get_method;
closure_handlers.write_property = zend_closure_write_property;
closure_handlers.read_property = zend_closure_read_property;
closure_handlers.get_property_ptr_ptr = zend_closure_get_property_ptr_ptr;
closure_handlers.has_property = zend_closure_has_property;
closure_handlers.unset_property = zend_closure_unset_property;
closure_handlers.compare = zend_closure_compare;
closure_handlers.clone_obj = zend_closure_clone;
closure_handlers.get_debug_info = zend_closure_get_debug_info;

View File

@ -2,6 +2,7 @@
/** @generate-class-entries */
/** @strict-properties */
final class Closure
{
private function __construct() {}

View File

@ -1,5 +1,5 @@
/* This is a generated file, edit the .stub.php file instead.
* Stub hash: 62da9b1e75331f30a0c63e82c9fd366e26b5724d */
* Stub hash: 7c4df531cdb30ac4206f43f0d40098666466b9a6 */
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_Closure___construct, 0, 0, 0)
ZEND_END_ARG_INFO()
@ -47,7 +47,7 @@ static zend_class_entry *register_class_Closure(void)
INIT_CLASS_ENTRY(ce, "Closure", class_Closure_methods);
class_entry = zend_register_internal_class_ex(&ce, NULL);
class_entry->ce_flags |= ZEND_ACC_FINAL;
class_entry->ce_flags |= ZEND_ACC_FINAL|ZEND_ACC_NO_DYNAMIC_PROPERTIES;
return class_entry;
}