blob: f07e625ec01956ede51834e8616a1e4160e70c84 [file] [log] [blame]
Ben Murdoch85b71792012-04-11 18:30:58 +01001// Copyright 2011 the V8 project authors. All rights reserved.
Steve Blocka7e24c12009-10-30 11:49:00 +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"
Iain Merrick75681382010-08-19 15:07:18 +010039#include "objects-visiting.h"
Steve Blockd0582a62009-12-15 09:54:21 +000040#include "snapshot.h"
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -080041#include "extensions/externalize-string-extension.h"
42#include "extensions/gc-extension.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000043
44namespace v8 {
45namespace internal {
46
Steve Blocka7e24c12009-10-30 11:49:00 +000047
Steve Block44f0eee2011-05-26 01:26:41 +010048NativesExternalStringResource::NativesExternalStringResource(
49 Bootstrapper* bootstrapper,
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000050 const char* source,
51 size_t length)
52 : data_(source), length_(length) {
Steve Block44f0eee2011-05-26 01:26:41 +010053 if (bootstrapper->delete_these_non_arrays_on_tear_down_ == NULL) {
54 bootstrapper->delete_these_non_arrays_on_tear_down_ = new List<char*>(2);
Steve Blockd0582a62009-12-15 09:54:21 +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.
Steve Block44f0eee2011-05-26 01:26:41 +010058 bootstrapper->delete_these_non_arrays_on_tear_down_->
Steve Blockd0582a62009-12-15 09:54:21 +000059 Add(reinterpret_cast<char*>(this));
60}
Steve Blocka7e24c12009-10-30 11:49:00 +000061
62
Steve Block44f0eee2011-05-26 01:26:41 +010063Bootstrapper::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
Steve Blocka7e24c12009-10-30 11:49:00 +000071Handle<String> Bootstrapper::NativesSourceLookup(int index) {
72 ASSERT(0 <= index && index < Natives::GetBuiltinsCount());
Steve Block44f0eee2011-05-26 01:26:41 +010073 Isolate* isolate = Isolate::Current();
74 Factory* factory = isolate->factory();
75 Heap* heap = isolate->heap();
76 if (heap->natives_source_cache()->get(index)->IsUndefined()) {
Ben Murdoch85b71792012-04-11 18:30:58 +010077 if (!Snapshot::IsEnabled() || FLAG_new_snapshot) {
78 // We can use external strings for the natives.
79 Vector<const char> source = Natives::GetRawScriptSource(index);
80 NativesExternalStringResource* resource =
81 new NativesExternalStringResource(this,
82 source.start(),
83 source.length());
84 Handle<String> source_code =
85 factory->NewExternalStringFromAscii(resource);
86 heap->natives_source_cache()->set(index, *source_code);
87 } else {
88 // Old snapshot code can't cope with external strings at all.
89 Handle<String> source_code =
90 factory->NewStringFromAscii(Natives::GetRawScriptSource(index));
91 heap->natives_source_cache()->set(index, *source_code);
92 }
Steve Blocka7e24c12009-10-30 11:49:00 +000093 }
Steve Block44f0eee2011-05-26 01:26:41 +010094 Handle<Object> cached_source(heap->natives_source_cache()->get(index));
Steve Blocka7e24c12009-10-30 11:49:00 +000095 return Handle<String>::cast(cached_source);
96}
97
98
Steve Blocka7e24c12009-10-30 11:49:00 +000099void Bootstrapper::Initialize(bool create_heap_objects) {
Steve Block44f0eee2011-05-26 01:26:41 +0100100 extensions_cache_.Initialize(create_heap_objects);
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800101 GCExtension::Register();
102 ExternalizeStringExtension::Register();
Steve Blocka7e24c12009-10-30 11:49:00 +0000103}
104
105
Leon Clarkee46be812010-01-19 14:06:41 +0000106char* Bootstrapper::AllocateAutoDeletedArray(int bytes) {
107 char* memory = new char[bytes];
108 if (memory != NULL) {
Steve Block44f0eee2011-05-26 01:26:41 +0100109 if (delete_these_arrays_on_tear_down_ == NULL) {
110 delete_these_arrays_on_tear_down_ = new List<char*>(2);
Leon Clarkee46be812010-01-19 14:06:41 +0000111 }
Steve Block44f0eee2011-05-26 01:26:41 +0100112 delete_these_arrays_on_tear_down_->Add(memory);
Leon Clarkee46be812010-01-19 14:06:41 +0000113 }
114 return memory;
115}
116
117
Steve Blocka7e24c12009-10-30 11:49:00 +0000118void Bootstrapper::TearDown() {
Steve Block44f0eee2011-05-26 01:26:41 +0100119 if (delete_these_non_arrays_on_tear_down_ != NULL) {
120 int len = delete_these_non_arrays_on_tear_down_->length();
Steve Blockd0582a62009-12-15 09:54:21 +0000121 ASSERT(len < 20); // Don't use this mechanism for unbounded allocations.
122 for (int i = 0; i < len; i++) {
Steve Block44f0eee2011-05-26 01:26:41 +0100123 delete delete_these_non_arrays_on_tear_down_->at(i);
124 delete_these_non_arrays_on_tear_down_->at(i) = NULL;
Steve Blockd0582a62009-12-15 09:54:21 +0000125 }
Steve Block44f0eee2011-05-26 01:26:41 +0100126 delete delete_these_non_arrays_on_tear_down_;
127 delete_these_non_arrays_on_tear_down_ = NULL;
Steve Blockd0582a62009-12-15 09:54:21 +0000128 }
129
Steve Block44f0eee2011-05-26 01:26:41 +0100130 if (delete_these_arrays_on_tear_down_ != NULL) {
131 int len = delete_these_arrays_on_tear_down_->length();
Leon Clarkee46be812010-01-19 14:06:41 +0000132 ASSERT(len < 1000); // Don't use this mechanism for unbounded allocations.
133 for (int i = 0; i < len; i++) {
Steve Block44f0eee2011-05-26 01:26:41 +0100134 delete[] delete_these_arrays_on_tear_down_->at(i);
135 delete_these_arrays_on_tear_down_->at(i) = NULL;
Leon Clarkee46be812010-01-19 14:06:41 +0000136 }
Steve Block44f0eee2011-05-26 01:26:41 +0100137 delete delete_these_arrays_on_tear_down_;
138 delete_these_arrays_on_tear_down_ = NULL;
Leon Clarkee46be812010-01-19 14:06:41 +0000139 }
140
Steve Block44f0eee2011-05-26 01:26:41 +0100141 extensions_cache_.Initialize(false); // Yes, symmetrical
Steve Blocka7e24c12009-10-30 11:49:00 +0000142}
143
144
Steve Blocka7e24c12009-10-30 11:49:00 +0000145class Genesis BASE_EMBEDDED {
146 public:
Ben Murdoch257744e2011-11-30 15:57:28 +0000147 Genesis(Isolate* isolate,
148 Handle<Object> global_object,
Steve Blocka7e24c12009-10-30 11:49:00 +0000149 v8::Handle<v8::ObjectTemplate> global_template,
150 v8::ExtensionConfiguration* extensions);
Andrei Popescu31002712010-02-23 13:46:05 +0000151 ~Genesis() { }
Steve Blocka7e24c12009-10-30 11:49:00 +0000152
153 Handle<Context> result() { return result_; }
154
155 Genesis* previous() { return previous_; }
Steve Blocka7e24c12009-10-30 11:49:00 +0000156
Ben Murdoch257744e2011-11-30 15:57:28 +0000157 Isolate* isolate() const { return isolate_; }
158 Factory* factory() const { return isolate_->factory(); }
159 Heap* heap() const { return isolate_->heap(); }
160
Steve Blocka7e24c12009-10-30 11:49:00 +0000161 private:
162 Handle<Context> global_context_;
Ben Murdoch257744e2011-11-30 15:57:28 +0000163 Isolate* isolate_;
Steve Blocka7e24c12009-10-30 11:49:00 +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_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000169
170 Handle<Context> global_context() { return global_context_; }
171
Andrei Popescu31002712010-02-23 13:46:05 +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.
Ben Murdoch257744e2011-11-30 15:57:28 +0000175 Handle<JSFunction> CreateEmptyFunction(Isolate* isolate);
Steve Block44f0eee2011-05-26 01:26:41 +0100176 // Creates the ThrowTypeError function. ECMA 5th Ed. 13.2.3
Ben Murdoch257744e2011-11-30 15:57:28 +0000177 Handle<JSFunction> GetThrowTypeErrorFunction();
Steve Block44f0eee2011-05-26 01:26:41 +0100178
179 void CreateStrictModeFunctionMaps(Handle<JSFunction> empty);
Andrei Popescu31002712010-02-23 13:46:05 +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);
Andrei Popescu402d9372010-02-26 13:31:12 +0000195 // 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);
Andrei Popescu31002712010-02-23 13:46:05 +0000199 // New context initialization. Used for creating a context from scratch.
Ben Murdoch85b71792012-04-11 18:30:58 +0100200 void InitializeGlobal(Handle<GlobalObject> inner_global,
Andrei Popescu31002712010-02-23 13:46:05 +0000201 Handle<JSFunction> empty_function);
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000202 void InitializeExperimentalGlobal();
Andrei Popescu31002712010-02-23 13:46:05 +0000203 // Installs the contents of the native .js files on the global objects.
204 // Used for creating a context from scratch.
Steve Blocka7e24c12009-10-30 11:49:00 +0000205 void InstallNativeFunctions();
Ben Murdoch257744e2011-11-30 15:57:28 +0000206 void InstallExperimentalNativeFunctions();
Steve Blocka7e24c12009-10-30 11:49:00 +0000207 bool InstallNatives();
Ben Murdoch257744e2011-11-30 15:57:28 +0000208 bool InstallExperimentalNatives();
Ben Murdochb0fe1622011-05-05 13:52:32 +0100209 void InstallBuiltinFunctionIds();
Steve Block6ded16b2010-05-10 14:33:55 +0100210 void InstallJSFunctionResultCaches();
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100211 void InitializeNormalizedMapCaches();
Andrei Popescu31002712010-02-23 13:46:05 +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);
Ben Murdoch85b71792012-04-11 18:30:58 +0100216 static bool InstallExtension(const char* name);
217 static bool InstallExtension(v8::RegisteredExtension* current);
Andrei Popescu31002712010-02-23 13:46:05 +0000218 static void InstallSpecialObjects(Handle<Context> global_context);
Andrei Popescu402d9372010-02-26 13:31:12 +0000219 bool InstallJSBuiltins(Handle<JSBuiltinsObject> builtins);
Steve Blocka7e24c12009-10-30 11:49:00 +0000220 bool ConfigureApiObject(Handle<JSObject> object,
221 Handle<ObjectTemplateInfo> object_template);
222 bool ConfigureGlobalObjects(v8::Handle<v8::ObjectTemplate> global_template);
223
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
Steve Block6ded16b2010-05-10 14:33:55 +0100231 enum PrototypePropertyMode {
232 DONT_ADD_PROTOTYPE,
233 ADD_READONLY_PROTOTYPE,
234 ADD_WRITEABLE_PROTOTYPE
235 };
Steve Block44f0eee2011-05-26 01:26:41 +0100236
237 Handle<Map> CreateFunctionMap(PrototypePropertyMode prototype_mode);
238
Steve Blocka7e24c12009-10-30 11:49:00 +0000239 Handle<DescriptorArray> ComputeFunctionInstanceDescriptor(
Steve Block6ded16b2010-05-10 14:33:55 +0100240 PrototypePropertyMode prototypeMode);
Steve Blocka7e24c12009-10-30 11:49:00 +0000241 void MakeFunctionInstancePrototypeWritable();
242
Steve Block44f0eee2011-05-26 01:26:41 +0100243 Handle<Map> CreateStrictModeFunctionMap(
244 PrototypePropertyMode prototype_mode,
Ben Murdoch85b71792012-04-11 18:30:58 +0100245 Handle<JSFunction> empty_function,
246 Handle<FixedArray> arguments_callbacks,
247 Handle<FixedArray> caller_callbacks);
Steve Block44f0eee2011-05-26 01:26:41 +0100248
249 Handle<DescriptorArray> ComputeStrictFunctionInstanceDescriptor(
Ben Murdoch85b71792012-04-11 18:30:58 +0100250 PrototypePropertyMode propertyMode,
251 Handle<FixedArray> arguments,
252 Handle<FixedArray> caller);
Steve Block44f0eee2011-05-26 01:26:41 +0100253
Ben Murdoch257744e2011-11-30 15:57:28 +0000254 static bool CompileBuiltin(Isolate* isolate, int index);
255 static bool CompileExperimentalBuiltin(Isolate* isolate, int index);
Steve Blocka7e24c12009-10-30 11:49:00 +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,
Andrei Popescu31002712010-02-23 13:46:05 +0000261 Handle<Context> top_context,
Steve Blocka7e24c12009-10-30 11:49:00 +0000262 bool use_runtime_context);
263
264 Handle<Context> result_;
Steve Block44f0eee2011-05-26 01:26:41 +0100265
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_;
Ben Murdoch257744e2011-11-30 15:57:28 +0000272 Handle<JSFunction> throw_type_error_function;
Steve Block44f0eee2011-05-26 01:26:41 +0100273
Andrei Popescu31002712010-02-23 13:46:05 +0000274 BootstrapperActive active_;
275 friend class Bootstrapper;
Steve Blocka7e24c12009-10-30 11:49:00 +0000276};
277
Steve Blocka7e24c12009-10-30 11:49:00 +0000278
279void Bootstrapper::Iterate(ObjectVisitor* v) {
Steve Block44f0eee2011-05-26 01:26:41 +0100280 extensions_cache_.Iterate(v);
Ben Murdoch85b71792012-04-11 18:30:58 +0100281 v->Synchronize("Extensions");
Steve Blocka7e24c12009-10-30 11:49:00 +0000282}
283
284
Steve Blocka7e24c12009-10-30 11:49:00 +0000285Handle<Context> Bootstrapper::CreateEnvironment(
Ben Murdoch257744e2011-11-30 15:57:28 +0000286 Isolate* isolate,
Steve Blocka7e24c12009-10-30 11:49:00 +0000287 Handle<Object> global_object,
288 v8::Handle<v8::ObjectTemplate> global_template,
289 v8::ExtensionConfiguration* extensions) {
Andrei Popescu31002712010-02-23 13:46:05 +0000290 HandleScope scope;
291 Handle<Context> env;
Ben Murdoch257744e2011-11-30 15:57:28 +0000292 Genesis genesis(isolate, global_object, global_template, extensions);
Andrei Popescu31002712010-02-23 13:46:05 +0000293 env = genesis.result();
294 if (!env.is_null()) {
295 if (InstallExtensions(env, extensions)) {
296 return env;
297 }
298 }
299 return Handle<Context>();
Steve Blocka7e24c12009-10-30 11:49:00 +0000300}
301
302
303static void SetObjectPrototype(Handle<JSObject> object, Handle<Object> proto) {
304 // object.__proto__ = proto;
Ben Murdoch257744e2011-11-30 15:57:28 +0000305 Factory* factory = object->GetIsolate()->factory();
Steve Blocka7e24c12009-10-30 11:49:00 +0000306 Handle<Map> old_to_map = Handle<Map>(object->map());
Ben Murdoch257744e2011-11-30 15:57:28 +0000307 Handle<Map> new_to_map = factory->CopyMapDropTransitions(old_to_map);
Steve Blocka7e24c12009-10-30 11:49:00 +0000308 new_to_map->set_prototype(*proto);
309 object->set_map(*new_to_map);
310}
311
312
313void Bootstrapper::DetachGlobal(Handle<Context> env) {
Ben Murdoch257744e2011-11-30 15:57:28 +0000314 Factory* factory = env->GetIsolate()->factory();
Steve Block44f0eee2011-05-26 01:26:41 +0100315 JSGlobalProxy::cast(env->global_proxy())->set_context(*factory->null_value());
Steve Blocka7e24c12009-10-30 11:49:00 +0000316 SetObjectPrototype(Handle<JSObject>(env->global_proxy()),
Steve Block44f0eee2011-05-26 01:26:41 +0100317 factory->null_value());
Steve Blocka7e24c12009-10-30 11:49:00 +0000318 env->set_global_proxy(env->global());
319 env->global()->set_global_receiver(env->global());
320}
321
322
Andrei Popescu74b3c142010-03-29 12:03:09 +0100323void 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
Steve Blocka7e24c12009-10-30 11:49:00 +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) {
Ben Murdoch257744e2011-11-30 15:57:28 +0000341 Isolate* isolate = target->GetIsolate();
Steve Block44f0eee2011-05-26 01:26:41 +0100342 Factory* factory = isolate->factory();
343 Handle<String> symbol = factory->LookupAsciiSymbol(name);
344 Handle<Code> call_code = Handle<Code>(isolate->builtins()->builtin(call));
Steve Block6ded16b2010-05-10 14:33:55 +0100345 Handle<JSFunction> function = prototype.is_null() ?
Steve Block44f0eee2011-05-26 01:26:41 +0100346 factory->NewFunctionWithoutPrototype(symbol, call_code) :
347 factory->NewFunctionWithPrototype(symbol,
Steve Blocka7e24c12009-10-30 11:49:00 +0000348 type,
349 instance_size,
350 prototype,
351 call_code,
352 is_ecma_native);
Ben Murdoch589d6972011-11-30 16:04:58 +0000353 PropertyAttributes attributes;
354 if (target->IsJSBuiltinsObject()) {
355 attributes =
356 static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE | READ_ONLY);
357 } else {
358 attributes = DONT_ENUM;
359 }
Ben Murdoch85b71792012-04-11 18:30:58 +0100360 SetLocalPropertyNoThrow(target, symbol, function, attributes);
Steve Blocka7e24c12009-10-30 11:49:00 +0000361 if (is_ecma_native) {
362 function->shared()->set_instance_class_name(*symbol);
363 }
364 return function;
365}
366
367
368Handle<DescriptorArray> Genesis::ComputeFunctionInstanceDescriptor(
Steve Block6ded16b2010-05-10 14:33:55 +0100369 PrototypePropertyMode prototypeMode) {
Ben Murdoch85b71792012-04-11 18:30:58 +0100370 Handle<DescriptorArray> descriptors =
371 factory()->NewDescriptorArray(prototypeMode == DONT_ADD_PROTOTYPE
372 ? 4
373 : 5);
374 PropertyAttributes attributes =
375 static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE | READ_ONLY);
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000376
Steve Block44f0eee2011-05-26 01:26:41 +0100377 { // Add length.
Ben Murdoch85b71792012-04-11 18:30:58 +0100378 Handle<Foreign> foreign = factory()->NewForeign(&Accessors::FunctionLength);
379 CallbacksDescriptor d(*factory()->length_symbol(), *foreign, attributes);
380 descriptors->Set(0, &d);
Steve Block44f0eee2011-05-26 01:26:41 +0100381 }
382 { // Add name.
Ben Murdoch85b71792012-04-11 18:30:58 +0100383 Handle<Foreign> foreign = factory()->NewForeign(&Accessors::FunctionName);
384 CallbacksDescriptor d(*factory()->name_symbol(), *foreign, attributes);
385 descriptors->Set(1, &d);
Steve Block44f0eee2011-05-26 01:26:41 +0100386 }
387 { // Add arguments.
Ben Murdoch85b71792012-04-11 18:30:58 +0100388 Handle<Foreign> foreign =
389 factory()->NewForeign(&Accessors::FunctionArguments);
390 CallbacksDescriptor d(*factory()->arguments_symbol(), *foreign, attributes);
391 descriptors->Set(2, &d);
Steve Block44f0eee2011-05-26 01:26:41 +0100392 }
393 { // Add caller.
Ben Murdoch85b71792012-04-11 18:30:58 +0100394 Handle<Foreign> foreign = factory()->NewForeign(&Accessors::FunctionCaller);
395 CallbacksDescriptor d(*factory()->caller_symbol(), *foreign, attributes);
396 descriptors->Set(3, &d);
Steve Block44f0eee2011-05-26 01:26:41 +0100397 }
398 if (prototypeMode != DONT_ADD_PROTOTYPE) {
399 // Add prototype.
400 if (prototypeMode == ADD_WRITEABLE_PROTOTYPE) {
Ben Murdoch85b71792012-04-11 18:30:58 +0100401 attributes = static_cast<PropertyAttributes>(attributes & ~READ_ONLY);
Steve Block44f0eee2011-05-26 01:26:41 +0100402 }
Ben Murdoch85b71792012-04-11 18:30:58 +0100403 Handle<Foreign> foreign =
404 factory()->NewForeign(&Accessors::FunctionPrototype);
405 CallbacksDescriptor d(*factory()->prototype_symbol(), *foreign, attributes);
406 descriptors->Set(4, &d);
Steve Block44f0eee2011-05-26 01:26:41 +0100407 }
Ben Murdoch85b71792012-04-11 18:30:58 +0100408 descriptors->Sort();
Steve Block44f0eee2011-05-26 01:26:41 +0100409 return descriptors;
410}
Steve Blocka7e24c12009-10-30 11:49:00 +0000411
Steve Blocka7e24c12009-10-30 11:49:00 +0000412
Steve Block44f0eee2011-05-26 01:26:41 +0100413Handle<Map> Genesis::CreateFunctionMap(PrototypePropertyMode prototype_mode) {
Ben Murdoch257744e2011-11-30 15:57:28 +0000414 Handle<Map> map = factory()->NewMap(JS_FUNCTION_TYPE, JSFunction::kSize);
Steve Block44f0eee2011-05-26 01:26:41 +0100415 Handle<DescriptorArray> descriptors =
416 ComputeFunctionInstanceDescriptor(prototype_mode);
417 map->set_instance_descriptors(*descriptors);
418 map->set_function_with_prototype(prototype_mode != DONT_ADD_PROTOTYPE);
419 return map;
Steve Blocka7e24c12009-10-30 11:49:00 +0000420}
421
422
Ben Murdoch257744e2011-11-30 15:57:28 +0000423Handle<JSFunction> Genesis::CreateEmptyFunction(Isolate* isolate) {
Steve Block44f0eee2011-05-26 01:26:41 +0100424 // Allocate the map for function instances. Maps are allocated first and their
425 // prototypes patched later, once empty function is created.
426
Steve Blocka7e24c12009-10-30 11:49:00 +0000427 // Please note that the prototype property for function instances must be
428 // writable.
Ben Murdoch8b112d22011-06-08 16:22:53 +0100429 Handle<Map> function_instance_map =
430 CreateFunctionMap(ADD_WRITEABLE_PROTOTYPE);
431 global_context()->set_function_instance_map(*function_instance_map);
Steve Block6ded16b2010-05-10 14:33:55 +0100432
433 // Functions with this map will not have a 'prototype' property, and
434 // can not be used as constructors.
Ben Murdoch8b112d22011-06-08 16:22:53 +0100435 Handle<Map> function_without_prototype_map =
436 CreateFunctionMap(DONT_ADD_PROTOTYPE);
Steve Block6ded16b2010-05-10 14:33:55 +0100437 global_context()->set_function_without_prototype_map(
Ben Murdoch8b112d22011-06-08 16:22:53 +0100438 *function_without_prototype_map);
Steve Blocka7e24c12009-10-30 11:49:00 +0000439
Steve Block44f0eee2011-05-26 01:26:41 +0100440 // Allocate the function map. This map is temporary, used only for processing
441 // of builtins.
442 // Later the map is replaced with writable prototype map, allocated below.
Ben Murdoch8b112d22011-06-08 16:22:53 +0100443 Handle<Map> function_map = CreateFunctionMap(ADD_READONLY_PROTOTYPE);
444 global_context()->set_function_map(*function_map);
Steve Blocka7e24c12009-10-30 11:49:00 +0000445
Steve Block44f0eee2011-05-26 01:26:41 +0100446 // The final map for functions. Writeable prototype.
447 // This map is installed in MakeFunctionInstancePrototypeWritable.
448 function_instance_map_writable_prototype_ =
449 CreateFunctionMap(ADD_WRITEABLE_PROTOTYPE);
450
Steve Block44f0eee2011-05-26 01:26:41 +0100451 Factory* factory = isolate->factory();
452 Heap* heap = isolate->heap();
453
454 Handle<String> object_name = Handle<String>(heap->Object_symbol());
Steve Blocka7e24c12009-10-30 11:49:00 +0000455
456 { // --- O b j e c t ---
457 Handle<JSFunction> object_fun =
Steve Block44f0eee2011-05-26 01:26:41 +0100458 factory->NewFunction(object_name, factory->null_value());
Steve Blocka7e24c12009-10-30 11:49:00 +0000459 Handle<Map> object_function_map =
Steve Block44f0eee2011-05-26 01:26:41 +0100460 factory->NewMap(JS_OBJECT_TYPE, JSObject::kHeaderSize);
Steve Blocka7e24c12009-10-30 11:49:00 +0000461 object_fun->set_initial_map(*object_function_map);
462 object_function_map->set_constructor(*object_fun);
463
464 global_context()->set_object_function(*object_fun);
465
466 // Allocate a new prototype for the object function.
Steve Block44f0eee2011-05-26 01:26:41 +0100467 Handle<JSObject> prototype = factory->NewJSObject(
468 isolate->object_function(),
469 TENURED);
Steve Blocka7e24c12009-10-30 11:49:00 +0000470
471 global_context()->set_initial_object_prototype(*prototype);
472 SetPrototype(object_fun, prototype);
473 object_function_map->
Steve Block44f0eee2011-05-26 01:26:41 +0100474 set_instance_descriptors(heap->empty_descriptor_array());
Steve Blocka7e24c12009-10-30 11:49:00 +0000475 }
476
477 // Allocate the empty function as the prototype for function ECMAScript
478 // 262 15.3.4.
Steve Block44f0eee2011-05-26 01:26:41 +0100479 Handle<String> symbol = factory->LookupAsciiSymbol("Empty");
Steve Blocka7e24c12009-10-30 11:49:00 +0000480 Handle<JSFunction> empty_function =
Ben Murdoch85b71792012-04-11 18:30:58 +0100481 factory->NewFunctionWithoutPrototype(symbol, kNonStrictMode);
Steve Blocka7e24c12009-10-30 11:49:00 +0000482
Andrei Popescu31002712010-02-23 13:46:05 +0000483 // --- E m p t y ---
484 Handle<Code> code =
Steve Block44f0eee2011-05-26 01:26:41 +0100485 Handle<Code>(isolate->builtins()->builtin(
486 Builtins::kEmptyFunction));
Andrei Popescu31002712010-02-23 13:46:05 +0000487 empty_function->set_code(*code);
Iain Merrick75681382010-08-19 15:07:18 +0100488 empty_function->shared()->set_code(*code);
Steve Block44f0eee2011-05-26 01:26:41 +0100489 Handle<String> source = factory->NewStringFromAscii(CStrVector("() {}"));
490 Handle<Script> script = factory->NewScript(source);
Andrei Popescu31002712010-02-23 13:46:05 +0000491 script->set_type(Smi::FromInt(Script::TYPE_NATIVE));
492 empty_function->shared()->set_script(*script);
493 empty_function->shared()->set_start_position(0);
494 empty_function->shared()->set_end_position(source->length());
495 empty_function->shared()->DontAdaptArguments();
Steve Block44f0eee2011-05-26 01:26:41 +0100496
497 // Set prototypes for the function maps.
Andrei Popescu31002712010-02-23 13:46:05 +0000498 global_context()->function_map()->set_prototype(*empty_function);
499 global_context()->function_instance_map()->set_prototype(*empty_function);
Steve Block6ded16b2010-05-10 14:33:55 +0100500 global_context()->function_without_prototype_map()->
501 set_prototype(*empty_function);
Steve Block44f0eee2011-05-26 01:26:41 +0100502 function_instance_map_writable_prototype_->set_prototype(*empty_function);
Steve Blocka7e24c12009-10-30 11:49:00 +0000503
Andrei Popescu31002712010-02-23 13:46:05 +0000504 // Allocate the function map first and then patch the prototype later
Steve Block44f0eee2011-05-26 01:26:41 +0100505 Handle<Map> empty_fm = factory->CopyMapDropDescriptors(
Steve Block6ded16b2010-05-10 14:33:55 +0100506 function_without_prototype_map);
507 empty_fm->set_instance_descriptors(
Steve Block44f0eee2011-05-26 01:26:41 +0100508 function_without_prototype_map->instance_descriptors());
Andrei Popescu31002712010-02-23 13:46:05 +0000509 empty_fm->set_prototype(global_context()->object_function()->prototype());
510 empty_function->set_map(*empty_fm);
511 return empty_function;
512}
513
514
Steve Block44f0eee2011-05-26 01:26:41 +0100515Handle<DescriptorArray> Genesis::ComputeStrictFunctionInstanceDescriptor(
Ben Murdoch85b71792012-04-11 18:30:58 +0100516 PrototypePropertyMode prototypeMode,
517 Handle<FixedArray> arguments,
518 Handle<FixedArray> caller) {
519 Handle<DescriptorArray> descriptors =
520 factory()->NewDescriptorArray(prototypeMode == DONT_ADD_PROTOTYPE
521 ? 4
522 : 5);
523 PropertyAttributes attributes = static_cast<PropertyAttributes>(
524 DONT_ENUM | DONT_DELETE | READ_ONLY);
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000525
Ben Murdoch85b71792012-04-11 18:30:58 +0100526 { // length
527 Handle<Foreign> foreign = factory()->NewForeign(&Accessors::FunctionLength);
528 CallbacksDescriptor d(*factory()->length_symbol(), *foreign, attributes);
529 descriptors->Set(0, &d);
Steve Block44f0eee2011-05-26 01:26:41 +0100530 }
Ben Murdoch85b71792012-04-11 18:30:58 +0100531 { // name
532 Handle<Foreign> foreign = factory()->NewForeign(&Accessors::FunctionName);
533 CallbacksDescriptor d(*factory()->name_symbol(), *foreign, attributes);
534 descriptors->Set(1, &d);
Steve Block44f0eee2011-05-26 01:26:41 +0100535 }
Ben Murdoch85b71792012-04-11 18:30:58 +0100536 { // arguments
537 CallbacksDescriptor d(*factory()->arguments_symbol(),
538 *arguments,
539 attributes);
540 descriptors->Set(2, &d);
Steve Block44f0eee2011-05-26 01:26:41 +0100541 }
Ben Murdoch85b71792012-04-11 18:30:58 +0100542 { // caller
543 CallbacksDescriptor d(*factory()->caller_symbol(), *caller, attributes);
544 descriptors->Set(3, &d);
Steve Block44f0eee2011-05-26 01:26:41 +0100545 }
546
Ben Murdoch85b71792012-04-11 18:30:58 +0100547 // prototype
Steve Block44f0eee2011-05-26 01:26:41 +0100548 if (prototypeMode != DONT_ADD_PROTOTYPE) {
Ben Murdoch85b71792012-04-11 18:30:58 +0100549 if (prototypeMode == ADD_WRITEABLE_PROTOTYPE) {
550 attributes = static_cast<PropertyAttributes>(attributes & ~READ_ONLY);
Steve Block44f0eee2011-05-26 01:26:41 +0100551 }
Ben Murdoch85b71792012-04-11 18:30:58 +0100552 Handle<Foreign> foreign =
553 factory()->NewForeign(&Accessors::FunctionPrototype);
554 CallbacksDescriptor d(*factory()->prototype_symbol(), *foreign, attributes);
555 descriptors->Set(4, &d);
Steve Block44f0eee2011-05-26 01:26:41 +0100556 }
557
Ben Murdoch85b71792012-04-11 18:30:58 +0100558 descriptors->Sort();
Steve Block44f0eee2011-05-26 01:26:41 +0100559 return descriptors;
560}
561
562
563// ECMAScript 5th Edition, 13.2.3
Ben Murdoch257744e2011-11-30 15:57:28 +0000564Handle<JSFunction> Genesis::GetThrowTypeErrorFunction() {
565 if (throw_type_error_function.is_null()) {
566 Handle<String> name = factory()->LookupAsciiSymbol("ThrowTypeError");
567 throw_type_error_function =
Ben Murdoch85b71792012-04-11 18:30:58 +0100568 factory()->NewFunctionWithoutPrototype(name, kNonStrictMode);
Ben Murdoch257744e2011-11-30 15:57:28 +0000569 Handle<Code> code(isolate()->builtins()->builtin(
570 Builtins::kStrictModePoisonPill));
571 throw_type_error_function->set_map(
572 global_context()->function_map());
573 throw_type_error_function->set_code(*code);
574 throw_type_error_function->shared()->set_code(*code);
575 throw_type_error_function->shared()->DontAdaptArguments();
Steve Block053d10c2011-06-13 19:13:29 +0100576
Ben Murdoch85b71792012-04-11 18:30:58 +0100577 PreventExtensions(throw_type_error_function);
Ben Murdoch257744e2011-11-30 15:57:28 +0000578 }
579 return throw_type_error_function;
Steve Block44f0eee2011-05-26 01:26:41 +0100580}
581
582
583Handle<Map> Genesis::CreateStrictModeFunctionMap(
584 PrototypePropertyMode prototype_mode,
Ben Murdoch85b71792012-04-11 18:30:58 +0100585 Handle<JSFunction> empty_function,
586 Handle<FixedArray> arguments_callbacks,
587 Handle<FixedArray> caller_callbacks) {
Ben Murdoch257744e2011-11-30 15:57:28 +0000588 Handle<Map> map = factory()->NewMap(JS_FUNCTION_TYPE, JSFunction::kSize);
Steve Block44f0eee2011-05-26 01:26:41 +0100589 Handle<DescriptorArray> descriptors =
Ben Murdoch85b71792012-04-11 18:30:58 +0100590 ComputeStrictFunctionInstanceDescriptor(prototype_mode,
591 arguments_callbacks,
592 caller_callbacks);
Steve Block44f0eee2011-05-26 01:26:41 +0100593 map->set_instance_descriptors(*descriptors);
594 map->set_function_with_prototype(prototype_mode != DONT_ADD_PROTOTYPE);
595 map->set_prototype(*empty_function);
596 return map;
597}
598
599
600void Genesis::CreateStrictModeFunctionMaps(Handle<JSFunction> empty) {
Ben Murdoch85b71792012-04-11 18:30:58 +0100601 // Create the callbacks arrays for ThrowTypeError functions.
602 // The get/set callacks are filled in after the maps are created below.
603 Factory* factory = empty->GetIsolate()->factory();
604 Handle<FixedArray> arguments = factory->NewFixedArray(2, TENURED);
605 Handle<FixedArray> caller = factory->NewFixedArray(2, TENURED);
606
Steve Block44f0eee2011-05-26 01:26:41 +0100607 // Allocate map for the strict mode function instances.
Ben Murdoch8b112d22011-06-08 16:22:53 +0100608 Handle<Map> strict_mode_function_instance_map =
Ben Murdoch85b71792012-04-11 18:30:58 +0100609 CreateStrictModeFunctionMap(
610 ADD_WRITEABLE_PROTOTYPE, empty, arguments, caller);
Steve Block44f0eee2011-05-26 01:26:41 +0100611 global_context()->set_strict_mode_function_instance_map(
Ben Murdoch8b112d22011-06-08 16:22:53 +0100612 *strict_mode_function_instance_map);
Steve Block44f0eee2011-05-26 01:26:41 +0100613
614 // Allocate map for the prototype-less strict mode instances.
Ben Murdoch8b112d22011-06-08 16:22:53 +0100615 Handle<Map> strict_mode_function_without_prototype_map =
Ben Murdoch85b71792012-04-11 18:30:58 +0100616 CreateStrictModeFunctionMap(
617 DONT_ADD_PROTOTYPE, empty, arguments, caller);
Steve Block44f0eee2011-05-26 01:26:41 +0100618 global_context()->set_strict_mode_function_without_prototype_map(
Ben Murdoch8b112d22011-06-08 16:22:53 +0100619 *strict_mode_function_without_prototype_map);
Steve Block44f0eee2011-05-26 01:26:41 +0100620
621 // Allocate map for the strict mode functions. This map is temporary, used
622 // only for processing of builtins.
623 // Later the map is replaced with writable prototype map, allocated below.
Ben Murdoch8b112d22011-06-08 16:22:53 +0100624 Handle<Map> strict_mode_function_map =
Ben Murdoch85b71792012-04-11 18:30:58 +0100625 CreateStrictModeFunctionMap(
626 ADD_READONLY_PROTOTYPE, empty, arguments, caller);
Steve Block44f0eee2011-05-26 01:26:41 +0100627 global_context()->set_strict_mode_function_map(
Ben Murdoch8b112d22011-06-08 16:22:53 +0100628 *strict_mode_function_map);
Steve Block44f0eee2011-05-26 01:26:41 +0100629
630 // The final map for the strict mode functions. Writeable prototype.
631 // This map is installed in MakeFunctionInstancePrototypeWritable.
632 strict_mode_function_instance_map_writable_prototype_ =
Ben Murdoch85b71792012-04-11 18:30:58 +0100633 CreateStrictModeFunctionMap(
634 ADD_WRITEABLE_PROTOTYPE, empty, arguments, caller);
Steve Block44f0eee2011-05-26 01:26:41 +0100635
Ben Murdoch85b71792012-04-11 18:30:58 +0100636 // Create the ThrowTypeError function instance.
637 Handle<JSFunction> throw_function =
638 GetThrowTypeErrorFunction();
Ben Murdoch5d4cdbf2012-04-11 10:23:59 +0100639
Ben Murdoch85b71792012-04-11 18:30:58 +0100640 // Complete the callback fixed arrays.
641 arguments->set(0, *throw_function);
642 arguments->set(1, *throw_function);
643 caller->set(0, *throw_function);
644 caller->set(1, *throw_function);
Steve Block44f0eee2011-05-26 01:26:41 +0100645}
646
647
Ben Murdochb0fe1622011-05-05 13:52:32 +0100648static void AddToWeakGlobalContextList(Context* context) {
649 ASSERT(context->IsGlobalContext());
Ben Murdoch257744e2011-11-30 15:57:28 +0000650 Heap* heap = context->GetIsolate()->heap();
Ben Murdochb0fe1622011-05-05 13:52:32 +0100651#ifdef DEBUG
652 { // NOLINT
653 ASSERT(context->get(Context::NEXT_CONTEXT_LINK)->IsUndefined());
654 // Check that context is not in the list yet.
Steve Block44f0eee2011-05-26 01:26:41 +0100655 for (Object* current = heap->global_contexts_list();
Ben Murdochb0fe1622011-05-05 13:52:32 +0100656 !current->IsUndefined();
657 current = Context::cast(current)->get(Context::NEXT_CONTEXT_LINK)) {
658 ASSERT(current != context);
659 }
660 }
661#endif
Steve Block44f0eee2011-05-26 01:26:41 +0100662 context->set(Context::NEXT_CONTEXT_LINK, heap->global_contexts_list());
663 heap->set_global_contexts_list(context);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100664}
665
666
Andrei Popescu31002712010-02-23 13:46:05 +0000667void Genesis::CreateRoots() {
668 // Allocate the global context FixedArray first and then patch the
669 // closure and extension object later (we need the empty function
670 // and the global object, but in order to create those, we need the
671 // global context).
Ben Murdoch257744e2011-11-30 15:57:28 +0000672 global_context_ = Handle<Context>::cast(isolate()->global_handles()->Create(
673 *factory()->NewGlobalContext()));
Ben Murdochb0fe1622011-05-05 13:52:32 +0100674 AddToWeakGlobalContextList(*global_context_);
Ben Murdoch257744e2011-11-30 15:57:28 +0000675 isolate()->set_context(*global_context());
Andrei Popescu31002712010-02-23 13:46:05 +0000676
677 // Allocate the message listeners object.
678 {
679 v8::NeanderArray listeners;
680 global_context()->set_message_listeners(*listeners.value());
681 }
682}
683
684
685Handle<JSGlobalProxy> Genesis::CreateNewGlobals(
686 v8::Handle<v8::ObjectTemplate> global_template,
687 Handle<Object> global_object,
688 Handle<GlobalObject>* inner_global_out) {
689 // The argument global_template aka data is an ObjectTemplateInfo.
690 // It has a constructor pointer that points at global_constructor which is a
691 // FunctionTemplateInfo.
692 // The global_constructor is used to create or reinitialize the global_proxy.
693 // The global_constructor also has a prototype_template pointer that points at
694 // js_global_template which is an ObjectTemplateInfo.
695 // That in turn has a constructor pointer that points at
696 // js_global_constructor which is a FunctionTemplateInfo.
697 // js_global_constructor is used to make js_global_function
698 // js_global_function is used to make the new inner_global.
699 //
700 // --- G l o b a l ---
701 // Step 1: Create a fresh inner JSGlobalObject.
702 Handle<JSFunction> js_global_function;
703 Handle<ObjectTemplateInfo> js_global_template;
704 if (!global_template.IsEmpty()) {
705 // Get prototype template of the global_template.
706 Handle<ObjectTemplateInfo> data =
707 v8::Utils::OpenHandle(*global_template);
708 Handle<FunctionTemplateInfo> global_constructor =
709 Handle<FunctionTemplateInfo>(
710 FunctionTemplateInfo::cast(data->constructor()));
711 Handle<Object> proto_template(global_constructor->prototype_template());
712 if (!proto_template->IsUndefined()) {
713 js_global_template =
714 Handle<ObjectTemplateInfo>::cast(proto_template);
715 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000716 }
717
Andrei Popescu31002712010-02-23 13:46:05 +0000718 if (js_global_template.is_null()) {
Ben Murdoch257744e2011-11-30 15:57:28 +0000719 Handle<String> name = Handle<String>(heap()->empty_symbol());
720 Handle<Code> code = Handle<Code>(isolate()->builtins()->builtin(
Steve Block44f0eee2011-05-26 01:26:41 +0100721 Builtins::kIllegal));
Andrei Popescu31002712010-02-23 13:46:05 +0000722 js_global_function =
Ben Murdoch257744e2011-11-30 15:57:28 +0000723 factory()->NewFunction(name, JS_GLOBAL_OBJECT_TYPE,
724 JSGlobalObject::kSize, code, true);
Andrei Popescu31002712010-02-23 13:46:05 +0000725 // Change the constructor property of the prototype of the
726 // hidden global function to refer to the Object function.
727 Handle<JSObject> prototype =
728 Handle<JSObject>(
729 JSObject::cast(js_global_function->instance_prototype()));
Ben Murdoch85b71792012-04-11 18:30:58 +0100730 SetLocalPropertyNoThrow(
731 prototype,
732 factory()->constructor_symbol(),
733 isolate()->object_function(),
734 NONE);
Andrei Popescu31002712010-02-23 13:46:05 +0000735 } else {
736 Handle<FunctionTemplateInfo> js_global_constructor(
737 FunctionTemplateInfo::cast(js_global_template->constructor()));
738 js_global_function =
Ben Murdoch257744e2011-11-30 15:57:28 +0000739 factory()->CreateApiFunction(js_global_constructor,
740 factory()->InnerGlobalObject);
Steve Blocka7e24c12009-10-30 11:49:00 +0000741 }
742
Andrei Popescu31002712010-02-23 13:46:05 +0000743 js_global_function->initial_map()->set_is_hidden_prototype();
744 Handle<GlobalObject> inner_global =
Ben Murdoch257744e2011-11-30 15:57:28 +0000745 factory()->NewGlobalObject(js_global_function);
Andrei Popescu31002712010-02-23 13:46:05 +0000746 if (inner_global_out != NULL) {
747 *inner_global_out = inner_global;
748 }
749
750 // Step 2: create or re-initialize the global proxy object.
751 Handle<JSFunction> global_proxy_function;
752 if (global_template.IsEmpty()) {
Ben Murdoch257744e2011-11-30 15:57:28 +0000753 Handle<String> name = Handle<String>(heap()->empty_symbol());
754 Handle<Code> code = Handle<Code>(isolate()->builtins()->builtin(
Steve Block44f0eee2011-05-26 01:26:41 +0100755 Builtins::kIllegal));
Andrei Popescu31002712010-02-23 13:46:05 +0000756 global_proxy_function =
Ben Murdoch257744e2011-11-30 15:57:28 +0000757 factory()->NewFunction(name, JS_GLOBAL_PROXY_TYPE,
758 JSGlobalProxy::kSize, code, true);
Andrei Popescu31002712010-02-23 13:46:05 +0000759 } else {
760 Handle<ObjectTemplateInfo> data =
761 v8::Utils::OpenHandle(*global_template);
762 Handle<FunctionTemplateInfo> global_constructor(
763 FunctionTemplateInfo::cast(data->constructor()));
764 global_proxy_function =
Ben Murdoch257744e2011-11-30 15:57:28 +0000765 factory()->CreateApiFunction(global_constructor,
766 factory()->OuterGlobalObject);
Andrei Popescu31002712010-02-23 13:46:05 +0000767 }
768
Ben Murdoch257744e2011-11-30 15:57:28 +0000769 Handle<String> global_name = factory()->LookupAsciiSymbol("global");
Andrei Popescu31002712010-02-23 13:46:05 +0000770 global_proxy_function->shared()->set_instance_class_name(*global_name);
771 global_proxy_function->initial_map()->set_is_access_check_needed(true);
772
773 // Set global_proxy.__proto__ to js_global after ConfigureGlobalObjects
774 // Return the global proxy.
775
776 if (global_object.location() != NULL) {
777 ASSERT(global_object->IsJSGlobalProxy());
778 return ReinitializeJSGlobalProxy(
779 global_proxy_function,
780 Handle<JSGlobalProxy>::cast(global_object));
781 } else {
782 return Handle<JSGlobalProxy>::cast(
Ben Murdoch257744e2011-11-30 15:57:28 +0000783 factory()->NewJSObject(global_proxy_function, TENURED));
Andrei Popescu31002712010-02-23 13:46:05 +0000784 }
785}
786
787
788void Genesis::HookUpGlobalProxy(Handle<GlobalObject> inner_global,
789 Handle<JSGlobalProxy> global_proxy) {
790 // Set the global context for the global object.
791 inner_global->set_global_context(*global_context());
792 inner_global->set_global_receiver(*global_proxy);
793 global_proxy->set_context(*global_context());
794 global_context()->set_global_proxy(*global_proxy);
795}
796
797
Andrei Popescu402d9372010-02-26 13:31:12 +0000798void Genesis::HookUpInnerGlobal(Handle<GlobalObject> inner_global) {
799 Handle<GlobalObject> inner_global_from_snapshot(
800 GlobalObject::cast(global_context_->extension()));
801 Handle<JSBuiltinsObject> builtins_global(global_context_->builtins());
802 global_context_->set_extension(*inner_global);
803 global_context_->set_global(*inner_global);
804 global_context_->set_security_token(*inner_global);
805 static const PropertyAttributes attributes =
806 static_cast<PropertyAttributes>(READ_ONLY | DONT_DELETE);
807 ForceSetProperty(builtins_global,
Ben Murdoch257744e2011-11-30 15:57:28 +0000808 factory()->LookupAsciiSymbol("global"),
Andrei Popescu402d9372010-02-26 13:31:12 +0000809 inner_global,
810 attributes);
Ben Murdoch85b71792012-04-11 18:30:58 +0100811 // Setup the reference from the global object to the builtins object.
Andrei Popescu402d9372010-02-26 13:31:12 +0000812 JSGlobalObject::cast(*inner_global)->set_builtins(*builtins_global);
813 TransferNamedProperties(inner_global_from_snapshot, inner_global);
814 TransferIndexedProperties(inner_global_from_snapshot, inner_global);
815}
816
817
818// This is only called if we are not using snapshots. The equivalent
819// work in the snapshot case is done in HookUpInnerGlobal.
Ben Murdoch85b71792012-04-11 18:30:58 +0100820void Genesis::InitializeGlobal(Handle<GlobalObject> inner_global,
Andrei Popescu31002712010-02-23 13:46:05 +0000821 Handle<JSFunction> empty_function) {
822 // --- G l o b a l C o n t e x t ---
823 // Use the empty function as closure (no scope info).
824 global_context()->set_closure(*empty_function);
Andrei Popescu31002712010-02-23 13:46:05 +0000825 global_context()->set_previous(NULL);
826 // Set extension and global object.
827 global_context()->set_extension(*inner_global);
828 global_context()->set_global(*inner_global);
829 // Security setup: Set the security token of the global object to
830 // its the inner global. This makes the security check between two
831 // different contexts fail by default even in case of global
832 // object reinitialization.
833 global_context()->set_security_token(*inner_global);
834
Ben Murdoch257744e2011-11-30 15:57:28 +0000835 Isolate* isolate = inner_global->GetIsolate();
Steve Block44f0eee2011-05-26 01:26:41 +0100836 Factory* factory = isolate->factory();
837 Heap* heap = isolate->heap();
838
839 Handle<String> object_name = Handle<String>(heap->Object_symbol());
Ben Murdoch85b71792012-04-11 18:30:58 +0100840 SetLocalPropertyNoThrow(inner_global, object_name,
841 isolate->object_function(), DONT_ENUM);
Andrei Popescu31002712010-02-23 13:46:05 +0000842
Steve Blocka7e24c12009-10-30 11:49:00 +0000843 Handle<JSObject> global = Handle<JSObject>(global_context()->global());
844
845 // Install global Function object
846 InstallFunction(global, "Function", JS_FUNCTION_TYPE, JSFunction::kSize,
Steve Block44f0eee2011-05-26 01:26:41 +0100847 empty_function, Builtins::kIllegal, true); // ECMA native.
Steve Blocka7e24c12009-10-30 11:49:00 +0000848
849 { // --- A r r a y ---
850 Handle<JSFunction> array_function =
851 InstallFunction(global, "Array", JS_ARRAY_TYPE, JSArray::kSize,
Steve Block44f0eee2011-05-26 01:26:41 +0100852 isolate->initial_object_prototype(),
853 Builtins::kArrayCode, true);
Steve Blocka7e24c12009-10-30 11:49:00 +0000854 array_function->shared()->set_construct_stub(
Steve Block44f0eee2011-05-26 01:26:41 +0100855 isolate->builtins()->builtin(Builtins::kArrayConstructCode));
Steve Blocka7e24c12009-10-30 11:49:00 +0000856 array_function->shared()->DontAdaptArguments();
857
858 // This seems a bit hackish, but we need to make sure Array.length
859 // is 1.
860 array_function->shared()->set_length(1);
861 Handle<DescriptorArray> array_descriptors =
Ben Murdoch257744e2011-11-30 15:57:28 +0000862 factory->CopyAppendForeignDescriptor(
Steve Block44f0eee2011-05-26 01:26:41 +0100863 factory->empty_descriptor_array(),
864 factory->length_symbol(),
Ben Murdoch257744e2011-11-30 15:57:28 +0000865 factory->NewForeign(&Accessors::ArrayLength),
Steve Blocka7e24c12009-10-30 11:49:00 +0000866 static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE));
867
Ben Murdoch85b71792012-04-11 18:30:58 +0100868 // Cache the fast JavaScript array map
869 global_context()->set_js_array_map(array_function->initial_map());
870 global_context()->js_array_map()->set_instance_descriptors(
871 *array_descriptors);
Steve Blocka7e24c12009-10-30 11:49:00 +0000872 // array_function is used internally. JS code creating array object should
873 // search for the 'Array' property on the global object and use that one
874 // as the constructor. 'Array' property on a global object can be
875 // overwritten by JS code.
876 global_context()->set_array_function(*array_function);
877 }
878
879 { // --- N u m b e r ---
880 Handle<JSFunction> number_fun =
881 InstallFunction(global, "Number", JS_VALUE_TYPE, JSValue::kSize,
Steve Block44f0eee2011-05-26 01:26:41 +0100882 isolate->initial_object_prototype(),
883 Builtins::kIllegal, true);
Steve Blocka7e24c12009-10-30 11:49:00 +0000884 global_context()->set_number_function(*number_fun);
885 }
886
887 { // --- B o o l e a n ---
888 Handle<JSFunction> boolean_fun =
889 InstallFunction(global, "Boolean", JS_VALUE_TYPE, JSValue::kSize,
Steve Block44f0eee2011-05-26 01:26:41 +0100890 isolate->initial_object_prototype(),
891 Builtins::kIllegal, true);
Steve Blocka7e24c12009-10-30 11:49:00 +0000892 global_context()->set_boolean_function(*boolean_fun);
893 }
894
895 { // --- S t r i n g ---
896 Handle<JSFunction> string_fun =
897 InstallFunction(global, "String", JS_VALUE_TYPE, JSValue::kSize,
Steve Block44f0eee2011-05-26 01:26:41 +0100898 isolate->initial_object_prototype(),
899 Builtins::kIllegal, true);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100900 string_fun->shared()->set_construct_stub(
Steve Block44f0eee2011-05-26 01:26:41 +0100901 isolate->builtins()->builtin(Builtins::kStringConstructCode));
Steve Blocka7e24c12009-10-30 11:49:00 +0000902 global_context()->set_string_function(*string_fun);
903 // Add 'length' property to strings.
904 Handle<DescriptorArray> string_descriptors =
Ben Murdoch257744e2011-11-30 15:57:28 +0000905 factory->CopyAppendForeignDescriptor(
Steve Block44f0eee2011-05-26 01:26:41 +0100906 factory->empty_descriptor_array(),
907 factory->length_symbol(),
Ben Murdoch257744e2011-11-30 15:57:28 +0000908 factory->NewForeign(&Accessors::StringLength),
Steve Blocka7e24c12009-10-30 11:49:00 +0000909 static_cast<PropertyAttributes>(DONT_ENUM |
910 DONT_DELETE |
911 READ_ONLY));
912
913 Handle<Map> string_map =
914 Handle<Map>(global_context()->string_function()->initial_map());
915 string_map->set_instance_descriptors(*string_descriptors);
916 }
917
918 { // --- D a t e ---
919 // Builtin functions for Date.prototype.
920 Handle<JSFunction> date_fun =
Ben Murdoch85b71792012-04-11 18:30:58 +0100921 InstallFunction(global, "Date", JS_VALUE_TYPE, JSValue::kSize,
Steve Block44f0eee2011-05-26 01:26:41 +0100922 isolate->initial_object_prototype(),
923 Builtins::kIllegal, true);
Steve Blocka7e24c12009-10-30 11:49:00 +0000924
925 global_context()->set_date_function(*date_fun);
926 }
927
928
929 { // -- R e g E x p
930 // Builtin functions for RegExp.prototype.
931 Handle<JSFunction> regexp_fun =
932 InstallFunction(global, "RegExp", JS_REGEXP_TYPE, JSRegExp::kSize,
Steve Block44f0eee2011-05-26 01:26:41 +0100933 isolate->initial_object_prototype(),
934 Builtins::kIllegal, true);
Steve Blocka7e24c12009-10-30 11:49:00 +0000935 global_context()->set_regexp_function(*regexp_fun);
Steve Block6ded16b2010-05-10 14:33:55 +0100936
937 ASSERT(regexp_fun->has_initial_map());
938 Handle<Map> initial_map(regexp_fun->initial_map());
939
940 ASSERT_EQ(0, initial_map->inobject_properties());
941
Steve Block44f0eee2011-05-26 01:26:41 +0100942 Handle<DescriptorArray> descriptors = factory->NewDescriptorArray(5);
Steve Block6ded16b2010-05-10 14:33:55 +0100943 PropertyAttributes final =
944 static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE | READ_ONLY);
945 int enum_index = 0;
946 {
947 // ECMA-262, section 15.10.7.1.
Steve Block44f0eee2011-05-26 01:26:41 +0100948 FieldDescriptor field(heap->source_symbol(),
Steve Block6ded16b2010-05-10 14:33:55 +0100949 JSRegExp::kSourceFieldIndex,
950 final,
951 enum_index++);
Ben Murdoch85b71792012-04-11 18:30:58 +0100952 descriptors->Set(0, &field);
Steve Block6ded16b2010-05-10 14:33:55 +0100953 }
954 {
955 // ECMA-262, section 15.10.7.2.
Steve Block44f0eee2011-05-26 01:26:41 +0100956 FieldDescriptor field(heap->global_symbol(),
Steve Block6ded16b2010-05-10 14:33:55 +0100957 JSRegExp::kGlobalFieldIndex,
958 final,
959 enum_index++);
Ben Murdoch85b71792012-04-11 18:30:58 +0100960 descriptors->Set(1, &field);
Steve Block6ded16b2010-05-10 14:33:55 +0100961 }
962 {
963 // ECMA-262, section 15.10.7.3.
Steve Block44f0eee2011-05-26 01:26:41 +0100964 FieldDescriptor field(heap->ignore_case_symbol(),
Steve Block6ded16b2010-05-10 14:33:55 +0100965 JSRegExp::kIgnoreCaseFieldIndex,
966 final,
967 enum_index++);
Ben Murdoch85b71792012-04-11 18:30:58 +0100968 descriptors->Set(2, &field);
Steve Block6ded16b2010-05-10 14:33:55 +0100969 }
970 {
971 // ECMA-262, section 15.10.7.4.
Steve Block44f0eee2011-05-26 01:26:41 +0100972 FieldDescriptor field(heap->multiline_symbol(),
Steve Block6ded16b2010-05-10 14:33:55 +0100973 JSRegExp::kMultilineFieldIndex,
974 final,
975 enum_index++);
Ben Murdoch85b71792012-04-11 18:30:58 +0100976 descriptors->Set(3, &field);
Steve Block6ded16b2010-05-10 14:33:55 +0100977 }
978 {
979 // ECMA-262, section 15.10.7.5.
980 PropertyAttributes writable =
981 static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE);
Steve Block44f0eee2011-05-26 01:26:41 +0100982 FieldDescriptor field(heap->last_index_symbol(),
Steve Block6ded16b2010-05-10 14:33:55 +0100983 JSRegExp::kLastIndexFieldIndex,
984 writable,
985 enum_index++);
Ben Murdoch85b71792012-04-11 18:30:58 +0100986 descriptors->Set(4, &field);
Steve Block6ded16b2010-05-10 14:33:55 +0100987 }
988 descriptors->SetNextEnumerationIndex(enum_index);
Ben Murdoch85b71792012-04-11 18:30:58 +0100989 descriptors->Sort();
Steve Block6ded16b2010-05-10 14:33:55 +0100990
991 initial_map->set_inobject_properties(5);
992 initial_map->set_pre_allocated_property_fields(5);
993 initial_map->set_unused_property_fields(0);
994 initial_map->set_instance_size(
995 initial_map->instance_size() + 5 * kPointerSize);
996 initial_map->set_instance_descriptors(*descriptors);
Iain Merrick75681382010-08-19 15:07:18 +0100997 initial_map->set_visitor_id(StaticVisitorBase::GetVisitorId(*initial_map));
Steve Blocka7e24c12009-10-30 11:49:00 +0000998 }
999
1000 { // -- J S O N
Steve Block44f0eee2011-05-26 01:26:41 +01001001 Handle<String> name = factory->NewStringFromAscii(CStrVector("JSON"));
Ben Murdoch85b71792012-04-11 18:30:58 +01001002 Handle<JSFunction> cons = factory->NewFunction(
1003 name,
1004 factory->the_hole_value());
1005 cons->SetInstancePrototype(global_context()->initial_object_prototype());
Steve Blocka7e24c12009-10-30 11:49:00 +00001006 cons->SetInstanceClassName(*name);
Steve Block44f0eee2011-05-26 01:26:41 +01001007 Handle<JSObject> json_object = factory->NewJSObject(cons, TENURED);
Steve Blocka7e24c12009-10-30 11:49:00 +00001008 ASSERT(json_object->IsJSObject());
Ben Murdoch85b71792012-04-11 18:30:58 +01001009 SetLocalPropertyNoThrow(global, name, json_object, DONT_ENUM);
Steve Blocka7e24c12009-10-30 11:49:00 +00001010 global_context()->set_json_object(*json_object);
1011 }
1012
1013 { // --- arguments_boilerplate_
1014 // Make sure we can recognize argument objects at runtime.
1015 // This is done by introducing an anonymous function with
1016 // class_name equals 'Arguments'.
Steve Block44f0eee2011-05-26 01:26:41 +01001017 Handle<String> symbol = factory->LookupAsciiSymbol("Arguments");
1018 Handle<Code> code = Handle<Code>(
1019 isolate->builtins()->builtin(Builtins::kIllegal));
Steve Blocka7e24c12009-10-30 11:49:00 +00001020 Handle<JSObject> prototype =
1021 Handle<JSObject>(
1022 JSObject::cast(global_context()->object_function()->prototype()));
1023
1024 Handle<JSFunction> function =
Steve Block44f0eee2011-05-26 01:26:41 +01001025 factory->NewFunctionWithPrototype(symbol,
Steve Blocka7e24c12009-10-30 11:49:00 +00001026 JS_OBJECT_TYPE,
1027 JSObject::kHeaderSize,
1028 prototype,
1029 code,
1030 false);
1031 ASSERT(!function->has_initial_map());
1032 function->shared()->set_instance_class_name(*symbol);
1033 function->shared()->set_expected_nof_properties(2);
Steve Block44f0eee2011-05-26 01:26:41 +01001034 Handle<JSObject> result = factory->NewJSObject(function);
Steve Blocka7e24c12009-10-30 11:49:00 +00001035
1036 global_context()->set_arguments_boilerplate(*result);
Steve Block44f0eee2011-05-26 01:26:41 +01001037 // Note: length must be added as the first property and
1038 // callee must be added as the second property.
Ben Murdoch85b71792012-04-11 18:30:58 +01001039 SetLocalPropertyNoThrow(result, factory->length_symbol(),
1040 factory->undefined_value(),
1041 DONT_ENUM);
1042 SetLocalPropertyNoThrow(result, factory->callee_symbol(),
1043 factory->undefined_value(),
1044 DONT_ENUM);
Steve Blocka7e24c12009-10-30 11:49:00 +00001045
1046#ifdef DEBUG
Ben Murdoch85b71792012-04-11 18:30:58 +01001047 LookupResult lookup;
Steve Block44f0eee2011-05-26 01:26:41 +01001048 result->LocalLookup(heap->callee_symbol(), &lookup);
Ben Murdoch85b71792012-04-11 18:30:58 +01001049 ASSERT(lookup.IsProperty() && (lookup.type() == FIELD));
Steve Block44f0eee2011-05-26 01:26:41 +01001050 ASSERT(lookup.GetFieldIndex() == Heap::kArgumentsCalleeIndex);
Steve Blocka7e24c12009-10-30 11:49:00 +00001051
Steve Block44f0eee2011-05-26 01:26:41 +01001052 result->LocalLookup(heap->length_symbol(), &lookup);
Ben Murdoch85b71792012-04-11 18:30:58 +01001053 ASSERT(lookup.IsProperty() && (lookup.type() == FIELD));
Steve Block44f0eee2011-05-26 01:26:41 +01001054 ASSERT(lookup.GetFieldIndex() == Heap::kArgumentsLengthIndex);
Steve Blocka7e24c12009-10-30 11:49:00 +00001055
Steve Block44f0eee2011-05-26 01:26:41 +01001056 ASSERT(result->map()->inobject_properties() > Heap::kArgumentsCalleeIndex);
1057 ASSERT(result->map()->inobject_properties() > Heap::kArgumentsLengthIndex);
1058
1059 // Check the state of the object.
1060 ASSERT(result->HasFastProperties());
1061 ASSERT(result->HasFastElements());
1062#endif
1063 }
1064
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001065 { // --- aliased_arguments_boilerplate_
Ben Murdoch85b71792012-04-11 18:30:58 +01001066 Handle<Map> old_map(global_context()->arguments_boilerplate()->map());
1067 Handle<Map> new_map = factory->CopyMapDropTransitions(old_map);
1068 new_map->set_pre_allocated_property_fields(2);
1069 Handle<JSObject> result = factory->NewJSObjectFromMap(new_map);
1070 new_map->set_elements_kind(NON_STRICT_ARGUMENTS_ELEMENTS);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001071 // Set up a well-formed parameter map to make assertions happy.
1072 Handle<FixedArray> elements = factory->NewFixedArray(2);
1073 elements->set_map(heap->non_strict_arguments_elements_map());
1074 Handle<FixedArray> array;
1075 array = factory->NewFixedArray(0);
1076 elements->set(0, *array);
1077 array = factory->NewFixedArray(0);
1078 elements->set(1, *array);
1079 result->set_elements(*elements);
1080 global_context()->set_aliased_arguments_boilerplate(*result);
1081 }
1082
Steve Block44f0eee2011-05-26 01:26:41 +01001083 { // --- strict mode arguments boilerplate
1084 const PropertyAttributes attributes =
1085 static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE | READ_ONLY);
1086
1087 // Create the ThrowTypeError functions.
Ben Murdoch85b71792012-04-11 18:30:58 +01001088 Handle<FixedArray> callee = factory->NewFixedArray(2, TENURED);
1089 Handle<FixedArray> caller = factory->NewFixedArray(2, TENURED);
Steve Block44f0eee2011-05-26 01:26:41 +01001090
Ben Murdoch257744e2011-11-30 15:57:28 +00001091 Handle<JSFunction> throw_function =
1092 GetThrowTypeErrorFunction();
Steve Block44f0eee2011-05-26 01:26:41 +01001093
1094 // Install the ThrowTypeError functions.
Ben Murdoch85b71792012-04-11 18:30:58 +01001095 callee->set(0, *throw_function);
1096 callee->set(1, *throw_function);
1097 caller->set(0, *throw_function);
1098 caller->set(1, *throw_function);
Steve Block44f0eee2011-05-26 01:26:41 +01001099
1100 // Create the descriptor array for the arguments object.
1101 Handle<DescriptorArray> descriptors = factory->NewDescriptorArray(3);
1102 { // length
1103 FieldDescriptor d(*factory->length_symbol(), 0, DONT_ENUM);
Ben Murdoch85b71792012-04-11 18:30:58 +01001104 descriptors->Set(0, &d);
Steve Block44f0eee2011-05-26 01:26:41 +01001105 }
1106 { // callee
1107 CallbacksDescriptor d(*factory->callee_symbol(), *callee, attributes);
Ben Murdoch85b71792012-04-11 18:30:58 +01001108 descriptors->Set(1, &d);
Steve Block44f0eee2011-05-26 01:26:41 +01001109 }
1110 { // caller
1111 CallbacksDescriptor d(*factory->caller_symbol(), *caller, attributes);
Ben Murdoch85b71792012-04-11 18:30:58 +01001112 descriptors->Set(2, &d);
Steve Block44f0eee2011-05-26 01:26:41 +01001113 }
Ben Murdoch85b71792012-04-11 18:30:58 +01001114 descriptors->Sort();
Steve Block44f0eee2011-05-26 01:26:41 +01001115
1116 // Create the map. Allocate one in-object field for length.
1117 Handle<Map> map = factory->NewMap(JS_OBJECT_TYPE,
1118 Heap::kArgumentsObjectSizeStrict);
1119 map->set_instance_descriptors(*descriptors);
1120 map->set_function_with_prototype(true);
1121 map->set_prototype(global_context()->object_function()->prototype());
1122 map->set_pre_allocated_property_fields(1);
1123 map->set_inobject_properties(1);
1124
1125 // Copy constructor from the non-strict arguments boilerplate.
1126 map->set_constructor(
1127 global_context()->arguments_boilerplate()->map()->constructor());
1128
1129 // Allocate the arguments boilerplate object.
1130 Handle<JSObject> result = factory->NewJSObjectFromMap(map);
1131 global_context()->set_strict_mode_arguments_boilerplate(*result);
1132
1133 // Add length property only for strict mode boilerplate.
Ben Murdoch85b71792012-04-11 18:30:58 +01001134 SetLocalPropertyNoThrow(result, factory->length_symbol(),
1135 factory->undefined_value(),
1136 DONT_ENUM);
Steve Block44f0eee2011-05-26 01:26:41 +01001137
1138#ifdef DEBUG
Ben Murdoch85b71792012-04-11 18:30:58 +01001139 LookupResult lookup;
Steve Block44f0eee2011-05-26 01:26:41 +01001140 result->LocalLookup(heap->length_symbol(), &lookup);
Ben Murdoch85b71792012-04-11 18:30:58 +01001141 ASSERT(lookup.IsProperty() && (lookup.type() == FIELD));
Steve Block44f0eee2011-05-26 01:26:41 +01001142 ASSERT(lookup.GetFieldIndex() == Heap::kArgumentsLengthIndex);
1143
1144 ASSERT(result->map()->inobject_properties() > Heap::kArgumentsLengthIndex);
Steve Blocka7e24c12009-10-30 11:49:00 +00001145
1146 // Check the state of the object.
1147 ASSERT(result->HasFastProperties());
1148 ASSERT(result->HasFastElements());
1149#endif
1150 }
1151
1152 { // --- context extension
1153 // Create a function for the context extension objects.
Steve Block44f0eee2011-05-26 01:26:41 +01001154 Handle<Code> code = Handle<Code>(
1155 isolate->builtins()->builtin(Builtins::kIllegal));
Steve Blocka7e24c12009-10-30 11:49:00 +00001156 Handle<JSFunction> context_extension_fun =
Steve Block44f0eee2011-05-26 01:26:41 +01001157 factory->NewFunction(factory->empty_symbol(),
Steve Blocka7e24c12009-10-30 11:49:00 +00001158 JS_CONTEXT_EXTENSION_OBJECT_TYPE,
1159 JSObject::kHeaderSize,
1160 code,
1161 true);
1162
Steve Block44f0eee2011-05-26 01:26:41 +01001163 Handle<String> name = factory->LookupAsciiSymbol("context_extension");
Steve Blocka7e24c12009-10-30 11:49:00 +00001164 context_extension_fun->shared()->set_instance_class_name(*name);
1165 global_context()->set_context_extension_function(*context_extension_fun);
1166 }
1167
1168
1169 {
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001170 // Set up the call-as-function delegate.
Steve Blocka7e24c12009-10-30 11:49:00 +00001171 Handle<Code> code =
Steve Block44f0eee2011-05-26 01:26:41 +01001172 Handle<Code>(isolate->builtins()->builtin(
1173 Builtins::kHandleApiCallAsFunction));
Steve Blocka7e24c12009-10-30 11:49:00 +00001174 Handle<JSFunction> delegate =
Steve Block44f0eee2011-05-26 01:26:41 +01001175 factory->NewFunction(factory->empty_symbol(), JS_OBJECT_TYPE,
Steve Blocka7e24c12009-10-30 11:49:00 +00001176 JSObject::kHeaderSize, code, true);
1177 global_context()->set_call_as_function_delegate(*delegate);
1178 delegate->shared()->DontAdaptArguments();
1179 }
1180
1181 {
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001182 // Set up the call-as-constructor delegate.
Steve Blocka7e24c12009-10-30 11:49:00 +00001183 Handle<Code> code =
Steve Block44f0eee2011-05-26 01:26:41 +01001184 Handle<Code>(isolate->builtins()->builtin(
1185 Builtins::kHandleApiCallAsConstructor));
Steve Blocka7e24c12009-10-30 11:49:00 +00001186 Handle<JSFunction> delegate =
Steve Block44f0eee2011-05-26 01:26:41 +01001187 factory->NewFunction(factory->empty_symbol(), JS_OBJECT_TYPE,
Steve Blocka7e24c12009-10-30 11:49:00 +00001188 JSObject::kHeaderSize, code, true);
1189 global_context()->set_call_as_constructor_delegate(*delegate);
1190 delegate->shared()->DontAdaptArguments();
1191 }
1192
Steve Blocka7e24c12009-10-30 11:49:00 +00001193 // Initialize the out of memory slot.
Steve Block44f0eee2011-05-26 01:26:41 +01001194 global_context()->set_out_of_memory(heap->false_value());
Steve Blocka7e24c12009-10-30 11:49:00 +00001195
1196 // Initialize the data slot.
Steve Block44f0eee2011-05-26 01:26:41 +01001197 global_context()->set_data(heap->undefined_value());
Steve Blocka7e24c12009-10-30 11:49:00 +00001198}
1199
1200
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001201void Genesis::InitializeExperimentalGlobal() {
1202 Handle<JSObject> global = Handle<JSObject>(global_context()->global());
1203
1204 // TODO(mstarzinger): Move this into Genesis::InitializeGlobal once we no
Ben Murdoch85b71792012-04-11 18:30:58 +01001205 // longer need to live behind a flag, so WeakMap gets added to the snapshot.
1206 if (FLAG_harmony_weakmaps) { // -- W e a k M a p
1207 Handle<JSObject> prototype =
1208 factory()->NewJSObject(isolate()->object_function(), TENURED);
1209 InstallFunction(global, "WeakMap", JS_WEAK_MAP_TYPE, JSWeakMap::kSize,
1210 prototype, Builtins::kIllegal, true);
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001211 }
1212}
1213
1214
Ben Murdoch257744e2011-11-30 15:57:28 +00001215bool Genesis::CompileBuiltin(Isolate* isolate, int index) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001216 Vector<const char> name = Natives::GetScriptName(index);
Steve Block44f0eee2011-05-26 01:26:41 +01001217 Handle<String> source_code =
Ben Murdoch257744e2011-11-30 15:57:28 +00001218 isolate->bootstrapper()->NativesSourceLookup(index);
1219 return CompileNative(name, source_code);
1220}
1221
1222
1223bool Genesis::CompileExperimentalBuiltin(Isolate* isolate, int index) {
1224 Vector<const char> name = ExperimentalNatives::GetScriptName(index);
1225 Factory* factory = isolate->factory();
1226 Handle<String> source_code =
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001227 factory->NewStringFromAscii(
1228 ExperimentalNatives::GetRawScriptSource(index));
Steve Blocka7e24c12009-10-30 11:49:00 +00001229 return CompileNative(name, source_code);
1230}
1231
1232
1233bool Genesis::CompileNative(Vector<const char> name, Handle<String> source) {
1234 HandleScope scope;
Ben Murdoch257744e2011-11-30 15:57:28 +00001235 Isolate* isolate = source->GetIsolate();
Steve Blocka7e24c12009-10-30 11:49:00 +00001236#ifdef ENABLE_DEBUGGER_SUPPORT
Steve Block44f0eee2011-05-26 01:26:41 +01001237 isolate->debugger()->set_compiling_natives(true);
Steve Blocka7e24c12009-10-30 11:49:00 +00001238#endif
Andrei Popescu31002712010-02-23 13:46:05 +00001239 bool result = CompileScriptCached(name,
1240 source,
1241 NULL,
1242 NULL,
Steve Block44f0eee2011-05-26 01:26:41 +01001243 Handle<Context>(isolate->context()),
Andrei Popescu31002712010-02-23 13:46:05 +00001244 true);
Steve Block44f0eee2011-05-26 01:26:41 +01001245 ASSERT(isolate->has_pending_exception() != result);
1246 if (!result) isolate->clear_pending_exception();
Steve Blocka7e24c12009-10-30 11:49:00 +00001247#ifdef ENABLE_DEBUGGER_SUPPORT
Steve Block44f0eee2011-05-26 01:26:41 +01001248 isolate->debugger()->set_compiling_natives(false);
Steve Blocka7e24c12009-10-30 11:49:00 +00001249#endif
1250 return result;
1251}
1252
1253
1254bool Genesis::CompileScriptCached(Vector<const char> name,
1255 Handle<String> source,
1256 SourceCodeCache* cache,
1257 v8::Extension* extension,
Andrei Popescu31002712010-02-23 13:46:05 +00001258 Handle<Context> top_context,
Steve Blocka7e24c12009-10-30 11:49:00 +00001259 bool use_runtime_context) {
Ben Murdoch257744e2011-11-30 15:57:28 +00001260 Factory* factory = source->GetIsolate()->factory();
Steve Blocka7e24c12009-10-30 11:49:00 +00001261 HandleScope scope;
Steve Block6ded16b2010-05-10 14:33:55 +01001262 Handle<SharedFunctionInfo> function_info;
Steve Blocka7e24c12009-10-30 11:49:00 +00001263
1264 // If we can't find the function in the cache, we compile a new
1265 // function and insert it into the cache.
Steve Block6ded16b2010-05-10 14:33:55 +01001266 if (cache == NULL || !cache->Lookup(name, &function_info)) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001267 ASSERT(source->IsAsciiRepresentation());
Steve Block44f0eee2011-05-26 01:26:41 +01001268 Handle<String> script_name = factory->NewStringFromUtf8(name);
Steve Block6ded16b2010-05-10 14:33:55 +01001269 function_info = Compiler::Compile(
Andrei Popescu31002712010-02-23 13:46:05 +00001270 source,
1271 script_name,
1272 0,
1273 0,
1274 extension,
1275 NULL,
Andrei Popescu402d9372010-02-26 13:31:12 +00001276 Handle<String>::null(),
Andrei Popescu31002712010-02-23 13:46:05 +00001277 use_runtime_context ? NATIVES_CODE : NOT_NATIVES_CODE);
Steve Block6ded16b2010-05-10 14:33:55 +01001278 if (function_info.is_null()) return false;
1279 if (cache != NULL) cache->Add(name, function_info);
Steve Blocka7e24c12009-10-30 11:49:00 +00001280 }
1281
Ben Murdoch85b71792012-04-11 18:30:58 +01001282 // Setup the function context. Conceptually, we should clone the
Steve Blocka7e24c12009-10-30 11:49:00 +00001283 // function before overwriting the context but since we're in a
1284 // single-threaded environment it is not strictly necessary.
Andrei Popescu31002712010-02-23 13:46:05 +00001285 ASSERT(top_context->IsGlobalContext());
Steve Blocka7e24c12009-10-30 11:49:00 +00001286 Handle<Context> context =
1287 Handle<Context>(use_runtime_context
Andrei Popescu31002712010-02-23 13:46:05 +00001288 ? Handle<Context>(top_context->runtime_context())
1289 : top_context);
Steve Blocka7e24c12009-10-30 11:49:00 +00001290 Handle<JSFunction> fun =
Steve Block44f0eee2011-05-26 01:26:41 +01001291 factory->NewFunctionFromSharedFunctionInfo(function_info, context);
Steve Blocka7e24c12009-10-30 11:49:00 +00001292
Leon Clarke4515c472010-02-03 11:58:03 +00001293 // Call function using either the runtime object or the global
Steve Blocka7e24c12009-10-30 11:49:00 +00001294 // object as the receiver. Provide no parameters.
1295 Handle<Object> receiver =
1296 Handle<Object>(use_runtime_context
Andrei Popescu31002712010-02-23 13:46:05 +00001297 ? top_context->builtins()
1298 : top_context->global());
Steve Blocka7e24c12009-10-30 11:49:00 +00001299 bool has_pending_exception;
Ben Murdoch257744e2011-11-30 15:57:28 +00001300 Execution::Call(fun, receiver, 0, NULL, &has_pending_exception);
Steve Blocka7e24c12009-10-30 11:49:00 +00001301 if (has_pending_exception) return false;
Andrei Popescu402d9372010-02-26 13:31:12 +00001302 return true;
Steve Blocka7e24c12009-10-30 11:49:00 +00001303}
1304
1305
Ben Murdoch257744e2011-11-30 15:57:28 +00001306#define INSTALL_NATIVE(Type, name, var) \
1307 Handle<String> var##_name = factory()->LookupAsciiSymbol(name); \
1308 Object* var##_native = \
1309 global_context()->builtins()->GetPropertyNoExceptionThrown( \
1310 *var##_name); \
Ben Murdoch8b112d22011-06-08 16:22:53 +01001311 global_context()->set_##var(Type::cast(var##_native));
Steve Blocka7e24c12009-10-30 11:49:00 +00001312
Steve Block44f0eee2011-05-26 01:26:41 +01001313
Steve Blocka7e24c12009-10-30 11:49:00 +00001314void Genesis::InstallNativeFunctions() {
1315 HandleScope scope;
1316 INSTALL_NATIVE(JSFunction, "CreateDate", create_date_fun);
1317 INSTALL_NATIVE(JSFunction, "ToNumber", to_number_fun);
1318 INSTALL_NATIVE(JSFunction, "ToString", to_string_fun);
1319 INSTALL_NATIVE(JSFunction, "ToDetailString", to_detail_string_fun);
1320 INSTALL_NATIVE(JSFunction, "ToObject", to_object_fun);
1321 INSTALL_NATIVE(JSFunction, "ToInteger", to_integer_fun);
1322 INSTALL_NATIVE(JSFunction, "ToUint32", to_uint32_fun);
1323 INSTALL_NATIVE(JSFunction, "ToInt32", to_int32_fun);
Leon Clarkee46be812010-01-19 14:06:41 +00001324 INSTALL_NATIVE(JSFunction, "GlobalEval", global_eval_fun);
Steve Blocka7e24c12009-10-30 11:49:00 +00001325 INSTALL_NATIVE(JSFunction, "Instantiate", instantiate_fun);
1326 INSTALL_NATIVE(JSFunction, "ConfigureTemplateInstance",
1327 configure_instance_fun);
Steve Blocka7e24c12009-10-30 11:49:00 +00001328 INSTALL_NATIVE(JSFunction, "GetStackTraceLine", get_stack_trace_line_fun);
1329 INSTALL_NATIVE(JSObject, "functionCache", function_cache);
1330}
1331
Ben Murdoch257744e2011-11-30 15:57:28 +00001332void Genesis::InstallExperimentalNativeFunctions() {
1333 if (FLAG_harmony_proxies) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001334 INSTALL_NATIVE(JSFunction, "DerivedHasTrap", derived_has_trap);
Ben Murdoch257744e2011-11-30 15:57:28 +00001335 INSTALL_NATIVE(JSFunction, "DerivedGetTrap", derived_get_trap);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001336 INSTALL_NATIVE(JSFunction, "DerivedSetTrap", derived_set_trap);
Ben Murdoch257744e2011-11-30 15:57:28 +00001337 }
1338}
1339
Steve Blocka7e24c12009-10-30 11:49:00 +00001340#undef INSTALL_NATIVE
1341
1342
1343bool Genesis::InstallNatives() {
1344 HandleScope scope;
1345
1346 // Create a function for the builtins object. Allocate space for the
1347 // JavaScript builtins, a reference to the builtins object
1348 // (itself) and a reference to the global_context directly in the object.
Steve Block44f0eee2011-05-26 01:26:41 +01001349 Handle<Code> code = Handle<Code>(
Ben Murdoch257744e2011-11-30 15:57:28 +00001350 isolate()->builtins()->builtin(Builtins::kIllegal));
Steve Blocka7e24c12009-10-30 11:49:00 +00001351 Handle<JSFunction> builtins_fun =
Ben Murdoch257744e2011-11-30 15:57:28 +00001352 factory()->NewFunction(factory()->empty_symbol(),
1353 JS_BUILTINS_OBJECT_TYPE,
1354 JSBuiltinsObject::kSize, code, true);
Steve Blocka7e24c12009-10-30 11:49:00 +00001355
Ben Murdoch257744e2011-11-30 15:57:28 +00001356 Handle<String> name = factory()->LookupAsciiSymbol("builtins");
Steve Blocka7e24c12009-10-30 11:49:00 +00001357 builtins_fun->shared()->set_instance_class_name(*name);
1358
1359 // Allocate the builtins object.
1360 Handle<JSBuiltinsObject> builtins =
Ben Murdoch257744e2011-11-30 15:57:28 +00001361 Handle<JSBuiltinsObject>::cast(factory()->NewGlobalObject(builtins_fun));
Steve Blocka7e24c12009-10-30 11:49:00 +00001362 builtins->set_builtins(*builtins);
1363 builtins->set_global_context(*global_context());
1364 builtins->set_global_receiver(*builtins);
1365
Ben Murdoch85b71792012-04-11 18:30:58 +01001366 // Setup the 'global' properties of the builtins object. The
Steve Blocka7e24c12009-10-30 11:49:00 +00001367 // 'global' property that refers to the global object is the only
1368 // way to get from code running in the builtins context to the
1369 // global object.
1370 static const PropertyAttributes attributes =
1371 static_cast<PropertyAttributes>(READ_ONLY | DONT_DELETE);
Ben Murdoch257744e2011-11-30 15:57:28 +00001372 Handle<String> global_symbol = factory()->LookupAsciiSymbol("global");
Steve Block1e0659c2011-05-24 12:43:12 +01001373 Handle<Object> global_obj(global_context()->global());
Ben Murdoch85b71792012-04-11 18:30:58 +01001374 SetLocalPropertyNoThrow(builtins, global_symbol, global_obj, attributes);
Steve Blocka7e24c12009-10-30 11:49:00 +00001375
Ben Murdoch85b71792012-04-11 18:30:58 +01001376 // Setup the reference from the global object to the builtins object.
Steve Blocka7e24c12009-10-30 11:49:00 +00001377 JSGlobalObject::cast(global_context()->global())->set_builtins(*builtins);
1378
1379 // Create a bridge function that has context in the global context.
1380 Handle<JSFunction> bridge =
Ben Murdoch257744e2011-11-30 15:57:28 +00001381 factory()->NewFunction(factory()->empty_symbol(),
1382 factory()->undefined_value());
1383 ASSERT(bridge->context() == *isolate()->global_context());
Steve Blocka7e24c12009-10-30 11:49:00 +00001384
1385 // Allocate the builtins context.
1386 Handle<Context> context =
Ben Murdoch257744e2011-11-30 15:57:28 +00001387 factory()->NewFunctionContext(Context::MIN_CONTEXT_SLOTS, bridge);
Steve Blocka7e24c12009-10-30 11:49:00 +00001388 context->set_global(*builtins); // override builtins global object
1389
1390 global_context()->set_runtime_context(*context);
1391
1392 { // -- S c r i p t
1393 // Builtin functions for Script.
1394 Handle<JSFunction> script_fun =
1395 InstallFunction(builtins, "Script", JS_VALUE_TYPE, JSValue::kSize,
Ben Murdoch257744e2011-11-30 15:57:28 +00001396 isolate()->initial_object_prototype(),
Steve Block44f0eee2011-05-26 01:26:41 +01001397 Builtins::kIllegal, false);
Steve Blocka7e24c12009-10-30 11:49:00 +00001398 Handle<JSObject> prototype =
Ben Murdoch257744e2011-11-30 15:57:28 +00001399 factory()->NewJSObject(isolate()->object_function(), TENURED);
Steve Blocka7e24c12009-10-30 11:49:00 +00001400 SetPrototype(script_fun, prototype);
1401 global_context()->set_script_function(*script_fun);
1402
1403 // Add 'source' and 'data' property to scripts.
1404 PropertyAttributes common_attributes =
1405 static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE | READ_ONLY);
Ben Murdoch257744e2011-11-30 15:57:28 +00001406 Handle<Foreign> foreign_source =
1407 factory()->NewForeign(&Accessors::ScriptSource);
Steve Blocka7e24c12009-10-30 11:49:00 +00001408 Handle<DescriptorArray> script_descriptors =
Ben Murdoch257744e2011-11-30 15:57:28 +00001409 factory()->CopyAppendForeignDescriptor(
1410 factory()->empty_descriptor_array(),
1411 factory()->LookupAsciiSymbol("source"),
1412 foreign_source,
Steve Blocka7e24c12009-10-30 11:49:00 +00001413 common_attributes);
Ben Murdoch257744e2011-11-30 15:57:28 +00001414 Handle<Foreign> foreign_name =
1415 factory()->NewForeign(&Accessors::ScriptName);
Steve Blocka7e24c12009-10-30 11:49:00 +00001416 script_descriptors =
Ben Murdoch257744e2011-11-30 15:57:28 +00001417 factory()->CopyAppendForeignDescriptor(
Steve Blocka7e24c12009-10-30 11:49:00 +00001418 script_descriptors,
Ben Murdoch257744e2011-11-30 15:57:28 +00001419 factory()->LookupAsciiSymbol("name"),
1420 foreign_name,
Steve Blocka7e24c12009-10-30 11:49:00 +00001421 common_attributes);
Ben Murdoch257744e2011-11-30 15:57:28 +00001422 Handle<Foreign> foreign_id = factory()->NewForeign(&Accessors::ScriptId);
Steve Blocka7e24c12009-10-30 11:49:00 +00001423 script_descriptors =
Ben Murdoch257744e2011-11-30 15:57:28 +00001424 factory()->CopyAppendForeignDescriptor(
Steve Blocka7e24c12009-10-30 11:49:00 +00001425 script_descriptors,
Ben Murdoch257744e2011-11-30 15:57:28 +00001426 factory()->LookupAsciiSymbol("id"),
1427 foreign_id,
Steve Blocka7e24c12009-10-30 11:49:00 +00001428 common_attributes);
Ben Murdoch257744e2011-11-30 15:57:28 +00001429 Handle<Foreign> foreign_line_offset =
1430 factory()->NewForeign(&Accessors::ScriptLineOffset);
Steve Blocka7e24c12009-10-30 11:49:00 +00001431 script_descriptors =
Ben Murdoch257744e2011-11-30 15:57:28 +00001432 factory()->CopyAppendForeignDescriptor(
Steve Blocka7e24c12009-10-30 11:49:00 +00001433 script_descriptors,
Ben Murdoch257744e2011-11-30 15:57:28 +00001434 factory()->LookupAsciiSymbol("line_offset"),
1435 foreign_line_offset,
Steve Blocka7e24c12009-10-30 11:49:00 +00001436 common_attributes);
Ben Murdoch257744e2011-11-30 15:57:28 +00001437 Handle<Foreign> foreign_column_offset =
1438 factory()->NewForeign(&Accessors::ScriptColumnOffset);
Steve Blocka7e24c12009-10-30 11:49:00 +00001439 script_descriptors =
Ben Murdoch257744e2011-11-30 15:57:28 +00001440 factory()->CopyAppendForeignDescriptor(
Steve Blocka7e24c12009-10-30 11:49:00 +00001441 script_descriptors,
Ben Murdoch257744e2011-11-30 15:57:28 +00001442 factory()->LookupAsciiSymbol("column_offset"),
1443 foreign_column_offset,
Steve Blocka7e24c12009-10-30 11:49:00 +00001444 common_attributes);
Ben Murdoch257744e2011-11-30 15:57:28 +00001445 Handle<Foreign> foreign_data =
1446 factory()->NewForeign(&Accessors::ScriptData);
Steve Blocka7e24c12009-10-30 11:49:00 +00001447 script_descriptors =
Ben Murdoch257744e2011-11-30 15:57:28 +00001448 factory()->CopyAppendForeignDescriptor(
Steve Blocka7e24c12009-10-30 11:49:00 +00001449 script_descriptors,
Ben Murdoch257744e2011-11-30 15:57:28 +00001450 factory()->LookupAsciiSymbol("data"),
1451 foreign_data,
Steve Blocka7e24c12009-10-30 11:49:00 +00001452 common_attributes);
Ben Murdoch257744e2011-11-30 15:57:28 +00001453 Handle<Foreign> foreign_type =
1454 factory()->NewForeign(&Accessors::ScriptType);
Steve Blocka7e24c12009-10-30 11:49:00 +00001455 script_descriptors =
Ben Murdoch257744e2011-11-30 15:57:28 +00001456 factory()->CopyAppendForeignDescriptor(
Steve Blocka7e24c12009-10-30 11:49:00 +00001457 script_descriptors,
Ben Murdoch257744e2011-11-30 15:57:28 +00001458 factory()->LookupAsciiSymbol("type"),
1459 foreign_type,
Steve Blocka7e24c12009-10-30 11:49:00 +00001460 common_attributes);
Ben Murdoch257744e2011-11-30 15:57:28 +00001461 Handle<Foreign> foreign_compilation_type =
1462 factory()->NewForeign(&Accessors::ScriptCompilationType);
Steve Blocka7e24c12009-10-30 11:49:00 +00001463 script_descriptors =
Ben Murdoch257744e2011-11-30 15:57:28 +00001464 factory()->CopyAppendForeignDescriptor(
Steve Blocka7e24c12009-10-30 11:49:00 +00001465 script_descriptors,
Ben Murdoch257744e2011-11-30 15:57:28 +00001466 factory()->LookupAsciiSymbol("compilation_type"),
1467 foreign_compilation_type,
Steve Blocka7e24c12009-10-30 11:49:00 +00001468 common_attributes);
Ben Murdoch257744e2011-11-30 15:57:28 +00001469 Handle<Foreign> foreign_line_ends =
1470 factory()->NewForeign(&Accessors::ScriptLineEnds);
Steve Blocka7e24c12009-10-30 11:49:00 +00001471 script_descriptors =
Ben Murdoch257744e2011-11-30 15:57:28 +00001472 factory()->CopyAppendForeignDescriptor(
Steve Blocka7e24c12009-10-30 11:49:00 +00001473 script_descriptors,
Ben Murdoch257744e2011-11-30 15:57:28 +00001474 factory()->LookupAsciiSymbol("line_ends"),
1475 foreign_line_ends,
Steve Blocka7e24c12009-10-30 11:49:00 +00001476 common_attributes);
Ben Murdoch257744e2011-11-30 15:57:28 +00001477 Handle<Foreign> foreign_context_data =
1478 factory()->NewForeign(&Accessors::ScriptContextData);
Steve Blocka7e24c12009-10-30 11:49:00 +00001479 script_descriptors =
Ben Murdoch257744e2011-11-30 15:57:28 +00001480 factory()->CopyAppendForeignDescriptor(
Steve Blocka7e24c12009-10-30 11:49:00 +00001481 script_descriptors,
Ben Murdoch257744e2011-11-30 15:57:28 +00001482 factory()->LookupAsciiSymbol("context_data"),
1483 foreign_context_data,
Steve Blocka7e24c12009-10-30 11:49:00 +00001484 common_attributes);
Ben Murdoch257744e2011-11-30 15:57:28 +00001485 Handle<Foreign> foreign_eval_from_script =
1486 factory()->NewForeign(&Accessors::ScriptEvalFromScript);
Steve Blocka7e24c12009-10-30 11:49:00 +00001487 script_descriptors =
Ben Murdoch257744e2011-11-30 15:57:28 +00001488 factory()->CopyAppendForeignDescriptor(
Steve Blocka7e24c12009-10-30 11:49:00 +00001489 script_descriptors,
Ben Murdoch257744e2011-11-30 15:57:28 +00001490 factory()->LookupAsciiSymbol("eval_from_script"),
1491 foreign_eval_from_script,
Steve Blocka7e24c12009-10-30 11:49:00 +00001492 common_attributes);
Ben Murdoch257744e2011-11-30 15:57:28 +00001493 Handle<Foreign> foreign_eval_from_script_position =
1494 factory()->NewForeign(&Accessors::ScriptEvalFromScriptPosition);
Steve Blocka7e24c12009-10-30 11:49:00 +00001495 script_descriptors =
Ben Murdoch257744e2011-11-30 15:57:28 +00001496 factory()->CopyAppendForeignDescriptor(
Steve Blocka7e24c12009-10-30 11:49:00 +00001497 script_descriptors,
Ben Murdoch257744e2011-11-30 15:57:28 +00001498 factory()->LookupAsciiSymbol("eval_from_script_position"),
1499 foreign_eval_from_script_position,
Steve Blockd0582a62009-12-15 09:54:21 +00001500 common_attributes);
Ben Murdoch257744e2011-11-30 15:57:28 +00001501 Handle<Foreign> foreign_eval_from_function_name =
1502 factory()->NewForeign(&Accessors::ScriptEvalFromFunctionName);
Steve Blockd0582a62009-12-15 09:54:21 +00001503 script_descriptors =
Ben Murdoch257744e2011-11-30 15:57:28 +00001504 factory()->CopyAppendForeignDescriptor(
Steve Blockd0582a62009-12-15 09:54:21 +00001505 script_descriptors,
Ben Murdoch257744e2011-11-30 15:57:28 +00001506 factory()->LookupAsciiSymbol("eval_from_function_name"),
1507 foreign_eval_from_function_name,
Steve Blocka7e24c12009-10-30 11:49:00 +00001508 common_attributes);
1509
1510 Handle<Map> script_map = Handle<Map>(script_fun->initial_map());
1511 script_map->set_instance_descriptors(*script_descriptors);
1512
1513 // Allocate the empty script.
Ben Murdoch257744e2011-11-30 15:57:28 +00001514 Handle<Script> script = factory()->NewScript(factory()->empty_string());
Steve Blocka7e24c12009-10-30 11:49:00 +00001515 script->set_type(Smi::FromInt(Script::TYPE_NATIVE));
Ben Murdoch257744e2011-11-30 15:57:28 +00001516 heap()->public_set_empty_script(*script);
Steve Blocka7e24c12009-10-30 11:49:00 +00001517 }
Steve Block6ded16b2010-05-10 14:33:55 +01001518 {
1519 // Builtin function for OpaqueReference -- a JSValue-based object,
1520 // that keeps its field isolated from JavaScript code. It may store
1521 // objects, that JavaScript code may not access.
1522 Handle<JSFunction> opaque_reference_fun =
1523 InstallFunction(builtins, "OpaqueReference", JS_VALUE_TYPE,
Steve Block44f0eee2011-05-26 01:26:41 +01001524 JSValue::kSize,
Ben Murdoch257744e2011-11-30 15:57:28 +00001525 isolate()->initial_object_prototype(),
Steve Block44f0eee2011-05-26 01:26:41 +01001526 Builtins::kIllegal, false);
Steve Block6ded16b2010-05-10 14:33:55 +01001527 Handle<JSObject> prototype =
Ben Murdoch257744e2011-11-30 15:57:28 +00001528 factory()->NewJSObject(isolate()->object_function(), TENURED);
Steve Block6ded16b2010-05-10 14:33:55 +01001529 SetPrototype(opaque_reference_fun, prototype);
1530 global_context()->set_opaque_reference_function(*opaque_reference_fun);
1531 }
1532
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001533 { // --- I n t e r n a l A r r a y ---
1534 // An array constructor on the builtins object that works like
1535 // the public Array constructor, except that its prototype
1536 // doesn't inherit from Object.prototype.
1537 // To be used only for internal work by builtins. Instances
1538 // must not be leaked to user code.
Ben Murdoch85b71792012-04-11 18:30:58 +01001539 // Only works correctly when called as a constructor. The normal
1540 // Array code uses Array.prototype as prototype when called as
1541 // a function.
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001542 Handle<JSFunction> array_function =
1543 InstallFunction(builtins,
1544 "InternalArray",
1545 JS_ARRAY_TYPE,
1546 JSArray::kSize,
Ben Murdoch257744e2011-11-30 15:57:28 +00001547 isolate()->initial_object_prototype(),
Ben Murdoch85b71792012-04-11 18:30:58 +01001548 Builtins::kArrayCode,
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001549 true);
1550 Handle<JSObject> prototype =
Ben Murdoch257744e2011-11-30 15:57:28 +00001551 factory()->NewJSObject(isolate()->object_function(), TENURED);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001552 SetPrototype(array_function, prototype);
1553
1554 array_function->shared()->set_construct_stub(
Ben Murdoch257744e2011-11-30 15:57:28 +00001555 isolate()->builtins()->builtin(Builtins::kArrayConstructCode));
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001556 array_function->shared()->DontAdaptArguments();
1557
1558 // Make "length" magic on instances.
1559 Handle<DescriptorArray> array_descriptors =
Ben Murdoch257744e2011-11-30 15:57:28 +00001560 factory()->CopyAppendForeignDescriptor(
1561 factory()->empty_descriptor_array(),
1562 factory()->length_symbol(),
1563 factory()->NewForeign(&Accessors::ArrayLength),
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001564 static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE));
1565
1566 array_function->initial_map()->set_instance_descriptors(
1567 *array_descriptors);
1568 }
1569
Steve Block6ded16b2010-05-10 14:33:55 +01001570 if (FLAG_disable_native_files) {
1571 PrintF("Warning: Running without installed natives!\n");
1572 return true;
1573 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001574
Andrei Popescu31002712010-02-23 13:46:05 +00001575 // Install natives.
1576 for (int i = Natives::GetDebuggerCount();
1577 i < Natives::GetBuiltinsCount();
1578 i++) {
Ben Murdoch257744e2011-11-30 15:57:28 +00001579 if (!CompileBuiltin(isolate(), i)) return false;
Andrei Popescu402d9372010-02-26 13:31:12 +00001580 // TODO(ager): We really only need to install the JS builtin
1581 // functions on the builtins object after compiling and running
1582 // runtime.js.
1583 if (!InstallJSBuiltins(builtins)) return false;
Steve Blocka7e24c12009-10-30 11:49:00 +00001584 }
1585
1586 InstallNativeFunctions();
1587
Iain Merrick75681382010-08-19 15:07:18 +01001588 // Store the map for the string prototype after the natives has been compiled
Ben Murdoch85b71792012-04-11 18:30:58 +01001589 // and the String function has been setup.
Iain Merrick75681382010-08-19 15:07:18 +01001590 Handle<JSFunction> string_function(global_context()->string_function());
1591 ASSERT(JSObject::cast(
1592 string_function->initial_map()->prototype())->HasFastProperties());
1593 global_context()->set_string_function_prototype_map(
1594 HeapObject::cast(string_function->initial_map()->prototype())->map());
1595
Steve Blocka7e24c12009-10-30 11:49:00 +00001596 // Install Function.prototype.call and apply.
Ben Murdoch257744e2011-11-30 15:57:28 +00001597 { Handle<String> key = factory()->function_class_symbol();
Steve Blocka7e24c12009-10-30 11:49:00 +00001598 Handle<JSFunction> function =
Ben Murdoch257744e2011-11-30 15:57:28 +00001599 Handle<JSFunction>::cast(GetProperty(isolate()->global(), key));
Steve Blocka7e24c12009-10-30 11:49:00 +00001600 Handle<JSObject> proto =
1601 Handle<JSObject>(JSObject::cast(function->instance_prototype()));
1602
1603 // Install the call and the apply functions.
1604 Handle<JSFunction> call =
1605 InstallFunction(proto, "call", JS_OBJECT_TYPE, JSObject::kHeaderSize,
Steve Block6ded16b2010-05-10 14:33:55 +01001606 Handle<JSObject>::null(),
Steve Block44f0eee2011-05-26 01:26:41 +01001607 Builtins::kFunctionCall,
Steve Blocka7e24c12009-10-30 11:49:00 +00001608 false);
1609 Handle<JSFunction> apply =
1610 InstallFunction(proto, "apply", JS_OBJECT_TYPE, JSObject::kHeaderSize,
Steve Block6ded16b2010-05-10 14:33:55 +01001611 Handle<JSObject>::null(),
Steve Block44f0eee2011-05-26 01:26:41 +01001612 Builtins::kFunctionApply,
Steve Blocka7e24c12009-10-30 11:49:00 +00001613 false);
1614
1615 // Make sure that Function.prototype.call appears to be compiled.
1616 // The code will never be called, but inline caching for call will
1617 // only work if it appears to be compiled.
1618 call->shared()->DontAdaptArguments();
1619 ASSERT(call->is_compiled());
1620
1621 // Set the expected parameters for apply to 2; required by builtin.
1622 apply->shared()->set_formal_parameter_count(2);
1623
1624 // Set the lengths for the functions to satisfy ECMA-262.
1625 call->shared()->set_length(1);
1626 apply->shared()->set_length(2);
1627 }
1628
Ben Murdoch42effa52011-08-19 16:40:31 +01001629 InstallBuiltinFunctionIds();
1630
Steve Block6ded16b2010-05-10 14:33:55 +01001631 // Create a constructor for RegExp results (a variant of Array that
1632 // predefines the two properties index and match).
1633 {
1634 // RegExpResult initial map.
1635
1636 // Find global.Array.prototype to inherit from.
1637 Handle<JSFunction> array_constructor(global_context()->array_function());
1638 Handle<JSObject> array_prototype(
1639 JSObject::cast(array_constructor->instance_prototype()));
1640
1641 // Add initial map.
1642 Handle<Map> initial_map =
Ben Murdoch257744e2011-11-30 15:57:28 +00001643 factory()->NewMap(JS_ARRAY_TYPE, JSRegExpResult::kSize);
Steve Block6ded16b2010-05-10 14:33:55 +01001644 initial_map->set_constructor(*array_constructor);
1645
1646 // Set prototype on map.
1647 initial_map->set_non_instance_prototype(false);
1648 initial_map->set_prototype(*array_prototype);
1649
1650 // Update map with length accessor from Array and add "index" and "input".
Ben Murdoch85b71792012-04-11 18:30:58 +01001651 Handle<Map> array_map(global_context()->js_array_map());
1652 Handle<DescriptorArray> array_descriptors(
1653 array_map->instance_descriptors());
1654 ASSERT_EQ(1, array_descriptors->number_of_descriptors());
1655
Steve Block6ded16b2010-05-10 14:33:55 +01001656 Handle<DescriptorArray> reresult_descriptors =
Ben Murdoch257744e2011-11-30 15:57:28 +00001657 factory()->NewDescriptorArray(3);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001658
Ben Murdoch85b71792012-04-11 18:30:58 +01001659 reresult_descriptors->CopyFrom(0, *array_descriptors, 0);
Steve Block6ded16b2010-05-10 14:33:55 +01001660
1661 int enum_index = 0;
1662 {
Ben Murdoch257744e2011-11-30 15:57:28 +00001663 FieldDescriptor index_field(heap()->index_symbol(),
Steve Block6ded16b2010-05-10 14:33:55 +01001664 JSRegExpResult::kIndexIndex,
1665 NONE,
1666 enum_index++);
Ben Murdoch85b71792012-04-11 18:30:58 +01001667 reresult_descriptors->Set(1, &index_field);
Steve Block6ded16b2010-05-10 14:33:55 +01001668 }
1669
1670 {
Ben Murdoch257744e2011-11-30 15:57:28 +00001671 FieldDescriptor input_field(heap()->input_symbol(),
Steve Block6ded16b2010-05-10 14:33:55 +01001672 JSRegExpResult::kInputIndex,
1673 NONE,
1674 enum_index++);
Ben Murdoch85b71792012-04-11 18:30:58 +01001675 reresult_descriptors->Set(2, &input_field);
Steve Block6ded16b2010-05-10 14:33:55 +01001676 }
Ben Murdoch85b71792012-04-11 18:30:58 +01001677 reresult_descriptors->Sort();
Steve Block6ded16b2010-05-10 14:33:55 +01001678
1679 initial_map->set_inobject_properties(2);
1680 initial_map->set_pre_allocated_property_fields(2);
1681 initial_map->set_unused_property_fields(0);
1682 initial_map->set_instance_descriptors(*reresult_descriptors);
1683
1684 global_context()->set_regexp_result_map(*initial_map);
1685 }
1686
Steve Blocka7e24c12009-10-30 11:49:00 +00001687#ifdef DEBUG
1688 builtins->Verify();
1689#endif
Andrei Popescu31002712010-02-23 13:46:05 +00001690
Steve Blocka7e24c12009-10-30 11:49:00 +00001691 return true;
1692}
1693
1694
Ben Murdoch257744e2011-11-30 15:57:28 +00001695bool Genesis::InstallExperimentalNatives() {
1696 for (int i = ExperimentalNatives::GetDebuggerCount();
1697 i < ExperimentalNatives::GetBuiltinsCount();
1698 i++) {
1699 if (FLAG_harmony_proxies &&
1700 strcmp(ExperimentalNatives::GetScriptName(i).start(),
1701 "native proxy.js") == 0) {
1702 if (!CompileExperimentalBuiltin(isolate(), i)) return false;
1703 }
Ben Murdoch85b71792012-04-11 18:30:58 +01001704 if (FLAG_harmony_weakmaps &&
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001705 strcmp(ExperimentalNatives::GetScriptName(i).start(),
Ben Murdoch85b71792012-04-11 18:30:58 +01001706 "native weakmap.js") == 0) {
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001707 if (!CompileExperimentalBuiltin(isolate(), i)) return false;
1708 }
Ben Murdoch257744e2011-11-30 15:57:28 +00001709 }
1710
1711 InstallExperimentalNativeFunctions();
1712
1713 return true;
1714}
1715
1716
Ben Murdochb0fe1622011-05-05 13:52:32 +01001717static Handle<JSObject> ResolveBuiltinIdHolder(
Kristian Monsen0d5e1162010-09-30 15:31:59 +01001718 Handle<Context> global_context,
1719 const char* holder_expr) {
Ben Murdoch257744e2011-11-30 15:57:28 +00001720 Factory* factory = global_context->GetIsolate()->factory();
Kristian Monsen0d5e1162010-09-30 15:31:59 +01001721 Handle<GlobalObject> global(global_context->global());
1722 const char* period_pos = strchr(holder_expr, '.');
1723 if (period_pos == NULL) {
1724 return Handle<JSObject>::cast(
Steve Block44f0eee2011-05-26 01:26:41 +01001725 GetProperty(global, factory->LookupAsciiSymbol(holder_expr)));
Steve Block59151502010-09-22 15:07:15 +01001726 }
Kristian Monsen0d5e1162010-09-30 15:31:59 +01001727 ASSERT_EQ(".prototype", period_pos);
1728 Vector<const char> property(holder_expr,
1729 static_cast<int>(period_pos - holder_expr));
1730 Handle<JSFunction> function = Handle<JSFunction>::cast(
Steve Block44f0eee2011-05-26 01:26:41 +01001731 GetProperty(global, factory->LookupSymbol(property)));
Kristian Monsen0d5e1162010-09-30 15:31:59 +01001732 return Handle<JSObject>(JSObject::cast(function->prototype()));
1733}
1734
1735
Ben Murdochb0fe1622011-05-05 13:52:32 +01001736static void InstallBuiltinFunctionId(Handle<JSObject> holder,
1737 const char* function_name,
1738 BuiltinFunctionId id) {
Ben Murdoch257744e2011-11-30 15:57:28 +00001739 Factory* factory = holder->GetIsolate()->factory();
1740 Handle<String> name = factory->LookupAsciiSymbol(function_name);
John Reck59135872010-11-02 12:39:01 -07001741 Object* function_object = holder->GetProperty(*name)->ToObjectUnchecked();
1742 Handle<JSFunction> function(JSFunction::cast(function_object));
Kristian Monsen25f61362010-05-21 11:50:48 +01001743 function->shared()->set_function_data(Smi::FromInt(id));
1744}
1745
1746
Ben Murdochb0fe1622011-05-05 13:52:32 +01001747void Genesis::InstallBuiltinFunctionIds() {
Kristian Monsen25f61362010-05-21 11:50:48 +01001748 HandleScope scope;
Ben Murdochb0fe1622011-05-05 13:52:32 +01001749#define INSTALL_BUILTIN_ID(holder_expr, fun_name, name) \
1750 { \
1751 Handle<JSObject> holder = ResolveBuiltinIdHolder( \
1752 global_context(), #holder_expr); \
1753 BuiltinFunctionId id = k##name; \
1754 InstallBuiltinFunctionId(holder, #fun_name, id); \
Kristian Monsen25f61362010-05-21 11:50:48 +01001755 }
Ben Murdochb0fe1622011-05-05 13:52:32 +01001756 FUNCTIONS_WITH_ID_LIST(INSTALL_BUILTIN_ID)
1757#undef INSTALL_BUILTIN_ID
Kristian Monsen25f61362010-05-21 11:50:48 +01001758}
1759
1760
Steve Block6ded16b2010-05-10 14:33:55 +01001761// Do not forget to update macros.py with named constant
1762// of cache id.
1763#define JSFUNCTION_RESULT_CACHE_LIST(F) \
1764 F(16, global_context()->regexp_function())
1765
1766
Ben Murdoch257744e2011-11-30 15:57:28 +00001767static FixedArray* CreateCache(int size, Handle<JSFunction> factory_function) {
1768 Factory* factory = factory_function->GetIsolate()->factory();
Steve Block6ded16b2010-05-10 14:33:55 +01001769 // Caches are supposed to live for a long time, allocate in old space.
1770 int array_size = JSFunctionResultCache::kEntriesIndex + 2 * size;
1771 // Cannot use cast as object is not fully initialized yet.
1772 JSFunctionResultCache* cache = reinterpret_cast<JSFunctionResultCache*>(
Ben Murdoch257744e2011-11-30 15:57:28 +00001773 *factory->NewFixedArrayWithHoles(array_size, TENURED));
1774 cache->set(JSFunctionResultCache::kFactoryIndex, *factory_function);
Steve Block6ded16b2010-05-10 14:33:55 +01001775 cache->MakeZeroSize();
1776 return cache;
1777}
1778
1779
1780void Genesis::InstallJSFunctionResultCaches() {
1781 const int kNumberOfCaches = 0 +
1782#define F(size, func) + 1
1783 JSFUNCTION_RESULT_CACHE_LIST(F)
1784#undef F
1785 ;
1786
Steve Block44f0eee2011-05-26 01:26:41 +01001787 Handle<FixedArray> caches = FACTORY->NewFixedArray(kNumberOfCaches, TENURED);
Steve Block6ded16b2010-05-10 14:33:55 +01001788
1789 int index = 0;
Kristian Monsen0d5e1162010-09-30 15:31:59 +01001790
Ben Murdoch257744e2011-11-30 15:57:28 +00001791#define F(size, func) do { \
1792 FixedArray* cache = CreateCache((size), Handle<JSFunction>(func)); \
1793 caches->set(index++, cache); \
Kristian Monsen0d5e1162010-09-30 15:31:59 +01001794 } while (false)
1795
1796 JSFUNCTION_RESULT_CACHE_LIST(F);
1797
Steve Block6ded16b2010-05-10 14:33:55 +01001798#undef F
1799
1800 global_context()->set_jsfunction_result_caches(*caches);
1801}
1802
1803
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001804void Genesis::InitializeNormalizedMapCaches() {
1805 Handle<FixedArray> array(
Steve Block44f0eee2011-05-26 01:26:41 +01001806 FACTORY->NewFixedArray(NormalizedMapCache::kEntries, TENURED));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001807 global_context()->set_normalized_map_cache(NormalizedMapCache::cast(*array));
1808}
1809
1810
Andrei Popescu31002712010-02-23 13:46:05 +00001811bool Bootstrapper::InstallExtensions(Handle<Context> global_context,
1812 v8::ExtensionConfiguration* extensions) {
Ben Murdoch257744e2011-11-30 15:57:28 +00001813 Isolate* isolate = global_context->GetIsolate();
Andrei Popescu31002712010-02-23 13:46:05 +00001814 BootstrapperActive active;
Steve Block44f0eee2011-05-26 01:26:41 +01001815 SaveContext saved_context(isolate);
1816 isolate->set_context(*global_context);
Andrei Popescu31002712010-02-23 13:46:05 +00001817 if (!Genesis::InstallExtensions(global_context, extensions)) return false;
1818 Genesis::InstallSpecialObjects(global_context);
1819 return true;
1820}
1821
1822
1823void Genesis::InstallSpecialObjects(Handle<Context> global_context) {
Ben Murdoch85b71792012-04-11 18:30:58 +01001824 Factory* factory = global_context->GetIsolate()->factory();
Steve Blocka7e24c12009-10-30 11:49:00 +00001825 HandleScope scope;
Ben Murdoch85b71792012-04-11 18:30:58 +01001826 Handle<JSGlobalObject> js_global(
1827 JSGlobalObject::cast(global_context->global()));
Steve Blocka7e24c12009-10-30 11:49:00 +00001828 // Expose the natives in global if a name for it is specified.
1829 if (FLAG_expose_natives_as != NULL && strlen(FLAG_expose_natives_as) != 0) {
Ben Murdoch85b71792012-04-11 18:30:58 +01001830 Handle<String> natives_string =
1831 factory->LookupAsciiSymbol(FLAG_expose_natives_as);
1832 SetLocalPropertyNoThrow(js_global, natives_string,
1833 Handle<JSObject>(js_global->builtins()), DONT_ENUM);
Steve Blocka7e24c12009-10-30 11:49:00 +00001834 }
1835
Ben Murdoch85b71792012-04-11 18:30:58 +01001836 Handle<Object> Error = GetProperty(js_global, "Error");
Steve Blocka7e24c12009-10-30 11:49:00 +00001837 if (Error->IsJSObject()) {
Steve Block44f0eee2011-05-26 01:26:41 +01001838 Handle<String> name = factory->LookupAsciiSymbol("stackTraceLimit");
Ben Murdoch85b71792012-04-11 18:30:58 +01001839 SetLocalPropertyNoThrow(Handle<JSObject>::cast(Error),
1840 name,
1841 Handle<Smi>(Smi::FromInt(FLAG_stack_trace_limit)),
1842 NONE);
Steve Blocka7e24c12009-10-30 11:49:00 +00001843 }
1844
1845#ifdef ENABLE_DEBUGGER_SUPPORT
1846 // Expose the debug global object in global if a name for it is specified.
1847 if (FLAG_expose_debug_as != NULL && strlen(FLAG_expose_debug_as) != 0) {
Steve Block44f0eee2011-05-26 01:26:41 +01001848 Debug* debug = Isolate::Current()->debug();
Steve Blocka7e24c12009-10-30 11:49:00 +00001849 // If loading fails we just bail out without installing the
1850 // debugger but without tanking the whole context.
Steve Block44f0eee2011-05-26 01:26:41 +01001851 if (!debug->Load()) return;
Steve Blocka7e24c12009-10-30 11:49:00 +00001852 // Set the security token for the debugger context to the same as
1853 // the shell global context to allow calling between these (otherwise
1854 // exposing debug global object doesn't make much sense).
Steve Block44f0eee2011-05-26 01:26:41 +01001855 debug->debug_context()->set_security_token(
Andrei Popescu31002712010-02-23 13:46:05 +00001856 global_context->security_token());
Steve Blocka7e24c12009-10-30 11:49:00 +00001857
1858 Handle<String> debug_string =
Steve Block44f0eee2011-05-26 01:26:41 +01001859 factory->LookupAsciiSymbol(FLAG_expose_debug_as);
1860 Handle<Object> global_proxy(debug->debug_context()->global_proxy());
Ben Murdoch85b71792012-04-11 18:30:58 +01001861 SetLocalPropertyNoThrow(js_global, debug_string, global_proxy, DONT_ENUM);
Steve Blocka7e24c12009-10-30 11:49:00 +00001862 }
1863#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00001864}
1865
1866
Andrei Popescu31002712010-02-23 13:46:05 +00001867bool Genesis::InstallExtensions(Handle<Context> global_context,
1868 v8::ExtensionConfiguration* extensions) {
Steve Block44f0eee2011-05-26 01:26:41 +01001869 // TODO(isolates): Extensions on multiple isolates may take a little more
1870 // effort. (The external API reads 'ignore'-- does that mean
1871 // we can break the interface?)
1872
Ben Murdoch85b71792012-04-11 18:30:58 +01001873 // Clear coloring of extension list
Steve Blocka7e24c12009-10-30 11:49:00 +00001874 v8::RegisteredExtension* current = v8::RegisteredExtension::first_extension();
1875 while (current != NULL) {
Ben Murdoch85b71792012-04-11 18:30:58 +01001876 current->set_state(v8::UNVISITED);
1877 current = current->next();
1878 }
1879 // Install auto extensions.
1880 current = v8::RegisteredExtension::first_extension();
1881 while (current != NULL) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001882 if (current->extension()->auto_enable())
Ben Murdoch85b71792012-04-11 18:30:58 +01001883 InstallExtension(current);
Steve Blocka7e24c12009-10-30 11:49:00 +00001884 current = current->next();
1885 }
1886
Ben Murdoch85b71792012-04-11 18:30:58 +01001887 if (FLAG_expose_gc) InstallExtension("v8/gc");
1888 if (FLAG_expose_externalize_string) InstallExtension("v8/externalize");
Steve Blocka7e24c12009-10-30 11:49:00 +00001889
1890 if (extensions == NULL) return true;
1891 // Install required extensions
1892 int count = v8::ImplementationUtilities::GetNameCount(extensions);
1893 const char** names = v8::ImplementationUtilities::GetNames(extensions);
1894 for (int i = 0; i < count; i++) {
Ben Murdoch85b71792012-04-11 18:30:58 +01001895 if (!InstallExtension(names[i]))
Steve Blocka7e24c12009-10-30 11:49:00 +00001896 return false;
1897 }
1898
1899 return true;
1900}
1901
1902
1903// Installs a named extension. This methods is unoptimized and does
1904// not scale well if we want to support a large number of extensions.
Ben Murdoch85b71792012-04-11 18:30:58 +01001905bool Genesis::InstallExtension(const char* name) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001906 v8::RegisteredExtension* current = v8::RegisteredExtension::first_extension();
1907 // Loop until we find the relevant extension
1908 while (current != NULL) {
1909 if (strcmp(name, current->extension()->name()) == 0) break;
1910 current = current->next();
1911 }
1912 // Didn't find the extension; fail.
1913 if (current == NULL) {
1914 v8::Utils::ReportApiFailure(
1915 "v8::Context::New()", "Cannot find required extension");
1916 return false;
1917 }
Ben Murdoch85b71792012-04-11 18:30:58 +01001918 return InstallExtension(current);
Steve Blocka7e24c12009-10-30 11:49:00 +00001919}
1920
1921
Ben Murdoch85b71792012-04-11 18:30:58 +01001922bool Genesis::InstallExtension(v8::RegisteredExtension* current) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001923 HandleScope scope;
1924
Ben Murdoch85b71792012-04-11 18:30:58 +01001925 if (current->state() == v8::INSTALLED) return true;
Steve Blocka7e24c12009-10-30 11:49:00 +00001926 // The current node has already been visited so there must be a
1927 // cycle in the dependency graph; fail.
Ben Murdoch85b71792012-04-11 18:30:58 +01001928 if (current->state() == v8::VISITED) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001929 v8::Utils::ReportApiFailure(
1930 "v8::Context::New()", "Circular extension dependency");
1931 return false;
1932 }
Ben Murdoch85b71792012-04-11 18:30:58 +01001933 ASSERT(current->state() == v8::UNVISITED);
1934 current->set_state(v8::VISITED);
Steve Blocka7e24c12009-10-30 11:49:00 +00001935 v8::Extension* extension = current->extension();
1936 // Install the extension's dependencies
1937 for (int i = 0; i < extension->dependency_count(); i++) {
Ben Murdoch85b71792012-04-11 18:30:58 +01001938 if (!InstallExtension(extension->dependencies()[i])) return false;
Steve Blocka7e24c12009-10-30 11:49:00 +00001939 }
Steve Block44f0eee2011-05-26 01:26:41 +01001940 Isolate* isolate = Isolate::Current();
Ben Murdoch85b71792012-04-11 18:30:58 +01001941 Vector<const char> source = CStrVector(extension->source());
1942 Handle<String> source_code = isolate->factory()->NewStringFromAscii(source);
1943 bool result = CompileScriptCached(CStrVector(extension->name()),
1944 source_code,
1945 isolate->bootstrapper()->extensions_cache(),
1946 extension,
1947 Handle<Context>(isolate->context()),
1948 false);
Steve Block44f0eee2011-05-26 01:26:41 +01001949 ASSERT(isolate->has_pending_exception() != result);
Steve Blocka7e24c12009-10-30 11:49:00 +00001950 if (!result) {
Steve Block44f0eee2011-05-26 01:26:41 +01001951 isolate->clear_pending_exception();
Steve Blocka7e24c12009-10-30 11:49:00 +00001952 }
Ben Murdoch85b71792012-04-11 18:30:58 +01001953 current->set_state(v8::INSTALLED);
Steve Blocka7e24c12009-10-30 11:49:00 +00001954 return result;
1955}
1956
1957
Andrei Popescu402d9372010-02-26 13:31:12 +00001958bool Genesis::InstallJSBuiltins(Handle<JSBuiltinsObject> builtins) {
1959 HandleScope scope;
Ben Murdoch257744e2011-11-30 15:57:28 +00001960 Factory* factory = builtins->GetIsolate()->factory();
Andrei Popescu402d9372010-02-26 13:31:12 +00001961 for (int i = 0; i < Builtins::NumberOfJavaScriptBuiltins(); i++) {
1962 Builtins::JavaScript id = static_cast<Builtins::JavaScript>(i);
Ben Murdoch257744e2011-11-30 15:57:28 +00001963 Handle<String> name = factory->LookupAsciiSymbol(Builtins::GetName(id));
John Reck59135872010-11-02 12:39:01 -07001964 Object* function_object = builtins->GetPropertyNoExceptionThrown(*name);
Andrei Popescu402d9372010-02-26 13:31:12 +00001965 Handle<JSFunction> function
John Reck59135872010-11-02 12:39:01 -07001966 = Handle<JSFunction>(JSFunction::cast(function_object));
Andrei Popescu402d9372010-02-26 13:31:12 +00001967 builtins->set_javascript_builtin(id, *function);
1968 Handle<SharedFunctionInfo> shared
1969 = Handle<SharedFunctionInfo>(function->shared());
Ben Murdoch85b71792012-04-11 18:30:58 +01001970 if (!EnsureCompiled(shared, CLEAR_EXCEPTION)) return false;
Iain Merrick75681382010-08-19 15:07:18 +01001971 // Set the code object on the function object.
Ben Murdochb0fe1622011-05-05 13:52:32 +01001972 function->ReplaceCode(function->shared()->code());
Steve Block6ded16b2010-05-10 14:33:55 +01001973 builtins->set_javascript_builtin_code(id, shared->code());
Andrei Popescu402d9372010-02-26 13:31:12 +00001974 }
1975 return true;
Andrei Popescu31002712010-02-23 13:46:05 +00001976}
1977
1978
Steve Blocka7e24c12009-10-30 11:49:00 +00001979bool Genesis::ConfigureGlobalObjects(
1980 v8::Handle<v8::ObjectTemplate> global_proxy_template) {
1981 Handle<JSObject> global_proxy(
1982 JSObject::cast(global_context()->global_proxy()));
Andrei Popescu402d9372010-02-26 13:31:12 +00001983 Handle<JSObject> inner_global(JSObject::cast(global_context()->global()));
Steve Blocka7e24c12009-10-30 11:49:00 +00001984
1985 if (!global_proxy_template.IsEmpty()) {
1986 // Configure the global proxy object.
1987 Handle<ObjectTemplateInfo> proxy_data =
1988 v8::Utils::OpenHandle(*global_proxy_template);
1989 if (!ConfigureApiObject(global_proxy, proxy_data)) return false;
1990
1991 // Configure the inner global object.
1992 Handle<FunctionTemplateInfo> proxy_constructor(
1993 FunctionTemplateInfo::cast(proxy_data->constructor()));
1994 if (!proxy_constructor->prototype_template()->IsUndefined()) {
1995 Handle<ObjectTemplateInfo> inner_data(
1996 ObjectTemplateInfo::cast(proxy_constructor->prototype_template()));
Andrei Popescu402d9372010-02-26 13:31:12 +00001997 if (!ConfigureApiObject(inner_global, inner_data)) return false;
Steve Blocka7e24c12009-10-30 11:49:00 +00001998 }
1999 }
2000
Andrei Popescu402d9372010-02-26 13:31:12 +00002001 SetObjectPrototype(global_proxy, inner_global);
Steve Blocka7e24c12009-10-30 11:49:00 +00002002 return true;
2003}
2004
2005
2006bool Genesis::ConfigureApiObject(Handle<JSObject> object,
2007 Handle<ObjectTemplateInfo> object_template) {
2008 ASSERT(!object_template.is_null());
2009 ASSERT(object->IsInstanceOf(
2010 FunctionTemplateInfo::cast(object_template->constructor())));
2011
2012 bool pending_exception = false;
2013 Handle<JSObject> obj =
2014 Execution::InstantiateObject(object_template, &pending_exception);
2015 if (pending_exception) {
Ben Murdoch257744e2011-11-30 15:57:28 +00002016 ASSERT(isolate()->has_pending_exception());
2017 isolate()->clear_pending_exception();
Steve Blocka7e24c12009-10-30 11:49:00 +00002018 return false;
2019 }
2020 TransferObject(obj, object);
2021 return true;
2022}
2023
2024
2025void Genesis::TransferNamedProperties(Handle<JSObject> from,
2026 Handle<JSObject> to) {
2027 if (from->HasFastProperties()) {
2028 Handle<DescriptorArray> descs =
2029 Handle<DescriptorArray>(from->map()->instance_descriptors());
2030 for (int i = 0; i < descs->number_of_descriptors(); i++) {
2031 PropertyDetails details = PropertyDetails(descs->GetDetails(i));
2032 switch (details.type()) {
2033 case FIELD: {
2034 HandleScope inner;
2035 Handle<String> key = Handle<String>(descs->GetKey(i));
2036 int index = descs->GetFieldIndex(i);
2037 Handle<Object> value = Handle<Object>(from->FastPropertyAt(index));
Ben Murdoch85b71792012-04-11 18:30:58 +01002038 SetLocalPropertyNoThrow(to, key, value, details.attributes());
Steve Blocka7e24c12009-10-30 11:49:00 +00002039 break;
2040 }
2041 case CONSTANT_FUNCTION: {
2042 HandleScope inner;
2043 Handle<String> key = Handle<String>(descs->GetKey(i));
2044 Handle<JSFunction> fun =
2045 Handle<JSFunction>(descs->GetConstantFunction(i));
Ben Murdoch85b71792012-04-11 18:30:58 +01002046 SetLocalPropertyNoThrow(to, key, fun, details.attributes());
Steve Blocka7e24c12009-10-30 11:49:00 +00002047 break;
2048 }
2049 case CALLBACKS: {
Ben Murdoch85b71792012-04-11 18:30:58 +01002050 LookupResult result;
Steve Blocka7e24c12009-10-30 11:49:00 +00002051 to->LocalLookup(descs->GetKey(i), &result);
2052 // If the property is already there we skip it
Andrei Popescu402d9372010-02-26 13:31:12 +00002053 if (result.IsProperty()) continue;
Steve Blocka7e24c12009-10-30 11:49:00 +00002054 HandleScope inner;
Andrei Popescu31002712010-02-23 13:46:05 +00002055 ASSERT(!to->HasFastProperties());
2056 // Add to dictionary.
Steve Blocka7e24c12009-10-30 11:49:00 +00002057 Handle<String> key = Handle<String>(descs->GetKey(i));
Andrei Popescu31002712010-02-23 13:46:05 +00002058 Handle<Object> callbacks(descs->GetCallbacksObject(i));
2059 PropertyDetails d =
2060 PropertyDetails(details.attributes(), CALLBACKS, details.index());
Ben Murdoch85b71792012-04-11 18:30:58 +01002061 SetNormalizedProperty(to, key, callbacks, d);
Steve Blocka7e24c12009-10-30 11:49:00 +00002062 break;
2063 }
2064 case MAP_TRANSITION:
Ben Murdoch589d6972011-11-30 16:04:58 +00002065 case ELEMENTS_TRANSITION:
Steve Blocka7e24c12009-10-30 11:49:00 +00002066 case CONSTANT_TRANSITION:
2067 case NULL_DESCRIPTOR:
2068 // Ignore non-properties.
2069 break;
2070 case NORMAL:
2071 // Do not occur since the from object has fast properties.
Ben Murdoch257744e2011-11-30 15:57:28 +00002072 case HANDLER:
Steve Blocka7e24c12009-10-30 11:49:00 +00002073 case INTERCEPTOR:
Ben Murdoch257744e2011-11-30 15:57:28 +00002074 // No element in instance descriptors have proxy or interceptor type.
Steve Blocka7e24c12009-10-30 11:49:00 +00002075 UNREACHABLE();
2076 break;
2077 }
2078 }
2079 } else {
2080 Handle<StringDictionary> properties =
2081 Handle<StringDictionary>(from->property_dictionary());
2082 int capacity = properties->Capacity();
2083 for (int i = 0; i < capacity; i++) {
2084 Object* raw_key(properties->KeyAt(i));
2085 if (properties->IsKey(raw_key)) {
2086 ASSERT(raw_key->IsString());
2087 // If the property is already there we skip it.
Ben Murdoch85b71792012-04-11 18:30:58 +01002088 LookupResult result;
Steve Blocka7e24c12009-10-30 11:49:00 +00002089 to->LocalLookup(String::cast(raw_key), &result);
Andrei Popescu402d9372010-02-26 13:31:12 +00002090 if (result.IsProperty()) continue;
Steve Blocka7e24c12009-10-30 11:49:00 +00002091 // Set the property.
2092 Handle<String> key = Handle<String>(String::cast(raw_key));
2093 Handle<Object> value = Handle<Object>(properties->ValueAt(i));
2094 if (value->IsJSGlobalPropertyCell()) {
2095 value = Handle<Object>(JSGlobalPropertyCell::cast(*value)->value());
2096 }
2097 PropertyDetails details = properties->DetailsAt(i);
Ben Murdoch85b71792012-04-11 18:30:58 +01002098 SetLocalPropertyNoThrow(to, key, value, details.attributes());
Steve Blocka7e24c12009-10-30 11:49:00 +00002099 }
2100 }
2101 }
2102}
2103
2104
2105void Genesis::TransferIndexedProperties(Handle<JSObject> from,
2106 Handle<JSObject> to) {
2107 // Cloning the elements array is sufficient.
2108 Handle<FixedArray> from_elements =
2109 Handle<FixedArray>(FixedArray::cast(from->elements()));
Steve Block44f0eee2011-05-26 01:26:41 +01002110 Handle<FixedArray> to_elements = FACTORY->CopyFixedArray(from_elements);
Steve Blocka7e24c12009-10-30 11:49:00 +00002111 to->set_elements(*to_elements);
2112}
2113
2114
2115void Genesis::TransferObject(Handle<JSObject> from, Handle<JSObject> to) {
2116 HandleScope outer;
Ben Murdoch257744e2011-11-30 15:57:28 +00002117 Factory* factory = from->GetIsolate()->factory();
Steve Blocka7e24c12009-10-30 11:49:00 +00002118
2119 ASSERT(!from->IsJSArray());
2120 ASSERT(!to->IsJSArray());
2121
2122 TransferNamedProperties(from, to);
2123 TransferIndexedProperties(from, to);
2124
2125 // Transfer the prototype (new map is needed).
2126 Handle<Map> old_to_map = Handle<Map>(to->map());
Ben Murdoch257744e2011-11-30 15:57:28 +00002127 Handle<Map> new_to_map = factory->CopyMapDropTransitions(old_to_map);
Steve Blocka7e24c12009-10-30 11:49:00 +00002128 new_to_map->set_prototype(from->map()->prototype());
2129 to->set_map(*new_to_map);
2130}
2131
2132
2133void Genesis::MakeFunctionInstancePrototypeWritable() {
Steve Block44f0eee2011-05-26 01:26:41 +01002134 // The maps with writable prototype are created in CreateEmptyFunction
2135 // and CreateStrictModeFunctionMaps respectively. Initially the maps are
2136 // created with read-only prototype for JS builtins processing.
2137 ASSERT(!function_instance_map_writable_prototype_.is_null());
2138 ASSERT(!strict_mode_function_instance_map_writable_prototype_.is_null());
Steve Blocka7e24c12009-10-30 11:49:00 +00002139
Steve Block44f0eee2011-05-26 01:26:41 +01002140 // Replace function instance maps to make prototype writable.
2141 global_context()->set_function_map(
2142 *function_instance_map_writable_prototype_);
2143 global_context()->set_strict_mode_function_map(
2144 *strict_mode_function_instance_map_writable_prototype_);
Steve Blocka7e24c12009-10-30 11:49:00 +00002145}
2146
2147
Ben Murdoch257744e2011-11-30 15:57:28 +00002148Genesis::Genesis(Isolate* isolate,
2149 Handle<Object> global_object,
Steve Blocka7e24c12009-10-30 11:49:00 +00002150 v8::Handle<v8::ObjectTemplate> global_template,
Ben Murdoch257744e2011-11-30 15:57:28 +00002151 v8::ExtensionConfiguration* extensions) : isolate_(isolate) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002152 result_ = Handle<Context>::null();
Steve Blocka7e24c12009-10-30 11:49:00 +00002153 // If V8 isn't running and cannot be initialized, just return.
2154 if (!V8::IsRunning() && !V8::Initialize(NULL)) return;
2155
2156 // Before creating the roots we must save the context and restore it
2157 // on all function exits.
2158 HandleScope scope;
Steve Block44f0eee2011-05-26 01:26:41 +01002159 SaveContext saved_context(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00002160
Andrei Popescu31002712010-02-23 13:46:05 +00002161 Handle<Context> new_context = Snapshot::NewContextFromSnapshot();
2162 if (!new_context.is_null()) {
2163 global_context_ =
Steve Block44f0eee2011-05-26 01:26:41 +01002164 Handle<Context>::cast(isolate->global_handles()->Create(*new_context));
Ben Murdochb0fe1622011-05-05 13:52:32 +01002165 AddToWeakGlobalContextList(*global_context_);
Steve Block44f0eee2011-05-26 01:26:41 +01002166 isolate->set_context(*global_context_);
2167 isolate->counters()->contexts_created_by_snapshot()->Increment();
Andrei Popescu402d9372010-02-26 13:31:12 +00002168 Handle<GlobalObject> inner_global;
Andrei Popescu31002712010-02-23 13:46:05 +00002169 Handle<JSGlobalProxy> global_proxy =
2170 CreateNewGlobals(global_template,
2171 global_object,
Andrei Popescu402d9372010-02-26 13:31:12 +00002172 &inner_global);
2173
Andrei Popescu31002712010-02-23 13:46:05 +00002174 HookUpGlobalProxy(inner_global, global_proxy);
Andrei Popescu402d9372010-02-26 13:31:12 +00002175 HookUpInnerGlobal(inner_global);
2176
Andrei Popescu31002712010-02-23 13:46:05 +00002177 if (!ConfigureGlobalObjects(global_template)) return;
2178 } else {
2179 // We get here if there was no context snapshot.
2180 CreateRoots();
Ben Murdoch257744e2011-11-30 15:57:28 +00002181 Handle<JSFunction> empty_function = CreateEmptyFunction(isolate);
Steve Block44f0eee2011-05-26 01:26:41 +01002182 CreateStrictModeFunctionMaps(empty_function);
Andrei Popescu31002712010-02-23 13:46:05 +00002183 Handle<GlobalObject> inner_global;
2184 Handle<JSGlobalProxy> global_proxy =
2185 CreateNewGlobals(global_template, global_object, &inner_global);
2186 HookUpGlobalProxy(inner_global, global_proxy);
Ben Murdoch85b71792012-04-11 18:30:58 +01002187 InitializeGlobal(inner_global, empty_function);
Steve Block6ded16b2010-05-10 14:33:55 +01002188 InstallJSFunctionResultCaches();
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002189 InitializeNormalizedMapCaches();
Kristian Monsen25f61362010-05-21 11:50:48 +01002190 if (!InstallNatives()) return;
Steve Blocka7e24c12009-10-30 11:49:00 +00002191
Andrei Popescu31002712010-02-23 13:46:05 +00002192 MakeFunctionInstancePrototypeWritable();
Steve Blocka7e24c12009-10-30 11:49:00 +00002193
Andrei Popescu31002712010-02-23 13:46:05 +00002194 if (!ConfigureGlobalObjects(global_template)) return;
Steve Block44f0eee2011-05-26 01:26:41 +01002195 isolate->counters()->contexts_created_from_scratch()->Increment();
Andrei Popescu31002712010-02-23 13:46:05 +00002196 }
Steve Blocka7e24c12009-10-30 11:49:00 +00002197
Ben Murdoch69a99ed2011-11-30 16:03:39 +00002198 // Initialize experimental globals and install experimental natives.
2199 InitializeExperimentalGlobal();
Ben Murdoch257744e2011-11-30 15:57:28 +00002200 if (!InstallExperimentalNatives()) return;
2201
Steve Blocka7e24c12009-10-30 11:49:00 +00002202 result_ = global_context_;
2203}
2204
2205
2206// Support for thread preemption.
2207
2208// Reserve space for statics needing saving and restoring.
2209int Bootstrapper::ArchiveSpacePerThread() {
Steve Block44f0eee2011-05-26 01:26:41 +01002210 return sizeof(NestingCounterType);
Steve Blocka7e24c12009-10-30 11:49:00 +00002211}
2212
2213
2214// Archive statics that are thread local.
2215char* Bootstrapper::ArchiveState(char* to) {
Steve Block44f0eee2011-05-26 01:26:41 +01002216 *reinterpret_cast<NestingCounterType*>(to) = nesting_;
2217 nesting_ = 0;
2218 return to + sizeof(NestingCounterType);
Steve Blocka7e24c12009-10-30 11:49:00 +00002219}
2220
2221
2222// Restore statics that are thread local.
2223char* Bootstrapper::RestoreState(char* from) {
Steve Block44f0eee2011-05-26 01:26:41 +01002224 nesting_ = *reinterpret_cast<NestingCounterType*>(from);
2225 return from + sizeof(NestingCounterType);
Steve Blocka7e24c12009-10-30 11:49:00 +00002226}
2227
2228
2229// Called when the top-level V8 mutex is destroyed.
2230void Bootstrapper::FreeThreadResources() {
Steve Block44f0eee2011-05-26 01:26:41 +01002231 ASSERT(!IsActive());
Steve Blocka7e24c12009-10-30 11:49:00 +00002232}
2233
2234} } // namespace v8::internal