Bug 1595745 - Part 6: Change Math to use ClassSpec. r=mgaudet
☠☠ backed out by ec8cad689121 ☠ ☠
authorAndré Bargull <andre.bargull@gmail.com>
Fri, 15 Nov 2019 15:03:20 +0000
changeset 502200 68c5cdced300d92c3102c2fdddab32459e14e95d
parent 502199 d5f5e9091fb049e19001b408e9c784178348fd5b
child 502201 fef5c8d5cb909dd147da181f45851603cddeb045
push id114172
push userdluca@mozilla.com
push dateTue, 19 Nov 2019 11:31:10 +0000
treeherdermozilla-inbound@b5c5ba07d3db [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmgaudet
bugs1595745
milestone72.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1595745 - Part 6: Change Math to use ClassSpec. r=mgaudet Similar to the JSON changes in part 5. Additionally 'FinishClassInitOp' is needed to initialise the double constant properties. Differential Revision: https://phabricator.services.mozilla.com/D52662
js/public/ProtoKey.h
js/src/jsmath.cpp
js/src/jsmath.h
--- a/js/public/ProtoKey.h
+++ b/js/public/ProtoKey.h
@@ -51,17 +51,17 @@
 #define JS_FOR_PROTOTYPES_(REAL, IMAGINARY, REAL_IF_INTL, REAL_IF_BDATA)     \
   IMAGINARY(Null, InitNullClass, dummy)                                      \
   REAL(Object, InitViaClassSpec, OCLASP(Plain))                              \
   REAL(Function, InitViaClassSpec, &JSFunction::class_)                      \
   REAL(Array, InitViaClassSpec, OCLASP(Array))                               \
   REAL(Boolean, InitViaClassSpec, OCLASP(Boolean))                           \
   REAL(JSON, InitViaClassSpec, CLASP(JSON))                                  \
   REAL(Date, InitViaClassSpec, OCLASP(Date))                                 \
-  REAL(Math, InitMathClass, CLASP(Math))                                     \
+  REAL(Math, InitViaClassSpec, CLASP(Math))                                  \
   REAL(Number, InitViaClassSpec, OCLASP(Number))                             \
   REAL(String, InitViaClassSpec, OCLASP(String))                             \
   REAL(RegExp, InitViaClassSpec, OCLASP(RegExp))                             \
   REAL(Error, InitViaClassSpec, ERROR_CLASP(JSEXN_ERR))                      \
   REAL(InternalError, InitViaClassSpec, ERROR_CLASP(JSEXN_INTERNALERR))      \
   REAL(EvalError, InitViaClassSpec, ERROR_CLASP(JSEXN_EVALERR))              \
   REAL(RangeError, InitViaClassSpec, ERROR_CLASP(JSEXN_RANGEERR))            \
   REAL(ReferenceError, InitViaClassSpec, ERROR_CLASP(JSEXN_REFERENCEERR))    \
--- a/js/src/jsmath.cpp
+++ b/js/src/jsmath.cpp
@@ -90,19 +90,16 @@ static bool math_function(JSContext* cx,
   if (args.length() == 0) {
     args.rval().setNaN();
     return true;
   }
 
   return math_function<F>(cx, args[0], args.rval());
 }
 
-const JSClass js::MathClass = {js_Math_str,
-                               JSCLASS_HAS_CACHED_PROTO(JSProto_Math)};
-
 bool js::math_abs_handle(JSContext* cx, js::HandleValue v,
                          js::MutableHandleValue r) {
   double x;
   if (!ToNumber(cx, v, &x)) {
     return false;
   }
 
   double z = Abs(x);
@@ -932,36 +929,32 @@ static const JSFunctionSpec math_static_
     JS_INLINABLE_FN("asinh", math_asinh, 1, 0, MathASinH),
     JS_INLINABLE_FN("atanh", math_atanh, 1, 0, MathATanH),
     JS_INLINABLE_FN("hypot", math_hypot, 2, 0, MathHypot),
     JS_INLINABLE_FN("trunc", math_trunc, 1, 0, MathTrunc),
     JS_INLINABLE_FN("sign", math_sign, 1, 0, MathSign),
     JS_INLINABLE_FN("cbrt", math_cbrt, 1, 0, MathCbrt),
     JS_FS_END};
 
-JSObject* js::InitMathClass(JSContext* cx, Handle<GlobalObject*> global) {
+static const JSPropertySpec math_static_properties[] = {
+    JS_STRING_SYM_PS(toStringTag, "Math", JSPROP_READONLY), JS_PS_END};
+
+static JSObject* CreateMathObject(JSContext* cx, JSProtoKey key) {
+  Handle<GlobalObject*> global = cx->global();
   RootedObject proto(cx, GlobalObject::getOrCreateObjectPrototype(cx, global));
   if (!proto) {
     return nullptr;
   }
-  RootedObject Math(
-      cx, NewObjectWithGivenProto(cx, &MathClass, proto, SingletonObject));
-  if (!Math) {
-    return nullptr;
-  }
+  return NewObjectWithGivenProto(cx, &MathClass, proto, SingletonObject);
+}
+
+static bool MathClassFinish(JSContext* cx, HandleObject ctor,
+                            HandleObject proto) {
+  return JS_DefineConstDoubles(cx, ctor, math_constants);
+}
 
-  if (!JS_DefineProperty(cx, global, js_Math_str, Math, JSPROP_RESOLVING)) {
-    return nullptr;
-  }
-  if (!JS_DefineFunctions(cx, Math, math_static_methods)) {
-    return nullptr;
-  }
-  if (!JS_DefineConstDoubles(cx, Math, math_constants)) {
-    return nullptr;
-  }
-  if (!DefineToStringTag(cx, Math, cx->names().Math)) {
-    return nullptr;
-  }
+static const ClassSpec MathClassSpec = {
+    CreateMathObject, nullptr, math_static_methods, math_static_properties,
+    nullptr,          nullptr, MathClassFinish};
 
-  global->setConstructor(JSProto_Math, ObjectValue(*Math));
-
-  return Math;
-}
+const JSClass js::MathClass = {js_Math_str,
+                               JSCLASS_HAS_CACHED_PROTO(JSProto_Math),
+                               JS_NULL_CLASS_OPS, &MathClassSpec};
--- a/js/src/jsmath.h
+++ b/js/src/jsmath.h
@@ -10,28 +10,24 @@
 #include "mozilla/MemoryReporting.h"
 
 #include <stdint.h>
 
 #include "NamespaceImports.h"
 
 namespace js {
 
-class GlobalObject;
-
 typedef double (*UnaryFunType)(double);
 
 /*
  * JS math functions.
  */
 
 extern const JSClass MathClass;
 
-extern JSObject* InitMathClass(JSContext* cx, Handle<GlobalObject*> global);
-
 extern uint64_t GenerateRandomSeed();
 
 // Fill |seed[0]| and |seed[1]| with random bits, suitable for
 // seeding a XorShift128+ random number generator.
 extern void GenerateXorShift128PlusSeed(mozilla::Array<uint64_t, 2>& seed);
 
 extern double math_random_impl(JSContext* cx);