Skip to content

Commit

Permalink
Clean up isnan/isinf, use isfinite
Browse files Browse the repository at this point in the history
Cleans up funky C code and uses std::isfinite, which also compiles to fewer instructions (https://godbolt.org/z/qMT8bMTKn).
  • Loading branch information
zbjornson committed Dec 8, 2021
1 parent 604db27 commit a721d51
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 16 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ project adheres to [Semantic Versioning](http://semver.org/).
* Changed `DOMPoint()` constructor to check for parameter nullability.
* Changed `DOMMatrix.js` to use string literals for non-special cases.
* Remove semicolons from Dommatrix.js.
* Clean up inf/nan macros and slightly speed up argument checking.
### Added
* Added `deregisterAllFonts` method to free up memory and reduce font conflicts.
### Fixed
Expand Down
19 changes: 3 additions & 16 deletions src/CanvasRenderingContext2d.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,6 @@

using namespace v8;

// Windows doesn't support the C99 names for these
#ifdef _MSC_VER
#define isnan(x) _isnan(x)
#define isinf(x) (!_finite(x))
#endif

#ifndef isnan
#define isnan(x) std::isnan(x)
#define isinf(x) std::isinf(x)
#endif

Nan::Persistent<FunctionTemplate> Context2d::constructor;

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

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

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

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

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

0 comments on commit a721d51

Please sign in to comment.