Bug 1599416 - Part 13: Make OutlineTypedObject::attach and createUnattached private methods. r=mgaudet
authorAndré Bargull <andre.bargull@gmail.com>
Wed, 27 Nov 2019 13:58:26 +0000
changeset 504063 8fee074100ca84c0a883b8f404c2d7628a09a584
parent 504062 12b167e83eddd8a1542ccbca9c8b404e37dbc847
child 504064 7a5a3cbc9b5be32af3b7666930e87aa33bc63981
push id101711
push useraciure@mozilla.com
push dateWed, 27 Nov 2019 14:49:56 +0000
treeherderautoland@7a5a3cbc9b5b [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmgaudet
bugs1599416
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 1599416 - Part 13: Make OutlineTypedObject::attach and createUnattached private methods. r=mgaudet `OutlineTypedObject::attach` is only called when creating a new OutlineTypedObject. By making the two `attach` functions and `createUnattached[WithClass]` private methods, we can enforce only OutlineTypedObjects with attached data are exposed to the user. This is in preparation for the next patch. Differential Revision: https://phabricator.services.mozilla.com/D54717
js/src/builtin/TypedObject.cpp
js/src/builtin/TypedObject.h
--- a/js/src/builtin/TypedObject.cpp
+++ b/js/src/builtin/TypedObject.cpp
@@ -1523,23 +1523,20 @@ bool TypedObject::isAttached() const {
 /******************************************************************************
  * Outline typed objects
  */
 
 /*static*/
 OutlineTypedObject* OutlineTypedObject::createUnattached(JSContext* cx,
                                                          HandleTypeDescr descr,
                                                          gc::InitialHeap heap) {
-  if (descr->opaque()) {
-    return createUnattachedWithClass(cx, &OutlineOpaqueTypedObject::class_,
-                                     descr, heap);
-  } else {
-    return createUnattachedWithClass(cx, &OutlineTransparentTypedObject::class_,
-                                     descr, heap);
-  }
+  const JSClass* clasp = descr->opaque()
+                             ? &OutlineOpaqueTypedObject::class_
+                             : &OutlineTransparentTypedObject::class_;
+  return createUnattachedWithClass(cx, clasp, descr, heap);
 }
 
 void OutlineTypedObject::setOwnerAndData(JSObject* owner, uint8_t* data) {
   // Typed objects cannot move from one owner to another, so don't worry
   // about pre barriers during this initialization.
   owner_ = owner;
   data_ = data;
 
@@ -1586,18 +1583,17 @@ OutlineTypedObject* OutlineTypedObject::
   if (!obj) {
     return nullptr;
   }
 
   obj->setOwnerAndData(nullptr, nullptr);
   return obj;
 }
 
-void OutlineTypedObject::attach(JSContext* cx, ArrayBufferObject& buffer,
-                                uint32_t offset) {
+void OutlineTypedObject::attach(ArrayBufferObject& buffer, uint32_t offset) {
   MOZ_ASSERT(!isAttached());
   MOZ_ASSERT(offset <= buffer.byteLength());
   MOZ_ASSERT(size() <= buffer.byteLength() - offset);
   MOZ_ASSERT(buffer.hasTypedObjectViews());
   MOZ_ASSERT(!buffer.isDetached());
 
   setOwnerAndData(&buffer, buffer.dataPointer() + offset);
 }
@@ -1610,26 +1606,49 @@ void OutlineTypedObject::attach(JSContex
   JSObject* owner = &typedObj;
   if (typedObj.is<OutlineTypedObject>()) {
     owner = &typedObj.as<OutlineTypedObject>().owner();
     MOZ_ASSERT(typedObj.offset() <= UINT32_MAX - offset);
     offset += typedObj.offset();
   }
 
   if (owner->is<ArrayBufferObject>()) {
-    attach(cx, owner->as<ArrayBufferObject>(), offset);
+    attach(owner->as<ArrayBufferObject>(), offset);
   } else {
     MOZ_ASSERT(owner->is<InlineTypedObject>());
     JS::AutoCheckCannotGC nogc(cx);
     setOwnerAndData(
         owner, owner->as<InlineTypedObject>().inlineTypedMem(nogc) + offset);
   }
 }
 
 /*static*/
+OutlineTypedObject* OutlineTypedObject::createZeroed(JSContext* cx,
+                                                     HandleTypeDescr descr,
+                                                     gc::InitialHeap heap) {
+  // Create unattached wrapper object.
+  Rooted<OutlineTypedObject*> obj(
+      cx, OutlineTypedObject::createUnattached(cx, descr, heap));
+  if (!obj) {
+    return nullptr;
+  }
+
+  // Allocate and initialize the memory for this instance.
+  size_t totalSize = descr->size();
+  Rooted<ArrayBufferObject*> buffer(cx);
+  buffer = ArrayBufferObject::createForTypedObject(cx, totalSize);
+  if (!buffer) {
+    return nullptr;
+  }
+  descr->initInstance(cx->runtime(), buffer->dataPointer());
+  obj->attach(*buffer, 0);
+  return obj;
+}
+
+/*static*/
 OutlineTypedObject* OutlineTypedObject::createDerived(
     JSContext* cx, HandleTypeDescr type, HandleTypedObject typedObj,
     uint32_t offset) {
   MOZ_ASSERT(offset <= typedObj->size());
   MOZ_ASSERT(offset + type->size() <= typedObj->size());
 
   const JSClass* clasp = typedObj->opaque()
                              ? &OutlineOpaqueTypedObject::class_
@@ -1670,33 +1689,17 @@ TypedObject* TypedObject::createZeroed(J
     if (!obj) {
       return nullptr;
     }
     JS::AutoCheckCannotGC nogc(cx);
     descr->initInstance(cx->runtime(), obj->inlineTypedMem(nogc));
     return obj;
   }
 
-  // Create unattached wrapper object.
-  Rooted<OutlineTypedObject*> obj(
-      cx, OutlineTypedObject::createUnattached(cx, descr, heap));
-  if (!obj) {
-    return nullptr;
-  }
-
-  // Allocate and initialize the memory for this instance.
-  size_t totalSize = descr->size();
-  Rooted<ArrayBufferObject*> buffer(cx);
-  buffer = ArrayBufferObject::createForTypedObject(cx, totalSize);
-  if (!buffer) {
-    return nullptr;
-  }
-  descr->initInstance(cx->runtime(), buffer->dataPointer());
-  obj->attach(cx, *buffer, 0);
-  return obj;
+  return OutlineTypedObject::createZeroed(cx, descr, heap);
 }
 
 /* static */
 void OutlineTypedObject::obj_trace(JSTracer* trc, JSObject* object) {
   OutlineTypedObject& typedObj = object->as<OutlineTypedObject>();
 
   TraceEdge(trc, typedObj.shapePtr(), "OutlineTypedObject_shape");
 
--- a/js/src/builtin/TypedObject.h
+++ b/js/src/builtin/TypedObject.h
@@ -660,49 +660,56 @@ class OutlineTypedObject : public TypedO
 
   JSObject& owner() const {
     MOZ_ASSERT(owner_);
     return *owner_;
   }
 
   uint8_t* outOfLineTypedMem() const { return data_; }
 
+ private:
   // Helper for createUnattached()
   static OutlineTypedObject* createUnattachedWithClass(
       JSContext* cx, const JSClass* clasp, HandleTypeDescr type,
       gc::InitialHeap heap = gc::DefaultHeap);
 
   // Creates an unattached typed object or handle (depending on the
   // type parameter T). Note that it is only legal for unattached
   // handles to escape to the end user; for non-handles, the caller
   // should always invoke one of the `attach()` methods below.
   //
   // Arguments:
   // - type: type object for resulting object
   static OutlineTypedObject* createUnattached(
       JSContext* cx, HandleTypeDescr type,
       gc::InitialHeap heap = gc::DefaultHeap);
 
+ public:
+  static OutlineTypedObject* createZeroed(JSContext* cx, HandleTypeDescr descr,
+                                          gc::InitialHeap heap);
+
   // Creates a typedObj that aliases the memory pointed at by `owner`
   // at the given offset. The typedObj will be a handle iff type is a
   // handle and a typed object otherwise.
   static OutlineTypedObject* createDerived(JSContext* cx, HandleTypeDescr type,
                                            Handle<TypedObject*> typedContents,
                                            uint32_t offset);
 
   static OutlineTypedObject* createOpaque(JSContext* cx, HandleTypeDescr descr,
                                           Handle<TypedObject*> target,
                                           uint32_t offset);
 
+ private:
   // Use this method when `buffer` is the owner of the memory.
-  void attach(JSContext* cx, ArrayBufferObject& buffer, uint32_t offset);
+  void attach(ArrayBufferObject& buffer, uint32_t offset);
 
   // Otherwise, use this to attach to memory referenced by another typedObj.
   void attach(JSContext* cx, TypedObject& typedObj, uint32_t offset);
 
+ public:
   static void obj_trace(JSTracer* trace, JSObject* object);
 };
 
 // Class for a transparent typed object whose owner is an array buffer.
 class OutlineTransparentTypedObject : public OutlineTypedObject {
  public:
   static const JSClass class_;
 };