Skip to content

Commit 6ec49f4

Browse files
committed
Register Exception/Error via stubs
1 parent bcd8514 commit 6ec49f4

File tree

1 file changed

+16
-36
lines changed

1 file changed

+16
-36
lines changed

Zend/zend_exceptions.c

Lines changed: 16 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,22 @@ static zend_object_handlers default_exception_handlers;
5353
/* {{{ zend_implement_throwable */
5454
static int zend_implement_throwable(zend_class_entry *interface, zend_class_entry *class_type)
5555
{
56-
if (instanceof_function(class_type, zend_ce_exception) || instanceof_function(class_type, zend_ce_error)) {
56+
/* During the registration of Exception/Error themselves, this may be called before
57+
* zend_ce_exception and zend_ce_error have been initialized, so handle these explicitly. */
58+
if (zend_ce_exception && instanceof_function(class_type, zend_ce_exception)) {
5759
return SUCCESS;
5860
}
59-
zend_error_noreturn(E_ERROR, "Class %s cannot implement interface %s, extend %s or %s instead",
61+
if (zend_ce_error && instanceof_function(class_type, zend_ce_error)) {
62+
return SUCCESS;
63+
}
64+
if (zend_string_equals_literal(class_type->name, "Exception")
65+
|| zend_string_equals_literal(class_type->name, "Error")) {
66+
return SUCCESS;
67+
}
68+
zend_error_noreturn(E_ERROR,
69+
"Class %s cannot implement interface %s, extend Exception or Error instead",
6070
ZSTR_VAL(class_type->name),
61-
ZSTR_VAL(interface->name),
62-
ZSTR_VAL(zend_ce_exception->name),
63-
ZSTR_VAL(zend_ce_error->name));
71+
ZSTR_VAL(interface->name));
6472
return FAILURE;
6573
}
6674
/* }}} */
@@ -740,52 +748,24 @@ ZEND_METHOD(Exception, __toString)
740748
}
741749
/* }}} */
742750

743-
static void declare_exception_properties(zend_class_entry *ce)
744-
{
745-
zval val;
746-
747-
zend_declare_property_string(ce, "message", sizeof("message")-1, "", ZEND_ACC_PROTECTED);
748-
zend_declare_property_string(ce, "string", sizeof("string")-1, "", ZEND_ACC_PRIVATE);
749-
zend_declare_property_long(ce, "code", sizeof("code")-1, 0, ZEND_ACC_PROTECTED);
750-
zend_declare_property_null(ce, "file", sizeof("file")-1, ZEND_ACC_PROTECTED);
751-
zend_declare_property_null(ce, "line", sizeof("line")-1, ZEND_ACC_PROTECTED);
752-
753-
ZVAL_EMPTY_ARRAY(&val);
754-
zend_declare_typed_property(
755-
ce, ZSTR_KNOWN(ZEND_STR_TRACE), &val, ZEND_ACC_PRIVATE, NULL,
756-
(zend_type) ZEND_TYPE_INIT_MASK(MAY_BE_ARRAY));
757-
758-
ZVAL_NULL(&val);
759-
zend_declare_typed_property(
760-
ce, ZSTR_KNOWN(ZEND_STR_PREVIOUS), &val, ZEND_ACC_PRIVATE, NULL,
761-
(zend_type) ZEND_TYPE_INIT_CE(zend_ce_throwable, /* allow_null */ 1, 0));
762-
}
763-
764751
void zend_register_default_exception(void) /* {{{ */
765752
{
766-
zend_class_entry ce;
767-
768753
zend_ce_throwable = register_class_Throwable(zend_ce_stringable);
769754
zend_ce_throwable->interface_gets_implemented = zend_implement_throwable;
770755

771756
memcpy(&default_exception_handlers, &std_object_handlers, sizeof(zend_object_handlers));
772757
default_exception_handlers.clone_obj = NULL;
773758

774-
INIT_CLASS_ENTRY(ce, "Exception", class_Exception_methods);
775-
zend_ce_exception = zend_register_internal_class_ex(&ce, NULL);
759+
zend_ce_exception = register_class_Exception(zend_ce_throwable);
776760
zend_ce_exception->create_object = zend_default_exception_new;
777-
zend_class_implements(zend_ce_exception, 1, zend_ce_throwable);
778-
declare_exception_properties(zend_ce_exception);
779761

780762
zend_ce_error_exception = register_class_ErrorException(zend_ce_exception);
781763
zend_ce_error_exception->create_object = zend_error_exception_new;
764+
/* Declared manually because it uses constant E_ERROR. */
782765
zend_declare_property_long(zend_ce_error_exception, "severity", sizeof("severity")-1, E_ERROR, ZEND_ACC_PROTECTED);
783766

784-
INIT_CLASS_ENTRY(ce, "Error", class_Error_methods);
785-
zend_ce_error = zend_register_internal_class_ex(&ce, NULL);
767+
zend_ce_error = register_class_Error(zend_ce_throwable);
786768
zend_ce_error->create_object = zend_default_exception_new;
787-
zend_class_implements(zend_ce_error, 1, zend_ce_throwable);
788-
declare_exception_properties(zend_ce_error);
789769

790770
zend_ce_compile_error = register_class_CompileError(zend_ce_error);
791771
zend_ce_compile_error->create_object = zend_default_exception_new;

0 commit comments

Comments
 (0)