blob: 726670aae54a01cffe1890fd36a9dbadb988d66d [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"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000045
kasperl@chromium.org71affb52009-05-26 05:44:31 +000046namespace v8 {
47namespace internal {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000048
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000049
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000050NativesExternalStringResource::NativesExternalStringResource(
51 Bootstrapper* bootstrapper,
jkummerow@chromium.orge297f592011-06-08 10:05:15 +000052 const char* source,
53 size_t length)
54 : data_(source), length_(length) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000055 if (bootstrapper->delete_these_non_arrays_on_tear_down_ == NULL) {
56 bootstrapper->delete_these_non_arrays_on_tear_down_ = new List<char*>(2);
ager@chromium.orgc4c92722009-11-18 14:12:51 +000057 }
58 // The resources are small objects and we only make a fixed number of
59 // them, but let's clean them up on exit for neatness.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000060 bootstrapper->delete_these_non_arrays_on_tear_down_->
ager@chromium.orgc4c92722009-11-18 14:12:51 +000061 Add(reinterpret_cast<char*>(this));
62}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000063
64
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000065Bootstrapper::Bootstrapper()
66 : nesting_(0),
67 extensions_cache_(Script::TYPE_EXTENSION),
68 delete_these_non_arrays_on_tear_down_(NULL),
69 delete_these_arrays_on_tear_down_(NULL) {
70}
71
72
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000073Handle<String> Bootstrapper::NativesSourceLookup(int index) {
74 ASSERT(0 <= index && index < Natives::GetBuiltinsCount());
lrn@chromium.org7516f052011-03-30 08:52:27 +000075 Isolate* isolate = Isolate::Current();
76 Factory* factory = isolate->factory();
77 Heap* heap = isolate->heap();
78 if (heap->natives_source_cache()->get(index)->IsUndefined()) {
danno@chromium.orgfa458e42012-02-01 10:48:36 +000079 // We can use external strings for the natives.
80 Vector<const char> source = Natives::GetRawScriptSource(index);
81 NativesExternalStringResource* resource =
82 new NativesExternalStringResource(this,
83 source.start(),
84 source.length());
85 Handle<String> source_code =
86 factory->NewExternalStringFromAscii(resource);
87 heap->natives_source_cache()->set(index, *source_code);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000088 }
lrn@chromium.org7516f052011-03-30 08:52:27 +000089 Handle<Object> cached_source(heap->natives_source_cache()->get(index));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000090 return Handle<String>::cast(cached_source);
91}
92
93
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000094void Bootstrapper::Initialize(bool create_heap_objects) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000095 extensions_cache_.Initialize(create_heap_objects);
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +000096 GCExtension::Register();
97 ExternalizeStringExtension::Register();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000098}
99
100
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000101char* Bootstrapper::AllocateAutoDeletedArray(int bytes) {
102 char* memory = new char[bytes];
103 if (memory != NULL) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000104 if (delete_these_arrays_on_tear_down_ == NULL) {
105 delete_these_arrays_on_tear_down_ = new List<char*>(2);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000106 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000107 delete_these_arrays_on_tear_down_->Add(memory);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000108 }
109 return memory;
110}
111
112
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000113void Bootstrapper::TearDown() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000114 if (delete_these_non_arrays_on_tear_down_ != NULL) {
115 int len = delete_these_non_arrays_on_tear_down_->length();
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000116 ASSERT(len < 20); // Don't use this mechanism for unbounded allocations.
117 for (int i = 0; i < len; i++) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000118 delete delete_these_non_arrays_on_tear_down_->at(i);
119 delete_these_non_arrays_on_tear_down_->at(i) = NULL;
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000120 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000121 delete delete_these_non_arrays_on_tear_down_;
122 delete_these_non_arrays_on_tear_down_ = NULL;
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000123 }
124
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000125 if (delete_these_arrays_on_tear_down_ != NULL) {
126 int len = delete_these_arrays_on_tear_down_->length();
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000127 ASSERT(len < 1000); // Don't use this mechanism for unbounded allocations.
128 for (int i = 0; i < len; i++) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000129 delete[] delete_these_arrays_on_tear_down_->at(i);
130 delete_these_arrays_on_tear_down_->at(i) = NULL;
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000131 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000132 delete delete_these_arrays_on_tear_down_;
133 delete_these_arrays_on_tear_down_ = NULL;
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000134 }
135
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000136 extensions_cache_.Initialize(false); // Yes, symmetrical
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000137}
138
139
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000140class Genesis BASE_EMBEDDED {
141 public:
danno@chromium.org160a7b02011-04-18 15:51:38 +0000142 Genesis(Isolate* isolate,
143 Handle<Object> global_object,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000144 v8::Handle<v8::ObjectTemplate> global_template,
145 v8::ExtensionConfiguration* extensions);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000146 ~Genesis() { }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000147
148 Handle<Context> result() { return result_; }
149
150 Genesis* previous() { return previous_; }
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000151
danno@chromium.org160a7b02011-04-18 15:51:38 +0000152 Isolate* isolate() const { return isolate_; }
153 Factory* factory() const { return isolate_->factory(); }
154 Heap* heap() const { return isolate_->heap(); }
155
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000156 private:
157 Handle<Context> global_context_;
danno@chromium.org160a7b02011-04-18 15:51:38 +0000158 Isolate* isolate_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000159
160 // There may be more than one active genesis object: When GC is
161 // triggered during environment creation there may be weak handle
162 // processing callbacks which may create new environments.
163 Genesis* previous_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000164
165 Handle<Context> global_context() { return global_context_; }
166
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000167 // Creates some basic objects. Used for creating a context from scratch.
168 void CreateRoots();
169 // Creates the empty function. Used for creating a context from scratch.
danno@chromium.org160a7b02011-04-18 15:51:38 +0000170 Handle<JSFunction> CreateEmptyFunction(Isolate* isolate);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000171 // Creates the ThrowTypeError function. ECMA 5th Ed. 13.2.3
danno@chromium.org40cb8782011-05-25 07:58:50 +0000172 Handle<JSFunction> GetThrowTypeErrorFunction();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000173
174 void CreateStrictModeFunctionMaps(Handle<JSFunction> empty);
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000175
176 // Make the "arguments" and "caller" properties throw a TypeError on access.
177 void PoisonArgumentsAndCaller(Handle<Map> map);
178
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000179 // Creates the global objects using the global and the template passed in
180 // through the API. We call this regardless of whether we are building a
181 // context from scratch or using a deserialized one from the partial snapshot
182 // but in the latter case we don't use the objects it produces directly, as
183 // we have to used the deserialized ones that are linked together with the
184 // rest of the context snapshot.
185 Handle<JSGlobalProxy> CreateNewGlobals(
186 v8::Handle<v8::ObjectTemplate> global_template,
187 Handle<Object> global_object,
188 Handle<GlobalObject>* global_proxy_out);
189 // Hooks the given global proxy into the context. If the context was created
190 // by deserialization then this will unhook the global proxy that was
191 // deserialized, leaving the GC to pick it up.
192 void HookUpGlobalProxy(Handle<GlobalObject> inner_global,
193 Handle<JSGlobalProxy> global_proxy);
194 // Similarly, we want to use the inner global that has been created by the
195 // templates passed through the API. The inner global from the snapshot is
196 // detached from the other objects in the snapshot.
197 void HookUpInnerGlobal(Handle<GlobalObject> inner_global);
198 // New context initialization. Used for creating a context from scratch.
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000199 bool InitializeGlobal(Handle<GlobalObject> inner_global,
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000200 Handle<JSFunction> empty_function);
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +0000201 void InitializeExperimentalGlobal();
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000202 // Installs the contents of the native .js files on the global objects.
203 // Used for creating a context from scratch.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000204 void InstallNativeFunctions();
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000205 void InstallExperimentalNativeFunctions();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000206 bool InstallNatives();
danno@chromium.org160a7b02011-04-18 15:51:38 +0000207 bool InstallExperimentalNatives();
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000208 void InstallBuiltinFunctionIds();
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000209 void InstallJSFunctionResultCaches();
ricow@chromium.org65fae842010-08-25 15:26:24 +0000210 void InitializeNormalizedMapCaches();
ricow@chromium.org27bf2882011-11-17 08:34:43 +0000211
212 enum ExtensionTraversalState {
213 UNVISITED, VISITED, INSTALLED
214 };
215
216 class ExtensionStates {
yangguo@chromium.orgab30bb82012-02-24 14:41:46 +0000217 public:
ricow@chromium.org27bf2882011-11-17 08:34:43 +0000218 ExtensionStates();
219 ExtensionTraversalState get_state(RegisteredExtension* extension);
220 void set_state(RegisteredExtension* extension,
221 ExtensionTraversalState state);
yangguo@chromium.orgab30bb82012-02-24 14:41:46 +0000222 private:
ricow@chromium.org27bf2882011-11-17 08:34:43 +0000223 HashMap map_;
224 DISALLOW_COPY_AND_ASSIGN(ExtensionStates);
225 };
226
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000227 // Used both for deserialized and from-scratch contexts to add the extensions
228 // provided.
229 static bool InstallExtensions(Handle<Context> global_context,
230 v8::ExtensionConfiguration* extensions);
ricow@chromium.org27bf2882011-11-17 08:34:43 +0000231 static bool InstallExtension(const char* name,
232 ExtensionStates* extension_states);
233 static bool InstallExtension(v8::RegisteredExtension* current,
234 ExtensionStates* extension_states);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000235 static void InstallSpecialObjects(Handle<Context> global_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
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000256 Handle<DescriptorArray> ComputeFunctionInstanceDescriptor(
kmillikin@chromium.org4111b802010-05-03 10:34:42 +0000257 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
264 Handle<DescriptorArray> ComputeStrictFunctionInstanceDescriptor(
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000265 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);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000269 static bool CompileNative(Vector<const char> name, Handle<String> source);
270 static bool CompileScriptCached(Vector<const char> name,
271 Handle<String> source,
272 SourceCodeCache* cache,
273 v8::Extension* extension,
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000274 Handle<Context> top_context,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000275 bool use_runtime_context);
276
277 Handle<Context> result_;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000278
279 // Function instance maps. Function literal maps are created initially with
280 // a read only prototype for the processing of JS builtins. Later the function
281 // instance maps are replaced in order to make prototype writable.
282 // These are the final, writable prototype, maps.
283 Handle<Map> function_instance_map_writable_prototype_;
284 Handle<Map> strict_mode_function_instance_map_writable_prototype_;
danno@chromium.org40cb8782011-05-25 07:58:50 +0000285 Handle<JSFunction> throw_type_error_function;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000286
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000287 BootstrapperActive active_;
288 friend class Bootstrapper;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000289};
290
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000291
292void Bootstrapper::Iterate(ObjectVisitor* v) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000293 extensions_cache_.Iterate(v);
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +0000294 v->Synchronize(VisitorSynchronization::kExtensions);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000295}
296
297
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000298Handle<Context> Bootstrapper::CreateEnvironment(
danno@chromium.org160a7b02011-04-18 15:51:38 +0000299 Isolate* isolate,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000300 Handle<Object> global_object,
301 v8::Handle<v8::ObjectTemplate> global_template,
302 v8::ExtensionConfiguration* extensions) {
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000303 HandleScope scope;
304 Handle<Context> env;
danno@chromium.org160a7b02011-04-18 15:51:38 +0000305 Genesis genesis(isolate, global_object, global_template, extensions);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000306 env = genesis.result();
307 if (!env.is_null()) {
308 if (InstallExtensions(env, extensions)) {
309 return env;
310 }
311 }
312 return Handle<Context>();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000313}
314
315
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000316static void SetObjectPrototype(Handle<JSObject> object, Handle<Object> proto) {
317 // object.__proto__ = proto;
danno@chromium.org160a7b02011-04-18 15:51:38 +0000318 Factory* factory = object->GetIsolate()->factory();
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000319 Handle<Map> old_to_map = Handle<Map>(object->map());
verwaest@chromium.org753aee42012-07-17 16:15:42 +0000320 Handle<Map> new_to_map = factory->CopyMap(old_to_map);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000321 new_to_map->set_prototype(*proto);
322 object->set_map(*new_to_map);
323}
324
325
326void Bootstrapper::DetachGlobal(Handle<Context> env) {
danno@chromium.org160a7b02011-04-18 15:51:38 +0000327 Factory* factory = env->GetIsolate()->factory();
lrn@chromium.org7516f052011-03-30 08:52:27 +0000328 JSGlobalProxy::cast(env->global_proxy())->set_context(*factory->null_value());
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000329 SetObjectPrototype(Handle<JSObject>(env->global_proxy()),
lrn@chromium.org7516f052011-03-30 08:52:27 +0000330 factory->null_value());
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000331 env->set_global_proxy(env->global());
332 env->global()->set_global_receiver(env->global());
333}
334
335
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +0000336void Bootstrapper::ReattachGlobal(Handle<Context> env,
337 Handle<Object> global_object) {
338 ASSERT(global_object->IsJSGlobalProxy());
339 Handle<JSGlobalProxy> global = Handle<JSGlobalProxy>::cast(global_object);
340 env->global()->set_global_receiver(*global);
341 env->set_global_proxy(*global);
342 SetObjectPrototype(global, Handle<JSObject>(env->global()));
343 global->set_context(*env);
344}
345
346
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000347static Handle<JSFunction> InstallFunction(Handle<JSObject> target,
348 const char* name,
349 InstanceType type,
350 int instance_size,
351 Handle<JSObject> prototype,
352 Builtins::Name call,
353 bool is_ecma_native) {
danno@chromium.org160a7b02011-04-18 15:51:38 +0000354 Isolate* isolate = target->GetIsolate();
lrn@chromium.org7516f052011-03-30 08:52:27 +0000355 Factory* factory = isolate->factory();
356 Handle<String> symbol = factory->LookupAsciiSymbol(name);
357 Handle<Code> call_code = Handle<Code>(isolate->builtins()->builtin(call));
kmillikin@chromium.org4111b802010-05-03 10:34:42 +0000358 Handle<JSFunction> function = prototype.is_null() ?
lrn@chromium.org7516f052011-03-30 08:52:27 +0000359 factory->NewFunctionWithoutPrototype(symbol, call_code) :
360 factory->NewFunctionWithPrototype(symbol,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000361 type,
362 instance_size,
363 prototype,
364 call_code,
365 is_ecma_native);
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000366 PropertyAttributes attributes;
367 if (target->IsJSBuiltinsObject()) {
368 attributes =
369 static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE | READ_ONLY);
370 } else {
371 attributes = DONT_ENUM;
372 }
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000373 CHECK_NOT_EMPTY_HANDLE(isolate,
374 JSObject::SetLocalPropertyIgnoreAttributes(
375 target, symbol, function, attributes));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000376 if (is_ecma_native) {
377 function->shared()->set_instance_class_name(*symbol);
378 }
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000379 function->shared()->set_native(true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000380 return function;
381}
382
383
384Handle<DescriptorArray> Genesis::ComputeFunctionInstanceDescriptor(
kmillikin@chromium.org4111b802010-05-03 10:34:42 +0000385 PrototypePropertyMode prototypeMode) {
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000386 int size = (prototypeMode == DONT_ADD_PROTOTYPE) ? 4 : 5;
387 Handle<DescriptorArray> descriptors(factory()->NewDescriptorArray(size));
388 PropertyAttributes attribs = static_cast<PropertyAttributes>(
389 DONT_ENUM | DONT_DELETE | READ_ONLY);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000390
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000391 DescriptorArray::WhitenessWitness witness(*descriptors);
392
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000393 { // Add length.
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000394 Handle<Foreign> f(factory()->NewForeign(&Accessors::FunctionLength));
jkummerow@chromium.org28583c92012-07-16 11:31:55 +0000395 CallbacksDescriptor d(*factory()->length_symbol(), *f, attribs);
396 descriptors->Append(&d, witness);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000397 }
398 { // Add name.
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000399 Handle<Foreign> f(factory()->NewForeign(&Accessors::FunctionName));
jkummerow@chromium.org28583c92012-07-16 11:31:55 +0000400 CallbacksDescriptor d(*factory()->name_symbol(), *f, attribs);
401 descriptors->Append(&d, witness);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000402 }
403 { // Add arguments.
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000404 Handle<Foreign> f(factory()->NewForeign(&Accessors::FunctionArguments));
jkummerow@chromium.org28583c92012-07-16 11:31:55 +0000405 CallbacksDescriptor d(*factory()->arguments_symbol(), *f, attribs);
406 descriptors->Append(&d, witness);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000407 }
408 { // Add caller.
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000409 Handle<Foreign> f(factory()->NewForeign(&Accessors::FunctionCaller));
jkummerow@chromium.org28583c92012-07-16 11:31:55 +0000410 CallbacksDescriptor d(*factory()->caller_symbol(), *f, attribs);
411 descriptors->Append(&d, witness);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000412 }
413 if (prototypeMode != DONT_ADD_PROTOTYPE) {
414 // Add prototype.
415 if (prototypeMode == ADD_WRITEABLE_PROTOTYPE) {
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000416 attribs = static_cast<PropertyAttributes>(attribs & ~READ_ONLY);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000417 }
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000418 Handle<Foreign> f(factory()->NewForeign(&Accessors::FunctionPrototype));
jkummerow@chromium.org28583c92012-07-16 11:31:55 +0000419 CallbacksDescriptor d(*factory()->prototype_symbol(), *f, attribs);
420 descriptors->Append(&d, witness);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000421 }
rossberg@chromium.org657d53b2012-07-12 11:06:03 +0000422
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000423 descriptors->Sort(witness);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000424 return descriptors;
425}
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);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000430 Handle<DescriptorArray> descriptors =
431 ComputeFunctionInstanceDescriptor(prototype_mode);
432 map->set_instance_descriptors(*descriptors);
433 map->set_function_with_prototype(prototype_mode != DONT_ADD_PROTOTYPE);
434 return map;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000435}
436
437
danno@chromium.org160a7b02011-04-18 15:51:38 +0000438Handle<JSFunction> Genesis::CreateEmptyFunction(Isolate* isolate) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000439 // Allocate the map for function instances. Maps are allocated first and their
440 // prototypes patched later, once empty function is created.
441
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000442 // Please note that the prototype property for function instances must be
443 // writable.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000444 Handle<Map> function_instance_map =
445 CreateFunctionMap(ADD_WRITEABLE_PROTOTYPE);
446 global_context()->set_function_instance_map(*function_instance_map);
kmillikin@chromium.org4111b802010-05-03 10:34:42 +0000447
448 // Functions with this map will not have a 'prototype' property, and
449 // can not be used as constructors.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000450 Handle<Map> function_without_prototype_map =
451 CreateFunctionMap(DONT_ADD_PROTOTYPE);
kmillikin@chromium.org4111b802010-05-03 10:34:42 +0000452 global_context()->set_function_without_prototype_map(
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000453 *function_without_prototype_map);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000454
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000455 // Allocate the function map. This map is temporary, used only for processing
456 // of builtins.
457 // Later the map is replaced with writable prototype map, allocated below.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000458 Handle<Map> function_map = CreateFunctionMap(ADD_READONLY_PROTOTYPE);
459 global_context()->set_function_map(*function_map);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000460
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000461 // The final map for functions. Writeable prototype.
462 // This map is installed in MakeFunctionInstancePrototypeWritable.
463 function_instance_map_writable_prototype_ =
464 CreateFunctionMap(ADD_WRITEABLE_PROTOTYPE);
465
lrn@chromium.org7516f052011-03-30 08:52:27 +0000466 Factory* factory = isolate->factory();
467 Heap* heap = isolate->heap();
468
469 Handle<String> object_name = Handle<String>(heap->Object_symbol());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000470
471 { // --- O b j e c t ---
472 Handle<JSFunction> object_fun =
lrn@chromium.org7516f052011-03-30 08:52:27 +0000473 factory->NewFunction(object_name, factory->null_value());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000474 Handle<Map> object_function_map =
lrn@chromium.org7516f052011-03-30 08:52:27 +0000475 factory->NewMap(JS_OBJECT_TYPE, JSObject::kHeaderSize);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000476 object_fun->set_initial_map(*object_function_map);
477 object_function_map->set_constructor(*object_fun);
478
479 global_context()->set_object_function(*object_fun);
480
481 // Allocate a new prototype for the object function.
lrn@chromium.org7516f052011-03-30 08:52:27 +0000482 Handle<JSObject> prototype = factory->NewJSObject(
483 isolate->object_function(),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000484 TENURED);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000485
486 global_context()->set_initial_object_prototype(*prototype);
487 SetPrototype(object_fun, prototype);
yangguo@chromium.org5f0b8ea2012-05-16 12:37:04 +0000488 object_function_map->set_instance_descriptors(
489 heap->empty_descriptor_array());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000490 }
491
492 // Allocate the empty function as the prototype for function ECMAScript
493 // 262 15.3.4.
lrn@chromium.org7516f052011-03-30 08:52:27 +0000494 Handle<String> symbol = factory->LookupAsciiSymbol("Empty");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000495 Handle<JSFunction> empty_function =
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000496 factory->NewFunctionWithoutPrototype(symbol, CLASSIC_MODE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000497
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000498 // --- E m p t y ---
499 Handle<Code> code =
lrn@chromium.org7516f052011-03-30 08:52:27 +0000500 Handle<Code>(isolate->builtins()->builtin(
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000501 Builtins::kEmptyFunction));
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000502 empty_function->set_code(*code);
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000503 empty_function->shared()->set_code(*code);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000504 Handle<String> source = factory->NewStringFromAscii(CStrVector("() {}"));
505 Handle<Script> script = factory->NewScript(source);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000506 script->set_type(Smi::FromInt(Script::TYPE_NATIVE));
507 empty_function->shared()->set_script(*script);
508 empty_function->shared()->set_start_position(0);
509 empty_function->shared()->set_end_position(source->length());
510 empty_function->shared()->DontAdaptArguments();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000511
512 // Set prototypes for the function maps.
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000513 global_context()->function_map()->set_prototype(*empty_function);
514 global_context()->function_instance_map()->set_prototype(*empty_function);
kmillikin@chromium.org4111b802010-05-03 10:34:42 +0000515 global_context()->function_without_prototype_map()->
516 set_prototype(*empty_function);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000517 function_instance_map_writable_prototype_->set_prototype(*empty_function);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000518
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000519 // Allocate the function map first and then patch the prototype later
yangguo@chromium.org5f0b8ea2012-05-16 12:37:04 +0000520 Handle<Map> empty_function_map = CreateFunctionMap(DONT_ADD_PROTOTYPE);
521 empty_function_map->set_prototype(
522 global_context()->object_function()->prototype());
523 empty_function->set_map(*empty_function_map);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000524 return empty_function;
525}
526
527
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000528Handle<DescriptorArray> Genesis::ComputeStrictFunctionInstanceDescriptor(
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000529 PrototypePropertyMode prototypeMode) {
530 int size = (prototypeMode == DONT_ADD_PROTOTYPE) ? 4 : 5;
531 Handle<DescriptorArray> descriptors(factory()->NewDescriptorArray(size));
532 PropertyAttributes attribs = static_cast<PropertyAttributes>(
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000533 DONT_ENUM | DONT_DELETE);
534
535 DescriptorArray::WhitenessWitness witness(*descriptors);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000536
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000537 { // Add length.
538 Handle<Foreign> f(factory()->NewForeign(&Accessors::FunctionLength));
jkummerow@chromium.org28583c92012-07-16 11:31:55 +0000539 CallbacksDescriptor d(*factory()->length_symbol(), *f, attribs);
540 descriptors->Append(&d, witness);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000541 }
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000542 { // Add name.
543 Handle<Foreign> f(factory()->NewForeign(&Accessors::FunctionName));
jkummerow@chromium.org28583c92012-07-16 11:31:55 +0000544 CallbacksDescriptor d(*factory()->name_symbol(), *f, attribs);
545 descriptors->Append(&d, witness);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000546 }
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000547 { // Add arguments.
548 Handle<AccessorPair> arguments(factory()->NewAccessorPair());
jkummerow@chromium.org28583c92012-07-16 11:31:55 +0000549 CallbacksDescriptor d(*factory()->arguments_symbol(), *arguments, attribs);
550 descriptors->Append(&d, witness);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000551 }
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000552 { // Add caller.
553 Handle<AccessorPair> caller(factory()->NewAccessorPair());
jkummerow@chromium.org28583c92012-07-16 11:31:55 +0000554 CallbacksDescriptor d(*factory()->caller_symbol(), *caller, attribs);
555 descriptors->Append(&d, witness);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000556 }
557
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000558 if (prototypeMode != DONT_ADD_PROTOTYPE) {
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000559 // Add prototype.
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000560 if (prototypeMode != ADD_WRITEABLE_PROTOTYPE) {
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000561 attribs = static_cast<PropertyAttributes>(attribs | READ_ONLY);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000562 }
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000563 Handle<Foreign> f(factory()->NewForeign(&Accessors::FunctionPrototype));
jkummerow@chromium.org28583c92012-07-16 11:31:55 +0000564 CallbacksDescriptor d(*factory()->prototype_symbol(), *f, attribs);
565 descriptors->Append(&d, witness);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000566 }
567
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000568 descriptors->Sort(witness);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000569 return descriptors;
570}
571
572
573// ECMAScript 5th Edition, 13.2.3
danno@chromium.org40cb8782011-05-25 07:58:50 +0000574Handle<JSFunction> Genesis::GetThrowTypeErrorFunction() {
575 if (throw_type_error_function.is_null()) {
576 Handle<String> name = factory()->LookupAsciiSymbol("ThrowTypeError");
577 throw_type_error_function =
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000578 factory()->NewFunctionWithoutPrototype(name, CLASSIC_MODE);
danno@chromium.org40cb8782011-05-25 07:58:50 +0000579 Handle<Code> code(isolate()->builtins()->builtin(
580 Builtins::kStrictModePoisonPill));
581 throw_type_error_function->set_map(
582 global_context()->function_map());
583 throw_type_error_function->set_code(*code);
584 throw_type_error_function->shared()->set_code(*code);
585 throw_type_error_function->shared()->DontAdaptArguments();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000586
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000587 JSObject::PreventExtensions(throw_type_error_function);
danno@chromium.org40cb8782011-05-25 07:58:50 +0000588 }
589 return throw_type_error_function;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000590}
591
592
593Handle<Map> Genesis::CreateStrictModeFunctionMap(
594 PrototypePropertyMode prototype_mode,
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000595 Handle<JSFunction> empty_function) {
danno@chromium.org160a7b02011-04-18 15:51:38 +0000596 Handle<Map> map = factory()->NewMap(JS_FUNCTION_TYPE, JSFunction::kSize);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000597 Handle<DescriptorArray> descriptors =
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000598 ComputeStrictFunctionInstanceDescriptor(prototype_mode);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000599 map->set_instance_descriptors(*descriptors);
600 map->set_function_with_prototype(prototype_mode != DONT_ADD_PROTOTYPE);
601 map->set_prototype(*empty_function);
602 return map;
603}
604
605
606void Genesis::CreateStrictModeFunctionMaps(Handle<JSFunction> empty) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000607 // Allocate map for the strict mode function instances.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000608 Handle<Map> strict_mode_function_instance_map =
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000609 CreateStrictModeFunctionMap(ADD_WRITEABLE_PROTOTYPE, empty);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000610 global_context()->set_strict_mode_function_instance_map(
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000611 *strict_mode_function_instance_map);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000612
613 // Allocate map for the prototype-less strict mode instances.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000614 Handle<Map> strict_mode_function_without_prototype_map =
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000615 CreateStrictModeFunctionMap(DONT_ADD_PROTOTYPE, empty);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000616 global_context()->set_strict_mode_function_without_prototype_map(
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000617 *strict_mode_function_without_prototype_map);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000618
619 // Allocate map for the strict mode functions. This map is temporary, used
620 // only for processing of builtins.
621 // Later the map is replaced with writable prototype map, allocated below.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000622 Handle<Map> strict_mode_function_map =
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000623 CreateStrictModeFunctionMap(ADD_READONLY_PROTOTYPE, empty);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000624 global_context()->set_strict_mode_function_map(
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000625 *strict_mode_function_map);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000626
627 // The final map for the strict mode functions. Writeable prototype.
628 // This map is installed in MakeFunctionInstancePrototypeWritable.
629 strict_mode_function_instance_map_writable_prototype_ =
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000630 CreateStrictModeFunctionMap(ADD_WRITEABLE_PROTOTYPE, empty);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000631
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000632 // Complete the callbacks.
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000633 PoisonArgumentsAndCaller(strict_mode_function_instance_map);
634 PoisonArgumentsAndCaller(strict_mode_function_without_prototype_map);
635 PoisonArgumentsAndCaller(strict_mode_function_map);
636 PoisonArgumentsAndCaller(
637 strict_mode_function_instance_map_writable_prototype_);
638}
639
640
641static void SetAccessors(Handle<Map> map,
642 Handle<String> name,
643 Handle<JSFunction> func) {
644 DescriptorArray* descs = map->instance_descriptors();
645 int number = descs->Search(*name);
646 AccessorPair* accessors = AccessorPair::cast(descs->GetValue(number));
647 accessors->set_getter(*func);
648 accessors->set_setter(*func);
649}
650
651
652void Genesis::PoisonArgumentsAndCaller(Handle<Map> map) {
653 SetAccessors(map, factory()->arguments_symbol(), GetThrowTypeErrorFunction());
654 SetAccessors(map, factory()->caller_symbol(), GetThrowTypeErrorFunction());
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000655}
656
657
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000658static void AddToWeakGlobalContextList(Context* context) {
659 ASSERT(context->IsGlobalContext());
danno@chromium.org160a7b02011-04-18 15:51:38 +0000660 Heap* heap = context->GetIsolate()->heap();
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000661#ifdef DEBUG
662 { // NOLINT
663 ASSERT(context->get(Context::NEXT_CONTEXT_LINK)->IsUndefined());
664 // Check that context is not in the list yet.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000665 for (Object* current = heap->global_contexts_list();
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000666 !current->IsUndefined();
667 current = Context::cast(current)->get(Context::NEXT_CONTEXT_LINK)) {
668 ASSERT(current != context);
669 }
670 }
671#endif
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000672 context->set(Context::NEXT_CONTEXT_LINK, heap->global_contexts_list());
673 heap->set_global_contexts_list(context);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000674}
675
676
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000677void Genesis::CreateRoots() {
678 // Allocate the global context FixedArray first and then patch the
679 // closure and extension object later (we need the empty function
680 // and the global object, but in order to create those, we need the
681 // global context).
danno@chromium.org160a7b02011-04-18 15:51:38 +0000682 global_context_ = Handle<Context>::cast(isolate()->global_handles()->Create(
683 *factory()->NewGlobalContext()));
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000684 AddToWeakGlobalContextList(*global_context_);
danno@chromium.org160a7b02011-04-18 15:51:38 +0000685 isolate()->set_context(*global_context());
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000686
687 // Allocate the message listeners object.
688 {
689 v8::NeanderArray listeners;
690 global_context()->set_message_listeners(*listeners.value());
691 }
692}
693
694
695Handle<JSGlobalProxy> Genesis::CreateNewGlobals(
696 v8::Handle<v8::ObjectTemplate> global_template,
697 Handle<Object> global_object,
698 Handle<GlobalObject>* inner_global_out) {
699 // The argument global_template aka data is an ObjectTemplateInfo.
700 // It has a constructor pointer that points at global_constructor which is a
701 // FunctionTemplateInfo.
702 // The global_constructor is used to create or reinitialize the global_proxy.
703 // The global_constructor also has a prototype_template pointer that points at
704 // js_global_template which is an ObjectTemplateInfo.
705 // That in turn has a constructor pointer that points at
706 // js_global_constructor which is a FunctionTemplateInfo.
707 // js_global_constructor is used to make js_global_function
708 // js_global_function is used to make the new inner_global.
709 //
710 // --- G l o b a l ---
711 // Step 1: Create a fresh inner JSGlobalObject.
712 Handle<JSFunction> js_global_function;
713 Handle<ObjectTemplateInfo> js_global_template;
714 if (!global_template.IsEmpty()) {
715 // Get prototype template of the global_template.
716 Handle<ObjectTemplateInfo> data =
717 v8::Utils::OpenHandle(*global_template);
718 Handle<FunctionTemplateInfo> global_constructor =
719 Handle<FunctionTemplateInfo>(
720 FunctionTemplateInfo::cast(data->constructor()));
721 Handle<Object> proto_template(global_constructor->prototype_template());
722 if (!proto_template->IsUndefined()) {
723 js_global_template =
724 Handle<ObjectTemplateInfo>::cast(proto_template);
725 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000726 }
727
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000728 if (js_global_template.is_null()) {
danno@chromium.org160a7b02011-04-18 15:51:38 +0000729 Handle<String> name = Handle<String>(heap()->empty_symbol());
730 Handle<Code> code = Handle<Code>(isolate()->builtins()->builtin(
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000731 Builtins::kIllegal));
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000732 js_global_function =
danno@chromium.org160a7b02011-04-18 15:51:38 +0000733 factory()->NewFunction(name, JS_GLOBAL_OBJECT_TYPE,
734 JSGlobalObject::kSize, code, true);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000735 // Change the constructor property of the prototype of the
736 // hidden global function to refer to the Object function.
737 Handle<JSObject> prototype =
738 Handle<JSObject>(
739 JSObject::cast(js_global_function->instance_prototype()));
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000740 CHECK_NOT_EMPTY_HANDLE(isolate(),
741 JSObject::SetLocalPropertyIgnoreAttributes(
742 prototype, factory()->constructor_symbol(),
743 isolate()->object_function(), NONE));
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000744 } else {
745 Handle<FunctionTemplateInfo> js_global_constructor(
746 FunctionTemplateInfo::cast(js_global_template->constructor()));
747 js_global_function =
danno@chromium.org160a7b02011-04-18 15:51:38 +0000748 factory()->CreateApiFunction(js_global_constructor,
749 factory()->InnerGlobalObject);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000750 }
751
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000752 js_global_function->initial_map()->set_is_hidden_prototype();
753 Handle<GlobalObject> inner_global =
danno@chromium.org160a7b02011-04-18 15:51:38 +0000754 factory()->NewGlobalObject(js_global_function);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000755 if (inner_global_out != NULL) {
756 *inner_global_out = inner_global;
757 }
758
759 // Step 2: create or re-initialize the global proxy object.
760 Handle<JSFunction> global_proxy_function;
761 if (global_template.IsEmpty()) {
danno@chromium.org160a7b02011-04-18 15:51:38 +0000762 Handle<String> name = Handle<String>(heap()->empty_symbol());
763 Handle<Code> code = Handle<Code>(isolate()->builtins()->builtin(
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000764 Builtins::kIllegal));
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000765 global_proxy_function =
danno@chromium.org160a7b02011-04-18 15:51:38 +0000766 factory()->NewFunction(name, JS_GLOBAL_PROXY_TYPE,
767 JSGlobalProxy::kSize, code, true);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000768 } else {
769 Handle<ObjectTemplateInfo> data =
770 v8::Utils::OpenHandle(*global_template);
771 Handle<FunctionTemplateInfo> global_constructor(
772 FunctionTemplateInfo::cast(data->constructor()));
773 global_proxy_function =
danno@chromium.org160a7b02011-04-18 15:51:38 +0000774 factory()->CreateApiFunction(global_constructor,
775 factory()->OuterGlobalObject);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000776 }
777
danno@chromium.org160a7b02011-04-18 15:51:38 +0000778 Handle<String> global_name = factory()->LookupAsciiSymbol("global");
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000779 global_proxy_function->shared()->set_instance_class_name(*global_name);
780 global_proxy_function->initial_map()->set_is_access_check_needed(true);
781
782 // Set global_proxy.__proto__ to js_global after ConfigureGlobalObjects
783 // Return the global proxy.
784
785 if (global_object.location() != NULL) {
786 ASSERT(global_object->IsJSGlobalProxy());
787 return ReinitializeJSGlobalProxy(
788 global_proxy_function,
789 Handle<JSGlobalProxy>::cast(global_object));
790 } else {
791 return Handle<JSGlobalProxy>::cast(
danno@chromium.org160a7b02011-04-18 15:51:38 +0000792 factory()->NewJSObject(global_proxy_function, TENURED));
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000793 }
794}
795
796
797void Genesis::HookUpGlobalProxy(Handle<GlobalObject> inner_global,
798 Handle<JSGlobalProxy> global_proxy) {
799 // Set the global context for the global object.
800 inner_global->set_global_context(*global_context());
801 inner_global->set_global_receiver(*global_proxy);
802 global_proxy->set_context(*global_context());
803 global_context()->set_global_proxy(*global_proxy);
804}
805
806
807void Genesis::HookUpInnerGlobal(Handle<GlobalObject> inner_global) {
808 Handle<GlobalObject> inner_global_from_snapshot(
809 GlobalObject::cast(global_context_->extension()));
810 Handle<JSBuiltinsObject> builtins_global(global_context_->builtins());
811 global_context_->set_extension(*inner_global);
812 global_context_->set_global(*inner_global);
813 global_context_->set_security_token(*inner_global);
814 static const PropertyAttributes attributes =
815 static_cast<PropertyAttributes>(READ_ONLY | DONT_DELETE);
816 ForceSetProperty(builtins_global,
danno@chromium.org160a7b02011-04-18 15:51:38 +0000817 factory()->LookupAsciiSymbol("global"),
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000818 inner_global,
819 attributes);
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000820 // Set up the reference from the global object to the builtins object.
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000821 JSGlobalObject::cast(*inner_global)->set_builtins(*builtins_global);
822 TransferNamedProperties(inner_global_from_snapshot, inner_global);
823 TransferIndexedProperties(inner_global_from_snapshot, inner_global);
824}
825
826
827// This is only called if we are not using snapshots. The equivalent
828// work in the snapshot case is done in HookUpInnerGlobal.
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000829bool Genesis::InitializeGlobal(Handle<GlobalObject> inner_global,
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000830 Handle<JSFunction> empty_function) {
831 // --- G l o b a l C o n t e x t ---
832 // Use the empty function as closure (no scope info).
833 global_context()->set_closure(*empty_function);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000834 global_context()->set_previous(NULL);
835 // Set extension and global object.
836 global_context()->set_extension(*inner_global);
837 global_context()->set_global(*inner_global);
838 // Security setup: Set the security token of the global object to
839 // its the inner global. This makes the security check between two
840 // different contexts fail by default even in case of global
841 // object reinitialization.
842 global_context()->set_security_token(*inner_global);
843
danno@chromium.org160a7b02011-04-18 15:51:38 +0000844 Isolate* isolate = inner_global->GetIsolate();
lrn@chromium.org7516f052011-03-30 08:52:27 +0000845 Factory* factory = isolate->factory();
846 Heap* heap = isolate->heap();
847
848 Handle<String> object_name = Handle<String>(heap->Object_symbol());
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000849 CHECK_NOT_EMPTY_HANDLE(isolate,
850 JSObject::SetLocalPropertyIgnoreAttributes(
851 inner_global, object_name,
852 isolate->object_function(), DONT_ENUM));
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000853
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000854 Handle<JSObject> global = Handle<JSObject>(global_context()->global());
855
856 // Install global Function object
857 InstallFunction(global, "Function", JS_FUNCTION_TYPE, JSFunction::kSize,
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000858 empty_function, Builtins::kIllegal, true); // ECMA native.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000859
860 { // --- A r r a y ---
861 Handle<JSFunction> array_function =
862 InstallFunction(global, "Array", JS_ARRAY_TYPE, JSArray::kSize,
lrn@chromium.org7516f052011-03-30 08:52:27 +0000863 isolate->initial_object_prototype(),
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000864 Builtins::kArrayCode, true);
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +0000865 array_function->shared()->set_construct_stub(
lrn@chromium.org7516f052011-03-30 08:52:27 +0000866 isolate->builtins()->builtin(Builtins::kArrayConstructCode));
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000867 array_function->shared()->DontAdaptArguments();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000868
869 // This seems a bit hackish, but we need to make sure Array.length
870 // is 1.
871 array_function->shared()->set_length(1);
872 Handle<DescriptorArray> array_descriptors =
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000873 factory->CopyAppendForeignDescriptor(
lrn@chromium.org7516f052011-03-30 08:52:27 +0000874 factory->empty_descriptor_array(),
875 factory->length_symbol(),
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000876 factory->NewForeign(&Accessors::ArrayLength),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000877 static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE));
878
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000879 // array_function is used internally. JS code creating array object should
880 // search for the 'Array' property on the global object and use that one
881 // as the constructor. 'Array' property on a global object can be
882 // overwritten by JS code.
883 global_context()->set_array_function(*array_function);
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000884 array_function->initial_map()->set_instance_descriptors(*array_descriptors);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000885 }
886
887 { // --- N u m b e r ---
888 Handle<JSFunction> number_fun =
889 InstallFunction(global, "Number", JS_VALUE_TYPE, JSValue::kSize,
lrn@chromium.org7516f052011-03-30 08:52:27 +0000890 isolate->initial_object_prototype(),
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000891 Builtins::kIllegal, true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000892 global_context()->set_number_function(*number_fun);
893 }
894
895 { // --- B o o l e a n ---
896 Handle<JSFunction> boolean_fun =
897 InstallFunction(global, "Boolean", JS_VALUE_TYPE, JSValue::kSize,
lrn@chromium.org7516f052011-03-30 08:52:27 +0000898 isolate->initial_object_prototype(),
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000899 Builtins::kIllegal, true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000900 global_context()->set_boolean_function(*boolean_fun);
901 }
902
903 { // --- S t r i n g ---
904 Handle<JSFunction> string_fun =
905 InstallFunction(global, "String", JS_VALUE_TYPE, JSValue::kSize,
lrn@chromium.org7516f052011-03-30 08:52:27 +0000906 isolate->initial_object_prototype(),
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000907 Builtins::kIllegal, true);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000908 string_fun->shared()->set_construct_stub(
lrn@chromium.org7516f052011-03-30 08:52:27 +0000909 isolate->builtins()->builtin(Builtins::kStringConstructCode));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000910 global_context()->set_string_function(*string_fun);
911 // Add 'length' property to strings.
912 Handle<DescriptorArray> string_descriptors =
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000913 factory->CopyAppendForeignDescriptor(
lrn@chromium.org7516f052011-03-30 08:52:27 +0000914 factory->empty_descriptor_array(),
915 factory->length_symbol(),
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000916 factory->NewForeign(&Accessors::StringLength),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000917 static_cast<PropertyAttributes>(DONT_ENUM |
918 DONT_DELETE |
919 READ_ONLY));
920
921 Handle<Map> string_map =
922 Handle<Map>(global_context()->string_function()->initial_map());
923 string_map->set_instance_descriptors(*string_descriptors);
924 }
925
926 { // --- D a t e ---
927 // Builtin functions for Date.prototype.
928 Handle<JSFunction> date_fun =
svenpanne@chromium.org4efbdb12012-03-12 08:18:42 +0000929 InstallFunction(global, "Date", JS_DATE_TYPE, JSDate::kSize,
lrn@chromium.org7516f052011-03-30 08:52:27 +0000930 isolate->initial_object_prototype(),
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000931 Builtins::kIllegal, true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000932
933 global_context()->set_date_function(*date_fun);
934 }
935
936
937 { // -- R e g E x p
938 // Builtin functions for RegExp.prototype.
939 Handle<JSFunction> regexp_fun =
ager@chromium.org236ad962008-09-25 09:45:57 +0000940 InstallFunction(global, "RegExp", JS_REGEXP_TYPE, JSRegExp::kSize,
lrn@chromium.org7516f052011-03-30 08:52:27 +0000941 isolate->initial_object_prototype(),
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000942 Builtins::kIllegal, true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000943 global_context()->set_regexp_function(*regexp_fun);
lrn@chromium.org25156de2010-04-06 13:10:27 +0000944
945 ASSERT(regexp_fun->has_initial_map());
946 Handle<Map> initial_map(regexp_fun->initial_map());
947
948 ASSERT_EQ(0, initial_map->inobject_properties());
949
lrn@chromium.org7516f052011-03-30 08:52:27 +0000950 Handle<DescriptorArray> descriptors = factory->NewDescriptorArray(5);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000951 DescriptorArray::WhitenessWitness witness(*descriptors);
lrn@chromium.org25156de2010-04-06 13:10:27 +0000952 PropertyAttributes final =
953 static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE | READ_ONLY);
lrn@chromium.org25156de2010-04-06 13:10:27 +0000954 {
955 // ECMA-262, section 15.10.7.1.
lrn@chromium.org7516f052011-03-30 08:52:27 +0000956 FieldDescriptor field(heap->source_symbol(),
lrn@chromium.org25156de2010-04-06 13:10:27 +0000957 JSRegExp::kSourceFieldIndex,
jkummerow@chromium.org28583c92012-07-16 11:31:55 +0000958 final);
959 descriptors->Append(&field, witness);
lrn@chromium.org25156de2010-04-06 13:10:27 +0000960 }
961 {
962 // ECMA-262, section 15.10.7.2.
lrn@chromium.org7516f052011-03-30 08:52:27 +0000963 FieldDescriptor field(heap->global_symbol(),
lrn@chromium.org25156de2010-04-06 13:10:27 +0000964 JSRegExp::kGlobalFieldIndex,
jkummerow@chromium.org28583c92012-07-16 11:31:55 +0000965 final);
966 descriptors->Append(&field, witness);
lrn@chromium.org25156de2010-04-06 13:10:27 +0000967 }
968 {
969 // ECMA-262, section 15.10.7.3.
lrn@chromium.org7516f052011-03-30 08:52:27 +0000970 FieldDescriptor field(heap->ignore_case_symbol(),
lrn@chromium.org25156de2010-04-06 13:10:27 +0000971 JSRegExp::kIgnoreCaseFieldIndex,
jkummerow@chromium.org28583c92012-07-16 11:31:55 +0000972 final);
973 descriptors->Append(&field, witness);
lrn@chromium.org25156de2010-04-06 13:10:27 +0000974 }
975 {
976 // ECMA-262, section 15.10.7.4.
lrn@chromium.org7516f052011-03-30 08:52:27 +0000977 FieldDescriptor field(heap->multiline_symbol(),
lrn@chromium.org25156de2010-04-06 13:10:27 +0000978 JSRegExp::kMultilineFieldIndex,
jkummerow@chromium.org28583c92012-07-16 11:31:55 +0000979 final);
980 descriptors->Append(&field, witness);
lrn@chromium.org25156de2010-04-06 13:10:27 +0000981 }
982 {
983 // ECMA-262, section 15.10.7.5.
984 PropertyAttributes writable =
985 static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000986 FieldDescriptor field(heap->last_index_symbol(),
lrn@chromium.org25156de2010-04-06 13:10:27 +0000987 JSRegExp::kLastIndexFieldIndex,
jkummerow@chromium.org28583c92012-07-16 11:31:55 +0000988 writable);
989 descriptors->Append(&field, witness);
lrn@chromium.org25156de2010-04-06 13:10:27 +0000990 }
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000991 descriptors->Sort(witness);
lrn@chromium.org25156de2010-04-06 13:10:27 +0000992
993 initial_map->set_inobject_properties(5);
994 initial_map->set_pre_allocated_property_fields(5);
995 initial_map->set_unused_property_fields(0);
996 initial_map->set_instance_size(
997 initial_map->instance_size() + 5 * kPointerSize);
998 initial_map->set_instance_descriptors(*descriptors);
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000999 initial_map->set_visitor_id(StaticVisitorBase::GetVisitorId(*initial_map));
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001000
1001 // RegExp prototype object is itself a RegExp.
verwaest@chromium.org753aee42012-07-17 16:15:42 +00001002 Handle<Map> proto_map = factory->CopyMap(initial_map);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001003 proto_map->set_prototype(global_context()->initial_object_prototype());
1004 Handle<JSObject> proto = factory->NewJSObjectFromMap(proto_map);
1005 proto->InObjectPropertyAtPut(JSRegExp::kSourceFieldIndex,
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00001006 heap->query_colon_symbol());
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001007 proto->InObjectPropertyAtPut(JSRegExp::kGlobalFieldIndex,
1008 heap->false_value());
1009 proto->InObjectPropertyAtPut(JSRegExp::kIgnoreCaseFieldIndex,
1010 heap->false_value());
1011 proto->InObjectPropertyAtPut(JSRegExp::kMultilineFieldIndex,
1012 heap->false_value());
1013 proto->InObjectPropertyAtPut(JSRegExp::kLastIndexFieldIndex,
1014 Smi::FromInt(0),
1015 SKIP_WRITE_BARRIER); // It's a Smi.
1016 initial_map->set_prototype(*proto);
1017 factory->SetRegExpIrregexpData(Handle<JSRegExp>::cast(proto),
1018 JSRegExp::IRREGEXP, factory->empty_string(),
1019 JSRegExp::Flags(0), 0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001020 }
1021
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00001022 { // -- J S O N
lrn@chromium.org7516f052011-03-30 08:52:27 +00001023 Handle<String> name = factory->NewStringFromAscii(CStrVector("JSON"));
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001024 Handle<JSFunction> cons = factory->NewFunction(name,
1025 factory->the_hole_value());
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +00001026 { MaybeObject* result = cons->SetInstancePrototype(
1027 global_context()->initial_object_prototype());
1028 if (result->IsFailure()) return false;
1029 }
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00001030 cons->SetInstanceClassName(*name);
lrn@chromium.org7516f052011-03-30 08:52:27 +00001031 Handle<JSObject> json_object = factory->NewJSObject(cons, TENURED);
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00001032 ASSERT(json_object->IsJSObject());
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001033 CHECK_NOT_EMPTY_HANDLE(isolate,
1034 JSObject::SetLocalPropertyIgnoreAttributes(
1035 global, name, json_object, DONT_ENUM));
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00001036 global_context()->set_json_object(*json_object);
1037 }
1038
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001039 { // --- arguments_boilerplate_
1040 // Make sure we can recognize argument objects at runtime.
1041 // This is done by introducing an anonymous function with
1042 // class_name equals 'Arguments'.
lrn@chromium.org7516f052011-03-30 08:52:27 +00001043 Handle<String> symbol = factory->LookupAsciiSymbol("Arguments");
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001044 Handle<Code> code = Handle<Code>(
lrn@chromium.org7516f052011-03-30 08:52:27 +00001045 isolate->builtins()->builtin(Builtins::kIllegal));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001046 Handle<JSObject> prototype =
1047 Handle<JSObject>(
1048 JSObject::cast(global_context()->object_function()->prototype()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001049
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001050 Handle<JSFunction> function =
lrn@chromium.org7516f052011-03-30 08:52:27 +00001051 factory->NewFunctionWithPrototype(symbol,
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001052 JS_OBJECT_TYPE,
1053 JSObject::kHeaderSize,
1054 prototype,
1055 code,
1056 false);
1057 ASSERT(!function->has_initial_map());
1058 function->shared()->set_instance_class_name(*symbol);
1059 function->shared()->set_expected_nof_properties(2);
lrn@chromium.org7516f052011-03-30 08:52:27 +00001060 Handle<JSObject> result = factory->NewJSObject(function);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001061
1062 global_context()->set_arguments_boilerplate(*result);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001063 // Note: length must be added as the first property and
1064 // callee must be added as the second property.
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001065 CHECK_NOT_EMPTY_HANDLE(isolate,
1066 JSObject::SetLocalPropertyIgnoreAttributes(
1067 result, factory->length_symbol(),
1068 factory->undefined_value(), DONT_ENUM));
1069 CHECK_NOT_EMPTY_HANDLE(isolate,
1070 JSObject::SetLocalPropertyIgnoreAttributes(
1071 result, factory->callee_symbol(),
1072 factory->undefined_value(), DONT_ENUM));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001073
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001074#ifdef DEBUG
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001075 LookupResult lookup(isolate);
lrn@chromium.org7516f052011-03-30 08:52:27 +00001076 result->LocalLookup(heap->callee_symbol(), &lookup);
yangguo@chromium.orgde0db002012-06-22 13:44:28 +00001077 ASSERT(lookup.IsField());
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001078 ASSERT(lookup.GetFieldIndex() == Heap::kArgumentsCalleeIndex);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001079
lrn@chromium.org7516f052011-03-30 08:52:27 +00001080 result->LocalLookup(heap->length_symbol(), &lookup);
yangguo@chromium.orgde0db002012-06-22 13:44:28 +00001081 ASSERT(lookup.IsField());
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001082 ASSERT(lookup.GetFieldIndex() == Heap::kArgumentsLengthIndex);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001083
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001084 ASSERT(result->map()->inobject_properties() > Heap::kArgumentsCalleeIndex);
1085 ASSERT(result->map()->inobject_properties() > Heap::kArgumentsLengthIndex);
1086
1087 // Check the state of the object.
1088 ASSERT(result->HasFastProperties());
svenpanne@chromium.org830d30c2012-05-29 13:20:14 +00001089 ASSERT(result->HasFastObjectElements());
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001090#endif
1091 }
1092
whesse@chromium.org7b260152011-06-20 15:33:18 +00001093 { // --- aliased_arguments_boilerplate_
whesse@chromium.org7b260152011-06-20 15:33:18 +00001094 // Set up a well-formed parameter map to make assertions happy.
1095 Handle<FixedArray> elements = factory->NewFixedArray(2);
1096 elements->set_map(heap->non_strict_arguments_elements_map());
1097 Handle<FixedArray> array;
1098 array = factory->NewFixedArray(0);
1099 elements->set(0, *array);
1100 array = factory->NewFixedArray(0);
1101 elements->set(1, *array);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001102
1103 Handle<Map> old_map(global_context()->arguments_boilerplate()->map());
verwaest@chromium.org753aee42012-07-17 16:15:42 +00001104 Handle<Map> new_map = factory->CopyMap(old_map);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001105 new_map->set_pre_allocated_property_fields(2);
1106 Handle<JSObject> result = factory->NewJSObjectFromMap(new_map);
1107 // Set elements kind after allocating the object because
1108 // NewJSObjectFromMap assumes a fast elements map.
1109 new_map->set_elements_kind(NON_STRICT_ARGUMENTS_ELEMENTS);
whesse@chromium.org7b260152011-06-20 15:33:18 +00001110 result->set_elements(*elements);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001111 ASSERT(result->HasNonStrictArgumentsElements());
whesse@chromium.org7b260152011-06-20 15:33:18 +00001112 global_context()->set_aliased_arguments_boilerplate(*result);
1113 }
1114
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001115 { // --- strict mode arguments boilerplate
1116 const PropertyAttributes attributes =
1117 static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE | READ_ONLY);
1118
1119 // Create the ThrowTypeError functions.
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001120 Handle<AccessorPair> callee = factory->NewAccessorPair();
1121 Handle<AccessorPair> caller = factory->NewAccessorPair();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001122
danno@chromium.org40cb8782011-05-25 07:58:50 +00001123 Handle<JSFunction> throw_function =
1124 GetThrowTypeErrorFunction();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001125
1126 // Install the ThrowTypeError functions.
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001127 callee->set_getter(*throw_function);
1128 callee->set_setter(*throw_function);
1129 caller->set_getter(*throw_function);
1130 caller->set_setter(*throw_function);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001131
1132 // Create the descriptor array for the arguments object.
lrn@chromium.org7516f052011-03-30 08:52:27 +00001133 Handle<DescriptorArray> descriptors = factory->NewDescriptorArray(3);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001134 DescriptorArray::WhitenessWitness witness(*descriptors);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001135 { // length
jkummerow@chromium.org28583c92012-07-16 11:31:55 +00001136 FieldDescriptor d(*factory->length_symbol(), 0, DONT_ENUM);
1137 descriptors->Append(&d, witness);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001138 }
1139 { // callee
rossberg@chromium.org657d53b2012-07-12 11:06:03 +00001140 CallbacksDescriptor d(*factory->callee_symbol(),
1141 *callee,
jkummerow@chromium.org28583c92012-07-16 11:31:55 +00001142 attributes);
1143 descriptors->Append(&d, witness);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001144 }
1145 { // caller
rossberg@chromium.org657d53b2012-07-12 11:06:03 +00001146 CallbacksDescriptor d(*factory->caller_symbol(),
1147 *caller,
jkummerow@chromium.org28583c92012-07-16 11:31:55 +00001148 attributes);
1149 descriptors->Append(&d, witness);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001150 }
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001151 descriptors->Sort(witness);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001152
1153 // Create the map. Allocate one in-object field for length.
lrn@chromium.org7516f052011-03-30 08:52:27 +00001154 Handle<Map> map = factory->NewMap(JS_OBJECT_TYPE,
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001155 Heap::kArgumentsObjectSizeStrict);
1156 map->set_instance_descriptors(*descriptors);
1157 map->set_function_with_prototype(true);
1158 map->set_prototype(global_context()->object_function()->prototype());
1159 map->set_pre_allocated_property_fields(1);
1160 map->set_inobject_properties(1);
1161
1162 // Copy constructor from the non-strict arguments boilerplate.
1163 map->set_constructor(
1164 global_context()->arguments_boilerplate()->map()->constructor());
1165
1166 // Allocate the arguments boilerplate object.
lrn@chromium.org7516f052011-03-30 08:52:27 +00001167 Handle<JSObject> result = factory->NewJSObjectFromMap(map);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001168 global_context()->set_strict_mode_arguments_boilerplate(*result);
1169
1170 // Add length property only for strict mode boilerplate.
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001171 CHECK_NOT_EMPTY_HANDLE(isolate,
1172 JSObject::SetLocalPropertyIgnoreAttributes(
1173 result, factory->length_symbol(),
1174 factory->undefined_value(), DONT_ENUM));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001175
1176#ifdef DEBUG
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001177 LookupResult lookup(isolate);
lrn@chromium.org7516f052011-03-30 08:52:27 +00001178 result->LocalLookup(heap->length_symbol(), &lookup);
yangguo@chromium.orgde0db002012-06-22 13:44:28 +00001179 ASSERT(lookup.IsField());
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001180 ASSERT(lookup.GetFieldIndex() == Heap::kArgumentsLengthIndex);
1181
1182 ASSERT(result->map()->inobject_properties() > Heap::kArgumentsLengthIndex);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001183
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001184 // Check the state of the object.
1185 ASSERT(result->HasFastProperties());
svenpanne@chromium.org830d30c2012-05-29 13:20:14 +00001186 ASSERT(result->HasFastObjectElements());
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001187#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001188 }
1189
1190 { // --- context extension
1191 // Create a function for the context extension objects.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001192 Handle<Code> code = Handle<Code>(
lrn@chromium.org7516f052011-03-30 08:52:27 +00001193 isolate->builtins()->builtin(Builtins::kIllegal));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001194 Handle<JSFunction> context_extension_fun =
lrn@chromium.org7516f052011-03-30 08:52:27 +00001195 factory->NewFunction(factory->empty_symbol(),
ager@chromium.org32912102009-01-16 10:38:43 +00001196 JS_CONTEXT_EXTENSION_OBJECT_TYPE,
1197 JSObject::kHeaderSize,
1198 code,
1199 true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001200
lrn@chromium.org7516f052011-03-30 08:52:27 +00001201 Handle<String> name = factory->LookupAsciiSymbol("context_extension");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001202 context_extension_fun->shared()->set_instance_class_name(*name);
1203 global_context()->set_context_extension_function(*context_extension_fun);
1204 }
1205
sgjesse@chromium.org05521fc2009-05-21 07:37:44 +00001206
1207 {
yangguo@chromium.org80c42ed2011-08-31 09:03:56 +00001208 // Set up the call-as-function delegate.
sgjesse@chromium.org05521fc2009-05-21 07:37:44 +00001209 Handle<Code> code =
lrn@chromium.org7516f052011-03-30 08:52:27 +00001210 Handle<Code>(isolate->builtins()->builtin(
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00001211 Builtins::kHandleApiCallAsFunction));
sgjesse@chromium.org05521fc2009-05-21 07:37:44 +00001212 Handle<JSFunction> delegate =
lrn@chromium.org7516f052011-03-30 08:52:27 +00001213 factory->NewFunction(factory->empty_symbol(), JS_OBJECT_TYPE,
sgjesse@chromium.org05521fc2009-05-21 07:37:44 +00001214 JSObject::kHeaderSize, code, true);
1215 global_context()->set_call_as_function_delegate(*delegate);
1216 delegate->shared()->DontAdaptArguments();
1217 }
1218
1219 {
yangguo@chromium.org80c42ed2011-08-31 09:03:56 +00001220 // Set up the call-as-constructor delegate.
sgjesse@chromium.org05521fc2009-05-21 07:37:44 +00001221 Handle<Code> code =
lrn@chromium.org7516f052011-03-30 08:52:27 +00001222 Handle<Code>(isolate->builtins()->builtin(
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00001223 Builtins::kHandleApiCallAsConstructor));
sgjesse@chromium.org05521fc2009-05-21 07:37:44 +00001224 Handle<JSFunction> delegate =
lrn@chromium.org7516f052011-03-30 08:52:27 +00001225 factory->NewFunction(factory->empty_symbol(), JS_OBJECT_TYPE,
sgjesse@chromium.org05521fc2009-05-21 07:37:44 +00001226 JSObject::kHeaderSize, code, true);
1227 global_context()->set_call_as_constructor_delegate(*delegate);
1228 delegate->shared()->DontAdaptArguments();
1229 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001230
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001231 // Initialize the out of memory slot.
lrn@chromium.org7516f052011-03-30 08:52:27 +00001232 global_context()->set_out_of_memory(heap->false_value());
ager@chromium.org9085a012009-05-11 19:22:57 +00001233
1234 // Initialize the data slot.
lrn@chromium.org7516f052011-03-30 08:52:27 +00001235 global_context()->set_data(heap->undefined_value());
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001236
1237 {
1238 // Initialize the random seed slot.
1239 Handle<ByteArray> zeroed_byte_array(
1240 factory->NewByteArray(kRandomStateSize));
1241 global_context()->set_random_seed(*zeroed_byte_array);
1242 memset(zeroed_byte_array->GetDataStartAddress(), 0, kRandomStateSize);
1243 }
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +00001244 return true;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001245}
1246
1247
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +00001248void Genesis::InitializeExperimentalGlobal() {
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +00001249 Handle<JSObject> global = Handle<JSObject>(global_context()->global());
1250
1251 // TODO(mstarzinger): Move this into Genesis::InitializeGlobal once we no
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001252 // longer need to live behind a flag, so functions get added to the snapshot.
1253 if (FLAG_harmony_collections) {
1254 { // -- S e t
1255 Handle<JSObject> prototype =
1256 factory()->NewJSObject(isolate()->object_function(), TENURED);
1257 InstallFunction(global, "Set", JS_SET_TYPE, JSSet::kSize,
1258 prototype, Builtins::kIllegal, true);
1259 }
1260 { // -- M a p
1261 Handle<JSObject> prototype =
1262 factory()->NewJSObject(isolate()->object_function(), TENURED);
1263 InstallFunction(global, "Map", JS_MAP_TYPE, JSMap::kSize,
1264 prototype, Builtins::kIllegal, true);
1265 }
1266 { // -- W e a k M a p
1267 Handle<JSObject> prototype =
1268 factory()->NewJSObject(isolate()->object_function(), TENURED);
1269 InstallFunction(global, "WeakMap", JS_WEAK_MAP_TYPE, JSWeakMap::kSize,
1270 prototype, Builtins::kIllegal, true);
1271 }
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +00001272 }
1273}
1274
1275
danno@chromium.org160a7b02011-04-18 15:51:38 +00001276bool Genesis::CompileBuiltin(Isolate* isolate, int index) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001277 Vector<const char> name = Natives::GetScriptName(index);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001278 Handle<String> source_code =
danno@chromium.org160a7b02011-04-18 15:51:38 +00001279 isolate->bootstrapper()->NativesSourceLookup(index);
1280 return CompileNative(name, source_code);
1281}
1282
1283
1284bool Genesis::CompileExperimentalBuiltin(Isolate* isolate, int index) {
1285 Vector<const char> name = ExperimentalNatives::GetScriptName(index);
1286 Factory* factory = isolate->factory();
1287 Handle<String> source_code =
jkummerow@chromium.orge297f592011-06-08 10:05:15 +00001288 factory->NewStringFromAscii(
1289 ExperimentalNatives::GetRawScriptSource(index));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001290 return CompileNative(name, source_code);
1291}
1292
1293
1294bool Genesis::CompileNative(Vector<const char> name, Handle<String> source) {
1295 HandleScope scope;
danno@chromium.org160a7b02011-04-18 15:51:38 +00001296 Isolate* isolate = source->GetIsolate();
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001297#ifdef ENABLE_DEBUGGER_SUPPORT
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001298 isolate->debugger()->set_compiling_natives(true);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001299#endif
fschneider@chromium.org7d10be52012-04-10 12:30:14 +00001300 // During genesis, the boilerplate for stack overflow won't work until the
1301 // environment has been at least partially initialized. Add a stack check
1302 // before entering JS code to catch overflow early.
1303 StackLimitCheck check(Isolate::Current());
1304 if (check.HasOverflowed()) return false;
1305
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001306 bool result = CompileScriptCached(name,
1307 source,
1308 NULL,
1309 NULL,
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001310 Handle<Context>(isolate->context()),
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001311 true);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001312 ASSERT(isolate->has_pending_exception() != result);
1313 if (!result) isolate->clear_pending_exception();
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001314#ifdef ENABLE_DEBUGGER_SUPPORT
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001315 isolate->debugger()->set_compiling_natives(false);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001316#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001317 return result;
1318}
1319
1320
1321bool Genesis::CompileScriptCached(Vector<const char> name,
1322 Handle<String> source,
1323 SourceCodeCache* cache,
1324 v8::Extension* extension,
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001325 Handle<Context> top_context,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001326 bool use_runtime_context) {
danno@chromium.org160a7b02011-04-18 15:51:38 +00001327 Factory* factory = source->GetIsolate()->factory();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001328 HandleScope scope;
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001329 Handle<SharedFunctionInfo> function_info;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001330
1331 // If we can't find the function in the cache, we compile a new
1332 // function and insert it into the cache.
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001333 if (cache == NULL || !cache->Lookup(name, &function_info)) {
ager@chromium.org5ec48922009-05-05 07:25:34 +00001334 ASSERT(source->IsAsciiRepresentation());
lrn@chromium.org7516f052011-03-30 08:52:27 +00001335 Handle<String> script_name = factory->NewStringFromUtf8(name);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001336 function_info = Compiler::Compile(
1337 source,
1338 script_name,
1339 0,
1340 0,
1341 extension,
1342 NULL,
1343 Handle<String>::null(),
1344 use_runtime_context ? NATIVES_CODE : NOT_NATIVES_CODE);
1345 if (function_info.is_null()) return false;
1346 if (cache != NULL) cache->Add(name, function_info);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001347 }
1348
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001349 // Set up the function context. Conceptually, we should clone the
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001350 // function before overwriting the context but since we're in a
1351 // single-threaded environment it is not strictly necessary.
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001352 ASSERT(top_context->IsGlobalContext());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001353 Handle<Context> context =
1354 Handle<Context>(use_runtime_context
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001355 ? Handle<Context>(top_context->runtime_context())
1356 : top_context);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001357 Handle<JSFunction> fun =
lrn@chromium.org7516f052011-03-30 08:52:27 +00001358 factory->NewFunctionFromSharedFunctionInfo(function_info, context);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001359
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001360 // Call function using either the runtime object or the global
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001361 // object as the receiver. Provide no parameters.
1362 Handle<Object> receiver =
1363 Handle<Object>(use_runtime_context
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001364 ? top_context->builtins()
1365 : top_context->global());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001366 bool has_pending_exception;
lrn@chromium.org1c092762011-05-09 09:42:16 +00001367 Execution::Call(fun, receiver, 0, NULL, &has_pending_exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001368 if (has_pending_exception) return false;
ager@chromium.org5c838252010-02-19 08:53:10 +00001369 return true;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001370}
1371
1372
danno@chromium.org160a7b02011-04-18 15:51:38 +00001373#define INSTALL_NATIVE(Type, name, var) \
1374 Handle<String> var##_name = factory()->LookupAsciiSymbol(name); \
1375 Object* var##_native = \
1376 global_context()->builtins()->GetPropertyNoExceptionThrown( \
1377 *var##_name); \
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001378 global_context()->set_##var(Type::cast(var##_native));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001379
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001380
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001381void Genesis::InstallNativeFunctions() {
1382 HandleScope scope;
1383 INSTALL_NATIVE(JSFunction, "CreateDate", create_date_fun);
1384 INSTALL_NATIVE(JSFunction, "ToNumber", to_number_fun);
1385 INSTALL_NATIVE(JSFunction, "ToString", to_string_fun);
1386 INSTALL_NATIVE(JSFunction, "ToDetailString", to_detail_string_fun);
1387 INSTALL_NATIVE(JSFunction, "ToObject", to_object_fun);
1388 INSTALL_NATIVE(JSFunction, "ToInteger", to_integer_fun);
1389 INSTALL_NATIVE(JSFunction, "ToUint32", to_uint32_fun);
1390 INSTALL_NATIVE(JSFunction, "ToInt32", to_int32_fun);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00001391 INSTALL_NATIVE(JSFunction, "GlobalEval", global_eval_fun);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001392 INSTALL_NATIVE(JSFunction, "Instantiate", instantiate_fun);
1393 INSTALL_NATIVE(JSFunction, "ConfigureTemplateInstance",
1394 configure_instance_fun);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001395 INSTALL_NATIVE(JSFunction, "GetStackTraceLine", get_stack_trace_line_fun);
1396 INSTALL_NATIVE(JSObject, "functionCache", function_cache);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001397 INSTALL_NATIVE(JSFunction, "ToCompletePropertyDescriptor",
1398 to_complete_property_descriptor);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001399}
1400
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001401void Genesis::InstallExperimentalNativeFunctions() {
1402 if (FLAG_harmony_proxies) {
rossberg@chromium.org717967f2011-07-20 13:44:42 +00001403 INSTALL_NATIVE(JSFunction, "DerivedHasTrap", derived_has_trap);
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001404 INSTALL_NATIVE(JSFunction, "DerivedGetTrap", derived_get_trap);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00001405 INSTALL_NATIVE(JSFunction, "DerivedSetTrap", derived_set_trap);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001406 INSTALL_NATIVE(JSFunction, "ProxyEnumerate", proxy_enumerate);
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001407 }
1408}
1409
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001410#undef INSTALL_NATIVE
1411
1412
1413bool Genesis::InstallNatives() {
1414 HandleScope scope;
1415
1416 // Create a function for the builtins object. Allocate space for the
1417 // JavaScript builtins, a reference to the builtins object
1418 // (itself) and a reference to the global_context directly in the object.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001419 Handle<Code> code = Handle<Code>(
danno@chromium.org160a7b02011-04-18 15:51:38 +00001420 isolate()->builtins()->builtin(Builtins::kIllegal));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001421 Handle<JSFunction> builtins_fun =
danno@chromium.org160a7b02011-04-18 15:51:38 +00001422 factory()->NewFunction(factory()->empty_symbol(),
1423 JS_BUILTINS_OBJECT_TYPE,
1424 JSBuiltinsObject::kSize, code, true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001425
danno@chromium.org160a7b02011-04-18 15:51:38 +00001426 Handle<String> name = factory()->LookupAsciiSymbol("builtins");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001427 builtins_fun->shared()->set_instance_class_name(*name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001428
1429 // Allocate the builtins object.
1430 Handle<JSBuiltinsObject> builtins =
danno@chromium.org160a7b02011-04-18 15:51:38 +00001431 Handle<JSBuiltinsObject>::cast(factory()->NewGlobalObject(builtins_fun));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001432 builtins->set_builtins(*builtins);
1433 builtins->set_global_context(*global_context());
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001434 builtins->set_global_receiver(*builtins);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001435
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001436 // Set up the 'global' properties of the builtins object. The
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001437 // 'global' property that refers to the global object is the only
1438 // way to get from code running in the builtins context to the
1439 // global object.
1440 static const PropertyAttributes attributes =
1441 static_cast<PropertyAttributes>(READ_ONLY | DONT_DELETE);
danno@chromium.org160a7b02011-04-18 15:51:38 +00001442 Handle<String> global_symbol = factory()->LookupAsciiSymbol("global");
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +00001443 Handle<Object> global_obj(global_context()->global());
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001444 CHECK_NOT_EMPTY_HANDLE(isolate(),
1445 JSObject::SetLocalPropertyIgnoreAttributes(
1446 builtins, global_symbol, global_obj, attributes));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001447
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001448 // Set up the reference from the global object to the builtins object.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001449 JSGlobalObject::cast(global_context()->global())->set_builtins(*builtins);
1450
1451 // Create a bridge function that has context in the global context.
1452 Handle<JSFunction> bridge =
danno@chromium.org160a7b02011-04-18 15:51:38 +00001453 factory()->NewFunction(factory()->empty_symbol(),
1454 factory()->undefined_value());
1455 ASSERT(bridge->context() == *isolate()->global_context());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001456
1457 // Allocate the builtins context.
1458 Handle<Context> context =
danno@chromium.org160a7b02011-04-18 15:51:38 +00001459 factory()->NewFunctionContext(Context::MIN_CONTEXT_SLOTS, bridge);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001460 context->set_global(*builtins); // override builtins global object
1461
1462 global_context()->set_runtime_context(*context);
1463
1464 { // -- S c r i p t
1465 // Builtin functions for Script.
1466 Handle<JSFunction> script_fun =
1467 InstallFunction(builtins, "Script", JS_VALUE_TYPE, JSValue::kSize,
danno@chromium.org160a7b02011-04-18 15:51:38 +00001468 isolate()->initial_object_prototype(),
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00001469 Builtins::kIllegal, false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001470 Handle<JSObject> prototype =
danno@chromium.org160a7b02011-04-18 15:51:38 +00001471 factory()->NewJSObject(isolate()->object_function(), TENURED);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001472 SetPrototype(script_fun, prototype);
1473 global_context()->set_script_function(*script_fun);
1474
1475 // Add 'source' and 'data' property to scripts.
1476 PropertyAttributes common_attributes =
1477 static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE | READ_ONLY);
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001478 Handle<Foreign> foreign_source =
1479 factory()->NewForeign(&Accessors::ScriptSource);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001480 Handle<DescriptorArray> script_descriptors =
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001481 factory()->CopyAppendForeignDescriptor(
danno@chromium.org160a7b02011-04-18 15:51:38 +00001482 factory()->empty_descriptor_array(),
1483 factory()->LookupAsciiSymbol("source"),
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001484 foreign_source,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001485 common_attributes);
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001486 Handle<Foreign> foreign_name =
1487 factory()->NewForeign(&Accessors::ScriptName);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001488 script_descriptors =
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001489 factory()->CopyAppendForeignDescriptor(
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001490 script_descriptors,
danno@chromium.org160a7b02011-04-18 15:51:38 +00001491 factory()->LookupAsciiSymbol("name"),
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001492 foreign_name,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001493 common_attributes);
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001494 Handle<Foreign> foreign_id = factory()->NewForeign(&Accessors::ScriptId);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001495 script_descriptors =
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001496 factory()->CopyAppendForeignDescriptor(
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001497 script_descriptors,
danno@chromium.org160a7b02011-04-18 15:51:38 +00001498 factory()->LookupAsciiSymbol("id"),
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001499 foreign_id,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001500 common_attributes);
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001501 Handle<Foreign> foreign_line_offset =
1502 factory()->NewForeign(&Accessors::ScriptLineOffset);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001503 script_descriptors =
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001504 factory()->CopyAppendForeignDescriptor(
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001505 script_descriptors,
danno@chromium.org160a7b02011-04-18 15:51:38 +00001506 factory()->LookupAsciiSymbol("line_offset"),
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001507 foreign_line_offset,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001508 common_attributes);
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001509 Handle<Foreign> foreign_column_offset =
1510 factory()->NewForeign(&Accessors::ScriptColumnOffset);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001511 script_descriptors =
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001512 factory()->CopyAppendForeignDescriptor(
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001513 script_descriptors,
danno@chromium.org160a7b02011-04-18 15:51:38 +00001514 factory()->LookupAsciiSymbol("column_offset"),
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001515 foreign_column_offset,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001516 common_attributes);
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001517 Handle<Foreign> foreign_data =
1518 factory()->NewForeign(&Accessors::ScriptData);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001519 script_descriptors =
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001520 factory()->CopyAppendForeignDescriptor(
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001521 script_descriptors,
danno@chromium.org160a7b02011-04-18 15:51:38 +00001522 factory()->LookupAsciiSymbol("data"),
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001523 foreign_data,
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001524 common_attributes);
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001525 Handle<Foreign> foreign_type =
1526 factory()->NewForeign(&Accessors::ScriptType);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001527 script_descriptors =
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001528 factory()->CopyAppendForeignDescriptor(
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001529 script_descriptors,
danno@chromium.org160a7b02011-04-18 15:51:38 +00001530 factory()->LookupAsciiSymbol("type"),
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001531 foreign_type,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001532 common_attributes);
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001533 Handle<Foreign> foreign_compilation_type =
1534 factory()->NewForeign(&Accessors::ScriptCompilationType);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001535 script_descriptors =
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001536 factory()->CopyAppendForeignDescriptor(
ager@chromium.orge2902be2009-06-08 12:21:35 +00001537 script_descriptors,
danno@chromium.org160a7b02011-04-18 15:51:38 +00001538 factory()->LookupAsciiSymbol("compilation_type"),
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001539 foreign_compilation_type,
ager@chromium.orge2902be2009-06-08 12:21:35 +00001540 common_attributes);
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001541 Handle<Foreign> foreign_line_ends =
1542 factory()->NewForeign(&Accessors::ScriptLineEnds);
iposva@chromium.org245aa852009-02-10 00:49:54 +00001543 script_descriptors =
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001544 factory()->CopyAppendForeignDescriptor(
iposva@chromium.org245aa852009-02-10 00:49:54 +00001545 script_descriptors,
danno@chromium.org160a7b02011-04-18 15:51:38 +00001546 factory()->LookupAsciiSymbol("line_ends"),
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001547 foreign_line_ends,
iposva@chromium.org245aa852009-02-10 00:49:54 +00001548 common_attributes);
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001549 Handle<Foreign> foreign_context_data =
1550 factory()->NewForeign(&Accessors::ScriptContextData);
ager@chromium.org9085a012009-05-11 19:22:57 +00001551 script_descriptors =
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001552 factory()->CopyAppendForeignDescriptor(
ager@chromium.org9085a012009-05-11 19:22:57 +00001553 script_descriptors,
danno@chromium.org160a7b02011-04-18 15:51:38 +00001554 factory()->LookupAsciiSymbol("context_data"),
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001555 foreign_context_data,
ager@chromium.org9085a012009-05-11 19:22:57 +00001556 common_attributes);
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001557 Handle<Foreign> foreign_eval_from_script =
1558 factory()->NewForeign(&Accessors::ScriptEvalFromScript);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001559 script_descriptors =
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001560 factory()->CopyAppendForeignDescriptor(
ager@chromium.orge2902be2009-06-08 12:21:35 +00001561 script_descriptors,
danno@chromium.org160a7b02011-04-18 15:51:38 +00001562 factory()->LookupAsciiSymbol("eval_from_script"),
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001563 foreign_eval_from_script,
ager@chromium.orge2902be2009-06-08 12:21:35 +00001564 common_attributes);
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001565 Handle<Foreign> foreign_eval_from_script_position =
1566 factory()->NewForeign(&Accessors::ScriptEvalFromScriptPosition);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001567 script_descriptors =
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001568 factory()->CopyAppendForeignDescriptor(
ager@chromium.orge2902be2009-06-08 12:21:35 +00001569 script_descriptors,
danno@chromium.org160a7b02011-04-18 15:51:38 +00001570 factory()->LookupAsciiSymbol("eval_from_script_position"),
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001571 foreign_eval_from_script_position,
sgjesse@chromium.org98180592009-12-02 08:17:28 +00001572 common_attributes);
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001573 Handle<Foreign> foreign_eval_from_function_name =
1574 factory()->NewForeign(&Accessors::ScriptEvalFromFunctionName);
sgjesse@chromium.org98180592009-12-02 08:17:28 +00001575 script_descriptors =
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001576 factory()->CopyAppendForeignDescriptor(
sgjesse@chromium.org98180592009-12-02 08:17:28 +00001577 script_descriptors,
danno@chromium.org160a7b02011-04-18 15:51:38 +00001578 factory()->LookupAsciiSymbol("eval_from_function_name"),
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001579 foreign_eval_from_function_name,
ager@chromium.orge2902be2009-06-08 12:21:35 +00001580 common_attributes);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001581
1582 Handle<Map> script_map = Handle<Map>(script_fun->initial_map());
1583 script_map->set_instance_descriptors(*script_descriptors);
1584
1585 // Allocate the empty script.
danno@chromium.org160a7b02011-04-18 15:51:38 +00001586 Handle<Script> script = factory()->NewScript(factory()->empty_string());
ager@chromium.orge2902be2009-06-08 12:21:35 +00001587 script->set_type(Smi::FromInt(Script::TYPE_NATIVE));
danno@chromium.org160a7b02011-04-18 15:51:38 +00001588 heap()->public_set_empty_script(*script);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001589 }
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001590 {
1591 // Builtin function for OpaqueReference -- a JSValue-based object,
1592 // that keeps its field isolated from JavaScript code. It may store
1593 // objects, that JavaScript code may not access.
1594 Handle<JSFunction> opaque_reference_fun =
1595 InstallFunction(builtins, "OpaqueReference", JS_VALUE_TYPE,
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001596 JSValue::kSize,
danno@chromium.org160a7b02011-04-18 15:51:38 +00001597 isolate()->initial_object_prototype(),
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00001598 Builtins::kIllegal, false);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001599 Handle<JSObject> prototype =
danno@chromium.org160a7b02011-04-18 15:51:38 +00001600 factory()->NewJSObject(isolate()->object_function(), TENURED);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001601 SetPrototype(opaque_reference_fun, prototype);
1602 global_context()->set_opaque_reference_function(*opaque_reference_fun);
1603 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001604
karlklose@chromium.org8f806e82011-03-07 14:06:08 +00001605 { // --- I n t e r n a l A r r a y ---
1606 // An array constructor on the builtins object that works like
1607 // the public Array constructor, except that its prototype
1608 // doesn't inherit from Object.prototype.
1609 // To be used only for internal work by builtins. Instances
1610 // must not be leaked to user code.
karlklose@chromium.org8f806e82011-03-07 14:06:08 +00001611 Handle<JSFunction> array_function =
1612 InstallFunction(builtins,
1613 "InternalArray",
1614 JS_ARRAY_TYPE,
1615 JSArray::kSize,
danno@chromium.org160a7b02011-04-18 15:51:38 +00001616 isolate()->initial_object_prototype(),
svenpanne@chromium.org3c93e772012-01-02 09:26:59 +00001617 Builtins::kInternalArrayCode,
karlklose@chromium.org8f806e82011-03-07 14:06:08 +00001618 true);
1619 Handle<JSObject> prototype =
danno@chromium.org160a7b02011-04-18 15:51:38 +00001620 factory()->NewJSObject(isolate()->object_function(), TENURED);
karlklose@chromium.org8f806e82011-03-07 14:06:08 +00001621 SetPrototype(array_function, prototype);
1622
1623 array_function->shared()->set_construct_stub(
danno@chromium.org160a7b02011-04-18 15:51:38 +00001624 isolate()->builtins()->builtin(Builtins::kArrayConstructCode));
karlklose@chromium.org8f806e82011-03-07 14:06:08 +00001625 array_function->shared()->DontAdaptArguments();
1626
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001627 // InternalArrays should not use Smi-Only array optimizations. There are too
1628 // many places in the C++ runtime code (e.g. RegEx) that assume that
1629 // elements in InternalArrays can be set to non-Smi values without going
1630 // through a common bottleneck that would make the SMI_ONLY -> FAST_ELEMENT
1631 // transition easy to trap. Moreover, they rarely are smi-only.
1632 MaybeObject* maybe_map =
verwaest@chromium.org753aee42012-07-17 16:15:42 +00001633 array_function->initial_map()->Copy(DescriptorArray::MAY_BE_SHARED);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001634 Map* new_map;
mmassi@chromium.org7028c052012-06-13 11:51:58 +00001635 if (!maybe_map->To(&new_map)) return false;
svenpanne@chromium.org830d30c2012-05-29 13:20:14 +00001636 new_map->set_elements_kind(FAST_HOLEY_ELEMENTS);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001637 array_function->set_initial_map(new_map);
1638
karlklose@chromium.org8f806e82011-03-07 14:06:08 +00001639 // Make "length" magic on instances.
1640 Handle<DescriptorArray> array_descriptors =
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001641 factory()->CopyAppendForeignDescriptor(
danno@chromium.org160a7b02011-04-18 15:51:38 +00001642 factory()->empty_descriptor_array(),
1643 factory()->length_symbol(),
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001644 factory()->NewForeign(&Accessors::ArrayLength),
karlklose@chromium.org8f806e82011-03-07 14:06:08 +00001645 static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE));
1646
1647 array_function->initial_map()->set_instance_descriptors(
1648 *array_descriptors);
svenpanne@chromium.org3c93e772012-01-02 09:26:59 +00001649
1650 global_context()->set_internal_array_function(*array_function);
karlklose@chromium.org8f806e82011-03-07 14:06:08 +00001651 }
1652
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001653 if (FLAG_disable_native_files) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001654 PrintF("Warning: Running without installed natives!\n");
1655 return true;
1656 }
1657
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001658 // Install natives.
1659 for (int i = Natives::GetDebuggerCount();
1660 i < Natives::GetBuiltinsCount();
1661 i++) {
danno@chromium.org160a7b02011-04-18 15:51:38 +00001662 if (!CompileBuiltin(isolate(), i)) return false;
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001663 // TODO(ager): We really only need to install the JS builtin
1664 // functions on the builtins object after compiling and running
1665 // runtime.js.
1666 if (!InstallJSBuiltins(builtins)) return false;
1667 }
1668
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001669 InstallNativeFunctions();
1670
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00001671 // Store the map for the string prototype after the natives has been compiled
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001672 // and the String function has been set up.
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00001673 Handle<JSFunction> string_function(global_context()->string_function());
1674 ASSERT(JSObject::cast(
1675 string_function->initial_map()->prototype())->HasFastProperties());
1676 global_context()->set_string_function_prototype_map(
1677 HeapObject::cast(string_function->initial_map()->prototype())->map());
1678
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001679 // Install Function.prototype.call and apply.
danno@chromium.org160a7b02011-04-18 15:51:38 +00001680 { Handle<String> key = factory()->function_class_symbol();
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001681 Handle<JSFunction> function =
danno@chromium.org160a7b02011-04-18 15:51:38 +00001682 Handle<JSFunction>::cast(GetProperty(isolate()->global(), key));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001683 Handle<JSObject> proto =
1684 Handle<JSObject>(JSObject::cast(function->instance_prototype()));
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001685
1686 // Install the call and the apply functions.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001687 Handle<JSFunction> call =
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001688 InstallFunction(proto, "call", JS_OBJECT_TYPE, JSObject::kHeaderSize,
kmillikin@chromium.org4111b802010-05-03 10:34:42 +00001689 Handle<JSObject>::null(),
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00001690 Builtins::kFunctionCall,
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001691 false);
1692 Handle<JSFunction> apply =
1693 InstallFunction(proto, "apply", JS_OBJECT_TYPE, JSObject::kHeaderSize,
kmillikin@chromium.org4111b802010-05-03 10:34:42 +00001694 Handle<JSObject>::null(),
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00001695 Builtins::kFunctionApply,
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001696 false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001697
1698 // Make sure that Function.prototype.call appears to be compiled.
1699 // The code will never be called, but inline caching for call will
1700 // only work if it appears to be compiled.
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001701 call->shared()->DontAdaptArguments();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001702 ASSERT(call->is_compiled());
1703
ager@chromium.org32912102009-01-16 10:38:43 +00001704 // Set the expected parameters for apply to 2; required by builtin.
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001705 apply->shared()->set_formal_parameter_count(2);
1706
1707 // Set the lengths for the functions to satisfy ECMA-262.
1708 call->shared()->set_length(1);
1709 apply->shared()->set_length(2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001710 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001711
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001712 InstallBuiltinFunctionIds();
1713
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00001714 // Create a constructor for RegExp results (a variant of Array that
1715 // predefines the two properties index and match).
1716 {
1717 // RegExpResult initial map.
1718
1719 // Find global.Array.prototype to inherit from.
1720 Handle<JSFunction> array_constructor(global_context()->array_function());
1721 Handle<JSObject> array_prototype(
1722 JSObject::cast(array_constructor->instance_prototype()));
1723
1724 // Add initial map.
1725 Handle<Map> initial_map =
danno@chromium.org160a7b02011-04-18 15:51:38 +00001726 factory()->NewMap(JS_ARRAY_TYPE, JSRegExpResult::kSize);
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00001727 initial_map->set_constructor(*array_constructor);
1728
1729 // Set prototype on map.
1730 initial_map->set_non_instance_prototype(false);
1731 initial_map->set_prototype(*array_prototype);
1732
1733 // Update map with length accessor from Array and add "index" and "input".
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00001734 Handle<DescriptorArray> reresult_descriptors =
danno@chromium.org160a7b02011-04-18 15:51:38 +00001735 factory()->NewDescriptorArray(3);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001736 DescriptorArray::WhitenessWitness witness(*reresult_descriptors);
1737
danno@chromium.orgfa458e42012-02-01 10:48:36 +00001738 JSFunction* array_function = global_context()->array_function();
1739 Handle<DescriptorArray> array_descriptors(
1740 array_function->initial_map()->instance_descriptors());
rossberg@chromium.org657d53b2012-07-12 11:06:03 +00001741 int old = array_descriptors->SearchWithCache(heap()->length_symbol());
verwaest@chromium.org753aee42012-07-17 16:15:42 +00001742 reresult_descriptors->CopyFrom(0, *array_descriptors, old, witness);
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00001743
jkummerow@chromium.org28583c92012-07-16 11:31:55 +00001744 reresult_descriptors->SetLastAdded(0);
1745
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00001746 {
danno@chromium.org160a7b02011-04-18 15:51:38 +00001747 FieldDescriptor index_field(heap()->index_symbol(),
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00001748 JSRegExpResult::kIndexIndex,
jkummerow@chromium.org28583c92012-07-16 11:31:55 +00001749 NONE);
1750 reresult_descriptors->Append(&index_field, witness);
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00001751 }
1752
1753 {
danno@chromium.org160a7b02011-04-18 15:51:38 +00001754 FieldDescriptor input_field(heap()->input_symbol(),
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00001755 JSRegExpResult::kInputIndex,
jkummerow@chromium.org28583c92012-07-16 11:31:55 +00001756 NONE);
1757 reresult_descriptors->Append(&input_field, witness);
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00001758 }
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001759 reresult_descriptors->Sort(witness);
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00001760
1761 initial_map->set_inobject_properties(2);
1762 initial_map->set_pre_allocated_property_fields(2);
1763 initial_map->set_unused_property_fields(0);
1764 initial_map->set_instance_descriptors(*reresult_descriptors);
1765
1766 global_context()->set_regexp_result_map(*initial_map);
1767 }
1768
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001769#ifdef DEBUG
1770 builtins->Verify();
1771#endif
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001772
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001773 return true;
1774}
1775
1776
danno@chromium.org160a7b02011-04-18 15:51:38 +00001777bool Genesis::InstallExperimentalNatives() {
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001778 for (int i = ExperimentalNatives::GetDebuggerCount();
1779 i < ExperimentalNatives::GetBuiltinsCount();
1780 i++) {
1781 if (FLAG_harmony_proxies &&
1782 strcmp(ExperimentalNatives::GetScriptName(i).start(),
1783 "native proxy.js") == 0) {
danno@chromium.org160a7b02011-04-18 15:51:38 +00001784 if (!CompileExperimentalBuiltin(isolate(), i)) return false;
1785 }
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001786 if (FLAG_harmony_collections &&
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +00001787 strcmp(ExperimentalNatives::GetScriptName(i).start(),
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001788 "native collection.js") == 0) {
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +00001789 if (!CompileExperimentalBuiltin(isolate(), i)) return false;
1790 }
danno@chromium.org160a7b02011-04-18 15:51:38 +00001791 }
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001792
1793 InstallExperimentalNativeFunctions();
1794
danno@chromium.org160a7b02011-04-18 15:51:38 +00001795 return true;
1796}
1797
1798
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001799static Handle<JSObject> ResolveBuiltinIdHolder(
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001800 Handle<Context> global_context,
1801 const char* holder_expr) {
danno@chromium.org160a7b02011-04-18 15:51:38 +00001802 Factory* factory = global_context->GetIsolate()->factory();
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001803 Handle<GlobalObject> global(global_context->global());
1804 const char* period_pos = strchr(holder_expr, '.');
1805 if (period_pos == NULL) {
1806 return Handle<JSObject>::cast(
lrn@chromium.org7516f052011-03-30 08:52:27 +00001807 GetProperty(global, factory->LookupAsciiSymbol(holder_expr)));
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00001808 }
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001809 ASSERT_EQ(".prototype", period_pos);
1810 Vector<const char> property(holder_expr,
1811 static_cast<int>(period_pos - holder_expr));
1812 Handle<JSFunction> function = Handle<JSFunction>::cast(
lrn@chromium.org7516f052011-03-30 08:52:27 +00001813 GetProperty(global, factory->LookupSymbol(property)));
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001814 return Handle<JSObject>(JSObject::cast(function->prototype()));
1815}
1816
1817
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001818static void InstallBuiltinFunctionId(Handle<JSObject> holder,
1819 const char* function_name,
1820 BuiltinFunctionId id) {
danno@chromium.org160a7b02011-04-18 15:51:38 +00001821 Factory* factory = holder->GetIsolate()->factory();
1822 Handle<String> name = factory->LookupAsciiSymbol(function_name);
lrn@chromium.org303ada72010-10-27 09:33:13 +00001823 Object* function_object = holder->GetProperty(*name)->ToObjectUnchecked();
1824 Handle<JSFunction> function(JSFunction::cast(function_object));
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001825 function->shared()->set_function_data(Smi::FromInt(id));
1826}
1827
1828
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001829void Genesis::InstallBuiltinFunctionIds() {
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001830 HandleScope scope;
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001831#define INSTALL_BUILTIN_ID(holder_expr, fun_name, name) \
1832 { \
1833 Handle<JSObject> holder = ResolveBuiltinIdHolder( \
1834 global_context(), #holder_expr); \
1835 BuiltinFunctionId id = k##name; \
1836 InstallBuiltinFunctionId(holder, #fun_name, id); \
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001837 }
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001838 FUNCTIONS_WITH_ID_LIST(INSTALL_BUILTIN_ID)
1839#undef INSTALL_BUILTIN_ID
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001840}
1841
1842
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00001843// Do not forget to update macros.py with named constant
1844// of cache id.
1845#define JSFUNCTION_RESULT_CACHE_LIST(F) \
1846 F(16, global_context()->regexp_function())
1847
1848
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001849static FixedArray* CreateCache(int size, Handle<JSFunction> factory_function) {
danno@chromium.org160a7b02011-04-18 15:51:38 +00001850 Factory* factory = factory_function->GetIsolate()->factory();
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00001851 // Caches are supposed to live for a long time, allocate in old space.
1852 int array_size = JSFunctionResultCache::kEntriesIndex + 2 * size;
ager@chromium.orgac091b72010-05-05 07:34:42 +00001853 // Cannot use cast as object is not fully initialized yet.
1854 JSFunctionResultCache* cache = reinterpret_cast<JSFunctionResultCache*>(
danno@chromium.org160a7b02011-04-18 15:51:38 +00001855 *factory->NewFixedArrayWithHoles(array_size, TENURED));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001856 cache->set(JSFunctionResultCache::kFactoryIndex, *factory_function);
ager@chromium.orgac091b72010-05-05 07:34:42 +00001857 cache->MakeZeroSize();
1858 return cache;
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00001859}
1860
1861
1862void Genesis::InstallJSFunctionResultCaches() {
1863 const int kNumberOfCaches = 0 +
1864#define F(size, func) + 1
1865 JSFUNCTION_RESULT_CACHE_LIST(F)
1866#undef F
1867 ;
1868
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001869 Handle<FixedArray> caches = FACTORY->NewFixedArray(kNumberOfCaches, TENURED);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00001870
1871 int index = 0;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001872
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001873#define F(size, func) do { \
1874 FixedArray* cache = CreateCache((size), Handle<JSFunction>(func)); \
1875 caches->set(index++, cache); \
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001876 } while (false)
1877
1878 JSFUNCTION_RESULT_CACHE_LIST(F);
1879
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00001880#undef F
1881
1882 global_context()->set_jsfunction_result_caches(*caches);
1883}
1884
1885
ricow@chromium.org65fae842010-08-25 15:26:24 +00001886void Genesis::InitializeNormalizedMapCaches() {
1887 Handle<FixedArray> array(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001888 FACTORY->NewFixedArray(NormalizedMapCache::kEntries, TENURED));
ricow@chromium.org65fae842010-08-25 15:26:24 +00001889 global_context()->set_normalized_map_cache(NormalizedMapCache::cast(*array));
1890}
1891
1892
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001893bool Bootstrapper::InstallExtensions(Handle<Context> global_context,
1894 v8::ExtensionConfiguration* extensions) {
danno@chromium.org160a7b02011-04-18 15:51:38 +00001895 Isolate* isolate = global_context->GetIsolate();
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001896 BootstrapperActive active;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001897 SaveContext saved_context(isolate);
1898 isolate->set_context(*global_context);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001899 if (!Genesis::InstallExtensions(global_context, extensions)) return false;
1900 Genesis::InstallSpecialObjects(global_context);
1901 return true;
1902}
1903
1904
1905void Genesis::InstallSpecialObjects(Handle<Context> global_context) {
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001906 Isolate* isolate = global_context->GetIsolate();
1907 Factory* factory = isolate->factory();
mads.s.agercbaa0602008-08-14 13:41:48 +00001908 HandleScope scope;
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001909 Handle<JSGlobalObject> global(JSGlobalObject::cast(global_context->global()));
mads.s.agercbaa0602008-08-14 13:41:48 +00001910 // Expose the natives in global if a name for it is specified.
1911 if (FLAG_expose_natives_as != NULL && strlen(FLAG_expose_natives_as) != 0) {
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001912 Handle<String> natives = factory->LookupAsciiSymbol(FLAG_expose_natives_as);
1913 CHECK_NOT_EMPTY_HANDLE(isolate,
1914 JSObject::SetLocalPropertyIgnoreAttributes(
1915 global, natives,
1916 Handle<JSObject>(global->builtins()),
1917 DONT_ENUM));
mads.s.agercbaa0602008-08-14 13:41:48 +00001918 }
1919
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001920 Handle<Object> Error = GetProperty(global, "Error");
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00001921 if (Error->IsJSObject()) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00001922 Handle<String> name = factory->LookupAsciiSymbol("stackTraceLimit");
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001923 Handle<Smi> stack_trace_limit(Smi::FromInt(FLAG_stack_trace_limit));
1924 CHECK_NOT_EMPTY_HANDLE(isolate,
1925 JSObject::SetLocalPropertyIgnoreAttributes(
1926 Handle<JSObject>::cast(Error), name,
1927 stack_trace_limit, NONE));
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001928 }
1929
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001930#ifdef ENABLE_DEBUGGER_SUPPORT
mads.s.agercbaa0602008-08-14 13:41:48 +00001931 // Expose the debug global object in global if a name for it is specified.
1932 if (FLAG_expose_debug_as != NULL && strlen(FLAG_expose_debug_as) != 0) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001933 Debug* debug = Isolate::Current()->debug();
mads.s.agercbaa0602008-08-14 13:41:48 +00001934 // If loading fails we just bail out without installing the
1935 // debugger but without tanking the whole context.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001936 if (!debug->Load()) return;
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001937 // Set the security token for the debugger context to the same as
1938 // the shell global context to allow calling between these (otherwise
1939 // exposing debug global object doesn't make much sense).
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001940 debug->debug_context()->set_security_token(
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001941 global_context->security_token());
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001942
mads.s.agercbaa0602008-08-14 13:41:48 +00001943 Handle<String> debug_string =
lrn@chromium.org7516f052011-03-30 08:52:27 +00001944 factory->LookupAsciiSymbol(FLAG_expose_debug_as);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001945 Handle<Object> global_proxy(debug->debug_context()->global_proxy());
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001946 CHECK_NOT_EMPTY_HANDLE(isolate,
1947 JSObject::SetLocalPropertyIgnoreAttributes(
1948 global, debug_string, global_proxy, DONT_ENUM));
mads.s.agercbaa0602008-08-14 13:41:48 +00001949 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001950#endif
mads.s.agercbaa0602008-08-14 13:41:48 +00001951}
1952
ricow@chromium.org27bf2882011-11-17 08:34:43 +00001953static uint32_t Hash(RegisteredExtension* extension) {
1954 return v8::internal::ComputePointerHash(extension);
1955}
1956
1957static bool MatchRegisteredExtensions(void* key1, void* key2) {
1958 return key1 == key2;
1959}
1960
1961Genesis::ExtensionStates::ExtensionStates()
yangguo@chromium.orgab30bb82012-02-24 14:41:46 +00001962 : map_(MatchRegisteredExtensions, 8) { }
ricow@chromium.org27bf2882011-11-17 08:34:43 +00001963
1964Genesis::ExtensionTraversalState Genesis::ExtensionStates::get_state(
1965 RegisteredExtension* extension) {
1966 i::HashMap::Entry* entry = map_.Lookup(extension, Hash(extension), false);
1967 if (entry == NULL) {
1968 return UNVISITED;
1969 }
1970 return static_cast<ExtensionTraversalState>(
1971 reinterpret_cast<intptr_t>(entry->value));
1972}
1973
1974void Genesis::ExtensionStates::set_state(RegisteredExtension* extension,
1975 ExtensionTraversalState state) {
1976 map_.Lookup(extension, Hash(extension), true)->value =
1977 reinterpret_cast<void*>(static_cast<intptr_t>(state));
1978}
mads.s.agercbaa0602008-08-14 13:41:48 +00001979
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001980bool Genesis::InstallExtensions(Handle<Context> global_context,
1981 v8::ExtensionConfiguration* extensions) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001982 // TODO(isolates): Extensions on multiple isolates may take a little more
1983 // effort. (The external API reads 'ignore'-- does that mean
1984 // we can break the interface?)
1985
ricow@chromium.org27bf2882011-11-17 08:34:43 +00001986
1987 ExtensionStates extension_states; // All extensions have state UNVISITED.
1988 // Install auto extensions.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001989 v8::RegisteredExtension* current = v8::RegisteredExtension::first_extension();
1990 while (current != NULL) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001991 if (current->extension()->auto_enable())
ricow@chromium.org27bf2882011-11-17 08:34:43 +00001992 InstallExtension(current, &extension_states);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001993 current = current->next();
1994 }
1995
ricow@chromium.org27bf2882011-11-17 08:34:43 +00001996 if (FLAG_expose_gc) InstallExtension("v8/gc", &extension_states);
1997 if (FLAG_expose_externalize_string) {
1998 InstallExtension("v8/externalize", &extension_states);
1999 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002000
2001 if (extensions == NULL) return true;
2002 // Install required extensions
2003 int count = v8::ImplementationUtilities::GetNameCount(extensions);
2004 const char** names = v8::ImplementationUtilities::GetNames(extensions);
2005 for (int i = 0; i < count; i++) {
ricow@chromium.org27bf2882011-11-17 08:34:43 +00002006 if (!InstallExtension(names[i], &extension_states))
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002007 return false;
2008 }
2009
2010 return true;
2011}
2012
2013
2014// Installs a named extension. This methods is unoptimized and does
2015// not scale well if we want to support a large number of extensions.
ricow@chromium.org27bf2882011-11-17 08:34:43 +00002016bool Genesis::InstallExtension(const char* name,
2017 ExtensionStates* extension_states) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002018 v8::RegisteredExtension* current = v8::RegisteredExtension::first_extension();
2019 // Loop until we find the relevant extension
2020 while (current != NULL) {
2021 if (strcmp(name, current->extension()->name()) == 0) break;
2022 current = current->next();
2023 }
2024 // Didn't find the extension; fail.
2025 if (current == NULL) {
2026 v8::Utils::ReportApiFailure(
2027 "v8::Context::New()", "Cannot find required extension");
2028 return false;
2029 }
ricow@chromium.org27bf2882011-11-17 08:34:43 +00002030 return InstallExtension(current, extension_states);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002031}
2032
2033
ricow@chromium.org27bf2882011-11-17 08:34:43 +00002034bool Genesis::InstallExtension(v8::RegisteredExtension* current,
2035 ExtensionStates* extension_states) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002036 HandleScope scope;
2037
ricow@chromium.org27bf2882011-11-17 08:34:43 +00002038 if (extension_states->get_state(current) == INSTALLED) return true;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002039 // The current node has already been visited so there must be a
2040 // cycle in the dependency graph; fail.
ricow@chromium.org27bf2882011-11-17 08:34:43 +00002041 if (extension_states->get_state(current) == VISITED) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002042 v8::Utils::ReportApiFailure(
2043 "v8::Context::New()", "Circular extension dependency");
2044 return false;
2045 }
ricow@chromium.org27bf2882011-11-17 08:34:43 +00002046 ASSERT(extension_states->get_state(current) == UNVISITED);
2047 extension_states->set_state(current, VISITED);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002048 v8::Extension* extension = current->extension();
2049 // Install the extension's dependencies
2050 for (int i = 0; i < extension->dependency_count(); i++) {
ricow@chromium.org27bf2882011-11-17 08:34:43 +00002051 if (!InstallExtension(extension->dependencies()[i], extension_states))
2052 return false;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002053 }
lrn@chromium.org7516f052011-03-30 08:52:27 +00002054 Isolate* isolate = Isolate::Current();
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002055 Handle<String> source_code =
2056 isolate->factory()->NewExternalStringFromAscii(extension->source());
2057 bool result = CompileScriptCached(
2058 CStrVector(extension->name()),
2059 source_code,
2060 isolate->bootstrapper()->extensions_cache(),
2061 extension,
2062 Handle<Context>(isolate->context()),
2063 false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002064 ASSERT(isolate->has_pending_exception() != result);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002065 if (!result) {
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002066 // We print out the name of the extension that fail to install.
2067 // When an error is thrown during bootstrapping we automatically print
2068 // the line number at which this happened to the console in the isolate
2069 // error throwing functionality.
2070 OS::PrintError("Error installing extension '%s'.\n",
2071 current->extension()->name());
lrn@chromium.org7516f052011-03-30 08:52:27 +00002072 isolate->clear_pending_exception();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002073 }
ricow@chromium.org27bf2882011-11-17 08:34:43 +00002074 extension_states->set_state(current, INSTALLED);
2075 isolate->NotifyExtensionInstalled();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002076 return result;
2077}
2078
2079
ager@chromium.org5c838252010-02-19 08:53:10 +00002080bool Genesis::InstallJSBuiltins(Handle<JSBuiltinsObject> builtins) {
2081 HandleScope scope;
danno@chromium.org160a7b02011-04-18 15:51:38 +00002082 Factory* factory = builtins->GetIsolate()->factory();
ager@chromium.org5c838252010-02-19 08:53:10 +00002083 for (int i = 0; i < Builtins::NumberOfJavaScriptBuiltins(); i++) {
2084 Builtins::JavaScript id = static_cast<Builtins::JavaScript>(i);
danno@chromium.org160a7b02011-04-18 15:51:38 +00002085 Handle<String> name = factory->LookupAsciiSymbol(Builtins::GetName(id));
lrn@chromium.org303ada72010-10-27 09:33:13 +00002086 Object* function_object = builtins->GetPropertyNoExceptionThrown(*name);
ager@chromium.org5c838252010-02-19 08:53:10 +00002087 Handle<JSFunction> function
lrn@chromium.org303ada72010-10-27 09:33:13 +00002088 = Handle<JSFunction>(JSFunction::cast(function_object));
ager@chromium.org5c838252010-02-19 08:53:10 +00002089 builtins->set_javascript_builtin(id, *function);
rossberg@chromium.org400388e2012-06-06 09:29:22 +00002090 if (!JSFunction::CompileLazy(function, CLEAR_EXCEPTION)) {
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002091 return false;
2092 }
rossberg@chromium.org400388e2012-06-06 09:29:22 +00002093 builtins->set_javascript_builtin_code(id, function->shared()->code());
ager@chromium.org5c838252010-02-19 08:53:10 +00002094 }
2095 return true;
2096}
2097
2098
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002099bool Genesis::ConfigureGlobalObjects(
2100 v8::Handle<v8::ObjectTemplate> global_proxy_template) {
2101 Handle<JSObject> global_proxy(
2102 JSObject::cast(global_context()->global_proxy()));
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002103 Handle<JSObject> inner_global(JSObject::cast(global_context()->global()));
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002104
2105 if (!global_proxy_template.IsEmpty()) {
2106 // Configure the global proxy object.
2107 Handle<ObjectTemplateInfo> proxy_data =
2108 v8::Utils::OpenHandle(*global_proxy_template);
2109 if (!ConfigureApiObject(global_proxy, proxy_data)) return false;
2110
2111 // Configure the inner global object.
2112 Handle<FunctionTemplateInfo> proxy_constructor(
2113 FunctionTemplateInfo::cast(proxy_data->constructor()));
2114 if (!proxy_constructor->prototype_template()->IsUndefined()) {
2115 Handle<ObjectTemplateInfo> inner_data(
2116 ObjectTemplateInfo::cast(proxy_constructor->prototype_template()));
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002117 if (!ConfigureApiObject(inner_global, inner_data)) return false;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002118 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002119 }
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002120
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002121 SetObjectPrototype(global_proxy, inner_global);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002122 return true;
2123}
2124
2125
2126bool Genesis::ConfigureApiObject(Handle<JSObject> object,
2127 Handle<ObjectTemplateInfo> object_template) {
2128 ASSERT(!object_template.is_null());
2129 ASSERT(object->IsInstanceOf(
2130 FunctionTemplateInfo::cast(object_template->constructor())));
2131
2132 bool pending_exception = false;
2133 Handle<JSObject> obj =
2134 Execution::InstantiateObject(object_template, &pending_exception);
2135 if (pending_exception) {
danno@chromium.org160a7b02011-04-18 15:51:38 +00002136 ASSERT(isolate()->has_pending_exception());
2137 isolate()->clear_pending_exception();
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002138 return false;
2139 }
2140 TransferObject(obj, object);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002141 return true;
2142}
2143
2144
2145void Genesis::TransferNamedProperties(Handle<JSObject> from,
2146 Handle<JSObject> to) {
2147 if (from->HasFastProperties()) {
2148 Handle<DescriptorArray> descs =
2149 Handle<DescriptorArray>(from->map()->instance_descriptors());
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002150 for (int i = 0; i < descs->number_of_descriptors(); i++) {
erik.corry@gmail.comed49e962012-04-17 11:57:53 +00002151 PropertyDetails details = descs->GetDetails(i);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002152 switch (details.type()) {
2153 case FIELD: {
2154 HandleScope inner;
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002155 Handle<String> key = Handle<String>(descs->GetKey(i));
2156 int index = descs->GetFieldIndex(i);
ager@chromium.org7c537e22008-10-16 08:43:32 +00002157 Handle<Object> value = Handle<Object>(from->FastPropertyAt(index));
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00002158 CHECK_NOT_EMPTY_HANDLE(to->GetIsolate(),
2159 JSObject::SetLocalPropertyIgnoreAttributes(
2160 to, key, value, details.attributes()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002161 break;
2162 }
2163 case CONSTANT_FUNCTION: {
2164 HandleScope inner;
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002165 Handle<String> key = Handle<String>(descs->GetKey(i));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002166 Handle<JSFunction> fun =
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002167 Handle<JSFunction>(descs->GetConstantFunction(i));
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00002168 CHECK_NOT_EMPTY_HANDLE(to->GetIsolate(),
2169 JSObject::SetLocalPropertyIgnoreAttributes(
2170 to, key, fun, details.attributes()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002171 break;
2172 }
2173 case CALLBACKS: {
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002174 LookupResult result(isolate());
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002175 to->LocalLookup(descs->GetKey(i), &result);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002176 // If the property is already there we skip it
verwaest@chromium.org753aee42012-07-17 16:15:42 +00002177 if (result.IsFound()) continue;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002178 HandleScope inner;
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002179 ASSERT(!to->HasFastProperties());
2180 // Add to dictionary.
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002181 Handle<String> key = Handle<String>(descs->GetKey(i));
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002182 Handle<Object> callbacks(descs->GetCallbacksObject(i));
2183 PropertyDetails d =
2184 PropertyDetails(details.attributes(), CALLBACKS, details.index());
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00002185 JSObject::SetNormalizedProperty(to, key, callbacks, d);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002186 break;
2187 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002188 case NORMAL:
2189 // Do not occur since the from object has fast properties.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002190 case HANDLER:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002191 case INTERCEPTOR:
yangguo@chromium.org99aa4902012-07-06 16:21:55 +00002192 case TRANSITION:
jkummerow@chromium.org7a6fc812012-06-27 11:12:38 +00002193 case NONEXISTENT:
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002194 // No element in instance descriptors have proxy or interceptor type.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002195 UNREACHABLE();
2196 break;
2197 }
2198 }
2199 } else {
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00002200 Handle<StringDictionary> properties =
2201 Handle<StringDictionary>(from->property_dictionary());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002202 int capacity = properties->Capacity();
2203 for (int i = 0; i < capacity; i++) {
2204 Object* raw_key(properties->KeyAt(i));
2205 if (properties->IsKey(raw_key)) {
2206 ASSERT(raw_key->IsString());
2207 // If the property is already there we skip it.
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002208 LookupResult result(isolate());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002209 to->LocalLookup(String::cast(raw_key), &result);
verwaest@chromium.org753aee42012-07-17 16:15:42 +00002210 if (result.IsFound()) continue;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002211 // Set the property.
2212 Handle<String> key = Handle<String>(String::cast(raw_key));
2213 Handle<Object> value = Handle<Object>(properties->ValueAt(i));
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002214 if (value->IsJSGlobalPropertyCell()) {
2215 value = Handle<Object>(JSGlobalPropertyCell::cast(*value)->value());
2216 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002217 PropertyDetails details = properties->DetailsAt(i);
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00002218 CHECK_NOT_EMPTY_HANDLE(to->GetIsolate(),
2219 JSObject::SetLocalPropertyIgnoreAttributes(
2220 to, key, value, details.attributes()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002221 }
2222 }
2223 }
2224}
2225
2226
2227void Genesis::TransferIndexedProperties(Handle<JSObject> from,
2228 Handle<JSObject> to) {
2229 // Cloning the elements array is sufficient.
2230 Handle<FixedArray> from_elements =
2231 Handle<FixedArray>(FixedArray::cast(from->elements()));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002232 Handle<FixedArray> to_elements = FACTORY->CopyFixedArray(from_elements);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002233 to->set_elements(*to_elements);
2234}
2235
2236
2237void Genesis::TransferObject(Handle<JSObject> from, Handle<JSObject> to) {
2238 HandleScope outer;
danno@chromium.org160a7b02011-04-18 15:51:38 +00002239 Factory* factory = from->GetIsolate()->factory();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002240
2241 ASSERT(!from->IsJSArray());
2242 ASSERT(!to->IsJSArray());
2243
2244 TransferNamedProperties(from, to);
2245 TransferIndexedProperties(from, to);
2246
2247 // Transfer the prototype (new map is needed).
2248 Handle<Map> old_to_map = Handle<Map>(to->map());
verwaest@chromium.org753aee42012-07-17 16:15:42 +00002249 Handle<Map> new_to_map = factory->CopyMap(old_to_map);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002250 new_to_map->set_prototype(from->map()->prototype());
2251 to->set_map(*new_to_map);
2252}
2253
2254
2255void Genesis::MakeFunctionInstancePrototypeWritable() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002256 // The maps with writable prototype are created in CreateEmptyFunction
2257 // and CreateStrictModeFunctionMaps respectively. Initially the maps are
2258 // created with read-only prototype for JS builtins processing.
2259 ASSERT(!function_instance_map_writable_prototype_.is_null());
2260 ASSERT(!strict_mode_function_instance_map_writable_prototype_.is_null());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002261
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002262 // Replace function instance maps to make prototype writable.
2263 global_context()->set_function_map(
2264 *function_instance_map_writable_prototype_);
2265 global_context()->set_strict_mode_function_map(
2266 *strict_mode_function_instance_map_writable_prototype_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002267}
2268
2269
danno@chromium.org160a7b02011-04-18 15:51:38 +00002270Genesis::Genesis(Isolate* isolate,
2271 Handle<Object> global_object,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002272 v8::Handle<v8::ObjectTemplate> global_template,
danno@chromium.org160a7b02011-04-18 15:51:38 +00002273 v8::ExtensionConfiguration* extensions) : isolate_(isolate) {
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00002274 result_ = Handle<Context>::null();
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002275 // If V8 isn't running and cannot be initialized, just return.
2276 if (!V8::IsRunning() && !V8::Initialize(NULL)) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002277
2278 // Before creating the roots we must save the context and restore it
2279 // on all function exits.
2280 HandleScope scope;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002281 SaveContext saved_context(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002282
fschneider@chromium.org7d10be52012-04-10 12:30:14 +00002283 // During genesis, the boilerplate for stack overflow won't work until the
2284 // environment has been at least partially initialized. Add a stack check
2285 // before entering JS code to catch overflow early.
2286 StackLimitCheck check(Isolate::Current());
2287 if (check.HasOverflowed()) return;
2288
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002289 Handle<Context> new_context = Snapshot::NewContextFromSnapshot();
2290 if (!new_context.is_null()) {
2291 global_context_ =
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002292 Handle<Context>::cast(isolate->global_handles()->Create(*new_context));
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002293 AddToWeakGlobalContextList(*global_context_);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002294 isolate->set_context(*global_context_);
2295 isolate->counters()->contexts_created_by_snapshot()->Increment();
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002296 Handle<GlobalObject> inner_global;
2297 Handle<JSGlobalProxy> global_proxy =
2298 CreateNewGlobals(global_template,
2299 global_object,
2300 &inner_global);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002301
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002302 HookUpGlobalProxy(inner_global, global_proxy);
2303 HookUpInnerGlobal(inner_global);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002304
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002305 if (!ConfigureGlobalObjects(global_template)) return;
2306 } else {
2307 // We get here if there was no context snapshot.
2308 CreateRoots();
danno@chromium.org160a7b02011-04-18 15:51:38 +00002309 Handle<JSFunction> empty_function = CreateEmptyFunction(isolate);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002310 CreateStrictModeFunctionMaps(empty_function);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002311 Handle<GlobalObject> inner_global;
2312 Handle<JSGlobalProxy> global_proxy =
2313 CreateNewGlobals(global_template, global_object, &inner_global);
2314 HookUpGlobalProxy(inner_global, global_proxy);
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +00002315 if (!InitializeGlobal(inner_global, empty_function)) return;
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00002316 InstallJSFunctionResultCaches();
ricow@chromium.org65fae842010-08-25 15:26:24 +00002317 InitializeNormalizedMapCaches();
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00002318 if (!InstallNatives()) return;
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002319
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002320 MakeFunctionInstancePrototypeWritable();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002321
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002322 if (!ConfigureGlobalObjects(global_template)) return;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002323 isolate->counters()->contexts_created_from_scratch()->Increment();
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002324 }
mads.s.agercbaa0602008-08-14 13:41:48 +00002325
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +00002326 // Initialize experimental globals and install experimental natives.
2327 InitializeExperimentalGlobal();
danno@chromium.org160a7b02011-04-18 15:51:38 +00002328 if (!InstallExperimentalNatives()) return;
2329
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002330 result_ = global_context_;
2331}
2332
ager@chromium.orgddb913d2009-01-27 10:01:48 +00002333
2334// Support for thread preemption.
2335
2336// Reserve space for statics needing saving and restoring.
2337int Bootstrapper::ArchiveSpacePerThread() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002338 return sizeof(NestingCounterType);
ager@chromium.orgddb913d2009-01-27 10:01:48 +00002339}
2340
2341
2342// Archive statics that are thread local.
2343char* Bootstrapper::ArchiveState(char* to) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002344 *reinterpret_cast<NestingCounterType*>(to) = nesting_;
2345 nesting_ = 0;
2346 return to + sizeof(NestingCounterType);
ager@chromium.orgddb913d2009-01-27 10:01:48 +00002347}
2348
2349
2350// Restore statics that are thread local.
2351char* Bootstrapper::RestoreState(char* from) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002352 nesting_ = *reinterpret_cast<NestingCounterType*>(from);
2353 return from + sizeof(NestingCounterType);
ager@chromium.orgddb913d2009-01-27 10:01:48 +00002354}
2355
2356
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002357// Called when the top-level V8 mutex is destroyed.
2358void Bootstrapper::FreeThreadResources() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002359 ASSERT(!IsActive());
ager@chromium.orgddb913d2009-01-27 10:01:48 +00002360}
2361
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002362} } // namespace v8::internal