From 154a767c0ec4f1f7b93e78df5757713f26c86545 Mon Sep 17 00:00:00 2001 From: John Alanbrook Date: Sat, 21 Feb 2026 03:07:55 -0600 Subject: [PATCH] fix gc error --- aseprite.c | 16 +++++++++++----- dxt.c | 6 ++++-- gif.c | 10 +++++++--- jpg.c | 12 ++++++++---- png.c | 12 ++++++++---- psd.c | 6 ++++-- qoi.c | 12 ++++++++---- resize.c | 6 ++++-- stb_image_common.h | 12 ++++++++---- tga.c | 12 ++++++++---- 10 files changed, 70 insertions(+), 34 deletions(-) diff --git a/aseprite.c b/aseprite.c index 611a130..fd951bc 100644 --- a/aseprite.c +++ b/aseprite.c @@ -21,14 +21,16 @@ static JSValue frame_to_js(JSContext *js, ase_frame_t *frame, int width, int hei JS_ROOT(obj, JS_NewObject(js)); JS_SetPropertyStr(js, obj.val, "width", JS_NewInt32(js, width)); JS_SetPropertyStr(js, obj.val, "height", JS_NewInt32(js, height)); - JS_SetPropertyStr(js, obj.val, "format", JS_NewString(js, "rgba32")); + JSValue fmt_str = JS_NewString(js, "rgba32"); + JS_SetPropertyStr(js, obj.val, "format", fmt_str); JS_SetPropertyStr(js, obj.val, "pitch", JS_NewInt32(js, width * 4)); JS_SetPropertyStr(js, obj.val, "depth", JS_NewInt32(js, 8)); JS_SetPropertyStr(js, obj.val, "hdr", JS_NewBool(js, 0)); // Copy pixel data size_t pixels_size = width * height * 4; - JS_SetPropertyStr(js, obj.val, "pixels", js_new_blob_stoned_copy(js, frame->pixels, pixels_size)); + JSValue pixels_blob = js_new_blob_stoned_copy(js, frame->pixels, pixels_size); + JS_SetPropertyStr(js, obj.val, "pixels", pixels_blob); // Add frame duration (in milliseconds) JS_SetPropertyStr(js, obj.val, "duration", JS_NewInt32(js, frame->duration_milliseconds)); @@ -66,8 +68,10 @@ JSValue js_aseprite_decode(JSContext *js, JSValue this_val, int argc, JSValueCon // Add palette if present if (ase->palette.entry_count > 0) { JS_ROOT(palette_array, JS_NewArray(js)); + JSGCRef color = { .val = JS_NULL, .prev = NULL }; + JS_PushGCRef(js, &color); for (int i = 0; i < ase->palette.entry_count; i++) { - JS_ROOT(color, JS_NewObject(js)); + color.val = JS_NewObject(js); JS_SetPropertyStr(js, color.val, "r", JS_NewUint32(js, ase->palette.entries[i].color.r)); JS_SetPropertyStr(js, color.val, "g", JS_NewUint32(js, ase->palette.entries[i].color.g)); JS_SetPropertyStr(js, color.val, "b", JS_NewUint32(js, ase->palette.entries[i].color.b)); @@ -223,9 +227,11 @@ JSValue js_aseprite_encode(JSContext *js, JSValue this_val, int argc, JSValueCon JS_SetPropertyStr(js, result.val, "width", JS_NewInt32(js, width)); JS_SetPropertyStr(js, result.val, "height", JS_NewInt32(js, height)); JS_SetPropertyStr(js, result.val, "frame_count", JS_NewInt32(js, frame_count)); - JS_SetPropertyStr(js, result.val, "format", JS_NewString(js, "aseprite")); + JSValue enc_fmt = JS_NewString(js, "aseprite"); + JS_SetPropertyStr(js, result.val, "format", enc_fmt); - JS_SetPropertyStr(js, result.val, "pixels", js_new_blob_stoned_copy(js, out_data, out_len)); + JSValue encode_blob = js_new_blob_stoned_copy(js, out_data, out_len); + JS_SetPropertyStr(js, result.val, "pixels", encode_blob); free(out_data); JS_RETURN(result.val); diff --git a/dxt.c b/dxt.c index 9be2dac..508b182 100644 --- a/dxt.c +++ b/dxt.c @@ -119,9 +119,11 @@ JSValue js_dxt_encode(JSContext *js, JSValue this_val, int argc, JSValueConst *a JS_ROOT(result, JS_NewObject(js)); JS_SetPropertyStr(js, result.val, "width", JS_NewInt32(js, width)); JS_SetPropertyStr(js, result.val, "height", JS_NewInt32(js, height)); - JS_SetPropertyStr(js, result.val, "format", JS_NewString(js, get_dxt_format_string(compression_type))); + JSValue fmt_str = JS_NewString(js, get_dxt_format_string(compression_type)); + JS_SetPropertyStr(js, result.val, "format", fmt_str); JS_SetPropertyStr(js, result.val, "compression", JS_NewInt32(js, compression_type)); - JS_SetPropertyStr(js, result.val, "pixels", js_new_blob_stoned_copy(js, output, output_size)); + JSValue pixels_blob = js_new_blob_stoned_copy(js, output, output_size); + JS_SetPropertyStr(js, result.val, "pixels", pixels_blob); free(output); JS_RETURN(result.val); diff --git a/gif.c b/gif.c index 1e52914..b95a64b 100644 --- a/gif.c +++ b/gif.c @@ -36,15 +36,19 @@ JSValue js_gif_decode(JSContext *js, JSValue this_val, int argc, JSValueConst *a JS_FRAME(js); JS_ROOT(frames_array, JS_NewArray(js)); + JSGCRef frame = { .val = JS_NULL, .prev = NULL }; + JS_PushGCRef(js, &frame); for (int i = 0; i < frames_count; i++) { - JS_ROOT(frame, JS_NewObject(js)); + frame.val = JS_NewObject(js); JS_SetPropertyStr(js, frame.val, "width", JS_NewInt32(js, width)); JS_SetPropertyStr(js, frame.val, "height", JS_NewInt32(js, height)); - JS_SetPropertyStr(js, frame.val, "format", JS_NewString(js, "rgba32")); + JSValue fmt_str = JS_NewString(js, "rgba32"); + JS_SetPropertyStr(js, frame.val, "format", fmt_str); JS_SetPropertyStr(js, frame.val, "pitch", JS_NewInt32(js, pitch)); void *frame_pixels = (unsigned char*)data + (frame_size * i); - JS_SetPropertyStr(js, frame.val, "pixels", js_new_blob_stoned_copy(js, frame_pixels, frame_size)); + JSValue pixels_blob = js_new_blob_stoned_copy(js, frame_pixels, frame_size); + JS_SetPropertyStr(js, frame.val, "pixels", pixels_blob); JS_SetPropertyStr(js, frame.val, "depth", JS_NewInt32(js, 8)); JS_SetPropertyStr(js, frame.val, "hdr", JS_NewBool(js, 0)); diff --git a/jpg.c b/jpg.c index b472ab8..77e9dfa 100644 --- a/jpg.c +++ b/jpg.c @@ -29,9 +29,11 @@ JSValue js_jpg_decode(JSContext *js, JSValue this_val, int argc, JSValueConst *a JS_ROOT(obj, JS_NewObject(js)); JS_SetPropertyStr(js, obj.val, "width", JS_NewInt32(js, width)); JS_SetPropertyStr(js, obj.val, "height", JS_NewInt32(js, height)); - JS_SetPropertyStr(js, obj.val, "format", JS_NewString(js, "rgba32")); + JSValue fmt_str = JS_NewString(js, "rgba32"); + JS_SetPropertyStr(js, obj.val, "format", fmt_str); JS_SetPropertyStr(js, obj.val, "pitch", JS_NewInt32(js, pitch)); - JS_SetPropertyStr(js, obj.val, "pixels", js_new_blob_stoned_copy(js, data, pixels_size)); + JSValue pixels_blob = js_new_blob_stoned_copy(js, data, pixels_size); + JS_SetPropertyStr(js, obj.val, "pixels", pixels_blob); JS_SetPropertyStr(js, obj.val, "depth", JS_NewInt32(js, 8)); JS_SetPropertyStr(js, obj.val, "hdr", JS_NewBool(js, 0)); @@ -105,8 +107,10 @@ JSValue js_jpg_encode(JSContext *js, JSValue this_val, int argc, JSValueConst *a JS_ROOT(result, JS_NewObject(js)); JS_SetPropertyStr(js, result.val, "width", JS_NewInt32(js, width)); JS_SetPropertyStr(js, result.val, "height", JS_NewInt32(js, height)); - JS_SetPropertyStr(js, result.val, "format", JS_NewString(js, "jpg")); - JS_SetPropertyStr(js, result.val, "pixels", js_new_blob_stoned_copy(js, buf.data, buf.size)); + JSValue enc_fmt = JS_NewString(js, "jpg"); + JS_SetPropertyStr(js, result.val, "format", enc_fmt); + JSValue enc_blob = js_new_blob_stoned_copy(js, buf.data, buf.size); + JS_SetPropertyStr(js, result.val, "pixels", enc_blob); free(buf.data); JS_RETURN(result.val); diff --git a/png.c b/png.c index 215968c..58ff164 100644 --- a/png.c +++ b/png.c @@ -29,9 +29,11 @@ JSValue js_png_decode(JSContext *js, JSValue this_val, int argc, JSValueConst *a JS_ROOT(obj, JS_NewObject(js)); JS_SetPropertyStr(js, obj.val, "width", JS_NewInt32(js, width)); JS_SetPropertyStr(js, obj.val, "height", JS_NewInt32(js, height)); - JS_SetPropertyStr(js, obj.val, "format", JS_NewString(js, "rgba32")); + JSValue fmt_str = JS_NewString(js, "rgba32"); + JS_SetPropertyStr(js, obj.val, "format", fmt_str); JS_SetPropertyStr(js, obj.val, "pitch", JS_NewInt32(js, pitch)); - JS_SetPropertyStr(js, obj.val, "pixels", js_new_blob_stoned_copy(js, data, pixels_size)); + JSValue pixels_blob = js_new_blob_stoned_copy(js, data, pixels_size); + JS_SetPropertyStr(js, obj.val, "pixels", pixels_blob); JS_SetPropertyStr(js, obj.val, "depth", JS_NewInt32(js, 8)); JS_SetPropertyStr(js, obj.val, "hdr", JS_NewBool(js, 0)); @@ -89,8 +91,10 @@ JSValue js_png_encode(JSContext *js, JSValue this_val, int argc, JSValueConst *a JS_ROOT(result, JS_NewObject(js)); JS_SetPropertyStr(js, result.val, "width", JS_NewInt32(js, width)); JS_SetPropertyStr(js, result.val, "height", JS_NewInt32(js, height)); - JS_SetPropertyStr(js, result.val, "format", JS_NewString(js, "png")); - JS_SetPropertyStr(js, result.val, "pixels", js_new_blob_stoned_copy(js, buf.data, buf.size)); + JSValue enc_fmt = JS_NewString(js, "png"); + JS_SetPropertyStr(js, result.val, "format", enc_fmt); + JSValue enc_blob = js_new_blob_stoned_copy(js, buf.data, buf.size); + JS_SetPropertyStr(js, result.val, "pixels", enc_blob); free(buf.data); JS_RETURN(result.val); diff --git a/psd.c b/psd.c index 812c97b..7abb710 100644 --- a/psd.c +++ b/psd.c @@ -29,9 +29,11 @@ JSValue js_psd_decode(JSContext *js, JSValue this_val, int argc, JSValueConst *a JS_ROOT(obj, JS_NewObject(js)); JS_SetPropertyStr(js, obj.val, "width", JS_NewInt32(js, width)); JS_SetPropertyStr(js, obj.val, "height", JS_NewInt32(js, height)); - JS_SetPropertyStr(js, obj.val, "format", JS_NewString(js, "rgba32")); + JSValue fmt_str = JS_NewString(js, "rgba32"); + JS_SetPropertyStr(js, obj.val, "format", fmt_str); JS_SetPropertyStr(js, obj.val, "pitch", JS_NewInt32(js, pitch)); - JS_SetPropertyStr(js, obj.val, "pixels", js_new_blob_stoned_copy(js, data, pixels_size)); + JSValue pixels_blob = js_new_blob_stoned_copy(js, data, pixels_size); + JS_SetPropertyStr(js, obj.val, "pixels", pixels_blob); JS_SetPropertyStr(js, obj.val, "depth", JS_NewInt32(js, 8)); JS_SetPropertyStr(js, obj.val, "hdr", JS_NewBool(js, 0)); diff --git a/qoi.c b/qoi.c index 29dd7c5..5039d23 100644 --- a/qoi.c +++ b/qoi.c @@ -100,10 +100,12 @@ JSValue js_qoi_encode(JSContext *js, JSValue this_val, int argc, JSValueConst *a JS_ROOT(result, JS_NewObject(js)); JS_SetPropertyStr(js, result.val, "width", JS_NewInt32(js, width)); JS_SetPropertyStr(js, result.val, "height", JS_NewInt32(js, height)); - JS_SetPropertyStr(js, result.val, "format", JS_NewString(js, "qoi")); + JSValue fmt_str = JS_NewString(js, "qoi"); + JS_SetPropertyStr(js, result.val, "format", fmt_str); JS_SetPropertyStr(js, result.val, "channels", JS_NewInt32(js, channels)); JS_SetPropertyStr(js, result.val, "colorspace", JS_NewInt32(js, colorspace)); - JS_SetPropertyStr(js, result.val, "pixels", js_new_blob_stoned_copy(js, qoi_data, out_len)); + JSValue enc_blob = js_new_blob_stoned_copy(js, qoi_data, out_len); + JS_SetPropertyStr(js, result.val, "pixels", enc_blob); free(qoi_data); JS_RETURN(result.val); @@ -156,9 +158,11 @@ JSValue js_qoi_decode(JSContext *js, JSValue this_val, int argc, JSValueConst *a JS_ROOT(obj, JS_NewObject(js)); JS_SetPropertyStr(js, obj.val, "width", JS_NewInt32(js, desc.width)); JS_SetPropertyStr(js, obj.val, "height", JS_NewInt32(js, desc.height)); - JS_SetPropertyStr(js, obj.val, "format", JS_NewString(js, "rgba32")); + JSValue dec_fmt = JS_NewString(js, "rgba32"); + JS_SetPropertyStr(js, obj.val, "format", dec_fmt); JS_SetPropertyStr(js, obj.val, "pitch", JS_NewInt32(js, pitch)); - JS_SetPropertyStr(js, obj.val, "pixels", js_new_blob_stoned_copy(js, rgba_data, pixels_size)); + JSValue pixels_blob = js_new_blob_stoned_copy(js, rgba_data, pixels_size); + JS_SetPropertyStr(js, obj.val, "pixels", pixels_blob); JS_SetPropertyStr(js, obj.val, "depth", JS_NewInt32(js, 8)); JS_SetPropertyStr(js, obj.val, "hdr", JS_NewBool(js, 0)); JS_SetPropertyStr(js, obj.val, "colorspace", JS_NewInt32(js, desc.colorspace)); diff --git a/resize.c b/resize.c index 7480477..325697a 100644 --- a/resize.c +++ b/resize.c @@ -105,9 +105,11 @@ JSValue js_resize_resize(JSContext *js, JSValue this_val, int argc, JSValueConst JS_ROOT(obj, JS_NewObject(js)); JS_SetPropertyStr(js, obj.val, "width", JS_NewInt32(js, dst_width)); JS_SetPropertyStr(js, obj.val, "height", JS_NewInt32(js, dst_height)); - JS_SetPropertyStr(js, obj.val, "format", JS_NewString(js, "rgba32")); + JSValue fmt_str = JS_NewString(js, "rgba32"); + JS_SetPropertyStr(js, obj.val, "format", fmt_str); JS_SetPropertyStr(js, obj.val, "pitch", JS_NewInt32(js, dst_width * 4)); - JS_SetPropertyStr(js, obj.val, "pixels", js_new_blob_stoned_copy(js, dst_pixels, dst_size)); + JSValue pixels_blob = js_new_blob_stoned_copy(js, dst_pixels, dst_size); + JS_SetPropertyStr(js, obj.val, "pixels", pixels_blob); JS_SetPropertyStr(js, obj.val, "depth", JS_NewInt32(js, 8)); free(dst_pixels); diff --git a/stb_image_common.h b/stb_image_common.h index dda7dd2..8c6f736 100644 --- a/stb_image_common.h +++ b/stb_image_common.h @@ -53,9 +53,11 @@ JSValue js_##fmt##_decode(JSContext *js, JSValue this_val, int argc, JSValueCons JS_ROOT(obj, JS_NewObject(js)); \ JS_SetPropertyStr(js, obj.val, "width", JS_NewInt32(js, width)); \ JS_SetPropertyStr(js, obj.val, "height", JS_NewInt32(js, height)); \ - JS_SetPropertyStr(js, obj.val, "format", JS_NewString(js, "rgba32")); \ + JSValue fmt_str = JS_NewString(js, "rgba32"); \ + JS_SetPropertyStr(js, obj.val, "format", fmt_str); \ JS_SetPropertyStr(js, obj.val, "pitch", JS_NewInt32(js, pitch)); \ - JS_SetPropertyStr(js, obj.val, "pixels", js_new_blob_stoned_copy(js, data, pixels_size)); \ + JSValue pixels_blob = js_new_blob_stoned_copy(js, data, pixels_size); \ + JS_SetPropertyStr(js, obj.val, "pixels", pixels_blob); \ JS_SetPropertyStr(js, obj.val, "depth", JS_NewInt32(js, 8)); \ JS_SetPropertyStr(js, obj.val, "hdr", JS_NewBool(js, 0)); \ \ @@ -114,8 +116,10 @@ JSValue js_##fmt##_encode(JSContext *js, JSValue this_val, int argc, JSValueCons JS_ROOT(result, JS_NewObject(js)); \ JS_SetPropertyStr(js, result.val, "width", JS_NewInt32(js, width)); \ JS_SetPropertyStr(js, result.val, "height", JS_NewInt32(js, height)); \ - JS_SetPropertyStr(js, result.val, "format", JS_NewString(js, #fmt)); \ - JS_SetPropertyStr(js, result.val, "pixels", js_new_blob_stoned_copy(js, buf.data, buf.size)); \ + JSValue enc_fmt = JS_NewString(js, #fmt); \ + JS_SetPropertyStr(js, result.val, "format", enc_fmt); \ + JSValue enc_blob = js_new_blob_stoned_copy(js, buf.data, buf.size); \ + JS_SetPropertyStr(js, result.val, "pixels", enc_blob); \ \ free(buf.data); \ JS_RETURN(result.val); \ diff --git a/tga.c b/tga.c index f19038d..7914f3d 100644 --- a/tga.c +++ b/tga.c @@ -29,9 +29,11 @@ JSValue js_tga_decode(JSContext *js, JSValue this_val, int argc, JSValueConst *a JS_ROOT(obj, JS_NewObject(js)); JS_SetPropertyStr(js, obj.val, "width", JS_NewInt32(js, width)); JS_SetPropertyStr(js, obj.val, "height", JS_NewInt32(js, height)); - JS_SetPropertyStr(js, obj.val, "format", JS_NewString(js, "rgba32")); + JSValue fmt_str = JS_NewString(js, "rgba32"); + JS_SetPropertyStr(js, obj.val, "format", fmt_str); JS_SetPropertyStr(js, obj.val, "pitch", JS_NewInt32(js, pitch)); - JS_SetPropertyStr(js, obj.val, "pixels", js_new_blob_stoned_copy(js, data, pixels_size)); + JSValue pixels_blob = js_new_blob_stoned_copy(js, data, pixels_size); + JS_SetPropertyStr(js, obj.val, "pixels", pixels_blob); JS_SetPropertyStr(js, obj.val, "depth", JS_NewInt32(js, 8)); JS_SetPropertyStr(js, obj.val, "hdr", JS_NewBool(js, 0)); @@ -90,8 +92,10 @@ JSValue js_tga_encode(JSContext *js, JSValue this_val, int argc, JSValueConst *a JS_ROOT(result, JS_NewObject(js)); JS_SetPropertyStr(js, result.val, "width", JS_NewInt32(js, width)); JS_SetPropertyStr(js, result.val, "height", JS_NewInt32(js, height)); - JS_SetPropertyStr(js, result.val, "format", JS_NewString(js, "tga")); - JS_SetPropertyStr(js, result.val, "pixels", js_new_blob_stoned_copy(js, buf.data, buf.size)); + JSValue enc_fmt = JS_NewString(js, "tga"); + JS_SetPropertyStr(js, result.val, "format", enc_fmt); + JSValue enc_blob = js_new_blob_stoned_copy(js, buf.data, buf.size); + JS_SetPropertyStr(js, result.val, "pixels", enc_blob); free(buf.data); JS_RETURN(result.val);