|
From: Peep P. <so...@us...> - 2004-07-24 17:57:20
|
Update of /cvsroot/agd/server/src In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv32102 Modified Files: interpret.c Log Message: Aesthetic stack traces; handling runtime errors which happen during loading an object (in create()); F_SLICE and F_SLICE_LVALUE work on arrays; removed some debugging code Index: interpret.c =================================================================== RCS file: /cvsroot/agd/server/src/interpret.c,v retrieving revision 1.27 retrieving revision 1.28 diff -u -d -r1.27 -r1.28 --- interpret.c 23 Jul 2004 17:17:01 -0000 1.27 +++ interpret.c 24 Jul 2004 17:57:11 -0000 1.28 @@ -41,13 +41,13 @@ void call_function(object_t *ob, int index); extern char *opc_name(int i); +extern int doing_load; + unsigned get_line_number(control_stack_t *p) { return 42; } char *get_fun_name(int num, control_stack_t *csp) { - if(num < 0) - return "*DFUN"; if(csp->this_ob) { if(csp->this_ob->prog) return csp->this_ob->prog->fun_table[num]; @@ -57,54 +57,80 @@ return this_ob->prog->fun_table[num]; } +char *make_errmsg(control_stack_t *p, char *s) +{ + char *buf, *bufp; + buf = xmalloc(1024); + memset(buf, 0, 1024); + + bufp = buf; + sprintf(bufp, "%s: ", p->this_ob ? p->this_ob->name : "unknown"); + bufp += strlen(bufp); + + if(s) { + sprintf(bufp, "%s in ", s); + bufp += strlen(bufp); + } + + if(p->fun >= 0) { + sprintf(bufp, "function %d (%s) on line %d\n", p->fun, + get_fun_name(p->fun, p), get_line_number(p)); + } else { + sprintf(bufp, "dfun %d\n", -p->fun); + } + return buf; +} + void stack_trace(void) { int i, len; len = csp - &control_stack[0]; printf("Stack trace:\n"); - for(i=len;i>0;i--) { - control_stack_t *p = &control_stack[i]; - printf("%d %s: function %d (%s) on line %d\n", - i, p->this_ob?p->this_ob->name:"unknown", - p->fun, get_fun_name(p->fun, p), - get_line_number(p)); + for(i=1;i<=len;i++) { + char *buf; + buf = make_errmsg(&control_stack[i], NULL); + printf("%d %s", i, buf); + free(buf); } } /* free_str: whether to free the string passed to it before * doing longjmp() or not - for dynamically allocated strings */ -void _runtime(char *s, int free_str) +void runtime(char *s) { char *buf; if(!this_ob) { - printf("%s on line %d (function %s)\n", s, - get_line_number(csp), - get_fun_name(csp->fun, csp)); + buf = make_errmsg(csp, s); + printf("%s", buf); + free(buf); goto out; } - buf = xmalloc(strlen(this_ob->name) + strlen(s) + 64); - sprintf(buf, "%s: %s on line %d function %d (%s)\n", this_ob->name, s, +/* sprintf(buf, "%s: %s on line %d function %d (%s)\n", this_ob->name, s, get_line_number(csp-1), - csp->fun, get_fun_name(csp->fun, csp)); + csp->fun, get_fun_name(csp->fun, csp));*/ + buf = make_errmsg(csp, s); + if(doing_load) + printf("While loading object "); printf(buf); - if(this_player) { + if(this_player) do_write(buf); - } free(buf); - if(free_str) - free(s); - stack_trace(); out: + if(doing_load) { + doing_load = 0; + return; + } + fp = value_stack; pop_locals(); csp = &control_stack[2]; pop_control_stack(0); - + longjmp(error_context, 1); } @@ -112,7 +138,7 @@ { sp->type = T_OBJECT; sp->u.ob = ob; - ref_ob(ob, sp); + ref_ob(ob, NULL); sp++; CHECK_SP() } @@ -332,6 +358,8 @@ GET_ARGS(); v = sp[-1].u.v; + index_check(v, arg); +#if 0 if(!v->u.s) runtime("taking substring of null string"); if(arg[0] > arg[1]) @@ -339,6 +367,7 @@ if(arg[0] < 0 || arg[1] > strlen(v->u.s)) { runtime("substring out of range"); } +#endif return v; } @@ -701,20 +730,21 @@ } break; case F_SLICE: - tmpvp = take_slice(arg); - tmps = xmalloc(arg[1] - arg[0] + 2); - for(tmpi=arg[0];tmpi<=arg[1];tmpi++) - tmps[tmpi-arg[0]] = tmpvp->u.s[tmpi]; - tmps[arg[1] - arg[0] + 1] = '\0'; + tmpvp = slice_var(take_slice(arg), arg[0], arg[1]); pop_stack(); - push_string(tmps, ST_MALLOC); + push_var(tmpvp); break; case F_SLICE_LVALUE: - tmpvp = take_slice(arg); - tmps = tmpvp->u.s + arg[1]; /* begin */ - tmpi = tmpvp->string_type; - pop_stack(); - push_string(tmps, tmpi); + tmpvp = slice_var(take_slice(arg), arg[0], arg[1]); + if(tmpvp->type & T_ARRAY) { + pop_stack(); + push_var(tmpvp); + } else { + tmps = tmpvp->u.s + arg[1]; /* begin */ + tmpi = tmpvp->string_type; + pop_stack(); + push_string(tmps, tmpi); + } push_lvalue(sp-1); break; case F_JUMP_IF_FALSE: @@ -848,8 +878,9 @@ /* Abnormal termination - need to pop unnecessary stuff off the stack. */ if(this_ob->flags & O_DESTRUCTED) { - while(csp->this_ob == this_ob) + while(csp->this_ob == this_ob) { pop_control_stack(1); + } pop_locals(); return; } @@ -863,9 +894,8 @@ int i; function_t *f = ob->prog->functions[index]; if(!f) { - char *buf = xmalloc(512); - sprintf(buf, "function %d doesn't exist in object \"%s\"", index, ob->name); - runtime_dyn(buf); + /* Shouldn't happen. */ + return; } csp->fun = index; @@ -932,19 +962,11 @@ char *p; int i, j; -#ifdef DEBUG - struct timeval tm, tm2; -#endif - index = get_fun_index(ob, fun); if(index == -1) return NULL; #ifdef DEBUG - gettimeofday(&tm, NULL); -#endif - -#ifdef DEBUG if(conf.debuglevel) printf("[lpc] apply \"%s\" on \"%s\"\n", fun, ob->name); #endif @@ -965,7 +987,7 @@ case 's': /* We duplicate the string, so we can be sure it's not static and can free() it. */ - push_string(xstrdup(va_arg(va, char*)), ST_MALLOC); + push_string(xstrdup(va_arg(va, char *)), ST_MALLOC); csp->num_arg++; break; default: @@ -998,9 +1020,8 @@ eval_instruction(); #ifdef DEBUG - gettimeofday(&tm2, NULL); if(conf.debuglevel) - printf("[lpc] apply \"%s\" done in %d microseconds\n", fun, tm2.tv_usec - tm.tv_usec); + printf("[lpc] apply \"%s\" done\n", fun); #endif have_error_context = 0; |