Skip to content

Commit 7630106

Browse files
committed
Add auxiliary structure for retrieving SAB tabs
1 parent 3ed591c commit 7630106

File tree

3 files changed

+35
-29
lines changed

3 files changed

+35
-29
lines changed

quickjs-libc.c

+9-9
Original file line numberDiff line numberDiff line change
@@ -3532,17 +3532,17 @@ static JSValue js_worker_postMessage(JSContext *ctx, JSValue this_val,
35323532
{
35333533
JSWorkerData *worker = JS_GetOpaque2(ctx, this_val, js_worker_class_id);
35343534
JSWorkerMessagePipe *ps;
3535-
size_t data_len, sab_tab_len, i;
3535+
size_t data_len, i;
35363536
uint8_t *data;
35373537
JSWorkerMessage *msg;
3538-
uint8_t **sab_tab;
3538+
JSSABTab sab_tab;
35393539

35403540
if (!worker)
35413541
return JS_EXCEPTION;
35423542

35433543
data = JS_WriteObject2(ctx, &data_len, argv[0],
35443544
JS_WRITE_OBJ_SAB | JS_WRITE_OBJ_REFERENCE,
3545-
&sab_tab, &sab_tab_len);
3545+
&sab_tab);
35463546
if (!data)
35473547
return JS_EXCEPTION;
35483548

@@ -3559,16 +3559,16 @@ static JSValue js_worker_postMessage(JSContext *ctx, JSValue this_val,
35593559
memcpy(msg->data, data, data_len);
35603560
msg->data_len = data_len;
35613561

3562-
if (sab_tab_len > 0) {
3563-
msg->sab_tab = malloc(sizeof(msg->sab_tab[0]) * sab_tab_len);
3562+
if (sab_tab.len > 0) {
3563+
msg->sab_tab = malloc(sizeof(msg->sab_tab[0]) * sab_tab.len);
35643564
if (!msg->sab_tab)
35653565
goto fail;
3566-
memcpy(msg->sab_tab, sab_tab, sizeof(msg->sab_tab[0]) * sab_tab_len);
3566+
memcpy(msg->sab_tab, sab_tab.tab, sizeof(msg->sab_tab[0]) * sab_tab.len);
35673567
}
3568-
msg->sab_tab_len = sab_tab_len;
3568+
msg->sab_tab_len = sab_tab.len;
35693569

35703570
js_free(ctx, data);
3571-
js_free(ctx, sab_tab);
3571+
js_free(ctx, sab_tab.tab);
35723572

35733573
/* increment the SAB reference counts */
35743574
for(i = 0; i < msg->sab_tab_len; i++) {
@@ -3599,7 +3599,7 @@ static JSValue js_worker_postMessage(JSContext *ctx, JSValue this_val,
35993599
free(msg);
36003600
}
36013601
js_free(ctx, data);
3602-
js_free(ctx, sab_tab);
3602+
js_free(ctx, sab_tab.tab);
36033603
return JS_EXCEPTION;
36043604

36053605
}

quickjs.c

+18-18
Original file line numberDiff line numberDiff line change
@@ -33928,7 +33928,7 @@ static int JS_WriteObjectAtoms(BCWriterState *s)
3392833928
}
3392933929

3393033930
uint8_t *JS_WriteObject2(JSContext *ctx, size_t *psize, JSValue obj,
33931-
int flags, uint8_t ***psab_tab, size_t *psab_tab_len)
33931+
int flags, JSSABTab *psab_tab)
3393233932
{
3393333933
BCWriterState ss, *s = &ss;
3393433934

@@ -33955,30 +33955,30 @@ uint8_t *JS_WriteObject2(JSContext *ctx, size_t *psize, JSValue obj,
3395533955
js_free(ctx, s->atom_to_idx);
3395633956
js_free(ctx, s->idx_to_atom);
3395733957
*psize = s->dbuf.size;
33958-
if (psab_tab)
33959-
*psab_tab = s->sab_tab;
33960-
else
33958+
if (psab_tab) {
33959+
psab_tab->tab = s->sab_tab;
33960+
psab_tab->len = s->sab_tab_len;
33961+
} else {
3396133962
js_free(ctx, s->sab_tab);
33962-
if (psab_tab_len)
33963-
*psab_tab_len = s->sab_tab_len;
33963+
}
3396433964
return s->dbuf.buf;
3396533965
fail:
3396633966
js_object_list_end(ctx, &s->object_list);
3396733967
js_free(ctx, s->atom_to_idx);
3396833968
js_free(ctx, s->idx_to_atom);
3396933969
dbuf_free(&s->dbuf);
3397033970
*psize = 0;
33971-
if (psab_tab)
33972-
*psab_tab = NULL;
33973-
if (psab_tab_len)
33974-
*psab_tab_len = 0;
33971+
if (psab_tab) {
33972+
psab_tab->tab = NULL;
33973+
psab_tab->len = 0;
33974+
}
3397533975
return NULL;
3397633976
}
3397733977

3397833978
uint8_t *JS_WriteObject(JSContext *ctx, size_t *psize, JSValue obj,
3397933979
int flags)
3398033980
{
33981-
return JS_WriteObject2(ctx, psize, obj, flags, NULL, NULL);
33981+
return JS_WriteObject2(ctx, psize, obj, flags, NULL);
3398233982
}
3398333983

3398433984
typedef struct BCReaderState {
@@ -35165,7 +35165,7 @@ static void bc_reader_free(BCReaderState *s)
3516535165
}
3516635166

3516735167
JSValue JS_ReadObject2(JSContext *ctx, const uint8_t *buf, size_t buf_len,
35168-
int flags, uint8_t ***psab_tab, size_t *psab_tab_len)
35168+
int flags, JSSABTab *psab_tab)
3516935169
{
3517035170
BCReaderState ss, *s = &ss;
3517135171
JSValue obj;
@@ -35190,20 +35190,20 @@ JSValue JS_ReadObject2(JSContext *ctx, const uint8_t *buf, size_t buf_len,
3519035190
} else {
3519135191
obj = JS_ReadObjectRec(s);
3519235192
}
35193-
if (psab_tab)
35194-
*psab_tab = s->sab_tab;
35195-
else
35193+
if (psab_tab) {
35194+
psab_tab->tab = s->sab_tab;
35195+
psab_tab->len = s->sab_tab_len;
35196+
} else {
3519635197
js_free(ctx, s->sab_tab);
35197-
if (psab_tab_len)
35198-
*psab_tab_len = s->sab_tab_len;
35198+
}
3519935199
bc_reader_free(s);
3520035200
return obj;
3520135201
}
3520235202

3520335203
JSValue JS_ReadObject(JSContext *ctx, const uint8_t *buf, size_t buf_len,
3520435204
int flags)
3520535205
{
35206-
return JS_ReadObject2(ctx, buf, buf_len, flags, NULL, NULL);
35206+
return JS_ReadObject2(ctx, buf, buf_len, flags, NULL);
3520735207
}
3520835208

3520935209
/*******************************************************************/

quickjs.h

+8-2
Original file line numberDiff line numberDiff line change
@@ -840,6 +840,12 @@ JS_EXTERN int JS_EnqueueJob(JSContext *ctx, JSJobFunc *job_func, int argc, JSVal
840840
JS_EXTERN JS_BOOL JS_IsJobPending(JSRuntime *rt);
841841
JS_EXTERN int JS_ExecutePendingJob(JSRuntime *rt, JSContext **pctx);
842842

843+
/* Structure to retrieve (de)serialized SharedArrayBuffer objects. */
844+
typedef struct JSSABTab {
845+
uint8_t **tab;
846+
size_t len;
847+
} JSSABTab;
848+
843849
/* Object Writer/Reader (currently only used to handle precompiled code) */
844850
#define JS_WRITE_OBJ_BYTECODE (1 << 0) /* allow function/module */
845851
#define JS_WRITE_OBJ_BSWAP (0) /* byte swapped output (obsolete, handled transparently) */
@@ -849,15 +855,15 @@ JS_EXTERN int JS_ExecutePendingJob(JSRuntime *rt, JSContext **pctx);
849855
#define JS_WRITE_OBJ_STRIP_DEBUG (1 << 5) /* do not write debug information */
850856
JS_EXTERN uint8_t *JS_WriteObject(JSContext *ctx, size_t *psize, JSValue obj, int flags);
851857
JS_EXTERN uint8_t *JS_WriteObject2(JSContext *ctx, size_t *psize, JSValue obj,
852-
int flags, uint8_t ***psab_tab, size_t *psab_tab_len);
858+
int flags, JSSABTab *psab_tab);
853859

854860
#define JS_READ_OBJ_BYTECODE (1 << 0) /* allow function/module */
855861
#define JS_READ_OBJ_ROM_DATA (0) /* avoid duplicating 'buf' data (obsolete, broken by ICs) */
856862
#define JS_READ_OBJ_SAB (1 << 2) /* allow SharedArrayBuffer */
857863
#define JS_READ_OBJ_REFERENCE (1 << 3) /* allow object references */
858864
JS_EXTERN JSValue JS_ReadObject(JSContext *ctx, const uint8_t *buf, size_t buf_len, int flags);
859865
JS_EXTERN JSValue JS_ReadObject2(JSContext *ctx, const uint8_t *buf, size_t buf_len,
860-
int flags, uint8_t ***psab_tab, size_t *psab_tab_len);
866+
int flags, JSSABTab *psab_tab);
861867
/* instantiate and evaluate a bytecode function. Only used when
862868
reading a script or module with JS_ReadObject() */
863869
JS_EXTERN JSValue JS_EvalFunction(JSContext *ctx, JSValue fun_obj);

0 commit comments

Comments
 (0)