From 1040c61863b94feea948f8c922cc784a34427278 Mon Sep 17 00:00:00 2001 From: John Alanbrook Date: Wed, 28 May 2025 23:56:18 -0500 Subject: [PATCH] fix blob usage errors --- examples/chess/main.js | 1 - scripts/core/_sdl_video.js | 12 ++++++++---- scripts/modules/graphics.js | 14 +++++++++----- scripts/moth.js | 1 + source/blob.h | 16 ++++++++++++++++ source/jsffi.c | 2 ++ source/qjs_blob.c | 3 ++- source/qjs_sdl.c | 1 + source/qjs_sdl_surface.c | 2 +- source/qjs_sdl_video.c | 22 +--------------------- 10 files changed, 41 insertions(+), 33 deletions(-) diff --git a/examples/chess/main.js b/examples/chess/main.js index 38905abe..f4c6e261 100644 --- a/examples/chess/main.js +++ b/examples/chess/main.js @@ -16,7 +16,6 @@ console.log(myblob.toString()) console.log(myblob) console.log(myblob.length) - var input = use('input') input.watch($_) diff --git a/scripts/core/_sdl_video.js b/scripts/core/_sdl_video.js index 09cfaee4..de05aaab 100644 --- a/scripts/core/_sdl_video.js +++ b/scripts/core/_sdl_video.js @@ -108,9 +108,9 @@ $_.receiver(function(msg) { } } catch (e) { response = {error: e.toString()}; + console.error(e) } - // Send response back send(msg, response); }); @@ -432,18 +432,22 @@ function handle_renderer(msg) { return {id: surf_id}; case 'loadTexture': - if (!msg.data) return {error: "Missing data"}; + if (!msg.data) throw new Error("Missing data") var tex; // Direct surface data + console.log(json.encode(msg.data)) var surf = new surface(msg.data) + console.log("GOT DATA") + console.log(json.encode(msg.data)) + if (!surf) - return {error: "Must provide surface_id or surface data"}; + throw new Error("Must provide surface_id or surface data") tex = ren.load_texture(surf); - if (!tex) return {error: "Failed to load texture"}; + if (!tex) throw new Error("Failed to load texture") var tex_id = allocate_id(); resources.texture[tex_id] = tex; return { diff --git a/scripts/modules/graphics.js b/scripts/modules/graphics.js index 74f90e89..4e3380ff 100644 --- a/scripts/modules/graphics.js +++ b/scripts/modules/graphics.js @@ -41,14 +41,17 @@ Object.defineProperties(graphics.Image.prototype, { var self = this; // Send message to load texture + console.log("LOADING") send(renderer_actor, { kind: "renderer", id: renderer_id, op: "loadTexture", data: this[CPU] }, function(response) { + console.log("GOT MSG") if (response.error) { - console.error("Failed to load texture:", response.error) + console.error("Failed to load texture:") + console.error(response.error) self[LOADING] = false; } else { self[GPU] = response; @@ -156,13 +159,14 @@ function decode_image(bytes, ext) function create_image(path){ try{ const bytes = io.slurpbytes(path); - console.log(bytes) - console.log(bytes.length) + let raw = decode_image(bytes, path.ext()); /* ── Case A: static image ─────────────────────────────────── */ - if(raw.surface) - return make_handle(raw.surface); + if(raw.surface) { + var gg = new graphics.Image(raw.surface) + return gg + } /* ── Case B: GIF helpers returned array [surf, …] ─────────── */ if(Array.isArray(raw)) diff --git a/scripts/moth.js b/scripts/moth.js index 166ec349..59b70e33 100644 --- a/scripts/moth.js +++ b/scripts/moth.js @@ -106,6 +106,7 @@ send(video_actor, { render = e.id graphics = use('graphics', video_actor, e.id) + console.log(`Created window and renderer id ${render}`) }) }) diff --git a/source/blob.h b/source/blob.h index e90c1f3b..6269c8a5 100644 --- a/source/blob.h +++ b/source/blob.h @@ -388,6 +388,22 @@ int blob_write_text(blob *b, const char *text) { return 0; } +int blob_write_bytes(blob *b, void *data, size_t length) { + if (!b || !data || b->is_stone) return -1; + + // Write each byte as 8 bits + uint8_t *bytes = (uint8_t *)data; + for (size_t i = 0; i < length; i++) { + for (int j = 0; j < 8; j++) { + if (blob_write_bit(b, (bytes[i] >> j) & 1) < 0) { + return -1; + } + } + } + + return 0; +} + int blob_read_bit(const blob *b, size_t pos, int *out_bit) { if (!b || !b->is_stone || !out_bit) return -1; if (pos >= b->bit_length) return -1; diff --git a/source/jsffi.c b/source/jsffi.c index a54d6311..2c91d0e1 100644 --- a/source/jsffi.c +++ b/source/jsffi.c @@ -1019,6 +1019,8 @@ JSC_CCALL(os_make_texture, int pitch = width*4; size_t pixels_size = pitch * height; + printf("making surface from blob %p of %u bytes\n", raw, len); + // Create JS object with surface data JSValue obj = JS_NewObject(js); JS_SetPropertyStr(js, obj, "width", JS_NewInt32(js, width)); diff --git a/source/qjs_blob.c b/source/qjs_blob.c index 56b9598c..b57157d5 100644 --- a/source/qjs_blob.c +++ b/source/qjs_blob.c @@ -605,6 +605,7 @@ JSValue js_new_blob_stoned_copy(JSContext *js, void *data, size_t bytes) printf("Making blob from %p with %u bytes\n", data, bytes); blob *b = blob_new(bytes*8); memcpy(b->data, data, bytes); + b->bit_length = bytes * 8; // Set the actual length in bits blob_make_stone(b); return blob2js(js, b); @@ -616,7 +617,7 @@ void *js_get_blob_data(JSContext *js, size_t *size, JSValue v) if (!b || !b->is_stone) return NULL; - *size = b->bit_capacity/8; + *size = (b->bit_length + 7) / 8; // Return actual byte size based on bit length return b->data; } diff --git a/source/qjs_sdl.c b/source/qjs_sdl.c index f39edb9b..939b5b00 100644 --- a/source/qjs_sdl.c +++ b/source/qjs_sdl.c @@ -295,6 +295,7 @@ SDL_PixelFormat str2pixelformat(const char *str) { if (!strcmp(str, "nv12")) return SDL_PIXELFORMAT_NV12; if (!strcmp(str, "nv21")) return SDL_PIXELFORMAT_NV21; if (!strcmp(str, "p010")) return SDL_PIXELFORMAT_P010; + if (!strcmp(str, "rgba32")) return SDL_PIXELFORMAT_RGBA32; return SDL_PIXELFORMAT_UNKNOWN; } diff --git a/source/qjs_sdl_surface.c b/source/qjs_sdl_surface.c index c944a043..ab6a4463 100644 --- a/source/qjs_sdl_surface.c +++ b/source/qjs_sdl_surface.c @@ -23,7 +23,7 @@ JSValue pixelformat2js(JSContext *js, SDL_PixelFormat fmt) return JS_NewString(js, str); } -static SDL_PixelFormat js2pixelformat(JSContext *js, JSValue v) +SDL_PixelFormat js2pixelformat(JSContext *js, JSValue v) { if (JS_IsUndefined(v)) return SDL_PIXELFORMAT_UNKNOWN; const char *s = JS_ToCString(js, v); diff --git a/source/qjs_sdl_video.c b/source/qjs_sdl_video.c index af6c73f4..a59fa9ea 100644 --- a/source/qjs_sdl_video.c +++ b/source/qjs_sdl_video.c @@ -13,6 +13,7 @@ #include #include #include +#include "qjs_sdl.h" // External function declarations extern prosperon_rt *create_actor(int argc, char **argv, void (*hook)(JSContext *)); @@ -1455,27 +1456,6 @@ static SDL_BlendMode js2blendmode(JSContext *js, JSValue v) { return mode; } -// Convert pixel format string to SDL_PixelFormat -static SDL_PixelFormat js2pixelformat(JSContext *js, JSValue v) { - if (JS_IsNumber(v)) { - return js2number(js, v); - } - const char *str = JS_ToCString(js, v); - SDL_PixelFormat fmt = SDL_PIXELFORMAT_RGBA8888; - if (str) { - if (strcmp(str, "rgba8888") == 0) fmt = SDL_PIXELFORMAT_RGBA8888; - else if (strcmp(str, "bgra8888") == 0) fmt = SDL_PIXELFORMAT_BGRA8888; - else if (strcmp(str, "argb8888") == 0) fmt = SDL_PIXELFORMAT_ARGB8888; - else if (strcmp(str, "abgr8888") == 0) fmt = SDL_PIXELFORMAT_ABGR8888; - else if (strcmp(str, "rgba32") == 0) fmt = SDL_PIXELFORMAT_RGBA32; - else if (strcmp(str, "bgra32") == 0) fmt = SDL_PIXELFORMAT_BGRA32; - else if (strcmp(str, "argb32") == 0) fmt = SDL_PIXELFORMAT_ARGB32; - else if (strcmp(str, "abgr32") == 0) fmt = SDL_PIXELFORMAT_ABGR32; - JS_FreeCString(js, str); - } - return fmt; -} - // Texture constructor static JSValue js_texture_constructor(JSContext *js, JSValueConst new_target, int argc, JSValueConst *argv) {