Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Introduce JS_ReadObject2 #468

Merged
merged 2 commits into from
Jul 15, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 23 additions & 3 deletions quickjs.c
Original file line number Diff line number Diff line change
Expand Up @@ -33995,7 +33995,10 @@ typedef struct BCReaderState {
JSObject **objects;
int objects_count;
int objects_size;

/* SAB references */
uint8_t **sab_tab;
int sab_tab_len;
int sab_tab_size;
/* used for DUMP_READ_OBJECT */
const uint8_t *ptr_last;
int level;
Expand Down Expand Up @@ -34903,6 +34906,11 @@ static JSValue JS_ReadSharedArrayBuffer(BCReaderState *s)
if (bc_get_u64(s, &u64))
return JS_EXCEPTION;
data_ptr = (uint8_t *)(uintptr_t)u64;
if (js_resize_array(s->ctx, (void **)&s->sab_tab, sizeof(s->sab_tab[0]),
&s->sab_tab_size, s->sab_tab_len + 1))
return JS_EXCEPTION;
/* keep the SAB pointer so that the user can clone it or free it */
s->sab_tab[s->sab_tab_len++] = data_ptr;
/* the SharedArrayBuffer is cloned */
obj = js_array_buffer_constructor3(ctx, JS_UNDEFINED, byte_length,
JS_CLASS_SHARED_ARRAY_BUFFER,
Expand Down Expand Up @@ -35156,8 +35164,8 @@ static void bc_reader_free(BCReaderState *s)
js_free(s->ctx, s->objects);
}

JSValue JS_ReadObject(JSContext *ctx, const uint8_t *buf, size_t buf_len,
int flags)
JSValue JS_ReadObject2(JSContext *ctx, const uint8_t *buf, size_t buf_len,
int flags, uint8_t ***psab_tab, size_t *psab_tab_len)
{
BCReaderState ss, *s = &ss;
JSValue obj;
Expand All @@ -35182,10 +35190,22 @@ JSValue JS_ReadObject(JSContext *ctx, const uint8_t *buf, size_t buf_len,
} else {
obj = JS_ReadObjectRec(s);
}
if (psab_tab)
*psab_tab = s->sab_tab;
else
js_free(ctx, s->sab_tab);
if (psab_tab_len)
*psab_tab_len = s->sab_tab_len;
bc_reader_free(s);
return obj;
}

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

/*******************************************************************/
/* runtime functions & objects */

Expand Down
2 changes: 2 additions & 0 deletions quickjs.h
Original file line number Diff line number Diff line change
Expand Up @@ -856,6 +856,8 @@ JS_EXTERN uint8_t *JS_WriteObject2(JSContext *ctx, size_t *psize, JSValue obj,
#define JS_READ_OBJ_SAB (1 << 2) /* allow SharedArrayBuffer */
#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);
/* 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);
Expand Down
Loading