Commit 31bcc60f

31bcc60f50e7Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 1 file · ~1 modified

Description

runtime: add sp_Exception type + sp_exc_new (Phase 2.1)

Foundation for Phase 2 of the exception modernization plan: replace Phase 1's side-channel @exc_var_* table with a first-class exception struct.

sp_Exception { const char *cls_name; const char *msg; } carries the two pieces of info Phase 1 already tracked (class name + message) in a single allocation. cls_name points into the rodata sp_class_names[] table (not GC-managed). msg is GC-managed -- the sp_exc_gc_scan hook marks it.

API:

  • sp_exc_new(cls_name, msg) -- allocate
  • sp_exc_class_name(e) / sp_exc_message(e) -- field accessors
  • sp_raise_exc(e) -- equivalent to sp_raise_cls(e->cls_name, e->msg)

No codegen changes yet -- emitted programs don't reference these helpers. The existing sp_raise_cls(cls, msg) setjmp pipeline stays unchanged; sp_Exception is additive.

Default tests 450/0/0, promote 449/0/0, optcarrot 59662 unchanged.

Co-Authored-By: Claude Opus 4.7 [email protected]

Details

Commit
31bcc60f50e7
Parent
555a7f47b0bd

lib/sp_runtime.hmodifiedView file

@@ -2177,6 +2177,35 @@
21772177 static void sp_raise(const char *msg) { sp_raise_cls("RuntimeError", msg); }
21782178 static void sp_mark_in_flight_exceptions(void) { for (int i = 0; i < sp_exc_top; i++) sp_mark_string(sp_exc_msg[i]); }
21792179
2180+/* sp_Exception: first-class exception object. cls_name is a pointer
2181+ to the per-class const string literal emitted by codegen
2182+ (sp_class_names[] entry; not GC-managed). msg is GC-managed
2183+ (sp_str_alloc'd). Phase 2 of the exception modernization plan --
2184+ replaces the side-channel `@exc_var_*` table that Phase 1
2185+ relied on. */
2186+typedef struct sp_Exception_s {
2187+ const char *cls_name;
2188+ const char *msg;
2189+} sp_Exception;
2190+static void sp_exc_gc_scan(void *p) {
2191+ sp_Exception *e = (sp_Exception *)p;
2192+ if (e->msg) sp_mark_string(e->msg);
2193+ /* cls_name points into the .rodata sp_class_names[] table -- not
2194+ a GC-managed string, no mark needed. */
2195+}
2196+static sp_Exception *sp_exc_new(const char *cls_name, const char *msg) {
2197+ sp_Exception *e = (sp_Exception *)sp_gc_alloc(sizeof(sp_Exception), NULL, sp_exc_gc_scan);
2198+ e->cls_name = cls_name ? cls_name : "RuntimeError";
2199+ e->msg = msg ? msg : sp_str_empty;
2200+ return e;
2201+}
2202+static const char *sp_exc_class_name(sp_Exception *e) { return e ? e->cls_name : "RuntimeError"; }
2203+static const char *sp_exc_message(sp_Exception *e) { return e ? e->msg : sp_str_empty; }
2204+static void sp_raise_exc(sp_Exception *e) {
2205+ if (!e) sp_raise("(nil exception)");
2206+ sp_raise_cls(e->cls_name, e->msg);
2207+}
2208+
21802209 /* Cross-TU bridge for sp_bigint.c (compiled as a separate translation
21812210 unit; can't see static helpers in this header). Defined non-static
21822211 so sp_bigint.c's mrb_raise macro can dispatch into spinel's