blob: 5b876405cd6d804e8ccb1594c33253d2561ffd56 [file] [log] [blame]
Steve Block053d10c2011-06-13 19:13:29 +01001// Copyright 2006-2008 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:
Steve Block053d10c2011-06-13 19:13:29 +0100144 Genesis(Handle<Object> global_object,
Steve Blocka7e24c12009-10-30 11:49:00 +0000145 v8::Handle<v8::ObjectTemplate> global_template,
146 v8::ExtensionConfiguration* extensions);
Andrei Popescu31002712010-02-23 13:46:05 +0000147 ~Genesis() { }
Steve Blocka7e24c12009-10-30 11:49:00 +0000148
149 Handle<Context> result() { return result_; }
150
151 Genesis* previous() { return previous_; }
Steve Blocka7e24c12009-10-30 11:49:00 +0000152
153 private:
154 Handle<Context> global_context_;
155
156 // There may be more than one active genesis object: When GC is
157 // triggered during environment creation there may be weak handle
158 // processing callbacks which may create new environments.
159 Genesis* previous_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000160
161 Handle<Context> global_context() { return global_context_; }
162
Andrei Popescu31002712010-02-23 13:46:05 +0000163 // Creates some basic objects. Used for creating a context from scratch.
164 void CreateRoots();
165 // Creates the empty function. Used for creating a context from scratch.
Steve Block053d10c2011-06-13 19:13:29 +0100166 Handle<JSFunction> CreateEmptyFunction();
Steve Block44f0eee2011-05-26 01:26:41 +0100167 // Creates the ThrowTypeError function. ECMA 5th Ed. 13.2.3
168 Handle<JSFunction> CreateThrowTypeErrorFunction(Builtins::Name builtin);
169
170 void CreateStrictModeFunctionMaps(Handle<JSFunction> empty);
Andrei Popescu31002712010-02-23 13:46:05 +0000171 // Creates the global objects using the global and the template passed in
172 // through the API. We call this regardless of whether we are building a
173 // context from scratch or using a deserialized one from the partial snapshot
174 // but in the latter case we don't use the objects it produces directly, as
175 // we have to used the deserialized ones that are linked together with the
176 // rest of the context snapshot.
177 Handle<JSGlobalProxy> CreateNewGlobals(
178 v8::Handle<v8::ObjectTemplate> global_template,
179 Handle<Object> global_object,
180 Handle<GlobalObject>* global_proxy_out);
181 // Hooks the given global proxy into the context. If the context was created
182 // by deserialization then this will unhook the global proxy that was
183 // deserialized, leaving the GC to pick it up.
184 void HookUpGlobalProxy(Handle<GlobalObject> inner_global,
185 Handle<JSGlobalProxy> global_proxy);
Andrei Popescu402d9372010-02-26 13:31:12 +0000186 // Similarly, we want to use the inner global that has been created by the
187 // templates passed through the API. The inner global from the snapshot is
188 // detached from the other objects in the snapshot.
189 void HookUpInnerGlobal(Handle<GlobalObject> inner_global);
Andrei Popescu31002712010-02-23 13:46:05 +0000190 // New context initialization. Used for creating a context from scratch.
191 void InitializeGlobal(Handle<GlobalObject> inner_global,
192 Handle<JSFunction> empty_function);
193 // Installs the contents of the native .js files on the global objects.
194 // Used for creating a context from scratch.
Steve Blocka7e24c12009-10-30 11:49:00 +0000195 void InstallNativeFunctions();
196 bool InstallNatives();
Ben Murdochb0fe1622011-05-05 13:52:32 +0100197 void InstallBuiltinFunctionIds();
Steve Block6ded16b2010-05-10 14:33:55 +0100198 void InstallJSFunctionResultCaches();
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100199 void InitializeNormalizedMapCaches();
Andrei Popescu31002712010-02-23 13:46:05 +0000200 // Used both for deserialized and from-scratch contexts to add the extensions
201 // provided.
202 static bool InstallExtensions(Handle<Context> global_context,
203 v8::ExtensionConfiguration* extensions);
204 static bool InstallExtension(const char* name);
205 static bool InstallExtension(v8::RegisteredExtension* current);
206 static void InstallSpecialObjects(Handle<Context> global_context);
Andrei Popescu402d9372010-02-26 13:31:12 +0000207 bool InstallJSBuiltins(Handle<JSBuiltinsObject> builtins);
Steve Blocka7e24c12009-10-30 11:49:00 +0000208 bool ConfigureApiObject(Handle<JSObject> object,
209 Handle<ObjectTemplateInfo> object_template);
210 bool ConfigureGlobalObjects(v8::Handle<v8::ObjectTemplate> global_template);
211
212 // Migrates all properties from the 'from' object to the 'to'
213 // object and overrides the prototype in 'to' with the one from
214 // 'from'.
215 void TransferObject(Handle<JSObject> from, Handle<JSObject> to);
216 void TransferNamedProperties(Handle<JSObject> from, Handle<JSObject> to);
217 void TransferIndexedProperties(Handle<JSObject> from, Handle<JSObject> to);
218
Steve Block6ded16b2010-05-10 14:33:55 +0100219 enum PrototypePropertyMode {
220 DONT_ADD_PROTOTYPE,
221 ADD_READONLY_PROTOTYPE,
222 ADD_WRITEABLE_PROTOTYPE
223 };
Steve Block44f0eee2011-05-26 01:26:41 +0100224
225 Handle<Map> CreateFunctionMap(PrototypePropertyMode prototype_mode);
226
Steve Blocka7e24c12009-10-30 11:49:00 +0000227 Handle<DescriptorArray> ComputeFunctionInstanceDescriptor(
Steve Block6ded16b2010-05-10 14:33:55 +0100228 PrototypePropertyMode prototypeMode);
Steve Blocka7e24c12009-10-30 11:49:00 +0000229 void MakeFunctionInstancePrototypeWritable();
230
Steve Block44f0eee2011-05-26 01:26:41 +0100231 Handle<Map> CreateStrictModeFunctionMap(
232 PrototypePropertyMode prototype_mode,
233 Handle<JSFunction> empty_function,
234 Handle<FixedArray> arguments_callbacks,
235 Handle<FixedArray> caller_callbacks);
236
237 Handle<DescriptorArray> ComputeStrictFunctionInstanceDescriptor(
238 PrototypePropertyMode propertyMode,
239 Handle<FixedArray> arguments,
240 Handle<FixedArray> caller);
241
Steve Block053d10c2011-06-13 19:13:29 +0100242 static bool CompileBuiltin(int index);
Steve Blocka7e24c12009-10-30 11:49:00 +0000243 static bool CompileNative(Vector<const char> name, Handle<String> source);
244 static bool CompileScriptCached(Vector<const char> name,
245 Handle<String> source,
246 SourceCodeCache* cache,
247 v8::Extension* extension,
Andrei Popescu31002712010-02-23 13:46:05 +0000248 Handle<Context> top_context,
Steve Blocka7e24c12009-10-30 11:49:00 +0000249 bool use_runtime_context);
250
251 Handle<Context> result_;
Steve Block44f0eee2011-05-26 01:26:41 +0100252
253 // Function instance maps. Function literal maps are created initially with
254 // a read only prototype for the processing of JS builtins. Later the function
255 // instance maps are replaced in order to make prototype writable.
256 // These are the final, writable prototype, maps.
257 Handle<Map> function_instance_map_writable_prototype_;
258 Handle<Map> strict_mode_function_instance_map_writable_prototype_;
259
Andrei Popescu31002712010-02-23 13:46:05 +0000260 BootstrapperActive active_;
261 friend class Bootstrapper;
Steve Blocka7e24c12009-10-30 11:49:00 +0000262};
263
Steve Blocka7e24c12009-10-30 11:49:00 +0000264
265void Bootstrapper::Iterate(ObjectVisitor* v) {
Steve Block44f0eee2011-05-26 01:26:41 +0100266 extensions_cache_.Iterate(v);
Steve Blockd0582a62009-12-15 09:54:21 +0000267 v->Synchronize("Extensions");
Steve Blocka7e24c12009-10-30 11:49:00 +0000268}
269
270
Steve Blocka7e24c12009-10-30 11:49:00 +0000271Handle<Context> Bootstrapper::CreateEnvironment(
272 Handle<Object> global_object,
273 v8::Handle<v8::ObjectTemplate> global_template,
274 v8::ExtensionConfiguration* extensions) {
Andrei Popescu31002712010-02-23 13:46:05 +0000275 HandleScope scope;
276 Handle<Context> env;
Steve Block053d10c2011-06-13 19:13:29 +0100277 Genesis genesis(global_object, global_template, extensions);
Andrei Popescu31002712010-02-23 13:46:05 +0000278 env = genesis.result();
279 if (!env.is_null()) {
280 if (InstallExtensions(env, extensions)) {
281 return env;
282 }
283 }
284 return Handle<Context>();
Steve Blocka7e24c12009-10-30 11:49:00 +0000285}
286
287
288static void SetObjectPrototype(Handle<JSObject> object, Handle<Object> proto) {
289 // object.__proto__ = proto;
290 Handle<Map> old_to_map = Handle<Map>(object->map());
Steve Block053d10c2011-06-13 19:13:29 +0100291 Handle<Map> new_to_map = FACTORY->CopyMapDropTransitions(old_to_map);
Steve Blocka7e24c12009-10-30 11:49:00 +0000292 new_to_map->set_prototype(*proto);
293 object->set_map(*new_to_map);
294}
295
296
297void Bootstrapper::DetachGlobal(Handle<Context> env) {
Steve Block053d10c2011-06-13 19:13:29 +0100298 Factory* factory = Isolate::Current()->factory();
Steve Block44f0eee2011-05-26 01:26:41 +0100299 JSGlobalProxy::cast(env->global_proxy())->set_context(*factory->null_value());
Steve Blocka7e24c12009-10-30 11:49:00 +0000300 SetObjectPrototype(Handle<JSObject>(env->global_proxy()),
Steve Block44f0eee2011-05-26 01:26:41 +0100301 factory->null_value());
Steve Blocka7e24c12009-10-30 11:49:00 +0000302 env->set_global_proxy(env->global());
303 env->global()->set_global_receiver(env->global());
304}
305
306
Andrei Popescu74b3c142010-03-29 12:03:09 +0100307void Bootstrapper::ReattachGlobal(Handle<Context> env,
308 Handle<Object> global_object) {
309 ASSERT(global_object->IsJSGlobalProxy());
310 Handle<JSGlobalProxy> global = Handle<JSGlobalProxy>::cast(global_object);
311 env->global()->set_global_receiver(*global);
312 env->set_global_proxy(*global);
313 SetObjectPrototype(global, Handle<JSObject>(env->global()));
314 global->set_context(*env);
315}
316
317
Steve Blocka7e24c12009-10-30 11:49:00 +0000318static Handle<JSFunction> InstallFunction(Handle<JSObject> target,
319 const char* name,
320 InstanceType type,
321 int instance_size,
322 Handle<JSObject> prototype,
323 Builtins::Name call,
324 bool is_ecma_native) {
Steve Block053d10c2011-06-13 19:13:29 +0100325 Isolate* isolate = Isolate::Current();
Steve Block44f0eee2011-05-26 01:26:41 +0100326 Factory* factory = isolate->factory();
327 Handle<String> symbol = factory->LookupAsciiSymbol(name);
328 Handle<Code> call_code = Handle<Code>(isolate->builtins()->builtin(call));
Steve Block6ded16b2010-05-10 14:33:55 +0100329 Handle<JSFunction> function = prototype.is_null() ?
Steve Block44f0eee2011-05-26 01:26:41 +0100330 factory->NewFunctionWithoutPrototype(symbol, call_code) :
331 factory->NewFunctionWithPrototype(symbol,
Steve Blocka7e24c12009-10-30 11:49:00 +0000332 type,
333 instance_size,
334 prototype,
335 call_code,
336 is_ecma_native);
Steve Block1e0659c2011-05-24 12:43:12 +0100337 SetLocalPropertyNoThrow(target, symbol, function, DONT_ENUM);
Steve Blocka7e24c12009-10-30 11:49:00 +0000338 if (is_ecma_native) {
339 function->shared()->set_instance_class_name(*symbol);
340 }
341 return function;
342}
343
344
345Handle<DescriptorArray> Genesis::ComputeFunctionInstanceDescriptor(
Steve Block6ded16b2010-05-10 14:33:55 +0100346 PrototypePropertyMode prototypeMode) {
Steve Block053d10c2011-06-13 19:13:29 +0100347 Factory* factory = Isolate::Current()->factory();
Steve Block44f0eee2011-05-26 01:26:41 +0100348 Handle<DescriptorArray> descriptors =
Steve Block053d10c2011-06-13 19:13:29 +0100349 factory->NewDescriptorArray(prototypeMode == DONT_ADD_PROTOTYPE ? 4 : 5);
Steve Block6ded16b2010-05-10 14:33:55 +0100350 PropertyAttributes attributes =
Steve Blocka7e24c12009-10-30 11:49:00 +0000351 static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE | READ_ONLY);
Steve Blocka7e24c12009-10-30 11:49:00 +0000352
Steve Block44f0eee2011-05-26 01:26:41 +0100353 { // Add length.
Steve Block053d10c2011-06-13 19:13:29 +0100354 Handle<Proxy> proxy = factory->NewProxy(&Accessors::FunctionLength);
355 CallbacksDescriptor d(*factory->length_symbol(), *proxy, attributes);
Steve Block44f0eee2011-05-26 01:26:41 +0100356 descriptors->Set(0, &d);
357 }
358 { // Add name.
Steve Block053d10c2011-06-13 19:13:29 +0100359 Handle<Proxy> proxy = factory->NewProxy(&Accessors::FunctionName);
360 CallbacksDescriptor d(*factory->name_symbol(), *proxy, attributes);
Steve Block44f0eee2011-05-26 01:26:41 +0100361 descriptors->Set(1, &d);
362 }
363 { // Add arguments.
Steve Block053d10c2011-06-13 19:13:29 +0100364 Handle<Proxy> proxy = factory->NewProxy(&Accessors::FunctionArguments);
365 CallbacksDescriptor d(*factory->arguments_symbol(), *proxy, attributes);
Steve Block44f0eee2011-05-26 01:26:41 +0100366 descriptors->Set(2, &d);
367 }
368 { // Add caller.
Steve Block053d10c2011-06-13 19:13:29 +0100369 Handle<Proxy> proxy = factory->NewProxy(&Accessors::FunctionCaller);
370 CallbacksDescriptor d(*factory->caller_symbol(), *proxy, attributes);
Steve Block44f0eee2011-05-26 01:26:41 +0100371 descriptors->Set(3, &d);
372 }
373 if (prototypeMode != DONT_ADD_PROTOTYPE) {
374 // Add prototype.
375 if (prototypeMode == ADD_WRITEABLE_PROTOTYPE) {
376 attributes = static_cast<PropertyAttributes>(attributes & ~READ_ONLY);
377 }
Steve Block053d10c2011-06-13 19:13:29 +0100378 Handle<Proxy> proxy = factory->NewProxy(&Accessors::FunctionPrototype);
379 CallbacksDescriptor d(*factory->prototype_symbol(), *proxy, attributes);
Steve Block44f0eee2011-05-26 01:26:41 +0100380 descriptors->Set(4, &d);
381 }
382 descriptors->Sort();
383 return descriptors;
384}
Steve Blocka7e24c12009-10-30 11:49:00 +0000385
Steve Blocka7e24c12009-10-30 11:49:00 +0000386
Steve Block44f0eee2011-05-26 01:26:41 +0100387Handle<Map> Genesis::CreateFunctionMap(PrototypePropertyMode prototype_mode) {
Steve Block053d10c2011-06-13 19:13:29 +0100388 Handle<Map> map = FACTORY->NewMap(JS_FUNCTION_TYPE, JSFunction::kSize);
Steve Block44f0eee2011-05-26 01:26:41 +0100389 Handle<DescriptorArray> descriptors =
390 ComputeFunctionInstanceDescriptor(prototype_mode);
391 map->set_instance_descriptors(*descriptors);
392 map->set_function_with_prototype(prototype_mode != DONT_ADD_PROTOTYPE);
393 return map;
Steve Blocka7e24c12009-10-30 11:49:00 +0000394}
395
396
Steve Block053d10c2011-06-13 19:13:29 +0100397Handle<JSFunction> Genesis::CreateEmptyFunction() {
Steve Block44f0eee2011-05-26 01:26:41 +0100398 // Allocate the map for function instances. Maps are allocated first and their
399 // prototypes patched later, once empty function is created.
400
Steve Blocka7e24c12009-10-30 11:49:00 +0000401 // Please note that the prototype property for function instances must be
402 // writable.
Ben Murdoch8b112d22011-06-08 16:22:53 +0100403 Handle<Map> function_instance_map =
404 CreateFunctionMap(ADD_WRITEABLE_PROTOTYPE);
405 global_context()->set_function_instance_map(*function_instance_map);
Steve Block6ded16b2010-05-10 14:33:55 +0100406
407 // Functions with this map will not have a 'prototype' property, and
408 // can not be used as constructors.
Ben Murdoch8b112d22011-06-08 16:22:53 +0100409 Handle<Map> function_without_prototype_map =
410 CreateFunctionMap(DONT_ADD_PROTOTYPE);
Steve Block6ded16b2010-05-10 14:33:55 +0100411 global_context()->set_function_without_prototype_map(
Ben Murdoch8b112d22011-06-08 16:22:53 +0100412 *function_without_prototype_map);
Steve Blocka7e24c12009-10-30 11:49:00 +0000413
Steve Block44f0eee2011-05-26 01:26:41 +0100414 // Allocate the function map. This map is temporary, used only for processing
415 // of builtins.
416 // Later the map is replaced with writable prototype map, allocated below.
Ben Murdoch8b112d22011-06-08 16:22:53 +0100417 Handle<Map> function_map = CreateFunctionMap(ADD_READONLY_PROTOTYPE);
418 global_context()->set_function_map(*function_map);
Steve Blocka7e24c12009-10-30 11:49:00 +0000419
Steve Block44f0eee2011-05-26 01:26:41 +0100420 // The final map for functions. Writeable prototype.
421 // This map is installed in MakeFunctionInstancePrototypeWritable.
422 function_instance_map_writable_prototype_ =
423 CreateFunctionMap(ADD_WRITEABLE_PROTOTYPE);
424
Steve Block053d10c2011-06-13 19:13:29 +0100425 Isolate* isolate = Isolate::Current();
Steve Block44f0eee2011-05-26 01:26:41 +0100426 Factory* factory = isolate->factory();
427 Heap* heap = isolate->heap();
428
429 Handle<String> object_name = Handle<String>(heap->Object_symbol());
Steve Blocka7e24c12009-10-30 11:49:00 +0000430
431 { // --- O b j e c t ---
432 Handle<JSFunction> object_fun =
Steve Block44f0eee2011-05-26 01:26:41 +0100433 factory->NewFunction(object_name, factory->null_value());
Steve Blocka7e24c12009-10-30 11:49:00 +0000434 Handle<Map> object_function_map =
Steve Block44f0eee2011-05-26 01:26:41 +0100435 factory->NewMap(JS_OBJECT_TYPE, JSObject::kHeaderSize);
Steve Blocka7e24c12009-10-30 11:49:00 +0000436 object_fun->set_initial_map(*object_function_map);
437 object_function_map->set_constructor(*object_fun);
438
439 global_context()->set_object_function(*object_fun);
440
441 // Allocate a new prototype for the object function.
Steve Block44f0eee2011-05-26 01:26:41 +0100442 Handle<JSObject> prototype = factory->NewJSObject(
443 isolate->object_function(),
444 TENURED);
Steve Blocka7e24c12009-10-30 11:49:00 +0000445
446 global_context()->set_initial_object_prototype(*prototype);
447 SetPrototype(object_fun, prototype);
448 object_function_map->
Steve Block44f0eee2011-05-26 01:26:41 +0100449 set_instance_descriptors(heap->empty_descriptor_array());
Steve Blocka7e24c12009-10-30 11:49:00 +0000450 }
451
452 // Allocate the empty function as the prototype for function ECMAScript
453 // 262 15.3.4.
Steve Block44f0eee2011-05-26 01:26:41 +0100454 Handle<String> symbol = factory->LookupAsciiSymbol("Empty");
Steve Blocka7e24c12009-10-30 11:49:00 +0000455 Handle<JSFunction> empty_function =
Steve Block44f0eee2011-05-26 01:26:41 +0100456 factory->NewFunctionWithoutPrototype(symbol, kNonStrictMode);
Steve Blocka7e24c12009-10-30 11:49:00 +0000457
Andrei Popescu31002712010-02-23 13:46:05 +0000458 // --- E m p t y ---
459 Handle<Code> code =
Steve Block44f0eee2011-05-26 01:26:41 +0100460 Handle<Code>(isolate->builtins()->builtin(
461 Builtins::kEmptyFunction));
Andrei Popescu31002712010-02-23 13:46:05 +0000462 empty_function->set_code(*code);
Iain Merrick75681382010-08-19 15:07:18 +0100463 empty_function->shared()->set_code(*code);
Steve Block44f0eee2011-05-26 01:26:41 +0100464 Handle<String> source = factory->NewStringFromAscii(CStrVector("() {}"));
465 Handle<Script> script = factory->NewScript(source);
Andrei Popescu31002712010-02-23 13:46:05 +0000466 script->set_type(Smi::FromInt(Script::TYPE_NATIVE));
467 empty_function->shared()->set_script(*script);
468 empty_function->shared()->set_start_position(0);
469 empty_function->shared()->set_end_position(source->length());
470 empty_function->shared()->DontAdaptArguments();
Steve Block44f0eee2011-05-26 01:26:41 +0100471
472 // Set prototypes for the function maps.
Andrei Popescu31002712010-02-23 13:46:05 +0000473 global_context()->function_map()->set_prototype(*empty_function);
474 global_context()->function_instance_map()->set_prototype(*empty_function);
Steve Block6ded16b2010-05-10 14:33:55 +0100475 global_context()->function_without_prototype_map()->
476 set_prototype(*empty_function);
Steve Block44f0eee2011-05-26 01:26:41 +0100477 function_instance_map_writable_prototype_->set_prototype(*empty_function);
Steve Blocka7e24c12009-10-30 11:49:00 +0000478
Andrei Popescu31002712010-02-23 13:46:05 +0000479 // Allocate the function map first and then patch the prototype later
Steve Block44f0eee2011-05-26 01:26:41 +0100480 Handle<Map> empty_fm = factory->CopyMapDropDescriptors(
Steve Block6ded16b2010-05-10 14:33:55 +0100481 function_without_prototype_map);
482 empty_fm->set_instance_descriptors(
Steve Block44f0eee2011-05-26 01:26:41 +0100483 function_without_prototype_map->instance_descriptors());
Andrei Popescu31002712010-02-23 13:46:05 +0000484 empty_fm->set_prototype(global_context()->object_function()->prototype());
485 empty_function->set_map(*empty_fm);
486 return empty_function;
487}
488
489
Steve Block44f0eee2011-05-26 01:26:41 +0100490Handle<DescriptorArray> Genesis::ComputeStrictFunctionInstanceDescriptor(
491 PrototypePropertyMode prototypeMode,
492 Handle<FixedArray> arguments,
493 Handle<FixedArray> caller) {
Steve Block053d10c2011-06-13 19:13:29 +0100494 Factory* factory = Isolate::Current()->factory();
Steve Block44f0eee2011-05-26 01:26:41 +0100495 Handle<DescriptorArray> descriptors =
Steve Block053d10c2011-06-13 19:13:29 +0100496 factory->NewDescriptorArray(prototypeMode == DONT_ADD_PROTOTYPE ? 4 : 5);
Steve Block44f0eee2011-05-26 01:26:41 +0100497 PropertyAttributes attributes = static_cast<PropertyAttributes>(
498 DONT_ENUM | DONT_DELETE | READ_ONLY);
499
500 { // length
Steve Block053d10c2011-06-13 19:13:29 +0100501 Handle<Proxy> proxy = factory->NewProxy(&Accessors::FunctionLength);
502 CallbacksDescriptor d(*factory->length_symbol(), *proxy, attributes);
Steve Block44f0eee2011-05-26 01:26:41 +0100503 descriptors->Set(0, &d);
504 }
505 { // name
Steve Block053d10c2011-06-13 19:13:29 +0100506 Handle<Proxy> proxy = factory->NewProxy(&Accessors::FunctionName);
507 CallbacksDescriptor d(*factory->name_symbol(), *proxy, attributes);
Steve Block44f0eee2011-05-26 01:26:41 +0100508 descriptors->Set(1, &d);
509 }
510 { // arguments
Steve Block053d10c2011-06-13 19:13:29 +0100511 CallbacksDescriptor d(*factory->arguments_symbol(), *arguments, attributes);
Steve Block44f0eee2011-05-26 01:26:41 +0100512 descriptors->Set(2, &d);
513 }
514 { // caller
Steve Block053d10c2011-06-13 19:13:29 +0100515 CallbacksDescriptor d(*factory->caller_symbol(), *caller, attributes);
Steve Block44f0eee2011-05-26 01:26:41 +0100516 descriptors->Set(3, &d);
517 }
518
519 // prototype
520 if (prototypeMode != DONT_ADD_PROTOTYPE) {
521 if (prototypeMode == ADD_WRITEABLE_PROTOTYPE) {
522 attributes = static_cast<PropertyAttributes>(attributes & ~READ_ONLY);
523 }
Steve Block053d10c2011-06-13 19:13:29 +0100524 Handle<Proxy> proxy = factory->NewProxy(&Accessors::FunctionPrototype);
525 CallbacksDescriptor d(*factory->prototype_symbol(), *proxy, attributes);
Steve Block44f0eee2011-05-26 01:26:41 +0100526 descriptors->Set(4, &d);
527 }
528
529 descriptors->Sort();
530 return descriptors;
531}
532
533
534// ECMAScript 5th Edition, 13.2.3
535Handle<JSFunction> Genesis::CreateThrowTypeErrorFunction(
536 Builtins::Name builtin) {
Steve Block053d10c2011-06-13 19:13:29 +0100537 Isolate* isolate = Isolate::Current();
538 Factory* factory = isolate->factory();
539
540 Handle<String> name = factory->LookupAsciiSymbol("ThrowTypeError");
Steve Block44f0eee2011-05-26 01:26:41 +0100541 Handle<JSFunction> throw_type_error =
Steve Block053d10c2011-06-13 19:13:29 +0100542 factory->NewFunctionWithoutPrototype(name, kStrictMode);
Steve Block44f0eee2011-05-26 01:26:41 +0100543 Handle<Code> code = Handle<Code>(
Steve Block053d10c2011-06-13 19:13:29 +0100544 isolate->builtins()->builtin(builtin));
Steve Block44f0eee2011-05-26 01:26:41 +0100545
546 throw_type_error->set_map(global_context()->strict_mode_function_map());
547 throw_type_error->set_code(*code);
548 throw_type_error->shared()->set_code(*code);
549 throw_type_error->shared()->DontAdaptArguments();
550
551 PreventExtensions(throw_type_error);
552
553 return throw_type_error;
554}
555
556
557Handle<Map> Genesis::CreateStrictModeFunctionMap(
558 PrototypePropertyMode prototype_mode,
559 Handle<JSFunction> empty_function,
560 Handle<FixedArray> arguments_callbacks,
561 Handle<FixedArray> caller_callbacks) {
Steve Block053d10c2011-06-13 19:13:29 +0100562 Handle<Map> map = FACTORY->NewMap(JS_FUNCTION_TYPE, JSFunction::kSize);
Steve Block44f0eee2011-05-26 01:26:41 +0100563 Handle<DescriptorArray> descriptors =
564 ComputeStrictFunctionInstanceDescriptor(prototype_mode,
565 arguments_callbacks,
566 caller_callbacks);
567 map->set_instance_descriptors(*descriptors);
568 map->set_function_with_prototype(prototype_mode != DONT_ADD_PROTOTYPE);
569 map->set_prototype(*empty_function);
570 return map;
571}
572
573
574void Genesis::CreateStrictModeFunctionMaps(Handle<JSFunction> empty) {
575 // Create the callbacks arrays for ThrowTypeError functions.
576 // The get/set callacks are filled in after the maps are created below.
Steve Block053d10c2011-06-13 19:13:29 +0100577 Factory* factory = Isolate::Current()->factory();
Steve Block44f0eee2011-05-26 01:26:41 +0100578 Handle<FixedArray> arguments = factory->NewFixedArray(2, TENURED);
579 Handle<FixedArray> caller = factory->NewFixedArray(2, TENURED);
580
581 // Allocate map for the strict mode function instances.
Ben Murdoch8b112d22011-06-08 16:22:53 +0100582 Handle<Map> strict_mode_function_instance_map =
583 CreateStrictModeFunctionMap(
584 ADD_WRITEABLE_PROTOTYPE, empty, arguments, caller);
Steve Block44f0eee2011-05-26 01:26:41 +0100585 global_context()->set_strict_mode_function_instance_map(
Ben Murdoch8b112d22011-06-08 16:22:53 +0100586 *strict_mode_function_instance_map);
Steve Block44f0eee2011-05-26 01:26:41 +0100587
588 // Allocate map for the prototype-less strict mode instances.
Ben Murdoch8b112d22011-06-08 16:22:53 +0100589 Handle<Map> strict_mode_function_without_prototype_map =
590 CreateStrictModeFunctionMap(
591 DONT_ADD_PROTOTYPE, empty, arguments, caller);
Steve Block44f0eee2011-05-26 01:26:41 +0100592 global_context()->set_strict_mode_function_without_prototype_map(
Ben Murdoch8b112d22011-06-08 16:22:53 +0100593 *strict_mode_function_without_prototype_map);
Steve Block44f0eee2011-05-26 01:26:41 +0100594
595 // Allocate map for the strict mode functions. This map is temporary, used
596 // only for processing of builtins.
597 // Later the map is replaced with writable prototype map, allocated below.
Ben Murdoch8b112d22011-06-08 16:22:53 +0100598 Handle<Map> strict_mode_function_map =
599 CreateStrictModeFunctionMap(
600 ADD_READONLY_PROTOTYPE, empty, arguments, caller);
Steve Block44f0eee2011-05-26 01:26:41 +0100601 global_context()->set_strict_mode_function_map(
Ben Murdoch8b112d22011-06-08 16:22:53 +0100602 *strict_mode_function_map);
Steve Block44f0eee2011-05-26 01:26:41 +0100603
604 // The final map for the strict mode functions. Writeable prototype.
605 // This map is installed in MakeFunctionInstancePrototypeWritable.
606 strict_mode_function_instance_map_writable_prototype_ =
607 CreateStrictModeFunctionMap(
608 ADD_WRITEABLE_PROTOTYPE, empty, arguments, caller);
609
610 // Create the ThrowTypeError function instances.
611 Handle<JSFunction> arguments_throw =
612 CreateThrowTypeErrorFunction(Builtins::kStrictFunctionArguments);
613 Handle<JSFunction> caller_throw =
614 CreateThrowTypeErrorFunction(Builtins::kStrictFunctionCaller);
615
616 // Complete the callback fixed arrays.
617 arguments->set(0, *arguments_throw);
618 arguments->set(1, *arguments_throw);
619 caller->set(0, *caller_throw);
620 caller->set(1, *caller_throw);
621}
622
623
Ben Murdochb0fe1622011-05-05 13:52:32 +0100624static void AddToWeakGlobalContextList(Context* context) {
625 ASSERT(context->IsGlobalContext());
Steve Block053d10c2011-06-13 19:13:29 +0100626 Heap* heap = Isolate::Current()->heap();
Ben Murdochb0fe1622011-05-05 13:52:32 +0100627#ifdef DEBUG
628 { // NOLINT
629 ASSERT(context->get(Context::NEXT_CONTEXT_LINK)->IsUndefined());
630 // Check that context is not in the list yet.
Steve Block44f0eee2011-05-26 01:26:41 +0100631 for (Object* current = heap->global_contexts_list();
Ben Murdochb0fe1622011-05-05 13:52:32 +0100632 !current->IsUndefined();
633 current = Context::cast(current)->get(Context::NEXT_CONTEXT_LINK)) {
634 ASSERT(current != context);
635 }
636 }
637#endif
Steve Block44f0eee2011-05-26 01:26:41 +0100638 context->set(Context::NEXT_CONTEXT_LINK, heap->global_contexts_list());
639 heap->set_global_contexts_list(context);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100640}
641
642
Andrei Popescu31002712010-02-23 13:46:05 +0000643void Genesis::CreateRoots() {
Steve Block053d10c2011-06-13 19:13:29 +0100644 Isolate* isolate = Isolate::Current();
Andrei Popescu31002712010-02-23 13:46:05 +0000645 // Allocate the global context FixedArray first and then patch the
646 // closure and extension object later (we need the empty function
647 // and the global object, but in order to create those, we need the
648 // global context).
Steve Block053d10c2011-06-13 19:13:29 +0100649 global_context_ = Handle<Context>::cast(isolate->global_handles()->Create(
650 *isolate->factory()->NewGlobalContext()));
Ben Murdochb0fe1622011-05-05 13:52:32 +0100651 AddToWeakGlobalContextList(*global_context_);
Steve Block053d10c2011-06-13 19:13:29 +0100652 isolate->set_context(*global_context());
Andrei Popescu31002712010-02-23 13:46:05 +0000653
654 // Allocate the message listeners object.
655 {
656 v8::NeanderArray listeners;
657 global_context()->set_message_listeners(*listeners.value());
658 }
659}
660
661
662Handle<JSGlobalProxy> Genesis::CreateNewGlobals(
663 v8::Handle<v8::ObjectTemplate> global_template,
664 Handle<Object> global_object,
665 Handle<GlobalObject>* inner_global_out) {
666 // The argument global_template aka data is an ObjectTemplateInfo.
667 // It has a constructor pointer that points at global_constructor which is a
668 // FunctionTemplateInfo.
669 // The global_constructor is used to create or reinitialize the global_proxy.
670 // The global_constructor also has a prototype_template pointer that points at
671 // js_global_template which is an ObjectTemplateInfo.
672 // That in turn has a constructor pointer that points at
673 // js_global_constructor which is a FunctionTemplateInfo.
674 // js_global_constructor is used to make js_global_function
675 // js_global_function is used to make the new inner_global.
676 //
677 // --- G l o b a l ---
678 // Step 1: Create a fresh inner JSGlobalObject.
679 Handle<JSFunction> js_global_function;
680 Handle<ObjectTemplateInfo> js_global_template;
681 if (!global_template.IsEmpty()) {
682 // Get prototype template of the global_template.
683 Handle<ObjectTemplateInfo> data =
684 v8::Utils::OpenHandle(*global_template);
685 Handle<FunctionTemplateInfo> global_constructor =
686 Handle<FunctionTemplateInfo>(
687 FunctionTemplateInfo::cast(data->constructor()));
688 Handle<Object> proto_template(global_constructor->prototype_template());
689 if (!proto_template->IsUndefined()) {
690 js_global_template =
691 Handle<ObjectTemplateInfo>::cast(proto_template);
692 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000693 }
694
Steve Block053d10c2011-06-13 19:13:29 +0100695 Isolate* isolate = Isolate::Current();
696 Factory* factory = isolate->factory();
697 Heap* heap = isolate->heap();
698
Andrei Popescu31002712010-02-23 13:46:05 +0000699 if (js_global_template.is_null()) {
Steve Block053d10c2011-06-13 19:13:29 +0100700 Handle<String> name = Handle<String>(heap->empty_symbol());
701 Handle<Code> code = Handle<Code>(isolate->builtins()->builtin(
Steve Block44f0eee2011-05-26 01:26:41 +0100702 Builtins::kIllegal));
Andrei Popescu31002712010-02-23 13:46:05 +0000703 js_global_function =
Steve Block053d10c2011-06-13 19:13:29 +0100704 factory->NewFunction(name, JS_GLOBAL_OBJECT_TYPE,
705 JSGlobalObject::kSize, code, true);
Andrei Popescu31002712010-02-23 13:46:05 +0000706 // Change the constructor property of the prototype of the
707 // hidden global function to refer to the Object function.
708 Handle<JSObject> prototype =
709 Handle<JSObject>(
710 JSObject::cast(js_global_function->instance_prototype()));
Steve Block1e0659c2011-05-24 12:43:12 +0100711 SetLocalPropertyNoThrow(
Steve Block44f0eee2011-05-26 01:26:41 +0100712 prototype,
Steve Block053d10c2011-06-13 19:13:29 +0100713 factory->constructor_symbol(),
714 isolate->object_function(),
Steve Block44f0eee2011-05-26 01:26:41 +0100715 NONE);
Andrei Popescu31002712010-02-23 13:46:05 +0000716 } else {
717 Handle<FunctionTemplateInfo> js_global_constructor(
718 FunctionTemplateInfo::cast(js_global_template->constructor()));
719 js_global_function =
Steve Block053d10c2011-06-13 19:13:29 +0100720 factory->CreateApiFunction(js_global_constructor,
721 factory->InnerGlobalObject);
Steve Blocka7e24c12009-10-30 11:49:00 +0000722 }
723
Andrei Popescu31002712010-02-23 13:46:05 +0000724 js_global_function->initial_map()->set_is_hidden_prototype();
725 Handle<GlobalObject> inner_global =
Steve Block053d10c2011-06-13 19:13:29 +0100726 factory->NewGlobalObject(js_global_function);
Andrei Popescu31002712010-02-23 13:46:05 +0000727 if (inner_global_out != NULL) {
728 *inner_global_out = inner_global;
729 }
730
731 // Step 2: create or re-initialize the global proxy object.
732 Handle<JSFunction> global_proxy_function;
733 if (global_template.IsEmpty()) {
Steve Block053d10c2011-06-13 19:13:29 +0100734 Handle<String> name = Handle<String>(heap->empty_symbol());
735 Handle<Code> code = Handle<Code>(isolate->builtins()->builtin(
Steve Block44f0eee2011-05-26 01:26:41 +0100736 Builtins::kIllegal));
Andrei Popescu31002712010-02-23 13:46:05 +0000737 global_proxy_function =
Steve Block053d10c2011-06-13 19:13:29 +0100738 factory->NewFunction(name, JS_GLOBAL_PROXY_TYPE,
739 JSGlobalProxy::kSize, code, true);
Andrei Popescu31002712010-02-23 13:46:05 +0000740 } else {
741 Handle<ObjectTemplateInfo> data =
742 v8::Utils::OpenHandle(*global_template);
743 Handle<FunctionTemplateInfo> global_constructor(
744 FunctionTemplateInfo::cast(data->constructor()));
745 global_proxy_function =
Steve Block053d10c2011-06-13 19:13:29 +0100746 factory->CreateApiFunction(global_constructor,
747 factory->OuterGlobalObject);
Andrei Popescu31002712010-02-23 13:46:05 +0000748 }
749
Steve Block053d10c2011-06-13 19:13:29 +0100750 Handle<String> global_name = factory->LookupAsciiSymbol("global");
Andrei Popescu31002712010-02-23 13:46:05 +0000751 global_proxy_function->shared()->set_instance_class_name(*global_name);
752 global_proxy_function->initial_map()->set_is_access_check_needed(true);
753
754 // Set global_proxy.__proto__ to js_global after ConfigureGlobalObjects
755 // Return the global proxy.
756
757 if (global_object.location() != NULL) {
758 ASSERT(global_object->IsJSGlobalProxy());
759 return ReinitializeJSGlobalProxy(
760 global_proxy_function,
761 Handle<JSGlobalProxy>::cast(global_object));
762 } else {
763 return Handle<JSGlobalProxy>::cast(
Steve Block053d10c2011-06-13 19:13:29 +0100764 factory->NewJSObject(global_proxy_function, TENURED));
Andrei Popescu31002712010-02-23 13:46:05 +0000765 }
766}
767
768
769void Genesis::HookUpGlobalProxy(Handle<GlobalObject> inner_global,
770 Handle<JSGlobalProxy> global_proxy) {
771 // Set the global context for the global object.
772 inner_global->set_global_context(*global_context());
773 inner_global->set_global_receiver(*global_proxy);
774 global_proxy->set_context(*global_context());
775 global_context()->set_global_proxy(*global_proxy);
776}
777
778
Andrei Popescu402d9372010-02-26 13:31:12 +0000779void Genesis::HookUpInnerGlobal(Handle<GlobalObject> inner_global) {
780 Handle<GlobalObject> inner_global_from_snapshot(
781 GlobalObject::cast(global_context_->extension()));
782 Handle<JSBuiltinsObject> builtins_global(global_context_->builtins());
783 global_context_->set_extension(*inner_global);
784 global_context_->set_global(*inner_global);
785 global_context_->set_security_token(*inner_global);
786 static const PropertyAttributes attributes =
787 static_cast<PropertyAttributes>(READ_ONLY | DONT_DELETE);
788 ForceSetProperty(builtins_global,
Steve Block053d10c2011-06-13 19:13:29 +0100789 FACTORY->LookupAsciiSymbol("global"),
Andrei Popescu402d9372010-02-26 13:31:12 +0000790 inner_global,
791 attributes);
792 // Setup the reference from the global object to the builtins object.
793 JSGlobalObject::cast(*inner_global)->set_builtins(*builtins_global);
794 TransferNamedProperties(inner_global_from_snapshot, inner_global);
795 TransferIndexedProperties(inner_global_from_snapshot, inner_global);
796}
797
798
799// This is only called if we are not using snapshots. The equivalent
800// work in the snapshot case is done in HookUpInnerGlobal.
Andrei Popescu31002712010-02-23 13:46:05 +0000801void Genesis::InitializeGlobal(Handle<GlobalObject> inner_global,
802 Handle<JSFunction> empty_function) {
803 // --- G l o b a l C o n t e x t ---
804 // Use the empty function as closure (no scope info).
805 global_context()->set_closure(*empty_function);
806 global_context()->set_fcontext(*global_context());
807 global_context()->set_previous(NULL);
808 // Set extension and global object.
809 global_context()->set_extension(*inner_global);
810 global_context()->set_global(*inner_global);
811 // Security setup: Set the security token of the global object to
812 // its the inner global. This makes the security check between two
813 // different contexts fail by default even in case of global
814 // object reinitialization.
815 global_context()->set_security_token(*inner_global);
816
Steve Block053d10c2011-06-13 19:13:29 +0100817 Isolate* isolate = Isolate::Current();
Steve Block44f0eee2011-05-26 01:26:41 +0100818 Factory* factory = isolate->factory();
819 Heap* heap = isolate->heap();
820
821 Handle<String> object_name = Handle<String>(heap->Object_symbol());
Steve Block1e0659c2011-05-24 12:43:12 +0100822 SetLocalPropertyNoThrow(inner_global, object_name,
Steve Block44f0eee2011-05-26 01:26:41 +0100823 isolate->object_function(), DONT_ENUM);
Andrei Popescu31002712010-02-23 13:46:05 +0000824
Steve Blocka7e24c12009-10-30 11:49:00 +0000825 Handle<JSObject> global = Handle<JSObject>(global_context()->global());
826
827 // Install global Function object
828 InstallFunction(global, "Function", JS_FUNCTION_TYPE, JSFunction::kSize,
Steve Block44f0eee2011-05-26 01:26:41 +0100829 empty_function, Builtins::kIllegal, true); // ECMA native.
Steve Blocka7e24c12009-10-30 11:49:00 +0000830
831 { // --- A r r a y ---
832 Handle<JSFunction> array_function =
833 InstallFunction(global, "Array", JS_ARRAY_TYPE, JSArray::kSize,
Steve Block44f0eee2011-05-26 01:26:41 +0100834 isolate->initial_object_prototype(),
835 Builtins::kArrayCode, true);
Steve Blocka7e24c12009-10-30 11:49:00 +0000836 array_function->shared()->set_construct_stub(
Steve Block44f0eee2011-05-26 01:26:41 +0100837 isolate->builtins()->builtin(Builtins::kArrayConstructCode));
Steve Blocka7e24c12009-10-30 11:49:00 +0000838 array_function->shared()->DontAdaptArguments();
839
840 // This seems a bit hackish, but we need to make sure Array.length
841 // is 1.
842 array_function->shared()->set_length(1);
843 Handle<DescriptorArray> array_descriptors =
Steve Block44f0eee2011-05-26 01:26:41 +0100844 factory->CopyAppendProxyDescriptor(
845 factory->empty_descriptor_array(),
846 factory->length_symbol(),
847 factory->NewProxy(&Accessors::ArrayLength),
Steve Blocka7e24c12009-10-30 11:49:00 +0000848 static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE));
849
850 // Cache the fast JavaScript array map
851 global_context()->set_js_array_map(array_function->initial_map());
852 global_context()->js_array_map()->set_instance_descriptors(
853 *array_descriptors);
854 // array_function is used internally. JS code creating array object should
855 // search for the 'Array' property on the global object and use that one
856 // as the constructor. 'Array' property on a global object can be
857 // overwritten by JS code.
858 global_context()->set_array_function(*array_function);
859 }
860
861 { // --- N u m b e r ---
862 Handle<JSFunction> number_fun =
863 InstallFunction(global, "Number", JS_VALUE_TYPE, JSValue::kSize,
Steve Block44f0eee2011-05-26 01:26:41 +0100864 isolate->initial_object_prototype(),
865 Builtins::kIllegal, true);
Steve Blocka7e24c12009-10-30 11:49:00 +0000866 global_context()->set_number_function(*number_fun);
867 }
868
869 { // --- B o o l e a n ---
870 Handle<JSFunction> boolean_fun =
871 InstallFunction(global, "Boolean", JS_VALUE_TYPE, JSValue::kSize,
Steve Block44f0eee2011-05-26 01:26:41 +0100872 isolate->initial_object_prototype(),
873 Builtins::kIllegal, true);
Steve Blocka7e24c12009-10-30 11:49:00 +0000874 global_context()->set_boolean_function(*boolean_fun);
875 }
876
877 { // --- S t r i n g ---
878 Handle<JSFunction> string_fun =
879 InstallFunction(global, "String", JS_VALUE_TYPE, JSValue::kSize,
Steve Block44f0eee2011-05-26 01:26:41 +0100880 isolate->initial_object_prototype(),
881 Builtins::kIllegal, true);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100882 string_fun->shared()->set_construct_stub(
Steve Block44f0eee2011-05-26 01:26:41 +0100883 isolate->builtins()->builtin(Builtins::kStringConstructCode));
Steve Blocka7e24c12009-10-30 11:49:00 +0000884 global_context()->set_string_function(*string_fun);
885 // Add 'length' property to strings.
886 Handle<DescriptorArray> string_descriptors =
Steve Block44f0eee2011-05-26 01:26:41 +0100887 factory->CopyAppendProxyDescriptor(
888 factory->empty_descriptor_array(),
889 factory->length_symbol(),
890 factory->NewProxy(&Accessors::StringLength),
Steve Blocka7e24c12009-10-30 11:49:00 +0000891 static_cast<PropertyAttributes>(DONT_ENUM |
892 DONT_DELETE |
893 READ_ONLY));
894
895 Handle<Map> string_map =
896 Handle<Map>(global_context()->string_function()->initial_map());
897 string_map->set_instance_descriptors(*string_descriptors);
898 }
899
900 { // --- D a t e ---
901 // Builtin functions for Date.prototype.
902 Handle<JSFunction> date_fun =
903 InstallFunction(global, "Date", JS_VALUE_TYPE, JSValue::kSize,
Steve Block44f0eee2011-05-26 01:26:41 +0100904 isolate->initial_object_prototype(),
905 Builtins::kIllegal, true);
Steve Blocka7e24c12009-10-30 11:49:00 +0000906
907 global_context()->set_date_function(*date_fun);
908 }
909
910
911 { // -- R e g E x p
912 // Builtin functions for RegExp.prototype.
913 Handle<JSFunction> regexp_fun =
914 InstallFunction(global, "RegExp", JS_REGEXP_TYPE, JSRegExp::kSize,
Steve Block44f0eee2011-05-26 01:26:41 +0100915 isolate->initial_object_prototype(),
916 Builtins::kIllegal, true);
Steve Blocka7e24c12009-10-30 11:49:00 +0000917 global_context()->set_regexp_function(*regexp_fun);
Steve Block6ded16b2010-05-10 14:33:55 +0100918
919 ASSERT(regexp_fun->has_initial_map());
920 Handle<Map> initial_map(regexp_fun->initial_map());
921
922 ASSERT_EQ(0, initial_map->inobject_properties());
923
Steve Block44f0eee2011-05-26 01:26:41 +0100924 Handle<DescriptorArray> descriptors = factory->NewDescriptorArray(5);
Steve Block6ded16b2010-05-10 14:33:55 +0100925 PropertyAttributes final =
926 static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE | READ_ONLY);
927 int enum_index = 0;
928 {
929 // ECMA-262, section 15.10.7.1.
Steve Block44f0eee2011-05-26 01:26:41 +0100930 FieldDescriptor field(heap->source_symbol(),
Steve Block6ded16b2010-05-10 14:33:55 +0100931 JSRegExp::kSourceFieldIndex,
932 final,
933 enum_index++);
934 descriptors->Set(0, &field);
935 }
936 {
937 // ECMA-262, section 15.10.7.2.
Steve Block44f0eee2011-05-26 01:26:41 +0100938 FieldDescriptor field(heap->global_symbol(),
Steve Block6ded16b2010-05-10 14:33:55 +0100939 JSRegExp::kGlobalFieldIndex,
940 final,
941 enum_index++);
942 descriptors->Set(1, &field);
943 }
944 {
945 // ECMA-262, section 15.10.7.3.
Steve Block44f0eee2011-05-26 01:26:41 +0100946 FieldDescriptor field(heap->ignore_case_symbol(),
Steve Block6ded16b2010-05-10 14:33:55 +0100947 JSRegExp::kIgnoreCaseFieldIndex,
948 final,
949 enum_index++);
950 descriptors->Set(2, &field);
951 }
952 {
953 // ECMA-262, section 15.10.7.4.
Steve Block44f0eee2011-05-26 01:26:41 +0100954 FieldDescriptor field(heap->multiline_symbol(),
Steve Block6ded16b2010-05-10 14:33:55 +0100955 JSRegExp::kMultilineFieldIndex,
956 final,
957 enum_index++);
958 descriptors->Set(3, &field);
959 }
960 {
961 // ECMA-262, section 15.10.7.5.
962 PropertyAttributes writable =
963 static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE);
Steve Block44f0eee2011-05-26 01:26:41 +0100964 FieldDescriptor field(heap->last_index_symbol(),
Steve Block6ded16b2010-05-10 14:33:55 +0100965 JSRegExp::kLastIndexFieldIndex,
966 writable,
967 enum_index++);
968 descriptors->Set(4, &field);
969 }
970 descriptors->SetNextEnumerationIndex(enum_index);
971 descriptors->Sort();
972
973 initial_map->set_inobject_properties(5);
974 initial_map->set_pre_allocated_property_fields(5);
975 initial_map->set_unused_property_fields(0);
976 initial_map->set_instance_size(
977 initial_map->instance_size() + 5 * kPointerSize);
978 initial_map->set_instance_descriptors(*descriptors);
Iain Merrick75681382010-08-19 15:07:18 +0100979 initial_map->set_visitor_id(StaticVisitorBase::GetVisitorId(*initial_map));
Steve Blocka7e24c12009-10-30 11:49:00 +0000980 }
981
982 { // -- J S O N
Steve Block44f0eee2011-05-26 01:26:41 +0100983 Handle<String> name = factory->NewStringFromAscii(CStrVector("JSON"));
984 Handle<JSFunction> cons = factory->NewFunction(
Steve Blocka7e24c12009-10-30 11:49:00 +0000985 name,
Steve Block44f0eee2011-05-26 01:26:41 +0100986 factory->the_hole_value());
Steve Blocka7e24c12009-10-30 11:49:00 +0000987 cons->SetInstancePrototype(global_context()->initial_object_prototype());
988 cons->SetInstanceClassName(*name);
Steve Block44f0eee2011-05-26 01:26:41 +0100989 Handle<JSObject> json_object = factory->NewJSObject(cons, TENURED);
Steve Blocka7e24c12009-10-30 11:49:00 +0000990 ASSERT(json_object->IsJSObject());
Steve Block1e0659c2011-05-24 12:43:12 +0100991 SetLocalPropertyNoThrow(global, name, json_object, DONT_ENUM);
Steve Blocka7e24c12009-10-30 11:49:00 +0000992 global_context()->set_json_object(*json_object);
993 }
994
995 { // --- arguments_boilerplate_
996 // Make sure we can recognize argument objects at runtime.
997 // This is done by introducing an anonymous function with
998 // class_name equals 'Arguments'.
Steve Block44f0eee2011-05-26 01:26:41 +0100999 Handle<String> symbol = factory->LookupAsciiSymbol("Arguments");
1000 Handle<Code> code = Handle<Code>(
1001 isolate->builtins()->builtin(Builtins::kIllegal));
Steve Blocka7e24c12009-10-30 11:49:00 +00001002 Handle<JSObject> prototype =
1003 Handle<JSObject>(
1004 JSObject::cast(global_context()->object_function()->prototype()));
1005
1006 Handle<JSFunction> function =
Steve Block44f0eee2011-05-26 01:26:41 +01001007 factory->NewFunctionWithPrototype(symbol,
Steve Blocka7e24c12009-10-30 11:49:00 +00001008 JS_OBJECT_TYPE,
1009 JSObject::kHeaderSize,
1010 prototype,
1011 code,
1012 false);
1013 ASSERT(!function->has_initial_map());
1014 function->shared()->set_instance_class_name(*symbol);
1015 function->shared()->set_expected_nof_properties(2);
Steve Block44f0eee2011-05-26 01:26:41 +01001016 Handle<JSObject> result = factory->NewJSObject(function);
Steve Blocka7e24c12009-10-30 11:49:00 +00001017
1018 global_context()->set_arguments_boilerplate(*result);
Steve Block44f0eee2011-05-26 01:26:41 +01001019 // Note: length must be added as the first property and
1020 // callee must be added as the second property.
1021 SetLocalPropertyNoThrow(result, factory->length_symbol(),
1022 factory->undefined_value(),
Steve Block1e0659c2011-05-24 12:43:12 +01001023 DONT_ENUM);
Steve Block44f0eee2011-05-26 01:26:41 +01001024 SetLocalPropertyNoThrow(result, factory->callee_symbol(),
1025 factory->undefined_value(),
Steve Block1e0659c2011-05-24 12:43:12 +01001026 DONT_ENUM);
Steve Blocka7e24c12009-10-30 11:49:00 +00001027
1028#ifdef DEBUG
1029 LookupResult lookup;
Steve Block44f0eee2011-05-26 01:26:41 +01001030 result->LocalLookup(heap->callee_symbol(), &lookup);
Andrei Popescu402d9372010-02-26 13:31:12 +00001031 ASSERT(lookup.IsProperty() && (lookup.type() == FIELD));
Steve Block44f0eee2011-05-26 01:26:41 +01001032 ASSERT(lookup.GetFieldIndex() == Heap::kArgumentsCalleeIndex);
Steve Blocka7e24c12009-10-30 11:49:00 +00001033
Steve Block44f0eee2011-05-26 01:26:41 +01001034 result->LocalLookup(heap->length_symbol(), &lookup);
Andrei Popescu402d9372010-02-26 13:31:12 +00001035 ASSERT(lookup.IsProperty() && (lookup.type() == FIELD));
Steve Block44f0eee2011-05-26 01:26:41 +01001036 ASSERT(lookup.GetFieldIndex() == Heap::kArgumentsLengthIndex);
Steve Blocka7e24c12009-10-30 11:49:00 +00001037
Steve Block44f0eee2011-05-26 01:26:41 +01001038 ASSERT(result->map()->inobject_properties() > Heap::kArgumentsCalleeIndex);
1039 ASSERT(result->map()->inobject_properties() > Heap::kArgumentsLengthIndex);
1040
1041 // Check the state of the object.
1042 ASSERT(result->HasFastProperties());
1043 ASSERT(result->HasFastElements());
1044#endif
1045 }
1046
1047 { // --- strict mode arguments boilerplate
1048 const PropertyAttributes attributes =
1049 static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE | READ_ONLY);
1050
1051 // Create the ThrowTypeError functions.
1052 Handle<FixedArray> callee = factory->NewFixedArray(2, TENURED);
1053 Handle<FixedArray> caller = factory->NewFixedArray(2, TENURED);
1054
1055 Handle<JSFunction> callee_throw =
1056 CreateThrowTypeErrorFunction(Builtins::kStrictArgumentsCallee);
1057 Handle<JSFunction> caller_throw =
1058 CreateThrowTypeErrorFunction(Builtins::kStrictArgumentsCaller);
1059
1060 // Install the ThrowTypeError functions.
1061 callee->set(0, *callee_throw);
1062 callee->set(1, *callee_throw);
1063 caller->set(0, *caller_throw);
1064 caller->set(1, *caller_throw);
1065
1066 // Create the descriptor array for the arguments object.
1067 Handle<DescriptorArray> descriptors = factory->NewDescriptorArray(3);
1068 { // length
1069 FieldDescriptor d(*factory->length_symbol(), 0, DONT_ENUM);
1070 descriptors->Set(0, &d);
1071 }
1072 { // callee
1073 CallbacksDescriptor d(*factory->callee_symbol(), *callee, attributes);
1074 descriptors->Set(1, &d);
1075 }
1076 { // caller
1077 CallbacksDescriptor d(*factory->caller_symbol(), *caller, attributes);
1078 descriptors->Set(2, &d);
1079 }
1080 descriptors->Sort();
1081
1082 // Create the map. Allocate one in-object field for length.
1083 Handle<Map> map = factory->NewMap(JS_OBJECT_TYPE,
1084 Heap::kArgumentsObjectSizeStrict);
1085 map->set_instance_descriptors(*descriptors);
1086 map->set_function_with_prototype(true);
1087 map->set_prototype(global_context()->object_function()->prototype());
1088 map->set_pre_allocated_property_fields(1);
1089 map->set_inobject_properties(1);
1090
1091 // Copy constructor from the non-strict arguments boilerplate.
1092 map->set_constructor(
1093 global_context()->arguments_boilerplate()->map()->constructor());
1094
1095 // Allocate the arguments boilerplate object.
1096 Handle<JSObject> result = factory->NewJSObjectFromMap(map);
1097 global_context()->set_strict_mode_arguments_boilerplate(*result);
1098
1099 // Add length property only for strict mode boilerplate.
1100 SetLocalPropertyNoThrow(result, factory->length_symbol(),
1101 factory->undefined_value(),
1102 DONT_ENUM);
1103
1104#ifdef DEBUG
1105 LookupResult lookup;
1106 result->LocalLookup(heap->length_symbol(), &lookup);
1107 ASSERT(lookup.IsProperty() && (lookup.type() == FIELD));
1108 ASSERT(lookup.GetFieldIndex() == Heap::kArgumentsLengthIndex);
1109
1110 ASSERT(result->map()->inobject_properties() > Heap::kArgumentsLengthIndex);
Steve Blocka7e24c12009-10-30 11:49:00 +00001111
1112 // Check the state of the object.
1113 ASSERT(result->HasFastProperties());
1114 ASSERT(result->HasFastElements());
1115#endif
1116 }
1117
1118 { // --- context extension
1119 // Create a function for the context extension objects.
Steve Block44f0eee2011-05-26 01:26:41 +01001120 Handle<Code> code = Handle<Code>(
1121 isolate->builtins()->builtin(Builtins::kIllegal));
Steve Blocka7e24c12009-10-30 11:49:00 +00001122 Handle<JSFunction> context_extension_fun =
Steve Block44f0eee2011-05-26 01:26:41 +01001123 factory->NewFunction(factory->empty_symbol(),
Steve Blocka7e24c12009-10-30 11:49:00 +00001124 JS_CONTEXT_EXTENSION_OBJECT_TYPE,
1125 JSObject::kHeaderSize,
1126 code,
1127 true);
1128
Steve Block44f0eee2011-05-26 01:26:41 +01001129 Handle<String> name = factory->LookupAsciiSymbol("context_extension");
Steve Blocka7e24c12009-10-30 11:49:00 +00001130 context_extension_fun->shared()->set_instance_class_name(*name);
1131 global_context()->set_context_extension_function(*context_extension_fun);
1132 }
1133
1134
1135 {
1136 // Setup the call-as-function delegate.
1137 Handle<Code> code =
Steve Block44f0eee2011-05-26 01:26:41 +01001138 Handle<Code>(isolate->builtins()->builtin(
1139 Builtins::kHandleApiCallAsFunction));
Steve Blocka7e24c12009-10-30 11:49:00 +00001140 Handle<JSFunction> delegate =
Steve Block44f0eee2011-05-26 01:26:41 +01001141 factory->NewFunction(factory->empty_symbol(), JS_OBJECT_TYPE,
Steve Blocka7e24c12009-10-30 11:49:00 +00001142 JSObject::kHeaderSize, code, true);
1143 global_context()->set_call_as_function_delegate(*delegate);
1144 delegate->shared()->DontAdaptArguments();
1145 }
1146
1147 {
1148 // Setup the call-as-constructor delegate.
1149 Handle<Code> code =
Steve Block44f0eee2011-05-26 01:26:41 +01001150 Handle<Code>(isolate->builtins()->builtin(
1151 Builtins::kHandleApiCallAsConstructor));
Steve Blocka7e24c12009-10-30 11:49:00 +00001152 Handle<JSFunction> delegate =
Steve Block44f0eee2011-05-26 01:26:41 +01001153 factory->NewFunction(factory->empty_symbol(), JS_OBJECT_TYPE,
Steve Blocka7e24c12009-10-30 11:49:00 +00001154 JSObject::kHeaderSize, code, true);
1155 global_context()->set_call_as_constructor_delegate(*delegate);
1156 delegate->shared()->DontAdaptArguments();
1157 }
1158
Steve Blocka7e24c12009-10-30 11:49:00 +00001159 // Initialize the out of memory slot.
Steve Block44f0eee2011-05-26 01:26:41 +01001160 global_context()->set_out_of_memory(heap->false_value());
Steve Blocka7e24c12009-10-30 11:49:00 +00001161
1162 // Initialize the data slot.
Steve Block44f0eee2011-05-26 01:26:41 +01001163 global_context()->set_data(heap->undefined_value());
Steve Blocka7e24c12009-10-30 11:49:00 +00001164}
1165
1166
Steve Block053d10c2011-06-13 19:13:29 +01001167bool Genesis::CompileBuiltin(int index) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001168 Vector<const char> name = Natives::GetScriptName(index);
Steve Block44f0eee2011-05-26 01:26:41 +01001169 Handle<String> source_code =
Steve Block053d10c2011-06-13 19:13:29 +01001170 Isolate::Current()->bootstrapper()->NativesSourceLookup(index);
Steve Blocka7e24c12009-10-30 11:49:00 +00001171 return CompileNative(name, source_code);
1172}
1173
1174
1175bool Genesis::CompileNative(Vector<const char> name, Handle<String> source) {
1176 HandleScope scope;
Steve Block053d10c2011-06-13 19:13:29 +01001177 Isolate* isolate = Isolate::Current();
Steve Blocka7e24c12009-10-30 11:49:00 +00001178#ifdef ENABLE_DEBUGGER_SUPPORT
Steve Block44f0eee2011-05-26 01:26:41 +01001179 isolate->debugger()->set_compiling_natives(true);
Steve Blocka7e24c12009-10-30 11:49:00 +00001180#endif
Andrei Popescu31002712010-02-23 13:46:05 +00001181 bool result = CompileScriptCached(name,
1182 source,
1183 NULL,
1184 NULL,
Steve Block44f0eee2011-05-26 01:26:41 +01001185 Handle<Context>(isolate->context()),
Andrei Popescu31002712010-02-23 13:46:05 +00001186 true);
Steve Block44f0eee2011-05-26 01:26:41 +01001187 ASSERT(isolate->has_pending_exception() != result);
1188 if (!result) isolate->clear_pending_exception();
Steve Blocka7e24c12009-10-30 11:49:00 +00001189#ifdef ENABLE_DEBUGGER_SUPPORT
Steve Block44f0eee2011-05-26 01:26:41 +01001190 isolate->debugger()->set_compiling_natives(false);
Steve Blocka7e24c12009-10-30 11:49:00 +00001191#endif
1192 return result;
1193}
1194
1195
1196bool Genesis::CompileScriptCached(Vector<const char> name,
1197 Handle<String> source,
1198 SourceCodeCache* cache,
1199 v8::Extension* extension,
Andrei Popescu31002712010-02-23 13:46:05 +00001200 Handle<Context> top_context,
Steve Blocka7e24c12009-10-30 11:49:00 +00001201 bool use_runtime_context) {
Steve Block053d10c2011-06-13 19:13:29 +01001202 Factory* factory = Isolate::Current()->factory();
Steve Blocka7e24c12009-10-30 11:49:00 +00001203 HandleScope scope;
Steve Block6ded16b2010-05-10 14:33:55 +01001204 Handle<SharedFunctionInfo> function_info;
Steve Blocka7e24c12009-10-30 11:49:00 +00001205
1206 // If we can't find the function in the cache, we compile a new
1207 // function and insert it into the cache.
Steve Block6ded16b2010-05-10 14:33:55 +01001208 if (cache == NULL || !cache->Lookup(name, &function_info)) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001209 ASSERT(source->IsAsciiRepresentation());
Steve Block44f0eee2011-05-26 01:26:41 +01001210 Handle<String> script_name = factory->NewStringFromUtf8(name);
Steve Block6ded16b2010-05-10 14:33:55 +01001211 function_info = Compiler::Compile(
Andrei Popescu31002712010-02-23 13:46:05 +00001212 source,
1213 script_name,
1214 0,
1215 0,
1216 extension,
1217 NULL,
Andrei Popescu402d9372010-02-26 13:31:12 +00001218 Handle<String>::null(),
Andrei Popescu31002712010-02-23 13:46:05 +00001219 use_runtime_context ? NATIVES_CODE : NOT_NATIVES_CODE);
Steve Block6ded16b2010-05-10 14:33:55 +01001220 if (function_info.is_null()) return false;
1221 if (cache != NULL) cache->Add(name, function_info);
Steve Blocka7e24c12009-10-30 11:49:00 +00001222 }
1223
1224 // Setup the function context. Conceptually, we should clone the
1225 // function before overwriting the context but since we're in a
1226 // single-threaded environment it is not strictly necessary.
Andrei Popescu31002712010-02-23 13:46:05 +00001227 ASSERT(top_context->IsGlobalContext());
Steve Blocka7e24c12009-10-30 11:49:00 +00001228 Handle<Context> context =
1229 Handle<Context>(use_runtime_context
Andrei Popescu31002712010-02-23 13:46:05 +00001230 ? Handle<Context>(top_context->runtime_context())
1231 : top_context);
Steve Blocka7e24c12009-10-30 11:49:00 +00001232 Handle<JSFunction> fun =
Steve Block44f0eee2011-05-26 01:26:41 +01001233 factory->NewFunctionFromSharedFunctionInfo(function_info, context);
Steve Blocka7e24c12009-10-30 11:49:00 +00001234
Leon Clarke4515c472010-02-03 11:58:03 +00001235 // Call function using either the runtime object or the global
Steve Blocka7e24c12009-10-30 11:49:00 +00001236 // object as the receiver. Provide no parameters.
1237 Handle<Object> receiver =
1238 Handle<Object>(use_runtime_context
Andrei Popescu31002712010-02-23 13:46:05 +00001239 ? top_context->builtins()
1240 : top_context->global());
Steve Blocka7e24c12009-10-30 11:49:00 +00001241 bool has_pending_exception;
1242 Handle<Object> result =
1243 Execution::Call(fun, receiver, 0, NULL, &has_pending_exception);
1244 if (has_pending_exception) return false;
Andrei Popescu402d9372010-02-26 13:31:12 +00001245 return true;
Steve Blocka7e24c12009-10-30 11:49:00 +00001246}
1247
1248
Steve Block053d10c2011-06-13 19:13:29 +01001249#define INSTALL_NATIVE(Type, name, var) \
1250 Handle<String> var##_name = factory->LookupAsciiSymbol(name); \
1251 Object* var##_native = \
1252 global_context()->builtins()->GetPropertyNoExceptionThrown(*var##_name); \
Ben Murdoch8b112d22011-06-08 16:22:53 +01001253 global_context()->set_##var(Type::cast(var##_native));
Steve Blocka7e24c12009-10-30 11:49:00 +00001254
Steve Block44f0eee2011-05-26 01:26:41 +01001255
Steve Blocka7e24c12009-10-30 11:49:00 +00001256void Genesis::InstallNativeFunctions() {
Steve Block053d10c2011-06-13 19:13:29 +01001257 Factory* factory = Isolate::Current()->factory();
Steve Blocka7e24c12009-10-30 11:49:00 +00001258 HandleScope scope;
1259 INSTALL_NATIVE(JSFunction, "CreateDate", create_date_fun);
1260 INSTALL_NATIVE(JSFunction, "ToNumber", to_number_fun);
1261 INSTALL_NATIVE(JSFunction, "ToString", to_string_fun);
1262 INSTALL_NATIVE(JSFunction, "ToDetailString", to_detail_string_fun);
1263 INSTALL_NATIVE(JSFunction, "ToObject", to_object_fun);
1264 INSTALL_NATIVE(JSFunction, "ToInteger", to_integer_fun);
1265 INSTALL_NATIVE(JSFunction, "ToUint32", to_uint32_fun);
1266 INSTALL_NATIVE(JSFunction, "ToInt32", to_int32_fun);
Leon Clarkee46be812010-01-19 14:06:41 +00001267 INSTALL_NATIVE(JSFunction, "GlobalEval", global_eval_fun);
Steve Blocka7e24c12009-10-30 11:49:00 +00001268 INSTALL_NATIVE(JSFunction, "Instantiate", instantiate_fun);
1269 INSTALL_NATIVE(JSFunction, "ConfigureTemplateInstance",
1270 configure_instance_fun);
Steve Blocka7e24c12009-10-30 11:49:00 +00001271 INSTALL_NATIVE(JSFunction, "GetStackTraceLine", get_stack_trace_line_fun);
1272 INSTALL_NATIVE(JSObject, "functionCache", function_cache);
1273}
1274
1275#undef INSTALL_NATIVE
1276
1277
1278bool Genesis::InstallNatives() {
1279 HandleScope scope;
Steve Block053d10c2011-06-13 19:13:29 +01001280 Isolate* isolate = Isolate::Current();
1281 Factory* factory = isolate->factory();
1282 Heap* heap = isolate->heap();
Steve Blocka7e24c12009-10-30 11:49:00 +00001283
1284 // Create a function for the builtins object. Allocate space for the
1285 // JavaScript builtins, a reference to the builtins object
1286 // (itself) and a reference to the global_context directly in the object.
Steve Block44f0eee2011-05-26 01:26:41 +01001287 Handle<Code> code = Handle<Code>(
Steve Block053d10c2011-06-13 19:13:29 +01001288 isolate->builtins()->builtin(Builtins::kIllegal));
Steve Blocka7e24c12009-10-30 11:49:00 +00001289 Handle<JSFunction> builtins_fun =
Steve Block053d10c2011-06-13 19:13:29 +01001290 factory->NewFunction(factory->empty_symbol(), JS_BUILTINS_OBJECT_TYPE,
1291 JSBuiltinsObject::kSize, code, true);
Steve Blocka7e24c12009-10-30 11:49:00 +00001292
Steve Block053d10c2011-06-13 19:13:29 +01001293 Handle<String> name = factory->LookupAsciiSymbol("builtins");
Steve Blocka7e24c12009-10-30 11:49:00 +00001294 builtins_fun->shared()->set_instance_class_name(*name);
1295
1296 // Allocate the builtins object.
1297 Handle<JSBuiltinsObject> builtins =
Steve Block053d10c2011-06-13 19:13:29 +01001298 Handle<JSBuiltinsObject>::cast(factory->NewGlobalObject(builtins_fun));
Steve Blocka7e24c12009-10-30 11:49:00 +00001299 builtins->set_builtins(*builtins);
1300 builtins->set_global_context(*global_context());
1301 builtins->set_global_receiver(*builtins);
1302
1303 // Setup the 'global' properties of the builtins object. The
1304 // 'global' property that refers to the global object is the only
1305 // way to get from code running in the builtins context to the
1306 // global object.
1307 static const PropertyAttributes attributes =
1308 static_cast<PropertyAttributes>(READ_ONLY | DONT_DELETE);
Steve Block053d10c2011-06-13 19:13:29 +01001309 Handle<String> global_symbol = factory->LookupAsciiSymbol("global");
Steve Block1e0659c2011-05-24 12:43:12 +01001310 Handle<Object> global_obj(global_context()->global());
1311 SetLocalPropertyNoThrow(builtins, global_symbol, global_obj, attributes);
Steve Blocka7e24c12009-10-30 11:49:00 +00001312
1313 // Setup the reference from the global object to the builtins object.
1314 JSGlobalObject::cast(global_context()->global())->set_builtins(*builtins);
1315
1316 // Create a bridge function that has context in the global context.
1317 Handle<JSFunction> bridge =
Steve Block053d10c2011-06-13 19:13:29 +01001318 factory->NewFunction(factory->empty_symbol(), factory->undefined_value());
1319 ASSERT(bridge->context() == *isolate->global_context());
Steve Blocka7e24c12009-10-30 11:49:00 +00001320
1321 // Allocate the builtins context.
1322 Handle<Context> context =
Steve Block053d10c2011-06-13 19:13:29 +01001323 factory->NewFunctionContext(Context::MIN_CONTEXT_SLOTS, bridge);
Steve Blocka7e24c12009-10-30 11:49:00 +00001324 context->set_global(*builtins); // override builtins global object
1325
1326 global_context()->set_runtime_context(*context);
1327
1328 { // -- S c r i p t
1329 // Builtin functions for Script.
1330 Handle<JSFunction> script_fun =
1331 InstallFunction(builtins, "Script", JS_VALUE_TYPE, JSValue::kSize,
Steve Block053d10c2011-06-13 19:13:29 +01001332 isolate->initial_object_prototype(),
Steve Block44f0eee2011-05-26 01:26:41 +01001333 Builtins::kIllegal, false);
Steve Blocka7e24c12009-10-30 11:49:00 +00001334 Handle<JSObject> prototype =
Steve Block053d10c2011-06-13 19:13:29 +01001335 factory->NewJSObject(isolate->object_function(), TENURED);
Steve Blocka7e24c12009-10-30 11:49:00 +00001336 SetPrototype(script_fun, prototype);
1337 global_context()->set_script_function(*script_fun);
1338
1339 // Add 'source' and 'data' property to scripts.
1340 PropertyAttributes common_attributes =
1341 static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE | READ_ONLY);
Steve Block053d10c2011-06-13 19:13:29 +01001342 Handle<Proxy> proxy_source = factory->NewProxy(&Accessors::ScriptSource);
Steve Blocka7e24c12009-10-30 11:49:00 +00001343 Handle<DescriptorArray> script_descriptors =
Steve Block053d10c2011-06-13 19:13:29 +01001344 factory->CopyAppendProxyDescriptor(
1345 factory->empty_descriptor_array(),
1346 factory->LookupAsciiSymbol("source"),
Steve Blocka7e24c12009-10-30 11:49:00 +00001347 proxy_source,
1348 common_attributes);
Steve Block053d10c2011-06-13 19:13:29 +01001349 Handle<Proxy> proxy_name = factory->NewProxy(&Accessors::ScriptName);
Steve Blocka7e24c12009-10-30 11:49:00 +00001350 script_descriptors =
Steve Block053d10c2011-06-13 19:13:29 +01001351 factory->CopyAppendProxyDescriptor(
Steve Blocka7e24c12009-10-30 11:49:00 +00001352 script_descriptors,
Steve Block053d10c2011-06-13 19:13:29 +01001353 factory->LookupAsciiSymbol("name"),
Steve Blocka7e24c12009-10-30 11:49:00 +00001354 proxy_name,
1355 common_attributes);
Steve Block053d10c2011-06-13 19:13:29 +01001356 Handle<Proxy> proxy_id = factory->NewProxy(&Accessors::ScriptId);
Steve Blocka7e24c12009-10-30 11:49:00 +00001357 script_descriptors =
Steve Block053d10c2011-06-13 19:13:29 +01001358 factory->CopyAppendProxyDescriptor(
Steve Blocka7e24c12009-10-30 11:49:00 +00001359 script_descriptors,
Steve Block053d10c2011-06-13 19:13:29 +01001360 factory->LookupAsciiSymbol("id"),
Steve Blocka7e24c12009-10-30 11:49:00 +00001361 proxy_id,
1362 common_attributes);
1363 Handle<Proxy> proxy_line_offset =
Steve Block053d10c2011-06-13 19:13:29 +01001364 factory->NewProxy(&Accessors::ScriptLineOffset);
Steve Blocka7e24c12009-10-30 11:49:00 +00001365 script_descriptors =
Steve Block053d10c2011-06-13 19:13:29 +01001366 factory->CopyAppendProxyDescriptor(
Steve Blocka7e24c12009-10-30 11:49:00 +00001367 script_descriptors,
Steve Block053d10c2011-06-13 19:13:29 +01001368 factory->LookupAsciiSymbol("line_offset"),
Steve Blocka7e24c12009-10-30 11:49:00 +00001369 proxy_line_offset,
1370 common_attributes);
1371 Handle<Proxy> proxy_column_offset =
Steve Block053d10c2011-06-13 19:13:29 +01001372 factory->NewProxy(&Accessors::ScriptColumnOffset);
Steve Blocka7e24c12009-10-30 11:49:00 +00001373 script_descriptors =
Steve Block053d10c2011-06-13 19:13:29 +01001374 factory->CopyAppendProxyDescriptor(
Steve Blocka7e24c12009-10-30 11:49:00 +00001375 script_descriptors,
Steve Block053d10c2011-06-13 19:13:29 +01001376 factory->LookupAsciiSymbol("column_offset"),
Steve Blocka7e24c12009-10-30 11:49:00 +00001377 proxy_column_offset,
1378 common_attributes);
Steve Block053d10c2011-06-13 19:13:29 +01001379 Handle<Proxy> proxy_data = factory->NewProxy(&Accessors::ScriptData);
Steve Blocka7e24c12009-10-30 11:49:00 +00001380 script_descriptors =
Steve Block053d10c2011-06-13 19:13:29 +01001381 factory->CopyAppendProxyDescriptor(
Steve Blocka7e24c12009-10-30 11:49:00 +00001382 script_descriptors,
Steve Block053d10c2011-06-13 19:13:29 +01001383 factory->LookupAsciiSymbol("data"),
Steve Blocka7e24c12009-10-30 11:49:00 +00001384 proxy_data,
1385 common_attributes);
Steve Block053d10c2011-06-13 19:13:29 +01001386 Handle<Proxy> proxy_type = factory->NewProxy(&Accessors::ScriptType);
Steve Blocka7e24c12009-10-30 11:49:00 +00001387 script_descriptors =
Steve Block053d10c2011-06-13 19:13:29 +01001388 factory->CopyAppendProxyDescriptor(
Steve Blocka7e24c12009-10-30 11:49:00 +00001389 script_descriptors,
Steve Block053d10c2011-06-13 19:13:29 +01001390 factory->LookupAsciiSymbol("type"),
Steve Blocka7e24c12009-10-30 11:49:00 +00001391 proxy_type,
1392 common_attributes);
1393 Handle<Proxy> proxy_compilation_type =
Steve Block053d10c2011-06-13 19:13:29 +01001394 factory->NewProxy(&Accessors::ScriptCompilationType);
Steve Blocka7e24c12009-10-30 11:49:00 +00001395 script_descriptors =
Steve Block053d10c2011-06-13 19:13:29 +01001396 factory->CopyAppendProxyDescriptor(
Steve Blocka7e24c12009-10-30 11:49:00 +00001397 script_descriptors,
Steve Block053d10c2011-06-13 19:13:29 +01001398 factory->LookupAsciiSymbol("compilation_type"),
Steve Blocka7e24c12009-10-30 11:49:00 +00001399 proxy_compilation_type,
1400 common_attributes);
1401 Handle<Proxy> proxy_line_ends =
Steve Block053d10c2011-06-13 19:13:29 +01001402 factory->NewProxy(&Accessors::ScriptLineEnds);
Steve Blocka7e24c12009-10-30 11:49:00 +00001403 script_descriptors =
Steve Block053d10c2011-06-13 19:13:29 +01001404 factory->CopyAppendProxyDescriptor(
Steve Blocka7e24c12009-10-30 11:49:00 +00001405 script_descriptors,
Steve Block053d10c2011-06-13 19:13:29 +01001406 factory->LookupAsciiSymbol("line_ends"),
Steve Blocka7e24c12009-10-30 11:49:00 +00001407 proxy_line_ends,
1408 common_attributes);
1409 Handle<Proxy> proxy_context_data =
Steve Block053d10c2011-06-13 19:13:29 +01001410 factory->NewProxy(&Accessors::ScriptContextData);
Steve Blocka7e24c12009-10-30 11:49:00 +00001411 script_descriptors =
Steve Block053d10c2011-06-13 19:13:29 +01001412 factory->CopyAppendProxyDescriptor(
Steve Blocka7e24c12009-10-30 11:49:00 +00001413 script_descriptors,
Steve Block053d10c2011-06-13 19:13:29 +01001414 factory->LookupAsciiSymbol("context_data"),
Steve Blocka7e24c12009-10-30 11:49:00 +00001415 proxy_context_data,
1416 common_attributes);
Steve Blockd0582a62009-12-15 09:54:21 +00001417 Handle<Proxy> proxy_eval_from_script =
Steve Block053d10c2011-06-13 19:13:29 +01001418 factory->NewProxy(&Accessors::ScriptEvalFromScript);
Steve Blocka7e24c12009-10-30 11:49:00 +00001419 script_descriptors =
Steve Block053d10c2011-06-13 19:13:29 +01001420 factory->CopyAppendProxyDescriptor(
Steve Blocka7e24c12009-10-30 11:49:00 +00001421 script_descriptors,
Steve Block053d10c2011-06-13 19:13:29 +01001422 factory->LookupAsciiSymbol("eval_from_script"),
Steve Blockd0582a62009-12-15 09:54:21 +00001423 proxy_eval_from_script,
Steve Blocka7e24c12009-10-30 11:49:00 +00001424 common_attributes);
Steve Blockd0582a62009-12-15 09:54:21 +00001425 Handle<Proxy> proxy_eval_from_script_position =
Steve Block053d10c2011-06-13 19:13:29 +01001426 factory->NewProxy(&Accessors::ScriptEvalFromScriptPosition);
Steve Blocka7e24c12009-10-30 11:49:00 +00001427 script_descriptors =
Steve Block053d10c2011-06-13 19:13:29 +01001428 factory->CopyAppendProxyDescriptor(
Steve Blocka7e24c12009-10-30 11:49:00 +00001429 script_descriptors,
Steve Block053d10c2011-06-13 19:13:29 +01001430 factory->LookupAsciiSymbol("eval_from_script_position"),
Steve Blockd0582a62009-12-15 09:54:21 +00001431 proxy_eval_from_script_position,
1432 common_attributes);
1433 Handle<Proxy> proxy_eval_from_function_name =
Steve Block053d10c2011-06-13 19:13:29 +01001434 factory->NewProxy(&Accessors::ScriptEvalFromFunctionName);
Steve Blockd0582a62009-12-15 09:54:21 +00001435 script_descriptors =
Steve Block053d10c2011-06-13 19:13:29 +01001436 factory->CopyAppendProxyDescriptor(
Steve Blockd0582a62009-12-15 09:54:21 +00001437 script_descriptors,
Steve Block053d10c2011-06-13 19:13:29 +01001438 factory->LookupAsciiSymbol("eval_from_function_name"),
Steve Blockd0582a62009-12-15 09:54:21 +00001439 proxy_eval_from_function_name,
Steve Blocka7e24c12009-10-30 11:49:00 +00001440 common_attributes);
1441
1442 Handle<Map> script_map = Handle<Map>(script_fun->initial_map());
1443 script_map->set_instance_descriptors(*script_descriptors);
1444
1445 // Allocate the empty script.
Steve Block053d10c2011-06-13 19:13:29 +01001446 Handle<Script> script = factory->NewScript(factory->empty_string());
Steve Blocka7e24c12009-10-30 11:49:00 +00001447 script->set_type(Smi::FromInt(Script::TYPE_NATIVE));
Steve Block053d10c2011-06-13 19:13:29 +01001448 heap->public_set_empty_script(*script);
Steve Blocka7e24c12009-10-30 11:49:00 +00001449 }
Steve Block6ded16b2010-05-10 14:33:55 +01001450 {
1451 // Builtin function for OpaqueReference -- a JSValue-based object,
1452 // that keeps its field isolated from JavaScript code. It may store
1453 // objects, that JavaScript code may not access.
1454 Handle<JSFunction> opaque_reference_fun =
1455 InstallFunction(builtins, "OpaqueReference", JS_VALUE_TYPE,
Steve Block44f0eee2011-05-26 01:26:41 +01001456 JSValue::kSize,
Steve Block053d10c2011-06-13 19:13:29 +01001457 isolate->initial_object_prototype(),
Steve Block44f0eee2011-05-26 01:26:41 +01001458 Builtins::kIllegal, false);
Steve Block6ded16b2010-05-10 14:33:55 +01001459 Handle<JSObject> prototype =
Steve Block053d10c2011-06-13 19:13:29 +01001460 factory->NewJSObject(isolate->object_function(), TENURED);
Steve Block6ded16b2010-05-10 14:33:55 +01001461 SetPrototype(opaque_reference_fun, prototype);
1462 global_context()->set_opaque_reference_function(*opaque_reference_fun);
1463 }
1464
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001465 { // --- I n t e r n a l A r r a y ---
1466 // An array constructor on the builtins object that works like
1467 // the public Array constructor, except that its prototype
1468 // doesn't inherit from Object.prototype.
1469 // To be used only for internal work by builtins. Instances
1470 // must not be leaked to user code.
1471 // Only works correctly when called as a constructor. The normal
1472 // Array code uses Array.prototype as prototype when called as
1473 // a function.
1474 Handle<JSFunction> array_function =
1475 InstallFunction(builtins,
1476 "InternalArray",
1477 JS_ARRAY_TYPE,
1478 JSArray::kSize,
Steve Block053d10c2011-06-13 19:13:29 +01001479 isolate->initial_object_prototype(),
Steve Block44f0eee2011-05-26 01:26:41 +01001480 Builtins::kArrayCode,
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001481 true);
1482 Handle<JSObject> prototype =
Steve Block053d10c2011-06-13 19:13:29 +01001483 factory->NewJSObject(isolate->object_function(), TENURED);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001484 SetPrototype(array_function, prototype);
1485
1486 array_function->shared()->set_construct_stub(
Steve Block053d10c2011-06-13 19:13:29 +01001487 isolate->builtins()->builtin(Builtins::kArrayConstructCode));
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001488 array_function->shared()->DontAdaptArguments();
1489
1490 // Make "length" magic on instances.
1491 Handle<DescriptorArray> array_descriptors =
Steve Block053d10c2011-06-13 19:13:29 +01001492 factory->CopyAppendProxyDescriptor(
1493 factory->empty_descriptor_array(),
1494 factory->length_symbol(),
1495 factory->NewProxy(&Accessors::ArrayLength),
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001496 static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE));
1497
1498 array_function->initial_map()->set_instance_descriptors(
1499 *array_descriptors);
1500 }
1501
Steve Block6ded16b2010-05-10 14:33:55 +01001502 if (FLAG_disable_native_files) {
1503 PrintF("Warning: Running without installed natives!\n");
1504 return true;
1505 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001506
Andrei Popescu31002712010-02-23 13:46:05 +00001507 // Install natives.
1508 for (int i = Natives::GetDebuggerCount();
1509 i < Natives::GetBuiltinsCount();
1510 i++) {
Steve Block053d10c2011-06-13 19:13:29 +01001511 Vector<const char> name = Natives::GetScriptName(i);
1512 if (!CompileBuiltin(i)) return false;
Andrei Popescu402d9372010-02-26 13:31:12 +00001513 // TODO(ager): We really only need to install the JS builtin
1514 // functions on the builtins object after compiling and running
1515 // runtime.js.
1516 if (!InstallJSBuiltins(builtins)) return false;
Steve Blocka7e24c12009-10-30 11:49:00 +00001517 }
1518
1519 InstallNativeFunctions();
1520
Iain Merrick75681382010-08-19 15:07:18 +01001521 // Store the map for the string prototype after the natives has been compiled
1522 // and the String function has been setup.
1523 Handle<JSFunction> string_function(global_context()->string_function());
1524 ASSERT(JSObject::cast(
1525 string_function->initial_map()->prototype())->HasFastProperties());
1526 global_context()->set_string_function_prototype_map(
1527 HeapObject::cast(string_function->initial_map()->prototype())->map());
1528
Steve Blocka7e24c12009-10-30 11:49:00 +00001529 // Install Function.prototype.call and apply.
Steve Block053d10c2011-06-13 19:13:29 +01001530 { Handle<String> key = factory->function_class_symbol();
Steve Blocka7e24c12009-10-30 11:49:00 +00001531 Handle<JSFunction> function =
Steve Block053d10c2011-06-13 19:13:29 +01001532 Handle<JSFunction>::cast(GetProperty(isolate->global(), key));
Steve Blocka7e24c12009-10-30 11:49:00 +00001533 Handle<JSObject> proto =
1534 Handle<JSObject>(JSObject::cast(function->instance_prototype()));
1535
1536 // Install the call and the apply functions.
1537 Handle<JSFunction> call =
1538 InstallFunction(proto, "call", JS_OBJECT_TYPE, JSObject::kHeaderSize,
Steve Block6ded16b2010-05-10 14:33:55 +01001539 Handle<JSObject>::null(),
Steve Block44f0eee2011-05-26 01:26:41 +01001540 Builtins::kFunctionCall,
Steve Blocka7e24c12009-10-30 11:49:00 +00001541 false);
1542 Handle<JSFunction> apply =
1543 InstallFunction(proto, "apply", JS_OBJECT_TYPE, JSObject::kHeaderSize,
Steve Block6ded16b2010-05-10 14:33:55 +01001544 Handle<JSObject>::null(),
Steve Block44f0eee2011-05-26 01:26:41 +01001545 Builtins::kFunctionApply,
Steve Blocka7e24c12009-10-30 11:49:00 +00001546 false);
1547
1548 // Make sure that Function.prototype.call appears to be compiled.
1549 // The code will never be called, but inline caching for call will
1550 // only work if it appears to be compiled.
1551 call->shared()->DontAdaptArguments();
1552 ASSERT(call->is_compiled());
1553
1554 // Set the expected parameters for apply to 2; required by builtin.
1555 apply->shared()->set_formal_parameter_count(2);
1556
1557 // Set the lengths for the functions to satisfy ECMA-262.
1558 call->shared()->set_length(1);
1559 apply->shared()->set_length(2);
1560 }
1561
Ben Murdoch42effa52011-08-19 16:40:31 +01001562 InstallBuiltinFunctionIds();
1563
Steve Block6ded16b2010-05-10 14:33:55 +01001564 // Create a constructor for RegExp results (a variant of Array that
1565 // predefines the two properties index and match).
1566 {
1567 // RegExpResult initial map.
1568
1569 // Find global.Array.prototype to inherit from.
1570 Handle<JSFunction> array_constructor(global_context()->array_function());
1571 Handle<JSObject> array_prototype(
1572 JSObject::cast(array_constructor->instance_prototype()));
1573
1574 // Add initial map.
1575 Handle<Map> initial_map =
Steve Block053d10c2011-06-13 19:13:29 +01001576 factory->NewMap(JS_ARRAY_TYPE, JSRegExpResult::kSize);
Steve Block6ded16b2010-05-10 14:33:55 +01001577 initial_map->set_constructor(*array_constructor);
1578
1579 // Set prototype on map.
1580 initial_map->set_non_instance_prototype(false);
1581 initial_map->set_prototype(*array_prototype);
1582
1583 // Update map with length accessor from Array and add "index" and "input".
1584 Handle<Map> array_map(global_context()->js_array_map());
1585 Handle<DescriptorArray> array_descriptors(
1586 array_map->instance_descriptors());
1587 ASSERT_EQ(1, array_descriptors->number_of_descriptors());
1588
1589 Handle<DescriptorArray> reresult_descriptors =
Steve Block053d10c2011-06-13 19:13:29 +01001590 factory->NewDescriptorArray(3);
Steve Block6ded16b2010-05-10 14:33:55 +01001591
1592 reresult_descriptors->CopyFrom(0, *array_descriptors, 0);
1593
1594 int enum_index = 0;
1595 {
Steve Block053d10c2011-06-13 19:13:29 +01001596 FieldDescriptor index_field(heap->index_symbol(),
Steve Block6ded16b2010-05-10 14:33:55 +01001597 JSRegExpResult::kIndexIndex,
1598 NONE,
1599 enum_index++);
1600 reresult_descriptors->Set(1, &index_field);
1601 }
1602
1603 {
Steve Block053d10c2011-06-13 19:13:29 +01001604 FieldDescriptor input_field(heap->input_symbol(),
Steve Block6ded16b2010-05-10 14:33:55 +01001605 JSRegExpResult::kInputIndex,
1606 NONE,
1607 enum_index++);
1608 reresult_descriptors->Set(2, &input_field);
1609 }
1610 reresult_descriptors->Sort();
1611
1612 initial_map->set_inobject_properties(2);
1613 initial_map->set_pre_allocated_property_fields(2);
1614 initial_map->set_unused_property_fields(0);
1615 initial_map->set_instance_descriptors(*reresult_descriptors);
1616
1617 global_context()->set_regexp_result_map(*initial_map);
1618 }
1619
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001620
Steve Blocka7e24c12009-10-30 11:49:00 +00001621#ifdef DEBUG
1622 builtins->Verify();
1623#endif
Andrei Popescu31002712010-02-23 13:46:05 +00001624
Steve Blocka7e24c12009-10-30 11:49:00 +00001625 return true;
1626}
1627
1628
Ben Murdochb0fe1622011-05-05 13:52:32 +01001629static Handle<JSObject> ResolveBuiltinIdHolder(
Kristian Monsen0d5e1162010-09-30 15:31:59 +01001630 Handle<Context> global_context,
1631 const char* holder_expr) {
Steve Block053d10c2011-06-13 19:13:29 +01001632 Factory* factory = Isolate::Current()->factory();
Kristian Monsen0d5e1162010-09-30 15:31:59 +01001633 Handle<GlobalObject> global(global_context->global());
1634 const char* period_pos = strchr(holder_expr, '.');
1635 if (period_pos == NULL) {
1636 return Handle<JSObject>::cast(
Steve Block44f0eee2011-05-26 01:26:41 +01001637 GetProperty(global, factory->LookupAsciiSymbol(holder_expr)));
Steve Block59151502010-09-22 15:07:15 +01001638 }
Kristian Monsen0d5e1162010-09-30 15:31:59 +01001639 ASSERT_EQ(".prototype", period_pos);
1640 Vector<const char> property(holder_expr,
1641 static_cast<int>(period_pos - holder_expr));
1642 Handle<JSFunction> function = Handle<JSFunction>::cast(
Steve Block44f0eee2011-05-26 01:26:41 +01001643 GetProperty(global, factory->LookupSymbol(property)));
Kristian Monsen0d5e1162010-09-30 15:31:59 +01001644 return Handle<JSObject>(JSObject::cast(function->prototype()));
1645}
1646
1647
Ben Murdochb0fe1622011-05-05 13:52:32 +01001648static void InstallBuiltinFunctionId(Handle<JSObject> holder,
1649 const char* function_name,
1650 BuiltinFunctionId id) {
Steve Block053d10c2011-06-13 19:13:29 +01001651 Handle<String> name = FACTORY->LookupAsciiSymbol(function_name);
John Reck59135872010-11-02 12:39:01 -07001652 Object* function_object = holder->GetProperty(*name)->ToObjectUnchecked();
1653 Handle<JSFunction> function(JSFunction::cast(function_object));
Kristian Monsen25f61362010-05-21 11:50:48 +01001654 function->shared()->set_function_data(Smi::FromInt(id));
1655}
1656
1657
Ben Murdochb0fe1622011-05-05 13:52:32 +01001658void Genesis::InstallBuiltinFunctionIds() {
Kristian Monsen25f61362010-05-21 11:50:48 +01001659 HandleScope scope;
Ben Murdochb0fe1622011-05-05 13:52:32 +01001660#define INSTALL_BUILTIN_ID(holder_expr, fun_name, name) \
1661 { \
1662 Handle<JSObject> holder = ResolveBuiltinIdHolder( \
1663 global_context(), #holder_expr); \
1664 BuiltinFunctionId id = k##name; \
1665 InstallBuiltinFunctionId(holder, #fun_name, id); \
Kristian Monsen25f61362010-05-21 11:50:48 +01001666 }
Ben Murdochb0fe1622011-05-05 13:52:32 +01001667 FUNCTIONS_WITH_ID_LIST(INSTALL_BUILTIN_ID)
1668#undef INSTALL_BUILTIN_ID
Kristian Monsen25f61362010-05-21 11:50:48 +01001669}
1670
1671
Steve Block6ded16b2010-05-10 14:33:55 +01001672// Do not forget to update macros.py with named constant
1673// of cache id.
1674#define JSFUNCTION_RESULT_CACHE_LIST(F) \
1675 F(16, global_context()->regexp_function())
1676
1677
Steve Block053d10c2011-06-13 19:13:29 +01001678static FixedArray* CreateCache(int size, JSFunction* factory) {
Steve Block6ded16b2010-05-10 14:33:55 +01001679 // Caches are supposed to live for a long time, allocate in old space.
1680 int array_size = JSFunctionResultCache::kEntriesIndex + 2 * size;
1681 // Cannot use cast as object is not fully initialized yet.
1682 JSFunctionResultCache* cache = reinterpret_cast<JSFunctionResultCache*>(
Steve Block053d10c2011-06-13 19:13:29 +01001683 *FACTORY->NewFixedArrayWithHoles(array_size, TENURED));
1684 cache->set(JSFunctionResultCache::kFactoryIndex, factory);
Steve Block6ded16b2010-05-10 14:33:55 +01001685 cache->MakeZeroSize();
1686 return cache;
1687}
1688
1689
1690void Genesis::InstallJSFunctionResultCaches() {
1691 const int kNumberOfCaches = 0 +
1692#define F(size, func) + 1
1693 JSFUNCTION_RESULT_CACHE_LIST(F)
1694#undef F
1695 ;
1696
Steve Block44f0eee2011-05-26 01:26:41 +01001697 Handle<FixedArray> caches = FACTORY->NewFixedArray(kNumberOfCaches, TENURED);
Steve Block6ded16b2010-05-10 14:33:55 +01001698
1699 int index = 0;
Kristian Monsen0d5e1162010-09-30 15:31:59 +01001700
1701#define F(size, func) do { \
1702 FixedArray* cache = CreateCache((size), (func)); \
1703 caches->set(index++, cache); \
1704 } while (false)
1705
1706 JSFUNCTION_RESULT_CACHE_LIST(F);
1707
Steve Block6ded16b2010-05-10 14:33:55 +01001708#undef F
1709
1710 global_context()->set_jsfunction_result_caches(*caches);
1711}
1712
1713
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001714void Genesis::InitializeNormalizedMapCaches() {
1715 Handle<FixedArray> array(
Steve Block44f0eee2011-05-26 01:26:41 +01001716 FACTORY->NewFixedArray(NormalizedMapCache::kEntries, TENURED));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001717 global_context()->set_normalized_map_cache(NormalizedMapCache::cast(*array));
1718}
1719
1720
Andrei Popescu31002712010-02-23 13:46:05 +00001721bool Bootstrapper::InstallExtensions(Handle<Context> global_context,
1722 v8::ExtensionConfiguration* extensions) {
Steve Block053d10c2011-06-13 19:13:29 +01001723 Isolate* isolate = Isolate::Current();
Andrei Popescu31002712010-02-23 13:46:05 +00001724 BootstrapperActive active;
Steve Block44f0eee2011-05-26 01:26:41 +01001725 SaveContext saved_context(isolate);
1726 isolate->set_context(*global_context);
Andrei Popescu31002712010-02-23 13:46:05 +00001727 if (!Genesis::InstallExtensions(global_context, extensions)) return false;
1728 Genesis::InstallSpecialObjects(global_context);
1729 return true;
1730}
1731
1732
1733void Genesis::InstallSpecialObjects(Handle<Context> global_context) {
Steve Block053d10c2011-06-13 19:13:29 +01001734 Factory* factory = Isolate::Current()->factory();
Steve Blocka7e24c12009-10-30 11:49:00 +00001735 HandleScope scope;
1736 Handle<JSGlobalObject> js_global(
Andrei Popescu31002712010-02-23 13:46:05 +00001737 JSGlobalObject::cast(global_context->global()));
Steve Blocka7e24c12009-10-30 11:49:00 +00001738 // Expose the natives in global if a name for it is specified.
1739 if (FLAG_expose_natives_as != NULL && strlen(FLAG_expose_natives_as) != 0) {
1740 Handle<String> natives_string =
Steve Block44f0eee2011-05-26 01:26:41 +01001741 factory->LookupAsciiSymbol(FLAG_expose_natives_as);
Steve Block1e0659c2011-05-24 12:43:12 +01001742 SetLocalPropertyNoThrow(js_global, natives_string,
1743 Handle<JSObject>(js_global->builtins()), DONT_ENUM);
Steve Blocka7e24c12009-10-30 11:49:00 +00001744 }
1745
1746 Handle<Object> Error = GetProperty(js_global, "Error");
1747 if (Error->IsJSObject()) {
Steve Block44f0eee2011-05-26 01:26:41 +01001748 Handle<String> name = factory->LookupAsciiSymbol("stackTraceLimit");
Steve Block1e0659c2011-05-24 12:43:12 +01001749 SetLocalPropertyNoThrow(Handle<JSObject>::cast(Error),
1750 name,
1751 Handle<Smi>(Smi::FromInt(FLAG_stack_trace_limit)),
1752 NONE);
Steve Blocka7e24c12009-10-30 11:49:00 +00001753 }
1754
1755#ifdef ENABLE_DEBUGGER_SUPPORT
1756 // Expose the debug global object in global if a name for it is specified.
1757 if (FLAG_expose_debug_as != NULL && strlen(FLAG_expose_debug_as) != 0) {
Steve Block44f0eee2011-05-26 01:26:41 +01001758 Debug* debug = Isolate::Current()->debug();
Steve Blocka7e24c12009-10-30 11:49:00 +00001759 // If loading fails we just bail out without installing the
1760 // debugger but without tanking the whole context.
Steve Block44f0eee2011-05-26 01:26:41 +01001761 if (!debug->Load()) return;
Steve Blocka7e24c12009-10-30 11:49:00 +00001762 // Set the security token for the debugger context to the same as
1763 // the shell global context to allow calling between these (otherwise
1764 // exposing debug global object doesn't make much sense).
Steve Block44f0eee2011-05-26 01:26:41 +01001765 debug->debug_context()->set_security_token(
Andrei Popescu31002712010-02-23 13:46:05 +00001766 global_context->security_token());
Steve Blocka7e24c12009-10-30 11:49:00 +00001767
1768 Handle<String> debug_string =
Steve Block44f0eee2011-05-26 01:26:41 +01001769 factory->LookupAsciiSymbol(FLAG_expose_debug_as);
1770 Handle<Object> global_proxy(debug->debug_context()->global_proxy());
Steve Block1e0659c2011-05-24 12:43:12 +01001771 SetLocalPropertyNoThrow(js_global, debug_string, global_proxy, DONT_ENUM);
Steve Blocka7e24c12009-10-30 11:49:00 +00001772 }
1773#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00001774}
1775
1776
Andrei Popescu31002712010-02-23 13:46:05 +00001777bool Genesis::InstallExtensions(Handle<Context> global_context,
1778 v8::ExtensionConfiguration* extensions) {
Steve Block44f0eee2011-05-26 01:26:41 +01001779 // TODO(isolates): Extensions on multiple isolates may take a little more
1780 // effort. (The external API reads 'ignore'-- does that mean
1781 // we can break the interface?)
1782
Steve Blocka7e24c12009-10-30 11:49:00 +00001783 // Clear coloring of extension list
1784 v8::RegisteredExtension* current = v8::RegisteredExtension::first_extension();
1785 while (current != NULL) {
1786 current->set_state(v8::UNVISITED);
1787 current = current->next();
1788 }
Andrei Popescu31002712010-02-23 13:46:05 +00001789 // Install auto extensions.
Steve Blocka7e24c12009-10-30 11:49:00 +00001790 current = v8::RegisteredExtension::first_extension();
1791 while (current != NULL) {
1792 if (current->extension()->auto_enable())
1793 InstallExtension(current);
1794 current = current->next();
1795 }
1796
1797 if (FLAG_expose_gc) InstallExtension("v8/gc");
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01001798 if (FLAG_expose_externalize_string) InstallExtension("v8/externalize");
Steve Blocka7e24c12009-10-30 11:49:00 +00001799
1800 if (extensions == NULL) return true;
1801 // Install required extensions
1802 int count = v8::ImplementationUtilities::GetNameCount(extensions);
1803 const char** names = v8::ImplementationUtilities::GetNames(extensions);
1804 for (int i = 0; i < count; i++) {
1805 if (!InstallExtension(names[i]))
1806 return false;
1807 }
1808
1809 return true;
1810}
1811
1812
1813// Installs a named extension. This methods is unoptimized and does
1814// not scale well if we want to support a large number of extensions.
1815bool Genesis::InstallExtension(const char* name) {
1816 v8::RegisteredExtension* current = v8::RegisteredExtension::first_extension();
1817 // Loop until we find the relevant extension
1818 while (current != NULL) {
1819 if (strcmp(name, current->extension()->name()) == 0) break;
1820 current = current->next();
1821 }
1822 // Didn't find the extension; fail.
1823 if (current == NULL) {
1824 v8::Utils::ReportApiFailure(
1825 "v8::Context::New()", "Cannot find required extension");
1826 return false;
1827 }
1828 return InstallExtension(current);
1829}
1830
1831
1832bool Genesis::InstallExtension(v8::RegisteredExtension* current) {
1833 HandleScope scope;
1834
1835 if (current->state() == v8::INSTALLED) return true;
1836 // The current node has already been visited so there must be a
1837 // cycle in the dependency graph; fail.
1838 if (current->state() == v8::VISITED) {
1839 v8::Utils::ReportApiFailure(
1840 "v8::Context::New()", "Circular extension dependency");
1841 return false;
1842 }
1843 ASSERT(current->state() == v8::UNVISITED);
1844 current->set_state(v8::VISITED);
1845 v8::Extension* extension = current->extension();
1846 // Install the extension's dependencies
1847 for (int i = 0; i < extension->dependency_count(); i++) {
1848 if (!InstallExtension(extension->dependencies()[i])) return false;
1849 }
Steve Block44f0eee2011-05-26 01:26:41 +01001850 Isolate* isolate = Isolate::Current();
Steve Blocka7e24c12009-10-30 11:49:00 +00001851 Vector<const char> source = CStrVector(extension->source());
Steve Block44f0eee2011-05-26 01:26:41 +01001852 Handle<String> source_code = isolate->factory()->NewStringFromAscii(source);
Steve Blocka7e24c12009-10-30 11:49:00 +00001853 bool result = CompileScriptCached(CStrVector(extension->name()),
1854 source_code,
Steve Block44f0eee2011-05-26 01:26:41 +01001855 isolate->bootstrapper()->extensions_cache(),
Andrei Popescu31002712010-02-23 13:46:05 +00001856 extension,
Steve Block44f0eee2011-05-26 01:26:41 +01001857 Handle<Context>(isolate->context()),
Steve Blocka7e24c12009-10-30 11:49:00 +00001858 false);
Steve Block44f0eee2011-05-26 01:26:41 +01001859 ASSERT(isolate->has_pending_exception() != result);
Steve Blocka7e24c12009-10-30 11:49:00 +00001860 if (!result) {
Steve Block44f0eee2011-05-26 01:26:41 +01001861 isolate->clear_pending_exception();
Steve Blocka7e24c12009-10-30 11:49:00 +00001862 }
1863 current->set_state(v8::INSTALLED);
1864 return result;
1865}
1866
1867
Andrei Popescu402d9372010-02-26 13:31:12 +00001868bool Genesis::InstallJSBuiltins(Handle<JSBuiltinsObject> builtins) {
1869 HandleScope scope;
1870 for (int i = 0; i < Builtins::NumberOfJavaScriptBuiltins(); i++) {
1871 Builtins::JavaScript id = static_cast<Builtins::JavaScript>(i);
Steve Block053d10c2011-06-13 19:13:29 +01001872 Handle<String> name = FACTORY->LookupAsciiSymbol(Builtins::GetName(id));
John Reck59135872010-11-02 12:39:01 -07001873 Object* function_object = builtins->GetPropertyNoExceptionThrown(*name);
Andrei Popescu402d9372010-02-26 13:31:12 +00001874 Handle<JSFunction> function
John Reck59135872010-11-02 12:39:01 -07001875 = Handle<JSFunction>(JSFunction::cast(function_object));
Andrei Popescu402d9372010-02-26 13:31:12 +00001876 builtins->set_javascript_builtin(id, *function);
1877 Handle<SharedFunctionInfo> shared
1878 = Handle<SharedFunctionInfo>(function->shared());
1879 if (!EnsureCompiled(shared, CLEAR_EXCEPTION)) return false;
Iain Merrick75681382010-08-19 15:07:18 +01001880 // Set the code object on the function object.
Ben Murdochb0fe1622011-05-05 13:52:32 +01001881 function->ReplaceCode(function->shared()->code());
Steve Block6ded16b2010-05-10 14:33:55 +01001882 builtins->set_javascript_builtin_code(id, shared->code());
Andrei Popescu402d9372010-02-26 13:31:12 +00001883 }
1884 return true;
Andrei Popescu31002712010-02-23 13:46:05 +00001885}
1886
1887
Steve Blocka7e24c12009-10-30 11:49:00 +00001888bool Genesis::ConfigureGlobalObjects(
1889 v8::Handle<v8::ObjectTemplate> global_proxy_template) {
1890 Handle<JSObject> global_proxy(
1891 JSObject::cast(global_context()->global_proxy()));
Andrei Popescu402d9372010-02-26 13:31:12 +00001892 Handle<JSObject> inner_global(JSObject::cast(global_context()->global()));
Steve Blocka7e24c12009-10-30 11:49:00 +00001893
1894 if (!global_proxy_template.IsEmpty()) {
1895 // Configure the global proxy object.
1896 Handle<ObjectTemplateInfo> proxy_data =
1897 v8::Utils::OpenHandle(*global_proxy_template);
1898 if (!ConfigureApiObject(global_proxy, proxy_data)) return false;
1899
1900 // Configure the inner global object.
1901 Handle<FunctionTemplateInfo> proxy_constructor(
1902 FunctionTemplateInfo::cast(proxy_data->constructor()));
1903 if (!proxy_constructor->prototype_template()->IsUndefined()) {
1904 Handle<ObjectTemplateInfo> inner_data(
1905 ObjectTemplateInfo::cast(proxy_constructor->prototype_template()));
Andrei Popescu402d9372010-02-26 13:31:12 +00001906 if (!ConfigureApiObject(inner_global, inner_data)) return false;
Steve Blocka7e24c12009-10-30 11:49:00 +00001907 }
1908 }
1909
Andrei Popescu402d9372010-02-26 13:31:12 +00001910 SetObjectPrototype(global_proxy, inner_global);
Steve Blocka7e24c12009-10-30 11:49:00 +00001911 return true;
1912}
1913
1914
1915bool Genesis::ConfigureApiObject(Handle<JSObject> object,
1916 Handle<ObjectTemplateInfo> object_template) {
1917 ASSERT(!object_template.is_null());
1918 ASSERT(object->IsInstanceOf(
1919 FunctionTemplateInfo::cast(object_template->constructor())));
1920
Steve Block053d10c2011-06-13 19:13:29 +01001921 Isolate* isolate = Isolate::Current();
Steve Blocka7e24c12009-10-30 11:49:00 +00001922 bool pending_exception = false;
1923 Handle<JSObject> obj =
1924 Execution::InstantiateObject(object_template, &pending_exception);
1925 if (pending_exception) {
Steve Block053d10c2011-06-13 19:13:29 +01001926 ASSERT(isolate->has_pending_exception());
1927 isolate->clear_pending_exception();
Steve Blocka7e24c12009-10-30 11:49:00 +00001928 return false;
1929 }
1930 TransferObject(obj, object);
1931 return true;
1932}
1933
1934
1935void Genesis::TransferNamedProperties(Handle<JSObject> from,
1936 Handle<JSObject> to) {
1937 if (from->HasFastProperties()) {
1938 Handle<DescriptorArray> descs =
1939 Handle<DescriptorArray>(from->map()->instance_descriptors());
1940 for (int i = 0; i < descs->number_of_descriptors(); i++) {
1941 PropertyDetails details = PropertyDetails(descs->GetDetails(i));
1942 switch (details.type()) {
1943 case FIELD: {
1944 HandleScope inner;
1945 Handle<String> key = Handle<String>(descs->GetKey(i));
1946 int index = descs->GetFieldIndex(i);
1947 Handle<Object> value = Handle<Object>(from->FastPropertyAt(index));
Steve Block1e0659c2011-05-24 12:43:12 +01001948 SetLocalPropertyNoThrow(to, key, value, details.attributes());
Steve Blocka7e24c12009-10-30 11:49:00 +00001949 break;
1950 }
1951 case CONSTANT_FUNCTION: {
1952 HandleScope inner;
1953 Handle<String> key = Handle<String>(descs->GetKey(i));
1954 Handle<JSFunction> fun =
1955 Handle<JSFunction>(descs->GetConstantFunction(i));
Steve Block1e0659c2011-05-24 12:43:12 +01001956 SetLocalPropertyNoThrow(to, key, fun, details.attributes());
Steve Blocka7e24c12009-10-30 11:49:00 +00001957 break;
1958 }
1959 case CALLBACKS: {
1960 LookupResult result;
1961 to->LocalLookup(descs->GetKey(i), &result);
1962 // If the property is already there we skip it
Andrei Popescu402d9372010-02-26 13:31:12 +00001963 if (result.IsProperty()) continue;
Steve Blocka7e24c12009-10-30 11:49:00 +00001964 HandleScope inner;
Andrei Popescu31002712010-02-23 13:46:05 +00001965 ASSERT(!to->HasFastProperties());
1966 // Add to dictionary.
Steve Blocka7e24c12009-10-30 11:49:00 +00001967 Handle<String> key = Handle<String>(descs->GetKey(i));
Andrei Popescu31002712010-02-23 13:46:05 +00001968 Handle<Object> callbacks(descs->GetCallbacksObject(i));
1969 PropertyDetails d =
1970 PropertyDetails(details.attributes(), CALLBACKS, details.index());
1971 SetNormalizedProperty(to, key, callbacks, d);
Steve Blocka7e24c12009-10-30 11:49:00 +00001972 break;
1973 }
1974 case MAP_TRANSITION:
Steve Block44f0eee2011-05-26 01:26:41 +01001975 case EXTERNAL_ARRAY_TRANSITION:
Steve Blocka7e24c12009-10-30 11:49:00 +00001976 case CONSTANT_TRANSITION:
1977 case NULL_DESCRIPTOR:
1978 // Ignore non-properties.
1979 break;
1980 case NORMAL:
1981 // Do not occur since the from object has fast properties.
1982 case INTERCEPTOR:
1983 // No element in instance descriptors have interceptor type.
1984 UNREACHABLE();
1985 break;
1986 }
1987 }
1988 } else {
1989 Handle<StringDictionary> properties =
1990 Handle<StringDictionary>(from->property_dictionary());
1991 int capacity = properties->Capacity();
1992 for (int i = 0; i < capacity; i++) {
1993 Object* raw_key(properties->KeyAt(i));
1994 if (properties->IsKey(raw_key)) {
1995 ASSERT(raw_key->IsString());
1996 // If the property is already there we skip it.
1997 LookupResult result;
1998 to->LocalLookup(String::cast(raw_key), &result);
Andrei Popescu402d9372010-02-26 13:31:12 +00001999 if (result.IsProperty()) continue;
Steve Blocka7e24c12009-10-30 11:49:00 +00002000 // Set the property.
2001 Handle<String> key = Handle<String>(String::cast(raw_key));
2002 Handle<Object> value = Handle<Object>(properties->ValueAt(i));
2003 if (value->IsJSGlobalPropertyCell()) {
2004 value = Handle<Object>(JSGlobalPropertyCell::cast(*value)->value());
2005 }
2006 PropertyDetails details = properties->DetailsAt(i);
Steve Block1e0659c2011-05-24 12:43:12 +01002007 SetLocalPropertyNoThrow(to, key, value, details.attributes());
Steve Blocka7e24c12009-10-30 11:49:00 +00002008 }
2009 }
2010 }
2011}
2012
2013
2014void Genesis::TransferIndexedProperties(Handle<JSObject> from,
2015 Handle<JSObject> to) {
2016 // Cloning the elements array is sufficient.
2017 Handle<FixedArray> from_elements =
2018 Handle<FixedArray>(FixedArray::cast(from->elements()));
Steve Block44f0eee2011-05-26 01:26:41 +01002019 Handle<FixedArray> to_elements = FACTORY->CopyFixedArray(from_elements);
Steve Blocka7e24c12009-10-30 11:49:00 +00002020 to->set_elements(*to_elements);
2021}
2022
2023
2024void Genesis::TransferObject(Handle<JSObject> from, Handle<JSObject> to) {
2025 HandleScope outer;
2026
2027 ASSERT(!from->IsJSArray());
2028 ASSERT(!to->IsJSArray());
2029
2030 TransferNamedProperties(from, to);
2031 TransferIndexedProperties(from, to);
2032
2033 // Transfer the prototype (new map is needed).
2034 Handle<Map> old_to_map = Handle<Map>(to->map());
Steve Block053d10c2011-06-13 19:13:29 +01002035 Handle<Map> new_to_map = FACTORY->CopyMapDropTransitions(old_to_map);
Steve Blocka7e24c12009-10-30 11:49:00 +00002036 new_to_map->set_prototype(from->map()->prototype());
2037 to->set_map(*new_to_map);
2038}
2039
2040
2041void Genesis::MakeFunctionInstancePrototypeWritable() {
Steve Block44f0eee2011-05-26 01:26:41 +01002042 // The maps with writable prototype are created in CreateEmptyFunction
2043 // and CreateStrictModeFunctionMaps respectively. Initially the maps are
2044 // created with read-only prototype for JS builtins processing.
2045 ASSERT(!function_instance_map_writable_prototype_.is_null());
2046 ASSERT(!strict_mode_function_instance_map_writable_prototype_.is_null());
Steve Blocka7e24c12009-10-30 11:49:00 +00002047
Steve Block44f0eee2011-05-26 01:26:41 +01002048 // Replace function instance maps to make prototype writable.
2049 global_context()->set_function_map(
2050 *function_instance_map_writable_prototype_);
2051 global_context()->set_strict_mode_function_map(
2052 *strict_mode_function_instance_map_writable_prototype_);
Steve Blocka7e24c12009-10-30 11:49:00 +00002053}
2054
2055
Steve Block053d10c2011-06-13 19:13:29 +01002056Genesis::Genesis(Handle<Object> global_object,
Steve Blocka7e24c12009-10-30 11:49:00 +00002057 v8::Handle<v8::ObjectTemplate> global_template,
Steve Block053d10c2011-06-13 19:13:29 +01002058 v8::ExtensionConfiguration* extensions) {
2059 Isolate* isolate = Isolate::Current();
Steve Blocka7e24c12009-10-30 11:49:00 +00002060 result_ = Handle<Context>::null();
Steve Blocka7e24c12009-10-30 11:49:00 +00002061 // If V8 isn't running and cannot be initialized, just return.
2062 if (!V8::IsRunning() && !V8::Initialize(NULL)) return;
2063
2064 // Before creating the roots we must save the context and restore it
2065 // on all function exits.
2066 HandleScope scope;
Steve Block44f0eee2011-05-26 01:26:41 +01002067 SaveContext saved_context(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00002068
Andrei Popescu31002712010-02-23 13:46:05 +00002069 Handle<Context> new_context = Snapshot::NewContextFromSnapshot();
2070 if (!new_context.is_null()) {
2071 global_context_ =
Steve Block44f0eee2011-05-26 01:26:41 +01002072 Handle<Context>::cast(isolate->global_handles()->Create(*new_context));
Ben Murdochb0fe1622011-05-05 13:52:32 +01002073 AddToWeakGlobalContextList(*global_context_);
Steve Block44f0eee2011-05-26 01:26:41 +01002074 isolate->set_context(*global_context_);
2075 isolate->counters()->contexts_created_by_snapshot()->Increment();
Andrei Popescu402d9372010-02-26 13:31:12 +00002076 Handle<GlobalObject> inner_global;
Andrei Popescu31002712010-02-23 13:46:05 +00002077 Handle<JSGlobalProxy> global_proxy =
2078 CreateNewGlobals(global_template,
2079 global_object,
Andrei Popescu402d9372010-02-26 13:31:12 +00002080 &inner_global);
2081
Andrei Popescu31002712010-02-23 13:46:05 +00002082 HookUpGlobalProxy(inner_global, global_proxy);
Andrei Popescu402d9372010-02-26 13:31:12 +00002083 HookUpInnerGlobal(inner_global);
2084
Andrei Popescu31002712010-02-23 13:46:05 +00002085 if (!ConfigureGlobalObjects(global_template)) return;
2086 } else {
2087 // We get here if there was no context snapshot.
2088 CreateRoots();
Steve Block053d10c2011-06-13 19:13:29 +01002089 Handle<JSFunction> empty_function = CreateEmptyFunction();
Steve Block44f0eee2011-05-26 01:26:41 +01002090 CreateStrictModeFunctionMaps(empty_function);
Andrei Popescu31002712010-02-23 13:46:05 +00002091 Handle<GlobalObject> inner_global;
2092 Handle<JSGlobalProxy> global_proxy =
2093 CreateNewGlobals(global_template, global_object, &inner_global);
2094 HookUpGlobalProxy(inner_global, global_proxy);
2095 InitializeGlobal(inner_global, empty_function);
Steve Block6ded16b2010-05-10 14:33:55 +01002096 InstallJSFunctionResultCaches();
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002097 InitializeNormalizedMapCaches();
Kristian Monsen25f61362010-05-21 11:50:48 +01002098 if (!InstallNatives()) return;
Steve Blocka7e24c12009-10-30 11:49:00 +00002099
Andrei Popescu31002712010-02-23 13:46:05 +00002100 MakeFunctionInstancePrototypeWritable();
Steve Blocka7e24c12009-10-30 11:49:00 +00002101
Andrei Popescu31002712010-02-23 13:46:05 +00002102 if (!ConfigureGlobalObjects(global_template)) return;
Steve Block44f0eee2011-05-26 01:26:41 +01002103 isolate->counters()->contexts_created_from_scratch()->Increment();
Andrei Popescu31002712010-02-23 13:46:05 +00002104 }
Steve Blocka7e24c12009-10-30 11:49:00 +00002105
2106 result_ = global_context_;
2107}
2108
2109
2110// Support for thread preemption.
2111
2112// Reserve space for statics needing saving and restoring.
2113int Bootstrapper::ArchiveSpacePerThread() {
Steve Block44f0eee2011-05-26 01:26:41 +01002114 return sizeof(NestingCounterType);
Steve Blocka7e24c12009-10-30 11:49:00 +00002115}
2116
2117
2118// Archive statics that are thread local.
2119char* Bootstrapper::ArchiveState(char* to) {
Steve Block44f0eee2011-05-26 01:26:41 +01002120 *reinterpret_cast<NestingCounterType*>(to) = nesting_;
2121 nesting_ = 0;
2122 return to + sizeof(NestingCounterType);
Steve Blocka7e24c12009-10-30 11:49:00 +00002123}
2124
2125
2126// Restore statics that are thread local.
2127char* Bootstrapper::RestoreState(char* from) {
Steve Block44f0eee2011-05-26 01:26:41 +01002128 nesting_ = *reinterpret_cast<NestingCounterType*>(from);
2129 return from + sizeof(NestingCounterType);
Steve Blocka7e24c12009-10-30 11:49:00 +00002130}
2131
2132
2133// Called when the top-level V8 mutex is destroyed.
2134void Bootstrapper::FreeThreadResources() {
Steve Block44f0eee2011-05-26 01:26:41 +01002135 ASSERT(!IsActive());
Steve Blocka7e24c12009-10-30 11:49:00 +00002136}
2137
2138} } // namespace v8::internal