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

Accept more flags in bjson read/write methods #479

Merged
merged 1 commit into from
Aug 12, 2024
Merged
Changes from all commits
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
20 changes: 14 additions & 6 deletions quickjs-libc.c
Original file line number Diff line number Diff line change
@@ -4103,14 +4103,13 @@ static JSValue js_bjson_read(JSContext *ctx, JSValue this_val,
return JS_EXCEPTION;
if (JS_ToIndex(ctx, &len, argv[2]))
return JS_EXCEPTION;
if (JS_ToInt32(ctx, &flags, argv[3]))
return JS_EXCEPTION;
buf = JS_GetArrayBuffer(ctx, &size, argv[0]);
if (!buf)
return JS_EXCEPTION;
if (pos + len > size)
return JS_ThrowRangeError(ctx, "array buffer overflow");
flags = 0;
if (JS_ToBool(ctx, argv[3]))
flags |= JS_READ_OBJ_REFERENCE;
obj = JS_ReadObject(ctx, buf + pos, len, flags);
return obj;
}
@@ -4123,9 +4122,8 @@ static JSValue js_bjson_write(JSContext *ctx, JSValue this_val,
JSValue array;
int flags;

flags = 0;
if (JS_ToBool(ctx, argv[1]))
flags |= JS_WRITE_OBJ_REFERENCE;
if (JS_ToInt32(ctx, &flags, argv[1]))
return JS_EXCEPTION;
buf = JS_WriteObject(ctx, &len, argv[0], flags);
if (!buf)
return JS_EXCEPTION;
@@ -4138,6 +4136,16 @@ static JSValue js_bjson_write(JSContext *ctx, JSValue this_val,
static const JSCFunctionListEntry js_bjson_funcs[] = {
JS_CFUNC_DEF("read", 4, js_bjson_read ),
JS_CFUNC_DEF("write", 2, js_bjson_write ),
#define DEF(x) JS_PROP_INT32_DEF(#x, JS_##x, JS_PROP_CONFIGURABLE)
DEF(READ_OBJ_BYTECODE),
DEF(READ_OBJ_REFERENCE),
DEF(READ_OBJ_SAB),
DEF(WRITE_OBJ_BYTECODE),
DEF(WRITE_OBJ_REFERENCE),
DEF(WRITE_OBJ_SAB),
DEF(WRITE_OBJ_STRIP_DEBUG),
DEF(WRITE_OBJ_STRIP_SOURCE),
#undef DEF
};

static int js_bjson_init(JSContext *ctx, JSModuleDef *m)
4 changes: 2 additions & 2 deletions tests/test_bjson.js
Original file line number Diff line number Diff line change
@@ -129,9 +129,9 @@ function bjson_test_reference()
array[i].idx = i;
array[i].typed_array = new Uint8Array(array_buffer, i, 1);
}
buf = bjson.write(array, true);
buf = bjson.write(array, bjson.WRITE_OBJ_REFERENCE);

array = bjson.read(buf, 0, buf.byteLength, true);
array = bjson.read(buf, 0, buf.byteLength, bjson.READ_OBJ_REFERENCE);

/* check the result */
for(i = 0; i < n; i++) {
Loading