Bug 1506324 - Add JS::DefaultGlobalClassOps to JSAPI. r=jorendorff
authorPhilip Chimento <philip.chimento@gmail.com>
Fri, 16 Nov 2018 15:30:28 +0000
changeset 503317 7be95c95a3a7254bcff022190ab2be27ca27ac50
parent 503316 26933ef9fa13275dd7005d1db78f5268008c25b0
child 503318 38844846c6ae6773afb8b9254bf3dbfec437d1ec
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)
reviewersjorendorff
bugs1506324
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 1506324 - Add JS::DefaultGlobalClassOps to JSAPI. r=jorendorff This also adds a js::ClassOps variant, js::DefaultGlobalClassOps which can be used in js::Class. Depends on D11569 Differential Revision: https://phabricator.services.mozilla.com/D11570
js/src/jsapi.cpp
js/src/jsapi.h
--- a/js/src/jsapi.cpp
+++ b/js/src/jsapi.cpp
@@ -1926,16 +1926,30 @@ JS_GlobalObjectTraceHook(JSTracer* trc, 
     // know the global is live.
     globalRealm->traceGlobal(trc);
 
     if (JSTraceOp trace = globalRealm->creationOptions().getTrace()) {
         trace(trc, global);
     }
 }
 
+const JSClassOps JS::DefaultGlobalClassOps = {
+    nullptr,  // addProperty
+    nullptr,  // deleteProperty
+    nullptr,  // enumerate
+    JS_NewEnumerateStandardClasses,
+    JS_ResolveStandardClass,
+    JS_MayResolveStandardClass,
+    nullptr,  // finalize
+    nullptr,  // call
+    nullptr,  // hasInstance
+    nullptr,  // construct
+    JS_GlobalObjectTraceHook
+};
+
 JS_PUBLIC_API(void)
 JS_FireOnNewGlobalObject(JSContext* cx, JS::HandleObject global)
 {
     // This hook is infallible, because we don't really want arbitrary script
     // to be able to throw errors during delicate global creation routines.
     // This infallibility will eat OOM and slow script, but if that happens
     // we'll likely run up into them again soon in a fallible context.
     cx->check(global);
--- a/js/src/jsapi.h
+++ b/js/src/jsapi.h
@@ -1830,16 +1830,31 @@ JS_NewGlobalObject(JSContext* cx, const 
  * object, from which we can be sure the compartment is relevant, and mark it.
  *
  * It is still possible to specify custom trace hooks for global object classes. They can be
  * provided via the RealmOptions passed to JS_NewGlobalObject.
  */
 extern JS_PUBLIC_API(void)
 JS_GlobalObjectTraceHook(JSTracer* trc, JSObject* global);
 
+namespace JS {
+
+/**
+ * This allows easily constructing a global object without having to deal with
+ * JSClassOps, forgetting to add JS_GlobalObjectTraceHook, or forgetting to call
+ * JS::InitRealmStandardClasses(). Example:
+ *
+ *     const JSClass globalClass = { "MyGlobal", JSCLASS_GLOBAL_FLAGS,
+ *         &JS::DefaultGlobalClassOps };
+ *     JS_NewGlobalObject(cx, &globalClass, ...);
+ */
+extern JS_PUBLIC_DATA(const JSClassOps) DefaultGlobalClassOps;
+
+} // namespace JS
+
 extern JS_PUBLIC_API(void)
 JS_FireOnNewGlobalObject(JSContext* cx, JS::HandleObject global);
 
 extern JS_PUBLIC_API(JSObject*)
 JS_NewObject(JSContext* cx, const JSClass* clasp);
 
 extern JS_PUBLIC_API(bool)
 JS_IsNative(JSObject* obj);