Bug 1599416 - Part 12: Fold AttachTypedObject into NewOpaqueTypedObject. r=mgaudet
authorAndré Bargull <andre.bargull@gmail.com>
Wed, 27 Nov 2019 13:58:16 +0000
changeset 504048 12b167e83eddd8a1542ccbca9c8b404e37dbc847
parent 504047 27b601310b36bd37243a4b0e8600533d1f052e68
child 504049 8fee074100ca84c0a883b8f404c2d7628a09a584
push id36855
push useraiakab@mozilla.com
push dateWed, 27 Nov 2019 21:56:55 +0000
treeherdermozilla-central@62ae0fda7cb6 [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 12: Fold AttachTypedObject into NewOpaqueTypedObject. r=mgaudet `AttacheTypedObject` is only ever called directly after `NewOpaqueTypedObject`, so we can merge both functions similar to the existing `NewDerivedTypedObject` function. This is in preparation of the next two patches. Differential Revision: https://phabricator.services.mozilla.com/D54716
js/src/builtin/TypedObject.cpp
js/src/builtin/TypedObject.h
js/src/builtin/TypedObject.js
js/src/vm/SelfHosting.cpp
--- a/js/src/builtin/TypedObject.cpp
+++ b/js/src/builtin/TypedObject.cpp
@@ -1640,16 +1640,31 @@ OutlineTypedObject* OutlineTypedObject::
     return nullptr;
   }
 
   obj->attach(cx, *typedObj, offset);
   return obj;
 }
 
 /*static*/
+OutlineTypedObject* OutlineTypedObject::createOpaque(JSContext* cx,
+                                                     HandleTypeDescr descr,
+                                                     HandleTypedObject target,
+                                                     uint32_t offset) {
+  Rooted<OutlineTypedObject*> obj(cx);
+  obj = createUnattachedWithClass(cx, &OutlineOpaqueTypedObject::class_, descr);
+  if (!obj) {
+    return nullptr;
+  }
+
+  obj->attach(cx, *target, offset);
+  return obj;
+}
+
+/*static*/
 TypedObject* TypedObject::createZeroed(JSContext* cx, HandleTypeDescr descr,
                                        gc::InitialHeap heap) {
   // If possible, create an object with inline data.
   if (InlineTypedObject::canAccommodateType(descr)) {
     AutoSetNewObjectMetadata metadata(cx);
 
     InlineTypedObject* obj = InlineTypedObject::create(cx, descr, heap);
     if (!obj) {
@@ -2343,26 +2358,29 @@ bool TypedObject::construct(JSContext* c
 }
 
 /******************************************************************************
  * Intrinsics
  */
 
 bool js::NewOpaqueTypedObject(JSContext* cx, unsigned argc, Value* vp) {
   CallArgs args = CallArgsFromVp(argc, vp);
-  MOZ_ASSERT(args.length() == 1);
+  MOZ_ASSERT(args.length() == 3);
   MOZ_ASSERT(args[0].isObject() && args[0].toObject().is<TypeDescr>());
+  MOZ_RELEASE_ASSERT(args[2].isInt32());
 
   Rooted<TypeDescr*> descr(cx, &args[0].toObject().as<TypeDescr>());
-  Rooted<OutlineTypedObject*> obj(cx);
-  obj = OutlineTypedObject::createUnattachedWithClass(
-      cx, &OutlineOpaqueTypedObject::class_, descr);
+  Rooted<TypedObject*> target(cx, &args[1].toObject().as<TypedObject>());
+  uint32_t offset = AssertedCast<uint32_t>(args[2].toInt32());
+
+  auto* obj = OutlineTypedObject::createOpaque(cx, descr, target, offset);
   if (!obj) {
     return false;
   }
+
   args.rval().setObject(*obj);
   return true;
 }
 
 bool js::NewDerivedTypedObject(JSContext* cx, unsigned argc, Value* vp) {
   CallArgs args = CallArgsFromVp(argc, vp);
   MOZ_ASSERT(args.length() == 3);
   MOZ_ASSERT(args[0].isObject() && args[0].toObject().is<TypeDescr>());
@@ -2378,31 +2396,16 @@ bool js::NewDerivedTypedObject(JSContext
   if (!obj) {
     return false;
   }
 
   args.rval().setObject(*obj);
   return true;
 }
 
-bool js::AttachTypedObject(JSContext* cx, unsigned argc, Value* vp) {
-  CallArgs args = CallArgsFromVp(argc, vp);
-  MOZ_ASSERT(args.length() == 3);
-  MOZ_RELEASE_ASSERT(args[2].isInt32());
-
-  OutlineTypedObject& handle = args[0].toObject().as<OutlineTypedObject>();
-  TypedObject& target = args[1].toObject().as<TypedObject>();
-  MOZ_ASSERT(!handle.isAttached());
-  uint32_t offset = AssertedCast<uint32_t>(args[2].toInt32());
-
-  handle.attach(cx, target, offset);
-
-  return true;
-}
-
 bool js::ObjectIsTypeDescr(JSContext*, unsigned argc, Value* vp) {
   CallArgs args = CallArgsFromVp(argc, vp);
   MOZ_ASSERT(args.length() == 1);
   MOZ_ASSERT(args[0].isObject());
   args.rval().setBoolean(args[0].toObject().is<TypeDescr>());
   return true;
 }
 
--- a/js/src/builtin/TypedObject.h
+++ b/js/src/builtin/TypedObject.h
@@ -683,16 +683,20 @@ class OutlineTypedObject : public TypedO
 
   // 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);
+
   // Use this method when `buffer` is the owner of the memory.
   void attach(JSContext* cx, 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);
 
   static void obj_trace(JSTracer* trace, JSObject* object);
 };
@@ -765,39 +769,33 @@ class InlineTransparentTypedObject : pub
 
 // Class for an opaque typed object with inline data and no array buffer.
 class InlineOpaqueTypedObject : public InlineTypedObject {
  public:
   static const JSClass class_;
 };
 
 /*
- * Usage: NewOpaqueTypedObject(typeObj)
+ * Usage: NewOpaqueTypedObject(typeObj, newDatum, newOffset)
  *
- * Constructs a new, unattached instance of `Handle`.
+ * Constructs a new, unattached instance of `Handle`, and then moves the new
+ * instance to point at the memory referenced by `newDatum` with the offset
+ * `newOffset`.
  */
 MOZ_MUST_USE bool NewOpaqueTypedObject(JSContext* cx, unsigned argc, Value* vp);
 
 /*
  * Usage: NewDerivedTypedObject(typeObj, owner, offset)
  *
  * Constructs a new, unattached instance of `Handle`.
  */
 MOZ_MUST_USE bool NewDerivedTypedObject(JSContext* cx, unsigned argc,
                                         Value* vp);
 
 /*
- * Usage: AttachTypedObject(typedObj, newDatum, newOffset)
- *
- * Moves `typedObj` to point at the memory referenced by `newDatum` with
- * the offset `newOffset`.
- */
-MOZ_MUST_USE bool AttachTypedObject(JSContext* cx, unsigned argc, Value* vp);
-
-/*
  * Usage: ObjectIsTypeDescr(obj)
  *
  * True if `obj` is a type object.
  */
 MOZ_MUST_USE bool ObjectIsTypeDescr(JSContext* cx, unsigned argc, Value* vp);
 
 /*
  * Usage: ObjectIsTypedObject(obj)
--- a/js/src/builtin/TypedObject.js
+++ b/js/src/builtin/TypedObject.js
@@ -74,19 +74,17 @@ function TypedObjectGetDerived(descr, ty
 
 function TypedObjectGetDerivedIf(descr, typedObj, offset, cond) {
   return (cond ? TypedObjectGetDerived(descr, typedObj, offset) : undefined);
 }
 
 function TypedObjectGetOpaque(descr, typedObj, offset) {
   assert(!TypeDescrIsSimpleType(descr),
          "getDerived() used with simple type");
-  var opaqueTypedObj = NewOpaqueTypedObject(descr);
-  AttachTypedObject(opaqueTypedObj, typedObj, offset | 0);
-  return opaqueTypedObj;
+  return NewOpaqueTypedObject(descr, typedObj, offset | 0);
 }
 
 function TypedObjectGetOpaqueIf(descr, typedObj, offset, cond) {
   return (cond ? TypedObjectGetOpaque(descr, typedObj, offset) : undefined);
 }
 
 function TypedObjectGetScalar(descr, typedObj, offset) {
   var type = DESCR_TYPE(descr);
--- a/js/src/vm/SelfHosting.cpp
+++ b/js/src/vm/SelfHosting.cpp
@@ -2369,19 +2369,18 @@ static const JSFunctionSpec intrinsic_fu
           CallNonGenericSelfhostedMethod<Is<MapObject>>, 2, 0),
 
     JS_INLINABLE_FN("GuardToSetObject", intrinsic_GuardToBuiltin<SetObject>, 1,
                     0, IntrinsicGuardToSetObject),
     JS_FN("CallSetMethodIfWrapped",
           CallNonGenericSelfhostedMethod<Is<SetObject>>, 2, 0),
 
     // See builtin/TypedObject.h for descriptors of the typedobj functions.
-    JS_FN("NewOpaqueTypedObject", js::NewOpaqueTypedObject, 1, 0),
+    JS_FN("NewOpaqueTypedObject", js::NewOpaqueTypedObject, 3, 0),
     JS_FN("NewDerivedTypedObject", js::NewDerivedTypedObject, 3, 0),
-    JS_FN("AttachTypedObject", js::AttachTypedObject, 3, 0),
     JS_FN("TypedObjectIsAttached", js::TypedObjectIsAttached, 1, 0),
     JS_FN("TypedObjectTypeDescr", js::TypedObjectTypeDescr, 1, 0),
     JS_FN("ClampToUint8", js::ClampToUint8, 1, 0),
     JS_FN("GetTypedObjectModule", js::GetTypedObjectModule, 0, 0),
 
     JS_INLINABLE_FN("ObjectIsTypeDescr", js::ObjectIsTypeDescr, 1, 0,
                     IntrinsicObjectIsTypeDescr),
     JS_INLINABLE_FN("ObjectIsTypedObject", js::ObjectIsTypedObject, 1, 0,