blob: 0e95b4b83974334a6419cabc95e600877def9a2b [file] [log] [blame]
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001// Copyright 2012 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"
Ben Murdoch3ef787d2012-04-12 10:51:47 +010037#include "isolate-inl.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000038#include "macro-assembler.h"
39#include "natives.h"
Iain Merrick75681382010-08-19 15:07:18 +010040#include "objects-visiting.h"
Ben Murdoch3ef787d2012-04-12 10:51:47 +010041#include "platform.h"
Steve Blockd0582a62009-12-15 09:54:21 +000042#include "snapshot.h"
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -080043#include "extensions/externalize-string-extension.h"
44#include "extensions/gc-extension.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000045
46namespace v8 {
47namespace internal {
48
Steve Blocka7e24c12009-10-30 11:49:00 +000049
Steve Block44f0eee2011-05-26 01:26:41 +010050NativesExternalStringResource::NativesExternalStringResource(
51 Bootstrapper* bootstrapper,
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000052 const char* source,
53 size_t length)
54 : data_(source), length_(length) {
Steve Block44f0eee2011-05-26 01:26:41 +010055 if (bootstrapper->delete_these_non_arrays_on_tear_down_ == NULL) {
56 bootstrapper->delete_these_non_arrays_on_tear_down_ = new List<char*>(2);
Steve Blockd0582a62009-12-15 09:54:21 +000057 }
58 // The resources are small objects and we only make a fixed number of
59 // them, but let's clean them up on exit for neatness.
Steve Block44f0eee2011-05-26 01:26:41 +010060 bootstrapper->delete_these_non_arrays_on_tear_down_->
Steve Blockd0582a62009-12-15 09:54:21 +000061 Add(reinterpret_cast<char*>(this));
62}
Steve Blocka7e24c12009-10-30 11:49:00 +000063
64
Steve Block44f0eee2011-05-26 01:26:41 +010065Bootstrapper::Bootstrapper()
66 : nesting_(0),
67 extensions_cache_(Script::TYPE_EXTENSION),
68 delete_these_non_arrays_on_tear_down_(NULL),
69 delete_these_arrays_on_tear_down_(NULL) {
70}
71
72
Steve Blocka7e24c12009-10-30 11:49:00 +000073Handle<String> Bootstrapper::NativesSourceLookup(int index) {
74 ASSERT(0 <= index && index < Natives::GetBuiltinsCount());
Steve Block44f0eee2011-05-26 01:26:41 +010075 Isolate* isolate = Isolate::Current();
76 Factory* factory = isolate->factory();
77 Heap* heap = isolate->heap();
78 if (heap->natives_source_cache()->get(index)->IsUndefined()) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +010079 // We can use external strings for the natives.
80 Vector<const char> source = Natives::GetRawScriptSource(index);
81 NativesExternalStringResource* resource =
82 new NativesExternalStringResource(this,
83 source.start(),
84 source.length());
85 Handle<String> source_code =
86 factory->NewExternalStringFromAscii(resource);
87 heap->natives_source_cache()->set(index, *source_code);
Steve Blocka7e24c12009-10-30 11:49:00 +000088 }
Steve Block44f0eee2011-05-26 01:26:41 +010089 Handle<Object> cached_source(heap->natives_source_cache()->get(index));
Steve Blocka7e24c12009-10-30 11:49:00 +000090 return Handle<String>::cast(cached_source);
91}
92
93
Steve Blocka7e24c12009-10-30 11:49:00 +000094void Bootstrapper::Initialize(bool create_heap_objects) {
Steve Block44f0eee2011-05-26 01:26:41 +010095 extensions_cache_.Initialize(create_heap_objects);
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -080096 GCExtension::Register();
97 ExternalizeStringExtension::Register();
Steve Blocka7e24c12009-10-30 11:49:00 +000098}
99
100
Leon Clarkee46be812010-01-19 14:06:41 +0000101char* Bootstrapper::AllocateAutoDeletedArray(int bytes) {
102 char* memory = new char[bytes];
103 if (memory != NULL) {
Steve Block44f0eee2011-05-26 01:26:41 +0100104 if (delete_these_arrays_on_tear_down_ == NULL) {
105 delete_these_arrays_on_tear_down_ = new List<char*>(2);
Leon Clarkee46be812010-01-19 14:06:41 +0000106 }
Steve Block44f0eee2011-05-26 01:26:41 +0100107 delete_these_arrays_on_tear_down_->Add(memory);
Leon Clarkee46be812010-01-19 14:06:41 +0000108 }
109 return memory;
110}
111
112
Steve Blocka7e24c12009-10-30 11:49:00 +0000113void Bootstrapper::TearDown() {
Steve Block44f0eee2011-05-26 01:26:41 +0100114 if (delete_these_non_arrays_on_tear_down_ != NULL) {
115 int len = delete_these_non_arrays_on_tear_down_->length();
Steve Blockd0582a62009-12-15 09:54:21 +0000116 ASSERT(len < 20); // Don't use this mechanism for unbounded allocations.
117 for (int i = 0; i < len; i++) {
Steve Block44f0eee2011-05-26 01:26:41 +0100118 delete delete_these_non_arrays_on_tear_down_->at(i);
119 delete_these_non_arrays_on_tear_down_->at(i) = NULL;
Steve Blockd0582a62009-12-15 09:54:21 +0000120 }
Steve Block44f0eee2011-05-26 01:26:41 +0100121 delete delete_these_non_arrays_on_tear_down_;
122 delete_these_non_arrays_on_tear_down_ = NULL;
Steve Blockd0582a62009-12-15 09:54:21 +0000123 }
124
Steve Block44f0eee2011-05-26 01:26:41 +0100125 if (delete_these_arrays_on_tear_down_ != NULL) {
126 int len = delete_these_arrays_on_tear_down_->length();
Leon Clarkee46be812010-01-19 14:06:41 +0000127 ASSERT(len < 1000); // Don't use this mechanism for unbounded allocations.
128 for (int i = 0; i < len; i++) {
Steve Block44f0eee2011-05-26 01:26:41 +0100129 delete[] delete_these_arrays_on_tear_down_->at(i);
130 delete_these_arrays_on_tear_down_->at(i) = NULL;
Leon Clarkee46be812010-01-19 14:06:41 +0000131 }
Steve Block44f0eee2011-05-26 01:26:41 +0100132 delete delete_these_arrays_on_tear_down_;
133 delete_these_arrays_on_tear_down_ = NULL;
Leon Clarkee46be812010-01-19 14:06:41 +0000134 }
135
Steve Block44f0eee2011-05-26 01:26:41 +0100136 extensions_cache_.Initialize(false); // Yes, symmetrical
Steve Blocka7e24c12009-10-30 11:49:00 +0000137}
138
139
Steve Blocka7e24c12009-10-30 11:49:00 +0000140class Genesis BASE_EMBEDDED {
141 public:
Ben Murdoch257744e2011-11-30 15:57:28 +0000142 Genesis(Isolate* isolate,
143 Handle<Object> global_object,
Steve Blocka7e24c12009-10-30 11:49:00 +0000144 v8::Handle<v8::ObjectTemplate> global_template,
145 v8::ExtensionConfiguration* extensions);
Andrei Popescu31002712010-02-23 13:46:05 +0000146 ~Genesis() { }
Steve Blocka7e24c12009-10-30 11:49:00 +0000147
148 Handle<Context> result() { return result_; }
149
150 Genesis* previous() { return previous_; }
Steve Blocka7e24c12009-10-30 11:49:00 +0000151
Ben Murdoch257744e2011-11-30 15:57:28 +0000152 Isolate* isolate() const { return isolate_; }
153 Factory* factory() const { return isolate_->factory(); }
154 Heap* heap() const { return isolate_->heap(); }
155
Steve Blocka7e24c12009-10-30 11:49:00 +0000156 private:
157 Handle<Context> global_context_;
Ben Murdoch257744e2011-11-30 15:57:28 +0000158 Isolate* isolate_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000159
160 // There may be more than one active genesis object: When GC is
161 // triggered during environment creation there may be weak handle
162 // processing callbacks which may create new environments.
163 Genesis* previous_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000164
165 Handle<Context> global_context() { return global_context_; }
166
Andrei Popescu31002712010-02-23 13:46:05 +0000167 // Creates some basic objects. Used for creating a context from scratch.
168 void CreateRoots();
169 // Creates the empty function. Used for creating a context from scratch.
Ben Murdoch257744e2011-11-30 15:57:28 +0000170 Handle<JSFunction> CreateEmptyFunction(Isolate* isolate);
Steve Block44f0eee2011-05-26 01:26:41 +0100171 // Creates the ThrowTypeError function. ECMA 5th Ed. 13.2.3
Ben Murdoch257744e2011-11-30 15:57:28 +0000172 Handle<JSFunction> GetThrowTypeErrorFunction();
Steve Block44f0eee2011-05-26 01:26:41 +0100173
174 void CreateStrictModeFunctionMaps(Handle<JSFunction> empty);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100175
176 // Make the "arguments" and "caller" properties throw a TypeError on access.
177 void PoisonArgumentsAndCaller(Handle<Map> map);
178
Andrei Popescu31002712010-02-23 13:46:05 +0000179 // Creates the global objects using the global and the template passed in
180 // through the API. We call this regardless of whether we are building a
181 // context from scratch or using a deserialized one from the partial snapshot
182 // but in the latter case we don't use the objects it produces directly, as
183 // we have to used the deserialized ones that are linked together with the
184 // rest of the context snapshot.
185 Handle<JSGlobalProxy> CreateNewGlobals(
186 v8::Handle<v8::ObjectTemplate> global_template,
187 Handle<Object> global_object,
188 Handle<GlobalObject>* global_proxy_out);
189 // Hooks the given global proxy into the context. If the context was created
190 // by deserialization then this will unhook the global proxy that was
191 // deserialized, leaving the GC to pick it up.
192 void HookUpGlobalProxy(Handle<GlobalObject> inner_global,
193 Handle<JSGlobalProxy> global_proxy);
Andrei Popescu402d9372010-02-26 13:31:12 +0000194 // Similarly, we want to use the inner global that has been created by the
195 // templates passed through the API. The inner global from the snapshot is
196 // detached from the other objects in the snapshot.
197 void HookUpInnerGlobal(Handle<GlobalObject> inner_global);
Andrei Popescu31002712010-02-23 13:46:05 +0000198 // New context initialization. Used for creating a context from scratch.
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100199 bool InitializeGlobal(Handle<GlobalObject> inner_global,
Andrei Popescu31002712010-02-23 13:46:05 +0000200 Handle<JSFunction> empty_function);
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000201 void InitializeExperimentalGlobal();
Andrei Popescu31002712010-02-23 13:46:05 +0000202 // Installs the contents of the native .js files on the global objects.
203 // Used for creating a context from scratch.
Steve Blocka7e24c12009-10-30 11:49:00 +0000204 void InstallNativeFunctions();
Ben Murdoch257744e2011-11-30 15:57:28 +0000205 void InstallExperimentalNativeFunctions();
Steve Blocka7e24c12009-10-30 11:49:00 +0000206 bool InstallNatives();
Ben Murdoch257744e2011-11-30 15:57:28 +0000207 bool InstallExperimentalNatives();
Ben Murdochb0fe1622011-05-05 13:52:32 +0100208 void InstallBuiltinFunctionIds();
Steve Block6ded16b2010-05-10 14:33:55 +0100209 void InstallJSFunctionResultCaches();
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100210 void InitializeNormalizedMapCaches();
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100211
212 enum ExtensionTraversalState {
213 UNVISITED, VISITED, INSTALLED
214 };
215
216 class ExtensionStates {
217 public:
218 ExtensionStates();
219 ExtensionTraversalState get_state(RegisteredExtension* extension);
220 void set_state(RegisteredExtension* extension,
221 ExtensionTraversalState state);
222 private:
223 HashMap map_;
224 DISALLOW_COPY_AND_ASSIGN(ExtensionStates);
225 };
226
Andrei Popescu31002712010-02-23 13:46:05 +0000227 // Used both for deserialized and from-scratch contexts to add the extensions
228 // provided.
229 static bool InstallExtensions(Handle<Context> global_context,
230 v8::ExtensionConfiguration* extensions);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100231 static bool InstallExtension(const char* name,
232 ExtensionStates* extension_states);
233 static bool InstallExtension(v8::RegisteredExtension* current,
234 ExtensionStates* extension_states);
Andrei Popescu31002712010-02-23 13:46:05 +0000235 static void InstallSpecialObjects(Handle<Context> global_context);
Andrei Popescu402d9372010-02-26 13:31:12 +0000236 bool InstallJSBuiltins(Handle<JSBuiltinsObject> builtins);
Steve Blocka7e24c12009-10-30 11:49:00 +0000237 bool ConfigureApiObject(Handle<JSObject> object,
238 Handle<ObjectTemplateInfo> object_template);
239 bool ConfigureGlobalObjects(v8::Handle<v8::ObjectTemplate> global_template);
240
241 // Migrates all properties from the 'from' object to the 'to'
242 // object and overrides the prototype in 'to' with the one from
243 // 'from'.
244 void TransferObject(Handle<JSObject> from, Handle<JSObject> to);
245 void TransferNamedProperties(Handle<JSObject> from, Handle<JSObject> to);
246 void TransferIndexedProperties(Handle<JSObject> from, Handle<JSObject> to);
247
Steve Block6ded16b2010-05-10 14:33:55 +0100248 enum PrototypePropertyMode {
249 DONT_ADD_PROTOTYPE,
250 ADD_READONLY_PROTOTYPE,
251 ADD_WRITEABLE_PROTOTYPE
252 };
Steve Block44f0eee2011-05-26 01:26:41 +0100253
254 Handle<Map> CreateFunctionMap(PrototypePropertyMode prototype_mode);
255
Steve Blocka7e24c12009-10-30 11:49:00 +0000256 Handle<DescriptorArray> ComputeFunctionInstanceDescriptor(
Steve Block6ded16b2010-05-10 14:33:55 +0100257 PrototypePropertyMode prototypeMode);
Steve Blocka7e24c12009-10-30 11:49:00 +0000258 void MakeFunctionInstancePrototypeWritable();
259
Steve Block44f0eee2011-05-26 01:26:41 +0100260 Handle<Map> CreateStrictModeFunctionMap(
261 PrototypePropertyMode prototype_mode,
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100262 Handle<JSFunction> empty_function);
Steve Block44f0eee2011-05-26 01:26:41 +0100263
264 Handle<DescriptorArray> ComputeStrictFunctionInstanceDescriptor(
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100265 PrototypePropertyMode propertyMode);
Steve Block44f0eee2011-05-26 01:26:41 +0100266
Ben Murdoch257744e2011-11-30 15:57:28 +0000267 static bool CompileBuiltin(Isolate* isolate, int index);
268 static bool CompileExperimentalBuiltin(Isolate* isolate, int index);
Steve Blocka7e24c12009-10-30 11:49:00 +0000269 static bool CompileNative(Vector<const char> name, Handle<String> source);
270 static bool CompileScriptCached(Vector<const char> name,
271 Handle<String> source,
272 SourceCodeCache* cache,
273 v8::Extension* extension,
Andrei Popescu31002712010-02-23 13:46:05 +0000274 Handle<Context> top_context,
Steve Blocka7e24c12009-10-30 11:49:00 +0000275 bool use_runtime_context);
276
277 Handle<Context> result_;
Steve Block44f0eee2011-05-26 01:26:41 +0100278
279 // Function instance maps. Function literal maps are created initially with
280 // a read only prototype for the processing of JS builtins. Later the function
281 // instance maps are replaced in order to make prototype writable.
282 // These are the final, writable prototype, maps.
283 Handle<Map> function_instance_map_writable_prototype_;
284 Handle<Map> strict_mode_function_instance_map_writable_prototype_;
Ben Murdoch257744e2011-11-30 15:57:28 +0000285 Handle<JSFunction> throw_type_error_function;
Steve Block44f0eee2011-05-26 01:26:41 +0100286
Andrei Popescu31002712010-02-23 13:46:05 +0000287 BootstrapperActive active_;
288 friend class Bootstrapper;
Steve Blocka7e24c12009-10-30 11:49:00 +0000289};
290
Steve Blocka7e24c12009-10-30 11:49:00 +0000291
292void Bootstrapper::Iterate(ObjectVisitor* v) {
Steve Block44f0eee2011-05-26 01:26:41 +0100293 extensions_cache_.Iterate(v);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100294 v->Synchronize(VisitorSynchronization::kExtensions);
Steve Blocka7e24c12009-10-30 11:49:00 +0000295}
296
297
Steve Blocka7e24c12009-10-30 11:49:00 +0000298Handle<Context> Bootstrapper::CreateEnvironment(
Ben Murdoch257744e2011-11-30 15:57:28 +0000299 Isolate* isolate,
Steve Blocka7e24c12009-10-30 11:49:00 +0000300 Handle<Object> global_object,
301 v8::Handle<v8::ObjectTemplate> global_template,
302 v8::ExtensionConfiguration* extensions) {
Andrei Popescu31002712010-02-23 13:46:05 +0000303 HandleScope scope;
304 Handle<Context> env;
Ben Murdoch257744e2011-11-30 15:57:28 +0000305 Genesis genesis(isolate, global_object, global_template, extensions);
Andrei Popescu31002712010-02-23 13:46:05 +0000306 env = genesis.result();
307 if (!env.is_null()) {
308 if (InstallExtensions(env, extensions)) {
309 return env;
310 }
311 }
312 return Handle<Context>();
Steve Blocka7e24c12009-10-30 11:49:00 +0000313}
314
315
316static void SetObjectPrototype(Handle<JSObject> object, Handle<Object> proto) {
317 // object.__proto__ = proto;
Ben Murdoch257744e2011-11-30 15:57:28 +0000318 Factory* factory = object->GetIsolate()->factory();
Steve Blocka7e24c12009-10-30 11:49:00 +0000319 Handle<Map> old_to_map = Handle<Map>(object->map());
Ben Murdoch257744e2011-11-30 15:57:28 +0000320 Handle<Map> new_to_map = factory->CopyMapDropTransitions(old_to_map);
Steve Blocka7e24c12009-10-30 11:49:00 +0000321 new_to_map->set_prototype(*proto);
322 object->set_map(*new_to_map);
323}
324
325
326void Bootstrapper::DetachGlobal(Handle<Context> env) {
Ben Murdoch257744e2011-11-30 15:57:28 +0000327 Factory* factory = env->GetIsolate()->factory();
Steve Block44f0eee2011-05-26 01:26:41 +0100328 JSGlobalProxy::cast(env->global_proxy())->set_context(*factory->null_value());
Steve Blocka7e24c12009-10-30 11:49:00 +0000329 SetObjectPrototype(Handle<JSObject>(env->global_proxy()),
Steve Block44f0eee2011-05-26 01:26:41 +0100330 factory->null_value());
Steve Blocka7e24c12009-10-30 11:49:00 +0000331 env->set_global_proxy(env->global());
332 env->global()->set_global_receiver(env->global());
333}
334
335
Andrei Popescu74b3c142010-03-29 12:03:09 +0100336void Bootstrapper::ReattachGlobal(Handle<Context> env,
337 Handle<Object> global_object) {
338 ASSERT(global_object->IsJSGlobalProxy());
339 Handle<JSGlobalProxy> global = Handle<JSGlobalProxy>::cast(global_object);
340 env->global()->set_global_receiver(*global);
341 env->set_global_proxy(*global);
342 SetObjectPrototype(global, Handle<JSObject>(env->global()));
343 global->set_context(*env);
344}
345
346
Steve Blocka7e24c12009-10-30 11:49:00 +0000347static Handle<JSFunction> InstallFunction(Handle<JSObject> target,
348 const char* name,
349 InstanceType type,
350 int instance_size,
351 Handle<JSObject> prototype,
352 Builtins::Name call,
353 bool is_ecma_native) {
Ben Murdoch257744e2011-11-30 15:57:28 +0000354 Isolate* isolate = target->GetIsolate();
Steve Block44f0eee2011-05-26 01:26:41 +0100355 Factory* factory = isolate->factory();
356 Handle<String> symbol = factory->LookupAsciiSymbol(name);
357 Handle<Code> call_code = Handle<Code>(isolate->builtins()->builtin(call));
Steve Block6ded16b2010-05-10 14:33:55 +0100358 Handle<JSFunction> function = prototype.is_null() ?
Steve Block44f0eee2011-05-26 01:26:41 +0100359 factory->NewFunctionWithoutPrototype(symbol, call_code) :
360 factory->NewFunctionWithPrototype(symbol,
Steve Blocka7e24c12009-10-30 11:49:00 +0000361 type,
362 instance_size,
363 prototype,
364 call_code,
365 is_ecma_native);
Ben Murdoch589d6972011-11-30 16:04:58 +0000366 PropertyAttributes attributes;
367 if (target->IsJSBuiltinsObject()) {
368 attributes =
369 static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE | READ_ONLY);
370 } else {
371 attributes = DONT_ENUM;
372 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100373 CHECK_NOT_EMPTY_HANDLE(isolate,
374 JSObject::SetLocalPropertyIgnoreAttributes(
375 target, symbol, function, attributes));
Steve Blocka7e24c12009-10-30 11:49:00 +0000376 if (is_ecma_native) {
377 function->shared()->set_instance_class_name(*symbol);
378 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100379 function->shared()->set_native(true);
Steve Blocka7e24c12009-10-30 11:49:00 +0000380 return function;
381}
382
383
384Handle<DescriptorArray> Genesis::ComputeFunctionInstanceDescriptor(
Steve Block6ded16b2010-05-10 14:33:55 +0100385 PrototypePropertyMode prototypeMode) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100386 int size = (prototypeMode == DONT_ADD_PROTOTYPE) ? 4 : 5;
387 Handle<DescriptorArray> descriptors(factory()->NewDescriptorArray(size));
388 PropertyAttributes attribs = static_cast<PropertyAttributes>(
389 DONT_ENUM | DONT_DELETE | READ_ONLY);
390
391 DescriptorArray::WhitenessWitness witness(*descriptors);
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000392
Steve Block44f0eee2011-05-26 01:26:41 +0100393 { // Add length.
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100394 Handle<Foreign> f(factory()->NewForeign(&Accessors::FunctionLength));
395 CallbacksDescriptor d(*factory()->length_symbol(), *f, attribs);
396 descriptors->Set(0, &d, witness);
Steve Block44f0eee2011-05-26 01:26:41 +0100397 }
398 { // Add name.
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100399 Handle<Foreign> f(factory()->NewForeign(&Accessors::FunctionName));
400 CallbacksDescriptor d(*factory()->name_symbol(), *f, attribs);
401 descriptors->Set(1, &d, witness);
Steve Block44f0eee2011-05-26 01:26:41 +0100402 }
403 { // Add arguments.
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100404 Handle<Foreign> f(factory()->NewForeign(&Accessors::FunctionArguments));
405 CallbacksDescriptor d(*factory()->arguments_symbol(), *f, attribs);
406 descriptors->Set(2, &d, witness);
Steve Block44f0eee2011-05-26 01:26:41 +0100407 }
408 { // Add caller.
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100409 Handle<Foreign> f(factory()->NewForeign(&Accessors::FunctionCaller));
410 CallbacksDescriptor d(*factory()->caller_symbol(), *f, attribs);
411 descriptors->Set(3, &d, witness);
Steve Block44f0eee2011-05-26 01:26:41 +0100412 }
413 if (prototypeMode != DONT_ADD_PROTOTYPE) {
414 // Add prototype.
415 if (prototypeMode == ADD_WRITEABLE_PROTOTYPE) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100416 attribs = static_cast<PropertyAttributes>(attribs & ~READ_ONLY);
Steve Block44f0eee2011-05-26 01:26:41 +0100417 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100418 Handle<Foreign> f(factory()->NewForeign(&Accessors::FunctionPrototype));
419 CallbacksDescriptor d(*factory()->prototype_symbol(), *f, attribs);
420 descriptors->Set(4, &d, witness);
Steve Block44f0eee2011-05-26 01:26:41 +0100421 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100422 descriptors->Sort(witness);
Steve Block44f0eee2011-05-26 01:26:41 +0100423 return descriptors;
424}
Steve Blocka7e24c12009-10-30 11:49:00 +0000425
Steve Blocka7e24c12009-10-30 11:49:00 +0000426
Steve Block44f0eee2011-05-26 01:26:41 +0100427Handle<Map> Genesis::CreateFunctionMap(PrototypePropertyMode prototype_mode) {
Ben Murdoch257744e2011-11-30 15:57:28 +0000428 Handle<Map> map = factory()->NewMap(JS_FUNCTION_TYPE, JSFunction::kSize);
Steve Block44f0eee2011-05-26 01:26:41 +0100429 Handle<DescriptorArray> descriptors =
430 ComputeFunctionInstanceDescriptor(prototype_mode);
431 map->set_instance_descriptors(*descriptors);
432 map->set_function_with_prototype(prototype_mode != DONT_ADD_PROTOTYPE);
433 return map;
Steve Blocka7e24c12009-10-30 11:49:00 +0000434}
435
436
Ben Murdoch257744e2011-11-30 15:57:28 +0000437Handle<JSFunction> Genesis::CreateEmptyFunction(Isolate* isolate) {
Steve Block44f0eee2011-05-26 01:26:41 +0100438 // Allocate the map for function instances. Maps are allocated first and their
439 // prototypes patched later, once empty function is created.
440
Steve Blocka7e24c12009-10-30 11:49:00 +0000441 // Please note that the prototype property for function instances must be
442 // writable.
Ben Murdoch8b112d22011-06-08 16:22:53 +0100443 Handle<Map> function_instance_map =
444 CreateFunctionMap(ADD_WRITEABLE_PROTOTYPE);
445 global_context()->set_function_instance_map(*function_instance_map);
Steve Block6ded16b2010-05-10 14:33:55 +0100446
447 // Functions with this map will not have a 'prototype' property, and
448 // can not be used as constructors.
Ben Murdoch8b112d22011-06-08 16:22:53 +0100449 Handle<Map> function_without_prototype_map =
450 CreateFunctionMap(DONT_ADD_PROTOTYPE);
Steve Block6ded16b2010-05-10 14:33:55 +0100451 global_context()->set_function_without_prototype_map(
Ben Murdoch8b112d22011-06-08 16:22:53 +0100452 *function_without_prototype_map);
Steve Blocka7e24c12009-10-30 11:49:00 +0000453
Steve Block44f0eee2011-05-26 01:26:41 +0100454 // Allocate the function map. This map is temporary, used only for processing
455 // of builtins.
456 // Later the map is replaced with writable prototype map, allocated below.
Ben Murdoch8b112d22011-06-08 16:22:53 +0100457 Handle<Map> function_map = CreateFunctionMap(ADD_READONLY_PROTOTYPE);
458 global_context()->set_function_map(*function_map);
Steve Blocka7e24c12009-10-30 11:49:00 +0000459
Steve Block44f0eee2011-05-26 01:26:41 +0100460 // The final map for functions. Writeable prototype.
461 // This map is installed in MakeFunctionInstancePrototypeWritable.
462 function_instance_map_writable_prototype_ =
463 CreateFunctionMap(ADD_WRITEABLE_PROTOTYPE);
464
Steve Block44f0eee2011-05-26 01:26:41 +0100465 Factory* factory = isolate->factory();
466 Heap* heap = isolate->heap();
467
468 Handle<String> object_name = Handle<String>(heap->Object_symbol());
Steve Blocka7e24c12009-10-30 11:49:00 +0000469
470 { // --- O b j e c t ---
471 Handle<JSFunction> object_fun =
Steve Block44f0eee2011-05-26 01:26:41 +0100472 factory->NewFunction(object_name, factory->null_value());
Steve Blocka7e24c12009-10-30 11:49:00 +0000473 Handle<Map> object_function_map =
Steve Block44f0eee2011-05-26 01:26:41 +0100474 factory->NewMap(JS_OBJECT_TYPE, JSObject::kHeaderSize);
Steve Blocka7e24c12009-10-30 11:49:00 +0000475 object_fun->set_initial_map(*object_function_map);
476 object_function_map->set_constructor(*object_fun);
477
478 global_context()->set_object_function(*object_fun);
479
480 // Allocate a new prototype for the object function.
Steve Block44f0eee2011-05-26 01:26:41 +0100481 Handle<JSObject> prototype = factory->NewJSObject(
482 isolate->object_function(),
483 TENURED);
Steve Blocka7e24c12009-10-30 11:49:00 +0000484
485 global_context()->set_initial_object_prototype(*prototype);
486 SetPrototype(object_fun, prototype);
487 object_function_map->
Steve Block44f0eee2011-05-26 01:26:41 +0100488 set_instance_descriptors(heap->empty_descriptor_array());
Steve Blocka7e24c12009-10-30 11:49:00 +0000489 }
490
491 // Allocate the empty function as the prototype for function ECMAScript
492 // 262 15.3.4.
Steve Block44f0eee2011-05-26 01:26:41 +0100493 Handle<String> symbol = factory->LookupAsciiSymbol("Empty");
Steve Blocka7e24c12009-10-30 11:49:00 +0000494 Handle<JSFunction> empty_function =
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100495 factory->NewFunctionWithoutPrototype(symbol, CLASSIC_MODE);
Steve Blocka7e24c12009-10-30 11:49:00 +0000496
Andrei Popescu31002712010-02-23 13:46:05 +0000497 // --- E m p t y ---
498 Handle<Code> code =
Steve Block44f0eee2011-05-26 01:26:41 +0100499 Handle<Code>(isolate->builtins()->builtin(
500 Builtins::kEmptyFunction));
Andrei Popescu31002712010-02-23 13:46:05 +0000501 empty_function->set_code(*code);
Iain Merrick75681382010-08-19 15:07:18 +0100502 empty_function->shared()->set_code(*code);
Steve Block44f0eee2011-05-26 01:26:41 +0100503 Handle<String> source = factory->NewStringFromAscii(CStrVector("() {}"));
504 Handle<Script> script = factory->NewScript(source);
Andrei Popescu31002712010-02-23 13:46:05 +0000505 script->set_type(Smi::FromInt(Script::TYPE_NATIVE));
506 empty_function->shared()->set_script(*script);
507 empty_function->shared()->set_start_position(0);
508 empty_function->shared()->set_end_position(source->length());
509 empty_function->shared()->DontAdaptArguments();
Steve Block44f0eee2011-05-26 01:26:41 +0100510
511 // Set prototypes for the function maps.
Andrei Popescu31002712010-02-23 13:46:05 +0000512 global_context()->function_map()->set_prototype(*empty_function);
513 global_context()->function_instance_map()->set_prototype(*empty_function);
Steve Block6ded16b2010-05-10 14:33:55 +0100514 global_context()->function_without_prototype_map()->
515 set_prototype(*empty_function);
Steve Block44f0eee2011-05-26 01:26:41 +0100516 function_instance_map_writable_prototype_->set_prototype(*empty_function);
Steve Blocka7e24c12009-10-30 11:49:00 +0000517
Andrei Popescu31002712010-02-23 13:46:05 +0000518 // Allocate the function map first and then patch the prototype later
Steve Block44f0eee2011-05-26 01:26:41 +0100519 Handle<Map> empty_fm = factory->CopyMapDropDescriptors(
Steve Block6ded16b2010-05-10 14:33:55 +0100520 function_without_prototype_map);
521 empty_fm->set_instance_descriptors(
Steve Block44f0eee2011-05-26 01:26:41 +0100522 function_without_prototype_map->instance_descriptors());
Andrei Popescu31002712010-02-23 13:46:05 +0000523 empty_fm->set_prototype(global_context()->object_function()->prototype());
524 empty_function->set_map(*empty_fm);
525 return empty_function;
526}
527
528
Steve Block44f0eee2011-05-26 01:26:41 +0100529Handle<DescriptorArray> Genesis::ComputeStrictFunctionInstanceDescriptor(
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100530 PrototypePropertyMode prototypeMode) {
531 int size = (prototypeMode == DONT_ADD_PROTOTYPE) ? 4 : 5;
532 Handle<DescriptorArray> descriptors(factory()->NewDescriptorArray(size));
533 PropertyAttributes attribs = static_cast<PropertyAttributes>(
534 DONT_ENUM | DONT_DELETE);
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000535
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100536 DescriptorArray::WhitenessWitness witness(*descriptors);
537
538 { // Add length.
539 Handle<Foreign> f(factory()->NewForeign(&Accessors::FunctionLength));
540 CallbacksDescriptor d(*factory()->length_symbol(), *f, attribs);
541 descriptors->Set(0, &d, witness);
Steve Block44f0eee2011-05-26 01:26:41 +0100542 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100543 { // Add name.
544 Handle<Foreign> f(factory()->NewForeign(&Accessors::FunctionName));
545 CallbacksDescriptor d(*factory()->name_symbol(), *f, attribs);
546 descriptors->Set(1, &d, witness);
Steve Block44f0eee2011-05-26 01:26:41 +0100547 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100548 { // Add arguments.
549 Handle<AccessorPair> arguments(factory()->NewAccessorPair());
550 CallbacksDescriptor d(*factory()->arguments_symbol(), *arguments, attribs);
551 descriptors->Set(2, &d, witness);
Steve Block44f0eee2011-05-26 01:26:41 +0100552 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100553 { // Add caller.
554 Handle<AccessorPair> caller(factory()->NewAccessorPair());
555 CallbacksDescriptor d(*factory()->caller_symbol(), *caller, attribs);
556 descriptors->Set(3, &d, witness);
Steve Block44f0eee2011-05-26 01:26:41 +0100557 }
558
Steve Block44f0eee2011-05-26 01:26:41 +0100559 if (prototypeMode != DONT_ADD_PROTOTYPE) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100560 // Add prototype.
561 if (prototypeMode != ADD_WRITEABLE_PROTOTYPE) {
562 attribs = static_cast<PropertyAttributes>(attribs | READ_ONLY);
Steve Block44f0eee2011-05-26 01:26:41 +0100563 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100564 Handle<Foreign> f(factory()->NewForeign(&Accessors::FunctionPrototype));
565 CallbacksDescriptor d(*factory()->prototype_symbol(), *f, attribs);
566 descriptors->Set(4, &d, witness);
Steve Block44f0eee2011-05-26 01:26:41 +0100567 }
568
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100569 descriptors->Sort(witness);
Steve Block44f0eee2011-05-26 01:26:41 +0100570 return descriptors;
571}
572
573
574// ECMAScript 5th Edition, 13.2.3
Ben Murdoch257744e2011-11-30 15:57:28 +0000575Handle<JSFunction> Genesis::GetThrowTypeErrorFunction() {
576 if (throw_type_error_function.is_null()) {
577 Handle<String> name = factory()->LookupAsciiSymbol("ThrowTypeError");
578 throw_type_error_function =
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100579 factory()->NewFunctionWithoutPrototype(name, CLASSIC_MODE);
Ben Murdoch257744e2011-11-30 15:57:28 +0000580 Handle<Code> code(isolate()->builtins()->builtin(
581 Builtins::kStrictModePoisonPill));
582 throw_type_error_function->set_map(
583 global_context()->function_map());
584 throw_type_error_function->set_code(*code);
585 throw_type_error_function->shared()->set_code(*code);
586 throw_type_error_function->shared()->DontAdaptArguments();
Steve Block053d10c2011-06-13 19:13:29 +0100587
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100588 JSObject::PreventExtensions(throw_type_error_function);
Ben Murdoch257744e2011-11-30 15:57:28 +0000589 }
590 return throw_type_error_function;
Steve Block44f0eee2011-05-26 01:26:41 +0100591}
592
593
594Handle<Map> Genesis::CreateStrictModeFunctionMap(
595 PrototypePropertyMode prototype_mode,
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100596 Handle<JSFunction> empty_function) {
Ben Murdoch257744e2011-11-30 15:57:28 +0000597 Handle<Map> map = factory()->NewMap(JS_FUNCTION_TYPE, JSFunction::kSize);
Steve Block44f0eee2011-05-26 01:26:41 +0100598 Handle<DescriptorArray> descriptors =
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100599 ComputeStrictFunctionInstanceDescriptor(prototype_mode);
Steve Block44f0eee2011-05-26 01:26:41 +0100600 map->set_instance_descriptors(*descriptors);
601 map->set_function_with_prototype(prototype_mode != DONT_ADD_PROTOTYPE);
602 map->set_prototype(*empty_function);
603 return map;
604}
605
606
607void Genesis::CreateStrictModeFunctionMaps(Handle<JSFunction> empty) {
Steve Block44f0eee2011-05-26 01:26:41 +0100608 // Allocate map for the strict mode function instances.
Ben Murdoch8b112d22011-06-08 16:22:53 +0100609 Handle<Map> strict_mode_function_instance_map =
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100610 CreateStrictModeFunctionMap(ADD_WRITEABLE_PROTOTYPE, empty);
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 Murdoch3ef787d2012-04-12 10:51:47 +0100616 CreateStrictModeFunctionMap(DONT_ADD_PROTOTYPE, empty);
Steve Block44f0eee2011-05-26 01:26:41 +0100617 global_context()->set_strict_mode_function_without_prototype_map(
Ben Murdoch8b112d22011-06-08 16:22:53 +0100618 *strict_mode_function_without_prototype_map);
Steve Block44f0eee2011-05-26 01:26:41 +0100619
620 // Allocate map for the strict mode functions. This map is temporary, used
621 // only for processing of builtins.
622 // Later the map is replaced with writable prototype map, allocated below.
Ben Murdoch8b112d22011-06-08 16:22:53 +0100623 Handle<Map> strict_mode_function_map =
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100624 CreateStrictModeFunctionMap(ADD_READONLY_PROTOTYPE, empty);
Steve Block44f0eee2011-05-26 01:26:41 +0100625 global_context()->set_strict_mode_function_map(
Ben Murdoch8b112d22011-06-08 16:22:53 +0100626 *strict_mode_function_map);
Steve Block44f0eee2011-05-26 01:26:41 +0100627
628 // The final map for the strict mode functions. Writeable prototype.
629 // This map is installed in MakeFunctionInstancePrototypeWritable.
630 strict_mode_function_instance_map_writable_prototype_ =
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100631 CreateStrictModeFunctionMap(ADD_WRITEABLE_PROTOTYPE, empty);
Steve Block44f0eee2011-05-26 01:26:41 +0100632
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100633 // Complete the callbacks.
634 PoisonArgumentsAndCaller(strict_mode_function_instance_map);
635 PoisonArgumentsAndCaller(strict_mode_function_without_prototype_map);
636 PoisonArgumentsAndCaller(strict_mode_function_map);
637 PoisonArgumentsAndCaller(
638 strict_mode_function_instance_map_writable_prototype_);
639}
Ben Murdoch5d4cdbf2012-04-11 10:23:59 +0100640
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100641
642static void SetAccessors(Handle<Map> map,
643 Handle<String> name,
644 Handle<JSFunction> func) {
645 DescriptorArray* descs = map->instance_descriptors();
646 int number = descs->Search(*name);
647 AccessorPair* accessors = AccessorPair::cast(descs->GetValue(number));
648 accessors->set_getter(*func);
649 accessors->set_setter(*func);
650}
651
652
653void Genesis::PoisonArgumentsAndCaller(Handle<Map> map) {
654 SetAccessors(map, factory()->arguments_symbol(), GetThrowTypeErrorFunction());
655 SetAccessors(map, factory()->caller_symbol(), GetThrowTypeErrorFunction());
Steve Block44f0eee2011-05-26 01:26:41 +0100656}
657
658
Ben Murdochb0fe1622011-05-05 13:52:32 +0100659static void AddToWeakGlobalContextList(Context* context) {
660 ASSERT(context->IsGlobalContext());
Ben Murdoch257744e2011-11-30 15:57:28 +0000661 Heap* heap = context->GetIsolate()->heap();
Ben Murdochb0fe1622011-05-05 13:52:32 +0100662#ifdef DEBUG
663 { // NOLINT
664 ASSERT(context->get(Context::NEXT_CONTEXT_LINK)->IsUndefined());
665 // Check that context is not in the list yet.
Steve Block44f0eee2011-05-26 01:26:41 +0100666 for (Object* current = heap->global_contexts_list();
Ben Murdochb0fe1622011-05-05 13:52:32 +0100667 !current->IsUndefined();
668 current = Context::cast(current)->get(Context::NEXT_CONTEXT_LINK)) {
669 ASSERT(current != context);
670 }
671 }
672#endif
Steve Block44f0eee2011-05-26 01:26:41 +0100673 context->set(Context::NEXT_CONTEXT_LINK, heap->global_contexts_list());
674 heap->set_global_contexts_list(context);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100675}
676
677
Andrei Popescu31002712010-02-23 13:46:05 +0000678void Genesis::CreateRoots() {
679 // Allocate the global context FixedArray first and then patch the
680 // closure and extension object later (we need the empty function
681 // and the global object, but in order to create those, we need the
682 // global context).
Ben Murdoch257744e2011-11-30 15:57:28 +0000683 global_context_ = Handle<Context>::cast(isolate()->global_handles()->Create(
684 *factory()->NewGlobalContext()));
Ben Murdochb0fe1622011-05-05 13:52:32 +0100685 AddToWeakGlobalContextList(*global_context_);
Ben Murdoch257744e2011-11-30 15:57:28 +0000686 isolate()->set_context(*global_context());
Andrei Popescu31002712010-02-23 13:46:05 +0000687
688 // Allocate the message listeners object.
689 {
690 v8::NeanderArray listeners;
691 global_context()->set_message_listeners(*listeners.value());
692 }
693}
694
695
696Handle<JSGlobalProxy> Genesis::CreateNewGlobals(
697 v8::Handle<v8::ObjectTemplate> global_template,
698 Handle<Object> global_object,
699 Handle<GlobalObject>* inner_global_out) {
700 // The argument global_template aka data is an ObjectTemplateInfo.
701 // It has a constructor pointer that points at global_constructor which is a
702 // FunctionTemplateInfo.
703 // The global_constructor is used to create or reinitialize the global_proxy.
704 // The global_constructor also has a prototype_template pointer that points at
705 // js_global_template which is an ObjectTemplateInfo.
706 // That in turn has a constructor pointer that points at
707 // js_global_constructor which is a FunctionTemplateInfo.
708 // js_global_constructor is used to make js_global_function
709 // js_global_function is used to make the new inner_global.
710 //
711 // --- G l o b a l ---
712 // Step 1: Create a fresh inner JSGlobalObject.
713 Handle<JSFunction> js_global_function;
714 Handle<ObjectTemplateInfo> js_global_template;
715 if (!global_template.IsEmpty()) {
716 // Get prototype template of the global_template.
717 Handle<ObjectTemplateInfo> data =
718 v8::Utils::OpenHandle(*global_template);
719 Handle<FunctionTemplateInfo> global_constructor =
720 Handle<FunctionTemplateInfo>(
721 FunctionTemplateInfo::cast(data->constructor()));
722 Handle<Object> proto_template(global_constructor->prototype_template());
723 if (!proto_template->IsUndefined()) {
724 js_global_template =
725 Handle<ObjectTemplateInfo>::cast(proto_template);
726 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000727 }
728
Andrei Popescu31002712010-02-23 13:46:05 +0000729 if (js_global_template.is_null()) {
Ben Murdoch257744e2011-11-30 15:57:28 +0000730 Handle<String> name = Handle<String>(heap()->empty_symbol());
731 Handle<Code> code = Handle<Code>(isolate()->builtins()->builtin(
Steve Block44f0eee2011-05-26 01:26:41 +0100732 Builtins::kIllegal));
Andrei Popescu31002712010-02-23 13:46:05 +0000733 js_global_function =
Ben Murdoch257744e2011-11-30 15:57:28 +0000734 factory()->NewFunction(name, JS_GLOBAL_OBJECT_TYPE,
735 JSGlobalObject::kSize, code, true);
Andrei Popescu31002712010-02-23 13:46:05 +0000736 // Change the constructor property of the prototype of the
737 // hidden global function to refer to the Object function.
738 Handle<JSObject> prototype =
739 Handle<JSObject>(
740 JSObject::cast(js_global_function->instance_prototype()));
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100741 CHECK_NOT_EMPTY_HANDLE(isolate(),
742 JSObject::SetLocalPropertyIgnoreAttributes(
743 prototype, factory()->constructor_symbol(),
744 isolate()->object_function(), NONE));
Andrei Popescu31002712010-02-23 13:46:05 +0000745 } else {
746 Handle<FunctionTemplateInfo> js_global_constructor(
747 FunctionTemplateInfo::cast(js_global_template->constructor()));
748 js_global_function =
Ben Murdoch257744e2011-11-30 15:57:28 +0000749 factory()->CreateApiFunction(js_global_constructor,
750 factory()->InnerGlobalObject);
Steve Blocka7e24c12009-10-30 11:49:00 +0000751 }
752
Andrei Popescu31002712010-02-23 13:46:05 +0000753 js_global_function->initial_map()->set_is_hidden_prototype();
754 Handle<GlobalObject> inner_global =
Ben Murdoch257744e2011-11-30 15:57:28 +0000755 factory()->NewGlobalObject(js_global_function);
Andrei Popescu31002712010-02-23 13:46:05 +0000756 if (inner_global_out != NULL) {
757 *inner_global_out = inner_global;
758 }
759
760 // Step 2: create or re-initialize the global proxy object.
761 Handle<JSFunction> global_proxy_function;
762 if (global_template.IsEmpty()) {
Ben Murdoch257744e2011-11-30 15:57:28 +0000763 Handle<String> name = Handle<String>(heap()->empty_symbol());
764 Handle<Code> code = Handle<Code>(isolate()->builtins()->builtin(
Steve Block44f0eee2011-05-26 01:26:41 +0100765 Builtins::kIllegal));
Andrei Popescu31002712010-02-23 13:46:05 +0000766 global_proxy_function =
Ben Murdoch257744e2011-11-30 15:57:28 +0000767 factory()->NewFunction(name, JS_GLOBAL_PROXY_TYPE,
768 JSGlobalProxy::kSize, code, true);
Andrei Popescu31002712010-02-23 13:46:05 +0000769 } else {
770 Handle<ObjectTemplateInfo> data =
771 v8::Utils::OpenHandle(*global_template);
772 Handle<FunctionTemplateInfo> global_constructor(
773 FunctionTemplateInfo::cast(data->constructor()));
774 global_proxy_function =
Ben Murdoch257744e2011-11-30 15:57:28 +0000775 factory()->CreateApiFunction(global_constructor,
776 factory()->OuterGlobalObject);
Andrei Popescu31002712010-02-23 13:46:05 +0000777 }
778
Ben Murdoch257744e2011-11-30 15:57:28 +0000779 Handle<String> global_name = factory()->LookupAsciiSymbol("global");
Andrei Popescu31002712010-02-23 13:46:05 +0000780 global_proxy_function->shared()->set_instance_class_name(*global_name);
781 global_proxy_function->initial_map()->set_is_access_check_needed(true);
782
783 // Set global_proxy.__proto__ to js_global after ConfigureGlobalObjects
784 // Return the global proxy.
785
786 if (global_object.location() != NULL) {
787 ASSERT(global_object->IsJSGlobalProxy());
788 return ReinitializeJSGlobalProxy(
789 global_proxy_function,
790 Handle<JSGlobalProxy>::cast(global_object));
791 } else {
792 return Handle<JSGlobalProxy>::cast(
Ben Murdoch257744e2011-11-30 15:57:28 +0000793 factory()->NewJSObject(global_proxy_function, TENURED));
Andrei Popescu31002712010-02-23 13:46:05 +0000794 }
795}
796
797
798void Genesis::HookUpGlobalProxy(Handle<GlobalObject> inner_global,
799 Handle<JSGlobalProxy> global_proxy) {
800 // Set the global context for the global object.
801 inner_global->set_global_context(*global_context());
802 inner_global->set_global_receiver(*global_proxy);
803 global_proxy->set_context(*global_context());
804 global_context()->set_global_proxy(*global_proxy);
805}
806
807
Andrei Popescu402d9372010-02-26 13:31:12 +0000808void Genesis::HookUpInnerGlobal(Handle<GlobalObject> inner_global) {
809 Handle<GlobalObject> inner_global_from_snapshot(
810 GlobalObject::cast(global_context_->extension()));
811 Handle<JSBuiltinsObject> builtins_global(global_context_->builtins());
812 global_context_->set_extension(*inner_global);
813 global_context_->set_global(*inner_global);
814 global_context_->set_security_token(*inner_global);
815 static const PropertyAttributes attributes =
816 static_cast<PropertyAttributes>(READ_ONLY | DONT_DELETE);
817 ForceSetProperty(builtins_global,
Ben Murdoch257744e2011-11-30 15:57:28 +0000818 factory()->LookupAsciiSymbol("global"),
Andrei Popescu402d9372010-02-26 13:31:12 +0000819 inner_global,
820 attributes);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100821 // Set up the reference from the global object to the builtins object.
Andrei Popescu402d9372010-02-26 13:31:12 +0000822 JSGlobalObject::cast(*inner_global)->set_builtins(*builtins_global);
823 TransferNamedProperties(inner_global_from_snapshot, inner_global);
824 TransferIndexedProperties(inner_global_from_snapshot, inner_global);
825}
826
827
828// This is only called if we are not using snapshots. The equivalent
829// work in the snapshot case is done in HookUpInnerGlobal.
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100830bool Genesis::InitializeGlobal(Handle<GlobalObject> inner_global,
Andrei Popescu31002712010-02-23 13:46:05 +0000831 Handle<JSFunction> empty_function) {
832 // --- G l o b a l C o n t e x t ---
833 // Use the empty function as closure (no scope info).
834 global_context()->set_closure(*empty_function);
Andrei Popescu31002712010-02-23 13:46:05 +0000835 global_context()->set_previous(NULL);
836 // Set extension and global object.
837 global_context()->set_extension(*inner_global);
838 global_context()->set_global(*inner_global);
839 // Security setup: Set the security token of the global object to
840 // its the inner global. This makes the security check between two
841 // different contexts fail by default even in case of global
842 // object reinitialization.
843 global_context()->set_security_token(*inner_global);
844
Ben Murdoch257744e2011-11-30 15:57:28 +0000845 Isolate* isolate = inner_global->GetIsolate();
Steve Block44f0eee2011-05-26 01:26:41 +0100846 Factory* factory = isolate->factory();
847 Heap* heap = isolate->heap();
848
849 Handle<String> object_name = Handle<String>(heap->Object_symbol());
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100850 CHECK_NOT_EMPTY_HANDLE(isolate,
851 JSObject::SetLocalPropertyIgnoreAttributes(
852 inner_global, object_name,
853 isolate->object_function(), DONT_ENUM));
Andrei Popescu31002712010-02-23 13:46:05 +0000854
Steve Blocka7e24c12009-10-30 11:49:00 +0000855 Handle<JSObject> global = Handle<JSObject>(global_context()->global());
856
857 // Install global Function object
858 InstallFunction(global, "Function", JS_FUNCTION_TYPE, JSFunction::kSize,
Steve Block44f0eee2011-05-26 01:26:41 +0100859 empty_function, Builtins::kIllegal, true); // ECMA native.
Steve Blocka7e24c12009-10-30 11:49:00 +0000860
861 { // --- A r r a y ---
862 Handle<JSFunction> array_function =
863 InstallFunction(global, "Array", JS_ARRAY_TYPE, JSArray::kSize,
Steve Block44f0eee2011-05-26 01:26:41 +0100864 isolate->initial_object_prototype(),
865 Builtins::kArrayCode, true);
Steve Blocka7e24c12009-10-30 11:49:00 +0000866 array_function->shared()->set_construct_stub(
Steve Block44f0eee2011-05-26 01:26:41 +0100867 isolate->builtins()->builtin(Builtins::kArrayConstructCode));
Steve Blocka7e24c12009-10-30 11:49:00 +0000868 array_function->shared()->DontAdaptArguments();
869
870 // This seems a bit hackish, but we need to make sure Array.length
871 // is 1.
872 array_function->shared()->set_length(1);
873 Handle<DescriptorArray> array_descriptors =
Ben Murdoch257744e2011-11-30 15:57:28 +0000874 factory->CopyAppendForeignDescriptor(
Steve Block44f0eee2011-05-26 01:26:41 +0100875 factory->empty_descriptor_array(),
876 factory->length_symbol(),
Ben Murdoch257744e2011-11-30 15:57:28 +0000877 factory->NewForeign(&Accessors::ArrayLength),
Steve Blocka7e24c12009-10-30 11:49:00 +0000878 static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE));
879
Steve Blocka7e24c12009-10-30 11:49:00 +0000880 // array_function is used internally. JS code creating array object should
881 // search for the 'Array' property on the global object and use that one
882 // as the constructor. 'Array' property on a global object can be
883 // overwritten by JS code.
884 global_context()->set_array_function(*array_function);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100885 array_function->initial_map()->set_instance_descriptors(*array_descriptors);
Steve Blocka7e24c12009-10-30 11:49:00 +0000886 }
887
888 { // --- N u m b e r ---
889 Handle<JSFunction> number_fun =
890 InstallFunction(global, "Number", JS_VALUE_TYPE, JSValue::kSize,
Steve Block44f0eee2011-05-26 01:26:41 +0100891 isolate->initial_object_prototype(),
892 Builtins::kIllegal, true);
Steve Blocka7e24c12009-10-30 11:49:00 +0000893 global_context()->set_number_function(*number_fun);
894 }
895
896 { // --- B o o l e a n ---
897 Handle<JSFunction> boolean_fun =
898 InstallFunction(global, "Boolean", JS_VALUE_TYPE, JSValue::kSize,
Steve Block44f0eee2011-05-26 01:26:41 +0100899 isolate->initial_object_prototype(),
900 Builtins::kIllegal, true);
Steve Blocka7e24c12009-10-30 11:49:00 +0000901 global_context()->set_boolean_function(*boolean_fun);
902 }
903
904 { // --- S t r i n g ---
905 Handle<JSFunction> string_fun =
906 InstallFunction(global, "String", JS_VALUE_TYPE, JSValue::kSize,
Steve Block44f0eee2011-05-26 01:26:41 +0100907 isolate->initial_object_prototype(),
908 Builtins::kIllegal, true);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100909 string_fun->shared()->set_construct_stub(
Steve Block44f0eee2011-05-26 01:26:41 +0100910 isolate->builtins()->builtin(Builtins::kStringConstructCode));
Steve Blocka7e24c12009-10-30 11:49:00 +0000911 global_context()->set_string_function(*string_fun);
912 // Add 'length' property to strings.
913 Handle<DescriptorArray> string_descriptors =
Ben Murdoch257744e2011-11-30 15:57:28 +0000914 factory->CopyAppendForeignDescriptor(
Steve Block44f0eee2011-05-26 01:26:41 +0100915 factory->empty_descriptor_array(),
916 factory->length_symbol(),
Ben Murdoch257744e2011-11-30 15:57:28 +0000917 factory->NewForeign(&Accessors::StringLength),
Steve Blocka7e24c12009-10-30 11:49:00 +0000918 static_cast<PropertyAttributes>(DONT_ENUM |
919 DONT_DELETE |
920 READ_ONLY));
921
922 Handle<Map> string_map =
923 Handle<Map>(global_context()->string_function()->initial_map());
924 string_map->set_instance_descriptors(*string_descriptors);
925 }
926
927 { // --- D a t e ---
928 // Builtin functions for Date.prototype.
929 Handle<JSFunction> date_fun =
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100930 InstallFunction(global, "Date", JS_DATE_TYPE, JSDate::kSize,
Steve Block44f0eee2011-05-26 01:26:41 +0100931 isolate->initial_object_prototype(),
932 Builtins::kIllegal, true);
Steve Blocka7e24c12009-10-30 11:49:00 +0000933
934 global_context()->set_date_function(*date_fun);
935 }
936
937
938 { // -- R e g E x p
939 // Builtin functions for RegExp.prototype.
940 Handle<JSFunction> regexp_fun =
941 InstallFunction(global, "RegExp", JS_REGEXP_TYPE, JSRegExp::kSize,
Steve Block44f0eee2011-05-26 01:26:41 +0100942 isolate->initial_object_prototype(),
943 Builtins::kIllegal, true);
Steve Blocka7e24c12009-10-30 11:49:00 +0000944 global_context()->set_regexp_function(*regexp_fun);
Steve Block6ded16b2010-05-10 14:33:55 +0100945
946 ASSERT(regexp_fun->has_initial_map());
947 Handle<Map> initial_map(regexp_fun->initial_map());
948
949 ASSERT_EQ(0, initial_map->inobject_properties());
950
Steve Block44f0eee2011-05-26 01:26:41 +0100951 Handle<DescriptorArray> descriptors = factory->NewDescriptorArray(5);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100952 DescriptorArray::WhitenessWitness witness(*descriptors);
Steve Block6ded16b2010-05-10 14:33:55 +0100953 PropertyAttributes final =
954 static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE | READ_ONLY);
955 int enum_index = 0;
956 {
957 // ECMA-262, section 15.10.7.1.
Steve Block44f0eee2011-05-26 01:26:41 +0100958 FieldDescriptor field(heap->source_symbol(),
Steve Block6ded16b2010-05-10 14:33:55 +0100959 JSRegExp::kSourceFieldIndex,
960 final,
961 enum_index++);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100962 descriptors->Set(0, &field, witness);
Steve Block6ded16b2010-05-10 14:33:55 +0100963 }
964 {
965 // ECMA-262, section 15.10.7.2.
Steve Block44f0eee2011-05-26 01:26:41 +0100966 FieldDescriptor field(heap->global_symbol(),
Steve Block6ded16b2010-05-10 14:33:55 +0100967 JSRegExp::kGlobalFieldIndex,
968 final,
969 enum_index++);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100970 descriptors->Set(1, &field, witness);
Steve Block6ded16b2010-05-10 14:33:55 +0100971 }
972 {
973 // ECMA-262, section 15.10.7.3.
Steve Block44f0eee2011-05-26 01:26:41 +0100974 FieldDescriptor field(heap->ignore_case_symbol(),
Steve Block6ded16b2010-05-10 14:33:55 +0100975 JSRegExp::kIgnoreCaseFieldIndex,
976 final,
977 enum_index++);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100978 descriptors->Set(2, &field, witness);
Steve Block6ded16b2010-05-10 14:33:55 +0100979 }
980 {
981 // ECMA-262, section 15.10.7.4.
Steve Block44f0eee2011-05-26 01:26:41 +0100982 FieldDescriptor field(heap->multiline_symbol(),
Steve Block6ded16b2010-05-10 14:33:55 +0100983 JSRegExp::kMultilineFieldIndex,
984 final,
985 enum_index++);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100986 descriptors->Set(3, &field, witness);
Steve Block6ded16b2010-05-10 14:33:55 +0100987 }
988 {
989 // ECMA-262, section 15.10.7.5.
990 PropertyAttributes writable =
991 static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE);
Steve Block44f0eee2011-05-26 01:26:41 +0100992 FieldDescriptor field(heap->last_index_symbol(),
Steve Block6ded16b2010-05-10 14:33:55 +0100993 JSRegExp::kLastIndexFieldIndex,
994 writable,
995 enum_index++);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100996 descriptors->Set(4, &field, witness);
Steve Block6ded16b2010-05-10 14:33:55 +0100997 }
998 descriptors->SetNextEnumerationIndex(enum_index);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100999 descriptors->Sort(witness);
Steve Block6ded16b2010-05-10 14:33:55 +01001000
1001 initial_map->set_inobject_properties(5);
1002 initial_map->set_pre_allocated_property_fields(5);
1003 initial_map->set_unused_property_fields(0);
1004 initial_map->set_instance_size(
1005 initial_map->instance_size() + 5 * kPointerSize);
1006 initial_map->set_instance_descriptors(*descriptors);
Iain Merrick75681382010-08-19 15:07:18 +01001007 initial_map->set_visitor_id(StaticVisitorBase::GetVisitorId(*initial_map));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001008
1009 // RegExp prototype object is itself a RegExp.
1010 Handle<Map> proto_map = factory->CopyMapDropTransitions(initial_map);
1011 proto_map->set_prototype(global_context()->initial_object_prototype());
1012 Handle<JSObject> proto = factory->NewJSObjectFromMap(proto_map);
1013 proto->InObjectPropertyAtPut(JSRegExp::kSourceFieldIndex,
1014 heap->empty_string());
1015 proto->InObjectPropertyAtPut(JSRegExp::kGlobalFieldIndex,
1016 heap->false_value());
1017 proto->InObjectPropertyAtPut(JSRegExp::kIgnoreCaseFieldIndex,
1018 heap->false_value());
1019 proto->InObjectPropertyAtPut(JSRegExp::kMultilineFieldIndex,
1020 heap->false_value());
1021 proto->InObjectPropertyAtPut(JSRegExp::kLastIndexFieldIndex,
1022 Smi::FromInt(0),
1023 SKIP_WRITE_BARRIER); // It's a Smi.
1024 initial_map->set_prototype(*proto);
1025 factory->SetRegExpIrregexpData(Handle<JSRegExp>::cast(proto),
1026 JSRegExp::IRREGEXP, factory->empty_string(),
1027 JSRegExp::Flags(0), 0);
Steve Blocka7e24c12009-10-30 11:49:00 +00001028 }
1029
1030 { // -- J S O N
Steve Block44f0eee2011-05-26 01:26:41 +01001031 Handle<String> name = factory->NewStringFromAscii(CStrVector("JSON"));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001032 Handle<JSFunction> cons = factory->NewFunction(name,
1033 factory->the_hole_value());
1034 { MaybeObject* result = cons->SetInstancePrototype(
1035 global_context()->initial_object_prototype());
1036 if (result->IsFailure()) return false;
1037 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001038 cons->SetInstanceClassName(*name);
Steve Block44f0eee2011-05-26 01:26:41 +01001039 Handle<JSObject> json_object = factory->NewJSObject(cons, TENURED);
Steve Blocka7e24c12009-10-30 11:49:00 +00001040 ASSERT(json_object->IsJSObject());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001041 CHECK_NOT_EMPTY_HANDLE(isolate,
1042 JSObject::SetLocalPropertyIgnoreAttributes(
1043 global, name, json_object, DONT_ENUM));
Steve Blocka7e24c12009-10-30 11:49:00 +00001044 global_context()->set_json_object(*json_object);
1045 }
1046
1047 { // --- arguments_boilerplate_
1048 // Make sure we can recognize argument objects at runtime.
1049 // This is done by introducing an anonymous function with
1050 // class_name equals 'Arguments'.
Steve Block44f0eee2011-05-26 01:26:41 +01001051 Handle<String> symbol = factory->LookupAsciiSymbol("Arguments");
1052 Handle<Code> code = Handle<Code>(
1053 isolate->builtins()->builtin(Builtins::kIllegal));
Steve Blocka7e24c12009-10-30 11:49:00 +00001054 Handle<JSObject> prototype =
1055 Handle<JSObject>(
1056 JSObject::cast(global_context()->object_function()->prototype()));
1057
1058 Handle<JSFunction> function =
Steve Block44f0eee2011-05-26 01:26:41 +01001059 factory->NewFunctionWithPrototype(symbol,
Steve Blocka7e24c12009-10-30 11:49:00 +00001060 JS_OBJECT_TYPE,
1061 JSObject::kHeaderSize,
1062 prototype,
1063 code,
1064 false);
1065 ASSERT(!function->has_initial_map());
1066 function->shared()->set_instance_class_name(*symbol);
1067 function->shared()->set_expected_nof_properties(2);
Steve Block44f0eee2011-05-26 01:26:41 +01001068 Handle<JSObject> result = factory->NewJSObject(function);
Steve Blocka7e24c12009-10-30 11:49:00 +00001069
1070 global_context()->set_arguments_boilerplate(*result);
Steve Block44f0eee2011-05-26 01:26:41 +01001071 // Note: length must be added as the first property and
1072 // callee must be added as the second property.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001073 CHECK_NOT_EMPTY_HANDLE(isolate,
1074 JSObject::SetLocalPropertyIgnoreAttributes(
1075 result, factory->length_symbol(),
1076 factory->undefined_value(), DONT_ENUM));
1077 CHECK_NOT_EMPTY_HANDLE(isolate,
1078 JSObject::SetLocalPropertyIgnoreAttributes(
1079 result, factory->callee_symbol(),
1080 factory->undefined_value(), DONT_ENUM));
Steve Blocka7e24c12009-10-30 11:49:00 +00001081
1082#ifdef DEBUG
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001083 LookupResult lookup(isolate);
Steve Block44f0eee2011-05-26 01:26:41 +01001084 result->LocalLookup(heap->callee_symbol(), &lookup);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001085 ASSERT(lookup.IsFound() && (lookup.type() == FIELD));
Steve Block44f0eee2011-05-26 01:26:41 +01001086 ASSERT(lookup.GetFieldIndex() == Heap::kArgumentsCalleeIndex);
Steve Blocka7e24c12009-10-30 11:49:00 +00001087
Steve Block44f0eee2011-05-26 01:26:41 +01001088 result->LocalLookup(heap->length_symbol(), &lookup);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001089 ASSERT(lookup.IsFound() && (lookup.type() == FIELD));
Steve Block44f0eee2011-05-26 01:26:41 +01001090 ASSERT(lookup.GetFieldIndex() == Heap::kArgumentsLengthIndex);
Steve Blocka7e24c12009-10-30 11:49:00 +00001091
Steve Block44f0eee2011-05-26 01:26:41 +01001092 ASSERT(result->map()->inobject_properties() > Heap::kArgumentsCalleeIndex);
1093 ASSERT(result->map()->inobject_properties() > Heap::kArgumentsLengthIndex);
1094
1095 // Check the state of the object.
1096 ASSERT(result->HasFastProperties());
1097 ASSERT(result->HasFastElements());
1098#endif
1099 }
1100
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001101 { // --- aliased_arguments_boilerplate_
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001102 // Set up a well-formed parameter map to make assertions happy.
1103 Handle<FixedArray> elements = factory->NewFixedArray(2);
1104 elements->set_map(heap->non_strict_arguments_elements_map());
1105 Handle<FixedArray> array;
1106 array = factory->NewFixedArray(0);
1107 elements->set(0, *array);
1108 array = factory->NewFixedArray(0);
1109 elements->set(1, *array);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001110
1111 Handle<Map> old_map(global_context()->arguments_boilerplate()->map());
1112 Handle<Map> new_map = factory->CopyMapDropTransitions(old_map);
1113 new_map->set_pre_allocated_property_fields(2);
1114 Handle<JSObject> result = factory->NewJSObjectFromMap(new_map);
1115 // Set elements kind after allocating the object because
1116 // NewJSObjectFromMap assumes a fast elements map.
1117 new_map->set_elements_kind(NON_STRICT_ARGUMENTS_ELEMENTS);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001118 result->set_elements(*elements);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001119 ASSERT(result->HasNonStrictArgumentsElements());
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001120 global_context()->set_aliased_arguments_boilerplate(*result);
1121 }
1122
Steve Block44f0eee2011-05-26 01:26:41 +01001123 { // --- strict mode arguments boilerplate
1124 const PropertyAttributes attributes =
1125 static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE | READ_ONLY);
1126
1127 // Create the ThrowTypeError functions.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001128 Handle<AccessorPair> callee = factory->NewAccessorPair();
1129 Handle<AccessorPair> caller = factory->NewAccessorPair();
Steve Block44f0eee2011-05-26 01:26:41 +01001130
Ben Murdoch257744e2011-11-30 15:57:28 +00001131 Handle<JSFunction> throw_function =
1132 GetThrowTypeErrorFunction();
Steve Block44f0eee2011-05-26 01:26:41 +01001133
1134 // Install the ThrowTypeError functions.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001135 callee->set_getter(*throw_function);
1136 callee->set_setter(*throw_function);
1137 caller->set_getter(*throw_function);
1138 caller->set_setter(*throw_function);
Steve Block44f0eee2011-05-26 01:26:41 +01001139
1140 // Create the descriptor array for the arguments object.
1141 Handle<DescriptorArray> descriptors = factory->NewDescriptorArray(3);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001142 DescriptorArray::WhitenessWitness witness(*descriptors);
Steve Block44f0eee2011-05-26 01:26:41 +01001143 { // length
1144 FieldDescriptor d(*factory->length_symbol(), 0, DONT_ENUM);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001145 descriptors->Set(0, &d, witness);
Steve Block44f0eee2011-05-26 01:26:41 +01001146 }
1147 { // callee
1148 CallbacksDescriptor d(*factory->callee_symbol(), *callee, attributes);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001149 descriptors->Set(1, &d, witness);
Steve Block44f0eee2011-05-26 01:26:41 +01001150 }
1151 { // caller
1152 CallbacksDescriptor d(*factory->caller_symbol(), *caller, attributes);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001153 descriptors->Set(2, &d, witness);
Steve Block44f0eee2011-05-26 01:26:41 +01001154 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001155 descriptors->Sort(witness);
Steve Block44f0eee2011-05-26 01:26:41 +01001156
1157 // Create the map. Allocate one in-object field for length.
1158 Handle<Map> map = factory->NewMap(JS_OBJECT_TYPE,
1159 Heap::kArgumentsObjectSizeStrict);
1160 map->set_instance_descriptors(*descriptors);
1161 map->set_function_with_prototype(true);
1162 map->set_prototype(global_context()->object_function()->prototype());
1163 map->set_pre_allocated_property_fields(1);
1164 map->set_inobject_properties(1);
1165
1166 // Copy constructor from the non-strict arguments boilerplate.
1167 map->set_constructor(
1168 global_context()->arguments_boilerplate()->map()->constructor());
1169
1170 // Allocate the arguments boilerplate object.
1171 Handle<JSObject> result = factory->NewJSObjectFromMap(map);
1172 global_context()->set_strict_mode_arguments_boilerplate(*result);
1173
1174 // Add length property only for strict mode boilerplate.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001175 CHECK_NOT_EMPTY_HANDLE(isolate,
1176 JSObject::SetLocalPropertyIgnoreAttributes(
1177 result, factory->length_symbol(),
1178 factory->undefined_value(), DONT_ENUM));
Steve Block44f0eee2011-05-26 01:26:41 +01001179
1180#ifdef DEBUG
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001181 LookupResult lookup(isolate);
Steve Block44f0eee2011-05-26 01:26:41 +01001182 result->LocalLookup(heap->length_symbol(), &lookup);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001183 ASSERT(lookup.IsFound() && (lookup.type() == FIELD));
Steve Block44f0eee2011-05-26 01:26:41 +01001184 ASSERT(lookup.GetFieldIndex() == Heap::kArgumentsLengthIndex);
1185
1186 ASSERT(result->map()->inobject_properties() > Heap::kArgumentsLengthIndex);
Steve Blocka7e24c12009-10-30 11:49:00 +00001187
1188 // Check the state of the object.
1189 ASSERT(result->HasFastProperties());
1190 ASSERT(result->HasFastElements());
1191#endif
1192 }
1193
1194 { // --- context extension
1195 // Create a function for the context extension objects.
Steve Block44f0eee2011-05-26 01:26:41 +01001196 Handle<Code> code = Handle<Code>(
1197 isolate->builtins()->builtin(Builtins::kIllegal));
Steve Blocka7e24c12009-10-30 11:49:00 +00001198 Handle<JSFunction> context_extension_fun =
Steve Block44f0eee2011-05-26 01:26:41 +01001199 factory->NewFunction(factory->empty_symbol(),
Steve Blocka7e24c12009-10-30 11:49:00 +00001200 JS_CONTEXT_EXTENSION_OBJECT_TYPE,
1201 JSObject::kHeaderSize,
1202 code,
1203 true);
1204
Steve Block44f0eee2011-05-26 01:26:41 +01001205 Handle<String> name = factory->LookupAsciiSymbol("context_extension");
Steve Blocka7e24c12009-10-30 11:49:00 +00001206 context_extension_fun->shared()->set_instance_class_name(*name);
1207 global_context()->set_context_extension_function(*context_extension_fun);
1208 }
1209
1210
1211 {
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001212 // Set up the call-as-function delegate.
Steve Blocka7e24c12009-10-30 11:49:00 +00001213 Handle<Code> code =
Steve Block44f0eee2011-05-26 01:26:41 +01001214 Handle<Code>(isolate->builtins()->builtin(
1215 Builtins::kHandleApiCallAsFunction));
Steve Blocka7e24c12009-10-30 11:49:00 +00001216 Handle<JSFunction> delegate =
Steve Block44f0eee2011-05-26 01:26:41 +01001217 factory->NewFunction(factory->empty_symbol(), JS_OBJECT_TYPE,
Steve Blocka7e24c12009-10-30 11:49:00 +00001218 JSObject::kHeaderSize, code, true);
1219 global_context()->set_call_as_function_delegate(*delegate);
1220 delegate->shared()->DontAdaptArguments();
1221 }
1222
1223 {
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001224 // Set up the call-as-constructor delegate.
Steve Blocka7e24c12009-10-30 11:49:00 +00001225 Handle<Code> code =
Steve Block44f0eee2011-05-26 01:26:41 +01001226 Handle<Code>(isolate->builtins()->builtin(
1227 Builtins::kHandleApiCallAsConstructor));
Steve Blocka7e24c12009-10-30 11:49:00 +00001228 Handle<JSFunction> delegate =
Steve Block44f0eee2011-05-26 01:26:41 +01001229 factory->NewFunction(factory->empty_symbol(), JS_OBJECT_TYPE,
Steve Blocka7e24c12009-10-30 11:49:00 +00001230 JSObject::kHeaderSize, code, true);
1231 global_context()->set_call_as_constructor_delegate(*delegate);
1232 delegate->shared()->DontAdaptArguments();
1233 }
1234
Steve Blocka7e24c12009-10-30 11:49:00 +00001235 // Initialize the out of memory slot.
Steve Block44f0eee2011-05-26 01:26:41 +01001236 global_context()->set_out_of_memory(heap->false_value());
Steve Blocka7e24c12009-10-30 11:49:00 +00001237
1238 // Initialize the data slot.
Steve Block44f0eee2011-05-26 01:26:41 +01001239 global_context()->set_data(heap->undefined_value());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001240
1241 {
1242 // Initialize the random seed slot.
1243 Handle<ByteArray> zeroed_byte_array(
1244 factory->NewByteArray(kRandomStateSize));
1245 global_context()->set_random_seed(*zeroed_byte_array);
1246 memset(zeroed_byte_array->GetDataStartAddress(), 0, kRandomStateSize);
1247 }
1248 return true;
Steve Blocka7e24c12009-10-30 11:49:00 +00001249}
1250
1251
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001252void Genesis::InitializeExperimentalGlobal() {
1253 Handle<JSObject> global = Handle<JSObject>(global_context()->global());
1254
1255 // TODO(mstarzinger): Move this into Genesis::InitializeGlobal once we no
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001256 // longer need to live behind a flag, so functions get added to the snapshot.
1257 if (FLAG_harmony_collections) {
1258 { // -- S e t
1259 Handle<JSObject> prototype =
1260 factory()->NewJSObject(isolate()->object_function(), TENURED);
1261 InstallFunction(global, "Set", JS_SET_TYPE, JSSet::kSize,
1262 prototype, Builtins::kIllegal, true);
1263 }
1264 { // -- M a p
1265 Handle<JSObject> prototype =
1266 factory()->NewJSObject(isolate()->object_function(), TENURED);
1267 InstallFunction(global, "Map", JS_MAP_TYPE, JSMap::kSize,
1268 prototype, Builtins::kIllegal, true);
1269 }
1270 { // -- W e a k M a p
1271 Handle<JSObject> prototype =
1272 factory()->NewJSObject(isolate()->object_function(), TENURED);
1273 InstallFunction(global, "WeakMap", JS_WEAK_MAP_TYPE, JSWeakMap::kSize,
1274 prototype, Builtins::kIllegal, true);
1275 }
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001276 }
1277}
1278
1279
Ben Murdoch257744e2011-11-30 15:57:28 +00001280bool Genesis::CompileBuiltin(Isolate* isolate, int index) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001281 Vector<const char> name = Natives::GetScriptName(index);
Steve Block44f0eee2011-05-26 01:26:41 +01001282 Handle<String> source_code =
Ben Murdoch257744e2011-11-30 15:57:28 +00001283 isolate->bootstrapper()->NativesSourceLookup(index);
1284 return CompileNative(name, source_code);
1285}
1286
1287
1288bool Genesis::CompileExperimentalBuiltin(Isolate* isolate, int index) {
1289 Vector<const char> name = ExperimentalNatives::GetScriptName(index);
1290 Factory* factory = isolate->factory();
1291 Handle<String> source_code =
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001292 factory->NewStringFromAscii(
1293 ExperimentalNatives::GetRawScriptSource(index));
Steve Blocka7e24c12009-10-30 11:49:00 +00001294 return CompileNative(name, source_code);
1295}
1296
1297
1298bool Genesis::CompileNative(Vector<const char> name, Handle<String> source) {
1299 HandleScope scope;
Ben Murdoch257744e2011-11-30 15:57:28 +00001300 Isolate* isolate = source->GetIsolate();
Steve Blocka7e24c12009-10-30 11:49:00 +00001301#ifdef ENABLE_DEBUGGER_SUPPORT
Steve Block44f0eee2011-05-26 01:26:41 +01001302 isolate->debugger()->set_compiling_natives(true);
Steve Blocka7e24c12009-10-30 11:49:00 +00001303#endif
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001304 // During genesis, the boilerplate for stack overflow won't work until the
1305 // environment has been at least partially initialized. Add a stack check
1306 // before entering JS code to catch overflow early.
1307 StackLimitCheck check(Isolate::Current());
1308 if (check.HasOverflowed()) return false;
1309
Andrei Popescu31002712010-02-23 13:46:05 +00001310 bool result = CompileScriptCached(name,
1311 source,
1312 NULL,
1313 NULL,
Steve Block44f0eee2011-05-26 01:26:41 +01001314 Handle<Context>(isolate->context()),
Andrei Popescu31002712010-02-23 13:46:05 +00001315 true);
Steve Block44f0eee2011-05-26 01:26:41 +01001316 ASSERT(isolate->has_pending_exception() != result);
1317 if (!result) isolate->clear_pending_exception();
Steve Blocka7e24c12009-10-30 11:49:00 +00001318#ifdef ENABLE_DEBUGGER_SUPPORT
Steve Block44f0eee2011-05-26 01:26:41 +01001319 isolate->debugger()->set_compiling_natives(false);
Steve Blocka7e24c12009-10-30 11:49:00 +00001320#endif
1321 return result;
1322}
1323
1324
1325bool Genesis::CompileScriptCached(Vector<const char> name,
1326 Handle<String> source,
1327 SourceCodeCache* cache,
1328 v8::Extension* extension,
Andrei Popescu31002712010-02-23 13:46:05 +00001329 Handle<Context> top_context,
Steve Blocka7e24c12009-10-30 11:49:00 +00001330 bool use_runtime_context) {
Ben Murdoch257744e2011-11-30 15:57:28 +00001331 Factory* factory = source->GetIsolate()->factory();
Steve Blocka7e24c12009-10-30 11:49:00 +00001332 HandleScope scope;
Steve Block6ded16b2010-05-10 14:33:55 +01001333 Handle<SharedFunctionInfo> function_info;
Steve Blocka7e24c12009-10-30 11:49:00 +00001334
1335 // If we can't find the function in the cache, we compile a new
1336 // function and insert it into the cache.
Steve Block6ded16b2010-05-10 14:33:55 +01001337 if (cache == NULL || !cache->Lookup(name, &function_info)) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001338 ASSERT(source->IsAsciiRepresentation());
Steve Block44f0eee2011-05-26 01:26:41 +01001339 Handle<String> script_name = factory->NewStringFromUtf8(name);
Steve Block6ded16b2010-05-10 14:33:55 +01001340 function_info = Compiler::Compile(
Andrei Popescu31002712010-02-23 13:46:05 +00001341 source,
1342 script_name,
1343 0,
1344 0,
1345 extension,
1346 NULL,
Andrei Popescu402d9372010-02-26 13:31:12 +00001347 Handle<String>::null(),
Andrei Popescu31002712010-02-23 13:46:05 +00001348 use_runtime_context ? NATIVES_CODE : NOT_NATIVES_CODE);
Steve Block6ded16b2010-05-10 14:33:55 +01001349 if (function_info.is_null()) return false;
1350 if (cache != NULL) cache->Add(name, function_info);
Steve Blocka7e24c12009-10-30 11:49:00 +00001351 }
1352
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001353 // Set up the function context. Conceptually, we should clone the
Steve Blocka7e24c12009-10-30 11:49:00 +00001354 // function before overwriting the context but since we're in a
1355 // single-threaded environment it is not strictly necessary.
Andrei Popescu31002712010-02-23 13:46:05 +00001356 ASSERT(top_context->IsGlobalContext());
Steve Blocka7e24c12009-10-30 11:49:00 +00001357 Handle<Context> context =
1358 Handle<Context>(use_runtime_context
Andrei Popescu31002712010-02-23 13:46:05 +00001359 ? Handle<Context>(top_context->runtime_context())
1360 : top_context);
Steve Blocka7e24c12009-10-30 11:49:00 +00001361 Handle<JSFunction> fun =
Steve Block44f0eee2011-05-26 01:26:41 +01001362 factory->NewFunctionFromSharedFunctionInfo(function_info, context);
Steve Blocka7e24c12009-10-30 11:49:00 +00001363
Leon Clarke4515c472010-02-03 11:58:03 +00001364 // Call function using either the runtime object or the global
Steve Blocka7e24c12009-10-30 11:49:00 +00001365 // object as the receiver. Provide no parameters.
1366 Handle<Object> receiver =
1367 Handle<Object>(use_runtime_context
Andrei Popescu31002712010-02-23 13:46:05 +00001368 ? top_context->builtins()
1369 : top_context->global());
Steve Blocka7e24c12009-10-30 11:49:00 +00001370 bool has_pending_exception;
Ben Murdoch257744e2011-11-30 15:57:28 +00001371 Execution::Call(fun, receiver, 0, NULL, &has_pending_exception);
Steve Blocka7e24c12009-10-30 11:49:00 +00001372 if (has_pending_exception) return false;
Andrei Popescu402d9372010-02-26 13:31:12 +00001373 return true;
Steve Blocka7e24c12009-10-30 11:49:00 +00001374}
1375
1376
Ben Murdoch257744e2011-11-30 15:57:28 +00001377#define INSTALL_NATIVE(Type, name, var) \
1378 Handle<String> var##_name = factory()->LookupAsciiSymbol(name); \
1379 Object* var##_native = \
1380 global_context()->builtins()->GetPropertyNoExceptionThrown( \
1381 *var##_name); \
Ben Murdoch8b112d22011-06-08 16:22:53 +01001382 global_context()->set_##var(Type::cast(var##_native));
Steve Blocka7e24c12009-10-30 11:49:00 +00001383
Steve Block44f0eee2011-05-26 01:26:41 +01001384
Steve Blocka7e24c12009-10-30 11:49:00 +00001385void Genesis::InstallNativeFunctions() {
1386 HandleScope scope;
1387 INSTALL_NATIVE(JSFunction, "CreateDate", create_date_fun);
1388 INSTALL_NATIVE(JSFunction, "ToNumber", to_number_fun);
1389 INSTALL_NATIVE(JSFunction, "ToString", to_string_fun);
1390 INSTALL_NATIVE(JSFunction, "ToDetailString", to_detail_string_fun);
1391 INSTALL_NATIVE(JSFunction, "ToObject", to_object_fun);
1392 INSTALL_NATIVE(JSFunction, "ToInteger", to_integer_fun);
1393 INSTALL_NATIVE(JSFunction, "ToUint32", to_uint32_fun);
1394 INSTALL_NATIVE(JSFunction, "ToInt32", to_int32_fun);
Leon Clarkee46be812010-01-19 14:06:41 +00001395 INSTALL_NATIVE(JSFunction, "GlobalEval", global_eval_fun);
Steve Blocka7e24c12009-10-30 11:49:00 +00001396 INSTALL_NATIVE(JSFunction, "Instantiate", instantiate_fun);
1397 INSTALL_NATIVE(JSFunction, "ConfigureTemplateInstance",
1398 configure_instance_fun);
Steve Blocka7e24c12009-10-30 11:49:00 +00001399 INSTALL_NATIVE(JSFunction, "GetStackTraceLine", get_stack_trace_line_fun);
1400 INSTALL_NATIVE(JSObject, "functionCache", function_cache);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001401 INSTALL_NATIVE(JSFunction, "ToCompletePropertyDescriptor",
1402 to_complete_property_descriptor);
Steve Blocka7e24c12009-10-30 11:49:00 +00001403}
1404
Ben Murdoch257744e2011-11-30 15:57:28 +00001405void Genesis::InstallExperimentalNativeFunctions() {
1406 if (FLAG_harmony_proxies) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001407 INSTALL_NATIVE(JSFunction, "DerivedHasTrap", derived_has_trap);
Ben Murdoch257744e2011-11-30 15:57:28 +00001408 INSTALL_NATIVE(JSFunction, "DerivedGetTrap", derived_get_trap);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001409 INSTALL_NATIVE(JSFunction, "DerivedSetTrap", derived_set_trap);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001410 INSTALL_NATIVE(JSFunction, "ProxyEnumerate", proxy_enumerate);
Ben Murdoch257744e2011-11-30 15:57:28 +00001411 }
1412}
1413
Steve Blocka7e24c12009-10-30 11:49:00 +00001414#undef INSTALL_NATIVE
1415
1416
1417bool Genesis::InstallNatives() {
1418 HandleScope scope;
1419
1420 // Create a function for the builtins object. Allocate space for the
1421 // JavaScript builtins, a reference to the builtins object
1422 // (itself) and a reference to the global_context directly in the object.
Steve Block44f0eee2011-05-26 01:26:41 +01001423 Handle<Code> code = Handle<Code>(
Ben Murdoch257744e2011-11-30 15:57:28 +00001424 isolate()->builtins()->builtin(Builtins::kIllegal));
Steve Blocka7e24c12009-10-30 11:49:00 +00001425 Handle<JSFunction> builtins_fun =
Ben Murdoch257744e2011-11-30 15:57:28 +00001426 factory()->NewFunction(factory()->empty_symbol(),
1427 JS_BUILTINS_OBJECT_TYPE,
1428 JSBuiltinsObject::kSize, code, true);
Steve Blocka7e24c12009-10-30 11:49:00 +00001429
Ben Murdoch257744e2011-11-30 15:57:28 +00001430 Handle<String> name = factory()->LookupAsciiSymbol("builtins");
Steve Blocka7e24c12009-10-30 11:49:00 +00001431 builtins_fun->shared()->set_instance_class_name(*name);
1432
1433 // Allocate the builtins object.
1434 Handle<JSBuiltinsObject> builtins =
Ben Murdoch257744e2011-11-30 15:57:28 +00001435 Handle<JSBuiltinsObject>::cast(factory()->NewGlobalObject(builtins_fun));
Steve Blocka7e24c12009-10-30 11:49:00 +00001436 builtins->set_builtins(*builtins);
1437 builtins->set_global_context(*global_context());
1438 builtins->set_global_receiver(*builtins);
1439
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001440 // Set up the 'global' properties of the builtins object. The
Steve Blocka7e24c12009-10-30 11:49:00 +00001441 // 'global' property that refers to the global object is the only
1442 // way to get from code running in the builtins context to the
1443 // global object.
1444 static const PropertyAttributes attributes =
1445 static_cast<PropertyAttributes>(READ_ONLY | DONT_DELETE);
Ben Murdoch257744e2011-11-30 15:57:28 +00001446 Handle<String> global_symbol = factory()->LookupAsciiSymbol("global");
Steve Block1e0659c2011-05-24 12:43:12 +01001447 Handle<Object> global_obj(global_context()->global());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001448 CHECK_NOT_EMPTY_HANDLE(isolate(),
1449 JSObject::SetLocalPropertyIgnoreAttributes(
1450 builtins, global_symbol, global_obj, attributes));
Steve Blocka7e24c12009-10-30 11:49:00 +00001451
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001452 // Set up the reference from the global object to the builtins object.
Steve Blocka7e24c12009-10-30 11:49:00 +00001453 JSGlobalObject::cast(global_context()->global())->set_builtins(*builtins);
1454
1455 // Create a bridge function that has context in the global context.
1456 Handle<JSFunction> bridge =
Ben Murdoch257744e2011-11-30 15:57:28 +00001457 factory()->NewFunction(factory()->empty_symbol(),
1458 factory()->undefined_value());
1459 ASSERT(bridge->context() == *isolate()->global_context());
Steve Blocka7e24c12009-10-30 11:49:00 +00001460
1461 // Allocate the builtins context.
1462 Handle<Context> context =
Ben Murdoch257744e2011-11-30 15:57:28 +00001463 factory()->NewFunctionContext(Context::MIN_CONTEXT_SLOTS, bridge);
Steve Blocka7e24c12009-10-30 11:49:00 +00001464 context->set_global(*builtins); // override builtins global object
1465
1466 global_context()->set_runtime_context(*context);
1467
1468 { // -- S c r i p t
1469 // Builtin functions for Script.
1470 Handle<JSFunction> script_fun =
1471 InstallFunction(builtins, "Script", JS_VALUE_TYPE, JSValue::kSize,
Ben Murdoch257744e2011-11-30 15:57:28 +00001472 isolate()->initial_object_prototype(),
Steve Block44f0eee2011-05-26 01:26:41 +01001473 Builtins::kIllegal, false);
Steve Blocka7e24c12009-10-30 11:49:00 +00001474 Handle<JSObject> prototype =
Ben Murdoch257744e2011-11-30 15:57:28 +00001475 factory()->NewJSObject(isolate()->object_function(), TENURED);
Steve Blocka7e24c12009-10-30 11:49:00 +00001476 SetPrototype(script_fun, prototype);
1477 global_context()->set_script_function(*script_fun);
1478
1479 // Add 'source' and 'data' property to scripts.
1480 PropertyAttributes common_attributes =
1481 static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE | READ_ONLY);
Ben Murdoch257744e2011-11-30 15:57:28 +00001482 Handle<Foreign> foreign_source =
1483 factory()->NewForeign(&Accessors::ScriptSource);
Steve Blocka7e24c12009-10-30 11:49:00 +00001484 Handle<DescriptorArray> script_descriptors =
Ben Murdoch257744e2011-11-30 15:57:28 +00001485 factory()->CopyAppendForeignDescriptor(
1486 factory()->empty_descriptor_array(),
1487 factory()->LookupAsciiSymbol("source"),
1488 foreign_source,
Steve Blocka7e24c12009-10-30 11:49:00 +00001489 common_attributes);
Ben Murdoch257744e2011-11-30 15:57:28 +00001490 Handle<Foreign> foreign_name =
1491 factory()->NewForeign(&Accessors::ScriptName);
Steve Blocka7e24c12009-10-30 11:49:00 +00001492 script_descriptors =
Ben Murdoch257744e2011-11-30 15:57:28 +00001493 factory()->CopyAppendForeignDescriptor(
Steve Blocka7e24c12009-10-30 11:49:00 +00001494 script_descriptors,
Ben Murdoch257744e2011-11-30 15:57:28 +00001495 factory()->LookupAsciiSymbol("name"),
1496 foreign_name,
Steve Blocka7e24c12009-10-30 11:49:00 +00001497 common_attributes);
Ben Murdoch257744e2011-11-30 15:57:28 +00001498 Handle<Foreign> foreign_id = factory()->NewForeign(&Accessors::ScriptId);
Steve Blocka7e24c12009-10-30 11:49:00 +00001499 script_descriptors =
Ben Murdoch257744e2011-11-30 15:57:28 +00001500 factory()->CopyAppendForeignDescriptor(
Steve Blocka7e24c12009-10-30 11:49:00 +00001501 script_descriptors,
Ben Murdoch257744e2011-11-30 15:57:28 +00001502 factory()->LookupAsciiSymbol("id"),
1503 foreign_id,
Steve Blocka7e24c12009-10-30 11:49:00 +00001504 common_attributes);
Ben Murdoch257744e2011-11-30 15:57:28 +00001505 Handle<Foreign> foreign_line_offset =
1506 factory()->NewForeign(&Accessors::ScriptLineOffset);
Steve Blocka7e24c12009-10-30 11:49:00 +00001507 script_descriptors =
Ben Murdoch257744e2011-11-30 15:57:28 +00001508 factory()->CopyAppendForeignDescriptor(
Steve Blocka7e24c12009-10-30 11:49:00 +00001509 script_descriptors,
Ben Murdoch257744e2011-11-30 15:57:28 +00001510 factory()->LookupAsciiSymbol("line_offset"),
1511 foreign_line_offset,
Steve Blocka7e24c12009-10-30 11:49:00 +00001512 common_attributes);
Ben Murdoch257744e2011-11-30 15:57:28 +00001513 Handle<Foreign> foreign_column_offset =
1514 factory()->NewForeign(&Accessors::ScriptColumnOffset);
Steve Blocka7e24c12009-10-30 11:49:00 +00001515 script_descriptors =
Ben Murdoch257744e2011-11-30 15:57:28 +00001516 factory()->CopyAppendForeignDescriptor(
Steve Blocka7e24c12009-10-30 11:49:00 +00001517 script_descriptors,
Ben Murdoch257744e2011-11-30 15:57:28 +00001518 factory()->LookupAsciiSymbol("column_offset"),
1519 foreign_column_offset,
Steve Blocka7e24c12009-10-30 11:49:00 +00001520 common_attributes);
Ben Murdoch257744e2011-11-30 15:57:28 +00001521 Handle<Foreign> foreign_data =
1522 factory()->NewForeign(&Accessors::ScriptData);
Steve Blocka7e24c12009-10-30 11:49:00 +00001523 script_descriptors =
Ben Murdoch257744e2011-11-30 15:57:28 +00001524 factory()->CopyAppendForeignDescriptor(
Steve Blocka7e24c12009-10-30 11:49:00 +00001525 script_descriptors,
Ben Murdoch257744e2011-11-30 15:57:28 +00001526 factory()->LookupAsciiSymbol("data"),
1527 foreign_data,
Steve Blocka7e24c12009-10-30 11:49:00 +00001528 common_attributes);
Ben Murdoch257744e2011-11-30 15:57:28 +00001529 Handle<Foreign> foreign_type =
1530 factory()->NewForeign(&Accessors::ScriptType);
Steve Blocka7e24c12009-10-30 11:49:00 +00001531 script_descriptors =
Ben Murdoch257744e2011-11-30 15:57:28 +00001532 factory()->CopyAppendForeignDescriptor(
Steve Blocka7e24c12009-10-30 11:49:00 +00001533 script_descriptors,
Ben Murdoch257744e2011-11-30 15:57:28 +00001534 factory()->LookupAsciiSymbol("type"),
1535 foreign_type,
Steve Blocka7e24c12009-10-30 11:49:00 +00001536 common_attributes);
Ben Murdoch257744e2011-11-30 15:57:28 +00001537 Handle<Foreign> foreign_compilation_type =
1538 factory()->NewForeign(&Accessors::ScriptCompilationType);
Steve Blocka7e24c12009-10-30 11:49:00 +00001539 script_descriptors =
Ben Murdoch257744e2011-11-30 15:57:28 +00001540 factory()->CopyAppendForeignDescriptor(
Steve Blocka7e24c12009-10-30 11:49:00 +00001541 script_descriptors,
Ben Murdoch257744e2011-11-30 15:57:28 +00001542 factory()->LookupAsciiSymbol("compilation_type"),
1543 foreign_compilation_type,
Steve Blocka7e24c12009-10-30 11:49:00 +00001544 common_attributes);
Ben Murdoch257744e2011-11-30 15:57:28 +00001545 Handle<Foreign> foreign_line_ends =
1546 factory()->NewForeign(&Accessors::ScriptLineEnds);
Steve Blocka7e24c12009-10-30 11:49:00 +00001547 script_descriptors =
Ben Murdoch257744e2011-11-30 15:57:28 +00001548 factory()->CopyAppendForeignDescriptor(
Steve Blocka7e24c12009-10-30 11:49:00 +00001549 script_descriptors,
Ben Murdoch257744e2011-11-30 15:57:28 +00001550 factory()->LookupAsciiSymbol("line_ends"),
1551 foreign_line_ends,
Steve Blocka7e24c12009-10-30 11:49:00 +00001552 common_attributes);
Ben Murdoch257744e2011-11-30 15:57:28 +00001553 Handle<Foreign> foreign_context_data =
1554 factory()->NewForeign(&Accessors::ScriptContextData);
Steve Blocka7e24c12009-10-30 11:49:00 +00001555 script_descriptors =
Ben Murdoch257744e2011-11-30 15:57:28 +00001556 factory()->CopyAppendForeignDescriptor(
Steve Blocka7e24c12009-10-30 11:49:00 +00001557 script_descriptors,
Ben Murdoch257744e2011-11-30 15:57:28 +00001558 factory()->LookupAsciiSymbol("context_data"),
1559 foreign_context_data,
Steve Blocka7e24c12009-10-30 11:49:00 +00001560 common_attributes);
Ben Murdoch257744e2011-11-30 15:57:28 +00001561 Handle<Foreign> foreign_eval_from_script =
1562 factory()->NewForeign(&Accessors::ScriptEvalFromScript);
Steve Blocka7e24c12009-10-30 11:49:00 +00001563 script_descriptors =
Ben Murdoch257744e2011-11-30 15:57:28 +00001564 factory()->CopyAppendForeignDescriptor(
Steve Blocka7e24c12009-10-30 11:49:00 +00001565 script_descriptors,
Ben Murdoch257744e2011-11-30 15:57:28 +00001566 factory()->LookupAsciiSymbol("eval_from_script"),
1567 foreign_eval_from_script,
Steve Blocka7e24c12009-10-30 11:49:00 +00001568 common_attributes);
Ben Murdoch257744e2011-11-30 15:57:28 +00001569 Handle<Foreign> foreign_eval_from_script_position =
1570 factory()->NewForeign(&Accessors::ScriptEvalFromScriptPosition);
Steve Blocka7e24c12009-10-30 11:49:00 +00001571 script_descriptors =
Ben Murdoch257744e2011-11-30 15:57:28 +00001572 factory()->CopyAppendForeignDescriptor(
Steve Blocka7e24c12009-10-30 11:49:00 +00001573 script_descriptors,
Ben Murdoch257744e2011-11-30 15:57:28 +00001574 factory()->LookupAsciiSymbol("eval_from_script_position"),
1575 foreign_eval_from_script_position,
Steve Blockd0582a62009-12-15 09:54:21 +00001576 common_attributes);
Ben Murdoch257744e2011-11-30 15:57:28 +00001577 Handle<Foreign> foreign_eval_from_function_name =
1578 factory()->NewForeign(&Accessors::ScriptEvalFromFunctionName);
Steve Blockd0582a62009-12-15 09:54:21 +00001579 script_descriptors =
Ben Murdoch257744e2011-11-30 15:57:28 +00001580 factory()->CopyAppendForeignDescriptor(
Steve Blockd0582a62009-12-15 09:54:21 +00001581 script_descriptors,
Ben Murdoch257744e2011-11-30 15:57:28 +00001582 factory()->LookupAsciiSymbol("eval_from_function_name"),
1583 foreign_eval_from_function_name,
Steve Blocka7e24c12009-10-30 11:49:00 +00001584 common_attributes);
1585
1586 Handle<Map> script_map = Handle<Map>(script_fun->initial_map());
1587 script_map->set_instance_descriptors(*script_descriptors);
1588
1589 // Allocate the empty script.
Ben Murdoch257744e2011-11-30 15:57:28 +00001590 Handle<Script> script = factory()->NewScript(factory()->empty_string());
Steve Blocka7e24c12009-10-30 11:49:00 +00001591 script->set_type(Smi::FromInt(Script::TYPE_NATIVE));
Ben Murdoch257744e2011-11-30 15:57:28 +00001592 heap()->public_set_empty_script(*script);
Steve Blocka7e24c12009-10-30 11:49:00 +00001593 }
Steve Block6ded16b2010-05-10 14:33:55 +01001594 {
1595 // Builtin function for OpaqueReference -- a JSValue-based object,
1596 // that keeps its field isolated from JavaScript code. It may store
1597 // objects, that JavaScript code may not access.
1598 Handle<JSFunction> opaque_reference_fun =
1599 InstallFunction(builtins, "OpaqueReference", JS_VALUE_TYPE,
Steve Block44f0eee2011-05-26 01:26:41 +01001600 JSValue::kSize,
Ben Murdoch257744e2011-11-30 15:57:28 +00001601 isolate()->initial_object_prototype(),
Steve Block44f0eee2011-05-26 01:26:41 +01001602 Builtins::kIllegal, false);
Steve Block6ded16b2010-05-10 14:33:55 +01001603 Handle<JSObject> prototype =
Ben Murdoch257744e2011-11-30 15:57:28 +00001604 factory()->NewJSObject(isolate()->object_function(), TENURED);
Steve Block6ded16b2010-05-10 14:33:55 +01001605 SetPrototype(opaque_reference_fun, prototype);
1606 global_context()->set_opaque_reference_function(*opaque_reference_fun);
1607 }
1608
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001609 { // --- I n t e r n a l A r r a y ---
1610 // An array constructor on the builtins object that works like
1611 // the public Array constructor, except that its prototype
1612 // doesn't inherit from Object.prototype.
1613 // To be used only for internal work by builtins. Instances
1614 // must not be leaked to user code.
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001615 Handle<JSFunction> array_function =
1616 InstallFunction(builtins,
1617 "InternalArray",
1618 JS_ARRAY_TYPE,
1619 JSArray::kSize,
Ben Murdoch257744e2011-11-30 15:57:28 +00001620 isolate()->initial_object_prototype(),
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001621 Builtins::kInternalArrayCode,
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001622 true);
1623 Handle<JSObject> prototype =
Ben Murdoch257744e2011-11-30 15:57:28 +00001624 factory()->NewJSObject(isolate()->object_function(), TENURED);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001625 SetPrototype(array_function, prototype);
1626
1627 array_function->shared()->set_construct_stub(
Ben Murdoch257744e2011-11-30 15:57:28 +00001628 isolate()->builtins()->builtin(Builtins::kArrayConstructCode));
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001629 array_function->shared()->DontAdaptArguments();
1630
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001631 // InternalArrays should not use Smi-Only array optimizations. There are too
1632 // many places in the C++ runtime code (e.g. RegEx) that assume that
1633 // elements in InternalArrays can be set to non-Smi values without going
1634 // through a common bottleneck that would make the SMI_ONLY -> FAST_ELEMENT
1635 // transition easy to trap. Moreover, they rarely are smi-only.
1636 MaybeObject* maybe_map =
1637 array_function->initial_map()->CopyDropTransitions();
1638 Map* new_map;
1639 if (!maybe_map->To<Map>(&new_map)) return false;
1640 new_map->set_elements_kind(FAST_ELEMENTS);
1641 array_function->set_initial_map(new_map);
1642
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001643 // Make "length" magic on instances.
1644 Handle<DescriptorArray> array_descriptors =
Ben Murdoch257744e2011-11-30 15:57:28 +00001645 factory()->CopyAppendForeignDescriptor(
1646 factory()->empty_descriptor_array(),
1647 factory()->length_symbol(),
1648 factory()->NewForeign(&Accessors::ArrayLength),
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001649 static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE));
1650
1651 array_function->initial_map()->set_instance_descriptors(
1652 *array_descriptors);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001653
1654 global_context()->set_internal_array_function(*array_function);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001655 }
1656
Steve Block6ded16b2010-05-10 14:33:55 +01001657 if (FLAG_disable_native_files) {
1658 PrintF("Warning: Running without installed natives!\n");
1659 return true;
1660 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001661
Andrei Popescu31002712010-02-23 13:46:05 +00001662 // Install natives.
1663 for (int i = Natives::GetDebuggerCount();
1664 i < Natives::GetBuiltinsCount();
1665 i++) {
Ben Murdoch257744e2011-11-30 15:57:28 +00001666 if (!CompileBuiltin(isolate(), i)) return false;
Andrei Popescu402d9372010-02-26 13:31:12 +00001667 // TODO(ager): We really only need to install the JS builtin
1668 // functions on the builtins object after compiling and running
1669 // runtime.js.
1670 if (!InstallJSBuiltins(builtins)) return false;
Steve Blocka7e24c12009-10-30 11:49:00 +00001671 }
1672
1673 InstallNativeFunctions();
1674
Iain Merrick75681382010-08-19 15:07:18 +01001675 // Store the map for the string prototype after the natives has been compiled
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001676 // and the String function has been set up.
Iain Merrick75681382010-08-19 15:07:18 +01001677 Handle<JSFunction> string_function(global_context()->string_function());
1678 ASSERT(JSObject::cast(
1679 string_function->initial_map()->prototype())->HasFastProperties());
1680 global_context()->set_string_function_prototype_map(
1681 HeapObject::cast(string_function->initial_map()->prototype())->map());
1682
Steve Blocka7e24c12009-10-30 11:49:00 +00001683 // Install Function.prototype.call and apply.
Ben Murdoch257744e2011-11-30 15:57:28 +00001684 { Handle<String> key = factory()->function_class_symbol();
Steve Blocka7e24c12009-10-30 11:49:00 +00001685 Handle<JSFunction> function =
Ben Murdoch257744e2011-11-30 15:57:28 +00001686 Handle<JSFunction>::cast(GetProperty(isolate()->global(), key));
Steve Blocka7e24c12009-10-30 11:49:00 +00001687 Handle<JSObject> proto =
1688 Handle<JSObject>(JSObject::cast(function->instance_prototype()));
1689
1690 // Install the call and the apply functions.
1691 Handle<JSFunction> call =
1692 InstallFunction(proto, "call", JS_OBJECT_TYPE, JSObject::kHeaderSize,
Steve Block6ded16b2010-05-10 14:33:55 +01001693 Handle<JSObject>::null(),
Steve Block44f0eee2011-05-26 01:26:41 +01001694 Builtins::kFunctionCall,
Steve Blocka7e24c12009-10-30 11:49:00 +00001695 false);
1696 Handle<JSFunction> apply =
1697 InstallFunction(proto, "apply", JS_OBJECT_TYPE, JSObject::kHeaderSize,
Steve Block6ded16b2010-05-10 14:33:55 +01001698 Handle<JSObject>::null(),
Steve Block44f0eee2011-05-26 01:26:41 +01001699 Builtins::kFunctionApply,
Steve Blocka7e24c12009-10-30 11:49:00 +00001700 false);
1701
1702 // Make sure that Function.prototype.call appears to be compiled.
1703 // The code will never be called, but inline caching for call will
1704 // only work if it appears to be compiled.
1705 call->shared()->DontAdaptArguments();
1706 ASSERT(call->is_compiled());
1707
1708 // Set the expected parameters for apply to 2; required by builtin.
1709 apply->shared()->set_formal_parameter_count(2);
1710
1711 // Set the lengths for the functions to satisfy ECMA-262.
1712 call->shared()->set_length(1);
1713 apply->shared()->set_length(2);
1714 }
1715
Ben Murdoch42effa52011-08-19 16:40:31 +01001716 InstallBuiltinFunctionIds();
1717
Steve Block6ded16b2010-05-10 14:33:55 +01001718 // Create a constructor for RegExp results (a variant of Array that
1719 // predefines the two properties index and match).
1720 {
1721 // RegExpResult initial map.
1722
1723 // Find global.Array.prototype to inherit from.
1724 Handle<JSFunction> array_constructor(global_context()->array_function());
1725 Handle<JSObject> array_prototype(
1726 JSObject::cast(array_constructor->instance_prototype()));
1727
1728 // Add initial map.
1729 Handle<Map> initial_map =
Ben Murdoch257744e2011-11-30 15:57:28 +00001730 factory()->NewMap(JS_ARRAY_TYPE, JSRegExpResult::kSize);
Steve Block6ded16b2010-05-10 14:33:55 +01001731 initial_map->set_constructor(*array_constructor);
1732
1733 // Set prototype on map.
1734 initial_map->set_non_instance_prototype(false);
1735 initial_map->set_prototype(*array_prototype);
1736
1737 // Update map with length accessor from Array and add "index" and "input".
Steve Block6ded16b2010-05-10 14:33:55 +01001738 Handle<DescriptorArray> reresult_descriptors =
Ben Murdoch257744e2011-11-30 15:57:28 +00001739 factory()->NewDescriptorArray(3);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001740 DescriptorArray::WhitenessWitness witness(*reresult_descriptors);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001741
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001742 JSFunction* array_function = global_context()->array_function();
1743 Handle<DescriptorArray> array_descriptors(
1744 array_function->initial_map()->instance_descriptors());
1745 int index = array_descriptors->SearchWithCache(heap()->length_symbol());
1746 MaybeObject* copy_result =
1747 reresult_descriptors->CopyFrom(0, *array_descriptors, index, witness);
1748 if (copy_result->IsFailure()) return false;
Steve Block6ded16b2010-05-10 14:33:55 +01001749
1750 int enum_index = 0;
1751 {
Ben Murdoch257744e2011-11-30 15:57:28 +00001752 FieldDescriptor index_field(heap()->index_symbol(),
Steve Block6ded16b2010-05-10 14:33:55 +01001753 JSRegExpResult::kIndexIndex,
1754 NONE,
1755 enum_index++);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001756 reresult_descriptors->Set(1, &index_field, witness);
Steve Block6ded16b2010-05-10 14:33:55 +01001757 }
1758
1759 {
Ben Murdoch257744e2011-11-30 15:57:28 +00001760 FieldDescriptor input_field(heap()->input_symbol(),
Steve Block6ded16b2010-05-10 14:33:55 +01001761 JSRegExpResult::kInputIndex,
1762 NONE,
1763 enum_index++);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001764 reresult_descriptors->Set(2, &input_field, witness);
Steve Block6ded16b2010-05-10 14:33:55 +01001765 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001766 reresult_descriptors->Sort(witness);
Steve Block6ded16b2010-05-10 14:33:55 +01001767
1768 initial_map->set_inobject_properties(2);
1769 initial_map->set_pre_allocated_property_fields(2);
1770 initial_map->set_unused_property_fields(0);
1771 initial_map->set_instance_descriptors(*reresult_descriptors);
1772
1773 global_context()->set_regexp_result_map(*initial_map);
1774 }
1775
Steve Blocka7e24c12009-10-30 11:49:00 +00001776#ifdef DEBUG
1777 builtins->Verify();
1778#endif
Andrei Popescu31002712010-02-23 13:46:05 +00001779
Steve Blocka7e24c12009-10-30 11:49:00 +00001780 return true;
1781}
1782
1783
Ben Murdoch257744e2011-11-30 15:57:28 +00001784bool Genesis::InstallExperimentalNatives() {
1785 for (int i = ExperimentalNatives::GetDebuggerCount();
1786 i < ExperimentalNatives::GetBuiltinsCount();
1787 i++) {
1788 if (FLAG_harmony_proxies &&
1789 strcmp(ExperimentalNatives::GetScriptName(i).start(),
1790 "native proxy.js") == 0) {
1791 if (!CompileExperimentalBuiltin(isolate(), i)) return false;
1792 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001793 if (FLAG_harmony_collections &&
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001794 strcmp(ExperimentalNatives::GetScriptName(i).start(),
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001795 "native collection.js") == 0) {
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001796 if (!CompileExperimentalBuiltin(isolate(), i)) return false;
1797 }
Ben Murdoch257744e2011-11-30 15:57:28 +00001798 }
1799
1800 InstallExperimentalNativeFunctions();
1801
1802 return true;
1803}
1804
1805
Ben Murdochb0fe1622011-05-05 13:52:32 +01001806static Handle<JSObject> ResolveBuiltinIdHolder(
Kristian Monsen0d5e1162010-09-30 15:31:59 +01001807 Handle<Context> global_context,
1808 const char* holder_expr) {
Ben Murdoch257744e2011-11-30 15:57:28 +00001809 Factory* factory = global_context->GetIsolate()->factory();
Kristian Monsen0d5e1162010-09-30 15:31:59 +01001810 Handle<GlobalObject> global(global_context->global());
1811 const char* period_pos = strchr(holder_expr, '.');
1812 if (period_pos == NULL) {
1813 return Handle<JSObject>::cast(
Steve Block44f0eee2011-05-26 01:26:41 +01001814 GetProperty(global, factory->LookupAsciiSymbol(holder_expr)));
Steve Block59151502010-09-22 15:07:15 +01001815 }
Kristian Monsen0d5e1162010-09-30 15:31:59 +01001816 ASSERT_EQ(".prototype", period_pos);
1817 Vector<const char> property(holder_expr,
1818 static_cast<int>(period_pos - holder_expr));
1819 Handle<JSFunction> function = Handle<JSFunction>::cast(
Steve Block44f0eee2011-05-26 01:26:41 +01001820 GetProperty(global, factory->LookupSymbol(property)));
Kristian Monsen0d5e1162010-09-30 15:31:59 +01001821 return Handle<JSObject>(JSObject::cast(function->prototype()));
1822}
1823
1824
Ben Murdochb0fe1622011-05-05 13:52:32 +01001825static void InstallBuiltinFunctionId(Handle<JSObject> holder,
1826 const char* function_name,
1827 BuiltinFunctionId id) {
Ben Murdoch257744e2011-11-30 15:57:28 +00001828 Factory* factory = holder->GetIsolate()->factory();
1829 Handle<String> name = factory->LookupAsciiSymbol(function_name);
John Reck59135872010-11-02 12:39:01 -07001830 Object* function_object = holder->GetProperty(*name)->ToObjectUnchecked();
1831 Handle<JSFunction> function(JSFunction::cast(function_object));
Kristian Monsen25f61362010-05-21 11:50:48 +01001832 function->shared()->set_function_data(Smi::FromInt(id));
1833}
1834
1835
Ben Murdochb0fe1622011-05-05 13:52:32 +01001836void Genesis::InstallBuiltinFunctionIds() {
Kristian Monsen25f61362010-05-21 11:50:48 +01001837 HandleScope scope;
Ben Murdochb0fe1622011-05-05 13:52:32 +01001838#define INSTALL_BUILTIN_ID(holder_expr, fun_name, name) \
1839 { \
1840 Handle<JSObject> holder = ResolveBuiltinIdHolder( \
1841 global_context(), #holder_expr); \
1842 BuiltinFunctionId id = k##name; \
1843 InstallBuiltinFunctionId(holder, #fun_name, id); \
Kristian Monsen25f61362010-05-21 11:50:48 +01001844 }
Ben Murdochb0fe1622011-05-05 13:52:32 +01001845 FUNCTIONS_WITH_ID_LIST(INSTALL_BUILTIN_ID)
1846#undef INSTALL_BUILTIN_ID
Kristian Monsen25f61362010-05-21 11:50:48 +01001847}
1848
1849
Steve Block6ded16b2010-05-10 14:33:55 +01001850// Do not forget to update macros.py with named constant
1851// of cache id.
1852#define JSFUNCTION_RESULT_CACHE_LIST(F) \
1853 F(16, global_context()->regexp_function())
1854
1855
Ben Murdoch257744e2011-11-30 15:57:28 +00001856static FixedArray* CreateCache(int size, Handle<JSFunction> factory_function) {
1857 Factory* factory = factory_function->GetIsolate()->factory();
Steve Block6ded16b2010-05-10 14:33:55 +01001858 // Caches are supposed to live for a long time, allocate in old space.
1859 int array_size = JSFunctionResultCache::kEntriesIndex + 2 * size;
1860 // Cannot use cast as object is not fully initialized yet.
1861 JSFunctionResultCache* cache = reinterpret_cast<JSFunctionResultCache*>(
Ben Murdoch257744e2011-11-30 15:57:28 +00001862 *factory->NewFixedArrayWithHoles(array_size, TENURED));
1863 cache->set(JSFunctionResultCache::kFactoryIndex, *factory_function);
Steve Block6ded16b2010-05-10 14:33:55 +01001864 cache->MakeZeroSize();
1865 return cache;
1866}
1867
1868
1869void Genesis::InstallJSFunctionResultCaches() {
1870 const int kNumberOfCaches = 0 +
1871#define F(size, func) + 1
1872 JSFUNCTION_RESULT_CACHE_LIST(F)
1873#undef F
1874 ;
1875
Steve Block44f0eee2011-05-26 01:26:41 +01001876 Handle<FixedArray> caches = FACTORY->NewFixedArray(kNumberOfCaches, TENURED);
Steve Block6ded16b2010-05-10 14:33:55 +01001877
1878 int index = 0;
Kristian Monsen0d5e1162010-09-30 15:31:59 +01001879
Ben Murdoch257744e2011-11-30 15:57:28 +00001880#define F(size, func) do { \
1881 FixedArray* cache = CreateCache((size), Handle<JSFunction>(func)); \
1882 caches->set(index++, cache); \
Kristian Monsen0d5e1162010-09-30 15:31:59 +01001883 } while (false)
1884
1885 JSFUNCTION_RESULT_CACHE_LIST(F);
1886
Steve Block6ded16b2010-05-10 14:33:55 +01001887#undef F
1888
1889 global_context()->set_jsfunction_result_caches(*caches);
1890}
1891
1892
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001893void Genesis::InitializeNormalizedMapCaches() {
1894 Handle<FixedArray> array(
Steve Block44f0eee2011-05-26 01:26:41 +01001895 FACTORY->NewFixedArray(NormalizedMapCache::kEntries, TENURED));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001896 global_context()->set_normalized_map_cache(NormalizedMapCache::cast(*array));
1897}
1898
1899
Andrei Popescu31002712010-02-23 13:46:05 +00001900bool Bootstrapper::InstallExtensions(Handle<Context> global_context,
1901 v8::ExtensionConfiguration* extensions) {
Ben Murdoch257744e2011-11-30 15:57:28 +00001902 Isolate* isolate = global_context->GetIsolate();
Andrei Popescu31002712010-02-23 13:46:05 +00001903 BootstrapperActive active;
Steve Block44f0eee2011-05-26 01:26:41 +01001904 SaveContext saved_context(isolate);
1905 isolate->set_context(*global_context);
Andrei Popescu31002712010-02-23 13:46:05 +00001906 if (!Genesis::InstallExtensions(global_context, extensions)) return false;
1907 Genesis::InstallSpecialObjects(global_context);
1908 return true;
1909}
1910
1911
1912void Genesis::InstallSpecialObjects(Handle<Context> global_context) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001913 Isolate* isolate = global_context->GetIsolate();
1914 Factory* factory = isolate->factory();
Steve Blocka7e24c12009-10-30 11:49:00 +00001915 HandleScope scope;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001916 Handle<JSGlobalObject> global(JSGlobalObject::cast(global_context->global()));
Steve Blocka7e24c12009-10-30 11:49:00 +00001917 // Expose the natives in global if a name for it is specified.
1918 if (FLAG_expose_natives_as != NULL && strlen(FLAG_expose_natives_as) != 0) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001919 Handle<String> natives = factory->LookupAsciiSymbol(FLAG_expose_natives_as);
1920 CHECK_NOT_EMPTY_HANDLE(isolate,
1921 JSObject::SetLocalPropertyIgnoreAttributes(
1922 global, natives,
1923 Handle<JSObject>(global->builtins()),
1924 DONT_ENUM));
Steve Blocka7e24c12009-10-30 11:49:00 +00001925 }
1926
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001927 Handle<Object> Error = GetProperty(global, "Error");
Steve Blocka7e24c12009-10-30 11:49:00 +00001928 if (Error->IsJSObject()) {
Steve Block44f0eee2011-05-26 01:26:41 +01001929 Handle<String> name = factory->LookupAsciiSymbol("stackTraceLimit");
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001930 Handle<Smi> stack_trace_limit(Smi::FromInt(FLAG_stack_trace_limit));
1931 CHECK_NOT_EMPTY_HANDLE(isolate,
1932 JSObject::SetLocalPropertyIgnoreAttributes(
1933 Handle<JSObject>::cast(Error), name,
1934 stack_trace_limit, NONE));
Steve Blocka7e24c12009-10-30 11:49:00 +00001935 }
1936
1937#ifdef ENABLE_DEBUGGER_SUPPORT
1938 // Expose the debug global object in global if a name for it is specified.
1939 if (FLAG_expose_debug_as != NULL && strlen(FLAG_expose_debug_as) != 0) {
Steve Block44f0eee2011-05-26 01:26:41 +01001940 Debug* debug = Isolate::Current()->debug();
Steve Blocka7e24c12009-10-30 11:49:00 +00001941 // If loading fails we just bail out without installing the
1942 // debugger but without tanking the whole context.
Steve Block44f0eee2011-05-26 01:26:41 +01001943 if (!debug->Load()) return;
Steve Blocka7e24c12009-10-30 11:49:00 +00001944 // Set the security token for the debugger context to the same as
1945 // the shell global context to allow calling between these (otherwise
1946 // exposing debug global object doesn't make much sense).
Steve Block44f0eee2011-05-26 01:26:41 +01001947 debug->debug_context()->set_security_token(
Andrei Popescu31002712010-02-23 13:46:05 +00001948 global_context->security_token());
Steve Blocka7e24c12009-10-30 11:49:00 +00001949
1950 Handle<String> debug_string =
Steve Block44f0eee2011-05-26 01:26:41 +01001951 factory->LookupAsciiSymbol(FLAG_expose_debug_as);
1952 Handle<Object> global_proxy(debug->debug_context()->global_proxy());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001953 CHECK_NOT_EMPTY_HANDLE(isolate,
1954 JSObject::SetLocalPropertyIgnoreAttributes(
1955 global, debug_string, global_proxy, DONT_ENUM));
Steve Blocka7e24c12009-10-30 11:49:00 +00001956 }
1957#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00001958}
1959
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001960static uint32_t Hash(RegisteredExtension* extension) {
1961 return v8::internal::ComputePointerHash(extension);
1962}
1963
1964static bool MatchRegisteredExtensions(void* key1, void* key2) {
1965 return key1 == key2;
1966}
1967
1968Genesis::ExtensionStates::ExtensionStates()
1969 : map_(MatchRegisteredExtensions, 8) { }
1970
1971Genesis::ExtensionTraversalState Genesis::ExtensionStates::get_state(
1972 RegisteredExtension* extension) {
1973 i::HashMap::Entry* entry = map_.Lookup(extension, Hash(extension), false);
1974 if (entry == NULL) {
1975 return UNVISITED;
1976 }
1977 return static_cast<ExtensionTraversalState>(
1978 reinterpret_cast<intptr_t>(entry->value));
1979}
1980
1981void Genesis::ExtensionStates::set_state(RegisteredExtension* extension,
1982 ExtensionTraversalState state) {
1983 map_.Lookup(extension, Hash(extension), true)->value =
1984 reinterpret_cast<void*>(static_cast<intptr_t>(state));
1985}
Steve Blocka7e24c12009-10-30 11:49:00 +00001986
Andrei Popescu31002712010-02-23 13:46:05 +00001987bool Genesis::InstallExtensions(Handle<Context> global_context,
1988 v8::ExtensionConfiguration* extensions) {
Steve Block44f0eee2011-05-26 01:26:41 +01001989 // TODO(isolates): Extensions on multiple isolates may take a little more
1990 // effort. (The external API reads 'ignore'-- does that mean
1991 // we can break the interface?)
1992
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001993
1994 ExtensionStates extension_states; // All extensions have state UNVISITED.
1995 // Install auto extensions.
Steve Blocka7e24c12009-10-30 11:49:00 +00001996 v8::RegisteredExtension* current = v8::RegisteredExtension::first_extension();
1997 while (current != NULL) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001998 if (current->extension()->auto_enable())
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001999 InstallExtension(current, &extension_states);
Steve Blocka7e24c12009-10-30 11:49:00 +00002000 current = current->next();
2001 }
2002
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002003 if (FLAG_expose_gc) InstallExtension("v8/gc", &extension_states);
2004 if (FLAG_expose_externalize_string) {
2005 InstallExtension("v8/externalize", &extension_states);
2006 }
Steve Blocka7e24c12009-10-30 11:49:00 +00002007
2008 if (extensions == NULL) return true;
2009 // Install required extensions
2010 int count = v8::ImplementationUtilities::GetNameCount(extensions);
2011 const char** names = v8::ImplementationUtilities::GetNames(extensions);
2012 for (int i = 0; i < count; i++) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002013 if (!InstallExtension(names[i], &extension_states))
Steve Blocka7e24c12009-10-30 11:49:00 +00002014 return false;
2015 }
2016
2017 return true;
2018}
2019
2020
2021// Installs a named extension. This methods is unoptimized and does
2022// not scale well if we want to support a large number of extensions.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002023bool Genesis::InstallExtension(const char* name,
2024 ExtensionStates* extension_states) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002025 v8::RegisteredExtension* current = v8::RegisteredExtension::first_extension();
2026 // Loop until we find the relevant extension
2027 while (current != NULL) {
2028 if (strcmp(name, current->extension()->name()) == 0) break;
2029 current = current->next();
2030 }
2031 // Didn't find the extension; fail.
2032 if (current == NULL) {
2033 v8::Utils::ReportApiFailure(
2034 "v8::Context::New()", "Cannot find required extension");
2035 return false;
2036 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002037 return InstallExtension(current, extension_states);
Steve Blocka7e24c12009-10-30 11:49:00 +00002038}
2039
2040
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002041bool Genesis::InstallExtension(v8::RegisteredExtension* current,
2042 ExtensionStates* extension_states) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002043 HandleScope scope;
2044
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002045 if (extension_states->get_state(current) == INSTALLED) return true;
Steve Blocka7e24c12009-10-30 11:49:00 +00002046 // The current node has already been visited so there must be a
2047 // cycle in the dependency graph; fail.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002048 if (extension_states->get_state(current) == VISITED) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002049 v8::Utils::ReportApiFailure(
2050 "v8::Context::New()", "Circular extension dependency");
2051 return false;
2052 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002053 ASSERT(extension_states->get_state(current) == UNVISITED);
2054 extension_states->set_state(current, VISITED);
Steve Blocka7e24c12009-10-30 11:49:00 +00002055 v8::Extension* extension = current->extension();
2056 // Install the extension's dependencies
2057 for (int i = 0; i < extension->dependency_count(); i++) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002058 if (!InstallExtension(extension->dependencies()[i], extension_states))
2059 return false;
Steve Blocka7e24c12009-10-30 11:49:00 +00002060 }
Steve Block44f0eee2011-05-26 01:26:41 +01002061 Isolate* isolate = Isolate::Current();
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002062 Handle<String> source_code =
2063 isolate->factory()->NewExternalStringFromAscii(extension->source());
2064 bool result = CompileScriptCached(
2065 CStrVector(extension->name()),
2066 source_code,
2067 isolate->bootstrapper()->extensions_cache(),
2068 extension,
2069 Handle<Context>(isolate->context()),
2070 false);
Steve Block44f0eee2011-05-26 01:26:41 +01002071 ASSERT(isolate->has_pending_exception() != result);
Steve Blocka7e24c12009-10-30 11:49:00 +00002072 if (!result) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002073 // We print out the name of the extension that fail to install.
2074 // When an error is thrown during bootstrapping we automatically print
2075 // the line number at which this happened to the console in the isolate
2076 // error throwing functionality.
2077 OS::PrintError("Error installing extension '%s'.\n",
2078 current->extension()->name());
Steve Block44f0eee2011-05-26 01:26:41 +01002079 isolate->clear_pending_exception();
Steve Blocka7e24c12009-10-30 11:49:00 +00002080 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002081 extension_states->set_state(current, INSTALLED);
2082 isolate->NotifyExtensionInstalled();
Steve Blocka7e24c12009-10-30 11:49:00 +00002083 return result;
2084}
2085
2086
Andrei Popescu402d9372010-02-26 13:31:12 +00002087bool Genesis::InstallJSBuiltins(Handle<JSBuiltinsObject> builtins) {
2088 HandleScope scope;
Ben Murdoch257744e2011-11-30 15:57:28 +00002089 Factory* factory = builtins->GetIsolate()->factory();
Andrei Popescu402d9372010-02-26 13:31:12 +00002090 for (int i = 0; i < Builtins::NumberOfJavaScriptBuiltins(); i++) {
2091 Builtins::JavaScript id = static_cast<Builtins::JavaScript>(i);
Ben Murdoch257744e2011-11-30 15:57:28 +00002092 Handle<String> name = factory->LookupAsciiSymbol(Builtins::GetName(id));
John Reck59135872010-11-02 12:39:01 -07002093 Object* function_object = builtins->GetPropertyNoExceptionThrown(*name);
Andrei Popescu402d9372010-02-26 13:31:12 +00002094 Handle<JSFunction> function
John Reck59135872010-11-02 12:39:01 -07002095 = Handle<JSFunction>(JSFunction::cast(function_object));
Andrei Popescu402d9372010-02-26 13:31:12 +00002096 builtins->set_javascript_builtin(id, *function);
2097 Handle<SharedFunctionInfo> shared
2098 = Handle<SharedFunctionInfo>(function->shared());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002099 if (!SharedFunctionInfo::EnsureCompiled(shared, CLEAR_EXCEPTION)) {
2100 return false;
2101 }
Iain Merrick75681382010-08-19 15:07:18 +01002102 // Set the code object on the function object.
Ben Murdochb0fe1622011-05-05 13:52:32 +01002103 function->ReplaceCode(function->shared()->code());
Steve Block6ded16b2010-05-10 14:33:55 +01002104 builtins->set_javascript_builtin_code(id, shared->code());
Andrei Popescu402d9372010-02-26 13:31:12 +00002105 }
2106 return true;
Andrei Popescu31002712010-02-23 13:46:05 +00002107}
2108
2109
Steve Blocka7e24c12009-10-30 11:49:00 +00002110bool Genesis::ConfigureGlobalObjects(
2111 v8::Handle<v8::ObjectTemplate> global_proxy_template) {
2112 Handle<JSObject> global_proxy(
2113 JSObject::cast(global_context()->global_proxy()));
Andrei Popescu402d9372010-02-26 13:31:12 +00002114 Handle<JSObject> inner_global(JSObject::cast(global_context()->global()));
Steve Blocka7e24c12009-10-30 11:49:00 +00002115
2116 if (!global_proxy_template.IsEmpty()) {
2117 // Configure the global proxy object.
2118 Handle<ObjectTemplateInfo> proxy_data =
2119 v8::Utils::OpenHandle(*global_proxy_template);
2120 if (!ConfigureApiObject(global_proxy, proxy_data)) return false;
2121
2122 // Configure the inner global object.
2123 Handle<FunctionTemplateInfo> proxy_constructor(
2124 FunctionTemplateInfo::cast(proxy_data->constructor()));
2125 if (!proxy_constructor->prototype_template()->IsUndefined()) {
2126 Handle<ObjectTemplateInfo> inner_data(
2127 ObjectTemplateInfo::cast(proxy_constructor->prototype_template()));
Andrei Popescu402d9372010-02-26 13:31:12 +00002128 if (!ConfigureApiObject(inner_global, inner_data)) return false;
Steve Blocka7e24c12009-10-30 11:49:00 +00002129 }
2130 }
2131
Andrei Popescu402d9372010-02-26 13:31:12 +00002132 SetObjectPrototype(global_proxy, inner_global);
Steve Blocka7e24c12009-10-30 11:49:00 +00002133 return true;
2134}
2135
2136
2137bool Genesis::ConfigureApiObject(Handle<JSObject> object,
2138 Handle<ObjectTemplateInfo> object_template) {
2139 ASSERT(!object_template.is_null());
2140 ASSERT(object->IsInstanceOf(
2141 FunctionTemplateInfo::cast(object_template->constructor())));
2142
2143 bool pending_exception = false;
2144 Handle<JSObject> obj =
2145 Execution::InstantiateObject(object_template, &pending_exception);
2146 if (pending_exception) {
Ben Murdoch257744e2011-11-30 15:57:28 +00002147 ASSERT(isolate()->has_pending_exception());
2148 isolate()->clear_pending_exception();
Steve Blocka7e24c12009-10-30 11:49:00 +00002149 return false;
2150 }
2151 TransferObject(obj, object);
2152 return true;
2153}
2154
2155
2156void Genesis::TransferNamedProperties(Handle<JSObject> from,
2157 Handle<JSObject> to) {
2158 if (from->HasFastProperties()) {
2159 Handle<DescriptorArray> descs =
2160 Handle<DescriptorArray>(from->map()->instance_descriptors());
2161 for (int i = 0; i < descs->number_of_descriptors(); i++) {
2162 PropertyDetails details = PropertyDetails(descs->GetDetails(i));
2163 switch (details.type()) {
2164 case FIELD: {
2165 HandleScope inner;
2166 Handle<String> key = Handle<String>(descs->GetKey(i));
2167 int index = descs->GetFieldIndex(i);
2168 Handle<Object> value = Handle<Object>(from->FastPropertyAt(index));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002169 CHECK_NOT_EMPTY_HANDLE(to->GetIsolate(),
2170 JSObject::SetLocalPropertyIgnoreAttributes(
2171 to, key, value, details.attributes()));
Steve Blocka7e24c12009-10-30 11:49:00 +00002172 break;
2173 }
2174 case CONSTANT_FUNCTION: {
2175 HandleScope inner;
2176 Handle<String> key = Handle<String>(descs->GetKey(i));
2177 Handle<JSFunction> fun =
2178 Handle<JSFunction>(descs->GetConstantFunction(i));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002179 CHECK_NOT_EMPTY_HANDLE(to->GetIsolate(),
2180 JSObject::SetLocalPropertyIgnoreAttributes(
2181 to, key, fun, details.attributes()));
Steve Blocka7e24c12009-10-30 11:49:00 +00002182 break;
2183 }
2184 case CALLBACKS: {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002185 LookupResult result(isolate());
Steve Blocka7e24c12009-10-30 11:49:00 +00002186 to->LocalLookup(descs->GetKey(i), &result);
2187 // If the property is already there we skip it
Andrei Popescu402d9372010-02-26 13:31:12 +00002188 if (result.IsProperty()) continue;
Steve Blocka7e24c12009-10-30 11:49:00 +00002189 HandleScope inner;
Andrei Popescu31002712010-02-23 13:46:05 +00002190 ASSERT(!to->HasFastProperties());
2191 // Add to dictionary.
Steve Blocka7e24c12009-10-30 11:49:00 +00002192 Handle<String> key = Handle<String>(descs->GetKey(i));
Andrei Popescu31002712010-02-23 13:46:05 +00002193 Handle<Object> callbacks(descs->GetCallbacksObject(i));
2194 PropertyDetails d =
2195 PropertyDetails(details.attributes(), CALLBACKS, details.index());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002196 JSObject::SetNormalizedProperty(to, key, callbacks, d);
Steve Blocka7e24c12009-10-30 11:49:00 +00002197 break;
2198 }
2199 case MAP_TRANSITION:
Ben Murdoch589d6972011-11-30 16:04:58 +00002200 case ELEMENTS_TRANSITION:
Steve Blocka7e24c12009-10-30 11:49:00 +00002201 case CONSTANT_TRANSITION:
2202 case NULL_DESCRIPTOR:
2203 // Ignore non-properties.
2204 break;
2205 case NORMAL:
2206 // Do not occur since the from object has fast properties.
Ben Murdoch257744e2011-11-30 15:57:28 +00002207 case HANDLER:
Steve Blocka7e24c12009-10-30 11:49:00 +00002208 case INTERCEPTOR:
Ben Murdoch257744e2011-11-30 15:57:28 +00002209 // No element in instance descriptors have proxy or interceptor type.
Steve Blocka7e24c12009-10-30 11:49:00 +00002210 UNREACHABLE();
2211 break;
2212 }
2213 }
2214 } else {
2215 Handle<StringDictionary> properties =
2216 Handle<StringDictionary>(from->property_dictionary());
2217 int capacity = properties->Capacity();
2218 for (int i = 0; i < capacity; i++) {
2219 Object* raw_key(properties->KeyAt(i));
2220 if (properties->IsKey(raw_key)) {
2221 ASSERT(raw_key->IsString());
2222 // If the property is already there we skip it.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002223 LookupResult result(isolate());
Steve Blocka7e24c12009-10-30 11:49:00 +00002224 to->LocalLookup(String::cast(raw_key), &result);
Andrei Popescu402d9372010-02-26 13:31:12 +00002225 if (result.IsProperty()) continue;
Steve Blocka7e24c12009-10-30 11:49:00 +00002226 // Set the property.
2227 Handle<String> key = Handle<String>(String::cast(raw_key));
2228 Handle<Object> value = Handle<Object>(properties->ValueAt(i));
2229 if (value->IsJSGlobalPropertyCell()) {
2230 value = Handle<Object>(JSGlobalPropertyCell::cast(*value)->value());
2231 }
2232 PropertyDetails details = properties->DetailsAt(i);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002233 CHECK_NOT_EMPTY_HANDLE(to->GetIsolate(),
2234 JSObject::SetLocalPropertyIgnoreAttributes(
2235 to, key, value, details.attributes()));
Steve Blocka7e24c12009-10-30 11:49:00 +00002236 }
2237 }
2238 }
2239}
2240
2241
2242void Genesis::TransferIndexedProperties(Handle<JSObject> from,
2243 Handle<JSObject> to) {
2244 // Cloning the elements array is sufficient.
2245 Handle<FixedArray> from_elements =
2246 Handle<FixedArray>(FixedArray::cast(from->elements()));
Steve Block44f0eee2011-05-26 01:26:41 +01002247 Handle<FixedArray> to_elements = FACTORY->CopyFixedArray(from_elements);
Steve Blocka7e24c12009-10-30 11:49:00 +00002248 to->set_elements(*to_elements);
2249}
2250
2251
2252void Genesis::TransferObject(Handle<JSObject> from, Handle<JSObject> to) {
2253 HandleScope outer;
Ben Murdoch257744e2011-11-30 15:57:28 +00002254 Factory* factory = from->GetIsolate()->factory();
Steve Blocka7e24c12009-10-30 11:49:00 +00002255
2256 ASSERT(!from->IsJSArray());
2257 ASSERT(!to->IsJSArray());
2258
2259 TransferNamedProperties(from, to);
2260 TransferIndexedProperties(from, to);
2261
2262 // Transfer the prototype (new map is needed).
2263 Handle<Map> old_to_map = Handle<Map>(to->map());
Ben Murdoch257744e2011-11-30 15:57:28 +00002264 Handle<Map> new_to_map = factory->CopyMapDropTransitions(old_to_map);
Steve Blocka7e24c12009-10-30 11:49:00 +00002265 new_to_map->set_prototype(from->map()->prototype());
2266 to->set_map(*new_to_map);
2267}
2268
2269
2270void Genesis::MakeFunctionInstancePrototypeWritable() {
Steve Block44f0eee2011-05-26 01:26:41 +01002271 // The maps with writable prototype are created in CreateEmptyFunction
2272 // and CreateStrictModeFunctionMaps respectively. Initially the maps are
2273 // created with read-only prototype for JS builtins processing.
2274 ASSERT(!function_instance_map_writable_prototype_.is_null());
2275 ASSERT(!strict_mode_function_instance_map_writable_prototype_.is_null());
Steve Blocka7e24c12009-10-30 11:49:00 +00002276
Steve Block44f0eee2011-05-26 01:26:41 +01002277 // Replace function instance maps to make prototype writable.
2278 global_context()->set_function_map(
2279 *function_instance_map_writable_prototype_);
2280 global_context()->set_strict_mode_function_map(
2281 *strict_mode_function_instance_map_writable_prototype_);
Steve Blocka7e24c12009-10-30 11:49:00 +00002282}
2283
2284
Ben Murdoch257744e2011-11-30 15:57:28 +00002285Genesis::Genesis(Isolate* isolate,
2286 Handle<Object> global_object,
Steve Blocka7e24c12009-10-30 11:49:00 +00002287 v8::Handle<v8::ObjectTemplate> global_template,
Ben Murdoch257744e2011-11-30 15:57:28 +00002288 v8::ExtensionConfiguration* extensions) : isolate_(isolate) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002289 result_ = Handle<Context>::null();
Steve Blocka7e24c12009-10-30 11:49:00 +00002290 // If V8 isn't running and cannot be initialized, just return.
2291 if (!V8::IsRunning() && !V8::Initialize(NULL)) return;
2292
2293 // Before creating the roots we must save the context and restore it
2294 // on all function exits.
2295 HandleScope scope;
Steve Block44f0eee2011-05-26 01:26:41 +01002296 SaveContext saved_context(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00002297
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002298 // During genesis, the boilerplate for stack overflow won't work until the
2299 // environment has been at least partially initialized. Add a stack check
2300 // before entering JS code to catch overflow early.
2301 StackLimitCheck check(Isolate::Current());
2302 if (check.HasOverflowed()) return;
2303
Andrei Popescu31002712010-02-23 13:46:05 +00002304 Handle<Context> new_context = Snapshot::NewContextFromSnapshot();
2305 if (!new_context.is_null()) {
2306 global_context_ =
Steve Block44f0eee2011-05-26 01:26:41 +01002307 Handle<Context>::cast(isolate->global_handles()->Create(*new_context));
Ben Murdochb0fe1622011-05-05 13:52:32 +01002308 AddToWeakGlobalContextList(*global_context_);
Steve Block44f0eee2011-05-26 01:26:41 +01002309 isolate->set_context(*global_context_);
2310 isolate->counters()->contexts_created_by_snapshot()->Increment();
Andrei Popescu402d9372010-02-26 13:31:12 +00002311 Handle<GlobalObject> inner_global;
Andrei Popescu31002712010-02-23 13:46:05 +00002312 Handle<JSGlobalProxy> global_proxy =
2313 CreateNewGlobals(global_template,
2314 global_object,
Andrei Popescu402d9372010-02-26 13:31:12 +00002315 &inner_global);
2316
Andrei Popescu31002712010-02-23 13:46:05 +00002317 HookUpGlobalProxy(inner_global, global_proxy);
Andrei Popescu402d9372010-02-26 13:31:12 +00002318 HookUpInnerGlobal(inner_global);
2319
Andrei Popescu31002712010-02-23 13:46:05 +00002320 if (!ConfigureGlobalObjects(global_template)) return;
2321 } else {
2322 // We get here if there was no context snapshot.
2323 CreateRoots();
Ben Murdoch257744e2011-11-30 15:57:28 +00002324 Handle<JSFunction> empty_function = CreateEmptyFunction(isolate);
Steve Block44f0eee2011-05-26 01:26:41 +01002325 CreateStrictModeFunctionMaps(empty_function);
Andrei Popescu31002712010-02-23 13:46:05 +00002326 Handle<GlobalObject> inner_global;
2327 Handle<JSGlobalProxy> global_proxy =
2328 CreateNewGlobals(global_template, global_object, &inner_global);
2329 HookUpGlobalProxy(inner_global, global_proxy);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002330 if (!InitializeGlobal(inner_global, empty_function)) return;
Steve Block6ded16b2010-05-10 14:33:55 +01002331 InstallJSFunctionResultCaches();
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002332 InitializeNormalizedMapCaches();
Kristian Monsen25f61362010-05-21 11:50:48 +01002333 if (!InstallNatives()) return;
Steve Blocka7e24c12009-10-30 11:49:00 +00002334
Andrei Popescu31002712010-02-23 13:46:05 +00002335 MakeFunctionInstancePrototypeWritable();
Steve Blocka7e24c12009-10-30 11:49:00 +00002336
Andrei Popescu31002712010-02-23 13:46:05 +00002337 if (!ConfigureGlobalObjects(global_template)) return;
Steve Block44f0eee2011-05-26 01:26:41 +01002338 isolate->counters()->contexts_created_from_scratch()->Increment();
Andrei Popescu31002712010-02-23 13:46:05 +00002339 }
Steve Blocka7e24c12009-10-30 11:49:00 +00002340
Ben Murdoch69a99ed2011-11-30 16:03:39 +00002341 // Initialize experimental globals and install experimental natives.
2342 InitializeExperimentalGlobal();
Ben Murdoch257744e2011-11-30 15:57:28 +00002343 if (!InstallExperimentalNatives()) return;
2344
Steve Blocka7e24c12009-10-30 11:49:00 +00002345 result_ = global_context_;
2346}
2347
2348
2349// Support for thread preemption.
2350
2351// Reserve space for statics needing saving and restoring.
2352int Bootstrapper::ArchiveSpacePerThread() {
Steve Block44f0eee2011-05-26 01:26:41 +01002353 return sizeof(NestingCounterType);
Steve Blocka7e24c12009-10-30 11:49:00 +00002354}
2355
2356
2357// Archive statics that are thread local.
2358char* Bootstrapper::ArchiveState(char* to) {
Steve Block44f0eee2011-05-26 01:26:41 +01002359 *reinterpret_cast<NestingCounterType*>(to) = nesting_;
2360 nesting_ = 0;
2361 return to + sizeof(NestingCounterType);
Steve Blocka7e24c12009-10-30 11:49:00 +00002362}
2363
2364
2365// Restore statics that are thread local.
2366char* Bootstrapper::RestoreState(char* from) {
Steve Block44f0eee2011-05-26 01:26:41 +01002367 nesting_ = *reinterpret_cast<NestingCounterType*>(from);
2368 return from + sizeof(NestingCounterType);
Steve Blocka7e24c12009-10-30 11:49:00 +00002369}
2370
2371
2372// Called when the top-level V8 mutex is destroyed.
2373void Bootstrapper::FreeThreadResources() {
Steve Block44f0eee2011-05-26 01:26:41 +01002374 ASSERT(!IsActive());
Steve Blocka7e24c12009-10-30 11:49:00 +00002375}
2376
2377} } // namespace v8::internal