Skip to content

Commit 51284bc

Browse files
authored
DRY typed array type checking (#855)
1 parent ea5046b commit 51284bc

File tree

1 file changed

+25
-36
lines changed

1 file changed

+25
-36
lines changed

quickjs.c

Lines changed: 25 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1220,6 +1220,7 @@ static JSValue js_typed_array_constructor_ta(JSContext *ctx,
12201220
JSValue new_target,
12211221
JSValue src_obj,
12221222
int classid, uint32_t len);
1223+
static bool is_typed_array(JSClassID class_id);
12231224
static bool typed_array_is_oob(JSObject *p);
12241225
static uint32_t typed_array_get_length(JSContext *ctx, JSObject *p);
12251226
static JSValue JS_ThrowTypeErrorDetachedArrayBuffer(JSContext *ctx);
@@ -7454,12 +7455,10 @@ static JSValue JS_GetPropertyInternal2(JSContext *ctx, JSValue obj,
74547455
if (idx < p->u.array.count) {
74557456
/* we avoid duplicating the code */
74567457
return JS_GetPropertyUint32(ctx, JS_MKPTR(JS_TAG_OBJECT, p), idx);
7457-
} else if (p->class_id >= JS_CLASS_UINT8C_ARRAY &&
7458-
p->class_id <= JS_CLASS_FLOAT64_ARRAY) {
7458+
} else if (is_typed_array(p->class_id)) {
74597459
return JS_UNDEFINED;
74607460
}
7461-
} else if (p->class_id >= JS_CLASS_UINT8C_ARRAY &&
7462-
p->class_id <= JS_CLASS_FLOAT64_ARRAY) {
7461+
} else if (is_typed_array(p->class_id)) {
74637462
int ret;
74647463
ret = JS_AtomIsNumericIndex(ctx, prop);
74657464
if (ret != 0) {
@@ -8135,8 +8134,7 @@ int JS_HasProperty(JSContext *ctx, JSValue obj, JSAtom prop)
81358134
JS_FreeValue(ctx, JS_MKPTR(JS_TAG_OBJECT, p));
81368135
if (ret != 0)
81378136
return ret;
8138-
if (p->class_id >= JS_CLASS_UINT8C_ARRAY &&
8139-
p->class_id <= JS_CLASS_FLOAT64_ARRAY) {
8137+
if (is_typed_array(p->class_id)) {
81408138
ret = JS_AtomIsNumericIndex(ctx, prop);
81418139
if (ret != 0) {
81428140
if (ret < 0)
@@ -8773,12 +8771,10 @@ static int JS_SetPropertyInternal2(JSContext *ctx, JSValue obj, JSAtom prop,
87738771
return JS_SetPropertyValue(ctx, this_obj, js_int32(idx), val, flags);
87748772
else
87758773
break;
8776-
} else if (p1->class_id >= JS_CLASS_UINT8C_ARRAY &&
8777-
p1->class_id <= JS_CLASS_FLOAT64_ARRAY) {
8774+
} else if (is_typed_array(p1->class_id)) {
87788775
goto typed_array_oob;
87798776
}
8780-
} else if (p1->class_id >= JS_CLASS_UINT8C_ARRAY &&
8781-
p1->class_id <= JS_CLASS_FLOAT64_ARRAY) {
8777+
} else if (is_typed_array(p1->class_id)) {
87828778
ret = JS_AtomIsNumericIndex(ctx, prop);
87838779
if (ret != 0) {
87848780
if (ret < 0)
@@ -9230,8 +9226,7 @@ static int JS_CreateProperty(JSContext *ctx, JSObject *p,
92309226
set_value(ctx, &plen->u.value, js_uint32(len));
92319227
}
92329228
}
9233-
} else if (p->class_id >= JS_CLASS_UINT8C_ARRAY &&
9234-
p->class_id <= JS_CLASS_FLOAT64_ARRAY) {
9229+
} else if (is_typed_array(p->class_id)) {
92359230
ret = JS_AtomIsNumericIndex(ctx, prop);
92369231
if (ret != 0) {
92379232
if (ret < 0)
@@ -9587,8 +9582,7 @@ int JS_DefineProperty(JSContext *ctx, JSValue this_obj,
95879582
return true;
95889583
}
95899584
}
9590-
} else if (p->class_id >= JS_CLASS_UINT8C_ARRAY &&
9591-
p->class_id <= JS_CLASS_FLOAT64_ARRAY) {
9585+
} else if (is_typed_array(p->class_id)) {
95929586
JSValue num;
95939587
int ret;
95949588

@@ -34491,8 +34485,7 @@ static int JS_WriteObjectRec(BCWriterState *s, JSValue obj)
3449134485
ret = JS_WriteSet(s, p->u.map_state);
3449234486
break;
3449334487
default:
34494-
if (p->class_id >= JS_CLASS_UINT8C_ARRAY &&
34495-
p->class_id <= JS_CLASS_FLOAT64_ARRAY) {
34488+
if (is_typed_array(p->class_id)) {
3449634489
ret = JS_WriteTypedArray(s, obj);
3449734490
} else {
3449834491
JS_ThrowTypeError(s->ctx, "unsupported object class");
@@ -38622,8 +38615,7 @@ static JSObject *get_typed_array(JSContext *ctx, JSValue this_val)
3862238615
if (JS_VALUE_GET_TAG(this_val) != JS_TAG_OBJECT)
3862338616
goto fail;
3862438617
p = JS_VALUE_GET_OBJ(this_val);
38625-
if (!(p->class_id >= JS_CLASS_UINT8C_ARRAY &&
38626-
p->class_id <= JS_CLASS_FLOAT64_ARRAY)) {
38618+
if (!is_typed_array(p->class_id)) {
3862738619
fail:
3862838620
JS_ThrowTypeError(ctx, "not a TypedArray");
3862938621
return NULL;
@@ -40124,8 +40116,7 @@ static JSValue js_array_iterator_next(JSContext *ctx, JSValue this_val,
4012440116
if (JS_IsUndefined(it->obj))
4012540117
goto done;
4012640118
p = JS_VALUE_GET_OBJ(it->obj);
40127-
if (p->class_id >= JS_CLASS_UINT8C_ARRAY &&
40128-
p->class_id <= JS_CLASS_FLOAT64_ARRAY) {
40119+
if (is_typed_array(p->class_id)) {
4012940120
if (typed_array_is_oob(p)) {
4013040121
JS_ThrowTypeErrorArrayBufferOOB(ctx);
4013140122
goto fail1;
@@ -52067,16 +52058,13 @@ static JSValue js_array_buffer_isView(JSContext *ctx,
5206752058
int argc, JSValue *argv)
5206852059
{
5206952060
JSObject *p;
52070-
bool res;
52071-
res = false;
52061+
5207252062
if (JS_VALUE_GET_TAG(argv[0]) == JS_TAG_OBJECT) {
5207352063
p = JS_VALUE_GET_OBJ(argv[0]);
52074-
if (p->class_id >= JS_CLASS_UINT8C_ARRAY &&
52075-
p->class_id <= JS_CLASS_DATAVIEW) {
52076-
res = true;
52077-
}
52064+
return js_bool(is_typed_array(p->class_id) ||
52065+
p->class_id == JS_CLASS_DATAVIEW);
5207852066
}
52079-
return js_bool(res);
52067+
return JS_FALSE;
5208052068
}
5208152069

5208252070
static const JSCFunctionListEntry js_array_buffer_funcs[] = {
@@ -52431,6 +52419,11 @@ static const JSCFunctionListEntry js_shared_array_buffer_proto_funcs[] = {
5243152419
JS_PROP_STRING_DEF("[Symbol.toStringTag]", "SharedArrayBuffer", JS_PROP_CONFIGURABLE ),
5243252420
};
5243352421

52422+
static bool is_typed_array(JSClassID class_id)
52423+
{
52424+
return class_id >= JS_CLASS_UINT8C_ARRAY && class_id <= JS_CLASS_FLOAT64_ARRAY;
52425+
}
52426+
5243452427
// is the typed array detached or out of bounds relative to its RAB?
5243552428
// |p| must be a typed array, *not* a DataView
5243652429
static bool typed_array_is_oob(JSObject *p)
@@ -52440,8 +52433,7 @@ static bool typed_array_is_oob(JSObject *p)
5244052433
int len, size_elem;
5244152434
int64_t end;
5244252435

52443-
assert(p->class_id >= JS_CLASS_UINT8C_ARRAY);
52444-
assert(p->class_id <= JS_CLASS_FLOAT64_ARRAY);
52436+
assert(is_typed_array(p->class_id));
5244552437

5244652438
ta = p->u.typed_array;
5244752439
abuf = ta->buffer->u.array_buffer;
@@ -52603,8 +52595,7 @@ static JSValue js_typed_array_get_toStringTag(JSContext *ctx,
5260352595
if (JS_VALUE_GET_TAG(this_val) != JS_TAG_OBJECT)
5260452596
return JS_UNDEFINED;
5260552597
p = JS_VALUE_GET_OBJ(this_val);
52606-
if (!(p->class_id >= JS_CLASS_UINT8C_ARRAY &&
52607-
p->class_id <= JS_CLASS_FLOAT64_ARRAY))
52598+
if (!is_typed_array(p->class_id))
5260852599
return JS_UNDEFINED;
5260952600
return JS_AtomToString(ctx, ctx->rt->class_array[p->class_id].class_name);
5261052601
}
@@ -52637,8 +52628,7 @@ static JSValue js_typed_array_set_internal(JSContext *ctx,
5263752628
if (JS_IsException(src_obj))
5263852629
goto fail;
5263952630
src_p = JS_VALUE_GET_OBJ(src_obj);
52640-
if (src_p->class_id >= JS_CLASS_UINT8C_ARRAY &&
52641-
src_p->class_id <= JS_CLASS_FLOAT64_ARRAY) {
52631+
if (is_typed_array(src_p->class_id)) {
5264252632
JSTypedArray *dest_ta = p->u.typed_array;
5264352633
JSArrayBuffer *dest_abuf = dest_ta->buffer->u.array_buffer;
5264452634
JSTypedArray *src_ta = src_p->u.typed_array;
@@ -54372,8 +54362,7 @@ static JSValue js_typed_array_constructor(JSContext *ctx,
5437254362
}
5437354363
buffer = js_dup(argv[0]);
5437454364
} else {
54375-
if (p->class_id >= JS_CLASS_UINT8C_ARRAY &&
54376-
p->class_id <= JS_CLASS_FLOAT64_ARRAY) {
54365+
if (is_typed_array(p->class_id)) {
5437754366
return js_typed_array_constructor_ta(ctx, new_target, argv[0],
5437854367
classid, p->u.array.count);
5437954368
} else {
@@ -54852,7 +54841,7 @@ JSValue JS_NewUint8ArrayCopy(JSContext *ctx, const uint8_t *buf, size_t len)
5485254841
int JS_GetTypedArrayType(JSValue obj)
5485354842
{
5485454843
JSClassID class_id = JS_GetClassID(obj);
54855-
if (class_id >= JS_CLASS_UINT8C_ARRAY && class_id <= JS_CLASS_FLOAT64_ARRAY)
54844+
if (is_typed_array(class_id))
5485654845
return class_id - JS_CLASS_UINT8C_ARRAY;
5485754846
else
5485854847
return -1;

0 commit comments

Comments
 (0)