blob: a5cb55525274f6b2c7e68443bc830cce096d6c00 [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);
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +0000353 SetLocalPropertyNoThrow(target, symbol, function, DONT_ENUM);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000354 if (is_ecma_native) {
355 function->shared()->set_instance_class_name(*symbol);
356 }
357 return function;
358}
359
360
361Handle<DescriptorArray> Genesis::ComputeFunctionInstanceDescriptor(
kmillikin@chromium.org4111b802010-05-03 10:34:42 +0000362 PrototypePropertyMode prototypeMode) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000363 Handle<DescriptorArray> descriptors =
danno@chromium.org160a7b02011-04-18 15:51:38 +0000364 factory()->NewDescriptorArray(prototypeMode == DONT_ADD_PROTOTYPE
365 ? 4
366 : 5);
kmillikin@chromium.org4111b802010-05-03 10:34:42 +0000367 PropertyAttributes attributes =
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000368 static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE | READ_ONLY);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000369
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000370 { // Add length.
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000371 Handle<Foreign> foreign = factory()->NewForeign(&Accessors::FunctionLength);
372 CallbacksDescriptor d(*factory()->length_symbol(), *foreign, attributes);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000373 descriptors->Set(0, &d);
374 }
375 { // Add name.
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000376 Handle<Foreign> foreign = factory()->NewForeign(&Accessors::FunctionName);
377 CallbacksDescriptor d(*factory()->name_symbol(), *foreign, attributes);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000378 descriptors->Set(1, &d);
379 }
380 { // Add arguments.
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000381 Handle<Foreign> foreign =
382 factory()->NewForeign(&Accessors::FunctionArguments);
383 CallbacksDescriptor d(*factory()->arguments_symbol(), *foreign, attributes);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000384 descriptors->Set(2, &d);
385 }
386 { // Add caller.
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000387 Handle<Foreign> foreign = factory()->NewForeign(&Accessors::FunctionCaller);
388 CallbacksDescriptor d(*factory()->caller_symbol(), *foreign, attributes);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000389 descriptors->Set(3, &d);
390 }
391 if (prototypeMode != DONT_ADD_PROTOTYPE) {
392 // Add prototype.
393 if (prototypeMode == ADD_WRITEABLE_PROTOTYPE) {
394 attributes = static_cast<PropertyAttributes>(attributes & ~READ_ONLY);
395 }
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000396 Handle<Foreign> foreign =
397 factory()->NewForeign(&Accessors::FunctionPrototype);
398 CallbacksDescriptor d(*factory()->prototype_symbol(), *foreign, attributes);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000399 descriptors->Set(4, &d);
400 }
401 descriptors->Sort();
402 return descriptors;
403}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000404
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000405
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000406Handle<Map> Genesis::CreateFunctionMap(PrototypePropertyMode prototype_mode) {
danno@chromium.org160a7b02011-04-18 15:51:38 +0000407 Handle<Map> map = factory()->NewMap(JS_FUNCTION_TYPE, JSFunction::kSize);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000408 Handle<DescriptorArray> descriptors =
409 ComputeFunctionInstanceDescriptor(prototype_mode);
410 map->set_instance_descriptors(*descriptors);
411 map->set_function_with_prototype(prototype_mode != DONT_ADD_PROTOTYPE);
412 return map;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000413}
414
415
danno@chromium.org160a7b02011-04-18 15:51:38 +0000416Handle<JSFunction> Genesis::CreateEmptyFunction(Isolate* isolate) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000417 // Allocate the map for function instances. Maps are allocated first and their
418 // prototypes patched later, once empty function is created.
419
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000420 // Please note that the prototype property for function instances must be
421 // writable.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000422 Handle<Map> function_instance_map =
423 CreateFunctionMap(ADD_WRITEABLE_PROTOTYPE);
424 global_context()->set_function_instance_map(*function_instance_map);
kmillikin@chromium.org4111b802010-05-03 10:34:42 +0000425
426 // Functions with this map will not have a 'prototype' property, and
427 // can not be used as constructors.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000428 Handle<Map> function_without_prototype_map =
429 CreateFunctionMap(DONT_ADD_PROTOTYPE);
kmillikin@chromium.org4111b802010-05-03 10:34:42 +0000430 global_context()->set_function_without_prototype_map(
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000431 *function_without_prototype_map);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000432
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000433 // Allocate the function map. This map is temporary, used only for processing
434 // of builtins.
435 // Later the map is replaced with writable prototype map, allocated below.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000436 Handle<Map> function_map = CreateFunctionMap(ADD_READONLY_PROTOTYPE);
437 global_context()->set_function_map(*function_map);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000438
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000439 // The final map for functions. Writeable prototype.
440 // This map is installed in MakeFunctionInstancePrototypeWritable.
441 function_instance_map_writable_prototype_ =
442 CreateFunctionMap(ADD_WRITEABLE_PROTOTYPE);
443
lrn@chromium.org7516f052011-03-30 08:52:27 +0000444 Factory* factory = isolate->factory();
445 Heap* heap = isolate->heap();
446
447 Handle<String> object_name = Handle<String>(heap->Object_symbol());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000448
449 { // --- O b j e c t ---
450 Handle<JSFunction> object_fun =
lrn@chromium.org7516f052011-03-30 08:52:27 +0000451 factory->NewFunction(object_name, factory->null_value());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000452 Handle<Map> object_function_map =
lrn@chromium.org7516f052011-03-30 08:52:27 +0000453 factory->NewMap(JS_OBJECT_TYPE, JSObject::kHeaderSize);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000454 object_fun->set_initial_map(*object_function_map);
455 object_function_map->set_constructor(*object_fun);
456
457 global_context()->set_object_function(*object_fun);
458
459 // Allocate a new prototype for the object function.
lrn@chromium.org7516f052011-03-30 08:52:27 +0000460 Handle<JSObject> prototype = factory->NewJSObject(
461 isolate->object_function(),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000462 TENURED);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000463
464 global_context()->set_initial_object_prototype(*prototype);
465 SetPrototype(object_fun, prototype);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000466 object_function_map->
lrn@chromium.org7516f052011-03-30 08:52:27 +0000467 set_instance_descriptors(heap->empty_descriptor_array());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000468 }
469
470 // Allocate the empty function as the prototype for function ECMAScript
471 // 262 15.3.4.
lrn@chromium.org7516f052011-03-30 08:52:27 +0000472 Handle<String> symbol = factory->LookupAsciiSymbol("Empty");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000473 Handle<JSFunction> empty_function =
lrn@chromium.org7516f052011-03-30 08:52:27 +0000474 factory->NewFunctionWithoutPrototype(symbol, kNonStrictMode);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000475
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000476 // --- E m p t y ---
477 Handle<Code> code =
lrn@chromium.org7516f052011-03-30 08:52:27 +0000478 Handle<Code>(isolate->builtins()->builtin(
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000479 Builtins::kEmptyFunction));
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000480 empty_function->set_code(*code);
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000481 empty_function->shared()->set_code(*code);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000482 Handle<String> source = factory->NewStringFromAscii(CStrVector("() {}"));
483 Handle<Script> script = factory->NewScript(source);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000484 script->set_type(Smi::FromInt(Script::TYPE_NATIVE));
485 empty_function->shared()->set_script(*script);
486 empty_function->shared()->set_start_position(0);
487 empty_function->shared()->set_end_position(source->length());
488 empty_function->shared()->DontAdaptArguments();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000489
490 // Set prototypes for the function maps.
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000491 global_context()->function_map()->set_prototype(*empty_function);
492 global_context()->function_instance_map()->set_prototype(*empty_function);
kmillikin@chromium.org4111b802010-05-03 10:34:42 +0000493 global_context()->function_without_prototype_map()->
494 set_prototype(*empty_function);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000495 function_instance_map_writable_prototype_->set_prototype(*empty_function);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000496
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000497 // Allocate the function map first and then patch the prototype later
lrn@chromium.org7516f052011-03-30 08:52:27 +0000498 Handle<Map> empty_fm = factory->CopyMapDropDescriptors(
kmillikin@chromium.org4111b802010-05-03 10:34:42 +0000499 function_without_prototype_map);
500 empty_fm->set_instance_descriptors(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000501 function_without_prototype_map->instance_descriptors());
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000502 empty_fm->set_prototype(global_context()->object_function()->prototype());
503 empty_function->set_map(*empty_fm);
504 return empty_function;
505}
506
507
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000508Handle<DescriptorArray> Genesis::ComputeStrictFunctionInstanceDescriptor(
509 PrototypePropertyMode prototypeMode,
510 Handle<FixedArray> arguments,
511 Handle<FixedArray> caller) {
512 Handle<DescriptorArray> descriptors =
danno@chromium.org160a7b02011-04-18 15:51:38 +0000513 factory()->NewDescriptorArray(prototypeMode == DONT_ADD_PROTOTYPE
514 ? 4
515 : 5);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000516 PropertyAttributes attributes = static_cast<PropertyAttributes>(
517 DONT_ENUM | DONT_DELETE | READ_ONLY);
518
519 { // length
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000520 Handle<Foreign> foreign = factory()->NewForeign(&Accessors::FunctionLength);
521 CallbacksDescriptor d(*factory()->length_symbol(), *foreign, attributes);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000522 descriptors->Set(0, &d);
523 }
524 { // name
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000525 Handle<Foreign> foreign = factory()->NewForeign(&Accessors::FunctionName);
526 CallbacksDescriptor d(*factory()->name_symbol(), *foreign, attributes);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000527 descriptors->Set(1, &d);
528 }
529 { // arguments
danno@chromium.org160a7b02011-04-18 15:51:38 +0000530 CallbacksDescriptor d(*factory()->arguments_symbol(),
531 *arguments,
532 attributes);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000533 descriptors->Set(2, &d);
534 }
535 { // caller
danno@chromium.org160a7b02011-04-18 15:51:38 +0000536 CallbacksDescriptor d(*factory()->caller_symbol(), *caller, attributes);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000537 descriptors->Set(3, &d);
538 }
539
540 // prototype
541 if (prototypeMode != DONT_ADD_PROTOTYPE) {
542 if (prototypeMode == ADD_WRITEABLE_PROTOTYPE) {
543 attributes = static_cast<PropertyAttributes>(attributes & ~READ_ONLY);
544 }
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000545 Handle<Foreign> foreign =
546 factory()->NewForeign(&Accessors::FunctionPrototype);
547 CallbacksDescriptor d(*factory()->prototype_symbol(), *foreign, attributes);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000548 descriptors->Set(4, &d);
549 }
550
551 descriptors->Sort();
552 return descriptors;
553}
554
555
556// ECMAScript 5th Edition, 13.2.3
danno@chromium.org40cb8782011-05-25 07:58:50 +0000557Handle<JSFunction> Genesis::GetThrowTypeErrorFunction() {
558 if (throw_type_error_function.is_null()) {
559 Handle<String> name = factory()->LookupAsciiSymbol("ThrowTypeError");
560 throw_type_error_function =
561 factory()->NewFunctionWithoutPrototype(name, kNonStrictMode);
562 Handle<Code> code(isolate()->builtins()->builtin(
563 Builtins::kStrictModePoisonPill));
564 throw_type_error_function->set_map(
565 global_context()->function_map());
566 throw_type_error_function->set_code(*code);
567 throw_type_error_function->shared()->set_code(*code);
568 throw_type_error_function->shared()->DontAdaptArguments();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000569
danno@chromium.org40cb8782011-05-25 07:58:50 +0000570 PreventExtensions(throw_type_error_function);
571 }
572 return throw_type_error_function;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000573}
574
575
576Handle<Map> Genesis::CreateStrictModeFunctionMap(
577 PrototypePropertyMode prototype_mode,
578 Handle<JSFunction> empty_function,
579 Handle<FixedArray> arguments_callbacks,
580 Handle<FixedArray> caller_callbacks) {
danno@chromium.org160a7b02011-04-18 15:51:38 +0000581 Handle<Map> map = factory()->NewMap(JS_FUNCTION_TYPE, JSFunction::kSize);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000582 Handle<DescriptorArray> descriptors =
583 ComputeStrictFunctionInstanceDescriptor(prototype_mode,
584 arguments_callbacks,
585 caller_callbacks);
586 map->set_instance_descriptors(*descriptors);
587 map->set_function_with_prototype(prototype_mode != DONT_ADD_PROTOTYPE);
588 map->set_prototype(*empty_function);
589 return map;
590}
591
592
593void Genesis::CreateStrictModeFunctionMaps(Handle<JSFunction> empty) {
594 // Create the callbacks arrays for ThrowTypeError functions.
595 // The get/set callacks are filled in after the maps are created below.
danno@chromium.org160a7b02011-04-18 15:51:38 +0000596 Factory* factory = empty->GetIsolate()->factory();
lrn@chromium.org7516f052011-03-30 08:52:27 +0000597 Handle<FixedArray> arguments = factory->NewFixedArray(2, TENURED);
598 Handle<FixedArray> caller = factory->NewFixedArray(2, TENURED);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000599
600 // Allocate map for the strict mode function instances.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000601 Handle<Map> strict_mode_function_instance_map =
602 CreateStrictModeFunctionMap(
603 ADD_WRITEABLE_PROTOTYPE, empty, arguments, caller);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000604 global_context()->set_strict_mode_function_instance_map(
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000605 *strict_mode_function_instance_map);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000606
607 // Allocate map for the prototype-less strict mode instances.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000608 Handle<Map> strict_mode_function_without_prototype_map =
609 CreateStrictModeFunctionMap(
610 DONT_ADD_PROTOTYPE, empty, arguments, caller);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000611 global_context()->set_strict_mode_function_without_prototype_map(
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000612 *strict_mode_function_without_prototype_map);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000613
614 // Allocate map for the strict mode functions. This map is temporary, used
615 // only for processing of builtins.
616 // Later the map is replaced with writable prototype map, allocated below.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000617 Handle<Map> strict_mode_function_map =
618 CreateStrictModeFunctionMap(
619 ADD_READONLY_PROTOTYPE, empty, arguments, caller);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000620 global_context()->set_strict_mode_function_map(
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000621 *strict_mode_function_map);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000622
623 // The final map for the strict mode functions. Writeable prototype.
624 // This map is installed in MakeFunctionInstancePrototypeWritable.
625 strict_mode_function_instance_map_writable_prototype_ =
626 CreateStrictModeFunctionMap(
627 ADD_WRITEABLE_PROTOTYPE, empty, arguments, caller);
628
danno@chromium.org40cb8782011-05-25 07:58:50 +0000629 // Create the ThrowTypeError function instance.
630 Handle<JSFunction> throw_function =
631 GetThrowTypeErrorFunction();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000632
633 // Complete the callback fixed arrays.
danno@chromium.org40cb8782011-05-25 07:58:50 +0000634 arguments->set(0, *throw_function);
635 arguments->set(1, *throw_function);
636 caller->set(0, *throw_function);
637 caller->set(1, *throw_function);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000638}
639
640
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000641static void AddToWeakGlobalContextList(Context* context) {
642 ASSERT(context->IsGlobalContext());
danno@chromium.org160a7b02011-04-18 15:51:38 +0000643 Heap* heap = context->GetIsolate()->heap();
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000644#ifdef DEBUG
645 { // NOLINT
646 ASSERT(context->get(Context::NEXT_CONTEXT_LINK)->IsUndefined());
647 // Check that context is not in the list yet.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000648 for (Object* current = heap->global_contexts_list();
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000649 !current->IsUndefined();
650 current = Context::cast(current)->get(Context::NEXT_CONTEXT_LINK)) {
651 ASSERT(current != context);
652 }
653 }
654#endif
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000655 context->set(Context::NEXT_CONTEXT_LINK, heap->global_contexts_list());
656 heap->set_global_contexts_list(context);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000657}
658
659
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000660void Genesis::CreateRoots() {
661 // Allocate the global context FixedArray first and then patch the
662 // closure and extension object later (we need the empty function
663 // and the global object, but in order to create those, we need the
664 // global context).
danno@chromium.org160a7b02011-04-18 15:51:38 +0000665 global_context_ = Handle<Context>::cast(isolate()->global_handles()->Create(
666 *factory()->NewGlobalContext()));
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000667 AddToWeakGlobalContextList(*global_context_);
danno@chromium.org160a7b02011-04-18 15:51:38 +0000668 isolate()->set_context(*global_context());
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000669
670 // Allocate the message listeners object.
671 {
672 v8::NeanderArray listeners;
673 global_context()->set_message_listeners(*listeners.value());
674 }
675}
676
677
678Handle<JSGlobalProxy> Genesis::CreateNewGlobals(
679 v8::Handle<v8::ObjectTemplate> global_template,
680 Handle<Object> global_object,
681 Handle<GlobalObject>* inner_global_out) {
682 // The argument global_template aka data is an ObjectTemplateInfo.
683 // It has a constructor pointer that points at global_constructor which is a
684 // FunctionTemplateInfo.
685 // The global_constructor is used to create or reinitialize the global_proxy.
686 // The global_constructor also has a prototype_template pointer that points at
687 // js_global_template which is an ObjectTemplateInfo.
688 // That in turn has a constructor pointer that points at
689 // js_global_constructor which is a FunctionTemplateInfo.
690 // js_global_constructor is used to make js_global_function
691 // js_global_function is used to make the new inner_global.
692 //
693 // --- G l o b a l ---
694 // Step 1: Create a fresh inner JSGlobalObject.
695 Handle<JSFunction> js_global_function;
696 Handle<ObjectTemplateInfo> js_global_template;
697 if (!global_template.IsEmpty()) {
698 // Get prototype template of the global_template.
699 Handle<ObjectTemplateInfo> data =
700 v8::Utils::OpenHandle(*global_template);
701 Handle<FunctionTemplateInfo> global_constructor =
702 Handle<FunctionTemplateInfo>(
703 FunctionTemplateInfo::cast(data->constructor()));
704 Handle<Object> proto_template(global_constructor->prototype_template());
705 if (!proto_template->IsUndefined()) {
706 js_global_template =
707 Handle<ObjectTemplateInfo>::cast(proto_template);
708 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000709 }
710
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000711 if (js_global_template.is_null()) {
danno@chromium.org160a7b02011-04-18 15:51:38 +0000712 Handle<String> name = Handle<String>(heap()->empty_symbol());
713 Handle<Code> code = Handle<Code>(isolate()->builtins()->builtin(
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000714 Builtins::kIllegal));
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000715 js_global_function =
danno@chromium.org160a7b02011-04-18 15:51:38 +0000716 factory()->NewFunction(name, JS_GLOBAL_OBJECT_TYPE,
717 JSGlobalObject::kSize, code, true);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000718 // Change the constructor property of the prototype of the
719 // hidden global function to refer to the Object function.
720 Handle<JSObject> prototype =
721 Handle<JSObject>(
722 JSObject::cast(js_global_function->instance_prototype()));
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +0000723 SetLocalPropertyNoThrow(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000724 prototype,
danno@chromium.org160a7b02011-04-18 15:51:38 +0000725 factory()->constructor_symbol(),
726 isolate()->object_function(),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000727 NONE);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000728 } else {
729 Handle<FunctionTemplateInfo> js_global_constructor(
730 FunctionTemplateInfo::cast(js_global_template->constructor()));
731 js_global_function =
danno@chromium.org160a7b02011-04-18 15:51:38 +0000732 factory()->CreateApiFunction(js_global_constructor,
733 factory()->InnerGlobalObject);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000734 }
735
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000736 js_global_function->initial_map()->set_is_hidden_prototype();
737 Handle<GlobalObject> inner_global =
danno@chromium.org160a7b02011-04-18 15:51:38 +0000738 factory()->NewGlobalObject(js_global_function);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000739 if (inner_global_out != NULL) {
740 *inner_global_out = inner_global;
741 }
742
743 // Step 2: create or re-initialize the global proxy object.
744 Handle<JSFunction> global_proxy_function;
745 if (global_template.IsEmpty()) {
danno@chromium.org160a7b02011-04-18 15:51:38 +0000746 Handle<String> name = Handle<String>(heap()->empty_symbol());
747 Handle<Code> code = Handle<Code>(isolate()->builtins()->builtin(
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000748 Builtins::kIllegal));
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000749 global_proxy_function =
danno@chromium.org160a7b02011-04-18 15:51:38 +0000750 factory()->NewFunction(name, JS_GLOBAL_PROXY_TYPE,
751 JSGlobalProxy::kSize, code, true);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000752 } else {
753 Handle<ObjectTemplateInfo> data =
754 v8::Utils::OpenHandle(*global_template);
755 Handle<FunctionTemplateInfo> global_constructor(
756 FunctionTemplateInfo::cast(data->constructor()));
757 global_proxy_function =
danno@chromium.org160a7b02011-04-18 15:51:38 +0000758 factory()->CreateApiFunction(global_constructor,
759 factory()->OuterGlobalObject);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000760 }
761
danno@chromium.org160a7b02011-04-18 15:51:38 +0000762 Handle<String> global_name = factory()->LookupAsciiSymbol("global");
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000763 global_proxy_function->shared()->set_instance_class_name(*global_name);
764 global_proxy_function->initial_map()->set_is_access_check_needed(true);
765
766 // Set global_proxy.__proto__ to js_global after ConfigureGlobalObjects
767 // Return the global proxy.
768
769 if (global_object.location() != NULL) {
770 ASSERT(global_object->IsJSGlobalProxy());
771 return ReinitializeJSGlobalProxy(
772 global_proxy_function,
773 Handle<JSGlobalProxy>::cast(global_object));
774 } else {
775 return Handle<JSGlobalProxy>::cast(
danno@chromium.org160a7b02011-04-18 15:51:38 +0000776 factory()->NewJSObject(global_proxy_function, TENURED));
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000777 }
778}
779
780
781void Genesis::HookUpGlobalProxy(Handle<GlobalObject> inner_global,
782 Handle<JSGlobalProxy> global_proxy) {
783 // Set the global context for the global object.
784 inner_global->set_global_context(*global_context());
785 inner_global->set_global_receiver(*global_proxy);
786 global_proxy->set_context(*global_context());
787 global_context()->set_global_proxy(*global_proxy);
788}
789
790
791void Genesis::HookUpInnerGlobal(Handle<GlobalObject> inner_global) {
792 Handle<GlobalObject> inner_global_from_snapshot(
793 GlobalObject::cast(global_context_->extension()));
794 Handle<JSBuiltinsObject> builtins_global(global_context_->builtins());
795 global_context_->set_extension(*inner_global);
796 global_context_->set_global(*inner_global);
797 global_context_->set_security_token(*inner_global);
798 static const PropertyAttributes attributes =
799 static_cast<PropertyAttributes>(READ_ONLY | DONT_DELETE);
800 ForceSetProperty(builtins_global,
danno@chromium.org160a7b02011-04-18 15:51:38 +0000801 factory()->LookupAsciiSymbol("global"),
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000802 inner_global,
803 attributes);
804 // Setup the reference from the global object to the builtins object.
805 JSGlobalObject::cast(*inner_global)->set_builtins(*builtins_global);
806 TransferNamedProperties(inner_global_from_snapshot, inner_global);
807 TransferIndexedProperties(inner_global_from_snapshot, inner_global);
808}
809
810
811// This is only called if we are not using snapshots. The equivalent
812// work in the snapshot case is done in HookUpInnerGlobal.
813void Genesis::InitializeGlobal(Handle<GlobalObject> inner_global,
814 Handle<JSFunction> empty_function) {
815 // --- G l o b a l C o n t e x t ---
816 // Use the empty function as closure (no scope info).
817 global_context()->set_closure(*empty_function);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000818 global_context()->set_previous(NULL);
819 // Set extension and global object.
820 global_context()->set_extension(*inner_global);
821 global_context()->set_global(*inner_global);
822 // Security setup: Set the security token of the global object to
823 // its the inner global. This makes the security check between two
824 // different contexts fail by default even in case of global
825 // object reinitialization.
826 global_context()->set_security_token(*inner_global);
827
danno@chromium.org160a7b02011-04-18 15:51:38 +0000828 Isolate* isolate = inner_global->GetIsolate();
lrn@chromium.org7516f052011-03-30 08:52:27 +0000829 Factory* factory = isolate->factory();
830 Heap* heap = isolate->heap();
831
832 Handle<String> object_name = Handle<String>(heap->Object_symbol());
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +0000833 SetLocalPropertyNoThrow(inner_global, object_name,
lrn@chromium.org7516f052011-03-30 08:52:27 +0000834 isolate->object_function(), DONT_ENUM);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000835
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000836 Handle<JSObject> global = Handle<JSObject>(global_context()->global());
837
838 // Install global Function object
839 InstallFunction(global, "Function", JS_FUNCTION_TYPE, JSFunction::kSize,
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000840 empty_function, Builtins::kIllegal, true); // ECMA native.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000841
842 { // --- A r r a y ---
843 Handle<JSFunction> array_function =
844 InstallFunction(global, "Array", JS_ARRAY_TYPE, JSArray::kSize,
lrn@chromium.org7516f052011-03-30 08:52:27 +0000845 isolate->initial_object_prototype(),
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000846 Builtins::kArrayCode, true);
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +0000847 array_function->shared()->set_construct_stub(
lrn@chromium.org7516f052011-03-30 08:52:27 +0000848 isolate->builtins()->builtin(Builtins::kArrayConstructCode));
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000849 array_function->shared()->DontAdaptArguments();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000850
851 // This seems a bit hackish, but we need to make sure Array.length
852 // is 1.
853 array_function->shared()->set_length(1);
854 Handle<DescriptorArray> array_descriptors =
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000855 factory->CopyAppendForeignDescriptor(
lrn@chromium.org7516f052011-03-30 08:52:27 +0000856 factory->empty_descriptor_array(),
857 factory->length_symbol(),
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000858 factory->NewForeign(&Accessors::ArrayLength),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000859 static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE));
860
861 // Cache the fast JavaScript array map
862 global_context()->set_js_array_map(array_function->initial_map());
863 global_context()->js_array_map()->set_instance_descriptors(
864 *array_descriptors);
865 // array_function is used internally. JS code creating array object should
866 // search for the 'Array' property on the global object and use that one
867 // as the constructor. 'Array' property on a global object can be
868 // overwritten by JS code.
869 global_context()->set_array_function(*array_function);
870 }
871
872 { // --- N u m b e r ---
873 Handle<JSFunction> number_fun =
874 InstallFunction(global, "Number", JS_VALUE_TYPE, JSValue::kSize,
lrn@chromium.org7516f052011-03-30 08:52:27 +0000875 isolate->initial_object_prototype(),
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000876 Builtins::kIllegal, true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000877 global_context()->set_number_function(*number_fun);
878 }
879
880 { // --- B o o l e a n ---
881 Handle<JSFunction> boolean_fun =
882 InstallFunction(global, "Boolean", JS_VALUE_TYPE, JSValue::kSize,
lrn@chromium.org7516f052011-03-30 08:52:27 +0000883 isolate->initial_object_prototype(),
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000884 Builtins::kIllegal, true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000885 global_context()->set_boolean_function(*boolean_fun);
886 }
887
888 { // --- S t r i n g ---
889 Handle<JSFunction> string_fun =
890 InstallFunction(global, "String", JS_VALUE_TYPE, JSValue::kSize,
lrn@chromium.org7516f052011-03-30 08:52:27 +0000891 isolate->initial_object_prototype(),
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000892 Builtins::kIllegal, true);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000893 string_fun->shared()->set_construct_stub(
lrn@chromium.org7516f052011-03-30 08:52:27 +0000894 isolate->builtins()->builtin(Builtins::kStringConstructCode));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000895 global_context()->set_string_function(*string_fun);
896 // Add 'length' property to strings.
897 Handle<DescriptorArray> string_descriptors =
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000898 factory->CopyAppendForeignDescriptor(
lrn@chromium.org7516f052011-03-30 08:52:27 +0000899 factory->empty_descriptor_array(),
900 factory->length_symbol(),
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000901 factory->NewForeign(&Accessors::StringLength),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000902 static_cast<PropertyAttributes>(DONT_ENUM |
903 DONT_DELETE |
904 READ_ONLY));
905
906 Handle<Map> string_map =
907 Handle<Map>(global_context()->string_function()->initial_map());
908 string_map->set_instance_descriptors(*string_descriptors);
909 }
910
911 { // --- D a t e ---
912 // Builtin functions for Date.prototype.
913 Handle<JSFunction> date_fun =
914 InstallFunction(global, "Date", JS_VALUE_TYPE, JSValue::kSize,
lrn@chromium.org7516f052011-03-30 08:52:27 +0000915 isolate->initial_object_prototype(),
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000916 Builtins::kIllegal, true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000917
918 global_context()->set_date_function(*date_fun);
919 }
920
921
922 { // -- R e g E x p
923 // Builtin functions for RegExp.prototype.
924 Handle<JSFunction> regexp_fun =
ager@chromium.org236ad962008-09-25 09:45:57 +0000925 InstallFunction(global, "RegExp", JS_REGEXP_TYPE, JSRegExp::kSize,
lrn@chromium.org7516f052011-03-30 08:52:27 +0000926 isolate->initial_object_prototype(),
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000927 Builtins::kIllegal, true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000928 global_context()->set_regexp_function(*regexp_fun);
lrn@chromium.org25156de2010-04-06 13:10:27 +0000929
930 ASSERT(regexp_fun->has_initial_map());
931 Handle<Map> initial_map(regexp_fun->initial_map());
932
933 ASSERT_EQ(0, initial_map->inobject_properties());
934
lrn@chromium.org7516f052011-03-30 08:52:27 +0000935 Handle<DescriptorArray> descriptors = factory->NewDescriptorArray(5);
lrn@chromium.org25156de2010-04-06 13:10:27 +0000936 PropertyAttributes final =
937 static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE | READ_ONLY);
938 int enum_index = 0;
939 {
940 // ECMA-262, section 15.10.7.1.
lrn@chromium.org7516f052011-03-30 08:52:27 +0000941 FieldDescriptor field(heap->source_symbol(),
lrn@chromium.org25156de2010-04-06 13:10:27 +0000942 JSRegExp::kSourceFieldIndex,
943 final,
944 enum_index++);
945 descriptors->Set(0, &field);
946 }
947 {
948 // ECMA-262, section 15.10.7.2.
lrn@chromium.org7516f052011-03-30 08:52:27 +0000949 FieldDescriptor field(heap->global_symbol(),
lrn@chromium.org25156de2010-04-06 13:10:27 +0000950 JSRegExp::kGlobalFieldIndex,
951 final,
952 enum_index++);
953 descriptors->Set(1, &field);
954 }
955 {
956 // ECMA-262, section 15.10.7.3.
lrn@chromium.org7516f052011-03-30 08:52:27 +0000957 FieldDescriptor field(heap->ignore_case_symbol(),
lrn@chromium.org25156de2010-04-06 13:10:27 +0000958 JSRegExp::kIgnoreCaseFieldIndex,
959 final,
960 enum_index++);
961 descriptors->Set(2, &field);
962 }
963 {
964 // ECMA-262, section 15.10.7.4.
lrn@chromium.org7516f052011-03-30 08:52:27 +0000965 FieldDescriptor field(heap->multiline_symbol(),
lrn@chromium.org25156de2010-04-06 13:10:27 +0000966 JSRegExp::kMultilineFieldIndex,
967 final,
968 enum_index++);
969 descriptors->Set(3, &field);
970 }
971 {
972 // ECMA-262, section 15.10.7.5.
973 PropertyAttributes writable =
974 static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000975 FieldDescriptor field(heap->last_index_symbol(),
lrn@chromium.org25156de2010-04-06 13:10:27 +0000976 JSRegExp::kLastIndexFieldIndex,
977 writable,
978 enum_index++);
979 descriptors->Set(4, &field);
980 }
981 descriptors->SetNextEnumerationIndex(enum_index);
982 descriptors->Sort();
983
984 initial_map->set_inobject_properties(5);
985 initial_map->set_pre_allocated_property_fields(5);
986 initial_map->set_unused_property_fields(0);
987 initial_map->set_instance_size(
988 initial_map->instance_size() + 5 * kPointerSize);
989 initial_map->set_instance_descriptors(*descriptors);
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000990 initial_map->set_visitor_id(StaticVisitorBase::GetVisitorId(*initial_map));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000991 }
992
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000993 { // -- J S O N
lrn@chromium.org7516f052011-03-30 08:52:27 +0000994 Handle<String> name = factory->NewStringFromAscii(CStrVector("JSON"));
995 Handle<JSFunction> cons = factory->NewFunction(
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000996 name,
lrn@chromium.org7516f052011-03-30 08:52:27 +0000997 factory->the_hole_value());
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000998 cons->SetInstancePrototype(global_context()->initial_object_prototype());
999 cons->SetInstanceClassName(*name);
lrn@chromium.org7516f052011-03-30 08:52:27 +00001000 Handle<JSObject> json_object = factory->NewJSObject(cons, TENURED);
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00001001 ASSERT(json_object->IsJSObject());
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +00001002 SetLocalPropertyNoThrow(global, name, json_object, DONT_ENUM);
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00001003 global_context()->set_json_object(*json_object);
1004 }
1005
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001006 { // --- arguments_boilerplate_
1007 // Make sure we can recognize argument objects at runtime.
1008 // This is done by introducing an anonymous function with
1009 // class_name equals 'Arguments'.
lrn@chromium.org7516f052011-03-30 08:52:27 +00001010 Handle<String> symbol = factory->LookupAsciiSymbol("Arguments");
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001011 Handle<Code> code = Handle<Code>(
lrn@chromium.org7516f052011-03-30 08:52:27 +00001012 isolate->builtins()->builtin(Builtins::kIllegal));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001013 Handle<JSObject> prototype =
1014 Handle<JSObject>(
1015 JSObject::cast(global_context()->object_function()->prototype()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001016
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001017 Handle<JSFunction> function =
lrn@chromium.org7516f052011-03-30 08:52:27 +00001018 factory->NewFunctionWithPrototype(symbol,
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001019 JS_OBJECT_TYPE,
1020 JSObject::kHeaderSize,
1021 prototype,
1022 code,
1023 false);
1024 ASSERT(!function->has_initial_map());
1025 function->shared()->set_instance_class_name(*symbol);
1026 function->shared()->set_expected_nof_properties(2);
lrn@chromium.org7516f052011-03-30 08:52:27 +00001027 Handle<JSObject> result = factory->NewJSObject(function);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001028
1029 global_context()->set_arguments_boilerplate(*result);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001030 // Note: length must be added as the first property and
1031 // callee must be added as the second property.
lrn@chromium.org7516f052011-03-30 08:52:27 +00001032 SetLocalPropertyNoThrow(result, factory->length_symbol(),
1033 factory->undefined_value(),
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +00001034 DONT_ENUM);
lrn@chromium.org7516f052011-03-30 08:52:27 +00001035 SetLocalPropertyNoThrow(result, factory->callee_symbol(),
1036 factory->undefined_value(),
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +00001037 DONT_ENUM);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001038
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001039#ifdef DEBUG
1040 LookupResult lookup;
lrn@chromium.org7516f052011-03-30 08:52:27 +00001041 result->LocalLookup(heap->callee_symbol(), &lookup);
ager@chromium.org5c838252010-02-19 08:53:10 +00001042 ASSERT(lookup.IsProperty() && (lookup.type() == FIELD));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001043 ASSERT(lookup.GetFieldIndex() == Heap::kArgumentsCalleeIndex);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001044
lrn@chromium.org7516f052011-03-30 08:52:27 +00001045 result->LocalLookup(heap->length_symbol(), &lookup);
ager@chromium.org5c838252010-02-19 08:53:10 +00001046 ASSERT(lookup.IsProperty() && (lookup.type() == FIELD));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001047 ASSERT(lookup.GetFieldIndex() == Heap::kArgumentsLengthIndex);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001048
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001049 ASSERT(result->map()->inobject_properties() > Heap::kArgumentsCalleeIndex);
1050 ASSERT(result->map()->inobject_properties() > Heap::kArgumentsLengthIndex);
1051
1052 // Check the state of the object.
1053 ASSERT(result->HasFastProperties());
1054 ASSERT(result->HasFastElements());
1055#endif
1056 }
1057
whesse@chromium.org7b260152011-06-20 15:33:18 +00001058 { // --- aliased_arguments_boilerplate_
1059 Handle<Map> old_map(global_context()->arguments_boilerplate()->map());
1060 Handle<Map> new_map = factory->CopyMapDropTransitions(old_map);
1061 new_map->set_pre_allocated_property_fields(2);
1062 Handle<JSObject> result = factory->NewJSObjectFromMap(new_map);
1063 new_map->set_elements_kind(JSObject::NON_STRICT_ARGUMENTS_ELEMENTS);
1064 // Set up a well-formed parameter map to make assertions happy.
1065 Handle<FixedArray> elements = factory->NewFixedArray(2);
1066 elements->set_map(heap->non_strict_arguments_elements_map());
1067 Handle<FixedArray> array;
1068 array = factory->NewFixedArray(0);
1069 elements->set(0, *array);
1070 array = factory->NewFixedArray(0);
1071 elements->set(1, *array);
1072 result->set_elements(*elements);
1073 global_context()->set_aliased_arguments_boilerplate(*result);
1074 }
1075
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001076 { // --- strict mode arguments boilerplate
1077 const PropertyAttributes attributes =
1078 static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE | READ_ONLY);
1079
1080 // Create the ThrowTypeError functions.
lrn@chromium.org7516f052011-03-30 08:52:27 +00001081 Handle<FixedArray> callee = factory->NewFixedArray(2, TENURED);
1082 Handle<FixedArray> caller = factory->NewFixedArray(2, TENURED);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001083
danno@chromium.org40cb8782011-05-25 07:58:50 +00001084 Handle<JSFunction> throw_function =
1085 GetThrowTypeErrorFunction();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001086
1087 // Install the ThrowTypeError functions.
danno@chromium.org40cb8782011-05-25 07:58:50 +00001088 callee->set(0, *throw_function);
1089 callee->set(1, *throw_function);
1090 caller->set(0, *throw_function);
1091 caller->set(1, *throw_function);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001092
1093 // Create the descriptor array for the arguments object.
lrn@chromium.org7516f052011-03-30 08:52:27 +00001094 Handle<DescriptorArray> descriptors = factory->NewDescriptorArray(3);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001095 { // length
lrn@chromium.org7516f052011-03-30 08:52:27 +00001096 FieldDescriptor d(*factory->length_symbol(), 0, DONT_ENUM);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001097 descriptors->Set(0, &d);
1098 }
1099 { // callee
lrn@chromium.org7516f052011-03-30 08:52:27 +00001100 CallbacksDescriptor d(*factory->callee_symbol(), *callee, attributes);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001101 descriptors->Set(1, &d);
1102 }
1103 { // caller
lrn@chromium.org7516f052011-03-30 08:52:27 +00001104 CallbacksDescriptor d(*factory->caller_symbol(), *caller, attributes);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001105 descriptors->Set(2, &d);
1106 }
1107 descriptors->Sort();
1108
1109 // Create the map. Allocate one in-object field for length.
lrn@chromium.org7516f052011-03-30 08:52:27 +00001110 Handle<Map> map = factory->NewMap(JS_OBJECT_TYPE,
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001111 Heap::kArgumentsObjectSizeStrict);
1112 map->set_instance_descriptors(*descriptors);
1113 map->set_function_with_prototype(true);
1114 map->set_prototype(global_context()->object_function()->prototype());
1115 map->set_pre_allocated_property_fields(1);
1116 map->set_inobject_properties(1);
1117
1118 // Copy constructor from the non-strict arguments boilerplate.
1119 map->set_constructor(
1120 global_context()->arguments_boilerplate()->map()->constructor());
1121
1122 // Allocate the arguments boilerplate object.
lrn@chromium.org7516f052011-03-30 08:52:27 +00001123 Handle<JSObject> result = factory->NewJSObjectFromMap(map);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001124 global_context()->set_strict_mode_arguments_boilerplate(*result);
1125
1126 // Add length property only for strict mode boilerplate.
lrn@chromium.org7516f052011-03-30 08:52:27 +00001127 SetLocalPropertyNoThrow(result, factory->length_symbol(),
1128 factory->undefined_value(),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001129 DONT_ENUM);
1130
1131#ifdef DEBUG
1132 LookupResult lookup;
lrn@chromium.org7516f052011-03-30 08:52:27 +00001133 result->LocalLookup(heap->length_symbol(), &lookup);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001134 ASSERT(lookup.IsProperty() && (lookup.type() == FIELD));
1135 ASSERT(lookup.GetFieldIndex() == Heap::kArgumentsLengthIndex);
1136
1137 ASSERT(result->map()->inobject_properties() > Heap::kArgumentsLengthIndex);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001138
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001139 // Check the state of the object.
1140 ASSERT(result->HasFastProperties());
1141 ASSERT(result->HasFastElements());
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001142#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001143 }
1144
1145 { // --- context extension
1146 // Create a function for the context extension objects.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001147 Handle<Code> code = Handle<Code>(
lrn@chromium.org7516f052011-03-30 08:52:27 +00001148 isolate->builtins()->builtin(Builtins::kIllegal));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001149 Handle<JSFunction> context_extension_fun =
lrn@chromium.org7516f052011-03-30 08:52:27 +00001150 factory->NewFunction(factory->empty_symbol(),
ager@chromium.org32912102009-01-16 10:38:43 +00001151 JS_CONTEXT_EXTENSION_OBJECT_TYPE,
1152 JSObject::kHeaderSize,
1153 code,
1154 true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001155
lrn@chromium.org7516f052011-03-30 08:52:27 +00001156 Handle<String> name = factory->LookupAsciiSymbol("context_extension");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001157 context_extension_fun->shared()->set_instance_class_name(*name);
1158 global_context()->set_context_extension_function(*context_extension_fun);
1159 }
1160
sgjesse@chromium.org05521fc2009-05-21 07:37:44 +00001161
1162 {
1163 // Setup the call-as-function delegate.
1164 Handle<Code> code =
lrn@chromium.org7516f052011-03-30 08:52:27 +00001165 Handle<Code>(isolate->builtins()->builtin(
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00001166 Builtins::kHandleApiCallAsFunction));
sgjesse@chromium.org05521fc2009-05-21 07:37:44 +00001167 Handle<JSFunction> delegate =
lrn@chromium.org7516f052011-03-30 08:52:27 +00001168 factory->NewFunction(factory->empty_symbol(), JS_OBJECT_TYPE,
sgjesse@chromium.org05521fc2009-05-21 07:37:44 +00001169 JSObject::kHeaderSize, code, true);
1170 global_context()->set_call_as_function_delegate(*delegate);
1171 delegate->shared()->DontAdaptArguments();
1172 }
1173
1174 {
1175 // Setup the call-as-constructor delegate.
1176 Handle<Code> code =
lrn@chromium.org7516f052011-03-30 08:52:27 +00001177 Handle<Code>(isolate->builtins()->builtin(
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00001178 Builtins::kHandleApiCallAsConstructor));
sgjesse@chromium.org05521fc2009-05-21 07:37:44 +00001179 Handle<JSFunction> delegate =
lrn@chromium.org7516f052011-03-30 08:52:27 +00001180 factory->NewFunction(factory->empty_symbol(), JS_OBJECT_TYPE,
sgjesse@chromium.org05521fc2009-05-21 07:37:44 +00001181 JSObject::kHeaderSize, code, true);
1182 global_context()->set_call_as_constructor_delegate(*delegate);
1183 delegate->shared()->DontAdaptArguments();
1184 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001185
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001186 // Initialize the out of memory slot.
lrn@chromium.org7516f052011-03-30 08:52:27 +00001187 global_context()->set_out_of_memory(heap->false_value());
ager@chromium.org9085a012009-05-11 19:22:57 +00001188
1189 // Initialize the data slot.
lrn@chromium.org7516f052011-03-30 08:52:27 +00001190 global_context()->set_data(heap->undefined_value());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001191}
1192
1193
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +00001194void Genesis::InitializeExperimentalGlobal() {
1195 Isolate* isolate = this->isolate();
1196 Handle<JSObject> global = Handle<JSObject>(global_context()->global());
1197
1198 // TODO(mstarzinger): Move this into Genesis::InitializeGlobal once we no
1199 // longer need to live behind a flag, so WeakMap gets added to the snapshot.
1200 if (FLAG_harmony_weakmaps) { // -- W e a k M a p
1201 Handle<JSFunction> weakmap_fun =
1202 InstallFunction(global, "WeakMap", JS_WEAK_MAP_TYPE, JSWeakMap::kSize,
1203 isolate->initial_object_prototype(),
1204 Builtins::kIllegal, true);
1205 }
1206}
1207
1208
danno@chromium.org160a7b02011-04-18 15:51:38 +00001209bool Genesis::CompileBuiltin(Isolate* isolate, int index) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001210 Vector<const char> name = Natives::GetScriptName(index);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001211 Handle<String> source_code =
danno@chromium.org160a7b02011-04-18 15:51:38 +00001212 isolate->bootstrapper()->NativesSourceLookup(index);
1213 return CompileNative(name, source_code);
1214}
1215
1216
1217bool Genesis::CompileExperimentalBuiltin(Isolate* isolate, int index) {
1218 Vector<const char> name = ExperimentalNatives::GetScriptName(index);
1219 Factory* factory = isolate->factory();
1220 Handle<String> source_code =
jkummerow@chromium.orge297f592011-06-08 10:05:15 +00001221 factory->NewStringFromAscii(
1222 ExperimentalNatives::GetRawScriptSource(index));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001223 return CompileNative(name, source_code);
1224}
1225
1226
1227bool Genesis::CompileNative(Vector<const char> name, Handle<String> source) {
1228 HandleScope scope;
danno@chromium.org160a7b02011-04-18 15:51:38 +00001229 Isolate* isolate = source->GetIsolate();
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001230#ifdef ENABLE_DEBUGGER_SUPPORT
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001231 isolate->debugger()->set_compiling_natives(true);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001232#endif
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001233 bool result = CompileScriptCached(name,
1234 source,
1235 NULL,
1236 NULL,
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001237 Handle<Context>(isolate->context()),
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001238 true);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001239 ASSERT(isolate->has_pending_exception() != result);
1240 if (!result) isolate->clear_pending_exception();
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001241#ifdef ENABLE_DEBUGGER_SUPPORT
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001242 isolate->debugger()->set_compiling_natives(false);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001243#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001244 return result;
1245}
1246
1247
1248bool Genesis::CompileScriptCached(Vector<const char> name,
1249 Handle<String> source,
1250 SourceCodeCache* cache,
1251 v8::Extension* extension,
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001252 Handle<Context> top_context,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001253 bool use_runtime_context) {
danno@chromium.org160a7b02011-04-18 15:51:38 +00001254 Factory* factory = source->GetIsolate()->factory();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001255 HandleScope scope;
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001256 Handle<SharedFunctionInfo> function_info;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001257
1258 // If we can't find the function in the cache, we compile a new
1259 // function and insert it into the cache.
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001260 if (cache == NULL || !cache->Lookup(name, &function_info)) {
ager@chromium.org5ec48922009-05-05 07:25:34 +00001261 ASSERT(source->IsAsciiRepresentation());
lrn@chromium.org7516f052011-03-30 08:52:27 +00001262 Handle<String> script_name = factory->NewStringFromUtf8(name);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001263 function_info = Compiler::Compile(
1264 source,
1265 script_name,
1266 0,
1267 0,
1268 extension,
1269 NULL,
1270 Handle<String>::null(),
1271 use_runtime_context ? NATIVES_CODE : NOT_NATIVES_CODE);
1272 if (function_info.is_null()) return false;
1273 if (cache != NULL) cache->Add(name, function_info);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001274 }
1275
1276 // Setup the function context. Conceptually, we should clone the
1277 // function before overwriting the context but since we're in a
1278 // single-threaded environment it is not strictly necessary.
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001279 ASSERT(top_context->IsGlobalContext());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001280 Handle<Context> context =
1281 Handle<Context>(use_runtime_context
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001282 ? Handle<Context>(top_context->runtime_context())
1283 : top_context);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001284 Handle<JSFunction> fun =
lrn@chromium.org7516f052011-03-30 08:52:27 +00001285 factory->NewFunctionFromSharedFunctionInfo(function_info, context);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001286
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001287 // Call function using either the runtime object or the global
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001288 // object as the receiver. Provide no parameters.
1289 Handle<Object> receiver =
1290 Handle<Object>(use_runtime_context
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001291 ? top_context->builtins()
1292 : top_context->global());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001293 bool has_pending_exception;
lrn@chromium.org1c092762011-05-09 09:42:16 +00001294 Execution::Call(fun, receiver, 0, NULL, &has_pending_exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001295 if (has_pending_exception) return false;
ager@chromium.org5c838252010-02-19 08:53:10 +00001296 return true;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001297}
1298
1299
danno@chromium.org160a7b02011-04-18 15:51:38 +00001300#define INSTALL_NATIVE(Type, name, var) \
1301 Handle<String> var##_name = factory()->LookupAsciiSymbol(name); \
1302 Object* var##_native = \
1303 global_context()->builtins()->GetPropertyNoExceptionThrown( \
1304 *var##_name); \
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001305 global_context()->set_##var(Type::cast(var##_native));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001306
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001307
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001308void Genesis::InstallNativeFunctions() {
1309 HandleScope scope;
1310 INSTALL_NATIVE(JSFunction, "CreateDate", create_date_fun);
1311 INSTALL_NATIVE(JSFunction, "ToNumber", to_number_fun);
1312 INSTALL_NATIVE(JSFunction, "ToString", to_string_fun);
1313 INSTALL_NATIVE(JSFunction, "ToDetailString", to_detail_string_fun);
1314 INSTALL_NATIVE(JSFunction, "ToObject", to_object_fun);
1315 INSTALL_NATIVE(JSFunction, "ToInteger", to_integer_fun);
1316 INSTALL_NATIVE(JSFunction, "ToUint32", to_uint32_fun);
1317 INSTALL_NATIVE(JSFunction, "ToInt32", to_int32_fun);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00001318 INSTALL_NATIVE(JSFunction, "GlobalEval", global_eval_fun);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001319 INSTALL_NATIVE(JSFunction, "Instantiate", instantiate_fun);
1320 INSTALL_NATIVE(JSFunction, "ConfigureTemplateInstance",
1321 configure_instance_fun);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001322 INSTALL_NATIVE(JSFunction, "GetStackTraceLine", get_stack_trace_line_fun);
1323 INSTALL_NATIVE(JSObject, "functionCache", function_cache);
1324}
1325
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001326void Genesis::InstallExperimentalNativeFunctions() {
1327 if (FLAG_harmony_proxies) {
rossberg@chromium.org717967f2011-07-20 13:44:42 +00001328 INSTALL_NATIVE(JSFunction, "DerivedHasTrap", derived_has_trap);
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001329 INSTALL_NATIVE(JSFunction, "DerivedGetTrap", derived_get_trap);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00001330 INSTALL_NATIVE(JSFunction, "DerivedSetTrap", derived_set_trap);
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001331 }
1332}
1333
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001334#undef INSTALL_NATIVE
1335
1336
1337bool Genesis::InstallNatives() {
1338 HandleScope scope;
1339
1340 // Create a function for the builtins object. Allocate space for the
1341 // JavaScript builtins, a reference to the builtins object
1342 // (itself) and a reference to the global_context directly in the object.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001343 Handle<Code> code = Handle<Code>(
danno@chromium.org160a7b02011-04-18 15:51:38 +00001344 isolate()->builtins()->builtin(Builtins::kIllegal));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001345 Handle<JSFunction> builtins_fun =
danno@chromium.org160a7b02011-04-18 15:51:38 +00001346 factory()->NewFunction(factory()->empty_symbol(),
1347 JS_BUILTINS_OBJECT_TYPE,
1348 JSBuiltinsObject::kSize, code, true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001349
danno@chromium.org160a7b02011-04-18 15:51:38 +00001350 Handle<String> name = factory()->LookupAsciiSymbol("builtins");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001351 builtins_fun->shared()->set_instance_class_name(*name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001352
1353 // Allocate the builtins object.
1354 Handle<JSBuiltinsObject> builtins =
danno@chromium.org160a7b02011-04-18 15:51:38 +00001355 Handle<JSBuiltinsObject>::cast(factory()->NewGlobalObject(builtins_fun));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001356 builtins->set_builtins(*builtins);
1357 builtins->set_global_context(*global_context());
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001358 builtins->set_global_receiver(*builtins);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001359
1360 // Setup the 'global' properties of the builtins object. The
1361 // 'global' property that refers to the global object is the only
1362 // way to get from code running in the builtins context to the
1363 // global object.
1364 static const PropertyAttributes attributes =
1365 static_cast<PropertyAttributes>(READ_ONLY | DONT_DELETE);
danno@chromium.org160a7b02011-04-18 15:51:38 +00001366 Handle<String> global_symbol = factory()->LookupAsciiSymbol("global");
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +00001367 Handle<Object> global_obj(global_context()->global());
1368 SetLocalPropertyNoThrow(builtins, global_symbol, global_obj, attributes);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001369
1370 // Setup the reference from the global object to the builtins object.
1371 JSGlobalObject::cast(global_context()->global())->set_builtins(*builtins);
1372
1373 // Create a bridge function that has context in the global context.
1374 Handle<JSFunction> bridge =
danno@chromium.org160a7b02011-04-18 15:51:38 +00001375 factory()->NewFunction(factory()->empty_symbol(),
1376 factory()->undefined_value());
1377 ASSERT(bridge->context() == *isolate()->global_context());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001378
1379 // Allocate the builtins context.
1380 Handle<Context> context =
danno@chromium.org160a7b02011-04-18 15:51:38 +00001381 factory()->NewFunctionContext(Context::MIN_CONTEXT_SLOTS, bridge);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001382 context->set_global(*builtins); // override builtins global object
1383
1384 global_context()->set_runtime_context(*context);
1385
1386 { // -- S c r i p t
1387 // Builtin functions for Script.
1388 Handle<JSFunction> script_fun =
1389 InstallFunction(builtins, "Script", JS_VALUE_TYPE, JSValue::kSize,
danno@chromium.org160a7b02011-04-18 15:51:38 +00001390 isolate()->initial_object_prototype(),
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00001391 Builtins::kIllegal, false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001392 Handle<JSObject> prototype =
danno@chromium.org160a7b02011-04-18 15:51:38 +00001393 factory()->NewJSObject(isolate()->object_function(), TENURED);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001394 SetPrototype(script_fun, prototype);
1395 global_context()->set_script_function(*script_fun);
1396
1397 // Add 'source' and 'data' property to scripts.
1398 PropertyAttributes common_attributes =
1399 static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE | READ_ONLY);
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001400 Handle<Foreign> foreign_source =
1401 factory()->NewForeign(&Accessors::ScriptSource);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001402 Handle<DescriptorArray> script_descriptors =
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001403 factory()->CopyAppendForeignDescriptor(
danno@chromium.org160a7b02011-04-18 15:51:38 +00001404 factory()->empty_descriptor_array(),
1405 factory()->LookupAsciiSymbol("source"),
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001406 foreign_source,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001407 common_attributes);
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001408 Handle<Foreign> foreign_name =
1409 factory()->NewForeign(&Accessors::ScriptName);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001410 script_descriptors =
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001411 factory()->CopyAppendForeignDescriptor(
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001412 script_descriptors,
danno@chromium.org160a7b02011-04-18 15:51:38 +00001413 factory()->LookupAsciiSymbol("name"),
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001414 foreign_name,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001415 common_attributes);
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001416 Handle<Foreign> foreign_id = factory()->NewForeign(&Accessors::ScriptId);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001417 script_descriptors =
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001418 factory()->CopyAppendForeignDescriptor(
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001419 script_descriptors,
danno@chromium.org160a7b02011-04-18 15:51:38 +00001420 factory()->LookupAsciiSymbol("id"),
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001421 foreign_id,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001422 common_attributes);
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001423 Handle<Foreign> foreign_line_offset =
1424 factory()->NewForeign(&Accessors::ScriptLineOffset);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001425 script_descriptors =
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001426 factory()->CopyAppendForeignDescriptor(
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001427 script_descriptors,
danno@chromium.org160a7b02011-04-18 15:51:38 +00001428 factory()->LookupAsciiSymbol("line_offset"),
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001429 foreign_line_offset,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001430 common_attributes);
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001431 Handle<Foreign> foreign_column_offset =
1432 factory()->NewForeign(&Accessors::ScriptColumnOffset);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001433 script_descriptors =
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001434 factory()->CopyAppendForeignDescriptor(
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001435 script_descriptors,
danno@chromium.org160a7b02011-04-18 15:51:38 +00001436 factory()->LookupAsciiSymbol("column_offset"),
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001437 foreign_column_offset,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001438 common_attributes);
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001439 Handle<Foreign> foreign_data =
1440 factory()->NewForeign(&Accessors::ScriptData);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001441 script_descriptors =
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001442 factory()->CopyAppendForeignDescriptor(
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001443 script_descriptors,
danno@chromium.org160a7b02011-04-18 15:51:38 +00001444 factory()->LookupAsciiSymbol("data"),
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001445 foreign_data,
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001446 common_attributes);
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001447 Handle<Foreign> foreign_type =
1448 factory()->NewForeign(&Accessors::ScriptType);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001449 script_descriptors =
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001450 factory()->CopyAppendForeignDescriptor(
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001451 script_descriptors,
danno@chromium.org160a7b02011-04-18 15:51:38 +00001452 factory()->LookupAsciiSymbol("type"),
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001453 foreign_type,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001454 common_attributes);
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001455 Handle<Foreign> foreign_compilation_type =
1456 factory()->NewForeign(&Accessors::ScriptCompilationType);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001457 script_descriptors =
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001458 factory()->CopyAppendForeignDescriptor(
ager@chromium.orge2902be2009-06-08 12:21:35 +00001459 script_descriptors,
danno@chromium.org160a7b02011-04-18 15:51:38 +00001460 factory()->LookupAsciiSymbol("compilation_type"),
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001461 foreign_compilation_type,
ager@chromium.orge2902be2009-06-08 12:21:35 +00001462 common_attributes);
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001463 Handle<Foreign> foreign_line_ends =
1464 factory()->NewForeign(&Accessors::ScriptLineEnds);
iposva@chromium.org245aa852009-02-10 00:49:54 +00001465 script_descriptors =
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001466 factory()->CopyAppendForeignDescriptor(
iposva@chromium.org245aa852009-02-10 00:49:54 +00001467 script_descriptors,
danno@chromium.org160a7b02011-04-18 15:51:38 +00001468 factory()->LookupAsciiSymbol("line_ends"),
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001469 foreign_line_ends,
iposva@chromium.org245aa852009-02-10 00:49:54 +00001470 common_attributes);
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001471 Handle<Foreign> foreign_context_data =
1472 factory()->NewForeign(&Accessors::ScriptContextData);
ager@chromium.org9085a012009-05-11 19:22:57 +00001473 script_descriptors =
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001474 factory()->CopyAppendForeignDescriptor(
ager@chromium.org9085a012009-05-11 19:22:57 +00001475 script_descriptors,
danno@chromium.org160a7b02011-04-18 15:51:38 +00001476 factory()->LookupAsciiSymbol("context_data"),
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001477 foreign_context_data,
ager@chromium.org9085a012009-05-11 19:22:57 +00001478 common_attributes);
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001479 Handle<Foreign> foreign_eval_from_script =
1480 factory()->NewForeign(&Accessors::ScriptEvalFromScript);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001481 script_descriptors =
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001482 factory()->CopyAppendForeignDescriptor(
ager@chromium.orge2902be2009-06-08 12:21:35 +00001483 script_descriptors,
danno@chromium.org160a7b02011-04-18 15:51:38 +00001484 factory()->LookupAsciiSymbol("eval_from_script"),
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001485 foreign_eval_from_script,
ager@chromium.orge2902be2009-06-08 12:21:35 +00001486 common_attributes);
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001487 Handle<Foreign> foreign_eval_from_script_position =
1488 factory()->NewForeign(&Accessors::ScriptEvalFromScriptPosition);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001489 script_descriptors =
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001490 factory()->CopyAppendForeignDescriptor(
ager@chromium.orge2902be2009-06-08 12:21:35 +00001491 script_descriptors,
danno@chromium.org160a7b02011-04-18 15:51:38 +00001492 factory()->LookupAsciiSymbol("eval_from_script_position"),
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001493 foreign_eval_from_script_position,
sgjesse@chromium.org98180592009-12-02 08:17:28 +00001494 common_attributes);
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001495 Handle<Foreign> foreign_eval_from_function_name =
1496 factory()->NewForeign(&Accessors::ScriptEvalFromFunctionName);
sgjesse@chromium.org98180592009-12-02 08:17:28 +00001497 script_descriptors =
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001498 factory()->CopyAppendForeignDescriptor(
sgjesse@chromium.org98180592009-12-02 08:17:28 +00001499 script_descriptors,
danno@chromium.org160a7b02011-04-18 15:51:38 +00001500 factory()->LookupAsciiSymbol("eval_from_function_name"),
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001501 foreign_eval_from_function_name,
ager@chromium.orge2902be2009-06-08 12:21:35 +00001502 common_attributes);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001503
1504 Handle<Map> script_map = Handle<Map>(script_fun->initial_map());
1505 script_map->set_instance_descriptors(*script_descriptors);
1506
1507 // Allocate the empty script.
danno@chromium.org160a7b02011-04-18 15:51:38 +00001508 Handle<Script> script = factory()->NewScript(factory()->empty_string());
ager@chromium.orge2902be2009-06-08 12:21:35 +00001509 script->set_type(Smi::FromInt(Script::TYPE_NATIVE));
danno@chromium.org160a7b02011-04-18 15:51:38 +00001510 heap()->public_set_empty_script(*script);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001511 }
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001512 {
1513 // Builtin function for OpaqueReference -- a JSValue-based object,
1514 // that keeps its field isolated from JavaScript code. It may store
1515 // objects, that JavaScript code may not access.
1516 Handle<JSFunction> opaque_reference_fun =
1517 InstallFunction(builtins, "OpaqueReference", JS_VALUE_TYPE,
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001518 JSValue::kSize,
danno@chromium.org160a7b02011-04-18 15:51:38 +00001519 isolate()->initial_object_prototype(),
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00001520 Builtins::kIllegal, false);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001521 Handle<JSObject> prototype =
danno@chromium.org160a7b02011-04-18 15:51:38 +00001522 factory()->NewJSObject(isolate()->object_function(), TENURED);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001523 SetPrototype(opaque_reference_fun, prototype);
1524 global_context()->set_opaque_reference_function(*opaque_reference_fun);
1525 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001526
karlklose@chromium.org8f806e82011-03-07 14:06:08 +00001527 { // --- I n t e r n a l A r r a y ---
1528 // An array constructor on the builtins object that works like
1529 // the public Array constructor, except that its prototype
1530 // doesn't inherit from Object.prototype.
1531 // To be used only for internal work by builtins. Instances
1532 // must not be leaked to user code.
1533 // Only works correctly when called as a constructor. The normal
1534 // Array code uses Array.prototype as prototype when called as
1535 // a function.
1536 Handle<JSFunction> array_function =
1537 InstallFunction(builtins,
1538 "InternalArray",
1539 JS_ARRAY_TYPE,
1540 JSArray::kSize,
danno@chromium.org160a7b02011-04-18 15:51:38 +00001541 isolate()->initial_object_prototype(),
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00001542 Builtins::kArrayCode,
karlklose@chromium.org8f806e82011-03-07 14:06:08 +00001543 true);
1544 Handle<JSObject> prototype =
danno@chromium.org160a7b02011-04-18 15:51:38 +00001545 factory()->NewJSObject(isolate()->object_function(), TENURED);
karlklose@chromium.org8f806e82011-03-07 14:06:08 +00001546 SetPrototype(array_function, prototype);
1547
1548 array_function->shared()->set_construct_stub(
danno@chromium.org160a7b02011-04-18 15:51:38 +00001549 isolate()->builtins()->builtin(Builtins::kArrayConstructCode));
karlklose@chromium.org8f806e82011-03-07 14:06:08 +00001550 array_function->shared()->DontAdaptArguments();
1551
1552 // Make "length" magic on instances.
1553 Handle<DescriptorArray> array_descriptors =
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001554 factory()->CopyAppendForeignDescriptor(
danno@chromium.org160a7b02011-04-18 15:51:38 +00001555 factory()->empty_descriptor_array(),
1556 factory()->length_symbol(),
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001557 factory()->NewForeign(&Accessors::ArrayLength),
karlklose@chromium.org8f806e82011-03-07 14:06:08 +00001558 static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE));
1559
1560 array_function->initial_map()->set_instance_descriptors(
1561 *array_descriptors);
1562 }
1563
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001564 if (FLAG_disable_native_files) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001565 PrintF("Warning: Running without installed natives!\n");
1566 return true;
1567 }
1568
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001569 // Install natives.
1570 for (int i = Natives::GetDebuggerCount();
1571 i < Natives::GetBuiltinsCount();
1572 i++) {
danno@chromium.org160a7b02011-04-18 15:51:38 +00001573 if (!CompileBuiltin(isolate(), i)) return false;
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001574 // TODO(ager): We really only need to install the JS builtin
1575 // functions on the builtins object after compiling and running
1576 // runtime.js.
1577 if (!InstallJSBuiltins(builtins)) return false;
1578 }
1579
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001580 InstallNativeFunctions();
1581
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00001582 // Store the map for the string prototype after the natives has been compiled
1583 // and the String function has been setup.
1584 Handle<JSFunction> string_function(global_context()->string_function());
1585 ASSERT(JSObject::cast(
1586 string_function->initial_map()->prototype())->HasFastProperties());
1587 global_context()->set_string_function_prototype_map(
1588 HeapObject::cast(string_function->initial_map()->prototype())->map());
1589
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001590 InstallBuiltinFunctionIds();
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001591
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001592 // Install Function.prototype.call and apply.
danno@chromium.org160a7b02011-04-18 15:51:38 +00001593 { Handle<String> key = factory()->function_class_symbol();
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001594 Handle<JSFunction> function =
danno@chromium.org160a7b02011-04-18 15:51:38 +00001595 Handle<JSFunction>::cast(GetProperty(isolate()->global(), key));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001596 Handle<JSObject> proto =
1597 Handle<JSObject>(JSObject::cast(function->instance_prototype()));
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001598
1599 // Install the call and the apply functions.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001600 Handle<JSFunction> call =
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001601 InstallFunction(proto, "call", JS_OBJECT_TYPE, JSObject::kHeaderSize,
kmillikin@chromium.org4111b802010-05-03 10:34:42 +00001602 Handle<JSObject>::null(),
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00001603 Builtins::kFunctionCall,
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001604 false);
1605 Handle<JSFunction> apply =
1606 InstallFunction(proto, "apply", JS_OBJECT_TYPE, JSObject::kHeaderSize,
kmillikin@chromium.org4111b802010-05-03 10:34:42 +00001607 Handle<JSObject>::null(),
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00001608 Builtins::kFunctionApply,
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001609 false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001610
1611 // Make sure that Function.prototype.call appears to be compiled.
1612 // The code will never be called, but inline caching for call will
1613 // only work if it appears to be compiled.
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001614 call->shared()->DontAdaptArguments();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001615 ASSERT(call->is_compiled());
1616
ager@chromium.org32912102009-01-16 10:38:43 +00001617 // Set the expected parameters for apply to 2; required by builtin.
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001618 apply->shared()->set_formal_parameter_count(2);
1619
1620 // Set the lengths for the functions to satisfy ECMA-262.
1621 call->shared()->set_length(1);
1622 apply->shared()->set_length(2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001623 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001624
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00001625 // Create a constructor for RegExp results (a variant of Array that
1626 // predefines the two properties index and match).
1627 {
1628 // RegExpResult initial map.
1629
1630 // Find global.Array.prototype to inherit from.
1631 Handle<JSFunction> array_constructor(global_context()->array_function());
1632 Handle<JSObject> array_prototype(
1633 JSObject::cast(array_constructor->instance_prototype()));
1634
1635 // Add initial map.
1636 Handle<Map> initial_map =
danno@chromium.org160a7b02011-04-18 15:51:38 +00001637 factory()->NewMap(JS_ARRAY_TYPE, JSRegExpResult::kSize);
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00001638 initial_map->set_constructor(*array_constructor);
1639
1640 // Set prototype on map.
1641 initial_map->set_non_instance_prototype(false);
1642 initial_map->set_prototype(*array_prototype);
1643
1644 // Update map with length accessor from Array and add "index" and "input".
1645 Handle<Map> array_map(global_context()->js_array_map());
1646 Handle<DescriptorArray> array_descriptors(
1647 array_map->instance_descriptors());
1648 ASSERT_EQ(1, array_descriptors->number_of_descriptors());
1649
1650 Handle<DescriptorArray> reresult_descriptors =
danno@chromium.org160a7b02011-04-18 15:51:38 +00001651 factory()->NewDescriptorArray(3);
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00001652
1653 reresult_descriptors->CopyFrom(0, *array_descriptors, 0);
1654
1655 int enum_index = 0;
1656 {
danno@chromium.org160a7b02011-04-18 15:51:38 +00001657 FieldDescriptor index_field(heap()->index_symbol(),
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00001658 JSRegExpResult::kIndexIndex,
1659 NONE,
1660 enum_index++);
1661 reresult_descriptors->Set(1, &index_field);
1662 }
1663
1664 {
danno@chromium.org160a7b02011-04-18 15:51:38 +00001665 FieldDescriptor input_field(heap()->input_symbol(),
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00001666 JSRegExpResult::kInputIndex,
1667 NONE,
1668 enum_index++);
1669 reresult_descriptors->Set(2, &input_field);
1670 }
1671 reresult_descriptors->Sort();
1672
1673 initial_map->set_inobject_properties(2);
1674 initial_map->set_pre_allocated_property_fields(2);
1675 initial_map->set_unused_property_fields(0);
1676 initial_map->set_instance_descriptors(*reresult_descriptors);
1677
1678 global_context()->set_regexp_result_map(*initial_map);
1679 }
1680
karlklose@chromium.org8f806e82011-03-07 14:06:08 +00001681
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001682#ifdef DEBUG
1683 builtins->Verify();
1684#endif
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001685
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001686 return true;
1687}
1688
1689
danno@chromium.org160a7b02011-04-18 15:51:38 +00001690bool Genesis::InstallExperimentalNatives() {
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001691 for (int i = ExperimentalNatives::GetDebuggerCount();
1692 i < ExperimentalNatives::GetBuiltinsCount();
1693 i++) {
1694 if (FLAG_harmony_proxies &&
1695 strcmp(ExperimentalNatives::GetScriptName(i).start(),
1696 "native proxy.js") == 0) {
danno@chromium.org160a7b02011-04-18 15:51:38 +00001697 if (!CompileExperimentalBuiltin(isolate(), i)) return false;
1698 }
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +00001699 if (FLAG_harmony_weakmaps &&
1700 strcmp(ExperimentalNatives::GetScriptName(i).start(),
1701 "native weakmap.js") == 0) {
1702 if (!CompileExperimentalBuiltin(isolate(), i)) return false;
1703 }
danno@chromium.org160a7b02011-04-18 15:51:38 +00001704 }
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001705
1706 InstallExperimentalNativeFunctions();
1707
danno@chromium.org160a7b02011-04-18 15:51:38 +00001708 return true;
1709}
1710
1711
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001712static Handle<JSObject> ResolveBuiltinIdHolder(
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001713 Handle<Context> global_context,
1714 const char* holder_expr) {
danno@chromium.org160a7b02011-04-18 15:51:38 +00001715 Factory* factory = global_context->GetIsolate()->factory();
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001716 Handle<GlobalObject> global(global_context->global());
1717 const char* period_pos = strchr(holder_expr, '.');
1718 if (period_pos == NULL) {
1719 return Handle<JSObject>::cast(
lrn@chromium.org7516f052011-03-30 08:52:27 +00001720 GetProperty(global, factory->LookupAsciiSymbol(holder_expr)));
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00001721 }
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001722 ASSERT_EQ(".prototype", period_pos);
1723 Vector<const char> property(holder_expr,
1724 static_cast<int>(period_pos - holder_expr));
1725 Handle<JSFunction> function = Handle<JSFunction>::cast(
lrn@chromium.org7516f052011-03-30 08:52:27 +00001726 GetProperty(global, factory->LookupSymbol(property)));
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001727 return Handle<JSObject>(JSObject::cast(function->prototype()));
1728}
1729
1730
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001731static void InstallBuiltinFunctionId(Handle<JSObject> holder,
1732 const char* function_name,
1733 BuiltinFunctionId id) {
danno@chromium.org160a7b02011-04-18 15:51:38 +00001734 Factory* factory = holder->GetIsolate()->factory();
1735 Handle<String> name = factory->LookupAsciiSymbol(function_name);
lrn@chromium.org303ada72010-10-27 09:33:13 +00001736 Object* function_object = holder->GetProperty(*name)->ToObjectUnchecked();
1737 Handle<JSFunction> function(JSFunction::cast(function_object));
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001738 function->shared()->set_function_data(Smi::FromInt(id));
1739}
1740
1741
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001742void Genesis::InstallBuiltinFunctionIds() {
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001743 HandleScope scope;
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001744#define INSTALL_BUILTIN_ID(holder_expr, fun_name, name) \
1745 { \
1746 Handle<JSObject> holder = ResolveBuiltinIdHolder( \
1747 global_context(), #holder_expr); \
1748 BuiltinFunctionId id = k##name; \
1749 InstallBuiltinFunctionId(holder, #fun_name, id); \
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001750 }
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001751 FUNCTIONS_WITH_ID_LIST(INSTALL_BUILTIN_ID)
1752#undef INSTALL_BUILTIN_ID
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001753}
1754
1755
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00001756// Do not forget to update macros.py with named constant
1757// of cache id.
1758#define JSFUNCTION_RESULT_CACHE_LIST(F) \
1759 F(16, global_context()->regexp_function())
1760
1761
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001762static FixedArray* CreateCache(int size, Handle<JSFunction> factory_function) {
danno@chromium.org160a7b02011-04-18 15:51:38 +00001763 Factory* factory = factory_function->GetIsolate()->factory();
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00001764 // Caches are supposed to live for a long time, allocate in old space.
1765 int array_size = JSFunctionResultCache::kEntriesIndex + 2 * size;
ager@chromium.orgac091b72010-05-05 07:34:42 +00001766 // Cannot use cast as object is not fully initialized yet.
1767 JSFunctionResultCache* cache = reinterpret_cast<JSFunctionResultCache*>(
danno@chromium.org160a7b02011-04-18 15:51:38 +00001768 *factory->NewFixedArrayWithHoles(array_size, TENURED));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001769 cache->set(JSFunctionResultCache::kFactoryIndex, *factory_function);
ager@chromium.orgac091b72010-05-05 07:34:42 +00001770 cache->MakeZeroSize();
1771 return cache;
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00001772}
1773
1774
1775void Genesis::InstallJSFunctionResultCaches() {
1776 const int kNumberOfCaches = 0 +
1777#define F(size, func) + 1
1778 JSFUNCTION_RESULT_CACHE_LIST(F)
1779#undef F
1780 ;
1781
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001782 Handle<FixedArray> caches = FACTORY->NewFixedArray(kNumberOfCaches, TENURED);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00001783
1784 int index = 0;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001785
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001786#define F(size, func) do { \
1787 FixedArray* cache = CreateCache((size), Handle<JSFunction>(func)); \
1788 caches->set(index++, cache); \
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001789 } while (false)
1790
1791 JSFUNCTION_RESULT_CACHE_LIST(F);
1792
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00001793#undef F
1794
1795 global_context()->set_jsfunction_result_caches(*caches);
1796}
1797
1798
ricow@chromium.org65fae842010-08-25 15:26:24 +00001799void Genesis::InitializeNormalizedMapCaches() {
1800 Handle<FixedArray> array(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001801 FACTORY->NewFixedArray(NormalizedMapCache::kEntries, TENURED));
ricow@chromium.org65fae842010-08-25 15:26:24 +00001802 global_context()->set_normalized_map_cache(NormalizedMapCache::cast(*array));
1803}
1804
1805
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001806bool Bootstrapper::InstallExtensions(Handle<Context> global_context,
1807 v8::ExtensionConfiguration* extensions) {
danno@chromium.org160a7b02011-04-18 15:51:38 +00001808 Isolate* isolate = global_context->GetIsolate();
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001809 BootstrapperActive active;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001810 SaveContext saved_context(isolate);
1811 isolate->set_context(*global_context);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001812 if (!Genesis::InstallExtensions(global_context, extensions)) return false;
1813 Genesis::InstallSpecialObjects(global_context);
1814 return true;
1815}
1816
1817
1818void Genesis::InstallSpecialObjects(Handle<Context> global_context) {
danno@chromium.org160a7b02011-04-18 15:51:38 +00001819 Factory* factory = global_context->GetIsolate()->factory();
mads.s.agercbaa0602008-08-14 13:41:48 +00001820 HandleScope scope;
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001821 Handle<JSGlobalObject> js_global(
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001822 JSGlobalObject::cast(global_context->global()));
mads.s.agercbaa0602008-08-14 13:41:48 +00001823 // Expose the natives in global if a name for it is specified.
1824 if (FLAG_expose_natives_as != NULL && strlen(FLAG_expose_natives_as) != 0) {
1825 Handle<String> natives_string =
lrn@chromium.org7516f052011-03-30 08:52:27 +00001826 factory->LookupAsciiSymbol(FLAG_expose_natives_as);
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +00001827 SetLocalPropertyNoThrow(js_global, natives_string,
1828 Handle<JSObject>(js_global->builtins()), DONT_ENUM);
mads.s.agercbaa0602008-08-14 13:41:48 +00001829 }
1830
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00001831 Handle<Object> Error = GetProperty(js_global, "Error");
1832 if (Error->IsJSObject()) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00001833 Handle<String> name = factory->LookupAsciiSymbol("stackTraceLimit");
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +00001834 SetLocalPropertyNoThrow(Handle<JSObject>::cast(Error),
1835 name,
1836 Handle<Smi>(Smi::FromInt(FLAG_stack_trace_limit)),
1837 NONE);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001838 }
1839
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001840#ifdef ENABLE_DEBUGGER_SUPPORT
mads.s.agercbaa0602008-08-14 13:41:48 +00001841 // Expose the debug global object in global if a name for it is specified.
1842 if (FLAG_expose_debug_as != NULL && strlen(FLAG_expose_debug_as) != 0) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001843 Debug* debug = Isolate::Current()->debug();
mads.s.agercbaa0602008-08-14 13:41:48 +00001844 // If loading fails we just bail out without installing the
1845 // debugger but without tanking the whole context.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001846 if (!debug->Load()) return;
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001847 // Set the security token for the debugger context to the same as
1848 // the shell global context to allow calling between these (otherwise
1849 // exposing debug global object doesn't make much sense).
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001850 debug->debug_context()->set_security_token(
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001851 global_context->security_token());
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001852
mads.s.agercbaa0602008-08-14 13:41:48 +00001853 Handle<String> debug_string =
lrn@chromium.org7516f052011-03-30 08:52:27 +00001854 factory->LookupAsciiSymbol(FLAG_expose_debug_as);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001855 Handle<Object> global_proxy(debug->debug_context()->global_proxy());
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +00001856 SetLocalPropertyNoThrow(js_global, debug_string, global_proxy, DONT_ENUM);
mads.s.agercbaa0602008-08-14 13:41:48 +00001857 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001858#endif
mads.s.agercbaa0602008-08-14 13:41:48 +00001859}
1860
1861
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001862bool Genesis::InstallExtensions(Handle<Context> global_context,
1863 v8::ExtensionConfiguration* extensions) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001864 // TODO(isolates): Extensions on multiple isolates may take a little more
1865 // effort. (The external API reads 'ignore'-- does that mean
1866 // we can break the interface?)
1867
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001868 // Clear coloring of extension list
1869 v8::RegisteredExtension* current = v8::RegisteredExtension::first_extension();
1870 while (current != NULL) {
1871 current->set_state(v8::UNVISITED);
1872 current = current->next();
1873 }
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001874 // Install auto extensions.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001875 current = v8::RegisteredExtension::first_extension();
1876 while (current != NULL) {
1877 if (current->extension()->auto_enable())
1878 InstallExtension(current);
1879 current = current->next();
1880 }
1881
1882 if (FLAG_expose_gc) InstallExtension("v8/gc");
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00001883 if (FLAG_expose_externalize_string) InstallExtension("v8/externalize");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001884
1885 if (extensions == NULL) return true;
1886 // Install required extensions
1887 int count = v8::ImplementationUtilities::GetNameCount(extensions);
1888 const char** names = v8::ImplementationUtilities::GetNames(extensions);
1889 for (int i = 0; i < count; i++) {
1890 if (!InstallExtension(names[i]))
1891 return false;
1892 }
1893
1894 return true;
1895}
1896
1897
1898// Installs a named extension. This methods is unoptimized and does
1899// not scale well if we want to support a large number of extensions.
1900bool Genesis::InstallExtension(const char* name) {
1901 v8::RegisteredExtension* current = v8::RegisteredExtension::first_extension();
1902 // Loop until we find the relevant extension
1903 while (current != NULL) {
1904 if (strcmp(name, current->extension()->name()) == 0) break;
1905 current = current->next();
1906 }
1907 // Didn't find the extension; fail.
1908 if (current == NULL) {
1909 v8::Utils::ReportApiFailure(
1910 "v8::Context::New()", "Cannot find required extension");
1911 return false;
1912 }
1913 return InstallExtension(current);
1914}
1915
1916
1917bool Genesis::InstallExtension(v8::RegisteredExtension* current) {
1918 HandleScope scope;
1919
1920 if (current->state() == v8::INSTALLED) return true;
1921 // The current node has already been visited so there must be a
1922 // cycle in the dependency graph; fail.
1923 if (current->state() == v8::VISITED) {
1924 v8::Utils::ReportApiFailure(
1925 "v8::Context::New()", "Circular extension dependency");
1926 return false;
1927 }
1928 ASSERT(current->state() == v8::UNVISITED);
1929 current->set_state(v8::VISITED);
1930 v8::Extension* extension = current->extension();
1931 // Install the extension's dependencies
1932 for (int i = 0; i < extension->dependency_count(); i++) {
1933 if (!InstallExtension(extension->dependencies()[i])) return false;
1934 }
lrn@chromium.org7516f052011-03-30 08:52:27 +00001935 Isolate* isolate = Isolate::Current();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001936 Vector<const char> source = CStrVector(extension->source());
lrn@chromium.org7516f052011-03-30 08:52:27 +00001937 Handle<String> source_code = isolate->factory()->NewStringFromAscii(source);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001938 bool result = CompileScriptCached(CStrVector(extension->name()),
1939 source_code,
lrn@chromium.org7516f052011-03-30 08:52:27 +00001940 isolate->bootstrapper()->extensions_cache(),
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001941 extension,
lrn@chromium.org7516f052011-03-30 08:52:27 +00001942 Handle<Context>(isolate->context()),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001943 false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00001944 ASSERT(isolate->has_pending_exception() != result);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001945 if (!result) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00001946 isolate->clear_pending_exception();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001947 }
1948 current->set_state(v8::INSTALLED);
1949 return result;
1950}
1951
1952
ager@chromium.org5c838252010-02-19 08:53:10 +00001953bool Genesis::InstallJSBuiltins(Handle<JSBuiltinsObject> builtins) {
1954 HandleScope scope;
danno@chromium.org160a7b02011-04-18 15:51:38 +00001955 Factory* factory = builtins->GetIsolate()->factory();
ager@chromium.org5c838252010-02-19 08:53:10 +00001956 for (int i = 0; i < Builtins::NumberOfJavaScriptBuiltins(); i++) {
1957 Builtins::JavaScript id = static_cast<Builtins::JavaScript>(i);
danno@chromium.org160a7b02011-04-18 15:51:38 +00001958 Handle<String> name = factory->LookupAsciiSymbol(Builtins::GetName(id));
lrn@chromium.org303ada72010-10-27 09:33:13 +00001959 Object* function_object = builtins->GetPropertyNoExceptionThrown(*name);
ager@chromium.org5c838252010-02-19 08:53:10 +00001960 Handle<JSFunction> function
lrn@chromium.org303ada72010-10-27 09:33:13 +00001961 = Handle<JSFunction>(JSFunction::cast(function_object));
ager@chromium.org5c838252010-02-19 08:53:10 +00001962 builtins->set_javascript_builtin(id, *function);
1963 Handle<SharedFunctionInfo> shared
1964 = Handle<SharedFunctionInfo>(function->shared());
1965 if (!EnsureCompiled(shared, CLEAR_EXCEPTION)) return false;
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00001966 // Set the code object on the function object.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001967 function->ReplaceCode(function->shared()->code());
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00001968 builtins->set_javascript_builtin_code(id, shared->code());
ager@chromium.org5c838252010-02-19 08:53:10 +00001969 }
1970 return true;
1971}
1972
1973
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001974bool Genesis::ConfigureGlobalObjects(
1975 v8::Handle<v8::ObjectTemplate> global_proxy_template) {
1976 Handle<JSObject> global_proxy(
1977 JSObject::cast(global_context()->global_proxy()));
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001978 Handle<JSObject> inner_global(JSObject::cast(global_context()->global()));
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001979
1980 if (!global_proxy_template.IsEmpty()) {
1981 // Configure the global proxy object.
1982 Handle<ObjectTemplateInfo> proxy_data =
1983 v8::Utils::OpenHandle(*global_proxy_template);
1984 if (!ConfigureApiObject(global_proxy, proxy_data)) return false;
1985
1986 // Configure the inner global object.
1987 Handle<FunctionTemplateInfo> proxy_constructor(
1988 FunctionTemplateInfo::cast(proxy_data->constructor()));
1989 if (!proxy_constructor->prototype_template()->IsUndefined()) {
1990 Handle<ObjectTemplateInfo> inner_data(
1991 ObjectTemplateInfo::cast(proxy_constructor->prototype_template()));
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001992 if (!ConfigureApiObject(inner_global, inner_data)) return false;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001993 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001994 }
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001995
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001996 SetObjectPrototype(global_proxy, inner_global);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001997 return true;
1998}
1999
2000
2001bool Genesis::ConfigureApiObject(Handle<JSObject> object,
2002 Handle<ObjectTemplateInfo> object_template) {
2003 ASSERT(!object_template.is_null());
2004 ASSERT(object->IsInstanceOf(
2005 FunctionTemplateInfo::cast(object_template->constructor())));
2006
2007 bool pending_exception = false;
2008 Handle<JSObject> obj =
2009 Execution::InstantiateObject(object_template, &pending_exception);
2010 if (pending_exception) {
danno@chromium.org160a7b02011-04-18 15:51:38 +00002011 ASSERT(isolate()->has_pending_exception());
2012 isolate()->clear_pending_exception();
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002013 return false;
2014 }
2015 TransferObject(obj, object);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002016 return true;
2017}
2018
2019
2020void Genesis::TransferNamedProperties(Handle<JSObject> from,
2021 Handle<JSObject> to) {
2022 if (from->HasFastProperties()) {
2023 Handle<DescriptorArray> descs =
2024 Handle<DescriptorArray>(from->map()->instance_descriptors());
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002025 for (int i = 0; i < descs->number_of_descriptors(); i++) {
2026 PropertyDetails details = PropertyDetails(descs->GetDetails(i));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002027 switch (details.type()) {
2028 case FIELD: {
2029 HandleScope inner;
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002030 Handle<String> key = Handle<String>(descs->GetKey(i));
2031 int index = descs->GetFieldIndex(i);
ager@chromium.org7c537e22008-10-16 08:43:32 +00002032 Handle<Object> value = Handle<Object>(from->FastPropertyAt(index));
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +00002033 SetLocalPropertyNoThrow(to, key, value, details.attributes());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002034 break;
2035 }
2036 case CONSTANT_FUNCTION: {
2037 HandleScope inner;
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002038 Handle<String> key = Handle<String>(descs->GetKey(i));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002039 Handle<JSFunction> fun =
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002040 Handle<JSFunction>(descs->GetConstantFunction(i));
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +00002041 SetLocalPropertyNoThrow(to, key, fun, details.attributes());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002042 break;
2043 }
2044 case CALLBACKS: {
2045 LookupResult result;
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002046 to->LocalLookup(descs->GetKey(i), &result);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002047 // If the property is already there we skip it
ager@chromium.org5c838252010-02-19 08:53:10 +00002048 if (result.IsProperty()) continue;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002049 HandleScope inner;
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002050 ASSERT(!to->HasFastProperties());
2051 // Add to dictionary.
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002052 Handle<String> key = Handle<String>(descs->GetKey(i));
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002053 Handle<Object> callbacks(descs->GetCallbacksObject(i));
2054 PropertyDetails d =
2055 PropertyDetails(details.attributes(), CALLBACKS, details.index());
2056 SetNormalizedProperty(to, key, callbacks, d);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002057 break;
2058 }
2059 case MAP_TRANSITION:
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00002060 case EXTERNAL_ARRAY_TRANSITION:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002061 case CONSTANT_TRANSITION:
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002062 case NULL_DESCRIPTOR:
2063 // Ignore non-properties.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002064 break;
2065 case NORMAL:
2066 // Do not occur since the from object has fast properties.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002067 case HANDLER:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002068 case INTERCEPTOR:
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002069 // No element in instance descriptors have proxy or interceptor type.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002070 UNREACHABLE();
2071 break;
2072 }
2073 }
2074 } else {
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00002075 Handle<StringDictionary> properties =
2076 Handle<StringDictionary>(from->property_dictionary());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002077 int capacity = properties->Capacity();
2078 for (int i = 0; i < capacity; i++) {
2079 Object* raw_key(properties->KeyAt(i));
2080 if (properties->IsKey(raw_key)) {
2081 ASSERT(raw_key->IsString());
2082 // If the property is already there we skip it.
2083 LookupResult result;
2084 to->LocalLookup(String::cast(raw_key), &result);
ager@chromium.org5c838252010-02-19 08:53:10 +00002085 if (result.IsProperty()) continue;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002086 // Set the property.
2087 Handle<String> key = Handle<String>(String::cast(raw_key));
2088 Handle<Object> value = Handle<Object>(properties->ValueAt(i));
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002089 if (value->IsJSGlobalPropertyCell()) {
2090 value = Handle<Object>(JSGlobalPropertyCell::cast(*value)->value());
2091 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002092 PropertyDetails details = properties->DetailsAt(i);
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +00002093 SetLocalPropertyNoThrow(to, key, value, details.attributes());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002094 }
2095 }
2096 }
2097}
2098
2099
2100void Genesis::TransferIndexedProperties(Handle<JSObject> from,
2101 Handle<JSObject> to) {
2102 // Cloning the elements array is sufficient.
2103 Handle<FixedArray> from_elements =
2104 Handle<FixedArray>(FixedArray::cast(from->elements()));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002105 Handle<FixedArray> to_elements = FACTORY->CopyFixedArray(from_elements);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002106 to->set_elements(*to_elements);
2107}
2108
2109
2110void Genesis::TransferObject(Handle<JSObject> from, Handle<JSObject> to) {
2111 HandleScope outer;
danno@chromium.org160a7b02011-04-18 15:51:38 +00002112 Factory* factory = from->GetIsolate()->factory();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002113
2114 ASSERT(!from->IsJSArray());
2115 ASSERT(!to->IsJSArray());
2116
2117 TransferNamedProperties(from, to);
2118 TransferIndexedProperties(from, to);
2119
2120 // Transfer the prototype (new map is needed).
2121 Handle<Map> old_to_map = Handle<Map>(to->map());
danno@chromium.org160a7b02011-04-18 15:51:38 +00002122 Handle<Map> new_to_map = factory->CopyMapDropTransitions(old_to_map);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002123 new_to_map->set_prototype(from->map()->prototype());
2124 to->set_map(*new_to_map);
2125}
2126
2127
2128void Genesis::MakeFunctionInstancePrototypeWritable() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002129 // The maps with writable prototype are created in CreateEmptyFunction
2130 // and CreateStrictModeFunctionMaps respectively. Initially the maps are
2131 // created with read-only prototype for JS builtins processing.
2132 ASSERT(!function_instance_map_writable_prototype_.is_null());
2133 ASSERT(!strict_mode_function_instance_map_writable_prototype_.is_null());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002134
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002135 // Replace function instance maps to make prototype writable.
2136 global_context()->set_function_map(
2137 *function_instance_map_writable_prototype_);
2138 global_context()->set_strict_mode_function_map(
2139 *strict_mode_function_instance_map_writable_prototype_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002140}
2141
2142
danno@chromium.org160a7b02011-04-18 15:51:38 +00002143Genesis::Genesis(Isolate* isolate,
2144 Handle<Object> global_object,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002145 v8::Handle<v8::ObjectTemplate> global_template,
danno@chromium.org160a7b02011-04-18 15:51:38 +00002146 v8::ExtensionConfiguration* extensions) : isolate_(isolate) {
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00002147 result_ = Handle<Context>::null();
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002148 // If V8 isn't running and cannot be initialized, just return.
2149 if (!V8::IsRunning() && !V8::Initialize(NULL)) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002150
2151 // Before creating the roots we must save the context and restore it
2152 // on all function exits.
2153 HandleScope scope;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002154 SaveContext saved_context(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002155
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002156 Handle<Context> new_context = Snapshot::NewContextFromSnapshot();
2157 if (!new_context.is_null()) {
2158 global_context_ =
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002159 Handle<Context>::cast(isolate->global_handles()->Create(*new_context));
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002160 AddToWeakGlobalContextList(*global_context_);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002161 isolate->set_context(*global_context_);
2162 isolate->counters()->contexts_created_by_snapshot()->Increment();
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002163 Handle<GlobalObject> inner_global;
2164 Handle<JSGlobalProxy> global_proxy =
2165 CreateNewGlobals(global_template,
2166 global_object,
2167 &inner_global);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002168
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002169 HookUpGlobalProxy(inner_global, global_proxy);
2170 HookUpInnerGlobal(inner_global);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002171
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002172 if (!ConfigureGlobalObjects(global_template)) return;
2173 } else {
2174 // We get here if there was no context snapshot.
2175 CreateRoots();
danno@chromium.org160a7b02011-04-18 15:51:38 +00002176 Handle<JSFunction> empty_function = CreateEmptyFunction(isolate);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002177 CreateStrictModeFunctionMaps(empty_function);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002178 Handle<GlobalObject> inner_global;
2179 Handle<JSGlobalProxy> global_proxy =
2180 CreateNewGlobals(global_template, global_object, &inner_global);
2181 HookUpGlobalProxy(inner_global, global_proxy);
2182 InitializeGlobal(inner_global, empty_function);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00002183 InstallJSFunctionResultCaches();
ricow@chromium.org65fae842010-08-25 15:26:24 +00002184 InitializeNormalizedMapCaches();
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00002185 if (!InstallNatives()) return;
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002186
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002187 MakeFunctionInstancePrototypeWritable();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002188
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002189 if (!ConfigureGlobalObjects(global_template)) return;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002190 isolate->counters()->contexts_created_from_scratch()->Increment();
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002191 }
mads.s.agercbaa0602008-08-14 13:41:48 +00002192
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +00002193 // Initialize experimental globals and install experimental natives.
2194 InitializeExperimentalGlobal();
danno@chromium.org160a7b02011-04-18 15:51:38 +00002195 if (!InstallExperimentalNatives()) return;
2196
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002197 result_ = global_context_;
2198}
2199
ager@chromium.orgddb913d2009-01-27 10:01:48 +00002200
2201// Support for thread preemption.
2202
2203// Reserve space for statics needing saving and restoring.
2204int Bootstrapper::ArchiveSpacePerThread() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002205 return sizeof(NestingCounterType);
ager@chromium.orgddb913d2009-01-27 10:01:48 +00002206}
2207
2208
2209// Archive statics that are thread local.
2210char* Bootstrapper::ArchiveState(char* to) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002211 *reinterpret_cast<NestingCounterType*>(to) = nesting_;
2212 nesting_ = 0;
2213 return to + sizeof(NestingCounterType);
ager@chromium.orgddb913d2009-01-27 10:01:48 +00002214}
2215
2216
2217// Restore statics that are thread local.
2218char* Bootstrapper::RestoreState(char* from) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002219 nesting_ = *reinterpret_cast<NestingCounterType*>(from);
2220 return from + sizeof(NestingCounterType);
ager@chromium.orgddb913d2009-01-27 10:01:48 +00002221}
2222
2223
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002224// Called when the top-level V8 mutex is destroyed.
2225void Bootstrapper::FreeThreadResources() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002226 ASSERT(!IsActive());
ager@chromium.orgddb913d2009-01-27 10:01:48 +00002227}
2228
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002229} } // namespace v8::internal