simplify gc model

This commit is contained in:
2026-02-13 02:33:25 -06:00
parent e346348eb5
commit 30a9cfee79
4 changed files with 162 additions and 292 deletions

View File

@@ -361,13 +361,7 @@ struct JSClass {
#define JS_MODE_BACKTRACE_BARRIER \
(1 << 3) /* stop backtrace before this frame */
typedef struct JSFrameRegister {
objhdr_t hdr; // capacity in this is the total number of words of the object, including the 4 words of overhead and all slots
JSValue function; // JSFunction, function object being invoked
JSValue caller; // JSFrameRegister, the frame that called this one
JSValue address; // address of the instruction in the code that should be executed upon return
JSValue slots[]; // inline memory. order is [this][input args][closed over vars][non closed over vars][temporaries]
} JSFrameRegister; /// extra note: when this frame returns, caller should be set to 0. If caller is found to be 0, then the GC can reduce this frame's slots down to [this][input_args][closed over vars]; if no closed over vars it can be totally removed; may happen naturally in GC since it would have no refs?
/* JSFrameRegister is now an alias for JSFrame — see JSFrame definition below */
/* ============================================================
Register-Based VM Data Structures
@@ -823,17 +817,18 @@ typedef struct JSCodeRegister {
} JSCodeRegister;
/* Frame for closures - used by link-time relocation model where closures
reference outer frames via (depth, slot) addressing.
Stores function as JSValue to survive GC movements. */
/* Unified frame struct — used by the register VM and closures.
All fields are JSValues so the GC can scan them uniformly. */
typedef struct JSFrame {
objhdr_t header; /* OBJ_FRAME, cap56 = slot count */
JSValue function; /* JSValue for GC safety (use JS_VALUE_GET_FUNCTION) */
JSValue caller; /* JSValue for GC safety (unused currently) */
uint32_t return_pc;
JSValue function; /* JSFunction, function object being invoked */
JSValue caller; /* JSFrame, the frame that called this one */
JSValue address; /* return PC stored as JS_NewInt32 */
JSValue slots[]; /* [this][args][captured][locals][temps] */
} JSFrame;
typedef JSFrame JSFrameRegister;
static inline objhdr_t objhdr_set_s (objhdr_t h, bool s) {
return s ? (h | OBJHDR_S_MASK) : (h & ~OBJHDR_S_MASK);
}
@@ -903,7 +898,8 @@ typedef struct JSArray {
JSValue values[]; /* inline flexible array member */
} JSArray;
/* JSBlob - binary data per memory.md */
/* JSBlob — not allocated on GC heap (blobs use JSRecord + opaque).
Struct kept for reference; gc_object_size/gc_scan_object do not handle OBJ_BLOB. */
typedef struct JSBlob {
objhdr_t mist_hdr;
word_t length;
@@ -926,7 +922,7 @@ typedef slot JSRecordEntry;
typedef struct JSRecord {
objhdr_t mist_hdr;
struct JSRecord *proto;
JSValue proto; /* prototype as JSValue (JS_NULL if none) */
word_t len; /* number of entries */
slot slots[]; /* slots[0] reserved: key low32=class_id, key high32=rec_id, val=opaque */
} JSRecord;
@@ -1071,6 +1067,13 @@ static JS_BOOL JSText_equal_ascii (const JSText *text, JSValue imm) {
enough to call the interrupt callback often. */
#define JS_INTERRUPT_COUNTER_INIT 10000
/* Auto-rooted C call argv — GC updates values in-place */
typedef struct CCallRoot {
JSValue *argv; /* points to C-stack-local array */
int argc;
struct CCallRoot *prev; /* stack for nesting (C -> JS -> C -> ...) */
} CCallRoot;
struct JSContext {
JSRuntime *rt;
@@ -1102,6 +1105,7 @@ struct JSContext {
JSGCRef *top_gc_ref; /* used to reference temporary GC roots (stack top) */
JSGCRef *last_gc_ref; /* used to reference temporary GC roots (list) */
CCallRoot *c_call_root; /* stack of auto-rooted C call argv arrays */
int class_count; /* size of class_array and class_proto */
JSClass *class_array;
@@ -1234,7 +1238,7 @@ typedef struct JSRegExp {
/* Get prototype from object (works for both JSRecord and JSRecord since they
* share layout) */
#define JS_OBJ_GET_PROTO(p) ((JSRecord *)((JSRecord *)(p))->proto)
#define JS_OBJ_GET_PROTO(p) (JS_IsNull(((JSRecord *)(p))->proto) ? NULL : (JSRecord *)JS_VALUE_GET_PTR(((JSRecord *)(p))->proto))
/* Initial capacity for new records (mask = 7, 8 slots total) */
#define JS_RECORD_INITIAL_MASK 7