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:
@@ -264,6 +264,52 @@ static void test_dclear(void)
|
||||
remove("t_clear.txt");
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief DLOAD from a prompt loads the whole program and nothing else.
|
||||
*
|
||||
* It used to lose the last line. DLOAD scans the file through the runtime's own
|
||||
* scanner, so by the time it returns, the tokens of the line that ran it are
|
||||
* gone and `hadlinenumber` and `environment->lineno` describe the last line of
|
||||
* the *file*. The REPL then carried on parsing the buffer it thought was still
|
||||
* its own, decided 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.
|
||||
*
|
||||
* Found while making the examples in docs/ execute: a save/load round trip is
|
||||
* the second thing chapter 2 shows a reader.
|
||||
*/
|
||||
static akerr_ErrorContext AKERR_NOIGNORE *test_dload_keeps_the_last_line(void)
|
||||
{
|
||||
PREPARE_ERROR(errctx);
|
||||
|
||||
remove("t_round.bas");
|
||||
TEST_REQUIRE_OK(harness_start("10 PRINT \"FIRST\"\n"
|
||||
"20 PRINT \"SECOND\"\n"
|
||||
"30 PRINT \"THIRD\"\n"
|
||||
"DSAVE \"t_round.bas\"\n"
|
||||
"NEW\n"
|
||||
"DLOAD \"t_round.bas\"\n"
|
||||
"LIST\n"
|
||||
"RUN\n"));
|
||||
PASS(errctx, akbasic_runtime_start(&HARNESS_RUNTIME, AKBASIC_MODE_REPL));
|
||||
PASS(errctx, akbasic_runtime_run(&HARNESS_RUNTIME, 200));
|
||||
|
||||
TEST_REQUIRE(strstr(HARNESS_OUTPUT, "FIRST\nSECOND\nTHIRD\n") != NULL,
|
||||
"a save/load round trip should run all three lines, got \"%s\"",
|
||||
HARNESS_OUTPUT);
|
||||
/*
|
||||
* And the command is not in the program. LIST rather than the run output,
|
||||
* because a clobbered last line reloads the file and re-runs it, which
|
||||
* prints the right text for the wrong reason.
|
||||
*/
|
||||
TEST_REQUIRE(strstr(HARNESS_OUTPUT, "DLOAD") == NULL,
|
||||
"the DLOAD command should not have been filed as program text, got \"%s\"",
|
||||
HARNESS_OUTPUT);
|
||||
harness_stop();
|
||||
remove("t_round.bas");
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
test_channel_round_trip();
|
||||
@@ -276,5 +322,6 @@ int main(void)
|
||||
test_bsave_bload();
|
||||
test_no_drive_verbs();
|
||||
test_dclear();
|
||||
IGNORE(test_dload_keeps_the_last_line());
|
||||
return akbasic_test_failures;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user