blob: 6db9a4819a3d6153706ab71bf9c579abedb2e4fd [file] [log] [blame]
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001// Copyright 2006-2008 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// A SourceCodeCache uses a FixedArray to store pairs of
48// (AsciiString*, JSFunction*), mapping names of native code files
49// (runtime.js, etc.) to precompiled functions. Instead of mapping
50// names to functions it might make sense to let the JS2C tool
51// generate an index for each native JS file.
52class SourceCodeCache BASE_EMBEDDED {
53 public:
kasperl@chromium.orge959c182009-07-27 08:59:04 +000054 explicit SourceCodeCache(Script::Type type): type_(type), cache_(NULL) { }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000055
56 void Initialize(bool create_heap_objects) {
kasperl@chromium.orge959c182009-07-27 08:59:04 +000057 cache_ = create_heap_objects ? Heap::empty_fixed_array() : NULL;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000058 }
59
60 void Iterate(ObjectVisitor* v) {
vegorov@chromium.org26c16f82010-08-11 13:41:03 +000061 v->VisitPointer(BitCast<Object**>(&cache_));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000062 }
63
64
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +000065 bool Lookup(Vector<const char> name, Handle<SharedFunctionInfo>* handle) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000066 for (int i = 0; i < cache_->length(); i+=2) {
ager@chromium.org7c537e22008-10-16 08:43:32 +000067 SeqAsciiString* str = SeqAsciiString::cast(cache_->get(i));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000068 if (str->IsEqualTo(name)) {
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +000069 *handle = Handle<SharedFunctionInfo>(
70 SharedFunctionInfo::cast(cache_->get(i + 1)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000071 return true;
72 }
73 }
74 return false;
75 }
76
77
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +000078 void Add(Vector<const char> name, Handle<SharedFunctionInfo> shared) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000079 HandleScope scope;
80 int length = cache_->length();
81 Handle<FixedArray> new_array =
82 Factory::NewFixedArray(length + 2, TENURED);
83 cache_->CopyTo(0, *new_array, 0, cache_->length());
84 cache_ = *new_array;
85 Handle<String> str = Factory::NewStringFromAscii(name, TENURED);
86 cache_->set(length, *str);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +000087 cache_->set(length + 1, *shared);
88 Script::cast(shared->script())->set_type(Smi::FromInt(type_));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000089 }
90
91 private:
ager@chromium.orge2902be2009-06-08 12:21:35 +000092 Script::Type type_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000093 FixedArray* cache_;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000094 DISALLOW_COPY_AND_ASSIGN(SourceCodeCache);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000095};
96
ager@chromium.orge2902be2009-06-08 12:21:35 +000097static SourceCodeCache extensions_cache(Script::TYPE_EXTENSION);
ager@chromium.orgc4c92722009-11-18 14:12:51 +000098// This is for delete, not delete[].
99static List<char*>* delete_these_non_arrays_on_tear_down = NULL;
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000100// This is for delete[]
101static List<char*>* delete_these_arrays_on_tear_down = NULL;
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000102
103
104NativesExternalStringResource::NativesExternalStringResource(const char* source)
105 : data_(source), length_(StrLength(source)) {
106 if (delete_these_non_arrays_on_tear_down == NULL) {
107 delete_these_non_arrays_on_tear_down = new List<char*>(2);
108 }
109 // The resources are small objects and we only make a fixed number of
110 // them, but let's clean them up on exit for neatness.
111 delete_these_non_arrays_on_tear_down->
112 Add(reinterpret_cast<char*>(this));
113}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000114
115
116Handle<String> Bootstrapper::NativesSourceLookup(int index) {
117 ASSERT(0 <= index && index < Natives::GetBuiltinsCount());
118 if (Heap::natives_source_cache()->get(index)->IsUndefined()) {
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000119 if (!Snapshot::IsEnabled() || FLAG_new_snapshot) {
120 // We can use external strings for the natives.
121 NativesExternalStringResource* resource =
122 new NativesExternalStringResource(
123 Natives::GetScriptSource(index).start());
124 Handle<String> source_code =
125 Factory::NewExternalStringFromAscii(resource);
126 Heap::natives_source_cache()->set(index, *source_code);
127 } else {
128 // Old snapshot code can't cope with external strings at all.
129 Handle<String> source_code =
130 Factory::NewStringFromAscii(Natives::GetScriptSource(index));
131 Heap::natives_source_cache()->set(index, *source_code);
132 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000133 }
134 Handle<Object> cached_source(Heap::natives_source_cache()->get(index));
135 return Handle<String>::cast(cached_source);
136}
137
138
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000139void Bootstrapper::Initialize(bool create_heap_objects) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000140 extensions_cache.Initialize(create_heap_objects);
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +0000141 GCExtension::Register();
142 ExternalizeStringExtension::Register();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000143}
144
145
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000146char* Bootstrapper::AllocateAutoDeletedArray(int bytes) {
147 char* memory = new char[bytes];
148 if (memory != NULL) {
149 if (delete_these_arrays_on_tear_down == NULL) {
150 delete_these_arrays_on_tear_down = new List<char*>(2);
151 }
152 delete_these_arrays_on_tear_down->Add(memory);
153 }
154 return memory;
155}
156
157
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000158void Bootstrapper::TearDown() {
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000159 if (delete_these_non_arrays_on_tear_down != NULL) {
160 int len = delete_these_non_arrays_on_tear_down->length();
161 ASSERT(len < 20); // Don't use this mechanism for unbounded allocations.
162 for (int i = 0; i < len; i++) {
163 delete delete_these_non_arrays_on_tear_down->at(i);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000164 delete_these_non_arrays_on_tear_down->at(i) = NULL;
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000165 }
166 delete delete_these_non_arrays_on_tear_down;
167 delete_these_non_arrays_on_tear_down = NULL;
168 }
169
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000170 if (delete_these_arrays_on_tear_down != NULL) {
171 int len = delete_these_arrays_on_tear_down->length();
172 ASSERT(len < 1000); // Don't use this mechanism for unbounded allocations.
173 for (int i = 0; i < len; i++) {
174 delete[] delete_these_arrays_on_tear_down->at(i);
175 delete_these_arrays_on_tear_down->at(i) = NULL;
176 }
177 delete delete_these_arrays_on_tear_down;
178 delete_these_arrays_on_tear_down = NULL;
179 }
180
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000181 extensions_cache.Initialize(false); // Yes, symmetrical
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000182}
183
184
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000185class Genesis BASE_EMBEDDED {
186 public:
187 Genesis(Handle<Object> global_object,
188 v8::Handle<v8::ObjectTemplate> global_template,
189 v8::ExtensionConfiguration* extensions);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000190 ~Genesis() { }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000191
192 Handle<Context> result() { return result_; }
193
194 Genesis* previous() { return previous_; }
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000195
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000196 private:
197 Handle<Context> global_context_;
198
199 // There may be more than one active genesis object: When GC is
200 // triggered during environment creation there may be weak handle
201 // processing callbacks which may create new environments.
202 Genesis* previous_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000203
204 Handle<Context> global_context() { return global_context_; }
205
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000206 // Creates some basic objects. Used for creating a context from scratch.
207 void CreateRoots();
208 // Creates the empty function. Used for creating a context from scratch.
209 Handle<JSFunction> CreateEmptyFunction();
210 // Creates the global objects using the global and the template passed in
211 // through the API. We call this regardless of whether we are building a
212 // context from scratch or using a deserialized one from the partial snapshot
213 // but in the latter case we don't use the objects it produces directly, as
214 // we have to used the deserialized ones that are linked together with the
215 // rest of the context snapshot.
216 Handle<JSGlobalProxy> CreateNewGlobals(
217 v8::Handle<v8::ObjectTemplate> global_template,
218 Handle<Object> global_object,
219 Handle<GlobalObject>* global_proxy_out);
220 // Hooks the given global proxy into the context. If the context was created
221 // by deserialization then this will unhook the global proxy that was
222 // deserialized, leaving the GC to pick it up.
223 void HookUpGlobalProxy(Handle<GlobalObject> inner_global,
224 Handle<JSGlobalProxy> global_proxy);
225 // Similarly, we want to use the inner global that has been created by the
226 // templates passed through the API. The inner global from the snapshot is
227 // detached from the other objects in the snapshot.
228 void HookUpInnerGlobal(Handle<GlobalObject> inner_global);
229 // New context initialization. Used for creating a context from scratch.
230 void InitializeGlobal(Handle<GlobalObject> inner_global,
231 Handle<JSFunction> empty_function);
232 // Installs the contents of the native .js files on the global objects.
233 // Used for creating a context from scratch.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000234 void InstallNativeFunctions();
235 bool InstallNatives();
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000236 void InstallBuiltinFunctionIds();
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000237 void InstallJSFunctionResultCaches();
ricow@chromium.org65fae842010-08-25 15:26:24 +0000238 void InitializeNormalizedMapCaches();
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000239 // Used both for deserialized and from-scratch contexts to add the extensions
240 // provided.
241 static bool InstallExtensions(Handle<Context> global_context,
242 v8::ExtensionConfiguration* extensions);
243 static bool InstallExtension(const char* name);
244 static bool InstallExtension(v8::RegisteredExtension* current);
245 static void InstallSpecialObjects(Handle<Context> global_context);
ager@chromium.org5c838252010-02-19 08:53:10 +0000246 bool InstallJSBuiltins(Handle<JSBuiltinsObject> builtins);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000247 bool ConfigureApiObject(Handle<JSObject> object,
248 Handle<ObjectTemplateInfo> object_template);
249 bool ConfigureGlobalObjects(v8::Handle<v8::ObjectTemplate> global_template);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000250
251 // Migrates all properties from the 'from' object to the 'to'
252 // object and overrides the prototype in 'to' with the one from
253 // 'from'.
254 void TransferObject(Handle<JSObject> from, Handle<JSObject> to);
255 void TransferNamedProperties(Handle<JSObject> from, Handle<JSObject> to);
256 void TransferIndexedProperties(Handle<JSObject> from, Handle<JSObject> to);
257
kmillikin@chromium.org4111b802010-05-03 10:34:42 +0000258 enum PrototypePropertyMode {
259 DONT_ADD_PROTOTYPE,
260 ADD_READONLY_PROTOTYPE,
261 ADD_WRITEABLE_PROTOTYPE
262 };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000263 Handle<DescriptorArray> ComputeFunctionInstanceDescriptor(
kmillikin@chromium.org4111b802010-05-03 10:34:42 +0000264 PrototypePropertyMode prototypeMode);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000265 void MakeFunctionInstancePrototypeWritable();
266
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000267 static bool CompileBuiltin(int index);
268 static bool CompileNative(Vector<const char> name, Handle<String> source);
269 static bool CompileScriptCached(Vector<const char> name,
270 Handle<String> source,
271 SourceCodeCache* cache,
272 v8::Extension* extension,
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000273 Handle<Context> top_context,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000274 bool use_runtime_context);
275
276 Handle<Context> result_;
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000277 Handle<JSFunction> empty_function_;
278 BootstrapperActive active_;
279 friend class Bootstrapper;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000280};
281
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000282
283void Bootstrapper::Iterate(ObjectVisitor* v) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000284 extensions_cache.Iterate(v);
ager@chromium.org3811b432009-10-28 14:53:37 +0000285 v->Synchronize("Extensions");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000286}
287
288
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000289Handle<Context> Bootstrapper::CreateEnvironment(
290 Handle<Object> global_object,
291 v8::Handle<v8::ObjectTemplate> global_template,
292 v8::ExtensionConfiguration* extensions) {
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000293 HandleScope scope;
294 Handle<Context> env;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000295 Genesis genesis(global_object, global_template, extensions);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000296 env = genesis.result();
297 if (!env.is_null()) {
298 if (InstallExtensions(env, extensions)) {
299 return env;
300 }
301 }
302 return Handle<Context>();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000303}
304
305
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000306static void SetObjectPrototype(Handle<JSObject> object, Handle<Object> proto) {
307 // object.__proto__ = proto;
308 Handle<Map> old_to_map = Handle<Map>(object->map());
309 Handle<Map> new_to_map = Factory::CopyMapDropTransitions(old_to_map);
310 new_to_map->set_prototype(*proto);
311 object->set_map(*new_to_map);
312}
313
314
315void Bootstrapper::DetachGlobal(Handle<Context> env) {
316 JSGlobalProxy::cast(env->global_proxy())->set_context(*Factory::null_value());
317 SetObjectPrototype(Handle<JSObject>(env->global_proxy()),
318 Factory::null_value());
319 env->set_global_proxy(env->global());
320 env->global()->set_global_receiver(env->global());
321}
322
323
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +0000324void Bootstrapper::ReattachGlobal(Handle<Context> env,
325 Handle<Object> global_object) {
326 ASSERT(global_object->IsJSGlobalProxy());
327 Handle<JSGlobalProxy> global = Handle<JSGlobalProxy>::cast(global_object);
328 env->global()->set_global_receiver(*global);
329 env->set_global_proxy(*global);
330 SetObjectPrototype(global, Handle<JSObject>(env->global()));
331 global->set_context(*env);
332}
333
334
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000335static Handle<JSFunction> InstallFunction(Handle<JSObject> target,
336 const char* name,
337 InstanceType type,
338 int instance_size,
339 Handle<JSObject> prototype,
340 Builtins::Name call,
341 bool is_ecma_native) {
342 Handle<String> symbol = Factory::LookupAsciiSymbol(name);
343 Handle<Code> call_code = Handle<Code>(Builtins::builtin(call));
kmillikin@chromium.org4111b802010-05-03 10:34:42 +0000344 Handle<JSFunction> function = prototype.is_null() ?
345 Factory::NewFunctionWithoutPrototype(symbol, call_code) :
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000346 Factory::NewFunctionWithPrototype(symbol,
347 type,
348 instance_size,
349 prototype,
350 call_code,
351 is_ecma_native);
352 SetProperty(target, symbol, function, DONT_ENUM);
353 if (is_ecma_native) {
354 function->shared()->set_instance_class_name(*symbol);
355 }
356 return function;
357}
358
359
360Handle<DescriptorArray> Genesis::ComputeFunctionInstanceDescriptor(
kmillikin@chromium.org4111b802010-05-03 10:34:42 +0000361 PrototypePropertyMode prototypeMode) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000362 Handle<DescriptorArray> result = Factory::empty_descriptor_array();
363
kmillikin@chromium.org4111b802010-05-03 10:34:42 +0000364 if (prototypeMode != DONT_ADD_PROTOTYPE) {
365 PropertyAttributes attributes = static_cast<PropertyAttributes>(
366 DONT_ENUM |
367 DONT_DELETE |
368 (prototypeMode == ADD_READONLY_PROTOTYPE ? READ_ONLY : 0));
369 result =
370 Factory::CopyAppendProxyDescriptor(
371 result,
372 Factory::prototype_symbol(),
373 Factory::NewProxy(&Accessors::FunctionPrototype),
374 attributes);
375 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000376
kmillikin@chromium.org4111b802010-05-03 10:34:42 +0000377 PropertyAttributes attributes =
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000378 static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE | READ_ONLY);
379 // Add length.
380 result =
381 Factory::CopyAppendProxyDescriptor(
382 result,
383 Factory::length_symbol(),
384 Factory::NewProxy(&Accessors::FunctionLength),
385 attributes);
386
387 // Add name.
388 result =
389 Factory::CopyAppendProxyDescriptor(
390 result,
391 Factory::name_symbol(),
392 Factory::NewProxy(&Accessors::FunctionName),
393 attributes);
394
395 // Add arguments.
396 result =
397 Factory::CopyAppendProxyDescriptor(
398 result,
399 Factory::arguments_symbol(),
400 Factory::NewProxy(&Accessors::FunctionArguments),
401 attributes);
402
403 // Add caller.
404 result =
405 Factory::CopyAppendProxyDescriptor(
406 result,
407 Factory::caller_symbol(),
408 Factory::NewProxy(&Accessors::FunctionCaller),
409 attributes);
410
411 return result;
412}
413
414
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000415Handle<JSFunction> Genesis::CreateEmptyFunction() {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000416 // Allocate the map for function instances.
417 Handle<Map> fm = Factory::NewMap(JS_FUNCTION_TYPE, JSFunction::kSize);
418 global_context()->set_function_instance_map(*fm);
419 // Please note that the prototype property for function instances must be
420 // writable.
421 Handle<DescriptorArray> function_map_descriptors =
kmillikin@chromium.org4111b802010-05-03 10:34:42 +0000422 ComputeFunctionInstanceDescriptor(ADD_WRITEABLE_PROTOTYPE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000423 fm->set_instance_descriptors(*function_map_descriptors);
kmillikin@chromium.org4111b802010-05-03 10:34:42 +0000424 fm->set_function_with_prototype(true);
425
426 // Functions with this map will not have a 'prototype' property, and
427 // can not be used as constructors.
428 Handle<Map> function_without_prototype_map =
429 Factory::NewMap(JS_FUNCTION_TYPE, JSFunction::kSize);
430 global_context()->set_function_without_prototype_map(
431 *function_without_prototype_map);
432 Handle<DescriptorArray> function_without_prototype_map_descriptors =
433 ComputeFunctionInstanceDescriptor(DONT_ADD_PROTOTYPE);
434 function_without_prototype_map->set_instance_descriptors(
435 *function_without_prototype_map_descriptors);
436 function_without_prototype_map->set_function_with_prototype(false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000437
438 // Allocate the function map first and then patch the prototype later
439 fm = Factory::NewMap(JS_FUNCTION_TYPE, JSFunction::kSize);
440 global_context()->set_function_map(*fm);
kmillikin@chromium.org4111b802010-05-03 10:34:42 +0000441 function_map_descriptors =
442 ComputeFunctionInstanceDescriptor(ADD_READONLY_PROTOTYPE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000443 fm->set_instance_descriptors(*function_map_descriptors);
kmillikin@chromium.org4111b802010-05-03 10:34:42 +0000444 fm->set_function_with_prototype(true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000445
446 Handle<String> object_name = Handle<String>(Heap::Object_symbol());
447
448 { // --- O b j e c t ---
449 Handle<JSFunction> object_fun =
450 Factory::NewFunction(object_name, Factory::null_value());
451 Handle<Map> object_function_map =
452 Factory::NewMap(JS_OBJECT_TYPE, JSObject::kHeaderSize);
453 object_fun->set_initial_map(*object_function_map);
454 object_function_map->set_constructor(*object_fun);
455
456 global_context()->set_object_function(*object_fun);
457
458 // Allocate a new prototype for the object function.
459 Handle<JSObject> prototype = Factory::NewJSObject(Top::object_function(),
460 TENURED);
461
462 global_context()->set_initial_object_prototype(*prototype);
463 SetPrototype(object_fun, prototype);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000464 object_function_map->
465 set_instance_descriptors(Heap::empty_descriptor_array());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000466 }
467
468 // Allocate the empty function as the prototype for function ECMAScript
469 // 262 15.3.4.
470 Handle<String> symbol = Factory::LookupAsciiSymbol("Empty");
471 Handle<JSFunction> empty_function =
kmillikin@chromium.org4111b802010-05-03 10:34:42 +0000472 Factory::NewFunctionWithoutPrototype(symbol);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000473
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000474 // --- E m p t y ---
475 Handle<Code> code =
476 Handle<Code>(Builtins::builtin(Builtins::EmptyFunction));
477 empty_function->set_code(*code);
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000478 empty_function->shared()->set_code(*code);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000479 Handle<String> source = Factory::NewStringFromAscii(CStrVector("() {}"));
480 Handle<Script> script = Factory::NewScript(source);
481 script->set_type(Smi::FromInt(Script::TYPE_NATIVE));
482 empty_function->shared()->set_script(*script);
483 empty_function->shared()->set_start_position(0);
484 empty_function->shared()->set_end_position(source->length());
485 empty_function->shared()->DontAdaptArguments();
486 global_context()->function_map()->set_prototype(*empty_function);
487 global_context()->function_instance_map()->set_prototype(*empty_function);
kmillikin@chromium.org4111b802010-05-03 10:34:42 +0000488 global_context()->function_without_prototype_map()->
489 set_prototype(*empty_function);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000490
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000491 // Allocate the function map first and then patch the prototype later
kmillikin@chromium.org4111b802010-05-03 10:34:42 +0000492 Handle<Map> empty_fm = Factory::CopyMapDropDescriptors(
493 function_without_prototype_map);
494 empty_fm->set_instance_descriptors(
495 *function_without_prototype_map_descriptors);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000496 empty_fm->set_prototype(global_context()->object_function()->prototype());
497 empty_function->set_map(*empty_fm);
498 return empty_function;
499}
500
501
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000502static void AddToWeakGlobalContextList(Context* context) {
503 ASSERT(context->IsGlobalContext());
504#ifdef DEBUG
505 { // NOLINT
506 ASSERT(context->get(Context::NEXT_CONTEXT_LINK)->IsUndefined());
507 // Check that context is not in the list yet.
508 for (Object* current = Heap::global_contexts_list();
509 !current->IsUndefined();
510 current = Context::cast(current)->get(Context::NEXT_CONTEXT_LINK)) {
511 ASSERT(current != context);
512 }
513 }
514#endif
515 context->set(Context::NEXT_CONTEXT_LINK, Heap::global_contexts_list());
516 Heap::set_global_contexts_list(context);
517}
518
519
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000520void Genesis::CreateRoots() {
521 // Allocate the global context FixedArray first and then patch the
522 // closure and extension object later (we need the empty function
523 // and the global object, but in order to create those, we need the
524 // global context).
525 global_context_ =
526 Handle<Context>::cast(
527 GlobalHandles::Create(*Factory::NewGlobalContext()));
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000528 AddToWeakGlobalContextList(*global_context_);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000529 Top::set_context(*global_context());
530
531 // Allocate the message listeners object.
532 {
533 v8::NeanderArray listeners;
534 global_context()->set_message_listeners(*listeners.value());
535 }
536}
537
538
539Handle<JSGlobalProxy> Genesis::CreateNewGlobals(
540 v8::Handle<v8::ObjectTemplate> global_template,
541 Handle<Object> global_object,
542 Handle<GlobalObject>* inner_global_out) {
543 // The argument global_template aka data is an ObjectTemplateInfo.
544 // It has a constructor pointer that points at global_constructor which is a
545 // FunctionTemplateInfo.
546 // The global_constructor is used to create or reinitialize the global_proxy.
547 // The global_constructor also has a prototype_template pointer that points at
548 // js_global_template which is an ObjectTemplateInfo.
549 // That in turn has a constructor pointer that points at
550 // js_global_constructor which is a FunctionTemplateInfo.
551 // js_global_constructor is used to make js_global_function
552 // js_global_function is used to make the new inner_global.
553 //
554 // --- G l o b a l ---
555 // Step 1: Create a fresh inner JSGlobalObject.
556 Handle<JSFunction> js_global_function;
557 Handle<ObjectTemplateInfo> js_global_template;
558 if (!global_template.IsEmpty()) {
559 // Get prototype template of the global_template.
560 Handle<ObjectTemplateInfo> data =
561 v8::Utils::OpenHandle(*global_template);
562 Handle<FunctionTemplateInfo> global_constructor =
563 Handle<FunctionTemplateInfo>(
564 FunctionTemplateInfo::cast(data->constructor()));
565 Handle<Object> proto_template(global_constructor->prototype_template());
566 if (!proto_template->IsUndefined()) {
567 js_global_template =
568 Handle<ObjectTemplateInfo>::cast(proto_template);
569 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000570 }
571
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000572 if (js_global_template.is_null()) {
573 Handle<String> name = Handle<String>(Heap::empty_symbol());
574 Handle<Code> code = Handle<Code>(Builtins::builtin(Builtins::Illegal));
575 js_global_function =
576 Factory::NewFunction(name, JS_GLOBAL_OBJECT_TYPE,
577 JSGlobalObject::kSize, code, true);
578 // Change the constructor property of the prototype of the
579 // hidden global function to refer to the Object function.
580 Handle<JSObject> prototype =
581 Handle<JSObject>(
582 JSObject::cast(js_global_function->instance_prototype()));
583 SetProperty(prototype, Factory::constructor_symbol(),
584 Top::object_function(), NONE);
585 } else {
586 Handle<FunctionTemplateInfo> js_global_constructor(
587 FunctionTemplateInfo::cast(js_global_template->constructor()));
588 js_global_function =
589 Factory::CreateApiFunction(js_global_constructor,
590 Factory::InnerGlobalObject);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000591 }
592
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000593 js_global_function->initial_map()->set_is_hidden_prototype();
594 Handle<GlobalObject> inner_global =
595 Factory::NewGlobalObject(js_global_function);
596 if (inner_global_out != NULL) {
597 *inner_global_out = inner_global;
598 }
599
600 // Step 2: create or re-initialize the global proxy object.
601 Handle<JSFunction> global_proxy_function;
602 if (global_template.IsEmpty()) {
603 Handle<String> name = Handle<String>(Heap::empty_symbol());
604 Handle<Code> code = Handle<Code>(Builtins::builtin(Builtins::Illegal));
605 global_proxy_function =
606 Factory::NewFunction(name, JS_GLOBAL_PROXY_TYPE,
607 JSGlobalProxy::kSize, code, true);
608 } else {
609 Handle<ObjectTemplateInfo> data =
610 v8::Utils::OpenHandle(*global_template);
611 Handle<FunctionTemplateInfo> global_constructor(
612 FunctionTemplateInfo::cast(data->constructor()));
613 global_proxy_function =
614 Factory::CreateApiFunction(global_constructor,
615 Factory::OuterGlobalObject);
616 }
617
618 Handle<String> global_name = Factory::LookupAsciiSymbol("global");
619 global_proxy_function->shared()->set_instance_class_name(*global_name);
620 global_proxy_function->initial_map()->set_is_access_check_needed(true);
621
622 // Set global_proxy.__proto__ to js_global after ConfigureGlobalObjects
623 // Return the global proxy.
624
625 if (global_object.location() != NULL) {
626 ASSERT(global_object->IsJSGlobalProxy());
627 return ReinitializeJSGlobalProxy(
628 global_proxy_function,
629 Handle<JSGlobalProxy>::cast(global_object));
630 } else {
631 return Handle<JSGlobalProxy>::cast(
632 Factory::NewJSObject(global_proxy_function, TENURED));
633 }
634}
635
636
637void Genesis::HookUpGlobalProxy(Handle<GlobalObject> inner_global,
638 Handle<JSGlobalProxy> global_proxy) {
639 // Set the global context for the global object.
640 inner_global->set_global_context(*global_context());
641 inner_global->set_global_receiver(*global_proxy);
642 global_proxy->set_context(*global_context());
643 global_context()->set_global_proxy(*global_proxy);
644}
645
646
647void Genesis::HookUpInnerGlobal(Handle<GlobalObject> inner_global) {
648 Handle<GlobalObject> inner_global_from_snapshot(
649 GlobalObject::cast(global_context_->extension()));
650 Handle<JSBuiltinsObject> builtins_global(global_context_->builtins());
651 global_context_->set_extension(*inner_global);
652 global_context_->set_global(*inner_global);
653 global_context_->set_security_token(*inner_global);
654 static const PropertyAttributes attributes =
655 static_cast<PropertyAttributes>(READ_ONLY | DONT_DELETE);
656 ForceSetProperty(builtins_global,
657 Factory::LookupAsciiSymbol("global"),
658 inner_global,
659 attributes);
660 // Setup the reference from the global object to the builtins object.
661 JSGlobalObject::cast(*inner_global)->set_builtins(*builtins_global);
662 TransferNamedProperties(inner_global_from_snapshot, inner_global);
663 TransferIndexedProperties(inner_global_from_snapshot, inner_global);
664}
665
666
667// This is only called if we are not using snapshots. The equivalent
668// work in the snapshot case is done in HookUpInnerGlobal.
669void Genesis::InitializeGlobal(Handle<GlobalObject> inner_global,
670 Handle<JSFunction> empty_function) {
671 // --- G l o b a l C o n t e x t ---
672 // Use the empty function as closure (no scope info).
673 global_context()->set_closure(*empty_function);
674 global_context()->set_fcontext(*global_context());
675 global_context()->set_previous(NULL);
676 // Set extension and global object.
677 global_context()->set_extension(*inner_global);
678 global_context()->set_global(*inner_global);
679 // Security setup: Set the security token of the global object to
680 // its the inner global. This makes the security check between two
681 // different contexts fail by default even in case of global
682 // object reinitialization.
683 global_context()->set_security_token(*inner_global);
684
685 Handle<String> object_name = Handle<String>(Heap::Object_symbol());
686 SetProperty(inner_global, object_name, Top::object_function(), DONT_ENUM);
687
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000688 Handle<JSObject> global = Handle<JSObject>(global_context()->global());
689
690 // Install global Function object
691 InstallFunction(global, "Function", JS_FUNCTION_TYPE, JSFunction::kSize,
692 empty_function, Builtins::Illegal, true); // ECMA native.
693
694 { // --- A r r a y ---
695 Handle<JSFunction> array_function =
696 InstallFunction(global, "Array", JS_ARRAY_TYPE, JSArray::kSize,
697 Top::initial_object_prototype(), Builtins::ArrayCode,
698 true);
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +0000699 array_function->shared()->set_construct_stub(
700 Builtins::builtin(Builtins::ArrayConstructCode));
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000701 array_function->shared()->DontAdaptArguments();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000702
703 // This seems a bit hackish, but we need to make sure Array.length
704 // is 1.
705 array_function->shared()->set_length(1);
706 Handle<DescriptorArray> array_descriptors =
707 Factory::CopyAppendProxyDescriptor(
708 Factory::empty_descriptor_array(),
709 Factory::length_symbol(),
710 Factory::NewProxy(&Accessors::ArrayLength),
711 static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE));
712
713 // Cache the fast JavaScript array map
714 global_context()->set_js_array_map(array_function->initial_map());
715 global_context()->js_array_map()->set_instance_descriptors(
716 *array_descriptors);
717 // array_function is used internally. JS code creating array object should
718 // search for the 'Array' property on the global object and use that one
719 // as the constructor. 'Array' property on a global object can be
720 // overwritten by JS code.
721 global_context()->set_array_function(*array_function);
722 }
723
724 { // --- N u m b e r ---
725 Handle<JSFunction> number_fun =
726 InstallFunction(global, "Number", JS_VALUE_TYPE, JSValue::kSize,
727 Top::initial_object_prototype(), Builtins::Illegal,
728 true);
729 global_context()->set_number_function(*number_fun);
730 }
731
732 { // --- B o o l e a n ---
733 Handle<JSFunction> boolean_fun =
734 InstallFunction(global, "Boolean", JS_VALUE_TYPE, JSValue::kSize,
735 Top::initial_object_prototype(), Builtins::Illegal,
736 true);
737 global_context()->set_boolean_function(*boolean_fun);
738 }
739
740 { // --- S t r i n g ---
741 Handle<JSFunction> string_fun =
742 InstallFunction(global, "String", JS_VALUE_TYPE, JSValue::kSize,
743 Top::initial_object_prototype(), Builtins::Illegal,
744 true);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000745 string_fun->shared()->set_construct_stub(
746 Builtins::builtin(Builtins::StringConstructCode));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000747 global_context()->set_string_function(*string_fun);
748 // Add 'length' property to strings.
749 Handle<DescriptorArray> string_descriptors =
750 Factory::CopyAppendProxyDescriptor(
751 Factory::empty_descriptor_array(),
752 Factory::length_symbol(),
753 Factory::NewProxy(&Accessors::StringLength),
754 static_cast<PropertyAttributes>(DONT_ENUM |
755 DONT_DELETE |
756 READ_ONLY));
757
758 Handle<Map> string_map =
759 Handle<Map>(global_context()->string_function()->initial_map());
760 string_map->set_instance_descriptors(*string_descriptors);
761 }
762
763 { // --- D a t e ---
764 // Builtin functions for Date.prototype.
765 Handle<JSFunction> date_fun =
766 InstallFunction(global, "Date", JS_VALUE_TYPE, JSValue::kSize,
767 Top::initial_object_prototype(), Builtins::Illegal,
768 true);
769
770 global_context()->set_date_function(*date_fun);
771 }
772
773
774 { // -- R e g E x p
775 // Builtin functions for RegExp.prototype.
776 Handle<JSFunction> regexp_fun =
ager@chromium.org236ad962008-09-25 09:45:57 +0000777 InstallFunction(global, "RegExp", JS_REGEXP_TYPE, JSRegExp::kSize,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000778 Top::initial_object_prototype(), Builtins::Illegal,
779 true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000780 global_context()->set_regexp_function(*regexp_fun);
lrn@chromium.org25156de2010-04-06 13:10:27 +0000781
782 ASSERT(regexp_fun->has_initial_map());
783 Handle<Map> initial_map(regexp_fun->initial_map());
784
785 ASSERT_EQ(0, initial_map->inobject_properties());
786
787 Handle<DescriptorArray> descriptors = Factory::NewDescriptorArray(5);
788 PropertyAttributes final =
789 static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE | READ_ONLY);
790 int enum_index = 0;
791 {
792 // ECMA-262, section 15.10.7.1.
793 FieldDescriptor field(Heap::source_symbol(),
794 JSRegExp::kSourceFieldIndex,
795 final,
796 enum_index++);
797 descriptors->Set(0, &field);
798 }
799 {
800 // ECMA-262, section 15.10.7.2.
801 FieldDescriptor field(Heap::global_symbol(),
802 JSRegExp::kGlobalFieldIndex,
803 final,
804 enum_index++);
805 descriptors->Set(1, &field);
806 }
807 {
808 // ECMA-262, section 15.10.7.3.
809 FieldDescriptor field(Heap::ignore_case_symbol(),
810 JSRegExp::kIgnoreCaseFieldIndex,
811 final,
812 enum_index++);
813 descriptors->Set(2, &field);
814 }
815 {
816 // ECMA-262, section 15.10.7.4.
817 FieldDescriptor field(Heap::multiline_symbol(),
818 JSRegExp::kMultilineFieldIndex,
819 final,
820 enum_index++);
821 descriptors->Set(3, &field);
822 }
823 {
824 // ECMA-262, section 15.10.7.5.
825 PropertyAttributes writable =
826 static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE);
827 FieldDescriptor field(Heap::last_index_symbol(),
828 JSRegExp::kLastIndexFieldIndex,
829 writable,
830 enum_index++);
831 descriptors->Set(4, &field);
832 }
833 descriptors->SetNextEnumerationIndex(enum_index);
834 descriptors->Sort();
835
836 initial_map->set_inobject_properties(5);
837 initial_map->set_pre_allocated_property_fields(5);
838 initial_map->set_unused_property_fields(0);
839 initial_map->set_instance_size(
840 initial_map->instance_size() + 5 * kPointerSize);
841 initial_map->set_instance_descriptors(*descriptors);
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000842 initial_map->set_visitor_id(StaticVisitorBase::GetVisitorId(*initial_map));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000843 }
844
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000845 { // -- J S O N
846 Handle<String> name = Factory::NewStringFromAscii(CStrVector("JSON"));
847 Handle<JSFunction> cons = Factory::NewFunction(
848 name,
849 Factory::the_hole_value());
850 cons->SetInstancePrototype(global_context()->initial_object_prototype());
851 cons->SetInstanceClassName(*name);
852 Handle<JSObject> json_object = Factory::NewJSObject(cons, TENURED);
853 ASSERT(json_object->IsJSObject());
854 SetProperty(global, name, json_object, DONT_ENUM);
855 global_context()->set_json_object(*json_object);
856 }
857
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000858 { // --- arguments_boilerplate_
859 // Make sure we can recognize argument objects at runtime.
860 // This is done by introducing an anonymous function with
861 // class_name equals 'Arguments'.
862 Handle<String> symbol = Factory::LookupAsciiSymbol("Arguments");
863 Handle<Code> code = Handle<Code>(Builtins::builtin(Builtins::Illegal));
864 Handle<JSObject> prototype =
865 Handle<JSObject>(
866 JSObject::cast(global_context()->object_function()->prototype()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000867
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000868 Handle<JSFunction> function =
869 Factory::NewFunctionWithPrototype(symbol,
870 JS_OBJECT_TYPE,
871 JSObject::kHeaderSize,
872 prototype,
873 code,
874 false);
875 ASSERT(!function->has_initial_map());
876 function->shared()->set_instance_class_name(*symbol);
877 function->shared()->set_expected_nof_properties(2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000878 Handle<JSObject> result = Factory::NewJSObject(function);
879
880 global_context()->set_arguments_boilerplate(*result);
881 // Note: callee must be added as the first property and
v8.team.kasperl727e9952008-09-02 14:56:44 +0000882 // length must be added as the second property.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000883 SetProperty(result, Factory::callee_symbol(),
884 Factory::undefined_value(),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000885 DONT_ENUM);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000886 SetProperty(result, Factory::length_symbol(),
887 Factory::undefined_value(),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000888 DONT_ENUM);
889
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000890#ifdef DEBUG
891 LookupResult lookup;
892 result->LocalLookup(Heap::callee_symbol(), &lookup);
ager@chromium.org5c838252010-02-19 08:53:10 +0000893 ASSERT(lookup.IsProperty() && (lookup.type() == FIELD));
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000894 ASSERT(lookup.GetFieldIndex() == Heap::arguments_callee_index);
895
896 result->LocalLookup(Heap::length_symbol(), &lookup);
ager@chromium.org5c838252010-02-19 08:53:10 +0000897 ASSERT(lookup.IsProperty() && (lookup.type() == FIELD));
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000898 ASSERT(lookup.GetFieldIndex() == Heap::arguments_length_index);
899
900 ASSERT(result->map()->inobject_properties() > Heap::arguments_callee_index);
901 ASSERT(result->map()->inobject_properties() > Heap::arguments_length_index);
902
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000903 // Check the state of the object.
904 ASSERT(result->HasFastProperties());
905 ASSERT(result->HasFastElements());
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000906#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000907 }
908
909 { // --- context extension
910 // Create a function for the context extension objects.
911 Handle<Code> code = Handle<Code>(Builtins::builtin(Builtins::Illegal));
912 Handle<JSFunction> context_extension_fun =
ager@chromium.org32912102009-01-16 10:38:43 +0000913 Factory::NewFunction(Factory::empty_symbol(),
914 JS_CONTEXT_EXTENSION_OBJECT_TYPE,
915 JSObject::kHeaderSize,
916 code,
917 true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000918
919 Handle<String> name = Factory::LookupAsciiSymbol("context_extension");
920 context_extension_fun->shared()->set_instance_class_name(*name);
921 global_context()->set_context_extension_function(*context_extension_fun);
922 }
923
sgjesse@chromium.org05521fc2009-05-21 07:37:44 +0000924
925 {
926 // Setup the call-as-function delegate.
927 Handle<Code> code =
928 Handle<Code>(Builtins::builtin(Builtins::HandleApiCallAsFunction));
929 Handle<JSFunction> delegate =
930 Factory::NewFunction(Factory::empty_symbol(), JS_OBJECT_TYPE,
931 JSObject::kHeaderSize, code, true);
932 global_context()->set_call_as_function_delegate(*delegate);
933 delegate->shared()->DontAdaptArguments();
934 }
935
936 {
937 // Setup the call-as-constructor delegate.
938 Handle<Code> code =
939 Handle<Code>(Builtins::builtin(Builtins::HandleApiCallAsConstructor));
940 Handle<JSFunction> delegate =
941 Factory::NewFunction(Factory::empty_symbol(), JS_OBJECT_TYPE,
942 JSObject::kHeaderSize, code, true);
943 global_context()->set_call_as_constructor_delegate(*delegate);
944 delegate->shared()->DontAdaptArguments();
945 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000946
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000947 // Initialize the out of memory slot.
948 global_context()->set_out_of_memory(Heap::false_value());
ager@chromium.org9085a012009-05-11 19:22:57 +0000949
950 // Initialize the data slot.
951 global_context()->set_data(Heap::undefined_value());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000952}
953
954
955bool Genesis::CompileBuiltin(int index) {
956 Vector<const char> name = Natives::GetScriptName(index);
957 Handle<String> source_code = Bootstrapper::NativesSourceLookup(index);
958 return CompileNative(name, source_code);
959}
960
961
962bool Genesis::CompileNative(Vector<const char> name, Handle<String> source) {
963 HandleScope scope;
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000964#ifdef ENABLE_DEBUGGER_SUPPORT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000965 Debugger::set_compiling_natives(true);
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000966#endif
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000967 bool result = CompileScriptCached(name,
968 source,
969 NULL,
970 NULL,
971 Handle<Context>(Top::context()),
972 true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000973 ASSERT(Top::has_pending_exception() != result);
974 if (!result) Top::clear_pending_exception();
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000975#ifdef ENABLE_DEBUGGER_SUPPORT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000976 Debugger::set_compiling_natives(false);
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000977#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000978 return result;
979}
980
981
982bool Genesis::CompileScriptCached(Vector<const char> name,
983 Handle<String> source,
984 SourceCodeCache* cache,
985 v8::Extension* extension,
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000986 Handle<Context> top_context,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000987 bool use_runtime_context) {
988 HandleScope scope;
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000989 Handle<SharedFunctionInfo> function_info;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000990
991 // If we can't find the function in the cache, we compile a new
992 // function and insert it into the cache.
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000993 if (cache == NULL || !cache->Lookup(name, &function_info)) {
ager@chromium.org5ec48922009-05-05 07:25:34 +0000994 ASSERT(source->IsAsciiRepresentation());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000995 Handle<String> script_name = Factory::NewStringFromUtf8(name);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000996 function_info = Compiler::Compile(
997 source,
998 script_name,
999 0,
1000 0,
1001 extension,
1002 NULL,
1003 Handle<String>::null(),
1004 use_runtime_context ? NATIVES_CODE : NOT_NATIVES_CODE);
1005 if (function_info.is_null()) return false;
1006 if (cache != NULL) cache->Add(name, function_info);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001007 }
1008
1009 // Setup the function context. Conceptually, we should clone the
1010 // function before overwriting the context but since we're in a
1011 // single-threaded environment it is not strictly necessary.
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001012 ASSERT(top_context->IsGlobalContext());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001013 Handle<Context> context =
1014 Handle<Context>(use_runtime_context
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001015 ? Handle<Context>(top_context->runtime_context())
1016 : top_context);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001017 Handle<JSFunction> fun =
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001018 Factory::NewFunctionFromSharedFunctionInfo(function_info, context);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001019
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001020 // Call function using either the runtime object or the global
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001021 // object as the receiver. Provide no parameters.
1022 Handle<Object> receiver =
1023 Handle<Object>(use_runtime_context
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001024 ? top_context->builtins()
1025 : top_context->global());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001026 bool has_pending_exception;
1027 Handle<Object> result =
1028 Execution::Call(fun, receiver, 0, NULL, &has_pending_exception);
1029 if (has_pending_exception) return false;
ager@chromium.org5c838252010-02-19 08:53:10 +00001030 return true;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001031}
1032
1033
lrn@chromium.org303ada72010-10-27 09:33:13 +00001034#define INSTALL_NATIVE(Type, name, var) \
1035 Handle<String> var##_name = Factory::LookupAsciiSymbol(name); \
1036 global_context()->set_##var(Type::cast( \
1037 global_context()->builtins()->GetPropertyNoExceptionThrown(*var##_name)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001038
1039void Genesis::InstallNativeFunctions() {
1040 HandleScope scope;
1041 INSTALL_NATIVE(JSFunction, "CreateDate", create_date_fun);
1042 INSTALL_NATIVE(JSFunction, "ToNumber", to_number_fun);
1043 INSTALL_NATIVE(JSFunction, "ToString", to_string_fun);
1044 INSTALL_NATIVE(JSFunction, "ToDetailString", to_detail_string_fun);
1045 INSTALL_NATIVE(JSFunction, "ToObject", to_object_fun);
1046 INSTALL_NATIVE(JSFunction, "ToInteger", to_integer_fun);
1047 INSTALL_NATIVE(JSFunction, "ToUint32", to_uint32_fun);
1048 INSTALL_NATIVE(JSFunction, "ToInt32", to_int32_fun);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00001049 INSTALL_NATIVE(JSFunction, "GlobalEval", global_eval_fun);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001050 INSTALL_NATIVE(JSFunction, "Instantiate", instantiate_fun);
1051 INSTALL_NATIVE(JSFunction, "ConfigureTemplateInstance",
1052 configure_instance_fun);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001053 INSTALL_NATIVE(JSFunction, "GetStackTraceLine", get_stack_trace_line_fun);
1054 INSTALL_NATIVE(JSObject, "functionCache", function_cache);
1055}
1056
1057#undef INSTALL_NATIVE
1058
1059
1060bool Genesis::InstallNatives() {
1061 HandleScope scope;
1062
1063 // Create a function for the builtins object. Allocate space for the
1064 // JavaScript builtins, a reference to the builtins object
1065 // (itself) and a reference to the global_context directly in the object.
1066 Handle<Code> code = Handle<Code>(Builtins::builtin(Builtins::Illegal));
1067 Handle<JSFunction> builtins_fun =
1068 Factory::NewFunction(Factory::empty_symbol(), JS_BUILTINS_OBJECT_TYPE,
1069 JSBuiltinsObject::kSize, code, true);
1070
1071 Handle<String> name = Factory::LookupAsciiSymbol("builtins");
1072 builtins_fun->shared()->set_instance_class_name(*name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001073
1074 // Allocate the builtins object.
1075 Handle<JSBuiltinsObject> builtins =
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001076 Handle<JSBuiltinsObject>::cast(Factory::NewGlobalObject(builtins_fun));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001077 builtins->set_builtins(*builtins);
1078 builtins->set_global_context(*global_context());
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001079 builtins->set_global_receiver(*builtins);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001080
1081 // Setup the 'global' properties of the builtins object. The
1082 // 'global' property that refers to the global object is the only
1083 // way to get from code running in the builtins context to the
1084 // global object.
1085 static const PropertyAttributes attributes =
1086 static_cast<PropertyAttributes>(READ_ONLY | DONT_DELETE);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001087 Handle<String> global_symbol = Factory::LookupAsciiSymbol("global");
1088 SetProperty(builtins,
1089 global_symbol,
1090 Handle<Object>(global_context()->global()),
1091 attributes);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001092
1093 // Setup the reference from the global object to the builtins object.
1094 JSGlobalObject::cast(global_context()->global())->set_builtins(*builtins);
1095
1096 // Create a bridge function that has context in the global context.
1097 Handle<JSFunction> bridge =
1098 Factory::NewFunction(Factory::empty_symbol(), Factory::undefined_value());
1099 ASSERT(bridge->context() == *Top::global_context());
1100
1101 // Allocate the builtins context.
1102 Handle<Context> context =
1103 Factory::NewFunctionContext(Context::MIN_CONTEXT_SLOTS, bridge);
1104 context->set_global(*builtins); // override builtins global object
1105
1106 global_context()->set_runtime_context(*context);
1107
1108 { // -- S c r i p t
1109 // Builtin functions for Script.
1110 Handle<JSFunction> script_fun =
1111 InstallFunction(builtins, "Script", JS_VALUE_TYPE, JSValue::kSize,
1112 Top::initial_object_prototype(), Builtins::Illegal,
1113 false);
1114 Handle<JSObject> prototype =
1115 Factory::NewJSObject(Top::object_function(), TENURED);
1116 SetPrototype(script_fun, prototype);
1117 global_context()->set_script_function(*script_fun);
1118
1119 // Add 'source' and 'data' property to scripts.
1120 PropertyAttributes common_attributes =
1121 static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE | READ_ONLY);
1122 Handle<Proxy> proxy_source = Factory::NewProxy(&Accessors::ScriptSource);
1123 Handle<DescriptorArray> script_descriptors =
1124 Factory::CopyAppendProxyDescriptor(
1125 Factory::empty_descriptor_array(),
1126 Factory::LookupAsciiSymbol("source"),
1127 proxy_source,
1128 common_attributes);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001129 Handle<Proxy> proxy_name = Factory::NewProxy(&Accessors::ScriptName);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001130 script_descriptors =
1131 Factory::CopyAppendProxyDescriptor(
1132 script_descriptors,
1133 Factory::LookupAsciiSymbol("name"),
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001134 proxy_name,
1135 common_attributes);
1136 Handle<Proxy> proxy_id = Factory::NewProxy(&Accessors::ScriptId);
1137 script_descriptors =
1138 Factory::CopyAppendProxyDescriptor(
1139 script_descriptors,
1140 Factory::LookupAsciiSymbol("id"),
1141 proxy_id,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001142 common_attributes);
1143 Handle<Proxy> proxy_line_offset =
1144 Factory::NewProxy(&Accessors::ScriptLineOffset);
1145 script_descriptors =
1146 Factory::CopyAppendProxyDescriptor(
1147 script_descriptors,
1148 Factory::LookupAsciiSymbol("line_offset"),
1149 proxy_line_offset,
1150 common_attributes);
1151 Handle<Proxy> proxy_column_offset =
1152 Factory::NewProxy(&Accessors::ScriptColumnOffset);
1153 script_descriptors =
1154 Factory::CopyAppendProxyDescriptor(
1155 script_descriptors,
1156 Factory::LookupAsciiSymbol("column_offset"),
1157 proxy_column_offset,
1158 common_attributes);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001159 Handle<Proxy> proxy_data = Factory::NewProxy(&Accessors::ScriptData);
1160 script_descriptors =
1161 Factory::CopyAppendProxyDescriptor(
1162 script_descriptors,
1163 Factory::LookupAsciiSymbol("data"),
1164 proxy_data,
1165 common_attributes);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001166 Handle<Proxy> proxy_type = Factory::NewProxy(&Accessors::ScriptType);
1167 script_descriptors =
1168 Factory::CopyAppendProxyDescriptor(
1169 script_descriptors,
1170 Factory::LookupAsciiSymbol("type"),
1171 proxy_type,
1172 common_attributes);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001173 Handle<Proxy> proxy_compilation_type =
1174 Factory::NewProxy(&Accessors::ScriptCompilationType);
1175 script_descriptors =
1176 Factory::CopyAppendProxyDescriptor(
1177 script_descriptors,
1178 Factory::LookupAsciiSymbol("compilation_type"),
1179 proxy_compilation_type,
1180 common_attributes);
iposva@chromium.org245aa852009-02-10 00:49:54 +00001181 Handle<Proxy> proxy_line_ends =
1182 Factory::NewProxy(&Accessors::ScriptLineEnds);
1183 script_descriptors =
1184 Factory::CopyAppendProxyDescriptor(
1185 script_descriptors,
1186 Factory::LookupAsciiSymbol("line_ends"),
1187 proxy_line_ends,
1188 common_attributes);
ager@chromium.org9085a012009-05-11 19:22:57 +00001189 Handle<Proxy> proxy_context_data =
1190 Factory::NewProxy(&Accessors::ScriptContextData);
1191 script_descriptors =
1192 Factory::CopyAppendProxyDescriptor(
1193 script_descriptors,
1194 Factory::LookupAsciiSymbol("context_data"),
1195 proxy_context_data,
1196 common_attributes);
sgjesse@chromium.org98180592009-12-02 08:17:28 +00001197 Handle<Proxy> proxy_eval_from_script =
1198 Factory::NewProxy(&Accessors::ScriptEvalFromScript);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001199 script_descriptors =
1200 Factory::CopyAppendProxyDescriptor(
1201 script_descriptors,
sgjesse@chromium.org98180592009-12-02 08:17:28 +00001202 Factory::LookupAsciiSymbol("eval_from_script"),
1203 proxy_eval_from_script,
ager@chromium.orge2902be2009-06-08 12:21:35 +00001204 common_attributes);
sgjesse@chromium.org98180592009-12-02 08:17:28 +00001205 Handle<Proxy> proxy_eval_from_script_position =
1206 Factory::NewProxy(&Accessors::ScriptEvalFromScriptPosition);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001207 script_descriptors =
1208 Factory::CopyAppendProxyDescriptor(
1209 script_descriptors,
sgjesse@chromium.org98180592009-12-02 08:17:28 +00001210 Factory::LookupAsciiSymbol("eval_from_script_position"),
1211 proxy_eval_from_script_position,
1212 common_attributes);
1213 Handle<Proxy> proxy_eval_from_function_name =
1214 Factory::NewProxy(&Accessors::ScriptEvalFromFunctionName);
1215 script_descriptors =
1216 Factory::CopyAppendProxyDescriptor(
1217 script_descriptors,
1218 Factory::LookupAsciiSymbol("eval_from_function_name"),
1219 proxy_eval_from_function_name,
ager@chromium.orge2902be2009-06-08 12:21:35 +00001220 common_attributes);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001221
1222 Handle<Map> script_map = Handle<Map>(script_fun->initial_map());
1223 script_map->set_instance_descriptors(*script_descriptors);
1224
1225 // Allocate the empty script.
1226 Handle<Script> script = Factory::NewScript(Factory::empty_string());
ager@chromium.orge2902be2009-06-08 12:21:35 +00001227 script->set_type(Smi::FromInt(Script::TYPE_NATIVE));
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001228 Heap::public_set_empty_script(*script);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001229 }
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001230 {
1231 // Builtin function for OpaqueReference -- a JSValue-based object,
1232 // that keeps its field isolated from JavaScript code. It may store
1233 // objects, that JavaScript code may not access.
1234 Handle<JSFunction> opaque_reference_fun =
1235 InstallFunction(builtins, "OpaqueReference", JS_VALUE_TYPE,
1236 JSValue::kSize, Top::initial_object_prototype(),
1237 Builtins::Illegal, false);
1238 Handle<JSObject> prototype =
1239 Factory::NewJSObject(Top::object_function(), TENURED);
1240 SetPrototype(opaque_reference_fun, prototype);
1241 global_context()->set_opaque_reference_function(*opaque_reference_fun);
1242 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001243
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001244 if (FLAG_disable_native_files) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001245 PrintF("Warning: Running without installed natives!\n");
1246 return true;
1247 }
1248
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001249 // Install natives.
1250 for (int i = Natives::GetDebuggerCount();
1251 i < Natives::GetBuiltinsCount();
1252 i++) {
1253 Vector<const char> name = Natives::GetScriptName(i);
1254 if (!CompileBuiltin(i)) return false;
1255 // TODO(ager): We really only need to install the JS builtin
1256 // functions on the builtins object after compiling and running
1257 // runtime.js.
1258 if (!InstallJSBuiltins(builtins)) return false;
1259 }
1260
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001261 InstallNativeFunctions();
1262
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00001263 // Store the map for the string prototype after the natives has been compiled
1264 // and the String function has been setup.
1265 Handle<JSFunction> string_function(global_context()->string_function());
1266 ASSERT(JSObject::cast(
1267 string_function->initial_map()->prototype())->HasFastProperties());
1268 global_context()->set_string_function_prototype_map(
1269 HeapObject::cast(string_function->initial_map()->prototype())->map());
1270
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001271 InstallBuiltinFunctionIds();
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001272
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001273 // Install Function.prototype.call and apply.
1274 { Handle<String> key = Factory::function_class_symbol();
1275 Handle<JSFunction> function =
1276 Handle<JSFunction>::cast(GetProperty(Top::global(), key));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001277 Handle<JSObject> proto =
1278 Handle<JSObject>(JSObject::cast(function->instance_prototype()));
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001279
1280 // Install the call and the apply functions.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001281 Handle<JSFunction> call =
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001282 InstallFunction(proto, "call", JS_OBJECT_TYPE, JSObject::kHeaderSize,
kmillikin@chromium.org4111b802010-05-03 10:34:42 +00001283 Handle<JSObject>::null(),
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001284 Builtins::FunctionCall,
1285 false);
1286 Handle<JSFunction> apply =
1287 InstallFunction(proto, "apply", JS_OBJECT_TYPE, JSObject::kHeaderSize,
kmillikin@chromium.org4111b802010-05-03 10:34:42 +00001288 Handle<JSObject>::null(),
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001289 Builtins::FunctionApply,
1290 false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001291
1292 // Make sure that Function.prototype.call appears to be compiled.
1293 // The code will never be called, but inline caching for call will
1294 // only work if it appears to be compiled.
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001295 call->shared()->DontAdaptArguments();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001296 ASSERT(call->is_compiled());
1297
ager@chromium.org32912102009-01-16 10:38:43 +00001298 // Set the expected parameters for apply to 2; required by builtin.
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001299 apply->shared()->set_formal_parameter_count(2);
1300
1301 // Set the lengths for the functions to satisfy ECMA-262.
1302 call->shared()->set_length(1);
1303 apply->shared()->set_length(2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001304 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001305
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00001306 // Create a constructor for RegExp results (a variant of Array that
1307 // predefines the two properties index and match).
1308 {
1309 // RegExpResult initial map.
1310
1311 // Find global.Array.prototype to inherit from.
1312 Handle<JSFunction> array_constructor(global_context()->array_function());
1313 Handle<JSObject> array_prototype(
1314 JSObject::cast(array_constructor->instance_prototype()));
1315
1316 // Add initial map.
1317 Handle<Map> initial_map =
1318 Factory::NewMap(JS_ARRAY_TYPE, JSRegExpResult::kSize);
1319 initial_map->set_constructor(*array_constructor);
1320
1321 // Set prototype on map.
1322 initial_map->set_non_instance_prototype(false);
1323 initial_map->set_prototype(*array_prototype);
1324
1325 // Update map with length accessor from Array and add "index" and "input".
1326 Handle<Map> array_map(global_context()->js_array_map());
1327 Handle<DescriptorArray> array_descriptors(
1328 array_map->instance_descriptors());
1329 ASSERT_EQ(1, array_descriptors->number_of_descriptors());
1330
1331 Handle<DescriptorArray> reresult_descriptors =
1332 Factory::NewDescriptorArray(3);
1333
1334 reresult_descriptors->CopyFrom(0, *array_descriptors, 0);
1335
1336 int enum_index = 0;
1337 {
1338 FieldDescriptor index_field(Heap::index_symbol(),
1339 JSRegExpResult::kIndexIndex,
1340 NONE,
1341 enum_index++);
1342 reresult_descriptors->Set(1, &index_field);
1343 }
1344
1345 {
1346 FieldDescriptor input_field(Heap::input_symbol(),
1347 JSRegExpResult::kInputIndex,
1348 NONE,
1349 enum_index++);
1350 reresult_descriptors->Set(2, &input_field);
1351 }
1352 reresult_descriptors->Sort();
1353
1354 initial_map->set_inobject_properties(2);
1355 initial_map->set_pre_allocated_property_fields(2);
1356 initial_map->set_unused_property_fields(0);
1357 initial_map->set_instance_descriptors(*reresult_descriptors);
1358
1359 global_context()->set_regexp_result_map(*initial_map);
1360 }
1361
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001362#ifdef DEBUG
1363 builtins->Verify();
1364#endif
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001365
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001366 return true;
1367}
1368
1369
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001370static Handle<JSObject> ResolveBuiltinIdHolder(
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001371 Handle<Context> global_context,
1372 const char* holder_expr) {
1373 Handle<GlobalObject> global(global_context->global());
1374 const char* period_pos = strchr(holder_expr, '.');
1375 if (period_pos == NULL) {
1376 return Handle<JSObject>::cast(
1377 GetProperty(global, Factory::LookupAsciiSymbol(holder_expr)));
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00001378 }
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00001379 ASSERT_EQ(".prototype", period_pos);
1380 Vector<const char> property(holder_expr,
1381 static_cast<int>(period_pos - holder_expr));
1382 Handle<JSFunction> function = Handle<JSFunction>::cast(
1383 GetProperty(global, Factory::LookupSymbol(property)));
1384 return Handle<JSObject>(JSObject::cast(function->prototype()));
1385}
1386
1387
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001388static void InstallBuiltinFunctionId(Handle<JSObject> holder,
1389 const char* function_name,
1390 BuiltinFunctionId id) {
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001391 Handle<String> name = Factory::LookupAsciiSymbol(function_name);
lrn@chromium.org303ada72010-10-27 09:33:13 +00001392 Object* function_object = holder->GetProperty(*name)->ToObjectUnchecked();
1393 Handle<JSFunction> function(JSFunction::cast(function_object));
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001394 function->shared()->set_function_data(Smi::FromInt(id));
1395}
1396
1397
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001398void Genesis::InstallBuiltinFunctionIds() {
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001399 HandleScope scope;
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001400#define INSTALL_BUILTIN_ID(holder_expr, fun_name, name) \
1401 { \
1402 Handle<JSObject> holder = ResolveBuiltinIdHolder( \
1403 global_context(), #holder_expr); \
1404 BuiltinFunctionId id = k##name; \
1405 InstallBuiltinFunctionId(holder, #fun_name, id); \
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001406 }
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001407 FUNCTIONS_WITH_ID_LIST(INSTALL_BUILTIN_ID)
1408#undef INSTALL_BUILTIN_ID
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001409}
1410
1411
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00001412// Do not forget to update macros.py with named constant
1413// of cache id.
1414#define JSFUNCTION_RESULT_CACHE_LIST(F) \
1415 F(16, global_context()->regexp_function())
1416
1417
1418static FixedArray* CreateCache(int size, JSFunction* factory) {
1419 // Caches are supposed to live for a long time, allocate in old space.
1420 int array_size = JSFunctionResultCache::kEntriesIndex + 2 * size;
ager@chromium.orgac091b72010-05-05 07:34:42 +00001421 // Cannot use cast as object is not fully initialized yet.
1422 JSFunctionResultCache* cache = reinterpret_cast<JSFunctionResultCache*>(
1423 *Factory::NewFixedArrayWithHoles(array_size, TENURED));
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00001424 cache->set(JSFunctionResultCache::kFactoryIndex, factory);
ager@chromium.orgac091b72010-05-05 07:34:42 +00001425 cache->MakeZeroSize();
1426 return cache;
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00001427}
1428
1429
1430void Genesis::InstallJSFunctionResultCaches() {
1431 const int kNumberOfCaches = 0 +
1432#define F(size, func) + 1
1433 JSFUNCTION_RESULT_CACHE_LIST(F)
1434#undef F
1435 ;
1436
1437 Handle<FixedArray> caches = Factory::NewFixedArray(kNumberOfCaches, TENURED);
1438
1439 int index = 0;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001440
1441#define F(size, func) do { \
1442 FixedArray* cache = CreateCache((size), (func)); \
1443 caches->set(index++, cache); \
1444 } while (false)
1445
1446 JSFUNCTION_RESULT_CACHE_LIST(F);
1447
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00001448#undef F
1449
1450 global_context()->set_jsfunction_result_caches(*caches);
1451}
1452
1453
ricow@chromium.org65fae842010-08-25 15:26:24 +00001454void Genesis::InitializeNormalizedMapCaches() {
1455 Handle<FixedArray> array(
1456 Factory::NewFixedArray(NormalizedMapCache::kEntries, TENURED));
1457 global_context()->set_normalized_map_cache(NormalizedMapCache::cast(*array));
1458}
1459
1460
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001461int BootstrapperActive::nesting_ = 0;
1462
1463
1464bool Bootstrapper::InstallExtensions(Handle<Context> global_context,
1465 v8::ExtensionConfiguration* extensions) {
1466 BootstrapperActive active;
1467 SaveContext saved_context;
1468 Top::set_context(*global_context);
1469 if (!Genesis::InstallExtensions(global_context, extensions)) return false;
1470 Genesis::InstallSpecialObjects(global_context);
1471 return true;
1472}
1473
1474
1475void Genesis::InstallSpecialObjects(Handle<Context> global_context) {
mads.s.agercbaa0602008-08-14 13:41:48 +00001476 HandleScope scope;
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001477 Handle<JSGlobalObject> js_global(
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001478 JSGlobalObject::cast(global_context->global()));
mads.s.agercbaa0602008-08-14 13:41:48 +00001479 // Expose the natives in global if a name for it is specified.
1480 if (FLAG_expose_natives_as != NULL && strlen(FLAG_expose_natives_as) != 0) {
1481 Handle<String> natives_string =
1482 Factory::LookupAsciiSymbol(FLAG_expose_natives_as);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001483 SetProperty(js_global, natives_string,
1484 Handle<JSObject>(js_global->builtins()), DONT_ENUM);
mads.s.agercbaa0602008-08-14 13:41:48 +00001485 }
1486
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00001487 Handle<Object> Error = GetProperty(js_global, "Error");
1488 if (Error->IsJSObject()) {
1489 Handle<String> name = Factory::LookupAsciiSymbol("stackTraceLimit");
1490 SetProperty(Handle<JSObject>::cast(Error),
1491 name,
1492 Handle<Smi>(Smi::FromInt(FLAG_stack_trace_limit)),
1493 NONE);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001494 }
1495
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001496#ifdef ENABLE_DEBUGGER_SUPPORT
mads.s.agercbaa0602008-08-14 13:41:48 +00001497 // Expose the debug global object in global if a name for it is specified.
1498 if (FLAG_expose_debug_as != NULL && strlen(FLAG_expose_debug_as) != 0) {
1499 // If loading fails we just bail out without installing the
1500 // debugger but without tanking the whole context.
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001501 if (!Debug::Load()) return;
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001502 // Set the security token for the debugger context to the same as
1503 // the shell global context to allow calling between these (otherwise
1504 // exposing debug global object doesn't make much sense).
1505 Debug::debug_context()->set_security_token(
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001506 global_context->security_token());
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001507
mads.s.agercbaa0602008-08-14 13:41:48 +00001508 Handle<String> debug_string =
1509 Factory::LookupAsciiSymbol(FLAG_expose_debug_as);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001510 SetProperty(js_global, debug_string,
1511 Handle<Object>(Debug::debug_context()->global_proxy()), DONT_ENUM);
mads.s.agercbaa0602008-08-14 13:41:48 +00001512 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001513#endif
mads.s.agercbaa0602008-08-14 13:41:48 +00001514}
1515
1516
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001517bool Genesis::InstallExtensions(Handle<Context> global_context,
1518 v8::ExtensionConfiguration* extensions) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001519 // Clear coloring of extension list
1520 v8::RegisteredExtension* current = v8::RegisteredExtension::first_extension();
1521 while (current != NULL) {
1522 current->set_state(v8::UNVISITED);
1523 current = current->next();
1524 }
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001525 // Install auto extensions.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001526 current = v8::RegisteredExtension::first_extension();
1527 while (current != NULL) {
1528 if (current->extension()->auto_enable())
1529 InstallExtension(current);
1530 current = current->next();
1531 }
1532
1533 if (FLAG_expose_gc) InstallExtension("v8/gc");
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00001534 if (FLAG_expose_externalize_string) InstallExtension("v8/externalize");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001535
1536 if (extensions == NULL) return true;
1537 // Install required extensions
1538 int count = v8::ImplementationUtilities::GetNameCount(extensions);
1539 const char** names = v8::ImplementationUtilities::GetNames(extensions);
1540 for (int i = 0; i < count; i++) {
1541 if (!InstallExtension(names[i]))
1542 return false;
1543 }
1544
1545 return true;
1546}
1547
1548
1549// Installs a named extension. This methods is unoptimized and does
1550// not scale well if we want to support a large number of extensions.
1551bool Genesis::InstallExtension(const char* name) {
1552 v8::RegisteredExtension* current = v8::RegisteredExtension::first_extension();
1553 // Loop until we find the relevant extension
1554 while (current != NULL) {
1555 if (strcmp(name, current->extension()->name()) == 0) break;
1556 current = current->next();
1557 }
1558 // Didn't find the extension; fail.
1559 if (current == NULL) {
1560 v8::Utils::ReportApiFailure(
1561 "v8::Context::New()", "Cannot find required extension");
1562 return false;
1563 }
1564 return InstallExtension(current);
1565}
1566
1567
1568bool Genesis::InstallExtension(v8::RegisteredExtension* current) {
1569 HandleScope scope;
1570
1571 if (current->state() == v8::INSTALLED) return true;
1572 // The current node has already been visited so there must be a
1573 // cycle in the dependency graph; fail.
1574 if (current->state() == v8::VISITED) {
1575 v8::Utils::ReportApiFailure(
1576 "v8::Context::New()", "Circular extension dependency");
1577 return false;
1578 }
1579 ASSERT(current->state() == v8::UNVISITED);
1580 current->set_state(v8::VISITED);
1581 v8::Extension* extension = current->extension();
1582 // Install the extension's dependencies
1583 for (int i = 0; i < extension->dependency_count(); i++) {
1584 if (!InstallExtension(extension->dependencies()[i])) return false;
1585 }
1586 Vector<const char> source = CStrVector(extension->source());
1587 Handle<String> source_code = Factory::NewStringFromAscii(source);
1588 bool result = CompileScriptCached(CStrVector(extension->name()),
1589 source_code,
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001590 &extensions_cache,
1591 extension,
1592 Handle<Context>(Top::context()),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001593 false);
1594 ASSERT(Top::has_pending_exception() != result);
1595 if (!result) {
1596 Top::clear_pending_exception();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001597 }
1598 current->set_state(v8::INSTALLED);
1599 return result;
1600}
1601
1602
ager@chromium.org5c838252010-02-19 08:53:10 +00001603bool Genesis::InstallJSBuiltins(Handle<JSBuiltinsObject> builtins) {
1604 HandleScope scope;
1605 for (int i = 0; i < Builtins::NumberOfJavaScriptBuiltins(); i++) {
1606 Builtins::JavaScript id = static_cast<Builtins::JavaScript>(i);
1607 Handle<String> name = Factory::LookupAsciiSymbol(Builtins::GetName(id));
lrn@chromium.org303ada72010-10-27 09:33:13 +00001608 Object* function_object = builtins->GetPropertyNoExceptionThrown(*name);
ager@chromium.org5c838252010-02-19 08:53:10 +00001609 Handle<JSFunction> function
lrn@chromium.org303ada72010-10-27 09:33:13 +00001610 = Handle<JSFunction>(JSFunction::cast(function_object));
ager@chromium.org5c838252010-02-19 08:53:10 +00001611 builtins->set_javascript_builtin(id, *function);
1612 Handle<SharedFunctionInfo> shared
1613 = Handle<SharedFunctionInfo>(function->shared());
1614 if (!EnsureCompiled(shared, CLEAR_EXCEPTION)) return false;
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00001615 // Set the code object on the function object.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001616 function->ReplaceCode(function->shared()->code());
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00001617 builtins->set_javascript_builtin_code(id, shared->code());
ager@chromium.org5c838252010-02-19 08:53:10 +00001618 }
1619 return true;
1620}
1621
1622
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001623bool Genesis::ConfigureGlobalObjects(
1624 v8::Handle<v8::ObjectTemplate> global_proxy_template) {
1625 Handle<JSObject> global_proxy(
1626 JSObject::cast(global_context()->global_proxy()));
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001627 Handle<JSObject> inner_global(JSObject::cast(global_context()->global()));
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001628
1629 if (!global_proxy_template.IsEmpty()) {
1630 // Configure the global proxy object.
1631 Handle<ObjectTemplateInfo> proxy_data =
1632 v8::Utils::OpenHandle(*global_proxy_template);
1633 if (!ConfigureApiObject(global_proxy, proxy_data)) return false;
1634
1635 // Configure the inner global object.
1636 Handle<FunctionTemplateInfo> proxy_constructor(
1637 FunctionTemplateInfo::cast(proxy_data->constructor()));
1638 if (!proxy_constructor->prototype_template()->IsUndefined()) {
1639 Handle<ObjectTemplateInfo> inner_data(
1640 ObjectTemplateInfo::cast(proxy_constructor->prototype_template()));
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001641 if (!ConfigureApiObject(inner_global, inner_data)) return false;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001642 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001643 }
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001644
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001645 SetObjectPrototype(global_proxy, inner_global);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001646 return true;
1647}
1648
1649
1650bool Genesis::ConfigureApiObject(Handle<JSObject> object,
1651 Handle<ObjectTemplateInfo> object_template) {
1652 ASSERT(!object_template.is_null());
1653 ASSERT(object->IsInstanceOf(
1654 FunctionTemplateInfo::cast(object_template->constructor())));
1655
1656 bool pending_exception = false;
1657 Handle<JSObject> obj =
1658 Execution::InstantiateObject(object_template, &pending_exception);
1659 if (pending_exception) {
1660 ASSERT(Top::has_pending_exception());
1661 Top::clear_pending_exception();
1662 return false;
1663 }
1664 TransferObject(obj, object);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001665 return true;
1666}
1667
1668
1669void Genesis::TransferNamedProperties(Handle<JSObject> from,
1670 Handle<JSObject> to) {
1671 if (from->HasFastProperties()) {
1672 Handle<DescriptorArray> descs =
1673 Handle<DescriptorArray>(from->map()->instance_descriptors());
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001674 for (int i = 0; i < descs->number_of_descriptors(); i++) {
1675 PropertyDetails details = PropertyDetails(descs->GetDetails(i));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001676 switch (details.type()) {
1677 case FIELD: {
1678 HandleScope inner;
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001679 Handle<String> key = Handle<String>(descs->GetKey(i));
1680 int index = descs->GetFieldIndex(i);
ager@chromium.org7c537e22008-10-16 08:43:32 +00001681 Handle<Object> value = Handle<Object>(from->FastPropertyAt(index));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001682 SetProperty(to, key, value, details.attributes());
1683 break;
1684 }
1685 case CONSTANT_FUNCTION: {
1686 HandleScope inner;
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001687 Handle<String> key = Handle<String>(descs->GetKey(i));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001688 Handle<JSFunction> fun =
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001689 Handle<JSFunction>(descs->GetConstantFunction(i));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001690 SetProperty(to, key, fun, details.attributes());
1691 break;
1692 }
1693 case CALLBACKS: {
1694 LookupResult result;
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001695 to->LocalLookup(descs->GetKey(i), &result);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001696 // If the property is already there we skip it
ager@chromium.org5c838252010-02-19 08:53:10 +00001697 if (result.IsProperty()) continue;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001698 HandleScope inner;
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001699 ASSERT(!to->HasFastProperties());
1700 // Add to dictionary.
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001701 Handle<String> key = Handle<String>(descs->GetKey(i));
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001702 Handle<Object> callbacks(descs->GetCallbacksObject(i));
1703 PropertyDetails d =
1704 PropertyDetails(details.attributes(), CALLBACKS, details.index());
1705 SetNormalizedProperty(to, key, callbacks, d);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001706 break;
1707 }
1708 case MAP_TRANSITION:
1709 case CONSTANT_TRANSITION:
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001710 case NULL_DESCRIPTOR:
1711 // Ignore non-properties.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001712 break;
1713 case NORMAL:
1714 // Do not occur since the from object has fast properties.
1715 case INTERCEPTOR:
1716 // No element in instance descriptors have interceptor type.
1717 UNREACHABLE();
1718 break;
1719 }
1720 }
1721 } else {
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00001722 Handle<StringDictionary> properties =
1723 Handle<StringDictionary>(from->property_dictionary());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001724 int capacity = properties->Capacity();
1725 for (int i = 0; i < capacity; i++) {
1726 Object* raw_key(properties->KeyAt(i));
1727 if (properties->IsKey(raw_key)) {
1728 ASSERT(raw_key->IsString());
1729 // If the property is already there we skip it.
1730 LookupResult result;
1731 to->LocalLookup(String::cast(raw_key), &result);
ager@chromium.org5c838252010-02-19 08:53:10 +00001732 if (result.IsProperty()) continue;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001733 // Set the property.
1734 Handle<String> key = Handle<String>(String::cast(raw_key));
1735 Handle<Object> value = Handle<Object>(properties->ValueAt(i));
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001736 if (value->IsJSGlobalPropertyCell()) {
1737 value = Handle<Object>(JSGlobalPropertyCell::cast(*value)->value());
1738 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001739 PropertyDetails details = properties->DetailsAt(i);
1740 SetProperty(to, key, value, details.attributes());
1741 }
1742 }
1743 }
1744}
1745
1746
1747void Genesis::TransferIndexedProperties(Handle<JSObject> from,
1748 Handle<JSObject> to) {
1749 // Cloning the elements array is sufficient.
1750 Handle<FixedArray> from_elements =
1751 Handle<FixedArray>(FixedArray::cast(from->elements()));
1752 Handle<FixedArray> to_elements = Factory::CopyFixedArray(from_elements);
1753 to->set_elements(*to_elements);
1754}
1755
1756
1757void Genesis::TransferObject(Handle<JSObject> from, Handle<JSObject> to) {
1758 HandleScope outer;
1759
1760 ASSERT(!from->IsJSArray());
1761 ASSERT(!to->IsJSArray());
1762
1763 TransferNamedProperties(from, to);
1764 TransferIndexedProperties(from, to);
1765
1766 // Transfer the prototype (new map is needed).
1767 Handle<Map> old_to_map = Handle<Map>(to->map());
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001768 Handle<Map> new_to_map = Factory::CopyMapDropTransitions(old_to_map);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001769 new_to_map->set_prototype(from->map()->prototype());
1770 to->set_map(*new_to_map);
1771}
1772
1773
1774void Genesis::MakeFunctionInstancePrototypeWritable() {
1775 // Make a new function map so all future functions
kasper.lund7276f142008-07-30 08:49:36 +00001776 // will have settable and enumerable prototype properties.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001777 HandleScope scope;
1778
1779 Handle<DescriptorArray> function_map_descriptors =
kmillikin@chromium.org4111b802010-05-03 10:34:42 +00001780 ComputeFunctionInstanceDescriptor(ADD_WRITEABLE_PROTOTYPE);
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00001781 Handle<Map> fm = Factory::CopyMapDropDescriptors(Top::function_map());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001782 fm->set_instance_descriptors(*function_map_descriptors);
kmillikin@chromium.org4111b802010-05-03 10:34:42 +00001783 fm->set_function_with_prototype(true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001784 Top::context()->global_context()->set_function_map(*fm);
1785}
1786
1787
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001788Genesis::Genesis(Handle<Object> global_object,
1789 v8::Handle<v8::ObjectTemplate> global_template,
1790 v8::ExtensionConfiguration* extensions) {
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00001791 result_ = Handle<Context>::null();
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001792 // If V8 isn't running and cannot be initialized, just return.
1793 if (!V8::IsRunning() && !V8::Initialize(NULL)) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001794
1795 // Before creating the roots we must save the context and restore it
1796 // on all function exits.
1797 HandleScope scope;
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001798 SaveContext saved_context;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001799
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001800 Handle<Context> new_context = Snapshot::NewContextFromSnapshot();
1801 if (!new_context.is_null()) {
1802 global_context_ =
1803 Handle<Context>::cast(GlobalHandles::Create(*new_context));
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001804 AddToWeakGlobalContextList(*global_context_);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001805 Top::set_context(*global_context_);
1806 i::Counters::contexts_created_by_snapshot.Increment();
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001807 JSFunction* empty_function =
ager@chromium.org378b34e2011-01-28 08:04:38 +00001808 JSFunction::cast(global_context_->function_map()->prototype());
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001809 empty_function_ = Handle<JSFunction>(empty_function);
1810 Handle<GlobalObject> inner_global;
1811 Handle<JSGlobalProxy> global_proxy =
1812 CreateNewGlobals(global_template,
1813 global_object,
1814 &inner_global);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001815
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001816 HookUpGlobalProxy(inner_global, global_proxy);
1817 HookUpInnerGlobal(inner_global);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001818
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001819 if (!ConfigureGlobalObjects(global_template)) return;
1820 } else {
1821 // We get here if there was no context snapshot.
1822 CreateRoots();
1823 Handle<JSFunction> empty_function = CreateEmptyFunction();
1824 Handle<GlobalObject> inner_global;
1825 Handle<JSGlobalProxy> global_proxy =
1826 CreateNewGlobals(global_template, global_object, &inner_global);
1827 HookUpGlobalProxy(inner_global, global_proxy);
1828 InitializeGlobal(inner_global, empty_function);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00001829 InstallJSFunctionResultCaches();
ricow@chromium.org65fae842010-08-25 15:26:24 +00001830 InitializeNormalizedMapCaches();
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00001831 if (!InstallNatives()) return;
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001832
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001833 MakeFunctionInstancePrototypeWritable();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001834
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001835 if (!ConfigureGlobalObjects(global_template)) return;
1836 i::Counters::contexts_created_from_scratch.Increment();
1837 }
mads.s.agercbaa0602008-08-14 13:41:48 +00001838
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001839 result_ = global_context_;
1840}
1841
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001842
1843// Support for thread preemption.
1844
1845// Reserve space for statics needing saving and restoring.
1846int Bootstrapper::ArchiveSpacePerThread() {
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001847 return BootstrapperActive::ArchiveSpacePerThread();
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001848}
1849
1850
1851// Archive statics that are thread local.
1852char* Bootstrapper::ArchiveState(char* to) {
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001853 return BootstrapperActive::ArchiveState(to);
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001854}
1855
1856
1857// Restore statics that are thread local.
1858char* Bootstrapper::RestoreState(char* from) {
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001859 return BootstrapperActive::RestoreState(from);
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001860}
1861
1862
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00001863// Called when the top-level V8 mutex is destroyed.
1864void Bootstrapper::FreeThreadResources() {
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001865 ASSERT(!BootstrapperActive::IsActive());
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00001866}
1867
1868
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001869// Reserve space for statics needing saving and restoring.
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001870int BootstrapperActive::ArchiveSpacePerThread() {
1871 return sizeof(nesting_);
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001872}
1873
1874
1875// Archive statics that are thread local.
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001876char* BootstrapperActive::ArchiveState(char* to) {
1877 *reinterpret_cast<int*>(to) = nesting_;
1878 nesting_ = 0;
1879 return to + sizeof(nesting_);
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001880}
1881
1882
1883// Restore statics that are thread local.
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001884char* BootstrapperActive::RestoreState(char* from) {
1885 nesting_ = *reinterpret_cast<int*>(from);
1886 return from + sizeof(nesting_);
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001887}
1888
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001889} } // namespace v8::internal