Skip to content

Commit a721d51

Browse files
committed
Clean up isnan/isinf, use isfinite
Cleans up funky C code and uses std::isfinite, which also compiles to fewer instructions (https://godbolt.org/z/qMT8bMTKn).
1 parent 604db27 commit a721d51

File tree

2 files changed

+4
-16
lines changed

2 files changed

+4
-16
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ project adheres to [Semantic Versioning](http://semver.org/).
1212
* Changed `DOMPoint()` constructor to check for parameter nullability.
1313
* Changed `DOMMatrix.js` to use string literals for non-special cases.
1414
* Remove semicolons from Dommatrix.js.
15+
* Clean up inf/nan macros and slightly speed up argument checking.
1516
### Added
1617
* Added `deregisterAllFonts` method to free up memory and reduce font conflicts.
1718
### Fixed

src/CanvasRenderingContext2d.cc

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,6 @@
2121

2222
using namespace v8;
2323

24-
// Windows doesn't support the C99 names for these
25-
#ifdef _MSC_VER
26-
#define isnan(x) _isnan(x)
27-
#define isinf(x) (!_finite(x))
28-
#endif
29-
30-
#ifndef isnan
31-
#define isnan(x) std::isnan(x)
32-
#define isinf(x) std::isinf(x)
33-
#endif
34-
3524
Nan::Persistent<FunctionTemplate> Context2d::constructor;
3625

3726
/*
@@ -77,9 +66,7 @@ inline static bool checkArgs(const Nan::FunctionCallbackInfo<Value> &info, doubl
7766
double val = Nan::To<double>(info[i]).FromMaybe(0);
7867

7968
if (areArgsValid) {
80-
if (val != val ||
81-
val == std::numeric_limits<double>::infinity() ||
82-
val == -std::numeric_limits<double>::infinity()) {
69+
if (!std::isfinite(val)) {
8370
// We should continue the loop instead of returning immediately
8471
// See https://html.spec.whatwg.org/multipage/canvas.html
8572

@@ -2787,7 +2774,7 @@ NAN_METHOD(Context2d::SetLineDash) {
27872774
if (!d->IsNumber()) return;
27882775
a[i] = Nan::To<double>(d).FromMaybe(0);
27892776
if (a[i] == 0) zero_dashes++;
2790-
if (a[i] < 0 || isnan(a[i]) || isinf(a[i])) return;
2777+
if (a[i] < 0 || !std::isfinite(a[i])) return;
27912778
}
27922779

27932780
Context2d *context = Nan::ObjectWrap::Unwrap<Context2d>(info.This());
@@ -2827,7 +2814,7 @@ NAN_METHOD(Context2d::GetLineDash) {
28272814
*/
28282815
NAN_SETTER(Context2d::SetLineDashOffset) {
28292816
double offset = Nan::To<double>(value).FromMaybe(0);
2830-
if (isnan(offset) || isinf(offset)) return;
2817+
if (!std::isfinite(offset)) return;
28312818

28322819
Context2d *context = Nan::ObjectWrap::Unwrap<Context2d>(info.This());
28332820
cairo_t *ctx = context->context();

0 commit comments

Comments
 (0)