Bug 1505511 - Part 1: Use ClassSpec in WeakMapObject. r=evilpie
authorTooru Fujisawa <arai_a@mac.com>
Tue, 20 Nov 2018 20:21:32 +0900
changeset 503651 a5d9fa8dc136d167e53b1ae4a7b494d2e691dd60
parent 503650 c77a41fad0e6a6b22df9a077b536098adb42db86
child 503652 32915637657a5e92dbdd8abec557c1f229b70cd1
push id10290
push userffxbld-merge
push dateMon, 03 Dec 2018 16:23:23 +0000
treeherdermozilla-beta@700bed2445e6 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersevilpie
bugs1505511
milestone65.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 1505511 - Part 1: Use ClassSpec in WeakMapObject. r=evilpie
js/public/ProtoKey.h
js/src/builtin/WeakMapObject.cpp
js/src/builtin/WeakMapObject.h
--- a/js/public/ProtoKey.h
+++ b/js/public/ProtoKey.h
@@ -86,17 +86,17 @@
     REAL(Uint16Array,           InitViaClassSpec,       TYPED_ARRAY_CLASP(Uint16)) \
     REAL(Int32Array,            InitViaClassSpec,       TYPED_ARRAY_CLASP(Int32)) \
     REAL(Uint32Array,           InitViaClassSpec,       TYPED_ARRAY_CLASP(Uint32)) \
     REAL(Float32Array,          InitViaClassSpec,       TYPED_ARRAY_CLASP(Float32)) \
     REAL(Float64Array,          InitViaClassSpec,       TYPED_ARRAY_CLASP(Float64)) \
     REAL(Uint8ClampedArray,     InitViaClassSpec,       TYPED_ARRAY_CLASP(Uint8Clamped)) \
     REAL_IF_BIGINT(BigInt, InitViaClassSpec, OCLASP(BigInt)) \
     REAL(Proxy,                 InitProxyClass,         &js::ProxyClass) \
-    REAL(WeakMap,               InitWeakMapClass,       OCLASP(WeakMap)) \
+    REAL(WeakMap,               InitViaClassSpec,       OCLASP(WeakMap)) \
     REAL(Map,                   InitViaClassSpec,       OCLASP(Map)) \
     REAL(Set,                   InitViaClassSpec,       OCLASP(Set)) \
     REAL(DataView,              InitViaClassSpec,       OCLASP(DataView)) \
     REAL(Symbol,                InitSymbolClass,        OCLASP(Symbol)) \
     REAL(SharedArrayBuffer,     InitViaClassSpec,       OCLASP(SharedArrayBuffer)) \
     REAL_IF_INTL(Intl, InitIntlClass, CLASP(Intl)) \
     REAL_IF_BDATA(TypedObject, InitTypedObjectModuleObject, OCLASP(TypedObjectModule)) \
     REAL(Reflect,               InitReflect,            nullptr) \
--- a/js/src/builtin/WeakMapObject.cpp
+++ b/js/src/builtin/WeakMapObject.cpp
@@ -12,26 +12,26 @@
 #include "gc/FreeOp.h"
 #include "vm/JSContext.h"
 #include "vm/SelfHosting.h"
 
 #include "vm/Interpreter-inl.h"
 
 using namespace js;
 
-MOZ_ALWAYS_INLINE bool
-IsWeakMap(HandleValue v)
+/* static */ MOZ_ALWAYS_INLINE bool
+WeakMapObject::is(HandleValue v)
 {
     return v.isObject() && v.toObject().is<WeakMapObject>();
 }
 
-MOZ_ALWAYS_INLINE bool
-WeakMap_has_impl(JSContext* cx, const CallArgs& args)
+/* static */ MOZ_ALWAYS_INLINE bool
+WeakMapObject::has_impl(JSContext* cx, const CallArgs& args)
 {
-    MOZ_ASSERT(IsWeakMap(args.thisv()));
+    MOZ_ASSERT(is(args.thisv()));
 
     if (!args.get(0).isObject()) {
         args.rval().setBoolean(false);
         return true;
     }
 
     if (ObjectValueMap* map = args.thisv().toObject().as<WeakMapObject>().getMap()) {
         JSObject* key = &args[0].toObject();
@@ -40,27 +40,27 @@ WeakMap_has_impl(JSContext* cx, const Ca
             return true;
         }
     }
 
     args.rval().setBoolean(false);
     return true;
 }
 
-static bool
-WeakMap_has(JSContext* cx, unsigned argc, Value* vp)
+/* static */ bool
+WeakMapObject::has(JSContext* cx, unsigned argc, Value* vp)
 {
     CallArgs args = CallArgsFromVp(argc, vp);
-    return CallNonGenericMethod<IsWeakMap, WeakMap_has_impl>(cx, args);
+    return CallNonGenericMethod<WeakMapObject::is, WeakMapObject::has_impl>(cx, args);
 }
 
-MOZ_ALWAYS_INLINE bool
-WeakMap_get_impl(JSContext* cx, const CallArgs& args)
+/* static */ MOZ_ALWAYS_INLINE bool
+WeakMapObject::get_impl(JSContext* cx, const CallArgs& args)
 {
-    MOZ_ASSERT(IsWeakMap(args.thisv()));
+    MOZ_ASSERT(WeakMapObject::is(args.thisv()));
 
     if (!args.get(0).isObject()) {
         args.rval().setUndefined();
         return true;
     }
 
     if (ObjectValueMap* map = args.thisv().toObject().as<WeakMapObject>().getMap()) {
         JSObject* key = &args[0].toObject();
@@ -69,27 +69,27 @@ WeakMap_get_impl(JSContext* cx, const Ca
             return true;
         }
     }
 
     args.rval().setUndefined();
     return true;
 }
 
-static bool
-WeakMap_get(JSContext* cx, unsigned argc, Value* vp)
+/* static */ bool
+WeakMapObject::get(JSContext* cx, unsigned argc, Value* vp)
 {
     CallArgs args = CallArgsFromVp(argc, vp);
-    return CallNonGenericMethod<IsWeakMap, WeakMap_get_impl>(cx, args);
+    return CallNonGenericMethod<WeakMapObject::is, WeakMapObject::get_impl>(cx, args);
 }
 
-MOZ_ALWAYS_INLINE bool
-WeakMap_delete_impl(JSContext* cx, const CallArgs& args)
+/* static */ MOZ_ALWAYS_INLINE bool
+WeakMapObject::delete_impl(JSContext* cx, const CallArgs& args)
 {
-    MOZ_ASSERT(IsWeakMap(args.thisv()));
+    MOZ_ASSERT(WeakMapObject::is(args.thisv()));
 
     if (!args.get(0).isObject()) {
         args.rval().setBoolean(false);
         return true;
     }
 
     if (ObjectValueMap* map = args.thisv().toObject().as<WeakMapObject>().getMap()) {
         JSObject* key = &args[0].toObject();
@@ -99,48 +99,48 @@ WeakMap_delete_impl(JSContext* cx, const
             return true;
         }
     }
 
     args.rval().setBoolean(false);
     return true;
 }
 
-static bool
-WeakMap_delete(JSContext* cx, unsigned argc, Value* vp)
+/* static */ bool
+WeakMapObject::delete_(JSContext* cx, unsigned argc, Value* vp)
 {
     CallArgs args = CallArgsFromVp(argc, vp);
-    return CallNonGenericMethod<IsWeakMap, WeakMap_delete_impl>(cx, args);
+    return CallNonGenericMethod<WeakMapObject::is, WeakMapObject::delete_impl>(cx, args);
 }
 
-MOZ_ALWAYS_INLINE bool
-WeakMap_set_impl(JSContext* cx, const CallArgs& args)
+/* static */ MOZ_ALWAYS_INLINE bool
+WeakMapObject::set_impl(JSContext* cx, const CallArgs& args)
 {
-    MOZ_ASSERT(IsWeakMap(args.thisv()));
+    MOZ_ASSERT(WeakMapObject::is(args.thisv()));
 
     if (!args.get(0).isObject()) {
         ReportNotObjectWithName(cx, "WeakMap key", args.get(0));
         return false;
     }
 
     RootedObject key(cx, &args[0].toObject());
     Rooted<WeakMapObject*> map(cx, &args.thisv().toObject().as<WeakMapObject>());
 
     if (!WeakCollectionPutEntryInternal(cx, map, key, args.get(1))) {
         return false;
     }
     args.rval().set(args.thisv());
     return true;
 }
 
-static bool
-WeakMap_set(JSContext* cx, unsigned argc, Value* vp)
+/* static */ bool
+WeakMapObject::set(JSContext* cx, unsigned argc, Value* vp)
 {
     CallArgs args = CallArgsFromVp(argc, vp);
-    return CallNonGenericMethod<IsWeakMap, WeakMap_set_impl>(cx, args);
+    return CallNonGenericMethod<WeakMapObject::is, WeakMapObject::set_impl>(cx, args);
 }
 
 bool
 WeakCollectionObject::nondeterministicGetKeys(JSContext* cx, Handle<WeakCollectionObject*> obj,
                                               MutableHandleObject ret)
 {
     RootedObject arr(cx, NewDenseEmptyArray(cx));
     if (!arr) {
@@ -229,18 +229,18 @@ JS::SetWeakMapEntry(JSContext* cx, Handl
                     HandleValue val)
 {
     CHECK_THREAD(cx);
     cx->check(key, val);
     Handle<WeakMapObject*> rootedMap = mapObj.as<WeakMapObject>();
     return WeakCollectionPutEntryInternal(cx, rootedMap, key, val);
 }
 
-static bool
-WeakMap_construct(JSContext* cx, unsigned argc, Value* vp)
+/* static */ bool
+WeakMapObject::construct(JSContext* cx, unsigned argc, Value* vp)
 {
     CallArgs args = CallArgsFromVp(argc, vp);
 
     // ES6 draft rev 31 (15 Jan 2015) 23.3.1.1 step 1.
     if (!ThrowIfNotConstructing(cx, args, "WeakMap")) {
         return false;
     }
 
@@ -278,54 +278,47 @@ const ClassOps WeakCollectionObject::cla
     nullptr, /* mayResolve */
     WeakCollection_finalize,
     nullptr, /* call */
     nullptr, /* hasInstance */
     nullptr, /* construct */
     WeakCollection_trace
 };
 
+const ClassSpec WeakMapObject::classSpec_ = {
+    GenericCreateConstructor<WeakMapObject::construct, 0, gc::AllocKind::FUNCTION>,
+    GenericCreatePrototype<WeakMapObject>,
+    nullptr,
+    nullptr,
+    WeakMapObject::methods,
+    WeakMapObject::properties,
+};
+
 const Class WeakMapObject::class_ = {
     "WeakMap",
     JSCLASS_HAS_PRIVATE |
     JSCLASS_HAS_CACHED_PROTO(JSProto_WeakMap) |
     JSCLASS_BACKGROUND_FINALIZE,
-    &WeakCollectionObject::classOps_
+    &WeakCollectionObject::classOps_,
+    &WeakMapObject::classSpec_
 };
 
-static const JSFunctionSpec weak_map_methods[] = {
-    JS_FN("has",    WeakMap_has, 1, 0),
-    JS_FN("get",    WeakMap_get, 1, 0),
-    JS_FN("delete", WeakMap_delete, 1, 0),
-    JS_FN("set",    WeakMap_set, 2, 0),
-    JS_FS_END
+const Class WeakMapObject::protoClass_ = {
+    js_Object_str,
+    JSCLASS_HAS_CACHED_PROTO(JSProto_WeakMap),
+    JS_NULL_CLASS_OPS,
+    &WeakMapObject::classSpec_
 };
 
-JSObject*
-js::InitWeakMapClass(JSContext* cx, Handle<GlobalObject*> global)
-{
-    RootedPlainObject proto(cx, NewBuiltinClassInstance<PlainObject>(cx));
-    if (!proto) {
-        return nullptr;
-    }
-
-    RootedFunction ctor(cx, GlobalObject::createConstructor(cx, WeakMap_construct,
-                                                            cx->names().WeakMap, 0));
-    if (!ctor) {
-        return nullptr;
-    }
+const JSPropertySpec WeakMapObject::properties[] = {
+    JS_STRING_SYM_PS(toStringTag, "WeakMap", JSPROP_READONLY),
+    JS_PS_END
+};
 
-    if (!LinkConstructorAndPrototype(cx, ctor, proto)) {
-        return nullptr;
-    }
-
-    if (!DefinePropertiesAndFunctions(cx, proto, nullptr, weak_map_methods)) {
-        return nullptr;
-    }
-    if (!DefineToStringTag(cx, proto, cx->names().WeakMap)) {
-        return nullptr;
-    }
-
-    if (!GlobalObject::initBuiltinConstructor(cx, global, JSProto_WeakMap, ctor, proto)) {
-        return nullptr;
-    }
-    return proto;
-}
+const JSFunctionSpec WeakMapObject::methods[] = {
+    // clang-format off
+    JS_FN("has", has, 1, 0),
+    JS_FN("get", get, 1, 0),
+    JS_FN("delete", delete_, 1, 0),
+    JS_FN("set", set, 2, 0),
+    JS_FS_END
+    // clang-format on
+};
--- a/js/src/builtin/WeakMapObject.h
+++ b/js/src/builtin/WeakMapObject.h
@@ -27,16 +27,33 @@ class WeakCollectionObject : public Nati
   protected:
     static const ClassOps classOps_;
 };
 
 class WeakMapObject : public WeakCollectionObject
 {
   public:
     static const Class class_;
-};
+    static const Class protoClass_;
+
+  private:
+    static const ClassSpec classSpec_;
+
+    static const JSPropertySpec properties[];
+    static const JSFunctionSpec methods[];
+
+    static MOZ_MUST_USE bool construct(JSContext* cx, unsigned argc, Value* vp);
 
-extern JSObject*
-InitWeakMapClass(JSContext* cx, Handle<GlobalObject*> global);
+    static MOZ_MUST_USE MOZ_ALWAYS_INLINE bool is(HandleValue v);
+
+    static MOZ_MUST_USE MOZ_ALWAYS_INLINE bool has_impl(JSContext* cx, const CallArgs& args);
+    static MOZ_MUST_USE bool has(JSContext* cx, unsigned argc, Value* vp);
+    static MOZ_MUST_USE MOZ_ALWAYS_INLINE bool get_impl(JSContext* cx, const CallArgs& args);
+    static MOZ_MUST_USE bool get(JSContext* cx, unsigned argc, Value* vp);
+    static MOZ_MUST_USE MOZ_ALWAYS_INLINE bool delete_impl(JSContext* cx, const CallArgs& args);
+    static MOZ_MUST_USE bool delete_(JSContext* cx, unsigned argc, Value* vp);
+    static MOZ_MUST_USE MOZ_ALWAYS_INLINE bool set_impl(JSContext* cx, const CallArgs& args);
+    static MOZ_MUST_USE bool set(JSContext* cx, unsigned argc, Value* vp);
+};
 
 } // namespace js
 
 #endif /* builtin_WeakMapObject_h */