Files
neoLegacy-mac/tests/support/as3vm_imp_string_test_support.c
2026-07-18 17:34:31 -05:00

402 lines
14 KiB
C

/*
* Host harness for the reconstructed as3vm_imp_string translation unit.
*
* as3vm_imp_string is Iggy's AS3 / AVM2 String class: charAt / charCodeAt /
* concat / fromCharCode / indexOf / lastIndexOf / localeCompare / match /
* replace / search / slice / split / substr / substring / toLocaleLowerCase /
* toLocaleUpperCase / toString / valueOf / get_length / __construct, plus the
* file-level export as3vm_stringdata_compare_caseinsensitive. It exports
* EXACTLY 21 entry points on all four platforms and -- LIKE as3vm_convert /
* as3vm_imp_math / as3vm_imp_array but UNLIKE as3vm_gc / as3vm_property -- OWNS
* NO externally named data global. (The durango object carries six MSVC 'R'
* read-only ??_C@... COMDATs -- the component-parse format strings "?d", "s?d",
* "s", "sv", "?dd", "?vd" -- which are rodata artifacts, local on the other
* three platforms, NOT owned exported data; the ps3 uppercase 'D' entries are
* .opd descriptors for the 21 exports and make_substring_GC is a module-LOCAL
* static, not an import.)
*
* STRING OBJECT / STRINGDATA MODEL (recovered from raw/durango, offsets 64-bit;
* the runtime test builds ONLY the durango dylib so the 64-bit layout is
* correct. The stringdata internal layout is INVARIANT across word sizes --
* cross-checked against the 32-bit vita raw, where length is still at
* stringdata+7 and the code units still start at stringdata+0xb; only the
* object->stringdata pointer width changes, 8 bytes on durango vs 4 on vita, and
* the interned empty-string slot moves from vm+0xe18 on durango to vm+0x948 on
* vita -- see the runtime docstring):
* - an as3value is a tagged word: (v & 3) is the tag. Tag 1 == heap cell,
* carrying a 1-byte type at v[-1] (0x08 == '\b' marks a String object).
* - a String object (tag-1, type 0x08 at obj[-1]) holds its stringdata pointer
* at obj+7 (raw/durango:25-37, 133-137).
* - a stringdata holds its length (int32) at stringdata+7 and a dense array of
* UTF-16 code units starting at stringdata+0xb; code unit [i] lives at
* stringdata+0xb+i*2 (raw:33-37, 62-68, 136-137). get_length returns
* length<<1 (the AS3 int tag); charCodeAt returns codeunit<<1.
* - the method thunk receives a four-word call frame: frame[0]=vm,
* frame[1]=&result slot, frame[2]=argc (int32 at +0x10), frame[3]=&this-slot
* (points at [this, arg0, arg1, ...]) (raw:6-42, 133-137).
* - the VM keeps a GC-visible operand stack whose top pointer lives at
* vm+0x6b18; fromCharCode / split / replace and the module-local
* make_substring_GC push values there across allocation so the collector can
* see them (raw:104-125, 856-862). The interned empty string lives at
* vm+0xe18 (raw:42/536/727/787).
*
* The symbols supplied below are exactly the genuine nm ' U ' imports of the
* retail object -- an INVARIANT real-Iggy / rr / libc set on all four platforms
* (verified identical; see the cross test). as3vm_String_toLowerCase /
* as3vm_String_toUpperCase are NOT genuinely unbuilt -- they are the plain-case
* siblings already reconstructed in iggy_unicode.c (this module's
* toLocaleLowerCase / toLocaleUpperCase tail-call them); they are modelled here
* as recording stubs only so the DURANGO dylib links in isolation.
* make_substring_GC is a module-internal static (NOT an import) so it is left to
* the module and runs with the host memcpy; memcmp / memcpy resolve against the
* host and are deliberately NOT stubbed (sibling convention).
*
* DEFERRED (needs the deep interpreter / RegExp / allocator paths, flagged
* structural in the runtime docstring, NOT exercised): the RegExp-backed
* match (raw:318-323 already throws) / the replace match-and-rebuild path
* (raw:360-421, reaches ToString / do_func_call_vv / make_string), the split
* empty-separator char split and non-empty-separator scan (raw:593-674, reach
* array_push_GC / make_substring), and the single-char search scan wart
* (raw:466-483).
*/
#include <stdarg.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
/* ---- Recording counters. ---- */
static size_t exception_count;
static size_t write_barrier_count;
static size_t make_string_count;
static size_t make_array_count;
static size_t toint32_count;
static size_t tostring_count;
static size_t parse_args_count;
static size_t array_join_count;
static size_t array_push_count;
static size_t do_func_call_count;
static size_t tolowercase_count;
static size_t touppercase_count;
static size_t compare_ci_count;
static int32_t last_exception_code;
static void *last_barrier_target;
static void *last_string;
static void *last_array;
static void *last_lower_frame;
static void *last_upper_frame;
/* rrCompareInsensitiveUTF16toUTF16 recorded arguments (stringdata_compare). */
static void *compare_ci_a;
static uint32_t compare_ci_a_len;
static void *compare_ci_b;
static uint32_t compare_ci_b_len;
/* ---- Controllable returns. ---- */
static int parse_status; /* 0 == success (default 0). */
static void *parse_ptr[8]; /* 's'/'v' outs, consumed in order. */
static int32_t parse_int[8]; /* 'd' outs, consumed in order. */
static int32_t toint32_values[16]; /* as3vm_ToInt32_GC outs, in order. */
static void *array_join_result; /* value as3vm_array_join returns. */
static int32_t compare_ci_result; /* rrCompareInsensitiveUTF16toUTF16 out. */
/* Bump pool backing the make_*_GC fakes. */
static uint8_t fake_pool[1 << 20];
static size_t fake_used;
static void *pool_alloc(size_t size) {
size_t base = (fake_used + 15u) & ~(size_t)15u;
size_t rounded = (size + 15u) & ~(size_t)15u;
if (rounded == 0u) {
rounded = 16u;
}
if (base + rounded > sizeof(fake_pool)) {
return 0;
}
fake_used = base + rounded;
return fake_pool + base;
}
void as3vm_imp_string_test_reset(void) {
exception_count = 0;
write_barrier_count = 0;
make_string_count = 0;
make_array_count = 0;
toint32_count = 0;
tostring_count = 0;
parse_args_count = 0;
array_join_count = 0;
array_push_count = 0;
do_func_call_count = 0;
tolowercase_count = 0;
touppercase_count = 0;
compare_ci_count = 0;
last_exception_code = 0;
last_barrier_target = 0;
last_string = 0;
last_array = 0;
last_lower_frame = 0;
last_upper_frame = 0;
compare_ci_a = 0;
compare_ci_a_len = 0;
compare_ci_b = 0;
compare_ci_b_len = 0;
parse_status = 0;
compare_ci_result = 0;
array_join_result = 0;
for (int i = 0; i < 8; ++i) {
parse_ptr[i] = 0;
parse_int[i] = 0;
}
for (int i = 0; i < 16; ++i) {
toint32_values[i] = 0;
}
fake_used = 0;
}
/* ---- Count / last-value accessors. ---- */
size_t as3vm_imp_string_test_exception_count(void) { return exception_count; }
size_t as3vm_imp_string_test_write_barrier_count(void) {
return write_barrier_count;
}
size_t as3vm_imp_string_test_make_string_count(void) { return make_string_count; }
size_t as3vm_imp_string_test_make_array_count(void) { return make_array_count; }
size_t as3vm_imp_string_test_toint32_count(void) { return toint32_count; }
size_t as3vm_imp_string_test_parse_args_count(void) { return parse_args_count; }
size_t as3vm_imp_string_test_array_join_count(void) { return array_join_count; }
size_t as3vm_imp_string_test_array_push_count(void) { return array_push_count; }
size_t as3vm_imp_string_test_do_func_call_count(void) {
return do_func_call_count;
}
size_t as3vm_imp_string_test_tolowercase_count(void) { return tolowercase_count; }
size_t as3vm_imp_string_test_touppercase_count(void) { return touppercase_count; }
size_t as3vm_imp_string_test_compare_ci_count(void) { return compare_ci_count; }
int32_t as3vm_imp_string_test_last_exception_code(void) {
return last_exception_code;
}
void *as3vm_imp_string_test_last_string(void) { return last_string; }
void *as3vm_imp_string_test_last_array(void) { return last_array; }
void *as3vm_imp_string_test_last_lower_frame(void) { return last_lower_frame; }
void *as3vm_imp_string_test_last_upper_frame(void) { return last_upper_frame; }
void *as3vm_imp_string_test_compare_ci_a(void) { return compare_ci_a; }
uint32_t as3vm_imp_string_test_compare_ci_a_len(void) { return compare_ci_a_len; }
void *as3vm_imp_string_test_compare_ci_b(void) { return compare_ci_b; }
uint32_t as3vm_imp_string_test_compare_ci_b_len(void) { return compare_ci_b_len; }
void *as3vm_imp_string_test_global_nan_addr(void);
void *as3vm_imp_string_test_nullundef_addr(void);
/* ---- Controls. ---- */
void as3vm_imp_string_test_set_parse_status(int v) { parse_status = v; }
void as3vm_imp_string_test_set_parse_ptr(int index, void *v) {
if (index >= 0 && index < 8) {
parse_ptr[index] = v;
}
}
void as3vm_imp_string_test_set_parse_int(int index, int32_t v) {
if (index >= 0 && index < 8) {
parse_int[index] = v;
}
}
void as3vm_imp_string_test_set_toint32(int index, int32_t v) {
if (index >= 0 && index < 16) {
toint32_values[index] = v;
}
}
void as3vm_imp_string_test_set_array_join_result(void *v) {
array_join_result = v;
}
void as3vm_imp_string_test_set_compare_ci_result(int32_t v) {
compare_ci_result = v;
}
/* ---- Exception + diagnostics (as3vm_interpreter / iggy_main, unbuilt). ---- */
void as3vm_exception_GC(void *vm, int code) {
(void)vm;
last_exception_code = code;
++exception_count;
}
/* ---- GC write barrier (as3vm_gc, satisfied elsewhere). ---- */
void as3vm_write_barrier_slowpath(void *vm, void *target, void *value) {
(void)vm;
(void)value;
last_barrier_target = target;
++write_barrier_count;
}
/*
* Argument parsing (as3vm_component_parse_arguments_GC, unbuilt) -- format
* driven, exactly matching the retail contract. The format string alphabet
* recovered from the durango rodata COMDATs is: '?' (marks the following
* specifier optional -- consumes no out arg), 's' (string -> pointer out),
* 'v' (value -> word out), 'd' (int -> int32 out). Each non-'?' specifier
* consumes one caller-supplied out pointer; 's'/'v' write the next parse_ptr[],
* 'd' writes the next parse_int[]. The caller always passes an out slot for
* every specifier (optionality only governs whether a real value was supplied),
* so a faithful reconstruction that passes these exact literals drives this stub
* regardless of its local variable widths.
*/
int as3vm_component_parse_arguments_GC(void *frame, const char *fmt, ...) {
(void)frame;
++parse_args_count;
if (parse_status != 0) {
return parse_status;
}
va_list ap;
va_start(ap, fmt);
int ptr_index = 0;
int int_index = 0;
for (const char *p = fmt; p && *p; ++p) {
if (*p == '?') {
continue;
}
void *out = va_arg(ap, void *);
if (out == 0) {
continue;
}
if (*p == 'd') {
*(int32_t *)out = (int_index < 8) ? parse_int[int_index] : 0;
++int_index;
} else { /* 's' or 'v' -> a word-sized out. */
*(void **)out = (ptr_index < 8) ? parse_ptr[ptr_index] : 0;
++ptr_index;
}
}
va_end(ap);
return 0;
}
/* ---- Type conversion (as3vm_convert; both unbuilt here). ---- */
void as3vm_ToInt32_GC(void *vm, void *value, int32_t *out) {
(void)vm;
(void)value;
if (out) {
*out = (toint32_count < 16) ? toint32_values[toint32_count] : 0;
}
++toint32_count;
}
void *as3vm_ToString_GC(void *vm) {
(void)vm;
++tostring_count;
return pool_alloc(64u);
}
/* ---- Interpreter re-entry (as3vm_interpreter, unbuilt). ---- */
void *as3vm_do_func_call_vv_GC(void *vm, void *out, void *fn, void *args,
int argc, int flag) {
(void)vm;
(void)fn;
(void)args;
(void)argc;
(void)flag;
++do_func_call_count;
if (out) {
*(void **)out = pool_alloc(64u);
}
return 0;
}
/* ---- String / array construction (as3vm_create / as3vm_imp_array). ---- */
/*
* Build a real String object so the shallow char paths (charAt / fromCharCode /
* slice / substr / substring / split char split) actually run and can be read
* back: a tag-1 cell with type 0x08 at obj[-1], its stringdata pointer at
* obj+7, length at stringdata+7, code units at stringdata+0xb. When src is
* non-null (split's per-code-unit path) the count code units are copied in.
*/
void *as3vm_make_string_wc_GC(void *vm, const void *src, int count) {
(void)vm;
++make_string_count;
int units = count < 0 ? 0 : count;
uint8_t *cell = (uint8_t *)pool_alloc(16u);
uint8_t *sd = (uint8_t *)pool_alloc(0x0bu + (size_t)units * 2u + 16u);
if (!cell || !sd) {
return 0;
}
cell[0] = 0x08;
uint8_t *obj = cell + 1;
memcpy(obj + 7, &sd, sizeof(sd));
*(int32_t *)(sd + 7) = count;
if (src && units > 0) {
memcpy(sd + 0x0b, src, (size_t)units * 2u);
}
last_string = obj;
return obj;
}
/*
* Build a real Array object with an arraydata at obj+0x2f (dense elements at
* arraydata+0x1f) so split's undefined-separator single-element path can store
* `this` and the test can read it back.
*/
void *as3vm_make_array_GC(void *vm, int a, int b) {
(void)vm;
(void)a;
(void)b;
++make_array_count;
uint8_t *cell = (uint8_t *)pool_alloc(0x40u);
uint8_t *ad = (uint8_t *)pool_alloc(0x80u);
if (!cell || !ad) {
return 0;
}
cell[0] = 0x20;
uint8_t *obj = cell + 1;
memcpy(obj + 0x2f, &ad, sizeof(ad));
last_array = obj;
return obj;
}
/* ---- imp_array helpers (as3vm_imp_array, satisfied elsewhere). ---- */
void *as3vm_array_join(void *vm, void *this_value, void *args, int argc, int a,
int b, int c, int d) {
(void)vm;
(void)this_value;
(void)args;
(void)argc;
(void)a;
(void)b;
(void)c;
(void)d;
++array_join_count;
return array_join_result;
}
void *as3vm_array_push_GC(void *vm, void *array_object, void *value) {
(void)vm;
(void)value;
++array_push_count;
return array_object;
}
/* ---- Plain-case siblings (iggy_unicode, already reconstructed). ---- */
void as3vm_String_toLowerCase(void *frame) {
last_lower_frame = frame;
++tolowercase_count;
}
void as3vm_String_toUpperCase(void *frame) {
last_upper_frame = frame;
++touppercase_count;
}
/* ---- Case-insensitive UTF-16 compare (rrstring / iggy_unicode). ---- */
int rrCompareInsensitiveUTF16toUTF16(void *a, uint32_t a_len, void *b,
uint32_t b_len) {
compare_ci_a = a;
compare_ci_a_len = a_len;
compare_ci_b = b;
compare_ci_b_len = b_len;
++compare_ci_count;
return compare_ci_result;
}
/* ---- DATA sentinels (referenced by address on every platform). ---- */
uint8_t as3vm_global_nan[64];
uint8_t as3vm_nullundef_storage[64];
void *as3vm_imp_string_test_global_nan_addr(void) { return as3vm_global_nan; }
void *as3vm_imp_string_test_nullundef_addr(void) { return as3vm_nullundef_storage; }