Share a host program's own C structures with a script

A BASIC TYPE and a host C struct are the same thing seen from two sides, so both
go in one type table and everything the language already does with a structure
works across the boundary with no second set of rules. The host describes its
struct once as a table of field descriptors and binds an instance:

    akbasic_host_bind(&SCRIPT, "FOE@", "ENEMY", &GOBLIN);

after which `FOE@.HP# = FOE@.HP# - 10` decrements GOBLIN.hp in place, with no
marshalling step the host has to remember to run.

The sharing is done with shadow slots: a binding takes a run from the same value
pool a DIMmed record uses, a field read refreshes its slot from host memory
first, and a write converts back and stores. So the script always sees current
values and its writes always land, while the rest of the interpreter goes on
seeing one storage model instead of two.

AKBASIC_HOST_FIELD takes the offset and the width from the same member, which is
the only reason it is a macro: writing offsetof and sizeof out by hand is two
chances to name the wrong member and no way to notice. A field name's suffix
must agree with the C type it describes, refused at registration -- a host
writing "HP%" over an int32_t has said two different things about one field and
the script would believe the suffix.

Conversion refuses rather than truncates. 200 into an int8_t, 70000 into an
int16_t, -1 into a uint8_t and thirteen characters into a char[8] are each an
error naming the field, because a silent wrap is found three frames later in
code that did nothing wrong. Each width is tested separately, since a range
check is exactly the thing that is right for int32_t and wrong for int8_t when
only one of them is covered.

The language's own distinction turns out to be the one a host needs, so there is
one API rather than two: assignment copies and gives a script a private
snapshot, POINT shares and lets it change the game, and which one happened is
visible in the listing.

Two things the work required. The prescan runs again on every RUN and used to
wipe the whole type table, unregistering the host's types the first time a
script ran; it now keeps what the host registered and drops only what the script
declared. And akbasic_runtime_start() did not rewind, which cost nothing while a
host started a script once and cost everything to a host running one per enemy
-- the second start began past the end and silently did nothing.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Co-Authored-By: Andrew Kesterson <andrew@aklabs.net>
This commit is contained in:
2026-08-01 11:56:04 -04:00
parent c83ffa9a3b
commit cd038a55a2
13 changed files with 1068 additions and 7 deletions

View File

@@ -372,8 +372,9 @@ static akerr_ErrorContext *assign_field(akbasic_Environment *obj, akbasic_ASTLea
PREPARE_ERROR(errctx);
akbasic_StructField *field = NULL;
akbasic_Value *slot = NULL;
void *hostbase = NULL;
PASS(errctx, akbasic_struct_resolve(obj->runtime, lval, &field, &slot));
PASS(errctx, akbasic_struct_resolve(obj->runtime, lval, &field, &slot, &hostbase));
switch ( field->kind ) {
case AKBASIC_FIELD_STRUCT:
@@ -413,6 +414,16 @@ static akerr_ErrorContext *assign_field(akbasic_Environment *obj, akbasic_ASTLea
PASS(errctx, akbasic_value_clone(rval, slot));
break;
}
/*
* And straight back into the host's own memory, so `FOE@.HP# = 0` changes
* the game's enemy rather than a copy of it. The conversion refuses what
* will not fit -- an int16_t field handed 70000 is an error naming the
* field, because a silent wrap is found three frames later in code that did
* nothing wrong.
*/
if ( hostbase != NULL && field->kind == AKBASIC_FIELD_PRIMITIVE ) {
PASS(errctx, akbasic_host_write_field(obj->runtime, field, hostbase, slot));
}
*dest = slot;
SUCCEED_RETURN(errctx);
}