Add auxiliary structure for retrieving SAB tabs

This commit is contained in:
Saúl Ibarra Corretgé 2024-07-15 16:11:26 +02:00
parent 3ed591c02d
commit 763010663b
3 changed files with 35 additions and 29 deletions

View file

@ -3532,17 +3532,17 @@ static JSValue js_worker_postMessage(JSContext *ctx, JSValue this_val,
{
JSWorkerData *worker = JS_GetOpaque2(ctx, this_val, js_worker_class_id);
JSWorkerMessagePipe *ps;
size_t data_len, sab_tab_len, i;
size_t data_len, i;
uint8_t *data;
JSWorkerMessage *msg;
uint8_t **sab_tab;
JSSABTab sab_tab;
if (!worker)
return JS_EXCEPTION;
data = JS_WriteObject2(ctx, &data_len, argv[0],
JS_WRITE_OBJ_SAB | JS_WRITE_OBJ_REFERENCE,
&sab_tab, &sab_tab_len);
&sab_tab);
if (!data)
return JS_EXCEPTION;
@ -3559,16 +3559,16 @@ static JSValue js_worker_postMessage(JSContext *ctx, JSValue this_val,
memcpy(msg->data, data, data_len);
msg->data_len = data_len;
if (sab_tab_len > 0) {
msg->sab_tab = malloc(sizeof(msg->sab_tab[0]) * sab_tab_len);
if (sab_tab.len > 0) {
msg->sab_tab = malloc(sizeof(msg->sab_tab[0]) * sab_tab.len);
if (!msg->sab_tab)
goto fail;
memcpy(msg->sab_tab, sab_tab, sizeof(msg->sab_tab[0]) * sab_tab_len);
memcpy(msg->sab_tab, sab_tab.tab, sizeof(msg->sab_tab[0]) * sab_tab.len);
}
msg->sab_tab_len = sab_tab_len;
msg->sab_tab_len = sab_tab.len;
js_free(ctx, data);
js_free(ctx, sab_tab);
js_free(ctx, sab_tab.tab);
/* increment the SAB reference counts */
for(i = 0; i < msg->sab_tab_len; i++) {
@ -3599,7 +3599,7 @@ static JSValue js_worker_postMessage(JSContext *ctx, JSValue this_val,
free(msg);
}
js_free(ctx, data);
js_free(ctx, sab_tab);
js_free(ctx, sab_tab.tab);
return JS_EXCEPTION;
}

View file

@ -33928,7 +33928,7 @@ static int JS_WriteObjectAtoms(BCWriterState *s)
}
uint8_t *JS_WriteObject2(JSContext *ctx, size_t *psize, JSValue obj,
int flags, uint8_t ***psab_tab, size_t *psab_tab_len)
int flags, JSSABTab *psab_tab)
{
BCWriterState ss, *s = &ss;
@ -33955,12 +33955,12 @@ uint8_t *JS_WriteObject2(JSContext *ctx, size_t *psize, JSValue obj,
js_free(ctx, s->atom_to_idx);
js_free(ctx, s->idx_to_atom);
*psize = s->dbuf.size;
if (psab_tab)
*psab_tab = s->sab_tab;
else
if (psab_tab) {
psab_tab->tab = s->sab_tab;
psab_tab->len = s->sab_tab_len;
} else {
js_free(ctx, s->sab_tab);
if (psab_tab_len)
*psab_tab_len = s->sab_tab_len;
}
return s->dbuf.buf;
fail:
js_object_list_end(ctx, &s->object_list);
@ -33968,17 +33968,17 @@ uint8_t *JS_WriteObject2(JSContext *ctx, size_t *psize, JSValue obj,
js_free(ctx, s->idx_to_atom);
dbuf_free(&s->dbuf);
*psize = 0;
if (psab_tab)
*psab_tab = NULL;
if (psab_tab_len)
*psab_tab_len = 0;
if (psab_tab) {
psab_tab->tab = NULL;
psab_tab->len = 0;
}
return NULL;
}
uint8_t *JS_WriteObject(JSContext *ctx, size_t *psize, JSValue obj,
int flags)
{
return JS_WriteObject2(ctx, psize, obj, flags, NULL, NULL);
return JS_WriteObject2(ctx, psize, obj, flags, NULL);
}
typedef struct BCReaderState {
@ -35165,7 +35165,7 @@ static void bc_reader_free(BCReaderState *s)
}
JSValue JS_ReadObject2(JSContext *ctx, const uint8_t *buf, size_t buf_len,
int flags, uint8_t ***psab_tab, size_t *psab_tab_len)
int flags, JSSABTab *psab_tab)
{
BCReaderState ss, *s = &ss;
JSValue obj;
@ -35190,12 +35190,12 @@ JSValue JS_ReadObject2(JSContext *ctx, const uint8_t *buf, size_t buf_len,
} else {
obj = JS_ReadObjectRec(s);
}
if (psab_tab)
*psab_tab = s->sab_tab;
else
if (psab_tab) {
psab_tab->tab = s->sab_tab;
psab_tab->len = s->sab_tab_len;
} else {
js_free(ctx, s->sab_tab);
if (psab_tab_len)
*psab_tab_len = s->sab_tab_len;
}
bc_reader_free(s);
return obj;
}
@ -35203,7 +35203,7 @@ JSValue JS_ReadObject2(JSContext *ctx, const uint8_t *buf, size_t buf_len,
JSValue JS_ReadObject(JSContext *ctx, const uint8_t *buf, size_t buf_len,
int flags)
{
return JS_ReadObject2(ctx, buf, buf_len, flags, NULL, NULL);
return JS_ReadObject2(ctx, buf, buf_len, flags, NULL);
}
/*******************************************************************/

View file

@ -840,6 +840,12 @@ JS_EXTERN int JS_EnqueueJob(JSContext *ctx, JSJobFunc *job_func, int argc, JSVal
JS_EXTERN JS_BOOL JS_IsJobPending(JSRuntime *rt);
JS_EXTERN int JS_ExecutePendingJob(JSRuntime *rt, JSContext **pctx);
/* Structure to retrieve (de)serialized SharedArrayBuffer objects. */
typedef struct JSSABTab {
uint8_t **tab;
size_t len;
} JSSABTab;
/* Object Writer/Reader (currently only used to handle precompiled code) */
#define JS_WRITE_OBJ_BYTECODE (1 << 0) /* allow function/module */
#define JS_WRITE_OBJ_BSWAP (0) /* byte swapped output (obsolete, handled transparently) */
@ -849,7 +855,7 @@ JS_EXTERN int JS_ExecutePendingJob(JSRuntime *rt, JSContext **pctx);
#define JS_WRITE_OBJ_STRIP_DEBUG (1 << 5) /* do not write debug information */
JS_EXTERN uint8_t *JS_WriteObject(JSContext *ctx, size_t *psize, JSValue obj, int flags);
JS_EXTERN uint8_t *JS_WriteObject2(JSContext *ctx, size_t *psize, JSValue obj,
int flags, uint8_t ***psab_tab, size_t *psab_tab_len);
int flags, JSSABTab *psab_tab);
#define JS_READ_OBJ_BYTECODE (1 << 0) /* allow function/module */
#define JS_READ_OBJ_ROM_DATA (0) /* avoid duplicating 'buf' data (obsolete, broken by ICs) */
@ -857,7 +863,7 @@ JS_EXTERN uint8_t *JS_WriteObject2(JSContext *ctx, size_t *psize, JSValue obj,
#define JS_READ_OBJ_REFERENCE (1 << 3) /* allow object references */
JS_EXTERN JSValue JS_ReadObject(JSContext *ctx, const uint8_t *buf, size_t buf_len, int flags);
JS_EXTERN JSValue JS_ReadObject2(JSContext *ctx, const uint8_t *buf, size_t buf_len,
int flags, uint8_t ***psab_tab, size_t *psab_tab_len);
int flags, JSSABTab *psab_tab);
/* instantiate and evaluate a bytecode function. Only used when
reading a script or module with JS_ReadObject() */
JS_EXTERN JSValue JS_EvalFunction(JSContext *ctx, JSValue fun_obj);