Commit 96efde02

96efde026835Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 2 files · ~2 modified

Description

Exception hierarchy: rescue matching + is_a? for user subclasses

  • Add sp_user_exc_parent_fn callback for user exception hierarchy
  • Add sp_exc_cls_matches() for hierarchy-aware rescue arm matching
  • Add sp_exc_new_for_catch() that populates parent_cls_name on catch
  • Add sp_raise_exc() for raise with sp_Exception object
  • Emit sp_user_exc_parent() static function in generated code
  • Register it in sp_re_init via sp_user_exc_parent_fn
  • Use sp_exc_cls_matches() instead of sp_str_eq() in rescue arms
  • Use sp_exc_new_for_catch() at rescue => binding sites
  • emit_regex_section always registers sp_user_exc_parent
  • Fix raise <exception_object> to use sp_raise_exc

Fixes rescue_walks_class_hierarchy, exception_class_new

Co-Authored-By: Claude Sonnet 4.6 [email protected]

Details

Commit
96efde026835
Parent
5020e75cdd1c

lib/sp_runtime.hmodifiedView file

@@ -4289,6 +4289,8 @@
42894289 const char *parent_cls_name; /* builtin ancestor for user subclasses, or NULL */
42904290 const char *msg;
42914291 } sp_Exception;
4292+/* Registered by the generated program to provide user exception hierarchy. */
4293+static const char *(*sp_user_exc_parent_fn)(const char *) = NULL;
42924294 static void sp_exc_gc_scan(void *p) {
42934295 sp_Exception *e = (sp_Exception *)p;
42944296 if (e->msg) sp_mark_string(e->msg);
@@ -4325,6 +4327,16 @@
43254327 if (!e) sp_raise("(nil exception)");
43264328 sp_raise_cls(e->cls_name, e->msg);
43274329 }
4330+/* Create an exception for a `rescue => e` binding: like sp_exc_new but
4331+ also looks up the parent class via the user hierarchy callback. */
4332+static sp_Exception *sp_exc_new_for_catch(const char *cls, const char *msg) {
4333+ sp_Exception *e = sp_exc_new(cls, msg);
4334+ if (sp_user_exc_parent_fn) {
4335+ const char *par = sp_user_exc_parent_fn(cls);
4336+ if (par) e->parent_cls_name = par;
4337+ }
4338+ return e;
4339+}
43284340 /* Exception#is_a?(ClassName): checks class name and known hierarchy. */
43294341 static mrb_int sp_exc_is_a(volatile sp_Exception *ve, const char *cn) {
43304342 sp_Exception *e = (sp_Exception *)ve;
@@ -4364,15 +4376,18 @@
43644376 for (int i = 0; HIER[i][0]; i++)
43654377 if (!strcmp(cls, HIER[i][0])) { parent = HIER[i][1]; break; }
43664378 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;
4379+ /* unknown (user) class: try user hierarchy first */
4380+ if (sp_user_exc_parent_fn) { parent = sp_user_exc_parent_fn(cls); }
4381+ if (!parent) {
4382+ if (!used_parent && e->parent_cls_name) {
4383+ cls = e->parent_cls_name;
4384+ used_parent = 1;
4385+ continue;
4386+ }
4387+ if (!strcmp(cn, "Exception")) return 1;
4388+ if (!strcmp(cn, "Object") || !strcmp(cn, "BasicObject")) return 1;
4389+ break;
43724390 }
4373- if (!strcmp(cn, "Exception")) return 1;
4374- if (!strcmp(cn, "Object") || !strcmp(cn, "BasicObject")) return 1;
4375- break;
43764391 }
43774392 cls = parent;
43784393 }
@@ -4380,6 +4395,54 @@
43804395 return 0;
43814396 }
43824397
4398+/* Check if exception class name `raised` is the same as or a subclass of
4399+ `target`, using both the built-in hierarchy and the user hierarchy callback. */
4400+static int sp_exc_cls_matches(const char *raised, const char *target) {
4401+ if (!raised || !target) return 0;
4402+ static const char *const HIER2[][2] = {
4403+ {"RuntimeError", "StandardError"},
4404+ {"ArgumentError", "StandardError"},
4405+ {"TypeError", "StandardError"},
4406+ {"NameError", "StandardError"},
4407+ {"NoMethodError", "NameError"},
4408+ {"IndexError", "StandardError"},
4409+ {"KeyError", "IndexError"},
4410+ {"RangeError", "StandardError"},
4411+ {"IOError", "StandardError"},
4412+ {"EOFError", "IOError"},
4413+ {"ZeroDivisionError", "StandardError"},
4414+ {"NotImplementedError", "StandardError"},
4415+ {"StopIteration", "IndexError"},
4416+ {"FloatDomainError", "RangeError"},
4417+ {"FrozenError", "RuntimeError"},
4418+ {"EncodingError", "StandardError"},
4419+ {"LoadError", "StandardError"},
4420+ {"RegexpError", "StandardError"},
4421+ {"SyntaxError", "ScriptError"},
4422+ {"ScriptError", "Exception"},
4423+ {"StandardError", "Exception"},
4424+ {NULL, NULL}
4425+ };
4426+ const char *cls = raised;
4427+ for (int depth = 0; depth < 30 && cls; depth++) {
4428+ if (!strcmp(cls, target)) return 1;
4429+ const char *parent = NULL;
4430+ /* user hierarchy first */
4431+ if (sp_user_exc_parent_fn) parent = sp_user_exc_parent_fn(cls);
4432+ if (!parent) {
4433+ for (int i = 0; HIER2[i][0]; i++)
4434+ if (!strcmp(cls, HIER2[i][0])) { parent = HIER2[i][1]; break; }
4435+ }
4436+ if (!parent) {
4437+ if (!strcmp(target, "Exception") || !strcmp(target, "Object") || !strcmp(target, "BasicObject")) return 1;
4438+ break;
4439+ }
4440+ cls = parent;
4441+ }
4442+ if (!strcmp(target, "Object") || !strcmp(target, "BasicObject") || !strcmp(target, "Kernel")) return 1;
4443+ return 0;
4444+}
4445+
43834446 /* Cross-TU bridge for sp_bigint.c (compiled as a separate translation
43844447 unit; can't see static helpers in this header). Defined non-static
43854448 so sp_bigint.c's mrb_raise macro can dispatch into spinel's