Skip to content

Commit a28d1b8

Browse files
committed
Replace JS_NewUint32() calls with js_uint32()
1 parent 0874415 commit a28d1b8

File tree

1 file changed

+28
-23
lines changed

1 file changed

+28
-23
lines changed

quickjs.c

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1286,6 +1286,13 @@ static JSValue js_int32(int32_t v)
12861286
return JS_MKVAL(JS_TAG_INT, v);
12871287
}
12881288

1289+
static JSValue js_uint32(uint32_t v)
1290+
{
1291+
if (v <= INT32_MAX)
1292+
return js_int32(v);
1293+
return js_float64(v);
1294+
}
1295+
12891296
static void js_trigger_gc(JSRuntime *rt, size_t size)
12901297
{
12911298
BOOL force_gc;
@@ -7803,7 +7810,7 @@ static JSValue JS_GetPropertyValue(JSContext *ctx, JSValueConst this_obj,
78037810
return js_int32(p->u.array.u.int32_ptr[idx]);
78047811
case JS_CLASS_UINT32_ARRAY:
78057812
if (unlikely(idx >= p->u.array.count)) goto slow_path;
7806-
return JS_NewUint32(ctx, p->u.array.u.uint32_ptr[idx]);
7813+
return js_uint32(p->u.array.u.uint32_ptr[idx]);
78077814
case JS_CLASS_BIG_INT64_ARRAY:
78087815
if (unlikely(idx >= p->u.array.count)) goto slow_path;
78097816
return JS_NewBigInt64(ctx, p->u.array.u.int64_ptr[idx]);
@@ -7834,7 +7841,7 @@ static JSValue JS_GetPropertyValue(JSContext *ctx, JSValueConst this_obj,
78347841
JSValue JS_GetPropertyUint32(JSContext *ctx, JSValueConst this_obj,
78357842
uint32_t idx)
78367843
{
7837-
return JS_GetPropertyValue(ctx, this_obj, JS_NewUint32(ctx, idx));
7844+
return JS_GetPropertyValue(ctx, this_obj, js_uint32(idx));
78387845
}
78397846

78407847
/* Check if an object has a generalized numeric property. Return value:
@@ -8115,7 +8122,7 @@ static int set_array_length(JSContext *ctx, JSObject *p, JSValue val,
81158122
}
81168123
p->u.array.count = len;
81178124
}
8118-
p->prop[0].u.value = JS_NewUint32(ctx, len);
8125+
p->prop[0].u.value = js_uint32(len);
81198126
} else {
81208127
/* Note: length is always a uint32 because the object is an
81218128
array */
@@ -8175,7 +8182,7 @@ static int set_array_length(JSContext *ctx, JSObject *p, JSValue val,
81758182
} else {
81768183
cur_len = len;
81778184
}
8178-
set_value(ctx, &p->prop[0].u.value, JS_NewUint32(ctx, cur_len));
8185+
set_value(ctx, &p->prop[0].u.value, js_uint32(cur_len));
81798186
if (unlikely(cur_len > len)) {
81808187
return JS_ThrowTypeErrorOrFalse(ctx, flags, "not configurable");
81818188
}
@@ -8731,7 +8738,7 @@ static int JS_SetPropertyValue(JSContext *ctx, JSValueConst this_obj,
87318738
int JS_SetPropertyUint32(JSContext *ctx, JSValueConst this_obj,
87328739
uint32_t idx, JSValue val)
87338740
{
8734-
return JS_SetPropertyValue(ctx, this_obj, JS_NewUint32(ctx, idx), val,
8741+
return JS_SetPropertyValue(ctx, this_obj, js_uint32(idx), val,
87358742
JS_PROP_THROW);
87368743
}
87378744

@@ -8828,7 +8835,7 @@ static int JS_CreateProperty(JSContext *ctx, JSObject *p,
88288835
/* XXX: should update the length after defining
88298836
the property */
88308837
len = idx + 1;
8831-
set_value(ctx, &plen->u.value, JS_NewUint32(ctx, len));
8838+
set_value(ctx, &plen->u.value, js_uint32(len));
88328839
}
88338840
}
88348841
} else if (p->class_id >= JS_CLASS_UINT8C_ARRAY &&
@@ -8997,7 +9004,7 @@ int JS_DefineProperty(JSContext *ctx, JSValueConst this_obj,
89979004
return -1;
89989005
}
89999006
/* this code relies on the fact that Uint32 are never allocated */
9000-
val = (JSValueConst)JS_NewUint32(ctx, array_length);
9007+
val = (JSValueConst)js_uint32(array_length);
90019008
/* prs may have been modified */
90029009
prs = find_own_property(&pr, p, prop);
90039010
assert(prs != NULL);
@@ -9299,7 +9306,7 @@ int JS_DefinePropertyValueValue(JSContext *ctx, JSValueConst this_obj,
92999306
int JS_DefinePropertyValueUint32(JSContext *ctx, JSValueConst this_obj,
93009307
uint32_t idx, JSValue val, int flags)
93019308
{
9302-
return JS_DefinePropertyValueValue(ctx, this_obj, JS_NewUint32(ctx, idx),
9309+
return JS_DefinePropertyValueValue(ctx, this_obj, js_uint32(idx),
93039310
val, flags);
93049311
}
93059312

@@ -12735,7 +12742,7 @@ static no_inline int js_shr_slow(JSContext *ctx, JSValue *sp)
1273512742
JS_ToUint32Free(ctx, &v1, op1);
1273612743
JS_ToUint32Free(ctx, &v2, op2);
1273712744
r = v1 >> (v2 & 0x1f);
12738-
sp[-2] = JS_NewUint32(ctx, r);
12745+
sp[-2] = js_uint32(r);
1273912746
return 0;
1274012747
exception:
1274112748
sp[-2] = JS_UNDEFINED;
@@ -16445,9 +16452,7 @@ static JSValue JS_CallInternal(JSContext *caller_ctx, JSValueConst func_obj,
1644516452
uint32_t v2;
1644616453
v2 = JS_VALUE_GET_INT(op2);
1644716454
v2 &= 0x1f;
16448-
sp[-2] = JS_NewUint32(ctx,
16449-
(uint32_t)JS_VALUE_GET_INT(op1) >>
16450-
v2);
16455+
sp[-2] = js_uint32((uint32_t)JS_VALUE_GET_INT(op1) >> v2);
1645116456
sp--;
1645216457
} else {
1645316458
if (js_shr_slow(ctx, sp))
@@ -36026,7 +36031,7 @@ static JSValue js_array_constructor(JSContext *ctx, JSValueConst new_target,
3602636031
uint32_t len;
3602736032
if (JS_ToArrayLengthFree(ctx, &len, JS_DupValue(ctx, argv[0]), TRUE))
3602836033
goto fail;
36029-
if (JS_SetProperty(ctx, obj, JS_ATOM_length, JS_NewUint32(ctx, len)) < 0)
36034+
if (JS_SetProperty(ctx, obj, JS_ATOM_length, js_uint32(len)) < 0)
3603036035
goto fail;
3603136036
} else {
3603236037
for(i = 0; i < argc; i++) {
@@ -36136,7 +36141,7 @@ static JSValue js_array_from(JSContext *ctx, JSValueConst this_val,
3613636141
goto exception;
3613736142
}
3613836143
}
36139-
if (JS_SetProperty(ctx, r, JS_ATOM_length, JS_NewUint32(ctx, k)) < 0)
36144+
if (JS_SetProperty(ctx, r, JS_ATOM_length, js_uint32(k)) < 0)
3614036145
goto exception;
3614136146
goto done;
3614236147

@@ -36173,7 +36178,7 @@ static JSValue js_array_of(JSContext *ctx, JSValueConst this_val,
3617336178
goto fail;
3617436179
}
3617536180
}
36176-
if (JS_SetProperty(ctx, obj, JS_ATOM_length, JS_NewUint32(ctx, argc)) < 0) {
36181+
if (JS_SetProperty(ctx, obj, JS_ATOM_length, js_uint32(argc)) < 0) {
3617736182
fail:
3617836183
JS_FreeValue(ctx, obj);
3617936184
return JS_EXCEPTION;
@@ -37946,7 +37951,7 @@ static JSValue js_array_iterator_next(JSContext *ctx, JSValueConst this_val,
3794637951
it->idx = idx + 1;
3794737952
*pdone = FALSE;
3794837953
if (it->kind == JS_ITERATOR_KIND_KEY) {
37949-
return JS_NewUint32(ctx, idx);
37954+
return js_uint32(idx);
3795037955
} else {
3795137956
val = JS_GetPropertyUint32(ctx, it->obj, idx);
3795237957
if (JS_IsException(val))
@@ -37956,7 +37961,7 @@ static JSValue js_array_iterator_next(JSContext *ctx, JSValueConst this_val,
3795637961
} else {
3795737962
JSValueConst args[2];
3795837963
JSValue num;
37959-
num = JS_NewUint32(ctx, idx);
37964+
num = js_uint32(idx);
3796037965
args[0] = num;
3796137966
args[1] = val;
3796237967
obj = js_create_array(ctx, 2, args);
@@ -44307,7 +44312,7 @@ static JSValue js_map_get_size(JSContext *ctx, JSValueConst this_val, int magic)
4430744312
JSMapState *s = JS_GetOpaque2(ctx, this_val, JS_CLASS_MAP + magic);
4430844313
if (!s)
4430944314
return JS_EXCEPTION;
44310-
return JS_NewUint32(ctx, s->record_count);
44315+
return js_uint32(s->record_count);
4431144316
}
4431244317

4431344318
static JSValue js_map_forEach(JSContext *ctx, JSValueConst this_val,
@@ -47940,7 +47945,7 @@ static JSValue js_array_buffer_get_byteLength(JSContext *ctx,
4794047945
if (!abuf)
4794147946
return JS_EXCEPTION;
4794247947
/* return 0 if detached */
47943-
return JS_NewUint32(ctx, abuf->byte_length);
47948+
return js_uint32(abuf->byte_length);
4794447949
}
4794547950

4794647951
void JS_DetachArrayBuffer(JSContext *ctx, JSValueConst obj)
@@ -48436,7 +48441,7 @@ static JSValue js_typed_array_at(JSContext *ctx, JSValueConst this_val,
4843648441
case JS_CLASS_INT32_ARRAY:
4843748442
return js_int32(p->u.array.u.int32_ptr[idx]);
4843848443
case JS_CLASS_UINT32_ARRAY:
48439-
return JS_NewUint32(ctx, p->u.array.u.uint32_ptr[idx]);
48444+
return js_uint32(p->u.array.u.uint32_ptr[idx]);
4844048445
case JS_CLASS_FLOAT32_ARRAY:
4844148446
return __JS_NewFloat64(p->u.array.u.float_ptr[idx]);
4844248447
case JS_CLASS_FLOAT64_ARRAY:
@@ -49385,7 +49390,7 @@ static JSValue js_TA_get_int32(JSContext *ctx, const void *a) {
4938549390
}
4938649391

4938749392
static JSValue js_TA_get_uint32(JSContext *ctx, const void *a) {
49388-
return JS_NewUint32(ctx, *(const uint32_t *)a);
49393+
return js_uint32(*(const uint32_t *)a);
4938949394
}
4939049395

4939149396
static JSValue js_TA_get_int64(JSContext *ctx, const void *a) {
@@ -50041,7 +50046,7 @@ static JSValue js_dataview_getValue(JSContext *ctx,
5004150046
v = get_u32(ptr);
5004250047
if (is_swap)
5004350048
v = bswap32(v);
50044-
return JS_NewUint32(ctx, v);
50049+
return js_uint32(v);
5004550050
case JS_CLASS_BIG_INT64_ARRAY:
5004650051
{
5004750052
uint64_t v;
@@ -50447,7 +50452,7 @@ static JSValue js_atomics_op(JSContext *ctx,
5044750452
ret = js_int32(a);
5044850453
break;
5044950454
case JS_CLASS_UINT32_ARRAY:
50450-
ret = JS_NewUint32(ctx, a);
50455+
ret = js_uint32(a);
5045150456
break;
5045250457
case JS_CLASS_BIG_INT64_ARRAY:
5045350458
ret = JS_NewBigInt64(ctx, a);

0 commit comments

Comments
 (0)