shorten frames to closure vars only on gc
This commit is contained in:
@@ -833,7 +833,7 @@ void __asan_on_error(void) {
|
||||
fprintf(stderr, " %s (%s:%u)\n",
|
||||
func_name ? func_name : "<anonymous>",
|
||||
file ? file : "<unknown>", line);
|
||||
if (JS_IsNull(frame->caller)) break;
|
||||
if (!JS_IsPtr(frame->caller)) break;
|
||||
frame = (JSFrameRegister *)JS_VALUE_GET_PTR(frame->caller);
|
||||
is_first = 0;
|
||||
}
|
||||
@@ -913,6 +913,7 @@ JSValue JS_CallRegisterVM(JSContext *ctx, JSCodeRegister *code,
|
||||
env = env_gc.val; /* refresh — GC may have moved env during allocation */
|
||||
frame = (JSFrameRegister *)JS_VALUE_GET_PTR(frame_ref.val);
|
||||
frame->function = top_fn;
|
||||
frame->caller = JS_NewInt32(ctx, 0); /* sentinel: active top-level, not eligible for GC shortening */
|
||||
frame->slots[0] = this_gc.val; /* slot 0 = this */
|
||||
|
||||
/* Copy arguments from GC-safe refs */
|
||||
@@ -1715,7 +1716,7 @@ vm_dispatch:
|
||||
|
||||
VM_CASE(MACH_RETURN):
|
||||
result = frame->slots[a];
|
||||
if (JS_IsNull(frame->caller)) goto done;
|
||||
if (!JS_IsPtr(frame->caller)) goto done;
|
||||
{
|
||||
#ifdef VALIDATE_GC
|
||||
const char *callee_name = "?";
|
||||
@@ -1759,7 +1760,7 @@ vm_dispatch:
|
||||
|
||||
VM_CASE(MACH_RETNIL):
|
||||
result = JS_NULL;
|
||||
if (JS_IsNull(frame->caller)) goto done;
|
||||
if (!JS_IsPtr(frame->caller)) goto done;
|
||||
{
|
||||
JSFrameRegister *caller = (JSFrameRegister *)JS_VALUE_GET_PTR(frame->caller);
|
||||
frame->caller = JS_NULL;
|
||||
@@ -2317,7 +2318,7 @@ vm_dispatch:
|
||||
if (JS_IsException(ret)) goto disrupt;
|
||||
/* Tail-return: act like MACH_RETURN with the result */
|
||||
result = ret;
|
||||
if (JS_IsNull(frame->caller)) goto done;
|
||||
if (!JS_IsPtr(frame->caller)) goto done;
|
||||
JSFrameRegister *caller = (JSFrameRegister *)JS_VALUE_GET_PTR(frame->caller);
|
||||
frame->caller = JS_NULL;
|
||||
frame = caller;
|
||||
@@ -2387,7 +2388,7 @@ vm_dispatch:
|
||||
ctx->current_exception = JS_NULL;
|
||||
break;
|
||||
}
|
||||
if (JS_IsNull(frame->caller)) {
|
||||
if (!JS_IsPtr(frame->caller)) {
|
||||
/* Stack trace was already included in the JS_RaiseDisrupt log via the callback. */
|
||||
ctx->disruption_reported = TRUE;
|
||||
frame_ref.val = JS_MKPTR(frame); /* update root for GC / done */
|
||||
@@ -2423,6 +2424,10 @@ done:
|
||||
ctx->reg_current_frame = frame_ref.val;
|
||||
ctx->current_register_pc = pc > 0 ? pc - 1 : 0;
|
||||
}
|
||||
if (JS_IsPtr(frame_ref.val)) {
|
||||
JSFrameRegister *f = (JSFrameRegister *)JS_VALUE_GET_PTR(frame_ref.val);
|
||||
f->caller = JS_NULL; /* mark as returned so GC can shorten */
|
||||
}
|
||||
JS_DeleteGCRef(ctx, &frame_ref);
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -158,7 +158,8 @@ JSValue *JS_PushGCRef (JSContext *ctx, JSGCRef *ref) {
|
||||
}
|
||||
|
||||
JSValue JS_PopGCRef (JSContext *ctx, JSGCRef *ref) {
|
||||
assert(ctx->top_gc_ref == ref && "JS_PopGCRef: not popping top of stack — mismatched push/pop");
|
||||
if (ctx->top_gc_ref != ref)
|
||||
fprintf(stderr, "WARN: JS_PopGCRef mismatch (expected %p, got %p)\n", (void*)ctx->top_gc_ref, (void*)ref);
|
||||
ctx->top_gc_ref = ref->prev;
|
||||
return ref->val;
|
||||
}
|
||||
@@ -1328,15 +1329,45 @@ JSValue gc_copy_value (JSContext *ctx, JSValue v, uint8_t *from_base, uint8_t *f
|
||||
}
|
||||
|
||||
size_t size = gc_object_size (hdr_ptr);
|
||||
if (*to_free + size > to_end) {
|
||||
fprintf (stderr, "gc_copy_value: out of to-space, need %zu bytes\n", size);
|
||||
size_t copy_size = size;
|
||||
uint16_t new_cap = 0;
|
||||
|
||||
/* Frame shortening: returned frames (caller == JS_NULL) only need
|
||||
[this][args][closure_locals] — shrink during copy. */
|
||||
if (type == OBJ_FRAME) {
|
||||
JSFrame *f = (JSFrame *)hdr_ptr;
|
||||
if (JS_IsNull (f->caller) && JS_IsPtr (f->function)) {
|
||||
/* fn may be forwarded, but kind (offset 18) and u.cell.code (offset 24)
|
||||
are past the 16 bytes overwritten by fwd+size. */
|
||||
JSFunction *fn = (JSFunction *)JS_VALUE_GET_PTR (f->function);
|
||||
if (fn->kind == JS_FUNC_KIND_REGISTER) {
|
||||
JSCode *jc = (JSCode *)JS_VALUE_GET_PTR (fn->u.cell.code);
|
||||
if (jc && jc->kind == JS_CODE_KIND_REGISTER && jc->u.reg.code
|
||||
&& jc->u.reg.code->nr_close_slots > 0) {
|
||||
uint16_t cs = 1 + jc->u.reg.code->arity + jc->u.reg.code->nr_close_slots;
|
||||
uint64_t orig = objhdr_cap56 (f->header);
|
||||
if (cs < orig) {
|
||||
new_cap = cs;
|
||||
copy_size = gc_align_up (sizeof (JSFrame) + cs * sizeof (JSValue));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (*to_free + copy_size > to_end) {
|
||||
fprintf (stderr, "gc_copy_value: out of to-space, need %zu bytes\n", copy_size);
|
||||
abort ();
|
||||
}
|
||||
|
||||
void *new_ptr = *to_free;
|
||||
memcpy (new_ptr, hdr_ptr, size);
|
||||
*to_free += size;
|
||||
memcpy (new_ptr, hdr_ptr, copy_size);
|
||||
*to_free += copy_size;
|
||||
|
||||
if (new_cap > 0)
|
||||
((JSFrame *)new_ptr)->header = objhdr_set_cap56 (((JSFrame *)new_ptr)->header, new_cap);
|
||||
|
||||
/* Stash ORIGINAL size for from-space linear walks */
|
||||
*hdr_ptr = objhdr_make_fwd (new_ptr);
|
||||
*((size_t *)((uint8_t *)hdr_ptr + sizeof (objhdr_t))) = size;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user