Reviewers: rossberg, rafaelw,

Message:
rossberg@: PTAL.
rafaelw@: FYI.

Description:
Introduce JSFunction::EnsureHasInitialMap method.

This change enforces explicit allocation of the initial map for each
JSFunction to introduce a proper layering between the JSFunction class
and the Heap class. A follow-up change will then handlify the two
functions AllocateInitialMap and AllocateFunctionPrototype.

[email protected]

Please review this at https://codereview.chromium.org/32323013/

SVN Base: https://v8.googlecode.com/svn/branches/bleeding_edge

Affected files (+43, -47 lines):
  M src/factory.h
  M src/factory.cc
  M src/heap.h
  M src/heap.cc
  M src/objects.h
  M src/objects.cc
  M src/runtime.cc


Index: src/factory.cc
diff --git a/src/factory.cc b/src/factory.cc
index 4927cac376176dbea65ca4ed45c2d893ee0b8d02..fb14663341c604a7ba546a4cd5a3b91904546386 100644
--- a/src/factory.cc
+++ b/src/factory.cc
@@ -580,6 +580,12 @@ Handle<JSObject> Factory::NewFunctionPrototype(Handle<JSFunction> function) {
 }


+Handle<Map> Factory::NewInitialMap(Handle<JSFunction> function) {
+  CALL_HEAP_FUNCTION(
+      isolate(), isolate()->heap()->AllocateInitialMap(*function), Map);
+}
+
+
Handle<Map> Factory::CopyWithPreallocatedFieldDescriptors(Handle<Map> src) {
   CALL_HEAP_FUNCTION(
       isolate(), src->CopyWithPreallocatedFieldDescriptors(), Map);
@@ -1045,6 +1051,7 @@ Handle<String> Factory::InternalizedStringFromString(Handle<String> value) {

 Handle<JSObject> Factory::NewJSObject(Handle<JSFunction> constructor,
                                       PretenureFlag pretenure) {
+  JSFunction::EnsureHasInitialMap(constructor);
   CALL_HEAP_FUNCTION(
       isolate(),
isolate()->heap()->AllocateJSObject(*constructor, pretenure), JSObject);
@@ -1191,6 +1198,19 @@ void Factory::SetContent(Handle<JSArray> array,
 }


+Handle<JSGeneratorObject> Factory::NewJSGeneratorObject(
+    Handle<JSFunction> function) {
+  ASSERT(function->shared()->is_generator());
+  JSFunction::EnsureHasInitialMap(function);
+  Handle<Map> map(function->initial_map());
+  ASSERT(map->instance_type() == JS_GENERATOR_OBJECT_TYPE);
+  CALL_HEAP_FUNCTION(
+      isolate(),
+      isolate()->heap()->AllocateJSObjectFromMap(*map),
+      JSGeneratorObject);
+}
+
+
 Handle<JSArrayBuffer> Factory::NewJSArrayBuffer() {
   Handle<JSFunction> array_buffer_fun(
       isolate()->context()->native_context()->array_buffer_fun());
Index: src/factory.h
diff --git a/src/factory.h b/src/factory.h
index 68980c521f384ed726f63d373223bc1d2fc606e8..e5b8ce6de4276d474a511f33351c3687f4619103 100644
--- a/src/factory.h
+++ b/src/factory.h
@@ -261,6 +261,8 @@ class Factory {

   Handle<JSObject> NewFunctionPrototype(Handle<JSFunction> function);

+  Handle<Map> NewInitialMap(Handle<JSFunction> function);
+
   Handle<Map> CopyWithPreallocatedFieldDescriptors(Handle<Map> map);

   // Copy the map adding more inobject properties if possible without
@@ -341,6 +343,8 @@ class Factory {

   void SetContent(Handle<JSArray> array, Handle<FixedArrayBase> elements);

+ Handle<JSGeneratorObject> NewJSGeneratorObject(Handle<JSFunction> function);
+
   Handle<JSArrayBuffer> NewJSArrayBuffer();

   Handle<JSTypedArray> NewJSTypedArray(ExternalArrayType type);
Index: src/heap.cc
diff --git a/src/heap.cc b/src/heap.cc
index 2e460487c75468ba5f629a3f8d39ee0684bdcf9e..8737c574b2d1d83e13722bc3d1b614c2f04c88aa 100644
--- a/src/heap.cc
+++ b/src/heap.cc
@@ -4606,15 +4606,7 @@ MaybeObject* Heap::AllocateJSObjectFromMapWithAllocationSite(

 MaybeObject* Heap::AllocateJSObject(JSFunction* constructor,
                                     PretenureFlag pretenure) {
-  // Allocate the initial map if absent.
-  if (!constructor->has_initial_map()) {
-    Object* initial_map;
-    { MaybeObject* maybe_initial_map = AllocateInitialMap(constructor);
- if (!maybe_initial_map->ToObject(&initial_map)) return maybe_initial_map;
-    }
-    constructor->set_initial_map(Map::cast(initial_map));
-    Map::cast(initial_map)->set_constructor(constructor);
-  }
+  ASSERT(constructor->has_initial_map());
   // Allocate the object based on the constructors initial map.
   MaybeObject* result = AllocateJSObjectFromMap(
       constructor->initial_map(), pretenure);
@@ -4629,15 +4621,7 @@ MaybeObject* Heap::AllocateJSObject(JSFunction* constructor,

MaybeObject* Heap::AllocateJSObjectWithAllocationSite(JSFunction* constructor,
     Handle<AllocationSite> allocation_site) {
-  // Allocate the initial map if absent.
-  if (!constructor->has_initial_map()) {
-    Object* initial_map;
-    { MaybeObject* maybe_initial_map = AllocateInitialMap(constructor);
- if (!maybe_initial_map->ToObject(&initial_map)) return maybe_initial_map;
-    }
-    constructor->set_initial_map(Map::cast(initial_map));
-    Map::cast(initial_map)->set_constructor(constructor);
-  }
+  ASSERT(constructor->has_initial_map());
// Allocate the object based on the constructors initial map, or the payload
   // advice
   Map* initial_map = constructor->initial_map();
@@ -4669,23 +4653,6 @@ MaybeObject* Heap::AllocateJSObjectWithAllocationSite(JSFunction* constructor,
 }


-MaybeObject* Heap::AllocateJSGeneratorObject(JSFunction *function) {
-  ASSERT(function->shared()->is_generator());
-  Map *map;
-  if (function->has_initial_map()) {
-    map = function->initial_map();
-  } else {
-    // Allocate the initial map if absent.
-    MaybeObject* maybe_map = AllocateInitialMap(function);
-    if (!maybe_map->To(&map)) return maybe_map;
-    function->set_initial_map(map);
-    map->set_constructor(function);
-  }
-  ASSERT(map->instance_type() == JS_GENERATOR_OBJECT_TYPE);
-  return AllocateJSObjectFromMap(map);
-}
-
-
MaybeObject* Heap::AllocateJSModule(Context* context, ScopeInfo* scope_info) {
   // Allocate a fresh map. Modules do not have a prototype.
   Map* map;
Index: src/heap.h
diff --git a/src/heap.h b/src/heap.h
index 0bd2c405f0ee09a78e6a3fea5d67317b6662edcb..ca9c1e6a03b132ef079c302b431de9e9936cfc8c 100644
--- a/src/heap.h
+++ b/src/heap.h
@@ -624,9 +624,6 @@ class Heap {
       JSFunction* constructor,
       Handle<AllocationSite> allocation_site);

-  MUST_USE_RESULT MaybeObject* AllocateJSGeneratorObject(
-      JSFunction* function);
-
   MUST_USE_RESULT MaybeObject* AllocateJSModule(Context* context,
                                                 ScopeInfo* scope_info);

Index: src/objects.cc
diff --git a/src/objects.cc b/src/objects.cc
index e0f160ba2b312981affdd1a3d220baf3ae3e4069..527a6ebc9569f579188bc93091c51d60d7ebd04f 100644
--- a/src/objects.cc
+++ b/src/objects.cc
@@ -9892,6 +9892,15 @@ void JSFunction::RemovePrototype() {
 }


+void JSFunction::EnsureHasInitialMap(Handle<JSFunction> function) {
+  if (function->has_initial_map()) return;
+  Isolate* isolate = function->GetIsolate();
+  Handle<Map> initial_map = isolate->factory()->NewInitialMap(function);
+  function->set_initial_map(*initial_map);
+  initial_map->set_constructor(*function);
+}
+
+
 void JSFunction::SetInstanceClassName(String* name) {
   shared()->set_instance_class_name(name);
 }
Index: src/objects.h
diff --git a/src/objects.h b/src/objects.h
index ec78d27e8ab50c0df2b00abfe74d89b5325e88d0..6782fd3c364340ee00a721d66ae5a05898bde5ac 100644
--- a/src/objects.h
+++ b/src/objects.h
@@ -7220,6 +7220,7 @@ class JSFunction: public JSObject {
   inline Map* initial_map();
   inline void set_initial_map(Map* value);
   inline bool has_initial_map();
+  static void EnsureHasInitialMap(Handle<JSFunction> function);

   // Get and set the prototype property on a JSFunction. If the
   // function has an initial map the prototype is set on the initial
Index: src/runtime.cc
diff --git a/src/runtime.cc b/src/runtime.cc
index feb78c03ec8155437f14aa60b5724092db486b4a..16e311c6e8f5f8edccfb0bb7ec3652d54d83e6d9 100644
--- a/src/runtime.cc
+++ b/src/runtime.cc
@@ -3000,30 +3000,28 @@ RUNTIME_FUNCTION(MaybeObject*, Runtime_SetExpectedNumberOfProperties) {


 RUNTIME_FUNCTION(MaybeObject*, Runtime_CreateJSGeneratorObject) {
-  SealHandleScope shs(isolate);
+  HandleScope scope(isolate);
   ASSERT(args.length() == 0);

   JavaScriptFrameIterator it(isolate);
   JavaScriptFrame* frame = it.frame();
-  JSFunction* function = frame->function();
+  Handle<JSFunction> function(frame->function());
   RUNTIME_ASSERT(function->shared()->is_generator());

-  JSGeneratorObject* generator;
+  Handle<JSGeneratorObject> generator;
   if (frame->IsConstructor()) {
-    generator = JSGeneratorObject::cast(frame->receiver());
+    generator = handle(JSGeneratorObject::cast(frame->receiver()));
   } else {
-    MaybeObject* maybe_generator =
-        isolate->heap()->AllocateJSGeneratorObject(function);
-    if (!maybe_generator->To(&generator)) return maybe_generator;
+    generator = isolate->factory()->NewJSGeneratorObject(function);
   }
-  generator->set_function(function);
+  generator->set_function(*function);
   generator->set_context(Context::cast(frame->context()));
   generator->set_receiver(frame->receiver());
   generator->set_continuation(0);
   generator->set_operand_stack(isolate->heap()->empty_fixed_array());
   generator->set_stack_handler_index(-1);

-  return generator;
+  return *generator;
 }




--
--
v8-dev mailing list
[email protected]
http://groups.google.com/group/v8-dev
--- You received this message because you are subscribed to the Google Groups "v8-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to