Skip to content

Commit da1d3cb

Browse files
authoredAug 12, 2024
Accept more flags in bjson read/write methods (#479)
Change the last argument from a boolean to an integer and export the JS_READ_* and JS_WRITE_* flags on the bjson module object.
1 parent 5a50ce3 commit da1d3cb

File tree

2 files changed

+16
-8
lines changed

2 files changed

+16
-8
lines changed
 

‎quickjs-libc.c

+14-6
Original file line numberDiff line numberDiff line change
@@ -4103,14 +4103,13 @@ static JSValue js_bjson_read(JSContext *ctx, JSValue this_val,
41034103
return JS_EXCEPTION;
41044104
if (JS_ToIndex(ctx, &len, argv[2]))
41054105
return JS_EXCEPTION;
4106+
if (JS_ToInt32(ctx, &flags, argv[3]))
4107+
return JS_EXCEPTION;
41064108
buf = JS_GetArrayBuffer(ctx, &size, argv[0]);
41074109
if (!buf)
41084110
return JS_EXCEPTION;
41094111
if (pos + len > size)
41104112
return JS_ThrowRangeError(ctx, "array buffer overflow");
4111-
flags = 0;
4112-
if (JS_ToBool(ctx, argv[3]))
4113-
flags |= JS_READ_OBJ_REFERENCE;
41144113
obj = JS_ReadObject(ctx, buf + pos, len, flags);
41154114
return obj;
41164115
}
@@ -4123,9 +4122,8 @@ static JSValue js_bjson_write(JSContext *ctx, JSValue this_val,
41234122
JSValue array;
41244123
int flags;
41254124

4126-
flags = 0;
4127-
if (JS_ToBool(ctx, argv[1]))
4128-
flags |= JS_WRITE_OBJ_REFERENCE;
4125+
if (JS_ToInt32(ctx, &flags, argv[1]))
4126+
return JS_EXCEPTION;
41294127
buf = JS_WriteObject(ctx, &len, argv[0], flags);
41304128
if (!buf)
41314129
return JS_EXCEPTION;
@@ -4138,6 +4136,16 @@ static JSValue js_bjson_write(JSContext *ctx, JSValue this_val,
41384136
static const JSCFunctionListEntry js_bjson_funcs[] = {
41394137
JS_CFUNC_DEF("read", 4, js_bjson_read ),
41404138
JS_CFUNC_DEF("write", 2, js_bjson_write ),
4139+
#define DEF(x) JS_PROP_INT32_DEF(#x, JS_##x, JS_PROP_CONFIGURABLE)
4140+
DEF(READ_OBJ_BYTECODE),
4141+
DEF(READ_OBJ_REFERENCE),
4142+
DEF(READ_OBJ_SAB),
4143+
DEF(WRITE_OBJ_BYTECODE),
4144+
DEF(WRITE_OBJ_REFERENCE),
4145+
DEF(WRITE_OBJ_SAB),
4146+
DEF(WRITE_OBJ_STRIP_DEBUG),
4147+
DEF(WRITE_OBJ_STRIP_SOURCE),
4148+
#undef DEF
41414149
};
41424150

41434151
static int js_bjson_init(JSContext *ctx, JSModuleDef *m)

‎tests/test_bjson.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -129,9 +129,9 @@ function bjson_test_reference()
129129
array[i].idx = i;
130130
array[i].typed_array = new Uint8Array(array_buffer, i, 1);
131131
}
132-
buf = bjson.write(array, true);
132+
buf = bjson.write(array, bjson.WRITE_OBJ_REFERENCE);
133133

134-
array = bjson.read(buf, 0, buf.byteLength, true);
134+
array = bjson.read(buf, 0, buf.byteLength, bjson.READ_OBJ_REFERENCE);
135135

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

0 commit comments

Comments
 (0)