fix syntax

This commit is contained in:
2026-02-17 09:12:48 -06:00
parent 6f6f987649
commit d509f7cbb8
21 changed files with 503 additions and 453 deletions

126
surface.c
View File

@@ -90,13 +90,14 @@ void SDL_Surface_free(JSRuntime *rt, SDL_Surface *s) {
QJSCLASS(SDL_Surface,)
JSValue make_surface(JSContext *js, SDL_Surface *s){
JSValue ret = SDL_Surface2js(js,s);
JS_FRAME(js);
JS_LOCAL(ret, SDL_Surface2js(js,s));
JS_SetPropertyStr(js, ret, "width", JS_NewInt32(js, s->w));
JS_SetPropertyStr(js, ret, "height", JS_NewInt32(js, s->h));
JS_SetPropertyStr(js, ret, "format", pixelformat2js(js, s->format));
JS_SetPropertyStr(js, ret, "pitch", JS_NewFloat64(js, s->pitch));
return JS_Stone(js, ret);
JS_RETURN(JS_Stone(js, ret));
}
// SDL_Surface methods
@@ -227,40 +228,30 @@ JSValue js_surface_get_pitch(JSContext *js, JSValue self) {
JSC_CCALL(surface_toJSON,
SDL_Surface *surf = js2SDL_Surface(js,self);
// Create the result object
JSValue obj = JS_NewObject(js);
// Add width and height
JS_FRAME(js);
JS_LOCAL(obj, JS_NewObject(js));
JS_SetPropertyStr(js, obj, "width", JS_NewInt32(js, surf->w));
JS_SetPropertyStr(js, obj, "height", JS_NewInt32(js, surf->h));
// Add format
JS_SetPropertyStr(js, obj, "format", pixelformat2js(js, surf->format));
// Add pitch
JS_SetPropertyStr(js, obj, "pitch", JS_NewInt32(js, surf->pitch));
// Lock surface if needed
int locked = 0;
if (SDL_MUSTLOCK(surf)) {
if (SDL_LockSurface(surf) < 0) {
JS_FreeValue(js, obj);
return JS_ThrowInternalError(js, "Lock surface failed: %s", SDL_GetError());
JS_RETURN_EX();
}
locked = 1;
}
// Add pixels as ArrayBuffer
size_t byte_size = surf->pitch * surf->h;
JSValue pixels = js_new_blob_stoned_copy(js, surf->pixels, byte_size);
JS_SetPropertyStr(js, obj, "pixels", pixels);
// Unlock if we locked
JS_SetPropertyStr(js, obj, "pixels", js_new_blob_stoned_copy(js, surf->pixels, byte_size));
if (locked)
SDL_UnlockSurface(surf);
return obj;
JS_RETURN(obj);
)
// Check for integer overflow in size calculations
@@ -420,17 +411,17 @@ static JSValue compress_bc_common(JSContext *js, JSValueConst *argv, int argc, i
free(rgba_data);
// Create result object
JSValue result = JS_NewObject(js);
JS_FRAME(js);
JS_LOCAL(result, JS_NewObject(js));
JS_SetPropertyStr(js, result, "width", JS_NewInt32(js, width));
JS_SetPropertyStr(js, result, "height", JS_NewInt32(js, height));
JS_SetPropertyStr(js, result, "format", JS_NewString(js, format_name));
JS_SetPropertyStr(js, result, "pitch", JS_NewInt32(js, blocks_x * bytes_per_block));
JSValue compressed_pixels = js_new_blob_stoned_copy(js, output, output_size);
free(output); // Free the output buffer after copying to blob
JS_SetPropertyStr(js, result, "pixels", compressed_pixels);
return result;
JS_SetPropertyStr(js, result, "pixels", js_new_blob_stoned_copy(js, output, output_size));
free(output);
JS_RETURN(result);
}
// BC1/DXT1 compression
@@ -587,19 +578,19 @@ static JSValue compress_bc_channels(JSContext *js, JSValueConst *argv, int argc,
}
free(channel_data);
// Create result object
JSValue result = JS_NewObject(js);
JS_FRAME(js);
JS_LOCAL(result, JS_NewObject(js));
JS_SetPropertyStr(js, result, "width", JS_NewInt32(js, width));
JS_SetPropertyStr(js, result, "height", JS_NewInt32(js, height));
JS_SetPropertyStr(js, result, "format", JS_NewString(js, format_name));
JS_SetPropertyStr(js, result, "pitch", JS_NewInt32(js, blocks_x * bytes_per_block));
JSValue compressed_pixels = js_new_blob_stoned_copy(js, output, output_size);
free(output); // Free the output buffer after copying to blob
JS_SetPropertyStr(js, result, "pixels", compressed_pixels);
return result;
JS_SetPropertyStr(js, result, "pixels", js_new_blob_stoned_copy(js, output, output_size));
free(output);
JS_RETURN(result);
}
// BC4 compression (single channel)
@@ -775,37 +766,32 @@ static SDL_Surface* image_to_surface(JSContext *js, JSValue img_obj)
// Helper function to convert SDL_Surface back to image object
static JSValue surface_to_image(JSContext *js, SDL_Surface *surf)
{
JSValue obj = JS_NewObject(js);
JS_FRAME(js);
JS_LOCAL(obj, JS_NewObject(js));
JS_SetPropertyStr(js, obj, "width", JS_NewInt32(js, surf->w));
JS_SetPropertyStr(js, obj, "height", JS_NewInt32(js, surf->h));
JS_SetPropertyStr(js, obj, "format", pixelformat2js(js, surf->format));
JS_SetPropertyStr(js, obj, "pitch", JS_NewInt32(js, surf->pitch));
// Lock surface if needed
int locked = 0;
if (SDL_MUSTLOCK(surf)) {
if (SDL_LockSurface(surf) < 0) {
JS_FreeValue(js, obj);
return JS_NULL;
JS_RETURN_NULL();
}
locked = 1;
}
// Add pixels as stoned blob
size_t byte_size = surf->pitch * surf->h;
JSValue pixels = js_new_blob_stoned_copy(js, surf->pixels, byte_size);
JS_SetPropertyStr(js, obj, "pixels", pixels);
// Unlock if we locked
JS_SetPropertyStr(js, obj, "pixels", js_new_blob_stoned_copy(js, surf->pixels, byte_size));
if (locked)
SDL_UnlockSurface(surf);
// Add depth and hdr properties for completeness
JS_SetPropertyStr(js, obj, "depth", JS_NewInt32(js, SDL_BITSPERPIXEL(surf->format)));
JS_SetPropertyStr(js, obj, "hdr", JS_FALSE);
return obj;
JS_RETURN(obj);
}
// Scale function for image objects
@@ -1068,44 +1054,38 @@ JSC_CCALL(surface_convert_generic,
}
// Create result image object
JSValue result = JS_NewObject(js);
JS_FRAME(js);
JS_LOCAL(result, JS_NewObject(js));
JS_SetPropertyStr(js, result, "width", JS_NewInt32(js, src_width));
JS_SetPropertyStr(js, result, "height", JS_NewInt32(js, src_height));
JS_SetPropertyStr(js, result, "format", pixelformat2js(js, dst_format));
JS_SetPropertyStr(js, result, "pitch", JS_NewInt32(js, dst_pitch));
JSValue pixels = js_new_blob_stoned_copy(js, dst_pixels, dst_size);
JS_SetPropertyStr(js, result, "pixels", js_new_blob_stoned_copy(js, dst_pixels, dst_size));
free(dst_pixels);
JS_SetPropertyStr(js, result, "pixels", pixels);
// Add depth and hdr for consistency
JS_SetPropertyStr(js, result, "depth", JS_NewInt32(js, SDL_BITSPERPIXEL(dst_format)));
JS_SetPropertyStr(js, result, "hdr", JS_FALSE);
return result;
JS_RETURN(result);
)
CELL_USE_INIT(
QJSCLASSPREP_FUNCS(SDL_Surface)
// Add the surface constructor
JSValue ctor = JS_NewCFunction2(js, js_surface_constructor, "surface", 1, JS_CFUNC_generic, 0);
// Add the generic convert function as a property on the constructor
JS_FRAME(js);
JS_LOCAL(ctor, JS_NewCFunction2(js, js_surface_constructor, "surface", 1, JS_CFUNC_generic, 0));
JS_SetPropertyStr(js, ctor, "convert", JS_NewCFunction(js, js_surface_convert_generic, "convert", 2));
// Add the compression functions as static methods on the constructor
JS_SetPropertyStr(js, ctor, "compress_bc1", JS_NewCFunction(js, js_surface_compress_bc1, "compress_bc1", 2));
JS_SetPropertyStr(js, ctor, "compress_bc3", JS_NewCFunction(js, js_surface_compress_bc3, "compress_bc3", 2));
JS_SetPropertyStr(js, ctor, "compress_bc4", JS_NewCFunction(js, js_surface_compress_bc4, "compress_bc4", 1));
JS_SetPropertyStr(js, ctor, "compress_bc5", JS_NewCFunction(js, js_surface_compress_bc5, "compress_bc5", 1));
// Add standalone image manipulation functions
JS_SetPropertyStr(js, ctor, "scale", JS_NewCFunction(js, js_surface_scale_img, "scale", 2));
JS_SetPropertyStr(js, ctor, "fill", JS_NewCFunction(js, js_surface_fill_img, "fill", 2));
JS_SetPropertyStr(js, ctor, "rect", JS_NewCFunction(js, js_surface_rect_img, "rect", 3));
JS_SetPropertyStr(js, ctor, "blit", JS_NewCFunction(js, js_surface_blit_img, "blit", 5));
JS_SetPropertyStr(js, ctor, "dup", JS_NewCFunction(js, js_surface_dup_img, "dup", 1));
return ctor;
JS_RETURN(ctor);
)