Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 7c3e4e3081 |
@@ -105,6 +105,8 @@ 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.
|
||||
@@ -112,6 +114,7 @@ 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);
|
||||
|
||||
@@ -118,12 +118,6 @@ typedef struct akbasic_Runtime
|
||||
{
|
||||
akbasic_SourceLine source[AKBASIC_MAX_SOURCE_LINES];
|
||||
|
||||
/* Scratch owned by this runtime for RENUMBER and its target prescan. */
|
||||
int16_t renumber_map[AKBASIC_MAX_SOURCE_LINES];
|
||||
uint8_t renumber_visited[AKBASIC_MAX_SOURCE_LINES];
|
||||
akbasic_SourceLine renumber_line;
|
||||
char renumber_discard[AKBASIC_MAX_LINE_LENGTH * 2];
|
||||
|
||||
/* Pools. Nothing here is malloc'd; everything is drawn from and returned. */
|
||||
akbasic_Environment environments[AKBASIC_MAX_ENVIRONMENTS];
|
||||
akbasic_Variable variables[AKBASIC_MAX_VARIABLES];
|
||||
|
||||
16
src/host.c
16
src/host.c
@@ -235,16 +235,9 @@ 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)));
|
||||
/*
|
||||
* 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);
|
||||
/* 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));
|
||||
dest->used = true;
|
||||
dest->ishost = true;
|
||||
dest->hostsize = type->size;
|
||||
@@ -270,8 +263,7 @@ 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));
|
||||
|
||||
/* Same silent truncation as the type name above, and the same fix. */
|
||||
snprintf(field->name, sizeof(field->name), "%s", src->name);
|
||||
PASS(errctx, aksl_strcpy(field->name, sizeof(field->name), src->name));
|
||||
field->hostkind = src->kind;
|
||||
field->hostoffset = src->offset;
|
||||
field->hostwidth = src->width;
|
||||
|
||||
@@ -72,7 +72,7 @@ struct akbasic_TargetWalk
|
||||
* rewritten: `GOTO 9999` in a program with no line 9999 is already broken, and
|
||||
* inventing a destination for it would hide that.
|
||||
*/
|
||||
static int64_t mapped(const int16_t *map, int64_t line)
|
||||
static int64_t mapped(const int64_t *map, int64_t line)
|
||||
{
|
||||
if ( line < 0 || line >= AKBASIC_MAX_SOURCE_LINES ) {
|
||||
return line;
|
||||
@@ -306,7 +306,7 @@ static akerr_ErrorContext *rewrite_line(akbasic_TargetWalk *walk, const char *co
|
||||
static akerr_ErrorContext *visit_renumber(akbasic_TargetWalk *walk, int64_t target, char *dest, size_t len)
|
||||
{
|
||||
PREPARE_ERROR(errctx);
|
||||
const int16_t *map = (const int16_t *)walk->self;
|
||||
const int64_t *map = (const int64_t *)walk->self;
|
||||
int written = 0;
|
||||
|
||||
PASS(errctx, aksl_snprintf(&written, dest, len, "%" PRId64, mapped(map, target)));
|
||||
@@ -316,7 +316,8 @@ static akerr_ErrorContext *visit_renumber(akbasic_TargetWalk *walk, int64_t targ
|
||||
akerr_ErrorContext *akbasic_renumber(akbasic_Runtime *obj, int64_t newstart, int64_t increment, int64_t oldstart)
|
||||
{
|
||||
PREPARE_ERROR(errctx);
|
||||
int16_t *map = obj == NULL ? NULL : obj->renumber_map;
|
||||
static int64_t map[AKBASIC_MAX_SOURCE_LINES];
|
||||
static akbasic_SourceLine rewritten[AKBASIC_MAX_SOURCE_LINES];
|
||||
akbasic_TargetWalk walk = { map, visit_renumber };
|
||||
int64_t next = newstart;
|
||||
int64_t i = 0;
|
||||
@@ -355,8 +356,10 @@ akerr_ErrorContext *akbasic_renumber(akbasic_Runtime *obj, int64_t newstart, int
|
||||
next += increment;
|
||||
}
|
||||
|
||||
PASS(errctx, aksl_memset(obj->renumber_visited, 0, sizeof(obj->renumber_visited)));
|
||||
PASS(errctx, aksl_memset(rewritten, 0, sizeof(rewritten)));
|
||||
for ( i = 0; i < AKBASIC_MAX_SOURCE_LINES; i++ ) {
|
||||
int64_t target = 0;
|
||||
|
||||
if ( obj->source[i].code[0] == '\0' ) {
|
||||
continue;
|
||||
}
|
||||
@@ -364,62 +367,20 @@ akerr_ErrorContext *akbasic_renumber(akbasic_Runtime *obj, int64_t newstart, int
|
||||
* Every line is rewritten, not just the moved ones: a line before
|
||||
* `oldstart` can branch into the region that moved.
|
||||
*/
|
||||
target = mapped(map, i);
|
||||
PASS(errctx, rewrite_line(&walk, obj->source[i].code,
|
||||
obj->renumber_line.code, sizeof(obj->renumber_line.code)));
|
||||
obj->renumber_line.lineno = i;
|
||||
rewritten[target].code, sizeof(rewritten[target].code)));
|
||||
rewritten[target].lineno = target;
|
||||
/*
|
||||
* Every line comes out numbered, whether or not it went in that way.
|
||||
* Asking for numbers is what RENUMBER is, and a program that has been
|
||||
* through it can be branched into by number -- which is the whole point
|
||||
* of running it over source that arrived without any.
|
||||
*/
|
||||
obj->renumber_line.numbered = true;
|
||||
PASS(errctx, aksl_memcpy(&obj->source[i], &obj->renumber_line,
|
||||
sizeof(obj->source[i])));
|
||||
rewritten[target].numbered = true;
|
||||
}
|
||||
|
||||
/* Move the already-rewritten lines in place. The map is a partial
|
||||
* permutation: a chain ends at an empty slot, while a cycle closes back
|
||||
* on its starting line. A single displaced line is sufficient for both. */
|
||||
for ( i = 0; i < AKBASIC_MAX_SOURCE_LINES; i++ ) {
|
||||
int64_t current = i;
|
||||
akbasic_SourceLine displaced;
|
||||
akbasic_SourceLine next_line;
|
||||
|
||||
if ( map[i] < 0 || obj->renumber_visited[i] ) {
|
||||
continue;
|
||||
}
|
||||
PASS(errctx, aksl_memcpy(&displaced, &obj->source[i], sizeof(displaced)));
|
||||
for ( ;; ) {
|
||||
int64_t destination = map[current];
|
||||
|
||||
obj->renumber_visited[current] = 1;
|
||||
if ( destination == i ) {
|
||||
displaced.lineno = destination;
|
||||
PASS(errctx, aksl_memcpy(&obj->source[destination], &displaced,
|
||||
sizeof(displaced)));
|
||||
break;
|
||||
}
|
||||
if ( map[destination] < 0 ) {
|
||||
displaced.lineno = destination;
|
||||
PASS(errctx, aksl_memcpy(&obj->source[destination], &displaced,
|
||||
sizeof(displaced)));
|
||||
PASS(errctx, aksl_memset(&obj->source[current], 0,
|
||||
sizeof(obj->source[current])));
|
||||
break;
|
||||
}
|
||||
PASS(errctx, aksl_memcpy(&next_line, &obj->source[destination],
|
||||
sizeof(displaced)));
|
||||
displaced.lineno = destination;
|
||||
PASS(errctx, aksl_memcpy(&obj->source[destination], &displaced,
|
||||
sizeof(obj->renumber_line)));
|
||||
PASS(errctx, aksl_memset(&obj->source[current], 0,
|
||||
sizeof(obj->source[current])));
|
||||
PASS(errctx, aksl_memcpy(&displaced, &next_line,
|
||||
sizeof(displaced)));
|
||||
current = destination;
|
||||
}
|
||||
}
|
||||
PASS(errctx, aksl_memcpy(obj->source, rewritten, sizeof(obj->source)));
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
|
||||
@@ -474,6 +435,7 @@ akerr_ErrorContext *akbasic_runtime_check_targets(akbasic_Runtime *obj)
|
||||
* step(). Nothing is read back out of it -- the walk needs somewhere to put
|
||||
* the text it would have written, and this is it.
|
||||
*/
|
||||
static char discard[AKBASIC_MAX_LINE_LENGTH * 2];
|
||||
CheckState state = { NULL, 0 };
|
||||
akbasic_TargetWalk walk = { &state, visit_check };
|
||||
int64_t entry = 0;
|
||||
@@ -501,8 +463,7 @@ akerr_ErrorContext *akbasic_runtime_check_targets(akbasic_Runtime *obj)
|
||||
* the whole point of setting it.
|
||||
*/
|
||||
obj->environment->lineno = i;
|
||||
PASS(errctx, rewrite_line(&walk, obj->source[i].code,
|
||||
obj->renumber_discard, sizeof(obj->renumber_discard)));
|
||||
PASS(errctx, rewrite_line(&walk, obj->source[i].code, discard, sizeof(discard)));
|
||||
}
|
||||
/* Nothing was refused, so leave the cursor as the caller had it. */
|
||||
obj->environment->lineno = entry;
|
||||
|
||||
@@ -208,6 +208,72 @@ 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.
|
||||
*
|
||||
@@ -258,6 +324,7 @@ 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();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user