Files
neoLegacy-mac/tests/support/as3vm_imp_array_test_support.c

336 lines
10 KiB
C

/*
* Host harness for the reconstructed as3vm_imp_array translation unit.
*
* as3vm_imp_array is Iggy's AS3 / AVM2 Array (and Vector) class implementation:
* the push / pop / shift / unshift / splice / slice / concat / sort / sortOn /
* indexOf / lastIndexOf / join / reverse / filter / map / forEach / length
* method family, plus the low-level arraydata backing ops (as3vm_array_push_GC,
* as3vm_array_set_value_GC, as3vm_array_delete_value_GC, as3vm_array_join,
* as3vm_array_clone_GC) and the shared as3vm_sort comparator engine. It exports
* EXACTLY 52 entry points on all four platforms (23 Array_* + 23 Vector_* +
* 6 helpers) and -- LIKE as3vm_convert / as3vm_class / as3vm_create but UNLIKE
* as3vm_gc / as3vm_property -- OWNS NO externally named data global (nm shows no
* B/D/C extern data symbol on any of the four objects; the ps3 'D' entries are
* .opd function descriptors for the 52 exports, and its lowercase 'd' symbols --
* array_makeroom_GC, comparison, splice_or_unshift_GC, strict_equality -- are
* module-LOCAL statics, not owned globals).
*
* ARRAY OBJECT / ARRAYDATA MODEL (recovered from raw/durango, offsets 64-bit;
* the runtime test builds ONLY the durango dylib so 64-bit layout is correct;
* cross-checked against the 32-bit vita raw, where the same model uses obj+0x1b
* for the arraydata pointer instead of obj+0x2f -- 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] (0x20 == ' ' marks an Array object);
* an odd non-1 word (tag 3) is an inline int; 7 is the module's empty-slot /
* "hole" as3value sentinel.
* - an Array object (tag-1, type 0x20 at obj[-1]) holds its arraydata pointer
* at obj+0x2f (raw/durango:439-440, 835, 1008).
* - an arraydata holds its capacity (int32) at arraydata+3, its length (int32)
* at arraydata+7, and a dense array of 8-byte tagged as3values starting at
* arraydata+0x1f; element[i] lives at arraydata+0x1f+i*8 (raw:2703-2729).
* as3vm_Array_get_length returns length<<1 (the int tag), pop/shift decrement
* length, set_value fills [old_len .. index-1] with the 7 hole sentinel then
* writes element[index] and sets length=index+1.
* - the VM keeps a GC-visible operand stack whose top pointer lives at
* vm+0x6b18 (&DAT_00006b18 + vm); as3vm_array_push_GC and the makeroom path
* of as3vm_array_set_value_GC push the value there across the potential
* allocation so the collector can see it (raw:2681-2687, 2708-2713).
*
* 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). This DURANGO harness models them as
* functional / recording stubs so the durango dylib links and the shallow
* Array paths actually run. array_makeroom_GC is a module-internal static (NOT
* an import) so it is left to the module; the pinned paths pre-size capacity to
* avoid it. memcpy resolves against the host and is deliberately NOT stubbed
* (sibling as3vm_property / as3vm_convert convention).
*
* DEFERRED (needs the deep interpreter / allocator paths, flagged structural in
* the runtime docstring, NOT exercised): the full as3vm_sort quicksort +
* comparator dispatch (default ToString-lexicographic vs numeric vs custom
* do_func_call_vv comparator), sortOn, splice interior shifts, slice / concat /
* join allocation, and the do_func_call_vv callback family (filter / map /
* forEach / every / some) -- all reach make_arraydata / make_string /
* do_func_call_vv_GC, whose real bodies are unbuilt.
*/
#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_arraydata_count;
static size_t make_string_count;
static size_t touint32_count;
static size_t parse_args_count;
static size_t tostring_count;
static size_t do_func_call_count;
static size_t qname_find_count;
static int32_t last_exception_code;
static void *last_barrier_target;
/* ---- Controllable returns. ---- */
static uint32_t touint32_result; /* value as3vm_ToUInt32_GC writes out. */
static int touint32_status; /* nonzero == success (default 1). */
static void *parse_args_search; /* searchval component_parse writes. */
static uint32_t parse_args_from; /* fromIndex component_parse writes. */
static int parse_args_status; /* 0 == success (default 0). */
/* 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_array_test_reset(void) {
exception_count = 0;
write_barrier_count = 0;
make_arraydata_count = 0;
make_string_count = 0;
touint32_count = 0;
parse_args_count = 0;
tostring_count = 0;
do_func_call_count = 0;
qname_find_count = 0;
last_exception_code = 0;
last_barrier_target = 0;
touint32_result = 0;
touint32_status = 1;
parse_args_search = 0;
parse_args_from = 0;
parse_args_status = 0;
fake_used = 0;
}
/* ---- Count / last-value accessors. ---- */
size_t as3vm_imp_array_test_exception_count(void) { return exception_count; }
size_t as3vm_imp_array_test_write_barrier_count(void) {
return write_barrier_count;
}
size_t as3vm_imp_array_test_make_arraydata_count(void) {
return make_arraydata_count;
}
size_t as3vm_imp_array_test_touint32_count(void) { return touint32_count; }
size_t as3vm_imp_array_test_parse_args_count(void) { return parse_args_count; }
size_t as3vm_imp_array_test_tostring_count(void) { return tostring_count; }
size_t as3vm_imp_array_test_do_func_call_count(void) {
return do_func_call_count;
}
int32_t as3vm_imp_array_test_last_exception_code(void) {
return last_exception_code;
}
void *as3vm_imp_array_test_last_barrier_target(void) {
return last_barrier_target;
}
/* ---- Controls. ---- */
void as3vm_imp_array_test_set_touint32_result(uint32_t v) {
touint32_result = v;
}
void as3vm_imp_array_test_set_touint32_status(int v) { touint32_status = v; }
void as3vm_imp_array_test_set_parse_args_search(void *v) {
parse_args_search = v;
}
void as3vm_imp_array_test_set_parse_args_from(uint32_t v) {
parse_args_from = v;
}
void as3vm_imp_array_test_set_parse_args_status(int v) {
parse_args_status = v;
}
/* ---- Exception + diagnostics (iggy_main / as3vm_exception, 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;
}
/* ---- Type conversion (as3vm_convert; ToUInt32 satisfied, rest unbuilt). ---- */
int as3vm_ToUInt32_GC(void *vm, void *value, uint32_t *out) {
(void)vm;
(void)value;
++touint32_count;
if (out) {
*out = touint32_result;
}
return touint32_status;
}
void *as3vm_ToString_GC(void *vm) {
(void)vm;
++tostring_count;
return pool_alloc(64u);
}
void *as3vm_ToNumber_GC(void *vm, void *value) {
(void)vm;
(void)value;
return pool_alloc(64u);
}
void *as3vm_ToNumberDirect_GC(void *vm, void *value) {
(void)vm;
(void)value;
return pool_alloc(64u);
}
void *as3vm_ToCharArray_GC(void *vm, void *value, void *scratch, void *out) {
(void)vm;
(void)value;
(void)scratch;
(void)out;
return 0;
}
void *as3vm_coerce_GC(void *vm, void *value, void *type) {
(void)vm;
(void)value;
(void)type;
return 0;
}
void *as3vm_convert_array_to_vector(void *vm, void *value, void *type) {
(void)vm;
(void)value;
(void)type;
return 0;
}
int as3vm_to_boolean_direct(void *value) {
(void)value;
return 0;
}
/* ---- Argument parsing (as3vm_component, unbuilt). ---- */
int as3vm_component_parse_arguments_GC(void *frame, void *fmt, void **out_a,
uint32_t *out_b) {
(void)frame;
(void)fmt;
++parse_args_count;
if (out_a) {
*out_a = parse_args_search;
}
if (out_b) {
out_b[0] = parse_args_from;
}
return parse_args_status;
}
/* ---- Interpreter re-entry (as3vm_interpreter, unbuilt). ---- */
void *as3vm_do_func_call_vv_GC(void *vm, void *fn, void *a, void *b) {
(void)vm;
(void)fn;
(void)a;
(void)b;
++do_func_call_count;
return 0;
}
void *as3vm_get_property_qname_GC(void *vm, void *object, void *qname) {
(void)vm;
(void)object;
(void)qname;
return 0;
}
/* ---- Naming / interning (as3vm_names satisfied + unbuilt). ---- */
intptr_t as3vm_qname_find(void *vm, void *name, void *table, int flags) {
(void)vm;
(void)name;
(void)table;
(void)flags;
++qname_find_count;
return 0;
}
void *as3vm_intern_string_value(void *vm, void *value) {
(void)vm;
(void)value;
return 0;
}
int as3vm_stringdata_compare_caseinsensitive(void *a, void *b) {
(void)a;
(void)b;
return 0;
}
/* ---- Value construction (as3vm_create, unbuilt) -- pool fakes. ---- */
void *as3vm_make_arraydata_GC(void *vm, int count, int flag) {
(void)vm;
(void)count;
(void)flag;
++make_arraydata_count;
return pool_alloc(4096u);
}
void *as3vm_make_array_GC(void *vm, int count) {
(void)vm;
(void)count;
return pool_alloc(256u);
}
void *as3vm_make_array_from_arraydata_GC(void *vm, void *arraydata) {
(void)vm;
(void)arraydata;
return pool_alloc(256u);
}
void *as3vm_make_string_GC(void *vm, void *stringdata) {
(void)vm;
(void)stringdata;
++make_string_count;
return pool_alloc(64u);
}
void *as3vm_make_stringdata_GC(void *vm, void *bytes, size_t length) {
(void)vm;
(void)bytes;
(void)length;
return pool_alloc(length + 64u);
}
/* ---- Allocator (iggy_allocation, satisfied) -- recording fakes. ---- */
void *iggy_malloc_fileline(void *heap, size_t size, void *file, int line) {
(void)heap;
(void)file;
(void)line;
return pool_alloc(size);
}
void iggy_free_alloc(void *heap, void *ptr) {
(void)heap;
(void)ptr;
}
/* ---- Float classification (rrMath) -- only the indexOf deep path uses it. ---- */
int rr_isnan(double value) {
return value != value;
}
/* ---- DATA sentinels (referenced by address on every platform). ---- */
uint8_t as3vm_nullundef_storage[64];
uint8_t as3vm_truefalse_storage[64];