[gjs: 2/3] testRegress: Verify that undefined can be passed to C functions
- From: Philip Chimento <pchimento src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gjs: 2/3] testRegress: Verify that undefined can be passed to C functions
- Date: Fri, 12 Mar 2021 01:31:19 +0000 (UTC)
commit 255af607ec3dc3e3d7e20738a775800fc50e5b30
Author: Marco Trevisan (TreviƱo) <mail 3v1n0 net>
Date: Mon Mar 8 22:01:26 2021 +0100
testRegress: Verify that undefined can be passed to C functions
When a C function takes integer values an undefined argument should be
handled as 0, as expected in JS, if instead the function takes a
floating point value the function value is set to NaN.
Add tests to ensure we handle undefined in function calls as expected.
Fixes: #379
gi/js-value-inl.h | 7 +++++++
installed-tests/js/testRegress.js | 14 ++++++++++++--
2 files changed, 19 insertions(+), 2 deletions(-)
---
diff --git a/gi/js-value-inl.h b/gi/js-value-inl.h
index da1d5135..7a73b72e 100644
--- a/gi/js-value-inl.h
+++ b/gi/js-value-inl.h
@@ -197,6 +197,13 @@ GJS_JSAPI_RETURN_CONVENTION inline bool js_value_to_c_checked(
if constexpr (std::is_same_v<WantedType, T>)
return js_value_to_c(cx, value, out);
+ if constexpr (std::is_integral_v<WantedType>) {
+ if (value.isUndefined()) {
+ *out = 0;
+ return true;
+ }
+ }
+
if constexpr (std::is_arithmetic_v<T>) {
bool ret = js_value_to_c(cx, value, out);
if (out_of_range) {
diff --git a/installed-tests/js/testRegress.js b/installed-tests/js/testRegress.js
index 56296d94..f067f1a1 100644
--- a/installed-tests/js/testRegress.js
+++ b/installed-tests/js/testRegress.js
@@ -28,10 +28,13 @@ describe('Life, the Universe and Everything', function () {
const method = `test_int${bits}`;
expect(Regress[method](42)).toBe(42);
expect(Regress[method](-42)).toBe(-42);
+ expect(Regress[method](undefined)).toBe(0);
});
it(`includes unsigned ${bits}-bit integers`, function () {
- expect(Regress[`test_uint${bits}`](42)).toBe(42);
+ const method = `test_uint${bits}`;
+ expect(Regress[method](42)).toBe(42);
+ expect(Regress[method](undefined)).toBe(0);
});
});
@@ -40,12 +43,19 @@ describe('Life, the Universe and Everything', function () {
const method = `test_${type}`;
expect(Regress[method](42)).toBe(42);
expect(Regress[method](-42)).toBe(-42);
+
+ if (['float', 'double'].includes(type))
+ expect(Number.isNaN(Regress[method](undefined))).toBeTruthy();
+ else
+ expect(Regress[method](undefined)).toBe(0);
});
});
['ushort', 'uint', 'ulong', 'size'].forEach(type => {
it(`includes ${type}s`, function () {
- expect(Regress[`test_${type}`](42)).toBe(42);
+ const method = `test_${type}`;
+ expect(Regress[method](42)).toBe(42);
+ expect(Regress[method](undefined)).toBe(0);
});
});
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]