Commit 8507ff7c
8507ff7c7e88Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 4 files · ~4 modified
Description
fix(regexp,runtime,codegen): extern error handler hook routes regex compile errors to sp_raise_cls (#781)
Previously compile_error called fprintf + exit(1), making any bad
pattern fatal even when the caller wrapped Regexp.new in rescue.
The blocker was linkage: sp_raise_cls is static inline per
translation unit, so the library .a can't reference the user
program's copy directly.
Three-piece bridge:
- lib/regexp/re_compile.c: a function-pointer hook (sp_re_error_handler) that compile_error calls before falling back to exit. Public setter: sp_re_set_error_handler(fn).
- lib/sp_runtime.h: a static-inline sp_re_default_error_handler that calls sp_raise_cls("RegexpError", msg). extern declaration for sp_re_set_error_handler.
- spinel_codegen.rb: when @needs_regexp is on, emit the sp_re_set_error_handler(sp_re_default_error_handler) call at the top of main, before sp_re_init runs.
After this, runtime regex compilation errors (Regexp.new(var) patterns) raise RegexpError and are catchable by rescue. The literal-pattern hoist (sp_re_init at startup) still runs outside any rescue context, so a bad literal at toplevel prints "unhandled exception: ..." and exits cleanly -- much better than the previous raw fprintf + exit.
The literal-in-rescue hoist mismatch (where a begin/rescue around Regexp.new("*") doesn't catch because the compile was hoisted) is a separate codegen-level design choice -- the optimization shouldn't cross rescue boundaries, but fixing it needs analyze- side scope detection.
Closes #781
Co-Authored-By: Claude Opus 4.7 [email protected]
Details
- Commit
- 8507ff7c7e88
- Parent
- 21965276c9f6
lib/regexp/re_compile.cmodifiedView file
static void compile_alt(re_compiler *c); /* forward *//* Issue #781: kept exit(1) for now -- routing through sp_raise_cls would require a non-static linkage hook (the runtime's sp_raise_cls is `static inline` per translation unit, so the library cannot reference the user program's copy directly). A later commit can introduce an extern raise hook + populate it from the user program. *//* Issue #781: error handler hook so the library can route through the user program's sp_raise_cls (which is `static inline` per translation unit and not directly linkable from a .a). The user program calls sp_re_set_error_handler(fn) at startup; fn should not return (typically wraps sp_raise_cls). If unset, fall back to fprintf + exit. */static void (*sp_re_error_handler)(const char *msg) = NULL;void sp_re_set_error_handler(void (*fn)(const char *msg)) { sp_re_error_handler = fn;}static voidcompile_error(re_compiler *c, const char *msg){ if (c->stripped) free(c->stripped); c->stripped = NULL; fprintf(stderr, "RegexpError: %s: /%.*s/\n", msg, (int)(c->src_end - c->src), c->src); exit(1); char buf[1024]; snprintf(buf, sizeof(buf), "%s: /%.*s/", msg, (int)(c->src_end - c->src), c->src); if (sp_re_error_handler) { sp_re_error_handler(buf); /* shouldn't return; fall through to exit as a safety net */ } fprintf(stderr, "RegexpError: %s\n", buf); exit(1);}static uint32_tlib/regexp/re_internal.hmodifiedView file
Loading diff…
lib/sp_runtime.hmodifiedView file
Loading diff…
spinel_codegen.rbmodifiedView file
Loading diff…