feat(parserbox): add shallow recursion guard to parse_program2 for Stage-B

This commit is contained in:
nyash-codex
2025-11-17 18:11:15 +09:00
parent 3c3e734f49
commit 04524f5894

View File

@ -235,6 +235,18 @@ box ParserBox {
// === Top-level program parser ===
parse_program2(src) {
// Shallow recursion guard for StageB / selfhost callers:
// prevent accidental self-recursion in ParserBox.parse_program2.
{
local depth = env.get("HAKO_STAGEB_PARSER_DEPTH")
if depth != null && ("" + depth) != "0" {
print("[stageb/recursion] ParserBox.parse_program2 recursion detected")
// Fail-fast: return an empty Program(JSON) so caller can detect failure.
return "{\"version\":0,\"kind\":\"Program\",\"body\":[]}"
}
env.set("HAKO_STAGEB_PARSER_DEPTH", "1")
}
local trace = me.trace_enabled()
// Inline skip_ws to avoid VM bug: method-with-loop called from within loop
@ -355,6 +367,7 @@ box ParserBox {
if trace == 1 {
print("[parser/trace:program2] done pos=" + ("" + i) + " len=" + ("" + n))
}
env.set("HAKO_STAGEB_PARSER_DEPTH", "0")
return "{\"version\":0,\"kind\":\"Program\",\"body\":" + body + "}"
}
}