blob: 5375cde7437e923d6db23c7cfd9eee8988f3d096 [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,
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000050 const char* source,
51 size_t length)
52 : data_(source), length_(length) {
Steve Block44f0eee2011-05-26 01:26:41 +010053 if (bootstrapper->delete_these_non_arrays_on_tear_down_ == NULL) {
54 bootstrapper->delete_these_non_arrays_on_tear_down_ = new List<char*>(2);
Steve Blockd0582a62009-12-15 09:54:21 +000055 }
56 // The resources are small objects and we only make a fixed number of
57 // them, but let's clean them up on exit for neatness.
Steve Block44f0eee2011-05-26 01:26:41 +010058 bootstrapper->delete_these_non_arrays_on_tear_down_->
Steve Blockd0582a62009-12-15 09:54:21 +000059 Add(reinterpret_cast<char*>(this));
60}
Steve Blocka7e24c12009-10-30 11:49:00 +000061
62
Steve Block44f0eee2011-05-26 01:26:41 +010063Bootstrapper::Bootstrapper()
64 : nesting_(0),
65 extensions_cache_(Script::TYPE_EXTENSION),
66 delete_these_non_arrays_on_tear_down_(NULL),
67 delete_these_arrays_on_tear_down_(NULL) {
68}
69
70
Steve Blocka7e24c12009-10-30 11:49:00 +000071Handle<String> Bootstrapper::NativesSourceLookup(int index) {
72 ASSERT(0 <= index && index < Natives::GetBuiltinsCount());
Steve Block44f0eee2011-05-26 01:26:41 +010073 Isolate* isolate = Isolate::Current();
74 Factory* factory = isolate->factory();
75 Heap* heap = isolate->heap();
76 if (heap->natives_source_cache()->get(index)->IsUndefined()) {
Steve Blockd0582a62009-12-15 09:54:21 +000077 if (!Snapshot::IsEnabled() || FLAG_new_snapshot) {
78 // We can use external strings for the natives.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000079 Vector<const char> source = Natives::GetRawScriptSource(index);
Steve Blockd0582a62009-12-15 09:54:21 +000080 NativesExternalStringResource* resource =
Steve Block44f0eee2011-05-26 01:26:41 +010081 new NativesExternalStringResource(this,
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000082 source.start(),
83 source.length());
Steve Blockd0582a62009-12-15 09:54:21 +000084 Handle<String> source_code =
Steve Block44f0eee2011-05-26 01:26:41 +010085 factory->NewExternalStringFromAscii(resource);
86 heap->natives_source_cache()->set(index, *source_code);
Steve Blockd0582a62009-12-15 09:54:21 +000087 } else {
88 // Old snapshot code can't cope with external strings at all.
89 Handle<String> source_code =
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000090 factory->NewStringFromAscii(Natives::GetRawScriptSource(index));
Steve Block44f0eee2011-05-26 01:26:41 +010091 heap->natives_source_cache()->set(index, *source_code);
Steve Blockd0582a62009-12-15 09:54:21 +000092 }
Steve Blocka7e24c12009-10-30 11:49:00 +000093 }
Steve Block44f0eee2011-05-26 01:26:41 +010094 Handle<Object> cached_source(heap->natives_source_cache()->get(index));
Steve Blocka7e24c12009-10-30 11:49:00 +000095 return Handle<String>::cast(cached_source);
96}
97
98
Steve Blocka7e24c12009-10-30 11:49:00 +000099void Bootstrapper::Initialize(bool create_heap_objects) {
Steve Block44f0eee2011-05-26 01:26:41 +0100100 extensions_cache_.Initialize(create_heap_objects);
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800101 GCExtension::Register();
102 ExternalizeStringExtension::Register();
Steve Blocka7e24c12009-10-30 11:49:00 +0000103}
104
105
Leon Clarkee46be812010-01-19 14:06:41 +0000106char* Bootstrapper::AllocateAutoDeletedArray(int bytes) {
107 char* memory = new char[bytes];
108 if (memory != NULL) {
Steve Block44f0eee2011-05-26 01:26:41 +0100109 if (delete_these_arrays_on_tear_down_ == NULL) {
110 delete_these_arrays_on_tear_down_ = new List<char*>(2);
Leon Clarkee46be812010-01-19 14:06:41 +0000111 }
Steve Block44f0eee2011-05-26 01:26:41 +0100112 delete_these_arrays_on_tear_down_->Add(memory);
Leon Clarkee46be812010-01-19 14:06:41 +0000113 }
114 return memory;
115}
116
117
Steve Blocka7e24c12009-10-30 11:49:00 +0000118void Bootstrapper::TearDown() {
Steve Block44f0eee2011-05-26 01:26:41 +0100119 if (delete_these_non_arrays_on_tear_down_ != NULL) {
120 int len = delete_these_non_arrays_on_tear_down_->length();
Steve Blockd0582a62009-12-15 09:54:21 +0000121 ASSERT(len < 20); // Don't use this mechanism for unbounded allocations.
122 for (int i = 0; i < len; i++) {
Steve Block44f0eee2011-05-26 01:26:41 +0100123 delete delete_these_non_arrays_on_tear_down_->at(i);
124 delete_these_non_arrays_on_tear_down_->at(i) = NULL;
Steve Blockd0582a62009-12-15 09:54:21 +0000125 }
Steve Block44f0eee2011-05-26 01:26:41 +0100126 delete delete_these_non_arrays_on_tear_down_;
127 delete_these_non_arrays_on_tear_down_ = NULL;
Steve Blockd0582a62009-12-15 09:54:21 +0000128 }
129
Steve Block44f0eee2011-05-26 01:26:41 +0100130 if (delete_these_arrays_on_tear_down_ != NULL) {
131 int len = delete_these_arrays_on_tear_down_->length();
Leon Clarkee46be812010-01-19 14:06:41 +0000132 ASSERT(len < 1000); // Don't use this mechanism for unbounded allocations.
133 for (int i = 0; i < len; i++) {
Steve Block44f0eee2011-05-26 01:26:41 +0100134 delete[] delete_these_arrays_on_tear_down_->at(i);
135 delete_these_arrays_on_tear_down_->at(i) = NULL;
Leon Clarkee46be812010-01-19 14:06:41 +0000136 }
Steve Block44f0eee2011-05-26 01:26:41 +0100137 delete delete_these_arrays_on_tear_down_;
138 delete_these_arrays_on_tear_down_ = NULL;
Leon Clarkee46be812010-01-19 14:06:41 +0000139 }
140
Steve Block44f0eee2011-05-26 01:26:41 +0100141 extensions_cache_.Initialize(false); // Yes, symmetrical
Steve Blocka7e24c12009-10-30 11:49:00 +0000142}
143
144
Steve Blocka7e24c12009-10-30 11:49:00 +0000145class Genesis BASE_EMBEDDED {
146 public:
Ben Murdoch257744e2011-11-30 15:57:28 +0000147 Genesis(Isolate* isolate,
148 Handle<Object> global_object,
Steve Blocka7e24c12009-10-30 11:49:00 +0000149 v8::Handle<v8::ObjectTemplate> global_template,
150 v8::ExtensionConfiguration* extensions);
Andrei Popescu31002712010-02-23 13:46:05 +0000151 ~Genesis() { }
Steve Blocka7e24c12009-10-30 11:49:00 +0000152
153 Handle<Context> result() { return result_; }
154
155 Genesis* previous() { return previous_; }
Steve Blocka7e24c12009-10-30 11:49:00 +0000156
Ben Murdoch257744e2011-11-30 15:57:28 +0000157 Isolate* isolate() const { return isolate_; }
158 Factory* factory() const { return isolate_->factory(); }
159 Heap* heap() const { return isolate_->heap(); }
160
Steve Blocka7e24c12009-10-30 11:49:00 +0000161 private:
162 Handle<Context> global_context_;
Ben Murdoch257744e2011-11-30 15:57:28 +0000163 Isolate* isolate_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000164
165 // There may be more than one active genesis object: When GC is
166 // triggered during environment creation there may be weak handle
167 // processing callbacks which may create new environments.
168 Genesis* previous_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000169
170 Handle<Context> global_context() { return global_context_; }
171
Andrei Popescu31002712010-02-23 13:46:05 +0000172 // Creates some basic objects. Used for creating a context from scratch.
173 void CreateRoots();
174 // Creates the empty function. Used for creating a context from scratch.
Ben Murdoch257744e2011-11-30 15:57:28 +0000175 Handle<JSFunction> CreateEmptyFunction(Isolate* isolate);
Steve Block44f0eee2011-05-26 01:26:41 +0100176 // Creates the ThrowTypeError function. ECMA 5th Ed. 13.2.3
Ben Murdoch257744e2011-11-30 15:57:28 +0000177 Handle<JSFunction> GetThrowTypeErrorFunction();
Steve Block44f0eee2011-05-26 01:26:41 +0100178
179 void CreateStrictModeFunctionMaps(Handle<JSFunction> empty);
Andrei Popescu31002712010-02-23 13:46:05 +0000180 // Creates the global objects using the global and the template passed in
181 // through the API. We call this regardless of whether we are building a
182 // context from scratch or using a deserialized one from the partial snapshot
183 // but in the latter case we don't use the objects it produces directly, as
184 // we have to used the deserialized ones that are linked together with the
185 // rest of the context snapshot.
186 Handle<JSGlobalProxy> CreateNewGlobals(
187 v8::Handle<v8::ObjectTemplate> global_template,
188 Handle<Object> global_object,
189 Handle<GlobalObject>* global_proxy_out);
190 // Hooks the given global proxy into the context. If the context was created
191 // by deserialization then this will unhook the global proxy that was
192 // deserialized, leaving the GC to pick it up.
193 void HookUpGlobalProxy(Handle<GlobalObject> inner_global,
194 Handle<JSGlobalProxy> global_proxy);
Andrei Popescu402d9372010-02-26 13:31:12 +0000195 // Similarly, we want to use the inner global that has been created by the
196 // templates passed through the API. The inner global from the snapshot is
197 // detached from the other objects in the snapshot.
198 void HookUpInnerGlobal(Handle<GlobalObject> inner_global);
Andrei Popescu31002712010-02-23 13:46:05 +0000199 // New context initialization. Used for creating a context from scratch.
200 void InitializeGlobal(Handle<GlobalObject> inner_global,
201 Handle<JSFunction> empty_function);
202 // Installs the contents of the native .js files on the global objects.
203 // Used for creating a context from scratch.
Steve Blocka7e24c12009-10-30 11:49:00 +0000204 void InstallNativeFunctions();
Ben Murdoch257744e2011-11-30 15:57:28 +0000205 void InstallExperimentalNativeFunctions();
Steve Blocka7e24c12009-10-30 11:49:00 +0000206 bool InstallNatives();
Ben Murdoch257744e2011-11-30 15:57:28 +0000207 bool InstallExperimentalNatives();
Ben Murdochb0fe1622011-05-05 13:52:32 +0100208 void InstallBuiltinFunctionIds();
Steve Block6ded16b2010-05-10 14:33:55 +0100209 void InstallJSFunctionResultCaches();
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100210 void InitializeNormalizedMapCaches();
Andrei Popescu31002712010-02-23 13:46:05 +0000211 // Used both for deserialized and from-scratch contexts to add the extensions
212 // provided.
213 static bool InstallExtensions(Handle<Context> global_context,
214 v8::ExtensionConfiguration* extensions);
215 static bool InstallExtension(const char* name);
216 static bool InstallExtension(v8::RegisteredExtension* current);
217 static void InstallSpecialObjects(Handle<Context> global_context);
Andrei Popescu402d9372010-02-26 13:31:12 +0000218 bool InstallJSBuiltins(Handle<JSBuiltinsObject> builtins);
Steve Blocka7e24c12009-10-30 11:49:00 +0000219 bool ConfigureApiObject(Handle<JSObject> object,
220 Handle<ObjectTemplateInfo> object_template);
221 bool ConfigureGlobalObjects(v8::Handle<v8::ObjectTemplate> global_template);
222
223 // Migrates all properties from the 'from' object to the 'to'
224 // object and overrides the prototype in 'to' with the one from
225 // 'from'.
226 void TransferObject(Handle<JSObject> from, Handle<JSObject> to);
227 void TransferNamedProperties(Handle<JSObject> from, Handle<JSObject> to);
228 void TransferIndexedProperties(Handle<JSObject> from, Handle<JSObject> to);
229
Steve Block6ded16b2010-05-10 14:33:55 +0100230 enum PrototypePropertyMode {
231 DONT_ADD_PROTOTYPE,
232 ADD_READONLY_PROTOTYPE,
233 ADD_WRITEABLE_PROTOTYPE
234 };
Steve Block44f0eee2011-05-26 01:26:41 +0100235
236 Handle<Map> CreateFunctionMap(PrototypePropertyMode prototype_mode);
237
Steve Blocka7e24c12009-10-30 11:49:00 +0000238 Handle<DescriptorArray> ComputeFunctionInstanceDescriptor(
Steve Block6ded16b2010-05-10 14:33:55 +0100239 PrototypePropertyMode prototypeMode);
Steve Blocka7e24c12009-10-30 11:49:00 +0000240 void MakeFunctionInstancePrototypeWritable();
241
Steve Block44f0eee2011-05-26 01:26:41 +0100242 Handle<Map> CreateStrictModeFunctionMap(
243 PrototypePropertyMode prototype_mode,
244 Handle<JSFunction> empty_function,
245 Handle<FixedArray> arguments_callbacks,
246 Handle<FixedArray> caller_callbacks);
247
248 Handle<DescriptorArray> ComputeStrictFunctionInstanceDescriptor(
249 PrototypePropertyMode propertyMode,
250 Handle<FixedArray> arguments,
251 Handle<FixedArray> caller);
252
Ben Murdoch257744e2011-11-30 15:57:28 +0000253 static bool CompileBuiltin(Isolate* isolate, int index);
254 static bool CompileExperimentalBuiltin(Isolate* isolate, int index);
Steve Blocka7e24c12009-10-30 11:49:00 +0000255 static bool CompileNative(Vector<const char> name, Handle<String> source);
256 static bool CompileScriptCached(Vector<const char> name,
257 Handle<String> source,
258 SourceCodeCache* cache,
259 v8::Extension* extension,
Andrei Popescu31002712010-02-23 13:46:05 +0000260 Handle<Context> top_context,
Steve Blocka7e24c12009-10-30 11:49:00 +0000261 bool use_runtime_context);
262
263 Handle<Context> result_;
Steve Block44f0eee2011-05-26 01:26:41 +0100264
265 // Function instance maps. Function literal maps are created initially with
266 // a read only prototype for the processing of JS builtins. Later the function
267 // instance maps are replaced in order to make prototype writable.
268 // These are the final, writable prototype, maps.
269 Handle<Map> function_instance_map_writable_prototype_;
270 Handle<Map> strict_mode_function_instance_map_writable_prototype_;
Ben Murdoch257744e2011-11-30 15:57:28 +0000271 Handle<JSFunction> throw_type_error_function;
Steve Block44f0eee2011-05-26 01:26:41 +0100272
Andrei Popescu31002712010-02-23 13:46:05 +0000273 BootstrapperActive active_;
274 friend class Bootstrapper;
Steve Blocka7e24c12009-10-30 11:49:00 +0000275};
276
Steve Blocka7e24c12009-10-30 11:49:00 +0000277
278void Bootstrapper::Iterate(ObjectVisitor* v) {
Steve Block44f0eee2011-05-26 01:26:41 +0100279 extensions_cache_.Iterate(v);
Steve Blockd0582a62009-12-15 09:54:21 +0000280 v->Synchronize("Extensions");
Steve Blocka7e24c12009-10-30 11:49:00 +0000281}
282
283
Steve Blocka7e24c12009-10-30 11:49:00 +0000284Handle<Context> Bootstrapper::CreateEnvironment(
Ben Murdoch257744e2011-11-30 15:57:28 +0000285 Isolate* isolate,
Steve Blocka7e24c12009-10-30 11:49:00 +0000286 Handle<Object> global_object,
287 v8::Handle<v8::ObjectTemplate> global_template,
288 v8::ExtensionConfiguration* extensions) {
Andrei Popescu31002712010-02-23 13:46:05 +0000289 HandleScope scope;
290 Handle<Context> env;
Ben Murdoch257744e2011-11-30 15:57:28 +0000291 Genesis genesis(isolate, global_object, global_template, extensions);
Andrei Popescu31002712010-02-23 13:46:05 +0000292 env = genesis.result();
293 if (!env.is_null()) {
294 if (InstallExtensions(env, extensions)) {
295 return env;
296 }
297 }
298 return Handle<Context>();
Steve Blocka7e24c12009-10-30 11:49:00 +0000299}
300
301
302static void SetObjectPrototype(Handle<JSObject> object, Handle<Object> proto) {
303 // object.__proto__ = proto;
Ben Murdoch257744e2011-11-30 15:57:28 +0000304 Factory* factory = object->GetIsolate()->factory();
Steve Blocka7e24c12009-10-30 11:49:00 +0000305 Handle<Map> old_to_map = Handle<Map>(object->map());
Ben Murdoch257744e2011-11-30 15:57:28 +0000306 Handle<Map> new_to_map = factory->CopyMapDropTransitions(old_to_map);
Steve Blocka7e24c12009-10-30 11:49:00 +0000307 new_to_map->set_prototype(*proto);
308 object->set_map(*new_to_map);
309}
310
311
312void Bootstrapper::DetachGlobal(Handle<Context> env) {
Ben Murdoch257744e2011-11-30 15:57:28 +0000313 Factory* factory = env->GetIsolate()->factory();
Steve Block44f0eee2011-05-26 01:26:41 +0100314 JSGlobalProxy::cast(env->global_proxy())->set_context(*factory->null_value());
Steve Blocka7e24c12009-10-30 11:49:00 +0000315 SetObjectPrototype(Handle<JSObject>(env->global_proxy()),
Steve Block44f0eee2011-05-26 01:26:41 +0100316 factory->null_value());
Steve Blocka7e24c12009-10-30 11:49:00 +0000317 env->set_global_proxy(env->global());
318 env->global()->set_global_receiver(env->global());
319}
320
321
Andrei Popescu74b3c142010-03-29 12:03:09 +0100322void Bootstrapper::ReattachGlobal(Handle<Context> env,
323 Handle<Object> global_object) {
324 ASSERT(global_object->IsJSGlobalProxy());
325 Handle<JSGlobalProxy> global = Handle<JSGlobalProxy>::cast(global_object);
326 env->global()->set_global_receiver(*global);
327 env->set_global_proxy(*global);
328 SetObjectPrototype(global, Handle<JSObject>(env->global()));
329 global->set_context(*env);
330}
331
332
Steve Blocka7e24c12009-10-30 11:49:00 +0000333static Handle<JSFunction> InstallFunction(Handle<JSObject> target,
334 const char* name,
335 InstanceType type,
336 int instance_size,
337 Handle<JSObject> prototype,
338 Builtins::Name call,
339 bool is_ecma_native) {
Ben Murdoch257744e2011-11-30 15:57:28 +0000340 Isolate* isolate = target->GetIsolate();
Steve Block44f0eee2011-05-26 01:26:41 +0100341 Factory* factory = isolate->factory();
342 Handle<String> symbol = factory->LookupAsciiSymbol(name);
343 Handle<Code> call_code = Handle<Code>(isolate->builtins()->builtin(call));
Steve Block6ded16b2010-05-10 14:33:55 +0100344 Handle<JSFunction> function = prototype.is_null() ?
Steve Block44f0eee2011-05-26 01:26:41 +0100345 factory->NewFunctionWithoutPrototype(symbol, call_code) :
346 factory->NewFunctionWithPrototype(symbol,
Steve Blocka7e24c12009-10-30 11:49:00 +0000347 type,
348 instance_size,
349 prototype,
350 call_code,
351 is_ecma_native);
Steve Block1e0659c2011-05-24 12:43:12 +0100352 SetLocalPropertyNoThrow(target, symbol, function, DONT_ENUM);
Steve Blocka7e24c12009-10-30 11:49:00 +0000353 if (is_ecma_native) {
354 function->shared()->set_instance_class_name(*symbol);
355 }
356 return function;
357}
358
359
360Handle<DescriptorArray> Genesis::ComputeFunctionInstanceDescriptor(
Steve Block6ded16b2010-05-10 14:33:55 +0100361 PrototypePropertyMode prototypeMode) {
Steve Block44f0eee2011-05-26 01:26:41 +0100362 Handle<DescriptorArray> descriptors =
Ben Murdoch257744e2011-11-30 15:57:28 +0000363 factory()->NewDescriptorArray(prototypeMode == DONT_ADD_PROTOTYPE
364 ? 4
365 : 5);
Steve Block6ded16b2010-05-10 14:33:55 +0100366 PropertyAttributes attributes =
Steve Blocka7e24c12009-10-30 11:49:00 +0000367 static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE | READ_ONLY);
Steve Blocka7e24c12009-10-30 11:49:00 +0000368
Steve Block44f0eee2011-05-26 01:26:41 +0100369 { // Add length.
Ben Murdoch257744e2011-11-30 15:57:28 +0000370 Handle<Foreign> foreign = factory()->NewForeign(&Accessors::FunctionLength);
371 CallbacksDescriptor d(*factory()->length_symbol(), *foreign, attributes);
Steve Block44f0eee2011-05-26 01:26:41 +0100372 descriptors->Set(0, &d);
373 }
374 { // Add name.
Ben Murdoch257744e2011-11-30 15:57:28 +0000375 Handle<Foreign> foreign = factory()->NewForeign(&Accessors::FunctionName);
376 CallbacksDescriptor d(*factory()->name_symbol(), *foreign, attributes);
Steve Block44f0eee2011-05-26 01:26:41 +0100377 descriptors->Set(1, &d);
378 }
379 { // Add arguments.
Ben Murdoch257744e2011-11-30 15:57:28 +0000380 Handle<Foreign> foreign =
381 factory()->NewForeign(&Accessors::FunctionArguments);
382 CallbacksDescriptor d(*factory()->arguments_symbol(), *foreign, attributes);
Steve Block44f0eee2011-05-26 01:26:41 +0100383 descriptors->Set(2, &d);
384 }
385 { // Add caller.
Ben Murdoch257744e2011-11-30 15:57:28 +0000386 Handle<Foreign> foreign = factory()->NewForeign(&Accessors::FunctionCaller);
387 CallbacksDescriptor d(*factory()->caller_symbol(), *foreign, attributes);
Steve Block44f0eee2011-05-26 01:26:41 +0100388 descriptors->Set(3, &d);
389 }
390 if (prototypeMode != DONT_ADD_PROTOTYPE) {
391 // Add prototype.
392 if (prototypeMode == ADD_WRITEABLE_PROTOTYPE) {
393 attributes = static_cast<PropertyAttributes>(attributes & ~READ_ONLY);
394 }
Ben Murdoch257744e2011-11-30 15:57:28 +0000395 Handle<Foreign> foreign =
396 factory()->NewForeign(&Accessors::FunctionPrototype);
397 CallbacksDescriptor d(*factory()->prototype_symbol(), *foreign, attributes);
Steve Block44f0eee2011-05-26 01:26:41 +0100398 descriptors->Set(4, &d);
399 }
400 descriptors->Sort();
401 return descriptors;
402}
Steve Blocka7e24c12009-10-30 11:49:00 +0000403
Steve Blocka7e24c12009-10-30 11:49:00 +0000404
Steve Block44f0eee2011-05-26 01:26:41 +0100405Handle<Map> Genesis::CreateFunctionMap(PrototypePropertyMode prototype_mode) {
Ben Murdoch257744e2011-11-30 15:57:28 +0000406 Handle<Map> map = factory()->NewMap(JS_FUNCTION_TYPE, JSFunction::kSize);
Steve Block44f0eee2011-05-26 01:26:41 +0100407 Handle<DescriptorArray> descriptors =
408 ComputeFunctionInstanceDescriptor(prototype_mode);
409 map->set_instance_descriptors(*descriptors);
410 map->set_function_with_prototype(prototype_mode != DONT_ADD_PROTOTYPE);
411 return map;
Steve Blocka7e24c12009-10-30 11:49:00 +0000412}
413
414
Ben Murdoch257744e2011-11-30 15:57:28 +0000415Handle<JSFunction> Genesis::CreateEmptyFunction(Isolate* isolate) {
Steve Block44f0eee2011-05-26 01:26:41 +0100416 // Allocate the map for function instances. Maps are allocated first and their
417 // prototypes patched later, once empty function is created.
418
Steve Blocka7e24c12009-10-30 11:49:00 +0000419 // Please note that the prototype property for function instances must be
420 // writable.
Ben Murdoch8b112d22011-06-08 16:22:53 +0100421 Handle<Map> function_instance_map =
422 CreateFunctionMap(ADD_WRITEABLE_PROTOTYPE);
423 global_context()->set_function_instance_map(*function_instance_map);
Steve Block6ded16b2010-05-10 14:33:55 +0100424
425 // Functions with this map will not have a 'prototype' property, and
426 // can not be used as constructors.
Ben Murdoch8b112d22011-06-08 16:22:53 +0100427 Handle<Map> function_without_prototype_map =
428 CreateFunctionMap(DONT_ADD_PROTOTYPE);
Steve Block6ded16b2010-05-10 14:33:55 +0100429 global_context()->set_function_without_prototype_map(
Ben Murdoch8b112d22011-06-08 16:22:53 +0100430 *function_without_prototype_map);
Steve Blocka7e24c12009-10-30 11:49:00 +0000431
Steve Block44f0eee2011-05-26 01:26:41 +0100432 // Allocate the function map. This map is temporary, used only for processing
433 // of builtins.
434 // Later the map is replaced with writable prototype map, allocated below.
Ben Murdoch8b112d22011-06-08 16:22:53 +0100435 Handle<Map> function_map = CreateFunctionMap(ADD_READONLY_PROTOTYPE);
436 global_context()->set_function_map(*function_map);
Steve Blocka7e24c12009-10-30 11:49:00 +0000437
Steve Block44f0eee2011-05-26 01:26:41 +0100438 // The final map for functions. Writeable prototype.
439 // This map is installed in MakeFunctionInstancePrototypeWritable.
440 function_instance_map_writable_prototype_ =
441 CreateFunctionMap(ADD_WRITEABLE_PROTOTYPE);
442
Steve Block44f0eee2011-05-26 01:26:41 +0100443 Factory* factory = isolate->factory();
444 Heap* heap = isolate->heap();
445
446 Handle<String> object_name = Handle<String>(heap->Object_symbol());
Steve Blocka7e24c12009-10-30 11:49:00 +0000447
448 { // --- O b j e c t ---
449 Handle<JSFunction> object_fun =
Steve Block44f0eee2011-05-26 01:26:41 +0100450 factory->NewFunction(object_name, factory->null_value());
Steve Blocka7e24c12009-10-30 11:49:00 +0000451 Handle<Map> object_function_map =
Steve Block44f0eee2011-05-26 01:26:41 +0100452 factory->NewMap(JS_OBJECT_TYPE, JSObject::kHeaderSize);
Steve Blocka7e24c12009-10-30 11:49:00 +0000453 object_fun->set_initial_map(*object_function_map);
454 object_function_map->set_constructor(*object_fun);
455
456 global_context()->set_object_function(*object_fun);
457
458 // Allocate a new prototype for the object function.
Steve Block44f0eee2011-05-26 01:26:41 +0100459 Handle<JSObject> prototype = factory->NewJSObject(
460 isolate->object_function(),
461 TENURED);
Steve Blocka7e24c12009-10-30 11:49:00 +0000462
463 global_context()->set_initial_object_prototype(*prototype);
464 SetPrototype(object_fun, prototype);
465 object_function_map->
Steve Block44f0eee2011-05-26 01:26:41 +0100466 set_instance_descriptors(heap->empty_descriptor_array());
Steve Blocka7e24c12009-10-30 11:49:00 +0000467 }
468
469 // Allocate the empty function as the prototype for function ECMAScript
470 // 262 15.3.4.
Steve Block44f0eee2011-05-26 01:26:41 +0100471 Handle<String> symbol = factory->LookupAsciiSymbol("Empty");
Steve Blocka7e24c12009-10-30 11:49:00 +0000472 Handle<JSFunction> empty_function =
Steve Block44f0eee2011-05-26 01:26:41 +0100473 factory->NewFunctionWithoutPrototype(symbol, kNonStrictMode);
Steve Blocka7e24c12009-10-30 11:49:00 +0000474
Andrei Popescu31002712010-02-23 13:46:05 +0000475 // --- E m p t y ---
476 Handle<Code> code =
Steve Block44f0eee2011-05-26 01:26:41 +0100477 Handle<Code>(isolate->builtins()->builtin(
478 Builtins::kEmptyFunction));
Andrei Popescu31002712010-02-23 13:46:05 +0000479 empty_function->set_code(*code);
Iain Merrick75681382010-08-19 15:07:18 +0100480 empty_function->shared()->set_code(*code);
Steve Block44f0eee2011-05-26 01:26:41 +0100481 Handle<String> source = factory->NewStringFromAscii(CStrVector("() {}"));
482 Handle<Script> script = factory->NewScript(source);
Andrei Popescu31002712010-02-23 13:46:05 +0000483 script->set_type(Smi::FromInt(Script::TYPE_NATIVE));
484 empty_function->shared()->set_script(*script);
485 empty_function->shared()->set_start_position(0);
486 empty_function->shared()->set_end_position(source->length());
487 empty_function->shared()->DontAdaptArguments();
Steve Block44f0eee2011-05-26 01:26:41 +0100488
489 // Set prototypes for the function maps.
Andrei Popescu31002712010-02-23 13:46:05 +0000490 global_context()->function_map()->set_prototype(*empty_function);
491 global_context()->function_instance_map()->set_prototype(*empty_function);
Steve Block6ded16b2010-05-10 14:33:55 +0100492 global_context()->function_without_prototype_map()->
493 set_prototype(*empty_function);
Steve Block44f0eee2011-05-26 01:26:41 +0100494 function_instance_map_writable_prototype_->set_prototype(*empty_function);
Steve Blocka7e24c12009-10-30 11:49:00 +0000495
Andrei Popescu31002712010-02-23 13:46:05 +0000496 // Allocate the function map first and then patch the prototype later
Steve Block44f0eee2011-05-26 01:26:41 +0100497 Handle<Map> empty_fm = factory->CopyMapDropDescriptors(
Steve Block6ded16b2010-05-10 14:33:55 +0100498 function_without_prototype_map);
499 empty_fm->set_instance_descriptors(
Steve Block44f0eee2011-05-26 01:26:41 +0100500 function_without_prototype_map->instance_descriptors());
Andrei Popescu31002712010-02-23 13:46:05 +0000501 empty_fm->set_prototype(global_context()->object_function()->prototype());
502 empty_function->set_map(*empty_fm);
503 return empty_function;
504}
505
506
Steve Block44f0eee2011-05-26 01:26:41 +0100507Handle<DescriptorArray> Genesis::ComputeStrictFunctionInstanceDescriptor(
508 PrototypePropertyMode prototypeMode,
509 Handle<FixedArray> arguments,
510 Handle<FixedArray> caller) {
Steve Block44f0eee2011-05-26 01:26:41 +0100511 Handle<DescriptorArray> descriptors =
Ben Murdoch257744e2011-11-30 15:57:28 +0000512 factory()->NewDescriptorArray(prototypeMode == DONT_ADD_PROTOTYPE
513 ? 4
514 : 5);
Steve Block44f0eee2011-05-26 01:26:41 +0100515 PropertyAttributes attributes = static_cast<PropertyAttributes>(
516 DONT_ENUM | DONT_DELETE | READ_ONLY);
517
518 { // length
Ben Murdoch257744e2011-11-30 15:57:28 +0000519 Handle<Foreign> foreign = factory()->NewForeign(&Accessors::FunctionLength);
520 CallbacksDescriptor d(*factory()->length_symbol(), *foreign, attributes);
Steve Block44f0eee2011-05-26 01:26:41 +0100521 descriptors->Set(0, &d);
522 }
523 { // name
Ben Murdoch257744e2011-11-30 15:57:28 +0000524 Handle<Foreign> foreign = factory()->NewForeign(&Accessors::FunctionName);
525 CallbacksDescriptor d(*factory()->name_symbol(), *foreign, attributes);
Steve Block44f0eee2011-05-26 01:26:41 +0100526 descriptors->Set(1, &d);
527 }
528 { // arguments
Ben Murdoch257744e2011-11-30 15:57:28 +0000529 CallbacksDescriptor d(*factory()->arguments_symbol(),
530 *arguments,
531 attributes);
Steve Block44f0eee2011-05-26 01:26:41 +0100532 descriptors->Set(2, &d);
533 }
534 { // caller
Ben Murdoch257744e2011-11-30 15:57:28 +0000535 CallbacksDescriptor d(*factory()->caller_symbol(), *caller, attributes);
Steve Block44f0eee2011-05-26 01:26:41 +0100536 descriptors->Set(3, &d);
537 }
538
539 // prototype
540 if (prototypeMode != DONT_ADD_PROTOTYPE) {
541 if (prototypeMode == ADD_WRITEABLE_PROTOTYPE) {
542 attributes = static_cast<PropertyAttributes>(attributes & ~READ_ONLY);
543 }
Ben Murdoch257744e2011-11-30 15:57:28 +0000544 Handle<Foreign> foreign =
545 factory()->NewForeign(&Accessors::FunctionPrototype);
546 CallbacksDescriptor d(*factory()->prototype_symbol(), *foreign, attributes);
Steve Block44f0eee2011-05-26 01:26:41 +0100547 descriptors->Set(4, &d);
548 }
549
550 descriptors->Sort();
551 return descriptors;
552}
553
554
555// ECMAScript 5th Edition, 13.2.3
Ben Murdoch257744e2011-11-30 15:57:28 +0000556Handle<JSFunction> Genesis::GetThrowTypeErrorFunction() {
557 if (throw_type_error_function.is_null()) {
558 Handle<String> name = factory()->LookupAsciiSymbol("ThrowTypeError");
559 throw_type_error_function =
560 factory()->NewFunctionWithoutPrototype(name, kNonStrictMode);
561 Handle<Code> code(isolate()->builtins()->builtin(
562 Builtins::kStrictModePoisonPill));
563 throw_type_error_function->set_map(
564 global_context()->function_map());
565 throw_type_error_function->set_code(*code);
566 throw_type_error_function->shared()->set_code(*code);
567 throw_type_error_function->shared()->DontAdaptArguments();
Steve Block053d10c2011-06-13 19:13:29 +0100568
Ben Murdoch257744e2011-11-30 15:57:28 +0000569 PreventExtensions(throw_type_error_function);
570 }
571 return throw_type_error_function;
Steve Block44f0eee2011-05-26 01:26:41 +0100572}
573
574
575Handle<Map> Genesis::CreateStrictModeFunctionMap(
576 PrototypePropertyMode prototype_mode,
577 Handle<JSFunction> empty_function,
578 Handle<FixedArray> arguments_callbacks,
579 Handle<FixedArray> caller_callbacks) {
Ben Murdoch257744e2011-11-30 15:57:28 +0000580 Handle<Map> map = factory()->NewMap(JS_FUNCTION_TYPE, JSFunction::kSize);
Steve Block44f0eee2011-05-26 01:26:41 +0100581 Handle<DescriptorArray> descriptors =
582 ComputeStrictFunctionInstanceDescriptor(prototype_mode,
583 arguments_callbacks,
584 caller_callbacks);
585 map->set_instance_descriptors(*descriptors);
586 map->set_function_with_prototype(prototype_mode != DONT_ADD_PROTOTYPE);
587 map->set_prototype(*empty_function);
588 return map;
589}
590
591
592void Genesis::CreateStrictModeFunctionMaps(Handle<JSFunction> empty) {
593 // Create the callbacks arrays for ThrowTypeError functions.
594 // The get/set callacks are filled in after the maps are created below.
Ben Murdoch257744e2011-11-30 15:57:28 +0000595 Factory* factory = empty->GetIsolate()->factory();
Steve Block44f0eee2011-05-26 01:26:41 +0100596 Handle<FixedArray> arguments = factory->NewFixedArray(2, TENURED);
597 Handle<FixedArray> caller = factory->NewFixedArray(2, TENURED);
598
599 // Allocate map for the strict mode function instances.
Ben Murdoch8b112d22011-06-08 16:22:53 +0100600 Handle<Map> strict_mode_function_instance_map =
601 CreateStrictModeFunctionMap(
602 ADD_WRITEABLE_PROTOTYPE, empty, arguments, caller);
Steve Block44f0eee2011-05-26 01:26:41 +0100603 global_context()->set_strict_mode_function_instance_map(
Ben Murdoch8b112d22011-06-08 16:22:53 +0100604 *strict_mode_function_instance_map);
Steve Block44f0eee2011-05-26 01:26:41 +0100605
606 // Allocate map for the prototype-less strict mode instances.
Ben Murdoch8b112d22011-06-08 16:22:53 +0100607 Handle<Map> strict_mode_function_without_prototype_map =
608 CreateStrictModeFunctionMap(
609 DONT_ADD_PROTOTYPE, empty, arguments, caller);
Steve Block44f0eee2011-05-26 01:26:41 +0100610 global_context()->set_strict_mode_function_without_prototype_map(
Ben Murdoch8b112d22011-06-08 16:22:53 +0100611 *strict_mode_function_without_prototype_map);
Steve Block44f0eee2011-05-26 01:26:41 +0100612
613 // Allocate map for the strict mode functions. This map is temporary, used
614 // only for processing of builtins.
615 // Later the map is replaced with writable prototype map, allocated below.
Ben Murdoch8b112d22011-06-08 16:22:53 +0100616 Handle<Map> strict_mode_function_map =
617 CreateStrictModeFunctionMap(
618 ADD_READONLY_PROTOTYPE, empty, arguments, caller);
Steve Block44f0eee2011-05-26 01:26:41 +0100619 global_context()->set_strict_mode_function_map(
Ben Murdoch8b112d22011-06-08 16:22:53 +0100620 *strict_mode_function_map);
Steve Block44f0eee2011-05-26 01:26:41 +0100621
622 // The final map for the strict mode functions. Writeable prototype.
623 // This map is installed in MakeFunctionInstancePrototypeWritable.
624 strict_mode_function_instance_map_writable_prototype_ =
625 CreateStrictModeFunctionMap(
626 ADD_WRITEABLE_PROTOTYPE, empty, arguments, caller);
627
Ben Murdoch257744e2011-11-30 15:57:28 +0000628 // Create the ThrowTypeError function instance.
629 Handle<JSFunction> throw_function =
630 GetThrowTypeErrorFunction();
Steve Block44f0eee2011-05-26 01:26:41 +0100631
632 // Complete the callback fixed arrays.
Ben Murdoch257744e2011-11-30 15:57:28 +0000633 arguments->set(0, *throw_function);
634 arguments->set(1, *throw_function);
635 caller->set(0, *throw_function);
636 caller->set(1, *throw_function);
Steve Block44f0eee2011-05-26 01:26:41 +0100637}
638
639
Ben Murdochb0fe1622011-05-05 13:52:32 +0100640static void AddToWeakGlobalContextList(Context* context) {
641 ASSERT(context->IsGlobalContext());
Ben Murdoch257744e2011-11-30 15:57:28 +0000642 Heap* heap = context->GetIsolate()->heap();
Ben Murdochb0fe1622011-05-05 13:52:32 +0100643#ifdef DEBUG
644 { // NOLINT
645 ASSERT(context->get(Context::NEXT_CONTEXT_LINK)->IsUndefined());
646 // Check that context is not in the list yet.
Steve Block44f0eee2011-05-26 01:26:41 +0100647 for (Object* current = heap->global_contexts_list();
Ben Murdochb0fe1622011-05-05 13:52:32 +0100648 !current->IsUndefined();
649 current = Context::cast(current)->get(Context::NEXT_CONTEXT_LINK)) {
650 ASSERT(current != context);
651 }
652 }
653#endif
Steve Block44f0eee2011-05-26 01:26:41 +0100654 context->set(Context::NEXT_CONTEXT_LINK, heap->global_contexts_list());
655 heap->set_global_contexts_list(context);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100656}
657
658
Andrei Popescu31002712010-02-23 13:46:05 +0000659void Genesis::CreateRoots() {
660 // Allocate the global context FixedArray first and then patch the
661 // closure and extension object later (we need the empty function
662 // and the global object, but in order to create those, we need the
663 // global context).
Ben Murdoch257744e2011-11-30 15:57:28 +0000664 global_context_ = Handle<Context>::cast(isolate()->global_handles()->Create(
665 *factory()->NewGlobalContext()));
Ben Murdochb0fe1622011-05-05 13:52:32 +0100666 AddToWeakGlobalContextList(*global_context_);
Ben Murdoch257744e2011-11-30 15:57:28 +0000667 isolate()->set_context(*global_context());
Andrei Popescu31002712010-02-23 13:46:05 +0000668
669 // Allocate the message listeners object.
670 {
671 v8::NeanderArray listeners;
672 global_context()->set_message_listeners(*listeners.value());
673 }
674}
675
676
677Handle<JSGlobalProxy> Genesis::CreateNewGlobals(
678 v8::Handle<v8::ObjectTemplate> global_template,
679 Handle<Object> global_object,
680 Handle<GlobalObject>* inner_global_out) {
681 // The argument global_template aka data is an ObjectTemplateInfo.
682 // It has a constructor pointer that points at global_constructor which is a
683 // FunctionTemplateInfo.
684 // The global_constructor is used to create or reinitialize the global_proxy.
685 // The global_constructor also has a prototype_template pointer that points at
686 // js_global_template which is an ObjectTemplateInfo.
687 // That in turn has a constructor pointer that points at
688 // js_global_constructor which is a FunctionTemplateInfo.
689 // js_global_constructor is used to make js_global_function
690 // js_global_function is used to make the new inner_global.
691 //
692 // --- G l o b a l ---
693 // Step 1: Create a fresh inner JSGlobalObject.
694 Handle<JSFunction> js_global_function;
695 Handle<ObjectTemplateInfo> js_global_template;
696 if (!global_template.IsEmpty()) {
697 // Get prototype template of the global_template.
698 Handle<ObjectTemplateInfo> data =
699 v8::Utils::OpenHandle(*global_template);
700 Handle<FunctionTemplateInfo> global_constructor =
701 Handle<FunctionTemplateInfo>(
702 FunctionTemplateInfo::cast(data->constructor()));
703 Handle<Object> proto_template(global_constructor->prototype_template());
704 if (!proto_template->IsUndefined()) {
705 js_global_template =
706 Handle<ObjectTemplateInfo>::cast(proto_template);
707 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000708 }
709
Andrei Popescu31002712010-02-23 13:46:05 +0000710 if (js_global_template.is_null()) {
Ben Murdoch257744e2011-11-30 15:57:28 +0000711 Handle<String> name = Handle<String>(heap()->empty_symbol());
712 Handle<Code> code = Handle<Code>(isolate()->builtins()->builtin(
Steve Block44f0eee2011-05-26 01:26:41 +0100713 Builtins::kIllegal));
Andrei Popescu31002712010-02-23 13:46:05 +0000714 js_global_function =
Ben Murdoch257744e2011-11-30 15:57:28 +0000715 factory()->NewFunction(name, JS_GLOBAL_OBJECT_TYPE,
716 JSGlobalObject::kSize, code, true);
Andrei Popescu31002712010-02-23 13:46:05 +0000717 // Change the constructor property of the prototype of the
718 // hidden global function to refer to the Object function.
719 Handle<JSObject> prototype =
720 Handle<JSObject>(
721 JSObject::cast(js_global_function->instance_prototype()));
Steve Block1e0659c2011-05-24 12:43:12 +0100722 SetLocalPropertyNoThrow(
Steve Block44f0eee2011-05-26 01:26:41 +0100723 prototype,
Ben Murdoch257744e2011-11-30 15:57:28 +0000724 factory()->constructor_symbol(),
725 isolate()->object_function(),
Steve Block44f0eee2011-05-26 01:26:41 +0100726 NONE);
Andrei Popescu31002712010-02-23 13:46:05 +0000727 } else {
728 Handle<FunctionTemplateInfo> js_global_constructor(
729 FunctionTemplateInfo::cast(js_global_template->constructor()));
730 js_global_function =
Ben Murdoch257744e2011-11-30 15:57:28 +0000731 factory()->CreateApiFunction(js_global_constructor,
732 factory()->InnerGlobalObject);
Steve Blocka7e24c12009-10-30 11:49:00 +0000733 }
734
Andrei Popescu31002712010-02-23 13:46:05 +0000735 js_global_function->initial_map()->set_is_hidden_prototype();
736 Handle<GlobalObject> inner_global =
Ben Murdoch257744e2011-11-30 15:57:28 +0000737 factory()->NewGlobalObject(js_global_function);
Andrei Popescu31002712010-02-23 13:46:05 +0000738 if (inner_global_out != NULL) {
739 *inner_global_out = inner_global;
740 }
741
742 // Step 2: create or re-initialize the global proxy object.
743 Handle<JSFunction> global_proxy_function;
744 if (global_template.IsEmpty()) {
Ben Murdoch257744e2011-11-30 15:57:28 +0000745 Handle<String> name = Handle<String>(heap()->empty_symbol());
746 Handle<Code> code = Handle<Code>(isolate()->builtins()->builtin(
Steve Block44f0eee2011-05-26 01:26:41 +0100747 Builtins::kIllegal));
Andrei Popescu31002712010-02-23 13:46:05 +0000748 global_proxy_function =
Ben Murdoch257744e2011-11-30 15:57:28 +0000749 factory()->NewFunction(name, JS_GLOBAL_PROXY_TYPE,
750 JSGlobalProxy::kSize, code, true);
Andrei Popescu31002712010-02-23 13:46:05 +0000751 } else {
752 Handle<ObjectTemplateInfo> data =
753 v8::Utils::OpenHandle(*global_template);
754 Handle<FunctionTemplateInfo> global_constructor(
755 FunctionTemplateInfo::cast(data->constructor()));
756 global_proxy_function =
Ben Murdoch257744e2011-11-30 15:57:28 +0000757 factory()->CreateApiFunction(global_constructor,
758 factory()->OuterGlobalObject);
Andrei Popescu31002712010-02-23 13:46:05 +0000759 }
760
Ben Murdoch257744e2011-11-30 15:57:28 +0000761 Handle<String> global_name = factory()->LookupAsciiSymbol("global");
Andrei Popescu31002712010-02-23 13:46:05 +0000762 global_proxy_function->shared()->set_instance_class_name(*global_name);
763 global_proxy_function->initial_map()->set_is_access_check_needed(true);
764
765 // Set global_proxy.__proto__ to js_global after ConfigureGlobalObjects
766 // Return the global proxy.
767
768 if (global_object.location() != NULL) {
769 ASSERT(global_object->IsJSGlobalProxy());
770 return ReinitializeJSGlobalProxy(
771 global_proxy_function,
772 Handle<JSGlobalProxy>::cast(global_object));
773 } else {
774 return Handle<JSGlobalProxy>::cast(
Ben Murdoch257744e2011-11-30 15:57:28 +0000775 factory()->NewJSObject(global_proxy_function, TENURED));
Andrei Popescu31002712010-02-23 13:46:05 +0000776 }
777}
778
779
780void Genesis::HookUpGlobalProxy(Handle<GlobalObject> inner_global,
781 Handle<JSGlobalProxy> global_proxy) {
782 // Set the global context for the global object.
783 inner_global->set_global_context(*global_context());
784 inner_global->set_global_receiver(*global_proxy);
785 global_proxy->set_context(*global_context());
786 global_context()->set_global_proxy(*global_proxy);
787}
788
789
Andrei Popescu402d9372010-02-26 13:31:12 +0000790void Genesis::HookUpInnerGlobal(Handle<GlobalObject> inner_global) {
791 Handle<GlobalObject> inner_global_from_snapshot(
792 GlobalObject::cast(global_context_->extension()));
793 Handle<JSBuiltinsObject> builtins_global(global_context_->builtins());
794 global_context_->set_extension(*inner_global);
795 global_context_->set_global(*inner_global);
796 global_context_->set_security_token(*inner_global);
797 static const PropertyAttributes attributes =
798 static_cast<PropertyAttributes>(READ_ONLY | DONT_DELETE);
799 ForceSetProperty(builtins_global,
Ben Murdoch257744e2011-11-30 15:57:28 +0000800 factory()->LookupAsciiSymbol("global"),
Andrei Popescu402d9372010-02-26 13:31:12 +0000801 inner_global,
802 attributes);
803 // Setup the reference from the global object to the builtins object.
804 JSGlobalObject::cast(*inner_global)->set_builtins(*builtins_global);
805 TransferNamedProperties(inner_global_from_snapshot, inner_global);
806 TransferIndexedProperties(inner_global_from_snapshot, inner_global);
807}
808
809
810// This is only called if we are not using snapshots. The equivalent
811// work in the snapshot case is done in HookUpInnerGlobal.
Andrei Popescu31002712010-02-23 13:46:05 +0000812void Genesis::InitializeGlobal(Handle<GlobalObject> inner_global,
813 Handle<JSFunction> empty_function) {
814 // --- G l o b a l C o n t e x t ---
815 // Use the empty function as closure (no scope info).
816 global_context()->set_closure(*empty_function);
Andrei Popescu31002712010-02-23 13:46:05 +0000817 global_context()->set_previous(NULL);
818 // Set extension and global object.
819 global_context()->set_extension(*inner_global);
820 global_context()->set_global(*inner_global);
821 // Security setup: Set the security token of the global object to
822 // its the inner global. This makes the security check between two
823 // different contexts fail by default even in case of global
824 // object reinitialization.
825 global_context()->set_security_token(*inner_global);
826
Ben Murdoch257744e2011-11-30 15:57:28 +0000827 Isolate* isolate = inner_global->GetIsolate();
Steve Block44f0eee2011-05-26 01:26:41 +0100828 Factory* factory = isolate->factory();
829 Heap* heap = isolate->heap();
830
831 Handle<String> object_name = Handle<String>(heap->Object_symbol());
Steve Block1e0659c2011-05-24 12:43:12 +0100832 SetLocalPropertyNoThrow(inner_global, object_name,
Steve Block44f0eee2011-05-26 01:26:41 +0100833 isolate->object_function(), DONT_ENUM);
Andrei Popescu31002712010-02-23 13:46:05 +0000834
Steve Blocka7e24c12009-10-30 11:49:00 +0000835 Handle<JSObject> global = Handle<JSObject>(global_context()->global());
836
837 // Install global Function object
838 InstallFunction(global, "Function", JS_FUNCTION_TYPE, JSFunction::kSize,
Steve Block44f0eee2011-05-26 01:26:41 +0100839 empty_function, Builtins::kIllegal, true); // ECMA native.
Steve Blocka7e24c12009-10-30 11:49:00 +0000840
841 { // --- A r r a y ---
842 Handle<JSFunction> array_function =
843 InstallFunction(global, "Array", JS_ARRAY_TYPE, JSArray::kSize,
Steve Block44f0eee2011-05-26 01:26:41 +0100844 isolate->initial_object_prototype(),
845 Builtins::kArrayCode, true);
Steve Blocka7e24c12009-10-30 11:49:00 +0000846 array_function->shared()->set_construct_stub(
Steve Block44f0eee2011-05-26 01:26:41 +0100847 isolate->builtins()->builtin(Builtins::kArrayConstructCode));
Steve Blocka7e24c12009-10-30 11:49:00 +0000848 array_function->shared()->DontAdaptArguments();
849
850 // This seems a bit hackish, but we need to make sure Array.length
851 // is 1.
852 array_function->shared()->set_length(1);
853 Handle<DescriptorArray> array_descriptors =
Ben Murdoch257744e2011-11-30 15:57:28 +0000854 factory->CopyAppendForeignDescriptor(
Steve Block44f0eee2011-05-26 01:26:41 +0100855 factory->empty_descriptor_array(),
856 factory->length_symbol(),
Ben Murdoch257744e2011-11-30 15:57:28 +0000857 factory->NewForeign(&Accessors::ArrayLength),
Steve Blocka7e24c12009-10-30 11:49:00 +0000858 static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE));
859
860 // Cache the fast JavaScript array map
861 global_context()->set_js_array_map(array_function->initial_map());
862 global_context()->js_array_map()->set_instance_descriptors(
863 *array_descriptors);
864 // array_function is used internally. JS code creating array object should
865 // search for the 'Array' property on the global object and use that one
866 // as the constructor. 'Array' property on a global object can be
867 // overwritten by JS code.
868 global_context()->set_array_function(*array_function);
869 }
870
871 { // --- N u m b e r ---
872 Handle<JSFunction> number_fun =
873 InstallFunction(global, "Number", JS_VALUE_TYPE, JSValue::kSize,
Steve Block44f0eee2011-05-26 01:26:41 +0100874 isolate->initial_object_prototype(),
875 Builtins::kIllegal, true);
Steve Blocka7e24c12009-10-30 11:49:00 +0000876 global_context()->set_number_function(*number_fun);
877 }
878
879 { // --- B o o l e a n ---
880 Handle<JSFunction> boolean_fun =
881 InstallFunction(global, "Boolean", JS_VALUE_TYPE, JSValue::kSize,
Steve Block44f0eee2011-05-26 01:26:41 +0100882 isolate->initial_object_prototype(),
883 Builtins::kIllegal, true);
Steve Blocka7e24c12009-10-30 11:49:00 +0000884 global_context()->set_boolean_function(*boolean_fun);
885 }
886
887 { // --- S t r i n g ---
888 Handle<JSFunction> string_fun =
889 InstallFunction(global, "String", JS_VALUE_TYPE, JSValue::kSize,
Steve Block44f0eee2011-05-26 01:26:41 +0100890 isolate->initial_object_prototype(),
891 Builtins::kIllegal, true);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100892 string_fun->shared()->set_construct_stub(
Steve Block44f0eee2011-05-26 01:26:41 +0100893 isolate->builtins()->builtin(Builtins::kStringConstructCode));
Steve Blocka7e24c12009-10-30 11:49:00 +0000894 global_context()->set_string_function(*string_fun);
895 // Add 'length' property to strings.
896 Handle<DescriptorArray> string_descriptors =
Ben Murdoch257744e2011-11-30 15:57:28 +0000897 factory->CopyAppendForeignDescriptor(
Steve Block44f0eee2011-05-26 01:26:41 +0100898 factory->empty_descriptor_array(),
899 factory->length_symbol(),
Ben Murdoch257744e2011-11-30 15:57:28 +0000900 factory->NewForeign(&Accessors::StringLength),
Steve Blocka7e24c12009-10-30 11:49:00 +0000901 static_cast<PropertyAttributes>(DONT_ENUM |
902 DONT_DELETE |
903 READ_ONLY));
904
905 Handle<Map> string_map =
906 Handle<Map>(global_context()->string_function()->initial_map());
907 string_map->set_instance_descriptors(*string_descriptors);
908 }
909
910 { // --- D a t e ---
911 // Builtin functions for Date.prototype.
912 Handle<JSFunction> date_fun =
913 InstallFunction(global, "Date", JS_VALUE_TYPE, JSValue::kSize,
Steve Block44f0eee2011-05-26 01:26:41 +0100914 isolate->initial_object_prototype(),
915 Builtins::kIllegal, true);
Steve Blocka7e24c12009-10-30 11:49:00 +0000916
917 global_context()->set_date_function(*date_fun);
918 }
919
920
921 { // -- R e g E x p
922 // Builtin functions for RegExp.prototype.
923 Handle<JSFunction> regexp_fun =
924 InstallFunction(global, "RegExp", JS_REGEXP_TYPE, JSRegExp::kSize,
Steve Block44f0eee2011-05-26 01:26:41 +0100925 isolate->initial_object_prototype(),
926 Builtins::kIllegal, true);
Steve Blocka7e24c12009-10-30 11:49:00 +0000927 global_context()->set_regexp_function(*regexp_fun);
Steve Block6ded16b2010-05-10 14:33:55 +0100928
929 ASSERT(regexp_fun->has_initial_map());
930 Handle<Map> initial_map(regexp_fun->initial_map());
931
932 ASSERT_EQ(0, initial_map->inobject_properties());
933
Steve Block44f0eee2011-05-26 01:26:41 +0100934 Handle<DescriptorArray> descriptors = factory->NewDescriptorArray(5);
Steve Block6ded16b2010-05-10 14:33:55 +0100935 PropertyAttributes final =
936 static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE | READ_ONLY);
937 int enum_index = 0;
938 {
939 // ECMA-262, section 15.10.7.1.
Steve Block44f0eee2011-05-26 01:26:41 +0100940 FieldDescriptor field(heap->source_symbol(),
Steve Block6ded16b2010-05-10 14:33:55 +0100941 JSRegExp::kSourceFieldIndex,
942 final,
943 enum_index++);
944 descriptors->Set(0, &field);
945 }
946 {
947 // ECMA-262, section 15.10.7.2.
Steve Block44f0eee2011-05-26 01:26:41 +0100948 FieldDescriptor field(heap->global_symbol(),
Steve Block6ded16b2010-05-10 14:33:55 +0100949 JSRegExp::kGlobalFieldIndex,
950 final,
951 enum_index++);
952 descriptors->Set(1, &field);
953 }
954 {
955 // ECMA-262, section 15.10.7.3.
Steve Block44f0eee2011-05-26 01:26:41 +0100956 FieldDescriptor field(heap->ignore_case_symbol(),
Steve Block6ded16b2010-05-10 14:33:55 +0100957 JSRegExp::kIgnoreCaseFieldIndex,
958 final,
959 enum_index++);
960 descriptors->Set(2, &field);
961 }
962 {
963 // ECMA-262, section 15.10.7.4.
Steve Block44f0eee2011-05-26 01:26:41 +0100964 FieldDescriptor field(heap->multiline_symbol(),
Steve Block6ded16b2010-05-10 14:33:55 +0100965 JSRegExp::kMultilineFieldIndex,
966 final,
967 enum_index++);
968 descriptors->Set(3, &field);
969 }
970 {
971 // ECMA-262, section 15.10.7.5.
972 PropertyAttributes writable =
973 static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE);
Steve Block44f0eee2011-05-26 01:26:41 +0100974 FieldDescriptor field(heap->last_index_symbol(),
Steve Block6ded16b2010-05-10 14:33:55 +0100975 JSRegExp::kLastIndexFieldIndex,
976 writable,
977 enum_index++);
978 descriptors->Set(4, &field);
979 }
980 descriptors->SetNextEnumerationIndex(enum_index);
981 descriptors->Sort();
982
983 initial_map->set_inobject_properties(5);
984 initial_map->set_pre_allocated_property_fields(5);
985 initial_map->set_unused_property_fields(0);
986 initial_map->set_instance_size(
987 initial_map->instance_size() + 5 * kPointerSize);
988 initial_map->set_instance_descriptors(*descriptors);
Iain Merrick75681382010-08-19 15:07:18 +0100989 initial_map->set_visitor_id(StaticVisitorBase::GetVisitorId(*initial_map));
Steve Blocka7e24c12009-10-30 11:49:00 +0000990 }
991
992 { // -- J S O N
Steve Block44f0eee2011-05-26 01:26:41 +0100993 Handle<String> name = factory->NewStringFromAscii(CStrVector("JSON"));
994 Handle<JSFunction> cons = factory->NewFunction(
Steve Blocka7e24c12009-10-30 11:49:00 +0000995 name,
Steve Block44f0eee2011-05-26 01:26:41 +0100996 factory->the_hole_value());
Steve Blocka7e24c12009-10-30 11:49:00 +0000997 cons->SetInstancePrototype(global_context()->initial_object_prototype());
998 cons->SetInstanceClassName(*name);
Steve Block44f0eee2011-05-26 01:26:41 +0100999 Handle<JSObject> json_object = factory->NewJSObject(cons, TENURED);
Steve Blocka7e24c12009-10-30 11:49:00 +00001000 ASSERT(json_object->IsJSObject());
Steve Block1e0659c2011-05-24 12:43:12 +01001001 SetLocalPropertyNoThrow(global, name, json_object, DONT_ENUM);
Steve Blocka7e24c12009-10-30 11:49:00 +00001002 global_context()->set_json_object(*json_object);
1003 }
1004
1005 { // --- arguments_boilerplate_
1006 // Make sure we can recognize argument objects at runtime.
1007 // This is done by introducing an anonymous function with
1008 // class_name equals 'Arguments'.
Steve Block44f0eee2011-05-26 01:26:41 +01001009 Handle<String> symbol = factory->LookupAsciiSymbol("Arguments");
1010 Handle<Code> code = Handle<Code>(
1011 isolate->builtins()->builtin(Builtins::kIllegal));
Steve Blocka7e24c12009-10-30 11:49:00 +00001012 Handle<JSObject> prototype =
1013 Handle<JSObject>(
1014 JSObject::cast(global_context()->object_function()->prototype()));
1015
1016 Handle<JSFunction> function =
Steve Block44f0eee2011-05-26 01:26:41 +01001017 factory->NewFunctionWithPrototype(symbol,
Steve Blocka7e24c12009-10-30 11:49:00 +00001018 JS_OBJECT_TYPE,
1019 JSObject::kHeaderSize,
1020 prototype,
1021 code,
1022 false);
1023 ASSERT(!function->has_initial_map());
1024 function->shared()->set_instance_class_name(*symbol);
1025 function->shared()->set_expected_nof_properties(2);
Steve Block44f0eee2011-05-26 01:26:41 +01001026 Handle<JSObject> result = factory->NewJSObject(function);
Steve Blocka7e24c12009-10-30 11:49:00 +00001027
1028 global_context()->set_arguments_boilerplate(*result);
Steve Block44f0eee2011-05-26 01:26:41 +01001029 // Note: length must be added as the first property and
1030 // callee must be added as the second property.
1031 SetLocalPropertyNoThrow(result, factory->length_symbol(),
1032 factory->undefined_value(),
Steve Block1e0659c2011-05-24 12:43:12 +01001033 DONT_ENUM);
Steve Block44f0eee2011-05-26 01:26:41 +01001034 SetLocalPropertyNoThrow(result, factory->callee_symbol(),
1035 factory->undefined_value(),
Steve Block1e0659c2011-05-24 12:43:12 +01001036 DONT_ENUM);
Steve Blocka7e24c12009-10-30 11:49:00 +00001037
1038#ifdef DEBUG
1039 LookupResult lookup;
Steve Block44f0eee2011-05-26 01:26:41 +01001040 result->LocalLookup(heap->callee_symbol(), &lookup);
Andrei Popescu402d9372010-02-26 13:31:12 +00001041 ASSERT(lookup.IsProperty() && (lookup.type() == FIELD));
Steve Block44f0eee2011-05-26 01:26:41 +01001042 ASSERT(lookup.GetFieldIndex() == Heap::kArgumentsCalleeIndex);
Steve Blocka7e24c12009-10-30 11:49:00 +00001043
Steve Block44f0eee2011-05-26 01:26:41 +01001044 result->LocalLookup(heap->length_symbol(), &lookup);
Andrei Popescu402d9372010-02-26 13:31:12 +00001045 ASSERT(lookup.IsProperty() && (lookup.type() == FIELD));
Steve Block44f0eee2011-05-26 01:26:41 +01001046 ASSERT(lookup.GetFieldIndex() == Heap::kArgumentsLengthIndex);
Steve Blocka7e24c12009-10-30 11:49:00 +00001047
Steve Block44f0eee2011-05-26 01:26:41 +01001048 ASSERT(result->map()->inobject_properties() > Heap::kArgumentsCalleeIndex);
1049 ASSERT(result->map()->inobject_properties() > Heap::kArgumentsLengthIndex);
1050
1051 // Check the state of the object.
1052 ASSERT(result->HasFastProperties());
1053 ASSERT(result->HasFastElements());
1054#endif
1055 }
1056
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001057 { // --- aliased_arguments_boilerplate_
1058 Handle<Map> old_map(global_context()->arguments_boilerplate()->map());
1059 Handle<Map> new_map = factory->CopyMapDropTransitions(old_map);
1060 new_map->set_pre_allocated_property_fields(2);
1061 Handle<JSObject> result = factory->NewJSObjectFromMap(new_map);
1062 new_map->set_elements_kind(JSObject::NON_STRICT_ARGUMENTS_ELEMENTS);
1063 // Set up a well-formed parameter map to make assertions happy.
1064 Handle<FixedArray> elements = factory->NewFixedArray(2);
1065 elements->set_map(heap->non_strict_arguments_elements_map());
1066 Handle<FixedArray> array;
1067 array = factory->NewFixedArray(0);
1068 elements->set(0, *array);
1069 array = factory->NewFixedArray(0);
1070 elements->set(1, *array);
1071 result->set_elements(*elements);
1072 global_context()->set_aliased_arguments_boilerplate(*result);
1073 }
1074
Steve Block44f0eee2011-05-26 01:26:41 +01001075 { // --- strict mode arguments boilerplate
1076 const PropertyAttributes attributes =
1077 static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE | READ_ONLY);
1078
1079 // Create the ThrowTypeError functions.
1080 Handle<FixedArray> callee = factory->NewFixedArray(2, TENURED);
1081 Handle<FixedArray> caller = factory->NewFixedArray(2, TENURED);
1082
Ben Murdoch257744e2011-11-30 15:57:28 +00001083 Handle<JSFunction> throw_function =
1084 GetThrowTypeErrorFunction();
Steve Block44f0eee2011-05-26 01:26:41 +01001085
1086 // Install the ThrowTypeError functions.
Ben Murdoch257744e2011-11-30 15:57:28 +00001087 callee->set(0, *throw_function);
1088 callee->set(1, *throw_function);
1089 caller->set(0, *throw_function);
1090 caller->set(1, *throw_function);
Steve Block44f0eee2011-05-26 01:26:41 +01001091
1092 // Create the descriptor array for the arguments object.
1093 Handle<DescriptorArray> descriptors = factory->NewDescriptorArray(3);
1094 { // length
1095 FieldDescriptor d(*factory->length_symbol(), 0, DONT_ENUM);
1096 descriptors->Set(0, &d);
1097 }
1098 { // callee
1099 CallbacksDescriptor d(*factory->callee_symbol(), *callee, attributes);
1100 descriptors->Set(1, &d);
1101 }
1102 { // caller
1103 CallbacksDescriptor d(*factory->caller_symbol(), *caller, attributes);
1104 descriptors->Set(2, &d);
1105 }
1106 descriptors->Sort();
1107
1108 // Create the map. Allocate one in-object field for length.
1109 Handle<Map> map = factory->NewMap(JS_OBJECT_TYPE,
1110 Heap::kArgumentsObjectSizeStrict);
1111 map->set_instance_descriptors(*descriptors);
1112 map->set_function_with_prototype(true);
1113 map->set_prototype(global_context()->object_function()->prototype());
1114 map->set_pre_allocated_property_fields(1);
1115 map->set_inobject_properties(1);
1116
1117 // Copy constructor from the non-strict arguments boilerplate.
1118 map->set_constructor(
1119 global_context()->arguments_boilerplate()->map()->constructor());
1120
1121 // Allocate the arguments boilerplate object.
1122 Handle<JSObject> result = factory->NewJSObjectFromMap(map);
1123 global_context()->set_strict_mode_arguments_boilerplate(*result);
1124
1125 // Add length property only for strict mode boilerplate.
1126 SetLocalPropertyNoThrow(result, factory->length_symbol(),
1127 factory->undefined_value(),
1128 DONT_ENUM);
1129
1130#ifdef DEBUG
1131 LookupResult lookup;
1132 result->LocalLookup(heap->length_symbol(), &lookup);
1133 ASSERT(lookup.IsProperty() && (lookup.type() == FIELD));
1134 ASSERT(lookup.GetFieldIndex() == Heap::kArgumentsLengthIndex);
1135
1136 ASSERT(result->map()->inobject_properties() > Heap::kArgumentsLengthIndex);
Steve Blocka7e24c12009-10-30 11:49:00 +00001137
1138 // Check the state of the object.
1139 ASSERT(result->HasFastProperties());
1140 ASSERT(result->HasFastElements());
1141#endif
1142 }
1143
1144 { // --- context extension
1145 // Create a function for the context extension objects.
Steve Block44f0eee2011-05-26 01:26:41 +01001146 Handle<Code> code = Handle<Code>(
1147 isolate->builtins()->builtin(Builtins::kIllegal));
Steve Blocka7e24c12009-10-30 11:49:00 +00001148 Handle<JSFunction> context_extension_fun =
Steve Block44f0eee2011-05-26 01:26:41 +01001149 factory->NewFunction(factory->empty_symbol(),
Steve Blocka7e24c12009-10-30 11:49:00 +00001150 JS_CONTEXT_EXTENSION_OBJECT_TYPE,
1151 JSObject::kHeaderSize,
1152 code,
1153 true);
1154
Steve Block44f0eee2011-05-26 01:26:41 +01001155 Handle<String> name = factory->LookupAsciiSymbol("context_extension");
Steve Blocka7e24c12009-10-30 11:49:00 +00001156 context_extension_fun->shared()->set_instance_class_name(*name);
1157 global_context()->set_context_extension_function(*context_extension_fun);
1158 }
1159
1160
1161 {
1162 // Setup the call-as-function delegate.
1163 Handle<Code> code =
Steve Block44f0eee2011-05-26 01:26:41 +01001164 Handle<Code>(isolate->builtins()->builtin(
1165 Builtins::kHandleApiCallAsFunction));
Steve Blocka7e24c12009-10-30 11:49:00 +00001166 Handle<JSFunction> delegate =
Steve Block44f0eee2011-05-26 01:26:41 +01001167 factory->NewFunction(factory->empty_symbol(), JS_OBJECT_TYPE,
Steve Blocka7e24c12009-10-30 11:49:00 +00001168 JSObject::kHeaderSize, code, true);
1169 global_context()->set_call_as_function_delegate(*delegate);
1170 delegate->shared()->DontAdaptArguments();
1171 }
1172
1173 {
1174 // Setup the call-as-constructor delegate.
1175 Handle<Code> code =
Steve Block44f0eee2011-05-26 01:26:41 +01001176 Handle<Code>(isolate->builtins()->builtin(
1177 Builtins::kHandleApiCallAsConstructor));
Steve Blocka7e24c12009-10-30 11:49:00 +00001178 Handle<JSFunction> delegate =
Steve Block44f0eee2011-05-26 01:26:41 +01001179 factory->NewFunction(factory->empty_symbol(), JS_OBJECT_TYPE,
Steve Blocka7e24c12009-10-30 11:49:00 +00001180 JSObject::kHeaderSize, code, true);
1181 global_context()->set_call_as_constructor_delegate(*delegate);
1182 delegate->shared()->DontAdaptArguments();
1183 }
1184
Steve Blocka7e24c12009-10-30 11:49:00 +00001185 // Initialize the out of memory slot.
Steve Block44f0eee2011-05-26 01:26:41 +01001186 global_context()->set_out_of_memory(heap->false_value());
Steve Blocka7e24c12009-10-30 11:49:00 +00001187
1188 // Initialize the data slot.
Steve Block44f0eee2011-05-26 01:26:41 +01001189 global_context()->set_data(heap->undefined_value());
Steve Blocka7e24c12009-10-30 11:49:00 +00001190}
1191
1192
Ben Murdoch257744e2011-11-30 15:57:28 +00001193bool Genesis::CompileBuiltin(Isolate* isolate, int index) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001194 Vector<const char> name = Natives::GetScriptName(index);
Steve Block44f0eee2011-05-26 01:26:41 +01001195 Handle<String> source_code =
Ben Murdoch257744e2011-11-30 15:57:28 +00001196 isolate->bootstrapper()->NativesSourceLookup(index);
1197 return CompileNative(name, source_code);
1198}
1199
1200
1201bool Genesis::CompileExperimentalBuiltin(Isolate* isolate, int index) {
1202 Vector<const char> name = ExperimentalNatives::GetScriptName(index);
1203 Factory* factory = isolate->factory();
1204 Handle<String> source_code =
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001205 factory->NewStringFromAscii(
1206 ExperimentalNatives::GetRawScriptSource(index));
Steve Blocka7e24c12009-10-30 11:49:00 +00001207 return CompileNative(name, source_code);
1208}
1209
1210
1211bool Genesis::CompileNative(Vector<const char> name, Handle<String> source) {
1212 HandleScope scope;
Ben Murdoch257744e2011-11-30 15:57:28 +00001213 Isolate* isolate = source->GetIsolate();
Steve Blocka7e24c12009-10-30 11:49:00 +00001214#ifdef ENABLE_DEBUGGER_SUPPORT
Steve Block44f0eee2011-05-26 01:26:41 +01001215 isolate->debugger()->set_compiling_natives(true);
Steve Blocka7e24c12009-10-30 11:49:00 +00001216#endif
Andrei Popescu31002712010-02-23 13:46:05 +00001217 bool result = CompileScriptCached(name,
1218 source,
1219 NULL,
1220 NULL,
Steve Block44f0eee2011-05-26 01:26:41 +01001221 Handle<Context>(isolate->context()),
Andrei Popescu31002712010-02-23 13:46:05 +00001222 true);
Steve Block44f0eee2011-05-26 01:26:41 +01001223 ASSERT(isolate->has_pending_exception() != result);
1224 if (!result) isolate->clear_pending_exception();
Steve Blocka7e24c12009-10-30 11:49:00 +00001225#ifdef ENABLE_DEBUGGER_SUPPORT
Steve Block44f0eee2011-05-26 01:26:41 +01001226 isolate->debugger()->set_compiling_natives(false);
Steve Blocka7e24c12009-10-30 11:49:00 +00001227#endif
1228 return result;
1229}
1230
1231
1232bool Genesis::CompileScriptCached(Vector<const char> name,
1233 Handle<String> source,
1234 SourceCodeCache* cache,
1235 v8::Extension* extension,
Andrei Popescu31002712010-02-23 13:46:05 +00001236 Handle<Context> top_context,
Steve Blocka7e24c12009-10-30 11:49:00 +00001237 bool use_runtime_context) {
Ben Murdoch257744e2011-11-30 15:57:28 +00001238 Factory* factory = source->GetIsolate()->factory();
Steve Blocka7e24c12009-10-30 11:49:00 +00001239 HandleScope scope;
Steve Block6ded16b2010-05-10 14:33:55 +01001240 Handle<SharedFunctionInfo> function_info;
Steve Blocka7e24c12009-10-30 11:49:00 +00001241
1242 // If we can't find the function in the cache, we compile a new
1243 // function and insert it into the cache.
Steve Block6ded16b2010-05-10 14:33:55 +01001244 if (cache == NULL || !cache->Lookup(name, &function_info)) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001245 ASSERT(source->IsAsciiRepresentation());
Steve Block44f0eee2011-05-26 01:26:41 +01001246 Handle<String> script_name = factory->NewStringFromUtf8(name);
Steve Block6ded16b2010-05-10 14:33:55 +01001247 function_info = Compiler::Compile(
Andrei Popescu31002712010-02-23 13:46:05 +00001248 source,
1249 script_name,
1250 0,
1251 0,
1252 extension,
1253 NULL,
Andrei Popescu402d9372010-02-26 13:31:12 +00001254 Handle<String>::null(),
Andrei Popescu31002712010-02-23 13:46:05 +00001255 use_runtime_context ? NATIVES_CODE : NOT_NATIVES_CODE);
Steve Block6ded16b2010-05-10 14:33:55 +01001256 if (function_info.is_null()) return false;
1257 if (cache != NULL) cache->Add(name, function_info);
Steve Blocka7e24c12009-10-30 11:49:00 +00001258 }
1259
1260 // Setup the function context. Conceptually, we should clone the
1261 // function before overwriting the context but since we're in a
1262 // single-threaded environment it is not strictly necessary.
Andrei Popescu31002712010-02-23 13:46:05 +00001263 ASSERT(top_context->IsGlobalContext());
Steve Blocka7e24c12009-10-30 11:49:00 +00001264 Handle<Context> context =
1265 Handle<Context>(use_runtime_context
Andrei Popescu31002712010-02-23 13:46:05 +00001266 ? Handle<Context>(top_context->runtime_context())
1267 : top_context);
Steve Blocka7e24c12009-10-30 11:49:00 +00001268 Handle<JSFunction> fun =
Steve Block44f0eee2011-05-26 01:26:41 +01001269 factory->NewFunctionFromSharedFunctionInfo(function_info, context);
Steve Blocka7e24c12009-10-30 11:49:00 +00001270
Leon Clarke4515c472010-02-03 11:58:03 +00001271 // Call function using either the runtime object or the global
Steve Blocka7e24c12009-10-30 11:49:00 +00001272 // object as the receiver. Provide no parameters.
1273 Handle<Object> receiver =
1274 Handle<Object>(use_runtime_context
Andrei Popescu31002712010-02-23 13:46:05 +00001275 ? top_context->builtins()
1276 : top_context->global());
Steve Blocka7e24c12009-10-30 11:49:00 +00001277 bool has_pending_exception;
Ben Murdoch257744e2011-11-30 15:57:28 +00001278 Execution::Call(fun, receiver, 0, NULL, &has_pending_exception);
Steve Blocka7e24c12009-10-30 11:49:00 +00001279 if (has_pending_exception) return false;
Andrei Popescu402d9372010-02-26 13:31:12 +00001280 return true;
Steve Blocka7e24c12009-10-30 11:49:00 +00001281}
1282
1283
Ben Murdoch257744e2011-11-30 15:57:28 +00001284#define INSTALL_NATIVE(Type, name, var) \
1285 Handle<String> var##_name = factory()->LookupAsciiSymbol(name); \
1286 Object* var##_native = \
1287 global_context()->builtins()->GetPropertyNoExceptionThrown( \
1288 *var##_name); \
Ben Murdoch8b112d22011-06-08 16:22:53 +01001289 global_context()->set_##var(Type::cast(var##_native));
Steve Blocka7e24c12009-10-30 11:49:00 +00001290
Steve Block44f0eee2011-05-26 01:26:41 +01001291
Steve Blocka7e24c12009-10-30 11:49:00 +00001292void Genesis::InstallNativeFunctions() {
1293 HandleScope scope;
1294 INSTALL_NATIVE(JSFunction, "CreateDate", create_date_fun);
1295 INSTALL_NATIVE(JSFunction, "ToNumber", to_number_fun);
1296 INSTALL_NATIVE(JSFunction, "ToString", to_string_fun);
1297 INSTALL_NATIVE(JSFunction, "ToDetailString", to_detail_string_fun);
1298 INSTALL_NATIVE(JSFunction, "ToObject", to_object_fun);
1299 INSTALL_NATIVE(JSFunction, "ToInteger", to_integer_fun);
1300 INSTALL_NATIVE(JSFunction, "ToUint32", to_uint32_fun);
1301 INSTALL_NATIVE(JSFunction, "ToInt32", to_int32_fun);
Leon Clarkee46be812010-01-19 14:06:41 +00001302 INSTALL_NATIVE(JSFunction, "GlobalEval", global_eval_fun);
Steve Blocka7e24c12009-10-30 11:49:00 +00001303 INSTALL_NATIVE(JSFunction, "Instantiate", instantiate_fun);
1304 INSTALL_NATIVE(JSFunction, "ConfigureTemplateInstance",
1305 configure_instance_fun);
Steve Blocka7e24c12009-10-30 11:49:00 +00001306 INSTALL_NATIVE(JSFunction, "GetStackTraceLine", get_stack_trace_line_fun);
1307 INSTALL_NATIVE(JSObject, "functionCache", function_cache);
1308}
1309
Ben Murdoch257744e2011-11-30 15:57:28 +00001310void Genesis::InstallExperimentalNativeFunctions() {
1311 if (FLAG_harmony_proxies) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001312 INSTALL_NATIVE(JSFunction, "DerivedHasTrap", derived_has_trap);
Ben Murdoch257744e2011-11-30 15:57:28 +00001313 INSTALL_NATIVE(JSFunction, "DerivedGetTrap", derived_get_trap);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001314 INSTALL_NATIVE(JSFunction, "DerivedSetTrap", derived_set_trap);
Ben Murdoch257744e2011-11-30 15:57:28 +00001315 }
1316}
1317
Steve Blocka7e24c12009-10-30 11:49:00 +00001318#undef INSTALL_NATIVE
1319
1320
1321bool Genesis::InstallNatives() {
1322 HandleScope scope;
1323
1324 // Create a function for the builtins object. Allocate space for the
1325 // JavaScript builtins, a reference to the builtins object
1326 // (itself) and a reference to the global_context directly in the object.
Steve Block44f0eee2011-05-26 01:26:41 +01001327 Handle<Code> code = Handle<Code>(
Ben Murdoch257744e2011-11-30 15:57:28 +00001328 isolate()->builtins()->builtin(Builtins::kIllegal));
Steve Blocka7e24c12009-10-30 11:49:00 +00001329 Handle<JSFunction> builtins_fun =
Ben Murdoch257744e2011-11-30 15:57:28 +00001330 factory()->NewFunction(factory()->empty_symbol(),
1331 JS_BUILTINS_OBJECT_TYPE,
1332 JSBuiltinsObject::kSize, code, true);
Steve Blocka7e24c12009-10-30 11:49:00 +00001333
Ben Murdoch257744e2011-11-30 15:57:28 +00001334 Handle<String> name = factory()->LookupAsciiSymbol("builtins");
Steve Blocka7e24c12009-10-30 11:49:00 +00001335 builtins_fun->shared()->set_instance_class_name(*name);
1336
1337 // Allocate the builtins object.
1338 Handle<JSBuiltinsObject> builtins =
Ben Murdoch257744e2011-11-30 15:57:28 +00001339 Handle<JSBuiltinsObject>::cast(factory()->NewGlobalObject(builtins_fun));
Steve Blocka7e24c12009-10-30 11:49:00 +00001340 builtins->set_builtins(*builtins);
1341 builtins->set_global_context(*global_context());
1342 builtins->set_global_receiver(*builtins);
1343
1344 // Setup the 'global' properties of the builtins object. The
1345 // 'global' property that refers to the global object is the only
1346 // way to get from code running in the builtins context to the
1347 // global object.
1348 static const PropertyAttributes attributes =
1349 static_cast<PropertyAttributes>(READ_ONLY | DONT_DELETE);
Ben Murdoch257744e2011-11-30 15:57:28 +00001350 Handle<String> global_symbol = factory()->LookupAsciiSymbol("global");
Steve Block1e0659c2011-05-24 12:43:12 +01001351 Handle<Object> global_obj(global_context()->global());
1352 SetLocalPropertyNoThrow(builtins, global_symbol, global_obj, attributes);
Steve Blocka7e24c12009-10-30 11:49:00 +00001353
1354 // Setup the reference from the global object to the builtins object.
1355 JSGlobalObject::cast(global_context()->global())->set_builtins(*builtins);
1356
1357 // Create a bridge function that has context in the global context.
1358 Handle<JSFunction> bridge =
Ben Murdoch257744e2011-11-30 15:57:28 +00001359 factory()->NewFunction(factory()->empty_symbol(),
1360 factory()->undefined_value());
1361 ASSERT(bridge->context() == *isolate()->global_context());
Steve Blocka7e24c12009-10-30 11:49:00 +00001362
1363 // Allocate the builtins context.
1364 Handle<Context> context =
Ben Murdoch257744e2011-11-30 15:57:28 +00001365 factory()->NewFunctionContext(Context::MIN_CONTEXT_SLOTS, bridge);
Steve Blocka7e24c12009-10-30 11:49:00 +00001366 context->set_global(*builtins); // override builtins global object
1367
1368 global_context()->set_runtime_context(*context);
1369
1370 { // -- S c r i p t
1371 // Builtin functions for Script.
1372 Handle<JSFunction> script_fun =
1373 InstallFunction(builtins, "Script", JS_VALUE_TYPE, JSValue::kSize,
Ben Murdoch257744e2011-11-30 15:57:28 +00001374 isolate()->initial_object_prototype(),
Steve Block44f0eee2011-05-26 01:26:41 +01001375 Builtins::kIllegal, false);
Steve Blocka7e24c12009-10-30 11:49:00 +00001376 Handle<JSObject> prototype =
Ben Murdoch257744e2011-11-30 15:57:28 +00001377 factory()->NewJSObject(isolate()->object_function(), TENURED);
Steve Blocka7e24c12009-10-30 11:49:00 +00001378 SetPrototype(script_fun, prototype);
1379 global_context()->set_script_function(*script_fun);
1380
1381 // Add 'source' and 'data' property to scripts.
1382 PropertyAttributes common_attributes =
1383 static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE | READ_ONLY);
Ben Murdoch257744e2011-11-30 15:57:28 +00001384 Handle<Foreign> foreign_source =
1385 factory()->NewForeign(&Accessors::ScriptSource);
Steve Blocka7e24c12009-10-30 11:49:00 +00001386 Handle<DescriptorArray> script_descriptors =
Ben Murdoch257744e2011-11-30 15:57:28 +00001387 factory()->CopyAppendForeignDescriptor(
1388 factory()->empty_descriptor_array(),
1389 factory()->LookupAsciiSymbol("source"),
1390 foreign_source,
Steve Blocka7e24c12009-10-30 11:49:00 +00001391 common_attributes);
Ben Murdoch257744e2011-11-30 15:57:28 +00001392 Handle<Foreign> foreign_name =
1393 factory()->NewForeign(&Accessors::ScriptName);
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("name"),
1398 foreign_name,
Steve Blocka7e24c12009-10-30 11:49:00 +00001399 common_attributes);
Ben Murdoch257744e2011-11-30 15:57:28 +00001400 Handle<Foreign> foreign_id = factory()->NewForeign(&Accessors::ScriptId);
Steve Blocka7e24c12009-10-30 11:49:00 +00001401 script_descriptors =
Ben Murdoch257744e2011-11-30 15:57:28 +00001402 factory()->CopyAppendForeignDescriptor(
Steve Blocka7e24c12009-10-30 11:49:00 +00001403 script_descriptors,
Ben Murdoch257744e2011-11-30 15:57:28 +00001404 factory()->LookupAsciiSymbol("id"),
1405 foreign_id,
Steve Blocka7e24c12009-10-30 11:49:00 +00001406 common_attributes);
Ben Murdoch257744e2011-11-30 15:57:28 +00001407 Handle<Foreign> foreign_line_offset =
1408 factory()->NewForeign(&Accessors::ScriptLineOffset);
Steve Blocka7e24c12009-10-30 11:49:00 +00001409 script_descriptors =
Ben Murdoch257744e2011-11-30 15:57:28 +00001410 factory()->CopyAppendForeignDescriptor(
Steve Blocka7e24c12009-10-30 11:49:00 +00001411 script_descriptors,
Ben Murdoch257744e2011-11-30 15:57:28 +00001412 factory()->LookupAsciiSymbol("line_offset"),
1413 foreign_line_offset,
Steve Blocka7e24c12009-10-30 11:49:00 +00001414 common_attributes);
Ben Murdoch257744e2011-11-30 15:57:28 +00001415 Handle<Foreign> foreign_column_offset =
1416 factory()->NewForeign(&Accessors::ScriptColumnOffset);
Steve Blocka7e24c12009-10-30 11:49:00 +00001417 script_descriptors =
Ben Murdoch257744e2011-11-30 15:57:28 +00001418 factory()->CopyAppendForeignDescriptor(
Steve Blocka7e24c12009-10-30 11:49:00 +00001419 script_descriptors,
Ben Murdoch257744e2011-11-30 15:57:28 +00001420 factory()->LookupAsciiSymbol("column_offset"),
1421 foreign_column_offset,
Steve Blocka7e24c12009-10-30 11:49:00 +00001422 common_attributes);
Ben Murdoch257744e2011-11-30 15:57:28 +00001423 Handle<Foreign> foreign_data =
1424 factory()->NewForeign(&Accessors::ScriptData);
Steve Blocka7e24c12009-10-30 11:49:00 +00001425 script_descriptors =
Ben Murdoch257744e2011-11-30 15:57:28 +00001426 factory()->CopyAppendForeignDescriptor(
Steve Blocka7e24c12009-10-30 11:49:00 +00001427 script_descriptors,
Ben Murdoch257744e2011-11-30 15:57:28 +00001428 factory()->LookupAsciiSymbol("data"),
1429 foreign_data,
Steve Blocka7e24c12009-10-30 11:49:00 +00001430 common_attributes);
Ben Murdoch257744e2011-11-30 15:57:28 +00001431 Handle<Foreign> foreign_type =
1432 factory()->NewForeign(&Accessors::ScriptType);
Steve Blocka7e24c12009-10-30 11:49:00 +00001433 script_descriptors =
Ben Murdoch257744e2011-11-30 15:57:28 +00001434 factory()->CopyAppendForeignDescriptor(
Steve Blocka7e24c12009-10-30 11:49:00 +00001435 script_descriptors,
Ben Murdoch257744e2011-11-30 15:57:28 +00001436 factory()->LookupAsciiSymbol("type"),
1437 foreign_type,
Steve Blocka7e24c12009-10-30 11:49:00 +00001438 common_attributes);
Ben Murdoch257744e2011-11-30 15:57:28 +00001439 Handle<Foreign> foreign_compilation_type =
1440 factory()->NewForeign(&Accessors::ScriptCompilationType);
Steve Blocka7e24c12009-10-30 11:49:00 +00001441 script_descriptors =
Ben Murdoch257744e2011-11-30 15:57:28 +00001442 factory()->CopyAppendForeignDescriptor(
Steve Blocka7e24c12009-10-30 11:49:00 +00001443 script_descriptors,
Ben Murdoch257744e2011-11-30 15:57:28 +00001444 factory()->LookupAsciiSymbol("compilation_type"),
1445 foreign_compilation_type,
Steve Blocka7e24c12009-10-30 11:49:00 +00001446 common_attributes);
Ben Murdoch257744e2011-11-30 15:57:28 +00001447 Handle<Foreign> foreign_line_ends =
1448 factory()->NewForeign(&Accessors::ScriptLineEnds);
Steve Blocka7e24c12009-10-30 11:49:00 +00001449 script_descriptors =
Ben Murdoch257744e2011-11-30 15:57:28 +00001450 factory()->CopyAppendForeignDescriptor(
Steve Blocka7e24c12009-10-30 11:49:00 +00001451 script_descriptors,
Ben Murdoch257744e2011-11-30 15:57:28 +00001452 factory()->LookupAsciiSymbol("line_ends"),
1453 foreign_line_ends,
Steve Blocka7e24c12009-10-30 11:49:00 +00001454 common_attributes);
Ben Murdoch257744e2011-11-30 15:57:28 +00001455 Handle<Foreign> foreign_context_data =
1456 factory()->NewForeign(&Accessors::ScriptContextData);
Steve Blocka7e24c12009-10-30 11:49:00 +00001457 script_descriptors =
Ben Murdoch257744e2011-11-30 15:57:28 +00001458 factory()->CopyAppendForeignDescriptor(
Steve Blocka7e24c12009-10-30 11:49:00 +00001459 script_descriptors,
Ben Murdoch257744e2011-11-30 15:57:28 +00001460 factory()->LookupAsciiSymbol("context_data"),
1461 foreign_context_data,
Steve Blocka7e24c12009-10-30 11:49:00 +00001462 common_attributes);
Ben Murdoch257744e2011-11-30 15:57:28 +00001463 Handle<Foreign> foreign_eval_from_script =
1464 factory()->NewForeign(&Accessors::ScriptEvalFromScript);
Steve Blocka7e24c12009-10-30 11:49:00 +00001465 script_descriptors =
Ben Murdoch257744e2011-11-30 15:57:28 +00001466 factory()->CopyAppendForeignDescriptor(
Steve Blocka7e24c12009-10-30 11:49:00 +00001467 script_descriptors,
Ben Murdoch257744e2011-11-30 15:57:28 +00001468 factory()->LookupAsciiSymbol("eval_from_script"),
1469 foreign_eval_from_script,
Steve Blocka7e24c12009-10-30 11:49:00 +00001470 common_attributes);
Ben Murdoch257744e2011-11-30 15:57:28 +00001471 Handle<Foreign> foreign_eval_from_script_position =
1472 factory()->NewForeign(&Accessors::ScriptEvalFromScriptPosition);
Steve Blocka7e24c12009-10-30 11:49:00 +00001473 script_descriptors =
Ben Murdoch257744e2011-11-30 15:57:28 +00001474 factory()->CopyAppendForeignDescriptor(
Steve Blocka7e24c12009-10-30 11:49:00 +00001475 script_descriptors,
Ben Murdoch257744e2011-11-30 15:57:28 +00001476 factory()->LookupAsciiSymbol("eval_from_script_position"),
1477 foreign_eval_from_script_position,
Steve Blockd0582a62009-12-15 09:54:21 +00001478 common_attributes);
Ben Murdoch257744e2011-11-30 15:57:28 +00001479 Handle<Foreign> foreign_eval_from_function_name =
1480 factory()->NewForeign(&Accessors::ScriptEvalFromFunctionName);
Steve Blockd0582a62009-12-15 09:54:21 +00001481 script_descriptors =
Ben Murdoch257744e2011-11-30 15:57:28 +00001482 factory()->CopyAppendForeignDescriptor(
Steve Blockd0582a62009-12-15 09:54:21 +00001483 script_descriptors,
Ben Murdoch257744e2011-11-30 15:57:28 +00001484 factory()->LookupAsciiSymbol("eval_from_function_name"),
1485 foreign_eval_from_function_name,
Steve Blocka7e24c12009-10-30 11:49:00 +00001486 common_attributes);
1487
1488 Handle<Map> script_map = Handle<Map>(script_fun->initial_map());
1489 script_map->set_instance_descriptors(*script_descriptors);
1490
1491 // Allocate the empty script.
Ben Murdoch257744e2011-11-30 15:57:28 +00001492 Handle<Script> script = factory()->NewScript(factory()->empty_string());
Steve Blocka7e24c12009-10-30 11:49:00 +00001493 script->set_type(Smi::FromInt(Script::TYPE_NATIVE));
Ben Murdoch257744e2011-11-30 15:57:28 +00001494 heap()->public_set_empty_script(*script);
Steve Blocka7e24c12009-10-30 11:49:00 +00001495 }
Steve Block6ded16b2010-05-10 14:33:55 +01001496 {
1497 // Builtin function for OpaqueReference -- a JSValue-based object,
1498 // that keeps its field isolated from JavaScript code. It may store
1499 // objects, that JavaScript code may not access.
1500 Handle<JSFunction> opaque_reference_fun =
1501 InstallFunction(builtins, "OpaqueReference", JS_VALUE_TYPE,
Steve Block44f0eee2011-05-26 01:26:41 +01001502 JSValue::kSize,
Ben Murdoch257744e2011-11-30 15:57:28 +00001503 isolate()->initial_object_prototype(),
Steve Block44f0eee2011-05-26 01:26:41 +01001504 Builtins::kIllegal, false);
Steve Block6ded16b2010-05-10 14:33:55 +01001505 Handle<JSObject> prototype =
Ben Murdoch257744e2011-11-30 15:57:28 +00001506 factory()->NewJSObject(isolate()->object_function(), TENURED);
Steve Block6ded16b2010-05-10 14:33:55 +01001507 SetPrototype(opaque_reference_fun, prototype);
1508 global_context()->set_opaque_reference_function(*opaque_reference_fun);
1509 }
1510
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001511 { // --- I n t e r n a l A r r a y ---
1512 // An array constructor on the builtins object that works like
1513 // the public Array constructor, except that its prototype
1514 // doesn't inherit from Object.prototype.
1515 // To be used only for internal work by builtins. Instances
1516 // must not be leaked to user code.
1517 // Only works correctly when called as a constructor. The normal
1518 // Array code uses Array.prototype as prototype when called as
1519 // a function.
1520 Handle<JSFunction> array_function =
1521 InstallFunction(builtins,
1522 "InternalArray",
1523 JS_ARRAY_TYPE,
1524 JSArray::kSize,
Ben Murdoch257744e2011-11-30 15:57:28 +00001525 isolate()->initial_object_prototype(),
Steve Block44f0eee2011-05-26 01:26:41 +01001526 Builtins::kArrayCode,
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001527 true);
1528 Handle<JSObject> prototype =
Ben Murdoch257744e2011-11-30 15:57:28 +00001529 factory()->NewJSObject(isolate()->object_function(), TENURED);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001530 SetPrototype(array_function, prototype);
1531
1532 array_function->shared()->set_construct_stub(
Ben Murdoch257744e2011-11-30 15:57:28 +00001533 isolate()->builtins()->builtin(Builtins::kArrayConstructCode));
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001534 array_function->shared()->DontAdaptArguments();
1535
1536 // Make "length" magic on instances.
1537 Handle<DescriptorArray> array_descriptors =
Ben Murdoch257744e2011-11-30 15:57:28 +00001538 factory()->CopyAppendForeignDescriptor(
1539 factory()->empty_descriptor_array(),
1540 factory()->length_symbol(),
1541 factory()->NewForeign(&Accessors::ArrayLength),
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001542 static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE));
1543
1544 array_function->initial_map()->set_instance_descriptors(
1545 *array_descriptors);
1546 }
1547
Steve Block6ded16b2010-05-10 14:33:55 +01001548 if (FLAG_disable_native_files) {
1549 PrintF("Warning: Running without installed natives!\n");
1550 return true;
1551 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001552
Andrei Popescu31002712010-02-23 13:46:05 +00001553 // Install natives.
1554 for (int i = Natives::GetDebuggerCount();
1555 i < Natives::GetBuiltinsCount();
1556 i++) {
Ben Murdoch257744e2011-11-30 15:57:28 +00001557 if (!CompileBuiltin(isolate(), i)) return false;
Andrei Popescu402d9372010-02-26 13:31:12 +00001558 // TODO(ager): We really only need to install the JS builtin
1559 // functions on the builtins object after compiling and running
1560 // runtime.js.
1561 if (!InstallJSBuiltins(builtins)) return false;
Steve Blocka7e24c12009-10-30 11:49:00 +00001562 }
1563
1564 InstallNativeFunctions();
1565
Iain Merrick75681382010-08-19 15:07:18 +01001566 // Store the map for the string prototype after the natives has been compiled
1567 // and the String function has been setup.
1568 Handle<JSFunction> string_function(global_context()->string_function());
1569 ASSERT(JSObject::cast(
1570 string_function->initial_map()->prototype())->HasFastProperties());
1571 global_context()->set_string_function_prototype_map(
1572 HeapObject::cast(string_function->initial_map()->prototype())->map());
1573
Steve Blocka7e24c12009-10-30 11:49:00 +00001574 // Install Function.prototype.call and apply.
Ben Murdoch257744e2011-11-30 15:57:28 +00001575 { Handle<String> key = factory()->function_class_symbol();
Steve Blocka7e24c12009-10-30 11:49:00 +00001576 Handle<JSFunction> function =
Ben Murdoch257744e2011-11-30 15:57:28 +00001577 Handle<JSFunction>::cast(GetProperty(isolate()->global(), key));
Steve Blocka7e24c12009-10-30 11:49:00 +00001578 Handle<JSObject> proto =
1579 Handle<JSObject>(JSObject::cast(function->instance_prototype()));
1580
1581 // Install the call and the apply functions.
1582 Handle<JSFunction> call =
1583 InstallFunction(proto, "call", JS_OBJECT_TYPE, JSObject::kHeaderSize,
Steve Block6ded16b2010-05-10 14:33:55 +01001584 Handle<JSObject>::null(),
Steve Block44f0eee2011-05-26 01:26:41 +01001585 Builtins::kFunctionCall,
Steve Blocka7e24c12009-10-30 11:49:00 +00001586 false);
1587 Handle<JSFunction> apply =
1588 InstallFunction(proto, "apply", JS_OBJECT_TYPE, JSObject::kHeaderSize,
Steve Block6ded16b2010-05-10 14:33:55 +01001589 Handle<JSObject>::null(),
Steve Block44f0eee2011-05-26 01:26:41 +01001590 Builtins::kFunctionApply,
Steve Blocka7e24c12009-10-30 11:49:00 +00001591 false);
1592
1593 // Make sure that Function.prototype.call appears to be compiled.
1594 // The code will never be called, but inline caching for call will
1595 // only work if it appears to be compiled.
1596 call->shared()->DontAdaptArguments();
1597 ASSERT(call->is_compiled());
1598
1599 // Set the expected parameters for apply to 2; required by builtin.
1600 apply->shared()->set_formal_parameter_count(2);
1601
1602 // Set the lengths for the functions to satisfy ECMA-262.
1603 call->shared()->set_length(1);
1604 apply->shared()->set_length(2);
1605 }
1606
Ben Murdoch42effa52011-08-19 16:40:31 +01001607 InstallBuiltinFunctionIds();
1608
Steve Block6ded16b2010-05-10 14:33:55 +01001609 // Create a constructor for RegExp results (a variant of Array that
1610 // predefines the two properties index and match).
1611 {
1612 // RegExpResult initial map.
1613
1614 // Find global.Array.prototype to inherit from.
1615 Handle<JSFunction> array_constructor(global_context()->array_function());
1616 Handle<JSObject> array_prototype(
1617 JSObject::cast(array_constructor->instance_prototype()));
1618
1619 // Add initial map.
1620 Handle<Map> initial_map =
Ben Murdoch257744e2011-11-30 15:57:28 +00001621 factory()->NewMap(JS_ARRAY_TYPE, JSRegExpResult::kSize);
Steve Block6ded16b2010-05-10 14:33:55 +01001622 initial_map->set_constructor(*array_constructor);
1623
1624 // Set prototype on map.
1625 initial_map->set_non_instance_prototype(false);
1626 initial_map->set_prototype(*array_prototype);
1627
1628 // Update map with length accessor from Array and add "index" and "input".
1629 Handle<Map> array_map(global_context()->js_array_map());
1630 Handle<DescriptorArray> array_descriptors(
1631 array_map->instance_descriptors());
1632 ASSERT_EQ(1, array_descriptors->number_of_descriptors());
1633
1634 Handle<DescriptorArray> reresult_descriptors =
Ben Murdoch257744e2011-11-30 15:57:28 +00001635 factory()->NewDescriptorArray(3);
Steve Block6ded16b2010-05-10 14:33:55 +01001636
1637 reresult_descriptors->CopyFrom(0, *array_descriptors, 0);
1638
1639 int enum_index = 0;
1640 {
Ben Murdoch257744e2011-11-30 15:57:28 +00001641 FieldDescriptor index_field(heap()->index_symbol(),
Steve Block6ded16b2010-05-10 14:33:55 +01001642 JSRegExpResult::kIndexIndex,
1643 NONE,
1644 enum_index++);
1645 reresult_descriptors->Set(1, &index_field);
1646 }
1647
1648 {
Ben Murdoch257744e2011-11-30 15:57:28 +00001649 FieldDescriptor input_field(heap()->input_symbol(),
Steve Block6ded16b2010-05-10 14:33:55 +01001650 JSRegExpResult::kInputIndex,
1651 NONE,
1652 enum_index++);
1653 reresult_descriptors->Set(2, &input_field);
1654 }
1655 reresult_descriptors->Sort();
1656
1657 initial_map->set_inobject_properties(2);
1658 initial_map->set_pre_allocated_property_fields(2);
1659 initial_map->set_unused_property_fields(0);
1660 initial_map->set_instance_descriptors(*reresult_descriptors);
1661
1662 global_context()->set_regexp_result_map(*initial_map);
1663 }
1664
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001665
Steve Blocka7e24c12009-10-30 11:49:00 +00001666#ifdef DEBUG
1667 builtins->Verify();
1668#endif
Andrei Popescu31002712010-02-23 13:46:05 +00001669
Steve Blocka7e24c12009-10-30 11:49:00 +00001670 return true;
1671}
1672
1673
Ben Murdoch257744e2011-11-30 15:57:28 +00001674bool Genesis::InstallExperimentalNatives() {
1675 for (int i = ExperimentalNatives::GetDebuggerCount();
1676 i < ExperimentalNatives::GetBuiltinsCount();
1677 i++) {
1678 if (FLAG_harmony_proxies &&
1679 strcmp(ExperimentalNatives::GetScriptName(i).start(),
1680 "native proxy.js") == 0) {
1681 if (!CompileExperimentalBuiltin(isolate(), i)) return false;
1682 }
1683 }
1684
1685 InstallExperimentalNativeFunctions();
1686
1687 return true;
1688}
1689
1690
Ben Murdochb0fe1622011-05-05 13:52:32 +01001691static Handle<JSObject> ResolveBuiltinIdHolder(
Kristian Monsen0d5e1162010-09-30 15:31:59 +01001692 Handle<Context> global_context,
1693 const char* holder_expr) {
Ben Murdoch257744e2011-11-30 15:57:28 +00001694 Factory* factory = global_context->GetIsolate()->factory();
Kristian Monsen0d5e1162010-09-30 15:31:59 +01001695 Handle<GlobalObject> global(global_context->global());
1696 const char* period_pos = strchr(holder_expr, '.');
1697 if (period_pos == NULL) {
1698 return Handle<JSObject>::cast(
Steve Block44f0eee2011-05-26 01:26:41 +01001699 GetProperty(global, factory->LookupAsciiSymbol(holder_expr)));
Steve Block59151502010-09-22 15:07:15 +01001700 }
Kristian Monsen0d5e1162010-09-30 15:31:59 +01001701 ASSERT_EQ(".prototype", period_pos);
1702 Vector<const char> property(holder_expr,
1703 static_cast<int>(period_pos - holder_expr));
1704 Handle<JSFunction> function = Handle<JSFunction>::cast(
Steve Block44f0eee2011-05-26 01:26:41 +01001705 GetProperty(global, factory->LookupSymbol(property)));
Kristian Monsen0d5e1162010-09-30 15:31:59 +01001706 return Handle<JSObject>(JSObject::cast(function->prototype()));
1707}
1708
1709
Ben Murdochb0fe1622011-05-05 13:52:32 +01001710static void InstallBuiltinFunctionId(Handle<JSObject> holder,
1711 const char* function_name,
1712 BuiltinFunctionId id) {
Ben Murdoch257744e2011-11-30 15:57:28 +00001713 Factory* factory = holder->GetIsolate()->factory();
1714 Handle<String> name = factory->LookupAsciiSymbol(function_name);
John Reck59135872010-11-02 12:39:01 -07001715 Object* function_object = holder->GetProperty(*name)->ToObjectUnchecked();
1716 Handle<JSFunction> function(JSFunction::cast(function_object));
Kristian Monsen25f61362010-05-21 11:50:48 +01001717 function->shared()->set_function_data(Smi::FromInt(id));
1718}
1719
1720
Ben Murdochb0fe1622011-05-05 13:52:32 +01001721void Genesis::InstallBuiltinFunctionIds() {
Kristian Monsen25f61362010-05-21 11:50:48 +01001722 HandleScope scope;
Ben Murdochb0fe1622011-05-05 13:52:32 +01001723#define INSTALL_BUILTIN_ID(holder_expr, fun_name, name) \
1724 { \
1725 Handle<JSObject> holder = ResolveBuiltinIdHolder( \
1726 global_context(), #holder_expr); \
1727 BuiltinFunctionId id = k##name; \
1728 InstallBuiltinFunctionId(holder, #fun_name, id); \
Kristian Monsen25f61362010-05-21 11:50:48 +01001729 }
Ben Murdochb0fe1622011-05-05 13:52:32 +01001730 FUNCTIONS_WITH_ID_LIST(INSTALL_BUILTIN_ID)
1731#undef INSTALL_BUILTIN_ID
Kristian Monsen25f61362010-05-21 11:50:48 +01001732}
1733
1734
Steve Block6ded16b2010-05-10 14:33:55 +01001735// Do not forget to update macros.py with named constant
1736// of cache id.
1737#define JSFUNCTION_RESULT_CACHE_LIST(F) \
1738 F(16, global_context()->regexp_function())
1739
1740
Ben Murdoch257744e2011-11-30 15:57:28 +00001741static FixedArray* CreateCache(int size, Handle<JSFunction> factory_function) {
1742 Factory* factory = factory_function->GetIsolate()->factory();
Steve Block6ded16b2010-05-10 14:33:55 +01001743 // Caches are supposed to live for a long time, allocate in old space.
1744 int array_size = JSFunctionResultCache::kEntriesIndex + 2 * size;
1745 // Cannot use cast as object is not fully initialized yet.
1746 JSFunctionResultCache* cache = reinterpret_cast<JSFunctionResultCache*>(
Ben Murdoch257744e2011-11-30 15:57:28 +00001747 *factory->NewFixedArrayWithHoles(array_size, TENURED));
1748 cache->set(JSFunctionResultCache::kFactoryIndex, *factory_function);
Steve Block6ded16b2010-05-10 14:33:55 +01001749 cache->MakeZeroSize();
1750 return cache;
1751}
1752
1753
1754void Genesis::InstallJSFunctionResultCaches() {
1755 const int kNumberOfCaches = 0 +
1756#define F(size, func) + 1
1757 JSFUNCTION_RESULT_CACHE_LIST(F)
1758#undef F
1759 ;
1760
Steve Block44f0eee2011-05-26 01:26:41 +01001761 Handle<FixedArray> caches = FACTORY->NewFixedArray(kNumberOfCaches, TENURED);
Steve Block6ded16b2010-05-10 14:33:55 +01001762
1763 int index = 0;
Kristian Monsen0d5e1162010-09-30 15:31:59 +01001764
Ben Murdoch257744e2011-11-30 15:57:28 +00001765#define F(size, func) do { \
1766 FixedArray* cache = CreateCache((size), Handle<JSFunction>(func)); \
1767 caches->set(index++, cache); \
Kristian Monsen0d5e1162010-09-30 15:31:59 +01001768 } while (false)
1769
1770 JSFUNCTION_RESULT_CACHE_LIST(F);
1771
Steve Block6ded16b2010-05-10 14:33:55 +01001772#undef F
1773
1774 global_context()->set_jsfunction_result_caches(*caches);
1775}
1776
1777
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001778void Genesis::InitializeNormalizedMapCaches() {
1779 Handle<FixedArray> array(
Steve Block44f0eee2011-05-26 01:26:41 +01001780 FACTORY->NewFixedArray(NormalizedMapCache::kEntries, TENURED));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001781 global_context()->set_normalized_map_cache(NormalizedMapCache::cast(*array));
1782}
1783
1784
Andrei Popescu31002712010-02-23 13:46:05 +00001785bool Bootstrapper::InstallExtensions(Handle<Context> global_context,
1786 v8::ExtensionConfiguration* extensions) {
Ben Murdoch257744e2011-11-30 15:57:28 +00001787 Isolate* isolate = global_context->GetIsolate();
Andrei Popescu31002712010-02-23 13:46:05 +00001788 BootstrapperActive active;
Steve Block44f0eee2011-05-26 01:26:41 +01001789 SaveContext saved_context(isolate);
1790 isolate->set_context(*global_context);
Andrei Popescu31002712010-02-23 13:46:05 +00001791 if (!Genesis::InstallExtensions(global_context, extensions)) return false;
1792 Genesis::InstallSpecialObjects(global_context);
1793 return true;
1794}
1795
1796
1797void Genesis::InstallSpecialObjects(Handle<Context> global_context) {
Ben Murdoch257744e2011-11-30 15:57:28 +00001798 Factory* factory = global_context->GetIsolate()->factory();
Steve Blocka7e24c12009-10-30 11:49:00 +00001799 HandleScope scope;
1800 Handle<JSGlobalObject> js_global(
Andrei Popescu31002712010-02-23 13:46:05 +00001801 JSGlobalObject::cast(global_context->global()));
Steve Blocka7e24c12009-10-30 11:49:00 +00001802 // Expose the natives in global if a name for it is specified.
1803 if (FLAG_expose_natives_as != NULL && strlen(FLAG_expose_natives_as) != 0) {
1804 Handle<String> natives_string =
Steve Block44f0eee2011-05-26 01:26:41 +01001805 factory->LookupAsciiSymbol(FLAG_expose_natives_as);
Steve Block1e0659c2011-05-24 12:43:12 +01001806 SetLocalPropertyNoThrow(js_global, natives_string,
1807 Handle<JSObject>(js_global->builtins()), DONT_ENUM);
Steve Blocka7e24c12009-10-30 11:49:00 +00001808 }
1809
1810 Handle<Object> Error = GetProperty(js_global, "Error");
1811 if (Error->IsJSObject()) {
Steve Block44f0eee2011-05-26 01:26:41 +01001812 Handle<String> name = factory->LookupAsciiSymbol("stackTraceLimit");
Steve Block1e0659c2011-05-24 12:43:12 +01001813 SetLocalPropertyNoThrow(Handle<JSObject>::cast(Error),
1814 name,
1815 Handle<Smi>(Smi::FromInt(FLAG_stack_trace_limit)),
1816 NONE);
Steve Blocka7e24c12009-10-30 11:49:00 +00001817 }
1818
1819#ifdef ENABLE_DEBUGGER_SUPPORT
1820 // Expose the debug global object in global if a name for it is specified.
1821 if (FLAG_expose_debug_as != NULL && strlen(FLAG_expose_debug_as) != 0) {
Steve Block44f0eee2011-05-26 01:26:41 +01001822 Debug* debug = Isolate::Current()->debug();
Steve Blocka7e24c12009-10-30 11:49:00 +00001823 // If loading fails we just bail out without installing the
1824 // debugger but without tanking the whole context.
Steve Block44f0eee2011-05-26 01:26:41 +01001825 if (!debug->Load()) return;
Steve Blocka7e24c12009-10-30 11:49:00 +00001826 // Set the security token for the debugger context to the same as
1827 // the shell global context to allow calling between these (otherwise
1828 // exposing debug global object doesn't make much sense).
Steve Block44f0eee2011-05-26 01:26:41 +01001829 debug->debug_context()->set_security_token(
Andrei Popescu31002712010-02-23 13:46:05 +00001830 global_context->security_token());
Steve Blocka7e24c12009-10-30 11:49:00 +00001831
1832 Handle<String> debug_string =
Steve Block44f0eee2011-05-26 01:26:41 +01001833 factory->LookupAsciiSymbol(FLAG_expose_debug_as);
1834 Handle<Object> global_proxy(debug->debug_context()->global_proxy());
Steve Block1e0659c2011-05-24 12:43:12 +01001835 SetLocalPropertyNoThrow(js_global, debug_string, global_proxy, DONT_ENUM);
Steve Blocka7e24c12009-10-30 11:49:00 +00001836 }
1837#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00001838}
1839
1840
Andrei Popescu31002712010-02-23 13:46:05 +00001841bool Genesis::InstallExtensions(Handle<Context> global_context,
1842 v8::ExtensionConfiguration* extensions) {
Steve Block44f0eee2011-05-26 01:26:41 +01001843 // TODO(isolates): Extensions on multiple isolates may take a little more
1844 // effort. (The external API reads 'ignore'-- does that mean
1845 // we can break the interface?)
1846
Steve Blocka7e24c12009-10-30 11:49:00 +00001847 // Clear coloring of extension list
1848 v8::RegisteredExtension* current = v8::RegisteredExtension::first_extension();
1849 while (current != NULL) {
1850 current->set_state(v8::UNVISITED);
1851 current = current->next();
1852 }
Andrei Popescu31002712010-02-23 13:46:05 +00001853 // Install auto extensions.
Steve Blocka7e24c12009-10-30 11:49:00 +00001854 current = v8::RegisteredExtension::first_extension();
1855 while (current != NULL) {
1856 if (current->extension()->auto_enable())
1857 InstallExtension(current);
1858 current = current->next();
1859 }
1860
1861 if (FLAG_expose_gc) InstallExtension("v8/gc");
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01001862 if (FLAG_expose_externalize_string) InstallExtension("v8/externalize");
Steve Blocka7e24c12009-10-30 11:49:00 +00001863
1864 if (extensions == NULL) return true;
1865 // Install required extensions
1866 int count = v8::ImplementationUtilities::GetNameCount(extensions);
1867 const char** names = v8::ImplementationUtilities::GetNames(extensions);
1868 for (int i = 0; i < count; i++) {
1869 if (!InstallExtension(names[i]))
1870 return false;
1871 }
1872
1873 return true;
1874}
1875
1876
1877// Installs a named extension. This methods is unoptimized and does
1878// not scale well if we want to support a large number of extensions.
1879bool Genesis::InstallExtension(const char* name) {
1880 v8::RegisteredExtension* current = v8::RegisteredExtension::first_extension();
1881 // Loop until we find the relevant extension
1882 while (current != NULL) {
1883 if (strcmp(name, current->extension()->name()) == 0) break;
1884 current = current->next();
1885 }
1886 // Didn't find the extension; fail.
1887 if (current == NULL) {
1888 v8::Utils::ReportApiFailure(
1889 "v8::Context::New()", "Cannot find required extension");
1890 return false;
1891 }
1892 return InstallExtension(current);
1893}
1894
1895
1896bool Genesis::InstallExtension(v8::RegisteredExtension* current) {
1897 HandleScope scope;
1898
1899 if (current->state() == v8::INSTALLED) return true;
1900 // The current node has already been visited so there must be a
1901 // cycle in the dependency graph; fail.
1902 if (current->state() == v8::VISITED) {
1903 v8::Utils::ReportApiFailure(
1904 "v8::Context::New()", "Circular extension dependency");
1905 return false;
1906 }
1907 ASSERT(current->state() == v8::UNVISITED);
1908 current->set_state(v8::VISITED);
1909 v8::Extension* extension = current->extension();
1910 // Install the extension's dependencies
1911 for (int i = 0; i < extension->dependency_count(); i++) {
1912 if (!InstallExtension(extension->dependencies()[i])) return false;
1913 }
Steve Block44f0eee2011-05-26 01:26:41 +01001914 Isolate* isolate = Isolate::Current();
Steve Blocka7e24c12009-10-30 11:49:00 +00001915 Vector<const char> source = CStrVector(extension->source());
Steve Block44f0eee2011-05-26 01:26:41 +01001916 Handle<String> source_code = isolate->factory()->NewStringFromAscii(source);
Steve Blocka7e24c12009-10-30 11:49:00 +00001917 bool result = CompileScriptCached(CStrVector(extension->name()),
1918 source_code,
Steve Block44f0eee2011-05-26 01:26:41 +01001919 isolate->bootstrapper()->extensions_cache(),
Andrei Popescu31002712010-02-23 13:46:05 +00001920 extension,
Steve Block44f0eee2011-05-26 01:26:41 +01001921 Handle<Context>(isolate->context()),
Steve Blocka7e24c12009-10-30 11:49:00 +00001922 false);
Steve Block44f0eee2011-05-26 01:26:41 +01001923 ASSERT(isolate->has_pending_exception() != result);
Steve Blocka7e24c12009-10-30 11:49:00 +00001924 if (!result) {
Steve Block44f0eee2011-05-26 01:26:41 +01001925 isolate->clear_pending_exception();
Steve Blocka7e24c12009-10-30 11:49:00 +00001926 }
1927 current->set_state(v8::INSTALLED);
1928 return result;
1929}
1930
1931
Andrei Popescu402d9372010-02-26 13:31:12 +00001932bool Genesis::InstallJSBuiltins(Handle<JSBuiltinsObject> builtins) {
1933 HandleScope scope;
Ben Murdoch257744e2011-11-30 15:57:28 +00001934 Factory* factory = builtins->GetIsolate()->factory();
Andrei Popescu402d9372010-02-26 13:31:12 +00001935 for (int i = 0; i < Builtins::NumberOfJavaScriptBuiltins(); i++) {
1936 Builtins::JavaScript id = static_cast<Builtins::JavaScript>(i);
Ben Murdoch257744e2011-11-30 15:57:28 +00001937 Handle<String> name = factory->LookupAsciiSymbol(Builtins::GetName(id));
John Reck59135872010-11-02 12:39:01 -07001938 Object* function_object = builtins->GetPropertyNoExceptionThrown(*name);
Andrei Popescu402d9372010-02-26 13:31:12 +00001939 Handle<JSFunction> function
John Reck59135872010-11-02 12:39:01 -07001940 = Handle<JSFunction>(JSFunction::cast(function_object));
Andrei Popescu402d9372010-02-26 13:31:12 +00001941 builtins->set_javascript_builtin(id, *function);
1942 Handle<SharedFunctionInfo> shared
1943 = Handle<SharedFunctionInfo>(function->shared());
1944 if (!EnsureCompiled(shared, CLEAR_EXCEPTION)) return false;
Iain Merrick75681382010-08-19 15:07:18 +01001945 // Set the code object on the function object.
Ben Murdochb0fe1622011-05-05 13:52:32 +01001946 function->ReplaceCode(function->shared()->code());
Steve Block6ded16b2010-05-10 14:33:55 +01001947 builtins->set_javascript_builtin_code(id, shared->code());
Andrei Popescu402d9372010-02-26 13:31:12 +00001948 }
1949 return true;
Andrei Popescu31002712010-02-23 13:46:05 +00001950}
1951
1952
Steve Blocka7e24c12009-10-30 11:49:00 +00001953bool Genesis::ConfigureGlobalObjects(
1954 v8::Handle<v8::ObjectTemplate> global_proxy_template) {
1955 Handle<JSObject> global_proxy(
1956 JSObject::cast(global_context()->global_proxy()));
Andrei Popescu402d9372010-02-26 13:31:12 +00001957 Handle<JSObject> inner_global(JSObject::cast(global_context()->global()));
Steve Blocka7e24c12009-10-30 11:49:00 +00001958
1959 if (!global_proxy_template.IsEmpty()) {
1960 // Configure the global proxy object.
1961 Handle<ObjectTemplateInfo> proxy_data =
1962 v8::Utils::OpenHandle(*global_proxy_template);
1963 if (!ConfigureApiObject(global_proxy, proxy_data)) return false;
1964
1965 // Configure the inner global object.
1966 Handle<FunctionTemplateInfo> proxy_constructor(
1967 FunctionTemplateInfo::cast(proxy_data->constructor()));
1968 if (!proxy_constructor->prototype_template()->IsUndefined()) {
1969 Handle<ObjectTemplateInfo> inner_data(
1970 ObjectTemplateInfo::cast(proxy_constructor->prototype_template()));
Andrei Popescu402d9372010-02-26 13:31:12 +00001971 if (!ConfigureApiObject(inner_global, inner_data)) return false;
Steve Blocka7e24c12009-10-30 11:49:00 +00001972 }
1973 }
1974
Andrei Popescu402d9372010-02-26 13:31:12 +00001975 SetObjectPrototype(global_proxy, inner_global);
Steve Blocka7e24c12009-10-30 11:49:00 +00001976 return true;
1977}
1978
1979
1980bool Genesis::ConfigureApiObject(Handle<JSObject> object,
1981 Handle<ObjectTemplateInfo> object_template) {
1982 ASSERT(!object_template.is_null());
1983 ASSERT(object->IsInstanceOf(
1984 FunctionTemplateInfo::cast(object_template->constructor())));
1985
1986 bool pending_exception = false;
1987 Handle<JSObject> obj =
1988 Execution::InstantiateObject(object_template, &pending_exception);
1989 if (pending_exception) {
Ben Murdoch257744e2011-11-30 15:57:28 +00001990 ASSERT(isolate()->has_pending_exception());
1991 isolate()->clear_pending_exception();
Steve Blocka7e24c12009-10-30 11:49:00 +00001992 return false;
1993 }
1994 TransferObject(obj, object);
1995 return true;
1996}
1997
1998
1999void Genesis::TransferNamedProperties(Handle<JSObject> from,
2000 Handle<JSObject> to) {
2001 if (from->HasFastProperties()) {
2002 Handle<DescriptorArray> descs =
2003 Handle<DescriptorArray>(from->map()->instance_descriptors());
2004 for (int i = 0; i < descs->number_of_descriptors(); i++) {
2005 PropertyDetails details = PropertyDetails(descs->GetDetails(i));
2006 switch (details.type()) {
2007 case FIELD: {
2008 HandleScope inner;
2009 Handle<String> key = Handle<String>(descs->GetKey(i));
2010 int index = descs->GetFieldIndex(i);
2011 Handle<Object> value = Handle<Object>(from->FastPropertyAt(index));
Steve Block1e0659c2011-05-24 12:43:12 +01002012 SetLocalPropertyNoThrow(to, key, value, details.attributes());
Steve Blocka7e24c12009-10-30 11:49:00 +00002013 break;
2014 }
2015 case CONSTANT_FUNCTION: {
2016 HandleScope inner;
2017 Handle<String> key = Handle<String>(descs->GetKey(i));
2018 Handle<JSFunction> fun =
2019 Handle<JSFunction>(descs->GetConstantFunction(i));
Steve Block1e0659c2011-05-24 12:43:12 +01002020 SetLocalPropertyNoThrow(to, key, fun, details.attributes());
Steve Blocka7e24c12009-10-30 11:49:00 +00002021 break;
2022 }
2023 case CALLBACKS: {
2024 LookupResult result;
2025 to->LocalLookup(descs->GetKey(i), &result);
2026 // If the property is already there we skip it
Andrei Popescu402d9372010-02-26 13:31:12 +00002027 if (result.IsProperty()) continue;
Steve Blocka7e24c12009-10-30 11:49:00 +00002028 HandleScope inner;
Andrei Popescu31002712010-02-23 13:46:05 +00002029 ASSERT(!to->HasFastProperties());
2030 // Add to dictionary.
Steve Blocka7e24c12009-10-30 11:49:00 +00002031 Handle<String> key = Handle<String>(descs->GetKey(i));
Andrei Popescu31002712010-02-23 13:46:05 +00002032 Handle<Object> callbacks(descs->GetCallbacksObject(i));
2033 PropertyDetails d =
2034 PropertyDetails(details.attributes(), CALLBACKS, details.index());
2035 SetNormalizedProperty(to, key, callbacks, d);
Steve Blocka7e24c12009-10-30 11:49:00 +00002036 break;
2037 }
2038 case MAP_TRANSITION:
Steve Block44f0eee2011-05-26 01:26:41 +01002039 case EXTERNAL_ARRAY_TRANSITION:
Steve Blocka7e24c12009-10-30 11:49:00 +00002040 case CONSTANT_TRANSITION:
2041 case NULL_DESCRIPTOR:
2042 // Ignore non-properties.
2043 break;
2044 case NORMAL:
2045 // Do not occur since the from object has fast properties.
Ben Murdoch257744e2011-11-30 15:57:28 +00002046 case HANDLER:
Steve Blocka7e24c12009-10-30 11:49:00 +00002047 case INTERCEPTOR:
Ben Murdoch257744e2011-11-30 15:57:28 +00002048 // No element in instance descriptors have proxy or interceptor type.
Steve Blocka7e24c12009-10-30 11:49:00 +00002049 UNREACHABLE();
2050 break;
2051 }
2052 }
2053 } else {
2054 Handle<StringDictionary> properties =
2055 Handle<StringDictionary>(from->property_dictionary());
2056 int capacity = properties->Capacity();
2057 for (int i = 0; i < capacity; i++) {
2058 Object* raw_key(properties->KeyAt(i));
2059 if (properties->IsKey(raw_key)) {
2060 ASSERT(raw_key->IsString());
2061 // If the property is already there we skip it.
2062 LookupResult result;
2063 to->LocalLookup(String::cast(raw_key), &result);
Andrei Popescu402d9372010-02-26 13:31:12 +00002064 if (result.IsProperty()) continue;
Steve Blocka7e24c12009-10-30 11:49:00 +00002065 // Set the property.
2066 Handle<String> key = Handle<String>(String::cast(raw_key));
2067 Handle<Object> value = Handle<Object>(properties->ValueAt(i));
2068 if (value->IsJSGlobalPropertyCell()) {
2069 value = Handle<Object>(JSGlobalPropertyCell::cast(*value)->value());
2070 }
2071 PropertyDetails details = properties->DetailsAt(i);
Steve Block1e0659c2011-05-24 12:43:12 +01002072 SetLocalPropertyNoThrow(to, key, value, details.attributes());
Steve Blocka7e24c12009-10-30 11:49:00 +00002073 }
2074 }
2075 }
2076}
2077
2078
2079void Genesis::TransferIndexedProperties(Handle<JSObject> from,
2080 Handle<JSObject> to) {
2081 // Cloning the elements array is sufficient.
2082 Handle<FixedArray> from_elements =
2083 Handle<FixedArray>(FixedArray::cast(from->elements()));
Steve Block44f0eee2011-05-26 01:26:41 +01002084 Handle<FixedArray> to_elements = FACTORY->CopyFixedArray(from_elements);
Steve Blocka7e24c12009-10-30 11:49:00 +00002085 to->set_elements(*to_elements);
2086}
2087
2088
2089void Genesis::TransferObject(Handle<JSObject> from, Handle<JSObject> to) {
2090 HandleScope outer;
Ben Murdoch257744e2011-11-30 15:57:28 +00002091 Factory* factory = from->GetIsolate()->factory();
Steve Blocka7e24c12009-10-30 11:49:00 +00002092
2093 ASSERT(!from->IsJSArray());
2094 ASSERT(!to->IsJSArray());
2095
2096 TransferNamedProperties(from, to);
2097 TransferIndexedProperties(from, to);
2098
2099 // Transfer the prototype (new map is needed).
2100 Handle<Map> old_to_map = Handle<Map>(to->map());
Ben Murdoch257744e2011-11-30 15:57:28 +00002101 Handle<Map> new_to_map = factory->CopyMapDropTransitions(old_to_map);
Steve Blocka7e24c12009-10-30 11:49:00 +00002102 new_to_map->set_prototype(from->map()->prototype());
2103 to->set_map(*new_to_map);
2104}
2105
2106
2107void Genesis::MakeFunctionInstancePrototypeWritable() {
Steve Block44f0eee2011-05-26 01:26:41 +01002108 // The maps with writable prototype are created in CreateEmptyFunction
2109 // and CreateStrictModeFunctionMaps respectively. Initially the maps are
2110 // created with read-only prototype for JS builtins processing.
2111 ASSERT(!function_instance_map_writable_prototype_.is_null());
2112 ASSERT(!strict_mode_function_instance_map_writable_prototype_.is_null());
Steve Blocka7e24c12009-10-30 11:49:00 +00002113
Steve Block44f0eee2011-05-26 01:26:41 +01002114 // Replace function instance maps to make prototype writable.
2115 global_context()->set_function_map(
2116 *function_instance_map_writable_prototype_);
2117 global_context()->set_strict_mode_function_map(
2118 *strict_mode_function_instance_map_writable_prototype_);
Steve Blocka7e24c12009-10-30 11:49:00 +00002119}
2120
2121
Ben Murdoch257744e2011-11-30 15:57:28 +00002122Genesis::Genesis(Isolate* isolate,
2123 Handle<Object> global_object,
Steve Blocka7e24c12009-10-30 11:49:00 +00002124 v8::Handle<v8::ObjectTemplate> global_template,
Ben Murdoch257744e2011-11-30 15:57:28 +00002125 v8::ExtensionConfiguration* extensions) : isolate_(isolate) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002126 result_ = Handle<Context>::null();
Steve Blocka7e24c12009-10-30 11:49:00 +00002127 // If V8 isn't running and cannot be initialized, just return.
2128 if (!V8::IsRunning() && !V8::Initialize(NULL)) return;
2129
2130 // Before creating the roots we must save the context and restore it
2131 // on all function exits.
2132 HandleScope scope;
Steve Block44f0eee2011-05-26 01:26:41 +01002133 SaveContext saved_context(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00002134
Andrei Popescu31002712010-02-23 13:46:05 +00002135 Handle<Context> new_context = Snapshot::NewContextFromSnapshot();
2136 if (!new_context.is_null()) {
2137 global_context_ =
Steve Block44f0eee2011-05-26 01:26:41 +01002138 Handle<Context>::cast(isolate->global_handles()->Create(*new_context));
Ben Murdochb0fe1622011-05-05 13:52:32 +01002139 AddToWeakGlobalContextList(*global_context_);
Steve Block44f0eee2011-05-26 01:26:41 +01002140 isolate->set_context(*global_context_);
2141 isolate->counters()->contexts_created_by_snapshot()->Increment();
Andrei Popescu402d9372010-02-26 13:31:12 +00002142 Handle<GlobalObject> inner_global;
Andrei Popescu31002712010-02-23 13:46:05 +00002143 Handle<JSGlobalProxy> global_proxy =
2144 CreateNewGlobals(global_template,
2145 global_object,
Andrei Popescu402d9372010-02-26 13:31:12 +00002146 &inner_global);
2147
Andrei Popescu31002712010-02-23 13:46:05 +00002148 HookUpGlobalProxy(inner_global, global_proxy);
Andrei Popescu402d9372010-02-26 13:31:12 +00002149 HookUpInnerGlobal(inner_global);
2150
Andrei Popescu31002712010-02-23 13:46:05 +00002151 if (!ConfigureGlobalObjects(global_template)) return;
2152 } else {
2153 // We get here if there was no context snapshot.
2154 CreateRoots();
Ben Murdoch257744e2011-11-30 15:57:28 +00002155 Handle<JSFunction> empty_function = CreateEmptyFunction(isolate);
Steve Block44f0eee2011-05-26 01:26:41 +01002156 CreateStrictModeFunctionMaps(empty_function);
Andrei Popescu31002712010-02-23 13:46:05 +00002157 Handle<GlobalObject> inner_global;
2158 Handle<JSGlobalProxy> global_proxy =
2159 CreateNewGlobals(global_template, global_object, &inner_global);
2160 HookUpGlobalProxy(inner_global, global_proxy);
2161 InitializeGlobal(inner_global, empty_function);
Steve Block6ded16b2010-05-10 14:33:55 +01002162 InstallJSFunctionResultCaches();
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002163 InitializeNormalizedMapCaches();
Kristian Monsen25f61362010-05-21 11:50:48 +01002164 if (!InstallNatives()) return;
Steve Blocka7e24c12009-10-30 11:49:00 +00002165
Andrei Popescu31002712010-02-23 13:46:05 +00002166 MakeFunctionInstancePrototypeWritable();
Steve Blocka7e24c12009-10-30 11:49:00 +00002167
Andrei Popescu31002712010-02-23 13:46:05 +00002168 if (!ConfigureGlobalObjects(global_template)) return;
Steve Block44f0eee2011-05-26 01:26:41 +01002169 isolate->counters()->contexts_created_from_scratch()->Increment();
Andrei Popescu31002712010-02-23 13:46:05 +00002170 }
Steve Blocka7e24c12009-10-30 11:49:00 +00002171
Ben Murdoch257744e2011-11-30 15:57:28 +00002172 // Install experimental natives.
2173 if (!InstallExperimentalNatives()) return;
2174
Steve Blocka7e24c12009-10-30 11:49:00 +00002175 result_ = global_context_;
2176}
2177
2178
2179// Support for thread preemption.
2180
2181// Reserve space for statics needing saving and restoring.
2182int Bootstrapper::ArchiveSpacePerThread() {
Steve Block44f0eee2011-05-26 01:26:41 +01002183 return sizeof(NestingCounterType);
Steve Blocka7e24c12009-10-30 11:49:00 +00002184}
2185
2186
2187// Archive statics that are thread local.
2188char* Bootstrapper::ArchiveState(char* to) {
Steve Block44f0eee2011-05-26 01:26:41 +01002189 *reinterpret_cast<NestingCounterType*>(to) = nesting_;
2190 nesting_ = 0;
2191 return to + sizeof(NestingCounterType);
Steve Blocka7e24c12009-10-30 11:49:00 +00002192}
2193
2194
2195// Restore statics that are thread local.
2196char* Bootstrapper::RestoreState(char* from) {
Steve Block44f0eee2011-05-26 01:26:41 +01002197 nesting_ = *reinterpret_cast<NestingCounterType*>(from);
2198 return from + sizeof(NestingCounterType);
Steve Blocka7e24c12009-10-30 11:49:00 +00002199}
2200
2201
2202// Called when the top-level V8 mutex is destroyed.
2203void Bootstrapper::FreeThreadResources() {
Steve Block44f0eee2011-05-26 01:26:41 +01002204 ASSERT(!IsActive());
Steve Blocka7e24c12009-10-30 11:49:00 +00002205}
2206
2207} } // namespace v8::internal