Skip to content
Snippets Groups Projects
Commit e86c8c3a authored by Martin Stjernholm's avatar Martin Stjernholm
Browse files

Made the variables in the builtin error objects more accessible by dropping

the "__" prefix. This so that a de-facto standard can be achieved for user
error objects simply by having these variables, without the need for
accessor functions. Also gave the variables more descriptive names since
they're now more part of an exposed API.

I blatantly ignore compatibility issues due to this - error objects are
still pretty much unknown.

Rev: src/error.c:1.119
Rev: src/errors.h:1.24
parent 30a1f4d0
Branches
Tags
No related merge requests found
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
|| This file is part of Pike. For copyright information see COPYRIGHT. || This file is part of Pike. For copyright information see COPYRIGHT.
|| Pike is distributed under GPL, LGPL and MPL. See the file COPYING || Pike is distributed under GPL, LGPL and MPL. See the file COPYING
|| for more information. || for more information.
|| $Id: error.c,v 1.118 2003/11/14 00:07:35 mast Exp $ || $Id: error.c,v 1.119 2003/11/14 04:10:32 mast Exp $
*/ */
#define NO_PIKE_SHORTHAND #define NO_PIKE_SHORTHAND
...@@ -23,7 +23,7 @@ ...@@ -23,7 +23,7 @@
#include "threads.h" #include "threads.h"
#include "gc.h" #include "gc.h"
RCSID("$Id: error.c,v 1.118 2003/11/14 00:07:35 mast Exp $"); RCSID("$Id: error.c,v 1.119 2003/11/14 04:10:32 mast Exp $");
#undef ATTRIBUTE #undef ATTRIBUTE
#define ATTRIBUTE(X) #define ATTRIBUTE(X)
...@@ -476,12 +476,12 @@ static void f_error_cast(INT32 args) ...@@ -476,12 +476,12 @@ static void f_error_cast(INT32 args)
if(!strncmp(s,"array",5)) if(!strncmp(s,"array",5))
{ {
pop_n_elems(args); pop_n_elems(args);
if(GENERIC_ERROR_THIS->desc) if(GENERIC_ERROR_THIS->error_message)
ref_push_string(GENERIC_ERROR_THIS->desc); ref_push_string(GENERIC_ERROR_THIS->error_message);
else else
push_int(0); push_int(0);
if(GENERIC_ERROR_THIS->backtrace) if(GENERIC_ERROR_THIS->error_backtrace)
ref_push_array(GENERIC_ERROR_THIS->backtrace); ref_push_array(GENERIC_ERROR_THIS->error_backtrace);
else else
push_int(0); push_int(0);
f_aggregate(2); f_aggregate(2);
...@@ -518,15 +518,15 @@ static void f_error_index(INT32 args) ...@@ -518,15 +518,15 @@ static void f_error_index(INT32 args)
{ {
case 0: case 0:
pop_n_elems(args); pop_n_elems(args);
if(GENERIC_ERROR_THIS->desc) if(GENERIC_ERROR_THIS->error_message)
ref_push_string(GENERIC_ERROR_THIS->desc); ref_push_string(GENERIC_ERROR_THIS->error_message);
else else
push_int(0); push_int(0);
break; break;
case 1: case 1:
pop_n_elems(args); pop_n_elems(args);
if(GENERIC_ERROR_THIS->backtrace) if(GENERIC_ERROR_THIS->error_backtrace)
ref_push_array(GENERIC_ERROR_THIS->backtrace); ref_push_array(GENERIC_ERROR_THIS->error_backtrace);
else else
push_int(0); push_int(0);
break; break;
...@@ -539,10 +539,7 @@ static void f_error_index(INT32 args) ...@@ -539,10 +539,7 @@ static void f_error_index(INT32 args)
/*! @decl string describe() /*! @decl string describe()
*! *!
*! Make a readable error-message. *! Return a readable error report that includes the backtrace.
*!
*! @note
*! Uses @[describe_backtrace()] to generate the message.
*/ */
static void f_error_describe(INT32 args) static void f_error_describe(INT32 args)
{ {
...@@ -551,9 +548,22 @@ static void f_error_describe(INT32 args) ...@@ -551,9 +548,22 @@ static void f_error_describe(INT32 args)
APPLY_MASTER("describe_backtrace",1); APPLY_MASTER("describe_backtrace",1);
} }
/*! @decl string message()
*!
*! Return a readable message describing the error.
*/
static void f_error_message(INT32 args)
{
pop_n_elems(args);
if (GENERIC_ERROR_THIS->error_message)
ref_push_string (GENERIC_ERROR_THIS->error_message);
else
push_int (0);
}
/*! @decl array backtrace() /*! @decl array backtrace()
*! *!
*! Get the backtrace from where the error occurred. *! Return the backtrace where the error occurred.
*! *!
*! @seealso *! @seealso
*! @[predef::backtrace()] *! @[predef::backtrace()]
...@@ -561,8 +571,8 @@ static void f_error_describe(INT32 args) ...@@ -561,8 +571,8 @@ static void f_error_describe(INT32 args)
static void f_error_backtrace(INT32 args) static void f_error_backtrace(INT32 args)
{ {
pop_n_elems(args); pop_n_elems(args);
if(GENERIC_ERROR_THIS->backtrace) if(GENERIC_ERROR_THIS->error_backtrace)
ref_push_array(GENERIC_ERROR_THIS->backtrace); ref_push_array(GENERIC_ERROR_THIS->error_backtrace);
else else
push_int(0); push_int(0);
} }
...@@ -585,8 +595,8 @@ static void f_error__sprintf(INT32 args) ...@@ -585,8 +595,8 @@ static void f_error__sprintf(INT32 args)
} }
push_svalue(&PROG_FROM_INT(p, i)->constants[id->func.offset].sval); push_svalue(&PROG_FROM_INT(p, i)->constants[id->func.offset].sval);
push_constant_text("(%O)"); push_constant_text("(%O)");
if(GENERIC_ERROR_THIS->desc) if(GENERIC_ERROR_THIS->error_message)
ref_push_string(GENERIC_ERROR_THIS->desc); ref_push_string(GENERIC_ERROR_THIS->error_message);
else else
push_int(0); push_int(0);
f_sprintf(2); f_sprintf(2);
...@@ -599,13 +609,13 @@ static void f_error_create(INT32 args) ...@@ -599,13 +609,13 @@ static void f_error_create(INT32 args)
{ {
struct pike_string *msg; struct pike_string *msg;
get_all_args("create", args, "%W", &msg); get_all_args("create", args, "%W", &msg);
do_free_string(GENERIC_ERROR_THIS->desc); do_free_string(GENERIC_ERROR_THIS->error_message);
copy_shared_string(GENERIC_ERROR_THIS->desc, msg); copy_shared_string(GENERIC_ERROR_THIS->error_message, msg);
f_backtrace(0); f_backtrace(0);
push_int (0); push_int (0);
push_int (Pike_sp[-2].u.array->size-2); push_int (Pike_sp[-2].u.array->size-2);
o_range (); o_range ();
assign_to_short_svalue ((union anything *)&GENERIC_ERROR_THIS->backtrace, assign_to_short_svalue ((union anything *)&GENERIC_ERROR_THIS->error_backtrace,
PIKE_T_ARRAY, Pike_sp-1); PIKE_T_ARRAY, Pike_sp-1);
pop_n_elems(args+1); pop_n_elems(args+1);
} }
...@@ -644,7 +654,7 @@ static void f_error_create(INT32 args) ...@@ -644,7 +654,7 @@ static void f_error_create(INT32 args)
assign_svalue_no_free( & ERROR_STRUCT(STRUCT,o)->X, X); \ assign_svalue_no_free( & ERROR_STRUCT(STRUCT,o)->X, X); \
} else { \ } else { \
ERROR_STRUCT(STRUCT, o)->X.type = PIKE_T_INT; \ ERROR_STRUCT(STRUCT, o)->X.type = PIKE_T_INT; \
ERROR_STRUCT(STRUCT, o)->X.subtype = 0; \ ERROR_STRUCT(STRUCT, o)->X.subtype = NUMBER_UNDEFINED; \
ERROR_STRUCT(STRUCT, o)->X.u.integer = 0; \ ERROR_STRUCT(STRUCT, o)->X.u.integer = 0; \
} \ } \
} while (0) } while (0)
...@@ -680,7 +690,7 @@ DECLSPEC(noreturn) void generic_error_va(struct object *o, ...@@ -680,7 +690,7 @@ DECLSPEC(noreturn) void generic_error_va(struct object *o,
} }
#endif #endif
ERROR_STRUCT(generic,o)->desc=make_shared_string(buf); ERROR_STRUCT(generic,o)->error_message=make_shared_string(buf);
f_backtrace(0); f_backtrace(0);
if(func) if(func)
...@@ -699,7 +709,7 @@ DECLSPEC(noreturn) void generic_error_va(struct object *o, ...@@ -699,7 +709,7 @@ DECLSPEC(noreturn) void generic_error_va(struct object *o,
if(Pike_sp[-1].type!=PIKE_T_ARRAY) if(Pike_sp[-1].type!=PIKE_T_ARRAY)
Pike_fatal("Error failed to generate a backtrace!\n"); Pike_fatal("Error failed to generate a backtrace!\n");
ERROR_STRUCT(generic,o)->backtrace=Pike_sp[-1].u.array; ERROR_STRUCT(generic,o)->error_backtrace=Pike_sp[-1].u.array;
Pike_sp--; Pike_sp--;
dmalloc_touch_svalue(Pike_sp); dmalloc_touch_svalue(Pike_sp);
...@@ -740,38 +750,31 @@ PMOD_EXPORT DECLSPEC(noreturn) void generic_error( ...@@ -740,38 +750,31 @@ PMOD_EXPORT DECLSPEC(noreturn) void generic_error(
PMOD_EXPORT DECLSPEC(noreturn) void index_error( PMOD_EXPORT DECLSPEC(noreturn) void index_error(
const char *func, const char *func,
struct svalue *base_sp, int args, struct svalue *base_sp, int args,
struct svalue *val, struct svalue *value,
struct svalue *ind, struct svalue *index,
const char *desc, ...) ATTRIBUTE((noreturn,format (printf, 6, 7))) const char *desc, ...) ATTRIBUTE((noreturn,format (printf, 6, 7)))
{ {
INIT_ERROR(index); INIT_ERROR(index);
ERROR_COPY_SVALUE(index, val); ERROR_COPY_SVALUE(index, value);
ERROR_COPY_SVALUE(index, ind); ERROR_COPY_SVALUE(index, index);
ERROR_DONE(generic); ERROR_DONE(generic);
} }
PMOD_EXPORT DECLSPEC(noreturn) void bad_arg_error( PMOD_EXPORT DECLSPEC(noreturn) void bad_arg_error(
const char *func, const char *func,
struct svalue *base_sp, int args, struct svalue *base_sp, int args,
int which_arg, int which_argument,
const char *expected_type, const char *expected_type,
struct svalue *got, struct svalue *got_value,
const char *desc, ...) ATTRIBUTE((noreturn,format (printf, 7, 8))) const char *desc, ...) ATTRIBUTE((noreturn,format (printf, 7, 8)))
{ {
INIT_ERROR(bad_arg); INIT_ERROR(bad_arg);
ERROR_COPY(bad_arg, which_arg); ERROR_COPY(bad_arg, which_argument);
if (expected_type) if (expected_type)
ERROR_STRUCT(bad_arg,o)->expected_type=make_shared_string(expected_type); ERROR_STRUCT(bad_arg,o)->expected_type=make_shared_string(expected_type);
else else
ERROR_STRUCT(bad_arg,o)->expected_type = NULL; ERROR_STRUCT(bad_arg,o)->expected_type = NULL;
if(got) ERROR_COPY_SVALUE(bad_arg, got_value);
{
ERROR_COPY_SVALUE(bad_arg, got);
}else{
ERROR_STRUCT(bad_arg,o)->got.type=PIKE_T_INT;
ERROR_STRUCT(bad_arg,o)->got.subtype=NUMBER_UNDEFINED;
ERROR_STRUCT(bad_arg,o)->got.u.integer=0;
}
DWERROR((stderr, "%s():Bad arg %d (expected %s)\n", DWERROR((stderr, "%s():Bad arg %d (expected %s)\n",
func, which_arg, expected_type)); func, which_arg, expected_type));
ERROR_DONE(generic); ERROR_DONE(generic);
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
|| This file is part of Pike. For copyright information see COPYRIGHT. || This file is part of Pike. For copyright information see COPYRIGHT.
|| Pike is distributed under GPL, LGPL and MPL. See the file COPYING || Pike is distributed under GPL, LGPL and MPL. See the file COPYING
|| for more information. || for more information.
|| $Id: errors.h,v 1.23 2003/07/21 23:41:34 mast Exp $ || $Id: errors.h,v 1.24 2003/11/14 04:10:32 mast Exp $
*/ */
#ifdef ERR_DECLARE #ifdef ERR_DECLARE
...@@ -44,7 +44,7 @@ struct PIKE_CONCAT(NAME,_error_struct) { \ ...@@ -44,7 +44,7 @@ struct PIKE_CONCAT(NAME,_error_struct) { \
}while(0); }while(0);
#define ERR_VAR(TYPE,CTYPE,RUNTYPE,NAME2) \ #define ERR_VAR(TYPE,CTYPE,RUNTYPE,NAME2) \
map_variable("__" #NAME2, #CTYPE, 0, \ map_variable(#NAME2, #CTYPE, 0, \
current_offset + (((char *)&(foo.NAME2))-((char *)&foo)), RUNTYPE); current_offset + (((char *)&(foo.NAME2))-((char *)&foo)), RUNTYPE);
#define EMPTY #define EMPTY
...@@ -79,11 +79,12 @@ struct PIKE_CONCAT(NAME,_error_struct) { \ ...@@ -79,11 +79,12 @@ struct PIKE_CONCAT(NAME,_error_struct) { \
#endif #endif
DECLARE_ERROR(generic, EMPTY , DECLARE_ERROR(generic, EMPTY ,
ERR_VAR(struct pike_string *,string,PIKE_T_STRING,desc) ERR_VAR(struct pike_string *,string,PIKE_T_STRING,error_message)
ERR_VAR(struct array *,array,PIKE_T_ARRAY,backtrace) ERR_VAR(struct array *,array,PIKE_T_ARRAY,error_backtrace)
ERR_FUNC("cast",f_error_cast,tFunc(tString,tMixed),0) ERR_FUNC("cast",f_error_cast,tFunc(tString,tMixed),0)
ERR_FUNC("`[]",f_error_index,tFunc(tString,tMixed),0) ERR_FUNC("`[]",f_error_index,tFunc(tString,tMixed),0)
ERR_FUNC("describe",f_error_describe,tFunc(tVoid,tString),0) ERR_FUNC("describe",f_error_describe,tFunc(tVoid,tString),0)
ERR_FUNC("message", f_error_message, tFunc(tVoid,tString), 0)
ERR_FUNC("backtrace",f_error_backtrace,tFunc(tVoid,tArr(tMixed)),0) ERR_FUNC("backtrace",f_error_backtrace,tFunc(tVoid,tArr(tMixed)),0)
ERR_FUNC("_sprintf",f_error__sprintf,tFunc(tNone,tString),0) ERR_FUNC("_sprintf",f_error__sprintf,tFunc(tNone,tString),0)
ERR_FUNC("create",f_error_create,tFunc(tStr,tVoid),ID_STATIC) ERR_FUNC("create",f_error_create,tFunc(tStr,tVoid),ID_STATIC)
...@@ -93,15 +94,15 @@ DECLARE_ERROR(generic, EMPTY , ...@@ -93,15 +94,15 @@ DECLARE_ERROR(generic, EMPTY ,
DECLARE_ERROR(index, DECLARE_ERROR(index,
ERR_INHERIT(generic), ERR_INHERIT(generic),
ERR_VAR(struct svalue, mixed, PIKE_T_MIXED, val) ERR_VAR(struct svalue, mixed, PIKE_T_MIXED, value)
ERR_VAR(struct svalue, mixed, PIKE_T_MIXED, ind) ERR_VAR(struct svalue, mixed, PIKE_T_MIXED, index)
) )
DECLARE_ERROR(bad_arg, DECLARE_ERROR(bad_arg,
ERR_INHERIT(generic), ERR_INHERIT(generic),
ERR_VAR(INT_TYPE, int, PIKE_T_INT, which_arg) ERR_VAR(INT_TYPE, int, PIKE_T_INT, which_argument)
ERR_VAR(struct pike_string *,string,PIKE_T_STRING,expected_type) ERR_VAR(struct pike_string *,string,PIKE_T_STRING,expected_type)
ERR_VAR(struct svalue, mixed, PIKE_T_MIXED, got) ERR_VAR(struct svalue, mixed, PIKE_T_MIXED, got_value)
) )
DECLARE_ERROR(math, DECLARE_ERROR(math,
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment