blob: 6c36cad03ee87fa571001d627d782acdb2abef24 [file] [log] [blame]
danno@chromium.org160a7b02011-04-18 15:51:38 +00001// Copyright 2011 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"
37#include "macro-assembler.h"
38#include "natives.h"
ager@chromium.orgea4f62e2010-08-16 16:28:43 +000039#include "objects-visiting.h"
ager@chromium.orgc4c92722009-11-18 14:12:51 +000040#include "snapshot.h"
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +000041#include "extensions/externalize-string-extension.h"
42#include "extensions/gc-extension.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000043
kasperl@chromium.org71affb52009-05-26 05:44:31 +000044namespace v8 {
45namespace internal {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000046
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000047
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000048NativesExternalStringResource::NativesExternalStringResource(
49 Bootstrapper* bootstrapper,
jkummerow@chromium.orge297f592011-06-08 10:05:15 +000050 const char* source,
51 size_t length)
52 : data_(source), length_(length) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000053 if (bootstrapper->delete_these_non_arrays_on_tear_down_ == NULL) {
54 bootstrapper->delete_these_non_arrays_on_tear_down_ = new List<char*>(2);
ager@chromium.orgc4c92722009-11-18 14:12:51 +000055 }
56 // The resources are small objects and we only make a fixed number of
57 // them, but let's clean them up on exit for neatness.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000058 bootstrapper->delete_these_non_arrays_on_tear_down_->
ager@chromium.orgc4c92722009-11-18 14:12:51 +000059 Add(reinterpret_cast<char*>(this));
60}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000061
62
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000063Bootstrapper::Bootstrapper()
64 : nesting_(0),
65 extensions_cache_(Script::TYPE_EXTENSION),
66 delete_these_non_arrays_on_tear_down_(NULL),
67 delete_these_arrays_on_tear_down_(NULL) {
68}
69
70
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000071Handle<String> Bootstrapper::NativesSourceLookup(int index) {
72 ASSERT(0 <= index && index < Natives::GetBuiltinsCount());
lrn@chromium.org7516f052011-03-30 08:52:27 +000073 Isolate* isolate = Isolate::Current();
74 Factory* factory = isolate->factory();
75 Heap* heap = isolate->heap();
76 if (heap->natives_source_cache()->get(index)->IsUndefined()) {
ager@chromium.orgc4c92722009-11-18 14:12:51 +000077 if (!Snapshot::IsEnabled() || FLAG_new_snapshot) {
78 // We can use external strings for the natives.
jkummerow@chromium.orge297f592011-06-08 10:05:15 +000079 Vector<const char> source = Natives::GetRawScriptSource(index);
ager@chromium.orgc4c92722009-11-18 14:12:51 +000080 NativesExternalStringResource* resource =
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000081 new NativesExternalStringResource(this,
jkummerow@chromium.orge297f592011-06-08 10:05:15 +000082 source.start(),
83 source.length());
ager@chromium.orgc4c92722009-11-18 14:12:51 +000084 Handle<String> source_code =
lrn@chromium.org7516f052011-03-30 08:52:27 +000085 factory->NewExternalStringFromAscii(resource);
86 heap->natives_source_cache()->set(index, *source_code);
ager@chromium.orgc4c92722009-11-18 14:12:51 +000087 } else {
88 // Old snapshot code can't cope with external strings at all.
89 Handle<String> source_code =
jkummerow@chromium.orge297f592011-06-08 10:05:15 +000090 factory->NewStringFromAscii(Natives::GetRawScriptSource(index));
lrn@chromium.org7516f052011-03-30 08:52:27 +000091 heap->natives_source_cache()->set(index, *source_code);
ager@chromium.orgc4c92722009-11-18 14:12:51 +000092 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000093 }
lrn@chromium.org7516f052011-03-30 08:52:27 +000094 Handle<Object> cached_source(heap->natives_source_cache()->get(index));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000095 return Handle<String>::cast(cached_source);
96}
97
98
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000099void Bootstrapper::Initialize(bool create_heap_objects) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000100 extensions_cache_.Initialize(create_heap_objects);
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +0000101 GCExtension::Register();
102 ExternalizeStringExtension::Register();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000103}
104
105
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000106char* Bootstrapper::AllocateAutoDeletedArray(int bytes) {
107 char* memory = new char[bytes];
108 if (memory != NULL) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000109 if (delete_these_arrays_on_tear_down_ == NULL) {
110 delete_these_arrays_on_tear_down_ = new List<char*>(2);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000111 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000112 delete_these_arrays_on_tear_down_->Add(memory);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000113 }
114 return memory;
115}
116
117
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000118void Bootstrapper::TearDown() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000119 if (delete_these_non_arrays_on_tear_down_ != NULL) {
120 int len = delete_these_non_arrays_on_tear_down_->length();
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000121 ASSERT(len < 20); // Don't use this mechanism for unbounded allocations.
122 for (int i = 0; i < len; i++) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000123 delete delete_these_non_arrays_on_tear_down_->at(i);
124 delete_these_non_arrays_on_tear_down_->at(i) = NULL;
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000125 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000126 delete delete_these_non_arrays_on_tear_down_;
127 delete_these_non_arrays_on_tear_down_ = NULL;
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000128 }
129
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000130 if (delete_these_arrays_on_tear_down_ != NULL) {
131 int len = delete_these_arrays_on_tear_down_->length();
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000132 ASSERT(len < 1000); // Don't use this mechanism for unbounded allocations.
133 for (int i = 0; i < len; i++) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000134 delete[] delete_these_arrays_on_tear_down_->at(i);
135 delete_these_arrays_on_tear_down_->at(i) = NULL;
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000136 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000137 delete delete_these_arrays_on_tear_down_;
138 delete_these_arrays_on_tear_down_ = NULL;
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000139 }
140
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000141 extensions_cache_.Initialize(false); // Yes, symmetrical
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000142}
143
144
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000145class Genesis BASE_EMBEDDED {
146 public:
danno@chromium.org160a7b02011-04-18 15:51:38 +0000147 Genesis(Isolate* isolate,
148 Handle<Object> global_object,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000149 v8::Handle<v8::ObjectTemplate> global_template,
150 v8::ExtensionConfiguration* extensions);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000151 ~Genesis() { }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000152
153 Handle<Context> result() { return result_; }
154
155 Genesis* previous() { return previous_; }
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000156
danno@chromium.org160a7b02011-04-18 15:51:38 +0000157 Isolate* isolate() const { return isolate_; }
158 Factory* factory() const { return isolate_->factory(); }
159 Heap* heap() const { return isolate_->heap(); }
160
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000161 private:
162 Handle<Context> global_context_;
danno@chromium.org160a7b02011-04-18 15:51:38 +0000163 Isolate* isolate_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000164
165 // There may be more than one active genesis object: When GC is
166 // triggered during environment creation there may be weak handle
167 // processing callbacks which may create new environments.
168 Genesis* previous_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000169
170 Handle<Context> global_context() { return global_context_; }
171
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000172 // Creates some basic objects. Used for creating a context from scratch.
173 void CreateRoots();
174 // Creates the empty function. Used for creating a context from scratch.
danno@chromium.org160a7b02011-04-18 15:51:38 +0000175 Handle<JSFunction> CreateEmptyFunction(Isolate* isolate);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000176 // Creates the ThrowTypeError function. ECMA 5th Ed. 13.2.3
danno@chromium.org40cb8782011-05-25 07:58:50 +0000177 Handle<JSFunction> GetThrowTypeErrorFunction();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000178
179 void CreateStrictModeFunctionMaps(Handle<JSFunction> empty);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000180 // Creates the global objects using the global and the template passed in
181 // through the API. We call this regardless of whether we are building a
182 // context from scratch or using a deserialized one from the partial snapshot
183 // but in the latter case we don't use the objects it produces directly, as
184 // we have to used the deserialized ones that are linked together with the
185 // rest of the context snapshot.
186 Handle<JSGlobalProxy> CreateNewGlobals(
187 v8::Handle<v8::ObjectTemplate> global_template,
188 Handle<Object> global_object,
189 Handle<GlobalObject>* global_proxy_out);
190 // Hooks the given global proxy into the context. If the context was created
191 // by deserialization then this will unhook the global proxy that was
192 // deserialized, leaving the GC to pick it up.
193 void HookUpGlobalProxy(Handle<GlobalObject> inner_global,
194 Handle<JSGlobalProxy> global_proxy);
195 // Similarly, we want to use the inner global that has been created by the
196 // templates passed through the API. The inner global from the snapshot is
197 // detached from the other objects in the snapshot.
198 void HookUpInnerGlobal(Handle<GlobalObject> inner_global);
199 // New context initialization. Used for creating a context from scratch.
200 void InitializeGlobal(Handle<GlobalObject> inner_global,
201 Handle<JSFunction> empty_function);
202 // 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();
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000211 // Used both for deserialized and from-scratch contexts to add the extensions
212 // provided.
213 static bool InstallExtensions(Handle<Context> global_context,
214 v8::ExtensionConfiguration* extensions);
215 static bool InstallExtension(const char* name);
216 static bool InstallExtension(v8::RegisteredExtension* current);
217 static void InstallSpecialObjects(Handle<Context> global_context);
ager@chromium.org5c838252010-02-19 08:53:10 +0000218 bool InstallJSBuiltins(Handle<JSBuiltinsObject> builtins);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000219 bool ConfigureApiObject(Handle<JSObject> object,
220 Handle<ObjectTemplateInfo> object_template);
221 bool ConfigureGlobalObjects(v8::Handle<v8::ObjectTemplate> global_template);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000222
223 // Migrates all properties from the 'from' object to the 'to'
224 // object and overrides the prototype in 'to' with the one from
225 // 'from'.
226 void TransferObject(Handle<JSObject> from, Handle<JSObject> to);
227 void TransferNamedProperties(Handle<JSObject> from, Handle<JSObject> to);
228 void TransferIndexedProperties(Handle<JSObject> from, Handle<JSObject> to);
229
kmillikin@chromium.org4111b802010-05-03 10:34:42 +0000230 enum PrototypePropertyMode {
231 DONT_ADD_PROTOTYPE,
232 ADD_READONLY_PROTOTYPE,
233 ADD_WRITEABLE_PROTOTYPE
234 };
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000235
236 Handle<Map> CreateFunctionMap(PrototypePropertyMode prototype_mode);
237
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000238 Handle<DescriptorArray> ComputeFunctionInstanceDescriptor(
kmillikin@chromium.org4111b802010-05-03 10:34:42 +0000239 PrototypePropertyMode prototypeMode);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000240 void MakeFunctionInstancePrototypeWritable();
241
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000242 Handle<Map> CreateStrictModeFunctionMap(
243 PrototypePropertyMode prototype_mode,
244 Handle<JSFunction> empty_function,
245 Handle<FixedArray> arguments_callbacks,
246 Handle<FixedArray> caller_callbacks);
247
248 Handle<DescriptorArray> ComputeStrictFunctionInstanceDescriptor(
249 PrototypePropertyMode propertyMode,
250 Handle<FixedArray> arguments,
251 Handle<FixedArray> caller);
252
danno@chromium.org160a7b02011-04-18 15:51:38 +0000253 static bool CompileBuiltin(Isolate* isolate, int index);
254 static bool CompileExperimentalBuiltin(Isolate* isolate, int index);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000255 static bool CompileNative(Vector<const char> name, Handle<String> source);
256 static bool CompileScriptCached(Vector<const char> name,
257 Handle<String> source,
258 SourceCodeCache* cache,
259 v8::Extension* extension,
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000260 Handle<Context> top_context,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000261 bool use_runtime_context);
262
263 Handle<Context> result_;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000264
265 // Function instance maps. Function literal maps are created initially with
266 // a read only prototype for the processing of JS builtins. Later the function
267 // instance maps are replaced in order to make prototype writable.
268 // These are the final, writable prototype, maps.
269 Handle<Map> function_instance_map_writable_prototype_;
270 Handle<Map> strict_mode_function_instance_map_writable_prototype_;
danno@chromium.org40cb8782011-05-25 07:58:50 +0000271 Handle<JSFunction> throw_type_error_function;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000272
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000273 BootstrapperActive active_;
274 friend class Bootstrapper;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000275};
276
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000277
278void Bootstrapper::Iterate(ObjectVisitor* v) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000279 extensions_cache_.Iterate(v);
ager@chromium.org3811b432009-10-28 14:53:37 +0000280 v->Synchronize("Extensions");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000281}
282
283
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000284Handle<Context> Bootstrapper::CreateEnvironment(
danno@chromium.org160a7b02011-04-18 15:51:38 +0000285 Isolate* isolate,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000286 Handle<Object> global_object,
287 v8::Handle<v8::ObjectTemplate> global_template,
288 v8::ExtensionConfiguration* extensions) {
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000289 HandleScope scope;
290 Handle<Context> env;
danno@chromium.org160a7b02011-04-18 15:51:38 +0000291 Genesis genesis(isolate, global_object, global_template, extensions);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000292 env = genesis.result();
293 if (!env.is_null()) {
294 if (InstallExtensions(env, extensions)) {
295 return env;
296 }
297 }
298 return Handle<Context>();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000299}
300
301
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000302static void SetObjectPrototype(Handle<JSObject> object, Handle<Object> proto) {
303 // object.__proto__ = proto;
danno@chromium.org160a7b02011-04-18 15:51:38 +0000304 Factory* factory = object->GetIsolate()->factory();
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000305 Handle<Map> old_to_map = Handle<Map>(object->map());
danno@chromium.org160a7b02011-04-18 15:51:38 +0000306 Handle<Map> new_to_map = factory->CopyMapDropTransitions(old_to_map);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000307 new_to_map->set_prototype(*proto);
308 object->set_map(*new_to_map);
309}
310
311
312void Bootstrapper::DetachGlobal(Handle<Context> env) {
danno@chromium.org160a7b02011-04-18 15:51:38 +0000313 Factory* factory = env->GetIsolate()->factory();
lrn@chromium.org7516f052011-03-30 08:52:27 +0000314 JSGlobalProxy::cast(env->global_proxy())->set_context(*factory->null_value());
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000315 SetObjectPrototype(Handle<JSObject>(env->global_proxy()),
lrn@chromium.org7516f052011-03-30 08:52:27 +0000316 factory->null_value());
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000317 env->set_global_proxy(env->global());
318 env->global()->set_global_receiver(env->global());
319}
320
321
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +0000322void Bootstrapper::ReattachGlobal(Handle<Context> env,
323 Handle<Object> global_object) {
324 ASSERT(global_object->IsJSGlobalProxy());
325 Handle<JSGlobalProxy> global = Handle<JSGlobalProxy>::cast(global_object);
326 env->global()->set_global_receiver(*global);
327 env->set_global_proxy(*global);
328 SetObjectPrototype(global, Handle<JSObject>(env->global()));
329 global->set_context(*env);
330}
331
332
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000333static Handle<JSFunction> InstallFunction(Handle<JSObject> target,
334 const char* name,
335 InstanceType type,
336 int instance_size,
337 Handle<JSObject> prototype,
338 Builtins::Name call,
339 bool is_ecma_native) {
danno@chromium.org160a7b02011-04-18 15:51:38 +0000340 Isolate* isolate = target->GetIsolate();
lrn@chromium.org7516f052011-03-30 08:52:27 +0000341 Factory* factory = isolate->factory();
342 Handle<String> symbol = factory->LookupAsciiSymbol(name);
343 Handle<Code> call_code = Handle<Code>(isolate->builtins()->builtin(call));
kmillikin@chromium.org4111b802010-05-03 10:34:42 +0000344 Handle<JSFunction> function = prototype.is_null() ?
lrn@chromium.org7516f052011-03-30 08:52:27 +0000345 factory->NewFunctionWithoutPrototype(symbol, call_code) :
346 factory->NewFunctionWithPrototype(symbol,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000347 type,
348 instance_size,
349 prototype,
350 call_code,
351 is_ecma_native);
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +0000352 SetLocalPropertyNoThrow(target, symbol, function, DONT_ENUM);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000353 if (is_ecma_native) {
354 function->shared()->set_instance_class_name(*symbol);
355 }
356 return function;
357}
358
359
360Handle<DescriptorArray> Genesis::ComputeFunctionInstanceDescriptor(
kmillikin@chromium.org4111b802010-05-03 10:34:42 +0000361 PrototypePropertyMode prototypeMode) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000362 Handle<DescriptorArray> descriptors =
danno@chromium.org160a7b02011-04-18 15:51:38 +0000363 factory()->NewDescriptorArray(prototypeMode == DONT_ADD_PROTOTYPE
364 ? 4
365 : 5);
kmillikin@chromium.org4111b802010-05-03 10:34:42 +0000366 PropertyAttributes attributes =
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000367 static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE | READ_ONLY);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000368
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000369 { // Add length.
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000370 Handle<Foreign> foreign = factory()->NewForeign(&Accessors::FunctionLength);
371 CallbacksDescriptor d(*factory()->length_symbol(), *foreign, attributes);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000372 descriptors->Set(0, &d);
373 }
374 { // Add name.
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000375 Handle<Foreign> foreign = factory()->NewForeign(&Accessors::FunctionName);
376 CallbacksDescriptor d(*factory()->name_symbol(), *foreign, attributes);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000377 descriptors->Set(1, &d);
378 }
379 { // Add arguments.
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000380 Handle<Foreign> foreign =
381 factory()->NewForeign(&Accessors::FunctionArguments);
382 CallbacksDescriptor d(*factory()->arguments_symbol(), *foreign, attributes);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000383 descriptors->Set(2, &d);
384 }
385 { // Add caller.
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000386 Handle<Foreign> foreign = factory()->NewForeign(&Accessors::FunctionCaller);
387 CallbacksDescriptor d(*factory()->caller_symbol(), *foreign, attributes);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000388 descriptors->Set(3, &d);
389 }
390 if (prototypeMode != DONT_ADD_PROTOTYPE) {
391 // Add prototype.
392 if (prototypeMode == ADD_WRITEABLE_PROTOTYPE) {
393 attributes = static_cast<PropertyAttributes>(attributes & ~READ_ONLY);
394 }
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000395 Handle<Foreign> foreign =
396 factory()->NewForeign(&Accessors::FunctionPrototype);
397 CallbacksDescriptor d(*factory()->prototype_symbol(), *foreign, attributes);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000398 descriptors->Set(4, &d);
399 }
400 descriptors->Sort();
401 return descriptors;
402}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000403
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000404
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000405Handle<Map> Genesis::CreateFunctionMap(PrototypePropertyMode prototype_mode) {
danno@chromium.org160a7b02011-04-18 15:51:38 +0000406 Handle<Map> map = factory()->NewMap(JS_FUNCTION_TYPE, JSFunction::kSize);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000407 Handle<DescriptorArray> descriptors =
408 ComputeFunctionInstanceDescriptor(prototype_mode);
409 map->set_instance_descriptors(*descriptors);
410 map->set_function_with_prototype(prototype_mode != DONT_ADD_PROTOTYPE);
411 return map;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000412}
413
414
danno@chromium.org160a7b02011-04-18 15:51:38 +0000415Handle<JSFunction> Genesis::CreateEmptyFunction(Isolate* isolate) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000416 // Allocate the map for function instances. Maps are allocated first and their
417 // prototypes patched later, once empty function is created.
418
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000419 // Please note that the prototype property for function instances must be
420 // writable.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000421 Handle<Map> function_instance_map =
422 CreateFunctionMap(ADD_WRITEABLE_PROTOTYPE);
423 global_context()->set_function_instance_map(*function_instance_map);
kmillikin@chromium.org4111b802010-05-03 10:34:42 +0000424
425 // Functions with this map will not have a 'prototype' property, and
426 // can not be used as constructors.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000427 Handle<Map> function_without_prototype_map =
428 CreateFunctionMap(DONT_ADD_PROTOTYPE);
kmillikin@chromium.org4111b802010-05-03 10:34:42 +0000429 global_context()->set_function_without_prototype_map(
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000430 *function_without_prototype_map);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000431
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000432 // Allocate the function map. This map is temporary, used only for processing
433 // of builtins.
434 // Later the map is replaced with writable prototype map, allocated below.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000435 Handle<Map> function_map = CreateFunctionMap(ADD_READONLY_PROTOTYPE);
436 global_context()->set_function_map(*function_map);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000437
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000438 // The final map for functions. Writeable prototype.
439 // This map is installed in MakeFunctionInstancePrototypeWritable.
440 function_instance_map_writable_prototype_ =
441 CreateFunctionMap(ADD_WRITEABLE_PROTOTYPE);
442
lrn@chromium.org7516f052011-03-30 08:52:27 +0000443 Factory* factory = isolate->factory();
444 Heap* heap = isolate->heap();
445
446 Handle<String> object_name = Handle<String>(heap->Object_symbol());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000447
448 { // --- O b j e c t ---
449 Handle<JSFunction> object_fun =
lrn@chromium.org7516f052011-03-30 08:52:27 +0000450 factory->NewFunction(object_name, factory->null_value());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000451 Handle<Map> object_function_map =
lrn@chromium.org7516f052011-03-30 08:52:27 +0000452 factory->NewMap(JS_OBJECT_TYPE, JSObject::kHeaderSize);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000453 object_fun->set_initial_map(*object_function_map);
454 object_function_map->set_constructor(*object_fun);
455
456 global_context()->set_object_function(*object_fun);
457
458 // Allocate a new prototype for the object function.
lrn@chromium.org7516f052011-03-30 08:52:27 +0000459 Handle<JSObject> prototype = factory->NewJSObject(
460 isolate->object_function(),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000461 TENURED);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000462
463 global_context()->set_initial_object_prototype(*prototype);
464 SetPrototype(object_fun, prototype);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000465 object_function_map->
lrn@chromium.org7516f052011-03-30 08:52:27 +0000466 set_instance_descriptors(heap->empty_descriptor_array());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000467 }
468
469 // Allocate the empty function as the prototype for function ECMAScript
470 // 262 15.3.4.
lrn@chromium.org7516f052011-03-30 08:52:27 +0000471 Handle<String> symbol = factory->LookupAsciiSymbol("Empty");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000472 Handle<JSFunction> empty_function =
lrn@chromium.org7516f052011-03-30 08:52:27 +0000473 factory->NewFunctionWithoutPrototype(symbol, kNonStrictMode);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000474
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000475 // --- E m p t y ---
476 Handle<Code> code =
lrn@chromium.org7516f052011-03-30 08:52:27 +0000477 Handle<Code>(isolate->builtins()->builtin(
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000478 Builtins::kEmptyFunction));
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000479 empty_function->set_code(*code);
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000480 empty_function->shared()->set_code(*code);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000481 Handle<String> source = factory->NewStringFromAscii(CStrVector("() {}"));
482 Handle<Script> script = factory->NewScript(source);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000483 script->set_type(Smi::FromInt(Script::TYPE_NATIVE));
484 empty_function->shared()->set_script(*script);
485 empty_function->shared()->set_start_position(0);
486 empty_function->shared()->set_end_position(source->length());
487 empty_function->shared()->DontAdaptArguments();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000488
489 // Set prototypes for the function maps.
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000490 global_context()->function_map()->set_prototype(*empty_function);
491 global_context()->function_instance_map()->set_prototype(*empty_function);
kmillikin@chromium.org4111b802010-05-03 10:34:42 +0000492 global_context()->function_without_prototype_map()->
493 set_prototype(*empty_function);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000494 function_instance_map_writable_prototype_->set_prototype(*empty_function);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000495
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000496 // Allocate the function map first and then patch the prototype later
lrn@chromium.org7516f052011-03-30 08:52:27 +0000497 Handle<Map> empty_fm = factory->CopyMapDropDescriptors(
kmillikin@chromium.org4111b802010-05-03 10:34:42 +0000498 function_without_prototype_map);
499 empty_fm->set_instance_descriptors(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000500 function_without_prototype_map->instance_descriptors());
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000501 empty_fm->set_prototype(global_context()->object_function()->prototype());
502 empty_function->set_map(*empty_fm);
503 return empty_function;
504}
505
506
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000507Handle<DescriptorArray> Genesis::ComputeStrictFunctionInstanceDescriptor(
508 PrototypePropertyMode prototypeMode,
509 Handle<FixedArray> arguments,
510 Handle<FixedArray> caller) {
511 Handle<DescriptorArray> descriptors =
danno@chromium.org160a7b02011-04-18 15:51:38 +0000512 factory()->NewDescriptorArray(prototypeMode == DONT_ADD_PROTOTYPE
513 ? 4
514 : 5);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000515 PropertyAttributes attributes = static_cast<PropertyAttributes>(
516 DONT_ENUM | DONT_DELETE | READ_ONLY);
517
518 { // length
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000519 Handle<Foreign> foreign = factory()->NewForeign(&Accessors::FunctionLength);
520 CallbacksDescriptor d(*factory()->length_symbol(), *foreign, attributes);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000521 descriptors->Set(0, &d);
522 }
523 { // name
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000524 Handle<Foreign> foreign = factory()->NewForeign(&Accessors::FunctionName);
525 CallbacksDescriptor d(*factory()->name_symbol(), *foreign, attributes);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000526 descriptors->Set(1, &d);
527 }
528 { // arguments
danno@chromium.org160a7b02011-04-18 15:51:38 +0000529 CallbacksDescriptor d(*factory()->arguments_symbol(),
530 *arguments,
531 attributes);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000532 descriptors->Set(2, &d);
533 }
534 { // caller
danno@chromium.org160a7b02011-04-18 15:51:38 +0000535 CallbacksDescriptor d(*factory()->caller_symbol(), *caller, attributes);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000536 descriptors->Set(3, &d);
537 }
538
539 // prototype
540 if (prototypeMode != DONT_ADD_PROTOTYPE) {
541 if (prototypeMode == ADD_WRITEABLE_PROTOTYPE) {
542 attributes = static_cast<PropertyAttributes>(attributes & ~READ_ONLY);
543 }
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000544 Handle<Foreign> foreign =
545 factory()->NewForeign(&Accessors::FunctionPrototype);
546 CallbacksDescriptor d(*factory()->prototype_symbol(), *foreign, attributes);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000547 descriptors->Set(4, &d);
548 }
549
550 descriptors->Sort();
551 return descriptors;
552}
553
554
555// ECMAScript 5th Edition, 13.2.3
danno@chromium.org40cb8782011-05-25 07:58:50 +0000556Handle<JSFunction> Genesis::GetThrowTypeErrorFunction() {
557 if (throw_type_error_function.is_null()) {
558 Handle<String> name = factory()->LookupAsciiSymbol("ThrowTypeError");
559 throw_type_error_function =
560 factory()->NewFunctionWithoutPrototype(name, kNonStrictMode);
561 Handle<Code> code(isolate()->builtins()->builtin(
562 Builtins::kStrictModePoisonPill));
563 throw_type_error_function->set_map(
564 global_context()->function_map());
565 throw_type_error_function->set_code(*code);
566 throw_type_error_function->shared()->set_code(*code);
567 throw_type_error_function->shared()->DontAdaptArguments();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000568
danno@chromium.org40cb8782011-05-25 07:58:50 +0000569 PreventExtensions(throw_type_error_function);
570 }
571 return throw_type_error_function;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000572}
573
574
575Handle<Map> Genesis::CreateStrictModeFunctionMap(
576 PrototypePropertyMode prototype_mode,
577 Handle<JSFunction> empty_function,
578 Handle<FixedArray> arguments_callbacks,
579 Handle<FixedArray> caller_callbacks) {
danno@chromium.org160a7b02011-04-18 15:51:38 +0000580 Handle<Map> map = factory()->NewMap(JS_FUNCTION_TYPE, JSFunction::kSize);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000581 Handle<DescriptorArray> descriptors =
582 ComputeStrictFunctionInstanceDescriptor(prototype_mode,
583 arguments_callbacks,
584 caller_callbacks);
585 map->set_instance_descriptors(*descriptors);
586 map->set_function_with_prototype(prototype_mode != DONT_ADD_PROTOTYPE);
587 map->set_prototype(*empty_function);
588 return map;
589}
590
591
592void Genesis::CreateStrictModeFunctionMaps(Handle<JSFunction> empty) {
593 // Create the callbacks arrays for ThrowTypeError functions.
594 // The get/set callacks are filled in after the maps are created below.
danno@chromium.org160a7b02011-04-18 15:51:38 +0000595 Factory* factory = empty->GetIsolate()->factory();
lrn@chromium.org7516f052011-03-30 08:52:27 +0000596 Handle<FixedArray> arguments = factory->NewFixedArray(2, TENURED);
597 Handle<FixedArray> caller = factory->NewFixedArray(2, TENURED);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000598
599 // Allocate map for the strict mode function instances.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000600 Handle<Map> strict_mode_function_instance_map =
601 CreateStrictModeFunctionMap(
602 ADD_WRITEABLE_PROTOTYPE, empty, arguments, caller);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000603 global_context()->set_strict_mode_function_instance_map(
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000604 *strict_mode_function_instance_map);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000605
606 // Allocate map for the prototype-less strict mode instances.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000607 Handle<Map> strict_mode_function_without_prototype_map =
608 CreateStrictModeFunctionMap(
609 DONT_ADD_PROTOTYPE, empty, arguments, caller);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000610 global_context()->set_strict_mode_function_without_prototype_map(
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000611 *strict_mode_function_without_prototype_map);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000612
613 // Allocate map for the strict mode functions. This map is temporary, used
614 // only for processing of builtins.
615 // Later the map is replaced with writable prototype map, allocated below.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000616 Handle<Map> strict_mode_function_map =
617 CreateStrictModeFunctionMap(
618 ADD_READONLY_PROTOTYPE, empty, arguments, caller);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000619 global_context()->set_strict_mode_function_map(
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000620 *strict_mode_function_map);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000621
622 // The final map for the strict mode functions. Writeable prototype.
623 // This map is installed in MakeFunctionInstancePrototypeWritable.
624 strict_mode_function_instance_map_writable_prototype_ =
625 CreateStrictModeFunctionMap(
626 ADD_WRITEABLE_PROTOTYPE, empty, arguments, caller);
627
danno@chromium.org40cb8782011-05-25 07:58:50 +0000628 // Create the ThrowTypeError function instance.
629 Handle<JSFunction> throw_function =
630 GetThrowTypeErrorFunction();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000631
632 // Complete the callback fixed arrays.
danno@chromium.org40cb8782011-05-25 07:58:50 +0000633 arguments->set(0, *throw_function);
634 arguments->set(1, *throw_function);
635 caller->set(0, *throw_function);
636 caller->set(1, *throw_function);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000637}
638
639
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000640static void AddToWeakGlobalContextList(Context* context) {
641 ASSERT(context->IsGlobalContext());
danno@chromium.org160a7b02011-04-18 15:51:38 +0000642 Heap* heap = context->GetIsolate()->heap();
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000643#ifdef DEBUG
644 { // NOLINT
645 ASSERT(context->get(Context::NEXT_CONTEXT_LINK)->IsUndefined());
646 // Check that context is not in the list yet.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000647 for (Object* current = heap->global_contexts_list();
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000648 !current->IsUndefined();
649 current = Context::cast(current)->get(Context::NEXT_CONTEXT_LINK)) {
650 ASSERT(current != context);
651 }
652 }
653#endif
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000654 context->set(Context::NEXT_CONTEXT_LINK, heap->global_contexts_list());
655 heap->set_global_contexts_list(context);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000656}
657
658
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000659void Genesis::CreateRoots() {
660 // Allocate the global context FixedArray first and then patch the
661 // closure and extension object later (we need the empty function
662 // and the global object, but in order to create those, we need the
663 // global context).
danno@chromium.org160a7b02011-04-18 15:51:38 +0000664 global_context_ = Handle<Context>::cast(isolate()->global_handles()->Create(
665 *factory()->NewGlobalContext()));
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000666 AddToWeakGlobalContextList(*global_context_);
danno@chromium.org160a7b02011-04-18 15:51:38 +0000667 isolate()->set_context(*global_context());
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000668
669 // Allocate the message listeners object.
670 {
671 v8::NeanderArray listeners;
672 global_context()->set_message_listeners(*listeners.value());
673 }
674}
675
676
677Handle<JSGlobalProxy> Genesis::CreateNewGlobals(
678 v8::Handle<v8::ObjectTemplate> global_template,
679 Handle<Object> global_object,
680 Handle<GlobalObject>* inner_global_out) {
681 // The argument global_template aka data is an ObjectTemplateInfo.
682 // It has a constructor pointer that points at global_constructor which is a
683 // FunctionTemplateInfo.
684 // The global_constructor is used to create or reinitialize the global_proxy.
685 // The global_constructor also has a prototype_template pointer that points at
686 // js_global_template which is an ObjectTemplateInfo.
687 // That in turn has a constructor pointer that points at
688 // js_global_constructor which is a FunctionTemplateInfo.
689 // js_global_constructor is used to make js_global_function
690 // js_global_function is used to make the new inner_global.
691 //
692 // --- G l o b a l ---
693 // Step 1: Create a fresh inner JSGlobalObject.
694 Handle<JSFunction> js_global_function;
695 Handle<ObjectTemplateInfo> js_global_template;
696 if (!global_template.IsEmpty()) {
697 // Get prototype template of the global_template.
698 Handle<ObjectTemplateInfo> data =
699 v8::Utils::OpenHandle(*global_template);
700 Handle<FunctionTemplateInfo> global_constructor =
701 Handle<FunctionTemplateInfo>(
702 FunctionTemplateInfo::cast(data->constructor()));
703 Handle<Object> proto_template(global_constructor->prototype_template());
704 if (!proto_template->IsUndefined()) {
705 js_global_template =
706 Handle<ObjectTemplateInfo>::cast(proto_template);
707 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000708 }
709
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000710 if (js_global_template.is_null()) {
danno@chromium.org160a7b02011-04-18 15:51:38 +0000711 Handle<String> name = Handle<String>(heap()->empty_symbol());
712 Handle<Code> code = Handle<Code>(isolate()->builtins()->builtin(
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000713 Builtins::kIllegal));
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000714 js_global_function =
danno@chromium.org160a7b02011-04-18 15:51:38 +0000715 factory()->NewFunction(name, JS_GLOBAL_OBJECT_TYPE,
716 JSGlobalObject::kSize, code, true);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000717 // Change the constructor property of the prototype of the
718 // hidden global function to refer to the Object function.
719 Handle<JSObject> prototype =
720 Handle<JSObject>(
721 JSObject::cast(js_global_function->instance_prototype()));
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +0000722 SetLocalPropertyNoThrow(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000723 prototype,
danno@chromium.org160a7b02011-04-18 15:51:38 +0000724 factory()->constructor_symbol(),
725 isolate()->object_function(),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000726 NONE);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000727 } else {
728 Handle<FunctionTemplateInfo> js_global_constructor(
729 FunctionTemplateInfo::cast(js_global_template->constructor()));
730 js_global_function =
danno@chromium.org160a7b02011-04-18 15:51:38 +0000731 factory()->CreateApiFunction(js_global_constructor,
732 factory()->InnerGlobalObject);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000733 }
734
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000735 js_global_function->initial_map()->set_is_hidden_prototype();
736 Handle<GlobalObject> inner_global =
danno@chromium.org160a7b02011-04-18 15:51:38 +0000737 factory()->NewGlobalObject(js_global_function);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000738 if (inner_global_out != NULL) {
739 *inner_global_out = inner_global;
740 }
741
742 // Step 2: create or re-initialize the global proxy object.
743 Handle<JSFunction> global_proxy_function;
744 if (global_template.IsEmpty()) {
danno@chromium.org160a7b02011-04-18 15:51:38 +0000745 Handle<String> name = Handle<String>(heap()->empty_symbol());
746 Handle<Code> code = Handle<Code>(isolate()->builtins()->builtin(
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000747 Builtins::kIllegal));
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000748 global_proxy_function =
danno@chromium.org160a7b02011-04-18 15:51:38 +0000749 factory()->NewFunction(name, JS_GLOBAL_PROXY_TYPE,
750 JSGlobalProxy::kSize, code, true);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000751 } else {
752 Handle<ObjectTemplateInfo> data =
753 v8::Utils::OpenHandle(*global_template);
754 Handle<FunctionTemplateInfo> global_constructor(
755 FunctionTemplateInfo::cast(data->constructor()));
756 global_proxy_function =
danno@chromium.org160a7b02011-04-18 15:51:38 +0000757 factory()->CreateApiFunction(global_constructor,
758 factory()->OuterGlobalObject);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000759 }
760
danno@chromium.org160a7b02011-04-18 15:51:38 +0000761 Handle<String> global_name = factory()->LookupAsciiSymbol("global");
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000762 global_proxy_function->shared()->set_instance_class_name(*global_name);
763 global_proxy_function->initial_map()->set_is_access_check_needed(true);
764
765 // Set global_proxy.__proto__ to js_global after ConfigureGlobalObjects
766 // Return the global proxy.
767
768 if (global_object.location() != NULL) {
769 ASSERT(global_object->IsJSGlobalProxy());
770 return ReinitializeJSGlobalProxy(
771 global_proxy_function,
772 Handle<JSGlobalProxy>::cast(global_object));
773 } else {
774 return Handle<JSGlobalProxy>::cast(
danno@chromium.org160a7b02011-04-18 15:51:38 +0000775 factory()->NewJSObject(global_proxy_function, TENURED));
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000776 }
777}
778
779
780void Genesis::HookUpGlobalProxy(Handle<GlobalObject> inner_global,
781 Handle<JSGlobalProxy> global_proxy) {
782 // Set the global context for the global object.
783 inner_global->set_global_context(*global_context());
784 inner_global->set_global_receiver(*global_proxy);
785 global_proxy->set_context(*global_context());
786 global_context()->set_global_proxy(*global_proxy);
787}
788
789
790void Genesis::HookUpInnerGlobal(Handle<GlobalObject> inner_global) {
791 Handle<GlobalObject> inner_global_from_snapshot(
792 GlobalObject::cast(global_context_->extension()));
793 Handle<JSBuiltinsObject> builtins_global(global_context_->builtins());
794 global_context_->set_extension(*inner_global);
795 global_context_->set_global(*inner_global);
796 global_context_->set_security_token(*inner_global);
797 static const PropertyAttributes attributes =
798 static_cast<PropertyAttributes>(READ_ONLY | DONT_DELETE);
799 ForceSetProperty(builtins_global,
danno@chromium.org160a7b02011-04-18 15:51:38 +0000800 factory()->LookupAsciiSymbol("global"),
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000801 inner_global,
802 attributes);
803 // Setup the reference from the global object to the builtins object.
804 JSGlobalObject::cast(*inner_global)->set_builtins(*builtins_global);
805 TransferNamedProperties(inner_global_from_snapshot, inner_global);
806 TransferIndexedProperties(inner_global_from_snapshot, inner_global);
807}
808
809
810// This is only called if we are not using snapshots. The equivalent
811// work in the snapshot case is done in HookUpInnerGlobal.
812void Genesis::InitializeGlobal(Handle<GlobalObject> inner_global,
813 Handle<JSFunction> empty_function) {
814 // --- G l o b a l C o n t e x t ---
815 // Use the empty function as closure (no scope info).
816 global_context()->set_closure(*empty_function);
817 global_context()->set_fcontext(*global_context());
818 global_context()->set_previous(NULL);
819 // Set extension and global object.
820 global_context()->set_extension(*inner_global);
821 global_context()->set_global(*inner_global);
822 // Security setup: Set the security token of the global object to
823 // its the inner global. This makes the security check between two
824 // different contexts fail by default even in case of global
825 // object reinitialization.
826 global_context()->set_security_token(*inner_global);
827
danno@chromium.org160a7b02011-04-18 15:51:38 +0000828 Isolate* isolate = inner_global->GetIsolate();
lrn@chromium.org7516f052011-03-30 08:52:27 +0000829 Factory* factory = isolate->factory();
830 Heap* heap = isolate->heap();
831
832 Handle<String> object_name = Handle<String>(heap->Object_symbol());
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +0000833 SetLocalPropertyNoThrow(inner_global, object_name,
lrn@chromium.org7516f052011-03-30 08:52:27 +0000834 isolate->object_function(), DONT_ENUM);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000835
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000836 Handle<JSObject> global = Handle<JSObject>(global_context()->global());
837
838 // Install global Function object
839 InstallFunction(global, "Function", JS_FUNCTION_TYPE, JSFunction::kSize,
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000840 empty_function, Builtins::kIllegal, true); // ECMA native.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000841
842 { // --- A r r a y ---
843 Handle<JSFunction> array_function =
844 InstallFunction(global, "Array", JS_ARRAY_TYPE, JSArray::kSize,
lrn@chromium.org7516f052011-03-30 08:52:27 +0000845 isolate->initial_object_prototype(),
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000846 Builtins::kArrayCode, true);
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +0000847 array_function->shared()->set_construct_stub(
lrn@chromium.org7516f052011-03-30 08:52:27 +0000848 isolate->builtins()->builtin(Builtins::kArrayConstructCode));
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000849 array_function->shared()->DontAdaptArguments();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000850
851 // This seems a bit hackish, but we need to make sure Array.length
852 // is 1.
853 array_function->shared()->set_length(1);
854 Handle<DescriptorArray> array_descriptors =
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000855 factory->CopyAppendForeignDescriptor(
lrn@chromium.org7516f052011-03-30 08:52:27 +0000856 factory->empty_descriptor_array(),
857 factory->length_symbol(),
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000858 factory->NewForeign(&Accessors::ArrayLength),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000859 static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE));
860
861 // Cache the fast JavaScript array map
862 global_context()->set_js_array_map(array_function->initial_map());
863 global_context()->js_array_map()->set_instance_descriptors(
864 *array_descriptors);
865 // array_function is used internally. JS code creating array object should
866 // search for the 'Array' property on the global object and use that one
867 // as the constructor. 'Array' property on a global object can be
868 // overwritten by JS code.
869 global_context()->set_array_function(*array_function);
870 }
871
872 { // --- N u m b e r ---
873 Handle<JSFunction> number_fun =
874 InstallFunction(global, "Number", JS_VALUE_TYPE, JSValue::kSize,
lrn@chromium.org7516f052011-03-30 08:52:27 +0000875 isolate->initial_object_prototype(),
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000876 Builtins::kIllegal, true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000877 global_context()->set_number_function(*number_fun);
878 }
879
880 { // --- B o o l e a n ---
881 Handle<JSFunction> boolean_fun =
882 InstallFunction(global, "Boolean", JS_VALUE_TYPE, JSValue::kSize,
lrn@chromium.org7516f052011-03-30 08:52:27 +0000883 isolate->initial_object_prototype(),
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000884 Builtins::kIllegal, true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000885 global_context()->set_boolean_function(*boolean_fun);
886 }
887
888 { // --- S t r i n g ---
889 Handle<JSFunction> string_fun =
890 InstallFunction(global, "String", JS_VALUE_TYPE, JSValue::kSize,
lrn@chromium.org7516f052011-03-30 08:52:27 +0000891 isolate->initial_object_prototype(),
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000892 Builtins::kIllegal, true);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000893 string_fun->shared()->set_construct_stub(
lrn@chromium.org7516f052011-03-30 08:52:27 +0000894 isolate->builtins()->builtin(Builtins::kStringConstructCode));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000895 global_context()->set_string_function(*string_fun);
896 // Add 'length' property to strings.
897 Handle<DescriptorArray> string_descriptors =
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000898 factory->CopyAppendForeignDescriptor(
lrn@chromium.org7516f052011-03-30 08:52:27 +0000899 factory->empty_descriptor_array(),
900 factory->length_symbol(),
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000901 factory->NewForeign(&Accessors::StringLength),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000902 static_cast<PropertyAttributes>(DONT_ENUM |
903 DONT_DELETE |
904 READ_ONLY));
905
906 Handle<Map> string_map =
907 Handle<Map>(global_context()->string_function()->initial_map());
908 string_map->set_instance_descriptors(*string_descriptors);
909 }
910
911 { // --- D a t e ---
912 // Builtin functions for Date.prototype.
913 Handle<JSFunction> date_fun =
914 InstallFunction(global, "Date", JS_VALUE_TYPE, JSValue::kSize,
lrn@chromium.org7516f052011-03-30 08:52:27 +0000915 isolate->initial_object_prototype(),
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000916 Builtins::kIllegal, true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000917
918 global_context()->set_date_function(*date_fun);
919 }
920
921
922 { // -- R e g E x p
923 // Builtin functions for RegExp.prototype.
924 Handle<JSFunction> regexp_fun =
ager@chromium.org236ad962008-09-25 09:45:57 +0000925 InstallFunction(global, "RegExp", JS_REGEXP_TYPE, JSRegExp::kSize,
lrn@chromium.org7516f052011-03-30 08:52:27 +0000926 isolate->initial_object_prototype(),
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000927 Builtins::kIllegal, true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000928 global_context()->set_regexp_function(*regexp_fun);
lrn@chromium.org25156de2010-04-06 13:10:27 +0000929
930 ASSERT(regexp_fun->has_initial_map());
931 Handle<Map> initial_map(regexp_fun->initial_map());
932
933 ASSERT_EQ(0, initial_map->inobject_properties());
934
lrn@chromium.org7516f052011-03-30 08:52:27 +0000935 Handle<DescriptorArray> descriptors = factory->NewDescriptorArray(5);
lrn@chromium.org25156de2010-04-06 13:10:27 +0000936 PropertyAttributes final =
937 static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE | READ_ONLY);
938 int enum_index = 0;
939 {
940 // ECMA-262, section 15.10.7.1.
lrn@chromium.org7516f052011-03-30 08:52:27 +0000941 FieldDescriptor field(heap->source_symbol(),
lrn@chromium.org25156de2010-04-06 13:10:27 +0000942 JSRegExp::kSourceFieldIndex,
943 final,
944 enum_index++);
945 descriptors->Set(0, &field);
946 }
947 {
948 // ECMA-262, section 15.10.7.2.
lrn@chromium.org7516f052011-03-30 08:52:27 +0000949 FieldDescriptor field(heap->global_symbol(),
lrn@chromium.org25156de2010-04-06 13:10:27 +0000950 JSRegExp::kGlobalFieldIndex,
951 final,
952 enum_index++);
953 descriptors->Set(1, &field);
954 }
955 {
956 // ECMA-262, section 15.10.7.3.
lrn@chromium.org7516f052011-03-30 08:52:27 +0000957 FieldDescriptor field(heap->ignore_case_symbol(),
lrn@chromium.org25156de2010-04-06 13:10:27 +0000958 JSRegExp::kIgnoreCaseFieldIndex,
959 final,
960 enum_index++);
961 descriptors->Set(2, &field);
962 }
963 {
964 // ECMA-262, section 15.10.7.4.
lrn@chromium.org7516f052011-03-30 08:52:27 +0000965 FieldDescriptor field(heap->multiline_symbol(),
lrn@chromium.org25156de2010-04-06 13:10:27 +0000966 JSRegExp::kMultilineFieldIndex,
967 final,
968 enum_index++);
969 descriptors->Set(3, &field);
970 }
971 {
972 // ECMA-262, section 15.10.7.5.
973 PropertyAttributes writable =
974 static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000975 FieldDescriptor field(heap->last_index_symbol(),
lrn@chromium.org25156de2010-04-06 13:10:27 +0000976 JSRegExp::kLastIndexFieldIndex,
977 writable,
978 enum_index++);
979 descriptors->Set(4, &field);
980 }
981 descriptors->SetNextEnumerationIndex(enum_index);
982 descriptors->Sort();
983
984 initial_map->set_inobject_properties(5);
985 initial_map->set_pre_allocated_property_fields(5);
986 initial_map->set_unused_property_fields(0);
987 initial_map->set_instance_size(
988 initial_map->instance_size() + 5 * kPointerSize);
989 initial_map->set_instance_descriptors(*descriptors);
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000990 initial_map->set_visitor_id(StaticVisitorBase::GetVisitorId(*initial_map));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000991 }
992
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000993 { // -- J S O N
lrn@chromium.org7516f052011-03-30 08:52:27 +0000994 Handle<String> name = factory->NewStringFromAscii(CStrVector("JSON"));
995 Handle<JSFunction> cons = factory->NewFunction(
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000996 name,
lrn@chromium.org7516f052011-03-30 08:52:27 +0000997 factory->the_hole_value());
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000998 cons->SetInstancePrototype(global_context()->initial_object_prototype());
999 cons->SetInstanceClassName(*name);
lrn@chromium.org7516f052011-03-30 08:52:27 +00001000 Handle<JSObject> json_object = factory->NewJSObject(cons, TENURED);
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00001001 ASSERT(json_object->IsJSObject());
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +00001002 SetLocalPropertyNoThrow(global, name, json_object, DONT_ENUM);
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00001003 global_context()->set_json_object(*json_object);
1004 }
1005
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001006 { // --- arguments_boilerplate_
1007 // Make sure we can recognize argument objects at runtime.
1008 // This is done by introducing an anonymous function with
1009 // class_name equals 'Arguments'.
lrn@chromium.org7516f052011-03-30 08:52:27 +00001010 Handle<String> symbol = factory->LookupAsciiSymbol("Arguments");
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001011 Handle<Code> code = Handle<Code>(
lrn@chromium.org7516f052011-03-30 08:52:27 +00001012 isolate->builtins()->builtin(Builtins::kIllegal));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001013 Handle<JSObject> prototype =
1014 Handle<JSObject>(
1015 JSObject::cast(global_context()->object_function()->prototype()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001016
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001017 Handle<JSFunction> function =
lrn@chromium.org7516f052011-03-30 08:52:27 +00001018 factory->NewFunctionWithPrototype(symbol,
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001019 JS_OBJECT_TYPE,
1020 JSObject::kHeaderSize,
1021 prototype,
1022 code,
1023 false);
1024 ASSERT(!function->has_initial_map());
1025 function->shared()->set_instance_class_name(*symbol);
1026 function->shared()->set_expected_nof_properties(2);
lrn@chromium.org7516f052011-03-30 08:52:27 +00001027 Handle<JSObject> result = factory->NewJSObject(function);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001028
1029 global_context()->set_arguments_boilerplate(*result);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001030 // Note: length must be added as the first property and
1031 // callee must be added as the second property.
lrn@chromium.org7516f052011-03-30 08:52:27 +00001032 SetLocalPropertyNoThrow(result, factory->length_symbol(),
1033 factory->undefined_value(),
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +00001034 DONT_ENUM);
lrn@chromium.org7516f052011-03-30 08:52:27 +00001035 SetLocalPropertyNoThrow(result, factory->callee_symbol(),
1036 factory->undefined_value(),
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +00001037 DONT_ENUM);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001038
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001039#ifdef DEBUG
1040 LookupResult lookup;
lrn@chromium.org7516f052011-03-30 08:52:27 +00001041 result->LocalLookup(heap->callee_symbol(), &lookup);
ager@chromium.org5c838252010-02-19 08:53:10 +00001042 ASSERT(lookup.IsProperty() && (lookup.type() == FIELD));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001043 ASSERT(lookup.GetFieldIndex() == Heap::kArgumentsCalleeIndex);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001044
lrn@chromium.org7516f052011-03-30 08:52:27 +00001045 result->LocalLookup(heap->length_symbol(), &lookup);
ager@chromium.org5c838252010-02-19 08:53:10 +00001046 ASSERT(lookup.IsProperty() && (lookup.type() == FIELD));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001047 ASSERT(lookup.GetFieldIndex() == Heap::kArgumentsLengthIndex);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001048
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001049 ASSERT(result->map()->inobject_properties() > Heap::kArgumentsCalleeIndex);
1050 ASSERT(result->map()->inobject_properties() > Heap::kArgumentsLengthIndex);
1051
1052 // Check the state of the object.
1053 ASSERT(result->HasFastProperties());
1054 ASSERT(result->HasFastElements());
1055#endif
1056 }
1057
1058 { // --- strict mode arguments boilerplate
1059 const PropertyAttributes attributes =
1060 static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE | READ_ONLY);
1061
1062 // Create the ThrowTypeError functions.
lrn@chromium.org7516f052011-03-30 08:52:27 +00001063 Handle<FixedArray> callee = factory->NewFixedArray(2, TENURED);
1064 Handle<FixedArray> caller = factory->NewFixedArray(2, TENURED);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001065
danno@chromium.org40cb8782011-05-25 07:58:50 +00001066 Handle<JSFunction> throw_function =
1067 GetThrowTypeErrorFunction();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001068
1069 // Install the ThrowTypeError functions.
danno@chromium.org40cb8782011-05-25 07:58:50 +00001070 callee->set(0, *throw_function);
1071 callee->set(1, *throw_function);
1072 caller->set(0, *throw_function);
1073 caller->set(1, *throw_function);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001074
1075 // Create the descriptor array for the arguments object.
lrn@chromium.org7516f052011-03-30 08:52:27 +00001076 Handle<DescriptorArray> descriptors = factory->NewDescriptorArray(3);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001077 { // length
lrn@chromium.org7516f052011-03-30 08:52:27 +00001078 FieldDescriptor d(*factory->length_symbol(), 0, DONT_ENUM);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001079 descriptors->Set(0, &d);
1080 }
1081 { // callee
lrn@chromium.org7516f052011-03-30 08:52:27 +00001082 CallbacksDescriptor d(*factory->callee_symbol(), *callee, attributes);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001083 descriptors->Set(1, &d);
1084 }
1085 { // caller
lrn@chromium.org7516f052011-03-30 08:52:27 +00001086 CallbacksDescriptor d(*factory->caller_symbol(), *caller, attributes);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001087 descriptors->Set(2, &d);
1088 }
1089 descriptors->Sort();
1090
1091 // Create the map. Allocate one in-object field for length.
lrn@chromium.org7516f052011-03-30 08:52:27 +00001092 Handle<Map> map = factory->NewMap(JS_OBJECT_TYPE,
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001093 Heap::kArgumentsObjectSizeStrict);
1094 map->set_instance_descriptors(*descriptors);
1095 map->set_function_with_prototype(true);
1096 map->set_prototype(global_context()->object_function()->prototype());
1097 map->set_pre_allocated_property_fields(1);
1098 map->set_inobject_properties(1);
1099
1100 // Copy constructor from the non-strict arguments boilerplate.
1101 map->set_constructor(
1102 global_context()->arguments_boilerplate()->map()->constructor());
1103
1104 // Allocate the arguments boilerplate object.
lrn@chromium.org7516f052011-03-30 08:52:27 +00001105 Handle<JSObject> result = factory->NewJSObjectFromMap(map);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001106 global_context()->set_strict_mode_arguments_boilerplate(*result);
1107
1108 // Add length property only for strict mode boilerplate.
lrn@chromium.org7516f052011-03-30 08:52:27 +00001109 SetLocalPropertyNoThrow(result, factory->length_symbol(),
1110 factory->undefined_value(),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001111 DONT_ENUM);
1112
1113#ifdef DEBUG
1114 LookupResult lookup;
lrn@chromium.org7516f052011-03-30 08:52:27 +00001115 result->LocalLookup(heap->length_symbol(), &lookup);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001116 ASSERT(lookup.IsProperty() && (lookup.type() == FIELD));
1117 ASSERT(lookup.GetFieldIndex() == Heap::kArgumentsLengthIndex);
1118
1119 ASSERT(result->map()->inobject_properties() > Heap::kArgumentsLengthIndex);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001120
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001121 // Check the state of the object.
1122 ASSERT(result->HasFastProperties());
1123 ASSERT(result->HasFastElements());
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001124#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001125 }
1126
1127 { // --- context extension
1128 // Create a function for the context extension objects.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001129 Handle<Code> code = Handle<Code>(
lrn@chromium.org7516f052011-03-30 08:52:27 +00001130 isolate->builtins()->builtin(Builtins::kIllegal));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001131 Handle<JSFunction> context_extension_fun =
lrn@chromium.org7516f052011-03-30 08:52:27 +00001132 factory->NewFunction(factory->empty_symbol(),
ager@chromium.org32912102009-01-16 10:38:43 +00001133 JS_CONTEXT_EXTENSION_OBJECT_TYPE,
1134 JSObject::kHeaderSize,
1135 code,
1136 true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001137
lrn@chromium.org7516f052011-03-30 08:52:27 +00001138 Handle<String> name = factory->LookupAsciiSymbol("context_extension");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001139 context_extension_fun->shared()->set_instance_class_name(*name);
1140 global_context()->set_context_extension_function(*context_extension_fun);
1141 }
1142
sgjesse@chromium.org05521fc2009-05-21 07:37:44 +00001143
1144 {
1145 // Setup the call-as-function delegate.
1146 Handle<Code> code =
lrn@chromium.org7516f052011-03-30 08:52:27 +00001147 Handle<Code>(isolate->builtins()->builtin(
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00001148 Builtins::kHandleApiCallAsFunction));
sgjesse@chromium.org05521fc2009-05-21 07:37:44 +00001149 Handle<JSFunction> delegate =
lrn@chromium.org7516f052011-03-30 08:52:27 +00001150 factory->NewFunction(factory->empty_symbol(), JS_OBJECT_TYPE,
sgjesse@chromium.org05521fc2009-05-21 07:37:44 +00001151 JSObject::kHeaderSize, code, true);
1152 global_context()->set_call_as_function_delegate(*delegate);
1153 delegate->shared()->DontAdaptArguments();
1154 }
1155
1156 {
1157 // Setup the call-as-constructor delegate.
1158 Handle<Code> code =
lrn@chromium.org7516f052011-03-30 08:52:27 +00001159 Handle<Code>(isolate->builtins()->builtin(
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00001160 Builtins::kHandleApiCallAsConstructor));
sgjesse@chromium.org05521fc2009-05-21 07:37:44 +00001161 Handle<JSFunction> delegate =
lrn@chromium.org7516f052011-03-30 08:52:27 +00001162 factory->NewFunction(factory->empty_symbol(), JS_OBJECT_TYPE,
sgjesse@chromium.org05521fc2009-05-21 07:37:44 +00001163 JSObject::kHeaderSize, code, true);
1164 global_context()->set_call_as_constructor_delegate(*delegate);
1165 delegate->shared()->DontAdaptArguments();
1166 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001167
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001168 // Initialize the out of memory slot.
lrn@chromium.org7516f052011-03-30 08:52:27 +00001169 global_context()->set_out_of_memory(heap->false_value());
ager@chromium.org9085a012009-05-11 19:22:57 +00001170
1171 // Initialize the data slot.
lrn@chromium.org7516f052011-03-30 08:52:27 +00001172 global_context()->set_data(heap->undefined_value());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001173}
1174
1175
danno@chromium.org160a7b02011-04-18 15:51:38 +00001176bool Genesis::CompileBuiltin(Isolate* isolate, int index) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001177 Vector<const char> name = Natives::GetScriptName(index);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001178 Handle<String> source_code =
danno@chromium.org160a7b02011-04-18 15:51:38 +00001179 isolate->bootstrapper()->NativesSourceLookup(index);
1180 return CompileNative(name, source_code);
1181}
1182
1183
1184bool Genesis::CompileExperimentalBuiltin(Isolate* isolate, int index) {
1185 Vector<const char> name = ExperimentalNatives::GetScriptName(index);
1186 Factory* factory = isolate->factory();
1187 Handle<String> source_code =
jkummerow@chromium.orge297f592011-06-08 10:05:15 +00001188 factory->NewStringFromAscii(
1189 ExperimentalNatives::GetRawScriptSource(index));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001190 return CompileNative(name, source_code);
1191}
1192
1193
1194bool Genesis::CompileNative(Vector<const char> name, Handle<String> source) {
1195 HandleScope scope;
danno@chromium.org160a7b02011-04-18 15:51:38 +00001196 Isolate* isolate = source->GetIsolate();
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001197#ifdef ENABLE_DEBUGGER_SUPPORT
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001198 isolate->debugger()->set_compiling_natives(true);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001199#endif
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001200 bool result = CompileScriptCached(name,
1201 source,
1202 NULL,
1203 NULL,
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001204 Handle<Context>(isolate->context()),
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001205 true);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001206 ASSERT(isolate->has_pending_exception() != result);
1207 if (!result) isolate->clear_pending_exception();
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001208#ifdef ENABLE_DEBUGGER_SUPPORT
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001209 isolate->debugger()->set_compiling_natives(false);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001210#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001211 return result;
1212}
1213
1214
1215bool Genesis::CompileScriptCached(Vector<const char> name,
1216 Handle<String> source,
1217 SourceCodeCache* cache,
1218 v8::Extension* extension,
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001219 Handle<Context> top_context,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001220 bool use_runtime_context) {
danno@chromium.org160a7b02011-04-18 15:51:38 +00001221 Factory* factory = source->GetIsolate()->factory();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001222 HandleScope scope;
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001223 Handle<SharedFunctionInfo> function_info;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001224
1225 // If we can't find the function in the cache, we compile a new
1226 // function and insert it into the cache.
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001227 if (cache == NULL || !cache->Lookup(name, &function_info)) {
ager@chromium.org5ec48922009-05-05 07:25:34 +00001228 ASSERT(source->IsAsciiRepresentation());
lrn@chromium.org7516f052011-03-30 08:52:27 +00001229 Handle<String> script_name = factory->NewStringFromUtf8(name);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001230 function_info = Compiler::Compile(
1231 source,
1232 script_name,
1233 0,
1234 0,
1235 extension,
1236 NULL,
1237 Handle<String>::null(),
1238 use_runtime_context ? NATIVES_CODE : NOT_NATIVES_CODE);
1239 if (function_info.is_null()) return false;
1240 if (cache != NULL) cache->Add(name, function_info);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001241 }
1242
1243 // Setup the function context. Conceptually, we should clone the
1244 // function before overwriting the context but since we're in a
1245 // single-threaded environment it is not strictly necessary.
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001246 ASSERT(top_context->IsGlobalContext());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001247 Handle<Context> context =
1248 Handle<Context>(use_runtime_context
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001249 ? Handle<Context>(top_context->runtime_context())
1250 : top_context);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001251 Handle<JSFunction> fun =
lrn@chromium.org7516f052011-03-30 08:52:27 +00001252 factory->NewFunctionFromSharedFunctionInfo(function_info, context);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001253
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001254 // Call function using either the runtime object or the global
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001255 // object as the receiver. Provide no parameters.
1256 Handle<Object> receiver =
1257 Handle<Object>(use_runtime_context
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001258 ? top_context->builtins()
1259 : top_context->global());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001260 bool has_pending_exception;
lrn@chromium.org1c092762011-05-09 09:42:16 +00001261 Execution::Call(fun, receiver, 0, NULL, &has_pending_exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001262 if (has_pending_exception) return false;
ager@chromium.org5c838252010-02-19 08:53:10 +00001263 return true;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001264}
1265
1266
danno@chromium.org160a7b02011-04-18 15:51:38 +00001267#define INSTALL_NATIVE(Type, name, var) \
1268 Handle<String> var##_name = factory()->LookupAsciiSymbol(name); \
1269 Object* var##_native = \
1270 global_context()->builtins()->GetPropertyNoExceptionThrown( \
1271 *var##_name); \
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001272 global_context()->set_##var(Type::cast(var##_native));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001273
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001274
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001275void Genesis::InstallNativeFunctions() {
1276 HandleScope scope;
1277 INSTALL_NATIVE(JSFunction, "CreateDate", create_date_fun);
1278 INSTALL_NATIVE(JSFunction, "ToNumber", to_number_fun);
1279 INSTALL_NATIVE(JSFunction, "ToString", to_string_fun);
1280 INSTALL_NATIVE(JSFunction, "ToDetailString", to_detail_string_fun);
1281 INSTALL_NATIVE(JSFunction, "ToObject", to_object_fun);
1282 INSTALL_NATIVE(JSFunction, "ToInteger", to_integer_fun);
1283 INSTALL_NATIVE(JSFunction, "ToUint32", to_uint32_fun);
1284 INSTALL_NATIVE(JSFunction, "ToInt32", to_int32_fun);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00001285 INSTALL_NATIVE(JSFunction, "GlobalEval", global_eval_fun);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001286 INSTALL_NATIVE(JSFunction, "Instantiate", instantiate_fun);
1287 INSTALL_NATIVE(JSFunction, "ConfigureTemplateInstance",
1288 configure_instance_fun);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001289 INSTALL_NATIVE(JSFunction, "GetStackTraceLine", get_stack_trace_line_fun);
1290 INSTALL_NATIVE(JSObject, "functionCache", function_cache);
1291}
1292
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001293void Genesis::InstallExperimentalNativeFunctions() {
1294 if (FLAG_harmony_proxies) {
1295 INSTALL_NATIVE(JSFunction, "DerivedGetTrap", derived_get_trap);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00001296 INSTALL_NATIVE(JSFunction, "DerivedSetTrap", derived_set_trap);
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001297 }
1298}
1299
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001300#undef INSTALL_NATIVE
1301
1302
1303bool Genesis::InstallNatives() {
1304 HandleScope scope;
1305
1306 // Create a function for the builtins object. Allocate space for the
1307 // JavaScript builtins, a reference to the builtins object
1308 // (itself) and a reference to the global_context directly in the object.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001309 Handle<Code> code = Handle<Code>(
danno@chromium.org160a7b02011-04-18 15:51:38 +00001310 isolate()->builtins()->builtin(Builtins::kIllegal));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001311 Handle<JSFunction> builtins_fun =
danno@chromium.org160a7b02011-04-18 15:51:38 +00001312 factory()->NewFunction(factory()->empty_symbol(),
1313 JS_BUILTINS_OBJECT_TYPE,
1314 JSBuiltinsObject::kSize, code, true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001315
danno@chromium.org160a7b02011-04-18 15:51:38 +00001316 Handle<String> name = factory()->LookupAsciiSymbol("builtins");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001317 builtins_fun->shared()->set_instance_class_name(*name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001318
1319 // Allocate the builtins object.
1320 Handle<JSBuiltinsObject> builtins =
danno@chromium.org160a7b02011-04-18 15:51:38 +00001321 Handle<JSBuiltinsObject>::cast(factory()->NewGlobalObject(builtins_fun));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001322 builtins->set_builtins(*builtins);
1323 builtins->set_global_context(*global_context());
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001324 builtins->set_global_receiver(*builtins);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001325
1326 // Setup the 'global' properties of the builtins object. The
1327 // 'global' property that refers to the global object is the only
1328 // way to get from code running in the builtins context to the
1329 // global object.
1330 static const PropertyAttributes attributes =
1331 static_cast<PropertyAttributes>(READ_ONLY | DONT_DELETE);
danno@chromium.org160a7b02011-04-18 15:51:38 +00001332 Handle<String> global_symbol = factory()->LookupAsciiSymbol("global");
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +00001333 Handle<Object> global_obj(global_context()->global());
1334 SetLocalPropertyNoThrow(builtins, global_symbol, global_obj, attributes);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001335
1336 // Setup the reference from the global object to the builtins object.
1337 JSGlobalObject::cast(global_context()->global())->set_builtins(*builtins);
1338
1339 // Create a bridge function that has context in the global context.
1340 Handle<JSFunction> bridge =
danno@chromium.org160a7b02011-04-18 15:51:38 +00001341 factory()->NewFunction(factory()->empty_symbol(),
1342 factory()->undefined_value());
1343 ASSERT(bridge->context() == *isolate()->global_context());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001344
1345 // Allocate the builtins context.
1346 Handle<Context> context =
danno@chromium.org160a7b02011-04-18 15:51:38 +00001347 factory()->NewFunctionContext(Context::MIN_CONTEXT_SLOTS, bridge);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001348 context->set_global(*builtins); // override builtins global object
1349
1350 global_context()->set_runtime_context(*context);
1351
1352 { // -- S c r i p t
1353 // Builtin functions for Script.
1354 Handle<JSFunction> script_fun =
1355 InstallFunction(builtins, "Script", JS_VALUE_TYPE, JSValue::kSize,
danno@chromium.org160a7b02011-04-18 15:51:38 +00001356 isolate()->initial_object_prototype(),
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00001357 Builtins::kIllegal, false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001358 Handle<JSObject> prototype =
danno@chromium.org160a7b02011-04-18 15:51:38 +00001359 factory()->NewJSObject(isolate()->object_function(), TENURED);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001360 SetPrototype(script_fun, prototype);
1361 global_context()->set_script_function(*script_fun);
1362
1363 // Add 'source' and 'data' property to scripts.
1364 PropertyAttributes common_attributes =
1365 static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE | READ_ONLY);
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001366 Handle<Foreign> foreign_source =
1367 factory()->NewForeign(&Accessors::ScriptSource);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001368 Handle<DescriptorArray> script_descriptors =
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001369 factory()->CopyAppendForeignDescriptor(
danno@chromium.org160a7b02011-04-18 15:51:38 +00001370 factory()->empty_descriptor_array(),
1371 factory()->LookupAsciiSymbol("source"),
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001372 foreign_source,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001373 common_attributes);
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001374 Handle<Foreign> foreign_name =
1375 factory()->NewForeign(&Accessors::ScriptName);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001376 script_descriptors =
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001377 factory()->CopyAppendForeignDescriptor(
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001378 script_descriptors,
danno@chromium.org160a7b02011-04-18 15:51:38 +00001379 factory()->LookupAsciiSymbol("name"),
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001380 foreign_name,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001381 common_attributes);
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001382 Handle<Foreign> foreign_id = factory()->NewForeign(&Accessors::ScriptId);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001383 script_descriptors =
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001384 factory()->CopyAppendForeignDescriptor(
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001385 script_descriptors,
danno@chromium.org160a7b02011-04-18 15:51:38 +00001386 factory()->LookupAsciiSymbol("id"),
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001387 foreign_id,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001388 common_attributes);
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001389 Handle<Foreign> foreign_line_offset =
1390 factory()->NewForeign(&Accessors::ScriptLineOffset);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001391 script_descriptors =
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001392 factory()->CopyAppendForeignDescriptor(
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001393 script_descriptors,
danno@chromium.org160a7b02011-04-18 15:51:38 +00001394 factory()->LookupAsciiSymbol("line_offset"),
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001395 foreign_line_offset,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001396 common_attributes);
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001397 Handle<Foreign> foreign_column_offset =
1398 factory()->NewForeign(&Accessors::ScriptColumnOffset);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001399 script_descriptors =
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001400 factory()->CopyAppendForeignDescriptor(
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001401 script_descriptors,
danno@chromium.org160a7b02011-04-18 15:51:38 +00001402 factory()->LookupAsciiSymbol("column_offset"),
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001403 foreign_column_offset,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001404 common_attributes);
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001405 Handle<Foreign> foreign_data =
1406 factory()->NewForeign(&Accessors::ScriptData);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001407 script_descriptors =
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001408 factory()->CopyAppendForeignDescriptor(
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001409 script_descriptors,
danno@chromium.org160a7b02011-04-18 15:51:38 +00001410 factory()->LookupAsciiSymbol("data"),
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001411 foreign_data,
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001412 common_attributes);
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001413 Handle<Foreign> foreign_type =
1414 factory()->NewForeign(&Accessors::ScriptType);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001415 script_descriptors =
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001416 factory()->CopyAppendForeignDescriptor(
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001417 script_descriptors,
danno@chromium.org160a7b02011-04-18 15:51:38 +00001418 factory()->LookupAsciiSymbol("type"),
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001419 foreign_type,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001420 common_attributes);
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001421 Handle<Foreign> foreign_compilation_type =
1422 factory()->NewForeign(&Accessors::ScriptCompilationType);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001423 script_descriptors =
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001424 factory()->CopyAppendForeignDescriptor(
ager@chromium.orge2902be2009-06-08 12:21:35 +00001425 script_descriptors,
danno@chromium.org160a7b02011-04-18 15:51:38 +00001426 factory()->LookupAsciiSymbol("compilation_type"),
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001427 foreign_compilation_type,
ager@chromium.orge2902be2009-06-08 12:21:35 +00001428 common_attributes);
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001429 Handle<Foreign> foreign_line_ends =
1430 factory()->NewForeign(&Accessors::ScriptLineEnds);
iposva@chromium.org245aa852009-02-10 00:49:54 +00001431 script_descriptors =
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001432 factory()->CopyAppendForeignDescriptor(
iposva@chromium.org245aa852009-02-10 00:49:54 +00001433 script_descriptors,
danno@chromium.org160a7b02011-04-18 15:51:38 +00001434 factory()->LookupAsciiSymbol("line_ends"),
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001435 foreign_line_ends,
iposva@chromium.org245aa852009-02-10 00:49:54 +00001436 common_attributes);
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001437 Handle<Foreign> foreign_context_data =
1438 factory()->NewForeign(&Accessors::ScriptContextData);
ager@chromium.org9085a012009-05-11 19:22:57 +00001439 script_descriptors =
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001440 factory()->CopyAppendForeignDescriptor(
ager@chromium.org9085a012009-05-11 19:22:57 +00001441 script_descriptors,
danno@chromium.org160a7b02011-04-18 15:51:38 +00001442 factory()->LookupAsciiSymbol("context_data"),
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001443 foreign_context_data,
ager@chromium.org9085a012009-05-11 19:22:57 +00001444 common_attributes);
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001445 Handle<Foreign> foreign_eval_from_script =
1446 factory()->NewForeign(&Accessors::ScriptEvalFromScript);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001447 script_descriptors =
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001448 factory()->CopyAppendForeignDescriptor(
ager@chromium.orge2902be2009-06-08 12:21:35 +00001449 script_descriptors,
danno@chromium.org160a7b02011-04-18 15:51:38 +00001450 factory()->LookupAsciiSymbol("eval_from_script"),
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001451 foreign_eval_from_script,
ager@chromium.orge2902be2009-06-08 12:21:35 +00001452 common_attributes);
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001453 Handle<Foreign> foreign_eval_from_script_position =
1454 factory()->NewForeign(&Accessors::ScriptEvalFromScriptPosition);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001455 script_descriptors =
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001456 factory()->CopyAppendForeignDescriptor(
ager@chromium.orge2902be2009-06-08 12:21:35 +00001457 script_descriptors,
danno@chromium.org160a7b02011-04-18 15:51:38 +00001458 factory()->LookupAsciiSymbol("eval_from_script_position"),
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001459 foreign_eval_from_script_position,
sgjesse@chromium.org98180592009-12-02 08:17:28 +00001460 common_attributes);
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001461 Handle<Foreign> foreign_eval_from_function_name =
1462 factory()->NewForeign(&Accessors::ScriptEvalFromFunctionName);
sgjesse@chromium.org98180592009-12-02 08:17:28 +00001463 script_descriptors =
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001464 factory()->CopyAppendForeignDescriptor(
sgjesse@chromium.org98180592009-12-02 08:17:28 +00001465 script_descriptors,
danno@chromium.org160a7b02011-04-18 15:51:38 +00001466 factory()->LookupAsciiSymbol("eval_from_function_name"),
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001467 foreign_eval_from_function_name,
ager@chromium.orge2902be2009-06-08 12:21:35 +00001468 common_attributes);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001469
1470 Handle<Map> script_map = Handle<Map>(script_fun->initial_map());
1471 script_map->set_instance_descriptors(*script_descriptors);
1472
1473 // Allocate the empty script.
danno@chromium.org160a7b02011-04-18 15:51:38 +00001474 Handle<Script> script = factory()->NewScript(factory()->empty_string());
ager@chromium.orge2902be2009-06-08 12:21:35 +00001475 script->set_type(Smi::FromInt(Script::TYPE_NATIVE));
danno@chromium.org160a7b02011-04-18 15:51:38 +00001476 heap()->public_set_empty_script(*script);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001477 }
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001478 {
1479 // Builtin function for OpaqueReference -- a JSValue-based object,
1480 // that keeps its field isolated from JavaScript code. It may store
1481 // objects, that JavaScript code may not access.
1482 Handle<JSFunction> opaque_reference_fun =
1483 InstallFunction(builtins, "OpaqueReference", JS_VALUE_TYPE,
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001484 JSValue::kSize,
danno@chromium.org160a7b02011-04-18 15:51:38 +00001485 isolate()->initial_object_prototype(),
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00001486 Builtins::kIllegal, false);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001487 Handle<JSObject> prototype =
danno@chromium.org160a7b02011-04-18 15:51:38 +00001488 factory()->NewJSObject(isolate()->object_function(), TENURED);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001489 SetPrototype(opaque_reference_fun, prototype);
1490 global_context()->set_opaque_reference_function(*opaque_reference_fun);
1491 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001492
karlklose@chromium.org8f806e82011-03-07 14:06:08 +00001493 { // --- I n t e r n a l A r r a y ---
1494 // An array constructor on the builtins object that works like
1495 // the public Array constructor, except that its prototype
1496 // doesn't inherit from Object.prototype.
1497 // To be used only for internal work by builtins. Instances
1498 // must not be leaked to user code.
1499 // Only works correctly when called as a constructor. The normal
1500 // Array code uses Array.prototype as prototype when called as
1501 // a function.
1502 Handle<JSFunction> array_function =
1503 InstallFunction(builtins,
1504 "InternalArray",
1505 JS_ARRAY_TYPE,
1506 JSArray::kSize,
danno@chromium.org160a7b02011-04-18 15:51:38 +00001507 isolate()->initial_object_prototype(),
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00001508 Builtins::kArrayCode,
karlklose@chromium.org8f806e82011-03-07 14:06:08 +00001509 true);
1510 Handle<JSObject> prototype =
danno@chromium.org160a7b02011-04-18 15:51:38 +00001511 factory()->NewJSObject(isolate()->object_function(), TENURED);
karlklose@chromium.org8f806e82011-03-07 14:06:08 +00001512 SetPrototype(array_function, prototype);
1513
1514 array_function->shared()->set_construct_stub(
danno@chromium.org160a7b02011-04-18 15:51:38 +00001515 isolate()->builtins()->builtin(Builtins::kArrayConstructCode));
karlklose@chromium.org8f806e82011-03-07 14:06:08 +00001516 array_function->shared()->DontAdaptArguments();
1517
1518 // Make "length" magic on instances.
1519 Handle<DescriptorArray> array_descriptors =
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001520 factory()->CopyAppendForeignDescriptor(
danno@chromium.org160a7b02011-04-18 15:51:38 +00001521 factory()->empty_descriptor_array(),
1522 factory()->length_symbol(),
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001523 factory()->NewForeign(&Accessors::ArrayLength),
karlklose@chromium.org8f806e82011-03-07 14:06:08 +00001524 static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE));
1525
1526 array_function->initial_map()->set_instance_descriptors(
1527 *array_descriptors);
1528 }
1529
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001530 if (FLAG_disable_native_files) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001531 PrintF("Warning: Running without installed natives!\n");
1532 return true;
1533 }
1534
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001535 // Install natives.
1536 for (int i = Natives::GetDebuggerCount();
1537 i < Natives::GetBuiltinsCount();
1538 i++) {
danno@chromium.org160a7b02011-04-18 15:51:38 +00001539 if (!CompileBuiltin(isolate(), i)) return false;
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001540 // TODO(ager): We really only need to install the JS builtin
1541 // functions on the builtins object after compiling and running
1542 // runtime.js.
1543 if (!InstallJSBuiltins(builtins)) return false;
1544 }
1545
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001546 InstallNativeFunctions();
1547
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00001548 // Store the map for the string prototype after the natives has been compiled
1549 // and the String function has been setup.
1550 Handle<JSFunction> string_function(global_context()->string_function());
1551 ASSERT(JSObject::cast(
1552 string_function->initial_map()->prototype())->HasFastProperties());
1553 global_context()->set_string_function_prototype_map(
1554 HeapObject::cast(string_function->initial_map()->prototype())->map());
1555
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001556 InstallBuiltinFunctionIds();
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001557
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001558 // Install Function.prototype.call and apply.
danno@chromium.org160a7b02011-04-18 15:51:38 +00001559 { Handle<String> key = factory()->function_class_symbol();
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001560 Handle<JSFunction> function =
danno@chromium.org160a7b02011-04-18 15:51:38 +00001561 Handle<JSFunction>::cast(GetProperty(isolate()->global(), key));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001562 Handle<JSObject> proto =
1563 Handle<JSObject>(JSObject::cast(function->instance_prototype()));
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001564
1565 // Install the call and the apply functions.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001566 Handle<JSFunction> call =
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001567 InstallFunction(proto, "call", JS_OBJECT_TYPE, JSObject::kHeaderSize,
kmillikin@chromium.org4111b802010-05-03 10:34:42 +00001568 Handle<JSObject>::null(),
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00001569 Builtins::kFunctionCall,
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001570 false);
1571 Handle<JSFunction> apply =
1572 InstallFunction(proto, "apply", JS_OBJECT_TYPE, JSObject::kHeaderSize,
kmillikin@chromium.org4111b802010-05-03 10:34:42 +00001573 Handle<JSObject>::null(),
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00001574 Builtins::kFunctionApply,
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001575 false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001576
1577 // Make sure that Function.prototype.call appears to be compiled.
1578 // The code will never be called, but inline caching for call will
1579 // only work if it appears to be compiled.
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001580 call->shared()->DontAdaptArguments();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001581 ASSERT(call->is_compiled());
1582
ager@chromium.org32912102009-01-16 10:38:43 +00001583 // Set the expected parameters for apply to 2; required by builtin.
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001584 apply->shared()->set_formal_parameter_count(2);
1585
1586 // Set the lengths for the functions to satisfy ECMA-262.
1587 call->shared()->set_length(1);
1588 apply->shared()->set_length(2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001589 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001590
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00001591 // Create a constructor for RegExp results (a variant of Array that
1592 // predefines the two properties index and match).
1593 {
1594 // RegExpResult initial map.
1595
1596 // Find global.Array.prototype to inherit from.
1597 Handle<JSFunction> array_constructor(global_context()->array_function());
1598 Handle<JSObject> array_prototype(
1599 JSObject::cast(array_constructor->instance_prototype()));
1600
1601 // Add initial map.
1602 Handle<Map> initial_map =
danno@chromium.org160a7b02011-04-18 15:51:38 +00001603 factory()->NewMap(JS_ARRAY_TYPE, JSRegExpResult::kSize);
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00001604 initial_map->set_constructor(*array_constructor);
1605
1606 // Set prototype on map.
1607 initial_map->set_non_instance_prototype(false);
1608 initial_map->set_prototype(*array_prototype);
1609
1610 // Update map with length accessor from Array and add "index" and "input".
1611 Handle<Map> array_map(global_context()->js_array_map());
1612 Handle<DescriptorArray> array_descriptors(
1613 array_map->instance_descriptors());
1614 ASSERT_EQ(1, array_descriptors->number_of_descriptors());
1615
1616 Handle<DescriptorArray> reresult_descriptors =
danno@chromium.org160a7b02011-04-18 15:51:38 +00001617 factory()->NewDescriptorArray(3);
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00001618
1619 reresult_descriptors->CopyFrom(0, *array_descriptors, 0);
1620
1621 int enum_index = 0;
1622 {
danno@chromium.org160a7b02011-04-18 15:51:38 +00001623 FieldDescriptor index_field(heap()->index_symbol(),
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00001624 JSRegExpResult::kIndexIndex,
1625 NONE,
1626 enum_index++);
1627 reresult_descriptors->Set(1, &index_field);
1628 }
1629
1630 {
danno@chromium.org160a7b02011-04-18 15:51:38 +00001631 FieldDescriptor input_field(heap()->input_symbol(),
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00001632 JSRegExpResult::kInputIndex,
1633 NONE,
1634 enum_index++);
1635 reresult_descriptors->Set(2, &input_field);
1636 }
1637 reresult_descriptors->Sort();
1638
1639 initial_map->set_inobject_properties(2);
1640 initial_map->set_pre_allocated_property_fields(2);
1641 initial_map->set_unused_property_fields(0);
1642 initial_map->set_instance_descriptors(*reresult_descriptors);
1643
1644 global_context()->set_regexp_result_map(*initial_map);
1645 }
1646
karlklose@chromium.org8f806e82011-03-07 14:06:08 +00001647
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001648#ifdef DEBUG
1649 builtins->Verify();
1650#endif
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001651
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001652 return true;
1653}
1654
1655
danno@chromium.org160a7b02011-04-18 15:51:38 +00001656bool Genesis::InstallExperimentalNatives() {
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001657 for (int i = ExperimentalNatives::GetDebuggerCount();
1658 i < ExperimentalNatives::GetBuiltinsCount();
1659 i++) {
1660 if (FLAG_harmony_proxies &&
1661 strcmp(ExperimentalNatives::GetScriptName(i).start(),
1662 "native proxy.js") == 0) {
danno@chromium.org160a7b02011-04-18 15:51:38 +00001663 if (!CompileExperimentalBuiltin(isolate(), i)) return false;
1664 }
1665 }
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001666
1667 InstallExperimentalNativeFunctions();
1668
danno@chromium.org160a7b02011-04-18 15:51:38 +00001669 return true;
1670}
1671
1672
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001673static Handle<JSObject> ResolveBuiltinIdHolder(
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001674 Handle<Context> global_context,
1675 const char* holder_expr) {
danno@chromium.org160a7b02011-04-18 15:51:38 +00001676 Factory* factory = global_context->GetIsolate()->factory();
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001677 Handle<GlobalObject> global(global_context->global());
1678 const char* period_pos = strchr(holder_expr, '.');
1679 if (period_pos == NULL) {
1680 return Handle<JSObject>::cast(
lrn@chromium.org7516f052011-03-30 08:52:27 +00001681 GetProperty(global, factory->LookupAsciiSymbol(holder_expr)));
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00001682 }
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001683 ASSERT_EQ(".prototype", period_pos);
1684 Vector<const char> property(holder_expr,
1685 static_cast<int>(period_pos - holder_expr));
1686 Handle<JSFunction> function = Handle<JSFunction>::cast(
lrn@chromium.org7516f052011-03-30 08:52:27 +00001687 GetProperty(global, factory->LookupSymbol(property)));
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001688 return Handle<JSObject>(JSObject::cast(function->prototype()));
1689}
1690
1691
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001692static void InstallBuiltinFunctionId(Handle<JSObject> holder,
1693 const char* function_name,
1694 BuiltinFunctionId id) {
danno@chromium.org160a7b02011-04-18 15:51:38 +00001695 Factory* factory = holder->GetIsolate()->factory();
1696 Handle<String> name = factory->LookupAsciiSymbol(function_name);
lrn@chromium.org303ada72010-10-27 09:33:13 +00001697 Object* function_object = holder->GetProperty(*name)->ToObjectUnchecked();
1698 Handle<JSFunction> function(JSFunction::cast(function_object));
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001699 function->shared()->set_function_data(Smi::FromInt(id));
1700}
1701
1702
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001703void Genesis::InstallBuiltinFunctionIds() {
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001704 HandleScope scope;
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001705#define INSTALL_BUILTIN_ID(holder_expr, fun_name, name) \
1706 { \
1707 Handle<JSObject> holder = ResolveBuiltinIdHolder( \
1708 global_context(), #holder_expr); \
1709 BuiltinFunctionId id = k##name; \
1710 InstallBuiltinFunctionId(holder, #fun_name, id); \
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001711 }
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001712 FUNCTIONS_WITH_ID_LIST(INSTALL_BUILTIN_ID)
1713#undef INSTALL_BUILTIN_ID
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001714}
1715
1716
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00001717// Do not forget to update macros.py with named constant
1718// of cache id.
1719#define JSFUNCTION_RESULT_CACHE_LIST(F) \
1720 F(16, global_context()->regexp_function())
1721
1722
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001723static FixedArray* CreateCache(int size, Handle<JSFunction> factory_function) {
danno@chromium.org160a7b02011-04-18 15:51:38 +00001724 Factory* factory = factory_function->GetIsolate()->factory();
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00001725 // Caches are supposed to live for a long time, allocate in old space.
1726 int array_size = JSFunctionResultCache::kEntriesIndex + 2 * size;
ager@chromium.orgac091b72010-05-05 07:34:42 +00001727 // Cannot use cast as object is not fully initialized yet.
1728 JSFunctionResultCache* cache = reinterpret_cast<JSFunctionResultCache*>(
danno@chromium.org160a7b02011-04-18 15:51:38 +00001729 *factory->NewFixedArrayWithHoles(array_size, TENURED));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001730 cache->set(JSFunctionResultCache::kFactoryIndex, *factory_function);
ager@chromium.orgac091b72010-05-05 07:34:42 +00001731 cache->MakeZeroSize();
1732 return cache;
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00001733}
1734
1735
1736void Genesis::InstallJSFunctionResultCaches() {
1737 const int kNumberOfCaches = 0 +
1738#define F(size, func) + 1
1739 JSFUNCTION_RESULT_CACHE_LIST(F)
1740#undef F
1741 ;
1742
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001743 Handle<FixedArray> caches = FACTORY->NewFixedArray(kNumberOfCaches, TENURED);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00001744
1745 int index = 0;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001746
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001747#define F(size, func) do { \
1748 FixedArray* cache = CreateCache((size), Handle<JSFunction>(func)); \
1749 caches->set(index++, cache); \
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001750 } while (false)
1751
1752 JSFUNCTION_RESULT_CACHE_LIST(F);
1753
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00001754#undef F
1755
1756 global_context()->set_jsfunction_result_caches(*caches);
1757}
1758
1759
ricow@chromium.org65fae842010-08-25 15:26:24 +00001760void Genesis::InitializeNormalizedMapCaches() {
1761 Handle<FixedArray> array(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001762 FACTORY->NewFixedArray(NormalizedMapCache::kEntries, TENURED));
ricow@chromium.org65fae842010-08-25 15:26:24 +00001763 global_context()->set_normalized_map_cache(NormalizedMapCache::cast(*array));
1764}
1765
1766
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001767bool Bootstrapper::InstallExtensions(Handle<Context> global_context,
1768 v8::ExtensionConfiguration* extensions) {
danno@chromium.org160a7b02011-04-18 15:51:38 +00001769 Isolate* isolate = global_context->GetIsolate();
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001770 BootstrapperActive active;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001771 SaveContext saved_context(isolate);
1772 isolate->set_context(*global_context);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001773 if (!Genesis::InstallExtensions(global_context, extensions)) return false;
1774 Genesis::InstallSpecialObjects(global_context);
1775 return true;
1776}
1777
1778
1779void Genesis::InstallSpecialObjects(Handle<Context> global_context) {
danno@chromium.org160a7b02011-04-18 15:51:38 +00001780 Factory* factory = global_context->GetIsolate()->factory();
mads.s.agercbaa0602008-08-14 13:41:48 +00001781 HandleScope scope;
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001782 Handle<JSGlobalObject> js_global(
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001783 JSGlobalObject::cast(global_context->global()));
mads.s.agercbaa0602008-08-14 13:41:48 +00001784 // Expose the natives in global if a name for it is specified.
1785 if (FLAG_expose_natives_as != NULL && strlen(FLAG_expose_natives_as) != 0) {
1786 Handle<String> natives_string =
lrn@chromium.org7516f052011-03-30 08:52:27 +00001787 factory->LookupAsciiSymbol(FLAG_expose_natives_as);
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +00001788 SetLocalPropertyNoThrow(js_global, natives_string,
1789 Handle<JSObject>(js_global->builtins()), DONT_ENUM);
mads.s.agercbaa0602008-08-14 13:41:48 +00001790 }
1791
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00001792 Handle<Object> Error = GetProperty(js_global, "Error");
1793 if (Error->IsJSObject()) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00001794 Handle<String> name = factory->LookupAsciiSymbol("stackTraceLimit");
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +00001795 SetLocalPropertyNoThrow(Handle<JSObject>::cast(Error),
1796 name,
1797 Handle<Smi>(Smi::FromInt(FLAG_stack_trace_limit)),
1798 NONE);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001799 }
1800
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001801#ifdef ENABLE_DEBUGGER_SUPPORT
mads.s.agercbaa0602008-08-14 13:41:48 +00001802 // Expose the debug global object in global if a name for it is specified.
1803 if (FLAG_expose_debug_as != NULL && strlen(FLAG_expose_debug_as) != 0) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001804 Debug* debug = Isolate::Current()->debug();
mads.s.agercbaa0602008-08-14 13:41:48 +00001805 // If loading fails we just bail out without installing the
1806 // debugger but without tanking the whole context.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001807 if (!debug->Load()) return;
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001808 // Set the security token for the debugger context to the same as
1809 // the shell global context to allow calling between these (otherwise
1810 // exposing debug global object doesn't make much sense).
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001811 debug->debug_context()->set_security_token(
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001812 global_context->security_token());
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001813
mads.s.agercbaa0602008-08-14 13:41:48 +00001814 Handle<String> debug_string =
lrn@chromium.org7516f052011-03-30 08:52:27 +00001815 factory->LookupAsciiSymbol(FLAG_expose_debug_as);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001816 Handle<Object> global_proxy(debug->debug_context()->global_proxy());
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +00001817 SetLocalPropertyNoThrow(js_global, debug_string, global_proxy, DONT_ENUM);
mads.s.agercbaa0602008-08-14 13:41:48 +00001818 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001819#endif
mads.s.agercbaa0602008-08-14 13:41:48 +00001820}
1821
1822
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001823bool Genesis::InstallExtensions(Handle<Context> global_context,
1824 v8::ExtensionConfiguration* extensions) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001825 // TODO(isolates): Extensions on multiple isolates may take a little more
1826 // effort. (The external API reads 'ignore'-- does that mean
1827 // we can break the interface?)
1828
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001829 // Clear coloring of extension list
1830 v8::RegisteredExtension* current = v8::RegisteredExtension::first_extension();
1831 while (current != NULL) {
1832 current->set_state(v8::UNVISITED);
1833 current = current->next();
1834 }
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001835 // Install auto extensions.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001836 current = v8::RegisteredExtension::first_extension();
1837 while (current != NULL) {
1838 if (current->extension()->auto_enable())
1839 InstallExtension(current);
1840 current = current->next();
1841 }
1842
1843 if (FLAG_expose_gc) InstallExtension("v8/gc");
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00001844 if (FLAG_expose_externalize_string) InstallExtension("v8/externalize");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001845
1846 if (extensions == NULL) return true;
1847 // Install required extensions
1848 int count = v8::ImplementationUtilities::GetNameCount(extensions);
1849 const char** names = v8::ImplementationUtilities::GetNames(extensions);
1850 for (int i = 0; i < count; i++) {
1851 if (!InstallExtension(names[i]))
1852 return false;
1853 }
1854
1855 return true;
1856}
1857
1858
1859// Installs a named extension. This methods is unoptimized and does
1860// not scale well if we want to support a large number of extensions.
1861bool Genesis::InstallExtension(const char* name) {
1862 v8::RegisteredExtension* current = v8::RegisteredExtension::first_extension();
1863 // Loop until we find the relevant extension
1864 while (current != NULL) {
1865 if (strcmp(name, current->extension()->name()) == 0) break;
1866 current = current->next();
1867 }
1868 // Didn't find the extension; fail.
1869 if (current == NULL) {
1870 v8::Utils::ReportApiFailure(
1871 "v8::Context::New()", "Cannot find required extension");
1872 return false;
1873 }
1874 return InstallExtension(current);
1875}
1876
1877
1878bool Genesis::InstallExtension(v8::RegisteredExtension* current) {
1879 HandleScope scope;
1880
1881 if (current->state() == v8::INSTALLED) return true;
1882 // The current node has already been visited so there must be a
1883 // cycle in the dependency graph; fail.
1884 if (current->state() == v8::VISITED) {
1885 v8::Utils::ReportApiFailure(
1886 "v8::Context::New()", "Circular extension dependency");
1887 return false;
1888 }
1889 ASSERT(current->state() == v8::UNVISITED);
1890 current->set_state(v8::VISITED);
1891 v8::Extension* extension = current->extension();
1892 // Install the extension's dependencies
1893 for (int i = 0; i < extension->dependency_count(); i++) {
1894 if (!InstallExtension(extension->dependencies()[i])) return false;
1895 }
lrn@chromium.org7516f052011-03-30 08:52:27 +00001896 Isolate* isolate = Isolate::Current();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001897 Vector<const char> source = CStrVector(extension->source());
lrn@chromium.org7516f052011-03-30 08:52:27 +00001898 Handle<String> source_code = isolate->factory()->NewStringFromAscii(source);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001899 bool result = CompileScriptCached(CStrVector(extension->name()),
1900 source_code,
lrn@chromium.org7516f052011-03-30 08:52:27 +00001901 isolate->bootstrapper()->extensions_cache(),
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001902 extension,
lrn@chromium.org7516f052011-03-30 08:52:27 +00001903 Handle<Context>(isolate->context()),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001904 false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00001905 ASSERT(isolate->has_pending_exception() != result);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001906 if (!result) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00001907 isolate->clear_pending_exception();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001908 }
1909 current->set_state(v8::INSTALLED);
1910 return result;
1911}
1912
1913
ager@chromium.org5c838252010-02-19 08:53:10 +00001914bool Genesis::InstallJSBuiltins(Handle<JSBuiltinsObject> builtins) {
1915 HandleScope scope;
danno@chromium.org160a7b02011-04-18 15:51:38 +00001916 Factory* factory = builtins->GetIsolate()->factory();
ager@chromium.org5c838252010-02-19 08:53:10 +00001917 for (int i = 0; i < Builtins::NumberOfJavaScriptBuiltins(); i++) {
1918 Builtins::JavaScript id = static_cast<Builtins::JavaScript>(i);
danno@chromium.org160a7b02011-04-18 15:51:38 +00001919 Handle<String> name = factory->LookupAsciiSymbol(Builtins::GetName(id));
lrn@chromium.org303ada72010-10-27 09:33:13 +00001920 Object* function_object = builtins->GetPropertyNoExceptionThrown(*name);
ager@chromium.org5c838252010-02-19 08:53:10 +00001921 Handle<JSFunction> function
lrn@chromium.org303ada72010-10-27 09:33:13 +00001922 = Handle<JSFunction>(JSFunction::cast(function_object));
ager@chromium.org5c838252010-02-19 08:53:10 +00001923 builtins->set_javascript_builtin(id, *function);
1924 Handle<SharedFunctionInfo> shared
1925 = Handle<SharedFunctionInfo>(function->shared());
1926 if (!EnsureCompiled(shared, CLEAR_EXCEPTION)) return false;
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00001927 // Set the code object on the function object.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001928 function->ReplaceCode(function->shared()->code());
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00001929 builtins->set_javascript_builtin_code(id, shared->code());
ager@chromium.org5c838252010-02-19 08:53:10 +00001930 }
1931 return true;
1932}
1933
1934
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001935bool Genesis::ConfigureGlobalObjects(
1936 v8::Handle<v8::ObjectTemplate> global_proxy_template) {
1937 Handle<JSObject> global_proxy(
1938 JSObject::cast(global_context()->global_proxy()));
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001939 Handle<JSObject> inner_global(JSObject::cast(global_context()->global()));
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001940
1941 if (!global_proxy_template.IsEmpty()) {
1942 // Configure the global proxy object.
1943 Handle<ObjectTemplateInfo> proxy_data =
1944 v8::Utils::OpenHandle(*global_proxy_template);
1945 if (!ConfigureApiObject(global_proxy, proxy_data)) return false;
1946
1947 // Configure the inner global object.
1948 Handle<FunctionTemplateInfo> proxy_constructor(
1949 FunctionTemplateInfo::cast(proxy_data->constructor()));
1950 if (!proxy_constructor->prototype_template()->IsUndefined()) {
1951 Handle<ObjectTemplateInfo> inner_data(
1952 ObjectTemplateInfo::cast(proxy_constructor->prototype_template()));
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001953 if (!ConfigureApiObject(inner_global, inner_data)) return false;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001954 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001955 }
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001956
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001957 SetObjectPrototype(global_proxy, inner_global);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001958 return true;
1959}
1960
1961
1962bool Genesis::ConfigureApiObject(Handle<JSObject> object,
1963 Handle<ObjectTemplateInfo> object_template) {
1964 ASSERT(!object_template.is_null());
1965 ASSERT(object->IsInstanceOf(
1966 FunctionTemplateInfo::cast(object_template->constructor())));
1967
1968 bool pending_exception = false;
1969 Handle<JSObject> obj =
1970 Execution::InstantiateObject(object_template, &pending_exception);
1971 if (pending_exception) {
danno@chromium.org160a7b02011-04-18 15:51:38 +00001972 ASSERT(isolate()->has_pending_exception());
1973 isolate()->clear_pending_exception();
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001974 return false;
1975 }
1976 TransferObject(obj, object);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001977 return true;
1978}
1979
1980
1981void Genesis::TransferNamedProperties(Handle<JSObject> from,
1982 Handle<JSObject> to) {
1983 if (from->HasFastProperties()) {
1984 Handle<DescriptorArray> descs =
1985 Handle<DescriptorArray>(from->map()->instance_descriptors());
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001986 for (int i = 0; i < descs->number_of_descriptors(); i++) {
1987 PropertyDetails details = PropertyDetails(descs->GetDetails(i));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001988 switch (details.type()) {
1989 case FIELD: {
1990 HandleScope inner;
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001991 Handle<String> key = Handle<String>(descs->GetKey(i));
1992 int index = descs->GetFieldIndex(i);
ager@chromium.org7c537e22008-10-16 08:43:32 +00001993 Handle<Object> value = Handle<Object>(from->FastPropertyAt(index));
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +00001994 SetLocalPropertyNoThrow(to, key, value, details.attributes());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001995 break;
1996 }
1997 case CONSTANT_FUNCTION: {
1998 HandleScope inner;
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001999 Handle<String> key = Handle<String>(descs->GetKey(i));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002000 Handle<JSFunction> fun =
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002001 Handle<JSFunction>(descs->GetConstantFunction(i));
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +00002002 SetLocalPropertyNoThrow(to, key, fun, details.attributes());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002003 break;
2004 }
2005 case CALLBACKS: {
2006 LookupResult result;
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002007 to->LocalLookup(descs->GetKey(i), &result);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002008 // If the property is already there we skip it
ager@chromium.org5c838252010-02-19 08:53:10 +00002009 if (result.IsProperty()) continue;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002010 HandleScope inner;
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002011 ASSERT(!to->HasFastProperties());
2012 // Add to dictionary.
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002013 Handle<String> key = Handle<String>(descs->GetKey(i));
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002014 Handle<Object> callbacks(descs->GetCallbacksObject(i));
2015 PropertyDetails d =
2016 PropertyDetails(details.attributes(), CALLBACKS, details.index());
2017 SetNormalizedProperty(to, key, callbacks, d);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002018 break;
2019 }
2020 case MAP_TRANSITION:
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00002021 case EXTERNAL_ARRAY_TRANSITION:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002022 case CONSTANT_TRANSITION:
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002023 case NULL_DESCRIPTOR:
2024 // Ignore non-properties.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002025 break;
2026 case NORMAL:
2027 // Do not occur since the from object has fast properties.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002028 case HANDLER:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002029 case INTERCEPTOR:
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002030 // No element in instance descriptors have proxy or interceptor type.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002031 UNREACHABLE();
2032 break;
2033 }
2034 }
2035 } else {
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00002036 Handle<StringDictionary> properties =
2037 Handle<StringDictionary>(from->property_dictionary());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002038 int capacity = properties->Capacity();
2039 for (int i = 0; i < capacity; i++) {
2040 Object* raw_key(properties->KeyAt(i));
2041 if (properties->IsKey(raw_key)) {
2042 ASSERT(raw_key->IsString());
2043 // If the property is already there we skip it.
2044 LookupResult result;
2045 to->LocalLookup(String::cast(raw_key), &result);
ager@chromium.org5c838252010-02-19 08:53:10 +00002046 if (result.IsProperty()) continue;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002047 // Set the property.
2048 Handle<String> key = Handle<String>(String::cast(raw_key));
2049 Handle<Object> value = Handle<Object>(properties->ValueAt(i));
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002050 if (value->IsJSGlobalPropertyCell()) {
2051 value = Handle<Object>(JSGlobalPropertyCell::cast(*value)->value());
2052 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002053 PropertyDetails details = properties->DetailsAt(i);
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +00002054 SetLocalPropertyNoThrow(to, key, value, details.attributes());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002055 }
2056 }
2057 }
2058}
2059
2060
2061void Genesis::TransferIndexedProperties(Handle<JSObject> from,
2062 Handle<JSObject> to) {
2063 // Cloning the elements array is sufficient.
2064 Handle<FixedArray> from_elements =
2065 Handle<FixedArray>(FixedArray::cast(from->elements()));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002066 Handle<FixedArray> to_elements = FACTORY->CopyFixedArray(from_elements);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002067 to->set_elements(*to_elements);
2068}
2069
2070
2071void Genesis::TransferObject(Handle<JSObject> from, Handle<JSObject> to) {
2072 HandleScope outer;
danno@chromium.org160a7b02011-04-18 15:51:38 +00002073 Factory* factory = from->GetIsolate()->factory();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002074
2075 ASSERT(!from->IsJSArray());
2076 ASSERT(!to->IsJSArray());
2077
2078 TransferNamedProperties(from, to);
2079 TransferIndexedProperties(from, to);
2080
2081 // Transfer the prototype (new map is needed).
2082 Handle<Map> old_to_map = Handle<Map>(to->map());
danno@chromium.org160a7b02011-04-18 15:51:38 +00002083 Handle<Map> new_to_map = factory->CopyMapDropTransitions(old_to_map);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002084 new_to_map->set_prototype(from->map()->prototype());
2085 to->set_map(*new_to_map);
2086}
2087
2088
2089void Genesis::MakeFunctionInstancePrototypeWritable() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002090 // The maps with writable prototype are created in CreateEmptyFunction
2091 // and CreateStrictModeFunctionMaps respectively. Initially the maps are
2092 // created with read-only prototype for JS builtins processing.
2093 ASSERT(!function_instance_map_writable_prototype_.is_null());
2094 ASSERT(!strict_mode_function_instance_map_writable_prototype_.is_null());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002095
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002096 // Replace function instance maps to make prototype writable.
2097 global_context()->set_function_map(
2098 *function_instance_map_writable_prototype_);
2099 global_context()->set_strict_mode_function_map(
2100 *strict_mode_function_instance_map_writable_prototype_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002101}
2102
2103
danno@chromium.org160a7b02011-04-18 15:51:38 +00002104Genesis::Genesis(Isolate* isolate,
2105 Handle<Object> global_object,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002106 v8::Handle<v8::ObjectTemplate> global_template,
danno@chromium.org160a7b02011-04-18 15:51:38 +00002107 v8::ExtensionConfiguration* extensions) : isolate_(isolate) {
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00002108 result_ = Handle<Context>::null();
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002109 // If V8 isn't running and cannot be initialized, just return.
2110 if (!V8::IsRunning() && !V8::Initialize(NULL)) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002111
2112 // Before creating the roots we must save the context and restore it
2113 // on all function exits.
2114 HandleScope scope;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002115 SaveContext saved_context(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002116
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002117 Handle<Context> new_context = Snapshot::NewContextFromSnapshot();
2118 if (!new_context.is_null()) {
2119 global_context_ =
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002120 Handle<Context>::cast(isolate->global_handles()->Create(*new_context));
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002121 AddToWeakGlobalContextList(*global_context_);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002122 isolate->set_context(*global_context_);
2123 isolate->counters()->contexts_created_by_snapshot()->Increment();
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002124 Handle<GlobalObject> inner_global;
2125 Handle<JSGlobalProxy> global_proxy =
2126 CreateNewGlobals(global_template,
2127 global_object,
2128 &inner_global);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002129
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002130 HookUpGlobalProxy(inner_global, global_proxy);
2131 HookUpInnerGlobal(inner_global);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002132
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002133 if (!ConfigureGlobalObjects(global_template)) return;
2134 } else {
2135 // We get here if there was no context snapshot.
2136 CreateRoots();
danno@chromium.org160a7b02011-04-18 15:51:38 +00002137 Handle<JSFunction> empty_function = CreateEmptyFunction(isolate);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002138 CreateStrictModeFunctionMaps(empty_function);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002139 Handle<GlobalObject> inner_global;
2140 Handle<JSGlobalProxy> global_proxy =
2141 CreateNewGlobals(global_template, global_object, &inner_global);
2142 HookUpGlobalProxy(inner_global, global_proxy);
2143 InitializeGlobal(inner_global, empty_function);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00002144 InstallJSFunctionResultCaches();
ricow@chromium.org65fae842010-08-25 15:26:24 +00002145 InitializeNormalizedMapCaches();
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00002146 if (!InstallNatives()) return;
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002147
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002148 MakeFunctionInstancePrototypeWritable();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002149
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002150 if (!ConfigureGlobalObjects(global_template)) return;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002151 isolate->counters()->contexts_created_from_scratch()->Increment();
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002152 }
mads.s.agercbaa0602008-08-14 13:41:48 +00002153
danno@chromium.org160a7b02011-04-18 15:51:38 +00002154 // Install experimental natives.
2155 if (!InstallExperimentalNatives()) return;
2156
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002157 result_ = global_context_;
2158}
2159
ager@chromium.orgddb913d2009-01-27 10:01:48 +00002160
2161// Support for thread preemption.
2162
2163// Reserve space for statics needing saving and restoring.
2164int Bootstrapper::ArchiveSpacePerThread() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002165 return sizeof(NestingCounterType);
ager@chromium.orgddb913d2009-01-27 10:01:48 +00002166}
2167
2168
2169// Archive statics that are thread local.
2170char* Bootstrapper::ArchiveState(char* to) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002171 *reinterpret_cast<NestingCounterType*>(to) = nesting_;
2172 nesting_ = 0;
2173 return to + sizeof(NestingCounterType);
ager@chromium.orgddb913d2009-01-27 10:01:48 +00002174}
2175
2176
2177// Restore statics that are thread local.
2178char* Bootstrapper::RestoreState(char* from) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002179 nesting_ = *reinterpret_cast<NestingCounterType*>(from);
2180 return from + sizeof(NestingCounterType);
ager@chromium.orgddb913d2009-01-27 10:01:48 +00002181}
2182
2183
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002184// Called when the top-level V8 mutex is destroyed.
2185void Bootstrapper::FreeThreadResources() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002186 ASSERT(!IsActive());
ager@chromium.orgddb913d2009-01-27 10:01:48 +00002187}
2188
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002189} } // namespace v8::internal