Commit 5020e75c
5020e75cdd1cMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 5 files · ~5 modified
Description
Exception subclass support: user classes, namespaced classes, hierarchy
- Add sp_exc_new_sub() for user exception subclasses with parent tracking
- Add sp_exc_is_a() with full Ruby exception hierarchy table
- Add enclosing_class field to ClassInfo for qualified name reconstruction
- Add class_ruby_name() helper building "Module::Class" from nesting
- Infer TY_EXCEPTION for .new on user classes inheriting builtin exceptions
- Emit sp_exc_new_sub() with full qualified name and builtin parent
- Support is_a?/kind_of? on TY_EXCEPTION with sp_exc_is_a()
- Fix integer bit-index in poly [] dispatch (SP_TAG_INT check)
- Suppress super() no-op in exception initializers
Fixes exception_user_subclass, exception_namespaced_default_message
Co-Authored-By: Claude Sonnet 4.6 [email protected]
Details
- Commit
- 5020e75cdd1c
- Parent
- 36534eff044b
lib/sp_runtime.hmodifiedView file
@@ -4286,18 +4286,25 @@
42864286
(sp_str_alloc'd). */42874287
typedef struct sp_Exception_s {42884288
const char *cls_name;4289+
const char *parent_cls_name; /* builtin ancestor for user subclasses, or NULL */42894290
const char *msg;42904291
} sp_Exception;42914292
static void sp_exc_gc_scan(void *p) {42924293
sp_Exception *e = (sp_Exception *)p;42934294
if (e->msg) sp_mark_string(e->msg);4294-
/* cls_name points into the .rodata sp_class_names[] table -- not4295-
a GC-managed string, no mark needed. */4295+
/* cls_name/parent_cls_name point into rodata -- not GC-managed strings */42964296
}42974297
static sp_Exception *sp_exc_new(const char *cls_name, const char *msg) {42984298
sp_Exception *e = (sp_Exception *)sp_gc_alloc(sizeof(sp_Exception), NULL, sp_exc_gc_scan);42994299
e->cls_name = cls_name ? cls_name : "RuntimeError";4300-
e->msg = msg ? msg : sp_str_empty;4300+
e->parent_cls_name = NULL;4301+
e->msg = (msg && msg[0]) ? msg : (cls_name ? cls_name : "RuntimeError");4302+
return e;4303+
}4304+
static sp_Exception *sp_exc_new_sub(const char *cls_name, const char *parent_cls, const char *msg) {4305+
sp_Exception *e = sp_exc_new(cls_name, msg);4306+
e->parent_cls_name = parent_cls;4307+
if (!msg || !msg[0]) e->msg = cls_name ? cls_name : "RuntimeError";43014308
return e;43024309
}43034310
/* Accept `volatile` pointers: LV slots holding sp_Exception * are@@ -4318,13 +4325,67 @@
43184325
if (!e) sp_raise("(nil exception)");43194326
sp_raise_cls(e->cls_name, e->msg);43204327
}4328+
/* Exception#is_a?(ClassName): checks class name and known hierarchy. */4329+
static mrb_int sp_exc_is_a(volatile sp_Exception *ve, const char *cn) {4330+
sp_Exception *e = (sp_Exception *)ve;4331+
if (!e || !cn) return 0;4332+
if (!strcmp(e->cls_name, cn)) return 1;4333+
/* Walk the well-known exception hierarchy */4334+
static const char *const HIER[][2] = {4335+
{"RuntimeError", "StandardError"},4336+
{"ArgumentError", "StandardError"},4337+
{"TypeError", "StandardError"},4338+
{"NameError", "StandardError"},4339+
{"NoMethodError", "NameError"},4340+
{"IndexError", "StandardError"},4341+
{"KeyError", "IndexError"},4342+
{"RangeError", "StandardError"},4343+
{"IOError", "StandardError"},4344+
{"EOFError", "IOError"},4345+
{"ZeroDivisionError", "StandardError"},4346+
{"NotImplementedError", "StandardError"},4347+
{"StopIteration", "IndexError"},4348+
{"FloatDomainError", "RangeError"},4349+
{"FrozenError", "RuntimeError"},4350+
{"EncodingError", "StandardError"},4351+
{"LoadError", "StandardError"},4352+
{"RegexpError", "StandardError"},4353+
{"SyntaxError", "ScriptError"},4354+
{"ScriptError", "Exception"},4355+
{"StandardError", "Exception"},4356+
{NULL, NULL}4357+
};4358+
/* find the exception's class chain and check if cn appears in it */4359+
const char *cls = e->cls_name;4360+
int used_parent = 0;4361+
for (int depth = 0; depth < 20 && cls; depth++) {4362+
if (!strcmp(cls, cn)) return 1;4363+
const char *parent = NULL;4364+
for (int i = 0; HIER[i][0]; i++)4365+
if (!strcmp(cls, HIER[i][0])) { parent = HIER[i][1]; break; }4366+
if (!parent) {4367+
/* unknown (user) class: jump to stored parent class if available */4368+
if (!used_parent && e->parent_cls_name) {4369+
cls = e->parent_cls_name;4370+
used_parent = 1;4371+
continue;4372+
}4373+
if (!strcmp(cn, "Exception")) return 1;4374+
if (!strcmp(cn, "Object") || !strcmp(cn, "BasicObject")) return 1;4375+
break;4376+
}4377+
cls = parent;4378+
}4379+
if (!strcmp(cn, "Object") || !strcmp(cn, "BasicObject") || !strcmp(cn, "Kernel")) return 1;4380+
return 0;4381+
}43214382
43224383
/* Cross-TU bridge for sp_bigint.c (compiled as a separate translation43234384
unit; can't see static helpers in this header). Defined non-static43244385
so sp_bigint.c's mrb_raise macro can dispatch into spinel's43254386
longjmp-based rescue net rather than fprintf+exit. */43264387
void sp_bigint_raise_zerodiv(const char *msg) { sp_raise_cls("ZeroDivisionError", msg); }4327-
static mrb_bool sp_exc_is_a(const char *cls, const char *target) { return strcmp(cls, target) == 0; }4388+
/* sp_exc_is_a: see earlier definition (takes volatile sp_Exception *) */43284389
43294390
#define SP_CATCH_STACK_MAX 6443304391
static jmp_buf sp_catch_stack[SP_CATCH_STACK_MAX];src/analyze.cmodifiedView file
Loading diff…
src/codegen.cmodifiedView file
Loading diff…
src/compiler.cmodifiedView file
Loading diff…
src/compiler.hmodifiedView file
Loading diff…