From 3622a5ec5868683782a05fdcdee81ff3d5ceff57 Mon Sep 17 00:00:00 2001 From: John Alanbrook Date: Sun, 8 Jun 2025 11:06:59 -0500 Subject: [PATCH] actor messages now delivered as blobs --- scripts/engine.cm | 8 +++++--- source/cell.c | 21 +++++++++++++-------- source/cell.h | 4 ++-- source/qjs_actor.c | 17 ++++++++++++----- 4 files changed, 32 insertions(+), 18 deletions(-) diff --git a/scripts/engine.cm b/scripts/engine.cm index cb06202e..7ca24d6a 100644 --- a/scripts/engine.cm +++ b/scripts/engine.cm @@ -571,7 +571,9 @@ function actor_send(actor, message) { // message to actor in same flock if (actor[ACTORDATA].id && actor_mod.mailbox_exist(actor[ACTORDATA].id)) { - actor_mod.mailbox_push(actor[ACTORDATA].id, wota.encode(message)) + var wota_blob = wota.encode(message) +// log.console(`sending wota blob of ${wota_blob.length/8} bytes`) + actor_mod.mailbox_push(actor[ACTORDATA].id, wota_blob) return } @@ -660,8 +662,8 @@ $_[ACTORDATA].id = cell.id // Actor's timeslice for processing a single message function turn(msg) { - var decoded = wota.decode(msg) - handle_message(decoded) + var mes = wota.decode(msg) + handle_message(mes) send_messages() } diff --git a/source/cell.c b/source/cell.c index 0189b30f..adaf5bf4 100644 --- a/source/cell.c +++ b/source/cell.c @@ -30,6 +30,9 @@ #include "wota.h" #include "qjs_wota.h" +#define BLOB_IMPLEMENTATION +#include "blob.h" + #include "physfs.h" #include "stb_ds.h" #include "jsffi.h" @@ -161,8 +164,8 @@ void actor_free(cell_rt *actor) /* Free all letters in the queue */ for (int i = 0; i < arrlen(actor->letters); i++) { - if (actor->letters[i].type == LETTER_WOTA) { - free(actor->letters[i].wota_data); + if (actor->letters[i].type == LETTER_BLOB) { + blob_destroy(actor->letters[i].blob_data); } else if (actor->letters[i].type == LETTER_CALLBACK) { JS_FreeValue(js, actor->letters[i].callback); } @@ -414,13 +417,13 @@ const char *send_message(const char *id, void *msg) { cell_rt *target = get_actor(id); if (!target) { - free(msg); + blob_destroy((blob *)msg); return "Could not get actor from id."; } letter l; - l.type = LETTER_WOTA; - l.wota_data = msg; + l.type = LETTER_BLOB; + l.blob_data = (blob *)msg; SDL_LockMutex(target->msg_mutex); @@ -530,9 +533,11 @@ void actor_turn(cell_rt *actor) arrdel(actor->letters, 0); SDL_UnlockMutex(actor->msg_mutex); - if (l.type == LETTER_WOTA) { - JSValue arg = wota2value(actor->context, l.wota_data); - free(l.wota_data); + if (l.type == LETTER_BLOB) { + // Create a JS blob from the C blob + size_t size = l.blob_data->length / 8; // Convert bits to bytes + JSValue arg = js_new_blob_stoned_copy(actor->context, l.blob_data->data, size); + blob_destroy(l.blob_data); result = JS_Call(actor->context, actor->message_handle, JS_UNDEFINED, 1, &arg); uncaught_exception(actor->context, result); JS_FreeValue(actor->context, arg); diff --git a/source/cell.h b/source/cell.h index b2cd9af8..fe9833d2 100644 --- a/source/cell.h +++ b/source/cell.h @@ -9,14 +9,14 @@ /* Letter type for unified message queue */ typedef enum { - LETTER_WOTA, /* Raw wota message */ + LETTER_BLOB, /* Blob message */ LETTER_CALLBACK /* JSValue callback function */ } letter_type; typedef struct letter { letter_type type; union { - void *wota_data; /* For LETTER_WOTA */ + blob *blob_data; /* For LETTER_BLOB */ JSValue callback; /* For LETTER_CALLBACK */ }; } letter; diff --git a/source/qjs_actor.c b/source/qjs_actor.c index da9f6809..ebc623d0 100644 --- a/source/qjs_actor.c +++ b/source/qjs_actor.c @@ -111,16 +111,23 @@ JSC_CCALL(os_mailbox_push, if (stop) return JS_UNDEFINED; } */ -// void *data = value2wota(js, argv[1], JS_UNDEFINED, NULL); size_t size; void *data = js_get_blob_data(js, &size, argv[1]); - void *copy = malloc(size); - memcpy(copy, data, size); + // Create a new blob and copy the data + blob *msg_blob = blob_new(size * 8); // Convert bytes to bits + if (!msg_blob) { + return JS_ThrowInternalError(js, "Could not allocate blob"); + } - const char *err = send_message(id, copy); + // Copy data to blob + memcpy(msg_blob->data, data, size); + msg_blob->length = size * 8; + blob_make_stone(msg_blob); // Make it immutable + + const char *err = send_message(id, msg_blob); if (err) { - free(data); + blob_destroy(msg_blob); return JS_ThrowInternalError(js, "Could not send message: %s", err); } )