blob: b0d3a5e50eedb2d662139642b2af315e3152be94 [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) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000097 extensions_cache_.Initialize(create_heap_objects);
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +000098 GCExtension::Register();
99 ExternalizeStringExtension::Register();
yangguo@chromium.org304cc332012-07-24 07:59:48 +0000100 StatisticsExtension::Register();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000101}
102
103
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000104char* Bootstrapper::AllocateAutoDeletedArray(int bytes) {
105 char* memory = new char[bytes];
106 if (memory != NULL) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000107 if (delete_these_arrays_on_tear_down_ == NULL) {
108 delete_these_arrays_on_tear_down_ = new List<char*>(2);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000109 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000110 delete_these_arrays_on_tear_down_->Add(memory);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000111 }
112 return memory;
113}
114
115
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000116void Bootstrapper::TearDown() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000117 if (delete_these_non_arrays_on_tear_down_ != NULL) {
118 int len = delete_these_non_arrays_on_tear_down_->length();
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000119 ASSERT(len < 20); // Don't use this mechanism for unbounded allocations.
120 for (int i = 0; i < len; i++) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000121 delete delete_these_non_arrays_on_tear_down_->at(i);
122 delete_these_non_arrays_on_tear_down_->at(i) = NULL;
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000123 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000124 delete delete_these_non_arrays_on_tear_down_;
125 delete_these_non_arrays_on_tear_down_ = NULL;
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000126 }
127
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000128 if (delete_these_arrays_on_tear_down_ != NULL) {
129 int len = delete_these_arrays_on_tear_down_->length();
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000130 ASSERT(len < 1000); // Don't use this mechanism for unbounded allocations.
131 for (int i = 0; i < len; i++) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000132 delete[] delete_these_arrays_on_tear_down_->at(i);
133 delete_these_arrays_on_tear_down_->at(i) = NULL;
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000134 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000135 delete delete_these_arrays_on_tear_down_;
136 delete_these_arrays_on_tear_down_ = NULL;
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000137 }
138
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000139 extensions_cache_.Initialize(false); // Yes, symmetrical
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000140}
141
142
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000143class Genesis BASE_EMBEDDED {
144 public:
danno@chromium.org160a7b02011-04-18 15:51:38 +0000145 Genesis(Isolate* isolate,
146 Handle<Object> global_object,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000147 v8::Handle<v8::ObjectTemplate> global_template,
148 v8::ExtensionConfiguration* extensions);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000149 ~Genesis() { }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000150
danno@chromium.org160a7b02011-04-18 15:51:38 +0000151 Isolate* isolate() const { return isolate_; }
152 Factory* factory() const { return isolate_->factory(); }
153 Heap* heap() const { return isolate_->heap(); }
154
jkummerow@chromium.org7bd87f02013-03-20 18:06:29 +0000155 Handle<Context> result() { return result_; }
jkummerow@chromium.org8718d732013-03-19 22:53:30 +0000156
jkummerow@chromium.org7bd87f02013-03-20 18:06:29 +0000157 private:
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000158 Handle<Context> native_context() { return native_context_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000159
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000160 // Creates some basic objects. Used for creating a context from scratch.
161 void CreateRoots();
162 // Creates the empty function. Used for creating a context from scratch.
danno@chromium.org160a7b02011-04-18 15:51:38 +0000163 Handle<JSFunction> CreateEmptyFunction(Isolate* isolate);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000164 // Creates the ThrowTypeError function. ECMA 5th Ed. 13.2.3
danno@chromium.org40cb8782011-05-25 07:58:50 +0000165 Handle<JSFunction> GetThrowTypeErrorFunction();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000166
167 void CreateStrictModeFunctionMaps(Handle<JSFunction> empty);
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000168
169 // Make the "arguments" and "caller" properties throw a TypeError on access.
170 void PoisonArgumentsAndCaller(Handle<Map> map);
171
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000172 // Creates the global objects using the global and the template passed in
173 // through the API. We call this regardless of whether we are building a
174 // context from scratch or using a deserialized one from the partial snapshot
175 // but in the latter case we don't use the objects it produces directly, as
176 // we have to used the deserialized ones that are linked together with the
177 // rest of the context snapshot.
178 Handle<JSGlobalProxy> CreateNewGlobals(
179 v8::Handle<v8::ObjectTemplate> global_template,
180 Handle<Object> global_object,
181 Handle<GlobalObject>* global_proxy_out);
182 // Hooks the given global proxy into the context. If the context was created
183 // by deserialization then this will unhook the global proxy that was
184 // deserialized, leaving the GC to pick it up.
185 void HookUpGlobalProxy(Handle<GlobalObject> inner_global,
186 Handle<JSGlobalProxy> global_proxy);
187 // Similarly, we want to use the inner global that has been created by the
188 // templates passed through the API. The inner global from the snapshot is
189 // detached from the other objects in the snapshot.
190 void HookUpInnerGlobal(Handle<GlobalObject> inner_global);
191 // New context initialization. Used for creating a context from scratch.
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000192 bool InitializeGlobal(Handle<GlobalObject> inner_global,
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000193 Handle<JSFunction> empty_function);
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +0000194 void InitializeExperimentalGlobal();
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000195 // Installs the contents of the native .js files on the global objects.
196 // Used for creating a context from scratch.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000197 void InstallNativeFunctions();
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000198 void InstallExperimentalNativeFunctions();
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +0000199 Handle<JSFunction> InstallInternalArray(Handle<JSBuiltinsObject> builtins,
200 const char* name,
201 ElementsKind elements_kind);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000202 bool InstallNatives();
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000203
danno@chromium.orgf005df62013-04-30 16:36:45 +0000204 Handle<JSFunction> InstallTypedArray(const char* name);
danno@chromium.org160a7b02011-04-18 15:51:38 +0000205 bool InstallExperimentalNatives();
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000206 void InstallBuiltinFunctionIds();
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000207 void InstallJSFunctionResultCaches();
ricow@chromium.org65fae842010-08-25 15:26:24 +0000208 void InitializeNormalizedMapCaches();
ricow@chromium.org27bf2882011-11-17 08:34:43 +0000209
210 enum ExtensionTraversalState {
211 UNVISITED, VISITED, INSTALLED
212 };
213
214 class ExtensionStates {
yangguo@chromium.orgab30bb82012-02-24 14:41:46 +0000215 public:
ricow@chromium.org27bf2882011-11-17 08:34:43 +0000216 ExtensionStates();
217 ExtensionTraversalState get_state(RegisteredExtension* extension);
218 void set_state(RegisteredExtension* extension,
219 ExtensionTraversalState state);
yangguo@chromium.orgab30bb82012-02-24 14:41:46 +0000220 private:
ricow@chromium.org27bf2882011-11-17 08:34:43 +0000221 HashMap map_;
222 DISALLOW_COPY_AND_ASSIGN(ExtensionStates);
223 };
224
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000225 // Used both for deserialized and from-scratch contexts to add the extensions
226 // provided.
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000227 static bool InstallExtensions(Handle<Context> native_context,
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000228 v8::ExtensionConfiguration* extensions);
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +0000229 static bool InstallExtension(Isolate* isolate,
230 const char* name,
ricow@chromium.org27bf2882011-11-17 08:34:43 +0000231 ExtensionStates* extension_states);
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +0000232 static bool InstallExtension(Isolate* isolate,
233 v8::RegisteredExtension* current,
ricow@chromium.org27bf2882011-11-17 08:34:43 +0000234 ExtensionStates* extension_states);
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000235 static void InstallSpecialObjects(Handle<Context> native_context);
ager@chromium.org5c838252010-02-19 08:53:10 +0000236 bool InstallJSBuiltins(Handle<JSBuiltinsObject> builtins);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000237 bool ConfigureApiObject(Handle<JSObject> object,
238 Handle<ObjectTemplateInfo> object_template);
239 bool ConfigureGlobalObjects(v8::Handle<v8::ObjectTemplate> global_template);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000240
241 // Migrates all properties from the 'from' object to the 'to'
242 // object and overrides the prototype in 'to' with the one from
243 // 'from'.
244 void TransferObject(Handle<JSObject> from, Handle<JSObject> to);
245 void TransferNamedProperties(Handle<JSObject> from, Handle<JSObject> to);
246 void TransferIndexedProperties(Handle<JSObject> from, Handle<JSObject> to);
247
kmillikin@chromium.org4111b802010-05-03 10:34:42 +0000248 enum PrototypePropertyMode {
249 DONT_ADD_PROTOTYPE,
250 ADD_READONLY_PROTOTYPE,
251 ADD_WRITEABLE_PROTOTYPE
252 };
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000253
254 Handle<Map> CreateFunctionMap(PrototypePropertyMode prototype_mode);
255
yangguo@chromium.org304cc332012-07-24 07:59:48 +0000256 void SetFunctionInstanceDescriptor(Handle<Map> map,
257 PrototypePropertyMode prototypeMode);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000258 void MakeFunctionInstancePrototypeWritable();
259
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000260 Handle<Map> CreateStrictModeFunctionMap(
261 PrototypePropertyMode prototype_mode,
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000262 Handle<JSFunction> empty_function);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000263
yangguo@chromium.org304cc332012-07-24 07:59:48 +0000264 void SetStrictFunctionInstanceDescriptor(Handle<Map> map,
265 PrototypePropertyMode propertyMode);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000266
danno@chromium.org160a7b02011-04-18 15:51:38 +0000267 static bool CompileBuiltin(Isolate* isolate, int index);
268 static bool CompileExperimentalBuiltin(Isolate* isolate, int index);
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +0000269 static bool CompileNative(Isolate* isolate,
270 Vector<const char> name,
271 Handle<String> source);
272 static bool CompileScriptCached(Isolate* isolate,
273 Vector<const char> name,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000274 Handle<String> source,
275 SourceCodeCache* cache,
276 v8::Extension* extension,
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000277 Handle<Context> top_context,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000278 bool use_runtime_context);
279
jkummerow@chromium.org7bd87f02013-03-20 18:06:29 +0000280 Isolate* isolate_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000281 Handle<Context> result_;
jkummerow@chromium.org7bd87f02013-03-20 18:06:29 +0000282 Handle<Context> native_context_;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000283
284 // Function instance maps. Function literal maps are created initially with
285 // a read only prototype for the processing of JS builtins. Later the function
286 // instance maps are replaced in order to make prototype writable.
287 // These are the final, writable prototype, maps.
288 Handle<Map> function_instance_map_writable_prototype_;
289 Handle<Map> strict_mode_function_instance_map_writable_prototype_;
danno@chromium.org40cb8782011-05-25 07:58:50 +0000290 Handle<JSFunction> throw_type_error_function;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000291
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000292 BootstrapperActive active_;
293 friend class Bootstrapper;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000294};
295
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000296
297void Bootstrapper::Iterate(ObjectVisitor* v) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000298 extensions_cache_.Iterate(v);
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +0000299 v->Synchronize(VisitorSynchronization::kExtensions);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000300}
301
302
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000303Handle<Context> Bootstrapper::CreateEnvironment(
304 Handle<Object> global_object,
305 v8::Handle<v8::ObjectTemplate> global_template,
306 v8::ExtensionConfiguration* extensions) {
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +0000307 HandleScope scope(isolate_);
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +0000308 Genesis genesis(isolate_, global_object, global_template, extensions);
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000309 Handle<Context> env = genesis.result();
310 if (env.is_null() || !InstallExtensions(env, extensions)) {
311 return Handle<Context>();
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000312 }
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000313 return scope.CloseAndEscape(env);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000314}
315
316
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000317static void SetObjectPrototype(Handle<JSObject> object, Handle<Object> proto) {
318 // object.__proto__ = proto;
danno@chromium.org160a7b02011-04-18 15:51:38 +0000319 Factory* factory = object->GetIsolate()->factory();
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000320 Handle<Map> old_to_map = Handle<Map>(object->map());
verwaest@chromium.org753aee42012-07-17 16:15:42 +0000321 Handle<Map> new_to_map = factory->CopyMap(old_to_map);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000322 new_to_map->set_prototype(*proto);
323 object->set_map(*new_to_map);
324}
325
326
327void Bootstrapper::DetachGlobal(Handle<Context> env) {
danno@chromium.org160a7b02011-04-18 15:51:38 +0000328 Factory* factory = env->GetIsolate()->factory();
yangguo@chromium.org355cfd12012-08-29 15:32:24 +0000329 Handle<JSGlobalProxy> global_proxy(JSGlobalProxy::cast(env->global_proxy()));
330 global_proxy->set_native_context(*factory->null_value());
331 SetObjectPrototype(global_proxy, factory->null_value());
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000332 env->set_global_proxy(env->global_object());
333 env->global_object()->set_global_receiver(env->global_object());
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000334}
335
336
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +0000337void Bootstrapper::ReattachGlobal(Handle<Context> env,
yangguo@chromium.org355cfd12012-08-29 15:32:24 +0000338 Handle<JSGlobalProxy> global_proxy) {
339 env->global_object()->set_global_receiver(*global_proxy);
340 env->set_global_proxy(*global_proxy);
341 SetObjectPrototype(global_proxy, Handle<JSObject>(env->global_object()));
342 global_proxy->set_native_context(*env);
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +0000343}
344
345
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000346static Handle<JSFunction> InstallFunction(Handle<JSObject> target,
347 const char* name,
348 InstanceType type,
349 int instance_size,
350 Handle<JSObject> prototype,
351 Builtins::Name call,
352 bool is_ecma_native) {
danno@chromium.org160a7b02011-04-18 15:51:38 +0000353 Isolate* isolate = target->GetIsolate();
lrn@chromium.org7516f052011-03-30 08:52:27 +0000354 Factory* factory = isolate->factory();
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000355 Handle<String> internalized_name = factory->InternalizeUtf8String(name);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000356 Handle<Code> call_code = Handle<Code>(isolate->builtins()->builtin(call));
kmillikin@chromium.org4111b802010-05-03 10:34:42 +0000357 Handle<JSFunction> function = prototype.is_null() ?
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000358 factory->NewFunctionWithoutPrototype(internalized_name, call_code) :
359 factory->NewFunctionWithPrototype(internalized_name,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000360 type,
361 instance_size,
362 prototype,
363 call_code,
364 is_ecma_native);
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000365 PropertyAttributes attributes;
366 if (target->IsJSBuiltinsObject()) {
367 attributes =
368 static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE | READ_ONLY);
369 } else {
370 attributes = DONT_ENUM;
371 }
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000372 CHECK_NOT_EMPTY_HANDLE(isolate,
373 JSObject::SetLocalPropertyIgnoreAttributes(
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000374 target, internalized_name, function, attributes));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000375 if (is_ecma_native) {
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000376 function->shared()->set_instance_class_name(*internalized_name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000377 }
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000378 function->shared()->set_native(true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000379 return function;
380}
381
382
yangguo@chromium.org304cc332012-07-24 07:59:48 +0000383void Genesis::SetFunctionInstanceDescriptor(
384 Handle<Map> map, PrototypePropertyMode prototypeMode) {
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000385 int size = (prototypeMode == DONT_ADD_PROTOTYPE) ? 4 : 5;
verwaest@chromium.org33e09c82012-10-10 17:07:22 +0000386 Handle<DescriptorArray> descriptors(factory()->NewDescriptorArray(0, size));
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000387 DescriptorArray::WhitenessWitness witness(*descriptors);
388
yangguo@chromium.org304cc332012-07-24 07:59:48 +0000389 Handle<Foreign> length(factory()->NewForeign(&Accessors::FunctionLength));
390 Handle<Foreign> name(factory()->NewForeign(&Accessors::FunctionName));
391 Handle<Foreign> args(factory()->NewForeign(&Accessors::FunctionArguments));
392 Handle<Foreign> caller(factory()->NewForeign(&Accessors::FunctionCaller));
393 Handle<Foreign> prototype;
394 if (prototypeMode != DONT_ADD_PROTOTYPE) {
395 prototype = factory()->NewForeign(&Accessors::FunctionPrototype);
396 }
397 PropertyAttributes attribs = static_cast<PropertyAttributes>(
398 DONT_ENUM | DONT_DELETE | READ_ONLY);
rossberg@chromium.org89e18f52012-10-22 13:09:53 +0000399 map->set_instance_descriptors(*descriptors);
yangguo@chromium.org304cc332012-07-24 07:59:48 +0000400
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000401 { // Add length.
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000402 CallbacksDescriptor d(*factory()->length_string(), *length, attribs);
yangguo@chromium.org304cc332012-07-24 07:59:48 +0000403 map->AppendDescriptor(&d, witness);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000404 }
405 { // Add name.
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000406 CallbacksDescriptor d(*factory()->name_string(), *name, attribs);
yangguo@chromium.org304cc332012-07-24 07:59:48 +0000407 map->AppendDescriptor(&d, witness);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000408 }
409 { // Add arguments.
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000410 CallbacksDescriptor d(*factory()->arguments_string(), *args, attribs);
yangguo@chromium.org304cc332012-07-24 07:59:48 +0000411 map->AppendDescriptor(&d, witness);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000412 }
413 { // Add caller.
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000414 CallbacksDescriptor d(*factory()->caller_string(), *caller, attribs);
yangguo@chromium.org304cc332012-07-24 07:59:48 +0000415 map->AppendDescriptor(&d, witness);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000416 }
417 if (prototypeMode != DONT_ADD_PROTOTYPE) {
418 // Add prototype.
419 if (prototypeMode == ADD_WRITEABLE_PROTOTYPE) {
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000420 attribs = static_cast<PropertyAttributes>(attribs & ~READ_ONLY);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000421 }
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000422 CallbacksDescriptor d(*factory()->prototype_string(), *prototype, attribs);
yangguo@chromium.org304cc332012-07-24 07:59:48 +0000423 map->AppendDescriptor(&d, witness);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000424 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000425}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000426
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000427
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000428Handle<Map> Genesis::CreateFunctionMap(PrototypePropertyMode prototype_mode) {
danno@chromium.org160a7b02011-04-18 15:51:38 +0000429 Handle<Map> map = factory()->NewMap(JS_FUNCTION_TYPE, JSFunction::kSize);
yangguo@chromium.org304cc332012-07-24 07:59:48 +0000430 SetFunctionInstanceDescriptor(map, prototype_mode);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000431 map->set_function_with_prototype(prototype_mode != DONT_ADD_PROTOTYPE);
432 return map;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000433}
434
435
danno@chromium.org160a7b02011-04-18 15:51:38 +0000436Handle<JSFunction> Genesis::CreateEmptyFunction(Isolate* isolate) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000437 // Allocate the map for function instances. Maps are allocated first and their
438 // prototypes patched later, once empty function is created.
439
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000440 // Please note that the prototype property for function instances must be
441 // writable.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000442 Handle<Map> function_instance_map =
443 CreateFunctionMap(ADD_WRITEABLE_PROTOTYPE);
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000444 native_context()->set_function_instance_map(*function_instance_map);
kmillikin@chromium.org4111b802010-05-03 10:34:42 +0000445
446 // 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.
461 function_instance_map_writable_prototype_ =
462 CreateFunctionMap(ADD_WRITEABLE_PROTOTYPE);
463
lrn@chromium.org7516f052011-03-30 08:52:27 +0000464 Factory* factory = isolate->factory();
465 Heap* heap = isolate->heap();
466
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000467 Handle<String> object_name = Handle<String>(heap->Object_string());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000468
469 { // --- O b j e c t ---
470 Handle<JSFunction> object_fun =
lrn@chromium.org7516f052011-03-30 08:52:27 +0000471 factory->NewFunction(object_name, factory->null_value());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000472 Handle<Map> object_function_map =
lrn@chromium.org7516f052011-03-30 08:52:27 +0000473 factory->NewMap(JS_OBJECT_TYPE, JSObject::kHeaderSize);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000474 object_fun->set_initial_map(*object_function_map);
475 object_function_map->set_constructor(*object_fun);
476
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000477 native_context()->set_object_function(*object_fun);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000478
479 // Allocate a new prototype for the object function.
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000480 Handle<JSObject> prototype = factory->NewJSObject(
481 isolate->object_function(),
mstarzinger@chromium.org71fc3462013-02-27 09:34:27 +0000482 TENURED);
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000483
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000484 native_context()->set_initial_object_prototype(*prototype);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000485 SetPrototype(object_fun, prototype);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000486 }
487
488 // Allocate the empty function as the prototype for function ECMAScript
489 // 262 15.3.4.
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000490 Handle<String> empty_string =
491 factory->InternalizeOneByteString(STATIC_ASCII_VECTOR("Empty"));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000492 Handle<JSFunction> empty_function =
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000493 factory->NewFunctionWithoutPrototype(empty_string, CLASSIC_MODE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000494
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000495 // --- E m p t y ---
496 Handle<Code> code =
lrn@chromium.org7516f052011-03-30 08:52:27 +0000497 Handle<Code>(isolate->builtins()->builtin(
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000498 Builtins::kEmptyFunction));
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000499 empty_function->set_code(*code);
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000500 empty_function->shared()->set_code(*code);
jkummerow@chromium.org59297c72013-01-09 16:32:23 +0000501 Handle<String> source =
502 factory->NewStringFromOneByte(STATIC_ASCII_VECTOR("() {}"));
lrn@chromium.org7516f052011-03-30 08:52:27 +0000503 Handle<Script> script = factory->NewScript(source);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000504 script->set_type(Smi::FromInt(Script::TYPE_NATIVE));
505 empty_function->shared()->set_script(*script);
506 empty_function->shared()->set_start_position(0);
507 empty_function->shared()->set_end_position(source->length());
508 empty_function->shared()->DontAdaptArguments();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000509
510 // Set prototypes for the function maps.
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000511 native_context()->function_map()->set_prototype(*empty_function);
512 native_context()->function_instance_map()->set_prototype(*empty_function);
513 native_context()->function_without_prototype_map()->
kmillikin@chromium.org4111b802010-05-03 10:34:42 +0000514 set_prototype(*empty_function);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000515 function_instance_map_writable_prototype_->set_prototype(*empty_function);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000516
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000517 // Allocate the function map first and then patch the prototype later
yangguo@chromium.org5f0b8ea2012-05-16 12:37:04 +0000518 Handle<Map> empty_function_map = CreateFunctionMap(DONT_ADD_PROTOTYPE);
519 empty_function_map->set_prototype(
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000520 native_context()->object_function()->prototype());
yangguo@chromium.org5f0b8ea2012-05-16 12:37:04 +0000521 empty_function->set_map(*empty_function_map);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000522 return empty_function;
523}
524
525
yangguo@chromium.org304cc332012-07-24 07:59:48 +0000526void Genesis::SetStrictFunctionInstanceDescriptor(
527 Handle<Map> map, PrototypePropertyMode prototypeMode) {
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000528 int size = (prototypeMode == DONT_ADD_PROTOTYPE) ? 4 : 5;
verwaest@chromium.org33e09c82012-10-10 17:07:22 +0000529 Handle<DescriptorArray> descriptors(factory()->NewDescriptorArray(0, size));
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000530 DescriptorArray::WhitenessWitness witness(*descriptors);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000531
yangguo@chromium.org304cc332012-07-24 07:59:48 +0000532 Handle<Foreign> length(factory()->NewForeign(&Accessors::FunctionLength));
533 Handle<Foreign> name(factory()->NewForeign(&Accessors::FunctionName));
534 Handle<AccessorPair> arguments(factory()->NewAccessorPair());
535 Handle<AccessorPair> caller(factory()->NewAccessorPair());
536 Handle<Foreign> prototype;
537 if (prototypeMode != DONT_ADD_PROTOTYPE) {
538 prototype = factory()->NewForeign(&Accessors::FunctionPrototype);
539 }
540 PropertyAttributes attribs = static_cast<PropertyAttributes>(
541 DONT_ENUM | DONT_DELETE);
rossberg@chromium.org89e18f52012-10-22 13:09:53 +0000542 map->set_instance_descriptors(*descriptors);
yangguo@chromium.org304cc332012-07-24 07:59:48 +0000543
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000544 { // Add length.
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000545 CallbacksDescriptor d(*factory()->length_string(), *length, attribs);
yangguo@chromium.org304cc332012-07-24 07:59:48 +0000546 map->AppendDescriptor(&d, witness);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000547 }
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000548 { // Add name.
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000549 CallbacksDescriptor d(*factory()->name_string(), *name, attribs);
yangguo@chromium.org304cc332012-07-24 07:59:48 +0000550 map->AppendDescriptor(&d, witness);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000551 }
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000552 { // Add arguments.
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000553 CallbacksDescriptor d(*factory()->arguments_string(), *arguments, attribs);
yangguo@chromium.org304cc332012-07-24 07:59:48 +0000554 map->AppendDescriptor(&d, witness);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000555 }
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000556 { // Add caller.
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000557 CallbacksDescriptor d(*factory()->caller_string(), *caller, attribs);
yangguo@chromium.org304cc332012-07-24 07:59:48 +0000558 map->AppendDescriptor(&d, witness);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000559 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000560 if (prototypeMode != DONT_ADD_PROTOTYPE) {
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000561 // Add prototype.
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000562 if (prototypeMode != ADD_WRITEABLE_PROTOTYPE) {
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000563 attribs = static_cast<PropertyAttributes>(attribs | READ_ONLY);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000564 }
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000565 CallbacksDescriptor d(*factory()->prototype_string(), *prototype, attribs);
yangguo@chromium.org304cc332012-07-24 07:59:48 +0000566 map->AppendDescriptor(&d, witness);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000567 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000568}
569
570
571// ECMAScript 5th Edition, 13.2.3
danno@chromium.org40cb8782011-05-25 07:58:50 +0000572Handle<JSFunction> Genesis::GetThrowTypeErrorFunction() {
573 if (throw_type_error_function.is_null()) {
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000574 Handle<String> name = factory()->InternalizeOneByteString(
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +0000575 STATIC_ASCII_VECTOR("ThrowTypeError"));
danno@chromium.org40cb8782011-05-25 07:58:50 +0000576 throw_type_error_function =
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000577 factory()->NewFunctionWithoutPrototype(name, CLASSIC_MODE);
danno@chromium.org40cb8782011-05-25 07:58:50 +0000578 Handle<Code> code(isolate()->builtins()->builtin(
579 Builtins::kStrictModePoisonPill));
580 throw_type_error_function->set_map(
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000581 native_context()->function_map());
danno@chromium.org40cb8782011-05-25 07:58:50 +0000582 throw_type_error_function->set_code(*code);
583 throw_type_error_function->shared()->set_code(*code);
584 throw_type_error_function->shared()->DontAdaptArguments();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000585
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000586 JSObject::PreventExtensions(throw_type_error_function);
danno@chromium.org40cb8782011-05-25 07:58:50 +0000587 }
588 return throw_type_error_function;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000589}
590
591
592Handle<Map> Genesis::CreateStrictModeFunctionMap(
593 PrototypePropertyMode prototype_mode,
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000594 Handle<JSFunction> empty_function) {
danno@chromium.org160a7b02011-04-18 15:51:38 +0000595 Handle<Map> map = factory()->NewMap(JS_FUNCTION_TYPE, JSFunction::kSize);
yangguo@chromium.org304cc332012-07-24 07:59:48 +0000596 SetStrictFunctionInstanceDescriptor(map, prototype_mode);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000597 map->set_function_with_prototype(prototype_mode != DONT_ADD_PROTOTYPE);
598 map->set_prototype(*empty_function);
599 return map;
600}
601
602
603void Genesis::CreateStrictModeFunctionMaps(Handle<JSFunction> empty) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000604 // Allocate map for the strict mode function instances.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000605 Handle<Map> strict_mode_function_instance_map =
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000606 CreateStrictModeFunctionMap(ADD_WRITEABLE_PROTOTYPE, empty);
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000607 native_context()->set_strict_mode_function_instance_map(
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000608 *strict_mode_function_instance_map);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000609
610 // Allocate map for the prototype-less strict mode instances.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000611 Handle<Map> strict_mode_function_without_prototype_map =
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000612 CreateStrictModeFunctionMap(DONT_ADD_PROTOTYPE, empty);
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000613 native_context()->set_strict_mode_function_without_prototype_map(
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000614 *strict_mode_function_without_prototype_map);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000615
616 // Allocate map for the strict mode functions. This map is temporary, used
617 // only for processing of builtins.
618 // Later the map is replaced with writable prototype map, allocated below.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000619 Handle<Map> strict_mode_function_map =
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000620 CreateStrictModeFunctionMap(ADD_READONLY_PROTOTYPE, empty);
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000621 native_context()->set_strict_mode_function_map(
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000622 *strict_mode_function_map);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000623
624 // The final map for the strict mode functions. Writeable prototype.
625 // This map is installed in MakeFunctionInstancePrototypeWritable.
626 strict_mode_function_instance_map_writable_prototype_ =
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000627 CreateStrictModeFunctionMap(ADD_WRITEABLE_PROTOTYPE, empty);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000628
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000629 // Complete the callbacks.
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000630 PoisonArgumentsAndCaller(strict_mode_function_instance_map);
631 PoisonArgumentsAndCaller(strict_mode_function_without_prototype_map);
632 PoisonArgumentsAndCaller(strict_mode_function_map);
633 PoisonArgumentsAndCaller(
634 strict_mode_function_instance_map_writable_prototype_);
635}
636
637
638static void SetAccessors(Handle<Map> map,
639 Handle<String> name,
640 Handle<JSFunction> func) {
641 DescriptorArray* descs = map->instance_descriptors();
verwaest@chromium.org06ab2ec2012-10-09 17:00:13 +0000642 int number = descs->SearchWithCache(*name, *map);
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000643 AccessorPair* accessors = AccessorPair::cast(descs->GetValue(number));
644 accessors->set_getter(*func);
645 accessors->set_setter(*func);
646}
647
648
649void Genesis::PoisonArgumentsAndCaller(Handle<Map> map) {
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000650 SetAccessors(map, factory()->arguments_string(), GetThrowTypeErrorFunction());
651 SetAccessors(map, factory()->caller_string(), GetThrowTypeErrorFunction());
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000652}
653
654
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000655static void AddToWeakNativeContextList(Context* context) {
656 ASSERT(context->IsNativeContext());
danno@chromium.org160a7b02011-04-18 15:51:38 +0000657 Heap* heap = context->GetIsolate()->heap();
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000658#ifdef DEBUG
659 { // NOLINT
660 ASSERT(context->get(Context::NEXT_CONTEXT_LINK)->IsUndefined());
661 // Check that context is not in the list yet.
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000662 for (Object* current = heap->native_contexts_list();
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000663 !current->IsUndefined();
664 current = Context::cast(current)->get(Context::NEXT_CONTEXT_LINK)) {
665 ASSERT(current != context);
666 }
667 }
668#endif
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000669 context->set(Context::NEXT_CONTEXT_LINK, heap->native_contexts_list());
670 heap->set_native_contexts_list(context);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000671}
672
673
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000674void Genesis::CreateRoots() {
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000675 // Allocate the native context FixedArray first and then patch the
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000676 // closure and extension object later (we need the empty function
677 // and the global object, but in order to create those, we need the
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000678 // native context).
jkummerow@chromium.org7bd87f02013-03-20 18:06:29 +0000679 native_context_ = factory()->NewNativeContext();
680 AddToWeakNativeContextList(*native_context());
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000681 isolate()->set_context(*native_context());
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000682
683 // Allocate the message listeners object.
684 {
685 v8::NeanderArray listeners;
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000686 native_context()->set_message_listeners(*listeners.value());
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000687 }
688}
689
690
691Handle<JSGlobalProxy> Genesis::CreateNewGlobals(
692 v8::Handle<v8::ObjectTemplate> global_template,
693 Handle<Object> global_object,
694 Handle<GlobalObject>* inner_global_out) {
695 // The argument global_template aka data is an ObjectTemplateInfo.
696 // It has a constructor pointer that points at global_constructor which is a
697 // FunctionTemplateInfo.
698 // The global_constructor is used to create or reinitialize the global_proxy.
699 // The global_constructor also has a prototype_template pointer that points at
700 // js_global_template which is an ObjectTemplateInfo.
701 // That in turn has a constructor pointer that points at
702 // js_global_constructor which is a FunctionTemplateInfo.
703 // js_global_constructor is used to make js_global_function
704 // js_global_function is used to make the new inner_global.
705 //
706 // --- G l o b a l ---
707 // Step 1: Create a fresh inner JSGlobalObject.
708 Handle<JSFunction> js_global_function;
709 Handle<ObjectTemplateInfo> js_global_template;
710 if (!global_template.IsEmpty()) {
711 // Get prototype template of the global_template.
712 Handle<ObjectTemplateInfo> data =
713 v8::Utils::OpenHandle(*global_template);
714 Handle<FunctionTemplateInfo> global_constructor =
715 Handle<FunctionTemplateInfo>(
716 FunctionTemplateInfo::cast(data->constructor()));
ulan@chromium.org09d7ab52013-02-25 15:50:35 +0000717 Handle<Object> proto_template(global_constructor->prototype_template(),
718 isolate());
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000719 if (!proto_template->IsUndefined()) {
720 js_global_template =
721 Handle<ObjectTemplateInfo>::cast(proto_template);
722 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000723 }
724
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000725 if (js_global_template.is_null()) {
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000726 Handle<String> name = Handle<String>(heap()->empty_string());
danno@chromium.org160a7b02011-04-18 15:51:38 +0000727 Handle<Code> code = Handle<Code>(isolate()->builtins()->builtin(
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000728 Builtins::kIllegal));
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000729 js_global_function =
danno@chromium.org160a7b02011-04-18 15:51:38 +0000730 factory()->NewFunction(name, JS_GLOBAL_OBJECT_TYPE,
731 JSGlobalObject::kSize, code, true);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000732 // Change the constructor property of the prototype of the
733 // hidden global function to refer to the Object function.
734 Handle<JSObject> prototype =
735 Handle<JSObject>(
736 JSObject::cast(js_global_function->instance_prototype()));
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000737 CHECK_NOT_EMPTY_HANDLE(isolate(),
738 JSObject::SetLocalPropertyIgnoreAttributes(
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000739 prototype, factory()->constructor_string(),
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000740 isolate()->object_function(), NONE));
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000741 } else {
742 Handle<FunctionTemplateInfo> js_global_constructor(
743 FunctionTemplateInfo::cast(js_global_template->constructor()));
744 js_global_function =
danno@chromium.org160a7b02011-04-18 15:51:38 +0000745 factory()->CreateApiFunction(js_global_constructor,
746 factory()->InnerGlobalObject);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000747 }
748
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000749 js_global_function->initial_map()->set_is_hidden_prototype();
erik.corry@gmail.com88767242012-08-08 14:43:45 +0000750 js_global_function->initial_map()->set_dictionary_map(true);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000751 Handle<GlobalObject> inner_global =
danno@chromium.org160a7b02011-04-18 15:51:38 +0000752 factory()->NewGlobalObject(js_global_function);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000753 if (inner_global_out != NULL) {
754 *inner_global_out = inner_global;
755 }
756
757 // Step 2: create or re-initialize the global proxy object.
758 Handle<JSFunction> global_proxy_function;
759 if (global_template.IsEmpty()) {
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000760 Handle<String> name = Handle<String>(heap()->empty_string());
danno@chromium.org160a7b02011-04-18 15:51:38 +0000761 Handle<Code> code = Handle<Code>(isolate()->builtins()->builtin(
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000762 Builtins::kIllegal));
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000763 global_proxy_function =
danno@chromium.org160a7b02011-04-18 15:51:38 +0000764 factory()->NewFunction(name, JS_GLOBAL_PROXY_TYPE,
765 JSGlobalProxy::kSize, code, true);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000766 } else {
767 Handle<ObjectTemplateInfo> data =
768 v8::Utils::OpenHandle(*global_template);
769 Handle<FunctionTemplateInfo> global_constructor(
770 FunctionTemplateInfo::cast(data->constructor()));
771 global_proxy_function =
danno@chromium.org160a7b02011-04-18 15:51:38 +0000772 factory()->CreateApiFunction(global_constructor,
773 factory()->OuterGlobalObject);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000774 }
775
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000776 Handle<String> global_name = factory()->InternalizeOneByteString(
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +0000777 STATIC_ASCII_VECTOR("global"));
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000778 global_proxy_function->shared()->set_instance_class_name(*global_name);
779 global_proxy_function->initial_map()->set_is_access_check_needed(true);
780
781 // Set global_proxy.__proto__ to js_global after ConfigureGlobalObjects
782 // Return the global proxy.
783
784 if (global_object.location() != NULL) {
785 ASSERT(global_object->IsJSGlobalProxy());
786 return ReinitializeJSGlobalProxy(
787 global_proxy_function,
788 Handle<JSGlobalProxy>::cast(global_object));
789 } else {
790 return Handle<JSGlobalProxy>::cast(
danno@chromium.org160a7b02011-04-18 15:51:38 +0000791 factory()->NewJSObject(global_proxy_function, TENURED));
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000792 }
793}
794
795
796void Genesis::HookUpGlobalProxy(Handle<GlobalObject> inner_global,
797 Handle<JSGlobalProxy> global_proxy) {
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000798 // Set the native context for the global object.
799 inner_global->set_native_context(*native_context());
yangguo@chromium.org355cfd12012-08-29 15:32:24 +0000800 inner_global->set_global_context(*native_context());
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000801 inner_global->set_global_receiver(*global_proxy);
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000802 global_proxy->set_native_context(*native_context());
803 native_context()->set_global_proxy(*global_proxy);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000804}
805
806
807void Genesis::HookUpInnerGlobal(Handle<GlobalObject> inner_global) {
808 Handle<GlobalObject> inner_global_from_snapshot(
jkummerow@chromium.org7bd87f02013-03-20 18:06:29 +0000809 GlobalObject::cast(native_context()->extension()));
810 Handle<JSBuiltinsObject> builtins_global(native_context()->builtins());
811 native_context()->set_extension(*inner_global);
812 native_context()->set_global_object(*inner_global);
813 native_context()->set_security_token(*inner_global);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000814 static const PropertyAttributes attributes =
815 static_cast<PropertyAttributes>(READ_ONLY | DONT_DELETE);
816 ForceSetProperty(builtins_global,
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000817 factory()->InternalizeOneByteString(
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +0000818 STATIC_ASCII_VECTOR("global")),
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000819 inner_global,
820 attributes);
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000821 // Set up the reference from the global object to the builtins object.
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000822 JSGlobalObject::cast(*inner_global)->set_builtins(*builtins_global);
823 TransferNamedProperties(inner_global_from_snapshot, inner_global);
824 TransferIndexedProperties(inner_global_from_snapshot, inner_global);
825}
826
827
828// This is only called if we are not using snapshots. The equivalent
829// work in the snapshot case is done in HookUpInnerGlobal.
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000830bool Genesis::InitializeGlobal(Handle<GlobalObject> inner_global,
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000831 Handle<JSFunction> empty_function) {
832 // --- G l o b a l C o n t e x t ---
833 // Use the empty function as closure (no scope info).
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000834 native_context()->set_closure(*empty_function);
835 native_context()->set_previous(NULL);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000836 // Set extension and global object.
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000837 native_context()->set_extension(*inner_global);
838 native_context()->set_global_object(*inner_global);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000839 // Security setup: Set the security token of the global object to
840 // its the inner global. This makes the security check between two
841 // different contexts fail by default even in case of global
842 // object reinitialization.
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000843 native_context()->set_security_token(*inner_global);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000844
danno@chromium.org160a7b02011-04-18 15:51:38 +0000845 Isolate* isolate = inner_global->GetIsolate();
lrn@chromium.org7516f052011-03-30 08:52:27 +0000846 Factory* factory = isolate->factory();
847 Heap* heap = isolate->heap();
848
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000849 Handle<String> object_name = Handle<String>(heap->Object_string());
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000850 CHECK_NOT_EMPTY_HANDLE(isolate,
851 JSObject::SetLocalPropertyIgnoreAttributes(
852 inner_global, object_name,
853 isolate->object_function(), DONT_ENUM));
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000854
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000855 Handle<JSObject> global = Handle<JSObject>(native_context()->global_object());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000856
857 // Install global Function object
858 InstallFunction(global, "Function", JS_FUNCTION_TYPE, JSFunction::kSize,
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000859 empty_function, Builtins::kIllegal, true); // ECMA native.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000860
861 { // --- A r r a y ---
862 Handle<JSFunction> array_function =
863 InstallFunction(global, "Array", JS_ARRAY_TYPE, JSArray::kSize,
lrn@chromium.org7516f052011-03-30 08:52:27 +0000864 isolate->initial_object_prototype(),
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000865 Builtins::kArrayCode, true);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000866 array_function->shared()->DontAdaptArguments();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000867
868 // This seems a bit hackish, but we need to make sure Array.length
869 // is 1.
870 array_function->shared()->set_length(1);
yangguo@chromium.org304cc332012-07-24 07:59:48 +0000871
verwaest@chromium.orgde64f722012-08-16 15:44:54 +0000872 Handle<Map> initial_map(array_function->initial_map());
verwaest@chromium.org33e09c82012-10-10 17:07:22 +0000873 Handle<DescriptorArray> array_descriptors(
874 factory->NewDescriptorArray(0, 1));
yangguo@chromium.org304cc332012-07-24 07:59:48 +0000875 DescriptorArray::WhitenessWitness witness(*array_descriptors);
876
877 Handle<Foreign> array_length(factory->NewForeign(&Accessors::ArrayLength));
878 PropertyAttributes attribs = static_cast<PropertyAttributes>(
879 DONT_ENUM | DONT_DELETE);
rossberg@chromium.org89e18f52012-10-22 13:09:53 +0000880 initial_map->set_instance_descriptors(*array_descriptors);
yangguo@chromium.org304cc332012-07-24 07:59:48 +0000881
882 { // Add length.
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000883 CallbacksDescriptor d(*factory->length_string(), *array_length, attribs);
yangguo@chromium.org304cc332012-07-24 07:59:48 +0000884 array_function->initial_map()->AppendDescriptor(&d, witness);
885 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000886
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000887 // array_function is used internally. JS code creating array object should
888 // search for the 'Array' property on the global object and use that one
889 // as the constructor. 'Array' property on a global object can be
890 // overwritten by JS code.
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000891 native_context()->set_array_function(*array_function);
danno@chromium.orgca29dd82013-04-26 11:59:48 +0000892
893 if (FLAG_optimize_constructed_arrays) {
894 // 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);
899 } else {
900 array_function->shared()->set_construct_stub(
901 isolate->builtins()->builtin(Builtins::kCommonArrayConstructCode));
902 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000903 }
904
905 { // --- N u m b e r ---
906 Handle<JSFunction> number_fun =
907 InstallFunction(global, "Number", JS_VALUE_TYPE, JSValue::kSize,
lrn@chromium.org7516f052011-03-30 08:52:27 +0000908 isolate->initial_object_prototype(),
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000909 Builtins::kIllegal, true);
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000910 native_context()->set_number_function(*number_fun);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000911 }
912
913 { // --- B o o l e a n ---
914 Handle<JSFunction> boolean_fun =
915 InstallFunction(global, "Boolean", JS_VALUE_TYPE, JSValue::kSize,
lrn@chromium.org7516f052011-03-30 08:52:27 +0000916 isolate->initial_object_prototype(),
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000917 Builtins::kIllegal, true);
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000918 native_context()->set_boolean_function(*boolean_fun);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000919 }
920
921 { // --- S t r i n g ---
922 Handle<JSFunction> string_fun =
923 InstallFunction(global, "String", JS_VALUE_TYPE, JSValue::kSize,
lrn@chromium.org7516f052011-03-30 08:52:27 +0000924 isolate->initial_object_prototype(),
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000925 Builtins::kIllegal, true);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000926 string_fun->shared()->set_construct_stub(
lrn@chromium.org7516f052011-03-30 08:52:27 +0000927 isolate->builtins()->builtin(Builtins::kStringConstructCode));
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000928 native_context()->set_string_function(*string_fun);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000929
930 Handle<Map> string_map =
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000931 Handle<Map>(native_context()->string_function()->initial_map());
verwaest@chromium.org33e09c82012-10-10 17:07:22 +0000932 Handle<DescriptorArray> string_descriptors(
933 factory->NewDescriptorArray(0, 1));
yangguo@chromium.org304cc332012-07-24 07:59:48 +0000934 DescriptorArray::WhitenessWitness witness(*string_descriptors);
935
936 Handle<Foreign> string_length(
937 factory->NewForeign(&Accessors::StringLength));
938 PropertyAttributes attribs = static_cast<PropertyAttributes>(
939 DONT_ENUM | DONT_DELETE | READ_ONLY);
rossberg@chromium.org89e18f52012-10-22 13:09:53 +0000940 string_map->set_instance_descriptors(*string_descriptors);
yangguo@chromium.org304cc332012-07-24 07:59:48 +0000941
942 { // Add length.
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000943 CallbacksDescriptor d(*factory->length_string(), *string_length, attribs);
yangguo@chromium.org304cc332012-07-24 07:59:48 +0000944 string_map->AppendDescriptor(&d, witness);
945 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000946 }
947
948 { // --- D a t e ---
949 // Builtin functions for Date.prototype.
950 Handle<JSFunction> date_fun =
svenpanne@chromium.org4efbdb12012-03-12 08:18:42 +0000951 InstallFunction(global, "Date", JS_DATE_TYPE, JSDate::kSize,
lrn@chromium.org7516f052011-03-30 08:52:27 +0000952 isolate->initial_object_prototype(),
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000953 Builtins::kIllegal, true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000954
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000955 native_context()->set_date_function(*date_fun);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000956 }
957
958
959 { // -- R e g E x p
960 // Builtin functions for RegExp.prototype.
961 Handle<JSFunction> regexp_fun =
ager@chromium.org236ad962008-09-25 09:45:57 +0000962 InstallFunction(global, "RegExp", JS_REGEXP_TYPE, JSRegExp::kSize,
lrn@chromium.org7516f052011-03-30 08:52:27 +0000963 isolate->initial_object_prototype(),
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000964 Builtins::kIllegal, true);
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000965 native_context()->set_regexp_function(*regexp_fun);
lrn@chromium.org25156de2010-04-06 13:10:27 +0000966
967 ASSERT(regexp_fun->has_initial_map());
968 Handle<Map> initial_map(regexp_fun->initial_map());
969
970 ASSERT_EQ(0, initial_map->inobject_properties());
971
lrn@chromium.org25156de2010-04-06 13:10:27 +0000972 PropertyAttributes final =
973 static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE | READ_ONLY);
verwaest@chromium.org33e09c82012-10-10 17:07:22 +0000974 Handle<DescriptorArray> descriptors = factory->NewDescriptorArray(0, 5);
yangguo@chromium.org304cc332012-07-24 07:59:48 +0000975 DescriptorArray::WhitenessWitness witness(*descriptors);
rossberg@chromium.org89e18f52012-10-22 13:09:53 +0000976 initial_map->set_instance_descriptors(*descriptors);
yangguo@chromium.org304cc332012-07-24 07:59:48 +0000977
lrn@chromium.org25156de2010-04-06 13:10:27 +0000978 {
979 // ECMA-262, section 15.10.7.1.
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000980 FieldDescriptor field(heap->source_string(),
lrn@chromium.org25156de2010-04-06 13:10:27 +0000981 JSRegExp::kSourceFieldIndex,
danno@chromium.orgf005df62013-04-30 16:36:45 +0000982 final,
983 Representation::Tagged());
yangguo@chromium.org304cc332012-07-24 07:59:48 +0000984 initial_map->AppendDescriptor(&field, witness);
lrn@chromium.org25156de2010-04-06 13:10:27 +0000985 }
986 {
987 // ECMA-262, section 15.10.7.2.
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000988 FieldDescriptor field(heap->global_string(),
lrn@chromium.org25156de2010-04-06 13:10:27 +0000989 JSRegExp::kGlobalFieldIndex,
danno@chromium.orgf005df62013-04-30 16:36:45 +0000990 final,
991 Representation::Tagged());
yangguo@chromium.org304cc332012-07-24 07:59:48 +0000992 initial_map->AppendDescriptor(&field, witness);
lrn@chromium.org25156de2010-04-06 13:10:27 +0000993 }
994 {
995 // ECMA-262, section 15.10.7.3.
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000996 FieldDescriptor field(heap->ignore_case_string(),
lrn@chromium.org25156de2010-04-06 13:10:27 +0000997 JSRegExp::kIgnoreCaseFieldIndex,
danno@chromium.orgf005df62013-04-30 16:36:45 +0000998 final,
999 Representation::Tagged());
yangguo@chromium.org304cc332012-07-24 07:59:48 +00001000 initial_map->AppendDescriptor(&field, witness);
lrn@chromium.org25156de2010-04-06 13:10:27 +00001001 }
1002 {
1003 // ECMA-262, section 15.10.7.4.
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001004 FieldDescriptor field(heap->multiline_string(),
lrn@chromium.org25156de2010-04-06 13:10:27 +00001005 JSRegExp::kMultilineFieldIndex,
danno@chromium.orgf005df62013-04-30 16:36:45 +00001006 final,
1007 Representation::Tagged());
yangguo@chromium.org304cc332012-07-24 07:59:48 +00001008 initial_map->AppendDescriptor(&field, witness);
lrn@chromium.org25156de2010-04-06 13:10:27 +00001009 }
1010 {
1011 // ECMA-262, section 15.10.7.5.
1012 PropertyAttributes writable =
1013 static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE);
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001014 FieldDescriptor field(heap->last_index_string(),
lrn@chromium.org25156de2010-04-06 13:10:27 +00001015 JSRegExp::kLastIndexFieldIndex,
danno@chromium.orgf005df62013-04-30 16:36:45 +00001016 writable,
1017 Representation::Tagged());
yangguo@chromium.org304cc332012-07-24 07:59:48 +00001018 initial_map->AppendDescriptor(&field, witness);
lrn@chromium.org25156de2010-04-06 13:10:27 +00001019 }
lrn@chromium.org25156de2010-04-06 13:10:27 +00001020
1021 initial_map->set_inobject_properties(5);
1022 initial_map->set_pre_allocated_property_fields(5);
1023 initial_map->set_unused_property_fields(0);
1024 initial_map->set_instance_size(
1025 initial_map->instance_size() + 5 * kPointerSize);
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00001026 initial_map->set_visitor_id(StaticVisitorBase::GetVisitorId(*initial_map));
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001027
1028 // RegExp prototype object is itself a RegExp.
verwaest@chromium.org753aee42012-07-17 16:15:42 +00001029 Handle<Map> proto_map = factory->CopyMap(initial_map);
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001030 proto_map->set_prototype(native_context()->initial_object_prototype());
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001031 Handle<JSObject> proto = factory->NewJSObjectFromMap(proto_map);
1032 proto->InObjectPropertyAtPut(JSRegExp::kSourceFieldIndex,
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001033 heap->query_colon_string());
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001034 proto->InObjectPropertyAtPut(JSRegExp::kGlobalFieldIndex,
1035 heap->false_value());
1036 proto->InObjectPropertyAtPut(JSRegExp::kIgnoreCaseFieldIndex,
1037 heap->false_value());
1038 proto->InObjectPropertyAtPut(JSRegExp::kMultilineFieldIndex,
1039 heap->false_value());
1040 proto->InObjectPropertyAtPut(JSRegExp::kLastIndexFieldIndex,
1041 Smi::FromInt(0),
1042 SKIP_WRITE_BARRIER); // It's a Smi.
1043 initial_map->set_prototype(*proto);
1044 factory->SetRegExpIrregexpData(Handle<JSRegExp>::cast(proto),
1045 JSRegExp::IRREGEXP, factory->empty_string(),
1046 JSRegExp::Flags(0), 0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001047 }
1048
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00001049 { // -- J S O N
lrn@chromium.org7516f052011-03-30 08:52:27 +00001050 Handle<String> name = factory->NewStringFromAscii(CStrVector("JSON"));
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001051 Handle<JSFunction> cons = factory->NewFunction(name,
1052 factory->the_hole_value());
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +00001053 { MaybeObject* result = cons->SetInstancePrototype(
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001054 native_context()->initial_object_prototype());
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +00001055 if (result->IsFailure()) return false;
1056 }
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00001057 cons->SetInstanceClassName(*name);
lrn@chromium.org7516f052011-03-30 08:52:27 +00001058 Handle<JSObject> json_object = factory->NewJSObject(cons, TENURED);
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00001059 ASSERT(json_object->IsJSObject());
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001060 CHECK_NOT_EMPTY_HANDLE(isolate,
1061 JSObject::SetLocalPropertyIgnoreAttributes(
1062 global, name, json_object, DONT_ENUM));
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001063 native_context()->set_json_object(*json_object);
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00001064 }
1065
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001066 { // --- arguments_boilerplate_
1067 // Make sure we can recognize argument objects at runtime.
1068 // This is done by introducing an anonymous function with
1069 // class_name equals 'Arguments'.
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001070 Handle<String> arguments_string = factory->InternalizeOneByteString(
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00001071 STATIC_ASCII_VECTOR("Arguments"));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001072 Handle<Code> code = Handle<Code>(
lrn@chromium.org7516f052011-03-30 08:52:27 +00001073 isolate->builtins()->builtin(Builtins::kIllegal));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001074 Handle<JSObject> prototype =
1075 Handle<JSObject>(
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001076 JSObject::cast(native_context()->object_function()->prototype()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001077
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001078 Handle<JSFunction> function =
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001079 factory->NewFunctionWithPrototype(arguments_string,
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001080 JS_OBJECT_TYPE,
1081 JSObject::kHeaderSize,
1082 prototype,
1083 code,
1084 false);
1085 ASSERT(!function->has_initial_map());
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001086 function->shared()->set_instance_class_name(*arguments_string);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001087 function->shared()->set_expected_nof_properties(2);
lrn@chromium.org7516f052011-03-30 08:52:27 +00001088 Handle<JSObject> result = factory->NewJSObject(function);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001089
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001090 native_context()->set_arguments_boilerplate(*result);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001091 // Note: length must be added as the first property and
1092 // callee must be added as the second property.
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001093 CHECK_NOT_EMPTY_HANDLE(isolate,
1094 JSObject::SetLocalPropertyIgnoreAttributes(
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001095 result, factory->length_string(),
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001096 factory->undefined_value(), DONT_ENUM));
1097 CHECK_NOT_EMPTY_HANDLE(isolate,
1098 JSObject::SetLocalPropertyIgnoreAttributes(
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001099 result, factory->callee_string(),
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001100 factory->undefined_value(), DONT_ENUM));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001101
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001102#ifdef DEBUG
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001103 LookupResult lookup(isolate);
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001104 result->LocalLookup(heap->callee_string(), &lookup);
yangguo@chromium.orgde0db002012-06-22 13:44:28 +00001105 ASSERT(lookup.IsField());
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00001106 ASSERT(lookup.GetFieldIndex().field_index() == Heap::kArgumentsCalleeIndex);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001107
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001108 result->LocalLookup(heap->length_string(), &lookup);
yangguo@chromium.orgde0db002012-06-22 13:44:28 +00001109 ASSERT(lookup.IsField());
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00001110 ASSERT(lookup.GetFieldIndex().field_index() == Heap::kArgumentsLengthIndex);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001111
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001112 ASSERT(result->map()->inobject_properties() > Heap::kArgumentsCalleeIndex);
1113 ASSERT(result->map()->inobject_properties() > Heap::kArgumentsLengthIndex);
1114
1115 // Check the state of the object.
1116 ASSERT(result->HasFastProperties());
svenpanne@chromium.org830d30c2012-05-29 13:20:14 +00001117 ASSERT(result->HasFastObjectElements());
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001118#endif
1119 }
1120
whesse@chromium.org7b260152011-06-20 15:33:18 +00001121 { // --- aliased_arguments_boilerplate_
whesse@chromium.org7b260152011-06-20 15:33:18 +00001122 // Set up a well-formed parameter map to make assertions happy.
1123 Handle<FixedArray> elements = factory->NewFixedArray(2);
1124 elements->set_map(heap->non_strict_arguments_elements_map());
1125 Handle<FixedArray> array;
1126 array = factory->NewFixedArray(0);
1127 elements->set(0, *array);
1128 array = factory->NewFixedArray(0);
1129 elements->set(1, *array);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001130
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001131 Handle<Map> old_map(native_context()->arguments_boilerplate()->map());
verwaest@chromium.org753aee42012-07-17 16:15:42 +00001132 Handle<Map> new_map = factory->CopyMap(old_map);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001133 new_map->set_pre_allocated_property_fields(2);
1134 Handle<JSObject> result = factory->NewJSObjectFromMap(new_map);
1135 // Set elements kind after allocating the object because
1136 // NewJSObjectFromMap assumes a fast elements map.
1137 new_map->set_elements_kind(NON_STRICT_ARGUMENTS_ELEMENTS);
whesse@chromium.org7b260152011-06-20 15:33:18 +00001138 result->set_elements(*elements);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001139 ASSERT(result->HasNonStrictArgumentsElements());
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001140 native_context()->set_aliased_arguments_boilerplate(*result);
whesse@chromium.org7b260152011-06-20 15:33:18 +00001141 }
1142
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001143 { // --- strict mode arguments boilerplate
1144 const PropertyAttributes attributes =
1145 static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE | READ_ONLY);
1146
1147 // Create the ThrowTypeError functions.
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001148 Handle<AccessorPair> callee = factory->NewAccessorPair();
1149 Handle<AccessorPair> caller = factory->NewAccessorPair();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001150
danno@chromium.org40cb8782011-05-25 07:58:50 +00001151 Handle<JSFunction> throw_function =
1152 GetThrowTypeErrorFunction();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001153
1154 // Install the ThrowTypeError functions.
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001155 callee->set_getter(*throw_function);
1156 callee->set_setter(*throw_function);
1157 caller->set_getter(*throw_function);
1158 caller->set_setter(*throw_function);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001159
yangguo@chromium.org304cc332012-07-24 07:59:48 +00001160 // Create the map. Allocate one in-object field for length.
1161 Handle<Map> map = factory->NewMap(JS_OBJECT_TYPE,
1162 Heap::kArgumentsObjectSizeStrict);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001163 // Create the descriptor array for the arguments object.
verwaest@chromium.org33e09c82012-10-10 17:07:22 +00001164 Handle<DescriptorArray> descriptors = factory->NewDescriptorArray(0, 3);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001165 DescriptorArray::WhitenessWitness witness(*descriptors);
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00001166 map->set_instance_descriptors(*descriptors);
yangguo@chromium.org304cc332012-07-24 07:59:48 +00001167
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001168 { // length
danno@chromium.orgf005df62013-04-30 16:36:45 +00001169 FieldDescriptor d(
1170 *factory->length_string(), 0, DONT_ENUM, Representation::Tagged());
yangguo@chromium.org304cc332012-07-24 07:59:48 +00001171 map->AppendDescriptor(&d, witness);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001172 }
1173 { // callee
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001174 CallbacksDescriptor d(*factory->callee_string(),
rossberg@chromium.org657d53b2012-07-12 11:06:03 +00001175 *callee,
jkummerow@chromium.org28583c92012-07-16 11:31:55 +00001176 attributes);
yangguo@chromium.org304cc332012-07-24 07:59:48 +00001177 map->AppendDescriptor(&d, witness);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001178 }
1179 { // caller
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001180 CallbacksDescriptor d(*factory->caller_string(),
rossberg@chromium.org657d53b2012-07-12 11:06:03 +00001181 *caller,
jkummerow@chromium.org28583c92012-07-16 11:31:55 +00001182 attributes);
yangguo@chromium.org304cc332012-07-24 07:59:48 +00001183 map->AppendDescriptor(&d, witness);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001184 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001185
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001186 map->set_function_with_prototype(true);
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001187 map->set_prototype(native_context()->object_function()->prototype());
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001188 map->set_pre_allocated_property_fields(1);
1189 map->set_inobject_properties(1);
1190
1191 // Copy constructor from the non-strict arguments boilerplate.
1192 map->set_constructor(
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001193 native_context()->arguments_boilerplate()->map()->constructor());
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001194
1195 // Allocate the arguments boilerplate object.
lrn@chromium.org7516f052011-03-30 08:52:27 +00001196 Handle<JSObject> result = factory->NewJSObjectFromMap(map);
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001197 native_context()->set_strict_mode_arguments_boilerplate(*result);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001198
1199 // Add length property only for strict mode boilerplate.
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001200 CHECK_NOT_EMPTY_HANDLE(isolate,
1201 JSObject::SetLocalPropertyIgnoreAttributes(
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001202 result, factory->length_string(),
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001203 factory->undefined_value(), DONT_ENUM));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001204
1205#ifdef DEBUG
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001206 LookupResult lookup(isolate);
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001207 result->LocalLookup(heap->length_string(), &lookup);
yangguo@chromium.orgde0db002012-06-22 13:44:28 +00001208 ASSERT(lookup.IsField());
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00001209 ASSERT(lookup.GetFieldIndex().field_index() == Heap::kArgumentsLengthIndex);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001210
1211 ASSERT(result->map()->inobject_properties() > Heap::kArgumentsLengthIndex);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001212
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001213 // Check the state of the object.
1214 ASSERT(result->HasFastProperties());
svenpanne@chromium.org830d30c2012-05-29 13:20:14 +00001215 ASSERT(result->HasFastObjectElements());
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001216#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001217 }
1218
1219 { // --- context extension
1220 // Create a function for the context extension objects.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001221 Handle<Code> code = Handle<Code>(
lrn@chromium.org7516f052011-03-30 08:52:27 +00001222 isolate->builtins()->builtin(Builtins::kIllegal));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001223 Handle<JSFunction> context_extension_fun =
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001224 factory->NewFunction(factory->empty_string(),
ager@chromium.org32912102009-01-16 10:38:43 +00001225 JS_CONTEXT_EXTENSION_OBJECT_TYPE,
1226 JSObject::kHeaderSize,
1227 code,
1228 true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001229
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001230 Handle<String> name = factory->InternalizeOneByteString(
1231 STATIC_ASCII_VECTOR("context_extension"));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001232 context_extension_fun->shared()->set_instance_class_name(*name);
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001233 native_context()->set_context_extension_function(*context_extension_fun);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001234 }
1235
sgjesse@chromium.org05521fc2009-05-21 07:37:44 +00001236
1237 {
yangguo@chromium.org80c42ed2011-08-31 09:03:56 +00001238 // Set up the call-as-function delegate.
sgjesse@chromium.org05521fc2009-05-21 07:37:44 +00001239 Handle<Code> code =
lrn@chromium.org7516f052011-03-30 08:52:27 +00001240 Handle<Code>(isolate->builtins()->builtin(
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00001241 Builtins::kHandleApiCallAsFunction));
sgjesse@chromium.org05521fc2009-05-21 07:37:44 +00001242 Handle<JSFunction> delegate =
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001243 factory->NewFunction(factory->empty_string(), JS_OBJECT_TYPE,
sgjesse@chromium.org05521fc2009-05-21 07:37:44 +00001244 JSObject::kHeaderSize, code, true);
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001245 native_context()->set_call_as_function_delegate(*delegate);
sgjesse@chromium.org05521fc2009-05-21 07:37:44 +00001246 delegate->shared()->DontAdaptArguments();
1247 }
1248
1249 {
yangguo@chromium.org80c42ed2011-08-31 09:03:56 +00001250 // Set up the call-as-constructor delegate.
sgjesse@chromium.org05521fc2009-05-21 07:37:44 +00001251 Handle<Code> code =
lrn@chromium.org7516f052011-03-30 08:52:27 +00001252 Handle<Code>(isolate->builtins()->builtin(
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00001253 Builtins::kHandleApiCallAsConstructor));
sgjesse@chromium.org05521fc2009-05-21 07:37:44 +00001254 Handle<JSFunction> delegate =
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001255 factory->NewFunction(factory->empty_string(), JS_OBJECT_TYPE,
sgjesse@chromium.org05521fc2009-05-21 07:37:44 +00001256 JSObject::kHeaderSize, code, true);
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001257 native_context()->set_call_as_constructor_delegate(*delegate);
sgjesse@chromium.org05521fc2009-05-21 07:37:44 +00001258 delegate->shared()->DontAdaptArguments();
1259 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001260
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001261 // Initialize the out of memory slot.
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001262 native_context()->set_out_of_memory(heap->false_value());
ager@chromium.org9085a012009-05-11 19:22:57 +00001263
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00001264 // Initialize the embedder data slot.
1265 Handle<FixedArray> embedder_data = factory->NewFixedArray(2);
1266 native_context()->set_embedder_data(*embedder_data);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001267
1268 {
1269 // Initialize the random seed slot.
1270 Handle<ByteArray> zeroed_byte_array(
1271 factory->NewByteArray(kRandomStateSize));
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001272 native_context()->set_random_seed(*zeroed_byte_array);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001273 memset(zeroed_byte_array->GetDataStartAddress(), 0, kRandomStateSize);
1274 }
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +00001275 return true;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001276}
1277
1278
danno@chromium.orgf005df62013-04-30 16:36:45 +00001279Handle<JSFunction> Genesis::InstallTypedArray(const char* name) {
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00001280 Handle<JSObject> global = Handle<JSObject>(native_context()->global_object());
danno@chromium.orgf005df62013-04-30 16:36:45 +00001281 return InstallFunction(global, name, JS_TYPED_ARRAY_TYPE,
1282 JSTypedArray::kSize, isolate()->initial_object_prototype(),
1283 Builtins::kIllegal, true);
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00001284}
1285
1286
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +00001287void Genesis::InitializeExperimentalGlobal() {
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001288 Handle<JSObject> global = Handle<JSObject>(native_context()->global_object());
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +00001289
1290 // TODO(mstarzinger): Move this into Genesis::InitializeGlobal once we no
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +00001291 // longer need to live behind flags, so functions get added to the snapshot.
1292
1293 if (FLAG_harmony_symbols) {
1294 // --- S y m b o l ---
1295 Handle<JSFunction> symbol_fun =
1296 InstallFunction(global, "Symbol", JS_VALUE_TYPE, JSValue::kSize,
1297 isolate()->initial_object_prototype(),
1298 Builtins::kIllegal, true);
1299 native_context()->set_symbol_function(*symbol_fun);
1300 }
1301
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001302 if (FLAG_harmony_collections) {
1303 { // -- S e t
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001304 InstallFunction(global, "Set", JS_SET_TYPE, JSSet::kSize,
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00001305 isolate()->initial_object_prototype(),
1306 Builtins::kIllegal, true);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001307 }
1308 { // -- M a p
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001309 InstallFunction(global, "Map", JS_MAP_TYPE, JSMap::kSize,
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00001310 isolate()->initial_object_prototype(),
1311 Builtins::kIllegal, true);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001312 }
1313 { // -- W e a k M a p
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001314 InstallFunction(global, "WeakMap", JS_WEAK_MAP_TYPE, JSWeakMap::kSize,
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00001315 isolate()->initial_object_prototype(),
1316 Builtins::kIllegal, true);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001317 }
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +00001318 }
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +00001319
ulan@chromium.org57ff8812013-05-10 08:16:55 +00001320 if (FLAG_harmony_array_buffer) {
1321 // -- A r r a y B u f f e r
1322 Handle<JSFunction> array_buffer_fun =
1323 InstallFunction(global, "ArrayBuffer", JS_ARRAY_BUFFER_TYPE,
1324 JSArrayBuffer::kSize,
1325 isolate()->initial_object_prototype(),
1326 Builtins::kIllegal, true);
1327 native_context()->set_array_buffer_fun(*array_buffer_fun);
1328 }
1329
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +00001330 if (FLAG_harmony_typed_arrays) {
ulan@chromium.org57ff8812013-05-10 08:16:55 +00001331 // -- T y p e d A r r a y s
1332 Handle<JSFunction> int8_fun = InstallTypedArray("Int8Array");
1333 native_context()->set_int8_array_fun(*int8_fun);
1334 Handle<JSFunction> uint8_fun = InstallTypedArray("Uint8Array");
1335 native_context()->set_uint8_array_fun(*uint8_fun);
1336 Handle<JSFunction> int16_fun = InstallTypedArray("Int16Array");
1337 native_context()->set_int16_array_fun(*int16_fun);
1338 Handle<JSFunction> uint16_fun = InstallTypedArray("Uint16Array");
1339 native_context()->set_uint16_array_fun(*uint16_fun);
1340 Handle<JSFunction> int32_fun = InstallTypedArray("Int32Array");
1341 native_context()->set_int32_array_fun(*int32_fun);
1342 Handle<JSFunction> uint32_fun = InstallTypedArray("Uint32Array");
1343 native_context()->set_uint32_array_fun(*uint32_fun);
1344 Handle<JSFunction> float_fun = InstallTypedArray("Float32Array");
1345 native_context()->set_float_array_fun(*float_fun);
1346 Handle<JSFunction> double_fun = InstallTypedArray("Float64Array");
1347 native_context()->set_double_array_fun(*double_fun);
1348 Handle<JSFunction> uint8c_fun = InstallTypedArray("Uint8ClampedArray");
1349 native_context()->set_uint8c_array_fun(*uint8c_fun);
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00001350 }
1351
1352 if (FLAG_harmony_generators) {
1353 // Create generator meta-objects and install them on the builtins object.
1354 Handle<JSObject> builtins(native_context()->builtins());
1355 Handle<JSObject> generator_object_prototype =
1356 factory()->NewJSObject(isolate()->object_function(), TENURED);
1357 Handle<JSFunction> generator_function_prototype =
1358 InstallFunction(builtins, "GeneratorFunctionPrototype",
1359 JS_FUNCTION_TYPE, JSFunction::kHeaderSize,
1360 generator_object_prototype, Builtins::kIllegal,
1361 false);
1362 InstallFunction(builtins, "GeneratorFunction",
1363 JS_FUNCTION_TYPE, JSFunction::kSize,
1364 generator_function_prototype, Builtins::kIllegal,
1365 false);
1366
1367 // Create maps for generator functions and their prototypes. Store those
1368 // maps in the native context.
1369 Handle<Map> function_map(native_context()->function_map());
1370 Handle<Map> generator_function_map = factory()->CopyMap(function_map);
1371 generator_function_map->set_prototype(*generator_function_prototype);
1372 native_context()->set_generator_function_map(*generator_function_map);
1373
1374 Handle<Map> strict_mode_function_map(
1375 native_context()->strict_mode_function_map());
1376 Handle<Map> strict_mode_generator_function_map = factory()->CopyMap(
1377 strict_mode_function_map);
1378 strict_mode_generator_function_map->set_prototype(
1379 *generator_function_prototype);
1380 native_context()->set_strict_mode_generator_function_map(
1381 *strict_mode_generator_function_map);
1382
1383 Handle<Map> object_map(native_context()->object_function()->initial_map());
1384 Handle<Map> generator_object_prototype_map = factory()->CopyMap(
1385 object_map, 0);
1386 generator_object_prototype_map->set_prototype(
1387 *generator_object_prototype);
1388 native_context()->set_generator_object_prototype_map(
1389 *generator_object_prototype_map);
ulan@chromium.org57ff8812013-05-10 08:16:55 +00001390
1391 // Create a map for generator result objects.
1392 ASSERT(object_map->inobject_properties() == 0);
1393 STATIC_ASSERT(JSGeneratorObject::kResultPropertyCount == 2);
1394 Handle<Map> generator_result_map = factory()->CopyMap(object_map,
1395 JSGeneratorObject::kResultPropertyCount);
1396 ASSERT(generator_result_map->inobject_properties() ==
1397 JSGeneratorObject::kResultPropertyCount);
1398
1399 Handle<DescriptorArray> descriptors = factory()->NewDescriptorArray(0,
1400 JSGeneratorObject::kResultPropertyCount);
1401 DescriptorArray::WhitenessWitness witness(*descriptors);
1402 generator_result_map->set_instance_descriptors(*descriptors);
1403
1404 Handle<String> value_string = factory()->InternalizeOneByteString(
1405 STATIC_ASCII_VECTOR("value"));
1406 FieldDescriptor value_descr(*value_string,
1407 JSGeneratorObject::kResultValuePropertyIndex,
1408 NONE,
1409 Representation::Tagged());
1410 generator_result_map->AppendDescriptor(&value_descr, witness);
1411
1412 Handle<String> done_string = factory()->InternalizeOneByteString(
1413 STATIC_ASCII_VECTOR("done"));
1414 FieldDescriptor done_descr(*done_string,
1415 JSGeneratorObject::kResultDonePropertyIndex,
1416 NONE,
1417 Representation::Tagged());
1418 generator_result_map->AppendDescriptor(&done_descr, witness);
1419
1420 generator_result_map->set_unused_property_fields(0);
1421 ASSERT_EQ(JSGeneratorObject::kResultSize,
1422 generator_result_map->instance_size());
1423 native_context()->set_generator_result_map(*generator_result_map);
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +00001424 }
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +00001425}
1426
1427
danno@chromium.org160a7b02011-04-18 15:51:38 +00001428bool Genesis::CompileBuiltin(Isolate* isolate, int index) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001429 Vector<const char> name = Natives::GetScriptName(index);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001430 Handle<String> source_code =
danno@chromium.org160a7b02011-04-18 15:51:38 +00001431 isolate->bootstrapper()->NativesSourceLookup(index);
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00001432 return CompileNative(isolate, name, source_code);
danno@chromium.org160a7b02011-04-18 15:51:38 +00001433}
1434
1435
1436bool Genesis::CompileExperimentalBuiltin(Isolate* isolate, int index) {
1437 Vector<const char> name = ExperimentalNatives::GetScriptName(index);
1438 Factory* factory = isolate->factory();
1439 Handle<String> source_code =
jkummerow@chromium.orge297f592011-06-08 10:05:15 +00001440 factory->NewStringFromAscii(
1441 ExperimentalNatives::GetRawScriptSource(index));
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00001442 return CompileNative(isolate, name, source_code);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001443}
1444
1445
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00001446bool Genesis::CompileNative(Isolate* isolate,
1447 Vector<const char> name,
1448 Handle<String> source) {
1449 HandleScope scope(isolate);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001450#ifdef ENABLE_DEBUGGER_SUPPORT
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001451 isolate->debugger()->set_compiling_natives(true);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001452#endif
fschneider@chromium.org7d10be52012-04-10 12:30:14 +00001453 // During genesis, the boilerplate for stack overflow won't work until the
1454 // environment has been at least partially initialized. Add a stack check
1455 // before entering JS code to catch overflow early.
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00001456 StackLimitCheck check(isolate);
fschneider@chromium.org7d10be52012-04-10 12:30:14 +00001457 if (check.HasOverflowed()) return false;
1458
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00001459 bool result = CompileScriptCached(isolate,
1460 name,
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001461 source,
1462 NULL,
1463 NULL,
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001464 Handle<Context>(isolate->context()),
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001465 true);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001466 ASSERT(isolate->has_pending_exception() != result);
1467 if (!result) isolate->clear_pending_exception();
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001468#ifdef ENABLE_DEBUGGER_SUPPORT
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001469 isolate->debugger()->set_compiling_natives(false);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001470#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001471 return result;
1472}
1473
1474
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00001475bool Genesis::CompileScriptCached(Isolate* isolate,
1476 Vector<const char> name,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001477 Handle<String> source,
1478 SourceCodeCache* cache,
1479 v8::Extension* extension,
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001480 Handle<Context> top_context,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001481 bool use_runtime_context) {
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00001482 Factory* factory = isolate->factory();
1483 HandleScope scope(isolate);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001484 Handle<SharedFunctionInfo> function_info;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001485
1486 // If we can't find the function in the cache, we compile a new
1487 // function and insert it into the cache.
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001488 if (cache == NULL || !cache->Lookup(name, &function_info)) {
ulan@chromium.org8e8d8822012-11-23 14:36:46 +00001489 ASSERT(source->IsOneByteRepresentation());
lrn@chromium.org7516f052011-03-30 08:52:27 +00001490 Handle<String> script_name = factory->NewStringFromUtf8(name);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001491 function_info = Compiler::Compile(
1492 source,
1493 script_name,
1494 0,
1495 0,
yangguo@chromium.org355cfd12012-08-29 15:32:24 +00001496 top_context,
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001497 extension,
1498 NULL,
1499 Handle<String>::null(),
1500 use_runtime_context ? NATIVES_CODE : NOT_NATIVES_CODE);
1501 if (function_info.is_null()) return false;
1502 if (cache != NULL) cache->Add(name, function_info);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001503 }
1504
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001505 // Set up the function context. Conceptually, we should clone the
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001506 // function before overwriting the context but since we're in a
1507 // single-threaded environment it is not strictly necessary.
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001508 ASSERT(top_context->IsNativeContext());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001509 Handle<Context> context =
1510 Handle<Context>(use_runtime_context
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001511 ? Handle<Context>(top_context->runtime_context())
1512 : top_context);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001513 Handle<JSFunction> fun =
lrn@chromium.org7516f052011-03-30 08:52:27 +00001514 factory->NewFunctionFromSharedFunctionInfo(function_info, context);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001515
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001516 // Call function using either the runtime object or the global
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001517 // object as the receiver. Provide no parameters.
1518 Handle<Object> receiver =
1519 Handle<Object>(use_runtime_context
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001520 ? top_context->builtins()
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00001521 : top_context->global_object(),
1522 isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001523 bool has_pending_exception;
lrn@chromium.org1c092762011-05-09 09:42:16 +00001524 Execution::Call(fun, receiver, 0, NULL, &has_pending_exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001525 if (has_pending_exception) return false;
ager@chromium.org5c838252010-02-19 08:53:10 +00001526 return true;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001527}
1528
1529
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00001530#define INSTALL_NATIVE(Type, name, var) \
1531 Handle<String> var##_name = \
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001532 factory()->InternalizeOneByteString(STATIC_ASCII_VECTOR(name)); \
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00001533 Object* var##_native = \
1534 native_context()->builtins()->GetPropertyNoExceptionThrown( \
1535 *var##_name); \
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001536 native_context()->set_##var(Type::cast(var##_native));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001537
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001538
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001539void Genesis::InstallNativeFunctions() {
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00001540 HandleScope scope(isolate());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001541 INSTALL_NATIVE(JSFunction, "CreateDate", create_date_fun);
1542 INSTALL_NATIVE(JSFunction, "ToNumber", to_number_fun);
1543 INSTALL_NATIVE(JSFunction, "ToString", to_string_fun);
1544 INSTALL_NATIVE(JSFunction, "ToDetailString", to_detail_string_fun);
1545 INSTALL_NATIVE(JSFunction, "ToObject", to_object_fun);
1546 INSTALL_NATIVE(JSFunction, "ToInteger", to_integer_fun);
1547 INSTALL_NATIVE(JSFunction, "ToUint32", to_uint32_fun);
1548 INSTALL_NATIVE(JSFunction, "ToInt32", to_int32_fun);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00001549 INSTALL_NATIVE(JSFunction, "GlobalEval", global_eval_fun);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001550 INSTALL_NATIVE(JSFunction, "Instantiate", instantiate_fun);
1551 INSTALL_NATIVE(JSFunction, "ConfigureTemplateInstance",
1552 configure_instance_fun);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001553 INSTALL_NATIVE(JSFunction, "GetStackTraceLine", get_stack_trace_line_fun);
1554 INSTALL_NATIVE(JSObject, "functionCache", function_cache);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001555 INSTALL_NATIVE(JSFunction, "ToCompletePropertyDescriptor",
1556 to_complete_property_descriptor);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001557}
1558
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001559void Genesis::InstallExperimentalNativeFunctions() {
1560 if (FLAG_harmony_proxies) {
rossberg@chromium.org717967f2011-07-20 13:44:42 +00001561 INSTALL_NATIVE(JSFunction, "DerivedHasTrap", derived_has_trap);
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001562 INSTALL_NATIVE(JSFunction, "DerivedGetTrap", derived_get_trap);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00001563 INSTALL_NATIVE(JSFunction, "DerivedSetTrap", derived_set_trap);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001564 INSTALL_NATIVE(JSFunction, "ProxyEnumerate", proxy_enumerate);
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001565 }
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +00001566 if (FLAG_harmony_observation) {
1567 INSTALL_NATIVE(JSFunction, "NotifyChange", observers_notify_change);
1568 INSTALL_NATIVE(JSFunction, "DeliverChangeRecords",
1569 observers_deliver_changes);
1570 }
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001571}
1572
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001573#undef INSTALL_NATIVE
1574
1575
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00001576Handle<JSFunction> Genesis::InstallInternalArray(
1577 Handle<JSBuiltinsObject> builtins,
1578 const char* name,
1579 ElementsKind elements_kind) {
1580 // --- I n t e r n a l A r r a y ---
1581 // An array constructor on the builtins object that works like
1582 // the public Array constructor, except that its prototype
1583 // doesn't inherit from Object.prototype.
1584 // To be used only for internal work by builtins. Instances
1585 // must not be leaked to user code.
1586 Handle<JSFunction> array_function =
1587 InstallFunction(builtins,
1588 name,
1589 JS_ARRAY_TYPE,
1590 JSArray::kSize,
1591 isolate()->initial_object_prototype(),
1592 Builtins::kInternalArrayCode,
1593 true);
1594 Handle<JSObject> prototype =
1595 factory()->NewJSObject(isolate()->object_function(), TENURED);
1596 SetPrototype(array_function, prototype);
1597
1598 array_function->shared()->set_construct_stub(
danno@chromium.orgca29dd82013-04-26 11:59:48 +00001599 isolate()->builtins()->builtin(Builtins::kCommonArrayConstructCode));
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001600
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00001601 array_function->shared()->DontAdaptArguments();
1602
1603 MaybeObject* maybe_map = array_function->initial_map()->Copy();
1604 Map* new_map;
1605 if (!maybe_map->To(&new_map)) return Handle<JSFunction>::null();
1606 new_map->set_elements_kind(elements_kind);
1607 array_function->set_initial_map(new_map);
1608
1609 // Make "length" magic on instances.
1610 Handle<Map> initial_map(array_function->initial_map());
1611 Handle<DescriptorArray> array_descriptors(
1612 factory()->NewDescriptorArray(0, 1));
1613 DescriptorArray::WhitenessWitness witness(*array_descriptors);
1614
1615 Handle<Foreign> array_length(factory()->NewForeign(
1616 &Accessors::ArrayLength));
1617 PropertyAttributes attribs = static_cast<PropertyAttributes>(
1618 DONT_ENUM | DONT_DELETE);
1619 initial_map->set_instance_descriptors(*array_descriptors);
1620
1621 { // Add length.
1622 CallbacksDescriptor d(
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001623 *factory()->length_string(), *array_length, attribs);
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00001624 array_function->initial_map()->AppendDescriptor(&d, witness);
1625 }
1626
1627 return array_function;
1628}
1629
1630
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001631bool Genesis::InstallNatives() {
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00001632 HandleScope scope(isolate());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001633
1634 // Create a function for the builtins object. Allocate space for the
1635 // JavaScript builtins, a reference to the builtins object
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001636 // (itself) and a reference to the native_context directly in the object.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001637 Handle<Code> code = Handle<Code>(
danno@chromium.org160a7b02011-04-18 15:51:38 +00001638 isolate()->builtins()->builtin(Builtins::kIllegal));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001639 Handle<JSFunction> builtins_fun =
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001640 factory()->NewFunction(factory()->empty_string(),
danno@chromium.org160a7b02011-04-18 15:51:38 +00001641 JS_BUILTINS_OBJECT_TYPE,
1642 JSBuiltinsObject::kSize, code, true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001643
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00001644 Handle<String> name =
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001645 factory()->InternalizeOneByteString(STATIC_ASCII_VECTOR("builtins"));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001646 builtins_fun->shared()->set_instance_class_name(*name);
erik.corry@gmail.com88767242012-08-08 14:43:45 +00001647 builtins_fun->initial_map()->set_dictionary_map(true);
verwaest@chromium.orgde64f722012-08-16 15:44:54 +00001648 builtins_fun->initial_map()->set_prototype(heap()->null_value());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001649
1650 // Allocate the builtins object.
1651 Handle<JSBuiltinsObject> builtins =
danno@chromium.org160a7b02011-04-18 15:51:38 +00001652 Handle<JSBuiltinsObject>::cast(factory()->NewGlobalObject(builtins_fun));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001653 builtins->set_builtins(*builtins);
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001654 builtins->set_native_context(*native_context());
yangguo@chromium.org355cfd12012-08-29 15:32:24 +00001655 builtins->set_global_context(*native_context());
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001656 builtins->set_global_receiver(*builtins);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001657
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001658 // Set up the 'global' properties of the builtins object. The
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001659 // 'global' property that refers to the global object is the only
1660 // way to get from code running in the builtins context to the
1661 // global object.
1662 static const PropertyAttributes attributes =
1663 static_cast<PropertyAttributes>(READ_ONLY | DONT_DELETE);
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001664 Handle<String> global_string =
1665 factory()->InternalizeOneByteString(STATIC_ASCII_VECTOR("global"));
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00001666 Handle<Object> global_obj(native_context()->global_object(), isolate());
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001667 CHECK_NOT_EMPTY_HANDLE(isolate(),
1668 JSObject::SetLocalPropertyIgnoreAttributes(
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001669 builtins, global_string, global_obj, attributes));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001670
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001671 // Set up the reference from the global object to the builtins object.
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001672 JSGlobalObject::cast(native_context()->global_object())->
1673 set_builtins(*builtins);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001674
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001675 // Create a bridge function that has context in the native context.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001676 Handle<JSFunction> bridge =
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001677 factory()->NewFunction(factory()->empty_string(),
danno@chromium.org160a7b02011-04-18 15:51:38 +00001678 factory()->undefined_value());
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001679 ASSERT(bridge->context() == *isolate()->native_context());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001680
1681 // Allocate the builtins context.
1682 Handle<Context> context =
danno@chromium.org160a7b02011-04-18 15:51:38 +00001683 factory()->NewFunctionContext(Context::MIN_CONTEXT_SLOTS, bridge);
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001684 context->set_global_object(*builtins); // override builtins global object
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001685
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001686 native_context()->set_runtime_context(*context);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001687
1688 { // -- S c r i p t
1689 // Builtin functions for Script.
1690 Handle<JSFunction> script_fun =
1691 InstallFunction(builtins, "Script", JS_VALUE_TYPE, JSValue::kSize,
danno@chromium.org160a7b02011-04-18 15:51:38 +00001692 isolate()->initial_object_prototype(),
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00001693 Builtins::kIllegal, false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001694 Handle<JSObject> prototype =
danno@chromium.org160a7b02011-04-18 15:51:38 +00001695 factory()->NewJSObject(isolate()->object_function(), TENURED);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001696 SetPrototype(script_fun, prototype);
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001697 native_context()->set_script_function(*script_fun);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001698
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001699 Handle<Map> script_map = Handle<Map>(script_fun->initial_map());
yangguo@chromium.org304cc332012-07-24 07:59:48 +00001700
1701 Handle<DescriptorArray> script_descriptors(
verwaest@chromium.org33e09c82012-10-10 17:07:22 +00001702 factory()->NewDescriptorArray(0, 13));
yangguo@chromium.org304cc332012-07-24 07:59:48 +00001703 DescriptorArray::WhitenessWitness witness(*script_descriptors);
1704
1705 Handle<Foreign> script_source(
1706 factory()->NewForeign(&Accessors::ScriptSource));
1707 Handle<Foreign> script_name(factory()->NewForeign(&Accessors::ScriptName));
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001708 Handle<String> id_string(factory()->InternalizeOneByteString(
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00001709 STATIC_ASCII_VECTOR("id")));
yangguo@chromium.org304cc332012-07-24 07:59:48 +00001710 Handle<Foreign> script_id(factory()->NewForeign(&Accessors::ScriptId));
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001711 Handle<String> line_offset_string(
1712 factory()->InternalizeOneByteString(
1713 STATIC_ASCII_VECTOR("line_offset")));
yangguo@chromium.org304cc332012-07-24 07:59:48 +00001714 Handle<Foreign> script_line_offset(
1715 factory()->NewForeign(&Accessors::ScriptLineOffset));
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001716 Handle<String> column_offset_string(
1717 factory()->InternalizeOneByteString(
1718 STATIC_ASCII_VECTOR("column_offset")));
yangguo@chromium.org304cc332012-07-24 07:59:48 +00001719 Handle<Foreign> script_column_offset(
1720 factory()->NewForeign(&Accessors::ScriptColumnOffset));
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001721 Handle<String> data_string(factory()->InternalizeOneByteString(
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00001722 STATIC_ASCII_VECTOR("data")));
yangguo@chromium.org304cc332012-07-24 07:59:48 +00001723 Handle<Foreign> script_data(factory()->NewForeign(&Accessors::ScriptData));
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001724 Handle<String> type_string(factory()->InternalizeOneByteString(
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00001725 STATIC_ASCII_VECTOR("type")));
yangguo@chromium.org304cc332012-07-24 07:59:48 +00001726 Handle<Foreign> script_type(factory()->NewForeign(&Accessors::ScriptType));
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001727 Handle<String> compilation_type_string(
1728 factory()->InternalizeOneByteString(
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00001729 STATIC_ASCII_VECTOR("compilation_type")));
yangguo@chromium.org304cc332012-07-24 07:59:48 +00001730 Handle<Foreign> script_compilation_type(
1731 factory()->NewForeign(&Accessors::ScriptCompilationType));
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001732 Handle<String> line_ends_string(factory()->InternalizeOneByteString(
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00001733 STATIC_ASCII_VECTOR("line_ends")));
yangguo@chromium.org304cc332012-07-24 07:59:48 +00001734 Handle<Foreign> script_line_ends(
1735 factory()->NewForeign(&Accessors::ScriptLineEnds));
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001736 Handle<String> context_data_string(
1737 factory()->InternalizeOneByteString(
1738 STATIC_ASCII_VECTOR("context_data")));
yangguo@chromium.org304cc332012-07-24 07:59:48 +00001739 Handle<Foreign> script_context_data(
1740 factory()->NewForeign(&Accessors::ScriptContextData));
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001741 Handle<String> eval_from_script_string(
1742 factory()->InternalizeOneByteString(
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00001743 STATIC_ASCII_VECTOR("eval_from_script")));
yangguo@chromium.org304cc332012-07-24 07:59:48 +00001744 Handle<Foreign> script_eval_from_script(
1745 factory()->NewForeign(&Accessors::ScriptEvalFromScript));
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001746 Handle<String> eval_from_script_position_string(
1747 factory()->InternalizeOneByteString(
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00001748 STATIC_ASCII_VECTOR("eval_from_script_position")));
yangguo@chromium.org304cc332012-07-24 07:59:48 +00001749 Handle<Foreign> script_eval_from_script_position(
1750 factory()->NewForeign(&Accessors::ScriptEvalFromScriptPosition));
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001751 Handle<String> eval_from_function_name_string(
1752 factory()->InternalizeOneByteString(
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00001753 STATIC_ASCII_VECTOR("eval_from_function_name")));
yangguo@chromium.org304cc332012-07-24 07:59:48 +00001754 Handle<Foreign> script_eval_from_function_name(
1755 factory()->NewForeign(&Accessors::ScriptEvalFromFunctionName));
1756 PropertyAttributes attribs =
1757 static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE | READ_ONLY);
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00001758 script_map->set_instance_descriptors(*script_descriptors);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001759
yangguo@chromium.org304cc332012-07-24 07:59:48 +00001760 {
1761 CallbacksDescriptor d(
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001762 *factory()->source_string(), *script_source, attribs);
yangguo@chromium.org304cc332012-07-24 07:59:48 +00001763 script_map->AppendDescriptor(&d, witness);
1764 }
1765
1766 {
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001767 CallbacksDescriptor d(*factory()->name_string(), *script_name, attribs);
yangguo@chromium.org304cc332012-07-24 07:59:48 +00001768 script_map->AppendDescriptor(&d, witness);
1769 }
1770
1771 {
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001772 CallbacksDescriptor d(*id_string, *script_id, attribs);
yangguo@chromium.org304cc332012-07-24 07:59:48 +00001773 script_map->AppendDescriptor(&d, witness);
1774 }
1775
1776 {
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001777 CallbacksDescriptor d(*line_offset_string, *script_line_offset, attribs);
yangguo@chromium.org304cc332012-07-24 07:59:48 +00001778 script_map->AppendDescriptor(&d, witness);
1779 }
1780
1781 {
1782 CallbacksDescriptor d(
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001783 *column_offset_string, *script_column_offset, attribs);
yangguo@chromium.org304cc332012-07-24 07:59:48 +00001784 script_map->AppendDescriptor(&d, witness);
1785 }
1786
1787 {
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001788 CallbacksDescriptor d(*data_string, *script_data, attribs);
yangguo@chromium.org304cc332012-07-24 07:59:48 +00001789 script_map->AppendDescriptor(&d, witness);
1790 }
1791
1792 {
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001793 CallbacksDescriptor d(*type_string, *script_type, attribs);
yangguo@chromium.org304cc332012-07-24 07:59:48 +00001794 script_map->AppendDescriptor(&d, witness);
1795 }
1796
1797 {
1798 CallbacksDescriptor d(
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001799 *compilation_type_string, *script_compilation_type, attribs);
yangguo@chromium.org304cc332012-07-24 07:59:48 +00001800 script_map->AppendDescriptor(&d, witness);
1801 }
1802
1803 {
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001804 CallbacksDescriptor d(*line_ends_string, *script_line_ends, attribs);
yangguo@chromium.org304cc332012-07-24 07:59:48 +00001805 script_map->AppendDescriptor(&d, witness);
1806 }
1807
1808 {
1809 CallbacksDescriptor d(
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001810 *context_data_string, *script_context_data, attribs);
yangguo@chromium.org304cc332012-07-24 07:59:48 +00001811 script_map->AppendDescriptor(&d, witness);
1812 }
1813
1814 {
1815 CallbacksDescriptor d(
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001816 *eval_from_script_string, *script_eval_from_script, attribs);
yangguo@chromium.org304cc332012-07-24 07:59:48 +00001817 script_map->AppendDescriptor(&d, witness);
1818 }
1819
1820 {
1821 CallbacksDescriptor d(
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001822 *eval_from_script_position_string,
yangguo@chromium.org304cc332012-07-24 07:59:48 +00001823 *script_eval_from_script_position,
1824 attribs);
1825 script_map->AppendDescriptor(&d, witness);
1826 }
1827
1828 {
1829 CallbacksDescriptor d(
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001830 *eval_from_function_name_string,
yangguo@chromium.org304cc332012-07-24 07:59:48 +00001831 *script_eval_from_function_name,
1832 attribs);
1833 script_map->AppendDescriptor(&d, witness);
1834 }
1835
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001836 // Allocate the empty script.
danno@chromium.org160a7b02011-04-18 15:51:38 +00001837 Handle<Script> script = factory()->NewScript(factory()->empty_string());
ager@chromium.orge2902be2009-06-08 12:21:35 +00001838 script->set_type(Smi::FromInt(Script::TYPE_NATIVE));
danno@chromium.org160a7b02011-04-18 15:51:38 +00001839 heap()->public_set_empty_script(*script);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001840 }
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001841 {
1842 // Builtin function for OpaqueReference -- a JSValue-based object,
1843 // that keeps its field isolated from JavaScript code. It may store
1844 // objects, that JavaScript code may not access.
1845 Handle<JSFunction> opaque_reference_fun =
1846 InstallFunction(builtins, "OpaqueReference", JS_VALUE_TYPE,
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001847 JSValue::kSize,
danno@chromium.org160a7b02011-04-18 15:51:38 +00001848 isolate()->initial_object_prototype(),
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00001849 Builtins::kIllegal, false);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001850 Handle<JSObject> prototype =
danno@chromium.org160a7b02011-04-18 15:51:38 +00001851 factory()->NewJSObject(isolate()->object_function(), TENURED);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001852 SetPrototype(opaque_reference_fun, prototype);
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001853 native_context()->set_opaque_reference_function(*opaque_reference_fun);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001854 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001855
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00001856 // InternalArrays should not use Smi-Only array optimizations. There are too
1857 // many places in the C++ runtime code (e.g. RegEx) that assume that
1858 // elements in InternalArrays can be set to non-Smi values without going
1859 // through a common bottleneck that would make the SMI_ONLY -> FAST_ELEMENT
1860 // transition easy to trap. Moreover, they rarely are smi-only.
1861 {
karlklose@chromium.org8f806e82011-03-07 14:06:08 +00001862 Handle<JSFunction> array_function =
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00001863 InstallInternalArray(builtins, "InternalArray", FAST_HOLEY_ELEMENTS);
1864 if (array_function.is_null()) return false;
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001865 native_context()->set_internal_array_function(*array_function);
karlklose@chromium.org8f806e82011-03-07 14:06:08 +00001866 }
1867
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00001868 {
1869 Handle<JSFunction> array_function =
1870 InstallInternalArray(builtins, "InternalPackedArray", FAST_ELEMENTS);
1871 if (array_function.is_null()) return false;
1872 }
1873
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001874 if (FLAG_disable_native_files) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001875 PrintF("Warning: Running without installed natives!\n");
1876 return true;
1877 }
1878
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001879 // Install natives.
1880 for (int i = Natives::GetDebuggerCount();
1881 i < Natives::GetBuiltinsCount();
1882 i++) {
danno@chromium.org160a7b02011-04-18 15:51:38 +00001883 if (!CompileBuiltin(isolate(), i)) return false;
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001884 // TODO(ager): We really only need to install the JS builtin
1885 // functions on the builtins object after compiling and running
1886 // runtime.js.
1887 if (!InstallJSBuiltins(builtins)) return false;
1888 }
1889
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001890 InstallNativeFunctions();
1891
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00001892 // Store the map for the string prototype after the natives has been compiled
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001893 // and the String function has been set up.
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001894 Handle<JSFunction> string_function(native_context()->string_function());
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00001895 ASSERT(JSObject::cast(
1896 string_function->initial_map()->prototype())->HasFastProperties());
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001897 native_context()->set_string_function_prototype_map(
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00001898 HeapObject::cast(string_function->initial_map()->prototype())->map());
1899
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001900 // Install Function.prototype.call and apply.
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001901 { Handle<String> key = factory()->function_class_string();
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001902 Handle<JSFunction> function =
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00001903 Handle<JSFunction>::cast(
1904 GetProperty(isolate(), isolate()->global_object(), key));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001905 Handle<JSObject> proto =
1906 Handle<JSObject>(JSObject::cast(function->instance_prototype()));
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001907
1908 // Install the call and the apply functions.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001909 Handle<JSFunction> call =
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001910 InstallFunction(proto, "call", JS_OBJECT_TYPE, JSObject::kHeaderSize,
kmillikin@chromium.org4111b802010-05-03 10:34:42 +00001911 Handle<JSObject>::null(),
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00001912 Builtins::kFunctionCall,
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001913 false);
1914 Handle<JSFunction> apply =
1915 InstallFunction(proto, "apply", JS_OBJECT_TYPE, JSObject::kHeaderSize,
kmillikin@chromium.org4111b802010-05-03 10:34:42 +00001916 Handle<JSObject>::null(),
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00001917 Builtins::kFunctionApply,
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001918 false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001919
1920 // Make sure that Function.prototype.call appears to be compiled.
1921 // The code will never be called, but inline caching for call will
1922 // only work if it appears to be compiled.
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001923 call->shared()->DontAdaptArguments();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001924 ASSERT(call->is_compiled());
1925
ager@chromium.org32912102009-01-16 10:38:43 +00001926 // Set the expected parameters for apply to 2; required by builtin.
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001927 apply->shared()->set_formal_parameter_count(2);
1928
1929 // Set the lengths for the functions to satisfy ECMA-262.
1930 call->shared()->set_length(1);
1931 apply->shared()->set_length(2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001932 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001933
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001934 InstallBuiltinFunctionIds();
1935
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00001936 // Create a constructor for RegExp results (a variant of Array that
1937 // predefines the two properties index and match).
1938 {
1939 // RegExpResult initial map.
1940
1941 // Find global.Array.prototype to inherit from.
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001942 Handle<JSFunction> array_constructor(native_context()->array_function());
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00001943 Handle<JSObject> array_prototype(
1944 JSObject::cast(array_constructor->instance_prototype()));
1945
1946 // Add initial map.
1947 Handle<Map> initial_map =
danno@chromium.org160a7b02011-04-18 15:51:38 +00001948 factory()->NewMap(JS_ARRAY_TYPE, JSRegExpResult::kSize);
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00001949 initial_map->set_constructor(*array_constructor);
1950
1951 // Set prototype on map.
1952 initial_map->set_non_instance_prototype(false);
1953 initial_map->set_prototype(*array_prototype);
1954
1955 // Update map with length accessor from Array and add "index" and "input".
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00001956 Handle<DescriptorArray> reresult_descriptors =
verwaest@chromium.org33e09c82012-10-10 17:07:22 +00001957 factory()->NewDescriptorArray(0, 3);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001958 DescriptorArray::WhitenessWitness witness(*reresult_descriptors);
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00001959 initial_map->set_instance_descriptors(*reresult_descriptors);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001960
yangguo@chromium.org304cc332012-07-24 07:59:48 +00001961 {
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001962 JSFunction* array_function = native_context()->array_function();
yangguo@chromium.org304cc332012-07-24 07:59:48 +00001963 Handle<DescriptorArray> array_descriptors(
1964 array_function->initial_map()->instance_descriptors());
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001965 String* length = heap()->length_string();
verwaest@chromium.org06ab2ec2012-10-09 17:00:13 +00001966 int old = array_descriptors->SearchWithCache(
1967 length, array_function->initial_map());
yangguo@chromium.org304cc332012-07-24 07:59:48 +00001968 ASSERT(old != DescriptorArray::kNotFound);
1969 CallbacksDescriptor desc(length,
1970 array_descriptors->GetValue(old),
1971 array_descriptors->GetDetails(old).attributes());
1972 initial_map->AppendDescriptor(&desc, witness);
1973 }
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00001974 {
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001975 FieldDescriptor index_field(heap()->index_string(),
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00001976 JSRegExpResult::kIndexIndex,
danno@chromium.orgf005df62013-04-30 16:36:45 +00001977 NONE,
1978 Representation::Tagged());
yangguo@chromium.org304cc332012-07-24 07:59:48 +00001979 initial_map->AppendDescriptor(&index_field, witness);
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00001980 }
1981
1982 {
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001983 FieldDescriptor input_field(heap()->input_string(),
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00001984 JSRegExpResult::kInputIndex,
danno@chromium.orgf005df62013-04-30 16:36:45 +00001985 NONE,
1986 Representation::Tagged());
yangguo@chromium.org304cc332012-07-24 07:59:48 +00001987 initial_map->AppendDescriptor(&input_field, witness);
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00001988 }
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00001989
1990 initial_map->set_inobject_properties(2);
1991 initial_map->set_pre_allocated_property_fields(2);
1992 initial_map->set_unused_property_fields(0);
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00001993
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001994 native_context()->set_regexp_result_map(*initial_map);
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00001995 }
1996
svenpanne@chromium.orgc859c4f2012-10-15 11:51:39 +00001997#ifdef VERIFY_HEAP
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001998 builtins->Verify();
1999#endif
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002000
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002001 return true;
2002}
2003
2004
danno@chromium.org160a7b02011-04-18 15:51:38 +00002005bool Genesis::InstallExperimentalNatives() {
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002006 for (int i = ExperimentalNatives::GetDebuggerCount();
2007 i < ExperimentalNatives::GetBuiltinsCount();
2008 i++) {
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00002009 if (FLAG_harmony_symbols &&
2010 strcmp(ExperimentalNatives::GetScriptName(i).start(),
2011 "native symbol.js") == 0) {
2012 if (!CompileExperimentalBuiltin(isolate(), i)) return false;
2013 }
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002014 if (FLAG_harmony_proxies &&
2015 strcmp(ExperimentalNatives::GetScriptName(i).start(),
2016 "native proxy.js") == 0) {
danno@chromium.org160a7b02011-04-18 15:51:38 +00002017 if (!CompileExperimentalBuiltin(isolate(), i)) return false;
2018 }
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002019 if (FLAG_harmony_collections &&
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +00002020 strcmp(ExperimentalNatives::GetScriptName(i).start(),
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002021 "native collection.js") == 0) {
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +00002022 if (!CompileExperimentalBuiltin(isolate(), i)) return false;
2023 }
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +00002024 if (FLAG_harmony_observation &&
danno@chromium.org72204d52012-10-31 10:02:10 +00002025 strcmp(ExperimentalNatives::GetScriptName(i).start(),
2026 "native object-observe.js") == 0) {
2027 if (!CompileExperimentalBuiltin(isolate(), i)) return false;
2028 }
ulan@chromium.org57ff8812013-05-10 08:16:55 +00002029 if (FLAG_harmony_array_buffer &&
2030 strcmp(ExperimentalNatives::GetScriptName(i).start(),
2031 "native arraybuffer.js") == 0) {
2032 if (!CompileExperimentalBuiltin(isolate(), i)) return false;
2033 }
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +00002034 if (FLAG_harmony_typed_arrays &&
2035 strcmp(ExperimentalNatives::GetScriptName(i).start(),
2036 "native typedarray.js") == 0) {
2037 if (!CompileExperimentalBuiltin(isolate(), i)) return false;
2038 }
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00002039 if (FLAG_harmony_generators &&
2040 strcmp(ExperimentalNatives::GetScriptName(i).start(),
2041 "native generator.js") == 0) {
2042 if (!CompileExperimentalBuiltin(isolate(), i)) return false;
2043 }
danno@chromium.org160a7b02011-04-18 15:51:38 +00002044 }
ager@chromium.orgea91cc52011-05-23 06:06:11 +00002045
2046 InstallExperimentalNativeFunctions();
2047
danno@chromium.org160a7b02011-04-18 15:51:38 +00002048 return true;
2049}
2050
2051
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00002052static Handle<JSObject> ResolveBuiltinIdHolder(
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00002053 Handle<Context> native_context,
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00002054 const char* holder_expr) {
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00002055 Isolate* isolate = native_context->GetIsolate();
2056 Factory* factory = isolate->factory();
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00002057 Handle<GlobalObject> global(native_context->global_object());
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00002058 const char* period_pos = strchr(holder_expr, '.');
2059 if (period_pos == NULL) {
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00002060 return Handle<JSObject>::cast(GetProperty(
2061 isolate, global, factory->InternalizeUtf8String(holder_expr)));
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00002062 }
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00002063 ASSERT_EQ(".prototype", period_pos);
2064 Vector<const char> property(holder_expr,
2065 static_cast<int>(period_pos - holder_expr));
2066 Handle<JSFunction> function = Handle<JSFunction>::cast(
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00002067 GetProperty(isolate, global, factory->InternalizeUtf8String(property)));
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00002068 return Handle<JSObject>(JSObject::cast(function->prototype()));
2069}
2070
2071
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00002072static void InstallBuiltinFunctionId(Handle<JSObject> holder,
2073 const char* function_name,
2074 BuiltinFunctionId id) {
danno@chromium.org160a7b02011-04-18 15:51:38 +00002075 Factory* factory = holder->GetIsolate()->factory();
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00002076 Handle<String> name = factory->InternalizeUtf8String(function_name);
lrn@chromium.org303ada72010-10-27 09:33:13 +00002077 Object* function_object = holder->GetProperty(*name)->ToObjectUnchecked();
2078 Handle<JSFunction> function(JSFunction::cast(function_object));
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00002079 function->shared()->set_function_data(Smi::FromInt(id));
2080}
2081
2082
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00002083void Genesis::InstallBuiltinFunctionIds() {
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00002084 HandleScope scope(isolate());
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00002085#define INSTALL_BUILTIN_ID(holder_expr, fun_name, name) \
2086 { \
2087 Handle<JSObject> holder = ResolveBuiltinIdHolder( \
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00002088 native_context(), #holder_expr); \
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00002089 BuiltinFunctionId id = k##name; \
2090 InstallBuiltinFunctionId(holder, #fun_name, id); \
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00002091 }
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00002092 FUNCTIONS_WITH_ID_LIST(INSTALL_BUILTIN_ID)
2093#undef INSTALL_BUILTIN_ID
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00002094}
2095
2096
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00002097// Do not forget to update macros.py with named constant
2098// of cache id.
2099#define JSFUNCTION_RESULT_CACHE_LIST(F) \
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00002100 F(16, native_context()->regexp_function())
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00002101
2102
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002103static FixedArray* CreateCache(int size, Handle<JSFunction> factory_function) {
danno@chromium.org160a7b02011-04-18 15:51:38 +00002104 Factory* factory = factory_function->GetIsolate()->factory();
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00002105 // Caches are supposed to live for a long time, allocate in old space.
2106 int array_size = JSFunctionResultCache::kEntriesIndex + 2 * size;
ager@chromium.orgac091b72010-05-05 07:34:42 +00002107 // Cannot use cast as object is not fully initialized yet.
2108 JSFunctionResultCache* cache = reinterpret_cast<JSFunctionResultCache*>(
danno@chromium.org160a7b02011-04-18 15:51:38 +00002109 *factory->NewFixedArrayWithHoles(array_size, TENURED));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002110 cache->set(JSFunctionResultCache::kFactoryIndex, *factory_function);
ager@chromium.orgac091b72010-05-05 07:34:42 +00002111 cache->MakeZeroSize();
2112 return cache;
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00002113}
2114
2115
2116void Genesis::InstallJSFunctionResultCaches() {
2117 const int kNumberOfCaches = 0 +
2118#define F(size, func) + 1
2119 JSFUNCTION_RESULT_CACHE_LIST(F)
2120#undef F
2121 ;
2122
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002123 Handle<FixedArray> caches = FACTORY->NewFixedArray(kNumberOfCaches, TENURED);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00002124
2125 int index = 0;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002126
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002127#define F(size, func) do { \
2128 FixedArray* cache = CreateCache((size), Handle<JSFunction>(func)); \
2129 caches->set(index++, cache); \
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002130 } while (false)
2131
2132 JSFUNCTION_RESULT_CACHE_LIST(F);
2133
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00002134#undef F
2135
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00002136 native_context()->set_jsfunction_result_caches(*caches);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00002137}
2138
2139
ricow@chromium.org65fae842010-08-25 15:26:24 +00002140void Genesis::InitializeNormalizedMapCaches() {
2141 Handle<FixedArray> array(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002142 FACTORY->NewFixedArray(NormalizedMapCache::kEntries, TENURED));
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00002143 native_context()->set_normalized_map_cache(NormalizedMapCache::cast(*array));
ricow@chromium.org65fae842010-08-25 15:26:24 +00002144}
2145
2146
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00002147bool Bootstrapper::InstallExtensions(Handle<Context> native_context,
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002148 v8::ExtensionConfiguration* extensions) {
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00002149 BootstrapperActive active(this);
2150 SaveContext saved_context(isolate_);
2151 isolate_->set_context(*native_context);
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00002152 if (!Genesis::InstallExtensions(native_context, extensions)) return false;
2153 Genesis::InstallSpecialObjects(native_context);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002154 return true;
2155}
2156
2157
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00002158void Genesis::InstallSpecialObjects(Handle<Context> native_context) {
2159 Isolate* isolate = native_context->GetIsolate();
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00002160 Factory* factory = isolate->factory();
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00002161 HandleScope scope(isolate);
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00002162 Handle<JSGlobalObject> global(JSGlobalObject::cast(
2163 native_context->global_object()));
mads.s.agercbaa0602008-08-14 13:41:48 +00002164 // Expose the natives in global if a name for it is specified.
2165 if (FLAG_expose_natives_as != NULL && strlen(FLAG_expose_natives_as) != 0) {
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00002166 Handle<String> natives =
2167 factory->InternalizeUtf8String(FLAG_expose_natives_as);
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00002168 CHECK_NOT_EMPTY_HANDLE(isolate,
2169 JSObject::SetLocalPropertyIgnoreAttributes(
2170 global, natives,
2171 Handle<JSObject>(global->builtins()),
2172 DONT_ENUM));
mads.s.agercbaa0602008-08-14 13:41:48 +00002173 }
2174
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00002175 Handle<Object> Error = GetProperty(global, "Error");
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00002176 if (Error->IsJSObject()) {
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00002177 Handle<String> name = factory->InternalizeOneByteString(
2178 STATIC_ASCII_VECTOR("stackTraceLimit"));
2179 Handle<Smi> stack_trace_limit(
2180 Smi::FromInt(FLAG_stack_trace_limit), isolate);
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00002181 CHECK_NOT_EMPTY_HANDLE(isolate,
2182 JSObject::SetLocalPropertyIgnoreAttributes(
2183 Handle<JSObject>::cast(Error), name,
2184 stack_trace_limit, NONE));
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002185 }
2186
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002187#ifdef ENABLE_DEBUGGER_SUPPORT
mads.s.agercbaa0602008-08-14 13:41:48 +00002188 // Expose the debug global object in global if a name for it is specified.
2189 if (FLAG_expose_debug_as != NULL && strlen(FLAG_expose_debug_as) != 0) {
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00002190 Debug* debug = isolate->debug();
mads.s.agercbaa0602008-08-14 13:41:48 +00002191 // If loading fails we just bail out without installing the
2192 // debugger but without tanking the whole context.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002193 if (!debug->Load()) return;
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002194 // Set the security token for the debugger context to the same as
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00002195 // the shell native context to allow calling between these (otherwise
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002196 // exposing debug global object doesn't make much sense).
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002197 debug->debug_context()->set_security_token(
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00002198 native_context->security_token());
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002199
mads.s.agercbaa0602008-08-14 13:41:48 +00002200 Handle<String> debug_string =
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00002201 factory->InternalizeUtf8String(FLAG_expose_debug_as);
2202 Handle<Object> global_proxy(
2203 debug->debug_context()->global_proxy(), isolate);
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00002204 CHECK_NOT_EMPTY_HANDLE(isolate,
2205 JSObject::SetLocalPropertyIgnoreAttributes(
2206 global, debug_string, global_proxy, DONT_ENUM));
mads.s.agercbaa0602008-08-14 13:41:48 +00002207 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002208#endif
mads.s.agercbaa0602008-08-14 13:41:48 +00002209}
2210
ricow@chromium.org27bf2882011-11-17 08:34:43 +00002211static uint32_t Hash(RegisteredExtension* extension) {
2212 return v8::internal::ComputePointerHash(extension);
2213}
2214
2215static bool MatchRegisteredExtensions(void* key1, void* key2) {
2216 return key1 == key2;
2217}
2218
2219Genesis::ExtensionStates::ExtensionStates()
yangguo@chromium.orgab30bb82012-02-24 14:41:46 +00002220 : map_(MatchRegisteredExtensions, 8) { }
ricow@chromium.org27bf2882011-11-17 08:34:43 +00002221
2222Genesis::ExtensionTraversalState Genesis::ExtensionStates::get_state(
2223 RegisteredExtension* extension) {
2224 i::HashMap::Entry* entry = map_.Lookup(extension, Hash(extension), false);
2225 if (entry == NULL) {
2226 return UNVISITED;
2227 }
2228 return static_cast<ExtensionTraversalState>(
2229 reinterpret_cast<intptr_t>(entry->value));
2230}
2231
2232void Genesis::ExtensionStates::set_state(RegisteredExtension* extension,
2233 ExtensionTraversalState state) {
2234 map_.Lookup(extension, Hash(extension), true)->value =
2235 reinterpret_cast<void*>(static_cast<intptr_t>(state));
2236}
mads.s.agercbaa0602008-08-14 13:41:48 +00002237
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00002238bool Genesis::InstallExtensions(Handle<Context> native_context,
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002239 v8::ExtensionConfiguration* extensions) {
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00002240 Isolate* isolate = native_context->GetIsolate();
ricow@chromium.org27bf2882011-11-17 08:34:43 +00002241 ExtensionStates extension_states; // All extensions have state UNVISITED.
2242 // Install auto extensions.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002243 v8::RegisteredExtension* current = v8::RegisteredExtension::first_extension();
2244 while (current != NULL) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002245 if (current->extension()->auto_enable())
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00002246 InstallExtension(isolate, current, &extension_states);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002247 current = current->next();
2248 }
2249
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00002250 if (FLAG_expose_gc) InstallExtension(isolate, "v8/gc", &extension_states);
ricow@chromium.org27bf2882011-11-17 08:34:43 +00002251 if (FLAG_expose_externalize_string) {
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00002252 InstallExtension(isolate, "v8/externalize", &extension_states);
ricow@chromium.org27bf2882011-11-17 08:34:43 +00002253 }
yangguo@chromium.org304cc332012-07-24 07:59:48 +00002254 if (FLAG_track_gc_object_stats) {
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00002255 InstallExtension(isolate, "v8/statistics", &extension_states);
yangguo@chromium.org304cc332012-07-24 07:59:48 +00002256 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002257
2258 if (extensions == NULL) return true;
2259 // Install required extensions
2260 int count = v8::ImplementationUtilities::GetNameCount(extensions);
2261 const char** names = v8::ImplementationUtilities::GetNames(extensions);
2262 for (int i = 0; i < count; i++) {
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00002263 if (!InstallExtension(isolate, names[i], &extension_states))
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002264 return false;
2265 }
2266
2267 return true;
2268}
2269
2270
2271// Installs a named extension. This methods is unoptimized and does
2272// not scale well if we want to support a large number of extensions.
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00002273bool Genesis::InstallExtension(Isolate* isolate,
2274 const char* name,
ricow@chromium.org27bf2882011-11-17 08:34:43 +00002275 ExtensionStates* extension_states) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002276 v8::RegisteredExtension* current = v8::RegisteredExtension::first_extension();
2277 // Loop until we find the relevant extension
2278 while (current != NULL) {
2279 if (strcmp(name, current->extension()->name()) == 0) break;
2280 current = current->next();
2281 }
2282 // Didn't find the extension; fail.
2283 if (current == NULL) {
2284 v8::Utils::ReportApiFailure(
2285 "v8::Context::New()", "Cannot find required extension");
2286 return false;
2287 }
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00002288 return InstallExtension(isolate, current, extension_states);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002289}
2290
2291
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00002292bool Genesis::InstallExtension(Isolate* isolate,
2293 v8::RegisteredExtension* current,
ricow@chromium.org27bf2882011-11-17 08:34:43 +00002294 ExtensionStates* extension_states) {
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00002295 HandleScope scope(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002296
ricow@chromium.org27bf2882011-11-17 08:34:43 +00002297 if (extension_states->get_state(current) == INSTALLED) return true;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002298 // The current node has already been visited so there must be a
2299 // cycle in the dependency graph; fail.
ricow@chromium.org27bf2882011-11-17 08:34:43 +00002300 if (extension_states->get_state(current) == VISITED) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002301 v8::Utils::ReportApiFailure(
2302 "v8::Context::New()", "Circular extension dependency");
2303 return false;
2304 }
ricow@chromium.org27bf2882011-11-17 08:34:43 +00002305 ASSERT(extension_states->get_state(current) == UNVISITED);
2306 extension_states->set_state(current, VISITED);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002307 v8::Extension* extension = current->extension();
2308 // Install the extension's dependencies
2309 for (int i = 0; i < extension->dependency_count(); i++) {
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00002310 if (!InstallExtension(isolate,
2311 extension->dependencies()[i],
2312 extension_states)) {
ricow@chromium.org27bf2882011-11-17 08:34:43 +00002313 return false;
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00002314 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002315 }
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002316 Handle<String> source_code =
2317 isolate->factory()->NewExternalStringFromAscii(extension->source());
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00002318 bool result = CompileScriptCached(isolate,
2319 CStrVector(extension->name()),
2320 source_code,
2321 isolate->bootstrapper()->extensions_cache(),
2322 extension,
2323 Handle<Context>(isolate->context()),
2324 false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002325 ASSERT(isolate->has_pending_exception() != result);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002326 if (!result) {
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002327 // We print out the name of the extension that fail to install.
2328 // When an error is thrown during bootstrapping we automatically print
2329 // the line number at which this happened to the console in the isolate
2330 // error throwing functionality.
2331 OS::PrintError("Error installing extension '%s'.\n",
2332 current->extension()->name());
lrn@chromium.org7516f052011-03-30 08:52:27 +00002333 isolate->clear_pending_exception();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002334 }
ricow@chromium.org27bf2882011-11-17 08:34:43 +00002335 extension_states->set_state(current, INSTALLED);
2336 isolate->NotifyExtensionInstalled();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002337 return result;
2338}
2339
2340
ager@chromium.org5c838252010-02-19 08:53:10 +00002341bool Genesis::InstallJSBuiltins(Handle<JSBuiltinsObject> builtins) {
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00002342 HandleScope scope(isolate());
ager@chromium.org5c838252010-02-19 08:53:10 +00002343 for (int i = 0; i < Builtins::NumberOfJavaScriptBuiltins(); i++) {
2344 Builtins::JavaScript id = static_cast<Builtins::JavaScript>(i);
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00002345 Handle<String> name =
2346 factory()->InternalizeUtf8String(Builtins::GetName(id));
lrn@chromium.org303ada72010-10-27 09:33:13 +00002347 Object* function_object = builtins->GetPropertyNoExceptionThrown(*name);
ager@chromium.org5c838252010-02-19 08:53:10 +00002348 Handle<JSFunction> function
lrn@chromium.org303ada72010-10-27 09:33:13 +00002349 = Handle<JSFunction>(JSFunction::cast(function_object));
ager@chromium.org5c838252010-02-19 08:53:10 +00002350 builtins->set_javascript_builtin(id, *function);
rossberg@chromium.org400388e2012-06-06 09:29:22 +00002351 if (!JSFunction::CompileLazy(function, CLEAR_EXCEPTION)) {
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002352 return false;
2353 }
rossberg@chromium.org400388e2012-06-06 09:29:22 +00002354 builtins->set_javascript_builtin_code(id, function->shared()->code());
ager@chromium.org5c838252010-02-19 08:53:10 +00002355 }
2356 return true;
2357}
2358
2359
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002360bool Genesis::ConfigureGlobalObjects(
2361 v8::Handle<v8::ObjectTemplate> global_proxy_template) {
2362 Handle<JSObject> global_proxy(
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00002363 JSObject::cast(native_context()->global_proxy()));
2364 Handle<JSObject> inner_global(
2365 JSObject::cast(native_context()->global_object()));
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002366
2367 if (!global_proxy_template.IsEmpty()) {
2368 // Configure the global proxy object.
2369 Handle<ObjectTemplateInfo> proxy_data =
2370 v8::Utils::OpenHandle(*global_proxy_template);
2371 if (!ConfigureApiObject(global_proxy, proxy_data)) return false;
2372
2373 // Configure the inner global object.
2374 Handle<FunctionTemplateInfo> proxy_constructor(
2375 FunctionTemplateInfo::cast(proxy_data->constructor()));
2376 if (!proxy_constructor->prototype_template()->IsUndefined()) {
2377 Handle<ObjectTemplateInfo> inner_data(
2378 ObjectTemplateInfo::cast(proxy_constructor->prototype_template()));
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002379 if (!ConfigureApiObject(inner_global, inner_data)) return false;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002380 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002381 }
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002382
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002383 SetObjectPrototype(global_proxy, inner_global);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002384 return true;
2385}
2386
2387
2388bool Genesis::ConfigureApiObject(Handle<JSObject> object,
2389 Handle<ObjectTemplateInfo> object_template) {
2390 ASSERT(!object_template.is_null());
2391 ASSERT(object->IsInstanceOf(
2392 FunctionTemplateInfo::cast(object_template->constructor())));
2393
2394 bool pending_exception = false;
2395 Handle<JSObject> obj =
2396 Execution::InstantiateObject(object_template, &pending_exception);
2397 if (pending_exception) {
danno@chromium.org160a7b02011-04-18 15:51:38 +00002398 ASSERT(isolate()->has_pending_exception());
2399 isolate()->clear_pending_exception();
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002400 return false;
2401 }
2402 TransferObject(obj, object);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002403 return true;
2404}
2405
2406
2407void Genesis::TransferNamedProperties(Handle<JSObject> from,
2408 Handle<JSObject> to) {
2409 if (from->HasFastProperties()) {
2410 Handle<DescriptorArray> descs =
2411 Handle<DescriptorArray>(from->map()->instance_descriptors());
ulan@chromium.org57ff8812013-05-10 08:16:55 +00002412 for (int i = 0; i < from->map()->NumberOfOwnDescriptors(); i++) {
erik.corry@gmail.comed49e962012-04-17 11:57:53 +00002413 PropertyDetails details = descs->GetDetails(i);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002414 switch (details.type()) {
2415 case FIELD: {
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00002416 HandleScope inner(isolate());
ulan@chromium.org750145a2013-03-07 15:14:13 +00002417 Handle<Name> key = Handle<Name>(descs->GetKey(i));
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002418 int index = descs->GetFieldIndex(i);
ulan@chromium.org57ff8812013-05-10 08:16:55 +00002419 ASSERT(!descs->GetDetails(i).representation().IsDouble());
2420 Handle<Object> value = Handle<Object>(from->RawFastPropertyAt(index),
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00002421 isolate());
2422 CHECK_NOT_EMPTY_HANDLE(isolate(),
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00002423 JSObject::SetLocalPropertyIgnoreAttributes(
2424 to, key, value, details.attributes()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002425 break;
2426 }
2427 case CONSTANT_FUNCTION: {
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00002428 HandleScope inner(isolate());
ulan@chromium.org750145a2013-03-07 15:14:13 +00002429 Handle<Name> key = Handle<Name>(descs->GetKey(i));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002430 Handle<JSFunction> fun =
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002431 Handle<JSFunction>(descs->GetConstantFunction(i));
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00002432 CHECK_NOT_EMPTY_HANDLE(isolate(),
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00002433 JSObject::SetLocalPropertyIgnoreAttributes(
2434 to, key, fun, details.attributes()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002435 break;
2436 }
2437 case CALLBACKS: {
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002438 LookupResult result(isolate());
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002439 to->LocalLookup(descs->GetKey(i), &result);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002440 // If the property is already there we skip it
verwaest@chromium.org753aee42012-07-17 16:15:42 +00002441 if (result.IsFound()) continue;
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00002442 HandleScope inner(isolate());
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002443 ASSERT(!to->HasFastProperties());
2444 // Add to dictionary.
ulan@chromium.org750145a2013-03-07 15:14:13 +00002445 Handle<Name> key = Handle<Name>(descs->GetKey(i));
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00002446 Handle<Object> callbacks(descs->GetCallbacksObject(i), isolate());
ulan@chromium.org57ff8812013-05-10 08:16:55 +00002447 PropertyDetails d = PropertyDetails(
2448 details.attributes(), CALLBACKS, i + 1);
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00002449 JSObject::SetNormalizedProperty(to, key, callbacks, d);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002450 break;
2451 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002452 case NORMAL:
2453 // Do not occur since the from object has fast properties.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002454 case HANDLER:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002455 case INTERCEPTOR:
yangguo@chromium.org99aa4902012-07-06 16:21:55 +00002456 case TRANSITION:
jkummerow@chromium.org7a6fc812012-06-27 11:12:38 +00002457 case NONEXISTENT:
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002458 // No element in instance descriptors have proxy or interceptor type.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002459 UNREACHABLE();
2460 break;
2461 }
2462 }
2463 } else {
ulan@chromium.org750145a2013-03-07 15:14:13 +00002464 Handle<NameDictionary> properties =
2465 Handle<NameDictionary>(from->property_dictionary());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002466 int capacity = properties->Capacity();
2467 for (int i = 0; i < capacity; i++) {
2468 Object* raw_key(properties->KeyAt(i));
2469 if (properties->IsKey(raw_key)) {
ulan@chromium.org750145a2013-03-07 15:14:13 +00002470 ASSERT(raw_key->IsName());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002471 // If the property is already there we skip it.
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002472 LookupResult result(isolate());
ulan@chromium.org750145a2013-03-07 15:14:13 +00002473 to->LocalLookup(Name::cast(raw_key), &result);
verwaest@chromium.org753aee42012-07-17 16:15:42 +00002474 if (result.IsFound()) continue;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002475 // Set the property.
ulan@chromium.org750145a2013-03-07 15:14:13 +00002476 Handle<Name> key = Handle<Name>(Name::cast(raw_key));
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00002477 Handle<Object> value = Handle<Object>(properties->ValueAt(i),
2478 isolate());
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002479 if (value->IsJSGlobalPropertyCell()) {
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00002480 value = Handle<Object>(JSGlobalPropertyCell::cast(*value)->value(),
2481 isolate());
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002482 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002483 PropertyDetails details = properties->DetailsAt(i);
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00002484 CHECK_NOT_EMPTY_HANDLE(isolate(),
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00002485 JSObject::SetLocalPropertyIgnoreAttributes(
2486 to, key, value, details.attributes()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002487 }
2488 }
2489 }
2490}
2491
2492
2493void Genesis::TransferIndexedProperties(Handle<JSObject> from,
2494 Handle<JSObject> to) {
2495 // Cloning the elements array is sufficient.
2496 Handle<FixedArray> from_elements =
2497 Handle<FixedArray>(FixedArray::cast(from->elements()));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002498 Handle<FixedArray> to_elements = FACTORY->CopyFixedArray(from_elements);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002499 to->set_elements(*to_elements);
2500}
2501
2502
2503void Genesis::TransferObject(Handle<JSObject> from, Handle<JSObject> to) {
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00002504 HandleScope outer(isolate());
2505 Factory* factory = isolate()->factory();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002506
2507 ASSERT(!from->IsJSArray());
2508 ASSERT(!to->IsJSArray());
2509
2510 TransferNamedProperties(from, to);
2511 TransferIndexedProperties(from, to);
2512
2513 // Transfer the prototype (new map is needed).
2514 Handle<Map> old_to_map = Handle<Map>(to->map());
verwaest@chromium.org753aee42012-07-17 16:15:42 +00002515 Handle<Map> new_to_map = factory->CopyMap(old_to_map);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002516 new_to_map->set_prototype(from->map()->prototype());
2517 to->set_map(*new_to_map);
2518}
2519
2520
2521void Genesis::MakeFunctionInstancePrototypeWritable() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002522 // The maps with writable prototype are created in CreateEmptyFunction
2523 // and CreateStrictModeFunctionMaps respectively. Initially the maps are
2524 // created with read-only prototype for JS builtins processing.
2525 ASSERT(!function_instance_map_writable_prototype_.is_null());
2526 ASSERT(!strict_mode_function_instance_map_writable_prototype_.is_null());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002527
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002528 // Replace function instance maps to make prototype writable.
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00002529 native_context()->set_function_map(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002530 *function_instance_map_writable_prototype_);
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00002531 native_context()->set_strict_mode_function_map(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002532 *strict_mode_function_instance_map_writable_prototype_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002533}
2534
2535
danno@chromium.org160a7b02011-04-18 15:51:38 +00002536Genesis::Genesis(Isolate* isolate,
2537 Handle<Object> global_object,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002538 v8::Handle<v8::ObjectTemplate> global_template,
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00002539 v8::ExtensionConfiguration* extensions)
2540 : isolate_(isolate),
2541 active_(isolate->bootstrapper()) {
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00002542 result_ = Handle<Context>::null();
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002543 // If V8 isn't running and cannot be initialized, just return.
2544 if (!V8::IsRunning() && !V8::Initialize(NULL)) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002545
2546 // Before creating the roots we must save the context and restore it
2547 // on all function exits.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002548 SaveContext saved_context(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002549
fschneider@chromium.org7d10be52012-04-10 12:30:14 +00002550 // During genesis, the boilerplate for stack overflow won't work until the
2551 // environment has been at least partially initialized. Add a stack check
2552 // before entering JS code to catch overflow early.
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00002553 StackLimitCheck check(isolate);
fschneider@chromium.org7d10be52012-04-10 12:30:14 +00002554 if (check.HasOverflowed()) return;
2555
jkummerow@chromium.org7bd87f02013-03-20 18:06:29 +00002556 native_context_ = Snapshot::NewContextFromSnapshot();
2557 if (!native_context().is_null()) {
2558 AddToWeakNativeContextList(*native_context());
2559 isolate->set_context(*native_context());
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002560 isolate->counters()->contexts_created_by_snapshot()->Increment();
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002561 Handle<GlobalObject> inner_global;
2562 Handle<JSGlobalProxy> global_proxy =
2563 CreateNewGlobals(global_template,
2564 global_object,
2565 &inner_global);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002566
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002567 HookUpGlobalProxy(inner_global, global_proxy);
2568 HookUpInnerGlobal(inner_global);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002569
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002570 if (!ConfigureGlobalObjects(global_template)) return;
2571 } else {
2572 // We get here if there was no context snapshot.
2573 CreateRoots();
danno@chromium.org160a7b02011-04-18 15:51:38 +00002574 Handle<JSFunction> empty_function = CreateEmptyFunction(isolate);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002575 CreateStrictModeFunctionMaps(empty_function);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002576 Handle<GlobalObject> inner_global;
2577 Handle<JSGlobalProxy> global_proxy =
2578 CreateNewGlobals(global_template, global_object, &inner_global);
2579 HookUpGlobalProxy(inner_global, global_proxy);
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +00002580 if (!InitializeGlobal(inner_global, empty_function)) return;
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00002581 InstallJSFunctionResultCaches();
ricow@chromium.org65fae842010-08-25 15:26:24 +00002582 InitializeNormalizedMapCaches();
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00002583 if (!InstallNatives()) return;
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002584
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002585 MakeFunctionInstancePrototypeWritable();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002586
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002587 if (!ConfigureGlobalObjects(global_template)) return;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002588 isolate->counters()->contexts_created_from_scratch()->Increment();
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002589 }
mads.s.agercbaa0602008-08-14 13:41:48 +00002590
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +00002591 // Initialize experimental globals and install experimental natives.
2592 InitializeExperimentalGlobal();
danno@chromium.org160a7b02011-04-18 15:51:38 +00002593 if (!InstallExperimentalNatives()) return;
2594
jkummerow@chromium.org7bd87f02013-03-20 18:06:29 +00002595 result_ = native_context();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002596}
2597
ager@chromium.orgddb913d2009-01-27 10:01:48 +00002598
2599// Support for thread preemption.
2600
2601// Reserve space for statics needing saving and restoring.
2602int Bootstrapper::ArchiveSpacePerThread() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002603 return sizeof(NestingCounterType);
ager@chromium.orgddb913d2009-01-27 10:01:48 +00002604}
2605
2606
2607// Archive statics that are thread local.
2608char* Bootstrapper::ArchiveState(char* to) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002609 *reinterpret_cast<NestingCounterType*>(to) = nesting_;
2610 nesting_ = 0;
2611 return to + sizeof(NestingCounterType);
ager@chromium.orgddb913d2009-01-27 10:01:48 +00002612}
2613
2614
2615// Restore statics that are thread local.
2616char* Bootstrapper::RestoreState(char* from) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002617 nesting_ = *reinterpret_cast<NestingCounterType*>(from);
2618 return from + sizeof(NestingCounterType);
ager@chromium.orgddb913d2009-01-27 10:01:48 +00002619}
2620
2621
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002622// Called when the top-level V8 mutex is destroyed.
2623void Bootstrapper::FreeThreadResources() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002624 ASSERT(!IsActive());
ager@chromium.orgddb913d2009-01-27 10:01:48 +00002625}
2626
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002627} } // namespace v8::internal