Skip to content

Commit 0658d9c

Browse files
authored
Fix js_math_imul (#356)
- follow ECMA specification - remove implementation defined signed conversion
1 parent 97c9186 commit 0658d9c

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

quickjs.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40943,16 +40943,16 @@ static double js_math_fround(double a)
4094340943
static JSValue js_math_imul(JSContext *ctx, JSValue this_val,
4094440944
int argc, JSValue *argv)
4094540945
{
40946-
int a, b;
40946+
uint32_t a, b, c;
40947+
int32_t d;
4094740948

40948-
if (JS_ToInt32(ctx, &a, argv[0]))
40949+
if (JS_ToUint32(ctx, &a, argv[0]))
4094940950
return JS_EXCEPTION;
40950-
if (JS_ToInt32(ctx, &b, argv[1]))
40951+
if (JS_ToUint32(ctx, &b, argv[1]))
4095140952
return JS_EXCEPTION;
40952-
/* TODO(bnoordhuis) Signed integral narrowing has implementation-defined
40953-
* behavior but that's a step up from the undefined behavior it replaced.
40954-
*/
40955-
return js_int32((int64_t)a * (int64_t)b);
40953+
c = a * b;
40954+
memcpy(&d, &c, sizeof(d));
40955+
return js_int32(d);
4095640956
}
4095740957

4095840958
static JSValue js_math_clz32(JSContext *ctx, JSValue this_val,

0 commit comments

Comments
 (0)