blob: c48385ce11ccec616e5435ee198b8b0d55ba23a6 [file] [log] [blame]
danno@chromium.orgfa458e42012-02-01 10:48:36 +00001// Copyright 2012 the V8 project authors. All rights reserved.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28#include "v8.h"
29
30#include "accessors.h"
31#include "api.h"
32#include "bootstrapper.h"
33#include "compiler.h"
34#include "debug.h"
35#include "execution.h"
36#include "global-handles.h"
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +000037#include "isolate-inl.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000038#include "macro-assembler.h"
39#include "natives.h"
ager@chromium.orgea4f62e2010-08-16 16:28:43 +000040#include "objects-visiting.h"
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +000041#include "platform.h"
ager@chromium.orgc4c92722009-11-18 14:12:51 +000042#include "snapshot.h"
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +000043#include "extensions/externalize-string-extension.h"
44#include "extensions/gc-extension.h"
yangguo@chromium.org304cc332012-07-24 07:59:48 +000045#include "extensions/statistics-extension.h"
danno@chromium.orgca29dd82013-04-26 11:59:48 +000046#include "code-stubs.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000047
kasperl@chromium.org71affb52009-05-26 05:44:31 +000048namespace v8 {
49namespace internal {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000050
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000051
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000052NativesExternalStringResource::NativesExternalStringResource(
53 Bootstrapper* bootstrapper,
jkummerow@chromium.orge297f592011-06-08 10:05:15 +000054 const char* source,
55 size_t length)
56 : data_(source), length_(length) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000057 if (bootstrapper->delete_these_non_arrays_on_tear_down_ == NULL) {
58 bootstrapper->delete_these_non_arrays_on_tear_down_ = new List<char*>(2);
ager@chromium.orgc4c92722009-11-18 14:12:51 +000059 }
60 // The resources are small objects and we only make a fixed number of
61 // them, but let's clean them up on exit for neatness.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000062 bootstrapper->delete_these_non_arrays_on_tear_down_->
ager@chromium.orgc4c92722009-11-18 14:12:51 +000063 Add(reinterpret_cast<char*>(this));
64}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000065
66
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +000067Bootstrapper::Bootstrapper(Isolate* isolate)
68 : isolate_(isolate),
69 nesting_(0),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000070 extensions_cache_(Script::TYPE_EXTENSION),
71 delete_these_non_arrays_on_tear_down_(NULL),
72 delete_these_arrays_on_tear_down_(NULL) {
73}
74
75
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000076Handle<String> Bootstrapper::NativesSourceLookup(int index) {
77 ASSERT(0 <= index && index < Natives::GetBuiltinsCount());
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +000078 Heap* heap = isolate_->heap();
lrn@chromium.org7516f052011-03-30 08:52:27 +000079 if (heap->natives_source_cache()->get(index)->IsUndefined()) {
danno@chromium.orgfa458e42012-02-01 10:48:36 +000080 // We can use external strings for the natives.
81 Vector<const char> source = Natives::GetRawScriptSource(index);
82 NativesExternalStringResource* resource =
83 new NativesExternalStringResource(this,
84 source.start(),
85 source.length());
86 Handle<String> source_code =
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +000087 isolate_->factory()->NewExternalStringFromAscii(resource);
danno@chromium.orgfa458e42012-02-01 10:48:36 +000088 heap->natives_source_cache()->set(index, *source_code);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000089 }
ulan@chromium.org09d7ab52013-02-25 15:50:35 +000090 Handle<Object> cached_source(heap->natives_source_cache()->get(index),
91 isolate_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000092 return Handle<String>::cast(cached_source);
93}
94
95
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000096void Bootstrapper::Initialize(bool create_heap_objects) {
dslomov@chromium.orge97852d2013-09-12 09:02:59 +000097 extensions_cache_.Initialize(isolate_, create_heap_objects);
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +000098}
99
100
101void Bootstrapper::InitializeOncePerProcess() {
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +0000102 GCExtension::Register();
103 ExternalizeStringExtension::Register();
yangguo@chromium.org304cc332012-07-24 07:59:48 +0000104 StatisticsExtension::Register();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000105}
106
107
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000108char* Bootstrapper::AllocateAutoDeletedArray(int bytes) {
109 char* memory = new char[bytes];
110 if (memory != NULL) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000111 if (delete_these_arrays_on_tear_down_ == NULL) {
112 delete_these_arrays_on_tear_down_ = new List<char*>(2);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000113 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000114 delete_these_arrays_on_tear_down_->Add(memory);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000115 }
116 return memory;
117}
118
119
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000120void Bootstrapper::TearDown() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000121 if (delete_these_non_arrays_on_tear_down_ != NULL) {
122 int len = delete_these_non_arrays_on_tear_down_->length();
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000123 ASSERT(len < 20); // Don't use this mechanism for unbounded allocations.
124 for (int i = 0; i < len; i++) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000125 delete delete_these_non_arrays_on_tear_down_->at(i);
126 delete_these_non_arrays_on_tear_down_->at(i) = NULL;
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000127 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000128 delete delete_these_non_arrays_on_tear_down_;
129 delete_these_non_arrays_on_tear_down_ = NULL;
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000130 }
131
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000132 if (delete_these_arrays_on_tear_down_ != NULL) {
133 int len = delete_these_arrays_on_tear_down_->length();
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000134 ASSERT(len < 1000); // Don't use this mechanism for unbounded allocations.
135 for (int i = 0; i < len; i++) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000136 delete[] delete_these_arrays_on_tear_down_->at(i);
137 delete_these_arrays_on_tear_down_->at(i) = NULL;
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000138 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000139 delete delete_these_arrays_on_tear_down_;
140 delete_these_arrays_on_tear_down_ = NULL;
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000141 }
142
dslomov@chromium.orge97852d2013-09-12 09:02:59 +0000143 extensions_cache_.Initialize(isolate_, false); // Yes, symmetrical
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000144}
145
146
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000147class Genesis BASE_EMBEDDED {
148 public:
danno@chromium.org160a7b02011-04-18 15:51:38 +0000149 Genesis(Isolate* isolate,
150 Handle<Object> global_object,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000151 v8::Handle<v8::ObjectTemplate> global_template,
152 v8::ExtensionConfiguration* extensions);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000153 ~Genesis() { }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000154
danno@chromium.org160a7b02011-04-18 15:51:38 +0000155 Isolate* isolate() const { return isolate_; }
156 Factory* factory() const { return isolate_->factory(); }
157 Heap* heap() const { return isolate_->heap(); }
158
jkummerow@chromium.org7bd87f02013-03-20 18:06:29 +0000159 Handle<Context> result() { return result_; }
jkummerow@chromium.org8718d732013-03-19 22:53:30 +0000160
jkummerow@chromium.org7bd87f02013-03-20 18:06:29 +0000161 private:
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000162 Handle<Context> native_context() { return native_context_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000163
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000164 // Creates some basic objects. Used for creating a context from scratch.
165 void CreateRoots();
166 // Creates the empty function. Used for creating a context from scratch.
danno@chromium.org160a7b02011-04-18 15:51:38 +0000167 Handle<JSFunction> CreateEmptyFunction(Isolate* isolate);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000168 // Creates the ThrowTypeError function. ECMA 5th Ed. 13.2.3
danno@chromium.org40cb8782011-05-25 07:58:50 +0000169 Handle<JSFunction> GetThrowTypeErrorFunction();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000170
171 void CreateStrictModeFunctionMaps(Handle<JSFunction> empty);
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000172
173 // Make the "arguments" and "caller" properties throw a TypeError on access.
174 void PoisonArgumentsAndCaller(Handle<Map> map);
175
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000176 // Creates the global objects using the global and the template passed in
177 // through the API. We call this regardless of whether we are building a
178 // context from scratch or using a deserialized one from the partial snapshot
179 // but in the latter case we don't use the objects it produces directly, as
180 // we have to used the deserialized ones that are linked together with the
181 // rest of the context snapshot.
182 Handle<JSGlobalProxy> CreateNewGlobals(
183 v8::Handle<v8::ObjectTemplate> global_template,
184 Handle<Object> global_object,
185 Handle<GlobalObject>* global_proxy_out);
186 // Hooks the given global proxy into the context. If the context was created
187 // by deserialization then this will unhook the global proxy that was
188 // deserialized, leaving the GC to pick it up.
189 void HookUpGlobalProxy(Handle<GlobalObject> inner_global,
190 Handle<JSGlobalProxy> global_proxy);
191 // Similarly, we want to use the inner global that has been created by the
192 // templates passed through the API. The inner global from the snapshot is
193 // detached from the other objects in the snapshot.
194 void HookUpInnerGlobal(Handle<GlobalObject> inner_global);
195 // New context initialization. Used for creating a context from scratch.
jkummerow@chromium.orgba72ec82013-07-22 09:21:20 +0000196 void InitializeGlobal(Handle<GlobalObject> inner_global,
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000197 Handle<JSFunction> empty_function);
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +0000198 void InitializeExperimentalGlobal();
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000199 // Installs the contents of the native .js files on the global objects.
200 // Used for creating a context from scratch.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000201 void InstallNativeFunctions();
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000202 void InstallExperimentalNativeFunctions();
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +0000203 Handle<JSFunction> InstallInternalArray(Handle<JSBuiltinsObject> builtins,
204 const char* name,
205 ElementsKind elements_kind);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000206 bool InstallNatives();
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000207
jkummerow@chromium.org4e308cf2013-05-17 13:39:16 +0000208 Handle<JSFunction> InstallTypedArray(const char* name,
209 ElementsKind elementsKind);
danno@chromium.org160a7b02011-04-18 15:51:38 +0000210 bool InstallExperimentalNatives();
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000211 void InstallBuiltinFunctionIds();
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000212 void InstallJSFunctionResultCaches();
ricow@chromium.org65fae842010-08-25 15:26:24 +0000213 void InitializeNormalizedMapCaches();
ricow@chromium.org27bf2882011-11-17 08:34:43 +0000214
215 enum ExtensionTraversalState {
216 UNVISITED, VISITED, INSTALLED
217 };
218
219 class ExtensionStates {
yangguo@chromium.orgab30bb82012-02-24 14:41:46 +0000220 public:
ricow@chromium.org27bf2882011-11-17 08:34:43 +0000221 ExtensionStates();
222 ExtensionTraversalState get_state(RegisteredExtension* extension);
223 void set_state(RegisteredExtension* extension,
224 ExtensionTraversalState state);
yangguo@chromium.orgab30bb82012-02-24 14:41:46 +0000225 private:
ricow@chromium.org27bf2882011-11-17 08:34:43 +0000226 HashMap map_;
227 DISALLOW_COPY_AND_ASSIGN(ExtensionStates);
228 };
229
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000230 // Used both for deserialized and from-scratch contexts to add the extensions
231 // provided.
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000232 static bool InstallExtensions(Handle<Context> native_context,
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000233 v8::ExtensionConfiguration* extensions);
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +0000234 static bool InstallExtension(Isolate* isolate,
235 const char* name,
ricow@chromium.org27bf2882011-11-17 08:34:43 +0000236 ExtensionStates* extension_states);
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +0000237 static bool InstallExtension(Isolate* isolate,
238 v8::RegisteredExtension* current,
ricow@chromium.org27bf2882011-11-17 08:34:43 +0000239 ExtensionStates* extension_states);
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000240 static void InstallSpecialObjects(Handle<Context> native_context);
ager@chromium.org5c838252010-02-19 08:53:10 +0000241 bool InstallJSBuiltins(Handle<JSBuiltinsObject> builtins);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000242 bool ConfigureApiObject(Handle<JSObject> object,
243 Handle<ObjectTemplateInfo> object_template);
244 bool ConfigureGlobalObjects(v8::Handle<v8::ObjectTemplate> global_template);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000245
246 // Migrates all properties from the 'from' object to the 'to'
247 // object and overrides the prototype in 'to' with the one from
248 // 'from'.
249 void TransferObject(Handle<JSObject> from, Handle<JSObject> to);
250 void TransferNamedProperties(Handle<JSObject> from, Handle<JSObject> to);
251 void TransferIndexedProperties(Handle<JSObject> from, Handle<JSObject> to);
252
kmillikin@chromium.org4111b802010-05-03 10:34:42 +0000253 enum PrototypePropertyMode {
254 DONT_ADD_PROTOTYPE,
255 ADD_READONLY_PROTOTYPE,
256 ADD_WRITEABLE_PROTOTYPE
257 };
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000258
259 Handle<Map> CreateFunctionMap(PrototypePropertyMode prototype_mode);
260
yangguo@chromium.org304cc332012-07-24 07:59:48 +0000261 void SetFunctionInstanceDescriptor(Handle<Map> map,
262 PrototypePropertyMode prototypeMode);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000263 void MakeFunctionInstancePrototypeWritable();
264
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000265 Handle<Map> CreateStrictModeFunctionMap(
266 PrototypePropertyMode prototype_mode,
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000267 Handle<JSFunction> empty_function);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000268
yangguo@chromium.org304cc332012-07-24 07:59:48 +0000269 void SetStrictFunctionInstanceDescriptor(Handle<Map> map,
270 PrototypePropertyMode propertyMode);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000271
danno@chromium.org160a7b02011-04-18 15:51:38 +0000272 static bool CompileBuiltin(Isolate* isolate, int index);
273 static bool CompileExperimentalBuiltin(Isolate* isolate, int index);
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +0000274 static bool CompileNative(Isolate* isolate,
275 Vector<const char> name,
276 Handle<String> source);
277 static bool CompileScriptCached(Isolate* isolate,
278 Vector<const char> name,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000279 Handle<String> source,
280 SourceCodeCache* cache,
281 v8::Extension* extension,
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000282 Handle<Context> top_context,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000283 bool use_runtime_context);
284
jkummerow@chromium.org7bd87f02013-03-20 18:06:29 +0000285 Isolate* isolate_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000286 Handle<Context> result_;
jkummerow@chromium.org7bd87f02013-03-20 18:06:29 +0000287 Handle<Context> native_context_;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000288
ulan@chromium.org906e2fb2013-05-14 08:14:38 +0000289 // Function maps. Function maps are created initially with a read only
290 // prototype for the processing of JS builtins. Later the function maps are
291 // replaced in order to make prototype writable. These are the final, writable
292 // prototype, maps.
293 Handle<Map> function_map_writable_prototype_;
294 Handle<Map> strict_mode_function_map_writable_prototype_;
danno@chromium.org40cb8782011-05-25 07:58:50 +0000295 Handle<JSFunction> throw_type_error_function;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000296
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000297 BootstrapperActive active_;
298 friend class Bootstrapper;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000299};
300
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000301
302void Bootstrapper::Iterate(ObjectVisitor* v) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000303 extensions_cache_.Iterate(v);
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +0000304 v->Synchronize(VisitorSynchronization::kExtensions);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000305}
306
307
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000308Handle<Context> Bootstrapper::CreateEnvironment(
309 Handle<Object> global_object,
310 v8::Handle<v8::ObjectTemplate> global_template,
311 v8::ExtensionConfiguration* extensions) {
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +0000312 HandleScope scope(isolate_);
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +0000313 Genesis genesis(isolate_, global_object, global_template, extensions);
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000314 Handle<Context> env = genesis.result();
315 if (env.is_null() || !InstallExtensions(env, extensions)) {
316 return Handle<Context>();
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000317 }
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000318 return scope.CloseAndEscape(env);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000319}
320
321
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000322static void SetObjectPrototype(Handle<JSObject> object, Handle<Object> proto) {
323 // object.__proto__ = proto;
danno@chromium.org160a7b02011-04-18 15:51:38 +0000324 Factory* factory = object->GetIsolate()->factory();
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000325 Handle<Map> old_to_map = Handle<Map>(object->map());
verwaest@chromium.org753aee42012-07-17 16:15:42 +0000326 Handle<Map> new_to_map = factory->CopyMap(old_to_map);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000327 new_to_map->set_prototype(*proto);
328 object->set_map(*new_to_map);
329}
330
331
332void Bootstrapper::DetachGlobal(Handle<Context> env) {
danno@chromium.org160a7b02011-04-18 15:51:38 +0000333 Factory* factory = env->GetIsolate()->factory();
yangguo@chromium.org355cfd12012-08-29 15:32:24 +0000334 Handle<JSGlobalProxy> global_proxy(JSGlobalProxy::cast(env->global_proxy()));
335 global_proxy->set_native_context(*factory->null_value());
336 SetObjectPrototype(global_proxy, factory->null_value());
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000337 env->set_global_proxy(env->global_object());
338 env->global_object()->set_global_receiver(env->global_object());
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000339}
340
341
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +0000342void Bootstrapper::ReattachGlobal(Handle<Context> env,
yangguo@chromium.org355cfd12012-08-29 15:32:24 +0000343 Handle<JSGlobalProxy> global_proxy) {
344 env->global_object()->set_global_receiver(*global_proxy);
345 env->set_global_proxy(*global_proxy);
346 SetObjectPrototype(global_proxy, Handle<JSObject>(env->global_object()));
347 global_proxy->set_native_context(*env);
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +0000348}
349
350
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000351static Handle<JSFunction> InstallFunction(Handle<JSObject> target,
352 const char* name,
353 InstanceType type,
354 int instance_size,
355 Handle<JSObject> prototype,
356 Builtins::Name call,
jkummerow@chromium.org4e308cf2013-05-17 13:39:16 +0000357 bool install_initial_map,
358 bool set_instance_class_name) {
danno@chromium.org160a7b02011-04-18 15:51:38 +0000359 Isolate* isolate = target->GetIsolate();
lrn@chromium.org7516f052011-03-30 08:52:27 +0000360 Factory* factory = isolate->factory();
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000361 Handle<String> internalized_name = factory->InternalizeUtf8String(name);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000362 Handle<Code> call_code = Handle<Code>(isolate->builtins()->builtin(call));
kmillikin@chromium.org4111b802010-05-03 10:34:42 +0000363 Handle<JSFunction> function = prototype.is_null() ?
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000364 factory->NewFunctionWithoutPrototype(internalized_name, call_code) :
365 factory->NewFunctionWithPrototype(internalized_name,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000366 type,
367 instance_size,
368 prototype,
369 call_code,
jkummerow@chromium.org4e308cf2013-05-17 13:39:16 +0000370 install_initial_map);
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000371 PropertyAttributes attributes;
372 if (target->IsJSBuiltinsObject()) {
373 attributes =
374 static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE | READ_ONLY);
375 } else {
376 attributes = DONT_ENUM;
377 }
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000378 CHECK_NOT_EMPTY_HANDLE(isolate,
379 JSObject::SetLocalPropertyIgnoreAttributes(
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000380 target, internalized_name, function, attributes));
jkummerow@chromium.org4e308cf2013-05-17 13:39:16 +0000381 if (set_instance_class_name) {
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000382 function->shared()->set_instance_class_name(*internalized_name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000383 }
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000384 function->shared()->set_native(true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000385 return function;
386}
387
388
yangguo@chromium.org304cc332012-07-24 07:59:48 +0000389void Genesis::SetFunctionInstanceDescriptor(
390 Handle<Map> map, PrototypePropertyMode prototypeMode) {
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000391 int size = (prototypeMode == DONT_ADD_PROTOTYPE) ? 4 : 5;
verwaest@chromium.org33e09c82012-10-10 17:07:22 +0000392 Handle<DescriptorArray> descriptors(factory()->NewDescriptorArray(0, size));
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000393 DescriptorArray::WhitenessWitness witness(*descriptors);
394
yangguo@chromium.org304cc332012-07-24 07:59:48 +0000395 Handle<Foreign> length(factory()->NewForeign(&Accessors::FunctionLength));
396 Handle<Foreign> name(factory()->NewForeign(&Accessors::FunctionName));
397 Handle<Foreign> args(factory()->NewForeign(&Accessors::FunctionArguments));
398 Handle<Foreign> caller(factory()->NewForeign(&Accessors::FunctionCaller));
399 Handle<Foreign> prototype;
400 if (prototypeMode != DONT_ADD_PROTOTYPE) {
401 prototype = factory()->NewForeign(&Accessors::FunctionPrototype);
402 }
403 PropertyAttributes attribs = static_cast<PropertyAttributes>(
404 DONT_ENUM | DONT_DELETE | READ_ONLY);
rossberg@chromium.org89e18f52012-10-22 13:09:53 +0000405 map->set_instance_descriptors(*descriptors);
yangguo@chromium.org304cc332012-07-24 07:59:48 +0000406
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000407 { // Add length.
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000408 CallbacksDescriptor d(*factory()->length_string(), *length, attribs);
yangguo@chromium.org304cc332012-07-24 07:59:48 +0000409 map->AppendDescriptor(&d, witness);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000410 }
411 { // Add name.
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000412 CallbacksDescriptor d(*factory()->name_string(), *name, attribs);
yangguo@chromium.org304cc332012-07-24 07:59:48 +0000413 map->AppendDescriptor(&d, witness);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000414 }
415 { // Add arguments.
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000416 CallbacksDescriptor d(*factory()->arguments_string(), *args, attribs);
yangguo@chromium.org304cc332012-07-24 07:59:48 +0000417 map->AppendDescriptor(&d, witness);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000418 }
419 { // Add caller.
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000420 CallbacksDescriptor d(*factory()->caller_string(), *caller, attribs);
yangguo@chromium.org304cc332012-07-24 07:59:48 +0000421 map->AppendDescriptor(&d, witness);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000422 }
423 if (prototypeMode != DONT_ADD_PROTOTYPE) {
424 // Add prototype.
425 if (prototypeMode == ADD_WRITEABLE_PROTOTYPE) {
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000426 attribs = static_cast<PropertyAttributes>(attribs & ~READ_ONLY);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000427 }
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000428 CallbacksDescriptor d(*factory()->prototype_string(), *prototype, attribs);
yangguo@chromium.org304cc332012-07-24 07:59:48 +0000429 map->AppendDescriptor(&d, witness);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000430 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000431}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000432
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000433
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000434Handle<Map> Genesis::CreateFunctionMap(PrototypePropertyMode prototype_mode) {
danno@chromium.org160a7b02011-04-18 15:51:38 +0000435 Handle<Map> map = factory()->NewMap(JS_FUNCTION_TYPE, JSFunction::kSize);
yangguo@chromium.org304cc332012-07-24 07:59:48 +0000436 SetFunctionInstanceDescriptor(map, prototype_mode);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000437 map->set_function_with_prototype(prototype_mode != DONT_ADD_PROTOTYPE);
438 return map;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000439}
440
441
danno@chromium.org160a7b02011-04-18 15:51:38 +0000442Handle<JSFunction> Genesis::CreateEmptyFunction(Isolate* isolate) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000443 // Allocate the map for function instances. Maps are allocated first and their
444 // prototypes patched later, once empty function is created.
445
kmillikin@chromium.org4111b802010-05-03 10:34:42 +0000446 // Functions with this map will not have a 'prototype' property, and
447 // can not be used as constructors.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000448 Handle<Map> function_without_prototype_map =
449 CreateFunctionMap(DONT_ADD_PROTOTYPE);
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000450 native_context()->set_function_without_prototype_map(
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000451 *function_without_prototype_map);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000452
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000453 // Allocate the function map. This map is temporary, used only for processing
454 // of builtins.
455 // Later the map is replaced with writable prototype map, allocated below.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000456 Handle<Map> function_map = CreateFunctionMap(ADD_READONLY_PROTOTYPE);
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000457 native_context()->set_function_map(*function_map);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000458
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000459 // The final map for functions. Writeable prototype.
460 // This map is installed in MakeFunctionInstancePrototypeWritable.
ulan@chromium.org906e2fb2013-05-14 08:14:38 +0000461 function_map_writable_prototype_ = CreateFunctionMap(ADD_WRITEABLE_PROTOTYPE);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000462
lrn@chromium.org7516f052011-03-30 08:52:27 +0000463 Factory* factory = isolate->factory();
lrn@chromium.org7516f052011-03-30 08:52:27 +0000464
ulan@chromium.org906e2fb2013-05-14 08:14:38 +0000465 Handle<String> object_name = factory->Object_string();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000466
467 { // --- O b j e c t ---
468 Handle<JSFunction> object_fun =
lrn@chromium.org7516f052011-03-30 08:52:27 +0000469 factory->NewFunction(object_name, factory->null_value());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000470 Handle<Map> object_function_map =
lrn@chromium.org7516f052011-03-30 08:52:27 +0000471 factory->NewMap(JS_OBJECT_TYPE, JSObject::kHeaderSize);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000472 object_fun->set_initial_map(*object_function_map);
473 object_function_map->set_constructor(*object_fun);
474
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000475 native_context()->set_object_function(*object_fun);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000476
477 // Allocate a new prototype for the object function.
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000478 Handle<JSObject> prototype = factory->NewJSObject(
479 isolate->object_function(),
mstarzinger@chromium.org71fc3462013-02-27 09:34:27 +0000480 TENURED);
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000481
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000482 native_context()->set_initial_object_prototype(*prototype);
ulan@chromium.org906e2fb2013-05-14 08:14:38 +0000483 // For bootstrapping set the array prototype to be the same as the object
484 // prototype, otherwise the missing initial_array_prototype will cause
485 // assertions during startup.
486 native_context()->set_initial_array_prototype(*prototype);
rossberg@chromium.orgebeba022013-08-19 09:36:44 +0000487 Accessors::FunctionSetPrototype(object_fun, prototype);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000488 }
489
490 // Allocate the empty function as the prototype for function ECMAScript
491 // 262 15.3.4.
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000492 Handle<String> empty_string =
493 factory->InternalizeOneByteString(STATIC_ASCII_VECTOR("Empty"));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000494 Handle<JSFunction> empty_function =
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000495 factory->NewFunctionWithoutPrototype(empty_string, CLASSIC_MODE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000496
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000497 // --- E m p t y ---
498 Handle<Code> code =
lrn@chromium.org7516f052011-03-30 08:52:27 +0000499 Handle<Code>(isolate->builtins()->builtin(
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000500 Builtins::kEmptyFunction));
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000501 empty_function->set_code(*code);
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000502 empty_function->shared()->set_code(*code);
jkummerow@chromium.org59297c72013-01-09 16:32:23 +0000503 Handle<String> source =
504 factory->NewStringFromOneByte(STATIC_ASCII_VECTOR("() {}"));
lrn@chromium.org7516f052011-03-30 08:52:27 +0000505 Handle<Script> script = factory->NewScript(source);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000506 script->set_type(Smi::FromInt(Script::TYPE_NATIVE));
507 empty_function->shared()->set_script(*script);
508 empty_function->shared()->set_start_position(0);
509 empty_function->shared()->set_end_position(source->length());
510 empty_function->shared()->DontAdaptArguments();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000511
512 // Set prototypes for the function maps.
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000513 native_context()->function_map()->set_prototype(*empty_function);
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000514 native_context()->function_without_prototype_map()->
kmillikin@chromium.org4111b802010-05-03 10:34:42 +0000515 set_prototype(*empty_function);
ulan@chromium.org906e2fb2013-05-14 08:14:38 +0000516 function_map_writable_prototype_->set_prototype(*empty_function);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000517
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000518 // Allocate the function map first and then patch the prototype later
yangguo@chromium.org5f0b8ea2012-05-16 12:37:04 +0000519 Handle<Map> empty_function_map = CreateFunctionMap(DONT_ADD_PROTOTYPE);
520 empty_function_map->set_prototype(
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000521 native_context()->object_function()->prototype());
yangguo@chromium.org5f0b8ea2012-05-16 12:37:04 +0000522 empty_function->set_map(*empty_function_map);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000523 return empty_function;
524}
525
526
yangguo@chromium.org304cc332012-07-24 07:59:48 +0000527void Genesis::SetStrictFunctionInstanceDescriptor(
528 Handle<Map> map, PrototypePropertyMode prototypeMode) {
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000529 int size = (prototypeMode == DONT_ADD_PROTOTYPE) ? 4 : 5;
verwaest@chromium.org33e09c82012-10-10 17:07:22 +0000530 Handle<DescriptorArray> descriptors(factory()->NewDescriptorArray(0, size));
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000531 DescriptorArray::WhitenessWitness witness(*descriptors);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000532
yangguo@chromium.org304cc332012-07-24 07:59:48 +0000533 Handle<Foreign> length(factory()->NewForeign(&Accessors::FunctionLength));
534 Handle<Foreign> name(factory()->NewForeign(&Accessors::FunctionName));
535 Handle<AccessorPair> arguments(factory()->NewAccessorPair());
536 Handle<AccessorPair> caller(factory()->NewAccessorPair());
537 Handle<Foreign> prototype;
538 if (prototypeMode != DONT_ADD_PROTOTYPE) {
539 prototype = factory()->NewForeign(&Accessors::FunctionPrototype);
540 }
dslomov@chromium.orgb752d402013-06-18 11:54:54 +0000541 PropertyAttributes rw_attribs =
542 static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE);
543 PropertyAttributes ro_attribs =
544 static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE | READ_ONLY);
rossberg@chromium.org89e18f52012-10-22 13:09:53 +0000545 map->set_instance_descriptors(*descriptors);
yangguo@chromium.org304cc332012-07-24 07:59:48 +0000546
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000547 { // Add length.
dslomov@chromium.orgb752d402013-06-18 11:54:54 +0000548 CallbacksDescriptor d(*factory()->length_string(), *length, ro_attribs);
yangguo@chromium.org304cc332012-07-24 07:59:48 +0000549 map->AppendDescriptor(&d, witness);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000550 }
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000551 { // Add name.
dslomov@chromium.orgb752d402013-06-18 11:54:54 +0000552 CallbacksDescriptor d(*factory()->name_string(), *name, rw_attribs);
yangguo@chromium.org304cc332012-07-24 07:59:48 +0000553 map->AppendDescriptor(&d, witness);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000554 }
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000555 { // Add arguments.
dslomov@chromium.orgb752d402013-06-18 11:54:54 +0000556 CallbacksDescriptor d(*factory()->arguments_string(), *arguments,
557 rw_attribs);
yangguo@chromium.org304cc332012-07-24 07:59:48 +0000558 map->AppendDescriptor(&d, witness);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000559 }
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000560 { // Add caller.
dslomov@chromium.orgb752d402013-06-18 11:54:54 +0000561 CallbacksDescriptor d(*factory()->caller_string(), *caller, rw_attribs);
yangguo@chromium.org304cc332012-07-24 07:59:48 +0000562 map->AppendDescriptor(&d, witness);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000563 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000564 if (prototypeMode != DONT_ADD_PROTOTYPE) {
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000565 // Add prototype.
dslomov@chromium.orgb752d402013-06-18 11:54:54 +0000566 PropertyAttributes attribs =
567 prototypeMode == ADD_WRITEABLE_PROTOTYPE ? rw_attribs : ro_attribs;
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000568 CallbacksDescriptor d(*factory()->prototype_string(), *prototype, attribs);
yangguo@chromium.org304cc332012-07-24 07:59:48 +0000569 map->AppendDescriptor(&d, witness);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000570 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000571}
572
573
574// ECMAScript 5th Edition, 13.2.3
danno@chromium.org40cb8782011-05-25 07:58:50 +0000575Handle<JSFunction> Genesis::GetThrowTypeErrorFunction() {
576 if (throw_type_error_function.is_null()) {
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000577 Handle<String> name = factory()->InternalizeOneByteString(
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +0000578 STATIC_ASCII_VECTOR("ThrowTypeError"));
danno@chromium.org40cb8782011-05-25 07:58:50 +0000579 throw_type_error_function =
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000580 factory()->NewFunctionWithoutPrototype(name, CLASSIC_MODE);
danno@chromium.org40cb8782011-05-25 07:58:50 +0000581 Handle<Code> code(isolate()->builtins()->builtin(
582 Builtins::kStrictModePoisonPill));
583 throw_type_error_function->set_map(
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000584 native_context()->function_map());
danno@chromium.org40cb8782011-05-25 07:58:50 +0000585 throw_type_error_function->set_code(*code);
586 throw_type_error_function->shared()->set_code(*code);
587 throw_type_error_function->shared()->DontAdaptArguments();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000588
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000589 JSObject::PreventExtensions(throw_type_error_function);
danno@chromium.org40cb8782011-05-25 07:58:50 +0000590 }
591 return throw_type_error_function;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000592}
593
594
595Handle<Map> Genesis::CreateStrictModeFunctionMap(
596 PrototypePropertyMode prototype_mode,
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000597 Handle<JSFunction> empty_function) {
danno@chromium.org160a7b02011-04-18 15:51:38 +0000598 Handle<Map> map = factory()->NewMap(JS_FUNCTION_TYPE, JSFunction::kSize);
yangguo@chromium.org304cc332012-07-24 07:59:48 +0000599 SetStrictFunctionInstanceDescriptor(map, prototype_mode);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000600 map->set_function_with_prototype(prototype_mode != DONT_ADD_PROTOTYPE);
601 map->set_prototype(*empty_function);
602 return map;
603}
604
605
606void Genesis::CreateStrictModeFunctionMaps(Handle<JSFunction> empty) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000607 // Allocate map for the prototype-less strict mode instances.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000608 Handle<Map> strict_mode_function_without_prototype_map =
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000609 CreateStrictModeFunctionMap(DONT_ADD_PROTOTYPE, empty);
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000610 native_context()->set_strict_mode_function_without_prototype_map(
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000611 *strict_mode_function_without_prototype_map);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000612
613 // Allocate map for the strict mode functions. This map is temporary, used
614 // only for processing of builtins.
615 // Later the map is replaced with writable prototype map, allocated below.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000616 Handle<Map> strict_mode_function_map =
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000617 CreateStrictModeFunctionMap(ADD_READONLY_PROTOTYPE, empty);
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000618 native_context()->set_strict_mode_function_map(
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000619 *strict_mode_function_map);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000620
621 // The final map for the strict mode functions. Writeable prototype.
622 // This map is installed in MakeFunctionInstancePrototypeWritable.
ulan@chromium.org906e2fb2013-05-14 08:14:38 +0000623 strict_mode_function_map_writable_prototype_ =
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000624 CreateStrictModeFunctionMap(ADD_WRITEABLE_PROTOTYPE, empty);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000625
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000626 // Complete the callbacks.
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000627 PoisonArgumentsAndCaller(strict_mode_function_without_prototype_map);
628 PoisonArgumentsAndCaller(strict_mode_function_map);
ulan@chromium.org906e2fb2013-05-14 08:14:38 +0000629 PoisonArgumentsAndCaller(strict_mode_function_map_writable_prototype_);
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000630}
631
632
633static void SetAccessors(Handle<Map> map,
634 Handle<String> name,
635 Handle<JSFunction> func) {
636 DescriptorArray* descs = map->instance_descriptors();
verwaest@chromium.org06ab2ec2012-10-09 17:00:13 +0000637 int number = descs->SearchWithCache(*name, *map);
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000638 AccessorPair* accessors = AccessorPair::cast(descs->GetValue(number));
639 accessors->set_getter(*func);
640 accessors->set_setter(*func);
641}
642
643
644void Genesis::PoisonArgumentsAndCaller(Handle<Map> map) {
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000645 SetAccessors(map, factory()->arguments_string(), GetThrowTypeErrorFunction());
646 SetAccessors(map, factory()->caller_string(), GetThrowTypeErrorFunction());
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000647}
648
649
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000650static void AddToWeakNativeContextList(Context* context) {
651 ASSERT(context->IsNativeContext());
danno@chromium.org160a7b02011-04-18 15:51:38 +0000652 Heap* heap = context->GetIsolate()->heap();
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000653#ifdef DEBUG
654 { // NOLINT
655 ASSERT(context->get(Context::NEXT_CONTEXT_LINK)->IsUndefined());
656 // Check that context is not in the list yet.
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000657 for (Object* current = heap->native_contexts_list();
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000658 !current->IsUndefined();
659 current = Context::cast(current)->get(Context::NEXT_CONTEXT_LINK)) {
660 ASSERT(current != context);
661 }
662 }
663#endif
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000664 context->set(Context::NEXT_CONTEXT_LINK, heap->native_contexts_list());
665 heap->set_native_contexts_list(context);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000666}
667
668
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000669void Genesis::CreateRoots() {
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000670 // Allocate the native context FixedArray first and then patch the
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000671 // closure and extension object later (we need the empty function
672 // and the global object, but in order to create those, we need the
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000673 // native context).
jkummerow@chromium.org7bd87f02013-03-20 18:06:29 +0000674 native_context_ = factory()->NewNativeContext();
675 AddToWeakNativeContextList(*native_context());
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000676 isolate()->set_context(*native_context());
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000677
678 // Allocate the message listeners object.
679 {
680 v8::NeanderArray listeners;
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000681 native_context()->set_message_listeners(*listeners.value());
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000682 }
683}
684
685
686Handle<JSGlobalProxy> Genesis::CreateNewGlobals(
687 v8::Handle<v8::ObjectTemplate> global_template,
688 Handle<Object> global_object,
689 Handle<GlobalObject>* inner_global_out) {
690 // The argument global_template aka data is an ObjectTemplateInfo.
691 // It has a constructor pointer that points at global_constructor which is a
692 // FunctionTemplateInfo.
693 // The global_constructor is used to create or reinitialize the global_proxy.
694 // The global_constructor also has a prototype_template pointer that points at
695 // js_global_template which is an ObjectTemplateInfo.
696 // That in turn has a constructor pointer that points at
697 // js_global_constructor which is a FunctionTemplateInfo.
698 // js_global_constructor is used to make js_global_function
699 // js_global_function is used to make the new inner_global.
700 //
701 // --- G l o b a l ---
702 // Step 1: Create a fresh inner JSGlobalObject.
703 Handle<JSFunction> js_global_function;
704 Handle<ObjectTemplateInfo> js_global_template;
705 if (!global_template.IsEmpty()) {
706 // Get prototype template of the global_template.
707 Handle<ObjectTemplateInfo> data =
708 v8::Utils::OpenHandle(*global_template);
709 Handle<FunctionTemplateInfo> global_constructor =
710 Handle<FunctionTemplateInfo>(
711 FunctionTemplateInfo::cast(data->constructor()));
ulan@chromium.org09d7ab52013-02-25 15:50:35 +0000712 Handle<Object> proto_template(global_constructor->prototype_template(),
713 isolate());
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000714 if (!proto_template->IsUndefined()) {
715 js_global_template =
716 Handle<ObjectTemplateInfo>::cast(proto_template);
717 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000718 }
719
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000720 if (js_global_template.is_null()) {
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000721 Handle<String> name = Handle<String>(heap()->empty_string());
danno@chromium.org160a7b02011-04-18 15:51:38 +0000722 Handle<Code> code = Handle<Code>(isolate()->builtins()->builtin(
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000723 Builtins::kIllegal));
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000724 js_global_function =
danno@chromium.org160a7b02011-04-18 15:51:38 +0000725 factory()->NewFunction(name, JS_GLOBAL_OBJECT_TYPE,
726 JSGlobalObject::kSize, code, true);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000727 // Change the constructor property of the prototype of the
728 // hidden global function to refer to the Object function.
729 Handle<JSObject> prototype =
730 Handle<JSObject>(
731 JSObject::cast(js_global_function->instance_prototype()));
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000732 CHECK_NOT_EMPTY_HANDLE(isolate(),
733 JSObject::SetLocalPropertyIgnoreAttributes(
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000734 prototype, factory()->constructor_string(),
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000735 isolate()->object_function(), NONE));
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000736 } else {
737 Handle<FunctionTemplateInfo> js_global_constructor(
738 FunctionTemplateInfo::cast(js_global_template->constructor()));
739 js_global_function =
danno@chromium.org160a7b02011-04-18 15:51:38 +0000740 factory()->CreateApiFunction(js_global_constructor,
741 factory()->InnerGlobalObject);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000742 }
743
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000744 js_global_function->initial_map()->set_is_hidden_prototype();
erik.corry@gmail.com88767242012-08-08 14:43:45 +0000745 js_global_function->initial_map()->set_dictionary_map(true);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000746 Handle<GlobalObject> inner_global =
danno@chromium.org160a7b02011-04-18 15:51:38 +0000747 factory()->NewGlobalObject(js_global_function);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000748 if (inner_global_out != NULL) {
749 *inner_global_out = inner_global;
750 }
751
752 // Step 2: create or re-initialize the global proxy object.
753 Handle<JSFunction> global_proxy_function;
754 if (global_template.IsEmpty()) {
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000755 Handle<String> name = Handle<String>(heap()->empty_string());
danno@chromium.org160a7b02011-04-18 15:51:38 +0000756 Handle<Code> code = Handle<Code>(isolate()->builtins()->builtin(
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000757 Builtins::kIllegal));
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000758 global_proxy_function =
danno@chromium.org160a7b02011-04-18 15:51:38 +0000759 factory()->NewFunction(name, JS_GLOBAL_PROXY_TYPE,
760 JSGlobalProxy::kSize, code, true);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000761 } else {
762 Handle<ObjectTemplateInfo> data =
763 v8::Utils::OpenHandle(*global_template);
764 Handle<FunctionTemplateInfo> global_constructor(
765 FunctionTemplateInfo::cast(data->constructor()));
766 global_proxy_function =
danno@chromium.org160a7b02011-04-18 15:51:38 +0000767 factory()->CreateApiFunction(global_constructor,
768 factory()->OuterGlobalObject);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000769 }
770
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000771 Handle<String> global_name = factory()->InternalizeOneByteString(
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +0000772 STATIC_ASCII_VECTOR("global"));
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000773 global_proxy_function->shared()->set_instance_class_name(*global_name);
774 global_proxy_function->initial_map()->set_is_access_check_needed(true);
775
776 // Set global_proxy.__proto__ to js_global after ConfigureGlobalObjects
777 // Return the global proxy.
778
779 if (global_object.location() != NULL) {
780 ASSERT(global_object->IsJSGlobalProxy());
781 return ReinitializeJSGlobalProxy(
782 global_proxy_function,
783 Handle<JSGlobalProxy>::cast(global_object));
784 } else {
785 return Handle<JSGlobalProxy>::cast(
danno@chromium.org160a7b02011-04-18 15:51:38 +0000786 factory()->NewJSObject(global_proxy_function, TENURED));
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000787 }
788}
789
790
791void Genesis::HookUpGlobalProxy(Handle<GlobalObject> inner_global,
792 Handle<JSGlobalProxy> global_proxy) {
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000793 // Set the native context for the global object.
794 inner_global->set_native_context(*native_context());
yangguo@chromium.org355cfd12012-08-29 15:32:24 +0000795 inner_global->set_global_context(*native_context());
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000796 inner_global->set_global_receiver(*global_proxy);
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000797 global_proxy->set_native_context(*native_context());
798 native_context()->set_global_proxy(*global_proxy);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000799}
800
801
802void Genesis::HookUpInnerGlobal(Handle<GlobalObject> inner_global) {
803 Handle<GlobalObject> inner_global_from_snapshot(
jkummerow@chromium.org7bd87f02013-03-20 18:06:29 +0000804 GlobalObject::cast(native_context()->extension()));
805 Handle<JSBuiltinsObject> builtins_global(native_context()->builtins());
806 native_context()->set_extension(*inner_global);
807 native_context()->set_global_object(*inner_global);
808 native_context()->set_security_token(*inner_global);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000809 static const PropertyAttributes attributes =
810 static_cast<PropertyAttributes>(READ_ONLY | DONT_DELETE);
811 ForceSetProperty(builtins_global,
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000812 factory()->InternalizeOneByteString(
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +0000813 STATIC_ASCII_VECTOR("global")),
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000814 inner_global,
815 attributes);
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000816 // Set up the reference from the global object to the builtins object.
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000817 JSGlobalObject::cast(*inner_global)->set_builtins(*builtins_global);
818 TransferNamedProperties(inner_global_from_snapshot, inner_global);
819 TransferIndexedProperties(inner_global_from_snapshot, inner_global);
820}
821
822
823// This is only called if we are not using snapshots. The equivalent
824// work in the snapshot case is done in HookUpInnerGlobal.
jkummerow@chromium.orgba72ec82013-07-22 09:21:20 +0000825void Genesis::InitializeGlobal(Handle<GlobalObject> inner_global,
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000826 Handle<JSFunction> empty_function) {
mstarzinger@chromium.orga2e1a402013-10-15 08:25:05 +0000827 // --- N a t i v e C o n t e x t ---
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000828 // Use the empty function as closure (no scope info).
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000829 native_context()->set_closure(*empty_function);
830 native_context()->set_previous(NULL);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000831 // Set extension and global object.
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000832 native_context()->set_extension(*inner_global);
833 native_context()->set_global_object(*inner_global);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000834 // Security setup: Set the security token of the global object to
835 // its the inner global. This makes the security check between two
836 // different contexts fail by default even in case of global
837 // object reinitialization.
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000838 native_context()->set_security_token(*inner_global);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000839
danno@chromium.org160a7b02011-04-18 15:51:38 +0000840 Isolate* isolate = inner_global->GetIsolate();
lrn@chromium.org7516f052011-03-30 08:52:27 +0000841 Factory* factory = isolate->factory();
842 Heap* heap = isolate->heap();
843
ulan@chromium.org906e2fb2013-05-14 08:14:38 +0000844 Handle<String> object_name = factory->Object_string();
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000845 CHECK_NOT_EMPTY_HANDLE(isolate,
846 JSObject::SetLocalPropertyIgnoreAttributes(
847 inner_global, object_name,
848 isolate->object_function(), DONT_ENUM));
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000849
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000850 Handle<JSObject> global = Handle<JSObject>(native_context()->global_object());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000851
852 // Install global Function object
853 InstallFunction(global, "Function", JS_FUNCTION_TYPE, JSFunction::kSize,
jkummerow@chromium.org4e308cf2013-05-17 13:39:16 +0000854 empty_function, Builtins::kIllegal, true, true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000855
856 { // --- A r r a y ---
857 Handle<JSFunction> array_function =
858 InstallFunction(global, "Array", JS_ARRAY_TYPE, JSArray::kSize,
lrn@chromium.org7516f052011-03-30 08:52:27 +0000859 isolate->initial_object_prototype(),
jkummerow@chromium.org4e308cf2013-05-17 13:39:16 +0000860 Builtins::kArrayCode, true, true);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000861 array_function->shared()->DontAdaptArguments();
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +0000862 array_function->shared()->set_function_data(Smi::FromInt(kArrayCode));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000863
864 // This seems a bit hackish, but we need to make sure Array.length
865 // is 1.
866 array_function->shared()->set_length(1);
yangguo@chromium.org304cc332012-07-24 07:59:48 +0000867
verwaest@chromium.orgde64f722012-08-16 15:44:54 +0000868 Handle<Map> initial_map(array_function->initial_map());
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +0000869
870 // This assert protects an optimization in
871 // HGraphBuilder::JSArrayBuilder::EmitMapCode()
872 ASSERT(initial_map->elements_kind() == GetInitialFastElementsKind());
873
verwaest@chromium.org33e09c82012-10-10 17:07:22 +0000874 Handle<DescriptorArray> array_descriptors(
875 factory->NewDescriptorArray(0, 1));
yangguo@chromium.org304cc332012-07-24 07:59:48 +0000876 DescriptorArray::WhitenessWitness witness(*array_descriptors);
877
878 Handle<Foreign> array_length(factory->NewForeign(&Accessors::ArrayLength));
879 PropertyAttributes attribs = static_cast<PropertyAttributes>(
880 DONT_ENUM | DONT_DELETE);
rossberg@chromium.org89e18f52012-10-22 13:09:53 +0000881 initial_map->set_instance_descriptors(*array_descriptors);
yangguo@chromium.org304cc332012-07-24 07:59:48 +0000882
883 { // Add length.
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000884 CallbacksDescriptor d(*factory->length_string(), *array_length, attribs);
yangguo@chromium.org304cc332012-07-24 07:59:48 +0000885 array_function->initial_map()->AppendDescriptor(&d, witness);
886 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000887
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000888 // array_function is used internally. JS code creating array object should
889 // search for the 'Array' property on the global object and use that one
890 // as the constructor. 'Array' property on a global object can be
891 // overwritten by JS code.
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000892 native_context()->set_array_function(*array_function);
danno@chromium.orgca29dd82013-04-26 11:59:48 +0000893
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +0000894 // Cache the array maps, needed by ArrayConstructorStub
895 CacheInitialJSArrayMaps(native_context(), initial_map);
896 ArrayConstructorStub array_constructor_stub(isolate);
897 Handle<Code> code = array_constructor_stub.GetCode(isolate);
898 array_function->shared()->set_construct_stub(*code);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000899 }
900
901 { // --- N u m b e r ---
902 Handle<JSFunction> number_fun =
903 InstallFunction(global, "Number", JS_VALUE_TYPE, JSValue::kSize,
lrn@chromium.org7516f052011-03-30 08:52:27 +0000904 isolate->initial_object_prototype(),
jkummerow@chromium.org4e308cf2013-05-17 13:39:16 +0000905 Builtins::kIllegal, true, true);
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000906 native_context()->set_number_function(*number_fun);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000907 }
908
909 { // --- B o o l e a n ---
910 Handle<JSFunction> boolean_fun =
911 InstallFunction(global, "Boolean", JS_VALUE_TYPE, JSValue::kSize,
lrn@chromium.org7516f052011-03-30 08:52:27 +0000912 isolate->initial_object_prototype(),
jkummerow@chromium.org4e308cf2013-05-17 13:39:16 +0000913 Builtins::kIllegal, true, true);
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000914 native_context()->set_boolean_function(*boolean_fun);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000915 }
916
917 { // --- S t r i n g ---
918 Handle<JSFunction> string_fun =
919 InstallFunction(global, "String", JS_VALUE_TYPE, JSValue::kSize,
lrn@chromium.org7516f052011-03-30 08:52:27 +0000920 isolate->initial_object_prototype(),
jkummerow@chromium.org4e308cf2013-05-17 13:39:16 +0000921 Builtins::kIllegal, true, true);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000922 string_fun->shared()->set_construct_stub(
lrn@chromium.org7516f052011-03-30 08:52:27 +0000923 isolate->builtins()->builtin(Builtins::kStringConstructCode));
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000924 native_context()->set_string_function(*string_fun);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000925
926 Handle<Map> string_map =
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000927 Handle<Map>(native_context()->string_function()->initial_map());
verwaest@chromium.org33e09c82012-10-10 17:07:22 +0000928 Handle<DescriptorArray> string_descriptors(
929 factory->NewDescriptorArray(0, 1));
yangguo@chromium.org304cc332012-07-24 07:59:48 +0000930 DescriptorArray::WhitenessWitness witness(*string_descriptors);
931
932 Handle<Foreign> string_length(
933 factory->NewForeign(&Accessors::StringLength));
934 PropertyAttributes attribs = static_cast<PropertyAttributes>(
935 DONT_ENUM | DONT_DELETE | READ_ONLY);
rossberg@chromium.org89e18f52012-10-22 13:09:53 +0000936 string_map->set_instance_descriptors(*string_descriptors);
yangguo@chromium.org304cc332012-07-24 07:59:48 +0000937
938 { // Add length.
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000939 CallbacksDescriptor d(*factory->length_string(), *string_length, attribs);
yangguo@chromium.org304cc332012-07-24 07:59:48 +0000940 string_map->AppendDescriptor(&d, witness);
941 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000942 }
943
944 { // --- D a t e ---
945 // Builtin functions for Date.prototype.
946 Handle<JSFunction> date_fun =
svenpanne@chromium.org4efbdb12012-03-12 08:18:42 +0000947 InstallFunction(global, "Date", JS_DATE_TYPE, JSDate::kSize,
lrn@chromium.org7516f052011-03-30 08:52:27 +0000948 isolate->initial_object_prototype(),
jkummerow@chromium.org4e308cf2013-05-17 13:39:16 +0000949 Builtins::kIllegal, true, true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000950
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000951 native_context()->set_date_function(*date_fun);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000952 }
953
954
955 { // -- R e g E x p
956 // Builtin functions for RegExp.prototype.
957 Handle<JSFunction> regexp_fun =
ager@chromium.org236ad962008-09-25 09:45:57 +0000958 InstallFunction(global, "RegExp", JS_REGEXP_TYPE, JSRegExp::kSize,
lrn@chromium.org7516f052011-03-30 08:52:27 +0000959 isolate->initial_object_prototype(),
jkummerow@chromium.org4e308cf2013-05-17 13:39:16 +0000960 Builtins::kIllegal, true, true);
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000961 native_context()->set_regexp_function(*regexp_fun);
lrn@chromium.org25156de2010-04-06 13:10:27 +0000962
963 ASSERT(regexp_fun->has_initial_map());
964 Handle<Map> initial_map(regexp_fun->initial_map());
965
966 ASSERT_EQ(0, initial_map->inobject_properties());
967
lrn@chromium.org25156de2010-04-06 13:10:27 +0000968 PropertyAttributes final =
969 static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE | READ_ONLY);
verwaest@chromium.org33e09c82012-10-10 17:07:22 +0000970 Handle<DescriptorArray> descriptors = factory->NewDescriptorArray(0, 5);
yangguo@chromium.org304cc332012-07-24 07:59:48 +0000971 DescriptorArray::WhitenessWitness witness(*descriptors);
rossberg@chromium.org89e18f52012-10-22 13:09:53 +0000972 initial_map->set_instance_descriptors(*descriptors);
yangguo@chromium.org304cc332012-07-24 07:59:48 +0000973
lrn@chromium.org25156de2010-04-06 13:10:27 +0000974 {
975 // ECMA-262, section 15.10.7.1.
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000976 FieldDescriptor field(heap->source_string(),
lrn@chromium.org25156de2010-04-06 13:10:27 +0000977 JSRegExp::kSourceFieldIndex,
danno@chromium.orgf005df62013-04-30 16:36:45 +0000978 final,
979 Representation::Tagged());
yangguo@chromium.org304cc332012-07-24 07:59:48 +0000980 initial_map->AppendDescriptor(&field, witness);
lrn@chromium.org25156de2010-04-06 13:10:27 +0000981 }
982 {
983 // ECMA-262, section 15.10.7.2.
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000984 FieldDescriptor field(heap->global_string(),
lrn@chromium.org25156de2010-04-06 13:10:27 +0000985 JSRegExp::kGlobalFieldIndex,
danno@chromium.orgf005df62013-04-30 16:36:45 +0000986 final,
987 Representation::Tagged());
yangguo@chromium.org304cc332012-07-24 07:59:48 +0000988 initial_map->AppendDescriptor(&field, witness);
lrn@chromium.org25156de2010-04-06 13:10:27 +0000989 }
990 {
991 // ECMA-262, section 15.10.7.3.
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000992 FieldDescriptor field(heap->ignore_case_string(),
lrn@chromium.org25156de2010-04-06 13:10:27 +0000993 JSRegExp::kIgnoreCaseFieldIndex,
danno@chromium.orgf005df62013-04-30 16:36:45 +0000994 final,
995 Representation::Tagged());
yangguo@chromium.org304cc332012-07-24 07:59:48 +0000996 initial_map->AppendDescriptor(&field, witness);
lrn@chromium.org25156de2010-04-06 13:10:27 +0000997 }
998 {
999 // ECMA-262, section 15.10.7.4.
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001000 FieldDescriptor field(heap->multiline_string(),
lrn@chromium.org25156de2010-04-06 13:10:27 +00001001 JSRegExp::kMultilineFieldIndex,
danno@chromium.orgf005df62013-04-30 16:36:45 +00001002 final,
1003 Representation::Tagged());
yangguo@chromium.org304cc332012-07-24 07:59:48 +00001004 initial_map->AppendDescriptor(&field, witness);
lrn@chromium.org25156de2010-04-06 13:10:27 +00001005 }
1006 {
1007 // ECMA-262, section 15.10.7.5.
1008 PropertyAttributes writable =
1009 static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE);
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001010 FieldDescriptor field(heap->last_index_string(),
lrn@chromium.org25156de2010-04-06 13:10:27 +00001011 JSRegExp::kLastIndexFieldIndex,
danno@chromium.orgf005df62013-04-30 16:36:45 +00001012 writable,
1013 Representation::Tagged());
yangguo@chromium.org304cc332012-07-24 07:59:48 +00001014 initial_map->AppendDescriptor(&field, witness);
lrn@chromium.org25156de2010-04-06 13:10:27 +00001015 }
lrn@chromium.org25156de2010-04-06 13:10:27 +00001016
1017 initial_map->set_inobject_properties(5);
1018 initial_map->set_pre_allocated_property_fields(5);
1019 initial_map->set_unused_property_fields(0);
1020 initial_map->set_instance_size(
1021 initial_map->instance_size() + 5 * kPointerSize);
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00001022 initial_map->set_visitor_id(StaticVisitorBase::GetVisitorId(*initial_map));
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001023
1024 // RegExp prototype object is itself a RegExp.
verwaest@chromium.org753aee42012-07-17 16:15:42 +00001025 Handle<Map> proto_map = factory->CopyMap(initial_map);
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001026 proto_map->set_prototype(native_context()->initial_object_prototype());
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001027 Handle<JSObject> proto = factory->NewJSObjectFromMap(proto_map);
1028 proto->InObjectPropertyAtPut(JSRegExp::kSourceFieldIndex,
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001029 heap->query_colon_string());
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001030 proto->InObjectPropertyAtPut(JSRegExp::kGlobalFieldIndex,
1031 heap->false_value());
1032 proto->InObjectPropertyAtPut(JSRegExp::kIgnoreCaseFieldIndex,
1033 heap->false_value());
1034 proto->InObjectPropertyAtPut(JSRegExp::kMultilineFieldIndex,
1035 heap->false_value());
1036 proto->InObjectPropertyAtPut(JSRegExp::kLastIndexFieldIndex,
1037 Smi::FromInt(0),
1038 SKIP_WRITE_BARRIER); // It's a Smi.
1039 initial_map->set_prototype(*proto);
1040 factory->SetRegExpIrregexpData(Handle<JSRegExp>::cast(proto),
1041 JSRegExp::IRREGEXP, factory->empty_string(),
1042 JSRegExp::Flags(0), 0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001043 }
1044
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00001045 { // -- J S O N
machenbach@chromium.org8e36b5b2013-09-26 07:36:30 +00001046 Handle<String> name = factory->InternalizeUtf8String("JSON");
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001047 Handle<JSFunction> cons = factory->NewFunction(name,
1048 factory->the_hole_value());
jkummerow@chromium.orgba72ec82013-07-22 09:21:20 +00001049 JSFunction::SetInstancePrototype(cons,
1050 Handle<Object>(native_context()->initial_object_prototype(), isolate));
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00001051 cons->SetInstanceClassName(*name);
lrn@chromium.org7516f052011-03-30 08:52:27 +00001052 Handle<JSObject> json_object = factory->NewJSObject(cons, TENURED);
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00001053 ASSERT(json_object->IsJSObject());
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001054 CHECK_NOT_EMPTY_HANDLE(isolate,
1055 JSObject::SetLocalPropertyIgnoreAttributes(
1056 global, name, json_object, DONT_ENUM));
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001057 native_context()->set_json_object(*json_object);
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00001058 }
1059
verwaest@chromium.org32cb9b22013-08-21 11:18:12 +00001060 { // -- A r r a y B u f f e r
1061 Handle<JSFunction> array_buffer_fun =
1062 InstallFunction(
1063 global, "ArrayBuffer", JS_ARRAY_BUFFER_TYPE,
1064 JSArrayBuffer::kSizeWithInternalFields,
1065 isolate->initial_object_prototype(),
1066 Builtins::kIllegal, true, true);
1067 native_context()->set_array_buffer_fun(*array_buffer_fun);
1068 }
1069
1070 { // -- T y p e d A r r a y s
1071 Handle<JSFunction> int8_fun = InstallTypedArray("Int8Array",
1072 EXTERNAL_BYTE_ELEMENTS);
1073 native_context()->set_int8_array_fun(*int8_fun);
1074 Handle<JSFunction> uint8_fun = InstallTypedArray("Uint8Array",
1075 EXTERNAL_UNSIGNED_BYTE_ELEMENTS);
1076 native_context()->set_uint8_array_fun(*uint8_fun);
1077 Handle<JSFunction> int16_fun = InstallTypedArray("Int16Array",
1078 EXTERNAL_SHORT_ELEMENTS);
1079 native_context()->set_int16_array_fun(*int16_fun);
1080 Handle<JSFunction> uint16_fun = InstallTypedArray("Uint16Array",
1081 EXTERNAL_UNSIGNED_SHORT_ELEMENTS);
1082 native_context()->set_uint16_array_fun(*uint16_fun);
1083 Handle<JSFunction> int32_fun = InstallTypedArray("Int32Array",
1084 EXTERNAL_INT_ELEMENTS);
1085 native_context()->set_int32_array_fun(*int32_fun);
1086 Handle<JSFunction> uint32_fun = InstallTypedArray("Uint32Array",
1087 EXTERNAL_UNSIGNED_INT_ELEMENTS);
1088 native_context()->set_uint32_array_fun(*uint32_fun);
1089 Handle<JSFunction> float_fun = InstallTypedArray("Float32Array",
1090 EXTERNAL_FLOAT_ELEMENTS);
1091 native_context()->set_float_array_fun(*float_fun);
1092 Handle<JSFunction> double_fun = InstallTypedArray("Float64Array",
1093 EXTERNAL_DOUBLE_ELEMENTS);
1094 native_context()->set_double_array_fun(*double_fun);
1095 Handle<JSFunction> uint8c_fun = InstallTypedArray("Uint8ClampedArray",
1096 EXTERNAL_PIXEL_ELEMENTS);
1097 native_context()->set_uint8c_array_fun(*uint8c_fun);
1098
1099 Handle<JSFunction> data_view_fun =
1100 InstallFunction(
1101 global, "DataView", JS_DATA_VIEW_TYPE,
1102 JSDataView::kSizeWithInternalFields,
1103 isolate->initial_object_prototype(),
1104 Builtins::kIllegal, true, true);
1105 native_context()->set_data_view_fun(*data_view_fun);
1106 }
1107
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001108 { // --- arguments_boilerplate_
1109 // Make sure we can recognize argument objects at runtime.
1110 // This is done by introducing an anonymous function with
1111 // class_name equals 'Arguments'.
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001112 Handle<String> arguments_string = factory->InternalizeOneByteString(
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00001113 STATIC_ASCII_VECTOR("Arguments"));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001114 Handle<Code> code = Handle<Code>(
lrn@chromium.org7516f052011-03-30 08:52:27 +00001115 isolate->builtins()->builtin(Builtins::kIllegal));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001116 Handle<JSObject> prototype =
1117 Handle<JSObject>(
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001118 JSObject::cast(native_context()->object_function()->prototype()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001119
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001120 Handle<JSFunction> function =
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001121 factory->NewFunctionWithPrototype(arguments_string,
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001122 JS_OBJECT_TYPE,
1123 JSObject::kHeaderSize,
1124 prototype,
1125 code,
1126 false);
1127 ASSERT(!function->has_initial_map());
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001128 function->shared()->set_instance_class_name(*arguments_string);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001129 function->shared()->set_expected_nof_properties(2);
lrn@chromium.org7516f052011-03-30 08:52:27 +00001130 Handle<JSObject> result = factory->NewJSObject(function);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001131
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001132 native_context()->set_arguments_boilerplate(*result);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001133 // Note: length must be added as the first property and
1134 // callee must be added as the second property.
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001135 CHECK_NOT_EMPTY_HANDLE(isolate,
1136 JSObject::SetLocalPropertyIgnoreAttributes(
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001137 result, factory->length_string(),
danno@chromium.org1fd77d52013-06-07 16:01:45 +00001138 factory->undefined_value(), DONT_ENUM,
rossberg@chromium.org92597162013-08-23 13:28:00 +00001139 Object::FORCE_TAGGED, FORCE_FIELD));
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001140 CHECK_NOT_EMPTY_HANDLE(isolate,
1141 JSObject::SetLocalPropertyIgnoreAttributes(
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001142 result, factory->callee_string(),
danno@chromium.org1fd77d52013-06-07 16:01:45 +00001143 factory->undefined_value(), DONT_ENUM,
rossberg@chromium.org92597162013-08-23 13:28:00 +00001144 Object::FORCE_TAGGED, FORCE_FIELD));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001145
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001146#ifdef DEBUG
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001147 LookupResult lookup(isolate);
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001148 result->LocalLookup(heap->callee_string(), &lookup);
yangguo@chromium.orgde0db002012-06-22 13:44:28 +00001149 ASSERT(lookup.IsField());
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00001150 ASSERT(lookup.GetFieldIndex().field_index() == Heap::kArgumentsCalleeIndex);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001151
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001152 result->LocalLookup(heap->length_string(), &lookup);
yangguo@chromium.orgde0db002012-06-22 13:44:28 +00001153 ASSERT(lookup.IsField());
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00001154 ASSERT(lookup.GetFieldIndex().field_index() == Heap::kArgumentsLengthIndex);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001155
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001156 ASSERT(result->map()->inobject_properties() > Heap::kArgumentsCalleeIndex);
1157 ASSERT(result->map()->inobject_properties() > Heap::kArgumentsLengthIndex);
1158
1159 // Check the state of the object.
1160 ASSERT(result->HasFastProperties());
svenpanne@chromium.org830d30c2012-05-29 13:20:14 +00001161 ASSERT(result->HasFastObjectElements());
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001162#endif
1163 }
1164
whesse@chromium.org7b260152011-06-20 15:33:18 +00001165 { // --- aliased_arguments_boilerplate_
whesse@chromium.org7b260152011-06-20 15:33:18 +00001166 // Set up a well-formed parameter map to make assertions happy.
1167 Handle<FixedArray> elements = factory->NewFixedArray(2);
1168 elements->set_map(heap->non_strict_arguments_elements_map());
1169 Handle<FixedArray> array;
1170 array = factory->NewFixedArray(0);
1171 elements->set(0, *array);
1172 array = factory->NewFixedArray(0);
1173 elements->set(1, *array);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001174
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001175 Handle<Map> old_map(native_context()->arguments_boilerplate()->map());
verwaest@chromium.org753aee42012-07-17 16:15:42 +00001176 Handle<Map> new_map = factory->CopyMap(old_map);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001177 new_map->set_pre_allocated_property_fields(2);
1178 Handle<JSObject> result = factory->NewJSObjectFromMap(new_map);
1179 // Set elements kind after allocating the object because
1180 // NewJSObjectFromMap assumes a fast elements map.
1181 new_map->set_elements_kind(NON_STRICT_ARGUMENTS_ELEMENTS);
whesse@chromium.org7b260152011-06-20 15:33:18 +00001182 result->set_elements(*elements);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001183 ASSERT(result->HasNonStrictArgumentsElements());
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001184 native_context()->set_aliased_arguments_boilerplate(*result);
whesse@chromium.org7b260152011-06-20 15:33:18 +00001185 }
1186
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001187 { // --- strict mode arguments boilerplate
1188 const PropertyAttributes attributes =
1189 static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE | READ_ONLY);
1190
1191 // Create the ThrowTypeError functions.
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001192 Handle<AccessorPair> callee = factory->NewAccessorPair();
1193 Handle<AccessorPair> caller = factory->NewAccessorPair();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001194
danno@chromium.org40cb8782011-05-25 07:58:50 +00001195 Handle<JSFunction> throw_function =
1196 GetThrowTypeErrorFunction();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001197
1198 // Install the ThrowTypeError functions.
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001199 callee->set_getter(*throw_function);
1200 callee->set_setter(*throw_function);
1201 caller->set_getter(*throw_function);
1202 caller->set_setter(*throw_function);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001203
yangguo@chromium.org304cc332012-07-24 07:59:48 +00001204 // Create the map. Allocate one in-object field for length.
1205 Handle<Map> map = factory->NewMap(JS_OBJECT_TYPE,
1206 Heap::kArgumentsObjectSizeStrict);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001207 // Create the descriptor array for the arguments object.
verwaest@chromium.org33e09c82012-10-10 17:07:22 +00001208 Handle<DescriptorArray> descriptors = factory->NewDescriptorArray(0, 3);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001209 DescriptorArray::WhitenessWitness witness(*descriptors);
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00001210 map->set_instance_descriptors(*descriptors);
yangguo@chromium.org304cc332012-07-24 07:59:48 +00001211
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001212 { // length
danno@chromium.orgf005df62013-04-30 16:36:45 +00001213 FieldDescriptor d(
1214 *factory->length_string(), 0, DONT_ENUM, Representation::Tagged());
yangguo@chromium.org304cc332012-07-24 07:59:48 +00001215 map->AppendDescriptor(&d, witness);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001216 }
1217 { // callee
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001218 CallbacksDescriptor d(*factory->callee_string(),
rossberg@chromium.org657d53b2012-07-12 11:06:03 +00001219 *callee,
jkummerow@chromium.org28583c92012-07-16 11:31:55 +00001220 attributes);
yangguo@chromium.org304cc332012-07-24 07:59:48 +00001221 map->AppendDescriptor(&d, witness);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001222 }
1223 { // caller
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001224 CallbacksDescriptor d(*factory->caller_string(),
rossberg@chromium.org657d53b2012-07-12 11:06:03 +00001225 *caller,
jkummerow@chromium.org28583c92012-07-16 11:31:55 +00001226 attributes);
yangguo@chromium.org304cc332012-07-24 07:59:48 +00001227 map->AppendDescriptor(&d, witness);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001228 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001229
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001230 map->set_function_with_prototype(true);
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001231 map->set_prototype(native_context()->object_function()->prototype());
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001232 map->set_pre_allocated_property_fields(1);
1233 map->set_inobject_properties(1);
1234
1235 // Copy constructor from the non-strict arguments boilerplate.
1236 map->set_constructor(
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001237 native_context()->arguments_boilerplate()->map()->constructor());
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001238
1239 // Allocate the arguments boilerplate object.
lrn@chromium.org7516f052011-03-30 08:52:27 +00001240 Handle<JSObject> result = factory->NewJSObjectFromMap(map);
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001241 native_context()->set_strict_mode_arguments_boilerplate(*result);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001242
1243 // Add length property only for strict mode boilerplate.
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001244 CHECK_NOT_EMPTY_HANDLE(isolate,
1245 JSObject::SetLocalPropertyIgnoreAttributes(
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001246 result, factory->length_string(),
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001247 factory->undefined_value(), DONT_ENUM));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001248
1249#ifdef DEBUG
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001250 LookupResult lookup(isolate);
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001251 result->LocalLookup(heap->length_string(), &lookup);
yangguo@chromium.orgde0db002012-06-22 13:44:28 +00001252 ASSERT(lookup.IsField());
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00001253 ASSERT(lookup.GetFieldIndex().field_index() == Heap::kArgumentsLengthIndex);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001254
1255 ASSERT(result->map()->inobject_properties() > Heap::kArgumentsLengthIndex);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001256
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001257 // Check the state of the object.
1258 ASSERT(result->HasFastProperties());
svenpanne@chromium.org830d30c2012-05-29 13:20:14 +00001259 ASSERT(result->HasFastObjectElements());
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001260#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001261 }
1262
1263 { // --- context extension
1264 // Create a function for the context extension objects.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001265 Handle<Code> code = Handle<Code>(
lrn@chromium.org7516f052011-03-30 08:52:27 +00001266 isolate->builtins()->builtin(Builtins::kIllegal));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001267 Handle<JSFunction> context_extension_fun =
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001268 factory->NewFunction(factory->empty_string(),
ager@chromium.org32912102009-01-16 10:38:43 +00001269 JS_CONTEXT_EXTENSION_OBJECT_TYPE,
1270 JSObject::kHeaderSize,
1271 code,
1272 true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001273
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001274 Handle<String> name = factory->InternalizeOneByteString(
1275 STATIC_ASCII_VECTOR("context_extension"));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001276 context_extension_fun->shared()->set_instance_class_name(*name);
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001277 native_context()->set_context_extension_function(*context_extension_fun);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001278 }
1279
sgjesse@chromium.org05521fc2009-05-21 07:37:44 +00001280
1281 {
yangguo@chromium.org80c42ed2011-08-31 09:03:56 +00001282 // Set up the call-as-function delegate.
sgjesse@chromium.org05521fc2009-05-21 07:37:44 +00001283 Handle<Code> code =
lrn@chromium.org7516f052011-03-30 08:52:27 +00001284 Handle<Code>(isolate->builtins()->builtin(
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00001285 Builtins::kHandleApiCallAsFunction));
sgjesse@chromium.org05521fc2009-05-21 07:37:44 +00001286 Handle<JSFunction> delegate =
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001287 factory->NewFunction(factory->empty_string(), JS_OBJECT_TYPE,
sgjesse@chromium.org05521fc2009-05-21 07:37:44 +00001288 JSObject::kHeaderSize, code, true);
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001289 native_context()->set_call_as_function_delegate(*delegate);
sgjesse@chromium.org05521fc2009-05-21 07:37:44 +00001290 delegate->shared()->DontAdaptArguments();
1291 }
1292
1293 {
yangguo@chromium.org80c42ed2011-08-31 09:03:56 +00001294 // Set up the call-as-constructor delegate.
sgjesse@chromium.org05521fc2009-05-21 07:37:44 +00001295 Handle<Code> code =
lrn@chromium.org7516f052011-03-30 08:52:27 +00001296 Handle<Code>(isolate->builtins()->builtin(
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00001297 Builtins::kHandleApiCallAsConstructor));
sgjesse@chromium.org05521fc2009-05-21 07:37:44 +00001298 Handle<JSFunction> delegate =
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001299 factory->NewFunction(factory->empty_string(), JS_OBJECT_TYPE,
sgjesse@chromium.org05521fc2009-05-21 07:37:44 +00001300 JSObject::kHeaderSize, code, true);
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001301 native_context()->set_call_as_constructor_delegate(*delegate);
sgjesse@chromium.org05521fc2009-05-21 07:37:44 +00001302 delegate->shared()->DontAdaptArguments();
1303 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001304
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001305 // Initialize the out of memory slot.
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001306 native_context()->set_out_of_memory(heap->false_value());
ager@chromium.org9085a012009-05-11 19:22:57 +00001307
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00001308 // Initialize the embedder data slot.
1309 Handle<FixedArray> embedder_data = factory->NewFixedArray(2);
1310 native_context()->set_embedder_data(*embedder_data);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001311
hpayer@chromium.orgc5d49712013-09-11 08:25:48 +00001312 // Allocate the random seed slot.
1313 Handle<ByteArray> random_seed = factory->NewByteArray(kRandomStateSize);
1314 native_context()->set_random_seed(*random_seed);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001315}
1316
1317
jkummerow@chromium.org4e308cf2013-05-17 13:39:16 +00001318Handle<JSFunction> Genesis::InstallTypedArray(
1319 const char* name, ElementsKind elementsKind) {
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00001320 Handle<JSObject> global = Handle<JSObject>(native_context()->global_object());
jkummerow@chromium.org4e308cf2013-05-17 13:39:16 +00001321 Handle<JSFunction> result = InstallFunction(global, name, JS_TYPED_ARRAY_TYPE,
danno@chromium.orgf005df62013-04-30 16:36:45 +00001322 JSTypedArray::kSize, isolate()->initial_object_prototype(),
jkummerow@chromium.org4e308cf2013-05-17 13:39:16 +00001323 Builtins::kIllegal, false, true);
1324
1325 Handle<Map> initial_map = isolate()->factory()->NewMap(
mstarzinger@chromium.orge0e1b0d2013-07-08 08:38:06 +00001326 JS_TYPED_ARRAY_TYPE, JSTypedArray::kSizeWithInternalFields, elementsKind);
jkummerow@chromium.org4e308cf2013-05-17 13:39:16 +00001327 result->set_initial_map(*initial_map);
1328 initial_map->set_constructor(*result);
1329 return result;
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00001330}
1331
1332
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +00001333void Genesis::InitializeExperimentalGlobal() {
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001334 Handle<JSObject> global = Handle<JSObject>(native_context()->global_object());
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +00001335
1336 // TODO(mstarzinger): Move this into Genesis::InitializeGlobal once we no
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +00001337 // longer need to live behind flags, so functions get added to the snapshot.
1338
1339 if (FLAG_harmony_symbols) {
1340 // --- S y m b o l ---
1341 Handle<JSFunction> symbol_fun =
1342 InstallFunction(global, "Symbol", JS_VALUE_TYPE, JSValue::kSize,
1343 isolate()->initial_object_prototype(),
jkummerow@chromium.org4e308cf2013-05-17 13:39:16 +00001344 Builtins::kIllegal, true, true);
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +00001345 native_context()->set_symbol_function(*symbol_fun);
1346 }
1347
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001348 if (FLAG_harmony_collections) {
1349 { // -- S e t
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001350 InstallFunction(global, "Set", JS_SET_TYPE, JSSet::kSize,
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00001351 isolate()->initial_object_prototype(),
jkummerow@chromium.org4e308cf2013-05-17 13:39:16 +00001352 Builtins::kIllegal, true, true);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001353 }
1354 { // -- M a p
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001355 InstallFunction(global, "Map", JS_MAP_TYPE, JSMap::kSize,
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00001356 isolate()->initial_object_prototype(),
jkummerow@chromium.org4e308cf2013-05-17 13:39:16 +00001357 Builtins::kIllegal, true, true);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001358 }
1359 { // -- W e a k M a p
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001360 InstallFunction(global, "WeakMap", JS_WEAK_MAP_TYPE, JSWeakMap::kSize,
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00001361 isolate()->initial_object_prototype(),
jkummerow@chromium.org4e308cf2013-05-17 13:39:16 +00001362 Builtins::kIllegal, true, true);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001363 }
jkummerow@chromium.orgba72ec82013-07-22 09:21:20 +00001364 { // -- W e a k S e t
1365 InstallFunction(global, "WeakSet", JS_WEAK_SET_TYPE, JSWeakSet::kSize,
1366 isolate()->initial_object_prototype(),
1367 Builtins::kIllegal, true, true);
1368 }
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +00001369 }
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +00001370
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00001371 if (FLAG_harmony_generators) {
1372 // Create generator meta-objects and install them on the builtins object.
1373 Handle<JSObject> builtins(native_context()->builtins());
1374 Handle<JSObject> generator_object_prototype =
1375 factory()->NewJSObject(isolate()->object_function(), TENURED);
1376 Handle<JSFunction> generator_function_prototype =
1377 InstallFunction(builtins, "GeneratorFunctionPrototype",
1378 JS_FUNCTION_TYPE, JSFunction::kHeaderSize,
1379 generator_object_prototype, Builtins::kIllegal,
jkummerow@chromium.org4e308cf2013-05-17 13:39:16 +00001380 false, false);
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00001381 InstallFunction(builtins, "GeneratorFunction",
1382 JS_FUNCTION_TYPE, JSFunction::kSize,
1383 generator_function_prototype, Builtins::kIllegal,
jkummerow@chromium.org4e308cf2013-05-17 13:39:16 +00001384 false, false);
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00001385
1386 // Create maps for generator functions and their prototypes. Store those
1387 // maps in the native context.
1388 Handle<Map> function_map(native_context()->function_map());
1389 Handle<Map> generator_function_map = factory()->CopyMap(function_map);
1390 generator_function_map->set_prototype(*generator_function_prototype);
1391 native_context()->set_generator_function_map(*generator_function_map);
1392
1393 Handle<Map> strict_mode_function_map(
1394 native_context()->strict_mode_function_map());
1395 Handle<Map> strict_mode_generator_function_map = factory()->CopyMap(
1396 strict_mode_function_map);
1397 strict_mode_generator_function_map->set_prototype(
1398 *generator_function_prototype);
1399 native_context()->set_strict_mode_generator_function_map(
1400 *strict_mode_generator_function_map);
1401
1402 Handle<Map> object_map(native_context()->object_function()->initial_map());
1403 Handle<Map> generator_object_prototype_map = factory()->CopyMap(
1404 object_map, 0);
1405 generator_object_prototype_map->set_prototype(
1406 *generator_object_prototype);
1407 native_context()->set_generator_object_prototype_map(
1408 *generator_object_prototype_map);
ulan@chromium.org57ff8812013-05-10 08:16:55 +00001409
1410 // Create a map for generator result objects.
1411 ASSERT(object_map->inobject_properties() == 0);
1412 STATIC_ASSERT(JSGeneratorObject::kResultPropertyCount == 2);
1413 Handle<Map> generator_result_map = factory()->CopyMap(object_map,
1414 JSGeneratorObject::kResultPropertyCount);
1415 ASSERT(generator_result_map->inobject_properties() ==
1416 JSGeneratorObject::kResultPropertyCount);
1417
1418 Handle<DescriptorArray> descriptors = factory()->NewDescriptorArray(0,
1419 JSGeneratorObject::kResultPropertyCount);
1420 DescriptorArray::WhitenessWitness witness(*descriptors);
1421 generator_result_map->set_instance_descriptors(*descriptors);
1422
1423 Handle<String> value_string = factory()->InternalizeOneByteString(
1424 STATIC_ASCII_VECTOR("value"));
1425 FieldDescriptor value_descr(*value_string,
1426 JSGeneratorObject::kResultValuePropertyIndex,
1427 NONE,
1428 Representation::Tagged());
1429 generator_result_map->AppendDescriptor(&value_descr, witness);
1430
1431 Handle<String> done_string = factory()->InternalizeOneByteString(
1432 STATIC_ASCII_VECTOR("done"));
1433 FieldDescriptor done_descr(*done_string,
1434 JSGeneratorObject::kResultDonePropertyIndex,
1435 NONE,
1436 Representation::Tagged());
1437 generator_result_map->AppendDescriptor(&done_descr, witness);
1438
1439 generator_result_map->set_unused_property_fields(0);
1440 ASSERT_EQ(JSGeneratorObject::kResultSize,
1441 generator_result_map->instance_size());
1442 native_context()->set_generator_result_map(*generator_result_map);
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +00001443 }
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +00001444}
1445
1446
danno@chromium.org160a7b02011-04-18 15:51:38 +00001447bool Genesis::CompileBuiltin(Isolate* isolate, int index) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001448 Vector<const char> name = Natives::GetScriptName(index);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001449 Handle<String> source_code =
danno@chromium.org160a7b02011-04-18 15:51:38 +00001450 isolate->bootstrapper()->NativesSourceLookup(index);
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00001451 return CompileNative(isolate, name, source_code);
danno@chromium.org160a7b02011-04-18 15:51:38 +00001452}
1453
1454
1455bool Genesis::CompileExperimentalBuiltin(Isolate* isolate, int index) {
1456 Vector<const char> name = ExperimentalNatives::GetScriptName(index);
1457 Factory* factory = isolate->factory();
1458 Handle<String> source_code =
jkummerow@chromium.orge297f592011-06-08 10:05:15 +00001459 factory->NewStringFromAscii(
1460 ExperimentalNatives::GetRawScriptSource(index));
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00001461 return CompileNative(isolate, name, source_code);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001462}
1463
1464
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00001465bool Genesis::CompileNative(Isolate* isolate,
1466 Vector<const char> name,
1467 Handle<String> source) {
1468 HandleScope scope(isolate);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001469#ifdef ENABLE_DEBUGGER_SUPPORT
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001470 isolate->debugger()->set_compiling_natives(true);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001471#endif
fschneider@chromium.org7d10be52012-04-10 12:30:14 +00001472 // During genesis, the boilerplate for stack overflow won't work until the
1473 // environment has been at least partially initialized. Add a stack check
1474 // before entering JS code to catch overflow early.
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00001475 StackLimitCheck check(isolate);
fschneider@chromium.org7d10be52012-04-10 12:30:14 +00001476 if (check.HasOverflowed()) return false;
1477
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00001478 bool result = CompileScriptCached(isolate,
1479 name,
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001480 source,
1481 NULL,
1482 NULL,
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001483 Handle<Context>(isolate->context()),
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001484 true);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001485 ASSERT(isolate->has_pending_exception() != result);
1486 if (!result) isolate->clear_pending_exception();
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001487#ifdef ENABLE_DEBUGGER_SUPPORT
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001488 isolate->debugger()->set_compiling_natives(false);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001489#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001490 return result;
1491}
1492
1493
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00001494bool Genesis::CompileScriptCached(Isolate* isolate,
1495 Vector<const char> name,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001496 Handle<String> source,
1497 SourceCodeCache* cache,
1498 v8::Extension* extension,
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001499 Handle<Context> top_context,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001500 bool use_runtime_context) {
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00001501 Factory* factory = isolate->factory();
1502 HandleScope scope(isolate);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001503 Handle<SharedFunctionInfo> function_info;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001504
1505 // If we can't find the function in the cache, we compile a new
1506 // function and insert it into the cache.
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001507 if (cache == NULL || !cache->Lookup(name, &function_info)) {
ulan@chromium.org8e8d8822012-11-23 14:36:46 +00001508 ASSERT(source->IsOneByteRepresentation());
lrn@chromium.org7516f052011-03-30 08:52:27 +00001509 Handle<String> script_name = factory->NewStringFromUtf8(name);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001510 function_info = Compiler::Compile(
1511 source,
1512 script_name,
1513 0,
1514 0,
danno@chromium.orgd3c42102013-08-01 16:58:23 +00001515 false,
yangguo@chromium.org355cfd12012-08-29 15:32:24 +00001516 top_context,
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001517 extension,
1518 NULL,
1519 Handle<String>::null(),
1520 use_runtime_context ? NATIVES_CODE : NOT_NATIVES_CODE);
1521 if (function_info.is_null()) return false;
1522 if (cache != NULL) cache->Add(name, function_info);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001523 }
1524
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001525 // Set up the function context. Conceptually, we should clone the
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001526 // function before overwriting the context but since we're in a
1527 // single-threaded environment it is not strictly necessary.
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001528 ASSERT(top_context->IsNativeContext());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001529 Handle<Context> context =
1530 Handle<Context>(use_runtime_context
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001531 ? Handle<Context>(top_context->runtime_context())
1532 : top_context);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001533 Handle<JSFunction> fun =
lrn@chromium.org7516f052011-03-30 08:52:27 +00001534 factory->NewFunctionFromSharedFunctionInfo(function_info, context);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001535
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001536 // Call function using either the runtime object or the global
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001537 // object as the receiver. Provide no parameters.
1538 Handle<Object> receiver =
1539 Handle<Object>(use_runtime_context
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001540 ? top_context->builtins()
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00001541 : top_context->global_object(),
1542 isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001543 bool has_pending_exception;
jkummerow@chromium.org2c9426b2013-09-05 16:31:13 +00001544 Execution::Call(isolate, fun, receiver, 0, NULL, &has_pending_exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001545 if (has_pending_exception) return false;
ager@chromium.org5c838252010-02-19 08:53:10 +00001546 return true;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001547}
1548
1549
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00001550#define INSTALL_NATIVE(Type, name, var) \
1551 Handle<String> var##_name = \
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001552 factory()->InternalizeOneByteString(STATIC_ASCII_VECTOR(name)); \
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00001553 Object* var##_native = \
1554 native_context()->builtins()->GetPropertyNoExceptionThrown( \
1555 *var##_name); \
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001556 native_context()->set_##var(Type::cast(var##_native));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001557
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001558
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001559void Genesis::InstallNativeFunctions() {
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00001560 HandleScope scope(isolate());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001561 INSTALL_NATIVE(JSFunction, "CreateDate", create_date_fun);
1562 INSTALL_NATIVE(JSFunction, "ToNumber", to_number_fun);
1563 INSTALL_NATIVE(JSFunction, "ToString", to_string_fun);
1564 INSTALL_NATIVE(JSFunction, "ToDetailString", to_detail_string_fun);
1565 INSTALL_NATIVE(JSFunction, "ToObject", to_object_fun);
1566 INSTALL_NATIVE(JSFunction, "ToInteger", to_integer_fun);
1567 INSTALL_NATIVE(JSFunction, "ToUint32", to_uint32_fun);
1568 INSTALL_NATIVE(JSFunction, "ToInt32", to_int32_fun);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00001569 INSTALL_NATIVE(JSFunction, "GlobalEval", global_eval_fun);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001570 INSTALL_NATIVE(JSFunction, "Instantiate", instantiate_fun);
1571 INSTALL_NATIVE(JSFunction, "ConfigureTemplateInstance",
1572 configure_instance_fun);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001573 INSTALL_NATIVE(JSFunction, "GetStackTraceLine", get_stack_trace_line_fun);
1574 INSTALL_NATIVE(JSObject, "functionCache", function_cache);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001575 INSTALL_NATIVE(JSFunction, "ToCompletePropertyDescriptor",
1576 to_complete_property_descriptor);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001577}
1578
mstarzinger@chromium.orge0e1b0d2013-07-08 08:38:06 +00001579
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001580void Genesis::InstallExperimentalNativeFunctions() {
1581 if (FLAG_harmony_proxies) {
rossberg@chromium.org717967f2011-07-20 13:44:42 +00001582 INSTALL_NATIVE(JSFunction, "DerivedHasTrap", derived_has_trap);
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001583 INSTALL_NATIVE(JSFunction, "DerivedGetTrap", derived_get_trap);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00001584 INSTALL_NATIVE(JSFunction, "DerivedSetTrap", derived_set_trap);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001585 INSTALL_NATIVE(JSFunction, "ProxyEnumerate", proxy_enumerate);
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001586 }
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +00001587 if (FLAG_harmony_observation) {
1588 INSTALL_NATIVE(JSFunction, "NotifyChange", observers_notify_change);
verwaest@chromium.orgd4be0f02013-06-05 13:39:03 +00001589 INSTALL_NATIVE(JSFunction, "EnqueueSpliceRecord", observers_enqueue_splice);
1590 INSTALL_NATIVE(JSFunction, "BeginPerformSplice",
1591 observers_begin_perform_splice);
1592 INSTALL_NATIVE(JSFunction, "EndPerformSplice",
1593 observers_end_perform_splice);
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +00001594 INSTALL_NATIVE(JSFunction, "DeliverChangeRecords",
1595 observers_deliver_changes);
1596 }
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001597}
1598
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001599#undef INSTALL_NATIVE
1600
1601
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00001602Handle<JSFunction> Genesis::InstallInternalArray(
1603 Handle<JSBuiltinsObject> builtins,
1604 const char* name,
1605 ElementsKind elements_kind) {
1606 // --- I n t e r n a l A r r a y ---
1607 // An array constructor on the builtins object that works like
1608 // the public Array constructor, except that its prototype
1609 // doesn't inherit from Object.prototype.
1610 // To be used only for internal work by builtins. Instances
1611 // must not be leaked to user code.
1612 Handle<JSFunction> array_function =
1613 InstallFunction(builtins,
1614 name,
1615 JS_ARRAY_TYPE,
1616 JSArray::kSize,
1617 isolate()->initial_object_prototype(),
1618 Builtins::kInternalArrayCode,
jkummerow@chromium.org4e308cf2013-05-17 13:39:16 +00001619 true, true);
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00001620 Handle<JSObject> prototype =
1621 factory()->NewJSObject(isolate()->object_function(), TENURED);
rossberg@chromium.orgebeba022013-08-19 09:36:44 +00001622 Accessors::FunctionSetPrototype(array_function, prototype);
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00001623
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +00001624 InternalArrayConstructorStub internal_array_constructor_stub(isolate());
1625 Handle<Code> code = internal_array_constructor_stub.GetCode(isolate());
1626 array_function->shared()->set_construct_stub(*code);
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00001627 array_function->shared()->DontAdaptArguments();
1628
rossberg@chromium.orgb99c7542013-05-31 11:40:45 +00001629 Handle<Map> original_map(array_function->initial_map());
1630 Handle<Map> initial_map = factory()->CopyMap(original_map);
1631 initial_map->set_elements_kind(elements_kind);
1632 array_function->set_initial_map(*initial_map);
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00001633
1634 // Make "length" magic on instances.
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00001635 Handle<DescriptorArray> array_descriptors(
1636 factory()->NewDescriptorArray(0, 1));
1637 DescriptorArray::WhitenessWitness witness(*array_descriptors);
1638
1639 Handle<Foreign> array_length(factory()->NewForeign(
1640 &Accessors::ArrayLength));
1641 PropertyAttributes attribs = static_cast<PropertyAttributes>(
1642 DONT_ENUM | DONT_DELETE);
1643 initial_map->set_instance_descriptors(*array_descriptors);
1644
1645 { // Add length.
1646 CallbacksDescriptor d(
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001647 *factory()->length_string(), *array_length, attribs);
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00001648 array_function->initial_map()->AppendDescriptor(&d, witness);
1649 }
1650
1651 return array_function;
1652}
1653
1654
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001655bool Genesis::InstallNatives() {
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00001656 HandleScope scope(isolate());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001657
1658 // Create a function for the builtins object. Allocate space for the
1659 // JavaScript builtins, a reference to the builtins object
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001660 // (itself) and a reference to the native_context directly in the object.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001661 Handle<Code> code = Handle<Code>(
danno@chromium.org160a7b02011-04-18 15:51:38 +00001662 isolate()->builtins()->builtin(Builtins::kIllegal));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001663 Handle<JSFunction> builtins_fun =
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001664 factory()->NewFunction(factory()->empty_string(),
danno@chromium.org160a7b02011-04-18 15:51:38 +00001665 JS_BUILTINS_OBJECT_TYPE,
1666 JSBuiltinsObject::kSize, code, true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001667
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00001668 Handle<String> name =
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001669 factory()->InternalizeOneByteString(STATIC_ASCII_VECTOR("builtins"));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001670 builtins_fun->shared()->set_instance_class_name(*name);
erik.corry@gmail.com88767242012-08-08 14:43:45 +00001671 builtins_fun->initial_map()->set_dictionary_map(true);
verwaest@chromium.orgde64f722012-08-16 15:44:54 +00001672 builtins_fun->initial_map()->set_prototype(heap()->null_value());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001673
1674 // Allocate the builtins object.
1675 Handle<JSBuiltinsObject> builtins =
danno@chromium.org160a7b02011-04-18 15:51:38 +00001676 Handle<JSBuiltinsObject>::cast(factory()->NewGlobalObject(builtins_fun));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001677 builtins->set_builtins(*builtins);
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001678 builtins->set_native_context(*native_context());
yangguo@chromium.org355cfd12012-08-29 15:32:24 +00001679 builtins->set_global_context(*native_context());
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001680 builtins->set_global_receiver(*builtins);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001681
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001682 // Set up the 'global' properties of the builtins object. The
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001683 // 'global' property that refers to the global object is the only
1684 // way to get from code running in the builtins context to the
1685 // global object.
1686 static const PropertyAttributes attributes =
1687 static_cast<PropertyAttributes>(READ_ONLY | DONT_DELETE);
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001688 Handle<String> global_string =
1689 factory()->InternalizeOneByteString(STATIC_ASCII_VECTOR("global"));
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00001690 Handle<Object> global_obj(native_context()->global_object(), isolate());
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001691 CHECK_NOT_EMPTY_HANDLE(isolate(),
1692 JSObject::SetLocalPropertyIgnoreAttributes(
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001693 builtins, global_string, global_obj, attributes));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001694
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001695 // Set up the reference from the global object to the builtins object.
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001696 JSGlobalObject::cast(native_context()->global_object())->
1697 set_builtins(*builtins);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001698
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001699 // Create a bridge function that has context in the native context.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001700 Handle<JSFunction> bridge =
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001701 factory()->NewFunction(factory()->empty_string(),
danno@chromium.org160a7b02011-04-18 15:51:38 +00001702 factory()->undefined_value());
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001703 ASSERT(bridge->context() == *isolate()->native_context());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001704
1705 // Allocate the builtins context.
1706 Handle<Context> context =
danno@chromium.org160a7b02011-04-18 15:51:38 +00001707 factory()->NewFunctionContext(Context::MIN_CONTEXT_SLOTS, bridge);
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001708 context->set_global_object(*builtins); // override builtins global object
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001709
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001710 native_context()->set_runtime_context(*context);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001711
1712 { // -- S c r i p t
1713 // Builtin functions for Script.
1714 Handle<JSFunction> script_fun =
1715 InstallFunction(builtins, "Script", JS_VALUE_TYPE, JSValue::kSize,
danno@chromium.org160a7b02011-04-18 15:51:38 +00001716 isolate()->initial_object_prototype(),
jkummerow@chromium.org4e308cf2013-05-17 13:39:16 +00001717 Builtins::kIllegal, false, false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001718 Handle<JSObject> prototype =
danno@chromium.org160a7b02011-04-18 15:51:38 +00001719 factory()->NewJSObject(isolate()->object_function(), TENURED);
rossberg@chromium.orgebeba022013-08-19 09:36:44 +00001720 Accessors::FunctionSetPrototype(script_fun, prototype);
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001721 native_context()->set_script_function(*script_fun);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001722
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001723 Handle<Map> script_map = Handle<Map>(script_fun->initial_map());
yangguo@chromium.org304cc332012-07-24 07:59:48 +00001724
1725 Handle<DescriptorArray> script_descriptors(
verwaest@chromium.org33e09c82012-10-10 17:07:22 +00001726 factory()->NewDescriptorArray(0, 13));
yangguo@chromium.org304cc332012-07-24 07:59:48 +00001727 DescriptorArray::WhitenessWitness witness(*script_descriptors);
1728
1729 Handle<Foreign> script_source(
1730 factory()->NewForeign(&Accessors::ScriptSource));
1731 Handle<Foreign> script_name(factory()->NewForeign(&Accessors::ScriptName));
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001732 Handle<String> id_string(factory()->InternalizeOneByteString(
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00001733 STATIC_ASCII_VECTOR("id")));
yangguo@chromium.org304cc332012-07-24 07:59:48 +00001734 Handle<Foreign> script_id(factory()->NewForeign(&Accessors::ScriptId));
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001735 Handle<String> line_offset_string(
1736 factory()->InternalizeOneByteString(
1737 STATIC_ASCII_VECTOR("line_offset")));
yangguo@chromium.org304cc332012-07-24 07:59:48 +00001738 Handle<Foreign> script_line_offset(
1739 factory()->NewForeign(&Accessors::ScriptLineOffset));
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001740 Handle<String> column_offset_string(
1741 factory()->InternalizeOneByteString(
1742 STATIC_ASCII_VECTOR("column_offset")));
yangguo@chromium.org304cc332012-07-24 07:59:48 +00001743 Handle<Foreign> script_column_offset(
1744 factory()->NewForeign(&Accessors::ScriptColumnOffset));
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001745 Handle<String> data_string(factory()->InternalizeOneByteString(
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00001746 STATIC_ASCII_VECTOR("data")));
yangguo@chromium.org304cc332012-07-24 07:59:48 +00001747 Handle<Foreign> script_data(factory()->NewForeign(&Accessors::ScriptData));
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001748 Handle<String> type_string(factory()->InternalizeOneByteString(
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00001749 STATIC_ASCII_VECTOR("type")));
yangguo@chromium.org304cc332012-07-24 07:59:48 +00001750 Handle<Foreign> script_type(factory()->NewForeign(&Accessors::ScriptType));
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001751 Handle<String> compilation_type_string(
1752 factory()->InternalizeOneByteString(
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00001753 STATIC_ASCII_VECTOR("compilation_type")));
yangguo@chromium.org304cc332012-07-24 07:59:48 +00001754 Handle<Foreign> script_compilation_type(
1755 factory()->NewForeign(&Accessors::ScriptCompilationType));
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001756 Handle<String> line_ends_string(factory()->InternalizeOneByteString(
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00001757 STATIC_ASCII_VECTOR("line_ends")));
yangguo@chromium.org304cc332012-07-24 07:59:48 +00001758 Handle<Foreign> script_line_ends(
1759 factory()->NewForeign(&Accessors::ScriptLineEnds));
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001760 Handle<String> context_data_string(
1761 factory()->InternalizeOneByteString(
1762 STATIC_ASCII_VECTOR("context_data")));
yangguo@chromium.org304cc332012-07-24 07:59:48 +00001763 Handle<Foreign> script_context_data(
1764 factory()->NewForeign(&Accessors::ScriptContextData));
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001765 Handle<String> eval_from_script_string(
1766 factory()->InternalizeOneByteString(
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00001767 STATIC_ASCII_VECTOR("eval_from_script")));
yangguo@chromium.org304cc332012-07-24 07:59:48 +00001768 Handle<Foreign> script_eval_from_script(
1769 factory()->NewForeign(&Accessors::ScriptEvalFromScript));
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001770 Handle<String> eval_from_script_position_string(
1771 factory()->InternalizeOneByteString(
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00001772 STATIC_ASCII_VECTOR("eval_from_script_position")));
yangguo@chromium.org304cc332012-07-24 07:59:48 +00001773 Handle<Foreign> script_eval_from_script_position(
1774 factory()->NewForeign(&Accessors::ScriptEvalFromScriptPosition));
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001775 Handle<String> eval_from_function_name_string(
1776 factory()->InternalizeOneByteString(
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00001777 STATIC_ASCII_VECTOR("eval_from_function_name")));
yangguo@chromium.org304cc332012-07-24 07:59:48 +00001778 Handle<Foreign> script_eval_from_function_name(
1779 factory()->NewForeign(&Accessors::ScriptEvalFromFunctionName));
1780 PropertyAttributes attribs =
1781 static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE | READ_ONLY);
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00001782 script_map->set_instance_descriptors(*script_descriptors);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001783
yangguo@chromium.org304cc332012-07-24 07:59:48 +00001784 {
1785 CallbacksDescriptor d(
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001786 *factory()->source_string(), *script_source, attribs);
yangguo@chromium.org304cc332012-07-24 07:59:48 +00001787 script_map->AppendDescriptor(&d, witness);
1788 }
1789
1790 {
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001791 CallbacksDescriptor d(*factory()->name_string(), *script_name, attribs);
yangguo@chromium.org304cc332012-07-24 07:59:48 +00001792 script_map->AppendDescriptor(&d, witness);
1793 }
1794
1795 {
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001796 CallbacksDescriptor d(*id_string, *script_id, attribs);
yangguo@chromium.org304cc332012-07-24 07:59:48 +00001797 script_map->AppendDescriptor(&d, witness);
1798 }
1799
1800 {
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001801 CallbacksDescriptor d(*line_offset_string, *script_line_offset, attribs);
yangguo@chromium.org304cc332012-07-24 07:59:48 +00001802 script_map->AppendDescriptor(&d, witness);
1803 }
1804
1805 {
1806 CallbacksDescriptor d(
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001807 *column_offset_string, *script_column_offset, attribs);
yangguo@chromium.org304cc332012-07-24 07:59:48 +00001808 script_map->AppendDescriptor(&d, witness);
1809 }
1810
1811 {
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001812 CallbacksDescriptor d(*data_string, *script_data, attribs);
yangguo@chromium.org304cc332012-07-24 07:59:48 +00001813 script_map->AppendDescriptor(&d, witness);
1814 }
1815
1816 {
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001817 CallbacksDescriptor d(*type_string, *script_type, attribs);
yangguo@chromium.org304cc332012-07-24 07:59:48 +00001818 script_map->AppendDescriptor(&d, witness);
1819 }
1820
1821 {
1822 CallbacksDescriptor d(
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001823 *compilation_type_string, *script_compilation_type, attribs);
yangguo@chromium.org304cc332012-07-24 07:59:48 +00001824 script_map->AppendDescriptor(&d, witness);
1825 }
1826
1827 {
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001828 CallbacksDescriptor d(*line_ends_string, *script_line_ends, attribs);
yangguo@chromium.org304cc332012-07-24 07:59:48 +00001829 script_map->AppendDescriptor(&d, witness);
1830 }
1831
1832 {
1833 CallbacksDescriptor d(
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001834 *context_data_string, *script_context_data, attribs);
yangguo@chromium.org304cc332012-07-24 07:59:48 +00001835 script_map->AppendDescriptor(&d, witness);
1836 }
1837
1838 {
1839 CallbacksDescriptor d(
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001840 *eval_from_script_string, *script_eval_from_script, attribs);
yangguo@chromium.org304cc332012-07-24 07:59:48 +00001841 script_map->AppendDescriptor(&d, witness);
1842 }
1843
1844 {
1845 CallbacksDescriptor d(
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001846 *eval_from_script_position_string,
yangguo@chromium.org304cc332012-07-24 07:59:48 +00001847 *script_eval_from_script_position,
1848 attribs);
1849 script_map->AppendDescriptor(&d, witness);
1850 }
1851
1852 {
1853 CallbacksDescriptor d(
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001854 *eval_from_function_name_string,
yangguo@chromium.org304cc332012-07-24 07:59:48 +00001855 *script_eval_from_function_name,
1856 attribs);
1857 script_map->AppendDescriptor(&d, witness);
1858 }
1859
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001860 // Allocate the empty script.
danno@chromium.org160a7b02011-04-18 15:51:38 +00001861 Handle<Script> script = factory()->NewScript(factory()->empty_string());
ager@chromium.orge2902be2009-06-08 12:21:35 +00001862 script->set_type(Smi::FromInt(Script::TYPE_NATIVE));
danno@chromium.org160a7b02011-04-18 15:51:38 +00001863 heap()->public_set_empty_script(*script);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001864 }
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001865 {
1866 // Builtin function for OpaqueReference -- a JSValue-based object,
1867 // that keeps its field isolated from JavaScript code. It may store
1868 // objects, that JavaScript code may not access.
1869 Handle<JSFunction> opaque_reference_fun =
1870 InstallFunction(builtins, "OpaqueReference", JS_VALUE_TYPE,
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001871 JSValue::kSize,
danno@chromium.org160a7b02011-04-18 15:51:38 +00001872 isolate()->initial_object_prototype(),
jkummerow@chromium.org4e308cf2013-05-17 13:39:16 +00001873 Builtins::kIllegal, false, false);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001874 Handle<JSObject> prototype =
danno@chromium.org160a7b02011-04-18 15:51:38 +00001875 factory()->NewJSObject(isolate()->object_function(), TENURED);
rossberg@chromium.orgebeba022013-08-19 09:36:44 +00001876 Accessors::FunctionSetPrototype(opaque_reference_fun, prototype);
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001877 native_context()->set_opaque_reference_function(*opaque_reference_fun);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001878 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001879
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00001880 // InternalArrays should not use Smi-Only array optimizations. There are too
1881 // many places in the C++ runtime code (e.g. RegEx) that assume that
1882 // elements in InternalArrays can be set to non-Smi values without going
1883 // through a common bottleneck that would make the SMI_ONLY -> FAST_ELEMENT
1884 // transition easy to trap. Moreover, they rarely are smi-only.
1885 {
karlklose@chromium.org8f806e82011-03-07 14:06:08 +00001886 Handle<JSFunction> array_function =
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00001887 InstallInternalArray(builtins, "InternalArray", FAST_HOLEY_ELEMENTS);
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001888 native_context()->set_internal_array_function(*array_function);
karlklose@chromium.org8f806e82011-03-07 14:06:08 +00001889 }
1890
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00001891 {
rossberg@chromium.orgb99c7542013-05-31 11:40:45 +00001892 InstallInternalArray(builtins, "InternalPackedArray", FAST_ELEMENTS);
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00001893 }
1894
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001895 if (FLAG_disable_native_files) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001896 PrintF("Warning: Running without installed natives!\n");
1897 return true;
1898 }
1899
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001900 // Install natives.
1901 for (int i = Natives::GetDebuggerCount();
1902 i < Natives::GetBuiltinsCount();
1903 i++) {
danno@chromium.org160a7b02011-04-18 15:51:38 +00001904 if (!CompileBuiltin(isolate(), i)) return false;
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001905 // TODO(ager): We really only need to install the JS builtin
1906 // functions on the builtins object after compiling and running
1907 // runtime.js.
1908 if (!InstallJSBuiltins(builtins)) return false;
1909 }
1910
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001911 InstallNativeFunctions();
1912
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00001913 // Store the map for the string prototype after the natives has been compiled
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001914 // and the String function has been set up.
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001915 Handle<JSFunction> string_function(native_context()->string_function());
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00001916 ASSERT(JSObject::cast(
1917 string_function->initial_map()->prototype())->HasFastProperties());
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001918 native_context()->set_string_function_prototype_map(
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00001919 HeapObject::cast(string_function->initial_map()->prototype())->map());
1920
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001921 // Install Function.prototype.call and apply.
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001922 { Handle<String> key = factory()->function_class_string();
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001923 Handle<JSFunction> function =
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00001924 Handle<JSFunction>::cast(
1925 GetProperty(isolate(), isolate()->global_object(), key));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001926 Handle<JSObject> proto =
1927 Handle<JSObject>(JSObject::cast(function->instance_prototype()));
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001928
1929 // Install the call and the apply functions.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001930 Handle<JSFunction> call =
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001931 InstallFunction(proto, "call", JS_OBJECT_TYPE, JSObject::kHeaderSize,
kmillikin@chromium.org4111b802010-05-03 10:34:42 +00001932 Handle<JSObject>::null(),
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00001933 Builtins::kFunctionCall,
jkummerow@chromium.org4e308cf2013-05-17 13:39:16 +00001934 false, false);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001935 Handle<JSFunction> apply =
1936 InstallFunction(proto, "apply", JS_OBJECT_TYPE, JSObject::kHeaderSize,
kmillikin@chromium.org4111b802010-05-03 10:34:42 +00001937 Handle<JSObject>::null(),
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00001938 Builtins::kFunctionApply,
jkummerow@chromium.org4e308cf2013-05-17 13:39:16 +00001939 false, false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001940
1941 // Make sure that Function.prototype.call appears to be compiled.
1942 // The code will never be called, but inline caching for call will
1943 // only work if it appears to be compiled.
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001944 call->shared()->DontAdaptArguments();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001945 ASSERT(call->is_compiled());
1946
ager@chromium.org32912102009-01-16 10:38:43 +00001947 // Set the expected parameters for apply to 2; required by builtin.
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001948 apply->shared()->set_formal_parameter_count(2);
1949
1950 // Set the lengths for the functions to satisfy ECMA-262.
1951 call->shared()->set_length(1);
1952 apply->shared()->set_length(2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001953 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001954
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001955 InstallBuiltinFunctionIds();
1956
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00001957 // Create a constructor for RegExp results (a variant of Array that
1958 // predefines the two properties index and match).
1959 {
1960 // RegExpResult initial map.
1961
1962 // Find global.Array.prototype to inherit from.
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001963 Handle<JSFunction> array_constructor(native_context()->array_function());
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00001964 Handle<JSObject> array_prototype(
1965 JSObject::cast(array_constructor->instance_prototype()));
1966
1967 // Add initial map.
1968 Handle<Map> initial_map =
danno@chromium.org160a7b02011-04-18 15:51:38 +00001969 factory()->NewMap(JS_ARRAY_TYPE, JSRegExpResult::kSize);
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00001970 initial_map->set_constructor(*array_constructor);
1971
1972 // Set prototype on map.
1973 initial_map->set_non_instance_prototype(false);
1974 initial_map->set_prototype(*array_prototype);
1975
1976 // Update map with length accessor from Array and add "index" and "input".
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00001977 Handle<DescriptorArray> reresult_descriptors =
verwaest@chromium.org33e09c82012-10-10 17:07:22 +00001978 factory()->NewDescriptorArray(0, 3);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001979 DescriptorArray::WhitenessWitness witness(*reresult_descriptors);
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00001980 initial_map->set_instance_descriptors(*reresult_descriptors);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001981
yangguo@chromium.org304cc332012-07-24 07:59:48 +00001982 {
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001983 JSFunction* array_function = native_context()->array_function();
yangguo@chromium.org304cc332012-07-24 07:59:48 +00001984 Handle<DescriptorArray> array_descriptors(
1985 array_function->initial_map()->instance_descriptors());
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001986 String* length = heap()->length_string();
verwaest@chromium.org06ab2ec2012-10-09 17:00:13 +00001987 int old = array_descriptors->SearchWithCache(
1988 length, array_function->initial_map());
yangguo@chromium.org304cc332012-07-24 07:59:48 +00001989 ASSERT(old != DescriptorArray::kNotFound);
1990 CallbacksDescriptor desc(length,
1991 array_descriptors->GetValue(old),
1992 array_descriptors->GetDetails(old).attributes());
1993 initial_map->AppendDescriptor(&desc, witness);
1994 }
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00001995 {
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001996 FieldDescriptor index_field(heap()->index_string(),
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00001997 JSRegExpResult::kIndexIndex,
danno@chromium.orgf005df62013-04-30 16:36:45 +00001998 NONE,
1999 Representation::Tagged());
yangguo@chromium.org304cc332012-07-24 07:59:48 +00002000 initial_map->AppendDescriptor(&index_field, witness);
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00002001 }
2002
2003 {
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00002004 FieldDescriptor input_field(heap()->input_string(),
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00002005 JSRegExpResult::kInputIndex,
danno@chromium.orgf005df62013-04-30 16:36:45 +00002006 NONE,
2007 Representation::Tagged());
yangguo@chromium.org304cc332012-07-24 07:59:48 +00002008 initial_map->AppendDescriptor(&input_field, witness);
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00002009 }
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00002010
2011 initial_map->set_inobject_properties(2);
2012 initial_map->set_pre_allocated_property_fields(2);
2013 initial_map->set_unused_property_fields(0);
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00002014
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00002015 native_context()->set_regexp_result_map(*initial_map);
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00002016 }
2017
svenpanne@chromium.orgc859c4f2012-10-15 11:51:39 +00002018#ifdef VERIFY_HEAP
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002019 builtins->Verify();
2020#endif
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002021
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002022 return true;
2023}
2024
2025
danno@chromium.org160a7b02011-04-18 15:51:38 +00002026bool Genesis::InstallExperimentalNatives() {
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002027 for (int i = ExperimentalNatives::GetDebuggerCount();
2028 i < ExperimentalNatives::GetBuiltinsCount();
2029 i++) {
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00002030 if (FLAG_harmony_symbols &&
2031 strcmp(ExperimentalNatives::GetScriptName(i).start(),
2032 "native symbol.js") == 0) {
2033 if (!CompileExperimentalBuiltin(isolate(), i)) return false;
2034 }
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002035 if (FLAG_harmony_proxies &&
2036 strcmp(ExperimentalNatives::GetScriptName(i).start(),
2037 "native proxy.js") == 0) {
danno@chromium.org160a7b02011-04-18 15:51:38 +00002038 if (!CompileExperimentalBuiltin(isolate(), i)) return false;
2039 }
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002040 if (FLAG_harmony_collections &&
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +00002041 strcmp(ExperimentalNatives::GetScriptName(i).start(),
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002042 "native collection.js") == 0) {
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +00002043 if (!CompileExperimentalBuiltin(isolate(), i)) return false;
2044 }
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +00002045 if (FLAG_harmony_observation &&
danno@chromium.org72204d52012-10-31 10:02:10 +00002046 strcmp(ExperimentalNatives::GetScriptName(i).start(),
2047 "native object-observe.js") == 0) {
2048 if (!CompileExperimentalBuiltin(isolate(), i)) return false;
2049 }
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00002050 if (FLAG_harmony_generators &&
2051 strcmp(ExperimentalNatives::GetScriptName(i).start(),
2052 "native generator.js") == 0) {
2053 if (!CompileExperimentalBuiltin(isolate(), i)) return false;
2054 }
danno@chromium.org169691d2013-07-15 08:01:13 +00002055 if (FLAG_harmony_iteration &&
2056 strcmp(ExperimentalNatives::GetScriptName(i).start(),
2057 "native array-iterator.js") == 0) {
2058 if (!CompileExperimentalBuiltin(isolate(), i)) return false;
2059 }
danno@chromium.orgd3c42102013-08-01 16:58:23 +00002060 if (FLAG_harmony_strings &&
2061 strcmp(ExperimentalNatives::GetScriptName(i).start(),
2062 "native harmony-string.js") == 0) {
2063 if (!CompileExperimentalBuiltin(isolate(), i)) return false;
2064 }
danno@chromium.org59400602013-08-13 17:09:37 +00002065 if (FLAG_harmony_arrays &&
2066 strcmp(ExperimentalNatives::GetScriptName(i).start(),
2067 "native harmony-array.js") == 0) {
2068 if (!CompileExperimentalBuiltin(isolate(), i)) return false;
2069 }
danno@chromium.org160a7b02011-04-18 15:51:38 +00002070 }
ager@chromium.orgea91cc52011-05-23 06:06:11 +00002071
2072 InstallExperimentalNativeFunctions();
2073
danno@chromium.org160a7b02011-04-18 15:51:38 +00002074 return true;
2075}
2076
2077
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00002078static Handle<JSObject> ResolveBuiltinIdHolder(
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00002079 Handle<Context> native_context,
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00002080 const char* holder_expr) {
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00002081 Isolate* isolate = native_context->GetIsolate();
2082 Factory* factory = isolate->factory();
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00002083 Handle<GlobalObject> global(native_context->global_object());
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00002084 const char* period_pos = strchr(holder_expr, '.');
2085 if (period_pos == NULL) {
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00002086 return Handle<JSObject>::cast(GetProperty(
2087 isolate, global, factory->InternalizeUtf8String(holder_expr)));
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00002088 }
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00002089 ASSERT_EQ(".prototype", period_pos);
2090 Vector<const char> property(holder_expr,
2091 static_cast<int>(period_pos - holder_expr));
2092 Handle<JSFunction> function = Handle<JSFunction>::cast(
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00002093 GetProperty(isolate, global, factory->InternalizeUtf8String(property)));
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00002094 return Handle<JSObject>(JSObject::cast(function->prototype()));
2095}
2096
2097
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00002098static void InstallBuiltinFunctionId(Handle<JSObject> holder,
2099 const char* function_name,
2100 BuiltinFunctionId id) {
danno@chromium.org160a7b02011-04-18 15:51:38 +00002101 Factory* factory = holder->GetIsolate()->factory();
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00002102 Handle<String> name = factory->InternalizeUtf8String(function_name);
lrn@chromium.org303ada72010-10-27 09:33:13 +00002103 Object* function_object = holder->GetProperty(*name)->ToObjectUnchecked();
2104 Handle<JSFunction> function(JSFunction::cast(function_object));
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00002105 function->shared()->set_function_data(Smi::FromInt(id));
2106}
2107
2108
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00002109void Genesis::InstallBuiltinFunctionIds() {
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00002110 HandleScope scope(isolate());
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00002111#define INSTALL_BUILTIN_ID(holder_expr, fun_name, name) \
2112 { \
2113 Handle<JSObject> holder = ResolveBuiltinIdHolder( \
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00002114 native_context(), #holder_expr); \
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00002115 BuiltinFunctionId id = k##name; \
2116 InstallBuiltinFunctionId(holder, #fun_name, id); \
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00002117 }
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00002118 FUNCTIONS_WITH_ID_LIST(INSTALL_BUILTIN_ID)
2119#undef INSTALL_BUILTIN_ID
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00002120}
2121
2122
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00002123// Do not forget to update macros.py with named constant
2124// of cache id.
2125#define JSFUNCTION_RESULT_CACHE_LIST(F) \
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00002126 F(16, native_context()->regexp_function())
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00002127
2128
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002129static FixedArray* CreateCache(int size, Handle<JSFunction> factory_function) {
danno@chromium.org160a7b02011-04-18 15:51:38 +00002130 Factory* factory = factory_function->GetIsolate()->factory();
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00002131 // Caches are supposed to live for a long time, allocate in old space.
2132 int array_size = JSFunctionResultCache::kEntriesIndex + 2 * size;
ager@chromium.orgac091b72010-05-05 07:34:42 +00002133 // Cannot use cast as object is not fully initialized yet.
2134 JSFunctionResultCache* cache = reinterpret_cast<JSFunctionResultCache*>(
danno@chromium.org160a7b02011-04-18 15:51:38 +00002135 *factory->NewFixedArrayWithHoles(array_size, TENURED));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002136 cache->set(JSFunctionResultCache::kFactoryIndex, *factory_function);
ager@chromium.orgac091b72010-05-05 07:34:42 +00002137 cache->MakeZeroSize();
2138 return cache;
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00002139}
2140
2141
2142void Genesis::InstallJSFunctionResultCaches() {
2143 const int kNumberOfCaches = 0 +
2144#define F(size, func) + 1
2145 JSFUNCTION_RESULT_CACHE_LIST(F)
2146#undef F
2147 ;
2148
verwaest@chromium.orgd4be0f02013-06-05 13:39:03 +00002149 Handle<FixedArray> caches =
2150 factory()->NewFixedArray(kNumberOfCaches, TENURED);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00002151
2152 int index = 0;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002153
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002154#define F(size, func) do { \
2155 FixedArray* cache = CreateCache((size), Handle<JSFunction>(func)); \
2156 caches->set(index++, cache); \
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002157 } while (false)
2158
2159 JSFUNCTION_RESULT_CACHE_LIST(F);
2160
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00002161#undef F
2162
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00002163 native_context()->set_jsfunction_result_caches(*caches);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00002164}
2165
2166
ricow@chromium.org65fae842010-08-25 15:26:24 +00002167void Genesis::InitializeNormalizedMapCaches() {
2168 Handle<FixedArray> array(
verwaest@chromium.orgd4be0f02013-06-05 13:39:03 +00002169 factory()->NewFixedArray(NormalizedMapCache::kEntries, TENURED));
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00002170 native_context()->set_normalized_map_cache(NormalizedMapCache::cast(*array));
ricow@chromium.org65fae842010-08-25 15:26:24 +00002171}
2172
2173
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00002174bool Bootstrapper::InstallExtensions(Handle<Context> native_context,
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002175 v8::ExtensionConfiguration* extensions) {
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00002176 BootstrapperActive active(this);
2177 SaveContext saved_context(isolate_);
2178 isolate_->set_context(*native_context);
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00002179 if (!Genesis::InstallExtensions(native_context, extensions)) return false;
2180 Genesis::InstallSpecialObjects(native_context);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002181 return true;
2182}
2183
2184
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00002185void Genesis::InstallSpecialObjects(Handle<Context> native_context) {
2186 Isolate* isolate = native_context->GetIsolate();
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00002187 Factory* factory = isolate->factory();
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00002188 HandleScope scope(isolate);
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00002189 Handle<JSGlobalObject> global(JSGlobalObject::cast(
2190 native_context->global_object()));
mads.s.agercbaa0602008-08-14 13:41:48 +00002191 // Expose the natives in global if a name for it is specified.
2192 if (FLAG_expose_natives_as != NULL && strlen(FLAG_expose_natives_as) != 0) {
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00002193 Handle<String> natives =
2194 factory->InternalizeUtf8String(FLAG_expose_natives_as);
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00002195 CHECK_NOT_EMPTY_HANDLE(isolate,
2196 JSObject::SetLocalPropertyIgnoreAttributes(
2197 global, natives,
2198 Handle<JSObject>(global->builtins()),
2199 DONT_ENUM));
mads.s.agercbaa0602008-08-14 13:41:48 +00002200 }
2201
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00002202 Handle<Object> Error = GetProperty(global, "Error");
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00002203 if (Error->IsJSObject()) {
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00002204 Handle<String> name = factory->InternalizeOneByteString(
2205 STATIC_ASCII_VECTOR("stackTraceLimit"));
2206 Handle<Smi> stack_trace_limit(
2207 Smi::FromInt(FLAG_stack_trace_limit), isolate);
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00002208 CHECK_NOT_EMPTY_HANDLE(isolate,
2209 JSObject::SetLocalPropertyIgnoreAttributes(
2210 Handle<JSObject>::cast(Error), name,
2211 stack_trace_limit, NONE));
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002212 }
2213
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002214#ifdef ENABLE_DEBUGGER_SUPPORT
mads.s.agercbaa0602008-08-14 13:41:48 +00002215 // Expose the debug global object in global if a name for it is specified.
2216 if (FLAG_expose_debug_as != NULL && strlen(FLAG_expose_debug_as) != 0) {
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00002217 Debug* debug = isolate->debug();
mads.s.agercbaa0602008-08-14 13:41:48 +00002218 // If loading fails we just bail out without installing the
2219 // debugger but without tanking the whole context.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002220 if (!debug->Load()) return;
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002221 // Set the security token for the debugger context to the same as
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00002222 // the shell native context to allow calling between these (otherwise
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002223 // exposing debug global object doesn't make much sense).
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002224 debug->debug_context()->set_security_token(
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00002225 native_context->security_token());
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002226
mads.s.agercbaa0602008-08-14 13:41:48 +00002227 Handle<String> debug_string =
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00002228 factory->InternalizeUtf8String(FLAG_expose_debug_as);
2229 Handle<Object> global_proxy(
2230 debug->debug_context()->global_proxy(), isolate);
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00002231 CHECK_NOT_EMPTY_HANDLE(isolate,
2232 JSObject::SetLocalPropertyIgnoreAttributes(
2233 global, debug_string, global_proxy, DONT_ENUM));
mads.s.agercbaa0602008-08-14 13:41:48 +00002234 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002235#endif
mads.s.agercbaa0602008-08-14 13:41:48 +00002236}
2237
mstarzinger@chromium.orge0e1b0d2013-07-08 08:38:06 +00002238
ricow@chromium.org27bf2882011-11-17 08:34:43 +00002239static uint32_t Hash(RegisteredExtension* extension) {
2240 return v8::internal::ComputePointerHash(extension);
2241}
2242
mstarzinger@chromium.orge0e1b0d2013-07-08 08:38:06 +00002243
ricow@chromium.org27bf2882011-11-17 08:34:43 +00002244static bool MatchRegisteredExtensions(void* key1, void* key2) {
2245 return key1 == key2;
2246}
2247
2248Genesis::ExtensionStates::ExtensionStates()
yangguo@chromium.orgab30bb82012-02-24 14:41:46 +00002249 : map_(MatchRegisteredExtensions, 8) { }
ricow@chromium.org27bf2882011-11-17 08:34:43 +00002250
2251Genesis::ExtensionTraversalState Genesis::ExtensionStates::get_state(
2252 RegisteredExtension* extension) {
2253 i::HashMap::Entry* entry = map_.Lookup(extension, Hash(extension), false);
2254 if (entry == NULL) {
2255 return UNVISITED;
2256 }
2257 return static_cast<ExtensionTraversalState>(
2258 reinterpret_cast<intptr_t>(entry->value));
2259}
2260
2261void Genesis::ExtensionStates::set_state(RegisteredExtension* extension,
2262 ExtensionTraversalState state) {
2263 map_.Lookup(extension, Hash(extension), true)->value =
2264 reinterpret_cast<void*>(static_cast<intptr_t>(state));
2265}
mads.s.agercbaa0602008-08-14 13:41:48 +00002266
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00002267bool Genesis::InstallExtensions(Handle<Context> native_context,
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002268 v8::ExtensionConfiguration* extensions) {
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00002269 Isolate* isolate = native_context->GetIsolate();
ricow@chromium.org27bf2882011-11-17 08:34:43 +00002270 ExtensionStates extension_states; // All extensions have state UNVISITED.
2271 // Install auto extensions.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002272 v8::RegisteredExtension* current = v8::RegisteredExtension::first_extension();
2273 while (current != NULL) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002274 if (current->extension()->auto_enable())
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00002275 InstallExtension(isolate, current, &extension_states);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002276 current = current->next();
2277 }
2278
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00002279 if (FLAG_expose_gc) InstallExtension(isolate, "v8/gc", &extension_states);
ricow@chromium.org27bf2882011-11-17 08:34:43 +00002280 if (FLAG_expose_externalize_string) {
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00002281 InstallExtension(isolate, "v8/externalize", &extension_states);
ricow@chromium.org27bf2882011-11-17 08:34:43 +00002282 }
yangguo@chromium.org304cc332012-07-24 07:59:48 +00002283 if (FLAG_track_gc_object_stats) {
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00002284 InstallExtension(isolate, "v8/statistics", &extension_states);
yangguo@chromium.org304cc332012-07-24 07:59:48 +00002285 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002286
2287 if (extensions == NULL) return true;
2288 // Install required extensions
2289 int count = v8::ImplementationUtilities::GetNameCount(extensions);
2290 const char** names = v8::ImplementationUtilities::GetNames(extensions);
2291 for (int i = 0; i < count; i++) {
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00002292 if (!InstallExtension(isolate, names[i], &extension_states))
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002293 return false;
2294 }
2295
2296 return true;
2297}
2298
2299
2300// Installs a named extension. This methods is unoptimized and does
2301// not scale well if we want to support a large number of extensions.
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00002302bool Genesis::InstallExtension(Isolate* isolate,
2303 const char* name,
ricow@chromium.org27bf2882011-11-17 08:34:43 +00002304 ExtensionStates* extension_states) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002305 v8::RegisteredExtension* current = v8::RegisteredExtension::first_extension();
2306 // Loop until we find the relevant extension
2307 while (current != NULL) {
2308 if (strcmp(name, current->extension()->name()) == 0) break;
2309 current = current->next();
2310 }
2311 // Didn't find the extension; fail.
2312 if (current == NULL) {
2313 v8::Utils::ReportApiFailure(
2314 "v8::Context::New()", "Cannot find required extension");
2315 return false;
2316 }
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00002317 return InstallExtension(isolate, current, extension_states);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002318}
2319
2320
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00002321bool Genesis::InstallExtension(Isolate* isolate,
2322 v8::RegisteredExtension* current,
ricow@chromium.org27bf2882011-11-17 08:34:43 +00002323 ExtensionStates* extension_states) {
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00002324 HandleScope scope(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002325
ricow@chromium.org27bf2882011-11-17 08:34:43 +00002326 if (extension_states->get_state(current) == INSTALLED) return true;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002327 // The current node has already been visited so there must be a
2328 // cycle in the dependency graph; fail.
ricow@chromium.org27bf2882011-11-17 08:34:43 +00002329 if (extension_states->get_state(current) == VISITED) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002330 v8::Utils::ReportApiFailure(
2331 "v8::Context::New()", "Circular extension dependency");
2332 return false;
2333 }
ricow@chromium.org27bf2882011-11-17 08:34:43 +00002334 ASSERT(extension_states->get_state(current) == UNVISITED);
2335 extension_states->set_state(current, VISITED);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002336 v8::Extension* extension = current->extension();
2337 // Install the extension's dependencies
2338 for (int i = 0; i < extension->dependency_count(); i++) {
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00002339 if (!InstallExtension(isolate,
2340 extension->dependencies()[i],
2341 extension_states)) {
ricow@chromium.org27bf2882011-11-17 08:34:43 +00002342 return false;
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00002343 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002344 }
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002345 Handle<String> source_code =
2346 isolate->factory()->NewExternalStringFromAscii(extension->source());
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00002347 bool result = CompileScriptCached(isolate,
2348 CStrVector(extension->name()),
2349 source_code,
2350 isolate->bootstrapper()->extensions_cache(),
2351 extension,
2352 Handle<Context>(isolate->context()),
2353 false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002354 ASSERT(isolate->has_pending_exception() != result);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002355 if (!result) {
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002356 // We print out the name of the extension that fail to install.
2357 // When an error is thrown during bootstrapping we automatically print
2358 // the line number at which this happened to the console in the isolate
2359 // error throwing functionality.
2360 OS::PrintError("Error installing extension '%s'.\n",
2361 current->extension()->name());
lrn@chromium.org7516f052011-03-30 08:52:27 +00002362 isolate->clear_pending_exception();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002363 }
ricow@chromium.org27bf2882011-11-17 08:34:43 +00002364 extension_states->set_state(current, INSTALLED);
2365 isolate->NotifyExtensionInstalled();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002366 return result;
2367}
2368
2369
ager@chromium.org5c838252010-02-19 08:53:10 +00002370bool Genesis::InstallJSBuiltins(Handle<JSBuiltinsObject> builtins) {
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00002371 HandleScope scope(isolate());
ager@chromium.org5c838252010-02-19 08:53:10 +00002372 for (int i = 0; i < Builtins::NumberOfJavaScriptBuiltins(); i++) {
2373 Builtins::JavaScript id = static_cast<Builtins::JavaScript>(i);
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00002374 Handle<String> name =
2375 factory()->InternalizeUtf8String(Builtins::GetName(id));
lrn@chromium.org303ada72010-10-27 09:33:13 +00002376 Object* function_object = builtins->GetPropertyNoExceptionThrown(*name);
ager@chromium.org5c838252010-02-19 08:53:10 +00002377 Handle<JSFunction> function
lrn@chromium.org303ada72010-10-27 09:33:13 +00002378 = Handle<JSFunction>(JSFunction::cast(function_object));
ager@chromium.org5c838252010-02-19 08:53:10 +00002379 builtins->set_javascript_builtin(id, *function);
rossberg@chromium.org400388e2012-06-06 09:29:22 +00002380 if (!JSFunction::CompileLazy(function, CLEAR_EXCEPTION)) {
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002381 return false;
2382 }
rossberg@chromium.org400388e2012-06-06 09:29:22 +00002383 builtins->set_javascript_builtin_code(id, function->shared()->code());
ager@chromium.org5c838252010-02-19 08:53:10 +00002384 }
2385 return true;
2386}
2387
2388
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002389bool Genesis::ConfigureGlobalObjects(
2390 v8::Handle<v8::ObjectTemplate> global_proxy_template) {
2391 Handle<JSObject> global_proxy(
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00002392 JSObject::cast(native_context()->global_proxy()));
2393 Handle<JSObject> inner_global(
2394 JSObject::cast(native_context()->global_object()));
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002395
2396 if (!global_proxy_template.IsEmpty()) {
2397 // Configure the global proxy object.
2398 Handle<ObjectTemplateInfo> proxy_data =
2399 v8::Utils::OpenHandle(*global_proxy_template);
2400 if (!ConfigureApiObject(global_proxy, proxy_data)) return false;
2401
2402 // Configure the inner global object.
2403 Handle<FunctionTemplateInfo> proxy_constructor(
2404 FunctionTemplateInfo::cast(proxy_data->constructor()));
2405 if (!proxy_constructor->prototype_template()->IsUndefined()) {
2406 Handle<ObjectTemplateInfo> inner_data(
2407 ObjectTemplateInfo::cast(proxy_constructor->prototype_template()));
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002408 if (!ConfigureApiObject(inner_global, inner_data)) return false;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002409 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002410 }
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002411
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002412 SetObjectPrototype(global_proxy, inner_global);
ulan@chromium.org906e2fb2013-05-14 08:14:38 +00002413
2414 native_context()->set_initial_array_prototype(
2415 JSArray::cast(native_context()->array_function()->prototype()));
2416
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002417 return true;
2418}
2419
2420
2421bool Genesis::ConfigureApiObject(Handle<JSObject> object,
2422 Handle<ObjectTemplateInfo> object_template) {
2423 ASSERT(!object_template.is_null());
2424 ASSERT(object->IsInstanceOf(
2425 FunctionTemplateInfo::cast(object_template->constructor())));
2426
2427 bool pending_exception = false;
2428 Handle<JSObject> obj =
2429 Execution::InstantiateObject(object_template, &pending_exception);
2430 if (pending_exception) {
danno@chromium.org160a7b02011-04-18 15:51:38 +00002431 ASSERT(isolate()->has_pending_exception());
2432 isolate()->clear_pending_exception();
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002433 return false;
2434 }
2435 TransferObject(obj, object);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002436 return true;
2437}
2438
2439
2440void Genesis::TransferNamedProperties(Handle<JSObject> from,
2441 Handle<JSObject> to) {
2442 if (from->HasFastProperties()) {
2443 Handle<DescriptorArray> descs =
2444 Handle<DescriptorArray>(from->map()->instance_descriptors());
ulan@chromium.org57ff8812013-05-10 08:16:55 +00002445 for (int i = 0; i < from->map()->NumberOfOwnDescriptors(); i++) {
erik.corry@gmail.comed49e962012-04-17 11:57:53 +00002446 PropertyDetails details = descs->GetDetails(i);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002447 switch (details.type()) {
2448 case FIELD: {
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00002449 HandleScope inner(isolate());
ulan@chromium.org750145a2013-03-07 15:14:13 +00002450 Handle<Name> key = Handle<Name>(descs->GetKey(i));
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002451 int index = descs->GetFieldIndex(i);
ulan@chromium.org57ff8812013-05-10 08:16:55 +00002452 ASSERT(!descs->GetDetails(i).representation().IsDouble());
2453 Handle<Object> value = Handle<Object>(from->RawFastPropertyAt(index),
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00002454 isolate());
2455 CHECK_NOT_EMPTY_HANDLE(isolate(),
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00002456 JSObject::SetLocalPropertyIgnoreAttributes(
2457 to, key, value, details.attributes()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002458 break;
2459 }
jkummerow@chromium.orgfb732b12013-07-26 10:27:09 +00002460 case CONSTANT: {
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00002461 HandleScope inner(isolate());
ulan@chromium.org750145a2013-03-07 15:14:13 +00002462 Handle<Name> key = Handle<Name>(descs->GetKey(i));
jkummerow@chromium.orgfb732b12013-07-26 10:27:09 +00002463 Handle<Object> constant(descs->GetConstant(i), isolate());
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00002464 CHECK_NOT_EMPTY_HANDLE(isolate(),
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00002465 JSObject::SetLocalPropertyIgnoreAttributes(
jkummerow@chromium.orgfb732b12013-07-26 10:27:09 +00002466 to, key, constant, details.attributes()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002467 break;
2468 }
2469 case CALLBACKS: {
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002470 LookupResult result(isolate());
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002471 to->LocalLookup(descs->GetKey(i), &result);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002472 // If the property is already there we skip it
verwaest@chromium.org753aee42012-07-17 16:15:42 +00002473 if (result.IsFound()) continue;
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00002474 HandleScope inner(isolate());
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002475 ASSERT(!to->HasFastProperties());
2476 // Add to dictionary.
ulan@chromium.org750145a2013-03-07 15:14:13 +00002477 Handle<Name> key = Handle<Name>(descs->GetKey(i));
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00002478 Handle<Object> callbacks(descs->GetCallbacksObject(i), isolate());
ulan@chromium.org57ff8812013-05-10 08:16:55 +00002479 PropertyDetails d = PropertyDetails(
2480 details.attributes(), CALLBACKS, i + 1);
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00002481 JSObject::SetNormalizedProperty(to, key, callbacks, d);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002482 break;
2483 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002484 case NORMAL:
2485 // Do not occur since the from object has fast properties.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002486 case HANDLER:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002487 case INTERCEPTOR:
yangguo@chromium.org99aa4902012-07-06 16:21:55 +00002488 case TRANSITION:
jkummerow@chromium.org7a6fc812012-06-27 11:12:38 +00002489 case NONEXISTENT:
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002490 // No element in instance descriptors have proxy or interceptor type.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002491 UNREACHABLE();
2492 break;
2493 }
2494 }
2495 } else {
ulan@chromium.org750145a2013-03-07 15:14:13 +00002496 Handle<NameDictionary> properties =
2497 Handle<NameDictionary>(from->property_dictionary());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002498 int capacity = properties->Capacity();
2499 for (int i = 0; i < capacity; i++) {
2500 Object* raw_key(properties->KeyAt(i));
2501 if (properties->IsKey(raw_key)) {
ulan@chromium.org750145a2013-03-07 15:14:13 +00002502 ASSERT(raw_key->IsName());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002503 // If the property is already there we skip it.
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002504 LookupResult result(isolate());
ulan@chromium.org750145a2013-03-07 15:14:13 +00002505 to->LocalLookup(Name::cast(raw_key), &result);
verwaest@chromium.org753aee42012-07-17 16:15:42 +00002506 if (result.IsFound()) continue;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002507 // Set the property.
ulan@chromium.org750145a2013-03-07 15:14:13 +00002508 Handle<Name> key = Handle<Name>(Name::cast(raw_key));
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00002509 Handle<Object> value = Handle<Object>(properties->ValueAt(i),
2510 isolate());
danno@chromium.org41728482013-06-12 22:31:22 +00002511 ASSERT(!value->IsCell());
dslomov@chromium.orgb752d402013-06-18 11:54:54 +00002512 if (value->IsPropertyCell()) {
2513 value = Handle<Object>(PropertyCell::cast(*value)->value(),
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00002514 isolate());
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002515 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002516 PropertyDetails details = properties->DetailsAt(i);
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00002517 CHECK_NOT_EMPTY_HANDLE(isolate(),
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00002518 JSObject::SetLocalPropertyIgnoreAttributes(
2519 to, key, value, details.attributes()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002520 }
2521 }
2522 }
2523}
2524
2525
2526void Genesis::TransferIndexedProperties(Handle<JSObject> from,
2527 Handle<JSObject> to) {
2528 // Cloning the elements array is sufficient.
2529 Handle<FixedArray> from_elements =
2530 Handle<FixedArray>(FixedArray::cast(from->elements()));
verwaest@chromium.orgd4be0f02013-06-05 13:39:03 +00002531 Handle<FixedArray> to_elements = factory()->CopyFixedArray(from_elements);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002532 to->set_elements(*to_elements);
2533}
2534
2535
2536void Genesis::TransferObject(Handle<JSObject> from, Handle<JSObject> to) {
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00002537 HandleScope outer(isolate());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002538
2539 ASSERT(!from->IsJSArray());
2540 ASSERT(!to->IsJSArray());
2541
2542 TransferNamedProperties(from, to);
2543 TransferIndexedProperties(from, to);
2544
2545 // Transfer the prototype (new map is needed).
2546 Handle<Map> old_to_map = Handle<Map>(to->map());
verwaest@chromium.orgd4be0f02013-06-05 13:39:03 +00002547 Handle<Map> new_to_map = factory()->CopyMap(old_to_map);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002548 new_to_map->set_prototype(from->map()->prototype());
2549 to->set_map(*new_to_map);
2550}
2551
2552
2553void Genesis::MakeFunctionInstancePrototypeWritable() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002554 // The maps with writable prototype are created in CreateEmptyFunction
2555 // and CreateStrictModeFunctionMaps respectively. Initially the maps are
2556 // created with read-only prototype for JS builtins processing.
ulan@chromium.org906e2fb2013-05-14 08:14:38 +00002557 ASSERT(!function_map_writable_prototype_.is_null());
2558 ASSERT(!strict_mode_function_map_writable_prototype_.is_null());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002559
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002560 // Replace function instance maps to make prototype writable.
ulan@chromium.org906e2fb2013-05-14 08:14:38 +00002561 native_context()->set_function_map(*function_map_writable_prototype_);
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00002562 native_context()->set_strict_mode_function_map(
ulan@chromium.org906e2fb2013-05-14 08:14:38 +00002563 *strict_mode_function_map_writable_prototype_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002564}
2565
2566
danno@chromium.org160a7b02011-04-18 15:51:38 +00002567Genesis::Genesis(Isolate* isolate,
2568 Handle<Object> global_object,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002569 v8::Handle<v8::ObjectTemplate> global_template,
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00002570 v8::ExtensionConfiguration* extensions)
2571 : isolate_(isolate),
2572 active_(isolate->bootstrapper()) {
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00002573 result_ = Handle<Context>::null();
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +00002574 // If V8 cannot be initialized, just return.
2575 if (!V8::Initialize(NULL)) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002576
2577 // Before creating the roots we must save the context and restore it
2578 // on all function exits.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002579 SaveContext saved_context(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002580
fschneider@chromium.org7d10be52012-04-10 12:30:14 +00002581 // During genesis, the boilerplate for stack overflow won't work until the
2582 // environment has been at least partially initialized. Add a stack check
2583 // before entering JS code to catch overflow early.
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00002584 StackLimitCheck check(isolate);
fschneider@chromium.org7d10be52012-04-10 12:30:14 +00002585 if (check.HasOverflowed()) return;
2586
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +00002587 // We can only de-serialize a context if the isolate was initialized from
2588 // a snapshot. Otherwise we have to build the context from scratch.
2589 if (isolate->initialized_from_snapshot()) {
jkummerow@chromium.org3d00d0a2013-09-04 13:57:32 +00002590 native_context_ = Snapshot::NewContextFromSnapshot(isolate);
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +00002591 } else {
2592 native_context_ = Handle<Context>();
2593 }
2594
jkummerow@chromium.org7bd87f02013-03-20 18:06:29 +00002595 if (!native_context().is_null()) {
2596 AddToWeakNativeContextList(*native_context());
2597 isolate->set_context(*native_context());
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002598 isolate->counters()->contexts_created_by_snapshot()->Increment();
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002599 Handle<GlobalObject> inner_global;
2600 Handle<JSGlobalProxy> global_proxy =
2601 CreateNewGlobals(global_template,
2602 global_object,
2603 &inner_global);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002604
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002605 HookUpGlobalProxy(inner_global, global_proxy);
2606 HookUpInnerGlobal(inner_global);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002607
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002608 if (!ConfigureGlobalObjects(global_template)) return;
2609 } else {
2610 // We get here if there was no context snapshot.
2611 CreateRoots();
danno@chromium.org160a7b02011-04-18 15:51:38 +00002612 Handle<JSFunction> empty_function = CreateEmptyFunction(isolate);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002613 CreateStrictModeFunctionMaps(empty_function);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002614 Handle<GlobalObject> inner_global;
2615 Handle<JSGlobalProxy> global_proxy =
2616 CreateNewGlobals(global_template, global_object, &inner_global);
2617 HookUpGlobalProxy(inner_global, global_proxy);
jkummerow@chromium.orgba72ec82013-07-22 09:21:20 +00002618 InitializeGlobal(inner_global, empty_function);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00002619 InstallJSFunctionResultCaches();
ricow@chromium.org65fae842010-08-25 15:26:24 +00002620 InitializeNormalizedMapCaches();
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00002621 if (!InstallNatives()) return;
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002622
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002623 MakeFunctionInstancePrototypeWritable();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002624
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002625 if (!ConfigureGlobalObjects(global_template)) return;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002626 isolate->counters()->contexts_created_from_scratch()->Increment();
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002627 }
mads.s.agercbaa0602008-08-14 13:41:48 +00002628
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +00002629 // Initialize experimental globals and install experimental natives.
2630 InitializeExperimentalGlobal();
danno@chromium.org160a7b02011-04-18 15:51:38 +00002631 if (!InstallExperimentalNatives()) return;
2632
hpayer@chromium.orgc5d49712013-09-11 08:25:48 +00002633 // Initially seed the per-context random number generator
2634 // using the per-isolate random number generator.
2635 uint32_t* state = reinterpret_cast<uint32_t*>(
2636 native_context()->random_seed()->GetDataStartAddress());
2637 do {
2638 isolate->random_number_generator()->NextBytes(state, kRandomStateSize);
2639 } while (state[0] == 0 || state[1] == 0);
2640
jkummerow@chromium.org7bd87f02013-03-20 18:06:29 +00002641 result_ = native_context();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002642}
2643
ager@chromium.orgddb913d2009-01-27 10:01:48 +00002644
2645// Support for thread preemption.
2646
2647// Reserve space for statics needing saving and restoring.
2648int Bootstrapper::ArchiveSpacePerThread() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002649 return sizeof(NestingCounterType);
ager@chromium.orgddb913d2009-01-27 10:01:48 +00002650}
2651
2652
2653// Archive statics that are thread local.
2654char* Bootstrapper::ArchiveState(char* to) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002655 *reinterpret_cast<NestingCounterType*>(to) = nesting_;
2656 nesting_ = 0;
2657 return to + sizeof(NestingCounterType);
ager@chromium.orgddb913d2009-01-27 10:01:48 +00002658}
2659
2660
2661// Restore statics that are thread local.
2662char* Bootstrapper::RestoreState(char* from) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002663 nesting_ = *reinterpret_cast<NestingCounterType*>(from);
2664 return from + sizeof(NestingCounterType);
ager@chromium.orgddb913d2009-01-27 10:01:48 +00002665}
2666
2667
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002668// Called when the top-level V8 mutex is destroyed.
2669void Bootstrapper::FreeThreadResources() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002670 ASSERT(!IsActive());
ager@chromium.orgddb913d2009-01-27 10:01:48 +00002671}
2672
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002673} } // namespace v8::internal