Files
neoLegacy-mac/tests/support/as3vm_create_test_support.c
2026-07-18 02:10:53 -05:00

246 lines
8.9 KiB
C

/*
* Host harness for the reconstructed as3vm_create translation unit.
*
* as3vm_create is Iggy's AS3 / AVM2 GC-managed value construction layer: it
* boxes int / uint / number / namespace / string / array / object / closure
* values, builds arraydata + stringdata backing buffers, mints interned +
* permanent strings, and lays down object headers with traits / proto /
* constructor slots. It exports 31 entry points and owns NO externally named
* data global -- every table it touches (the built-in class prototypes at
* vm+0x2830 / 0x2850 / 0x2868 / 0x28c8, the GC root stack at vm+0x6b18, the
* class-name qnames at vm+0xda8 / 0xdc0) lives INSIDE the caller's VM context,
* not in this TU (verified: nm shows no B/D/C data symbols on any of the four
* retail objects; the durango R constants are read-only float literals).
*
* The value-tagging scheme (invariant across all four platforms, cross-checked
* durango 64-bit vs vita 32-bit): a small integer boxes to (value << 1) with a
* clear low bit; a heap value is (node_pointer + 1) with a set low bit, where
* node is 16-byte aligned so the +1 tag is unambiguous. node[0] is the kind
* byte (7 number, 8 string, 1 stringdata, 0xc namespace, 0x12 method closure,
* 0x14 function closure, 0x20 array, 0x23 object-with-traits, 0x25
* object-with-proto, 0x26 class closure).
*
* The only symbols that have to be supplied from outside are the genuine nm
* ' U ' imports. This DURANGO harness provides the full genuine import set
* (identical real-Iggy set on all four platforms; per-platform libc/libm
* extras -- floor, strlen -- differ, see the cross test):
*
* - GC allocator (as3vm_gc / as3vm_interp, NOT yet reconstructed -- expected
* unbuilt imports): as3vm_gc_alloc, as3vm_gc_alloc_remembered
* -> functional bump arena returning zeroed 16-aligned buffers so the
* makers actually run; the requested word count is recorded.
* - write barrier (as3vm_gc, unbuilt): as3vm_write_barrier_slowpath -> record.
* - finalizer / property naming (as3vm_gc / as3vm_class, unbuilt):
* as3vm_register_finalizer, as3vm_set_property_name_GC,
* as3vm_qname_set_dont_enum_GC -> record.
* - interning (as3vm_names, NOW reconstructed): as3vm_find_intern_string
* -> controllable return; records the create flag.
* - closure implementations (as3vm_interp, unbuilt): their ADDRESS is captured
* into the closure node, never called -- provided as real functions whose
* address the tests read back through an accessor.
* - null/undefined sentinel (as3vm_gc, unbuilt): as3vm_nullundef_storage is a
* DATA symbol (referenced by address, dot-unprefixed on ps3 like the other
* address-taken symbols) -- provided as a data array.
* - arena (iggy_arena_allocator, satisfied): iggy_arena_malloc_aligned
* -> functional bump arena (the non-GC permanent + static makers).
* - rrstring (satisfied): rrUTF8toUTF16 (6-arg canonical), rrwcslen -> real
* ASCII-range conversions so string makers copy real chars.
* - libc: memcpy resolves against the host and is deliberately NOT stubbed,
* matching the sibling as3vm_names / iggy_sound harness convention.
*/
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
/* ---- Counters + controllable collaborator state. ---- */
static size_t gc_alloc_count;
static size_t gc_alloc_remembered_count;
static size_t write_barrier_count;
static size_t register_finalizer_count;
static size_t set_property_name_count;
static size_t qname_set_dont_enum_count;
static size_t find_intern_string_count;
static size_t arena_malloc_aligned_count;
static size_t last_alloc_words;
static void *last_alloc_ptr;
static void *last_finalizer_ptr;
static int32_t last_intern_create;
static intptr_t intern_result;
/* Bump arena backing the alloc stubs. */
static uint8_t arena_pool[4 << 20];
static size_t arena_used;
static void *pool_alloc(size_t size, size_t align) {
if (align < 16u) {
align = 16u;
}
size_t base = (arena_used + (align - 1u)) & ~(align - 1u);
/* Pad generously: the widest maker writes node+0x38 plus a trait tail. */
size_t rounded = (size + 63u) & ~(size_t)63u;
if (rounded == 0u) {
rounded = 64u;
}
if (base + rounded > sizeof(arena_pool)) {
return 0;
}
arena_used = base + rounded;
return arena_pool + base;
}
void as3vm_create_test_reset(void) {
gc_alloc_count = 0;
gc_alloc_remembered_count = 0;
write_barrier_count = 0;
register_finalizer_count = 0;
set_property_name_count = 0;
qname_set_dont_enum_count = 0;
find_intern_string_count = 0;
arena_malloc_aligned_count = 0;
last_alloc_words = 0;
last_alloc_ptr = 0;
last_finalizer_ptr = 0;
last_intern_create = 0;
intern_result = 0;
arena_used = 0;
}
size_t as3vm_create_test_gc_alloc_count(void) { return gc_alloc_count; }
size_t as3vm_create_test_gc_alloc_remembered_count(void) {
return gc_alloc_remembered_count;
}
size_t as3vm_create_test_write_barrier_count(void) { return write_barrier_count; }
size_t as3vm_create_test_register_finalizer_count(void) {
return register_finalizer_count;
}
size_t as3vm_create_test_set_property_name_count(void) {
return set_property_name_count;
}
size_t as3vm_create_test_qname_set_dont_enum_count(void) {
return qname_set_dont_enum_count;
}
size_t as3vm_create_test_find_intern_string_count(void) {
return find_intern_string_count;
}
size_t as3vm_create_test_arena_malloc_aligned_count(void) {
return arena_malloc_aligned_count;
}
size_t as3vm_create_test_last_alloc_words(void) { return last_alloc_words; }
void *as3vm_create_test_last_alloc_ptr(void) { return last_alloc_ptr; }
void *as3vm_create_test_last_finalizer_ptr(void) { return last_finalizer_ptr; }
int32_t as3vm_create_test_last_intern_create(void) { return last_intern_create; }
void as3vm_create_test_set_intern_result(void *value) {
intern_result = (intptr_t)value;
}
/* ---- GC allocator imports (functional bump arena; size is in 8-byte words). */
void *as3vm_gc_alloc(void *vm, uint32_t words) {
(void)vm;
++gc_alloc_count;
last_alloc_words = words;
last_alloc_ptr = pool_alloc((size_t)words * 8u, 16u);
return last_alloc_ptr;
}
void *as3vm_gc_alloc_remembered(void *vm, uint32_t words) {
(void)vm;
++gc_alloc_remembered_count;
last_alloc_words = words;
last_alloc_ptr = pool_alloc((size_t)words * 8u, 16u);
return last_alloc_ptr;
}
/* ---- Write barrier (record only). ---- */
void as3vm_write_barrier_slowpath(void *vm, void *node, void *target) {
(void)vm;
(void)node;
(void)target;
++write_barrier_count;
}
/* ---- Finalizer + property-naming collaborators (record only). ---- */
void as3vm_register_finalizer(void *vm, void *object) {
(void)vm;
last_finalizer_ptr = object;
++register_finalizer_count;
}
void as3vm_set_property_name_GC(void *vm, void *name, void *qname, void *value,
int32_t flag) {
(void)vm;
(void)name;
(void)qname;
(void)value;
(void)flag;
++set_property_name_count;
}
void as3vm_qname_set_dont_enum_GC(void *vm, void *name, void *qname) {
(void)vm;
(void)name;
(void)qname;
++qname_set_dont_enum_count;
}
/* ---- Interning import (controllable; drives make_interned_string_c). ---- */
void *as3vm_find_intern_string(void *vm, size_t units, void *chars,
int32_t create) {
(void)vm;
(void)units;
(void)chars;
last_intern_create = create;
++find_intern_string_count;
return (void *)intern_result;
}
/* ---- Closure implementations: address captured, never called. ---- */
void as3vm_functionclosure_implementation(void) {}
void as3vm_methodclosure_implementation(void) {}
void *as3vm_create_test_functionclosure_impl_addr(void) {
return (void *)&as3vm_functionclosure_implementation;
}
void *as3vm_create_test_methodclosure_impl_addr(void) {
return (void *)&as3vm_methodclosure_implementation;
}
/* ---- null/undefined sentinel: DATA symbol referenced by address. ---- */
uint8_t as3vm_nullundef_storage[64];
/* ---- Arena allocator import (non-GC permanent + static makers). ---- */
void *iggy_arena_malloc_aligned(void *player, void *heap, uint32_t size,
int32_t align, int32_t tag) {
(void)player;
(void)heap;
(void)tag;
++arena_malloc_aligned_count;
last_alloc_ptr = pool_alloc(size, (size_t)(align > 0 ? align : 16));
return last_alloc_ptr;
}
/* ---- rrstring imports (satisfied; real ASCII-range conversions). ---- */
int32_t rrUTF8toUTF16(uint16_t *destination, int32_t destination_units,
const uint8_t *source, int32_t source_bytes,
int32_t replacement, int32_t truncate) {
(void)replacement;
(void)truncate;
int32_t count = 0;
for (int32_t i = 0;
(source_bytes < 0 ? source[i] != 0 : i < source_bytes) &&
count < destination_units;
++i) {
destination[count++] = (uint16_t)source[i];
}
return count;
}
int32_t rrwcslen(const uint16_t *text) {
int32_t length = 0;
while (text[length] != 0) {
++length;
}
return length;
}