blob: d32ac805cd4d816d96a91bfa7ea57a62eb3a5c80 [file] [log] [blame]
Ben Murdoch257744e2011-11-30 15:57:28 +00001// Copyright 2011 the V8 project authors. All rights reserved.
Steve Blocka7e24c12009-10-30 11:49:00 +00002// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28#include "v8.h"
29
30#include "accessors.h"
31#include "api.h"
32#include "bootstrapper.h"
33#include "compiler.h"
34#include "debug.h"
35#include "execution.h"
36#include "global-handles.h"
37#include "macro-assembler.h"
38#include "natives.h"
Iain Merrick75681382010-08-19 15:07:18 +010039#include "objects-visiting.h"
Steve Blockd0582a62009-12-15 09:54:21 +000040#include "snapshot.h"
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -080041#include "extensions/externalize-string-extension.h"
42#include "extensions/gc-extension.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000043
44namespace v8 {
45namespace internal {
46
Steve Blocka7e24c12009-10-30 11:49:00 +000047
Steve Block44f0eee2011-05-26 01:26:41 +010048NativesExternalStringResource::NativesExternalStringResource(
49 Bootstrapper* bootstrapper,
50 const char* source)
Steve Blockd0582a62009-12-15 09:54:21 +000051 : data_(source), length_(StrLength(source)) {
Steve Block44f0eee2011-05-26 01:26:41 +010052 if (bootstrapper->delete_these_non_arrays_on_tear_down_ == NULL) {
53 bootstrapper->delete_these_non_arrays_on_tear_down_ = new List<char*>(2);
Steve Blockd0582a62009-12-15 09:54:21 +000054 }
55 // The resources are small objects and we only make a fixed number of
56 // them, but let's clean them up on exit for neatness.
Steve Block44f0eee2011-05-26 01:26:41 +010057 bootstrapper->delete_these_non_arrays_on_tear_down_->
Steve Blockd0582a62009-12-15 09:54:21 +000058 Add(reinterpret_cast<char*>(this));
59}
Steve Blocka7e24c12009-10-30 11:49:00 +000060
61
Steve Block44f0eee2011-05-26 01:26:41 +010062Bootstrapper::Bootstrapper()
63 : nesting_(0),
64 extensions_cache_(Script::TYPE_EXTENSION),
65 delete_these_non_arrays_on_tear_down_(NULL),
66 delete_these_arrays_on_tear_down_(NULL) {
67}
68
69
Steve Blocka7e24c12009-10-30 11:49:00 +000070Handle<String> Bootstrapper::NativesSourceLookup(int index) {
71 ASSERT(0 <= index && index < Natives::GetBuiltinsCount());
Steve Block44f0eee2011-05-26 01:26:41 +010072 Isolate* isolate = Isolate::Current();
73 Factory* factory = isolate->factory();
74 Heap* heap = isolate->heap();
75 if (heap->natives_source_cache()->get(index)->IsUndefined()) {
Steve Blockd0582a62009-12-15 09:54:21 +000076 if (!Snapshot::IsEnabled() || FLAG_new_snapshot) {
77 // We can use external strings for the natives.
78 NativesExternalStringResource* resource =
Steve Block44f0eee2011-05-26 01:26:41 +010079 new NativesExternalStringResource(this,
Steve Blockd0582a62009-12-15 09:54:21 +000080 Natives::GetScriptSource(index).start());
81 Handle<String> source_code =
Steve Block44f0eee2011-05-26 01:26:41 +010082 factory->NewExternalStringFromAscii(resource);
83 heap->natives_source_cache()->set(index, *source_code);
Steve Blockd0582a62009-12-15 09:54:21 +000084 } else {
85 // Old snapshot code can't cope with external strings at all.
86 Handle<String> source_code =
Steve Block44f0eee2011-05-26 01:26:41 +010087 factory->NewStringFromAscii(Natives::GetScriptSource(index));
88 heap->natives_source_cache()->set(index, *source_code);
Steve Blockd0582a62009-12-15 09:54:21 +000089 }
Steve Blocka7e24c12009-10-30 11:49:00 +000090 }
Steve Block44f0eee2011-05-26 01:26:41 +010091 Handle<Object> cached_source(heap->natives_source_cache()->get(index));
Steve Blocka7e24c12009-10-30 11:49:00 +000092 return Handle<String>::cast(cached_source);
93}
94
95
Steve Blocka7e24c12009-10-30 11:49:00 +000096void Bootstrapper::Initialize(bool create_heap_objects) {
Steve Block44f0eee2011-05-26 01:26:41 +010097 extensions_cache_.Initialize(create_heap_objects);
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -080098 GCExtension::Register();
99 ExternalizeStringExtension::Register();
Steve Blocka7e24c12009-10-30 11:49:00 +0000100}
101
102
Leon Clarkee46be812010-01-19 14:06:41 +0000103char* Bootstrapper::AllocateAutoDeletedArray(int bytes) {
104 char* memory = new char[bytes];
105 if (memory != NULL) {
Steve Block44f0eee2011-05-26 01:26:41 +0100106 if (delete_these_arrays_on_tear_down_ == NULL) {
107 delete_these_arrays_on_tear_down_ = new List<char*>(2);
Leon Clarkee46be812010-01-19 14:06:41 +0000108 }
Steve Block44f0eee2011-05-26 01:26:41 +0100109 delete_these_arrays_on_tear_down_->Add(memory);
Leon Clarkee46be812010-01-19 14:06:41 +0000110 }
111 return memory;
112}
113
114
Steve Blocka7e24c12009-10-30 11:49:00 +0000115void Bootstrapper::TearDown() {
Steve Block44f0eee2011-05-26 01:26:41 +0100116 if (delete_these_non_arrays_on_tear_down_ != NULL) {
117 int len = delete_these_non_arrays_on_tear_down_->length();
Steve Blockd0582a62009-12-15 09:54:21 +0000118 ASSERT(len < 20); // Don't use this mechanism for unbounded allocations.
119 for (int i = 0; i < len; i++) {
Steve Block44f0eee2011-05-26 01:26:41 +0100120 delete delete_these_non_arrays_on_tear_down_->at(i);
121 delete_these_non_arrays_on_tear_down_->at(i) = NULL;
Steve Blockd0582a62009-12-15 09:54:21 +0000122 }
Steve Block44f0eee2011-05-26 01:26:41 +0100123 delete delete_these_non_arrays_on_tear_down_;
124 delete_these_non_arrays_on_tear_down_ = NULL;
Steve Blockd0582a62009-12-15 09:54:21 +0000125 }
126
Steve Block44f0eee2011-05-26 01:26:41 +0100127 if (delete_these_arrays_on_tear_down_ != NULL) {
128 int len = delete_these_arrays_on_tear_down_->length();
Leon Clarkee46be812010-01-19 14:06:41 +0000129 ASSERT(len < 1000); // Don't use this mechanism for unbounded allocations.
130 for (int i = 0; i < len; i++) {
Steve Block44f0eee2011-05-26 01:26:41 +0100131 delete[] delete_these_arrays_on_tear_down_->at(i);
132 delete_these_arrays_on_tear_down_->at(i) = NULL;
Leon Clarkee46be812010-01-19 14:06:41 +0000133 }
Steve Block44f0eee2011-05-26 01:26:41 +0100134 delete delete_these_arrays_on_tear_down_;
135 delete_these_arrays_on_tear_down_ = NULL;
Leon Clarkee46be812010-01-19 14:06:41 +0000136 }
137
Steve Block44f0eee2011-05-26 01:26:41 +0100138 extensions_cache_.Initialize(false); // Yes, symmetrical
Steve Blocka7e24c12009-10-30 11:49:00 +0000139}
140
141
Steve Blocka7e24c12009-10-30 11:49:00 +0000142class Genesis BASE_EMBEDDED {
143 public:
Ben Murdoch257744e2011-11-30 15:57:28 +0000144 Genesis(Isolate* isolate,
145 Handle<Object> global_object,
Steve Blocka7e24c12009-10-30 11:49:00 +0000146 v8::Handle<v8::ObjectTemplate> global_template,
147 v8::ExtensionConfiguration* extensions);
Andrei Popescu31002712010-02-23 13:46:05 +0000148 ~Genesis() { }
Steve Blocka7e24c12009-10-30 11:49:00 +0000149
150 Handle<Context> result() { return result_; }
151
152 Genesis* previous() { return previous_; }
Steve Blocka7e24c12009-10-30 11:49:00 +0000153
Ben Murdoch257744e2011-11-30 15:57:28 +0000154 Isolate* isolate() const { return isolate_; }
155 Factory* factory() const { return isolate_->factory(); }
156 Heap* heap() const { return isolate_->heap(); }
157
Steve Blocka7e24c12009-10-30 11:49:00 +0000158 private:
159 Handle<Context> global_context_;
Ben Murdoch257744e2011-11-30 15:57:28 +0000160 Isolate* isolate_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000161
162 // There may be more than one active genesis object: When GC is
163 // triggered during environment creation there may be weak handle
164 // processing callbacks which may create new environments.
165 Genesis* previous_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000166
167 Handle<Context> global_context() { return global_context_; }
168
Andrei Popescu31002712010-02-23 13:46:05 +0000169 // Creates some basic objects. Used for creating a context from scratch.
170 void CreateRoots();
171 // Creates the empty function. Used for creating a context from scratch.
Ben Murdoch257744e2011-11-30 15:57:28 +0000172 Handle<JSFunction> CreateEmptyFunction(Isolate* isolate);
Steve Block44f0eee2011-05-26 01:26:41 +0100173 // Creates the ThrowTypeError function. ECMA 5th Ed. 13.2.3
Ben Murdoch257744e2011-11-30 15:57:28 +0000174 Handle<JSFunction> GetThrowTypeErrorFunction();
Steve Block44f0eee2011-05-26 01:26:41 +0100175
176 void CreateStrictModeFunctionMaps(Handle<JSFunction> empty);
Andrei Popescu31002712010-02-23 13:46:05 +0000177 // Creates the global objects using the global and the template passed in
178 // through the API. We call this regardless of whether we are building a
179 // context from scratch or using a deserialized one from the partial snapshot
180 // but in the latter case we don't use the objects it produces directly, as
181 // we have to used the deserialized ones that are linked together with the
182 // rest of the context snapshot.
183 Handle<JSGlobalProxy> CreateNewGlobals(
184 v8::Handle<v8::ObjectTemplate> global_template,
185 Handle<Object> global_object,
186 Handle<GlobalObject>* global_proxy_out);
187 // Hooks the given global proxy into the context. If the context was created
188 // by deserialization then this will unhook the global proxy that was
189 // deserialized, leaving the GC to pick it up.
190 void HookUpGlobalProxy(Handle<GlobalObject> inner_global,
191 Handle<JSGlobalProxy> global_proxy);
Andrei Popescu402d9372010-02-26 13:31:12 +0000192 // Similarly, we want to use the inner global that has been created by the
193 // templates passed through the API. The inner global from the snapshot is
194 // detached from the other objects in the snapshot.
195 void HookUpInnerGlobal(Handle<GlobalObject> inner_global);
Andrei Popescu31002712010-02-23 13:46:05 +0000196 // New context initialization. Used for creating a context from scratch.
197 void InitializeGlobal(Handle<GlobalObject> inner_global,
198 Handle<JSFunction> empty_function);
199 // Installs the contents of the native .js files on the global objects.
200 // Used for creating a context from scratch.
Steve Blocka7e24c12009-10-30 11:49:00 +0000201 void InstallNativeFunctions();
Ben Murdoch257744e2011-11-30 15:57:28 +0000202 void InstallExperimentalNativeFunctions();
Steve Blocka7e24c12009-10-30 11:49:00 +0000203 bool InstallNatives();
Ben Murdoch257744e2011-11-30 15:57:28 +0000204 bool InstallExperimentalNatives();
Ben Murdochb0fe1622011-05-05 13:52:32 +0100205 void InstallBuiltinFunctionIds();
Steve Block6ded16b2010-05-10 14:33:55 +0100206 void InstallJSFunctionResultCaches();
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100207 void InitializeNormalizedMapCaches();
Andrei Popescu31002712010-02-23 13:46:05 +0000208 // Used both for deserialized and from-scratch contexts to add the extensions
209 // provided.
210 static bool InstallExtensions(Handle<Context> global_context,
211 v8::ExtensionConfiguration* extensions);
212 static bool InstallExtension(const char* name);
213 static bool InstallExtension(v8::RegisteredExtension* current);
214 static void InstallSpecialObjects(Handle<Context> global_context);
Andrei Popescu402d9372010-02-26 13:31:12 +0000215 bool InstallJSBuiltins(Handle<JSBuiltinsObject> builtins);
Steve Blocka7e24c12009-10-30 11:49:00 +0000216 bool ConfigureApiObject(Handle<JSObject> object,
217 Handle<ObjectTemplateInfo> object_template);
218 bool ConfigureGlobalObjects(v8::Handle<v8::ObjectTemplate> global_template);
219
220 // Migrates all properties from the 'from' object to the 'to'
221 // object and overrides the prototype in 'to' with the one from
222 // 'from'.
223 void TransferObject(Handle<JSObject> from, Handle<JSObject> to);
224 void TransferNamedProperties(Handle<JSObject> from, Handle<JSObject> to);
225 void TransferIndexedProperties(Handle<JSObject> from, Handle<JSObject> to);
226
Steve Block6ded16b2010-05-10 14:33:55 +0100227 enum PrototypePropertyMode {
228 DONT_ADD_PROTOTYPE,
229 ADD_READONLY_PROTOTYPE,
230 ADD_WRITEABLE_PROTOTYPE
231 };
Steve Block44f0eee2011-05-26 01:26:41 +0100232
233 Handle<Map> CreateFunctionMap(PrototypePropertyMode prototype_mode);
234
Steve Blocka7e24c12009-10-30 11:49:00 +0000235 Handle<DescriptorArray> ComputeFunctionInstanceDescriptor(
Steve Block6ded16b2010-05-10 14:33:55 +0100236 PrototypePropertyMode prototypeMode);
Steve Blocka7e24c12009-10-30 11:49:00 +0000237 void MakeFunctionInstancePrototypeWritable();
238
Steve Block44f0eee2011-05-26 01:26:41 +0100239 Handle<Map> CreateStrictModeFunctionMap(
240 PrototypePropertyMode prototype_mode,
241 Handle<JSFunction> empty_function,
242 Handle<FixedArray> arguments_callbacks,
243 Handle<FixedArray> caller_callbacks);
244
245 Handle<DescriptorArray> ComputeStrictFunctionInstanceDescriptor(
246 PrototypePropertyMode propertyMode,
247 Handle<FixedArray> arguments,
248 Handle<FixedArray> caller);
249
Ben Murdoch257744e2011-11-30 15:57:28 +0000250 static bool CompileBuiltin(Isolate* isolate, int index);
251 static bool CompileExperimentalBuiltin(Isolate* isolate, int index);
Steve Blocka7e24c12009-10-30 11:49:00 +0000252 static bool CompileNative(Vector<const char> name, Handle<String> source);
253 static bool CompileScriptCached(Vector<const char> name,
254 Handle<String> source,
255 SourceCodeCache* cache,
256 v8::Extension* extension,
Andrei Popescu31002712010-02-23 13:46:05 +0000257 Handle<Context> top_context,
Steve Blocka7e24c12009-10-30 11:49:00 +0000258 bool use_runtime_context);
259
260 Handle<Context> result_;
Steve Block44f0eee2011-05-26 01:26:41 +0100261
262 // Function instance maps. Function literal maps are created initially with
263 // a read only prototype for the processing of JS builtins. Later the function
264 // instance maps are replaced in order to make prototype writable.
265 // These are the final, writable prototype, maps.
266 Handle<Map> function_instance_map_writable_prototype_;
267 Handle<Map> strict_mode_function_instance_map_writable_prototype_;
Ben Murdoch257744e2011-11-30 15:57:28 +0000268 Handle<JSFunction> throw_type_error_function;
Steve Block44f0eee2011-05-26 01:26:41 +0100269
Andrei Popescu31002712010-02-23 13:46:05 +0000270 BootstrapperActive active_;
271 friend class Bootstrapper;
Steve Blocka7e24c12009-10-30 11:49:00 +0000272};
273
Steve Blocka7e24c12009-10-30 11:49:00 +0000274
275void Bootstrapper::Iterate(ObjectVisitor* v) {
Steve Block44f0eee2011-05-26 01:26:41 +0100276 extensions_cache_.Iterate(v);
Steve Blockd0582a62009-12-15 09:54:21 +0000277 v->Synchronize("Extensions");
Steve Blocka7e24c12009-10-30 11:49:00 +0000278}
279
280
Steve Blocka7e24c12009-10-30 11:49:00 +0000281Handle<Context> Bootstrapper::CreateEnvironment(
Ben Murdoch257744e2011-11-30 15:57:28 +0000282 Isolate* isolate,
Steve Blocka7e24c12009-10-30 11:49:00 +0000283 Handle<Object> global_object,
284 v8::Handle<v8::ObjectTemplate> global_template,
285 v8::ExtensionConfiguration* extensions) {
Andrei Popescu31002712010-02-23 13:46:05 +0000286 HandleScope scope;
287 Handle<Context> env;
Ben Murdoch257744e2011-11-30 15:57:28 +0000288 Genesis genesis(isolate, global_object, global_template, extensions);
Andrei Popescu31002712010-02-23 13:46:05 +0000289 env = genesis.result();
290 if (!env.is_null()) {
291 if (InstallExtensions(env, extensions)) {
292 return env;
293 }
294 }
295 return Handle<Context>();
Steve Blocka7e24c12009-10-30 11:49:00 +0000296}
297
298
299static void SetObjectPrototype(Handle<JSObject> object, Handle<Object> proto) {
300 // object.__proto__ = proto;
Ben Murdoch257744e2011-11-30 15:57:28 +0000301 Factory* factory = object->GetIsolate()->factory();
Steve Blocka7e24c12009-10-30 11:49:00 +0000302 Handle<Map> old_to_map = Handle<Map>(object->map());
Ben Murdoch257744e2011-11-30 15:57:28 +0000303 Handle<Map> new_to_map = factory->CopyMapDropTransitions(old_to_map);
Steve Blocka7e24c12009-10-30 11:49:00 +0000304 new_to_map->set_prototype(*proto);
305 object->set_map(*new_to_map);
306}
307
308
309void Bootstrapper::DetachGlobal(Handle<Context> env) {
Ben Murdoch257744e2011-11-30 15:57:28 +0000310 Factory* factory = env->GetIsolate()->factory();
Steve Block44f0eee2011-05-26 01:26:41 +0100311 JSGlobalProxy::cast(env->global_proxy())->set_context(*factory->null_value());
Steve Blocka7e24c12009-10-30 11:49:00 +0000312 SetObjectPrototype(Handle<JSObject>(env->global_proxy()),
Steve Block44f0eee2011-05-26 01:26:41 +0100313 factory->null_value());
Steve Blocka7e24c12009-10-30 11:49:00 +0000314 env->set_global_proxy(env->global());
315 env->global()->set_global_receiver(env->global());
316}
317
318
Andrei Popescu74b3c142010-03-29 12:03:09 +0100319void Bootstrapper::ReattachGlobal(Handle<Context> env,
320 Handle<Object> global_object) {
321 ASSERT(global_object->IsJSGlobalProxy());
322 Handle<JSGlobalProxy> global = Handle<JSGlobalProxy>::cast(global_object);
323 env->global()->set_global_receiver(*global);
324 env->set_global_proxy(*global);
325 SetObjectPrototype(global, Handle<JSObject>(env->global()));
326 global->set_context(*env);
327}
328
329
Steve Blocka7e24c12009-10-30 11:49:00 +0000330static Handle<JSFunction> InstallFunction(Handle<JSObject> target,
331 const char* name,
332 InstanceType type,
333 int instance_size,
334 Handle<JSObject> prototype,
335 Builtins::Name call,
336 bool is_ecma_native) {
Ben Murdoch257744e2011-11-30 15:57:28 +0000337 Isolate* isolate = target->GetIsolate();
Steve Block44f0eee2011-05-26 01:26:41 +0100338 Factory* factory = isolate->factory();
339 Handle<String> symbol = factory->LookupAsciiSymbol(name);
340 Handle<Code> call_code = Handle<Code>(isolate->builtins()->builtin(call));
Steve Block6ded16b2010-05-10 14:33:55 +0100341 Handle<JSFunction> function = prototype.is_null() ?
Steve Block44f0eee2011-05-26 01:26:41 +0100342 factory->NewFunctionWithoutPrototype(symbol, call_code) :
343 factory->NewFunctionWithPrototype(symbol,
Steve Blocka7e24c12009-10-30 11:49:00 +0000344 type,
345 instance_size,
346 prototype,
347 call_code,
348 is_ecma_native);
Steve Block1e0659c2011-05-24 12:43:12 +0100349 SetLocalPropertyNoThrow(target, symbol, function, DONT_ENUM);
Steve Blocka7e24c12009-10-30 11:49:00 +0000350 if (is_ecma_native) {
351 function->shared()->set_instance_class_name(*symbol);
352 }
353 return function;
354}
355
356
357Handle<DescriptorArray> Genesis::ComputeFunctionInstanceDescriptor(
Steve Block6ded16b2010-05-10 14:33:55 +0100358 PrototypePropertyMode prototypeMode) {
Steve Block44f0eee2011-05-26 01:26:41 +0100359 Handle<DescriptorArray> descriptors =
Ben Murdoch257744e2011-11-30 15:57:28 +0000360 factory()->NewDescriptorArray(prototypeMode == DONT_ADD_PROTOTYPE
361 ? 4
362 : 5);
Steve Block6ded16b2010-05-10 14:33:55 +0100363 PropertyAttributes attributes =
Steve Blocka7e24c12009-10-30 11:49:00 +0000364 static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE | READ_ONLY);
Steve Blocka7e24c12009-10-30 11:49:00 +0000365
Steve Block44f0eee2011-05-26 01:26:41 +0100366 { // Add length.
Ben Murdoch257744e2011-11-30 15:57:28 +0000367 Handle<Foreign> foreign = factory()->NewForeign(&Accessors::FunctionLength);
368 CallbacksDescriptor d(*factory()->length_symbol(), *foreign, attributes);
Steve Block44f0eee2011-05-26 01:26:41 +0100369 descriptors->Set(0, &d);
370 }
371 { // Add name.
Ben Murdoch257744e2011-11-30 15:57:28 +0000372 Handle<Foreign> foreign = factory()->NewForeign(&Accessors::FunctionName);
373 CallbacksDescriptor d(*factory()->name_symbol(), *foreign, attributes);
Steve Block44f0eee2011-05-26 01:26:41 +0100374 descriptors->Set(1, &d);
375 }
376 { // Add arguments.
Ben Murdoch257744e2011-11-30 15:57:28 +0000377 Handle<Foreign> foreign =
378 factory()->NewForeign(&Accessors::FunctionArguments);
379 CallbacksDescriptor d(*factory()->arguments_symbol(), *foreign, attributes);
Steve Block44f0eee2011-05-26 01:26:41 +0100380 descriptors->Set(2, &d);
381 }
382 { // Add caller.
Ben Murdoch257744e2011-11-30 15:57:28 +0000383 Handle<Foreign> foreign = factory()->NewForeign(&Accessors::FunctionCaller);
384 CallbacksDescriptor d(*factory()->caller_symbol(), *foreign, attributes);
Steve Block44f0eee2011-05-26 01:26:41 +0100385 descriptors->Set(3, &d);
386 }
387 if (prototypeMode != DONT_ADD_PROTOTYPE) {
388 // Add prototype.
389 if (prototypeMode == ADD_WRITEABLE_PROTOTYPE) {
390 attributes = static_cast<PropertyAttributes>(attributes & ~READ_ONLY);
391 }
Ben Murdoch257744e2011-11-30 15:57:28 +0000392 Handle<Foreign> foreign =
393 factory()->NewForeign(&Accessors::FunctionPrototype);
394 CallbacksDescriptor d(*factory()->prototype_symbol(), *foreign, attributes);
Steve Block44f0eee2011-05-26 01:26:41 +0100395 descriptors->Set(4, &d);
396 }
397 descriptors->Sort();
398 return descriptors;
399}
Steve Blocka7e24c12009-10-30 11:49:00 +0000400
Steve Blocka7e24c12009-10-30 11:49:00 +0000401
Steve Block44f0eee2011-05-26 01:26:41 +0100402Handle<Map> Genesis::CreateFunctionMap(PrototypePropertyMode prototype_mode) {
Ben Murdoch257744e2011-11-30 15:57:28 +0000403 Handle<Map> map = factory()->NewMap(JS_FUNCTION_TYPE, JSFunction::kSize);
Steve Block44f0eee2011-05-26 01:26:41 +0100404 Handle<DescriptorArray> descriptors =
405 ComputeFunctionInstanceDescriptor(prototype_mode);
406 map->set_instance_descriptors(*descriptors);
407 map->set_function_with_prototype(prototype_mode != DONT_ADD_PROTOTYPE);
408 return map;
Steve Blocka7e24c12009-10-30 11:49:00 +0000409}
410
411
Ben Murdoch257744e2011-11-30 15:57:28 +0000412Handle<JSFunction> Genesis::CreateEmptyFunction(Isolate* isolate) {
Steve Block44f0eee2011-05-26 01:26:41 +0100413 // Allocate the map for function instances. Maps are allocated first and their
414 // prototypes patched later, once empty function is created.
415
Steve Blocka7e24c12009-10-30 11:49:00 +0000416 // Please note that the prototype property for function instances must be
417 // writable.
Ben Murdoch8b112d22011-06-08 16:22:53 +0100418 Handle<Map> function_instance_map =
419 CreateFunctionMap(ADD_WRITEABLE_PROTOTYPE);
420 global_context()->set_function_instance_map(*function_instance_map);
Steve Block6ded16b2010-05-10 14:33:55 +0100421
422 // Functions with this map will not have a 'prototype' property, and
423 // can not be used as constructors.
Ben Murdoch8b112d22011-06-08 16:22:53 +0100424 Handle<Map> function_without_prototype_map =
425 CreateFunctionMap(DONT_ADD_PROTOTYPE);
Steve Block6ded16b2010-05-10 14:33:55 +0100426 global_context()->set_function_without_prototype_map(
Ben Murdoch8b112d22011-06-08 16:22:53 +0100427 *function_without_prototype_map);
Steve Blocka7e24c12009-10-30 11:49:00 +0000428
Steve Block44f0eee2011-05-26 01:26:41 +0100429 // Allocate the function map. This map is temporary, used only for processing
430 // of builtins.
431 // Later the map is replaced with writable prototype map, allocated below.
Ben Murdoch8b112d22011-06-08 16:22:53 +0100432 Handle<Map> function_map = CreateFunctionMap(ADD_READONLY_PROTOTYPE);
433 global_context()->set_function_map(*function_map);
Steve Blocka7e24c12009-10-30 11:49:00 +0000434
Steve Block44f0eee2011-05-26 01:26:41 +0100435 // The final map for functions. Writeable prototype.
436 // This map is installed in MakeFunctionInstancePrototypeWritable.
437 function_instance_map_writable_prototype_ =
438 CreateFunctionMap(ADD_WRITEABLE_PROTOTYPE);
439
Steve Block44f0eee2011-05-26 01:26:41 +0100440 Factory* factory = isolate->factory();
441 Heap* heap = isolate->heap();
442
443 Handle<String> object_name = Handle<String>(heap->Object_symbol());
Steve Blocka7e24c12009-10-30 11:49:00 +0000444
445 { // --- O b j e c t ---
446 Handle<JSFunction> object_fun =
Steve Block44f0eee2011-05-26 01:26:41 +0100447 factory->NewFunction(object_name, factory->null_value());
Steve Blocka7e24c12009-10-30 11:49:00 +0000448 Handle<Map> object_function_map =
Steve Block44f0eee2011-05-26 01:26:41 +0100449 factory->NewMap(JS_OBJECT_TYPE, JSObject::kHeaderSize);
Steve Blocka7e24c12009-10-30 11:49:00 +0000450 object_fun->set_initial_map(*object_function_map);
451 object_function_map->set_constructor(*object_fun);
452
453 global_context()->set_object_function(*object_fun);
454
455 // Allocate a new prototype for the object function.
Steve Block44f0eee2011-05-26 01:26:41 +0100456 Handle<JSObject> prototype = factory->NewJSObject(
457 isolate->object_function(),
458 TENURED);
Steve Blocka7e24c12009-10-30 11:49:00 +0000459
460 global_context()->set_initial_object_prototype(*prototype);
461 SetPrototype(object_fun, prototype);
462 object_function_map->
Steve Block44f0eee2011-05-26 01:26:41 +0100463 set_instance_descriptors(heap->empty_descriptor_array());
Steve Blocka7e24c12009-10-30 11:49:00 +0000464 }
465
466 // Allocate the empty function as the prototype for function ECMAScript
467 // 262 15.3.4.
Steve Block44f0eee2011-05-26 01:26:41 +0100468 Handle<String> symbol = factory->LookupAsciiSymbol("Empty");
Steve Blocka7e24c12009-10-30 11:49:00 +0000469 Handle<JSFunction> empty_function =
Steve Block44f0eee2011-05-26 01:26:41 +0100470 factory->NewFunctionWithoutPrototype(symbol, kNonStrictMode);
Steve Blocka7e24c12009-10-30 11:49:00 +0000471
Andrei Popescu31002712010-02-23 13:46:05 +0000472 // --- E m p t y ---
473 Handle<Code> code =
Steve Block44f0eee2011-05-26 01:26:41 +0100474 Handle<Code>(isolate->builtins()->builtin(
475 Builtins::kEmptyFunction));
Andrei Popescu31002712010-02-23 13:46:05 +0000476 empty_function->set_code(*code);
Iain Merrick75681382010-08-19 15:07:18 +0100477 empty_function->shared()->set_code(*code);
Steve Block44f0eee2011-05-26 01:26:41 +0100478 Handle<String> source = factory->NewStringFromAscii(CStrVector("() {}"));
479 Handle<Script> script = factory->NewScript(source);
Andrei Popescu31002712010-02-23 13:46:05 +0000480 script->set_type(Smi::FromInt(Script::TYPE_NATIVE));
481 empty_function->shared()->set_script(*script);
482 empty_function->shared()->set_start_position(0);
483 empty_function->shared()->set_end_position(source->length());
484 empty_function->shared()->DontAdaptArguments();
Steve Block44f0eee2011-05-26 01:26:41 +0100485
486 // Set prototypes for the function maps.
Andrei Popescu31002712010-02-23 13:46:05 +0000487 global_context()->function_map()->set_prototype(*empty_function);
488 global_context()->function_instance_map()->set_prototype(*empty_function);
Steve Block6ded16b2010-05-10 14:33:55 +0100489 global_context()->function_without_prototype_map()->
490 set_prototype(*empty_function);
Steve Block44f0eee2011-05-26 01:26:41 +0100491 function_instance_map_writable_prototype_->set_prototype(*empty_function);
Steve Blocka7e24c12009-10-30 11:49:00 +0000492
Andrei Popescu31002712010-02-23 13:46:05 +0000493 // Allocate the function map first and then patch the prototype later
Steve Block44f0eee2011-05-26 01:26:41 +0100494 Handle<Map> empty_fm = factory->CopyMapDropDescriptors(
Steve Block6ded16b2010-05-10 14:33:55 +0100495 function_without_prototype_map);
496 empty_fm->set_instance_descriptors(
Steve Block44f0eee2011-05-26 01:26:41 +0100497 function_without_prototype_map->instance_descriptors());
Andrei Popescu31002712010-02-23 13:46:05 +0000498 empty_fm->set_prototype(global_context()->object_function()->prototype());
499 empty_function->set_map(*empty_fm);
500 return empty_function;
501}
502
503
Steve Block44f0eee2011-05-26 01:26:41 +0100504Handle<DescriptorArray> Genesis::ComputeStrictFunctionInstanceDescriptor(
505 PrototypePropertyMode prototypeMode,
506 Handle<FixedArray> arguments,
507 Handle<FixedArray> caller) {
Steve Block44f0eee2011-05-26 01:26:41 +0100508 Handle<DescriptorArray> descriptors =
Ben Murdoch257744e2011-11-30 15:57:28 +0000509 factory()->NewDescriptorArray(prototypeMode == DONT_ADD_PROTOTYPE
510 ? 4
511 : 5);
Steve Block44f0eee2011-05-26 01:26:41 +0100512 PropertyAttributes attributes = static_cast<PropertyAttributes>(
513 DONT_ENUM | DONT_DELETE | READ_ONLY);
514
515 { // length
Ben Murdoch257744e2011-11-30 15:57:28 +0000516 Handle<Foreign> foreign = factory()->NewForeign(&Accessors::FunctionLength);
517 CallbacksDescriptor d(*factory()->length_symbol(), *foreign, attributes);
Steve Block44f0eee2011-05-26 01:26:41 +0100518 descriptors->Set(0, &d);
519 }
520 { // name
Ben Murdoch257744e2011-11-30 15:57:28 +0000521 Handle<Foreign> foreign = factory()->NewForeign(&Accessors::FunctionName);
522 CallbacksDescriptor d(*factory()->name_symbol(), *foreign, attributes);
Steve Block44f0eee2011-05-26 01:26:41 +0100523 descriptors->Set(1, &d);
524 }
525 { // arguments
Ben Murdoch257744e2011-11-30 15:57:28 +0000526 CallbacksDescriptor d(*factory()->arguments_symbol(),
527 *arguments,
528 attributes);
Steve Block44f0eee2011-05-26 01:26:41 +0100529 descriptors->Set(2, &d);
530 }
531 { // caller
Ben Murdoch257744e2011-11-30 15:57:28 +0000532 CallbacksDescriptor d(*factory()->caller_symbol(), *caller, attributes);
Steve Block44f0eee2011-05-26 01:26:41 +0100533 descriptors->Set(3, &d);
534 }
535
536 // prototype
537 if (prototypeMode != DONT_ADD_PROTOTYPE) {
538 if (prototypeMode == ADD_WRITEABLE_PROTOTYPE) {
539 attributes = static_cast<PropertyAttributes>(attributes & ~READ_ONLY);
540 }
Ben Murdoch257744e2011-11-30 15:57:28 +0000541 Handle<Foreign> foreign =
542 factory()->NewForeign(&Accessors::FunctionPrototype);
543 CallbacksDescriptor d(*factory()->prototype_symbol(), *foreign, attributes);
Steve Block44f0eee2011-05-26 01:26:41 +0100544 descriptors->Set(4, &d);
545 }
546
547 descriptors->Sort();
548 return descriptors;
549}
550
551
552// ECMAScript 5th Edition, 13.2.3
Ben Murdoch257744e2011-11-30 15:57:28 +0000553Handle<JSFunction> Genesis::GetThrowTypeErrorFunction() {
554 if (throw_type_error_function.is_null()) {
555 Handle<String> name = factory()->LookupAsciiSymbol("ThrowTypeError");
556 throw_type_error_function =
557 factory()->NewFunctionWithoutPrototype(name, kNonStrictMode);
558 Handle<Code> code(isolate()->builtins()->builtin(
559 Builtins::kStrictModePoisonPill));
560 throw_type_error_function->set_map(
561 global_context()->function_map());
562 throw_type_error_function->set_code(*code);
563 throw_type_error_function->shared()->set_code(*code);
564 throw_type_error_function->shared()->DontAdaptArguments();
Steve Block053d10c2011-06-13 19:13:29 +0100565
Ben Murdoch257744e2011-11-30 15:57:28 +0000566 PreventExtensions(throw_type_error_function);
567 }
568 return throw_type_error_function;
Steve Block44f0eee2011-05-26 01:26:41 +0100569}
570
571
572Handle<Map> Genesis::CreateStrictModeFunctionMap(
573 PrototypePropertyMode prototype_mode,
574 Handle<JSFunction> empty_function,
575 Handle<FixedArray> arguments_callbacks,
576 Handle<FixedArray> caller_callbacks) {
Ben Murdoch257744e2011-11-30 15:57:28 +0000577 Handle<Map> map = factory()->NewMap(JS_FUNCTION_TYPE, JSFunction::kSize);
Steve Block44f0eee2011-05-26 01:26:41 +0100578 Handle<DescriptorArray> descriptors =
579 ComputeStrictFunctionInstanceDescriptor(prototype_mode,
580 arguments_callbacks,
581 caller_callbacks);
582 map->set_instance_descriptors(*descriptors);
583 map->set_function_with_prototype(prototype_mode != DONT_ADD_PROTOTYPE);
584 map->set_prototype(*empty_function);
585 return map;
586}
587
588
589void Genesis::CreateStrictModeFunctionMaps(Handle<JSFunction> empty) {
590 // Create the callbacks arrays for ThrowTypeError functions.
591 // The get/set callacks are filled in after the maps are created below.
Ben Murdoch257744e2011-11-30 15:57:28 +0000592 Factory* factory = empty->GetIsolate()->factory();
Steve Block44f0eee2011-05-26 01:26:41 +0100593 Handle<FixedArray> arguments = factory->NewFixedArray(2, TENURED);
594 Handle<FixedArray> caller = factory->NewFixedArray(2, TENURED);
595
596 // Allocate map for the strict mode function instances.
Ben Murdoch8b112d22011-06-08 16:22:53 +0100597 Handle<Map> strict_mode_function_instance_map =
598 CreateStrictModeFunctionMap(
599 ADD_WRITEABLE_PROTOTYPE, empty, arguments, caller);
Steve Block44f0eee2011-05-26 01:26:41 +0100600 global_context()->set_strict_mode_function_instance_map(
Ben Murdoch8b112d22011-06-08 16:22:53 +0100601 *strict_mode_function_instance_map);
Steve Block44f0eee2011-05-26 01:26:41 +0100602
603 // Allocate map for the prototype-less strict mode instances.
Ben Murdoch8b112d22011-06-08 16:22:53 +0100604 Handle<Map> strict_mode_function_without_prototype_map =
605 CreateStrictModeFunctionMap(
606 DONT_ADD_PROTOTYPE, empty, arguments, caller);
Steve Block44f0eee2011-05-26 01:26:41 +0100607 global_context()->set_strict_mode_function_without_prototype_map(
Ben Murdoch8b112d22011-06-08 16:22:53 +0100608 *strict_mode_function_without_prototype_map);
Steve Block44f0eee2011-05-26 01:26:41 +0100609
610 // Allocate map for the strict mode functions. This map is temporary, used
611 // only for processing of builtins.
612 // Later the map is replaced with writable prototype map, allocated below.
Ben Murdoch8b112d22011-06-08 16:22:53 +0100613 Handle<Map> strict_mode_function_map =
614 CreateStrictModeFunctionMap(
615 ADD_READONLY_PROTOTYPE, empty, arguments, caller);
Steve Block44f0eee2011-05-26 01:26:41 +0100616 global_context()->set_strict_mode_function_map(
Ben Murdoch8b112d22011-06-08 16:22:53 +0100617 *strict_mode_function_map);
Steve Block44f0eee2011-05-26 01:26:41 +0100618
619 // The final map for the strict mode functions. Writeable prototype.
620 // This map is installed in MakeFunctionInstancePrototypeWritable.
621 strict_mode_function_instance_map_writable_prototype_ =
622 CreateStrictModeFunctionMap(
623 ADD_WRITEABLE_PROTOTYPE, empty, arguments, caller);
624
Ben Murdoch257744e2011-11-30 15:57:28 +0000625 // Create the ThrowTypeError function instance.
626 Handle<JSFunction> throw_function =
627 GetThrowTypeErrorFunction();
Steve Block44f0eee2011-05-26 01:26:41 +0100628
629 // Complete the callback fixed arrays.
Ben Murdoch257744e2011-11-30 15:57:28 +0000630 arguments->set(0, *throw_function);
631 arguments->set(1, *throw_function);
632 caller->set(0, *throw_function);
633 caller->set(1, *throw_function);
Steve Block44f0eee2011-05-26 01:26:41 +0100634}
635
636
Ben Murdochb0fe1622011-05-05 13:52:32 +0100637static void AddToWeakGlobalContextList(Context* context) {
638 ASSERT(context->IsGlobalContext());
Ben Murdoch257744e2011-11-30 15:57:28 +0000639 Heap* heap = context->GetIsolate()->heap();
Ben Murdochb0fe1622011-05-05 13:52:32 +0100640#ifdef DEBUG
641 { // NOLINT
642 ASSERT(context->get(Context::NEXT_CONTEXT_LINK)->IsUndefined());
643 // Check that context is not in the list yet.
Steve Block44f0eee2011-05-26 01:26:41 +0100644 for (Object* current = heap->global_contexts_list();
Ben Murdochb0fe1622011-05-05 13:52:32 +0100645 !current->IsUndefined();
646 current = Context::cast(current)->get(Context::NEXT_CONTEXT_LINK)) {
647 ASSERT(current != context);
648 }
649 }
650#endif
Steve Block44f0eee2011-05-26 01:26:41 +0100651 context->set(Context::NEXT_CONTEXT_LINK, heap->global_contexts_list());
652 heap->set_global_contexts_list(context);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100653}
654
655
Andrei Popescu31002712010-02-23 13:46:05 +0000656void Genesis::CreateRoots() {
657 // Allocate the global context FixedArray first and then patch the
658 // closure and extension object later (we need the empty function
659 // and the global object, but in order to create those, we need the
660 // global context).
Ben Murdoch257744e2011-11-30 15:57:28 +0000661 global_context_ = Handle<Context>::cast(isolate()->global_handles()->Create(
662 *factory()->NewGlobalContext()));
Ben Murdochb0fe1622011-05-05 13:52:32 +0100663 AddToWeakGlobalContextList(*global_context_);
Ben Murdoch257744e2011-11-30 15:57:28 +0000664 isolate()->set_context(*global_context());
Andrei Popescu31002712010-02-23 13:46:05 +0000665
666 // Allocate the message listeners object.
667 {
668 v8::NeanderArray listeners;
669 global_context()->set_message_listeners(*listeners.value());
670 }
671}
672
673
674Handle<JSGlobalProxy> Genesis::CreateNewGlobals(
675 v8::Handle<v8::ObjectTemplate> global_template,
676 Handle<Object> global_object,
677 Handle<GlobalObject>* inner_global_out) {
678 // The argument global_template aka data is an ObjectTemplateInfo.
679 // It has a constructor pointer that points at global_constructor which is a
680 // FunctionTemplateInfo.
681 // The global_constructor is used to create or reinitialize the global_proxy.
682 // The global_constructor also has a prototype_template pointer that points at
683 // js_global_template which is an ObjectTemplateInfo.
684 // That in turn has a constructor pointer that points at
685 // js_global_constructor which is a FunctionTemplateInfo.
686 // js_global_constructor is used to make js_global_function
687 // js_global_function is used to make the new inner_global.
688 //
689 // --- G l o b a l ---
690 // Step 1: Create a fresh inner JSGlobalObject.
691 Handle<JSFunction> js_global_function;
692 Handle<ObjectTemplateInfo> js_global_template;
693 if (!global_template.IsEmpty()) {
694 // Get prototype template of the global_template.
695 Handle<ObjectTemplateInfo> data =
696 v8::Utils::OpenHandle(*global_template);
697 Handle<FunctionTemplateInfo> global_constructor =
698 Handle<FunctionTemplateInfo>(
699 FunctionTemplateInfo::cast(data->constructor()));
700 Handle<Object> proto_template(global_constructor->prototype_template());
701 if (!proto_template->IsUndefined()) {
702 js_global_template =
703 Handle<ObjectTemplateInfo>::cast(proto_template);
704 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000705 }
706
Andrei Popescu31002712010-02-23 13:46:05 +0000707 if (js_global_template.is_null()) {
Ben Murdoch257744e2011-11-30 15:57:28 +0000708 Handle<String> name = Handle<String>(heap()->empty_symbol());
709 Handle<Code> code = Handle<Code>(isolate()->builtins()->builtin(
Steve Block44f0eee2011-05-26 01:26:41 +0100710 Builtins::kIllegal));
Andrei Popescu31002712010-02-23 13:46:05 +0000711 js_global_function =
Ben Murdoch257744e2011-11-30 15:57:28 +0000712 factory()->NewFunction(name, JS_GLOBAL_OBJECT_TYPE,
713 JSGlobalObject::kSize, code, true);
Andrei Popescu31002712010-02-23 13:46:05 +0000714 // Change the constructor property of the prototype of the
715 // hidden global function to refer to the Object function.
716 Handle<JSObject> prototype =
717 Handle<JSObject>(
718 JSObject::cast(js_global_function->instance_prototype()));
Steve Block1e0659c2011-05-24 12:43:12 +0100719 SetLocalPropertyNoThrow(
Steve Block44f0eee2011-05-26 01:26:41 +0100720 prototype,
Ben Murdoch257744e2011-11-30 15:57:28 +0000721 factory()->constructor_symbol(),
722 isolate()->object_function(),
Steve Block44f0eee2011-05-26 01:26:41 +0100723 NONE);
Andrei Popescu31002712010-02-23 13:46:05 +0000724 } else {
725 Handle<FunctionTemplateInfo> js_global_constructor(
726 FunctionTemplateInfo::cast(js_global_template->constructor()));
727 js_global_function =
Ben Murdoch257744e2011-11-30 15:57:28 +0000728 factory()->CreateApiFunction(js_global_constructor,
729 factory()->InnerGlobalObject);
Steve Blocka7e24c12009-10-30 11:49:00 +0000730 }
731
Andrei Popescu31002712010-02-23 13:46:05 +0000732 js_global_function->initial_map()->set_is_hidden_prototype();
733 Handle<GlobalObject> inner_global =
Ben Murdoch257744e2011-11-30 15:57:28 +0000734 factory()->NewGlobalObject(js_global_function);
Andrei Popescu31002712010-02-23 13:46:05 +0000735 if (inner_global_out != NULL) {
736 *inner_global_out = inner_global;
737 }
738
739 // Step 2: create or re-initialize the global proxy object.
740 Handle<JSFunction> global_proxy_function;
741 if (global_template.IsEmpty()) {
Ben Murdoch257744e2011-11-30 15:57:28 +0000742 Handle<String> name = Handle<String>(heap()->empty_symbol());
743 Handle<Code> code = Handle<Code>(isolate()->builtins()->builtin(
Steve Block44f0eee2011-05-26 01:26:41 +0100744 Builtins::kIllegal));
Andrei Popescu31002712010-02-23 13:46:05 +0000745 global_proxy_function =
Ben Murdoch257744e2011-11-30 15:57:28 +0000746 factory()->NewFunction(name, JS_GLOBAL_PROXY_TYPE,
747 JSGlobalProxy::kSize, code, true);
Andrei Popescu31002712010-02-23 13:46:05 +0000748 } else {
749 Handle<ObjectTemplateInfo> data =
750 v8::Utils::OpenHandle(*global_template);
751 Handle<FunctionTemplateInfo> global_constructor(
752 FunctionTemplateInfo::cast(data->constructor()));
753 global_proxy_function =
Ben Murdoch257744e2011-11-30 15:57:28 +0000754 factory()->CreateApiFunction(global_constructor,
755 factory()->OuterGlobalObject);
Andrei Popescu31002712010-02-23 13:46:05 +0000756 }
757
Ben Murdoch257744e2011-11-30 15:57:28 +0000758 Handle<String> global_name = factory()->LookupAsciiSymbol("global");
Andrei Popescu31002712010-02-23 13:46:05 +0000759 global_proxy_function->shared()->set_instance_class_name(*global_name);
760 global_proxy_function->initial_map()->set_is_access_check_needed(true);
761
762 // Set global_proxy.__proto__ to js_global after ConfigureGlobalObjects
763 // Return the global proxy.
764
765 if (global_object.location() != NULL) {
766 ASSERT(global_object->IsJSGlobalProxy());
767 return ReinitializeJSGlobalProxy(
768 global_proxy_function,
769 Handle<JSGlobalProxy>::cast(global_object));
770 } else {
771 return Handle<JSGlobalProxy>::cast(
Ben Murdoch257744e2011-11-30 15:57:28 +0000772 factory()->NewJSObject(global_proxy_function, TENURED));
Andrei Popescu31002712010-02-23 13:46:05 +0000773 }
774}
775
776
777void Genesis::HookUpGlobalProxy(Handle<GlobalObject> inner_global,
778 Handle<JSGlobalProxy> global_proxy) {
779 // Set the global context for the global object.
780 inner_global->set_global_context(*global_context());
781 inner_global->set_global_receiver(*global_proxy);
782 global_proxy->set_context(*global_context());
783 global_context()->set_global_proxy(*global_proxy);
784}
785
786
Andrei Popescu402d9372010-02-26 13:31:12 +0000787void Genesis::HookUpInnerGlobal(Handle<GlobalObject> inner_global) {
788 Handle<GlobalObject> inner_global_from_snapshot(
789 GlobalObject::cast(global_context_->extension()));
790 Handle<JSBuiltinsObject> builtins_global(global_context_->builtins());
791 global_context_->set_extension(*inner_global);
792 global_context_->set_global(*inner_global);
793 global_context_->set_security_token(*inner_global);
794 static const PropertyAttributes attributes =
795 static_cast<PropertyAttributes>(READ_ONLY | DONT_DELETE);
796 ForceSetProperty(builtins_global,
Ben Murdoch257744e2011-11-30 15:57:28 +0000797 factory()->LookupAsciiSymbol("global"),
Andrei Popescu402d9372010-02-26 13:31:12 +0000798 inner_global,
799 attributes);
800 // Setup the reference from the global object to the builtins object.
801 JSGlobalObject::cast(*inner_global)->set_builtins(*builtins_global);
802 TransferNamedProperties(inner_global_from_snapshot, inner_global);
803 TransferIndexedProperties(inner_global_from_snapshot, inner_global);
804}
805
806
807// This is only called if we are not using snapshots. The equivalent
808// work in the snapshot case is done in HookUpInnerGlobal.
Andrei Popescu31002712010-02-23 13:46:05 +0000809void Genesis::InitializeGlobal(Handle<GlobalObject> inner_global,
810 Handle<JSFunction> empty_function) {
811 // --- G l o b a l C o n t e x t ---
812 // Use the empty function as closure (no scope info).
813 global_context()->set_closure(*empty_function);
814 global_context()->set_fcontext(*global_context());
815 global_context()->set_previous(NULL);
816 // Set extension and global object.
817 global_context()->set_extension(*inner_global);
818 global_context()->set_global(*inner_global);
819 // Security setup: Set the security token of the global object to
820 // its the inner global. This makes the security check between two
821 // different contexts fail by default even in case of global
822 // object reinitialization.
823 global_context()->set_security_token(*inner_global);
824
Ben Murdoch257744e2011-11-30 15:57:28 +0000825 Isolate* isolate = inner_global->GetIsolate();
Steve Block44f0eee2011-05-26 01:26:41 +0100826 Factory* factory = isolate->factory();
827 Heap* heap = isolate->heap();
828
829 Handle<String> object_name = Handle<String>(heap->Object_symbol());
Steve Block1e0659c2011-05-24 12:43:12 +0100830 SetLocalPropertyNoThrow(inner_global, object_name,
Steve Block44f0eee2011-05-26 01:26:41 +0100831 isolate->object_function(), DONT_ENUM);
Andrei Popescu31002712010-02-23 13:46:05 +0000832
Steve Blocka7e24c12009-10-30 11:49:00 +0000833 Handle<JSObject> global = Handle<JSObject>(global_context()->global());
834
835 // Install global Function object
836 InstallFunction(global, "Function", JS_FUNCTION_TYPE, JSFunction::kSize,
Steve Block44f0eee2011-05-26 01:26:41 +0100837 empty_function, Builtins::kIllegal, true); // ECMA native.
Steve Blocka7e24c12009-10-30 11:49:00 +0000838
839 { // --- A r r a y ---
840 Handle<JSFunction> array_function =
841 InstallFunction(global, "Array", JS_ARRAY_TYPE, JSArray::kSize,
Steve Block44f0eee2011-05-26 01:26:41 +0100842 isolate->initial_object_prototype(),
843 Builtins::kArrayCode, true);
Steve Blocka7e24c12009-10-30 11:49:00 +0000844 array_function->shared()->set_construct_stub(
Steve Block44f0eee2011-05-26 01:26:41 +0100845 isolate->builtins()->builtin(Builtins::kArrayConstructCode));
Steve Blocka7e24c12009-10-30 11:49:00 +0000846 array_function->shared()->DontAdaptArguments();
847
848 // This seems a bit hackish, but we need to make sure Array.length
849 // is 1.
850 array_function->shared()->set_length(1);
851 Handle<DescriptorArray> array_descriptors =
Ben Murdoch257744e2011-11-30 15:57:28 +0000852 factory->CopyAppendForeignDescriptor(
Steve Block44f0eee2011-05-26 01:26:41 +0100853 factory->empty_descriptor_array(),
854 factory->length_symbol(),
Ben Murdoch257744e2011-11-30 15:57:28 +0000855 factory->NewForeign(&Accessors::ArrayLength),
Steve Blocka7e24c12009-10-30 11:49:00 +0000856 static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE));
857
858 // Cache the fast JavaScript array map
859 global_context()->set_js_array_map(array_function->initial_map());
860 global_context()->js_array_map()->set_instance_descriptors(
861 *array_descriptors);
862 // array_function is used internally. JS code creating array object should
863 // search for the 'Array' property on the global object and use that one
864 // as the constructor. 'Array' property on a global object can be
865 // overwritten by JS code.
866 global_context()->set_array_function(*array_function);
867 }
868
869 { // --- N u m b e r ---
870 Handle<JSFunction> number_fun =
871 InstallFunction(global, "Number", 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_number_function(*number_fun);
875 }
876
877 { // --- B o o l e a n ---
878 Handle<JSFunction> boolean_fun =
879 InstallFunction(global, "Boolean", JS_VALUE_TYPE, JSValue::kSize,
Steve Block44f0eee2011-05-26 01:26:41 +0100880 isolate->initial_object_prototype(),
881 Builtins::kIllegal, true);
Steve Blocka7e24c12009-10-30 11:49:00 +0000882 global_context()->set_boolean_function(*boolean_fun);
883 }
884
885 { // --- S t r i n g ---
886 Handle<JSFunction> string_fun =
887 InstallFunction(global, "String", JS_VALUE_TYPE, JSValue::kSize,
Steve Block44f0eee2011-05-26 01:26:41 +0100888 isolate->initial_object_prototype(),
889 Builtins::kIllegal, true);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100890 string_fun->shared()->set_construct_stub(
Steve Block44f0eee2011-05-26 01:26:41 +0100891 isolate->builtins()->builtin(Builtins::kStringConstructCode));
Steve Blocka7e24c12009-10-30 11:49:00 +0000892 global_context()->set_string_function(*string_fun);
893 // Add 'length' property to strings.
894 Handle<DescriptorArray> string_descriptors =
Ben Murdoch257744e2011-11-30 15:57:28 +0000895 factory->CopyAppendForeignDescriptor(
Steve Block44f0eee2011-05-26 01:26:41 +0100896 factory->empty_descriptor_array(),
897 factory->length_symbol(),
Ben Murdoch257744e2011-11-30 15:57:28 +0000898 factory->NewForeign(&Accessors::StringLength),
Steve Blocka7e24c12009-10-30 11:49:00 +0000899 static_cast<PropertyAttributes>(DONT_ENUM |
900 DONT_DELETE |
901 READ_ONLY));
902
903 Handle<Map> string_map =
904 Handle<Map>(global_context()->string_function()->initial_map());
905 string_map->set_instance_descriptors(*string_descriptors);
906 }
907
908 { // --- D a t e ---
909 // Builtin functions for Date.prototype.
910 Handle<JSFunction> date_fun =
911 InstallFunction(global, "Date", JS_VALUE_TYPE, JSValue::kSize,
Steve Block44f0eee2011-05-26 01:26:41 +0100912 isolate->initial_object_prototype(),
913 Builtins::kIllegal, true);
Steve Blocka7e24c12009-10-30 11:49:00 +0000914
915 global_context()->set_date_function(*date_fun);
916 }
917
918
919 { // -- R e g E x p
920 // Builtin functions for RegExp.prototype.
921 Handle<JSFunction> regexp_fun =
922 InstallFunction(global, "RegExp", JS_REGEXP_TYPE, JSRegExp::kSize,
Steve Block44f0eee2011-05-26 01:26:41 +0100923 isolate->initial_object_prototype(),
924 Builtins::kIllegal, true);
Steve Blocka7e24c12009-10-30 11:49:00 +0000925 global_context()->set_regexp_function(*regexp_fun);
Steve Block6ded16b2010-05-10 14:33:55 +0100926
927 ASSERT(regexp_fun->has_initial_map());
928 Handle<Map> initial_map(regexp_fun->initial_map());
929
930 ASSERT_EQ(0, initial_map->inobject_properties());
931
Steve Block44f0eee2011-05-26 01:26:41 +0100932 Handle<DescriptorArray> descriptors = factory->NewDescriptorArray(5);
Steve Block6ded16b2010-05-10 14:33:55 +0100933 PropertyAttributes final =
934 static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE | READ_ONLY);
935 int enum_index = 0;
936 {
937 // ECMA-262, section 15.10.7.1.
Steve Block44f0eee2011-05-26 01:26:41 +0100938 FieldDescriptor field(heap->source_symbol(),
Steve Block6ded16b2010-05-10 14:33:55 +0100939 JSRegExp::kSourceFieldIndex,
940 final,
941 enum_index++);
942 descriptors->Set(0, &field);
943 }
944 {
945 // ECMA-262, section 15.10.7.2.
Steve Block44f0eee2011-05-26 01:26:41 +0100946 FieldDescriptor field(heap->global_symbol(),
Steve Block6ded16b2010-05-10 14:33:55 +0100947 JSRegExp::kGlobalFieldIndex,
948 final,
949 enum_index++);
950 descriptors->Set(1, &field);
951 }
952 {
953 // ECMA-262, section 15.10.7.3.
Steve Block44f0eee2011-05-26 01:26:41 +0100954 FieldDescriptor field(heap->ignore_case_symbol(),
Steve Block6ded16b2010-05-10 14:33:55 +0100955 JSRegExp::kIgnoreCaseFieldIndex,
956 final,
957 enum_index++);
958 descriptors->Set(2, &field);
959 }
960 {
961 // ECMA-262, section 15.10.7.4.
Steve Block44f0eee2011-05-26 01:26:41 +0100962 FieldDescriptor field(heap->multiline_symbol(),
Steve Block6ded16b2010-05-10 14:33:55 +0100963 JSRegExp::kMultilineFieldIndex,
964 final,
965 enum_index++);
966 descriptors->Set(3, &field);
967 }
968 {
969 // ECMA-262, section 15.10.7.5.
970 PropertyAttributes writable =
971 static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE);
Steve Block44f0eee2011-05-26 01:26:41 +0100972 FieldDescriptor field(heap->last_index_symbol(),
Steve Block6ded16b2010-05-10 14:33:55 +0100973 JSRegExp::kLastIndexFieldIndex,
974 writable,
975 enum_index++);
976 descriptors->Set(4, &field);
977 }
978 descriptors->SetNextEnumerationIndex(enum_index);
979 descriptors->Sort();
980
981 initial_map->set_inobject_properties(5);
982 initial_map->set_pre_allocated_property_fields(5);
983 initial_map->set_unused_property_fields(0);
984 initial_map->set_instance_size(
985 initial_map->instance_size() + 5 * kPointerSize);
986 initial_map->set_instance_descriptors(*descriptors);
Iain Merrick75681382010-08-19 15:07:18 +0100987 initial_map->set_visitor_id(StaticVisitorBase::GetVisitorId(*initial_map));
Steve Blocka7e24c12009-10-30 11:49:00 +0000988 }
989
990 { // -- J S O N
Steve Block44f0eee2011-05-26 01:26:41 +0100991 Handle<String> name = factory->NewStringFromAscii(CStrVector("JSON"));
992 Handle<JSFunction> cons = factory->NewFunction(
Steve Blocka7e24c12009-10-30 11:49:00 +0000993 name,
Steve Block44f0eee2011-05-26 01:26:41 +0100994 factory->the_hole_value());
Steve Blocka7e24c12009-10-30 11:49:00 +0000995 cons->SetInstancePrototype(global_context()->initial_object_prototype());
996 cons->SetInstanceClassName(*name);
Steve Block44f0eee2011-05-26 01:26:41 +0100997 Handle<JSObject> json_object = factory->NewJSObject(cons, TENURED);
Steve Blocka7e24c12009-10-30 11:49:00 +0000998 ASSERT(json_object->IsJSObject());
Steve Block1e0659c2011-05-24 12:43:12 +0100999 SetLocalPropertyNoThrow(global, name, json_object, DONT_ENUM);
Steve Blocka7e24c12009-10-30 11:49:00 +00001000 global_context()->set_json_object(*json_object);
1001 }
1002
1003 { // --- arguments_boilerplate_
1004 // Make sure we can recognize argument objects at runtime.
1005 // This is done by introducing an anonymous function with
1006 // class_name equals 'Arguments'.
Steve Block44f0eee2011-05-26 01:26:41 +01001007 Handle<String> symbol = factory->LookupAsciiSymbol("Arguments");
1008 Handle<Code> code = Handle<Code>(
1009 isolate->builtins()->builtin(Builtins::kIllegal));
Steve Blocka7e24c12009-10-30 11:49:00 +00001010 Handle<JSObject> prototype =
1011 Handle<JSObject>(
1012 JSObject::cast(global_context()->object_function()->prototype()));
1013
1014 Handle<JSFunction> function =
Steve Block44f0eee2011-05-26 01:26:41 +01001015 factory->NewFunctionWithPrototype(symbol,
Steve Blocka7e24c12009-10-30 11:49:00 +00001016 JS_OBJECT_TYPE,
1017 JSObject::kHeaderSize,
1018 prototype,
1019 code,
1020 false);
1021 ASSERT(!function->has_initial_map());
1022 function->shared()->set_instance_class_name(*symbol);
1023 function->shared()->set_expected_nof_properties(2);
Steve Block44f0eee2011-05-26 01:26:41 +01001024 Handle<JSObject> result = factory->NewJSObject(function);
Steve Blocka7e24c12009-10-30 11:49:00 +00001025
1026 global_context()->set_arguments_boilerplate(*result);
Steve Block44f0eee2011-05-26 01:26:41 +01001027 // Note: length must be added as the first property and
1028 // callee must be added as the second property.
1029 SetLocalPropertyNoThrow(result, factory->length_symbol(),
1030 factory->undefined_value(),
Steve Block1e0659c2011-05-24 12:43:12 +01001031 DONT_ENUM);
Steve Block44f0eee2011-05-26 01:26:41 +01001032 SetLocalPropertyNoThrow(result, factory->callee_symbol(),
1033 factory->undefined_value(),
Steve Block1e0659c2011-05-24 12:43:12 +01001034 DONT_ENUM);
Steve Blocka7e24c12009-10-30 11:49:00 +00001035
1036#ifdef DEBUG
1037 LookupResult lookup;
Steve Block44f0eee2011-05-26 01:26:41 +01001038 result->LocalLookup(heap->callee_symbol(), &lookup);
Andrei Popescu402d9372010-02-26 13:31:12 +00001039 ASSERT(lookup.IsProperty() && (lookup.type() == FIELD));
Steve Block44f0eee2011-05-26 01:26:41 +01001040 ASSERT(lookup.GetFieldIndex() == Heap::kArgumentsCalleeIndex);
Steve Blocka7e24c12009-10-30 11:49:00 +00001041
Steve Block44f0eee2011-05-26 01:26:41 +01001042 result->LocalLookup(heap->length_symbol(), &lookup);
Andrei Popescu402d9372010-02-26 13:31:12 +00001043 ASSERT(lookup.IsProperty() && (lookup.type() == FIELD));
Steve Block44f0eee2011-05-26 01:26:41 +01001044 ASSERT(lookup.GetFieldIndex() == Heap::kArgumentsLengthIndex);
Steve Blocka7e24c12009-10-30 11:49:00 +00001045
Steve Block44f0eee2011-05-26 01:26:41 +01001046 ASSERT(result->map()->inobject_properties() > Heap::kArgumentsCalleeIndex);
1047 ASSERT(result->map()->inobject_properties() > Heap::kArgumentsLengthIndex);
1048
1049 // Check the state of the object.
1050 ASSERT(result->HasFastProperties());
1051 ASSERT(result->HasFastElements());
1052#endif
1053 }
1054
1055 { // --- strict mode arguments boilerplate
1056 const PropertyAttributes attributes =
1057 static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE | READ_ONLY);
1058
1059 // Create the ThrowTypeError functions.
1060 Handle<FixedArray> callee = factory->NewFixedArray(2, TENURED);
1061 Handle<FixedArray> caller = factory->NewFixedArray(2, TENURED);
1062
Ben Murdoch257744e2011-11-30 15:57:28 +00001063 Handle<JSFunction> throw_function =
1064 GetThrowTypeErrorFunction();
Steve Block44f0eee2011-05-26 01:26:41 +01001065
1066 // Install the ThrowTypeError functions.
Ben Murdoch257744e2011-11-30 15:57:28 +00001067 callee->set(0, *throw_function);
1068 callee->set(1, *throw_function);
1069 caller->set(0, *throw_function);
1070 caller->set(1, *throw_function);
Steve Block44f0eee2011-05-26 01:26:41 +01001071
1072 // Create the descriptor array for the arguments object.
1073 Handle<DescriptorArray> descriptors = factory->NewDescriptorArray(3);
1074 { // length
1075 FieldDescriptor d(*factory->length_symbol(), 0, DONT_ENUM);
1076 descriptors->Set(0, &d);
1077 }
1078 { // callee
1079 CallbacksDescriptor d(*factory->callee_symbol(), *callee, attributes);
1080 descriptors->Set(1, &d);
1081 }
1082 { // caller
1083 CallbacksDescriptor d(*factory->caller_symbol(), *caller, attributes);
1084 descriptors->Set(2, &d);
1085 }
1086 descriptors->Sort();
1087
1088 // Create the map. Allocate one in-object field for length.
1089 Handle<Map> map = factory->NewMap(JS_OBJECT_TYPE,
1090 Heap::kArgumentsObjectSizeStrict);
1091 map->set_instance_descriptors(*descriptors);
1092 map->set_function_with_prototype(true);
1093 map->set_prototype(global_context()->object_function()->prototype());
1094 map->set_pre_allocated_property_fields(1);
1095 map->set_inobject_properties(1);
1096
1097 // Copy constructor from the non-strict arguments boilerplate.
1098 map->set_constructor(
1099 global_context()->arguments_boilerplate()->map()->constructor());
1100
1101 // Allocate the arguments boilerplate object.
1102 Handle<JSObject> result = factory->NewJSObjectFromMap(map);
1103 global_context()->set_strict_mode_arguments_boilerplate(*result);
1104
1105 // Add length property only for strict mode boilerplate.
1106 SetLocalPropertyNoThrow(result, factory->length_symbol(),
1107 factory->undefined_value(),
1108 DONT_ENUM);
1109
1110#ifdef DEBUG
1111 LookupResult lookup;
1112 result->LocalLookup(heap->length_symbol(), &lookup);
1113 ASSERT(lookup.IsProperty() && (lookup.type() == FIELD));
1114 ASSERT(lookup.GetFieldIndex() == Heap::kArgumentsLengthIndex);
1115
1116 ASSERT(result->map()->inobject_properties() > Heap::kArgumentsLengthIndex);
Steve Blocka7e24c12009-10-30 11:49:00 +00001117
1118 // Check the state of the object.
1119 ASSERT(result->HasFastProperties());
1120 ASSERT(result->HasFastElements());
1121#endif
1122 }
1123
1124 { // --- context extension
1125 // Create a function for the context extension objects.
Steve Block44f0eee2011-05-26 01:26:41 +01001126 Handle<Code> code = Handle<Code>(
1127 isolate->builtins()->builtin(Builtins::kIllegal));
Steve Blocka7e24c12009-10-30 11:49:00 +00001128 Handle<JSFunction> context_extension_fun =
Steve Block44f0eee2011-05-26 01:26:41 +01001129 factory->NewFunction(factory->empty_symbol(),
Steve Blocka7e24c12009-10-30 11:49:00 +00001130 JS_CONTEXT_EXTENSION_OBJECT_TYPE,
1131 JSObject::kHeaderSize,
1132 code,
1133 true);
1134
Steve Block44f0eee2011-05-26 01:26:41 +01001135 Handle<String> name = factory->LookupAsciiSymbol("context_extension");
Steve Blocka7e24c12009-10-30 11:49:00 +00001136 context_extension_fun->shared()->set_instance_class_name(*name);
1137 global_context()->set_context_extension_function(*context_extension_fun);
1138 }
1139
1140
1141 {
1142 // Setup the call-as-function delegate.
1143 Handle<Code> code =
Steve Block44f0eee2011-05-26 01:26:41 +01001144 Handle<Code>(isolate->builtins()->builtin(
1145 Builtins::kHandleApiCallAsFunction));
Steve Blocka7e24c12009-10-30 11:49:00 +00001146 Handle<JSFunction> delegate =
Steve Block44f0eee2011-05-26 01:26:41 +01001147 factory->NewFunction(factory->empty_symbol(), JS_OBJECT_TYPE,
Steve Blocka7e24c12009-10-30 11:49:00 +00001148 JSObject::kHeaderSize, code, true);
1149 global_context()->set_call_as_function_delegate(*delegate);
1150 delegate->shared()->DontAdaptArguments();
1151 }
1152
1153 {
1154 // Setup the call-as-constructor delegate.
1155 Handle<Code> code =
Steve Block44f0eee2011-05-26 01:26:41 +01001156 Handle<Code>(isolate->builtins()->builtin(
1157 Builtins::kHandleApiCallAsConstructor));
Steve Blocka7e24c12009-10-30 11:49:00 +00001158 Handle<JSFunction> delegate =
Steve Block44f0eee2011-05-26 01:26:41 +01001159 factory->NewFunction(factory->empty_symbol(), JS_OBJECT_TYPE,
Steve Blocka7e24c12009-10-30 11:49:00 +00001160 JSObject::kHeaderSize, code, true);
1161 global_context()->set_call_as_constructor_delegate(*delegate);
1162 delegate->shared()->DontAdaptArguments();
1163 }
1164
Steve Blocka7e24c12009-10-30 11:49:00 +00001165 // Initialize the out of memory slot.
Steve Block44f0eee2011-05-26 01:26:41 +01001166 global_context()->set_out_of_memory(heap->false_value());
Steve Blocka7e24c12009-10-30 11:49:00 +00001167
1168 // Initialize the data slot.
Steve Block44f0eee2011-05-26 01:26:41 +01001169 global_context()->set_data(heap->undefined_value());
Steve Blocka7e24c12009-10-30 11:49:00 +00001170}
1171
1172
Ben Murdoch257744e2011-11-30 15:57:28 +00001173bool Genesis::CompileBuiltin(Isolate* isolate, int index) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001174 Vector<const char> name = Natives::GetScriptName(index);
Steve Block44f0eee2011-05-26 01:26:41 +01001175 Handle<String> source_code =
Ben Murdoch257744e2011-11-30 15:57:28 +00001176 isolate->bootstrapper()->NativesSourceLookup(index);
1177 return CompileNative(name, source_code);
1178}
1179
1180
1181bool Genesis::CompileExperimentalBuiltin(Isolate* isolate, int index) {
1182 Vector<const char> name = ExperimentalNatives::GetScriptName(index);
1183 Factory* factory = isolate->factory();
1184 Handle<String> source_code =
1185 factory->NewStringFromAscii(ExperimentalNatives::GetScriptSource(index));
Steve Blocka7e24c12009-10-30 11:49:00 +00001186 return CompileNative(name, source_code);
1187}
1188
1189
1190bool Genesis::CompileNative(Vector<const char> name, Handle<String> source) {
1191 HandleScope scope;
Ben Murdoch257744e2011-11-30 15:57:28 +00001192 Isolate* isolate = source->GetIsolate();
Steve Blocka7e24c12009-10-30 11:49:00 +00001193#ifdef ENABLE_DEBUGGER_SUPPORT
Steve Block44f0eee2011-05-26 01:26:41 +01001194 isolate->debugger()->set_compiling_natives(true);
Steve Blocka7e24c12009-10-30 11:49:00 +00001195#endif
Andrei Popescu31002712010-02-23 13:46:05 +00001196 bool result = CompileScriptCached(name,
1197 source,
1198 NULL,
1199 NULL,
Steve Block44f0eee2011-05-26 01:26:41 +01001200 Handle<Context>(isolate->context()),
Andrei Popescu31002712010-02-23 13:46:05 +00001201 true);
Steve Block44f0eee2011-05-26 01:26:41 +01001202 ASSERT(isolate->has_pending_exception() != result);
1203 if (!result) isolate->clear_pending_exception();
Steve Blocka7e24c12009-10-30 11:49:00 +00001204#ifdef ENABLE_DEBUGGER_SUPPORT
Steve Block44f0eee2011-05-26 01:26:41 +01001205 isolate->debugger()->set_compiling_natives(false);
Steve Blocka7e24c12009-10-30 11:49:00 +00001206#endif
1207 return result;
1208}
1209
1210
1211bool Genesis::CompileScriptCached(Vector<const char> name,
1212 Handle<String> source,
1213 SourceCodeCache* cache,
1214 v8::Extension* extension,
Andrei Popescu31002712010-02-23 13:46:05 +00001215 Handle<Context> top_context,
Steve Blocka7e24c12009-10-30 11:49:00 +00001216 bool use_runtime_context) {
Ben Murdoch257744e2011-11-30 15:57:28 +00001217 Factory* factory = source->GetIsolate()->factory();
Steve Blocka7e24c12009-10-30 11:49:00 +00001218 HandleScope scope;
Steve Block6ded16b2010-05-10 14:33:55 +01001219 Handle<SharedFunctionInfo> function_info;
Steve Blocka7e24c12009-10-30 11:49:00 +00001220
1221 // If we can't find the function in the cache, we compile a new
1222 // function and insert it into the cache.
Steve Block6ded16b2010-05-10 14:33:55 +01001223 if (cache == NULL || !cache->Lookup(name, &function_info)) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001224 ASSERT(source->IsAsciiRepresentation());
Steve Block44f0eee2011-05-26 01:26:41 +01001225 Handle<String> script_name = factory->NewStringFromUtf8(name);
Steve Block6ded16b2010-05-10 14:33:55 +01001226 function_info = Compiler::Compile(
Andrei Popescu31002712010-02-23 13:46:05 +00001227 source,
1228 script_name,
1229 0,
1230 0,
1231 extension,
1232 NULL,
Andrei Popescu402d9372010-02-26 13:31:12 +00001233 Handle<String>::null(),
Andrei Popescu31002712010-02-23 13:46:05 +00001234 use_runtime_context ? NATIVES_CODE : NOT_NATIVES_CODE);
Steve Block6ded16b2010-05-10 14:33:55 +01001235 if (function_info.is_null()) return false;
1236 if (cache != NULL) cache->Add(name, function_info);
Steve Blocka7e24c12009-10-30 11:49:00 +00001237 }
1238
1239 // Setup the function context. Conceptually, we should clone the
1240 // function before overwriting the context but since we're in a
1241 // single-threaded environment it is not strictly necessary.
Andrei Popescu31002712010-02-23 13:46:05 +00001242 ASSERT(top_context->IsGlobalContext());
Steve Blocka7e24c12009-10-30 11:49:00 +00001243 Handle<Context> context =
1244 Handle<Context>(use_runtime_context
Andrei Popescu31002712010-02-23 13:46:05 +00001245 ? Handle<Context>(top_context->runtime_context())
1246 : top_context);
Steve Blocka7e24c12009-10-30 11:49:00 +00001247 Handle<JSFunction> fun =
Steve Block44f0eee2011-05-26 01:26:41 +01001248 factory->NewFunctionFromSharedFunctionInfo(function_info, context);
Steve Blocka7e24c12009-10-30 11:49:00 +00001249
Leon Clarke4515c472010-02-03 11:58:03 +00001250 // Call function using either the runtime object or the global
Steve Blocka7e24c12009-10-30 11:49:00 +00001251 // object as the receiver. Provide no parameters.
1252 Handle<Object> receiver =
1253 Handle<Object>(use_runtime_context
Andrei Popescu31002712010-02-23 13:46:05 +00001254 ? top_context->builtins()
1255 : top_context->global());
Steve Blocka7e24c12009-10-30 11:49:00 +00001256 bool has_pending_exception;
Ben Murdoch257744e2011-11-30 15:57:28 +00001257 Execution::Call(fun, receiver, 0, NULL, &has_pending_exception);
Steve Blocka7e24c12009-10-30 11:49:00 +00001258 if (has_pending_exception) return false;
Andrei Popescu402d9372010-02-26 13:31:12 +00001259 return true;
Steve Blocka7e24c12009-10-30 11:49:00 +00001260}
1261
1262
Ben Murdoch257744e2011-11-30 15:57:28 +00001263#define INSTALL_NATIVE(Type, name, var) \
1264 Handle<String> var##_name = factory()->LookupAsciiSymbol(name); \
1265 Object* var##_native = \
1266 global_context()->builtins()->GetPropertyNoExceptionThrown( \
1267 *var##_name); \
Ben Murdoch8b112d22011-06-08 16:22:53 +01001268 global_context()->set_##var(Type::cast(var##_native));
Steve Blocka7e24c12009-10-30 11:49:00 +00001269
Steve Block44f0eee2011-05-26 01:26:41 +01001270
Steve Blocka7e24c12009-10-30 11:49:00 +00001271void Genesis::InstallNativeFunctions() {
1272 HandleScope scope;
1273 INSTALL_NATIVE(JSFunction, "CreateDate", create_date_fun);
1274 INSTALL_NATIVE(JSFunction, "ToNumber", to_number_fun);
1275 INSTALL_NATIVE(JSFunction, "ToString", to_string_fun);
1276 INSTALL_NATIVE(JSFunction, "ToDetailString", to_detail_string_fun);
1277 INSTALL_NATIVE(JSFunction, "ToObject", to_object_fun);
1278 INSTALL_NATIVE(JSFunction, "ToInteger", to_integer_fun);
1279 INSTALL_NATIVE(JSFunction, "ToUint32", to_uint32_fun);
1280 INSTALL_NATIVE(JSFunction, "ToInt32", to_int32_fun);
Leon Clarkee46be812010-01-19 14:06:41 +00001281 INSTALL_NATIVE(JSFunction, "GlobalEval", global_eval_fun);
Steve Blocka7e24c12009-10-30 11:49:00 +00001282 INSTALL_NATIVE(JSFunction, "Instantiate", instantiate_fun);
1283 INSTALL_NATIVE(JSFunction, "ConfigureTemplateInstance",
1284 configure_instance_fun);
Steve Blocka7e24c12009-10-30 11:49:00 +00001285 INSTALL_NATIVE(JSFunction, "GetStackTraceLine", get_stack_trace_line_fun);
1286 INSTALL_NATIVE(JSObject, "functionCache", function_cache);
1287}
1288
Ben Murdoch257744e2011-11-30 15:57:28 +00001289void Genesis::InstallExperimentalNativeFunctions() {
1290 if (FLAG_harmony_proxies) {
1291 INSTALL_NATIVE(JSFunction, "DerivedGetTrap", derived_get_trap);
1292 }
1293}
1294
Steve Blocka7e24c12009-10-30 11:49:00 +00001295#undef INSTALL_NATIVE
1296
1297
1298bool Genesis::InstallNatives() {
1299 HandleScope scope;
1300
1301 // Create a function for the builtins object. Allocate space for the
1302 // JavaScript builtins, a reference to the builtins object
1303 // (itself) and a reference to the global_context directly in the object.
Steve Block44f0eee2011-05-26 01:26:41 +01001304 Handle<Code> code = Handle<Code>(
Ben Murdoch257744e2011-11-30 15:57:28 +00001305 isolate()->builtins()->builtin(Builtins::kIllegal));
Steve Blocka7e24c12009-10-30 11:49:00 +00001306 Handle<JSFunction> builtins_fun =
Ben Murdoch257744e2011-11-30 15:57:28 +00001307 factory()->NewFunction(factory()->empty_symbol(),
1308 JS_BUILTINS_OBJECT_TYPE,
1309 JSBuiltinsObject::kSize, code, true);
Steve Blocka7e24c12009-10-30 11:49:00 +00001310
Ben Murdoch257744e2011-11-30 15:57:28 +00001311 Handle<String> name = factory()->LookupAsciiSymbol("builtins");
Steve Blocka7e24c12009-10-30 11:49:00 +00001312 builtins_fun->shared()->set_instance_class_name(*name);
1313
1314 // Allocate the builtins object.
1315 Handle<JSBuiltinsObject> builtins =
Ben Murdoch257744e2011-11-30 15:57:28 +00001316 Handle<JSBuiltinsObject>::cast(factory()->NewGlobalObject(builtins_fun));
Steve Blocka7e24c12009-10-30 11:49:00 +00001317 builtins->set_builtins(*builtins);
1318 builtins->set_global_context(*global_context());
1319 builtins->set_global_receiver(*builtins);
1320
1321 // Setup the 'global' properties of the builtins object. The
1322 // 'global' property that refers to the global object is the only
1323 // way to get from code running in the builtins context to the
1324 // global object.
1325 static const PropertyAttributes attributes =
1326 static_cast<PropertyAttributes>(READ_ONLY | DONT_DELETE);
Ben Murdoch257744e2011-11-30 15:57:28 +00001327 Handle<String> global_symbol = factory()->LookupAsciiSymbol("global");
Steve Block1e0659c2011-05-24 12:43:12 +01001328 Handle<Object> global_obj(global_context()->global());
1329 SetLocalPropertyNoThrow(builtins, global_symbol, global_obj, attributes);
Steve Blocka7e24c12009-10-30 11:49:00 +00001330
1331 // Setup the reference from the global object to the builtins object.
1332 JSGlobalObject::cast(global_context()->global())->set_builtins(*builtins);
1333
1334 // Create a bridge function that has context in the global context.
1335 Handle<JSFunction> bridge =
Ben Murdoch257744e2011-11-30 15:57:28 +00001336 factory()->NewFunction(factory()->empty_symbol(),
1337 factory()->undefined_value());
1338 ASSERT(bridge->context() == *isolate()->global_context());
Steve Blocka7e24c12009-10-30 11:49:00 +00001339
1340 // Allocate the builtins context.
1341 Handle<Context> context =
Ben Murdoch257744e2011-11-30 15:57:28 +00001342 factory()->NewFunctionContext(Context::MIN_CONTEXT_SLOTS, bridge);
Steve Blocka7e24c12009-10-30 11:49:00 +00001343 context->set_global(*builtins); // override builtins global object
1344
1345 global_context()->set_runtime_context(*context);
1346
1347 { // -- S c r i p t
1348 // Builtin functions for Script.
1349 Handle<JSFunction> script_fun =
1350 InstallFunction(builtins, "Script", JS_VALUE_TYPE, JSValue::kSize,
Ben Murdoch257744e2011-11-30 15:57:28 +00001351 isolate()->initial_object_prototype(),
Steve Block44f0eee2011-05-26 01:26:41 +01001352 Builtins::kIllegal, false);
Steve Blocka7e24c12009-10-30 11:49:00 +00001353 Handle<JSObject> prototype =
Ben Murdoch257744e2011-11-30 15:57:28 +00001354 factory()->NewJSObject(isolate()->object_function(), TENURED);
Steve Blocka7e24c12009-10-30 11:49:00 +00001355 SetPrototype(script_fun, prototype);
1356 global_context()->set_script_function(*script_fun);
1357
1358 // Add 'source' and 'data' property to scripts.
1359 PropertyAttributes common_attributes =
1360 static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE | READ_ONLY);
Ben Murdoch257744e2011-11-30 15:57:28 +00001361 Handle<Foreign> foreign_source =
1362 factory()->NewForeign(&Accessors::ScriptSource);
Steve Blocka7e24c12009-10-30 11:49:00 +00001363 Handle<DescriptorArray> script_descriptors =
Ben Murdoch257744e2011-11-30 15:57:28 +00001364 factory()->CopyAppendForeignDescriptor(
1365 factory()->empty_descriptor_array(),
1366 factory()->LookupAsciiSymbol("source"),
1367 foreign_source,
Steve Blocka7e24c12009-10-30 11:49:00 +00001368 common_attributes);
Ben Murdoch257744e2011-11-30 15:57:28 +00001369 Handle<Foreign> foreign_name =
1370 factory()->NewForeign(&Accessors::ScriptName);
Steve Blocka7e24c12009-10-30 11:49:00 +00001371 script_descriptors =
Ben Murdoch257744e2011-11-30 15:57:28 +00001372 factory()->CopyAppendForeignDescriptor(
Steve Blocka7e24c12009-10-30 11:49:00 +00001373 script_descriptors,
Ben Murdoch257744e2011-11-30 15:57:28 +00001374 factory()->LookupAsciiSymbol("name"),
1375 foreign_name,
Steve Blocka7e24c12009-10-30 11:49:00 +00001376 common_attributes);
Ben Murdoch257744e2011-11-30 15:57:28 +00001377 Handle<Foreign> foreign_id = factory()->NewForeign(&Accessors::ScriptId);
Steve Blocka7e24c12009-10-30 11:49:00 +00001378 script_descriptors =
Ben Murdoch257744e2011-11-30 15:57:28 +00001379 factory()->CopyAppendForeignDescriptor(
Steve Blocka7e24c12009-10-30 11:49:00 +00001380 script_descriptors,
Ben Murdoch257744e2011-11-30 15:57:28 +00001381 factory()->LookupAsciiSymbol("id"),
1382 foreign_id,
Steve Blocka7e24c12009-10-30 11:49:00 +00001383 common_attributes);
Ben Murdoch257744e2011-11-30 15:57:28 +00001384 Handle<Foreign> foreign_line_offset =
1385 factory()->NewForeign(&Accessors::ScriptLineOffset);
Steve Blocka7e24c12009-10-30 11:49:00 +00001386 script_descriptors =
Ben Murdoch257744e2011-11-30 15:57:28 +00001387 factory()->CopyAppendForeignDescriptor(
Steve Blocka7e24c12009-10-30 11:49:00 +00001388 script_descriptors,
Ben Murdoch257744e2011-11-30 15:57:28 +00001389 factory()->LookupAsciiSymbol("line_offset"),
1390 foreign_line_offset,
Steve Blocka7e24c12009-10-30 11:49:00 +00001391 common_attributes);
Ben Murdoch257744e2011-11-30 15:57:28 +00001392 Handle<Foreign> foreign_column_offset =
1393 factory()->NewForeign(&Accessors::ScriptColumnOffset);
Steve Blocka7e24c12009-10-30 11:49:00 +00001394 script_descriptors =
Ben Murdoch257744e2011-11-30 15:57:28 +00001395 factory()->CopyAppendForeignDescriptor(
Steve Blocka7e24c12009-10-30 11:49:00 +00001396 script_descriptors,
Ben Murdoch257744e2011-11-30 15:57:28 +00001397 factory()->LookupAsciiSymbol("column_offset"),
1398 foreign_column_offset,
Steve Blocka7e24c12009-10-30 11:49:00 +00001399 common_attributes);
Ben Murdoch257744e2011-11-30 15:57:28 +00001400 Handle<Foreign> foreign_data =
1401 factory()->NewForeign(&Accessors::ScriptData);
Steve Blocka7e24c12009-10-30 11:49:00 +00001402 script_descriptors =
Ben Murdoch257744e2011-11-30 15:57:28 +00001403 factory()->CopyAppendForeignDescriptor(
Steve Blocka7e24c12009-10-30 11:49:00 +00001404 script_descriptors,
Ben Murdoch257744e2011-11-30 15:57:28 +00001405 factory()->LookupAsciiSymbol("data"),
1406 foreign_data,
Steve Blocka7e24c12009-10-30 11:49:00 +00001407 common_attributes);
Ben Murdoch257744e2011-11-30 15:57:28 +00001408 Handle<Foreign> foreign_type =
1409 factory()->NewForeign(&Accessors::ScriptType);
Steve Blocka7e24c12009-10-30 11:49:00 +00001410 script_descriptors =
Ben Murdoch257744e2011-11-30 15:57:28 +00001411 factory()->CopyAppendForeignDescriptor(
Steve Blocka7e24c12009-10-30 11:49:00 +00001412 script_descriptors,
Ben Murdoch257744e2011-11-30 15:57:28 +00001413 factory()->LookupAsciiSymbol("type"),
1414 foreign_type,
Steve Blocka7e24c12009-10-30 11:49:00 +00001415 common_attributes);
Ben Murdoch257744e2011-11-30 15:57:28 +00001416 Handle<Foreign> foreign_compilation_type =
1417 factory()->NewForeign(&Accessors::ScriptCompilationType);
Steve Blocka7e24c12009-10-30 11:49:00 +00001418 script_descriptors =
Ben Murdoch257744e2011-11-30 15:57:28 +00001419 factory()->CopyAppendForeignDescriptor(
Steve Blocka7e24c12009-10-30 11:49:00 +00001420 script_descriptors,
Ben Murdoch257744e2011-11-30 15:57:28 +00001421 factory()->LookupAsciiSymbol("compilation_type"),
1422 foreign_compilation_type,
Steve Blocka7e24c12009-10-30 11:49:00 +00001423 common_attributes);
Ben Murdoch257744e2011-11-30 15:57:28 +00001424 Handle<Foreign> foreign_line_ends =
1425 factory()->NewForeign(&Accessors::ScriptLineEnds);
Steve Blocka7e24c12009-10-30 11:49:00 +00001426 script_descriptors =
Ben Murdoch257744e2011-11-30 15:57:28 +00001427 factory()->CopyAppendForeignDescriptor(
Steve Blocka7e24c12009-10-30 11:49:00 +00001428 script_descriptors,
Ben Murdoch257744e2011-11-30 15:57:28 +00001429 factory()->LookupAsciiSymbol("line_ends"),
1430 foreign_line_ends,
Steve Blocka7e24c12009-10-30 11:49:00 +00001431 common_attributes);
Ben Murdoch257744e2011-11-30 15:57:28 +00001432 Handle<Foreign> foreign_context_data =
1433 factory()->NewForeign(&Accessors::ScriptContextData);
Steve Blocka7e24c12009-10-30 11:49:00 +00001434 script_descriptors =
Ben Murdoch257744e2011-11-30 15:57:28 +00001435 factory()->CopyAppendForeignDescriptor(
Steve Blocka7e24c12009-10-30 11:49:00 +00001436 script_descriptors,
Ben Murdoch257744e2011-11-30 15:57:28 +00001437 factory()->LookupAsciiSymbol("context_data"),
1438 foreign_context_data,
Steve Blocka7e24c12009-10-30 11:49:00 +00001439 common_attributes);
Ben Murdoch257744e2011-11-30 15:57:28 +00001440 Handle<Foreign> foreign_eval_from_script =
1441 factory()->NewForeign(&Accessors::ScriptEvalFromScript);
Steve Blocka7e24c12009-10-30 11:49:00 +00001442 script_descriptors =
Ben Murdoch257744e2011-11-30 15:57:28 +00001443 factory()->CopyAppendForeignDescriptor(
Steve Blocka7e24c12009-10-30 11:49:00 +00001444 script_descriptors,
Ben Murdoch257744e2011-11-30 15:57:28 +00001445 factory()->LookupAsciiSymbol("eval_from_script"),
1446 foreign_eval_from_script,
Steve Blocka7e24c12009-10-30 11:49:00 +00001447 common_attributes);
Ben Murdoch257744e2011-11-30 15:57:28 +00001448 Handle<Foreign> foreign_eval_from_script_position =
1449 factory()->NewForeign(&Accessors::ScriptEvalFromScriptPosition);
Steve Blocka7e24c12009-10-30 11:49:00 +00001450 script_descriptors =
Ben Murdoch257744e2011-11-30 15:57:28 +00001451 factory()->CopyAppendForeignDescriptor(
Steve Blocka7e24c12009-10-30 11:49:00 +00001452 script_descriptors,
Ben Murdoch257744e2011-11-30 15:57:28 +00001453 factory()->LookupAsciiSymbol("eval_from_script_position"),
1454 foreign_eval_from_script_position,
Steve Blockd0582a62009-12-15 09:54:21 +00001455 common_attributes);
Ben Murdoch257744e2011-11-30 15:57:28 +00001456 Handle<Foreign> foreign_eval_from_function_name =
1457 factory()->NewForeign(&Accessors::ScriptEvalFromFunctionName);
Steve Blockd0582a62009-12-15 09:54:21 +00001458 script_descriptors =
Ben Murdoch257744e2011-11-30 15:57:28 +00001459 factory()->CopyAppendForeignDescriptor(
Steve Blockd0582a62009-12-15 09:54:21 +00001460 script_descriptors,
Ben Murdoch257744e2011-11-30 15:57:28 +00001461 factory()->LookupAsciiSymbol("eval_from_function_name"),
1462 foreign_eval_from_function_name,
Steve Blocka7e24c12009-10-30 11:49:00 +00001463 common_attributes);
1464
1465 Handle<Map> script_map = Handle<Map>(script_fun->initial_map());
1466 script_map->set_instance_descriptors(*script_descriptors);
1467
1468 // Allocate the empty script.
Ben Murdoch257744e2011-11-30 15:57:28 +00001469 Handle<Script> script = factory()->NewScript(factory()->empty_string());
Steve Blocka7e24c12009-10-30 11:49:00 +00001470 script->set_type(Smi::FromInt(Script::TYPE_NATIVE));
Ben Murdoch257744e2011-11-30 15:57:28 +00001471 heap()->public_set_empty_script(*script);
Steve Blocka7e24c12009-10-30 11:49:00 +00001472 }
Steve Block6ded16b2010-05-10 14:33:55 +01001473 {
1474 // Builtin function for OpaqueReference -- a JSValue-based object,
1475 // that keeps its field isolated from JavaScript code. It may store
1476 // objects, that JavaScript code may not access.
1477 Handle<JSFunction> opaque_reference_fun =
1478 InstallFunction(builtins, "OpaqueReference", JS_VALUE_TYPE,
Steve Block44f0eee2011-05-26 01:26:41 +01001479 JSValue::kSize,
Ben Murdoch257744e2011-11-30 15:57:28 +00001480 isolate()->initial_object_prototype(),
Steve Block44f0eee2011-05-26 01:26:41 +01001481 Builtins::kIllegal, false);
Steve Block6ded16b2010-05-10 14:33:55 +01001482 Handle<JSObject> prototype =
Ben Murdoch257744e2011-11-30 15:57:28 +00001483 factory()->NewJSObject(isolate()->object_function(), TENURED);
Steve Block6ded16b2010-05-10 14:33:55 +01001484 SetPrototype(opaque_reference_fun, prototype);
1485 global_context()->set_opaque_reference_function(*opaque_reference_fun);
1486 }
1487
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001488 { // --- I n t e r n a l A r r a y ---
1489 // An array constructor on the builtins object that works like
1490 // the public Array constructor, except that its prototype
1491 // doesn't inherit from Object.prototype.
1492 // To be used only for internal work by builtins. Instances
1493 // must not be leaked to user code.
1494 // Only works correctly when called as a constructor. The normal
1495 // Array code uses Array.prototype as prototype when called as
1496 // a function.
1497 Handle<JSFunction> array_function =
1498 InstallFunction(builtins,
1499 "InternalArray",
1500 JS_ARRAY_TYPE,
1501 JSArray::kSize,
Ben Murdoch257744e2011-11-30 15:57:28 +00001502 isolate()->initial_object_prototype(),
Steve Block44f0eee2011-05-26 01:26:41 +01001503 Builtins::kArrayCode,
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001504 true);
1505 Handle<JSObject> prototype =
Ben Murdoch257744e2011-11-30 15:57:28 +00001506 factory()->NewJSObject(isolate()->object_function(), TENURED);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001507 SetPrototype(array_function, prototype);
1508
1509 array_function->shared()->set_construct_stub(
Ben Murdoch257744e2011-11-30 15:57:28 +00001510 isolate()->builtins()->builtin(Builtins::kArrayConstructCode));
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001511 array_function->shared()->DontAdaptArguments();
1512
1513 // Make "length" magic on instances.
1514 Handle<DescriptorArray> array_descriptors =
Ben Murdoch257744e2011-11-30 15:57:28 +00001515 factory()->CopyAppendForeignDescriptor(
1516 factory()->empty_descriptor_array(),
1517 factory()->length_symbol(),
1518 factory()->NewForeign(&Accessors::ArrayLength),
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001519 static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE));
1520
1521 array_function->initial_map()->set_instance_descriptors(
1522 *array_descriptors);
1523 }
1524
Steve Block6ded16b2010-05-10 14:33:55 +01001525 if (FLAG_disable_native_files) {
1526 PrintF("Warning: Running without installed natives!\n");
1527 return true;
1528 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001529
Andrei Popescu31002712010-02-23 13:46:05 +00001530 // Install natives.
1531 for (int i = Natives::GetDebuggerCount();
1532 i < Natives::GetBuiltinsCount();
1533 i++) {
Ben Murdoch257744e2011-11-30 15:57:28 +00001534 if (!CompileBuiltin(isolate(), i)) return false;
Andrei Popescu402d9372010-02-26 13:31:12 +00001535 // TODO(ager): We really only need to install the JS builtin
1536 // functions on the builtins object after compiling and running
1537 // runtime.js.
1538 if (!InstallJSBuiltins(builtins)) return false;
Steve Blocka7e24c12009-10-30 11:49:00 +00001539 }
1540
1541 InstallNativeFunctions();
1542
Iain Merrick75681382010-08-19 15:07:18 +01001543 // Store the map for the string prototype after the natives has been compiled
1544 // and the String function has been setup.
1545 Handle<JSFunction> string_function(global_context()->string_function());
1546 ASSERT(JSObject::cast(
1547 string_function->initial_map()->prototype())->HasFastProperties());
1548 global_context()->set_string_function_prototype_map(
1549 HeapObject::cast(string_function->initial_map()->prototype())->map());
1550
Steve Blocka7e24c12009-10-30 11:49:00 +00001551 // Install Function.prototype.call and apply.
Ben Murdoch257744e2011-11-30 15:57:28 +00001552 { Handle<String> key = factory()->function_class_symbol();
Steve Blocka7e24c12009-10-30 11:49:00 +00001553 Handle<JSFunction> function =
Ben Murdoch257744e2011-11-30 15:57:28 +00001554 Handle<JSFunction>::cast(GetProperty(isolate()->global(), key));
Steve Blocka7e24c12009-10-30 11:49:00 +00001555 Handle<JSObject> proto =
1556 Handle<JSObject>(JSObject::cast(function->instance_prototype()));
1557
1558 // Install the call and the apply functions.
1559 Handle<JSFunction> call =
1560 InstallFunction(proto, "call", JS_OBJECT_TYPE, JSObject::kHeaderSize,
Steve Block6ded16b2010-05-10 14:33:55 +01001561 Handle<JSObject>::null(),
Steve Block44f0eee2011-05-26 01:26:41 +01001562 Builtins::kFunctionCall,
Steve Blocka7e24c12009-10-30 11:49:00 +00001563 false);
1564 Handle<JSFunction> apply =
1565 InstallFunction(proto, "apply", JS_OBJECT_TYPE, JSObject::kHeaderSize,
Steve Block6ded16b2010-05-10 14:33:55 +01001566 Handle<JSObject>::null(),
Steve Block44f0eee2011-05-26 01:26:41 +01001567 Builtins::kFunctionApply,
Steve Blocka7e24c12009-10-30 11:49:00 +00001568 false);
1569
1570 // Make sure that Function.prototype.call appears to be compiled.
1571 // The code will never be called, but inline caching for call will
1572 // only work if it appears to be compiled.
1573 call->shared()->DontAdaptArguments();
1574 ASSERT(call->is_compiled());
1575
1576 // Set the expected parameters for apply to 2; required by builtin.
1577 apply->shared()->set_formal_parameter_count(2);
1578
1579 // Set the lengths for the functions to satisfy ECMA-262.
1580 call->shared()->set_length(1);
1581 apply->shared()->set_length(2);
1582 }
1583
Ben Murdoch42effa52011-08-19 16:40:31 +01001584 InstallBuiltinFunctionIds();
1585
Steve Block6ded16b2010-05-10 14:33:55 +01001586 // Create a constructor for RegExp results (a variant of Array that
1587 // predefines the two properties index and match).
1588 {
1589 // RegExpResult initial map.
1590
1591 // Find global.Array.prototype to inherit from.
1592 Handle<JSFunction> array_constructor(global_context()->array_function());
1593 Handle<JSObject> array_prototype(
1594 JSObject::cast(array_constructor->instance_prototype()));
1595
1596 // Add initial map.
1597 Handle<Map> initial_map =
Ben Murdoch257744e2011-11-30 15:57:28 +00001598 factory()->NewMap(JS_ARRAY_TYPE, JSRegExpResult::kSize);
Steve Block6ded16b2010-05-10 14:33:55 +01001599 initial_map->set_constructor(*array_constructor);
1600
1601 // Set prototype on map.
1602 initial_map->set_non_instance_prototype(false);
1603 initial_map->set_prototype(*array_prototype);
1604
1605 // Update map with length accessor from Array and add "index" and "input".
1606 Handle<Map> array_map(global_context()->js_array_map());
1607 Handle<DescriptorArray> array_descriptors(
1608 array_map->instance_descriptors());
1609 ASSERT_EQ(1, array_descriptors->number_of_descriptors());
1610
1611 Handle<DescriptorArray> reresult_descriptors =
Ben Murdoch257744e2011-11-30 15:57:28 +00001612 factory()->NewDescriptorArray(3);
Steve Block6ded16b2010-05-10 14:33:55 +01001613
1614 reresult_descriptors->CopyFrom(0, *array_descriptors, 0);
1615
1616 int enum_index = 0;
1617 {
Ben Murdoch257744e2011-11-30 15:57:28 +00001618 FieldDescriptor index_field(heap()->index_symbol(),
Steve Block6ded16b2010-05-10 14:33:55 +01001619 JSRegExpResult::kIndexIndex,
1620 NONE,
1621 enum_index++);
1622 reresult_descriptors->Set(1, &index_field);
1623 }
1624
1625 {
Ben Murdoch257744e2011-11-30 15:57:28 +00001626 FieldDescriptor input_field(heap()->input_symbol(),
Steve Block6ded16b2010-05-10 14:33:55 +01001627 JSRegExpResult::kInputIndex,
1628 NONE,
1629 enum_index++);
1630 reresult_descriptors->Set(2, &input_field);
1631 }
1632 reresult_descriptors->Sort();
1633
1634 initial_map->set_inobject_properties(2);
1635 initial_map->set_pre_allocated_property_fields(2);
1636 initial_map->set_unused_property_fields(0);
1637 initial_map->set_instance_descriptors(*reresult_descriptors);
1638
1639 global_context()->set_regexp_result_map(*initial_map);
1640 }
1641
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001642
Steve Blocka7e24c12009-10-30 11:49:00 +00001643#ifdef DEBUG
1644 builtins->Verify();
1645#endif
Andrei Popescu31002712010-02-23 13:46:05 +00001646
Steve Blocka7e24c12009-10-30 11:49:00 +00001647 return true;
1648}
1649
1650
Ben Murdoch257744e2011-11-30 15:57:28 +00001651bool Genesis::InstallExperimentalNatives() {
1652 for (int i = ExperimentalNatives::GetDebuggerCount();
1653 i < ExperimentalNatives::GetBuiltinsCount();
1654 i++) {
1655 if (FLAG_harmony_proxies &&
1656 strcmp(ExperimentalNatives::GetScriptName(i).start(),
1657 "native proxy.js") == 0) {
1658 if (!CompileExperimentalBuiltin(isolate(), i)) return false;
1659 }
1660 }
1661
1662 InstallExperimentalNativeFunctions();
1663
1664 return true;
1665}
1666
1667
Ben Murdochb0fe1622011-05-05 13:52:32 +01001668static Handle<JSObject> ResolveBuiltinIdHolder(
Kristian Monsen0d5e1162010-09-30 15:31:59 +01001669 Handle<Context> global_context,
1670 const char* holder_expr) {
Ben Murdoch257744e2011-11-30 15:57:28 +00001671 Factory* factory = global_context->GetIsolate()->factory();
Kristian Monsen0d5e1162010-09-30 15:31:59 +01001672 Handle<GlobalObject> global(global_context->global());
1673 const char* period_pos = strchr(holder_expr, '.');
1674 if (period_pos == NULL) {
1675 return Handle<JSObject>::cast(
Steve Block44f0eee2011-05-26 01:26:41 +01001676 GetProperty(global, factory->LookupAsciiSymbol(holder_expr)));
Steve Block59151502010-09-22 15:07:15 +01001677 }
Kristian Monsen0d5e1162010-09-30 15:31:59 +01001678 ASSERT_EQ(".prototype", period_pos);
1679 Vector<const char> property(holder_expr,
1680 static_cast<int>(period_pos - holder_expr));
1681 Handle<JSFunction> function = Handle<JSFunction>::cast(
Steve Block44f0eee2011-05-26 01:26:41 +01001682 GetProperty(global, factory->LookupSymbol(property)));
Kristian Monsen0d5e1162010-09-30 15:31:59 +01001683 return Handle<JSObject>(JSObject::cast(function->prototype()));
1684}
1685
1686
Ben Murdochb0fe1622011-05-05 13:52:32 +01001687static void InstallBuiltinFunctionId(Handle<JSObject> holder,
1688 const char* function_name,
1689 BuiltinFunctionId id) {
Ben Murdoch257744e2011-11-30 15:57:28 +00001690 Factory* factory = holder->GetIsolate()->factory();
1691 Handle<String> name = factory->LookupAsciiSymbol(function_name);
John Reck59135872010-11-02 12:39:01 -07001692 Object* function_object = holder->GetProperty(*name)->ToObjectUnchecked();
1693 Handle<JSFunction> function(JSFunction::cast(function_object));
Kristian Monsen25f61362010-05-21 11:50:48 +01001694 function->shared()->set_function_data(Smi::FromInt(id));
1695}
1696
1697
Ben Murdochb0fe1622011-05-05 13:52:32 +01001698void Genesis::InstallBuiltinFunctionIds() {
Kristian Monsen25f61362010-05-21 11:50:48 +01001699 HandleScope scope;
Ben Murdochb0fe1622011-05-05 13:52:32 +01001700#define INSTALL_BUILTIN_ID(holder_expr, fun_name, name) \
1701 { \
1702 Handle<JSObject> holder = ResolveBuiltinIdHolder( \
1703 global_context(), #holder_expr); \
1704 BuiltinFunctionId id = k##name; \
1705 InstallBuiltinFunctionId(holder, #fun_name, id); \
Kristian Monsen25f61362010-05-21 11:50:48 +01001706 }
Ben Murdochb0fe1622011-05-05 13:52:32 +01001707 FUNCTIONS_WITH_ID_LIST(INSTALL_BUILTIN_ID)
1708#undef INSTALL_BUILTIN_ID
Kristian Monsen25f61362010-05-21 11:50:48 +01001709}
1710
1711
Steve Block6ded16b2010-05-10 14:33:55 +01001712// Do not forget to update macros.py with named constant
1713// of cache id.
1714#define JSFUNCTION_RESULT_CACHE_LIST(F) \
1715 F(16, global_context()->regexp_function())
1716
1717
Ben Murdoch257744e2011-11-30 15:57:28 +00001718static FixedArray* CreateCache(int size, Handle<JSFunction> factory_function) {
1719 Factory* factory = factory_function->GetIsolate()->factory();
Steve Block6ded16b2010-05-10 14:33:55 +01001720 // Caches are supposed to live for a long time, allocate in old space.
1721 int array_size = JSFunctionResultCache::kEntriesIndex + 2 * size;
1722 // Cannot use cast as object is not fully initialized yet.
1723 JSFunctionResultCache* cache = reinterpret_cast<JSFunctionResultCache*>(
Ben Murdoch257744e2011-11-30 15:57:28 +00001724 *factory->NewFixedArrayWithHoles(array_size, TENURED));
1725 cache->set(JSFunctionResultCache::kFactoryIndex, *factory_function);
Steve Block6ded16b2010-05-10 14:33:55 +01001726 cache->MakeZeroSize();
1727 return cache;
1728}
1729
1730
1731void Genesis::InstallJSFunctionResultCaches() {
1732 const int kNumberOfCaches = 0 +
1733#define F(size, func) + 1
1734 JSFUNCTION_RESULT_CACHE_LIST(F)
1735#undef F
1736 ;
1737
Steve Block44f0eee2011-05-26 01:26:41 +01001738 Handle<FixedArray> caches = FACTORY->NewFixedArray(kNumberOfCaches, TENURED);
Steve Block6ded16b2010-05-10 14:33:55 +01001739
1740 int index = 0;
Kristian Monsen0d5e1162010-09-30 15:31:59 +01001741
Ben Murdoch257744e2011-11-30 15:57:28 +00001742#define F(size, func) do { \
1743 FixedArray* cache = CreateCache((size), Handle<JSFunction>(func)); \
1744 caches->set(index++, cache); \
Kristian Monsen0d5e1162010-09-30 15:31:59 +01001745 } while (false)
1746
1747 JSFUNCTION_RESULT_CACHE_LIST(F);
1748
Steve Block6ded16b2010-05-10 14:33:55 +01001749#undef F
1750
1751 global_context()->set_jsfunction_result_caches(*caches);
1752}
1753
1754
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001755void Genesis::InitializeNormalizedMapCaches() {
1756 Handle<FixedArray> array(
Steve Block44f0eee2011-05-26 01:26:41 +01001757 FACTORY->NewFixedArray(NormalizedMapCache::kEntries, TENURED));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001758 global_context()->set_normalized_map_cache(NormalizedMapCache::cast(*array));
1759}
1760
1761
Andrei Popescu31002712010-02-23 13:46:05 +00001762bool Bootstrapper::InstallExtensions(Handle<Context> global_context,
1763 v8::ExtensionConfiguration* extensions) {
Ben Murdoch257744e2011-11-30 15:57:28 +00001764 Isolate* isolate = global_context->GetIsolate();
Andrei Popescu31002712010-02-23 13:46:05 +00001765 BootstrapperActive active;
Steve Block44f0eee2011-05-26 01:26:41 +01001766 SaveContext saved_context(isolate);
1767 isolate->set_context(*global_context);
Andrei Popescu31002712010-02-23 13:46:05 +00001768 if (!Genesis::InstallExtensions(global_context, extensions)) return false;
1769 Genesis::InstallSpecialObjects(global_context);
1770 return true;
1771}
1772
1773
1774void Genesis::InstallSpecialObjects(Handle<Context> global_context) {
Ben Murdoch257744e2011-11-30 15:57:28 +00001775 Factory* factory = global_context->GetIsolate()->factory();
Steve Blocka7e24c12009-10-30 11:49:00 +00001776 HandleScope scope;
1777 Handle<JSGlobalObject> js_global(
Andrei Popescu31002712010-02-23 13:46:05 +00001778 JSGlobalObject::cast(global_context->global()));
Steve Blocka7e24c12009-10-30 11:49:00 +00001779 // Expose the natives in global if a name for it is specified.
1780 if (FLAG_expose_natives_as != NULL && strlen(FLAG_expose_natives_as) != 0) {
1781 Handle<String> natives_string =
Steve Block44f0eee2011-05-26 01:26:41 +01001782 factory->LookupAsciiSymbol(FLAG_expose_natives_as);
Steve Block1e0659c2011-05-24 12:43:12 +01001783 SetLocalPropertyNoThrow(js_global, natives_string,
1784 Handle<JSObject>(js_global->builtins()), DONT_ENUM);
Steve Blocka7e24c12009-10-30 11:49:00 +00001785 }
1786
1787 Handle<Object> Error = GetProperty(js_global, "Error");
1788 if (Error->IsJSObject()) {
Steve Block44f0eee2011-05-26 01:26:41 +01001789 Handle<String> name = factory->LookupAsciiSymbol("stackTraceLimit");
Steve Block1e0659c2011-05-24 12:43:12 +01001790 SetLocalPropertyNoThrow(Handle<JSObject>::cast(Error),
1791 name,
1792 Handle<Smi>(Smi::FromInt(FLAG_stack_trace_limit)),
1793 NONE);
Steve Blocka7e24c12009-10-30 11:49:00 +00001794 }
1795
1796#ifdef ENABLE_DEBUGGER_SUPPORT
1797 // Expose the debug global object in global if a name for it is specified.
1798 if (FLAG_expose_debug_as != NULL && strlen(FLAG_expose_debug_as) != 0) {
Steve Block44f0eee2011-05-26 01:26:41 +01001799 Debug* debug = Isolate::Current()->debug();
Steve Blocka7e24c12009-10-30 11:49:00 +00001800 // If loading fails we just bail out without installing the
1801 // debugger but without tanking the whole context.
Steve Block44f0eee2011-05-26 01:26:41 +01001802 if (!debug->Load()) return;
Steve Blocka7e24c12009-10-30 11:49:00 +00001803 // Set the security token for the debugger context to the same as
1804 // the shell global context to allow calling between these (otherwise
1805 // exposing debug global object doesn't make much sense).
Steve Block44f0eee2011-05-26 01:26:41 +01001806 debug->debug_context()->set_security_token(
Andrei Popescu31002712010-02-23 13:46:05 +00001807 global_context->security_token());
Steve Blocka7e24c12009-10-30 11:49:00 +00001808
1809 Handle<String> debug_string =
Steve Block44f0eee2011-05-26 01:26:41 +01001810 factory->LookupAsciiSymbol(FLAG_expose_debug_as);
1811 Handle<Object> global_proxy(debug->debug_context()->global_proxy());
Steve Block1e0659c2011-05-24 12:43:12 +01001812 SetLocalPropertyNoThrow(js_global, debug_string, global_proxy, DONT_ENUM);
Steve Blocka7e24c12009-10-30 11:49:00 +00001813 }
1814#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00001815}
1816
1817
Andrei Popescu31002712010-02-23 13:46:05 +00001818bool Genesis::InstallExtensions(Handle<Context> global_context,
1819 v8::ExtensionConfiguration* extensions) {
Steve Block44f0eee2011-05-26 01:26:41 +01001820 // TODO(isolates): Extensions on multiple isolates may take a little more
1821 // effort. (The external API reads 'ignore'-- does that mean
1822 // we can break the interface?)
1823
Steve Blocka7e24c12009-10-30 11:49:00 +00001824 // Clear coloring of extension list
1825 v8::RegisteredExtension* current = v8::RegisteredExtension::first_extension();
1826 while (current != NULL) {
1827 current->set_state(v8::UNVISITED);
1828 current = current->next();
1829 }
Andrei Popescu31002712010-02-23 13:46:05 +00001830 // Install auto extensions.
Steve Blocka7e24c12009-10-30 11:49:00 +00001831 current = v8::RegisteredExtension::first_extension();
1832 while (current != NULL) {
1833 if (current->extension()->auto_enable())
1834 InstallExtension(current);
1835 current = current->next();
1836 }
1837
1838 if (FLAG_expose_gc) InstallExtension("v8/gc");
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01001839 if (FLAG_expose_externalize_string) InstallExtension("v8/externalize");
Steve Blocka7e24c12009-10-30 11:49:00 +00001840
1841 if (extensions == NULL) return true;
1842 // Install required extensions
1843 int count = v8::ImplementationUtilities::GetNameCount(extensions);
1844 const char** names = v8::ImplementationUtilities::GetNames(extensions);
1845 for (int i = 0; i < count; i++) {
1846 if (!InstallExtension(names[i]))
1847 return false;
1848 }
1849
1850 return true;
1851}
1852
1853
1854// Installs a named extension. This methods is unoptimized and does
1855// not scale well if we want to support a large number of extensions.
1856bool Genesis::InstallExtension(const char* name) {
1857 v8::RegisteredExtension* current = v8::RegisteredExtension::first_extension();
1858 // Loop until we find the relevant extension
1859 while (current != NULL) {
1860 if (strcmp(name, current->extension()->name()) == 0) break;
1861 current = current->next();
1862 }
1863 // Didn't find the extension; fail.
1864 if (current == NULL) {
1865 v8::Utils::ReportApiFailure(
1866 "v8::Context::New()", "Cannot find required extension");
1867 return false;
1868 }
1869 return InstallExtension(current);
1870}
1871
1872
1873bool Genesis::InstallExtension(v8::RegisteredExtension* current) {
1874 HandleScope scope;
1875
1876 if (current->state() == v8::INSTALLED) return true;
1877 // The current node has already been visited so there must be a
1878 // cycle in the dependency graph; fail.
1879 if (current->state() == v8::VISITED) {
1880 v8::Utils::ReportApiFailure(
1881 "v8::Context::New()", "Circular extension dependency");
1882 return false;
1883 }
1884 ASSERT(current->state() == v8::UNVISITED);
1885 current->set_state(v8::VISITED);
1886 v8::Extension* extension = current->extension();
1887 // Install the extension's dependencies
1888 for (int i = 0; i < extension->dependency_count(); i++) {
1889 if (!InstallExtension(extension->dependencies()[i])) return false;
1890 }
Steve Block44f0eee2011-05-26 01:26:41 +01001891 Isolate* isolate = Isolate::Current();
Steve Blocka7e24c12009-10-30 11:49:00 +00001892 Vector<const char> source = CStrVector(extension->source());
Steve Block44f0eee2011-05-26 01:26:41 +01001893 Handle<String> source_code = isolate->factory()->NewStringFromAscii(source);
Steve Blocka7e24c12009-10-30 11:49:00 +00001894 bool result = CompileScriptCached(CStrVector(extension->name()),
1895 source_code,
Steve Block44f0eee2011-05-26 01:26:41 +01001896 isolate->bootstrapper()->extensions_cache(),
Andrei Popescu31002712010-02-23 13:46:05 +00001897 extension,
Steve Block44f0eee2011-05-26 01:26:41 +01001898 Handle<Context>(isolate->context()),
Steve Blocka7e24c12009-10-30 11:49:00 +00001899 false);
Steve Block44f0eee2011-05-26 01:26:41 +01001900 ASSERT(isolate->has_pending_exception() != result);
Steve Blocka7e24c12009-10-30 11:49:00 +00001901 if (!result) {
Steve Block44f0eee2011-05-26 01:26:41 +01001902 isolate->clear_pending_exception();
Steve Blocka7e24c12009-10-30 11:49:00 +00001903 }
1904 current->set_state(v8::INSTALLED);
1905 return result;
1906}
1907
1908
Andrei Popescu402d9372010-02-26 13:31:12 +00001909bool Genesis::InstallJSBuiltins(Handle<JSBuiltinsObject> builtins) {
1910 HandleScope scope;
Ben Murdoch257744e2011-11-30 15:57:28 +00001911 Factory* factory = builtins->GetIsolate()->factory();
Andrei Popescu402d9372010-02-26 13:31:12 +00001912 for (int i = 0; i < Builtins::NumberOfJavaScriptBuiltins(); i++) {
1913 Builtins::JavaScript id = static_cast<Builtins::JavaScript>(i);
Ben Murdoch257744e2011-11-30 15:57:28 +00001914 Handle<String> name = factory->LookupAsciiSymbol(Builtins::GetName(id));
John Reck59135872010-11-02 12:39:01 -07001915 Object* function_object = builtins->GetPropertyNoExceptionThrown(*name);
Andrei Popescu402d9372010-02-26 13:31:12 +00001916 Handle<JSFunction> function
John Reck59135872010-11-02 12:39:01 -07001917 = Handle<JSFunction>(JSFunction::cast(function_object));
Andrei Popescu402d9372010-02-26 13:31:12 +00001918 builtins->set_javascript_builtin(id, *function);
1919 Handle<SharedFunctionInfo> shared
1920 = Handle<SharedFunctionInfo>(function->shared());
1921 if (!EnsureCompiled(shared, CLEAR_EXCEPTION)) return false;
Iain Merrick75681382010-08-19 15:07:18 +01001922 // Set the code object on the function object.
Ben Murdochb0fe1622011-05-05 13:52:32 +01001923 function->ReplaceCode(function->shared()->code());
Steve Block6ded16b2010-05-10 14:33:55 +01001924 builtins->set_javascript_builtin_code(id, shared->code());
Andrei Popescu402d9372010-02-26 13:31:12 +00001925 }
1926 return true;
Andrei Popescu31002712010-02-23 13:46:05 +00001927}
1928
1929
Steve Blocka7e24c12009-10-30 11:49:00 +00001930bool Genesis::ConfigureGlobalObjects(
1931 v8::Handle<v8::ObjectTemplate> global_proxy_template) {
1932 Handle<JSObject> global_proxy(
1933 JSObject::cast(global_context()->global_proxy()));
Andrei Popescu402d9372010-02-26 13:31:12 +00001934 Handle<JSObject> inner_global(JSObject::cast(global_context()->global()));
Steve Blocka7e24c12009-10-30 11:49:00 +00001935
1936 if (!global_proxy_template.IsEmpty()) {
1937 // Configure the global proxy object.
1938 Handle<ObjectTemplateInfo> proxy_data =
1939 v8::Utils::OpenHandle(*global_proxy_template);
1940 if (!ConfigureApiObject(global_proxy, proxy_data)) return false;
1941
1942 // Configure the inner global object.
1943 Handle<FunctionTemplateInfo> proxy_constructor(
1944 FunctionTemplateInfo::cast(proxy_data->constructor()));
1945 if (!proxy_constructor->prototype_template()->IsUndefined()) {
1946 Handle<ObjectTemplateInfo> inner_data(
1947 ObjectTemplateInfo::cast(proxy_constructor->prototype_template()));
Andrei Popescu402d9372010-02-26 13:31:12 +00001948 if (!ConfigureApiObject(inner_global, inner_data)) return false;
Steve Blocka7e24c12009-10-30 11:49:00 +00001949 }
1950 }
1951
Andrei Popescu402d9372010-02-26 13:31:12 +00001952 SetObjectPrototype(global_proxy, inner_global);
Steve Blocka7e24c12009-10-30 11:49:00 +00001953 return true;
1954}
1955
1956
1957bool Genesis::ConfigureApiObject(Handle<JSObject> object,
1958 Handle<ObjectTemplateInfo> object_template) {
1959 ASSERT(!object_template.is_null());
1960 ASSERT(object->IsInstanceOf(
1961 FunctionTemplateInfo::cast(object_template->constructor())));
1962
1963 bool pending_exception = false;
1964 Handle<JSObject> obj =
1965 Execution::InstantiateObject(object_template, &pending_exception);
1966 if (pending_exception) {
Ben Murdoch257744e2011-11-30 15:57:28 +00001967 ASSERT(isolate()->has_pending_exception());
1968 isolate()->clear_pending_exception();
Steve Blocka7e24c12009-10-30 11:49:00 +00001969 return false;
1970 }
1971 TransferObject(obj, object);
1972 return true;
1973}
1974
1975
1976void Genesis::TransferNamedProperties(Handle<JSObject> from,
1977 Handle<JSObject> to) {
1978 if (from->HasFastProperties()) {
1979 Handle<DescriptorArray> descs =
1980 Handle<DescriptorArray>(from->map()->instance_descriptors());
1981 for (int i = 0; i < descs->number_of_descriptors(); i++) {
1982 PropertyDetails details = PropertyDetails(descs->GetDetails(i));
1983 switch (details.type()) {
1984 case FIELD: {
1985 HandleScope inner;
1986 Handle<String> key = Handle<String>(descs->GetKey(i));
1987 int index = descs->GetFieldIndex(i);
1988 Handle<Object> value = Handle<Object>(from->FastPropertyAt(index));
Steve Block1e0659c2011-05-24 12:43:12 +01001989 SetLocalPropertyNoThrow(to, key, value, details.attributes());
Steve Blocka7e24c12009-10-30 11:49:00 +00001990 break;
1991 }
1992 case CONSTANT_FUNCTION: {
1993 HandleScope inner;
1994 Handle<String> key = Handle<String>(descs->GetKey(i));
1995 Handle<JSFunction> fun =
1996 Handle<JSFunction>(descs->GetConstantFunction(i));
Steve Block1e0659c2011-05-24 12:43:12 +01001997 SetLocalPropertyNoThrow(to, key, fun, details.attributes());
Steve Blocka7e24c12009-10-30 11:49:00 +00001998 break;
1999 }
2000 case CALLBACKS: {
2001 LookupResult result;
2002 to->LocalLookup(descs->GetKey(i), &result);
2003 // If the property is already there we skip it
Andrei Popescu402d9372010-02-26 13:31:12 +00002004 if (result.IsProperty()) continue;
Steve Blocka7e24c12009-10-30 11:49:00 +00002005 HandleScope inner;
Andrei Popescu31002712010-02-23 13:46:05 +00002006 ASSERT(!to->HasFastProperties());
2007 // Add to dictionary.
Steve Blocka7e24c12009-10-30 11:49:00 +00002008 Handle<String> key = Handle<String>(descs->GetKey(i));
Andrei Popescu31002712010-02-23 13:46:05 +00002009 Handle<Object> callbacks(descs->GetCallbacksObject(i));
2010 PropertyDetails d =
2011 PropertyDetails(details.attributes(), CALLBACKS, details.index());
2012 SetNormalizedProperty(to, key, callbacks, d);
Steve Blocka7e24c12009-10-30 11:49:00 +00002013 break;
2014 }
2015 case MAP_TRANSITION:
Steve Block44f0eee2011-05-26 01:26:41 +01002016 case EXTERNAL_ARRAY_TRANSITION:
Steve Blocka7e24c12009-10-30 11:49:00 +00002017 case CONSTANT_TRANSITION:
2018 case NULL_DESCRIPTOR:
2019 // Ignore non-properties.
2020 break;
2021 case NORMAL:
2022 // Do not occur since the from object has fast properties.
Ben Murdoch257744e2011-11-30 15:57:28 +00002023 case HANDLER:
Steve Blocka7e24c12009-10-30 11:49:00 +00002024 case INTERCEPTOR:
Ben Murdoch257744e2011-11-30 15:57:28 +00002025 // No element in instance descriptors have proxy or interceptor type.
Steve Blocka7e24c12009-10-30 11:49:00 +00002026 UNREACHABLE();
2027 break;
2028 }
2029 }
2030 } else {
2031 Handle<StringDictionary> properties =
2032 Handle<StringDictionary>(from->property_dictionary());
2033 int capacity = properties->Capacity();
2034 for (int i = 0; i < capacity; i++) {
2035 Object* raw_key(properties->KeyAt(i));
2036 if (properties->IsKey(raw_key)) {
2037 ASSERT(raw_key->IsString());
2038 // If the property is already there we skip it.
2039 LookupResult result;
2040 to->LocalLookup(String::cast(raw_key), &result);
Andrei Popescu402d9372010-02-26 13:31:12 +00002041 if (result.IsProperty()) continue;
Steve Blocka7e24c12009-10-30 11:49:00 +00002042 // Set the property.
2043 Handle<String> key = Handle<String>(String::cast(raw_key));
2044 Handle<Object> value = Handle<Object>(properties->ValueAt(i));
2045 if (value->IsJSGlobalPropertyCell()) {
2046 value = Handle<Object>(JSGlobalPropertyCell::cast(*value)->value());
2047 }
2048 PropertyDetails details = properties->DetailsAt(i);
Steve Block1e0659c2011-05-24 12:43:12 +01002049 SetLocalPropertyNoThrow(to, key, value, details.attributes());
Steve Blocka7e24c12009-10-30 11:49:00 +00002050 }
2051 }
2052 }
2053}
2054
2055
2056void Genesis::TransferIndexedProperties(Handle<JSObject> from,
2057 Handle<JSObject> to) {
2058 // Cloning the elements array is sufficient.
2059 Handle<FixedArray> from_elements =
2060 Handle<FixedArray>(FixedArray::cast(from->elements()));
Steve Block44f0eee2011-05-26 01:26:41 +01002061 Handle<FixedArray> to_elements = FACTORY->CopyFixedArray(from_elements);
Steve Blocka7e24c12009-10-30 11:49:00 +00002062 to->set_elements(*to_elements);
2063}
2064
2065
2066void Genesis::TransferObject(Handle<JSObject> from, Handle<JSObject> to) {
2067 HandleScope outer;
Ben Murdoch257744e2011-11-30 15:57:28 +00002068 Factory* factory = from->GetIsolate()->factory();
Steve Blocka7e24c12009-10-30 11:49:00 +00002069
2070 ASSERT(!from->IsJSArray());
2071 ASSERT(!to->IsJSArray());
2072
2073 TransferNamedProperties(from, to);
2074 TransferIndexedProperties(from, to);
2075
2076 // Transfer the prototype (new map is needed).
2077 Handle<Map> old_to_map = Handle<Map>(to->map());
Ben Murdoch257744e2011-11-30 15:57:28 +00002078 Handle<Map> new_to_map = factory->CopyMapDropTransitions(old_to_map);
Steve Blocka7e24c12009-10-30 11:49:00 +00002079 new_to_map->set_prototype(from->map()->prototype());
2080 to->set_map(*new_to_map);
2081}
2082
2083
2084void Genesis::MakeFunctionInstancePrototypeWritable() {
Steve Block44f0eee2011-05-26 01:26:41 +01002085 // The maps with writable prototype are created in CreateEmptyFunction
2086 // and CreateStrictModeFunctionMaps respectively. Initially the maps are
2087 // created with read-only prototype for JS builtins processing.
2088 ASSERT(!function_instance_map_writable_prototype_.is_null());
2089 ASSERT(!strict_mode_function_instance_map_writable_prototype_.is_null());
Steve Blocka7e24c12009-10-30 11:49:00 +00002090
Steve Block44f0eee2011-05-26 01:26:41 +01002091 // Replace function instance maps to make prototype writable.
2092 global_context()->set_function_map(
2093 *function_instance_map_writable_prototype_);
2094 global_context()->set_strict_mode_function_map(
2095 *strict_mode_function_instance_map_writable_prototype_);
Steve Blocka7e24c12009-10-30 11:49:00 +00002096}
2097
2098
Ben Murdoch257744e2011-11-30 15:57:28 +00002099Genesis::Genesis(Isolate* isolate,
2100 Handle<Object> global_object,
Steve Blocka7e24c12009-10-30 11:49:00 +00002101 v8::Handle<v8::ObjectTemplate> global_template,
Ben Murdoch257744e2011-11-30 15:57:28 +00002102 v8::ExtensionConfiguration* extensions) : isolate_(isolate) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002103 result_ = Handle<Context>::null();
Steve Blocka7e24c12009-10-30 11:49:00 +00002104 // If V8 isn't running and cannot be initialized, just return.
2105 if (!V8::IsRunning() && !V8::Initialize(NULL)) return;
2106
2107 // Before creating the roots we must save the context and restore it
2108 // on all function exits.
2109 HandleScope scope;
Steve Block44f0eee2011-05-26 01:26:41 +01002110 SaveContext saved_context(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00002111
Andrei Popescu31002712010-02-23 13:46:05 +00002112 Handle<Context> new_context = Snapshot::NewContextFromSnapshot();
2113 if (!new_context.is_null()) {
2114 global_context_ =
Steve Block44f0eee2011-05-26 01:26:41 +01002115 Handle<Context>::cast(isolate->global_handles()->Create(*new_context));
Ben Murdochb0fe1622011-05-05 13:52:32 +01002116 AddToWeakGlobalContextList(*global_context_);
Steve Block44f0eee2011-05-26 01:26:41 +01002117 isolate->set_context(*global_context_);
2118 isolate->counters()->contexts_created_by_snapshot()->Increment();
Andrei Popescu402d9372010-02-26 13:31:12 +00002119 Handle<GlobalObject> inner_global;
Andrei Popescu31002712010-02-23 13:46:05 +00002120 Handle<JSGlobalProxy> global_proxy =
2121 CreateNewGlobals(global_template,
2122 global_object,
Andrei Popescu402d9372010-02-26 13:31:12 +00002123 &inner_global);
2124
Andrei Popescu31002712010-02-23 13:46:05 +00002125 HookUpGlobalProxy(inner_global, global_proxy);
Andrei Popescu402d9372010-02-26 13:31:12 +00002126 HookUpInnerGlobal(inner_global);
2127
Andrei Popescu31002712010-02-23 13:46:05 +00002128 if (!ConfigureGlobalObjects(global_template)) return;
2129 } else {
2130 // We get here if there was no context snapshot.
2131 CreateRoots();
Ben Murdoch257744e2011-11-30 15:57:28 +00002132 Handle<JSFunction> empty_function = CreateEmptyFunction(isolate);
Steve Block44f0eee2011-05-26 01:26:41 +01002133 CreateStrictModeFunctionMaps(empty_function);
Andrei Popescu31002712010-02-23 13:46:05 +00002134 Handle<GlobalObject> inner_global;
2135 Handle<JSGlobalProxy> global_proxy =
2136 CreateNewGlobals(global_template, global_object, &inner_global);
2137 HookUpGlobalProxy(inner_global, global_proxy);
2138 InitializeGlobal(inner_global, empty_function);
Steve Block6ded16b2010-05-10 14:33:55 +01002139 InstallJSFunctionResultCaches();
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002140 InitializeNormalizedMapCaches();
Kristian Monsen25f61362010-05-21 11:50:48 +01002141 if (!InstallNatives()) return;
Steve Blocka7e24c12009-10-30 11:49:00 +00002142
Andrei Popescu31002712010-02-23 13:46:05 +00002143 MakeFunctionInstancePrototypeWritable();
Steve Blocka7e24c12009-10-30 11:49:00 +00002144
Andrei Popescu31002712010-02-23 13:46:05 +00002145 if (!ConfigureGlobalObjects(global_template)) return;
Steve Block44f0eee2011-05-26 01:26:41 +01002146 isolate->counters()->contexts_created_from_scratch()->Increment();
Andrei Popescu31002712010-02-23 13:46:05 +00002147 }
Steve Blocka7e24c12009-10-30 11:49:00 +00002148
Ben Murdoch257744e2011-11-30 15:57:28 +00002149 // Install experimental natives.
2150 if (!InstallExperimentalNatives()) return;
2151
Steve Blocka7e24c12009-10-30 11:49:00 +00002152 result_ = global_context_;
2153}
2154
2155
2156// Support for thread preemption.
2157
2158// Reserve space for statics needing saving and restoring.
2159int Bootstrapper::ArchiveSpacePerThread() {
Steve Block44f0eee2011-05-26 01:26:41 +01002160 return sizeof(NestingCounterType);
Steve Blocka7e24c12009-10-30 11:49:00 +00002161}
2162
2163
2164// Archive statics that are thread local.
2165char* Bootstrapper::ArchiveState(char* to) {
Steve Block44f0eee2011-05-26 01:26:41 +01002166 *reinterpret_cast<NestingCounterType*>(to) = nesting_;
2167 nesting_ = 0;
2168 return to + sizeof(NestingCounterType);
Steve Blocka7e24c12009-10-30 11:49:00 +00002169}
2170
2171
2172// Restore statics that are thread local.
2173char* Bootstrapper::RestoreState(char* from) {
Steve Block44f0eee2011-05-26 01:26:41 +01002174 nesting_ = *reinterpret_cast<NestingCounterType*>(from);
2175 return from + sizeof(NestingCounterType);
Steve Blocka7e24c12009-10-30 11:49:00 +00002176}
2177
2178
2179// Called when the top-level V8 mutex is destroyed.
2180void Bootstrapper::FreeThreadResources() {
Steve Block44f0eee2011-05-26 01:26:41 +01002181 ASSERT(!IsActive());
Steve Blocka7e24c12009-10-30 11:49:00 +00002182}
2183
2184} } // namespace v8::internal