blob: 08007146baf549fc8ca0a4d3520614c3bd2b039d [file] [log] [blame]
Ben Murdoch8b112d22011-06-08 16:22:53 +01001// Copyright 2011 the V8 project authors. All rights reserved.
Steve Blocka7e24c12009-10-30 11:49:00 +00002// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28#include "v8.h"
29
30#include "accessors.h"
31#include "api.h"
32#include "bootstrapper.h"
33#include "compiler.h"
34#include "debug.h"
35#include "execution.h"
36#include "global-handles.h"
37#include "macro-assembler.h"
38#include "natives.h"
Iain Merrick75681382010-08-19 15:07:18 +010039#include "objects-visiting.h"
Steve Blockd0582a62009-12-15 09:54:21 +000040#include "snapshot.h"
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -080041#include "extensions/externalize-string-extension.h"
42#include "extensions/gc-extension.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000043
44namespace v8 {
45namespace internal {
46
Steve Blocka7e24c12009-10-30 11:49:00 +000047
Steve Block44f0eee2011-05-26 01:26:41 +010048NativesExternalStringResource::NativesExternalStringResource(
49 Bootstrapper* bootstrapper,
50 const char* source)
Steve Blockd0582a62009-12-15 09:54:21 +000051 : data_(source), length_(StrLength(source)) {
Steve Block44f0eee2011-05-26 01:26:41 +010052 if (bootstrapper->delete_these_non_arrays_on_tear_down_ == NULL) {
53 bootstrapper->delete_these_non_arrays_on_tear_down_ = new List<char*>(2);
Steve Blockd0582a62009-12-15 09:54:21 +000054 }
55 // The resources are small objects and we only make a fixed number of
56 // them, but let's clean them up on exit for neatness.
Steve Block44f0eee2011-05-26 01:26:41 +010057 bootstrapper->delete_these_non_arrays_on_tear_down_->
Steve Blockd0582a62009-12-15 09:54:21 +000058 Add(reinterpret_cast<char*>(this));
59}
Steve Blocka7e24c12009-10-30 11:49:00 +000060
61
Steve Block44f0eee2011-05-26 01:26:41 +010062Bootstrapper::Bootstrapper()
63 : nesting_(0),
64 extensions_cache_(Script::TYPE_EXTENSION),
65 delete_these_non_arrays_on_tear_down_(NULL),
66 delete_these_arrays_on_tear_down_(NULL) {
67}
68
69
Steve Blocka7e24c12009-10-30 11:49:00 +000070Handle<String> Bootstrapper::NativesSourceLookup(int index) {
71 ASSERT(0 <= index && index < Natives::GetBuiltinsCount());
Steve Block44f0eee2011-05-26 01:26:41 +010072 Isolate* isolate = Isolate::Current();
73 Factory* factory = isolate->factory();
74 Heap* heap = isolate->heap();
75 if (heap->natives_source_cache()->get(index)->IsUndefined()) {
Steve Blockd0582a62009-12-15 09:54:21 +000076 if (!Snapshot::IsEnabled() || FLAG_new_snapshot) {
77 // We can use external strings for the natives.
78 NativesExternalStringResource* resource =
Steve Block44f0eee2011-05-26 01:26:41 +010079 new NativesExternalStringResource(this,
Steve Blockd0582a62009-12-15 09:54:21 +000080 Natives::GetScriptSource(index).start());
81 Handle<String> source_code =
Steve Block44f0eee2011-05-26 01:26:41 +010082 factory->NewExternalStringFromAscii(resource);
83 heap->natives_source_cache()->set(index, *source_code);
Steve Blockd0582a62009-12-15 09:54:21 +000084 } else {
85 // Old snapshot code can't cope with external strings at all.
86 Handle<String> source_code =
Steve Block44f0eee2011-05-26 01:26:41 +010087 factory->NewStringFromAscii(Natives::GetScriptSource(index));
88 heap->natives_source_cache()->set(index, *source_code);
Steve Blockd0582a62009-12-15 09:54:21 +000089 }
Steve Blocka7e24c12009-10-30 11:49:00 +000090 }
Steve Block44f0eee2011-05-26 01:26:41 +010091 Handle<Object> cached_source(heap->natives_source_cache()->get(index));
Steve Blocka7e24c12009-10-30 11:49:00 +000092 return Handle<String>::cast(cached_source);
93}
94
95
Steve Blocka7e24c12009-10-30 11:49:00 +000096void Bootstrapper::Initialize(bool create_heap_objects) {
Steve Block44f0eee2011-05-26 01:26:41 +010097 extensions_cache_.Initialize(create_heap_objects);
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -080098 GCExtension::Register();
99 ExternalizeStringExtension::Register();
Steve Blocka7e24c12009-10-30 11:49:00 +0000100}
101
102
Leon Clarkee46be812010-01-19 14:06:41 +0000103char* Bootstrapper::AllocateAutoDeletedArray(int bytes) {
104 char* memory = new char[bytes];
105 if (memory != NULL) {
Steve Block44f0eee2011-05-26 01:26:41 +0100106 if (delete_these_arrays_on_tear_down_ == NULL) {
107 delete_these_arrays_on_tear_down_ = new List<char*>(2);
Leon Clarkee46be812010-01-19 14:06:41 +0000108 }
Steve Block44f0eee2011-05-26 01:26:41 +0100109 delete_these_arrays_on_tear_down_->Add(memory);
Leon Clarkee46be812010-01-19 14:06:41 +0000110 }
111 return memory;
112}
113
114
Steve Blocka7e24c12009-10-30 11:49:00 +0000115void Bootstrapper::TearDown() {
Steve Block44f0eee2011-05-26 01:26:41 +0100116 if (delete_these_non_arrays_on_tear_down_ != NULL) {
117 int len = delete_these_non_arrays_on_tear_down_->length();
Steve Blockd0582a62009-12-15 09:54:21 +0000118 ASSERT(len < 20); // Don't use this mechanism for unbounded allocations.
119 for (int i = 0; i < len; i++) {
Steve Block44f0eee2011-05-26 01:26:41 +0100120 delete delete_these_non_arrays_on_tear_down_->at(i);
121 delete_these_non_arrays_on_tear_down_->at(i) = NULL;
Steve Blockd0582a62009-12-15 09:54:21 +0000122 }
Steve Block44f0eee2011-05-26 01:26:41 +0100123 delete delete_these_non_arrays_on_tear_down_;
124 delete_these_non_arrays_on_tear_down_ = NULL;
Steve Blockd0582a62009-12-15 09:54:21 +0000125 }
126
Steve Block44f0eee2011-05-26 01:26:41 +0100127 if (delete_these_arrays_on_tear_down_ != NULL) {
128 int len = delete_these_arrays_on_tear_down_->length();
Leon Clarkee46be812010-01-19 14:06:41 +0000129 ASSERT(len < 1000); // Don't use this mechanism for unbounded allocations.
130 for (int i = 0; i < len; i++) {
Steve Block44f0eee2011-05-26 01:26:41 +0100131 delete[] delete_these_arrays_on_tear_down_->at(i);
132 delete_these_arrays_on_tear_down_->at(i) = NULL;
Leon Clarkee46be812010-01-19 14:06:41 +0000133 }
Steve Block44f0eee2011-05-26 01:26:41 +0100134 delete delete_these_arrays_on_tear_down_;
135 delete_these_arrays_on_tear_down_ = NULL;
Leon Clarkee46be812010-01-19 14:06:41 +0000136 }
137
Steve Block44f0eee2011-05-26 01:26:41 +0100138 extensions_cache_.Initialize(false); // Yes, symmetrical
Steve Blocka7e24c12009-10-30 11:49:00 +0000139}
140
141
Steve Blocka7e24c12009-10-30 11:49:00 +0000142class Genesis BASE_EMBEDDED {
143 public:
Ben Murdoch8b112d22011-06-08 16:22:53 +0100144 Genesis(Isolate* isolate,
145 Handle<Object> global_object,
Steve Blocka7e24c12009-10-30 11:49:00 +0000146 v8::Handle<v8::ObjectTemplate> global_template,
147 v8::ExtensionConfiguration* extensions);
Andrei Popescu31002712010-02-23 13:46:05 +0000148 ~Genesis() { }
Steve Blocka7e24c12009-10-30 11:49:00 +0000149
150 Handle<Context> result() { return result_; }
151
152 Genesis* previous() { return previous_; }
Steve Blocka7e24c12009-10-30 11:49:00 +0000153
Ben Murdoch8b112d22011-06-08 16:22:53 +0100154 Isolate* isolate() const { return isolate_; }
155 Factory* factory() const { return isolate_->factory(); }
156 Heap* heap() const { return isolate_->heap(); }
157
Steve Blocka7e24c12009-10-30 11:49:00 +0000158 private:
159 Handle<Context> global_context_;
Ben Murdoch8b112d22011-06-08 16:22:53 +0100160 Isolate* isolate_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000161
162 // There may be more than one active genesis object: When GC is
163 // triggered during environment creation there may be weak handle
164 // processing callbacks which may create new environments.
165 Genesis* previous_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000166
167 Handle<Context> global_context() { return global_context_; }
168
Andrei Popescu31002712010-02-23 13:46:05 +0000169 // Creates some basic objects. Used for creating a context from scratch.
170 void CreateRoots();
171 // Creates the empty function. Used for creating a context from scratch.
Ben Murdoch8b112d22011-06-08 16:22:53 +0100172 Handle<JSFunction> CreateEmptyFunction(Isolate* isolate);
Steve Block44f0eee2011-05-26 01:26:41 +0100173 // Creates the ThrowTypeError function. ECMA 5th Ed. 13.2.3
174 Handle<JSFunction> CreateThrowTypeErrorFunction(Builtins::Name builtin);
175
176 void CreateStrictModeFunctionMaps(Handle<JSFunction> empty);
Andrei Popescu31002712010-02-23 13:46:05 +0000177 // Creates the global objects using the global and the template passed in
178 // through the API. We call this regardless of whether we are building a
179 // context from scratch or using a deserialized one from the partial snapshot
180 // but in the latter case we don't use the objects it produces directly, as
181 // we have to used the deserialized ones that are linked together with the
182 // rest of the context snapshot.
183 Handle<JSGlobalProxy> CreateNewGlobals(
184 v8::Handle<v8::ObjectTemplate> global_template,
185 Handle<Object> global_object,
186 Handle<GlobalObject>* global_proxy_out);
187 // Hooks the given global proxy into the context. If the context was created
188 // by deserialization then this will unhook the global proxy that was
189 // deserialized, leaving the GC to pick it up.
190 void HookUpGlobalProxy(Handle<GlobalObject> inner_global,
191 Handle<JSGlobalProxy> global_proxy);
Andrei Popescu402d9372010-02-26 13:31:12 +0000192 // Similarly, we want to use the inner global that has been created by the
193 // templates passed through the API. The inner global from the snapshot is
194 // detached from the other objects in the snapshot.
195 void HookUpInnerGlobal(Handle<GlobalObject> inner_global);
Andrei Popescu31002712010-02-23 13:46:05 +0000196 // New context initialization. Used for creating a context from scratch.
197 void InitializeGlobal(Handle<GlobalObject> inner_global,
198 Handle<JSFunction> empty_function);
199 // Installs the contents of the native .js files on the global objects.
200 // Used for creating a context from scratch.
Steve Blocka7e24c12009-10-30 11:49:00 +0000201 void InstallNativeFunctions();
202 bool InstallNatives();
Ben Murdoch8b112d22011-06-08 16:22:53 +0100203 bool InstallExperimentalNatives();
Ben Murdochb0fe1622011-05-05 13:52:32 +0100204 void InstallBuiltinFunctionIds();
Steve Block6ded16b2010-05-10 14:33:55 +0100205 void InstallJSFunctionResultCaches();
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100206 void InitializeNormalizedMapCaches();
Andrei Popescu31002712010-02-23 13:46:05 +0000207 // Used both for deserialized and from-scratch contexts to add the extensions
208 // provided.
209 static bool InstallExtensions(Handle<Context> global_context,
210 v8::ExtensionConfiguration* extensions);
211 static bool InstallExtension(const char* name);
212 static bool InstallExtension(v8::RegisteredExtension* current);
213 static void InstallSpecialObjects(Handle<Context> global_context);
Andrei Popescu402d9372010-02-26 13:31:12 +0000214 bool InstallJSBuiltins(Handle<JSBuiltinsObject> builtins);
Steve Blocka7e24c12009-10-30 11:49:00 +0000215 bool ConfigureApiObject(Handle<JSObject> object,
216 Handle<ObjectTemplateInfo> object_template);
217 bool ConfigureGlobalObjects(v8::Handle<v8::ObjectTemplate> global_template);
218
219 // Migrates all properties from the 'from' object to the 'to'
220 // object and overrides the prototype in 'to' with the one from
221 // 'from'.
222 void TransferObject(Handle<JSObject> from, Handle<JSObject> to);
223 void TransferNamedProperties(Handle<JSObject> from, Handle<JSObject> to);
224 void TransferIndexedProperties(Handle<JSObject> from, Handle<JSObject> to);
225
Steve Block6ded16b2010-05-10 14:33:55 +0100226 enum PrototypePropertyMode {
227 DONT_ADD_PROTOTYPE,
228 ADD_READONLY_PROTOTYPE,
229 ADD_WRITEABLE_PROTOTYPE
230 };
Steve Block44f0eee2011-05-26 01:26:41 +0100231
232 Handle<Map> CreateFunctionMap(PrototypePropertyMode prototype_mode);
233
Steve Blocka7e24c12009-10-30 11:49:00 +0000234 Handle<DescriptorArray> ComputeFunctionInstanceDescriptor(
Steve Block6ded16b2010-05-10 14:33:55 +0100235 PrototypePropertyMode prototypeMode);
Steve Blocka7e24c12009-10-30 11:49:00 +0000236 void MakeFunctionInstancePrototypeWritable();
237
Steve Block44f0eee2011-05-26 01:26:41 +0100238 Handle<Map> CreateStrictModeFunctionMap(
239 PrototypePropertyMode prototype_mode,
240 Handle<JSFunction> empty_function,
241 Handle<FixedArray> arguments_callbacks,
242 Handle<FixedArray> caller_callbacks);
243
244 Handle<DescriptorArray> ComputeStrictFunctionInstanceDescriptor(
245 PrototypePropertyMode propertyMode,
246 Handle<FixedArray> arguments,
247 Handle<FixedArray> caller);
248
Ben Murdoch8b112d22011-06-08 16:22:53 +0100249 static bool CompileBuiltin(Isolate* isolate, int index);
250 static bool CompileExperimentalBuiltin(Isolate* isolate, int index);
Steve Blocka7e24c12009-10-30 11:49:00 +0000251 static bool CompileNative(Vector<const char> name, Handle<String> source);
252 static bool CompileScriptCached(Vector<const char> name,
253 Handle<String> source,
254 SourceCodeCache* cache,
255 v8::Extension* extension,
Andrei Popescu31002712010-02-23 13:46:05 +0000256 Handle<Context> top_context,
Steve Blocka7e24c12009-10-30 11:49:00 +0000257 bool use_runtime_context);
258
259 Handle<Context> result_;
Steve Block44f0eee2011-05-26 01:26:41 +0100260
261 // Function instance maps. Function literal maps are created initially with
262 // a read only prototype for the processing of JS builtins. Later the function
263 // instance maps are replaced in order to make prototype writable.
264 // These are the final, writable prototype, maps.
265 Handle<Map> function_instance_map_writable_prototype_;
266 Handle<Map> strict_mode_function_instance_map_writable_prototype_;
267
Andrei Popescu31002712010-02-23 13:46:05 +0000268 BootstrapperActive active_;
269 friend class Bootstrapper;
Steve Blocka7e24c12009-10-30 11:49:00 +0000270};
271
Steve Blocka7e24c12009-10-30 11:49:00 +0000272
273void Bootstrapper::Iterate(ObjectVisitor* v) {
Steve Block44f0eee2011-05-26 01:26:41 +0100274 extensions_cache_.Iterate(v);
Steve Blockd0582a62009-12-15 09:54:21 +0000275 v->Synchronize("Extensions");
Steve Blocka7e24c12009-10-30 11:49:00 +0000276}
277
278
Steve Blocka7e24c12009-10-30 11:49:00 +0000279Handle<Context> Bootstrapper::CreateEnvironment(
Ben Murdoch8b112d22011-06-08 16:22:53 +0100280 Isolate* isolate,
Steve Blocka7e24c12009-10-30 11:49:00 +0000281 Handle<Object> global_object,
282 v8::Handle<v8::ObjectTemplate> global_template,
283 v8::ExtensionConfiguration* extensions) {
Andrei Popescu31002712010-02-23 13:46:05 +0000284 HandleScope scope;
285 Handle<Context> env;
Ben Murdoch8b112d22011-06-08 16:22:53 +0100286 Genesis genesis(isolate, global_object, global_template, extensions);
Andrei Popescu31002712010-02-23 13:46:05 +0000287 env = genesis.result();
288 if (!env.is_null()) {
289 if (InstallExtensions(env, extensions)) {
290 return env;
291 }
292 }
293 return Handle<Context>();
Steve Blocka7e24c12009-10-30 11:49:00 +0000294}
295
296
297static void SetObjectPrototype(Handle<JSObject> object, Handle<Object> proto) {
298 // object.__proto__ = proto;
Ben Murdoch8b112d22011-06-08 16:22:53 +0100299 Factory* factory = object->GetIsolate()->factory();
Steve Blocka7e24c12009-10-30 11:49:00 +0000300 Handle<Map> old_to_map = Handle<Map>(object->map());
Ben Murdoch8b112d22011-06-08 16:22:53 +0100301 Handle<Map> new_to_map = factory->CopyMapDropTransitions(old_to_map);
Steve Blocka7e24c12009-10-30 11:49:00 +0000302 new_to_map->set_prototype(*proto);
303 object->set_map(*new_to_map);
304}
305
306
307void Bootstrapper::DetachGlobal(Handle<Context> env) {
Ben Murdoch8b112d22011-06-08 16:22:53 +0100308 Factory* factory = env->GetIsolate()->factory();
Steve Block44f0eee2011-05-26 01:26:41 +0100309 JSGlobalProxy::cast(env->global_proxy())->set_context(*factory->null_value());
Steve Blocka7e24c12009-10-30 11:49:00 +0000310 SetObjectPrototype(Handle<JSObject>(env->global_proxy()),
Steve Block44f0eee2011-05-26 01:26:41 +0100311 factory->null_value());
Steve Blocka7e24c12009-10-30 11:49:00 +0000312 env->set_global_proxy(env->global());
313 env->global()->set_global_receiver(env->global());
314}
315
316
Andrei Popescu74b3c142010-03-29 12:03:09 +0100317void Bootstrapper::ReattachGlobal(Handle<Context> env,
318 Handle<Object> global_object) {
319 ASSERT(global_object->IsJSGlobalProxy());
320 Handle<JSGlobalProxy> global = Handle<JSGlobalProxy>::cast(global_object);
321 env->global()->set_global_receiver(*global);
322 env->set_global_proxy(*global);
323 SetObjectPrototype(global, Handle<JSObject>(env->global()));
324 global->set_context(*env);
325}
326
327
Steve Blocka7e24c12009-10-30 11:49:00 +0000328static Handle<JSFunction> InstallFunction(Handle<JSObject> target,
329 const char* name,
330 InstanceType type,
331 int instance_size,
332 Handle<JSObject> prototype,
333 Builtins::Name call,
334 bool is_ecma_native) {
Ben Murdoch8b112d22011-06-08 16:22:53 +0100335 Isolate* isolate = target->GetIsolate();
Steve Block44f0eee2011-05-26 01:26:41 +0100336 Factory* factory = isolate->factory();
337 Handle<String> symbol = factory->LookupAsciiSymbol(name);
338 Handle<Code> call_code = Handle<Code>(isolate->builtins()->builtin(call));
Steve Block6ded16b2010-05-10 14:33:55 +0100339 Handle<JSFunction> function = prototype.is_null() ?
Steve Block44f0eee2011-05-26 01:26:41 +0100340 factory->NewFunctionWithoutPrototype(symbol, call_code) :
341 factory->NewFunctionWithPrototype(symbol,
Steve Blocka7e24c12009-10-30 11:49:00 +0000342 type,
343 instance_size,
344 prototype,
345 call_code,
346 is_ecma_native);
Steve Block1e0659c2011-05-24 12:43:12 +0100347 SetLocalPropertyNoThrow(target, symbol, function, DONT_ENUM);
Steve Blocka7e24c12009-10-30 11:49:00 +0000348 if (is_ecma_native) {
349 function->shared()->set_instance_class_name(*symbol);
350 }
351 return function;
352}
353
354
355Handle<DescriptorArray> Genesis::ComputeFunctionInstanceDescriptor(
Steve Block6ded16b2010-05-10 14:33:55 +0100356 PrototypePropertyMode prototypeMode) {
Steve Block44f0eee2011-05-26 01:26:41 +0100357 Handle<DescriptorArray> descriptors =
Ben Murdoch8b112d22011-06-08 16:22:53 +0100358 factory()->NewDescriptorArray(prototypeMode == DONT_ADD_PROTOTYPE
359 ? 4
360 : 5);
Steve Block6ded16b2010-05-10 14:33:55 +0100361 PropertyAttributes attributes =
Steve Blocka7e24c12009-10-30 11:49:00 +0000362 static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE | READ_ONLY);
Steve Blocka7e24c12009-10-30 11:49:00 +0000363
Steve Block44f0eee2011-05-26 01:26:41 +0100364 { // Add length.
Ben Murdoch8b112d22011-06-08 16:22:53 +0100365 Handle<Proxy> proxy = factory()->NewProxy(&Accessors::FunctionLength);
366 CallbacksDescriptor d(*factory()->length_symbol(), *proxy, attributes);
Steve Block44f0eee2011-05-26 01:26:41 +0100367 descriptors->Set(0, &d);
368 }
369 { // Add name.
Ben Murdoch8b112d22011-06-08 16:22:53 +0100370 Handle<Proxy> proxy = factory()->NewProxy(&Accessors::FunctionName);
371 CallbacksDescriptor d(*factory()->name_symbol(), *proxy, attributes);
Steve Block44f0eee2011-05-26 01:26:41 +0100372 descriptors->Set(1, &d);
373 }
374 { // Add arguments.
Ben Murdoch8b112d22011-06-08 16:22:53 +0100375 Handle<Proxy> proxy = factory()->NewProxy(&Accessors::FunctionArguments);
376 CallbacksDescriptor d(*factory()->arguments_symbol(), *proxy, attributes);
Steve Block44f0eee2011-05-26 01:26:41 +0100377 descriptors->Set(2, &d);
378 }
379 { // Add caller.
Ben Murdoch8b112d22011-06-08 16:22:53 +0100380 Handle<Proxy> proxy = factory()->NewProxy(&Accessors::FunctionCaller);
381 CallbacksDescriptor d(*factory()->caller_symbol(), *proxy, attributes);
Steve Block44f0eee2011-05-26 01:26:41 +0100382 descriptors->Set(3, &d);
383 }
384 if (prototypeMode != DONT_ADD_PROTOTYPE) {
385 // Add prototype.
386 if (prototypeMode == ADD_WRITEABLE_PROTOTYPE) {
387 attributes = static_cast<PropertyAttributes>(attributes & ~READ_ONLY);
388 }
Ben Murdoch8b112d22011-06-08 16:22:53 +0100389 Handle<Proxy> proxy = factory()->NewProxy(&Accessors::FunctionPrototype);
390 CallbacksDescriptor d(*factory()->prototype_symbol(), *proxy, attributes);
Steve Block44f0eee2011-05-26 01:26:41 +0100391 descriptors->Set(4, &d);
392 }
393 descriptors->Sort();
394 return descriptors;
395}
Steve Blocka7e24c12009-10-30 11:49:00 +0000396
Steve Blocka7e24c12009-10-30 11:49:00 +0000397
Steve Block44f0eee2011-05-26 01:26:41 +0100398Handle<Map> Genesis::CreateFunctionMap(PrototypePropertyMode prototype_mode) {
Ben Murdoch8b112d22011-06-08 16:22:53 +0100399 Handle<Map> map = factory()->NewMap(JS_FUNCTION_TYPE, JSFunction::kSize);
Steve Block44f0eee2011-05-26 01:26:41 +0100400 Handle<DescriptorArray> descriptors =
401 ComputeFunctionInstanceDescriptor(prototype_mode);
402 map->set_instance_descriptors(*descriptors);
403 map->set_function_with_prototype(prototype_mode != DONT_ADD_PROTOTYPE);
404 return map;
Steve Blocka7e24c12009-10-30 11:49:00 +0000405}
406
407
Ben Murdoch8b112d22011-06-08 16:22:53 +0100408Handle<JSFunction> Genesis::CreateEmptyFunction(Isolate* isolate) {
Steve Block44f0eee2011-05-26 01:26:41 +0100409 // Allocate the map for function instances. Maps are allocated first and their
410 // prototypes patched later, once empty function is created.
411
Steve Blocka7e24c12009-10-30 11:49:00 +0000412 // Please note that the prototype property for function instances must be
413 // writable.
Ben Murdoch8b112d22011-06-08 16:22:53 +0100414 Handle<Map> function_instance_map =
415 CreateFunctionMap(ADD_WRITEABLE_PROTOTYPE);
416 global_context()->set_function_instance_map(*function_instance_map);
Steve Block6ded16b2010-05-10 14:33:55 +0100417
418 // Functions with this map will not have a 'prototype' property, and
419 // can not be used as constructors.
Ben Murdoch8b112d22011-06-08 16:22:53 +0100420 Handle<Map> function_without_prototype_map =
421 CreateFunctionMap(DONT_ADD_PROTOTYPE);
Steve Block6ded16b2010-05-10 14:33:55 +0100422 global_context()->set_function_without_prototype_map(
Ben Murdoch8b112d22011-06-08 16:22:53 +0100423 *function_without_prototype_map);
Steve Blocka7e24c12009-10-30 11:49:00 +0000424
Steve Block44f0eee2011-05-26 01:26:41 +0100425 // Allocate the function map. This map is temporary, used only for processing
426 // of builtins.
427 // Later the map is replaced with writable prototype map, allocated below.
Ben Murdoch8b112d22011-06-08 16:22:53 +0100428 Handle<Map> function_map = CreateFunctionMap(ADD_READONLY_PROTOTYPE);
429 global_context()->set_function_map(*function_map);
Steve Blocka7e24c12009-10-30 11:49:00 +0000430
Steve Block44f0eee2011-05-26 01:26:41 +0100431 // The final map for functions. Writeable prototype.
432 // This map is installed in MakeFunctionInstancePrototypeWritable.
433 function_instance_map_writable_prototype_ =
434 CreateFunctionMap(ADD_WRITEABLE_PROTOTYPE);
435
Steve Block44f0eee2011-05-26 01:26:41 +0100436 Factory* factory = isolate->factory();
437 Heap* heap = isolate->heap();
438
439 Handle<String> object_name = Handle<String>(heap->Object_symbol());
Steve Blocka7e24c12009-10-30 11:49:00 +0000440
441 { // --- O b j e c t ---
442 Handle<JSFunction> object_fun =
Steve Block44f0eee2011-05-26 01:26:41 +0100443 factory->NewFunction(object_name, factory->null_value());
Steve Blocka7e24c12009-10-30 11:49:00 +0000444 Handle<Map> object_function_map =
Steve Block44f0eee2011-05-26 01:26:41 +0100445 factory->NewMap(JS_OBJECT_TYPE, JSObject::kHeaderSize);
Steve Blocka7e24c12009-10-30 11:49:00 +0000446 object_fun->set_initial_map(*object_function_map);
447 object_function_map->set_constructor(*object_fun);
448
449 global_context()->set_object_function(*object_fun);
450
451 // Allocate a new prototype for the object function.
Steve Block44f0eee2011-05-26 01:26:41 +0100452 Handle<JSObject> prototype = factory->NewJSObject(
453 isolate->object_function(),
454 TENURED);
Steve Blocka7e24c12009-10-30 11:49:00 +0000455
456 global_context()->set_initial_object_prototype(*prototype);
457 SetPrototype(object_fun, prototype);
458 object_function_map->
Steve Block44f0eee2011-05-26 01:26:41 +0100459 set_instance_descriptors(heap->empty_descriptor_array());
Steve Blocka7e24c12009-10-30 11:49:00 +0000460 }
461
462 // Allocate the empty function as the prototype for function ECMAScript
463 // 262 15.3.4.
Steve Block44f0eee2011-05-26 01:26:41 +0100464 Handle<String> symbol = factory->LookupAsciiSymbol("Empty");
Steve Blocka7e24c12009-10-30 11:49:00 +0000465 Handle<JSFunction> empty_function =
Steve Block44f0eee2011-05-26 01:26:41 +0100466 factory->NewFunctionWithoutPrototype(symbol, kNonStrictMode);
Steve Blocka7e24c12009-10-30 11:49:00 +0000467
Andrei Popescu31002712010-02-23 13:46:05 +0000468 // --- E m p t y ---
469 Handle<Code> code =
Steve Block44f0eee2011-05-26 01:26:41 +0100470 Handle<Code>(isolate->builtins()->builtin(
471 Builtins::kEmptyFunction));
Andrei Popescu31002712010-02-23 13:46:05 +0000472 empty_function->set_code(*code);
Iain Merrick75681382010-08-19 15:07:18 +0100473 empty_function->shared()->set_code(*code);
Steve Block44f0eee2011-05-26 01:26:41 +0100474 Handle<String> source = factory->NewStringFromAscii(CStrVector("() {}"));
475 Handle<Script> script = factory->NewScript(source);
Andrei Popescu31002712010-02-23 13:46:05 +0000476 script->set_type(Smi::FromInt(Script::TYPE_NATIVE));
477 empty_function->shared()->set_script(*script);
478 empty_function->shared()->set_start_position(0);
479 empty_function->shared()->set_end_position(source->length());
480 empty_function->shared()->DontAdaptArguments();
Steve Block44f0eee2011-05-26 01:26:41 +0100481
482 // Set prototypes for the function maps.
Andrei Popescu31002712010-02-23 13:46:05 +0000483 global_context()->function_map()->set_prototype(*empty_function);
484 global_context()->function_instance_map()->set_prototype(*empty_function);
Steve Block6ded16b2010-05-10 14:33:55 +0100485 global_context()->function_without_prototype_map()->
486 set_prototype(*empty_function);
Steve Block44f0eee2011-05-26 01:26:41 +0100487 function_instance_map_writable_prototype_->set_prototype(*empty_function);
Steve Blocka7e24c12009-10-30 11:49:00 +0000488
Andrei Popescu31002712010-02-23 13:46:05 +0000489 // Allocate the function map first and then patch the prototype later
Steve Block44f0eee2011-05-26 01:26:41 +0100490 Handle<Map> empty_fm = factory->CopyMapDropDescriptors(
Steve Block6ded16b2010-05-10 14:33:55 +0100491 function_without_prototype_map);
492 empty_fm->set_instance_descriptors(
Steve Block44f0eee2011-05-26 01:26:41 +0100493 function_without_prototype_map->instance_descriptors());
Andrei Popescu31002712010-02-23 13:46:05 +0000494 empty_fm->set_prototype(global_context()->object_function()->prototype());
495 empty_function->set_map(*empty_fm);
496 return empty_function;
497}
498
499
Steve Block44f0eee2011-05-26 01:26:41 +0100500Handle<DescriptorArray> Genesis::ComputeStrictFunctionInstanceDescriptor(
501 PrototypePropertyMode prototypeMode,
502 Handle<FixedArray> arguments,
503 Handle<FixedArray> caller) {
Steve Block44f0eee2011-05-26 01:26:41 +0100504 Handle<DescriptorArray> descriptors =
Ben Murdoch8b112d22011-06-08 16:22:53 +0100505 factory()->NewDescriptorArray(prototypeMode == DONT_ADD_PROTOTYPE
506 ? 4
507 : 5);
Steve Block44f0eee2011-05-26 01:26:41 +0100508 PropertyAttributes attributes = static_cast<PropertyAttributes>(
509 DONT_ENUM | DONT_DELETE | READ_ONLY);
510
511 { // length
Ben Murdoch8b112d22011-06-08 16:22:53 +0100512 Handle<Proxy> proxy = factory()->NewProxy(&Accessors::FunctionLength);
513 CallbacksDescriptor d(*factory()->length_symbol(), *proxy, attributes);
Steve Block44f0eee2011-05-26 01:26:41 +0100514 descriptors->Set(0, &d);
515 }
516 { // name
Ben Murdoch8b112d22011-06-08 16:22:53 +0100517 Handle<Proxy> proxy = factory()->NewProxy(&Accessors::FunctionName);
518 CallbacksDescriptor d(*factory()->name_symbol(), *proxy, attributes);
Steve Block44f0eee2011-05-26 01:26:41 +0100519 descriptors->Set(1, &d);
520 }
521 { // arguments
Ben Murdoch8b112d22011-06-08 16:22:53 +0100522 CallbacksDescriptor d(*factory()->arguments_symbol(),
523 *arguments,
524 attributes);
Steve Block44f0eee2011-05-26 01:26:41 +0100525 descriptors->Set(2, &d);
526 }
527 { // caller
Ben Murdoch8b112d22011-06-08 16:22:53 +0100528 CallbacksDescriptor d(*factory()->caller_symbol(), *caller, attributes);
Steve Block44f0eee2011-05-26 01:26:41 +0100529 descriptors->Set(3, &d);
530 }
531
532 // prototype
533 if (prototypeMode != DONT_ADD_PROTOTYPE) {
534 if (prototypeMode == ADD_WRITEABLE_PROTOTYPE) {
535 attributes = static_cast<PropertyAttributes>(attributes & ~READ_ONLY);
536 }
Ben Murdoch8b112d22011-06-08 16:22:53 +0100537 Handle<Proxy> proxy = factory()->NewProxy(&Accessors::FunctionPrototype);
538 CallbacksDescriptor d(*factory()->prototype_symbol(), *proxy, attributes);
Steve Block44f0eee2011-05-26 01:26:41 +0100539 descriptors->Set(4, &d);
540 }
541
542 descriptors->Sort();
543 return descriptors;
544}
545
546
547// ECMAScript 5th Edition, 13.2.3
548Handle<JSFunction> Genesis::CreateThrowTypeErrorFunction(
549 Builtins::Name builtin) {
Ben Murdoch8b112d22011-06-08 16:22:53 +0100550 Handle<String> name = factory()->LookupAsciiSymbol("ThrowTypeError");
Steve Block44f0eee2011-05-26 01:26:41 +0100551 Handle<JSFunction> throw_type_error =
Ben Murdoch8b112d22011-06-08 16:22:53 +0100552 factory()->NewFunctionWithoutPrototype(name, kStrictMode);
Steve Block44f0eee2011-05-26 01:26:41 +0100553 Handle<Code> code = Handle<Code>(
Ben Murdoch8b112d22011-06-08 16:22:53 +0100554 isolate()->builtins()->builtin(builtin));
Steve Block44f0eee2011-05-26 01:26:41 +0100555
556 throw_type_error->set_map(global_context()->strict_mode_function_map());
557 throw_type_error->set_code(*code);
558 throw_type_error->shared()->set_code(*code);
559 throw_type_error->shared()->DontAdaptArguments();
560
561 PreventExtensions(throw_type_error);
562
563 return throw_type_error;
564}
565
566
567Handle<Map> Genesis::CreateStrictModeFunctionMap(
568 PrototypePropertyMode prototype_mode,
569 Handle<JSFunction> empty_function,
570 Handle<FixedArray> arguments_callbacks,
571 Handle<FixedArray> caller_callbacks) {
Ben Murdoch8b112d22011-06-08 16:22:53 +0100572 Handle<Map> map = factory()->NewMap(JS_FUNCTION_TYPE, JSFunction::kSize);
Steve Block44f0eee2011-05-26 01:26:41 +0100573 Handle<DescriptorArray> descriptors =
574 ComputeStrictFunctionInstanceDescriptor(prototype_mode,
575 arguments_callbacks,
576 caller_callbacks);
577 map->set_instance_descriptors(*descriptors);
578 map->set_function_with_prototype(prototype_mode != DONT_ADD_PROTOTYPE);
579 map->set_prototype(*empty_function);
580 return map;
581}
582
583
584void Genesis::CreateStrictModeFunctionMaps(Handle<JSFunction> empty) {
585 // Create the callbacks arrays for ThrowTypeError functions.
586 // The get/set callacks are filled in after the maps are created below.
Ben Murdoch8b112d22011-06-08 16:22:53 +0100587 Factory* factory = empty->GetIsolate()->factory();
Steve Block44f0eee2011-05-26 01:26:41 +0100588 Handle<FixedArray> arguments = factory->NewFixedArray(2, TENURED);
589 Handle<FixedArray> caller = factory->NewFixedArray(2, TENURED);
590
591 // Allocate map for the strict mode function instances.
Ben Murdoch8b112d22011-06-08 16:22:53 +0100592 Handle<Map> strict_mode_function_instance_map =
593 CreateStrictModeFunctionMap(
594 ADD_WRITEABLE_PROTOTYPE, empty, arguments, caller);
Steve Block44f0eee2011-05-26 01:26:41 +0100595 global_context()->set_strict_mode_function_instance_map(
Ben Murdoch8b112d22011-06-08 16:22:53 +0100596 *strict_mode_function_instance_map);
Steve Block44f0eee2011-05-26 01:26:41 +0100597
598 // Allocate map for the prototype-less strict mode instances.
Ben Murdoch8b112d22011-06-08 16:22:53 +0100599 Handle<Map> strict_mode_function_without_prototype_map =
600 CreateStrictModeFunctionMap(
601 DONT_ADD_PROTOTYPE, empty, arguments, caller);
Steve Block44f0eee2011-05-26 01:26:41 +0100602 global_context()->set_strict_mode_function_without_prototype_map(
Ben Murdoch8b112d22011-06-08 16:22:53 +0100603 *strict_mode_function_without_prototype_map);
Steve Block44f0eee2011-05-26 01:26:41 +0100604
605 // Allocate map for the strict mode functions. This map is temporary, used
606 // only for processing of builtins.
607 // Later the map is replaced with writable prototype map, allocated below.
Ben Murdoch8b112d22011-06-08 16:22:53 +0100608 Handle<Map> strict_mode_function_map =
609 CreateStrictModeFunctionMap(
610 ADD_READONLY_PROTOTYPE, empty, arguments, caller);
Steve Block44f0eee2011-05-26 01:26:41 +0100611 global_context()->set_strict_mode_function_map(
Ben Murdoch8b112d22011-06-08 16:22:53 +0100612 *strict_mode_function_map);
Steve Block44f0eee2011-05-26 01:26:41 +0100613
614 // The final map for the strict mode functions. Writeable prototype.
615 // This map is installed in MakeFunctionInstancePrototypeWritable.
616 strict_mode_function_instance_map_writable_prototype_ =
617 CreateStrictModeFunctionMap(
618 ADD_WRITEABLE_PROTOTYPE, empty, arguments, caller);
619
620 // Create the ThrowTypeError function instances.
621 Handle<JSFunction> arguments_throw =
622 CreateThrowTypeErrorFunction(Builtins::kStrictFunctionArguments);
623 Handle<JSFunction> caller_throw =
624 CreateThrowTypeErrorFunction(Builtins::kStrictFunctionCaller);
625
626 // Complete the callback fixed arrays.
627 arguments->set(0, *arguments_throw);
628 arguments->set(1, *arguments_throw);
629 caller->set(0, *caller_throw);
630 caller->set(1, *caller_throw);
631}
632
633
Ben Murdochb0fe1622011-05-05 13:52:32 +0100634static void AddToWeakGlobalContextList(Context* context) {
635 ASSERT(context->IsGlobalContext());
Ben Murdoch8b112d22011-06-08 16:22:53 +0100636 Heap* heap = context->GetIsolate()->heap();
Ben Murdochb0fe1622011-05-05 13:52:32 +0100637#ifdef DEBUG
638 { // NOLINT
639 ASSERT(context->get(Context::NEXT_CONTEXT_LINK)->IsUndefined());
640 // Check that context is not in the list yet.
Steve Block44f0eee2011-05-26 01:26:41 +0100641 for (Object* current = heap->global_contexts_list();
Ben Murdochb0fe1622011-05-05 13:52:32 +0100642 !current->IsUndefined();
643 current = Context::cast(current)->get(Context::NEXT_CONTEXT_LINK)) {
644 ASSERT(current != context);
645 }
646 }
647#endif
Steve Block44f0eee2011-05-26 01:26:41 +0100648 context->set(Context::NEXT_CONTEXT_LINK, heap->global_contexts_list());
649 heap->set_global_contexts_list(context);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100650}
651
652
Andrei Popescu31002712010-02-23 13:46:05 +0000653void Genesis::CreateRoots() {
654 // Allocate the global context FixedArray first and then patch the
655 // closure and extension object later (we need the empty function
656 // and the global object, but in order to create those, we need the
657 // global context).
Ben Murdoch8b112d22011-06-08 16:22:53 +0100658 global_context_ = Handle<Context>::cast(isolate()->global_handles()->Create(
659 *factory()->NewGlobalContext()));
Ben Murdochb0fe1622011-05-05 13:52:32 +0100660 AddToWeakGlobalContextList(*global_context_);
Ben Murdoch8b112d22011-06-08 16:22:53 +0100661 isolate()->set_context(*global_context());
Andrei Popescu31002712010-02-23 13:46:05 +0000662
663 // Allocate the message listeners object.
664 {
665 v8::NeanderArray listeners;
666 global_context()->set_message_listeners(*listeners.value());
667 }
668}
669
670
671Handle<JSGlobalProxy> Genesis::CreateNewGlobals(
672 v8::Handle<v8::ObjectTemplate> global_template,
673 Handle<Object> global_object,
674 Handle<GlobalObject>* inner_global_out) {
675 // The argument global_template aka data is an ObjectTemplateInfo.
676 // It has a constructor pointer that points at global_constructor which is a
677 // FunctionTemplateInfo.
678 // The global_constructor is used to create or reinitialize the global_proxy.
679 // The global_constructor also has a prototype_template pointer that points at
680 // js_global_template which is an ObjectTemplateInfo.
681 // That in turn has a constructor pointer that points at
682 // js_global_constructor which is a FunctionTemplateInfo.
683 // js_global_constructor is used to make js_global_function
684 // js_global_function is used to make the new inner_global.
685 //
686 // --- G l o b a l ---
687 // Step 1: Create a fresh inner JSGlobalObject.
688 Handle<JSFunction> js_global_function;
689 Handle<ObjectTemplateInfo> js_global_template;
690 if (!global_template.IsEmpty()) {
691 // Get prototype template of the global_template.
692 Handle<ObjectTemplateInfo> data =
693 v8::Utils::OpenHandle(*global_template);
694 Handle<FunctionTemplateInfo> global_constructor =
695 Handle<FunctionTemplateInfo>(
696 FunctionTemplateInfo::cast(data->constructor()));
697 Handle<Object> proto_template(global_constructor->prototype_template());
698 if (!proto_template->IsUndefined()) {
699 js_global_template =
700 Handle<ObjectTemplateInfo>::cast(proto_template);
701 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000702 }
703
Andrei Popescu31002712010-02-23 13:46:05 +0000704 if (js_global_template.is_null()) {
Ben Murdoch8b112d22011-06-08 16:22:53 +0100705 Handle<String> name = Handle<String>(heap()->empty_symbol());
706 Handle<Code> code = Handle<Code>(isolate()->builtins()->builtin(
Steve Block44f0eee2011-05-26 01:26:41 +0100707 Builtins::kIllegal));
Andrei Popescu31002712010-02-23 13:46:05 +0000708 js_global_function =
Ben Murdoch8b112d22011-06-08 16:22:53 +0100709 factory()->NewFunction(name, JS_GLOBAL_OBJECT_TYPE,
710 JSGlobalObject::kSize, code, true);
Andrei Popescu31002712010-02-23 13:46:05 +0000711 // Change the constructor property of the prototype of the
712 // hidden global function to refer to the Object function.
713 Handle<JSObject> prototype =
714 Handle<JSObject>(
715 JSObject::cast(js_global_function->instance_prototype()));
Steve Block1e0659c2011-05-24 12:43:12 +0100716 SetLocalPropertyNoThrow(
Steve Block44f0eee2011-05-26 01:26:41 +0100717 prototype,
Ben Murdoch8b112d22011-06-08 16:22:53 +0100718 factory()->constructor_symbol(),
719 isolate()->object_function(),
Steve Block44f0eee2011-05-26 01:26:41 +0100720 NONE);
Andrei Popescu31002712010-02-23 13:46:05 +0000721 } else {
722 Handle<FunctionTemplateInfo> js_global_constructor(
723 FunctionTemplateInfo::cast(js_global_template->constructor()));
724 js_global_function =
Ben Murdoch8b112d22011-06-08 16:22:53 +0100725 factory()->CreateApiFunction(js_global_constructor,
726 factory()->InnerGlobalObject);
Steve Blocka7e24c12009-10-30 11:49:00 +0000727 }
728
Andrei Popescu31002712010-02-23 13:46:05 +0000729 js_global_function->initial_map()->set_is_hidden_prototype();
730 Handle<GlobalObject> inner_global =
Ben Murdoch8b112d22011-06-08 16:22:53 +0100731 factory()->NewGlobalObject(js_global_function);
Andrei Popescu31002712010-02-23 13:46:05 +0000732 if (inner_global_out != NULL) {
733 *inner_global_out = inner_global;
734 }
735
736 // Step 2: create or re-initialize the global proxy object.
737 Handle<JSFunction> global_proxy_function;
738 if (global_template.IsEmpty()) {
Ben Murdoch8b112d22011-06-08 16:22:53 +0100739 Handle<String> name = Handle<String>(heap()->empty_symbol());
740 Handle<Code> code = Handle<Code>(isolate()->builtins()->builtin(
Steve Block44f0eee2011-05-26 01:26:41 +0100741 Builtins::kIllegal));
Andrei Popescu31002712010-02-23 13:46:05 +0000742 global_proxy_function =
Ben Murdoch8b112d22011-06-08 16:22:53 +0100743 factory()->NewFunction(name, JS_GLOBAL_PROXY_TYPE,
744 JSGlobalProxy::kSize, code, true);
Andrei Popescu31002712010-02-23 13:46:05 +0000745 } else {
746 Handle<ObjectTemplateInfo> data =
747 v8::Utils::OpenHandle(*global_template);
748 Handle<FunctionTemplateInfo> global_constructor(
749 FunctionTemplateInfo::cast(data->constructor()));
750 global_proxy_function =
Ben Murdoch8b112d22011-06-08 16:22:53 +0100751 factory()->CreateApiFunction(global_constructor,
752 factory()->OuterGlobalObject);
Andrei Popescu31002712010-02-23 13:46:05 +0000753 }
754
Ben Murdoch8b112d22011-06-08 16:22:53 +0100755 Handle<String> global_name = factory()->LookupAsciiSymbol("global");
Andrei Popescu31002712010-02-23 13:46:05 +0000756 global_proxy_function->shared()->set_instance_class_name(*global_name);
757 global_proxy_function->initial_map()->set_is_access_check_needed(true);
758
759 // Set global_proxy.__proto__ to js_global after ConfigureGlobalObjects
760 // Return the global proxy.
761
762 if (global_object.location() != NULL) {
763 ASSERT(global_object->IsJSGlobalProxy());
764 return ReinitializeJSGlobalProxy(
765 global_proxy_function,
766 Handle<JSGlobalProxy>::cast(global_object));
767 } else {
768 return Handle<JSGlobalProxy>::cast(
Ben Murdoch8b112d22011-06-08 16:22:53 +0100769 factory()->NewJSObject(global_proxy_function, TENURED));
Andrei Popescu31002712010-02-23 13:46:05 +0000770 }
771}
772
773
774void Genesis::HookUpGlobalProxy(Handle<GlobalObject> inner_global,
775 Handle<JSGlobalProxy> global_proxy) {
776 // Set the global context for the global object.
777 inner_global->set_global_context(*global_context());
778 inner_global->set_global_receiver(*global_proxy);
779 global_proxy->set_context(*global_context());
780 global_context()->set_global_proxy(*global_proxy);
781}
782
783
Andrei Popescu402d9372010-02-26 13:31:12 +0000784void Genesis::HookUpInnerGlobal(Handle<GlobalObject> inner_global) {
785 Handle<GlobalObject> inner_global_from_snapshot(
786 GlobalObject::cast(global_context_->extension()));
787 Handle<JSBuiltinsObject> builtins_global(global_context_->builtins());
788 global_context_->set_extension(*inner_global);
789 global_context_->set_global(*inner_global);
790 global_context_->set_security_token(*inner_global);
791 static const PropertyAttributes attributes =
792 static_cast<PropertyAttributes>(READ_ONLY | DONT_DELETE);
793 ForceSetProperty(builtins_global,
Ben Murdoch8b112d22011-06-08 16:22:53 +0100794 factory()->LookupAsciiSymbol("global"),
Andrei Popescu402d9372010-02-26 13:31:12 +0000795 inner_global,
796 attributes);
797 // Setup the reference from the global object to the builtins object.
798 JSGlobalObject::cast(*inner_global)->set_builtins(*builtins_global);
799 TransferNamedProperties(inner_global_from_snapshot, inner_global);
800 TransferIndexedProperties(inner_global_from_snapshot, inner_global);
801}
802
803
804// This is only called if we are not using snapshots. The equivalent
805// work in the snapshot case is done in HookUpInnerGlobal.
Andrei Popescu31002712010-02-23 13:46:05 +0000806void Genesis::InitializeGlobal(Handle<GlobalObject> inner_global,
807 Handle<JSFunction> empty_function) {
808 // --- G l o b a l C o n t e x t ---
809 // Use the empty function as closure (no scope info).
810 global_context()->set_closure(*empty_function);
811 global_context()->set_fcontext(*global_context());
812 global_context()->set_previous(NULL);
813 // Set extension and global object.
814 global_context()->set_extension(*inner_global);
815 global_context()->set_global(*inner_global);
816 // Security setup: Set the security token of the global object to
817 // its the inner global. This makes the security check between two
818 // different contexts fail by default even in case of global
819 // object reinitialization.
820 global_context()->set_security_token(*inner_global);
821
Ben Murdoch8b112d22011-06-08 16:22:53 +0100822 Isolate* isolate = inner_global->GetIsolate();
Steve Block44f0eee2011-05-26 01:26:41 +0100823 Factory* factory = isolate->factory();
824 Heap* heap = isolate->heap();
825
826 Handle<String> object_name = Handle<String>(heap->Object_symbol());
Steve Block1e0659c2011-05-24 12:43:12 +0100827 SetLocalPropertyNoThrow(inner_global, object_name,
Steve Block44f0eee2011-05-26 01:26:41 +0100828 isolate->object_function(), DONT_ENUM);
Andrei Popescu31002712010-02-23 13:46:05 +0000829
Steve Blocka7e24c12009-10-30 11:49:00 +0000830 Handle<JSObject> global = Handle<JSObject>(global_context()->global());
831
832 // Install global Function object
833 InstallFunction(global, "Function", JS_FUNCTION_TYPE, JSFunction::kSize,
Steve Block44f0eee2011-05-26 01:26:41 +0100834 empty_function, Builtins::kIllegal, true); // ECMA native.
Steve Blocka7e24c12009-10-30 11:49:00 +0000835
836 { // --- A r r a y ---
837 Handle<JSFunction> array_function =
838 InstallFunction(global, "Array", JS_ARRAY_TYPE, JSArray::kSize,
Steve Block44f0eee2011-05-26 01:26:41 +0100839 isolate->initial_object_prototype(),
840 Builtins::kArrayCode, true);
Steve Blocka7e24c12009-10-30 11:49:00 +0000841 array_function->shared()->set_construct_stub(
Steve Block44f0eee2011-05-26 01:26:41 +0100842 isolate->builtins()->builtin(Builtins::kArrayConstructCode));
Steve Blocka7e24c12009-10-30 11:49:00 +0000843 array_function->shared()->DontAdaptArguments();
844
845 // This seems a bit hackish, but we need to make sure Array.length
846 // is 1.
847 array_function->shared()->set_length(1);
848 Handle<DescriptorArray> array_descriptors =
Steve Block44f0eee2011-05-26 01:26:41 +0100849 factory->CopyAppendProxyDescriptor(
850 factory->empty_descriptor_array(),
851 factory->length_symbol(),
852 factory->NewProxy(&Accessors::ArrayLength),
Steve Blocka7e24c12009-10-30 11:49:00 +0000853 static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE));
854
855 // Cache the fast JavaScript array map
856 global_context()->set_js_array_map(array_function->initial_map());
857 global_context()->js_array_map()->set_instance_descriptors(
858 *array_descriptors);
859 // array_function is used internally. JS code creating array object should
860 // search for the 'Array' property on the global object and use that one
861 // as the constructor. 'Array' property on a global object can be
862 // overwritten by JS code.
863 global_context()->set_array_function(*array_function);
864 }
865
866 { // --- N u m b e r ---
867 Handle<JSFunction> number_fun =
868 InstallFunction(global, "Number", JS_VALUE_TYPE, JSValue::kSize,
Steve Block44f0eee2011-05-26 01:26:41 +0100869 isolate->initial_object_prototype(),
870 Builtins::kIllegal, true);
Steve Blocka7e24c12009-10-30 11:49:00 +0000871 global_context()->set_number_function(*number_fun);
872 }
873
874 { // --- B o o l e a n ---
875 Handle<JSFunction> boolean_fun =
876 InstallFunction(global, "Boolean", JS_VALUE_TYPE, JSValue::kSize,
Steve Block44f0eee2011-05-26 01:26:41 +0100877 isolate->initial_object_prototype(),
878 Builtins::kIllegal, true);
Steve Blocka7e24c12009-10-30 11:49:00 +0000879 global_context()->set_boolean_function(*boolean_fun);
880 }
881
882 { // --- S t r i n g ---
883 Handle<JSFunction> string_fun =
884 InstallFunction(global, "String", JS_VALUE_TYPE, JSValue::kSize,
Steve Block44f0eee2011-05-26 01:26:41 +0100885 isolate->initial_object_prototype(),
886 Builtins::kIllegal, true);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100887 string_fun->shared()->set_construct_stub(
Steve Block44f0eee2011-05-26 01:26:41 +0100888 isolate->builtins()->builtin(Builtins::kStringConstructCode));
Steve Blocka7e24c12009-10-30 11:49:00 +0000889 global_context()->set_string_function(*string_fun);
890 // Add 'length' property to strings.
891 Handle<DescriptorArray> string_descriptors =
Steve Block44f0eee2011-05-26 01:26:41 +0100892 factory->CopyAppendProxyDescriptor(
893 factory->empty_descriptor_array(),
894 factory->length_symbol(),
895 factory->NewProxy(&Accessors::StringLength),
Steve Blocka7e24c12009-10-30 11:49:00 +0000896 static_cast<PropertyAttributes>(DONT_ENUM |
897 DONT_DELETE |
898 READ_ONLY));
899
900 Handle<Map> string_map =
901 Handle<Map>(global_context()->string_function()->initial_map());
902 string_map->set_instance_descriptors(*string_descriptors);
903 }
904
905 { // --- D a t e ---
906 // Builtin functions for Date.prototype.
907 Handle<JSFunction> date_fun =
908 InstallFunction(global, "Date", JS_VALUE_TYPE, JSValue::kSize,
Steve Block44f0eee2011-05-26 01:26:41 +0100909 isolate->initial_object_prototype(),
910 Builtins::kIllegal, true);
Steve Blocka7e24c12009-10-30 11:49:00 +0000911
912 global_context()->set_date_function(*date_fun);
913 }
914
915
916 { // -- R e g E x p
917 // Builtin functions for RegExp.prototype.
918 Handle<JSFunction> regexp_fun =
919 InstallFunction(global, "RegExp", JS_REGEXP_TYPE, JSRegExp::kSize,
Steve Block44f0eee2011-05-26 01:26:41 +0100920 isolate->initial_object_prototype(),
921 Builtins::kIllegal, true);
Steve Blocka7e24c12009-10-30 11:49:00 +0000922 global_context()->set_regexp_function(*regexp_fun);
Steve Block6ded16b2010-05-10 14:33:55 +0100923
924 ASSERT(regexp_fun->has_initial_map());
925 Handle<Map> initial_map(regexp_fun->initial_map());
926
927 ASSERT_EQ(0, initial_map->inobject_properties());
928
Steve Block44f0eee2011-05-26 01:26:41 +0100929 Handle<DescriptorArray> descriptors = factory->NewDescriptorArray(5);
Steve Block6ded16b2010-05-10 14:33:55 +0100930 PropertyAttributes final =
931 static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE | READ_ONLY);
932 int enum_index = 0;
933 {
934 // ECMA-262, section 15.10.7.1.
Steve Block44f0eee2011-05-26 01:26:41 +0100935 FieldDescriptor field(heap->source_symbol(),
Steve Block6ded16b2010-05-10 14:33:55 +0100936 JSRegExp::kSourceFieldIndex,
937 final,
938 enum_index++);
939 descriptors->Set(0, &field);
940 }
941 {
942 // ECMA-262, section 15.10.7.2.
Steve Block44f0eee2011-05-26 01:26:41 +0100943 FieldDescriptor field(heap->global_symbol(),
Steve Block6ded16b2010-05-10 14:33:55 +0100944 JSRegExp::kGlobalFieldIndex,
945 final,
946 enum_index++);
947 descriptors->Set(1, &field);
948 }
949 {
950 // ECMA-262, section 15.10.7.3.
Steve Block44f0eee2011-05-26 01:26:41 +0100951 FieldDescriptor field(heap->ignore_case_symbol(),
Steve Block6ded16b2010-05-10 14:33:55 +0100952 JSRegExp::kIgnoreCaseFieldIndex,
953 final,
954 enum_index++);
955 descriptors->Set(2, &field);
956 }
957 {
958 // ECMA-262, section 15.10.7.4.
Steve Block44f0eee2011-05-26 01:26:41 +0100959 FieldDescriptor field(heap->multiline_symbol(),
Steve Block6ded16b2010-05-10 14:33:55 +0100960 JSRegExp::kMultilineFieldIndex,
961 final,
962 enum_index++);
963 descriptors->Set(3, &field);
964 }
965 {
966 // ECMA-262, section 15.10.7.5.
967 PropertyAttributes writable =
968 static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE);
Steve Block44f0eee2011-05-26 01:26:41 +0100969 FieldDescriptor field(heap->last_index_symbol(),
Steve Block6ded16b2010-05-10 14:33:55 +0100970 JSRegExp::kLastIndexFieldIndex,
971 writable,
972 enum_index++);
973 descriptors->Set(4, &field);
974 }
975 descriptors->SetNextEnumerationIndex(enum_index);
976 descriptors->Sort();
977
978 initial_map->set_inobject_properties(5);
979 initial_map->set_pre_allocated_property_fields(5);
980 initial_map->set_unused_property_fields(0);
981 initial_map->set_instance_size(
982 initial_map->instance_size() + 5 * kPointerSize);
983 initial_map->set_instance_descriptors(*descriptors);
Iain Merrick75681382010-08-19 15:07:18 +0100984 initial_map->set_visitor_id(StaticVisitorBase::GetVisitorId(*initial_map));
Steve Blocka7e24c12009-10-30 11:49:00 +0000985 }
986
987 { // -- J S O N
Steve Block44f0eee2011-05-26 01:26:41 +0100988 Handle<String> name = factory->NewStringFromAscii(CStrVector("JSON"));
989 Handle<JSFunction> cons = factory->NewFunction(
Steve Blocka7e24c12009-10-30 11:49:00 +0000990 name,
Steve Block44f0eee2011-05-26 01:26:41 +0100991 factory->the_hole_value());
Steve Blocka7e24c12009-10-30 11:49:00 +0000992 cons->SetInstancePrototype(global_context()->initial_object_prototype());
993 cons->SetInstanceClassName(*name);
Steve Block44f0eee2011-05-26 01:26:41 +0100994 Handle<JSObject> json_object = factory->NewJSObject(cons, TENURED);
Steve Blocka7e24c12009-10-30 11:49:00 +0000995 ASSERT(json_object->IsJSObject());
Steve Block1e0659c2011-05-24 12:43:12 +0100996 SetLocalPropertyNoThrow(global, name, json_object, DONT_ENUM);
Steve Blocka7e24c12009-10-30 11:49:00 +0000997 global_context()->set_json_object(*json_object);
998 }
999
1000 { // --- arguments_boilerplate_
1001 // Make sure we can recognize argument objects at runtime.
1002 // This is done by introducing an anonymous function with
1003 // class_name equals 'Arguments'.
Steve Block44f0eee2011-05-26 01:26:41 +01001004 Handle<String> symbol = factory->LookupAsciiSymbol("Arguments");
1005 Handle<Code> code = Handle<Code>(
1006 isolate->builtins()->builtin(Builtins::kIllegal));
Steve Blocka7e24c12009-10-30 11:49:00 +00001007 Handle<JSObject> prototype =
1008 Handle<JSObject>(
1009 JSObject::cast(global_context()->object_function()->prototype()));
1010
1011 Handle<JSFunction> function =
Steve Block44f0eee2011-05-26 01:26:41 +01001012 factory->NewFunctionWithPrototype(symbol,
Steve Blocka7e24c12009-10-30 11:49:00 +00001013 JS_OBJECT_TYPE,
1014 JSObject::kHeaderSize,
1015 prototype,
1016 code,
1017 false);
1018 ASSERT(!function->has_initial_map());
1019 function->shared()->set_instance_class_name(*symbol);
1020 function->shared()->set_expected_nof_properties(2);
Steve Block44f0eee2011-05-26 01:26:41 +01001021 Handle<JSObject> result = factory->NewJSObject(function);
Steve Blocka7e24c12009-10-30 11:49:00 +00001022
1023 global_context()->set_arguments_boilerplate(*result);
Steve Block44f0eee2011-05-26 01:26:41 +01001024 // Note: length must be added as the first property and
1025 // callee must be added as the second property.
1026 SetLocalPropertyNoThrow(result, factory->length_symbol(),
1027 factory->undefined_value(),
Steve Block1e0659c2011-05-24 12:43:12 +01001028 DONT_ENUM);
Steve Block44f0eee2011-05-26 01:26:41 +01001029 SetLocalPropertyNoThrow(result, factory->callee_symbol(),
1030 factory->undefined_value(),
Steve Block1e0659c2011-05-24 12:43:12 +01001031 DONT_ENUM);
Steve Blocka7e24c12009-10-30 11:49:00 +00001032
1033#ifdef DEBUG
1034 LookupResult lookup;
Steve Block44f0eee2011-05-26 01:26:41 +01001035 result->LocalLookup(heap->callee_symbol(), &lookup);
Andrei Popescu402d9372010-02-26 13:31:12 +00001036 ASSERT(lookup.IsProperty() && (lookup.type() == FIELD));
Steve Block44f0eee2011-05-26 01:26:41 +01001037 ASSERT(lookup.GetFieldIndex() == Heap::kArgumentsCalleeIndex);
Steve Blocka7e24c12009-10-30 11:49:00 +00001038
Steve Block44f0eee2011-05-26 01:26:41 +01001039 result->LocalLookup(heap->length_symbol(), &lookup);
Andrei Popescu402d9372010-02-26 13:31:12 +00001040 ASSERT(lookup.IsProperty() && (lookup.type() == FIELD));
Steve Block44f0eee2011-05-26 01:26:41 +01001041 ASSERT(lookup.GetFieldIndex() == Heap::kArgumentsLengthIndex);
Steve Blocka7e24c12009-10-30 11:49:00 +00001042
Steve Block44f0eee2011-05-26 01:26:41 +01001043 ASSERT(result->map()->inobject_properties() > Heap::kArgumentsCalleeIndex);
1044 ASSERT(result->map()->inobject_properties() > Heap::kArgumentsLengthIndex);
1045
1046 // Check the state of the object.
1047 ASSERT(result->HasFastProperties());
1048 ASSERT(result->HasFastElements());
1049#endif
1050 }
1051
1052 { // --- strict mode arguments boilerplate
1053 const PropertyAttributes attributes =
1054 static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE | READ_ONLY);
1055
1056 // Create the ThrowTypeError functions.
1057 Handle<FixedArray> callee = factory->NewFixedArray(2, TENURED);
1058 Handle<FixedArray> caller = factory->NewFixedArray(2, TENURED);
1059
1060 Handle<JSFunction> callee_throw =
1061 CreateThrowTypeErrorFunction(Builtins::kStrictArgumentsCallee);
1062 Handle<JSFunction> caller_throw =
1063 CreateThrowTypeErrorFunction(Builtins::kStrictArgumentsCaller);
1064
1065 // Install the ThrowTypeError functions.
1066 callee->set(0, *callee_throw);
1067 callee->set(1, *callee_throw);
1068 caller->set(0, *caller_throw);
1069 caller->set(1, *caller_throw);
1070
1071 // Create the descriptor array for the arguments object.
1072 Handle<DescriptorArray> descriptors = factory->NewDescriptorArray(3);
1073 { // length
1074 FieldDescriptor d(*factory->length_symbol(), 0, DONT_ENUM);
1075 descriptors->Set(0, &d);
1076 }
1077 { // callee
1078 CallbacksDescriptor d(*factory->callee_symbol(), *callee, attributes);
1079 descriptors->Set(1, &d);
1080 }
1081 { // caller
1082 CallbacksDescriptor d(*factory->caller_symbol(), *caller, attributes);
1083 descriptors->Set(2, &d);
1084 }
1085 descriptors->Sort();
1086
1087 // Create the map. Allocate one in-object field for length.
1088 Handle<Map> map = factory->NewMap(JS_OBJECT_TYPE,
1089 Heap::kArgumentsObjectSizeStrict);
1090 map->set_instance_descriptors(*descriptors);
1091 map->set_function_with_prototype(true);
1092 map->set_prototype(global_context()->object_function()->prototype());
1093 map->set_pre_allocated_property_fields(1);
1094 map->set_inobject_properties(1);
1095
1096 // Copy constructor from the non-strict arguments boilerplate.
1097 map->set_constructor(
1098 global_context()->arguments_boilerplate()->map()->constructor());
1099
1100 // Allocate the arguments boilerplate object.
1101 Handle<JSObject> result = factory->NewJSObjectFromMap(map);
1102 global_context()->set_strict_mode_arguments_boilerplate(*result);
1103
1104 // Add length property only for strict mode boilerplate.
1105 SetLocalPropertyNoThrow(result, factory->length_symbol(),
1106 factory->undefined_value(),
1107 DONT_ENUM);
1108
1109#ifdef DEBUG
1110 LookupResult lookup;
1111 result->LocalLookup(heap->length_symbol(), &lookup);
1112 ASSERT(lookup.IsProperty() && (lookup.type() == FIELD));
1113 ASSERT(lookup.GetFieldIndex() == Heap::kArgumentsLengthIndex);
1114
1115 ASSERT(result->map()->inobject_properties() > Heap::kArgumentsLengthIndex);
Steve Blocka7e24c12009-10-30 11:49:00 +00001116
1117 // Check the state of the object.
1118 ASSERT(result->HasFastProperties());
1119 ASSERT(result->HasFastElements());
1120#endif
1121 }
1122
1123 { // --- context extension
1124 // Create a function for the context extension objects.
Steve Block44f0eee2011-05-26 01:26:41 +01001125 Handle<Code> code = Handle<Code>(
1126 isolate->builtins()->builtin(Builtins::kIllegal));
Steve Blocka7e24c12009-10-30 11:49:00 +00001127 Handle<JSFunction> context_extension_fun =
Steve Block44f0eee2011-05-26 01:26:41 +01001128 factory->NewFunction(factory->empty_symbol(),
Steve Blocka7e24c12009-10-30 11:49:00 +00001129 JS_CONTEXT_EXTENSION_OBJECT_TYPE,
1130 JSObject::kHeaderSize,
1131 code,
1132 true);
1133
Steve Block44f0eee2011-05-26 01:26:41 +01001134 Handle<String> name = factory->LookupAsciiSymbol("context_extension");
Steve Blocka7e24c12009-10-30 11:49:00 +00001135 context_extension_fun->shared()->set_instance_class_name(*name);
1136 global_context()->set_context_extension_function(*context_extension_fun);
1137 }
1138
1139
1140 {
1141 // Setup the call-as-function delegate.
1142 Handle<Code> code =
Steve Block44f0eee2011-05-26 01:26:41 +01001143 Handle<Code>(isolate->builtins()->builtin(
1144 Builtins::kHandleApiCallAsFunction));
Steve Blocka7e24c12009-10-30 11:49:00 +00001145 Handle<JSFunction> delegate =
Steve Block44f0eee2011-05-26 01:26:41 +01001146 factory->NewFunction(factory->empty_symbol(), JS_OBJECT_TYPE,
Steve Blocka7e24c12009-10-30 11:49:00 +00001147 JSObject::kHeaderSize, code, true);
1148 global_context()->set_call_as_function_delegate(*delegate);
1149 delegate->shared()->DontAdaptArguments();
1150 }
1151
1152 {
1153 // Setup the call-as-constructor delegate.
1154 Handle<Code> code =
Steve Block44f0eee2011-05-26 01:26:41 +01001155 Handle<Code>(isolate->builtins()->builtin(
1156 Builtins::kHandleApiCallAsConstructor));
Steve Blocka7e24c12009-10-30 11:49:00 +00001157 Handle<JSFunction> delegate =
Steve Block44f0eee2011-05-26 01:26:41 +01001158 factory->NewFunction(factory->empty_symbol(), JS_OBJECT_TYPE,
Steve Blocka7e24c12009-10-30 11:49:00 +00001159 JSObject::kHeaderSize, code, true);
1160 global_context()->set_call_as_constructor_delegate(*delegate);
1161 delegate->shared()->DontAdaptArguments();
1162 }
1163
Steve Blocka7e24c12009-10-30 11:49:00 +00001164 // Initialize the out of memory slot.
Steve Block44f0eee2011-05-26 01:26:41 +01001165 global_context()->set_out_of_memory(heap->false_value());
Steve Blocka7e24c12009-10-30 11:49:00 +00001166
1167 // Initialize the data slot.
Steve Block44f0eee2011-05-26 01:26:41 +01001168 global_context()->set_data(heap->undefined_value());
Steve Blocka7e24c12009-10-30 11:49:00 +00001169}
1170
1171
Ben Murdoch8b112d22011-06-08 16:22:53 +01001172bool Genesis::CompileBuiltin(Isolate* isolate, int index) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001173 Vector<const char> name = Natives::GetScriptName(index);
Steve Block44f0eee2011-05-26 01:26:41 +01001174 Handle<String> source_code =
Ben Murdoch8b112d22011-06-08 16:22:53 +01001175 isolate->bootstrapper()->NativesSourceLookup(index);
1176 return CompileNative(name, source_code);
1177}
1178
1179
1180bool Genesis::CompileExperimentalBuiltin(Isolate* isolate, int index) {
1181 Vector<const char> name = ExperimentalNatives::GetScriptName(index);
1182 Factory* factory = isolate->factory();
1183 Handle<String> source_code =
1184 factory->NewStringFromAscii(ExperimentalNatives::GetScriptSource(index));
Steve Blocka7e24c12009-10-30 11:49:00 +00001185 return CompileNative(name, source_code);
1186}
1187
1188
1189bool Genesis::CompileNative(Vector<const char> name, Handle<String> source) {
1190 HandleScope scope;
Ben Murdoch8b112d22011-06-08 16:22:53 +01001191 Isolate* isolate = source->GetIsolate();
Steve Blocka7e24c12009-10-30 11:49:00 +00001192#ifdef ENABLE_DEBUGGER_SUPPORT
Steve Block44f0eee2011-05-26 01:26:41 +01001193 isolate->debugger()->set_compiling_natives(true);
Steve Blocka7e24c12009-10-30 11:49:00 +00001194#endif
Andrei Popescu31002712010-02-23 13:46:05 +00001195 bool result = CompileScriptCached(name,
1196 source,
1197 NULL,
1198 NULL,
Steve Block44f0eee2011-05-26 01:26:41 +01001199 Handle<Context>(isolate->context()),
Andrei Popescu31002712010-02-23 13:46:05 +00001200 true);
Steve Block44f0eee2011-05-26 01:26:41 +01001201 ASSERT(isolate->has_pending_exception() != result);
1202 if (!result) isolate->clear_pending_exception();
Steve Blocka7e24c12009-10-30 11:49:00 +00001203#ifdef ENABLE_DEBUGGER_SUPPORT
Steve Block44f0eee2011-05-26 01:26:41 +01001204 isolate->debugger()->set_compiling_natives(false);
Steve Blocka7e24c12009-10-30 11:49:00 +00001205#endif
1206 return result;
1207}
1208
1209
1210bool Genesis::CompileScriptCached(Vector<const char> name,
1211 Handle<String> source,
1212 SourceCodeCache* cache,
1213 v8::Extension* extension,
Andrei Popescu31002712010-02-23 13:46:05 +00001214 Handle<Context> top_context,
Steve Blocka7e24c12009-10-30 11:49:00 +00001215 bool use_runtime_context) {
Ben Murdoch8b112d22011-06-08 16:22:53 +01001216 Factory* factory = source->GetIsolate()->factory();
Steve Blocka7e24c12009-10-30 11:49:00 +00001217 HandleScope scope;
Steve Block6ded16b2010-05-10 14:33:55 +01001218 Handle<SharedFunctionInfo> function_info;
Steve Blocka7e24c12009-10-30 11:49:00 +00001219
1220 // If we can't find the function in the cache, we compile a new
1221 // function and insert it into the cache.
Steve Block6ded16b2010-05-10 14:33:55 +01001222 if (cache == NULL || !cache->Lookup(name, &function_info)) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001223 ASSERT(source->IsAsciiRepresentation());
Steve Block44f0eee2011-05-26 01:26:41 +01001224 Handle<String> script_name = factory->NewStringFromUtf8(name);
Steve Block6ded16b2010-05-10 14:33:55 +01001225 function_info = Compiler::Compile(
Andrei Popescu31002712010-02-23 13:46:05 +00001226 source,
1227 script_name,
1228 0,
1229 0,
1230 extension,
1231 NULL,
Andrei Popescu402d9372010-02-26 13:31:12 +00001232 Handle<String>::null(),
Andrei Popescu31002712010-02-23 13:46:05 +00001233 use_runtime_context ? NATIVES_CODE : NOT_NATIVES_CODE);
Steve Block6ded16b2010-05-10 14:33:55 +01001234 if (function_info.is_null()) return false;
1235 if (cache != NULL) cache->Add(name, function_info);
Steve Blocka7e24c12009-10-30 11:49:00 +00001236 }
1237
1238 // Setup the function context. Conceptually, we should clone the
1239 // function before overwriting the context but since we're in a
1240 // single-threaded environment it is not strictly necessary.
Andrei Popescu31002712010-02-23 13:46:05 +00001241 ASSERT(top_context->IsGlobalContext());
Steve Blocka7e24c12009-10-30 11:49:00 +00001242 Handle<Context> context =
1243 Handle<Context>(use_runtime_context
Andrei Popescu31002712010-02-23 13:46:05 +00001244 ? Handle<Context>(top_context->runtime_context())
1245 : top_context);
Steve Blocka7e24c12009-10-30 11:49:00 +00001246 Handle<JSFunction> fun =
Steve Block44f0eee2011-05-26 01:26:41 +01001247 factory->NewFunctionFromSharedFunctionInfo(function_info, context);
Steve Blocka7e24c12009-10-30 11:49:00 +00001248
Leon Clarke4515c472010-02-03 11:58:03 +00001249 // Call function using either the runtime object or the global
Steve Blocka7e24c12009-10-30 11:49:00 +00001250 // object as the receiver. Provide no parameters.
1251 Handle<Object> receiver =
1252 Handle<Object>(use_runtime_context
Andrei Popescu31002712010-02-23 13:46:05 +00001253 ? top_context->builtins()
1254 : top_context->global());
Steve Blocka7e24c12009-10-30 11:49:00 +00001255 bool has_pending_exception;
1256 Handle<Object> result =
1257 Execution::Call(fun, receiver, 0, NULL, &has_pending_exception);
1258 if (has_pending_exception) return false;
Andrei Popescu402d9372010-02-26 13:31:12 +00001259 return true;
Steve Blocka7e24c12009-10-30 11:49:00 +00001260}
1261
1262
Ben Murdoch8b112d22011-06-08 16:22:53 +01001263#define INSTALL_NATIVE(Type, name, var) \
1264 Handle<String> var##_name = factory()->LookupAsciiSymbol(name); \
1265 Object* var##_native = \
1266 global_context()->builtins()->GetPropertyNoExceptionThrown( \
1267 *var##_name); \
1268 global_context()->set_##var(Type::cast(var##_native));
Steve Blocka7e24c12009-10-30 11:49:00 +00001269
Steve Block44f0eee2011-05-26 01:26:41 +01001270
Steve Blocka7e24c12009-10-30 11:49:00 +00001271void Genesis::InstallNativeFunctions() {
1272 HandleScope scope;
1273 INSTALL_NATIVE(JSFunction, "CreateDate", create_date_fun);
1274 INSTALL_NATIVE(JSFunction, "ToNumber", to_number_fun);
1275 INSTALL_NATIVE(JSFunction, "ToString", to_string_fun);
1276 INSTALL_NATIVE(JSFunction, "ToDetailString", to_detail_string_fun);
1277 INSTALL_NATIVE(JSFunction, "ToObject", to_object_fun);
1278 INSTALL_NATIVE(JSFunction, "ToInteger", to_integer_fun);
1279 INSTALL_NATIVE(JSFunction, "ToUint32", to_uint32_fun);
1280 INSTALL_NATIVE(JSFunction, "ToInt32", to_int32_fun);
Leon Clarkee46be812010-01-19 14:06:41 +00001281 INSTALL_NATIVE(JSFunction, "GlobalEval", global_eval_fun);
Steve Blocka7e24c12009-10-30 11:49:00 +00001282 INSTALL_NATIVE(JSFunction, "Instantiate", instantiate_fun);
1283 INSTALL_NATIVE(JSFunction, "ConfigureTemplateInstance",
1284 configure_instance_fun);
Steve Blocka7e24c12009-10-30 11:49:00 +00001285 INSTALL_NATIVE(JSFunction, "GetStackTraceLine", get_stack_trace_line_fun);
1286 INSTALL_NATIVE(JSObject, "functionCache", function_cache);
1287}
1288
1289#undef INSTALL_NATIVE
1290
1291
1292bool Genesis::InstallNatives() {
1293 HandleScope scope;
1294
1295 // Create a function for the builtins object. Allocate space for the
1296 // JavaScript builtins, a reference to the builtins object
1297 // (itself) and a reference to the global_context directly in the object.
Steve Block44f0eee2011-05-26 01:26:41 +01001298 Handle<Code> code = Handle<Code>(
Ben Murdoch8b112d22011-06-08 16:22:53 +01001299 isolate()->builtins()->builtin(Builtins::kIllegal));
Steve Blocka7e24c12009-10-30 11:49:00 +00001300 Handle<JSFunction> builtins_fun =
Ben Murdoch8b112d22011-06-08 16:22:53 +01001301 factory()->NewFunction(factory()->empty_symbol(),
1302 JS_BUILTINS_OBJECT_TYPE,
1303 JSBuiltinsObject::kSize, code, true);
Steve Blocka7e24c12009-10-30 11:49:00 +00001304
Ben Murdoch8b112d22011-06-08 16:22:53 +01001305 Handle<String> name = factory()->LookupAsciiSymbol("builtins");
Steve Blocka7e24c12009-10-30 11:49:00 +00001306 builtins_fun->shared()->set_instance_class_name(*name);
1307
1308 // Allocate the builtins object.
1309 Handle<JSBuiltinsObject> builtins =
Ben Murdoch8b112d22011-06-08 16:22:53 +01001310 Handle<JSBuiltinsObject>::cast(factory()->NewGlobalObject(builtins_fun));
Steve Blocka7e24c12009-10-30 11:49:00 +00001311 builtins->set_builtins(*builtins);
1312 builtins->set_global_context(*global_context());
1313 builtins->set_global_receiver(*builtins);
1314
1315 // Setup the 'global' properties of the builtins object. The
1316 // 'global' property that refers to the global object is the only
1317 // way to get from code running in the builtins context to the
1318 // global object.
1319 static const PropertyAttributes attributes =
1320 static_cast<PropertyAttributes>(READ_ONLY | DONT_DELETE);
Ben Murdoch8b112d22011-06-08 16:22:53 +01001321 Handle<String> global_symbol = factory()->LookupAsciiSymbol("global");
Steve Block1e0659c2011-05-24 12:43:12 +01001322 Handle<Object> global_obj(global_context()->global());
1323 SetLocalPropertyNoThrow(builtins, global_symbol, global_obj, attributes);
Steve Blocka7e24c12009-10-30 11:49:00 +00001324
1325 // Setup the reference from the global object to the builtins object.
1326 JSGlobalObject::cast(global_context()->global())->set_builtins(*builtins);
1327
1328 // Create a bridge function that has context in the global context.
1329 Handle<JSFunction> bridge =
Ben Murdoch8b112d22011-06-08 16:22:53 +01001330 factory()->NewFunction(factory()->empty_symbol(),
1331 factory()->undefined_value());
1332 ASSERT(bridge->context() == *isolate()->global_context());
Steve Blocka7e24c12009-10-30 11:49:00 +00001333
1334 // Allocate the builtins context.
1335 Handle<Context> context =
Ben Murdoch8b112d22011-06-08 16:22:53 +01001336 factory()->NewFunctionContext(Context::MIN_CONTEXT_SLOTS, bridge);
Steve Blocka7e24c12009-10-30 11:49:00 +00001337 context->set_global(*builtins); // override builtins global object
1338
1339 global_context()->set_runtime_context(*context);
1340
1341 { // -- S c r i p t
1342 // Builtin functions for Script.
1343 Handle<JSFunction> script_fun =
1344 InstallFunction(builtins, "Script", JS_VALUE_TYPE, JSValue::kSize,
Ben Murdoch8b112d22011-06-08 16:22:53 +01001345 isolate()->initial_object_prototype(),
Steve Block44f0eee2011-05-26 01:26:41 +01001346 Builtins::kIllegal, false);
Steve Blocka7e24c12009-10-30 11:49:00 +00001347 Handle<JSObject> prototype =
Ben Murdoch8b112d22011-06-08 16:22:53 +01001348 factory()->NewJSObject(isolate()->object_function(), TENURED);
Steve Blocka7e24c12009-10-30 11:49:00 +00001349 SetPrototype(script_fun, prototype);
1350 global_context()->set_script_function(*script_fun);
1351
1352 // Add 'source' and 'data' property to scripts.
1353 PropertyAttributes common_attributes =
1354 static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE | READ_ONLY);
Ben Murdoch8b112d22011-06-08 16:22:53 +01001355 Handle<Proxy> proxy_source = factory()->NewProxy(&Accessors::ScriptSource);
Steve Blocka7e24c12009-10-30 11:49:00 +00001356 Handle<DescriptorArray> script_descriptors =
Ben Murdoch8b112d22011-06-08 16:22:53 +01001357 factory()->CopyAppendProxyDescriptor(
1358 factory()->empty_descriptor_array(),
1359 factory()->LookupAsciiSymbol("source"),
Steve Blocka7e24c12009-10-30 11:49:00 +00001360 proxy_source,
1361 common_attributes);
Ben Murdoch8b112d22011-06-08 16:22:53 +01001362 Handle<Proxy> proxy_name = factory()->NewProxy(&Accessors::ScriptName);
Steve Blocka7e24c12009-10-30 11:49:00 +00001363 script_descriptors =
Ben Murdoch8b112d22011-06-08 16:22:53 +01001364 factory()->CopyAppendProxyDescriptor(
Steve Blocka7e24c12009-10-30 11:49:00 +00001365 script_descriptors,
Ben Murdoch8b112d22011-06-08 16:22:53 +01001366 factory()->LookupAsciiSymbol("name"),
Steve Blocka7e24c12009-10-30 11:49:00 +00001367 proxy_name,
1368 common_attributes);
Ben Murdoch8b112d22011-06-08 16:22:53 +01001369 Handle<Proxy> proxy_id = factory()->NewProxy(&Accessors::ScriptId);
Steve Blocka7e24c12009-10-30 11:49:00 +00001370 script_descriptors =
Ben Murdoch8b112d22011-06-08 16:22:53 +01001371 factory()->CopyAppendProxyDescriptor(
Steve Blocka7e24c12009-10-30 11:49:00 +00001372 script_descriptors,
Ben Murdoch8b112d22011-06-08 16:22:53 +01001373 factory()->LookupAsciiSymbol("id"),
Steve Blocka7e24c12009-10-30 11:49:00 +00001374 proxy_id,
1375 common_attributes);
1376 Handle<Proxy> proxy_line_offset =
Ben Murdoch8b112d22011-06-08 16:22:53 +01001377 factory()->NewProxy(&Accessors::ScriptLineOffset);
Steve Blocka7e24c12009-10-30 11:49:00 +00001378 script_descriptors =
Ben Murdoch8b112d22011-06-08 16:22:53 +01001379 factory()->CopyAppendProxyDescriptor(
Steve Blocka7e24c12009-10-30 11:49:00 +00001380 script_descriptors,
Ben Murdoch8b112d22011-06-08 16:22:53 +01001381 factory()->LookupAsciiSymbol("line_offset"),
Steve Blocka7e24c12009-10-30 11:49:00 +00001382 proxy_line_offset,
1383 common_attributes);
1384 Handle<Proxy> proxy_column_offset =
Ben Murdoch8b112d22011-06-08 16:22:53 +01001385 factory()->NewProxy(&Accessors::ScriptColumnOffset);
Steve Blocka7e24c12009-10-30 11:49:00 +00001386 script_descriptors =
Ben Murdoch8b112d22011-06-08 16:22:53 +01001387 factory()->CopyAppendProxyDescriptor(
Steve Blocka7e24c12009-10-30 11:49:00 +00001388 script_descriptors,
Ben Murdoch8b112d22011-06-08 16:22:53 +01001389 factory()->LookupAsciiSymbol("column_offset"),
Steve Blocka7e24c12009-10-30 11:49:00 +00001390 proxy_column_offset,
1391 common_attributes);
Ben Murdoch8b112d22011-06-08 16:22:53 +01001392 Handle<Proxy> proxy_data = factory()->NewProxy(&Accessors::ScriptData);
Steve Blocka7e24c12009-10-30 11:49:00 +00001393 script_descriptors =
Ben Murdoch8b112d22011-06-08 16:22:53 +01001394 factory()->CopyAppendProxyDescriptor(
Steve Blocka7e24c12009-10-30 11:49:00 +00001395 script_descriptors,
Ben Murdoch8b112d22011-06-08 16:22:53 +01001396 factory()->LookupAsciiSymbol("data"),
Steve Blocka7e24c12009-10-30 11:49:00 +00001397 proxy_data,
1398 common_attributes);
Ben Murdoch8b112d22011-06-08 16:22:53 +01001399 Handle<Proxy> proxy_type = factory()->NewProxy(&Accessors::ScriptType);
Steve Blocka7e24c12009-10-30 11:49:00 +00001400 script_descriptors =
Ben Murdoch8b112d22011-06-08 16:22:53 +01001401 factory()->CopyAppendProxyDescriptor(
Steve Blocka7e24c12009-10-30 11:49:00 +00001402 script_descriptors,
Ben Murdoch8b112d22011-06-08 16:22:53 +01001403 factory()->LookupAsciiSymbol("type"),
Steve Blocka7e24c12009-10-30 11:49:00 +00001404 proxy_type,
1405 common_attributes);
1406 Handle<Proxy> proxy_compilation_type =
Ben Murdoch8b112d22011-06-08 16:22:53 +01001407 factory()->NewProxy(&Accessors::ScriptCompilationType);
Steve Blocka7e24c12009-10-30 11:49:00 +00001408 script_descriptors =
Ben Murdoch8b112d22011-06-08 16:22:53 +01001409 factory()->CopyAppendProxyDescriptor(
Steve Blocka7e24c12009-10-30 11:49:00 +00001410 script_descriptors,
Ben Murdoch8b112d22011-06-08 16:22:53 +01001411 factory()->LookupAsciiSymbol("compilation_type"),
Steve Blocka7e24c12009-10-30 11:49:00 +00001412 proxy_compilation_type,
1413 common_attributes);
1414 Handle<Proxy> proxy_line_ends =
Ben Murdoch8b112d22011-06-08 16:22:53 +01001415 factory()->NewProxy(&Accessors::ScriptLineEnds);
Steve Blocka7e24c12009-10-30 11:49:00 +00001416 script_descriptors =
Ben Murdoch8b112d22011-06-08 16:22:53 +01001417 factory()->CopyAppendProxyDescriptor(
Steve Blocka7e24c12009-10-30 11:49:00 +00001418 script_descriptors,
Ben Murdoch8b112d22011-06-08 16:22:53 +01001419 factory()->LookupAsciiSymbol("line_ends"),
Steve Blocka7e24c12009-10-30 11:49:00 +00001420 proxy_line_ends,
1421 common_attributes);
1422 Handle<Proxy> proxy_context_data =
Ben Murdoch8b112d22011-06-08 16:22:53 +01001423 factory()->NewProxy(&Accessors::ScriptContextData);
Steve Blocka7e24c12009-10-30 11:49:00 +00001424 script_descriptors =
Ben Murdoch8b112d22011-06-08 16:22:53 +01001425 factory()->CopyAppendProxyDescriptor(
Steve Blocka7e24c12009-10-30 11:49:00 +00001426 script_descriptors,
Ben Murdoch8b112d22011-06-08 16:22:53 +01001427 factory()->LookupAsciiSymbol("context_data"),
Steve Blocka7e24c12009-10-30 11:49:00 +00001428 proxy_context_data,
1429 common_attributes);
Steve Blockd0582a62009-12-15 09:54:21 +00001430 Handle<Proxy> proxy_eval_from_script =
Ben Murdoch8b112d22011-06-08 16:22:53 +01001431 factory()->NewProxy(&Accessors::ScriptEvalFromScript);
Steve Blocka7e24c12009-10-30 11:49:00 +00001432 script_descriptors =
Ben Murdoch8b112d22011-06-08 16:22:53 +01001433 factory()->CopyAppendProxyDescriptor(
Steve Blocka7e24c12009-10-30 11:49:00 +00001434 script_descriptors,
Ben Murdoch8b112d22011-06-08 16:22:53 +01001435 factory()->LookupAsciiSymbol("eval_from_script"),
Steve Blockd0582a62009-12-15 09:54:21 +00001436 proxy_eval_from_script,
Steve Blocka7e24c12009-10-30 11:49:00 +00001437 common_attributes);
Steve Blockd0582a62009-12-15 09:54:21 +00001438 Handle<Proxy> proxy_eval_from_script_position =
Ben Murdoch8b112d22011-06-08 16:22:53 +01001439 factory()->NewProxy(&Accessors::ScriptEvalFromScriptPosition);
Steve Blocka7e24c12009-10-30 11:49:00 +00001440 script_descriptors =
Ben Murdoch8b112d22011-06-08 16:22:53 +01001441 factory()->CopyAppendProxyDescriptor(
Steve Blocka7e24c12009-10-30 11:49:00 +00001442 script_descriptors,
Ben Murdoch8b112d22011-06-08 16:22:53 +01001443 factory()->LookupAsciiSymbol("eval_from_script_position"),
Steve Blockd0582a62009-12-15 09:54:21 +00001444 proxy_eval_from_script_position,
1445 common_attributes);
1446 Handle<Proxy> proxy_eval_from_function_name =
Ben Murdoch8b112d22011-06-08 16:22:53 +01001447 factory()->NewProxy(&Accessors::ScriptEvalFromFunctionName);
Steve Blockd0582a62009-12-15 09:54:21 +00001448 script_descriptors =
Ben Murdoch8b112d22011-06-08 16:22:53 +01001449 factory()->CopyAppendProxyDescriptor(
Steve Blockd0582a62009-12-15 09:54:21 +00001450 script_descriptors,
Ben Murdoch8b112d22011-06-08 16:22:53 +01001451 factory()->LookupAsciiSymbol("eval_from_function_name"),
Steve Blockd0582a62009-12-15 09:54:21 +00001452 proxy_eval_from_function_name,
Steve Blocka7e24c12009-10-30 11:49:00 +00001453 common_attributes);
1454
1455 Handle<Map> script_map = Handle<Map>(script_fun->initial_map());
1456 script_map->set_instance_descriptors(*script_descriptors);
1457
1458 // Allocate the empty script.
Ben Murdoch8b112d22011-06-08 16:22:53 +01001459 Handle<Script> script = factory()->NewScript(factory()->empty_string());
Steve Blocka7e24c12009-10-30 11:49:00 +00001460 script->set_type(Smi::FromInt(Script::TYPE_NATIVE));
Ben Murdoch8b112d22011-06-08 16:22:53 +01001461 heap()->public_set_empty_script(*script);
Steve Blocka7e24c12009-10-30 11:49:00 +00001462 }
Steve Block6ded16b2010-05-10 14:33:55 +01001463 {
1464 // Builtin function for OpaqueReference -- a JSValue-based object,
1465 // that keeps its field isolated from JavaScript code. It may store
1466 // objects, that JavaScript code may not access.
1467 Handle<JSFunction> opaque_reference_fun =
1468 InstallFunction(builtins, "OpaqueReference", JS_VALUE_TYPE,
Steve Block44f0eee2011-05-26 01:26:41 +01001469 JSValue::kSize,
Ben Murdoch8b112d22011-06-08 16:22:53 +01001470 isolate()->initial_object_prototype(),
Steve Block44f0eee2011-05-26 01:26:41 +01001471 Builtins::kIllegal, false);
Steve Block6ded16b2010-05-10 14:33:55 +01001472 Handle<JSObject> prototype =
Ben Murdoch8b112d22011-06-08 16:22:53 +01001473 factory()->NewJSObject(isolate()->object_function(), TENURED);
Steve Block6ded16b2010-05-10 14:33:55 +01001474 SetPrototype(opaque_reference_fun, prototype);
1475 global_context()->set_opaque_reference_function(*opaque_reference_fun);
1476 }
1477
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001478 { // --- I n t e r n a l A r r a y ---
1479 // An array constructor on the builtins object that works like
1480 // the public Array constructor, except that its prototype
1481 // doesn't inherit from Object.prototype.
1482 // To be used only for internal work by builtins. Instances
1483 // must not be leaked to user code.
1484 // Only works correctly when called as a constructor. The normal
1485 // Array code uses Array.prototype as prototype when called as
1486 // a function.
1487 Handle<JSFunction> array_function =
1488 InstallFunction(builtins,
1489 "InternalArray",
1490 JS_ARRAY_TYPE,
1491 JSArray::kSize,
Ben Murdoch8b112d22011-06-08 16:22:53 +01001492 isolate()->initial_object_prototype(),
Steve Block44f0eee2011-05-26 01:26:41 +01001493 Builtins::kArrayCode,
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001494 true);
1495 Handle<JSObject> prototype =
Ben Murdoch8b112d22011-06-08 16:22:53 +01001496 factory()->NewJSObject(isolate()->object_function(), TENURED);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001497 SetPrototype(array_function, prototype);
1498
1499 array_function->shared()->set_construct_stub(
Ben Murdoch8b112d22011-06-08 16:22:53 +01001500 isolate()->builtins()->builtin(Builtins::kArrayConstructCode));
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001501 array_function->shared()->DontAdaptArguments();
1502
1503 // Make "length" magic on instances.
1504 Handle<DescriptorArray> array_descriptors =
Ben Murdoch8b112d22011-06-08 16:22:53 +01001505 factory()->CopyAppendProxyDescriptor(
1506 factory()->empty_descriptor_array(),
1507 factory()->length_symbol(),
1508 factory()->NewProxy(&Accessors::ArrayLength),
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001509 static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE));
1510
1511 array_function->initial_map()->set_instance_descriptors(
1512 *array_descriptors);
1513 }
1514
Steve Block6ded16b2010-05-10 14:33:55 +01001515 if (FLAG_disable_native_files) {
1516 PrintF("Warning: Running without installed natives!\n");
1517 return true;
1518 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001519
Andrei Popescu31002712010-02-23 13:46:05 +00001520 // Install natives.
1521 for (int i = Natives::GetDebuggerCount();
1522 i < Natives::GetBuiltinsCount();
1523 i++) {
Ben Murdoch8b112d22011-06-08 16:22:53 +01001524 if (!CompileBuiltin(isolate(), i)) return false;
Andrei Popescu402d9372010-02-26 13:31:12 +00001525 // TODO(ager): We really only need to install the JS builtin
1526 // functions on the builtins object after compiling and running
1527 // runtime.js.
1528 if (!InstallJSBuiltins(builtins)) return false;
Steve Blocka7e24c12009-10-30 11:49:00 +00001529 }
1530
1531 InstallNativeFunctions();
1532
Iain Merrick75681382010-08-19 15:07:18 +01001533 // Store the map for the string prototype after the natives has been compiled
1534 // and the String function has been setup.
1535 Handle<JSFunction> string_function(global_context()->string_function());
1536 ASSERT(JSObject::cast(
1537 string_function->initial_map()->prototype())->HasFastProperties());
1538 global_context()->set_string_function_prototype_map(
1539 HeapObject::cast(string_function->initial_map()->prototype())->map());
1540
Ben Murdochb0fe1622011-05-05 13:52:32 +01001541 InstallBuiltinFunctionIds();
Kristian Monsen25f61362010-05-21 11:50:48 +01001542
Steve Blocka7e24c12009-10-30 11:49:00 +00001543 // Install Function.prototype.call and apply.
Ben Murdoch8b112d22011-06-08 16:22:53 +01001544 { Handle<String> key = factory()->function_class_symbol();
Steve Blocka7e24c12009-10-30 11:49:00 +00001545 Handle<JSFunction> function =
Ben Murdoch8b112d22011-06-08 16:22:53 +01001546 Handle<JSFunction>::cast(GetProperty(isolate()->global(), key));
Steve Blocka7e24c12009-10-30 11:49:00 +00001547 Handle<JSObject> proto =
1548 Handle<JSObject>(JSObject::cast(function->instance_prototype()));
1549
1550 // Install the call and the apply functions.
1551 Handle<JSFunction> call =
1552 InstallFunction(proto, "call", JS_OBJECT_TYPE, JSObject::kHeaderSize,
Steve Block6ded16b2010-05-10 14:33:55 +01001553 Handle<JSObject>::null(),
Steve Block44f0eee2011-05-26 01:26:41 +01001554 Builtins::kFunctionCall,
Steve Blocka7e24c12009-10-30 11:49:00 +00001555 false);
1556 Handle<JSFunction> apply =
1557 InstallFunction(proto, "apply", JS_OBJECT_TYPE, JSObject::kHeaderSize,
Steve Block6ded16b2010-05-10 14:33:55 +01001558 Handle<JSObject>::null(),
Steve Block44f0eee2011-05-26 01:26:41 +01001559 Builtins::kFunctionApply,
Steve Blocka7e24c12009-10-30 11:49:00 +00001560 false);
1561
1562 // Make sure that Function.prototype.call appears to be compiled.
1563 // The code will never be called, but inline caching for call will
1564 // only work if it appears to be compiled.
1565 call->shared()->DontAdaptArguments();
1566 ASSERT(call->is_compiled());
1567
1568 // Set the expected parameters for apply to 2; required by builtin.
1569 apply->shared()->set_formal_parameter_count(2);
1570
1571 // Set the lengths for the functions to satisfy ECMA-262.
1572 call->shared()->set_length(1);
1573 apply->shared()->set_length(2);
1574 }
1575
Steve Block6ded16b2010-05-10 14:33:55 +01001576 // Create a constructor for RegExp results (a variant of Array that
1577 // predefines the two properties index and match).
1578 {
1579 // RegExpResult initial map.
1580
1581 // Find global.Array.prototype to inherit from.
1582 Handle<JSFunction> array_constructor(global_context()->array_function());
1583 Handle<JSObject> array_prototype(
1584 JSObject::cast(array_constructor->instance_prototype()));
1585
1586 // Add initial map.
1587 Handle<Map> initial_map =
Ben Murdoch8b112d22011-06-08 16:22:53 +01001588 factory()->NewMap(JS_ARRAY_TYPE, JSRegExpResult::kSize);
Steve Block6ded16b2010-05-10 14:33:55 +01001589 initial_map->set_constructor(*array_constructor);
1590
1591 // Set prototype on map.
1592 initial_map->set_non_instance_prototype(false);
1593 initial_map->set_prototype(*array_prototype);
1594
1595 // Update map with length accessor from Array and add "index" and "input".
1596 Handle<Map> array_map(global_context()->js_array_map());
1597 Handle<DescriptorArray> array_descriptors(
1598 array_map->instance_descriptors());
1599 ASSERT_EQ(1, array_descriptors->number_of_descriptors());
1600
1601 Handle<DescriptorArray> reresult_descriptors =
Ben Murdoch8b112d22011-06-08 16:22:53 +01001602 factory()->NewDescriptorArray(3);
Steve Block6ded16b2010-05-10 14:33:55 +01001603
1604 reresult_descriptors->CopyFrom(0, *array_descriptors, 0);
1605
1606 int enum_index = 0;
1607 {
Ben Murdoch8b112d22011-06-08 16:22:53 +01001608 FieldDescriptor index_field(heap()->index_symbol(),
Steve Block6ded16b2010-05-10 14:33:55 +01001609 JSRegExpResult::kIndexIndex,
1610 NONE,
1611 enum_index++);
1612 reresult_descriptors->Set(1, &index_field);
1613 }
1614
1615 {
Ben Murdoch8b112d22011-06-08 16:22:53 +01001616 FieldDescriptor input_field(heap()->input_symbol(),
Steve Block6ded16b2010-05-10 14:33:55 +01001617 JSRegExpResult::kInputIndex,
1618 NONE,
1619 enum_index++);
1620 reresult_descriptors->Set(2, &input_field);
1621 }
1622 reresult_descriptors->Sort();
1623
1624 initial_map->set_inobject_properties(2);
1625 initial_map->set_pre_allocated_property_fields(2);
1626 initial_map->set_unused_property_fields(0);
1627 initial_map->set_instance_descriptors(*reresult_descriptors);
1628
1629 global_context()->set_regexp_result_map(*initial_map);
1630 }
1631
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001632
Steve Blocka7e24c12009-10-30 11:49:00 +00001633#ifdef DEBUG
1634 builtins->Verify();
1635#endif
Andrei Popescu31002712010-02-23 13:46:05 +00001636
Steve Blocka7e24c12009-10-30 11:49:00 +00001637 return true;
1638}
1639
1640
Ben Murdoch8b112d22011-06-08 16:22:53 +01001641bool Genesis::InstallExperimentalNatives() {
1642 if (FLAG_harmony_proxies) {
1643 for (int i = ExperimentalNatives::GetDebuggerCount();
1644 i < ExperimentalNatives::GetBuiltinsCount();
1645 i++) {
1646 if (!CompileExperimentalBuiltin(isolate(), i)) return false;
1647 }
1648 }
1649 return true;
1650}
1651
1652
Ben Murdochb0fe1622011-05-05 13:52:32 +01001653static Handle<JSObject> ResolveBuiltinIdHolder(
Kristian Monsen0d5e1162010-09-30 15:31:59 +01001654 Handle<Context> global_context,
1655 const char* holder_expr) {
Ben Murdoch8b112d22011-06-08 16:22:53 +01001656 Factory* factory = global_context->GetIsolate()->factory();
Kristian Monsen0d5e1162010-09-30 15:31:59 +01001657 Handle<GlobalObject> global(global_context->global());
1658 const char* period_pos = strchr(holder_expr, '.');
1659 if (period_pos == NULL) {
1660 return Handle<JSObject>::cast(
Steve Block44f0eee2011-05-26 01:26:41 +01001661 GetProperty(global, factory->LookupAsciiSymbol(holder_expr)));
Steve Block59151502010-09-22 15:07:15 +01001662 }
Kristian Monsen0d5e1162010-09-30 15:31:59 +01001663 ASSERT_EQ(".prototype", period_pos);
1664 Vector<const char> property(holder_expr,
1665 static_cast<int>(period_pos - holder_expr));
1666 Handle<JSFunction> function = Handle<JSFunction>::cast(
Steve Block44f0eee2011-05-26 01:26:41 +01001667 GetProperty(global, factory->LookupSymbol(property)));
Kristian Monsen0d5e1162010-09-30 15:31:59 +01001668 return Handle<JSObject>(JSObject::cast(function->prototype()));
1669}
1670
1671
Ben Murdochb0fe1622011-05-05 13:52:32 +01001672static void InstallBuiltinFunctionId(Handle<JSObject> holder,
1673 const char* function_name,
1674 BuiltinFunctionId id) {
Ben Murdoch8b112d22011-06-08 16:22:53 +01001675 Factory* factory = holder->GetIsolate()->factory();
1676 Handle<String> name = factory->LookupAsciiSymbol(function_name);
John Reck59135872010-11-02 12:39:01 -07001677 Object* function_object = holder->GetProperty(*name)->ToObjectUnchecked();
1678 Handle<JSFunction> function(JSFunction::cast(function_object));
Kristian Monsen25f61362010-05-21 11:50:48 +01001679 function->shared()->set_function_data(Smi::FromInt(id));
1680}
1681
1682
Ben Murdochb0fe1622011-05-05 13:52:32 +01001683void Genesis::InstallBuiltinFunctionIds() {
Kristian Monsen25f61362010-05-21 11:50:48 +01001684 HandleScope scope;
Ben Murdochb0fe1622011-05-05 13:52:32 +01001685#define INSTALL_BUILTIN_ID(holder_expr, fun_name, name) \
1686 { \
1687 Handle<JSObject> holder = ResolveBuiltinIdHolder( \
1688 global_context(), #holder_expr); \
1689 BuiltinFunctionId id = k##name; \
1690 InstallBuiltinFunctionId(holder, #fun_name, id); \
Kristian Monsen25f61362010-05-21 11:50:48 +01001691 }
Ben Murdochb0fe1622011-05-05 13:52:32 +01001692 FUNCTIONS_WITH_ID_LIST(INSTALL_BUILTIN_ID)
1693#undef INSTALL_BUILTIN_ID
Kristian Monsen25f61362010-05-21 11:50:48 +01001694}
1695
1696
Steve Block6ded16b2010-05-10 14:33:55 +01001697// Do not forget to update macros.py with named constant
1698// of cache id.
1699#define JSFUNCTION_RESULT_CACHE_LIST(F) \
1700 F(16, global_context()->regexp_function())
1701
1702
Ben Murdoch8b112d22011-06-08 16:22:53 +01001703static FixedArray* CreateCache(int size, JSFunction* factory_function) {
1704 Factory* factory = factory_function->GetIsolate()->factory();
Steve Block6ded16b2010-05-10 14:33:55 +01001705 // Caches are supposed to live for a long time, allocate in old space.
1706 int array_size = JSFunctionResultCache::kEntriesIndex + 2 * size;
1707 // Cannot use cast as object is not fully initialized yet.
1708 JSFunctionResultCache* cache = reinterpret_cast<JSFunctionResultCache*>(
Ben Murdoch8b112d22011-06-08 16:22:53 +01001709 *factory->NewFixedArrayWithHoles(array_size, TENURED));
1710 cache->set(JSFunctionResultCache::kFactoryIndex, factory_function);
Steve Block6ded16b2010-05-10 14:33:55 +01001711 cache->MakeZeroSize();
1712 return cache;
1713}
1714
1715
1716void Genesis::InstallJSFunctionResultCaches() {
1717 const int kNumberOfCaches = 0 +
1718#define F(size, func) + 1
1719 JSFUNCTION_RESULT_CACHE_LIST(F)
1720#undef F
1721 ;
1722
Steve Block44f0eee2011-05-26 01:26:41 +01001723 Handle<FixedArray> caches = FACTORY->NewFixedArray(kNumberOfCaches, TENURED);
Steve Block6ded16b2010-05-10 14:33:55 +01001724
1725 int index = 0;
Kristian Monsen0d5e1162010-09-30 15:31:59 +01001726
1727#define F(size, func) do { \
1728 FixedArray* cache = CreateCache((size), (func)); \
1729 caches->set(index++, cache); \
1730 } while (false)
1731
1732 JSFUNCTION_RESULT_CACHE_LIST(F);
1733
Steve Block6ded16b2010-05-10 14:33:55 +01001734#undef F
1735
1736 global_context()->set_jsfunction_result_caches(*caches);
1737}
1738
1739
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001740void Genesis::InitializeNormalizedMapCaches() {
1741 Handle<FixedArray> array(
Steve Block44f0eee2011-05-26 01:26:41 +01001742 FACTORY->NewFixedArray(NormalizedMapCache::kEntries, TENURED));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001743 global_context()->set_normalized_map_cache(NormalizedMapCache::cast(*array));
1744}
1745
1746
Andrei Popescu31002712010-02-23 13:46:05 +00001747bool Bootstrapper::InstallExtensions(Handle<Context> global_context,
1748 v8::ExtensionConfiguration* extensions) {
Ben Murdoch8b112d22011-06-08 16:22:53 +01001749 Isolate* isolate = global_context->GetIsolate();
Andrei Popescu31002712010-02-23 13:46:05 +00001750 BootstrapperActive active;
Steve Block44f0eee2011-05-26 01:26:41 +01001751 SaveContext saved_context(isolate);
1752 isolate->set_context(*global_context);
Andrei Popescu31002712010-02-23 13:46:05 +00001753 if (!Genesis::InstallExtensions(global_context, extensions)) return false;
1754 Genesis::InstallSpecialObjects(global_context);
1755 return true;
1756}
1757
1758
1759void Genesis::InstallSpecialObjects(Handle<Context> global_context) {
Ben Murdoch8b112d22011-06-08 16:22:53 +01001760 Factory* factory = global_context->GetIsolate()->factory();
Steve Blocka7e24c12009-10-30 11:49:00 +00001761 HandleScope scope;
1762 Handle<JSGlobalObject> js_global(
Andrei Popescu31002712010-02-23 13:46:05 +00001763 JSGlobalObject::cast(global_context->global()));
Steve Blocka7e24c12009-10-30 11:49:00 +00001764 // Expose the natives in global if a name for it is specified.
1765 if (FLAG_expose_natives_as != NULL && strlen(FLAG_expose_natives_as) != 0) {
1766 Handle<String> natives_string =
Steve Block44f0eee2011-05-26 01:26:41 +01001767 factory->LookupAsciiSymbol(FLAG_expose_natives_as);
Steve Block1e0659c2011-05-24 12:43:12 +01001768 SetLocalPropertyNoThrow(js_global, natives_string,
1769 Handle<JSObject>(js_global->builtins()), DONT_ENUM);
Steve Blocka7e24c12009-10-30 11:49:00 +00001770 }
1771
1772 Handle<Object> Error = GetProperty(js_global, "Error");
1773 if (Error->IsJSObject()) {
Steve Block44f0eee2011-05-26 01:26:41 +01001774 Handle<String> name = factory->LookupAsciiSymbol("stackTraceLimit");
Steve Block1e0659c2011-05-24 12:43:12 +01001775 SetLocalPropertyNoThrow(Handle<JSObject>::cast(Error),
1776 name,
1777 Handle<Smi>(Smi::FromInt(FLAG_stack_trace_limit)),
1778 NONE);
Steve Blocka7e24c12009-10-30 11:49:00 +00001779 }
1780
1781#ifdef ENABLE_DEBUGGER_SUPPORT
1782 // Expose the debug global object in global if a name for it is specified.
1783 if (FLAG_expose_debug_as != NULL && strlen(FLAG_expose_debug_as) != 0) {
Steve Block44f0eee2011-05-26 01:26:41 +01001784 Debug* debug = Isolate::Current()->debug();
Steve Blocka7e24c12009-10-30 11:49:00 +00001785 // If loading fails we just bail out without installing the
1786 // debugger but without tanking the whole context.
Steve Block44f0eee2011-05-26 01:26:41 +01001787 if (!debug->Load()) return;
Steve Blocka7e24c12009-10-30 11:49:00 +00001788 // Set the security token for the debugger context to the same as
1789 // the shell global context to allow calling between these (otherwise
1790 // exposing debug global object doesn't make much sense).
Steve Block44f0eee2011-05-26 01:26:41 +01001791 debug->debug_context()->set_security_token(
Andrei Popescu31002712010-02-23 13:46:05 +00001792 global_context->security_token());
Steve Blocka7e24c12009-10-30 11:49:00 +00001793
1794 Handle<String> debug_string =
Steve Block44f0eee2011-05-26 01:26:41 +01001795 factory->LookupAsciiSymbol(FLAG_expose_debug_as);
1796 Handle<Object> global_proxy(debug->debug_context()->global_proxy());
Steve Block1e0659c2011-05-24 12:43:12 +01001797 SetLocalPropertyNoThrow(js_global, debug_string, global_proxy, DONT_ENUM);
Steve Blocka7e24c12009-10-30 11:49:00 +00001798 }
1799#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00001800}
1801
1802
Andrei Popescu31002712010-02-23 13:46:05 +00001803bool Genesis::InstallExtensions(Handle<Context> global_context,
1804 v8::ExtensionConfiguration* extensions) {
Steve Block44f0eee2011-05-26 01:26:41 +01001805 // TODO(isolates): Extensions on multiple isolates may take a little more
1806 // effort. (The external API reads 'ignore'-- does that mean
1807 // we can break the interface?)
1808
Steve Blocka7e24c12009-10-30 11:49:00 +00001809 // Clear coloring of extension list
1810 v8::RegisteredExtension* current = v8::RegisteredExtension::first_extension();
1811 while (current != NULL) {
1812 current->set_state(v8::UNVISITED);
1813 current = current->next();
1814 }
Andrei Popescu31002712010-02-23 13:46:05 +00001815 // Install auto extensions.
Steve Blocka7e24c12009-10-30 11:49:00 +00001816 current = v8::RegisteredExtension::first_extension();
1817 while (current != NULL) {
1818 if (current->extension()->auto_enable())
1819 InstallExtension(current);
1820 current = current->next();
1821 }
1822
1823 if (FLAG_expose_gc) InstallExtension("v8/gc");
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01001824 if (FLAG_expose_externalize_string) InstallExtension("v8/externalize");
Steve Blocka7e24c12009-10-30 11:49:00 +00001825
1826 if (extensions == NULL) return true;
1827 // Install required extensions
1828 int count = v8::ImplementationUtilities::GetNameCount(extensions);
1829 const char** names = v8::ImplementationUtilities::GetNames(extensions);
1830 for (int i = 0; i < count; i++) {
1831 if (!InstallExtension(names[i]))
1832 return false;
1833 }
1834
1835 return true;
1836}
1837
1838
1839// Installs a named extension. This methods is unoptimized and does
1840// not scale well if we want to support a large number of extensions.
1841bool Genesis::InstallExtension(const char* name) {
1842 v8::RegisteredExtension* current = v8::RegisteredExtension::first_extension();
1843 // Loop until we find the relevant extension
1844 while (current != NULL) {
1845 if (strcmp(name, current->extension()->name()) == 0) break;
1846 current = current->next();
1847 }
1848 // Didn't find the extension; fail.
1849 if (current == NULL) {
1850 v8::Utils::ReportApiFailure(
1851 "v8::Context::New()", "Cannot find required extension");
1852 return false;
1853 }
1854 return InstallExtension(current);
1855}
1856
1857
1858bool Genesis::InstallExtension(v8::RegisteredExtension* current) {
1859 HandleScope scope;
1860
1861 if (current->state() == v8::INSTALLED) return true;
1862 // The current node has already been visited so there must be a
1863 // cycle in the dependency graph; fail.
1864 if (current->state() == v8::VISITED) {
1865 v8::Utils::ReportApiFailure(
1866 "v8::Context::New()", "Circular extension dependency");
1867 return false;
1868 }
1869 ASSERT(current->state() == v8::UNVISITED);
1870 current->set_state(v8::VISITED);
1871 v8::Extension* extension = current->extension();
1872 // Install the extension's dependencies
1873 for (int i = 0; i < extension->dependency_count(); i++) {
1874 if (!InstallExtension(extension->dependencies()[i])) return false;
1875 }
Steve Block44f0eee2011-05-26 01:26:41 +01001876 Isolate* isolate = Isolate::Current();
Steve Blocka7e24c12009-10-30 11:49:00 +00001877 Vector<const char> source = CStrVector(extension->source());
Steve Block44f0eee2011-05-26 01:26:41 +01001878 Handle<String> source_code = isolate->factory()->NewStringFromAscii(source);
Steve Blocka7e24c12009-10-30 11:49:00 +00001879 bool result = CompileScriptCached(CStrVector(extension->name()),
1880 source_code,
Steve Block44f0eee2011-05-26 01:26:41 +01001881 isolate->bootstrapper()->extensions_cache(),
Andrei Popescu31002712010-02-23 13:46:05 +00001882 extension,
Steve Block44f0eee2011-05-26 01:26:41 +01001883 Handle<Context>(isolate->context()),
Steve Blocka7e24c12009-10-30 11:49:00 +00001884 false);
Steve Block44f0eee2011-05-26 01:26:41 +01001885 ASSERT(isolate->has_pending_exception() != result);
Steve Blocka7e24c12009-10-30 11:49:00 +00001886 if (!result) {
Steve Block44f0eee2011-05-26 01:26:41 +01001887 isolate->clear_pending_exception();
Steve Blocka7e24c12009-10-30 11:49:00 +00001888 }
1889 current->set_state(v8::INSTALLED);
1890 return result;
1891}
1892
1893
Andrei Popescu402d9372010-02-26 13:31:12 +00001894bool Genesis::InstallJSBuiltins(Handle<JSBuiltinsObject> builtins) {
1895 HandleScope scope;
Ben Murdoch8b112d22011-06-08 16:22:53 +01001896 Factory* factory = builtins->GetIsolate()->factory();
Andrei Popescu402d9372010-02-26 13:31:12 +00001897 for (int i = 0; i < Builtins::NumberOfJavaScriptBuiltins(); i++) {
1898 Builtins::JavaScript id = static_cast<Builtins::JavaScript>(i);
Ben Murdoch8b112d22011-06-08 16:22:53 +01001899 Handle<String> name = factory->LookupAsciiSymbol(Builtins::GetName(id));
John Reck59135872010-11-02 12:39:01 -07001900 Object* function_object = builtins->GetPropertyNoExceptionThrown(*name);
Andrei Popescu402d9372010-02-26 13:31:12 +00001901 Handle<JSFunction> function
John Reck59135872010-11-02 12:39:01 -07001902 = Handle<JSFunction>(JSFunction::cast(function_object));
Andrei Popescu402d9372010-02-26 13:31:12 +00001903 builtins->set_javascript_builtin(id, *function);
1904 Handle<SharedFunctionInfo> shared
1905 = Handle<SharedFunctionInfo>(function->shared());
1906 if (!EnsureCompiled(shared, CLEAR_EXCEPTION)) return false;
Iain Merrick75681382010-08-19 15:07:18 +01001907 // Set the code object on the function object.
Ben Murdochb0fe1622011-05-05 13:52:32 +01001908 function->ReplaceCode(function->shared()->code());
Steve Block6ded16b2010-05-10 14:33:55 +01001909 builtins->set_javascript_builtin_code(id, shared->code());
Andrei Popescu402d9372010-02-26 13:31:12 +00001910 }
1911 return true;
Andrei Popescu31002712010-02-23 13:46:05 +00001912}
1913
1914
Steve Blocka7e24c12009-10-30 11:49:00 +00001915bool Genesis::ConfigureGlobalObjects(
1916 v8::Handle<v8::ObjectTemplate> global_proxy_template) {
1917 Handle<JSObject> global_proxy(
1918 JSObject::cast(global_context()->global_proxy()));
Andrei Popescu402d9372010-02-26 13:31:12 +00001919 Handle<JSObject> inner_global(JSObject::cast(global_context()->global()));
Steve Blocka7e24c12009-10-30 11:49:00 +00001920
1921 if (!global_proxy_template.IsEmpty()) {
1922 // Configure the global proxy object.
1923 Handle<ObjectTemplateInfo> proxy_data =
1924 v8::Utils::OpenHandle(*global_proxy_template);
1925 if (!ConfigureApiObject(global_proxy, proxy_data)) return false;
1926
1927 // Configure the inner global object.
1928 Handle<FunctionTemplateInfo> proxy_constructor(
1929 FunctionTemplateInfo::cast(proxy_data->constructor()));
1930 if (!proxy_constructor->prototype_template()->IsUndefined()) {
1931 Handle<ObjectTemplateInfo> inner_data(
1932 ObjectTemplateInfo::cast(proxy_constructor->prototype_template()));
Andrei Popescu402d9372010-02-26 13:31:12 +00001933 if (!ConfigureApiObject(inner_global, inner_data)) return false;
Steve Blocka7e24c12009-10-30 11:49:00 +00001934 }
1935 }
1936
Andrei Popescu402d9372010-02-26 13:31:12 +00001937 SetObjectPrototype(global_proxy, inner_global);
Steve Blocka7e24c12009-10-30 11:49:00 +00001938 return true;
1939}
1940
1941
1942bool Genesis::ConfigureApiObject(Handle<JSObject> object,
1943 Handle<ObjectTemplateInfo> object_template) {
1944 ASSERT(!object_template.is_null());
1945 ASSERT(object->IsInstanceOf(
1946 FunctionTemplateInfo::cast(object_template->constructor())));
1947
1948 bool pending_exception = false;
1949 Handle<JSObject> obj =
1950 Execution::InstantiateObject(object_template, &pending_exception);
1951 if (pending_exception) {
Ben Murdoch8b112d22011-06-08 16:22:53 +01001952 ASSERT(isolate()->has_pending_exception());
1953 isolate()->clear_pending_exception();
Steve Blocka7e24c12009-10-30 11:49:00 +00001954 return false;
1955 }
1956 TransferObject(obj, object);
1957 return true;
1958}
1959
1960
1961void Genesis::TransferNamedProperties(Handle<JSObject> from,
1962 Handle<JSObject> to) {
1963 if (from->HasFastProperties()) {
1964 Handle<DescriptorArray> descs =
1965 Handle<DescriptorArray>(from->map()->instance_descriptors());
1966 for (int i = 0; i < descs->number_of_descriptors(); i++) {
1967 PropertyDetails details = PropertyDetails(descs->GetDetails(i));
1968 switch (details.type()) {
1969 case FIELD: {
1970 HandleScope inner;
1971 Handle<String> key = Handle<String>(descs->GetKey(i));
1972 int index = descs->GetFieldIndex(i);
1973 Handle<Object> value = Handle<Object>(from->FastPropertyAt(index));
Steve Block1e0659c2011-05-24 12:43:12 +01001974 SetLocalPropertyNoThrow(to, key, value, details.attributes());
Steve Blocka7e24c12009-10-30 11:49:00 +00001975 break;
1976 }
1977 case CONSTANT_FUNCTION: {
1978 HandleScope inner;
1979 Handle<String> key = Handle<String>(descs->GetKey(i));
1980 Handle<JSFunction> fun =
1981 Handle<JSFunction>(descs->GetConstantFunction(i));
Steve Block1e0659c2011-05-24 12:43:12 +01001982 SetLocalPropertyNoThrow(to, key, fun, details.attributes());
Steve Blocka7e24c12009-10-30 11:49:00 +00001983 break;
1984 }
1985 case CALLBACKS: {
1986 LookupResult result;
1987 to->LocalLookup(descs->GetKey(i), &result);
1988 // If the property is already there we skip it
Andrei Popescu402d9372010-02-26 13:31:12 +00001989 if (result.IsProperty()) continue;
Steve Blocka7e24c12009-10-30 11:49:00 +00001990 HandleScope inner;
Andrei Popescu31002712010-02-23 13:46:05 +00001991 ASSERT(!to->HasFastProperties());
1992 // Add to dictionary.
Steve Blocka7e24c12009-10-30 11:49:00 +00001993 Handle<String> key = Handle<String>(descs->GetKey(i));
Andrei Popescu31002712010-02-23 13:46:05 +00001994 Handle<Object> callbacks(descs->GetCallbacksObject(i));
1995 PropertyDetails d =
1996 PropertyDetails(details.attributes(), CALLBACKS, details.index());
1997 SetNormalizedProperty(to, key, callbacks, d);
Steve Blocka7e24c12009-10-30 11:49:00 +00001998 break;
1999 }
2000 case MAP_TRANSITION:
Steve Block44f0eee2011-05-26 01:26:41 +01002001 case EXTERNAL_ARRAY_TRANSITION:
Steve Blocka7e24c12009-10-30 11:49:00 +00002002 case CONSTANT_TRANSITION:
2003 case NULL_DESCRIPTOR:
2004 // Ignore non-properties.
2005 break;
2006 case NORMAL:
2007 // Do not occur since the from object has fast properties.
2008 case INTERCEPTOR:
2009 // No element in instance descriptors have interceptor type.
2010 UNREACHABLE();
2011 break;
2012 }
2013 }
2014 } else {
2015 Handle<StringDictionary> properties =
2016 Handle<StringDictionary>(from->property_dictionary());
2017 int capacity = properties->Capacity();
2018 for (int i = 0; i < capacity; i++) {
2019 Object* raw_key(properties->KeyAt(i));
2020 if (properties->IsKey(raw_key)) {
2021 ASSERT(raw_key->IsString());
2022 // If the property is already there we skip it.
2023 LookupResult result;
2024 to->LocalLookup(String::cast(raw_key), &result);
Andrei Popescu402d9372010-02-26 13:31:12 +00002025 if (result.IsProperty()) continue;
Steve Blocka7e24c12009-10-30 11:49:00 +00002026 // Set the property.
2027 Handle<String> key = Handle<String>(String::cast(raw_key));
2028 Handle<Object> value = Handle<Object>(properties->ValueAt(i));
2029 if (value->IsJSGlobalPropertyCell()) {
2030 value = Handle<Object>(JSGlobalPropertyCell::cast(*value)->value());
2031 }
2032 PropertyDetails details = properties->DetailsAt(i);
Steve Block1e0659c2011-05-24 12:43:12 +01002033 SetLocalPropertyNoThrow(to, key, value, details.attributes());
Steve Blocka7e24c12009-10-30 11:49:00 +00002034 }
2035 }
2036 }
2037}
2038
2039
2040void Genesis::TransferIndexedProperties(Handle<JSObject> from,
2041 Handle<JSObject> to) {
2042 // Cloning the elements array is sufficient.
2043 Handle<FixedArray> from_elements =
2044 Handle<FixedArray>(FixedArray::cast(from->elements()));
Steve Block44f0eee2011-05-26 01:26:41 +01002045 Handle<FixedArray> to_elements = FACTORY->CopyFixedArray(from_elements);
Steve Blocka7e24c12009-10-30 11:49:00 +00002046 to->set_elements(*to_elements);
2047}
2048
2049
2050void Genesis::TransferObject(Handle<JSObject> from, Handle<JSObject> to) {
2051 HandleScope outer;
Ben Murdoch8b112d22011-06-08 16:22:53 +01002052 Factory* factory = from->GetIsolate()->factory();
Steve Blocka7e24c12009-10-30 11:49:00 +00002053
2054 ASSERT(!from->IsJSArray());
2055 ASSERT(!to->IsJSArray());
2056
2057 TransferNamedProperties(from, to);
2058 TransferIndexedProperties(from, to);
2059
2060 // Transfer the prototype (new map is needed).
2061 Handle<Map> old_to_map = Handle<Map>(to->map());
Ben Murdoch8b112d22011-06-08 16:22:53 +01002062 Handle<Map> new_to_map = factory->CopyMapDropTransitions(old_to_map);
Steve Blocka7e24c12009-10-30 11:49:00 +00002063 new_to_map->set_prototype(from->map()->prototype());
2064 to->set_map(*new_to_map);
2065}
2066
2067
2068void Genesis::MakeFunctionInstancePrototypeWritable() {
Steve Block44f0eee2011-05-26 01:26:41 +01002069 // The maps with writable prototype are created in CreateEmptyFunction
2070 // and CreateStrictModeFunctionMaps respectively. Initially the maps are
2071 // created with read-only prototype for JS builtins processing.
2072 ASSERT(!function_instance_map_writable_prototype_.is_null());
2073 ASSERT(!strict_mode_function_instance_map_writable_prototype_.is_null());
Steve Blocka7e24c12009-10-30 11:49:00 +00002074
Steve Block44f0eee2011-05-26 01:26:41 +01002075 // Replace function instance maps to make prototype writable.
2076 global_context()->set_function_map(
2077 *function_instance_map_writable_prototype_);
2078 global_context()->set_strict_mode_function_map(
2079 *strict_mode_function_instance_map_writable_prototype_);
Steve Blocka7e24c12009-10-30 11:49:00 +00002080}
2081
2082
Ben Murdoch8b112d22011-06-08 16:22:53 +01002083Genesis::Genesis(Isolate* isolate,
2084 Handle<Object> global_object,
Steve Blocka7e24c12009-10-30 11:49:00 +00002085 v8::Handle<v8::ObjectTemplate> global_template,
Ben Murdoch8b112d22011-06-08 16:22:53 +01002086 v8::ExtensionConfiguration* extensions) : isolate_(isolate) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002087 result_ = Handle<Context>::null();
Steve Blocka7e24c12009-10-30 11:49:00 +00002088 // If V8 isn't running and cannot be initialized, just return.
2089 if (!V8::IsRunning() && !V8::Initialize(NULL)) return;
2090
2091 // Before creating the roots we must save the context and restore it
2092 // on all function exits.
2093 HandleScope scope;
Steve Block44f0eee2011-05-26 01:26:41 +01002094 SaveContext saved_context(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00002095
Andrei Popescu31002712010-02-23 13:46:05 +00002096 Handle<Context> new_context = Snapshot::NewContextFromSnapshot();
2097 if (!new_context.is_null()) {
2098 global_context_ =
Steve Block44f0eee2011-05-26 01:26:41 +01002099 Handle<Context>::cast(isolate->global_handles()->Create(*new_context));
Ben Murdochb0fe1622011-05-05 13:52:32 +01002100 AddToWeakGlobalContextList(*global_context_);
Steve Block44f0eee2011-05-26 01:26:41 +01002101 isolate->set_context(*global_context_);
2102 isolate->counters()->contexts_created_by_snapshot()->Increment();
Andrei Popescu402d9372010-02-26 13:31:12 +00002103 Handle<GlobalObject> inner_global;
Andrei Popescu31002712010-02-23 13:46:05 +00002104 Handle<JSGlobalProxy> global_proxy =
2105 CreateNewGlobals(global_template,
2106 global_object,
Andrei Popescu402d9372010-02-26 13:31:12 +00002107 &inner_global);
2108
Andrei Popescu31002712010-02-23 13:46:05 +00002109 HookUpGlobalProxy(inner_global, global_proxy);
Andrei Popescu402d9372010-02-26 13:31:12 +00002110 HookUpInnerGlobal(inner_global);
2111
Andrei Popescu31002712010-02-23 13:46:05 +00002112 if (!ConfigureGlobalObjects(global_template)) return;
2113 } else {
2114 // We get here if there was no context snapshot.
2115 CreateRoots();
Ben Murdoch8b112d22011-06-08 16:22:53 +01002116 Handle<JSFunction> empty_function = CreateEmptyFunction(isolate);
Steve Block44f0eee2011-05-26 01:26:41 +01002117 CreateStrictModeFunctionMaps(empty_function);
Andrei Popescu31002712010-02-23 13:46:05 +00002118 Handle<GlobalObject> inner_global;
2119 Handle<JSGlobalProxy> global_proxy =
2120 CreateNewGlobals(global_template, global_object, &inner_global);
2121 HookUpGlobalProxy(inner_global, global_proxy);
2122 InitializeGlobal(inner_global, empty_function);
Steve Block6ded16b2010-05-10 14:33:55 +01002123 InstallJSFunctionResultCaches();
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002124 InitializeNormalizedMapCaches();
Kristian Monsen25f61362010-05-21 11:50:48 +01002125 if (!InstallNatives()) return;
Steve Blocka7e24c12009-10-30 11:49:00 +00002126
Andrei Popescu31002712010-02-23 13:46:05 +00002127 MakeFunctionInstancePrototypeWritable();
Steve Blocka7e24c12009-10-30 11:49:00 +00002128
Andrei Popescu31002712010-02-23 13:46:05 +00002129 if (!ConfigureGlobalObjects(global_template)) return;
Steve Block44f0eee2011-05-26 01:26:41 +01002130 isolate->counters()->contexts_created_from_scratch()->Increment();
Andrei Popescu31002712010-02-23 13:46:05 +00002131 }
Steve Blocka7e24c12009-10-30 11:49:00 +00002132
Ben Murdoch8b112d22011-06-08 16:22:53 +01002133 // Install experimental natives.
2134 if (!InstallExperimentalNatives()) return;
2135
Steve Blocka7e24c12009-10-30 11:49:00 +00002136 result_ = global_context_;
2137}
2138
2139
2140// Support for thread preemption.
2141
2142// Reserve space for statics needing saving and restoring.
2143int Bootstrapper::ArchiveSpacePerThread() {
Steve Block44f0eee2011-05-26 01:26:41 +01002144 return sizeof(NestingCounterType);
Steve Blocka7e24c12009-10-30 11:49:00 +00002145}
2146
2147
2148// Archive statics that are thread local.
2149char* Bootstrapper::ArchiveState(char* to) {
Steve Block44f0eee2011-05-26 01:26:41 +01002150 *reinterpret_cast<NestingCounterType*>(to) = nesting_;
2151 nesting_ = 0;
2152 return to + sizeof(NestingCounterType);
Steve Blocka7e24c12009-10-30 11:49:00 +00002153}
2154
2155
2156// Restore statics that are thread local.
2157char* Bootstrapper::RestoreState(char* from) {
Steve Block44f0eee2011-05-26 01:26:41 +01002158 nesting_ = *reinterpret_cast<NestingCounterType*>(from);
2159 return from + sizeof(NestingCounterType);
Steve Blocka7e24c12009-10-30 11:49:00 +00002160}
2161
2162
2163// Called when the top-level V8 mutex is destroyed.
2164void Bootstrapper::FreeThreadResources() {
Steve Block44f0eee2011-05-26 01:26:41 +01002165 ASSERT(!IsActive());
Steve Blocka7e24c12009-10-30 11:49:00 +00002166}
2167
2168} } // namespace v8::internal