[gjs: 1/3] value: Fix regression converting Number to 64-bit int
- From: Philip Chimento <pchimento src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gjs: 1/3] value: Fix regression converting Number to 64-bit int
- Date: Sun, 13 Feb 2022 19:47:49 +0000 (UTC)
commit 62aecb69563fb51fb04284a5cf4140bd00e47639
Author: Philip Chimento <philip chimento gmail com>
Date: Wed Feb 9 21:15:43 2022 -0800
value: Fix regression converting Number to 64-bit int
Since accepting BigInt for introspected 64-bit values, handling
non-integer Numbers regressed, because non-integers cannot be passed to
JS::NumberToBigInt.
Truncate all non-integer Numbers to integers before converting them to
64-bit int, as JS::ToInt64/JS::ToUint64 would.
Closes: #459
gi/js-value-inl.h | 4 +++-
installed-tests/js/testRegress.js | 13 +++++++++++--
2 files changed, 14 insertions(+), 3 deletions(-)
---
diff --git a/gi/js-value-inl.h b/gi/js-value-inl.h
index ff2358d25..fe5be3c0f 100644
--- a/gi/js-value-inl.h
+++ b/gi/js-value-inl.h
@@ -247,7 +247,9 @@ GJS_JSAPI_RETURN_CONVENTION inline bool js_value_to_c_checked(
if (value.isBigInt()) {
bi = value.toBigInt();
} else if (value.isNumber()) {
- bi = JS::NumberToBigInt(cx, value.toNumber());
+ double number = value.toNumber();
+ number = std::trunc(number);
+ bi = JS::NumberToBigInt(cx, number);
if (!bi)
return false;
}
diff --git a/installed-tests/js/testRegress.js b/installed-tests/js/testRegress.js
index 91eff3544..f5935300a 100644
--- a/installed-tests/js/testRegress.js
+++ b/installed-tests/js/testRegress.js
@@ -46,6 +46,8 @@ describe('Life, the Universe and Everything', function () {
expect(Regress[method](42)).toBe(42);
expect(Regress[method](-42)).toBe(-42);
expect(Regress[method](undefined)).toBe(0);
+ expect(Regress[method](42.42)).toBe(42);
+ expect(Regress[method](-42.42)).toBe(-42);
if (bits >= 64) {
expect(Regress[method](42n)).toBe(42);
@@ -60,6 +62,7 @@ describe('Life, the Universe and Everything', function () {
const method = `test_uint${bits}`;
expect(Regress[method](42)).toBe(42);
expect(Regress[method](undefined)).toBe(0);
+ expect(Regress[method](42.42)).toBe(42);
if (bits >= 64)
expect(Regress[method](42n)).toEqual(42);
@@ -74,10 +77,15 @@ describe('Life, the Universe and Everything', function () {
expect(Regress[method](42)).toBe(42);
expect(Regress[method](-42)).toBe(-42);
- if (['float', 'double'].includes(type))
+ if (['float', 'double'].includes(type)) {
expect(Number.isNaN(Regress[method](undefined))).toBeTruthy();
- else
+ expect(Regress[method](42.42)).toBeCloseTo(42.42);
+ expect(Regress[method](-42.42)).toBeCloseTo(-42.42);
+ } else {
expect(Regress[method](undefined)).toBe(0);
+ expect(Regress[method](42.42)).toBe(42);
+ expect(Regress[method](-42.42)).toBe(-42);
+ }
if (bit64Types.includes(type)) {
expect(Regress[method](42n)).toBe(42);
@@ -94,6 +102,7 @@ describe('Life, the Universe and Everything', function () {
const method = `test_${type}`;
expect(Regress[method](42)).toBe(42);
expect(Regress[method](undefined)).toBe(0);
+ expect(Regress[method](42.42)).toBe(42);
if (bit64Types.includes(type))
expect(Regress[method](42n)).toBe(42);
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]