Skip to content

Commit

Permalink
Bug 1364875: Append double in DOMMatrix stringifier r=bzbarsky,jwalden
Browse files Browse the repository at this point in the history
Fixed the stringifier as the spec mandates [using full double precision](w3c/fxtf-drafts#120 (comment)).

Differential Revision: https://phabricator.services.mozilla.com/D35593
  • Loading branch information
saschanaz committed Jul 11, 2019
1 parent b06a2ec commit 190682d
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 254 deletions.
83 changes: 33 additions & 50 deletions dom/base/DOMMatrix.cpp
Expand Up @@ -19,7 +19,8 @@

#include <math.h>

#include "js/Equality.h" // JS::SameValueZero
#include "js/Conversions.h" // JS::NumberToString
#include "js/Equality.h" // JS::SameValueZero

namespace mozilla {
namespace dom {
Expand Down Expand Up @@ -470,65 +471,47 @@ void DOMMatrixReadOnly::ToFloat64Array(JSContext* aCx,
aResult.set(&value.toObject());
}

// Convenient way to append things as floats, not doubles. We use this because
// we only want to output about 6 digits of precision for our matrix()
// functions, to preserve the behavior we used to have when we used
// AppendPrintf.
static void AppendFloat(nsAString& aStr, float f) { aStr.AppendFloat(f); }

void DOMMatrixReadOnly::Stringify(nsAString& aResult) {
void DOMMatrixReadOnly::Stringify(nsAString& aResult, ErrorResult& aRv) {
char cbuf[JS::MaximumNumberToStringLength];
nsAutoString matrixStr;
auto AppendDouble = [&aRv, &cbuf, &matrixStr](double d,
bool isLastItem = false) {
if (!mozilla::IsFinite(d)) {
aRv.ThrowDOMException(
NS_ERROR_DOM_INVALID_STATE_ERR,
NS_LITERAL_CSTRING(
"Matrix with a non-finite element cannot be stringified."));
return false;
}
JS::NumberToString(d, cbuf);
matrixStr.AppendASCII(cbuf);
if (!isLastItem) {
matrixStr.AppendLiteral(", ");
}
return true;
};

if (mMatrix3D) {
// We can't use AppendPrintf here, because it does locale-specific
// formatting of floating-point values.
matrixStr.AssignLiteral("matrix3d(");
AppendFloat(matrixStr, M11());
matrixStr.AppendLiteral(", ");
AppendFloat(matrixStr, M12());
matrixStr.AppendLiteral(", ");
AppendFloat(matrixStr, M13());
matrixStr.AppendLiteral(", ");
AppendFloat(matrixStr, M14());
matrixStr.AppendLiteral(", ");
AppendFloat(matrixStr, M21());
matrixStr.AppendLiteral(", ");
AppendFloat(matrixStr, M22());
matrixStr.AppendLiteral(", ");
AppendFloat(matrixStr, M23());
matrixStr.AppendLiteral(", ");
AppendFloat(matrixStr, M24());
matrixStr.AppendLiteral(", ");
AppendFloat(matrixStr, M31());
matrixStr.AppendLiteral(", ");
AppendFloat(matrixStr, M32());
matrixStr.AppendLiteral(", ");
AppendFloat(matrixStr, M33());
matrixStr.AppendLiteral(", ");
AppendFloat(matrixStr, M34());
matrixStr.AppendLiteral(", ");
AppendFloat(matrixStr, M41());
matrixStr.AppendLiteral(", ");
AppendFloat(matrixStr, M42());
matrixStr.AppendLiteral(", ");
AppendFloat(matrixStr, M43());
matrixStr.AppendLiteral(", ");
AppendFloat(matrixStr, M44());
if (!AppendDouble(M11()) || !AppendDouble(M12()) || !AppendDouble(M13()) ||
!AppendDouble(M14()) || !AppendDouble(M21()) || !AppendDouble(M22()) ||
!AppendDouble(M23()) || !AppendDouble(M24()) || !AppendDouble(M31()) ||
!AppendDouble(M32()) || !AppendDouble(M33()) || !AppendDouble(M34()) ||
!AppendDouble(M41()) || !AppendDouble(M42()) || !AppendDouble(M43()) ||
!AppendDouble(M44(), true)) {
return;
}
matrixStr.AppendLiteral(")");
} else {
// We can't use AppendPrintf here, because it does locale-specific
// formatting of floating-point values.
matrixStr.AssignLiteral("matrix(");
AppendFloat(matrixStr, A());
matrixStr.AppendLiteral(", ");
AppendFloat(matrixStr, B());
matrixStr.AppendLiteral(", ");
AppendFloat(matrixStr, C());
matrixStr.AppendLiteral(", ");
AppendFloat(matrixStr, D());
matrixStr.AppendLiteral(", ");
AppendFloat(matrixStr, E());
matrixStr.AppendLiteral(", ");
AppendFloat(matrixStr, F());
if (!AppendDouble(A()) || !AppendDouble(B()) || !AppendDouble(C()) ||
!AppendDouble(D()) || !AppendDouble(E()) || !AppendDouble(F(), true)) {
return;
}
matrixStr.AppendLiteral(")");
}

Expand Down
2 changes: 1 addition & 1 deletion dom/base/DOMMatrix.h
Expand Up @@ -203,7 +203,7 @@ class DOMMatrixReadOnly : public nsWrapperCache {
ErrorResult& aRv) const;
void ToFloat64Array(JSContext* aCx, JS::MutableHandle<JSObject*> aResult,
ErrorResult& aRv) const;
void Stringify(nsAString& aResult);
void Stringify(nsAString& aResult, ErrorResult& aRv);

bool WriteStructuredClone(JSContext* aCx,
JSStructuredCloneWriter* aWriter) const;
Expand Down
2 changes: 1 addition & 1 deletion dom/webidl/DOMMatrix.webidl
Expand Up @@ -83,7 +83,7 @@ interface DOMMatrixReadOnly {
DOMPoint transformPoint(optional DOMPointInit point = {});
[Throws] Float32Array toFloat32Array();
[Throws] Float64Array toFloat64Array();
[Exposed=Window] stringifier;
[Exposed=Window, Throws] stringifier;
[Default] object toJSON();
};

Expand Down
199 changes: 0 additions & 199 deletions testing/web-platform/meta/css/geometry/DOMMatrix-stringifier.html.ini

This file was deleted.

3 changes: 0 additions & 3 deletions testing/web-platform/meta/css/geometry/spec-examples.html.ini

This file was deleted.

0 comments on commit 190682d

Please sign in to comment.