Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 31 additions & 18 deletions Zend/zend_API.c
Original file line number Diff line number Diff line change
Expand Up @@ -1785,26 +1785,34 @@ ZEND_API void object_properties_load(zend_object *object, const HashTable *prope
}
/* }}} */

static ZEND_COLD zend_never_inline void zend_cannot_instantiate_class(zend_class_entry *class_type) {
if (class_type->ce_flags & ZEND_ACC_INTERFACE) {
zend_throw_error(NULL, "Cannot instantiate interface %s", ZSTR_VAL(class_type->name));
} else if (class_type->ce_flags & ZEND_ACC_TRAIT) {
zend_throw_error(NULL, "Cannot instantiate trait %s", ZSTR_VAL(class_type->name));
} else if (class_type->ce_flags & ZEND_ACC_ENUM) {
zend_throw_error(NULL, "Cannot instantiate enum %s", ZSTR_VAL(class_type->name));
} else {
ZEND_ASSERT(class_type->ce_flags & (ZEND_ACC_IMPLICIT_ABSTRACT_CLASS|ZEND_ACC_EXPLICIT_ABSTRACT_CLASS));
zend_throw_error(NULL, "Cannot instantiate abstract class %s", ZSTR_VAL(class_type->name));
}
}

/* This function requires 'properties' to contain all props declared in the
* class and all props being public. If only a subset is given or the class
* has protected members then you need to merge the properties separately by
* calling zend_merge_properties(). */
static zend_always_inline zend_result _object_and_properties_init(zval *arg, zend_class_entry *class_type, HashTable *properties) /* {{{ */
{
if (UNEXPECTED(class_type->ce_flags & ZEND_ACC_UNINSTANTIABLE)) {
if (class_type->ce_flags & ZEND_ACC_INTERFACE) {
zend_throw_error(NULL, "Cannot instantiate interface %s", ZSTR_VAL(class_type->name));
} else if (class_type->ce_flags & ZEND_ACC_TRAIT) {
zend_throw_error(NULL, "Cannot instantiate trait %s", ZSTR_VAL(class_type->name));
} else if (class_type->ce_flags & ZEND_ACC_ENUM) {
zend_throw_error(NULL, "Cannot instantiate enum %s", ZSTR_VAL(class_type->name));
} else {
ZEND_ASSERT(class_type->ce_flags & (ZEND_ACC_IMPLICIT_ABSTRACT_CLASS|ZEND_ACC_EXPLICIT_ABSTRACT_CLASS));
zend_throw_error(NULL, "Cannot instantiate abstract class %s", ZSTR_VAL(class_type->name));
static zend_always_inline zend_result _object_and_properties_init(zval *arg, zend_class_entry *class_type, HashTable *properties, bool known_instantiable) /* {{{ */
{
if (!known_instantiable) {
if (UNEXPECTED(class_type->ce_flags & ZEND_ACC_UNINSTANTIABLE)) {
zend_cannot_instantiate_class(class_type);
ZVAL_NULL(arg);
Z_OBJ_P(arg) = NULL;
return FAILURE;
}
ZVAL_NULL(arg);
Z_OBJ_P(arg) = NULL;
return FAILURE;
} else {
ZEND_ASSERT((class_type->ce_flags & ZEND_ACC_UNINSTANTIABLE) == 0);
}

if (UNEXPECTED(!(class_type->ce_flags & ZEND_ACC_CONSTANTS_UPDATED))) {
Expand Down Expand Up @@ -1833,19 +1841,24 @@ static zend_always_inline zend_result _object_and_properties_init(zval *arg, zen

ZEND_API zend_result object_and_properties_init(zval *arg, zend_class_entry *class_type, HashTable *properties) /* {{{ */
{
return _object_and_properties_init(arg, class_type, properties);
return _object_and_properties_init(arg, class_type, properties, false);
}
/* }}} */

ZEND_API zend_result object_init_ex(zval *arg, zend_class_entry *class_type) /* {{{ */
{
return _object_and_properties_init(arg, class_type, NULL);
return _object_and_properties_init(arg, class_type, NULL, false);
}
/* }}} */

ZEND_API zend_result object_init_instantiable_class(zval *arg, zend_class_entry *class_type) /* {{{ */
{
return _object_and_properties_init(arg, class_type, NULL, true);
}

ZEND_API zend_result object_init_with_constructor(zval *arg, zend_class_entry *class_type, uint32_t param_count, zval *params, HashTable *named_params) /* {{{ */
{
zend_result status = _object_and_properties_init(arg, class_type, NULL);
zend_result status = _object_and_properties_init(arg, class_type, NULL, false);
if (UNEXPECTED(status == FAILURE)) {
ZVAL_UNDEF(arg);
return FAILURE;
Expand Down
1 change: 1 addition & 0 deletions Zend/zend_API.h
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,7 @@ static zend_always_inline void array_init(zval *arg)

ZEND_API void object_init(zval *arg);
ZEND_API zend_result object_init_ex(zval *arg, zend_class_entry *ce);
ZEND_API zend_result object_init_instantiable_class(zval *arg, zend_class_entry *class_type);
ZEND_API zend_result object_init_with_constructor(zval *arg, zend_class_entry *class_type, uint32_t param_count, zval *params, HashTable *named_params);
ZEND_API zend_result object_and_properties_init(zval *arg, zend_class_entry *ce, HashTable *properties);
ZEND_API void object_properties_init(zend_object *object, zend_class_entry *class_type);
Expand Down
2 changes: 1 addition & 1 deletion Zend/zend_closures.c
Original file line number Diff line number Diff line change
Expand Up @@ -762,7 +762,7 @@ static void zend_create_closure_ex(zval *res, zend_function *func, zend_class_en
zend_closure *closure;
void *ptr;

object_init_ex(res, zend_ce_closure);
object_init_instantiable_class(res, zend_ce_closure);

closure = (zend_closure *)Z_OBJ_P(res);

Expand Down
2 changes: 1 addition & 1 deletion Zend/zend_gc.c
Original file line number Diff line number Diff line change
Expand Up @@ -1915,7 +1915,7 @@ static zend_fiber *gc_create_destructor_fiber(void)

GC_TRACE("starting destructor fiber");

if (UNEXPECTED(object_init_ex(&zobj, zend_ce_fiber) == FAILURE)) {
if (UNEXPECTED(object_init_instantiable_class(&zobj, zend_ce_fiber) == FAILURE)) {
gc_create_destructor_fiber_error();
}

Expand Down
2 changes: 1 addition & 1 deletion Zend/zend_vm_def.h
Original file line number Diff line number Diff line change
Expand Up @@ -4697,7 +4697,7 @@ ZEND_VM_HANDLER(139, ZEND_GENERATOR_CREATE, ANY, ANY)
uint32_t num_args, used_stack, call_info;

SAVE_OPLINE();
object_init_ex(return_value, zend_ce_generator);
object_init_instantiable_class(return_value, zend_ce_generator);

/*
* Normally the execute_data is allocated on the VM stack (because it does
Expand Down
2 changes: 1 addition & 1 deletion Zend/zend_weakrefs.c
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ static zend_always_inline bool zend_weakref_find(zend_object *referent, zval *re
static zend_always_inline void zend_weakref_create(zend_object *referent, zval *return_value) {
zend_weakref *wr;

object_init_ex(return_value, zend_ce_weakref);
object_init_instantiable_class(return_value, zend_ce_weakref);

wr = zend_weakref_fetch(return_value);
wr->referent = referent;
Expand Down
2 changes: 1 addition & 1 deletion ext/curl/curl_file.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ ZEND_METHOD(CURLFile, __construct)
/* {{{ Create the CURLFile object */
PHP_FUNCTION(curl_file_create)
{
object_init_ex( return_value, curl_CURLFile_class );
object_init_instantiable_class( return_value, curl_CURLFile_class );
curlfile_ctor(INTERNAL_FUNCTION_PARAM_PASSTHRU);
}
/* }}} */
Expand Down
2 changes: 1 addition & 1 deletion ext/curl/interface.c
Original file line number Diff line number Diff line change
Expand Up @@ -1024,7 +1024,7 @@ php_curl *init_curl_handle_into_zval(zval *curl)
{
php_curl *ch;

object_init_ex(curl, curl_ce);
object_init_instantiable_class(curl, curl_ce);
ch = Z_CURL_P(curl);

init_curl_handle(ch);
Expand Down
2 changes: 1 addition & 1 deletion ext/curl/multi.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ PHP_FUNCTION(curl_multi_init)
zend_throw_error(NULL, "%s(): Could not initialize a new cURL multi handle", get_active_function_name());
RETURN_THROWS();
}
object_init_ex(return_value, curl_multi_ce);
object_init_instantiable_class(return_value, curl_multi_ce);
mh = Z_CURL_MULTI_P(return_value);
mh->multi = multi;

Expand Down
4 changes: 2 additions & 2 deletions ext/curl/share.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ PHP_FUNCTION(curl_share_init)

ZEND_PARSE_PARAMETERS_NONE();

object_init_ex(return_value, curl_share_ce);
object_init_instantiable_class(return_value, curl_share_ce);
sh = Z_CURL_SHARE_P(return_value);

sh->share = curl_share_init();
Expand Down Expand Up @@ -194,7 +194,7 @@ PHP_FUNCTION(curl_share_init_persistent)
} ZEND_HASH_FOREACH_END();

// We're now decently confident that we'll be returning a CurlSharePersistentHandle object, so we construct it here.
object_init_ex(return_value, curl_share_persistent_ce);
object_init_instantiable_class(return_value, curl_share_persistent_ce);

// Next we initialize a property field for the CurlSharePersistentHandle object with the enabled share options.
array_init(&share_opts_prop);
Expand Down
6 changes: 3 additions & 3 deletions ext/date/php_date.c
Original file line number Diff line number Diff line change
Expand Up @@ -1487,7 +1487,7 @@ static void create_date_period_datetime(timelib_time *datetime, zend_class_entry
if (datetime) {
php_date_obj *date_obj;

zend_result result = object_init_ex(zv, ce);
zend_result result = object_init_instantiable_class(zv, ce);
ZEND_ASSERT(result == SUCCESS && "should succeed as it reuses an existing object's ce");
date_obj = Z_PHPDATE_P(zv);
date_obj->time = timelib_time_clone(datetime);
Expand All @@ -1501,7 +1501,7 @@ static void create_date_period_interval(timelib_rel_time *interval, zval *zv)
if (interval) {
php_interval_obj *interval_obj;

object_init_ex(zv, date_ce_interval);
object_init_instantiable_class(zv, date_ce_interval);
interval_obj = Z_PHPINTERVAL_P(zv);
interval_obj->diff = timelib_rel_time_clone(interval);
interval_obj->initialized = true;
Expand Down Expand Up @@ -5838,7 +5838,7 @@ PHP_METHOD(DatePeriod, __set_state)
Z_PARAM_ARRAY_HT(myht)
ZEND_PARSE_PARAMETERS_END();

object_init_ex(return_value, date_ce_period);
object_init_instantiable_class(return_value, date_ce_period);
period_obj = Z_PHPPERIOD_P(return_value);
if (!php_date_period_initialize_from_hash(period_obj, myht)) {
zend_throw_error(NULL, "Invalid serialization data for DatePeriod object");
Expand Down
4 changes: 2 additions & 2 deletions ext/dba/dba.c
Original file line number Diff line number Diff line change
Expand Up @@ -589,7 +589,7 @@ static void php_dba_open(INTERNAL_FUNCTION_PARAMETERS, bool persistent)
RETURN_FALSE;
}

object_init_ex(return_value, dba_connection_ce);
object_init_instantiable_class(return_value, dba_connection_ce);
dba_connection *connection = Z_DBA_CONNECTION_P(return_value);
connection->info = (dba_info *)le->ptr;
connection->hash = zend_string_dup(resource_key, /* persistent */ true);
Expand Down Expand Up @@ -783,7 +783,7 @@ static void php_dba_open(INTERNAL_FUNCTION_PARAMETERS, bool persistent)
zval *connection_zval;
dba_connection *connection;
if ((connection_zval = zend_hash_find(&DBA_G(connections), resource_key)) == NULL) {
object_init_ex(return_value, dba_connection_ce);
object_init_instantiable_class(return_value, dba_connection_ce);
connection = Z_DBA_CONNECTION_P(return_value);

connection->info = pecalloc(1, sizeof(dba_info), persistent);
Expand Down
4 changes: 2 additions & 2 deletions ext/dom/documenttype.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ zend_result dom_documenttype_entities_read(dom_object *obj, zval *retval)
{
DOM_PROP_NODE(xmlDtdPtr, dtdptr, obj);

object_init_ex(retval, dom_get_dtd_namednodemap_ce(instanceof_function(obj->std.ce, dom_modern_documenttype_class_entry)));
object_init_instantiable_class(retval, dom_get_dtd_namednodemap_ce(instanceof_function(obj->std.ce, dom_modern_documenttype_class_entry)));

xmlHashTable *entityht = (xmlHashTable *) dtdptr->entities;

Expand All @@ -72,7 +72,7 @@ zend_result dom_documenttype_notations_read(dom_object *obj, zval *retval)
{
DOM_PROP_NODE(xmlDtdPtr, dtdptr, obj);

object_init_ex(retval, dom_get_dtd_namednodemap_ce(instanceof_function(obj->std.ce, dom_modern_documenttype_class_entry)));
object_init_instantiable_class(retval, dom_get_dtd_namednodemap_ce(instanceof_function(obj->std.ce, dom_modern_documenttype_class_entry)));

xmlHashTable *notationht = (xmlHashTable *) dtdptr->notations;

Expand Down
10 changes: 5 additions & 5 deletions ext/dom/element.c
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ zend_result dom_element_class_list_read(dom_object *obj, zval *retval)
{
zval *cached_token_list = dom_element_class_list_zval(obj);
if (Z_ISUNDEF_P(cached_token_list)) {
object_init_ex(cached_token_list, dom_token_list_class_entry);
object_init_instantiable_class(cached_token_list, dom_token_list_class_entry);
dom_token_list_object *intern = php_dom_token_list_from_obj(Z_OBJ_P(cached_token_list));
dom_token_list_ctor(intern, obj);
}
Expand Down Expand Up @@ -824,7 +824,7 @@ static void dom_element_get_elements_by_tag_name(INTERNAL_FUNCTION_PARAMETERS, z

DOM_GET_THIS_INTERN(intern);

object_init_ex(return_value, iter_ce);
object_init_instantiable_class(return_value, iter_ce);
namednode = Z_DOMOBJ_P(return_value);
php_dom_create_obj_map(intern, namednode, NULL, name, NULL, &php_dom_obj_map_by_tag_name);
}
Expand Down Expand Up @@ -856,7 +856,7 @@ PHP_METHOD(Dom_Element, getElementsByClassName)

DOM_GET_THIS_INTERN(intern);

object_init_ex(return_value, dom_html_collection_class_entry);
object_init_instantiable_class(return_value, dom_html_collection_class_entry);
namednode = Z_DOMOBJ_P(return_value);

HashTable *token_set;
Expand Down Expand Up @@ -1287,7 +1287,7 @@ static void dom_element_get_elements_by_tag_name_ns(INTERNAL_FUNCTION_PARAMETERS

DOM_GET_THIS_INTERN(intern);

object_init_ex(return_value, iter_ce);
object_init_instantiable_class(return_value, iter_ce);
namednode = Z_DOMOBJ_P(return_value);
php_dom_create_obj_map(intern, namednode, NULL, name, uri, &php_dom_obj_map_by_tag_name);
}
Expand Down Expand Up @@ -2080,7 +2080,7 @@ static void dom_element_get_in_scope_namespace_info(php_dom_libxml_ns_mapper *ns
}

zval zv;
object_init_ex(&zv, dom_namespace_info_class_entry);
object_init_instantiable_class(&zv, dom_namespace_info_class_entry);
zend_object *obj = Z_OBJ(zv);

if (ZSTR_LEN(prefix) != 0) {
Expand Down
6 changes: 3 additions & 3 deletions ext/dom/node.c
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ zend_result dom_node_child_nodes_read(dom_object *obj, zval *retval)
{
DOM_PROP_NODE(xmlNodePtr, nodep, obj);

object_init_ex(retval, dom_get_nodelist_ce(false));
object_init_instantiable_class(retval, dom_get_nodelist_ce(false));
dom_object *intern = Z_DOMOBJ_P(retval);
php_dom_create_obj_map(obj, intern, NULL, NULL, NULL, &php_dom_obj_map_child_nodes);

Expand All @@ -303,7 +303,7 @@ zend_result dom_modern_node_child_nodes_read(dom_object *obj, zval *retval)
{
DOM_PROP_NODE(xmlNodePtr, nodep, obj);

object_init_ex(retval, dom_get_nodelist_ce(true));
object_init_instantiable_class(retval, dom_get_nodelist_ce(true));
dom_object *intern = Z_DOMOBJ_P(retval);
php_dom_create_obj_map(obj, intern, NULL, NULL, NULL, &php_dom_obj_map_child_nodes);

Expand Down Expand Up @@ -437,7 +437,7 @@ zend_result dom_node_attributes_read(dom_object *obj, zval *retval)
DOM_PROP_NODE(xmlNodePtr, nodep, obj);

if (nodep->type == XML_ELEMENT_NODE) {
object_init_ex(retval, dom_get_namednodemap_ce(php_dom_follow_spec_intern(obj)));
object_init_instantiable_class(retval, dom_get_namednodemap_ce(php_dom_follow_spec_intern(obj)));
dom_object *intern = Z_DOMOBJ_P(retval);
php_dom_create_obj_map(obj, intern, NULL, NULL, NULL, &php_dom_obj_map_attributes);
} else {
Expand Down
2 changes: 1 addition & 1 deletion ext/dom/parentnode/css_selectors.c
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ void dom_parent_node_query_selector_all(xmlNodePtr thisp, dom_object *intern, zv
zend_array_destroy(list);
RETURN_THROWS();
} else {
object_init_ex(return_value, dom_modern_nodelist_class_entry);
object_init_instantiable_class(return_value, dom_modern_nodelist_class_entry);
dom_object *ret_obj = Z_DOMOBJ_P(return_value);
dom_nnodemap_object *mapptr = (dom_nnodemap_object *) ret_obj->ptr;
mapptr->array = list;
Expand Down
2 changes: 1 addition & 1 deletion ext/dom/parentnode/tree.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ zend_result dom_parent_node_children_read(dom_object *obj, zval *retval)
{
zval *cached_children = dom_parent_node_children(obj);
if (Z_ISUNDEF_P(cached_children)) {
object_init_ex(cached_children, dom_html_collection_class_entry);
object_init_instantiable_class(cached_children, dom_html_collection_class_entry);
php_dom_create_obj_map(obj, Z_DOMOBJ_P(cached_children), NULL, NULL, NULL, &php_dom_obj_map_child_elements);

/* Handle cycles for potential TMPVARs (could also be CV but we can't differentiate).
Expand Down
2 changes: 1 addition & 1 deletion ext/dom/php_dom.c
Original file line number Diff line number Diff line change
Expand Up @@ -1761,7 +1761,7 @@ dom_object *php_dom_instantiate_object_helper(zval *return_value, zend_class_ent
}

void php_dom_create_implementation(zval *retval, bool modern) {
object_init_ex(retval, dom_get_domimplementation_ce(modern));
object_init_instantiable_class(retval, dom_get_domimplementation_ce(modern));
}

/* {{{ int dom_hierarchy(xmlNodePtr parent, xmlNodePtr child) */
Expand Down
2 changes: 1 addition & 1 deletion ext/dom/xpath.c
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ static void php_xpath_eval(INTERNAL_FUNCTION_PARAMETERS, int type, bool modern)
ZVAL_EMPTY_ARRAY(&retval);
}

object_init_ex(return_value, dom_get_nodelist_ce(modern));
object_init_instantiable_class(return_value, dom_get_nodelist_ce(modern));
nodeobj = Z_DOMOBJ_P(return_value);
dom_nnodemap_object *mapptr = nodeobj->ptr;

Expand Down
6 changes: 3 additions & 3 deletions ext/enchant/enchant.c
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ PHP_FUNCTION(enchant_broker_init)

pbroker = enchant_broker_init();
if (pbroker) {
object_init_ex(return_value, enchant_broker_ce);
object_init_instantiable_class(return_value, enchant_broker_ce);
broker = Z_ENCHANT_BROKER_P(return_value);
broker->pbroker = pbroker;
broker->nb_dict = 0;
Expand Down Expand Up @@ -434,7 +434,7 @@ PHP_FUNCTION(enchant_broker_request_dict)
if (pdict) {
pbroker->nb_dict++;

object_init_ex(return_value, enchant_dict_ce);
object_init_instantiable_class(return_value, enchant_dict_ce);
dict = Z_ENCHANT_DICT_P(return_value);
dict->pdict = pdict;
ZVAL_COPY(&dict->zbroker, broker);
Expand Down Expand Up @@ -468,7 +468,7 @@ PHP_FUNCTION(enchant_broker_request_pwl_dict)
if (pdict) {
pbroker->nb_dict++;

object_init_ex(return_value, enchant_dict_ce);
object_init_instantiable_class(return_value, enchant_dict_ce);
dict = Z_ENCHANT_DICT_P(return_value);
dict->pdict = pdict;
ZVAL_COPY(&dict->zbroker, broker);
Expand Down
4 changes: 2 additions & 2 deletions ext/ftp/php_ftp.c
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ PHP_FUNCTION(ftp_connect)
ftp->use_ssl = false;
#endif

object_init_ex(return_value, php_ftp_ce);
object_init_instantiable_class(return_value, php_ftp_ce);
ftp_object_from_zend_object(Z_OBJ_P(return_value))->ftp = ftp;
}
/* }}} */
Expand Down Expand Up @@ -203,7 +203,7 @@ PHP_FUNCTION(ftp_ssl_connect)
/* enable ssl */
ftp->use_ssl = true;

object_init_ex(return_value, php_ftp_ce);
object_init_instantiable_class(return_value, php_ftp_ce);
ftp_object_from_zend_object(Z_OBJ_P(return_value))->ftp = ftp;
}
/* }}} */
Expand Down
Loading
Loading