Stop a scalar created inside a scope costing value-pool slots
A scalar now lives in the variable record (`akbasic_Variable::inlinevalue`)
rather than drawing from the value pool, so a `GOSUB` local, a `FOR` counter and
a `DEF` parameter cost nothing at all.
The pool is a bump allocator with no free, and its comment justified that with
"nothing in BASIC destroys a variable". Scope exit does: it marks the variable
slot unused, `new_variable()` memsets the slot it hands back -- clearing
`values` -- and `variable_init()` therefore took *fresh* slots for a variable
whose old ones were still counted. Every scope that created a local leaked, with
no diagnostic until the pool ran dry on whichever line happened to be unlucky.
Six thousand `GOSUB`s creating one local used to die on the 4091st at `LOC# = 1`
with "Array of 1 elements does not fit in the 0 remaining value slots". They now
run. A `DEF` called eight thousand times used to die between the four and five
thousandth -- the leaking slot was the call scope's parameter, which is a scalar
-- and both forms now run. A game creating one name per tick was dead in half a
minute; the Breakout in examples/ was, after twenty-five seconds.
**A `@` name is the one exclusion, and it is the whole of it.** A structure or a
pointer to one keeps pool storage, because a pointer into a record outlives the
scope that DIMmed it -- docs/16-structures.md says nothing is reclaimed and
`prev_environment()` relies on it. The name suffix is the right test rather than
`structtype`, which the DIM path sets *after* calling `variable_init()`. A local
array therefore still leaks, deliberately, and is now the narrow rule the
tutorial teaches.
`SWAP` needed the other half: it copies whole variable records, so the `values`
pointer that came over named the other variable's inline slot -- which by then
held this variable's own old value -- and SWAP silently did nothing. Caught by
tests/language/housekeeping/verbs.bas, which is the golden corpus earning its
keep.
tests/value_pool.c is the new coverage. It asserts the mechanism as well as the
consequence: a later change that moved arrays inline too would pass every
behavioural case and quietly break the pointer guarantee. The sharpest case
takes the pool's whole 4096 slots in four arrays after two hundred scope
entries, so one leaked slot has nowhere to go.
Chapter 17 Step 3 taught "declare every name at the top" and no longer needs to.
It now teaches what is still true -- a name first seen inside a subroutine dies
at RETURN, so a routine cannot answer its caller through one -- and its
demonstration is the array case, which still fails.
TODO.md section 6 item 30 and section 9 item 1, both struck.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Co-Authored-By: Andrew Kesterson <andrew@aklabs.net>
2026-08-01 23:47:49 -04:00
|
|
|
/**
|
|
|
|
|
* @file value_pool.c
|
|
|
|
|
* @brief Tests that creating a name inside a scope costs the value pool nothing.
|
|
|
|
|
*
|
|
|
|
|
* This is the defect a Breakout written against the interpreter died of after
|
|
|
|
|
* twenty-five seconds, and nothing in either corpus could have found it: the
|
|
|
|
|
* pool holds 4096 values, so it takes *thousands* of scope entries to see, and
|
|
|
|
|
* nothing else here runs that long.
|
|
|
|
|
*
|
|
|
|
|
* The cause was that scope exit returns the variable *slot* and not its storage.
|
|
|
|
|
* akbasic_runtime_prev_environment() marks the variable unused,
|
|
|
|
|
* akbasic_runtime_new_variable() memsets the slot it hands back -- clearing
|
|
|
|
|
* `values` -- and akbasic_variable_init() therefore drew fresh slots for a
|
|
|
|
|
* variable whose old ones were still counted. Every GOSUB that created a local
|
|
|
|
|
* leaked, with no diagnostic until the pool ran dry on whichever line happened
|
|
|
|
|
* to be unlucky.
|
|
|
|
|
*
|
|
|
|
|
* A scalar now lives in the variable itself, so the pool is not involved at all.
|
|
|
|
|
* Two of the counts below are past the old 4096 ceiling on purpose -- under it,
|
|
|
|
|
* they would pass against the broken interpreter too. The third takes the
|
|
|
|
|
* opposite approach and asks for the whole pool after only a few scopes, which
|
|
|
|
|
* is cheaper and sharper: one leaked slot and it has nowhere to go.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
|
|
#include <akbasic/error.h>
|
|
|
|
|
#include <akbasic/runtime.h>
|
|
|
|
|
|
|
|
|
|
#include "harness.h"
|
|
|
|
|
#include "testutil.h"
|
|
|
|
|
|
|
|
|
|
/** @brief Run a program to completion, bounded so a hang fails rather than waits. */
|
|
|
|
|
static akerr_ErrorContext AKERR_NOIGNORE *run_program(const char *source)
|
|
|
|
|
{
|
|
|
|
|
PREPARE_ERROR(errctx);
|
|
|
|
|
|
|
|
|
|
PASS(errctx, harness_start(NULL));
|
|
|
|
|
PASS(errctx, akbasic_runtime_load(&HARNESS_RUNTIME, source));
|
|
|
|
|
PASS(errctx, akbasic_runtime_start(&HARNESS_RUNTIME, AKBASIC_MODE_RUN));
|
|
|
|
|
PASS(errctx, akbasic_runtime_run(&HARNESS_RUNTIME, 2000000));
|
|
|
|
|
SUCCEED_RETURN(errctx);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief 6,000 GOSUBs, each creating a local, cost nothing.
|
|
|
|
|
*
|
|
|
|
|
* TODO.md section 6 item 30's reduction, unchanged. It used to fail at
|
|
|
|
|
* `LOC# = 1` on the 4091st call with "Array of 1 elements does not fit in the
|
|
|
|
|
* 0 remaining value slots" -- naming the line that was unlucky rather than the
|
|
|
|
|
* line that was wrong, which is most of why it cost an evening to find.
|
|
|
|
|
*/
|
|
|
|
|
static void test_scoped_scalar_costs_nothing(void)
|
|
|
|
|
{
|
|
|
|
|
TEST_REQUIRE_OK(run_program("10 X# = 0\n"
|
|
|
|
|
"20 FOR T# = 1 TO 6000\n"
|
|
|
|
|
"30 GOSUB SUBA\n"
|
|
|
|
|
"40 NEXT T#\n"
|
|
|
|
|
"50 PRINT \"OK \" + X#\n"
|
|
|
|
|
"60 END\n"
|
|
|
|
|
"70 LABEL SUBA\n"
|
|
|
|
|
"80 LOC# = 1\n"
|
|
|
|
|
"90 X# = X# + LOC#\n"
|
|
|
|
|
"100 RETURN\n"));
|
|
|
|
|
TEST_REQUIRE_STR(HARNESS_OUTPUT, "OK 6000\n");
|
|
|
|
|
harness_stop();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief A `FOR` counter is a name like any other, and costs nothing either.
|
|
|
|
|
*
|
|
|
|
|
* Worth its own case: the counter is created by the loop rather than by an
|
|
|
|
|
* assignment the author wrote, so it was the half of this defect that a program
|
|
|
|
|
* could hit without appearing to create anything at all.
|
|
|
|
|
*
|
|
|
|
|
* `TO 2` rather than `TO 1` because equal bounds run the body zero times in this
|
|
|
|
|
* dialect -- see docs/13-differences.md. A body that never runs would pass this
|
|
|
|
|
* test for the wrong reason.
|
|
|
|
|
*/
|
|
|
|
|
static void test_scoped_loop_counter_costs_nothing(void)
|
|
|
|
|
{
|
|
|
|
|
TEST_REQUIRE_OK(run_program("10 X# = 0\n"
|
|
|
|
|
"20 FOR T# = 1 TO 6000\n"
|
|
|
|
|
"30 GOSUB SUBA\n"
|
|
|
|
|
"40 NEXT T#\n"
|
|
|
|
|
"50 PRINT \"OK \" + X#\n"
|
|
|
|
|
"60 END\n"
|
|
|
|
|
"70 LABEL SUBA\n"
|
|
|
|
|
"80 FOR INNER# = 1 TO 2\n"
|
|
|
|
|
"90 X# = X# + 1\n"
|
|
|
|
|
"100 NEXT INNER#\n"
|
|
|
|
|
"110 RETURN\n"));
|
|
|
|
|
TEST_REQUIRE_STR(HARNESS_OUTPUT, "OK 12000\n");
|
|
|
|
|
harness_stop();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief The pool is genuinely untouched, not merely large enough.
|
|
|
|
|
*
|
Cut akbasic_Runtime's static footprint from 10.75 MiB to 2.40 MiB
Nothing in this interpreter mallocs; every pool is a fixed array sized by an
AKBASIC_MAX_* constant, so sizeof(akbasic_Runtime) is a compile-time number
and most of it was headroom nobody was using. Measured concurrent-use
high-water marks off examples/breakout and examples/megademo -- the two most
demanding programs this interpreter runs -- against each pool's ceiling:
AKBASIC_MAX_ENVIRONMENTS 32 -> 12 (measured peak concurrency: 6-7)
AKBASIC_MAX_FUNCTIONS 64 -> 8 (measured: 0, neither program uses DEF FN)
AKBASIC_MAX_ARRAY_VALUES 4096 -> 2048 (measured peak: 1618 slots)
AKBASIC_MAX_SOURCE_LINES 9999 -> 2048 (measured: ~1270-1496 non-blank lines)
AKBASIC_SYMTAB_MAX_SLOTS 256 -> 172 (no caller ever requests more than 128)
AKBASIC_SYMTAB_MAX_KEY 64 -> 24 (longest identifier measured: 11 chars)
AKBASIC_MAX_LINE_LENGTH 256 -> 80 (Commodore BASIC's own line limit)
AKBASIC_MAX_VARIABLES (128) is untouched on purpose: breakout alone reaches
121 of 128 concurrent named variables, so it has the least slack of any pool
measured and is not a shrink candidate.
akbasic_Variable.name shrinks from AKBASIC_MAX_STRING_LENGTH (256) to
AKBASIC_SYMTAB_MAX_KEY: every variable name is registered with
akbasic_symtab_set() right after this field is populated
(akbasic_environment_create(), src/environment.c), and that call already
refuses anything AKBASIC_SYMTAB_MAX_KEY characters or longer. The wider field
was headroom nothing could ever put a byte into.
Two defects surfaced while testing the line-length drop against the golden
corpus, both fixed here because the 80-byte ceiling makes them routine rather
than theoretical:
- sourcepath (runtime.h) was borrowing AKBASIC_MAX_LINE_LENGTH by accident.
It holds a directory, not a line of BASIC, and this checkout's own test
paths are 81+ characters deep -- every golden test failed to load until
this split into its own AKBASIC_MAX_SOURCE_PATH_LENGTH, backed by PATH_MAX
the way libakerror already sizes its own path buffers.
- src/sink_stdio.c's stdio_readline() called aksl_fgets() but never checked
its own documented contract: a full buffer with no trailing newline means
the line was longer than the buffer, and the unread remainder is still in
the stream. Unchecked, the next readline() picks that remainder up as its
own statement -- a real line silently becomes two wrong ones instead of a
clean AKBASIC_ERR_BOUNDS refusal. At 256 bytes this was theoretical; at 80
it is not, so it now refuses loudly.
tests/value_pool.c's test_pool_is_untouched_by_scopes() was pinned to the old
4x1024=4096 pool math (four max-size arrays proving nothing leaked); rewritten
to 2x1024=2048 for the same proof against the new AKBASIC_MAX_ARRAY_VALUES.
Known consequence, tracked in andrew/akbasic#32 rather than worked around
here: two files in the protected tests/reference/ corpus
(language/functions/mod.bas, language/flowcontrol/nestedforloopwaitingfor
command.bas) have 82-character lines and cannot be shortened -- MAINTENANCE.md
and CMakeLists.txt:585 are explicit that tests/reference/ is never edited to
suit this interpreter. Twelve tests/language/ cases and one docs/18 line are
in the same position but are this project's own content. Shipping 80 anyway,
with the fallout tracked rather than hidden, was an explicit call on this PR
rather than something decided here.
Verified: cmake --build build-akgl && ctest --test-dir build-akgl, 97/112 (15
known failures, all AKBASIC_MAX_LINE_LENGTH-related, filed as #32).
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Co-Authored-By: Andrew Kesterson <andrew@aklabs.net>
2026-08-03 21:44:16 -04:00
|
|
|
* Two hundred scope entries and then the pool's *entire* width -- two arrays,
|
Stop a scalar created inside a scope costing value-pool slots
A scalar now lives in the variable record (`akbasic_Variable::inlinevalue`)
rather than drawing from the value pool, so a `GOSUB` local, a `FOR` counter and
a `DEF` parameter cost nothing at all.
The pool is a bump allocator with no free, and its comment justified that with
"nothing in BASIC destroys a variable". Scope exit does: it marks the variable
slot unused, `new_variable()` memsets the slot it hands back -- clearing
`values` -- and `variable_init()` therefore took *fresh* slots for a variable
whose old ones were still counted. Every scope that created a local leaked, with
no diagnostic until the pool ran dry on whichever line happened to be unlucky.
Six thousand `GOSUB`s creating one local used to die on the 4091st at `LOC# = 1`
with "Array of 1 elements does not fit in the 0 remaining value slots". They now
run. A `DEF` called eight thousand times used to die between the four and five
thousandth -- the leaking slot was the call scope's parameter, which is a scalar
-- and both forms now run. A game creating one name per tick was dead in half a
minute; the Breakout in examples/ was, after twenty-five seconds.
**A `@` name is the one exclusion, and it is the whole of it.** A structure or a
pointer to one keeps pool storage, because a pointer into a record outlives the
scope that DIMmed it -- docs/16-structures.md says nothing is reclaimed and
`prev_environment()` relies on it. The name suffix is the right test rather than
`structtype`, which the DIM path sets *after* calling `variable_init()`. A local
array therefore still leaks, deliberately, and is now the narrow rule the
tutorial teaches.
`SWAP` needed the other half: it copies whole variable records, so the `values`
pointer that came over named the other variable's inline slot -- which by then
held this variable's own old value -- and SWAP silently did nothing. Caught by
tests/language/housekeeping/verbs.bas, which is the golden corpus earning its
keep.
tests/value_pool.c is the new coverage. It asserts the mechanism as well as the
consequence: a later change that moved arrays inline too would pass every
behavioural case and quietly break the pointer guarantee. The sharpest case
takes the pool's whole 4096 slots in four arrays after two hundred scope
entries, so one leaked slot has nowhere to go.
Chapter 17 Step 3 taught "declare every name at the top" and no longer needs to.
It now teaches what is still true -- a name first seen inside a subroutine dies
at RETURN, so a routine cannot answer its caller through one -- and its
demonstration is the array case, which still fails.
TODO.md section 6 item 30 and section 9 item 1, both struck.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Co-Authored-By: Andrew Kesterson <andrew@aklabs.net>
2026-08-01 23:47:49 -04:00
|
|
|
* because AKBASIC_MAX_ARRAY_ELEMENTS caps any one of them at 1024 while the pool
|
Cut akbasic_Runtime's static footprint from 10.75 MiB to 2.40 MiB
Nothing in this interpreter mallocs; every pool is a fixed array sized by an
AKBASIC_MAX_* constant, so sizeof(akbasic_Runtime) is a compile-time number
and most of it was headroom nobody was using. Measured concurrent-use
high-water marks off examples/breakout and examples/megademo -- the two most
demanding programs this interpreter runs -- against each pool's ceiling:
AKBASIC_MAX_ENVIRONMENTS 32 -> 12 (measured peak concurrency: 6-7)
AKBASIC_MAX_FUNCTIONS 64 -> 8 (measured: 0, neither program uses DEF FN)
AKBASIC_MAX_ARRAY_VALUES 4096 -> 2048 (measured peak: 1618 slots)
AKBASIC_MAX_SOURCE_LINES 9999 -> 2048 (measured: ~1270-1496 non-blank lines)
AKBASIC_SYMTAB_MAX_SLOTS 256 -> 172 (no caller ever requests more than 128)
AKBASIC_SYMTAB_MAX_KEY 64 -> 24 (longest identifier measured: 11 chars)
AKBASIC_MAX_LINE_LENGTH 256 -> 80 (Commodore BASIC's own line limit)
AKBASIC_MAX_VARIABLES (128) is untouched on purpose: breakout alone reaches
121 of 128 concurrent named variables, so it has the least slack of any pool
measured and is not a shrink candidate.
akbasic_Variable.name shrinks from AKBASIC_MAX_STRING_LENGTH (256) to
AKBASIC_SYMTAB_MAX_KEY: every variable name is registered with
akbasic_symtab_set() right after this field is populated
(akbasic_environment_create(), src/environment.c), and that call already
refuses anything AKBASIC_SYMTAB_MAX_KEY characters or longer. The wider field
was headroom nothing could ever put a byte into.
Two defects surfaced while testing the line-length drop against the golden
corpus, both fixed here because the 80-byte ceiling makes them routine rather
than theoretical:
- sourcepath (runtime.h) was borrowing AKBASIC_MAX_LINE_LENGTH by accident.
It holds a directory, not a line of BASIC, and this checkout's own test
paths are 81+ characters deep -- every golden test failed to load until
this split into its own AKBASIC_MAX_SOURCE_PATH_LENGTH, backed by PATH_MAX
the way libakerror already sizes its own path buffers.
- src/sink_stdio.c's stdio_readline() called aksl_fgets() but never checked
its own documented contract: a full buffer with no trailing newline means
the line was longer than the buffer, and the unread remainder is still in
the stream. Unchecked, the next readline() picks that remainder up as its
own statement -- a real line silently becomes two wrong ones instead of a
clean AKBASIC_ERR_BOUNDS refusal. At 256 bytes this was theoretical; at 80
it is not, so it now refuses loudly.
tests/value_pool.c's test_pool_is_untouched_by_scopes() was pinned to the old
4x1024=4096 pool math (four max-size arrays proving nothing leaked); rewritten
to 2x1024=2048 for the same proof against the new AKBASIC_MAX_ARRAY_VALUES.
Known consequence, tracked in andrew/akbasic#32 rather than worked around
here: two files in the protected tests/reference/ corpus
(language/functions/mod.bas, language/flowcontrol/nestedforloopwaitingfor
command.bas) have 82-character lines and cannot be shortened -- MAINTENANCE.md
and CMakeLists.txt:585 are explicit that tests/reference/ is never edited to
suit this interpreter. Twelve tests/language/ cases and one docs/18 line are
in the same position but are this project's own content. Shipping 80 anyway,
with the fallout tracked rather than hidden, was an explicit call on this PR
rather than something decided here.
Verified: cmake --build build-akgl && ctest --test-dir build-akgl, 97/112 (15
known failures, all AKBASIC_MAX_LINE_LENGTH-related, filed as #32).
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Co-Authored-By: Andrew Kesterson <andrew@aklabs.net>
2026-08-03 21:44:16 -04:00
|
|
|
* holds 2048. One leaked slot and the second `DIM` has nowhere to go, which
|
Stop a scalar created inside a scope costing value-pool slots
A scalar now lives in the variable record (`akbasic_Variable::inlinevalue`)
rather than drawing from the value pool, so a `GOSUB` local, a `FOR` counter and
a `DEF` parameter cost nothing at all.
The pool is a bump allocator with no free, and its comment justified that with
"nothing in BASIC destroys a variable". Scope exit does: it marks the variable
slot unused, `new_variable()` memsets the slot it hands back -- clearing
`values` -- and `variable_init()` therefore took *fresh* slots for a variable
whose old ones were still counted. Every scope that created a local leaked, with
no diagnostic until the pool ran dry on whichever line happened to be unlucky.
Six thousand `GOSUB`s creating one local used to die on the 4091st at `LOC# = 1`
with "Array of 1 elements does not fit in the 0 remaining value slots". They now
run. A `DEF` called eight thousand times used to die between the four and five
thousandth -- the leaking slot was the call scope's parameter, which is a scalar
-- and both forms now run. A game creating one name per tick was dead in half a
minute; the Breakout in examples/ was, after twenty-five seconds.
**A `@` name is the one exclusion, and it is the whole of it.** A structure or a
pointer to one keeps pool storage, because a pointer into a record outlives the
scope that DIMmed it -- docs/16-structures.md says nothing is reclaimed and
`prev_environment()` relies on it. The name suffix is the right test rather than
`structtype`, which the DIM path sets *after* calling `variable_init()`. A local
array therefore still leaks, deliberately, and is now the narrow rule the
tutorial teaches.
`SWAP` needed the other half: it copies whole variable records, so the `values`
pointer that came over named the other variable's inline slot -- which by then
held this variable's own old value -- and SWAP silently did nothing. Caught by
tests/language/housekeeping/verbs.bas, which is the golden corpus earning its
keep.
tests/value_pool.c is the new coverage. It asserts the mechanism as well as the
consequence: a later change that moved arrays inline too would pass every
behavioural case and quietly break the pointer guarantee. The sharpest case
takes the pool's whole 4096 slots in four arrays after two hundred scope
entries, so one leaked slot has nowhere to go.
Chapter 17 Step 3 taught "declare every name at the top" and no longer needs to.
It now teaches what is still true -- a name first seen inside a subroutine dies
at RETURN, so a routine cannot answer its caller through one -- and its
demonstration is the array case, which still fails.
TODO.md section 6 item 30 and section 9 item 1, both struck.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Co-Authored-By: Andrew Kesterson <andrew@aklabs.net>
2026-08-01 23:47:49 -04:00
|
|
|
* makes this the sharpest of the three and by far the cheapest: the two above
|
|
|
|
|
* prove a program finishes, this proves nothing at all was spent. The long ones
|
|
|
|
|
* stay because they are the shape the defect was found in.
|
|
|
|
|
*/
|
|
|
|
|
static void test_pool_is_untouched_by_scopes(void)
|
|
|
|
|
{
|
|
|
|
|
TEST_REQUIRE_OK(run_program("10 FOR T# = 1 TO 200\n"
|
|
|
|
|
"20 GOSUB SUBA\n"
|
|
|
|
|
"30 NEXT T#\n"
|
|
|
|
|
"40 DIM A#(1024)\n"
|
|
|
|
|
"50 DIM B#(1024)\n"
|
Cut akbasic_Runtime's static footprint from 10.75 MiB to 2.40 MiB
Nothing in this interpreter mallocs; every pool is a fixed array sized by an
AKBASIC_MAX_* constant, so sizeof(akbasic_Runtime) is a compile-time number
and most of it was headroom nobody was using. Measured concurrent-use
high-water marks off examples/breakout and examples/megademo -- the two most
demanding programs this interpreter runs -- against each pool's ceiling:
AKBASIC_MAX_ENVIRONMENTS 32 -> 12 (measured peak concurrency: 6-7)
AKBASIC_MAX_FUNCTIONS 64 -> 8 (measured: 0, neither program uses DEF FN)
AKBASIC_MAX_ARRAY_VALUES 4096 -> 2048 (measured peak: 1618 slots)
AKBASIC_MAX_SOURCE_LINES 9999 -> 2048 (measured: ~1270-1496 non-blank lines)
AKBASIC_SYMTAB_MAX_SLOTS 256 -> 172 (no caller ever requests more than 128)
AKBASIC_SYMTAB_MAX_KEY 64 -> 24 (longest identifier measured: 11 chars)
AKBASIC_MAX_LINE_LENGTH 256 -> 80 (Commodore BASIC's own line limit)
AKBASIC_MAX_VARIABLES (128) is untouched on purpose: breakout alone reaches
121 of 128 concurrent named variables, so it has the least slack of any pool
measured and is not a shrink candidate.
akbasic_Variable.name shrinks from AKBASIC_MAX_STRING_LENGTH (256) to
AKBASIC_SYMTAB_MAX_KEY: every variable name is registered with
akbasic_symtab_set() right after this field is populated
(akbasic_environment_create(), src/environment.c), and that call already
refuses anything AKBASIC_SYMTAB_MAX_KEY characters or longer. The wider field
was headroom nothing could ever put a byte into.
Two defects surfaced while testing the line-length drop against the golden
corpus, both fixed here because the 80-byte ceiling makes them routine rather
than theoretical:
- sourcepath (runtime.h) was borrowing AKBASIC_MAX_LINE_LENGTH by accident.
It holds a directory, not a line of BASIC, and this checkout's own test
paths are 81+ characters deep -- every golden test failed to load until
this split into its own AKBASIC_MAX_SOURCE_PATH_LENGTH, backed by PATH_MAX
the way libakerror already sizes its own path buffers.
- src/sink_stdio.c's stdio_readline() called aksl_fgets() but never checked
its own documented contract: a full buffer with no trailing newline means
the line was longer than the buffer, and the unread remainder is still in
the stream. Unchecked, the next readline() picks that remainder up as its
own statement -- a real line silently becomes two wrong ones instead of a
clean AKBASIC_ERR_BOUNDS refusal. At 256 bytes this was theoretical; at 80
it is not, so it now refuses loudly.
tests/value_pool.c's test_pool_is_untouched_by_scopes() was pinned to the old
4x1024=4096 pool math (four max-size arrays proving nothing leaked); rewritten
to 2x1024=2048 for the same proof against the new AKBASIC_MAX_ARRAY_VALUES.
Known consequence, tracked in andrew/akbasic#32 rather than worked around
here: two files in the protected tests/reference/ corpus
(language/functions/mod.bas, language/flowcontrol/nestedforloopwaitingfor
command.bas) have 82-character lines and cannot be shortened -- MAINTENANCE.md
and CMakeLists.txt:585 are explicit that tests/reference/ is never edited to
suit this interpreter. Twelve tests/language/ cases and one docs/18 line are
in the same position but are this project's own content. Shipping 80 anyway,
with the fallout tracked rather than hidden, was an explicit call on this PR
rather than something decided here.
Verified: cmake --build build-akgl && ctest --test-dir build-akgl, 97/112 (15
known failures, all AKBASIC_MAX_LINE_LENGTH-related, filed as #32).
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Co-Authored-By: Andrew Kesterson <andrew@aklabs.net>
2026-08-03 21:44:16 -04:00
|
|
|
"70 B#(1023) = 7\n"
|
|
|
|
|
"90 PRINT B#(1023)\n"
|
Stop a scalar created inside a scope costing value-pool slots
A scalar now lives in the variable record (`akbasic_Variable::inlinevalue`)
rather than drawing from the value pool, so a `GOSUB` local, a `FOR` counter and
a `DEF` parameter cost nothing at all.
The pool is a bump allocator with no free, and its comment justified that with
"nothing in BASIC destroys a variable". Scope exit does: it marks the variable
slot unused, `new_variable()` memsets the slot it hands back -- clearing
`values` -- and `variable_init()` therefore took *fresh* slots for a variable
whose old ones were still counted. Every scope that created a local leaked, with
no diagnostic until the pool ran dry on whichever line happened to be unlucky.
Six thousand `GOSUB`s creating one local used to die on the 4091st at `LOC# = 1`
with "Array of 1 elements does not fit in the 0 remaining value slots". They now
run. A `DEF` called eight thousand times used to die between the four and five
thousandth -- the leaking slot was the call scope's parameter, which is a scalar
-- and both forms now run. A game creating one name per tick was dead in half a
minute; the Breakout in examples/ was, after twenty-five seconds.
**A `@` name is the one exclusion, and it is the whole of it.** A structure or a
pointer to one keeps pool storage, because a pointer into a record outlives the
scope that DIMmed it -- docs/16-structures.md says nothing is reclaimed and
`prev_environment()` relies on it. The name suffix is the right test rather than
`structtype`, which the DIM path sets *after* calling `variable_init()`. A local
array therefore still leaks, deliberately, and is now the narrow rule the
tutorial teaches.
`SWAP` needed the other half: it copies whole variable records, so the `values`
pointer that came over named the other variable's inline slot -- which by then
held this variable's own old value -- and SWAP silently did nothing. Caught by
tests/language/housekeeping/verbs.bas, which is the golden corpus earning its
keep.
tests/value_pool.c is the new coverage. It asserts the mechanism as well as the
consequence: a later change that moved arrays inline too would pass every
behavioural case and quietly break the pointer guarantee. The sharpest case
takes the pool's whole 4096 slots in four arrays after two hundred scope
entries, so one leaked slot has nowhere to go.
Chapter 17 Step 3 taught "declare every name at the top" and no longer needs to.
It now teaches what is still true -- a name first seen inside a subroutine dies
at RETURN, so a routine cannot answer its caller through one -- and its
demonstration is the array case, which still fails.
TODO.md section 6 item 30 and section 9 item 1, both struck.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Co-Authored-By: Andrew Kesterson <andrew@aklabs.net>
2026-08-01 23:47:49 -04:00
|
|
|
"100 END\n"
|
|
|
|
|
"110 LABEL SUBA\n"
|
|
|
|
|
"120 LOC# = 1\n"
|
|
|
|
|
"130 RETURN\n"));
|
|
|
|
|
TEST_REQUIRE_STR(HARNESS_OUTPUT, "7\n");
|
|
|
|
|
harness_stop();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief A structure keeps pool storage, which is what a pointer relies on.
|
|
|
|
|
*
|
|
|
|
|
* The exclusion that makes the fix safe. docs/16-structures.md says nothing is
|
|
|
|
|
* reclaimed and akbasic_runtime_prev_environment() says a pointer into a record
|
|
|
|
|
* DIMmed in a scope stays sound after the scope is gone -- so a `@` name must
|
|
|
|
|
* *not* move into the variable, where the next occupant of the slot would
|
|
|
|
|
* overwrite it. A single-field TYPE is the case that would slip through a test
|
|
|
|
|
* written against size alone.
|
|
|
|
|
*/
|
|
|
|
|
static void test_structure_storage_stays_in_the_pool(void)
|
|
|
|
|
{
|
|
|
|
|
akbasic_Variable *variable = NULL;
|
|
|
|
|
|
|
|
|
|
TEST_REQUIRE_OK(run_program("10 TYPE ONE\n"
|
|
|
|
|
"20 N#\n"
|
|
|
|
|
"30 END TYPE\n"
|
|
|
|
|
"40 DIM R@ AS ONE\n"
|
|
|
|
|
"50 R@.N# = 5\n"
|
|
|
|
|
"60 PRINT R@.N#\n"));
|
|
|
|
|
TEST_REQUIRE_STR(HARNESS_OUTPUT, "5\n");
|
|
|
|
|
|
|
|
|
|
TEST_REQUIRE_OK(akbasic_environment_get(HARNESS_RUNTIME.environment, "R@", &variable));
|
|
|
|
|
TEST_REQUIRE(variable != NULL, "the structure variable must exist");
|
|
|
|
|
TEST_REQUIRE(variable->values != &variable->inlinevalue,
|
|
|
|
|
"a structure must keep pool storage, not inline storage");
|
|
|
|
|
harness_stop();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief A scalar's storage is inside the variable, and an array's is not.
|
|
|
|
|
*
|
|
|
|
|
* Asserted directly rather than only through a program, so the mechanism is
|
|
|
|
|
* pinned and not just its consequence. A future change that made arrays inline
|
|
|
|
|
* would pass every test above and break the pointer guarantee.
|
|
|
|
|
*/
|
|
|
|
|
static void test_storage_lands_where_it_should(void)
|
|
|
|
|
{
|
|
|
|
|
akbasic_Variable *scalar = NULL;
|
|
|
|
|
akbasic_Variable *array = NULL;
|
|
|
|
|
|
|
|
|
|
TEST_REQUIRE_OK(run_program("10 S# = 1\n"
|
|
|
|
|
"20 DIM A#(4)\n"));
|
|
|
|
|
|
|
|
|
|
TEST_REQUIRE_OK(akbasic_environment_get(HARNESS_RUNTIME.environment, "S#", &scalar));
|
|
|
|
|
TEST_REQUIRE(scalar != NULL, "the scalar must exist");
|
|
|
|
|
TEST_REQUIRE(scalar->values == &scalar->inlinevalue,
|
|
|
|
|
"a scalar must be stored in the variable");
|
|
|
|
|
|
|
|
|
|
TEST_REQUIRE_OK(akbasic_environment_get(HARNESS_RUNTIME.environment, "A#", &array));
|
|
|
|
|
TEST_REQUIRE(array != NULL, "the array must exist");
|
|
|
|
|
TEST_REQUIRE(array->values != &array->inlinevalue,
|
|
|
|
|
"an array must be stored in the pool");
|
|
|
|
|
harness_stop();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief `SWAP` exchanges two scalars, whose storage moves with the record.
|
|
|
|
|
*
|
|
|
|
|
* A regression rather than a feature: SWAP copies the whole variable record, so
|
|
|
|
|
* the `values` pointer that comes over names the *other* variable's inline slot
|
|
|
|
|
* -- which by then holds this variable's own old value. Left alone each side
|
|
|
|
|
* reads back what it started with and SWAP silently does nothing, which is
|
|
|
|
|
* exactly what the housekeeping golden case caught.
|
|
|
|
|
*/
|
|
|
|
|
static void test_swap_still_exchanges_scalars(void)
|
|
|
|
|
{
|
|
|
|
|
TEST_REQUIRE_OK(run_program("10 A# = 1 : B# = 2\n"
|
|
|
|
|
"20 SWAP A#, B#\n"
|
|
|
|
|
"30 PRINT A# : PRINT B#\n"));
|
|
|
|
|
TEST_REQUIRE_STR(HARNESS_OUTPUT, "2\n1\n");
|
|
|
|
|
harness_stop();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int main(void)
|
|
|
|
|
{
|
|
|
|
|
test_scoped_scalar_costs_nothing();
|
|
|
|
|
test_scoped_loop_counter_costs_nothing();
|
|
|
|
|
test_pool_is_untouched_by_scopes();
|
|
|
|
|
test_structure_storage_stays_in_the_pool();
|
|
|
|
|
test_storage_lands_where_it_should();
|
|
|
|
|
test_swap_still_exchanges_scalars();
|
|
|
|
|
return akbasic_test_failures;
|
|
|
|
|
}
|