actor messages now delivered as blobs

This commit is contained in:
2025-06-08 11:06:59 -05:00
parent 8a5f8a4d74
commit 3622a5ec58
4 changed files with 32 additions and 18 deletions

View File

@@ -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()
}

View File

@@ -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);

View File

@@ -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;

View File

@@ -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);
}
)