blob: 7abd45cac0294975f6b2d9e9a607a5beae180707 [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);
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +0000202 void InitializeExperimentalGlobal();
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000203 // Installs the contents of the native .js files on the global objects.
204 // Used for creating a context from scratch.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000205 void InstallNativeFunctions();
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000206 void InstallExperimentalNativeFunctions();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000207 bool InstallNatives();
danno@chromium.org160a7b02011-04-18 15:51:38 +0000208 bool InstallExperimentalNatives();
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000209 void InstallBuiltinFunctionIds();
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000210 void InstallJSFunctionResultCaches();
ricow@chromium.org65fae842010-08-25 15:26:24 +0000211 void InitializeNormalizedMapCaches();
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000212 // Used both for deserialized and from-scratch contexts to add the extensions
213 // provided.
214 static bool InstallExtensions(Handle<Context> global_context,
215 v8::ExtensionConfiguration* extensions);
216 static bool InstallExtension(const char* name);
217 static bool InstallExtension(v8::RegisteredExtension* current);
218 static void InstallSpecialObjects(Handle<Context> global_context);
ager@chromium.org5c838252010-02-19 08:53:10 +0000219 bool InstallJSBuiltins(Handle<JSBuiltinsObject> builtins);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000220 bool ConfigureApiObject(Handle<JSObject> object,
221 Handle<ObjectTemplateInfo> object_template);
222 bool ConfigureGlobalObjects(v8::Handle<v8::ObjectTemplate> global_template);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000223
224 // Migrates all properties from the 'from' object to the 'to'
225 // object and overrides the prototype in 'to' with the one from
226 // 'from'.
227 void TransferObject(Handle<JSObject> from, Handle<JSObject> to);
228 void TransferNamedProperties(Handle<JSObject> from, Handle<JSObject> to);
229 void TransferIndexedProperties(Handle<JSObject> from, Handle<JSObject> to);
230
kmillikin@chromium.org4111b802010-05-03 10:34:42 +0000231 enum PrototypePropertyMode {
232 DONT_ADD_PROTOTYPE,
233 ADD_READONLY_PROTOTYPE,
234 ADD_WRITEABLE_PROTOTYPE
235 };
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000236
237 Handle<Map> CreateFunctionMap(PrototypePropertyMode prototype_mode);
238
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000239 Handle<DescriptorArray> ComputeFunctionInstanceDescriptor(
kmillikin@chromium.org4111b802010-05-03 10:34:42 +0000240 PrototypePropertyMode prototypeMode);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000241 void MakeFunctionInstancePrototypeWritable();
242
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000243 Handle<Map> CreateStrictModeFunctionMap(
244 PrototypePropertyMode prototype_mode,
245 Handle<JSFunction> empty_function,
246 Handle<FixedArray> arguments_callbacks,
247 Handle<FixedArray> caller_callbacks);
248
249 Handle<DescriptorArray> ComputeStrictFunctionInstanceDescriptor(
250 PrototypePropertyMode propertyMode,
251 Handle<FixedArray> arguments,
252 Handle<FixedArray> caller);
253
danno@chromium.org160a7b02011-04-18 15:51:38 +0000254 static bool CompileBuiltin(Isolate* isolate, int index);
255 static bool CompileExperimentalBuiltin(Isolate* isolate, int index);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000256 static bool CompileNative(Vector<const char> name, Handle<String> source);
257 static bool CompileScriptCached(Vector<const char> name,
258 Handle<String> source,
259 SourceCodeCache* cache,
260 v8::Extension* extension,
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000261 Handle<Context> top_context,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000262 bool use_runtime_context);
263
264 Handle<Context> result_;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000265
266 // Function instance maps. Function literal maps are created initially with
267 // a read only prototype for the processing of JS builtins. Later the function
268 // instance maps are replaced in order to make prototype writable.
269 // These are the final, writable prototype, maps.
270 Handle<Map> function_instance_map_writable_prototype_;
271 Handle<Map> strict_mode_function_instance_map_writable_prototype_;
danno@chromium.org40cb8782011-05-25 07:58:50 +0000272 Handle<JSFunction> throw_type_error_function;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000273
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000274 BootstrapperActive active_;
275 friend class Bootstrapper;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000276};
277
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000278
279void Bootstrapper::Iterate(ObjectVisitor* v) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000280 extensions_cache_.Iterate(v);
ager@chromium.org3811b432009-10-28 14:53:37 +0000281 v->Synchronize("Extensions");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000282}
283
284
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000285Handle<Context> Bootstrapper::CreateEnvironment(
danno@chromium.org160a7b02011-04-18 15:51:38 +0000286 Isolate* isolate,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000287 Handle<Object> global_object,
288 v8::Handle<v8::ObjectTemplate> global_template,
289 v8::ExtensionConfiguration* extensions) {
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000290 HandleScope scope;
291 Handle<Context> env;
danno@chromium.org160a7b02011-04-18 15:51:38 +0000292 Genesis genesis(isolate, global_object, global_template, extensions);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000293 env = genesis.result();
294 if (!env.is_null()) {
295 if (InstallExtensions(env, extensions)) {
296 return env;
297 }
298 }
299 return Handle<Context>();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000300}
301
302
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000303static void SetObjectPrototype(Handle<JSObject> object, Handle<Object> proto) {
304 // object.__proto__ = proto;
danno@chromium.org160a7b02011-04-18 15:51:38 +0000305 Factory* factory = object->GetIsolate()->factory();
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000306 Handle<Map> old_to_map = Handle<Map>(object->map());
danno@chromium.org160a7b02011-04-18 15:51:38 +0000307 Handle<Map> new_to_map = factory->CopyMapDropTransitions(old_to_map);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000308 new_to_map->set_prototype(*proto);
309 object->set_map(*new_to_map);
310}
311
312
313void Bootstrapper::DetachGlobal(Handle<Context> env) {
danno@chromium.org160a7b02011-04-18 15:51:38 +0000314 Factory* factory = env->GetIsolate()->factory();
lrn@chromium.org7516f052011-03-30 08:52:27 +0000315 JSGlobalProxy::cast(env->global_proxy())->set_context(*factory->null_value());
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000316 SetObjectPrototype(Handle<JSObject>(env->global_proxy()),
lrn@chromium.org7516f052011-03-30 08:52:27 +0000317 factory->null_value());
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000318 env->set_global_proxy(env->global());
319 env->global()->set_global_receiver(env->global());
320}
321
322
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +0000323void Bootstrapper::ReattachGlobal(Handle<Context> env,
324 Handle<Object> global_object) {
325 ASSERT(global_object->IsJSGlobalProxy());
326 Handle<JSGlobalProxy> global = Handle<JSGlobalProxy>::cast(global_object);
327 env->global()->set_global_receiver(*global);
328 env->set_global_proxy(*global);
329 SetObjectPrototype(global, Handle<JSObject>(env->global()));
330 global->set_context(*env);
331}
332
333
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000334static Handle<JSFunction> InstallFunction(Handle<JSObject> target,
335 const char* name,
336 InstanceType type,
337 int instance_size,
338 Handle<JSObject> prototype,
339 Builtins::Name call,
340 bool is_ecma_native) {
danno@chromium.org160a7b02011-04-18 15:51:38 +0000341 Isolate* isolate = target->GetIsolate();
lrn@chromium.org7516f052011-03-30 08:52:27 +0000342 Factory* factory = isolate->factory();
343 Handle<String> symbol = factory->LookupAsciiSymbol(name);
344 Handle<Code> call_code = Handle<Code>(isolate->builtins()->builtin(call));
kmillikin@chromium.org4111b802010-05-03 10:34:42 +0000345 Handle<JSFunction> function = prototype.is_null() ?
lrn@chromium.org7516f052011-03-30 08:52:27 +0000346 factory->NewFunctionWithoutPrototype(symbol, call_code) :
347 factory->NewFunctionWithPrototype(symbol,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000348 type,
349 instance_size,
350 prototype,
351 call_code,
352 is_ecma_native);
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000353 PropertyAttributes attributes;
354 if (target->IsJSBuiltinsObject()) {
355 attributes =
356 static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE | READ_ONLY);
357 } else {
358 attributes = DONT_ENUM;
359 }
360 SetLocalPropertyNoThrow(target, symbol, function, attributes);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000361 if (is_ecma_native) {
362 function->shared()->set_instance_class_name(*symbol);
363 }
364 return function;
365}
366
367
368Handle<DescriptorArray> Genesis::ComputeFunctionInstanceDescriptor(
kmillikin@chromium.org4111b802010-05-03 10:34:42 +0000369 PrototypePropertyMode prototypeMode) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000370 Handle<DescriptorArray> descriptors =
danno@chromium.org160a7b02011-04-18 15:51:38 +0000371 factory()->NewDescriptorArray(prototypeMode == DONT_ADD_PROTOTYPE
372 ? 4
373 : 5);
kmillikin@chromium.org4111b802010-05-03 10:34:42 +0000374 PropertyAttributes attributes =
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000375 static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE | READ_ONLY);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000376
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000377 { // Add length.
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000378 Handle<Foreign> foreign = factory()->NewForeign(&Accessors::FunctionLength);
379 CallbacksDescriptor d(*factory()->length_symbol(), *foreign, attributes);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000380 descriptors->Set(0, &d);
381 }
382 { // Add name.
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000383 Handle<Foreign> foreign = factory()->NewForeign(&Accessors::FunctionName);
384 CallbacksDescriptor d(*factory()->name_symbol(), *foreign, attributes);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000385 descriptors->Set(1, &d);
386 }
387 { // Add arguments.
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000388 Handle<Foreign> foreign =
389 factory()->NewForeign(&Accessors::FunctionArguments);
390 CallbacksDescriptor d(*factory()->arguments_symbol(), *foreign, attributes);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000391 descriptors->Set(2, &d);
392 }
393 { // Add caller.
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000394 Handle<Foreign> foreign = factory()->NewForeign(&Accessors::FunctionCaller);
395 CallbacksDescriptor d(*factory()->caller_symbol(), *foreign, attributes);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000396 descriptors->Set(3, &d);
397 }
398 if (prototypeMode != DONT_ADD_PROTOTYPE) {
399 // Add prototype.
400 if (prototypeMode == ADD_WRITEABLE_PROTOTYPE) {
401 attributes = static_cast<PropertyAttributes>(attributes & ~READ_ONLY);
402 }
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000403 Handle<Foreign> foreign =
404 factory()->NewForeign(&Accessors::FunctionPrototype);
405 CallbacksDescriptor d(*factory()->prototype_symbol(), *foreign, attributes);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000406 descriptors->Set(4, &d);
407 }
408 descriptors->Sort();
409 return descriptors;
410}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000411
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000412
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000413Handle<Map> Genesis::CreateFunctionMap(PrototypePropertyMode prototype_mode) {
danno@chromium.org160a7b02011-04-18 15:51:38 +0000414 Handle<Map> map = factory()->NewMap(JS_FUNCTION_TYPE, JSFunction::kSize);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000415 Handle<DescriptorArray> descriptors =
416 ComputeFunctionInstanceDescriptor(prototype_mode);
417 map->set_instance_descriptors(*descriptors);
418 map->set_function_with_prototype(prototype_mode != DONT_ADD_PROTOTYPE);
419 return map;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000420}
421
422
danno@chromium.org160a7b02011-04-18 15:51:38 +0000423Handle<JSFunction> Genesis::CreateEmptyFunction(Isolate* isolate) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000424 // Allocate the map for function instances. Maps are allocated first and their
425 // prototypes patched later, once empty function is created.
426
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000427 // Please note that the prototype property for function instances must be
428 // writable.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000429 Handle<Map> function_instance_map =
430 CreateFunctionMap(ADD_WRITEABLE_PROTOTYPE);
431 global_context()->set_function_instance_map(*function_instance_map);
kmillikin@chromium.org4111b802010-05-03 10:34:42 +0000432
433 // Functions with this map will not have a 'prototype' property, and
434 // can not be used as constructors.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000435 Handle<Map> function_without_prototype_map =
436 CreateFunctionMap(DONT_ADD_PROTOTYPE);
kmillikin@chromium.org4111b802010-05-03 10:34:42 +0000437 global_context()->set_function_without_prototype_map(
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000438 *function_without_prototype_map);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000439
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000440 // Allocate the function map. This map is temporary, used only for processing
441 // of builtins.
442 // Later the map is replaced with writable prototype map, allocated below.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000443 Handle<Map> function_map = CreateFunctionMap(ADD_READONLY_PROTOTYPE);
444 global_context()->set_function_map(*function_map);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000445
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000446 // The final map for functions. Writeable prototype.
447 // This map is installed in MakeFunctionInstancePrototypeWritable.
448 function_instance_map_writable_prototype_ =
449 CreateFunctionMap(ADD_WRITEABLE_PROTOTYPE);
450
lrn@chromium.org7516f052011-03-30 08:52:27 +0000451 Factory* factory = isolate->factory();
452 Heap* heap = isolate->heap();
453
454 Handle<String> object_name = Handle<String>(heap->Object_symbol());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000455
456 { // --- O b j e c t ---
457 Handle<JSFunction> object_fun =
lrn@chromium.org7516f052011-03-30 08:52:27 +0000458 factory->NewFunction(object_name, factory->null_value());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000459 Handle<Map> object_function_map =
lrn@chromium.org7516f052011-03-30 08:52:27 +0000460 factory->NewMap(JS_OBJECT_TYPE, JSObject::kHeaderSize);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000461 object_fun->set_initial_map(*object_function_map);
462 object_function_map->set_constructor(*object_fun);
463
464 global_context()->set_object_function(*object_fun);
465
466 // Allocate a new prototype for the object function.
lrn@chromium.org7516f052011-03-30 08:52:27 +0000467 Handle<JSObject> prototype = factory->NewJSObject(
468 isolate->object_function(),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000469 TENURED);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000470
471 global_context()->set_initial_object_prototype(*prototype);
472 SetPrototype(object_fun, prototype);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000473 object_function_map->
lrn@chromium.org7516f052011-03-30 08:52:27 +0000474 set_instance_descriptors(heap->empty_descriptor_array());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000475 }
476
477 // Allocate the empty function as the prototype for function ECMAScript
478 // 262 15.3.4.
lrn@chromium.org7516f052011-03-30 08:52:27 +0000479 Handle<String> symbol = factory->LookupAsciiSymbol("Empty");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000480 Handle<JSFunction> empty_function =
lrn@chromium.org7516f052011-03-30 08:52:27 +0000481 factory->NewFunctionWithoutPrototype(symbol, kNonStrictMode);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000482
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000483 // --- E m p t y ---
484 Handle<Code> code =
lrn@chromium.org7516f052011-03-30 08:52:27 +0000485 Handle<Code>(isolate->builtins()->builtin(
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000486 Builtins::kEmptyFunction));
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000487 empty_function->set_code(*code);
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000488 empty_function->shared()->set_code(*code);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000489 Handle<String> source = factory->NewStringFromAscii(CStrVector("() {}"));
490 Handle<Script> script = factory->NewScript(source);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000491 script->set_type(Smi::FromInt(Script::TYPE_NATIVE));
492 empty_function->shared()->set_script(*script);
493 empty_function->shared()->set_start_position(0);
494 empty_function->shared()->set_end_position(source->length());
495 empty_function->shared()->DontAdaptArguments();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000496
497 // Set prototypes for the function maps.
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000498 global_context()->function_map()->set_prototype(*empty_function);
499 global_context()->function_instance_map()->set_prototype(*empty_function);
kmillikin@chromium.org4111b802010-05-03 10:34:42 +0000500 global_context()->function_without_prototype_map()->
501 set_prototype(*empty_function);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000502 function_instance_map_writable_prototype_->set_prototype(*empty_function);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000503
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000504 // Allocate the function map first and then patch the prototype later
lrn@chromium.org7516f052011-03-30 08:52:27 +0000505 Handle<Map> empty_fm = factory->CopyMapDropDescriptors(
kmillikin@chromium.org4111b802010-05-03 10:34:42 +0000506 function_without_prototype_map);
507 empty_fm->set_instance_descriptors(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000508 function_without_prototype_map->instance_descriptors());
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000509 empty_fm->set_prototype(global_context()->object_function()->prototype());
510 empty_function->set_map(*empty_fm);
511 return empty_function;
512}
513
514
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000515Handle<DescriptorArray> Genesis::ComputeStrictFunctionInstanceDescriptor(
516 PrototypePropertyMode prototypeMode,
517 Handle<FixedArray> arguments,
518 Handle<FixedArray> caller) {
519 Handle<DescriptorArray> descriptors =
danno@chromium.org160a7b02011-04-18 15:51:38 +0000520 factory()->NewDescriptorArray(prototypeMode == DONT_ADD_PROTOTYPE
521 ? 4
522 : 5);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000523 PropertyAttributes attributes = static_cast<PropertyAttributes>(
524 DONT_ENUM | DONT_DELETE | READ_ONLY);
525
526 { // length
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000527 Handle<Foreign> foreign = factory()->NewForeign(&Accessors::FunctionLength);
528 CallbacksDescriptor d(*factory()->length_symbol(), *foreign, attributes);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000529 descriptors->Set(0, &d);
530 }
531 { // name
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000532 Handle<Foreign> foreign = factory()->NewForeign(&Accessors::FunctionName);
533 CallbacksDescriptor d(*factory()->name_symbol(), *foreign, attributes);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000534 descriptors->Set(1, &d);
535 }
536 { // arguments
danno@chromium.org160a7b02011-04-18 15:51:38 +0000537 CallbacksDescriptor d(*factory()->arguments_symbol(),
538 *arguments,
539 attributes);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000540 descriptors->Set(2, &d);
541 }
542 { // caller
danno@chromium.org160a7b02011-04-18 15:51:38 +0000543 CallbacksDescriptor d(*factory()->caller_symbol(), *caller, attributes);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000544 descriptors->Set(3, &d);
545 }
546
547 // prototype
548 if (prototypeMode != DONT_ADD_PROTOTYPE) {
549 if (prototypeMode == ADD_WRITEABLE_PROTOTYPE) {
550 attributes = static_cast<PropertyAttributes>(attributes & ~READ_ONLY);
551 }
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000552 Handle<Foreign> foreign =
553 factory()->NewForeign(&Accessors::FunctionPrototype);
554 CallbacksDescriptor d(*factory()->prototype_symbol(), *foreign, attributes);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000555 descriptors->Set(4, &d);
556 }
557
558 descriptors->Sort();
559 return descriptors;
560}
561
562
563// ECMAScript 5th Edition, 13.2.3
danno@chromium.org40cb8782011-05-25 07:58:50 +0000564Handle<JSFunction> Genesis::GetThrowTypeErrorFunction() {
565 if (throw_type_error_function.is_null()) {
566 Handle<String> name = factory()->LookupAsciiSymbol("ThrowTypeError");
567 throw_type_error_function =
568 factory()->NewFunctionWithoutPrototype(name, kNonStrictMode);
569 Handle<Code> code(isolate()->builtins()->builtin(
570 Builtins::kStrictModePoisonPill));
571 throw_type_error_function->set_map(
572 global_context()->function_map());
573 throw_type_error_function->set_code(*code);
574 throw_type_error_function->shared()->set_code(*code);
575 throw_type_error_function->shared()->DontAdaptArguments();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000576
danno@chromium.org40cb8782011-05-25 07:58:50 +0000577 PreventExtensions(throw_type_error_function);
578 }
579 return throw_type_error_function;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000580}
581
582
583Handle<Map> Genesis::CreateStrictModeFunctionMap(
584 PrototypePropertyMode prototype_mode,
585 Handle<JSFunction> empty_function,
586 Handle<FixedArray> arguments_callbacks,
587 Handle<FixedArray> caller_callbacks) {
danno@chromium.org160a7b02011-04-18 15:51:38 +0000588 Handle<Map> map = factory()->NewMap(JS_FUNCTION_TYPE, JSFunction::kSize);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000589 Handle<DescriptorArray> descriptors =
590 ComputeStrictFunctionInstanceDescriptor(prototype_mode,
591 arguments_callbacks,
592 caller_callbacks);
593 map->set_instance_descriptors(*descriptors);
594 map->set_function_with_prototype(prototype_mode != DONT_ADD_PROTOTYPE);
595 map->set_prototype(*empty_function);
596 return map;
597}
598
599
600void Genesis::CreateStrictModeFunctionMaps(Handle<JSFunction> empty) {
601 // Create the callbacks arrays for ThrowTypeError functions.
602 // The get/set callacks are filled in after the maps are created below.
danno@chromium.org160a7b02011-04-18 15:51:38 +0000603 Factory* factory = empty->GetIsolate()->factory();
lrn@chromium.org7516f052011-03-30 08:52:27 +0000604 Handle<FixedArray> arguments = factory->NewFixedArray(2, TENURED);
605 Handle<FixedArray> caller = factory->NewFixedArray(2, TENURED);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000606
607 // Allocate map for the strict mode function instances.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000608 Handle<Map> strict_mode_function_instance_map =
609 CreateStrictModeFunctionMap(
610 ADD_WRITEABLE_PROTOTYPE, empty, arguments, caller);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000611 global_context()->set_strict_mode_function_instance_map(
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000612 *strict_mode_function_instance_map);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000613
614 // Allocate map for the prototype-less strict mode instances.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000615 Handle<Map> strict_mode_function_without_prototype_map =
616 CreateStrictModeFunctionMap(
617 DONT_ADD_PROTOTYPE, empty, arguments, caller);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000618 global_context()->set_strict_mode_function_without_prototype_map(
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000619 *strict_mode_function_without_prototype_map);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000620
621 // Allocate map for the strict mode functions. This map is temporary, used
622 // only for processing of builtins.
623 // Later the map is replaced with writable prototype map, allocated below.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000624 Handle<Map> strict_mode_function_map =
625 CreateStrictModeFunctionMap(
626 ADD_READONLY_PROTOTYPE, empty, arguments, caller);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000627 global_context()->set_strict_mode_function_map(
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000628 *strict_mode_function_map);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000629
630 // The final map for the strict mode functions. Writeable prototype.
631 // This map is installed in MakeFunctionInstancePrototypeWritable.
632 strict_mode_function_instance_map_writable_prototype_ =
633 CreateStrictModeFunctionMap(
634 ADD_WRITEABLE_PROTOTYPE, empty, arguments, caller);
635
danno@chromium.org40cb8782011-05-25 07:58:50 +0000636 // Create the ThrowTypeError function instance.
637 Handle<JSFunction> throw_function =
638 GetThrowTypeErrorFunction();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000639
640 // Complete the callback fixed arrays.
danno@chromium.org40cb8782011-05-25 07:58:50 +0000641 arguments->set(0, *throw_function);
642 arguments->set(1, *throw_function);
643 caller->set(0, *throw_function);
644 caller->set(1, *throw_function);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000645}
646
647
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000648static void AddToWeakGlobalContextList(Context* context) {
649 ASSERT(context->IsGlobalContext());
danno@chromium.org160a7b02011-04-18 15:51:38 +0000650 Heap* heap = context->GetIsolate()->heap();
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000651#ifdef DEBUG
652 { // NOLINT
653 ASSERT(context->get(Context::NEXT_CONTEXT_LINK)->IsUndefined());
654 // Check that context is not in the list yet.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000655 for (Object* current = heap->global_contexts_list();
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000656 !current->IsUndefined();
657 current = Context::cast(current)->get(Context::NEXT_CONTEXT_LINK)) {
658 ASSERT(current != context);
659 }
660 }
661#endif
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000662 context->set(Context::NEXT_CONTEXT_LINK, heap->global_contexts_list());
663 heap->set_global_contexts_list(context);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000664}
665
666
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000667void Genesis::CreateRoots() {
668 // Allocate the global context FixedArray first and then patch the
669 // closure and extension object later (we need the empty function
670 // and the global object, but in order to create those, we need the
671 // global context).
danno@chromium.org160a7b02011-04-18 15:51:38 +0000672 global_context_ = Handle<Context>::cast(isolate()->global_handles()->Create(
673 *factory()->NewGlobalContext()));
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000674 AddToWeakGlobalContextList(*global_context_);
danno@chromium.org160a7b02011-04-18 15:51:38 +0000675 isolate()->set_context(*global_context());
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000676
677 // Allocate the message listeners object.
678 {
679 v8::NeanderArray listeners;
680 global_context()->set_message_listeners(*listeners.value());
681 }
682}
683
684
685Handle<JSGlobalProxy> Genesis::CreateNewGlobals(
686 v8::Handle<v8::ObjectTemplate> global_template,
687 Handle<Object> global_object,
688 Handle<GlobalObject>* inner_global_out) {
689 // The argument global_template aka data is an ObjectTemplateInfo.
690 // It has a constructor pointer that points at global_constructor which is a
691 // FunctionTemplateInfo.
692 // The global_constructor is used to create or reinitialize the global_proxy.
693 // The global_constructor also has a prototype_template pointer that points at
694 // js_global_template which is an ObjectTemplateInfo.
695 // That in turn has a constructor pointer that points at
696 // js_global_constructor which is a FunctionTemplateInfo.
697 // js_global_constructor is used to make js_global_function
698 // js_global_function is used to make the new inner_global.
699 //
700 // --- G l o b a l ---
701 // Step 1: Create a fresh inner JSGlobalObject.
702 Handle<JSFunction> js_global_function;
703 Handle<ObjectTemplateInfo> js_global_template;
704 if (!global_template.IsEmpty()) {
705 // Get prototype template of the global_template.
706 Handle<ObjectTemplateInfo> data =
707 v8::Utils::OpenHandle(*global_template);
708 Handle<FunctionTemplateInfo> global_constructor =
709 Handle<FunctionTemplateInfo>(
710 FunctionTemplateInfo::cast(data->constructor()));
711 Handle<Object> proto_template(global_constructor->prototype_template());
712 if (!proto_template->IsUndefined()) {
713 js_global_template =
714 Handle<ObjectTemplateInfo>::cast(proto_template);
715 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000716 }
717
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000718 if (js_global_template.is_null()) {
danno@chromium.org160a7b02011-04-18 15:51:38 +0000719 Handle<String> name = Handle<String>(heap()->empty_symbol());
720 Handle<Code> code = Handle<Code>(isolate()->builtins()->builtin(
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000721 Builtins::kIllegal));
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000722 js_global_function =
danno@chromium.org160a7b02011-04-18 15:51:38 +0000723 factory()->NewFunction(name, JS_GLOBAL_OBJECT_TYPE,
724 JSGlobalObject::kSize, code, true);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000725 // Change the constructor property of the prototype of the
726 // hidden global function to refer to the Object function.
727 Handle<JSObject> prototype =
728 Handle<JSObject>(
729 JSObject::cast(js_global_function->instance_prototype()));
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +0000730 SetLocalPropertyNoThrow(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000731 prototype,
danno@chromium.org160a7b02011-04-18 15:51:38 +0000732 factory()->constructor_symbol(),
733 isolate()->object_function(),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000734 NONE);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000735 } else {
736 Handle<FunctionTemplateInfo> js_global_constructor(
737 FunctionTemplateInfo::cast(js_global_template->constructor()));
738 js_global_function =
danno@chromium.org160a7b02011-04-18 15:51:38 +0000739 factory()->CreateApiFunction(js_global_constructor,
740 factory()->InnerGlobalObject);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000741 }
742
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000743 js_global_function->initial_map()->set_is_hidden_prototype();
744 Handle<GlobalObject> inner_global =
danno@chromium.org160a7b02011-04-18 15:51:38 +0000745 factory()->NewGlobalObject(js_global_function);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000746 if (inner_global_out != NULL) {
747 *inner_global_out = inner_global;
748 }
749
750 // Step 2: create or re-initialize the global proxy object.
751 Handle<JSFunction> global_proxy_function;
752 if (global_template.IsEmpty()) {
danno@chromium.org160a7b02011-04-18 15:51:38 +0000753 Handle<String> name = Handle<String>(heap()->empty_symbol());
754 Handle<Code> code = Handle<Code>(isolate()->builtins()->builtin(
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000755 Builtins::kIllegal));
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000756 global_proxy_function =
danno@chromium.org160a7b02011-04-18 15:51:38 +0000757 factory()->NewFunction(name, JS_GLOBAL_PROXY_TYPE,
758 JSGlobalProxy::kSize, code, true);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000759 } else {
760 Handle<ObjectTemplateInfo> data =
761 v8::Utils::OpenHandle(*global_template);
762 Handle<FunctionTemplateInfo> global_constructor(
763 FunctionTemplateInfo::cast(data->constructor()));
764 global_proxy_function =
danno@chromium.org160a7b02011-04-18 15:51:38 +0000765 factory()->CreateApiFunction(global_constructor,
766 factory()->OuterGlobalObject);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000767 }
768
danno@chromium.org160a7b02011-04-18 15:51:38 +0000769 Handle<String> global_name = factory()->LookupAsciiSymbol("global");
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000770 global_proxy_function->shared()->set_instance_class_name(*global_name);
771 global_proxy_function->initial_map()->set_is_access_check_needed(true);
772
773 // Set global_proxy.__proto__ to js_global after ConfigureGlobalObjects
774 // Return the global proxy.
775
776 if (global_object.location() != NULL) {
777 ASSERT(global_object->IsJSGlobalProxy());
778 return ReinitializeJSGlobalProxy(
779 global_proxy_function,
780 Handle<JSGlobalProxy>::cast(global_object));
781 } else {
782 return Handle<JSGlobalProxy>::cast(
danno@chromium.org160a7b02011-04-18 15:51:38 +0000783 factory()->NewJSObject(global_proxy_function, TENURED));
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000784 }
785}
786
787
788void Genesis::HookUpGlobalProxy(Handle<GlobalObject> inner_global,
789 Handle<JSGlobalProxy> global_proxy) {
790 // Set the global context for the global object.
791 inner_global->set_global_context(*global_context());
792 inner_global->set_global_receiver(*global_proxy);
793 global_proxy->set_context(*global_context());
794 global_context()->set_global_proxy(*global_proxy);
795}
796
797
798void Genesis::HookUpInnerGlobal(Handle<GlobalObject> inner_global) {
799 Handle<GlobalObject> inner_global_from_snapshot(
800 GlobalObject::cast(global_context_->extension()));
801 Handle<JSBuiltinsObject> builtins_global(global_context_->builtins());
802 global_context_->set_extension(*inner_global);
803 global_context_->set_global(*inner_global);
804 global_context_->set_security_token(*inner_global);
805 static const PropertyAttributes attributes =
806 static_cast<PropertyAttributes>(READ_ONLY | DONT_DELETE);
807 ForceSetProperty(builtins_global,
danno@chromium.org160a7b02011-04-18 15:51:38 +0000808 factory()->LookupAsciiSymbol("global"),
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000809 inner_global,
810 attributes);
811 // Setup the reference from the global object to the builtins object.
812 JSGlobalObject::cast(*inner_global)->set_builtins(*builtins_global);
813 TransferNamedProperties(inner_global_from_snapshot, inner_global);
814 TransferIndexedProperties(inner_global_from_snapshot, inner_global);
815}
816
817
818// This is only called if we are not using snapshots. The equivalent
819// work in the snapshot case is done in HookUpInnerGlobal.
820void Genesis::InitializeGlobal(Handle<GlobalObject> inner_global,
821 Handle<JSFunction> empty_function) {
822 // --- G l o b a l C o n t e x t ---
823 // Use the empty function as closure (no scope info).
824 global_context()->set_closure(*empty_function);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000825 global_context()->set_previous(NULL);
826 // Set extension and global object.
827 global_context()->set_extension(*inner_global);
828 global_context()->set_global(*inner_global);
829 // Security setup: Set the security token of the global object to
830 // its the inner global. This makes the security check between two
831 // different contexts fail by default even in case of global
832 // object reinitialization.
833 global_context()->set_security_token(*inner_global);
834
danno@chromium.org160a7b02011-04-18 15:51:38 +0000835 Isolate* isolate = inner_global->GetIsolate();
lrn@chromium.org7516f052011-03-30 08:52:27 +0000836 Factory* factory = isolate->factory();
837 Heap* heap = isolate->heap();
838
839 Handle<String> object_name = Handle<String>(heap->Object_symbol());
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +0000840 SetLocalPropertyNoThrow(inner_global, object_name,
lrn@chromium.org7516f052011-03-30 08:52:27 +0000841 isolate->object_function(), DONT_ENUM);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000842
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000843 Handle<JSObject> global = Handle<JSObject>(global_context()->global());
844
845 // Install global Function object
846 InstallFunction(global, "Function", JS_FUNCTION_TYPE, JSFunction::kSize,
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000847 empty_function, Builtins::kIllegal, true); // ECMA native.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000848
849 { // --- A r r a y ---
850 Handle<JSFunction> array_function =
851 InstallFunction(global, "Array", JS_ARRAY_TYPE, JSArray::kSize,
lrn@chromium.org7516f052011-03-30 08:52:27 +0000852 isolate->initial_object_prototype(),
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000853 Builtins::kArrayCode, true);
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +0000854 array_function->shared()->set_construct_stub(
lrn@chromium.org7516f052011-03-30 08:52:27 +0000855 isolate->builtins()->builtin(Builtins::kArrayConstructCode));
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000856 array_function->shared()->DontAdaptArguments();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000857
858 // This seems a bit hackish, but we need to make sure Array.length
859 // is 1.
860 array_function->shared()->set_length(1);
861 Handle<DescriptorArray> array_descriptors =
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000862 factory->CopyAppendForeignDescriptor(
lrn@chromium.org7516f052011-03-30 08:52:27 +0000863 factory->empty_descriptor_array(),
864 factory->length_symbol(),
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000865 factory->NewForeign(&Accessors::ArrayLength),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000866 static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE));
867
868 // Cache the fast JavaScript array map
869 global_context()->set_js_array_map(array_function->initial_map());
870 global_context()->js_array_map()->set_instance_descriptors(
871 *array_descriptors);
872 // array_function is used internally. JS code creating array object should
873 // search for the 'Array' property on the global object and use that one
874 // as the constructor. 'Array' property on a global object can be
875 // overwritten by JS code.
876 global_context()->set_array_function(*array_function);
877 }
878
879 { // --- N u m b e r ---
880 Handle<JSFunction> number_fun =
881 InstallFunction(global, "Number", JS_VALUE_TYPE, JSValue::kSize,
lrn@chromium.org7516f052011-03-30 08:52:27 +0000882 isolate->initial_object_prototype(),
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000883 Builtins::kIllegal, true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000884 global_context()->set_number_function(*number_fun);
885 }
886
887 { // --- B o o l e a n ---
888 Handle<JSFunction> boolean_fun =
889 InstallFunction(global, "Boolean", JS_VALUE_TYPE, JSValue::kSize,
lrn@chromium.org7516f052011-03-30 08:52:27 +0000890 isolate->initial_object_prototype(),
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000891 Builtins::kIllegal, true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000892 global_context()->set_boolean_function(*boolean_fun);
893 }
894
895 { // --- S t r i n g ---
896 Handle<JSFunction> string_fun =
897 InstallFunction(global, "String", JS_VALUE_TYPE, JSValue::kSize,
lrn@chromium.org7516f052011-03-30 08:52:27 +0000898 isolate->initial_object_prototype(),
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000899 Builtins::kIllegal, true);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000900 string_fun->shared()->set_construct_stub(
lrn@chromium.org7516f052011-03-30 08:52:27 +0000901 isolate->builtins()->builtin(Builtins::kStringConstructCode));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000902 global_context()->set_string_function(*string_fun);
903 // Add 'length' property to strings.
904 Handle<DescriptorArray> string_descriptors =
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000905 factory->CopyAppendForeignDescriptor(
lrn@chromium.org7516f052011-03-30 08:52:27 +0000906 factory->empty_descriptor_array(),
907 factory->length_symbol(),
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000908 factory->NewForeign(&Accessors::StringLength),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000909 static_cast<PropertyAttributes>(DONT_ENUM |
910 DONT_DELETE |
911 READ_ONLY));
912
913 Handle<Map> string_map =
914 Handle<Map>(global_context()->string_function()->initial_map());
915 string_map->set_instance_descriptors(*string_descriptors);
916 }
917
918 { // --- D a t e ---
919 // Builtin functions for Date.prototype.
920 Handle<JSFunction> date_fun =
921 InstallFunction(global, "Date", JS_VALUE_TYPE, JSValue::kSize,
lrn@chromium.org7516f052011-03-30 08:52:27 +0000922 isolate->initial_object_prototype(),
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000923 Builtins::kIllegal, true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000924
925 global_context()->set_date_function(*date_fun);
926 }
927
928
929 { // -- R e g E x p
930 // Builtin functions for RegExp.prototype.
931 Handle<JSFunction> regexp_fun =
ager@chromium.org236ad962008-09-25 09:45:57 +0000932 InstallFunction(global, "RegExp", JS_REGEXP_TYPE, JSRegExp::kSize,
lrn@chromium.org7516f052011-03-30 08:52:27 +0000933 isolate->initial_object_prototype(),
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000934 Builtins::kIllegal, true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000935 global_context()->set_regexp_function(*regexp_fun);
lrn@chromium.org25156de2010-04-06 13:10:27 +0000936
937 ASSERT(regexp_fun->has_initial_map());
938 Handle<Map> initial_map(regexp_fun->initial_map());
939
940 ASSERT_EQ(0, initial_map->inobject_properties());
941
lrn@chromium.org7516f052011-03-30 08:52:27 +0000942 Handle<DescriptorArray> descriptors = factory->NewDescriptorArray(5);
lrn@chromium.org25156de2010-04-06 13:10:27 +0000943 PropertyAttributes final =
944 static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE | READ_ONLY);
945 int enum_index = 0;
946 {
947 // ECMA-262, section 15.10.7.1.
lrn@chromium.org7516f052011-03-30 08:52:27 +0000948 FieldDescriptor field(heap->source_symbol(),
lrn@chromium.org25156de2010-04-06 13:10:27 +0000949 JSRegExp::kSourceFieldIndex,
950 final,
951 enum_index++);
952 descriptors->Set(0, &field);
953 }
954 {
955 // ECMA-262, section 15.10.7.2.
lrn@chromium.org7516f052011-03-30 08:52:27 +0000956 FieldDescriptor field(heap->global_symbol(),
lrn@chromium.org25156de2010-04-06 13:10:27 +0000957 JSRegExp::kGlobalFieldIndex,
958 final,
959 enum_index++);
960 descriptors->Set(1, &field);
961 }
962 {
963 // ECMA-262, section 15.10.7.3.
lrn@chromium.org7516f052011-03-30 08:52:27 +0000964 FieldDescriptor field(heap->ignore_case_symbol(),
lrn@chromium.org25156de2010-04-06 13:10:27 +0000965 JSRegExp::kIgnoreCaseFieldIndex,
966 final,
967 enum_index++);
968 descriptors->Set(2, &field);
969 }
970 {
971 // ECMA-262, section 15.10.7.4.
lrn@chromium.org7516f052011-03-30 08:52:27 +0000972 FieldDescriptor field(heap->multiline_symbol(),
lrn@chromium.org25156de2010-04-06 13:10:27 +0000973 JSRegExp::kMultilineFieldIndex,
974 final,
975 enum_index++);
976 descriptors->Set(3, &field);
977 }
978 {
979 // ECMA-262, section 15.10.7.5.
980 PropertyAttributes writable =
981 static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000982 FieldDescriptor field(heap->last_index_symbol(),
lrn@chromium.org25156de2010-04-06 13:10:27 +0000983 JSRegExp::kLastIndexFieldIndex,
984 writable,
985 enum_index++);
986 descriptors->Set(4, &field);
987 }
988 descriptors->SetNextEnumerationIndex(enum_index);
989 descriptors->Sort();
990
991 initial_map->set_inobject_properties(5);
992 initial_map->set_pre_allocated_property_fields(5);
993 initial_map->set_unused_property_fields(0);
994 initial_map->set_instance_size(
995 initial_map->instance_size() + 5 * kPointerSize);
996 initial_map->set_instance_descriptors(*descriptors);
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000997 initial_map->set_visitor_id(StaticVisitorBase::GetVisitorId(*initial_map));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000998 }
999
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00001000 { // -- J S O N
lrn@chromium.org7516f052011-03-30 08:52:27 +00001001 Handle<String> name = factory->NewStringFromAscii(CStrVector("JSON"));
1002 Handle<JSFunction> cons = factory->NewFunction(
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00001003 name,
lrn@chromium.org7516f052011-03-30 08:52:27 +00001004 factory->the_hole_value());
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00001005 cons->SetInstancePrototype(global_context()->initial_object_prototype());
1006 cons->SetInstanceClassName(*name);
lrn@chromium.org7516f052011-03-30 08:52:27 +00001007 Handle<JSObject> json_object = factory->NewJSObject(cons, TENURED);
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00001008 ASSERT(json_object->IsJSObject());
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +00001009 SetLocalPropertyNoThrow(global, name, json_object, DONT_ENUM);
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00001010 global_context()->set_json_object(*json_object);
1011 }
1012
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001013 { // --- arguments_boilerplate_
1014 // Make sure we can recognize argument objects at runtime.
1015 // This is done by introducing an anonymous function with
1016 // class_name equals 'Arguments'.
lrn@chromium.org7516f052011-03-30 08:52:27 +00001017 Handle<String> symbol = factory->LookupAsciiSymbol("Arguments");
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001018 Handle<Code> code = Handle<Code>(
lrn@chromium.org7516f052011-03-30 08:52:27 +00001019 isolate->builtins()->builtin(Builtins::kIllegal));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001020 Handle<JSObject> prototype =
1021 Handle<JSObject>(
1022 JSObject::cast(global_context()->object_function()->prototype()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001023
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001024 Handle<JSFunction> function =
lrn@chromium.org7516f052011-03-30 08:52:27 +00001025 factory->NewFunctionWithPrototype(symbol,
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001026 JS_OBJECT_TYPE,
1027 JSObject::kHeaderSize,
1028 prototype,
1029 code,
1030 false);
1031 ASSERT(!function->has_initial_map());
1032 function->shared()->set_instance_class_name(*symbol);
1033 function->shared()->set_expected_nof_properties(2);
lrn@chromium.org7516f052011-03-30 08:52:27 +00001034 Handle<JSObject> result = factory->NewJSObject(function);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001035
1036 global_context()->set_arguments_boilerplate(*result);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001037 // Note: length must be added as the first property and
1038 // callee must be added as the second property.
lrn@chromium.org7516f052011-03-30 08:52:27 +00001039 SetLocalPropertyNoThrow(result, factory->length_symbol(),
1040 factory->undefined_value(),
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +00001041 DONT_ENUM);
lrn@chromium.org7516f052011-03-30 08:52:27 +00001042 SetLocalPropertyNoThrow(result, factory->callee_symbol(),
1043 factory->undefined_value(),
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +00001044 DONT_ENUM);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001045
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001046#ifdef DEBUG
1047 LookupResult lookup;
lrn@chromium.org7516f052011-03-30 08:52:27 +00001048 result->LocalLookup(heap->callee_symbol(), &lookup);
ager@chromium.org5c838252010-02-19 08:53:10 +00001049 ASSERT(lookup.IsProperty() && (lookup.type() == FIELD));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001050 ASSERT(lookup.GetFieldIndex() == Heap::kArgumentsCalleeIndex);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001051
lrn@chromium.org7516f052011-03-30 08:52:27 +00001052 result->LocalLookup(heap->length_symbol(), &lookup);
ager@chromium.org5c838252010-02-19 08:53:10 +00001053 ASSERT(lookup.IsProperty() && (lookup.type() == FIELD));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001054 ASSERT(lookup.GetFieldIndex() == Heap::kArgumentsLengthIndex);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001055
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001056 ASSERT(result->map()->inobject_properties() > Heap::kArgumentsCalleeIndex);
1057 ASSERT(result->map()->inobject_properties() > Heap::kArgumentsLengthIndex);
1058
1059 // Check the state of the object.
1060 ASSERT(result->HasFastProperties());
1061 ASSERT(result->HasFastElements());
1062#endif
1063 }
1064
whesse@chromium.org7b260152011-06-20 15:33:18 +00001065 { // --- aliased_arguments_boilerplate_
1066 Handle<Map> old_map(global_context()->arguments_boilerplate()->map());
1067 Handle<Map> new_map = factory->CopyMapDropTransitions(old_map);
1068 new_map->set_pre_allocated_property_fields(2);
1069 Handle<JSObject> result = factory->NewJSObjectFromMap(new_map);
1070 new_map->set_elements_kind(JSObject::NON_STRICT_ARGUMENTS_ELEMENTS);
1071 // Set up a well-formed parameter map to make assertions happy.
1072 Handle<FixedArray> elements = factory->NewFixedArray(2);
1073 elements->set_map(heap->non_strict_arguments_elements_map());
1074 Handle<FixedArray> array;
1075 array = factory->NewFixedArray(0);
1076 elements->set(0, *array);
1077 array = factory->NewFixedArray(0);
1078 elements->set(1, *array);
1079 result->set_elements(*elements);
1080 global_context()->set_aliased_arguments_boilerplate(*result);
1081 }
1082
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001083 { // --- strict mode arguments boilerplate
1084 const PropertyAttributes attributes =
1085 static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE | READ_ONLY);
1086
1087 // Create the ThrowTypeError functions.
lrn@chromium.org7516f052011-03-30 08:52:27 +00001088 Handle<FixedArray> callee = factory->NewFixedArray(2, TENURED);
1089 Handle<FixedArray> caller = factory->NewFixedArray(2, TENURED);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001090
danno@chromium.org40cb8782011-05-25 07:58:50 +00001091 Handle<JSFunction> throw_function =
1092 GetThrowTypeErrorFunction();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001093
1094 // Install the ThrowTypeError functions.
danno@chromium.org40cb8782011-05-25 07:58:50 +00001095 callee->set(0, *throw_function);
1096 callee->set(1, *throw_function);
1097 caller->set(0, *throw_function);
1098 caller->set(1, *throw_function);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001099
1100 // Create the descriptor array for the arguments object.
lrn@chromium.org7516f052011-03-30 08:52:27 +00001101 Handle<DescriptorArray> descriptors = factory->NewDescriptorArray(3);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001102 { // length
lrn@chromium.org7516f052011-03-30 08:52:27 +00001103 FieldDescriptor d(*factory->length_symbol(), 0, DONT_ENUM);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001104 descriptors->Set(0, &d);
1105 }
1106 { // callee
lrn@chromium.org7516f052011-03-30 08:52:27 +00001107 CallbacksDescriptor d(*factory->callee_symbol(), *callee, attributes);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001108 descriptors->Set(1, &d);
1109 }
1110 { // caller
lrn@chromium.org7516f052011-03-30 08:52:27 +00001111 CallbacksDescriptor d(*factory->caller_symbol(), *caller, attributes);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001112 descriptors->Set(2, &d);
1113 }
1114 descriptors->Sort();
1115
1116 // Create the map. Allocate one in-object field for length.
lrn@chromium.org7516f052011-03-30 08:52:27 +00001117 Handle<Map> map = factory->NewMap(JS_OBJECT_TYPE,
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001118 Heap::kArgumentsObjectSizeStrict);
1119 map->set_instance_descriptors(*descriptors);
1120 map->set_function_with_prototype(true);
1121 map->set_prototype(global_context()->object_function()->prototype());
1122 map->set_pre_allocated_property_fields(1);
1123 map->set_inobject_properties(1);
1124
1125 // Copy constructor from the non-strict arguments boilerplate.
1126 map->set_constructor(
1127 global_context()->arguments_boilerplate()->map()->constructor());
1128
1129 // Allocate the arguments boilerplate object.
lrn@chromium.org7516f052011-03-30 08:52:27 +00001130 Handle<JSObject> result = factory->NewJSObjectFromMap(map);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001131 global_context()->set_strict_mode_arguments_boilerplate(*result);
1132
1133 // Add length property only for strict mode boilerplate.
lrn@chromium.org7516f052011-03-30 08:52:27 +00001134 SetLocalPropertyNoThrow(result, factory->length_symbol(),
1135 factory->undefined_value(),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001136 DONT_ENUM);
1137
1138#ifdef DEBUG
1139 LookupResult lookup;
lrn@chromium.org7516f052011-03-30 08:52:27 +00001140 result->LocalLookup(heap->length_symbol(), &lookup);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001141 ASSERT(lookup.IsProperty() && (lookup.type() == FIELD));
1142 ASSERT(lookup.GetFieldIndex() == Heap::kArgumentsLengthIndex);
1143
1144 ASSERT(result->map()->inobject_properties() > Heap::kArgumentsLengthIndex);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001145
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001146 // Check the state of the object.
1147 ASSERT(result->HasFastProperties());
1148 ASSERT(result->HasFastElements());
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001149#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001150 }
1151
1152 { // --- context extension
1153 // Create a function for the context extension objects.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001154 Handle<Code> code = Handle<Code>(
lrn@chromium.org7516f052011-03-30 08:52:27 +00001155 isolate->builtins()->builtin(Builtins::kIllegal));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001156 Handle<JSFunction> context_extension_fun =
lrn@chromium.org7516f052011-03-30 08:52:27 +00001157 factory->NewFunction(factory->empty_symbol(),
ager@chromium.org32912102009-01-16 10:38:43 +00001158 JS_CONTEXT_EXTENSION_OBJECT_TYPE,
1159 JSObject::kHeaderSize,
1160 code,
1161 true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001162
lrn@chromium.org7516f052011-03-30 08:52:27 +00001163 Handle<String> name = factory->LookupAsciiSymbol("context_extension");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001164 context_extension_fun->shared()->set_instance_class_name(*name);
1165 global_context()->set_context_extension_function(*context_extension_fun);
1166 }
1167
sgjesse@chromium.org05521fc2009-05-21 07:37:44 +00001168
1169 {
yangguo@chromium.org80c42ed2011-08-31 09:03:56 +00001170 // Set up the call-as-function delegate.
sgjesse@chromium.org05521fc2009-05-21 07:37:44 +00001171 Handle<Code> code =
lrn@chromium.org7516f052011-03-30 08:52:27 +00001172 Handle<Code>(isolate->builtins()->builtin(
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00001173 Builtins::kHandleApiCallAsFunction));
sgjesse@chromium.org05521fc2009-05-21 07:37:44 +00001174 Handle<JSFunction> delegate =
lrn@chromium.org7516f052011-03-30 08:52:27 +00001175 factory->NewFunction(factory->empty_symbol(), JS_OBJECT_TYPE,
sgjesse@chromium.org05521fc2009-05-21 07:37:44 +00001176 JSObject::kHeaderSize, code, true);
1177 global_context()->set_call_as_function_delegate(*delegate);
1178 delegate->shared()->DontAdaptArguments();
1179 }
1180
1181 {
yangguo@chromium.org80c42ed2011-08-31 09:03:56 +00001182 // Set up the call-as-constructor delegate.
sgjesse@chromium.org05521fc2009-05-21 07:37:44 +00001183 Handle<Code> code =
lrn@chromium.org7516f052011-03-30 08:52:27 +00001184 Handle<Code>(isolate->builtins()->builtin(
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00001185 Builtins::kHandleApiCallAsConstructor));
sgjesse@chromium.org05521fc2009-05-21 07:37:44 +00001186 Handle<JSFunction> delegate =
lrn@chromium.org7516f052011-03-30 08:52:27 +00001187 factory->NewFunction(factory->empty_symbol(), JS_OBJECT_TYPE,
sgjesse@chromium.org05521fc2009-05-21 07:37:44 +00001188 JSObject::kHeaderSize, code, true);
1189 global_context()->set_call_as_constructor_delegate(*delegate);
1190 delegate->shared()->DontAdaptArguments();
1191 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001192
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001193 // Initialize the out of memory slot.
lrn@chromium.org7516f052011-03-30 08:52:27 +00001194 global_context()->set_out_of_memory(heap->false_value());
ager@chromium.org9085a012009-05-11 19:22:57 +00001195
1196 // Initialize the data slot.
lrn@chromium.org7516f052011-03-30 08:52:27 +00001197 global_context()->set_data(heap->undefined_value());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001198}
1199
1200
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +00001201void Genesis::InitializeExperimentalGlobal() {
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +00001202 Handle<JSObject> global = Handle<JSObject>(global_context()->global());
1203
1204 // TODO(mstarzinger): Move this into Genesis::InitializeGlobal once we no
1205 // longer need to live behind a flag, so WeakMap gets added to the snapshot.
1206 if (FLAG_harmony_weakmaps) { // -- W e a k M a p
yangguo@chromium.org80c42ed2011-08-31 09:03:56 +00001207 Handle<JSObject> prototype =
1208 factory()->NewJSObject(isolate()->object_function(), TENURED);
danno@chromium.orgb6451162011-08-17 14:33:23 +00001209 InstallFunction(global, "WeakMap", JS_WEAK_MAP_TYPE, JSWeakMap::kSize,
yangguo@chromium.org80c42ed2011-08-31 09:03:56 +00001210 prototype, Builtins::kIllegal, true);
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +00001211 }
1212}
1213
1214
danno@chromium.org160a7b02011-04-18 15:51:38 +00001215bool Genesis::CompileBuiltin(Isolate* isolate, int index) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001216 Vector<const char> name = Natives::GetScriptName(index);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001217 Handle<String> source_code =
danno@chromium.org160a7b02011-04-18 15:51:38 +00001218 isolate->bootstrapper()->NativesSourceLookup(index);
1219 return CompileNative(name, source_code);
1220}
1221
1222
1223bool Genesis::CompileExperimentalBuiltin(Isolate* isolate, int index) {
1224 Vector<const char> name = ExperimentalNatives::GetScriptName(index);
1225 Factory* factory = isolate->factory();
1226 Handle<String> source_code =
jkummerow@chromium.orge297f592011-06-08 10:05:15 +00001227 factory->NewStringFromAscii(
1228 ExperimentalNatives::GetRawScriptSource(index));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001229 return CompileNative(name, source_code);
1230}
1231
1232
1233bool Genesis::CompileNative(Vector<const char> name, Handle<String> source) {
1234 HandleScope scope;
danno@chromium.org160a7b02011-04-18 15:51:38 +00001235 Isolate* isolate = source->GetIsolate();
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001236#ifdef ENABLE_DEBUGGER_SUPPORT
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001237 isolate->debugger()->set_compiling_natives(true);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001238#endif
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001239 bool result = CompileScriptCached(name,
1240 source,
1241 NULL,
1242 NULL,
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001243 Handle<Context>(isolate->context()),
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001244 true);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001245 ASSERT(isolate->has_pending_exception() != result);
1246 if (!result) isolate->clear_pending_exception();
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001247#ifdef ENABLE_DEBUGGER_SUPPORT
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001248 isolate->debugger()->set_compiling_natives(false);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001249#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001250 return result;
1251}
1252
1253
1254bool Genesis::CompileScriptCached(Vector<const char> name,
1255 Handle<String> source,
1256 SourceCodeCache* cache,
1257 v8::Extension* extension,
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001258 Handle<Context> top_context,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001259 bool use_runtime_context) {
danno@chromium.org160a7b02011-04-18 15:51:38 +00001260 Factory* factory = source->GetIsolate()->factory();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001261 HandleScope scope;
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001262 Handle<SharedFunctionInfo> function_info;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001263
1264 // If we can't find the function in the cache, we compile a new
1265 // function and insert it into the cache.
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001266 if (cache == NULL || !cache->Lookup(name, &function_info)) {
ager@chromium.org5ec48922009-05-05 07:25:34 +00001267 ASSERT(source->IsAsciiRepresentation());
lrn@chromium.org7516f052011-03-30 08:52:27 +00001268 Handle<String> script_name = factory->NewStringFromUtf8(name);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001269 function_info = Compiler::Compile(
1270 source,
1271 script_name,
1272 0,
1273 0,
1274 extension,
1275 NULL,
1276 Handle<String>::null(),
1277 use_runtime_context ? NATIVES_CODE : NOT_NATIVES_CODE);
1278 if (function_info.is_null()) return false;
1279 if (cache != NULL) cache->Add(name, function_info);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001280 }
1281
1282 // Setup the function context. Conceptually, we should clone the
1283 // function before overwriting the context but since we're in a
1284 // single-threaded environment it is not strictly necessary.
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001285 ASSERT(top_context->IsGlobalContext());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001286 Handle<Context> context =
1287 Handle<Context>(use_runtime_context
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001288 ? Handle<Context>(top_context->runtime_context())
1289 : top_context);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001290 Handle<JSFunction> fun =
lrn@chromium.org7516f052011-03-30 08:52:27 +00001291 factory->NewFunctionFromSharedFunctionInfo(function_info, context);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001292
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001293 // Call function using either the runtime object or the global
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001294 // object as the receiver. Provide no parameters.
1295 Handle<Object> receiver =
1296 Handle<Object>(use_runtime_context
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001297 ? top_context->builtins()
1298 : top_context->global());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001299 bool has_pending_exception;
lrn@chromium.org1c092762011-05-09 09:42:16 +00001300 Execution::Call(fun, receiver, 0, NULL, &has_pending_exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001301 if (has_pending_exception) return false;
ager@chromium.org5c838252010-02-19 08:53:10 +00001302 return true;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001303}
1304
1305
danno@chromium.org160a7b02011-04-18 15:51:38 +00001306#define INSTALL_NATIVE(Type, name, var) \
1307 Handle<String> var##_name = factory()->LookupAsciiSymbol(name); \
1308 Object* var##_native = \
1309 global_context()->builtins()->GetPropertyNoExceptionThrown( \
1310 *var##_name); \
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001311 global_context()->set_##var(Type::cast(var##_native));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001312
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001313
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001314void Genesis::InstallNativeFunctions() {
1315 HandleScope scope;
1316 INSTALL_NATIVE(JSFunction, "CreateDate", create_date_fun);
1317 INSTALL_NATIVE(JSFunction, "ToNumber", to_number_fun);
1318 INSTALL_NATIVE(JSFunction, "ToString", to_string_fun);
1319 INSTALL_NATIVE(JSFunction, "ToDetailString", to_detail_string_fun);
1320 INSTALL_NATIVE(JSFunction, "ToObject", to_object_fun);
1321 INSTALL_NATIVE(JSFunction, "ToInteger", to_integer_fun);
1322 INSTALL_NATIVE(JSFunction, "ToUint32", to_uint32_fun);
1323 INSTALL_NATIVE(JSFunction, "ToInt32", to_int32_fun);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00001324 INSTALL_NATIVE(JSFunction, "GlobalEval", global_eval_fun);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001325 INSTALL_NATIVE(JSFunction, "Instantiate", instantiate_fun);
1326 INSTALL_NATIVE(JSFunction, "ConfigureTemplateInstance",
1327 configure_instance_fun);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001328 INSTALL_NATIVE(JSFunction, "GetStackTraceLine", get_stack_trace_line_fun);
1329 INSTALL_NATIVE(JSObject, "functionCache", function_cache);
1330}
1331
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001332void Genesis::InstallExperimentalNativeFunctions() {
1333 if (FLAG_harmony_proxies) {
rossberg@chromium.org717967f2011-07-20 13:44:42 +00001334 INSTALL_NATIVE(JSFunction, "DerivedHasTrap", derived_has_trap);
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001335 INSTALL_NATIVE(JSFunction, "DerivedGetTrap", derived_get_trap);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00001336 INSTALL_NATIVE(JSFunction, "DerivedSetTrap", derived_set_trap);
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001337 }
1338}
1339
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001340#undef INSTALL_NATIVE
1341
1342
1343bool Genesis::InstallNatives() {
1344 HandleScope scope;
1345
1346 // Create a function for the builtins object. Allocate space for the
1347 // JavaScript builtins, a reference to the builtins object
1348 // (itself) and a reference to the global_context directly in the object.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001349 Handle<Code> code = Handle<Code>(
danno@chromium.org160a7b02011-04-18 15:51:38 +00001350 isolate()->builtins()->builtin(Builtins::kIllegal));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001351 Handle<JSFunction> builtins_fun =
danno@chromium.org160a7b02011-04-18 15:51:38 +00001352 factory()->NewFunction(factory()->empty_symbol(),
1353 JS_BUILTINS_OBJECT_TYPE,
1354 JSBuiltinsObject::kSize, code, true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001355
danno@chromium.org160a7b02011-04-18 15:51:38 +00001356 Handle<String> name = factory()->LookupAsciiSymbol("builtins");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001357 builtins_fun->shared()->set_instance_class_name(*name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001358
1359 // Allocate the builtins object.
1360 Handle<JSBuiltinsObject> builtins =
danno@chromium.org160a7b02011-04-18 15:51:38 +00001361 Handle<JSBuiltinsObject>::cast(factory()->NewGlobalObject(builtins_fun));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001362 builtins->set_builtins(*builtins);
1363 builtins->set_global_context(*global_context());
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001364 builtins->set_global_receiver(*builtins);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001365
1366 // Setup the 'global' properties of the builtins object. The
1367 // 'global' property that refers to the global object is the only
1368 // way to get from code running in the builtins context to the
1369 // global object.
1370 static const PropertyAttributes attributes =
1371 static_cast<PropertyAttributes>(READ_ONLY | DONT_DELETE);
danno@chromium.org160a7b02011-04-18 15:51:38 +00001372 Handle<String> global_symbol = factory()->LookupAsciiSymbol("global");
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +00001373 Handle<Object> global_obj(global_context()->global());
1374 SetLocalPropertyNoThrow(builtins, global_symbol, global_obj, attributes);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001375
1376 // Setup the reference from the global object to the builtins object.
1377 JSGlobalObject::cast(global_context()->global())->set_builtins(*builtins);
1378
1379 // Create a bridge function that has context in the global context.
1380 Handle<JSFunction> bridge =
danno@chromium.org160a7b02011-04-18 15:51:38 +00001381 factory()->NewFunction(factory()->empty_symbol(),
1382 factory()->undefined_value());
1383 ASSERT(bridge->context() == *isolate()->global_context());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001384
1385 // Allocate the builtins context.
1386 Handle<Context> context =
danno@chromium.org160a7b02011-04-18 15:51:38 +00001387 factory()->NewFunctionContext(Context::MIN_CONTEXT_SLOTS, bridge);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001388 context->set_global(*builtins); // override builtins global object
1389
1390 global_context()->set_runtime_context(*context);
1391
1392 { // -- S c r i p t
1393 // Builtin functions for Script.
1394 Handle<JSFunction> script_fun =
1395 InstallFunction(builtins, "Script", JS_VALUE_TYPE, JSValue::kSize,
danno@chromium.org160a7b02011-04-18 15:51:38 +00001396 isolate()->initial_object_prototype(),
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00001397 Builtins::kIllegal, false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001398 Handle<JSObject> prototype =
danno@chromium.org160a7b02011-04-18 15:51:38 +00001399 factory()->NewJSObject(isolate()->object_function(), TENURED);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001400 SetPrototype(script_fun, prototype);
1401 global_context()->set_script_function(*script_fun);
1402
1403 // Add 'source' and 'data' property to scripts.
1404 PropertyAttributes common_attributes =
1405 static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE | READ_ONLY);
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001406 Handle<Foreign> foreign_source =
1407 factory()->NewForeign(&Accessors::ScriptSource);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001408 Handle<DescriptorArray> script_descriptors =
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001409 factory()->CopyAppendForeignDescriptor(
danno@chromium.org160a7b02011-04-18 15:51:38 +00001410 factory()->empty_descriptor_array(),
1411 factory()->LookupAsciiSymbol("source"),
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001412 foreign_source,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001413 common_attributes);
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001414 Handle<Foreign> foreign_name =
1415 factory()->NewForeign(&Accessors::ScriptName);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001416 script_descriptors =
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001417 factory()->CopyAppendForeignDescriptor(
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001418 script_descriptors,
danno@chromium.org160a7b02011-04-18 15:51:38 +00001419 factory()->LookupAsciiSymbol("name"),
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001420 foreign_name,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001421 common_attributes);
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001422 Handle<Foreign> foreign_id = factory()->NewForeign(&Accessors::ScriptId);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001423 script_descriptors =
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001424 factory()->CopyAppendForeignDescriptor(
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001425 script_descriptors,
danno@chromium.org160a7b02011-04-18 15:51:38 +00001426 factory()->LookupAsciiSymbol("id"),
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001427 foreign_id,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001428 common_attributes);
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001429 Handle<Foreign> foreign_line_offset =
1430 factory()->NewForeign(&Accessors::ScriptLineOffset);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001431 script_descriptors =
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001432 factory()->CopyAppendForeignDescriptor(
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001433 script_descriptors,
danno@chromium.org160a7b02011-04-18 15:51:38 +00001434 factory()->LookupAsciiSymbol("line_offset"),
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001435 foreign_line_offset,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001436 common_attributes);
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001437 Handle<Foreign> foreign_column_offset =
1438 factory()->NewForeign(&Accessors::ScriptColumnOffset);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001439 script_descriptors =
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001440 factory()->CopyAppendForeignDescriptor(
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001441 script_descriptors,
danno@chromium.org160a7b02011-04-18 15:51:38 +00001442 factory()->LookupAsciiSymbol("column_offset"),
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001443 foreign_column_offset,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001444 common_attributes);
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001445 Handle<Foreign> foreign_data =
1446 factory()->NewForeign(&Accessors::ScriptData);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001447 script_descriptors =
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001448 factory()->CopyAppendForeignDescriptor(
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001449 script_descriptors,
danno@chromium.org160a7b02011-04-18 15:51:38 +00001450 factory()->LookupAsciiSymbol("data"),
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001451 foreign_data,
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001452 common_attributes);
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001453 Handle<Foreign> foreign_type =
1454 factory()->NewForeign(&Accessors::ScriptType);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001455 script_descriptors =
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001456 factory()->CopyAppendForeignDescriptor(
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001457 script_descriptors,
danno@chromium.org160a7b02011-04-18 15:51:38 +00001458 factory()->LookupAsciiSymbol("type"),
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001459 foreign_type,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001460 common_attributes);
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001461 Handle<Foreign> foreign_compilation_type =
1462 factory()->NewForeign(&Accessors::ScriptCompilationType);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001463 script_descriptors =
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001464 factory()->CopyAppendForeignDescriptor(
ager@chromium.orge2902be2009-06-08 12:21:35 +00001465 script_descriptors,
danno@chromium.org160a7b02011-04-18 15:51:38 +00001466 factory()->LookupAsciiSymbol("compilation_type"),
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001467 foreign_compilation_type,
ager@chromium.orge2902be2009-06-08 12:21:35 +00001468 common_attributes);
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001469 Handle<Foreign> foreign_line_ends =
1470 factory()->NewForeign(&Accessors::ScriptLineEnds);
iposva@chromium.org245aa852009-02-10 00:49:54 +00001471 script_descriptors =
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001472 factory()->CopyAppendForeignDescriptor(
iposva@chromium.org245aa852009-02-10 00:49:54 +00001473 script_descriptors,
danno@chromium.org160a7b02011-04-18 15:51:38 +00001474 factory()->LookupAsciiSymbol("line_ends"),
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001475 foreign_line_ends,
iposva@chromium.org245aa852009-02-10 00:49:54 +00001476 common_attributes);
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001477 Handle<Foreign> foreign_context_data =
1478 factory()->NewForeign(&Accessors::ScriptContextData);
ager@chromium.org9085a012009-05-11 19:22:57 +00001479 script_descriptors =
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001480 factory()->CopyAppendForeignDescriptor(
ager@chromium.org9085a012009-05-11 19:22:57 +00001481 script_descriptors,
danno@chromium.org160a7b02011-04-18 15:51:38 +00001482 factory()->LookupAsciiSymbol("context_data"),
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001483 foreign_context_data,
ager@chromium.org9085a012009-05-11 19:22:57 +00001484 common_attributes);
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001485 Handle<Foreign> foreign_eval_from_script =
1486 factory()->NewForeign(&Accessors::ScriptEvalFromScript);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001487 script_descriptors =
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001488 factory()->CopyAppendForeignDescriptor(
ager@chromium.orge2902be2009-06-08 12:21:35 +00001489 script_descriptors,
danno@chromium.org160a7b02011-04-18 15:51:38 +00001490 factory()->LookupAsciiSymbol("eval_from_script"),
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001491 foreign_eval_from_script,
ager@chromium.orge2902be2009-06-08 12:21:35 +00001492 common_attributes);
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001493 Handle<Foreign> foreign_eval_from_script_position =
1494 factory()->NewForeign(&Accessors::ScriptEvalFromScriptPosition);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001495 script_descriptors =
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001496 factory()->CopyAppendForeignDescriptor(
ager@chromium.orge2902be2009-06-08 12:21:35 +00001497 script_descriptors,
danno@chromium.org160a7b02011-04-18 15:51:38 +00001498 factory()->LookupAsciiSymbol("eval_from_script_position"),
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001499 foreign_eval_from_script_position,
sgjesse@chromium.org98180592009-12-02 08:17:28 +00001500 common_attributes);
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001501 Handle<Foreign> foreign_eval_from_function_name =
1502 factory()->NewForeign(&Accessors::ScriptEvalFromFunctionName);
sgjesse@chromium.org98180592009-12-02 08:17:28 +00001503 script_descriptors =
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001504 factory()->CopyAppendForeignDescriptor(
sgjesse@chromium.org98180592009-12-02 08:17:28 +00001505 script_descriptors,
danno@chromium.org160a7b02011-04-18 15:51:38 +00001506 factory()->LookupAsciiSymbol("eval_from_function_name"),
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001507 foreign_eval_from_function_name,
ager@chromium.orge2902be2009-06-08 12:21:35 +00001508 common_attributes);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001509
1510 Handle<Map> script_map = Handle<Map>(script_fun->initial_map());
1511 script_map->set_instance_descriptors(*script_descriptors);
1512
1513 // Allocate the empty script.
danno@chromium.org160a7b02011-04-18 15:51:38 +00001514 Handle<Script> script = factory()->NewScript(factory()->empty_string());
ager@chromium.orge2902be2009-06-08 12:21:35 +00001515 script->set_type(Smi::FromInt(Script::TYPE_NATIVE));
danno@chromium.org160a7b02011-04-18 15:51:38 +00001516 heap()->public_set_empty_script(*script);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001517 }
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001518 {
1519 // Builtin function for OpaqueReference -- a JSValue-based object,
1520 // that keeps its field isolated from JavaScript code. It may store
1521 // objects, that JavaScript code may not access.
1522 Handle<JSFunction> opaque_reference_fun =
1523 InstallFunction(builtins, "OpaqueReference", JS_VALUE_TYPE,
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001524 JSValue::kSize,
danno@chromium.org160a7b02011-04-18 15:51:38 +00001525 isolate()->initial_object_prototype(),
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00001526 Builtins::kIllegal, false);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001527 Handle<JSObject> prototype =
danno@chromium.org160a7b02011-04-18 15:51:38 +00001528 factory()->NewJSObject(isolate()->object_function(), TENURED);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001529 SetPrototype(opaque_reference_fun, prototype);
1530 global_context()->set_opaque_reference_function(*opaque_reference_fun);
1531 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001532
karlklose@chromium.org8f806e82011-03-07 14:06:08 +00001533 { // --- I n t e r n a l A r r a y ---
1534 // An array constructor on the builtins object that works like
1535 // the public Array constructor, except that its prototype
1536 // doesn't inherit from Object.prototype.
1537 // To be used only for internal work by builtins. Instances
1538 // must not be leaked to user code.
1539 // Only works correctly when called as a constructor. The normal
1540 // Array code uses Array.prototype as prototype when called as
1541 // a function.
1542 Handle<JSFunction> array_function =
1543 InstallFunction(builtins,
1544 "InternalArray",
1545 JS_ARRAY_TYPE,
1546 JSArray::kSize,
danno@chromium.org160a7b02011-04-18 15:51:38 +00001547 isolate()->initial_object_prototype(),
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00001548 Builtins::kArrayCode,
karlklose@chromium.org8f806e82011-03-07 14:06:08 +00001549 true);
1550 Handle<JSObject> prototype =
danno@chromium.org160a7b02011-04-18 15:51:38 +00001551 factory()->NewJSObject(isolate()->object_function(), TENURED);
karlklose@chromium.org8f806e82011-03-07 14:06:08 +00001552 SetPrototype(array_function, prototype);
1553
1554 array_function->shared()->set_construct_stub(
danno@chromium.org160a7b02011-04-18 15:51:38 +00001555 isolate()->builtins()->builtin(Builtins::kArrayConstructCode));
karlklose@chromium.org8f806e82011-03-07 14:06:08 +00001556 array_function->shared()->DontAdaptArguments();
1557
1558 // Make "length" magic on instances.
1559 Handle<DescriptorArray> array_descriptors =
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001560 factory()->CopyAppendForeignDescriptor(
danno@chromium.org160a7b02011-04-18 15:51:38 +00001561 factory()->empty_descriptor_array(),
1562 factory()->length_symbol(),
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001563 factory()->NewForeign(&Accessors::ArrayLength),
karlklose@chromium.org8f806e82011-03-07 14:06:08 +00001564 static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE));
1565
1566 array_function->initial_map()->set_instance_descriptors(
1567 *array_descriptors);
1568 }
1569
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001570 if (FLAG_disable_native_files) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001571 PrintF("Warning: Running without installed natives!\n");
1572 return true;
1573 }
1574
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001575 // Install natives.
1576 for (int i = Natives::GetDebuggerCount();
1577 i < Natives::GetBuiltinsCount();
1578 i++) {
danno@chromium.org160a7b02011-04-18 15:51:38 +00001579 if (!CompileBuiltin(isolate(), i)) return false;
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001580 // TODO(ager): We really only need to install the JS builtin
1581 // functions on the builtins object after compiling and running
1582 // runtime.js.
1583 if (!InstallJSBuiltins(builtins)) return false;
1584 }
1585
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001586 InstallNativeFunctions();
1587
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00001588 // Store the map for the string prototype after the natives has been compiled
1589 // and the String function has been setup.
1590 Handle<JSFunction> string_function(global_context()->string_function());
1591 ASSERT(JSObject::cast(
1592 string_function->initial_map()->prototype())->HasFastProperties());
1593 global_context()->set_string_function_prototype_map(
1594 HeapObject::cast(string_function->initial_map()->prototype())->map());
1595
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001596 // Install Function.prototype.call and apply.
danno@chromium.org160a7b02011-04-18 15:51:38 +00001597 { Handle<String> key = factory()->function_class_symbol();
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001598 Handle<JSFunction> function =
danno@chromium.org160a7b02011-04-18 15:51:38 +00001599 Handle<JSFunction>::cast(GetProperty(isolate()->global(), key));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001600 Handle<JSObject> proto =
1601 Handle<JSObject>(JSObject::cast(function->instance_prototype()));
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001602
1603 // Install the call and the apply functions.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001604 Handle<JSFunction> call =
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001605 InstallFunction(proto, "call", JS_OBJECT_TYPE, JSObject::kHeaderSize,
kmillikin@chromium.org4111b802010-05-03 10:34:42 +00001606 Handle<JSObject>::null(),
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00001607 Builtins::kFunctionCall,
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001608 false);
1609 Handle<JSFunction> apply =
1610 InstallFunction(proto, "apply", JS_OBJECT_TYPE, JSObject::kHeaderSize,
kmillikin@chromium.org4111b802010-05-03 10:34:42 +00001611 Handle<JSObject>::null(),
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00001612 Builtins::kFunctionApply,
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001613 false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001614
1615 // Make sure that Function.prototype.call appears to be compiled.
1616 // The code will never be called, but inline caching for call will
1617 // only work if it appears to be compiled.
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001618 call->shared()->DontAdaptArguments();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001619 ASSERT(call->is_compiled());
1620
ager@chromium.org32912102009-01-16 10:38:43 +00001621 // Set the expected parameters for apply to 2; required by builtin.
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001622 apply->shared()->set_formal_parameter_count(2);
1623
1624 // Set the lengths for the functions to satisfy ECMA-262.
1625 call->shared()->set_length(1);
1626 apply->shared()->set_length(2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001627 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001628
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001629 InstallBuiltinFunctionIds();
1630
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00001631 // Create a constructor for RegExp results (a variant of Array that
1632 // predefines the two properties index and match).
1633 {
1634 // RegExpResult initial map.
1635
1636 // Find global.Array.prototype to inherit from.
1637 Handle<JSFunction> array_constructor(global_context()->array_function());
1638 Handle<JSObject> array_prototype(
1639 JSObject::cast(array_constructor->instance_prototype()));
1640
1641 // Add initial map.
1642 Handle<Map> initial_map =
danno@chromium.org160a7b02011-04-18 15:51:38 +00001643 factory()->NewMap(JS_ARRAY_TYPE, JSRegExpResult::kSize);
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00001644 initial_map->set_constructor(*array_constructor);
1645
1646 // Set prototype on map.
1647 initial_map->set_non_instance_prototype(false);
1648 initial_map->set_prototype(*array_prototype);
1649
1650 // Update map with length accessor from Array and add "index" and "input".
1651 Handle<Map> array_map(global_context()->js_array_map());
1652 Handle<DescriptorArray> array_descriptors(
1653 array_map->instance_descriptors());
1654 ASSERT_EQ(1, array_descriptors->number_of_descriptors());
1655
1656 Handle<DescriptorArray> reresult_descriptors =
danno@chromium.org160a7b02011-04-18 15:51:38 +00001657 factory()->NewDescriptorArray(3);
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00001658
1659 reresult_descriptors->CopyFrom(0, *array_descriptors, 0);
1660
1661 int enum_index = 0;
1662 {
danno@chromium.org160a7b02011-04-18 15:51:38 +00001663 FieldDescriptor index_field(heap()->index_symbol(),
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00001664 JSRegExpResult::kIndexIndex,
1665 NONE,
1666 enum_index++);
1667 reresult_descriptors->Set(1, &index_field);
1668 }
1669
1670 {
danno@chromium.org160a7b02011-04-18 15:51:38 +00001671 FieldDescriptor input_field(heap()->input_symbol(),
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00001672 JSRegExpResult::kInputIndex,
1673 NONE,
1674 enum_index++);
1675 reresult_descriptors->Set(2, &input_field);
1676 }
1677 reresult_descriptors->Sort();
1678
1679 initial_map->set_inobject_properties(2);
1680 initial_map->set_pre_allocated_property_fields(2);
1681 initial_map->set_unused_property_fields(0);
1682 initial_map->set_instance_descriptors(*reresult_descriptors);
1683
1684 global_context()->set_regexp_result_map(*initial_map);
1685 }
1686
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001687#ifdef DEBUG
1688 builtins->Verify();
1689#endif
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001690
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001691 return true;
1692}
1693
1694
danno@chromium.org160a7b02011-04-18 15:51:38 +00001695bool Genesis::InstallExperimentalNatives() {
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001696 for (int i = ExperimentalNatives::GetDebuggerCount();
1697 i < ExperimentalNatives::GetBuiltinsCount();
1698 i++) {
1699 if (FLAG_harmony_proxies &&
1700 strcmp(ExperimentalNatives::GetScriptName(i).start(),
1701 "native proxy.js") == 0) {
danno@chromium.org160a7b02011-04-18 15:51:38 +00001702 if (!CompileExperimentalBuiltin(isolate(), i)) return false;
1703 }
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +00001704 if (FLAG_harmony_weakmaps &&
1705 strcmp(ExperimentalNatives::GetScriptName(i).start(),
1706 "native weakmap.js") == 0) {
1707 if (!CompileExperimentalBuiltin(isolate(), i)) return false;
1708 }
danno@chromium.org160a7b02011-04-18 15:51:38 +00001709 }
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001710
1711 InstallExperimentalNativeFunctions();
1712
danno@chromium.org160a7b02011-04-18 15:51:38 +00001713 return true;
1714}
1715
1716
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001717static Handle<JSObject> ResolveBuiltinIdHolder(
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001718 Handle<Context> global_context,
1719 const char* holder_expr) {
danno@chromium.org160a7b02011-04-18 15:51:38 +00001720 Factory* factory = global_context->GetIsolate()->factory();
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001721 Handle<GlobalObject> global(global_context->global());
1722 const char* period_pos = strchr(holder_expr, '.');
1723 if (period_pos == NULL) {
1724 return Handle<JSObject>::cast(
lrn@chromium.org7516f052011-03-30 08:52:27 +00001725 GetProperty(global, factory->LookupAsciiSymbol(holder_expr)));
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00001726 }
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001727 ASSERT_EQ(".prototype", period_pos);
1728 Vector<const char> property(holder_expr,
1729 static_cast<int>(period_pos - holder_expr));
1730 Handle<JSFunction> function = Handle<JSFunction>::cast(
lrn@chromium.org7516f052011-03-30 08:52:27 +00001731 GetProperty(global, factory->LookupSymbol(property)));
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001732 return Handle<JSObject>(JSObject::cast(function->prototype()));
1733}
1734
1735
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001736static void InstallBuiltinFunctionId(Handle<JSObject> holder,
1737 const char* function_name,
1738 BuiltinFunctionId id) {
danno@chromium.org160a7b02011-04-18 15:51:38 +00001739 Factory* factory = holder->GetIsolate()->factory();
1740 Handle<String> name = factory->LookupAsciiSymbol(function_name);
lrn@chromium.org303ada72010-10-27 09:33:13 +00001741 Object* function_object = holder->GetProperty(*name)->ToObjectUnchecked();
1742 Handle<JSFunction> function(JSFunction::cast(function_object));
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001743 function->shared()->set_function_data(Smi::FromInt(id));
1744}
1745
1746
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001747void Genesis::InstallBuiltinFunctionIds() {
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001748 HandleScope scope;
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001749#define INSTALL_BUILTIN_ID(holder_expr, fun_name, name) \
1750 { \
1751 Handle<JSObject> holder = ResolveBuiltinIdHolder( \
1752 global_context(), #holder_expr); \
1753 BuiltinFunctionId id = k##name; \
1754 InstallBuiltinFunctionId(holder, #fun_name, id); \
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001755 }
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001756 FUNCTIONS_WITH_ID_LIST(INSTALL_BUILTIN_ID)
1757#undef INSTALL_BUILTIN_ID
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001758}
1759
1760
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00001761// Do not forget to update macros.py with named constant
1762// of cache id.
1763#define JSFUNCTION_RESULT_CACHE_LIST(F) \
1764 F(16, global_context()->regexp_function())
1765
1766
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001767static FixedArray* CreateCache(int size, Handle<JSFunction> factory_function) {
danno@chromium.org160a7b02011-04-18 15:51:38 +00001768 Factory* factory = factory_function->GetIsolate()->factory();
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00001769 // Caches are supposed to live for a long time, allocate in old space.
1770 int array_size = JSFunctionResultCache::kEntriesIndex + 2 * size;
ager@chromium.orgac091b72010-05-05 07:34:42 +00001771 // Cannot use cast as object is not fully initialized yet.
1772 JSFunctionResultCache* cache = reinterpret_cast<JSFunctionResultCache*>(
danno@chromium.org160a7b02011-04-18 15:51:38 +00001773 *factory->NewFixedArrayWithHoles(array_size, TENURED));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001774 cache->set(JSFunctionResultCache::kFactoryIndex, *factory_function);
ager@chromium.orgac091b72010-05-05 07:34:42 +00001775 cache->MakeZeroSize();
1776 return cache;
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00001777}
1778
1779
1780void Genesis::InstallJSFunctionResultCaches() {
1781 const int kNumberOfCaches = 0 +
1782#define F(size, func) + 1
1783 JSFUNCTION_RESULT_CACHE_LIST(F)
1784#undef F
1785 ;
1786
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001787 Handle<FixedArray> caches = FACTORY->NewFixedArray(kNumberOfCaches, TENURED);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00001788
1789 int index = 0;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001790
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001791#define F(size, func) do { \
1792 FixedArray* cache = CreateCache((size), Handle<JSFunction>(func)); \
1793 caches->set(index++, cache); \
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001794 } while (false)
1795
1796 JSFUNCTION_RESULT_CACHE_LIST(F);
1797
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00001798#undef F
1799
1800 global_context()->set_jsfunction_result_caches(*caches);
1801}
1802
1803
ricow@chromium.org65fae842010-08-25 15:26:24 +00001804void Genesis::InitializeNormalizedMapCaches() {
1805 Handle<FixedArray> array(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001806 FACTORY->NewFixedArray(NormalizedMapCache::kEntries, TENURED));
ricow@chromium.org65fae842010-08-25 15:26:24 +00001807 global_context()->set_normalized_map_cache(NormalizedMapCache::cast(*array));
1808}
1809
1810
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001811bool Bootstrapper::InstallExtensions(Handle<Context> global_context,
1812 v8::ExtensionConfiguration* extensions) {
danno@chromium.org160a7b02011-04-18 15:51:38 +00001813 Isolate* isolate = global_context->GetIsolate();
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001814 BootstrapperActive active;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001815 SaveContext saved_context(isolate);
1816 isolate->set_context(*global_context);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001817 if (!Genesis::InstallExtensions(global_context, extensions)) return false;
1818 Genesis::InstallSpecialObjects(global_context);
1819 return true;
1820}
1821
1822
1823void Genesis::InstallSpecialObjects(Handle<Context> global_context) {
danno@chromium.org160a7b02011-04-18 15:51:38 +00001824 Factory* factory = global_context->GetIsolate()->factory();
mads.s.agercbaa0602008-08-14 13:41:48 +00001825 HandleScope scope;
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001826 Handle<JSGlobalObject> js_global(
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001827 JSGlobalObject::cast(global_context->global()));
mads.s.agercbaa0602008-08-14 13:41:48 +00001828 // Expose the natives in global if a name for it is specified.
1829 if (FLAG_expose_natives_as != NULL && strlen(FLAG_expose_natives_as) != 0) {
1830 Handle<String> natives_string =
lrn@chromium.org7516f052011-03-30 08:52:27 +00001831 factory->LookupAsciiSymbol(FLAG_expose_natives_as);
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +00001832 SetLocalPropertyNoThrow(js_global, natives_string,
1833 Handle<JSObject>(js_global->builtins()), DONT_ENUM);
mads.s.agercbaa0602008-08-14 13:41:48 +00001834 }
1835
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00001836 Handle<Object> Error = GetProperty(js_global, "Error");
1837 if (Error->IsJSObject()) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00001838 Handle<String> name = factory->LookupAsciiSymbol("stackTraceLimit");
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +00001839 SetLocalPropertyNoThrow(Handle<JSObject>::cast(Error),
1840 name,
1841 Handle<Smi>(Smi::FromInt(FLAG_stack_trace_limit)),
1842 NONE);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001843 }
1844
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001845#ifdef ENABLE_DEBUGGER_SUPPORT
mads.s.agercbaa0602008-08-14 13:41:48 +00001846 // Expose the debug global object in global if a name for it is specified.
1847 if (FLAG_expose_debug_as != NULL && strlen(FLAG_expose_debug_as) != 0) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001848 Debug* debug = Isolate::Current()->debug();
mads.s.agercbaa0602008-08-14 13:41:48 +00001849 // If loading fails we just bail out without installing the
1850 // debugger but without tanking the whole context.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001851 if (!debug->Load()) return;
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001852 // Set the security token for the debugger context to the same as
1853 // the shell global context to allow calling between these (otherwise
1854 // exposing debug global object doesn't make much sense).
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001855 debug->debug_context()->set_security_token(
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001856 global_context->security_token());
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001857
mads.s.agercbaa0602008-08-14 13:41:48 +00001858 Handle<String> debug_string =
lrn@chromium.org7516f052011-03-30 08:52:27 +00001859 factory->LookupAsciiSymbol(FLAG_expose_debug_as);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001860 Handle<Object> global_proxy(debug->debug_context()->global_proxy());
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +00001861 SetLocalPropertyNoThrow(js_global, debug_string, global_proxy, DONT_ENUM);
mads.s.agercbaa0602008-08-14 13:41:48 +00001862 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001863#endif
mads.s.agercbaa0602008-08-14 13:41:48 +00001864}
1865
1866
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001867bool Genesis::InstallExtensions(Handle<Context> global_context,
1868 v8::ExtensionConfiguration* extensions) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001869 // TODO(isolates): Extensions on multiple isolates may take a little more
1870 // effort. (The external API reads 'ignore'-- does that mean
1871 // we can break the interface?)
1872
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001873 // Clear coloring of extension list
1874 v8::RegisteredExtension* current = v8::RegisteredExtension::first_extension();
1875 while (current != NULL) {
1876 current->set_state(v8::UNVISITED);
1877 current = current->next();
1878 }
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001879 // Install auto extensions.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001880 current = v8::RegisteredExtension::first_extension();
1881 while (current != NULL) {
1882 if (current->extension()->auto_enable())
1883 InstallExtension(current);
1884 current = current->next();
1885 }
1886
1887 if (FLAG_expose_gc) InstallExtension("v8/gc");
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00001888 if (FLAG_expose_externalize_string) InstallExtension("v8/externalize");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001889
1890 if (extensions == NULL) return true;
1891 // Install required extensions
1892 int count = v8::ImplementationUtilities::GetNameCount(extensions);
1893 const char** names = v8::ImplementationUtilities::GetNames(extensions);
1894 for (int i = 0; i < count; i++) {
1895 if (!InstallExtension(names[i]))
1896 return false;
1897 }
1898
1899 return true;
1900}
1901
1902
1903// Installs a named extension. This methods is unoptimized and does
1904// not scale well if we want to support a large number of extensions.
1905bool Genesis::InstallExtension(const char* name) {
1906 v8::RegisteredExtension* current = v8::RegisteredExtension::first_extension();
1907 // Loop until we find the relevant extension
1908 while (current != NULL) {
1909 if (strcmp(name, current->extension()->name()) == 0) break;
1910 current = current->next();
1911 }
1912 // Didn't find the extension; fail.
1913 if (current == NULL) {
1914 v8::Utils::ReportApiFailure(
1915 "v8::Context::New()", "Cannot find required extension");
1916 return false;
1917 }
1918 return InstallExtension(current);
1919}
1920
1921
1922bool Genesis::InstallExtension(v8::RegisteredExtension* current) {
1923 HandleScope scope;
1924
1925 if (current->state() == v8::INSTALLED) return true;
1926 // The current node has already been visited so there must be a
1927 // cycle in the dependency graph; fail.
1928 if (current->state() == v8::VISITED) {
1929 v8::Utils::ReportApiFailure(
1930 "v8::Context::New()", "Circular extension dependency");
1931 return false;
1932 }
1933 ASSERT(current->state() == v8::UNVISITED);
1934 current->set_state(v8::VISITED);
1935 v8::Extension* extension = current->extension();
1936 // Install the extension's dependencies
1937 for (int i = 0; i < extension->dependency_count(); i++) {
1938 if (!InstallExtension(extension->dependencies()[i])) return false;
1939 }
lrn@chromium.org7516f052011-03-30 08:52:27 +00001940 Isolate* isolate = Isolate::Current();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001941 Vector<const char> source = CStrVector(extension->source());
lrn@chromium.org7516f052011-03-30 08:52:27 +00001942 Handle<String> source_code = isolate->factory()->NewStringFromAscii(source);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001943 bool result = CompileScriptCached(CStrVector(extension->name()),
1944 source_code,
lrn@chromium.org7516f052011-03-30 08:52:27 +00001945 isolate->bootstrapper()->extensions_cache(),
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001946 extension,
lrn@chromium.org7516f052011-03-30 08:52:27 +00001947 Handle<Context>(isolate->context()),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001948 false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00001949 ASSERT(isolate->has_pending_exception() != result);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001950 if (!result) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00001951 isolate->clear_pending_exception();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001952 }
1953 current->set_state(v8::INSTALLED);
1954 return result;
1955}
1956
1957
ager@chromium.org5c838252010-02-19 08:53:10 +00001958bool Genesis::InstallJSBuiltins(Handle<JSBuiltinsObject> builtins) {
1959 HandleScope scope;
danno@chromium.org160a7b02011-04-18 15:51:38 +00001960 Factory* factory = builtins->GetIsolate()->factory();
ager@chromium.org5c838252010-02-19 08:53:10 +00001961 for (int i = 0; i < Builtins::NumberOfJavaScriptBuiltins(); i++) {
1962 Builtins::JavaScript id = static_cast<Builtins::JavaScript>(i);
danno@chromium.org160a7b02011-04-18 15:51:38 +00001963 Handle<String> name = factory->LookupAsciiSymbol(Builtins::GetName(id));
lrn@chromium.org303ada72010-10-27 09:33:13 +00001964 Object* function_object = builtins->GetPropertyNoExceptionThrown(*name);
ager@chromium.org5c838252010-02-19 08:53:10 +00001965 Handle<JSFunction> function
lrn@chromium.org303ada72010-10-27 09:33:13 +00001966 = Handle<JSFunction>(JSFunction::cast(function_object));
ager@chromium.org5c838252010-02-19 08:53:10 +00001967 builtins->set_javascript_builtin(id, *function);
1968 Handle<SharedFunctionInfo> shared
1969 = Handle<SharedFunctionInfo>(function->shared());
1970 if (!EnsureCompiled(shared, CLEAR_EXCEPTION)) return false;
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00001971 // Set the code object on the function object.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001972 function->ReplaceCode(function->shared()->code());
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00001973 builtins->set_javascript_builtin_code(id, shared->code());
ager@chromium.org5c838252010-02-19 08:53:10 +00001974 }
1975 return true;
1976}
1977
1978
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001979bool Genesis::ConfigureGlobalObjects(
1980 v8::Handle<v8::ObjectTemplate> global_proxy_template) {
1981 Handle<JSObject> global_proxy(
1982 JSObject::cast(global_context()->global_proxy()));
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001983 Handle<JSObject> inner_global(JSObject::cast(global_context()->global()));
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001984
1985 if (!global_proxy_template.IsEmpty()) {
1986 // Configure the global proxy object.
1987 Handle<ObjectTemplateInfo> proxy_data =
1988 v8::Utils::OpenHandle(*global_proxy_template);
1989 if (!ConfigureApiObject(global_proxy, proxy_data)) return false;
1990
1991 // Configure the inner global object.
1992 Handle<FunctionTemplateInfo> proxy_constructor(
1993 FunctionTemplateInfo::cast(proxy_data->constructor()));
1994 if (!proxy_constructor->prototype_template()->IsUndefined()) {
1995 Handle<ObjectTemplateInfo> inner_data(
1996 ObjectTemplateInfo::cast(proxy_constructor->prototype_template()));
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001997 if (!ConfigureApiObject(inner_global, inner_data)) return false;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001998 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001999 }
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002000
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002001 SetObjectPrototype(global_proxy, inner_global);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002002 return true;
2003}
2004
2005
2006bool Genesis::ConfigureApiObject(Handle<JSObject> object,
2007 Handle<ObjectTemplateInfo> object_template) {
2008 ASSERT(!object_template.is_null());
2009 ASSERT(object->IsInstanceOf(
2010 FunctionTemplateInfo::cast(object_template->constructor())));
2011
2012 bool pending_exception = false;
2013 Handle<JSObject> obj =
2014 Execution::InstantiateObject(object_template, &pending_exception);
2015 if (pending_exception) {
danno@chromium.org160a7b02011-04-18 15:51:38 +00002016 ASSERT(isolate()->has_pending_exception());
2017 isolate()->clear_pending_exception();
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002018 return false;
2019 }
2020 TransferObject(obj, object);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002021 return true;
2022}
2023
2024
2025void Genesis::TransferNamedProperties(Handle<JSObject> from,
2026 Handle<JSObject> to) {
2027 if (from->HasFastProperties()) {
2028 Handle<DescriptorArray> descs =
2029 Handle<DescriptorArray>(from->map()->instance_descriptors());
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002030 for (int i = 0; i < descs->number_of_descriptors(); i++) {
2031 PropertyDetails details = PropertyDetails(descs->GetDetails(i));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002032 switch (details.type()) {
2033 case FIELD: {
2034 HandleScope inner;
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002035 Handle<String> key = Handle<String>(descs->GetKey(i));
2036 int index = descs->GetFieldIndex(i);
ager@chromium.org7c537e22008-10-16 08:43:32 +00002037 Handle<Object> value = Handle<Object>(from->FastPropertyAt(index));
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +00002038 SetLocalPropertyNoThrow(to, key, value, details.attributes());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002039 break;
2040 }
2041 case CONSTANT_FUNCTION: {
2042 HandleScope inner;
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002043 Handle<String> key = Handle<String>(descs->GetKey(i));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002044 Handle<JSFunction> fun =
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002045 Handle<JSFunction>(descs->GetConstantFunction(i));
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +00002046 SetLocalPropertyNoThrow(to, key, fun, details.attributes());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002047 break;
2048 }
2049 case CALLBACKS: {
2050 LookupResult result;
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002051 to->LocalLookup(descs->GetKey(i), &result);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002052 // If the property is already there we skip it
ager@chromium.org5c838252010-02-19 08:53:10 +00002053 if (result.IsProperty()) continue;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002054 HandleScope inner;
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002055 ASSERT(!to->HasFastProperties());
2056 // Add to dictionary.
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002057 Handle<String> key = Handle<String>(descs->GetKey(i));
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002058 Handle<Object> callbacks(descs->GetCallbacksObject(i));
2059 PropertyDetails d =
2060 PropertyDetails(details.attributes(), CALLBACKS, details.index());
2061 SetNormalizedProperty(to, key, callbacks, d);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002062 break;
2063 }
2064 case MAP_TRANSITION:
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00002065 case EXTERNAL_ARRAY_TRANSITION:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002066 case CONSTANT_TRANSITION:
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002067 case NULL_DESCRIPTOR:
2068 // Ignore non-properties.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002069 break;
2070 case NORMAL:
2071 // Do not occur since the from object has fast properties.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002072 case HANDLER:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002073 case INTERCEPTOR:
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002074 // No element in instance descriptors have proxy or interceptor type.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002075 UNREACHABLE();
2076 break;
2077 }
2078 }
2079 } else {
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00002080 Handle<StringDictionary> properties =
2081 Handle<StringDictionary>(from->property_dictionary());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002082 int capacity = properties->Capacity();
2083 for (int i = 0; i < capacity; i++) {
2084 Object* raw_key(properties->KeyAt(i));
2085 if (properties->IsKey(raw_key)) {
2086 ASSERT(raw_key->IsString());
2087 // If the property is already there we skip it.
2088 LookupResult result;
2089 to->LocalLookup(String::cast(raw_key), &result);
ager@chromium.org5c838252010-02-19 08:53:10 +00002090 if (result.IsProperty()) continue;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002091 // Set the property.
2092 Handle<String> key = Handle<String>(String::cast(raw_key));
2093 Handle<Object> value = Handle<Object>(properties->ValueAt(i));
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002094 if (value->IsJSGlobalPropertyCell()) {
2095 value = Handle<Object>(JSGlobalPropertyCell::cast(*value)->value());
2096 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002097 PropertyDetails details = properties->DetailsAt(i);
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +00002098 SetLocalPropertyNoThrow(to, key, value, details.attributes());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002099 }
2100 }
2101 }
2102}
2103
2104
2105void Genesis::TransferIndexedProperties(Handle<JSObject> from,
2106 Handle<JSObject> to) {
2107 // Cloning the elements array is sufficient.
2108 Handle<FixedArray> from_elements =
2109 Handle<FixedArray>(FixedArray::cast(from->elements()));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002110 Handle<FixedArray> to_elements = FACTORY->CopyFixedArray(from_elements);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002111 to->set_elements(*to_elements);
2112}
2113
2114
2115void Genesis::TransferObject(Handle<JSObject> from, Handle<JSObject> to) {
2116 HandleScope outer;
danno@chromium.org160a7b02011-04-18 15:51:38 +00002117 Factory* factory = from->GetIsolate()->factory();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002118
2119 ASSERT(!from->IsJSArray());
2120 ASSERT(!to->IsJSArray());
2121
2122 TransferNamedProperties(from, to);
2123 TransferIndexedProperties(from, to);
2124
2125 // Transfer the prototype (new map is needed).
2126 Handle<Map> old_to_map = Handle<Map>(to->map());
danno@chromium.org160a7b02011-04-18 15:51:38 +00002127 Handle<Map> new_to_map = factory->CopyMapDropTransitions(old_to_map);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002128 new_to_map->set_prototype(from->map()->prototype());
2129 to->set_map(*new_to_map);
2130}
2131
2132
2133void Genesis::MakeFunctionInstancePrototypeWritable() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002134 // The maps with writable prototype are created in CreateEmptyFunction
2135 // and CreateStrictModeFunctionMaps respectively. Initially the maps are
2136 // created with read-only prototype for JS builtins processing.
2137 ASSERT(!function_instance_map_writable_prototype_.is_null());
2138 ASSERT(!strict_mode_function_instance_map_writable_prototype_.is_null());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002139
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002140 // Replace function instance maps to make prototype writable.
2141 global_context()->set_function_map(
2142 *function_instance_map_writable_prototype_);
2143 global_context()->set_strict_mode_function_map(
2144 *strict_mode_function_instance_map_writable_prototype_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002145}
2146
2147
danno@chromium.org160a7b02011-04-18 15:51:38 +00002148Genesis::Genesis(Isolate* isolate,
2149 Handle<Object> global_object,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002150 v8::Handle<v8::ObjectTemplate> global_template,
danno@chromium.org160a7b02011-04-18 15:51:38 +00002151 v8::ExtensionConfiguration* extensions) : isolate_(isolate) {
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00002152 result_ = Handle<Context>::null();
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002153 // If V8 isn't running and cannot be initialized, just return.
2154 if (!V8::IsRunning() && !V8::Initialize(NULL)) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002155
2156 // Before creating the roots we must save the context and restore it
2157 // on all function exits.
2158 HandleScope scope;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002159 SaveContext saved_context(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002160
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002161 Handle<Context> new_context = Snapshot::NewContextFromSnapshot();
2162 if (!new_context.is_null()) {
2163 global_context_ =
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002164 Handle<Context>::cast(isolate->global_handles()->Create(*new_context));
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002165 AddToWeakGlobalContextList(*global_context_);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002166 isolate->set_context(*global_context_);
2167 isolate->counters()->contexts_created_by_snapshot()->Increment();
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002168 Handle<GlobalObject> inner_global;
2169 Handle<JSGlobalProxy> global_proxy =
2170 CreateNewGlobals(global_template,
2171 global_object,
2172 &inner_global);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002173
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002174 HookUpGlobalProxy(inner_global, global_proxy);
2175 HookUpInnerGlobal(inner_global);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002176
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002177 if (!ConfigureGlobalObjects(global_template)) return;
2178 } else {
2179 // We get here if there was no context snapshot.
2180 CreateRoots();
danno@chromium.org160a7b02011-04-18 15:51:38 +00002181 Handle<JSFunction> empty_function = CreateEmptyFunction(isolate);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002182 CreateStrictModeFunctionMaps(empty_function);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002183 Handle<GlobalObject> inner_global;
2184 Handle<JSGlobalProxy> global_proxy =
2185 CreateNewGlobals(global_template, global_object, &inner_global);
2186 HookUpGlobalProxy(inner_global, global_proxy);
2187 InitializeGlobal(inner_global, empty_function);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00002188 InstallJSFunctionResultCaches();
ricow@chromium.org65fae842010-08-25 15:26:24 +00002189 InitializeNormalizedMapCaches();
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00002190 if (!InstallNatives()) return;
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002191
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002192 MakeFunctionInstancePrototypeWritable();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002193
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002194 if (!ConfigureGlobalObjects(global_template)) return;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002195 isolate->counters()->contexts_created_from_scratch()->Increment();
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002196 }
mads.s.agercbaa0602008-08-14 13:41:48 +00002197
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +00002198 // Initialize experimental globals and install experimental natives.
2199 InitializeExperimentalGlobal();
danno@chromium.org160a7b02011-04-18 15:51:38 +00002200 if (!InstallExperimentalNatives()) return;
2201
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002202 result_ = global_context_;
2203}
2204
ager@chromium.orgddb913d2009-01-27 10:01:48 +00002205
2206// Support for thread preemption.
2207
2208// Reserve space for statics needing saving and restoring.
2209int Bootstrapper::ArchiveSpacePerThread() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002210 return sizeof(NestingCounterType);
ager@chromium.orgddb913d2009-01-27 10:01:48 +00002211}
2212
2213
2214// Archive statics that are thread local.
2215char* Bootstrapper::ArchiveState(char* to) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002216 *reinterpret_cast<NestingCounterType*>(to) = nesting_;
2217 nesting_ = 0;
2218 return to + sizeof(NestingCounterType);
ager@chromium.orgddb913d2009-01-27 10:01:48 +00002219}
2220
2221
2222// Restore statics that are thread local.
2223char* Bootstrapper::RestoreState(char* from) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002224 nesting_ = *reinterpret_cast<NestingCounterType*>(from);
2225 return from + sizeof(NestingCounterType);
ager@chromium.orgddb913d2009-01-27 10:01:48 +00002226}
2227
2228
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002229// Called when the top-level V8 mutex is destroyed.
2230void Bootstrapper::FreeThreadResources() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002231 ASSERT(!IsActive());
ager@chromium.orgddb913d2009-01-27 10:01:48 +00002232}
2233
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002234} } // namespace v8::internal