1 Commits

Author SHA1 Message Date
Ishikawa
2cb68665d0 Fix generator teardown leaks, add RETURN-in-GEN and LOOP conditions on DO EACH
All checks were successful
akbasic CI Build / cmake_build (push) Successful in 3m26s
akbasic CI Build / coverage (push) Successful in 4m0s
akbasic CI Build / sanitizers (push) Successful in 6m33s
akbasic CI Build / akgl_build (push) Successful in 9m32s
akbasic CI Build / mutation_test (push) Successful in 18m47s
Review findings and follow-ups from PR #61 review:

- runtime_generator.c: akbasic_runtime_release_generator() now releases the
  forGeneratorEnv of every scope it walks through. Abandoning a generator
  that was itself suspended inside a FOR EACH over another generator
  stranded the inner generator's pool slot; a loop doing so exhausted the
  twelve-slot pool and died far from the cause.
- runtime.c/runtime.h: new akbasic_runtime_unwind_to_environment(), the
  shared teardown for the error unwinds in pump_generator() and
  call_function() -- both previously bare prev_environment() loops with the
  same suspended-generator blindness.
- runtime_commands.c: bare RETURN standing in a GEN's own frame ends the
  generator exactly as END GEN does -- a GEN is a function at heart. RETURN
  with a value there is refused (values leave a GEN only through EMIT). The
  no-frame error message now says "GOSUB, DEF, or GEN".
- runtime_structure.c: LOOP WHILE/UNTIL composes with DO EACH -- checked
  after each trip with the loop variable still holding that trip's value; a
  condition that stops the loop abandons the generator exactly as EXIT
  does. Previously the condition was silently ignored, while the verb
  reference documented it as working.
- parser_commands.c: trailing tokens after the generator call on a FOR
  EACH/DO EACH line are refused at parse. Previously they sat unparsed and
  blew up only after the loop completed, when the parent scope resumed the
  line mid-statement -- an error at the loop's end pointing at its start.
- tests/generators.c: pool-exhaustion tests for the nested-abandonment and
  LOOP-condition paths, RETURN semantics tests, and a direct test of the
  unwind primitive. Three new golden pairs cover RETURN, LOOP conditions
  and the misplaced-condition parse error.
- docs: RETURN and LOOP-condition semantics in 04-control-flow.md and
  11-verb-reference.md; corrected the self-recursion analogy (functions
  are re-entrant here). TODO.md 1.10 records the generator design
  decisions the code comments were already citing, plus the zero-arg
  parameter-list limitation. MAINTENANCE.md gains the abandoned-generators
  invariant those comments also cited.

Co-Authored-By: Andrew Kesterson <andrew@aklabs.net>
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-08-06 11:29:17 -04:00
3 changed files with 12 additions and 74 deletions

View File

@@ -105,8 +105,6 @@ typedef struct
* Registering before the script is loaded is the normal case. A host type and a
* `TYPE` the script declares share one namespace, so a script cannot declare a
* type the host already registered -- and would be refused if it tried.
* Host type and field names are limited to 31 characters, matching
* script-declared types and fields.
*
* @param obj Object to initialize, inspect, or modify.
* @param type The host's description of its own struct.
@@ -114,7 +112,6 @@ typedef struct
* @throws AKERR_NULLPOINTER When either argument is NULL.
* @throws AKBASIC_ERR_VALUE When a field name carries no type suffix, a nested
* type is not registered, or the name is already taken.
* @throws AKERR_OUTOFBOUNDS When a type or field name exceeds 31 characters.
* @throws AKBASIC_ERR_BOUNDS When the type table or a field list is full.
*/
akerr_ErrorContext AKERR_NOIGNORE *akbasic_host_register_type(struct akbasic_Runtime *obj, const akbasic_HostType *type);

View File

@@ -235,9 +235,16 @@ akerr_ErrorContext *akbasic_host_register_type(akbasic_Runtime *obj, const akbas
dest = &obj->structtypes.types[obj->structtypes.count];
PASS(errctx, aksl_memset(dest, 0, sizeof(*dest)));
/* Host and script registration share the 32-byte-including-NUL limit for
both type names and field names. */
PASS(errctx, aksl_strcpy(dest->name, sizeof(dest->name), type->name));
/*
* Raw snprintf, and a latent defect rather than a settled decision: a host
* type name over 31 characters truncates silently here, and two that share a
* 31-character prefix then collide in akbasic_structtype_find. scan_names()
* in structtype.c already refuses the same case for a script-declared type
* with an explicit limit message, so the two paths disagree. Converting this
* to aksl_strcpy is the fix and it is a behaviour change on a public
* registration call, so it wants its own issue rather than this port.
*/
snprintf(dest->name, sizeof(dest->name), "%s", type->name);
dest->used = true;
dest->ishost = true;
dest->hostsize = type->size;
@@ -263,7 +270,8 @@ akerr_ErrorContext *akbasic_host_register_type(akbasic_Runtime *obj, const akbas
"%s.%s must end in '%c' for the C type it describes",
type->name, src->name, suffix_for(src->kind));
PASS(errctx, aksl_strcpy(field->name, sizeof(field->name), src->name));
/* Same silent truncation as the type name above, and the same fix. */
snprintf(field->name, sizeof(field->name), "%s", src->name);
field->hostkind = src->kind;
field->hostoffset = src->offset;
field->hostwidth = src->width;

View File

@@ -208,72 +208,6 @@ static void test_suffix_must_match_the_c_type(void)
harness_stop();
}
/** @brief Host names use the same bounded storage as script-declared names. */
static void test_registration_name_limits(void)
{
static const akbasic_HostField SHORT_FIELD[] = {
AKBASIC_HOST_FIELD( test_Enemy, hp, "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234#",
AKBASIC_HOSTFIELD_INT32 )
};
static const akbasic_HostField LONG_FIELD_A[] = {
AKBASIC_HOST_FIELD( test_Enemy, hp, "ABCDEFGHIJKLMNOPQRSTUVWXYZ123456A#",
AKBASIC_HOSTFIELD_INT32 )
};
static const akbasic_HostField LONG_FIELD_B[] = {
AKBASIC_HOST_FIELD( test_Enemy, hp, "ABCDEFGHIJKLMNOPQRSTUVWXYZ123456B#",
AKBASIC_HOSTFIELD_INT32 )
};
static const akbasic_HostType SHORT_TYPE = {
"ABCDEFGHIJKLMNOPQRSTUVWXYZ12345", sizeof(test_Enemy), SHORT_FIELD, 1
};
static const akbasic_HostType LONG_TYPE_A = {
"ABCDEFGHIJKLMNOPQRSTUVWXYZ123456A", sizeof(test_Enemy), SHORT_FIELD, 1
};
static const akbasic_HostType LONG_TYPE_B = {
"ABCDEFGHIJKLMNOPQRSTUVWXYZ123456B", sizeof(test_Enemy), SHORT_FIELD, 1
};
static const akbasic_HostType LONG_FIELD_TYPE_A = {
"LONGFIELDA", sizeof(test_Enemy), LONG_FIELD_A, 1
};
static const akbasic_HostType LONG_FIELD_TYPE_B = {
"LONGFIELDB", sizeof(test_Enemy), LONG_FIELD_B, 1
};
akerr_ErrorContext *raised = NULL;
TEST_REQUIRE_OK(harness_start(NULL));
TEST_REQUIRE_OK(akbasic_host_register_type(&HARNESS_RUNTIME, &SHORT_TYPE));
harness_stop();
TEST_REQUIRE_OK(harness_start(NULL));
raised = akbasic_host_register_type(&HARNESS_RUNTIME, &LONG_TYPE_A);
TEST_REQUIRE(raised != NULL, "an overlong type name must be refused");
TEST_REQUIRE_INT(raised->status, AKERR_OUTOFBOUNDS);
test_discard_error(raised);
raised = akbasic_host_register_type(&HARNESS_RUNTIME, &LONG_TYPE_B);
TEST_REQUIRE(raised != NULL, "a second long type name must fail cleanly");
TEST_REQUIRE_INT(raised->status, AKERR_OUTOFBOUNDS);
test_discard_error(raised);
harness_stop();
TEST_REQUIRE_OK(harness_start(NULL));
TEST_REQUIRE_OK(akbasic_host_register_type(&HARNESS_RUNTIME,
&(akbasic_HostType){
"SHORTFIELDS", sizeof(test_Enemy), SHORT_FIELD, 1
}));
harness_stop();
TEST_REQUIRE_OK(harness_start(NULL));
raised = akbasic_host_register_type(&HARNESS_RUNTIME, &LONG_FIELD_TYPE_A);
TEST_REQUIRE(raised != NULL, "an overlong field name must be refused");
TEST_REQUIRE_INT(raised->status, AKERR_OUTOFBOUNDS);
test_discard_error(raised);
raised = akbasic_host_register_type(&HARNESS_RUNTIME, &LONG_FIELD_TYPE_B);
TEST_REQUIRE(raised != NULL, "a second long field name must fail cleanly");
TEST_REQUIRE_INT(raised->status, AKERR_OUTOFBOUNDS);
test_discard_error(raised);
harness_stop();
}
/**
* @brief After unbinding, the name is refused rather than read.
*
@@ -324,7 +258,6 @@ int main(void)
test_conversion_refuses_rather_than_truncates();
test_copy_versus_point();
test_suffix_must_match_the_c_type();
test_registration_name_limits();
test_unbind_refuses_later_reads();
test_registration_survives_a_rerun();