Skip to content

Commit

Permalink
- The compiler can't detect all abstract function calls so we need to…
Browse files Browse the repository at this point in the history
… check.

# In this case throwing a dedicated exception is better than the error we
# show when the compiler can detect the abstract call because its run-time.
  • Loading branch information
helly25 committed Aug 30, 2003
1 parent ad31a02 commit f7f49e4
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 7 deletions.
15 changes: 12 additions & 3 deletions Zend/zend_default_classes.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "zend_builtin_functions.h"

zend_class_entry *default_exception_ptr;
zend_class_entry *abstract_exception_ptr;

static zend_object_value zend_default_exception_new(zend_class_entry *class_type TSRMLS_DC)
{
Expand Down Expand Up @@ -138,24 +139,32 @@ static zend_function_entry default_exception_functions[] = {

static void zend_register_default_exception(TSRMLS_D)
{
zend_class_entry default_exception;
zend_class_entry ce;

INIT_CLASS_ENTRY(default_exception, "exception", default_exception_functions);
default_exception_ptr = zend_register_internal_class(&default_exception TSRMLS_CC);
INIT_CLASS_ENTRY(ce, "exception", default_exception_functions);
default_exception_ptr = zend_register_internal_class(&ce TSRMLS_CC);
default_exception_ptr->create_object = zend_default_exception_new;

zend_declare_property_string(default_exception_ptr, "message", sizeof("message")-1, "Unknown exception", ZEND_ACC_PROTECTED);
zend_declare_property_long(default_exception_ptr, "code", sizeof("code")-1, 0, ZEND_ACC_PROTECTED);
zend_declare_property_null(default_exception_ptr, "file", sizeof("file")-1, ZEND_ACC_PROTECTED);
zend_declare_property_null(default_exception_ptr, "line", sizeof("line")-1, ZEND_ACC_PROTECTED);
zend_declare_property_null(default_exception_ptr, "trace", sizeof("trace")-1, ZEND_ACC_PROTECTED);

INIT_CLASS_ENTRY(ce, "abstract_exception", NULL);
abstract_exception_ptr = zend_register_internal_class_ex(&ce, default_exception_ptr, NULL TSRMLS_CC);
}

ZEND_API zend_class_entry *zend_exception_get_default(void)
{
return default_exception_ptr;
}

ZEND_API zend_class_entry *zend_exception_get_abstract(void)
{
return abstract_exception_ptr;
}

ZEND_API void zend_throw_exception_ex(zend_class_entry *exception_ce, long code TSRMLS_DC, char *format, ...)
{
zval *ex;
Expand Down
1 change: 1 addition & 0 deletions Zend/zend_default_classes.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
BEGIN_EXTERN_C()

ZEND_API zend_class_entry *zend_exception_get_default(void);
ZEND_API zend_class_entry *zend_exception_get_abstract(void);
ZEND_API void zend_register_default_classes(TSRMLS_D);

/* exception_ce NULL or zend_exception_get_default() or a derived class
Expand Down
15 changes: 12 additions & 3 deletions Zend/zend_exceptions.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "zend_builtin_functions.h"

zend_class_entry *default_exception_ptr;
zend_class_entry *abstract_exception_ptr;

static zend_object_value zend_default_exception_new(zend_class_entry *class_type TSRMLS_DC)
{
Expand Down Expand Up @@ -138,24 +139,32 @@ static zend_function_entry default_exception_functions[] = {

static void zend_register_default_exception(TSRMLS_D)
{
zend_class_entry default_exception;
zend_class_entry ce;

INIT_CLASS_ENTRY(default_exception, "exception", default_exception_functions);
default_exception_ptr = zend_register_internal_class(&default_exception TSRMLS_CC);
INIT_CLASS_ENTRY(ce, "exception", default_exception_functions);
default_exception_ptr = zend_register_internal_class(&ce TSRMLS_CC);
default_exception_ptr->create_object = zend_default_exception_new;

zend_declare_property_string(default_exception_ptr, "message", sizeof("message")-1, "Unknown exception", ZEND_ACC_PROTECTED);
zend_declare_property_long(default_exception_ptr, "code", sizeof("code")-1, 0, ZEND_ACC_PROTECTED);
zend_declare_property_null(default_exception_ptr, "file", sizeof("file")-1, ZEND_ACC_PROTECTED);
zend_declare_property_null(default_exception_ptr, "line", sizeof("line")-1, ZEND_ACC_PROTECTED);
zend_declare_property_null(default_exception_ptr, "trace", sizeof("trace")-1, ZEND_ACC_PROTECTED);

INIT_CLASS_ENTRY(ce, "abstract_exception", NULL);
abstract_exception_ptr = zend_register_internal_class_ex(&ce, default_exception_ptr, NULL TSRMLS_CC);
}

ZEND_API zend_class_entry *zend_exception_get_default(void)
{
return default_exception_ptr;
}

ZEND_API zend_class_entry *zend_exception_get_abstract(void)
{
return abstract_exception_ptr;
}

ZEND_API void zend_throw_exception_ex(zend_class_entry *exception_ce, long code TSRMLS_DC, char *format, ...)
{
zval *ex;
Expand Down
1 change: 1 addition & 0 deletions Zend/zend_exceptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
BEGIN_EXTERN_C()

ZEND_API zend_class_entry *zend_exception_get_default(void);
ZEND_API zend_class_entry *zend_exception_get_abstract(void);
ZEND_API void zend_register_default_classes(TSRMLS_D);

/* exception_ce NULL or zend_exception_get_default() or a derived class
Expand Down
9 changes: 8 additions & 1 deletion Zend/zend_execute.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include "zend_extensions.h"
#include "zend_fast_cache.h"
#include "zend_ini.h"
#include "zend_default_classes.h"

#define get_zval_ptr(node, Ts, should_free, type) _get_zval_ptr(node, Ts, should_free TSRMLS_CC)
#define get_zval_ptr_ptr(node, Ts, type) _get_zval_ptr_ptr(node, Ts TSRMLS_CC)
Expand Down Expand Up @@ -2498,7 +2499,12 @@ int zend_do_fcall_common_helper(ZEND_OPCODE_HANDLER_ARGS)
zval *current_this;
int return_value_used = RETURN_VALUE_USED(EX(opline));
zend_bool should_change_scope;


if (EX(function_state).function->common.fn_flags & ZEND_ACC_ABSTRACT) {
zend_throw_exception_ex(zend_exception_get_abstract(), 0 TSRMLS_CC, "Abstract method %s::%s called", EX(function_state).function->common.scope->name, EX(function_state).function->common.function_name);
goto fcall_exception;
}

zend_ptr_stack_n_push(&EG(argument_stack), 2, (void *) EX(opline)->extended_value, NULL);

EX_T(EX(opline)->result.u.var).var.ptr_ptr = &EX_T(EX(opline)->result.u.var).var.ptr;
Expand Down Expand Up @@ -2629,6 +2635,7 @@ int zend_do_fcall_common_helper(ZEND_OPCODE_HANDLER_ARGS)
EG(function_state_ptr) = &EX(function_state);
zend_ptr_stack_clear_multiple(TSRMLS_C);

fcall_exception:
if (EG(exception)) {
if (return_value_used && EX_T(EX(opline)->result.u.var).var.ptr) {
zval_ptr_dtor(&EX_T(EX(opline)->result.u.var).var.ptr);
Expand Down

0 comments on commit f7f49e4

Please sign in to comment.