mirror of
https://github.com/bellard/quickjs.git
synced 2025-11-16 18:51:51 +03:00
fixed BJSON array serialization (#457)
This commit is contained in:
76
quickjs.c
76
quickjs.c
@@ -37258,14 +37258,17 @@ static int JS_WriteModule(BCWriterState *s, JSValueConst obj)
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* XXX: be compatible with the structured clone algorithm */
|
||||||
static int JS_WriteArray(BCWriterState *s, JSValueConst obj)
|
static int JS_WriteArray(BCWriterState *s, JSValueConst obj)
|
||||||
{
|
{
|
||||||
|
JSContext *ctx = s->ctx;
|
||||||
JSObject *p = JS_VALUE_GET_OBJ(obj);
|
JSObject *p = JS_VALUE_GET_OBJ(obj);
|
||||||
uint32_t i, len;
|
uint32_t i, len;
|
||||||
JSValue val;
|
|
||||||
int ret;
|
int ret;
|
||||||
BOOL is_template;
|
BOOL is_template;
|
||||||
|
JSShapeProperty *prs;
|
||||||
|
JSProperty *pr;
|
||||||
|
|
||||||
if (s->allow_bytecode && !p->extensible) {
|
if (s->allow_bytecode && !p->extensible) {
|
||||||
/* not extensible array: we consider it is a
|
/* not extensible array: we consider it is a
|
||||||
template when we are saving bytecode */
|
template when we are saving bytecode */
|
||||||
@@ -37275,29 +37278,62 @@ static int JS_WriteArray(BCWriterState *s, JSValueConst obj)
|
|||||||
bc_put_u8(s, BC_TAG_ARRAY);
|
bc_put_u8(s, BC_TAG_ARRAY);
|
||||||
is_template = FALSE;
|
is_template = FALSE;
|
||||||
}
|
}
|
||||||
if (js_get_length32(s->ctx, &len, obj))
|
if (js_get_length32(ctx, &len, obj)) /* no side effect */
|
||||||
goto fail1;
|
goto fail;
|
||||||
bc_put_leb128(s, len);
|
bc_put_leb128(s, len);
|
||||||
for(i = 0; i < len; i++) {
|
if (p->fast_array) {
|
||||||
val = JS_GetPropertyUint32(s->ctx, obj, i);
|
for(i = 0; i < p->u.array.count; i++) {
|
||||||
if (JS_IsException(val))
|
ret = JS_WriteObjectRec(s, p->u.array.u.values[i]);
|
||||||
goto fail1;
|
if (ret)
|
||||||
ret = JS_WriteObjectRec(s, val);
|
goto fail;
|
||||||
JS_FreeValue(s->ctx, val);
|
}
|
||||||
if (ret)
|
for(i = p->u.array.count; i < len; i++) {
|
||||||
goto fail1;
|
ret = JS_WriteObjectRec(s, JS_UNDEFINED);
|
||||||
|
if (ret)
|
||||||
|
goto fail;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
for(i = 0; i < len; i++) {
|
||||||
|
JSAtom atom;
|
||||||
|
atom = JS_NewAtomUInt32(ctx, i);
|
||||||
|
if (atom == JS_ATOM_NULL)
|
||||||
|
goto fail;
|
||||||
|
prs = find_own_property(&pr, p, atom);
|
||||||
|
JS_FreeAtom(ctx, atom);
|
||||||
|
if (prs && (prs->flags & JS_PROP_ENUMERABLE)) {
|
||||||
|
if (prs->flags & JS_PROP_TMASK) {
|
||||||
|
JS_ThrowTypeError(ctx, "only value properties are supported");
|
||||||
|
goto fail;
|
||||||
|
}
|
||||||
|
ret = JS_WriteObjectRec(s, pr->u.value);
|
||||||
|
if (ret)
|
||||||
|
goto fail;
|
||||||
|
} else {
|
||||||
|
ret = JS_WriteObjectRec(s, JS_UNDEFINED);
|
||||||
|
if (ret)
|
||||||
|
goto fail;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (is_template) {
|
if (is_template) {
|
||||||
val = JS_GetProperty(s->ctx, obj, JS_ATOM_raw);
|
/* the 'raw' property is not enumerable */
|
||||||
if (JS_IsException(val))
|
prs = find_own_property(&pr, p, JS_ATOM_raw);
|
||||||
goto fail1;
|
if (prs) {
|
||||||
ret = JS_WriteObjectRec(s, val);
|
if (prs->flags & JS_PROP_TMASK) {
|
||||||
JS_FreeValue(s->ctx, val);
|
JS_ThrowTypeError(ctx, "only value properties are supported");
|
||||||
if (ret)
|
goto fail;
|
||||||
goto fail1;
|
}
|
||||||
|
ret = JS_WriteObjectRec(s, pr->u.value);
|
||||||
|
if (ret)
|
||||||
|
goto fail;
|
||||||
|
} else {
|
||||||
|
ret = JS_WriteObjectRec(s, JS_UNDEFINED);
|
||||||
|
if (ret)
|
||||||
|
goto fail;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
fail1:
|
fail:
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -184,7 +184,18 @@ function bjson_test_all()
|
|||||||
var obj;
|
var obj;
|
||||||
|
|
||||||
bjson_test({x:1, y:2, if:3});
|
bjson_test({x:1, y:2, if:3});
|
||||||
|
|
||||||
bjson_test([1, 2, 3]);
|
bjson_test([1, 2, 3]);
|
||||||
|
|
||||||
|
/* array with holes */
|
||||||
|
bjson_test([1, , 2, , 3]);
|
||||||
|
|
||||||
|
/* fast array with hole */
|
||||||
|
obj = new Array(5);
|
||||||
|
obj[0] = 1;
|
||||||
|
obj[1] = 2;
|
||||||
|
bjson_test(obj);
|
||||||
|
|
||||||
bjson_test([1.0, "aa", true, false, undefined, null, NaN, -Infinity, -0.0]);
|
bjson_test([1.0, "aa", true, false, undefined, null, NaN, -Infinity, -0.0]);
|
||||||
if (typeof BigInt !== "undefined") {
|
if (typeof BigInt !== "undefined") {
|
||||||
bjson_test([BigInt("1"), -BigInt("0x123456789"),
|
bjson_test([BigInt("1"), -BigInt("0x123456789"),
|
||||||
|
|||||||
Reference in New Issue
Block a user