Stop DLOAD eating a line, and a prompt error killing the driver

Two defects that the same mechanism produced, both found by making the
examples in docs/ actually run.

DLOAD scans the file it reads through the runtime's own scanner, so by the
time it returns, the tokens of the line that invoked it are gone and
hadlinenumber and environment->lineno describe the last line of the *file*.
The REPL carried on parsing what it believed was still its own buffer,
concluded the leftovers were program text because hadlinenumber was now true,
and filed the DLOAD command itself under that line number -- silently
replacing the last line of every program loaded from a prompt. It now
abandons the rest of the line, which is the only thing that can sensibly
follow replacing the whole program.

The second is the same class of oversight one layer up. process_line_run()
has always swallowed the error context after interpret() puts the BASIC-level
line on the sink, with a comment saying that is what goal 3 requires. The
direct-mode branch of process_line_repl() used a bare PASS instead, so
`VERIFY` against a file that did not match -- an ordinary user answer, not a
fault -- came back as a stack trace and terminated the driver. An embedding
host would have gone with it.

Both tests fail when their fix is reverted.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-07-31 22:41:19 -04:00
parent cdaefcc941
commit 6f49f6a7f2
4 changed files with 116 additions and 1 deletions

View File

@@ -281,6 +281,50 @@ static akerr_ErrorContext AKERR_NOIGNORE *test_direct_mode(void)
SUCCEED_RETURN(errctx);
}
/**
* @brief A BASIC error typed at the prompt is reported, not raised at the host.
*
* Goal 3: a script's mistakes are the script's problem. process_line_run() has
* always swallowed the context after interpret() put the error line on the sink,
* but the direct-mode branch of process_line_repl() used a bare PASS and let it
* out -- so `VERIFY` against a file that did not match, which is an ordinary
* answer rather than a fault, terminated the driver with a stack trace and took
* an embedding host with it.
*
* VERIFY is used because it is the shortest verb that raises through
* report_and_reraise() with a message a reader would recognise as their own
* mistake. Found while making the examples in docs/ execute.
*/
static akerr_ErrorContext AKERR_NOIGNORE *test_direct_mode_error_stays_inside(void)
{
PREPARE_ERROR(errctx);
remove("t_direct.bas");
TEST_REQUIRE_OK(harness_start("10 PRINT \"HI\"\n"
"DSAVE \"t_direct.bas\"\n"
"20 PRINT \"EXTRA\"\n"
"VERIFY \"t_direct.bas\"\n"
"PRINT \"STILL HERE\"\n"));
PASS(errctx, akbasic_runtime_start(&HARNESS_RUNTIME, AKBASIC_MODE_REPL));
/*
* PASS, not CATCH: the failure being tested is an error escaping this call,
* so letting it reach this test's own handler is the assertion.
*/
PASS(errctx, akbasic_runtime_run(&HARNESS_RUNTIME, 200));
TEST_REQUIRE(strstr(HARNESS_OUTPUT, "does not match") != NULL,
"the mismatch should have been reported to the sink, got \"%s\"",
HARNESS_OUTPUT);
TEST_REQUIRE(strstr(HARNESS_OUTPUT, "STILL HERE") != NULL,
"the prompt should have carried on after the error, got \"%s\"",
HARNESS_OUTPUT);
harness_stop();
remove("t_direct.bas");
SUCCEED_RETURN(errctx);
}
int main(void)
{
PREPARE_ERROR(errctx);
@@ -295,6 +339,7 @@ int main(void)
CATCH(errctx, test_trace_flag());
CATCH(errctx, test_repl_stops_after_an_error());
CATCH(errctx, test_direct_mode());
CATCH(errctx, test_direct_mode_error_stays_inside());
} CLEANUP {
} PROCESS(errctx) {
} HANDLE_DEFAULT(errctx) {