forked from apmckinlay/csuneido
-
Notifications
You must be signed in to change notification settings - Fork 0
/
except.cpp
182 lines (165 loc) · 4.3 KB
/
except.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\
* This file is part of Suneido - The Integrated Application Platform
* see: http://www.suneido.com for more information.
*
* Copyright (c) 2000 Suneido Software Corp.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation - version 2.
*
* This program is distributed in the hope that it will be
* useful, but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License in the file COPYING
* for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program; if not, write to the Free
* Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA
\* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#include "except.h"
#include "exceptimp.h"
#include "interp.h"
#include "ostreamstr.h"
#include "trace.h"
#include "suobject.h"
#include "sufunction.h"
#include "errlog.h"
SuObject* copyCallStack();
Except::Except(gcstring x) : SuString(x.trim()),
fp_fn_(tls().proc ? tls().proc->fp->fn : 0), is_block_return(x == "block return")
{
if (x.has_prefix("block") &&
(x == "block:continue" || x == "block:break" || is_block_return))
{
static SuObject* empty = new SuObject;
calls_ = empty;
if (is_block_return)
verify(fp_fn_);
}
else
{
TRACE(EXCEPTIONS, "throwing: " << x);
calls_ = copyCallStack();
}
}
Except::Except(const Except& e, gcstring s)
: SuString(s), fp_fn_(e.fp_fn_), calls_(e.calls_), is_block_return(e.is_block_return)
{ }
Ostream& operator<<(Ostream& os, const Except& e)
{
return os << e.str();
}
static OstreamStr os(200);
Ostream& osexcept()
{
return os;
}
void except_()
{
Except e(os.str());
os.clear();
throw e;
}
// to allow setting breakpoints
void except_err_()
{
Except e(os.str());
os.clear();
throw e;
}
#define TRY(stuff) do try { stuff; } catch (...) { } while (false)
SuObject* copyCallStack()
{
static Value qqq = new SuString("???");
SuObject* calls = new SuObject;
if (! tls().proc || ! tls().proc->fp)
return calls;
for (Frame* f = tls().proc->fp; f > tls().proc->frames; --f)
{
SuObject* call = new SuObject;
SuObject* vars = new SuObject;
if (f->fn)
{
call->put("fn", f->fn);
int i = 0, n = 0;
TRY(i = f->fn->source(f->ip - f->fn->code - 1, &n));
call->put("src_i", i);
call->put("src_n", n);
for (i = 0; i < f->fn->nlocals; ++i)
if (f->local[i])
{
Value value = qqq;
TRY(value = f->local[i]);
TRY(vars->put(symbol(f->fn->locals[i]), value));
}
if (f->self)
vars->put("this", f->self);
}
else if (f->prim)
{
TRY(call->put("fn", f->prim));
}
call->put("locals", vars);
calls->add(call);
}
return calls;
}
// used for "function call overflow" and "value stack overflow"
void except_log_stack_()
{
static bool first = true;
const char* stk = "";
if (first)
{
first = false;
stk = callStackString();
}
errlog(os.str(), stk);
except_();
}
char* callStackString()
{
OstreamStr stk;
gcstring indent = "";
for (Frame* f = tls().proc->fp; f > tls().proc->frames; --f)
{
stk << endl << indent;
if (f->fn)
stk << f->fn;
else if (f->prim)
stk << f->prim;
indent += " ";
}
return stk.str();
}
Value Except::call(Value self, Value member,
short nargs, short nargnames, ushort* argnames, int each)
{
static Value As("As");
static Value Callstack("Callstack");
if (member == As)
{
argseach(nargs, nargnames, argnames, each);
if (nargs != 1)
except("usage: exception.As(string)");
return new Except(*this, ARG(0).gcstr());
}
if (member == Callstack)
{
argseach(nargs, nargnames, argnames, each);
NOARGS("exception.Callstack()");
return calls();
}
else
return SuString::call(self, member, nargs, nargnames, argnames, each);
}
char* Except::callstack() const
{
OstreamStr oss;
for (int i = 0; i < calls_->size(); ++i)
oss << endl << calls_->get(i).getdata("fn");
return oss.str();
}