| 1 |
/* |
| 2 |
Copyright(C) 2001-2002 arton |
| 3 |
|
| 4 |
Permission is granted for use, copying, modification, distribution, |
| 5 |
and distribution of modified versions of this work as long as the |
| 6 |
above copyright notice is included. |
| 7 |
*/ |
| 8 |
|
| 9 |
using System; |
| 10 |
|
| 11 |
namespace arton.NETRuby |
| 12 |
{ |
| 13 |
public class Tag |
| 14 |
{ |
| 15 |
public enum TAG { |
| 16 |
EMPTY = 0, |
| 17 |
RETURN = 1, |
| 18 |
BREAK = 2, |
| 19 |
NEXT = 3, |
| 20 |
RETRY = 4, |
| 21 |
REDO = 5, |
| 22 |
RAISE = 6, |
| 23 |
THROW = 7, |
| 24 |
FATAL = 8, |
| 25 |
MASK = 15, |
| 26 |
// |
| 27 |
PROT_NONE = 129, |
| 28 |
PROT_FUNC = 130, |
| 29 |
PROT_THREAD = 131, |
| 30 |
} |
| 31 |
internal Tag(TAG t, RThread th) |
| 32 |
{ |
| 33 |
retval = null; |
| 34 |
frame = th.frame; |
| 35 |
iter = th.iter.Peek(); |
| 36 |
scope = th.scope; |
| 37 |
tag = t; |
| 38 |
} |
| 39 |
internal void Jump(TAG st, RThread th) |
| 40 |
{ |
| 41 |
th.frame = frame; |
| 42 |
if (th.scopes.Contains(scope)) |
| 43 |
{ |
| 44 |
while (scope != (Scope)th.scopes.Peek()) |
| 45 |
{ |
| 46 |
th.scopes.Pop(); |
| 47 |
} |
| 48 |
} |
| 49 |
if (th.iter.Contains(iter)) |
| 50 |
{ |
| 51 |
while (iter != th.iter.Peek()) |
| 52 |
{ |
| 53 |
th.iter.Pop(); |
| 54 |
} |
| 55 |
} |
| 56 |
throw new eTagJump(st); |
| 57 |
} |
| 58 |
internal void Pop(RThread th) |
| 59 |
{ |
| 60 |
if (th.protTag.Contains(this)) |
| 61 |
{ |
| 62 |
while (this != (Tag)th.protTag.Peek()) |
| 63 |
{ |
| 64 |
th.protTag.Pop(); |
| 65 |
} |
| 66 |
} |
| 67 |
th.CurrentTag.retval = retval; |
| 68 |
} |
| 69 |
internal void SetResult(Tag prv) |
| 70 |
{ |
| 71 |
if (prv != null) |
| 72 |
{ |
| 73 |
prv.retval = retval; |
| 74 |
} |
| 75 |
} |
| 76 |
internal object Result |
| 77 |
{ |
| 78 |
get { return retval; } |
| 79 |
set { retval = value; } |
| 80 |
} |
| 81 |
object retval; |
| 82 |
Frame frame; |
| 83 |
object iter; |
| 84 |
Scope scope; |
| 85 |
internal TAG tag; |
| 86 |
} |
| 87 |
|
| 88 |
public class eArgError : Exception |
| 89 |
{ |
| 90 |
public eArgError(string message) |
| 91 |
: base(message) |
| 92 |
{} |
| 93 |
} |
| 94 |
|
| 95 |
public class eNameError : Exception |
| 96 |
{ |
| 97 |
public eNameError(string message) |
| 98 |
: base(message) |
| 99 |
{} |
| 100 |
} |
| 101 |
|
| 102 |
public class eRangeError : Exception |
| 103 |
{ |
| 104 |
public eRangeError(string message) |
| 105 |
: base(message) |
| 106 |
{} |
| 107 |
} |
| 108 |
|
| 109 |
public class eFloatDomainError : eRangeError |
| 110 |
{ |
| 111 |
public eFloatDomainError(string message) : base(message) { |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
public class eTypeError : Exception |
| 116 |
{ |
| 117 |
public eTypeError(string message) : base(message) { |
| 118 |
} |
| 119 |
public eTypeError(string message, Exception e) : base(message, e) { |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
public class eLocalJumpError : Exception |
| 124 |
{ |
| 125 |
public eLocalJumpError(string message) |
| 126 |
: base(message) |
| 127 |
{} |
| 128 |
} |
| 129 |
|
| 130 |
public class eThreadError : Exception |
| 131 |
{ |
| 132 |
public eThreadError(string message) |
| 133 |
: base(message) |
| 134 |
{} |
| 135 |
} |
| 136 |
|
| 137 |
public class eIndexError : Exception |
| 138 |
{ |
| 139 |
public eIndexError(string message) |
| 140 |
: base(message) |
| 141 |
{} |
| 142 |
} |
| 143 |
|
| 144 |
public class eLoadError : Exception |
| 145 |
{ |
| 146 |
public eLoadError(string message) |
| 147 |
: base(message) |
| 148 |
{} |
| 149 |
} |
| 150 |
|
| 151 |
public class NetRubyException : Exception |
| 152 |
{ |
| 153 |
internal NetRubyException(RException ex) |
| 154 |
{ |
| 155 |
errInfo = ex; |
| 156 |
} |
| 157 |
public RException ErrorInfo |
| 158 |
{ |
| 159 |
get { return errInfo; } |
| 160 |
} |
| 161 |
public new Exception InnerException |
| 162 |
{ |
| 163 |
get { return errInfo.InnerException; } |
| 164 |
} |
| 165 |
public string[] Backtrace |
| 166 |
{ |
| 167 |
get { |
| 168 |
RArray bt = errInfo.Backtrace; |
| 169 |
if (bt == null) return null; |
| 170 |
string[] ret = new string[bt.Count]; |
| 171 |
bt.ArrayList.CopyTo(ret); |
| 172 |
return ret; |
| 173 |
} |
| 174 |
} |
| 175 |
RException errInfo; |
| 176 |
} |
| 177 |
|
| 178 |
public class eTagJump : Exception |
| 179 |
{ |
| 180 |
public eTagJump(Tag.TAG t) |
| 181 |
{ |
| 182 |
state = t; |
| 183 |
} |
| 184 |
internal eTagJump(Tag.TAG t, Exception e, string s) : |
| 185 |
base(s, e) |
| 186 |
{ |
| 187 |
state = t; |
| 188 |
} |
| 189 |
internal eTagJump(RException e) |
| 190 |
{ |
| 191 |
state = Tag.TAG.RAISE; |
| 192 |
rex = e; |
| 193 |
NetRuby rb = e.ruby; |
| 194 |
RThread th = rb.GetCurrentContext(); |
| 195 |
if (th.file != null) |
| 196 |
{ |
| 197 |
RArray at = e.Backtrace; |
| 198 |
if (at == null) |
| 199 |
{ |
| 200 |
at = rb.Backtrace(-1); |
| 201 |
e.Backtrace = at; |
| 202 |
} |
| 203 |
} |
| 204 |
th.errInfo = e; |
| 205 |
if (rb.debug && e.IsKindOf(rb.eSystemExit) == false) |
| 206 |
{ |
| 207 |
Console.Error.WriteLine("Exception `{0}' at {1}:{2} - {3}", |
| 208 |
rb.ClassOf(e).Name, th.file, th.line, |
| 209 |
e.ToString()); |
| 210 |
} |
| 211 |
// trace |
| 212 |
if (th.protTag.Count <= 1) |
| 213 |
{ |
| 214 |
rb.errorPrint(th); |
| 215 |
} |
| 216 |
} |
| 217 |
internal Tag.TAG state; |
| 218 |
internal RException rex; |
| 219 |
} |
| 220 |
|
| 221 |
public class RException : RObject |
| 222 |
{ |
| 223 |
public RException(NetRuby ruby, Exception ex) : |
| 224 |
base(ruby, ruby.eException) |
| 225 |
{ |
| 226 |
innerException = ex; |
| 227 |
IVarSet("mesg", ex.Message); |
| 228 |
Backtrace = ruby.Backtrace(-1); |
| 229 |
} |
| 230 |
internal RException(NetRuby ruby, RMetaObject meta) : |
| 231 |
base(ruby, meta) |
| 232 |
{ |
| 233 |
innerException = null; |
| 234 |
} |
| 235 |
internal RException(NetRuby ruby, string str, RMetaObject meta) : |
| 236 |
base(ruby, meta) |
| 237 |
{ |
| 238 |
innerException = null; |
| 239 |
IVarSet("mesg", str); |
| 240 |
} |
| 241 |
public Exception InnerException |
| 242 |
{ |
| 243 |
get { return innerException; } |
| 244 |
} |
| 245 |
Exception innerException; |
| 246 |
public override string ToString() |
| 247 |
{ |
| 248 |
uint msg = ruby.intern("mesg"); |
| 249 |
object o = null; |
| 250 |
if (IsIVarDefined(msg)) |
| 251 |
{ |
| 252 |
o = IVarGet("mesg"); |
| 253 |
} |
| 254 |
if (o == null) return ruby.ClassOf(this).ClassPath; |
| 255 |
return o.ToString(); |
| 256 |
} |
| 257 |
public override object Inspect() |
| 258 |
{ |
| 259 |
RMetaObject klass = ruby.ClassOf(this); |
| 260 |
string s = ToString(); |
| 261 |
if (s.Length == 0) |
| 262 |
return klass.ClassPath; |
| 263 |
return String.Format("#<{0}: {1}>", klass.ClassPath, s); |
| 264 |
} |
| 265 |
|
| 266 |
static internal object exc_new(RBasic r, params object[] args) |
| 267 |
{ |
| 268 |
RException ex = new RException(r.ruby, (RMetaObject)r); |
| 269 |
r.ruby.CallInit(ex, args); |
| 270 |
return ex; |
| 271 |
} |
| 272 |
static internal object exc_exception(RBasic r, params object[] args) |
| 273 |
{ |
| 274 |
if (args.Length == 0) return r; |
| 275 |
if (args.Length == 1 && args[0] == r) return r; |
| 276 |
NetRuby rb = r.ruby; |
| 277 |
RMetaObject etype = rb.ClassOf(r); |
| 278 |
while (etype is RSingletonClass) |
| 279 |
{ |
| 280 |
etype = etype.super; |
| 281 |
} |
| 282 |
RException ex = new RException(rb, etype); |
| 283 |
r.ruby.CallInit(ex, args); |
| 284 |
return ex; |
| 285 |
} |
| 286 |
static internal object exc_initialize(RBasic r, params object[] argv) |
| 287 |
{ |
| 288 |
string s = String.Empty; |
| 289 |
if (argv.Length == 1) |
| 290 |
{ |
| 291 |
s = argv[0].ToString(); |
| 292 |
} |
| 293 |
r.IVarSet("mesg", s); |
| 294 |
return r; |
| 295 |
} |
| 296 |
public RArray Backtrace |
| 297 |
{ |
| 298 |
get { |
| 299 |
uint bt = ruby.intern("bt"); |
| 300 |
return (IsIVarDefined(bt)) ? (RArray)IVarGet(bt) : null; |
| 301 |
} |
| 302 |
set { IVarSet("bt", CheckBacktrace(ruby, value));} |
| 303 |
} |
| 304 |
static internal object exc_to_s(RBasic r, params object[] args) |
| 305 |
{ |
| 306 |
return r.ToRString(); |
| 307 |
} |
| 308 |
static internal object exc_backtrace(RBasic r, params object[] args) |
| 309 |
{ |
| 310 |
return ((RException)r).Backtrace; |
| 311 |
} |
| 312 |
static private RArray CheckBacktrace(NetRuby rb, object o) |
| 313 |
{ |
| 314 |
RArray a = null; |
| 315 |
if (o != null) |
| 316 |
{ |
| 317 |
if (o is string || o is RString) |
| 318 |
return new RArray(rb, new object[1] { o }); |
| 319 |
if (o is RArray == false) |
| 320 |
{ |
| 321 |
throw new eTypeError("backtrace must be Array of String"); |
| 322 |
} |
| 323 |
a = (RArray)o; |
| 324 |
foreach (object x in a) |
| 325 |
{ |
| 326 |
if (x is string == false && x is RString == false) |
| 327 |
throw new eTypeError("backtrace must be Array of String"); |
| 328 |
} |
| 329 |
} |
| 330 |
return a; |
| 331 |
} |
| 332 |
static internal object exc_set_backtrace(RBasic r, params object[] args) |
| 333 |
{ |
| 334 |
return ((RException)r).Backtrace = CheckBacktrace(r.ruby, args[0]); |
| 335 |
} |
| 336 |
|
| 337 |
internal static void Init(NetRuby rb) |
| 338 |
{ |
| 339 |
RClass ex = rb.DefineClass("Exception", rb.cObject); |
| 340 |
rb.eException = ex; |
| 341 |
ex.DefineSingletonMethod("exception", new RMethod(exc_new), -1); |
| 342 |
ex.DefineMethod("exception", new RMethod(exc_exception), -1); |
| 343 |
ex.DefineMethod("initialize", new RMethod(exc_initialize), -1); |
| 344 |
ex.DefineMethod("message", new RMethod(exc_to_s), 0); |
| 345 |
ex.DefineMethod("backtrace", new RMethod(exc_backtrace), 0); |
| 346 |
ex.DefineMethod("set_backtrace", new RMethod(exc_set_backtrace), 0); |
| 347 |
rb.eSystemExit = rb.DefineClass("SystemExit", ex); |
| 348 |
rb.eFatal = rb.DefineClass("Fatal", ex); |
| 349 |
rb.eInterrupt = rb.DefineClass("Interrupt", ex); |
| 350 |
rb.eSignal = rb.DefineClass("SignalException", ex); |
| 351 |
rb.eStandardError = rb.DefineClass("StandardError", ex); |
| 352 |
rb.eTypeError = rb.DefineClass("TypeError", rb.eStandardError); |
| 353 |
rb.eArgError = rb.DefineClass("ArgumentError", rb.eStandardError); |
| 354 |
rb.eIndexError = rb.DefineClass("IndexError", rb.eStandardError); |
| 355 |
rb.eRangeError = rb.DefineClass("RangeError", rb.eStandardError); |
| 356 |
rb.eScriptError = rb.DefineClass("ScriptError", ex); |
| 357 |
rb.eSyntaxError = rb.DefineClass("SyntaxError", rb.eScriptError); |
| 358 |
rb.eNameError = rb.DefineClass("NameError", rb.eScriptError); |
| 359 |
rb.eLoadError = rb.DefineClass("LoadError", rb.eScriptError); |
| 360 |
rb.eNotImpError = rb.DefineClass("NotImplementedError", rb.eScriptError); |
| 361 |
rb.eRuntimeError = rb.DefineClass("RuntimeError", rb.eStandardError); |
| 362 |
rb.eSecurityError = rb.DefineClass("SecurityError", rb.eStandardError); |
| 363 |
rb.eNoMemError = rb.DefineClass("NoMemoryError", ex); |
| 364 |
} |
| 365 |
} |
| 366 |
} |