blob: 250562a2aa17a0a8c47dfc8567b449b5ccade0f0 [file] [log] [blame]
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001// Copyright 2014 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
Steve Blocka7e24c12009-10-30 11:49:00 +00004
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005#include "src/bootstrapper.h"
Steve Blocka7e24c12009-10-30 11:49:00 +00006
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007#include "src/accessors.h"
8#include "src/code-stubs.h"
9#include "src/extensions/externalize-string-extension.h"
10#include "src/extensions/free-buffer-extension.h"
11#include "src/extensions/gc-extension.h"
12#include "src/extensions/statistics-extension.h"
13#include "src/extensions/trigger-failure-extension.h"
14#include "src/isolate-inl.h"
15#include "src/natives.h"
16#include "src/snapshot.h"
17#include "third_party/fdlibm/fdlibm.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000018
19namespace v8 {
20namespace internal {
21
Steve Block44f0eee2011-05-26 01:26:41 +010022NativesExternalStringResource::NativesExternalStringResource(
23 Bootstrapper* bootstrapper,
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000024 const char* source,
25 size_t length)
26 : data_(source), length_(length) {
Steve Block44f0eee2011-05-26 01:26:41 +010027 if (bootstrapper->delete_these_non_arrays_on_tear_down_ == NULL) {
28 bootstrapper->delete_these_non_arrays_on_tear_down_ = new List<char*>(2);
Steve Blockd0582a62009-12-15 09:54:21 +000029 }
30 // The resources are small objects and we only make a fixed number of
31 // them, but let's clean them up on exit for neatness.
Steve Block44f0eee2011-05-26 01:26:41 +010032 bootstrapper->delete_these_non_arrays_on_tear_down_->
Steve Blockd0582a62009-12-15 09:54:21 +000033 Add(reinterpret_cast<char*>(this));
34}
Steve Blocka7e24c12009-10-30 11:49:00 +000035
36
Ben Murdochb8a8cc12014-11-26 15:28:44 +000037Bootstrapper::Bootstrapper(Isolate* isolate)
38 : isolate_(isolate),
39 nesting_(0),
Steve Block44f0eee2011-05-26 01:26:41 +010040 extensions_cache_(Script::TYPE_EXTENSION),
41 delete_these_non_arrays_on_tear_down_(NULL),
42 delete_these_arrays_on_tear_down_(NULL) {
43}
44
45
Steve Blocka7e24c12009-10-30 11:49:00 +000046Handle<String> Bootstrapper::NativesSourceLookup(int index) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000047 DCHECK(0 <= index && index < Natives::GetBuiltinsCount());
48 Heap* heap = isolate_->heap();
Steve Block44f0eee2011-05-26 01:26:41 +010049 if (heap->natives_source_cache()->get(index)->IsUndefined()) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +010050 // We can use external strings for the natives.
51 Vector<const char> source = Natives::GetRawScriptSource(index);
52 NativesExternalStringResource* resource =
53 new NativesExternalStringResource(this,
54 source.start(),
55 source.length());
Ben Murdochb8a8cc12014-11-26 15:28:44 +000056 // We do not expect this to throw an exception. Change this if it does.
57 Handle<String> source_code = isolate_->factory()
58 ->NewExternalStringFromOneByte(resource)
59 .ToHandleChecked();
Ben Murdoch3ef787d2012-04-12 10:51:47 +010060 heap->natives_source_cache()->set(index, *source_code);
Steve Blocka7e24c12009-10-30 11:49:00 +000061 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +000062 Handle<Object> cached_source(heap->natives_source_cache()->get(index),
63 isolate_);
Steve Blocka7e24c12009-10-30 11:49:00 +000064 return Handle<String>::cast(cached_source);
65}
66
67
Steve Blocka7e24c12009-10-30 11:49:00 +000068void Bootstrapper::Initialize(bool create_heap_objects) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000069 extensions_cache_.Initialize(isolate_, create_heap_objects);
70}
71
72
73static const char* GCFunctionName() {
74 bool flag_given = FLAG_expose_gc_as != NULL && strlen(FLAG_expose_gc_as) != 0;
75 return flag_given ? FLAG_expose_gc_as : "gc";
76}
77
78
79v8::Extension* Bootstrapper::free_buffer_extension_ = NULL;
80v8::Extension* Bootstrapper::gc_extension_ = NULL;
81v8::Extension* Bootstrapper::externalize_string_extension_ = NULL;
82v8::Extension* Bootstrapper::statistics_extension_ = NULL;
83v8::Extension* Bootstrapper::trigger_failure_extension_ = NULL;
84
85
86void Bootstrapper::InitializeOncePerProcess() {
87 free_buffer_extension_ = new FreeBufferExtension;
88 v8::RegisterExtension(free_buffer_extension_);
89 gc_extension_ = new GCExtension(GCFunctionName());
90 v8::RegisterExtension(gc_extension_);
91 externalize_string_extension_ = new ExternalizeStringExtension;
92 v8::RegisterExtension(externalize_string_extension_);
93 statistics_extension_ = new StatisticsExtension;
94 v8::RegisterExtension(statistics_extension_);
95 trigger_failure_extension_ = new TriggerFailureExtension;
96 v8::RegisterExtension(trigger_failure_extension_);
97}
98
99
100void Bootstrapper::TearDownExtensions() {
101 delete free_buffer_extension_;
102 free_buffer_extension_ = NULL;
103 delete gc_extension_;
104 gc_extension_ = NULL;
105 delete externalize_string_extension_;
106 externalize_string_extension_ = NULL;
107 delete statistics_extension_;
108 statistics_extension_ = NULL;
109 delete trigger_failure_extension_;
110 trigger_failure_extension_ = NULL;
Steve Blocka7e24c12009-10-30 11:49:00 +0000111}
112
113
Leon Clarkee46be812010-01-19 14:06:41 +0000114char* Bootstrapper::AllocateAutoDeletedArray(int bytes) {
115 char* memory = new char[bytes];
116 if (memory != NULL) {
Steve Block44f0eee2011-05-26 01:26:41 +0100117 if (delete_these_arrays_on_tear_down_ == NULL) {
118 delete_these_arrays_on_tear_down_ = new List<char*>(2);
Leon Clarkee46be812010-01-19 14:06:41 +0000119 }
Steve Block44f0eee2011-05-26 01:26:41 +0100120 delete_these_arrays_on_tear_down_->Add(memory);
Leon Clarkee46be812010-01-19 14:06:41 +0000121 }
122 return memory;
123}
124
125
Steve Blocka7e24c12009-10-30 11:49:00 +0000126void Bootstrapper::TearDown() {
Steve Block44f0eee2011-05-26 01:26:41 +0100127 if (delete_these_non_arrays_on_tear_down_ != NULL) {
128 int len = delete_these_non_arrays_on_tear_down_->length();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000129 DCHECK(len < 28); // Don't use this mechanism for unbounded allocations.
Steve Blockd0582a62009-12-15 09:54:21 +0000130 for (int i = 0; i < len; i++) {
Steve Block44f0eee2011-05-26 01:26:41 +0100131 delete delete_these_non_arrays_on_tear_down_->at(i);
132 delete_these_non_arrays_on_tear_down_->at(i) = NULL;
Steve Blockd0582a62009-12-15 09:54:21 +0000133 }
Steve Block44f0eee2011-05-26 01:26:41 +0100134 delete delete_these_non_arrays_on_tear_down_;
135 delete_these_non_arrays_on_tear_down_ = NULL;
Steve Blockd0582a62009-12-15 09:54:21 +0000136 }
137
Steve Block44f0eee2011-05-26 01:26:41 +0100138 if (delete_these_arrays_on_tear_down_ != NULL) {
139 int len = delete_these_arrays_on_tear_down_->length();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000140 DCHECK(len < 1000); // Don't use this mechanism for unbounded allocations.
Leon Clarkee46be812010-01-19 14:06:41 +0000141 for (int i = 0; i < len; i++) {
Steve Block44f0eee2011-05-26 01:26:41 +0100142 delete[] delete_these_arrays_on_tear_down_->at(i);
143 delete_these_arrays_on_tear_down_->at(i) = NULL;
Leon Clarkee46be812010-01-19 14:06:41 +0000144 }
Steve Block44f0eee2011-05-26 01:26:41 +0100145 delete delete_these_arrays_on_tear_down_;
146 delete_these_arrays_on_tear_down_ = NULL;
Leon Clarkee46be812010-01-19 14:06:41 +0000147 }
148
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000149 extensions_cache_.Initialize(isolate_, false); // Yes, symmetrical
Steve Blocka7e24c12009-10-30 11:49:00 +0000150}
151
152
Steve Blocka7e24c12009-10-30 11:49:00 +0000153class Genesis BASE_EMBEDDED {
154 public:
Ben Murdoch257744e2011-11-30 15:57:28 +0000155 Genesis(Isolate* isolate,
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000156 MaybeHandle<JSGlobalProxy> maybe_global_proxy,
157 v8::Handle<v8::ObjectTemplate> global_proxy_template,
Steve Blocka7e24c12009-10-30 11:49:00 +0000158 v8::ExtensionConfiguration* extensions);
Andrei Popescu31002712010-02-23 13:46:05 +0000159 ~Genesis() { }
Steve Blocka7e24c12009-10-30 11:49:00 +0000160
Ben Murdoch257744e2011-11-30 15:57:28 +0000161 Isolate* isolate() const { return isolate_; }
162 Factory* factory() const { return isolate_->factory(); }
163 Heap* heap() const { return isolate_->heap(); }
164
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000165 Handle<Context> result() { return result_; }
166
Steve Blocka7e24c12009-10-30 11:49:00 +0000167 private:
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000168 Handle<Context> native_context() { return native_context_; }
Steve Blocka7e24c12009-10-30 11:49:00 +0000169
Andrei Popescu31002712010-02-23 13:46:05 +0000170 // Creates some basic objects. Used for creating a context from scratch.
171 void CreateRoots();
172 // Creates the empty function. Used for creating a context from scratch.
Ben Murdoch257744e2011-11-30 15:57:28 +0000173 Handle<JSFunction> CreateEmptyFunction(Isolate* isolate);
Steve Block44f0eee2011-05-26 01:26:41 +0100174 // Creates the ThrowTypeError function. ECMA 5th Ed. 13.2.3
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000175 Handle<JSFunction> GetStrictPoisonFunction();
176 // Poison for sloppy generator function arguments/callee.
177 Handle<JSFunction> GetGeneratorPoisonFunction();
Steve Block44f0eee2011-05-26 01:26:41 +0100178
179 void CreateStrictModeFunctionMaps(Handle<JSFunction> empty);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100180
181 // Make the "arguments" and "caller" properties throw a TypeError on access.
182 void PoisonArgumentsAndCaller(Handle<Map> map);
183
Andrei Popescu31002712010-02-23 13:46:05 +0000184 // Creates the global objects using the global and the template passed in
185 // through the API. We call this regardless of whether we are building a
186 // context from scratch or using a deserialized one from the partial snapshot
187 // but in the latter case we don't use the objects it produces directly, as
188 // we have to used the deserialized ones that are linked together with the
189 // rest of the context snapshot.
190 Handle<JSGlobalProxy> CreateNewGlobals(
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000191 v8::Handle<v8::ObjectTemplate> global_proxy_template,
192 MaybeHandle<JSGlobalProxy> maybe_global_proxy,
193 Handle<GlobalObject>* global_object_out);
Andrei Popescu31002712010-02-23 13:46:05 +0000194 // Hooks the given global proxy into the context. If the context was created
195 // by deserialization then this will unhook the global proxy that was
196 // deserialized, leaving the GC to pick it up.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000197 void HookUpGlobalProxy(Handle<GlobalObject> global_object,
Andrei Popescu31002712010-02-23 13:46:05 +0000198 Handle<JSGlobalProxy> global_proxy);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000199 // Similarly, we want to use the global that has been created by the templates
200 // passed through the API. The global from the snapshot is detached from the
201 // other objects in the snapshot.
202 void HookUpGlobalObject(Handle<GlobalObject> global_object);
Andrei Popescu31002712010-02-23 13:46:05 +0000203 // New context initialization. Used for creating a context from scratch.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000204 void InitializeGlobal(Handle<GlobalObject> global_object,
Andrei Popescu31002712010-02-23 13:46:05 +0000205 Handle<JSFunction> empty_function);
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000206 void InitializeExperimentalGlobal();
Andrei Popescu31002712010-02-23 13:46:05 +0000207 // Installs the contents of the native .js files on the global objects.
208 // Used for creating a context from scratch.
Steve Blocka7e24c12009-10-30 11:49:00 +0000209 void InstallNativeFunctions();
Ben Murdoch257744e2011-11-30 15:57:28 +0000210 void InstallExperimentalNativeFunctions();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000211 Handle<JSFunction> InstallInternalArray(Handle<JSBuiltinsObject> builtins,
212 const char* name,
213 ElementsKind elements_kind);
Steve Blocka7e24c12009-10-30 11:49:00 +0000214 bool InstallNatives();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000215
216 void InstallTypedArray(
217 const char* name,
218 ElementsKind elements_kind,
219 Handle<JSFunction>* fun,
220 Handle<Map>* external_map);
Ben Murdoch257744e2011-11-30 15:57:28 +0000221 bool InstallExperimentalNatives();
Ben Murdochb0fe1622011-05-05 13:52:32 +0100222 void InstallBuiltinFunctionIds();
Steve Block6ded16b2010-05-10 14:33:55 +0100223 void InstallJSFunctionResultCaches();
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100224 void InitializeNormalizedMapCaches();
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100225
226 enum ExtensionTraversalState {
227 UNVISITED, VISITED, INSTALLED
228 };
229
230 class ExtensionStates {
231 public:
232 ExtensionStates();
233 ExtensionTraversalState get_state(RegisteredExtension* extension);
234 void set_state(RegisteredExtension* extension,
235 ExtensionTraversalState state);
236 private:
237 HashMap map_;
238 DISALLOW_COPY_AND_ASSIGN(ExtensionStates);
239 };
240
Andrei Popescu31002712010-02-23 13:46:05 +0000241 // Used both for deserialized and from-scratch contexts to add the extensions
242 // provided.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000243 static bool InstallExtensions(Handle<Context> native_context,
Andrei Popescu31002712010-02-23 13:46:05 +0000244 v8::ExtensionConfiguration* extensions);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000245 static bool InstallAutoExtensions(Isolate* isolate,
246 ExtensionStates* extension_states);
247 static bool InstallRequestedExtensions(Isolate* isolate,
248 v8::ExtensionConfiguration* extensions,
249 ExtensionStates* extension_states);
250 static bool InstallExtension(Isolate* isolate,
251 const char* name,
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100252 ExtensionStates* extension_states);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000253 static bool InstallExtension(Isolate* isolate,
254 v8::RegisteredExtension* current,
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100255 ExtensionStates* extension_states);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000256 static bool InstallSpecialObjects(Handle<Context> native_context);
Andrei Popescu402d9372010-02-26 13:31:12 +0000257 bool InstallJSBuiltins(Handle<JSBuiltinsObject> builtins);
Steve Blocka7e24c12009-10-30 11:49:00 +0000258 bool ConfigureApiObject(Handle<JSObject> object,
259 Handle<ObjectTemplateInfo> object_template);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000260 bool ConfigureGlobalObjects(
261 v8::Handle<v8::ObjectTemplate> global_proxy_template);
Steve Blocka7e24c12009-10-30 11:49:00 +0000262
263 // Migrates all properties from the 'from' object to the 'to'
264 // object and overrides the prototype in 'to' with the one from
265 // 'from'.
266 void TransferObject(Handle<JSObject> from, Handle<JSObject> to);
267 void TransferNamedProperties(Handle<JSObject> from, Handle<JSObject> to);
268 void TransferIndexedProperties(Handle<JSObject> from, Handle<JSObject> to);
269
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000270 enum FunctionMode {
271 // With prototype.
272 FUNCTION_WITH_WRITEABLE_PROTOTYPE,
273 FUNCTION_WITH_READONLY_PROTOTYPE,
274 // Without prototype.
275 FUNCTION_WITHOUT_PROTOTYPE,
276 BOUND_FUNCTION
Steve Block6ded16b2010-05-10 14:33:55 +0100277 };
Steve Block44f0eee2011-05-26 01:26:41 +0100278
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000279 static bool IsFunctionModeWithPrototype(FunctionMode function_mode) {
280 return (function_mode == FUNCTION_WITH_WRITEABLE_PROTOTYPE ||
281 function_mode == FUNCTION_WITH_READONLY_PROTOTYPE);
282 }
Steve Block44f0eee2011-05-26 01:26:41 +0100283
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000284 Handle<Map> CreateFunctionMap(FunctionMode function_mode);
285
286 void SetFunctionInstanceDescriptor(Handle<Map> map,
287 FunctionMode function_mode);
Steve Blocka7e24c12009-10-30 11:49:00 +0000288 void MakeFunctionInstancePrototypeWritable();
289
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000290 Handle<Map> CreateStrictFunctionMap(
291 FunctionMode function_mode,
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100292 Handle<JSFunction> empty_function);
Steve Block44f0eee2011-05-26 01:26:41 +0100293
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000294 void SetStrictFunctionInstanceDescriptor(Handle<Map> map,
295 FunctionMode function_mode);
Steve Block44f0eee2011-05-26 01:26:41 +0100296
Ben Murdoch257744e2011-11-30 15:57:28 +0000297 static bool CompileBuiltin(Isolate* isolate, int index);
298 static bool CompileExperimentalBuiltin(Isolate* isolate, int index);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000299 static bool CompileNative(Isolate* isolate,
300 Vector<const char> name,
301 Handle<String> source);
302 static bool CompileScriptCached(Isolate* isolate,
303 Vector<const char> name,
Steve Blocka7e24c12009-10-30 11:49:00 +0000304 Handle<String> source,
305 SourceCodeCache* cache,
306 v8::Extension* extension,
Andrei Popescu31002712010-02-23 13:46:05 +0000307 Handle<Context> top_context,
Steve Blocka7e24c12009-10-30 11:49:00 +0000308 bool use_runtime_context);
309
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000310 Isolate* isolate_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000311 Handle<Context> result_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000312 Handle<Context> native_context_;
Steve Block44f0eee2011-05-26 01:26:41 +0100313
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000314 // Function maps. Function maps are created initially with a read only
315 // prototype for the processing of JS builtins. Later the function maps are
316 // replaced in order to make prototype writable. These are the final, writable
317 // prototype, maps.
318 Handle<Map> sloppy_function_map_writable_prototype_;
319 Handle<Map> strict_function_map_writable_prototype_;
320 Handle<JSFunction> strict_poison_function;
321 Handle<JSFunction> generator_poison_function;
Steve Block44f0eee2011-05-26 01:26:41 +0100322
Andrei Popescu31002712010-02-23 13:46:05 +0000323 BootstrapperActive active_;
324 friend class Bootstrapper;
Steve Blocka7e24c12009-10-30 11:49:00 +0000325};
326
Steve Blocka7e24c12009-10-30 11:49:00 +0000327
328void Bootstrapper::Iterate(ObjectVisitor* v) {
Steve Block44f0eee2011-05-26 01:26:41 +0100329 extensions_cache_.Iterate(v);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100330 v->Synchronize(VisitorSynchronization::kExtensions);
Steve Blocka7e24c12009-10-30 11:49:00 +0000331}
332
333
Steve Blocka7e24c12009-10-30 11:49:00 +0000334Handle<Context> Bootstrapper::CreateEnvironment(
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000335 MaybeHandle<JSGlobalProxy> maybe_global_proxy,
336 v8::Handle<v8::ObjectTemplate> global_proxy_template,
Steve Blocka7e24c12009-10-30 11:49:00 +0000337 v8::ExtensionConfiguration* extensions) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000338 HandleScope scope(isolate_);
339 Genesis genesis(
340 isolate_, maybe_global_proxy, global_proxy_template, extensions);
341 Handle<Context> env = genesis.result();
342 if (env.is_null() || !InstallExtensions(env, extensions)) {
343 return Handle<Context>();
Andrei Popescu31002712010-02-23 13:46:05 +0000344 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000345 return scope.CloseAndEscape(env);
Steve Blocka7e24c12009-10-30 11:49:00 +0000346}
347
348
349static void SetObjectPrototype(Handle<JSObject> object, Handle<Object> proto) {
350 // object.__proto__ = proto;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000351 Handle<Map> old_map = Handle<Map>(object->map());
352 Handle<Map> new_map = Map::Copy(old_map);
353 new_map->set_prototype(*proto);
354 JSObject::MigrateToMap(object, new_map);
Steve Blocka7e24c12009-10-30 11:49:00 +0000355}
356
357
358void Bootstrapper::DetachGlobal(Handle<Context> env) {
Ben Murdoch257744e2011-11-30 15:57:28 +0000359 Factory* factory = env->GetIsolate()->factory();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000360 Handle<JSGlobalProxy> global_proxy(JSGlobalProxy::cast(env->global_proxy()));
361 global_proxy->set_native_context(*factory->null_value());
362 SetObjectPrototype(global_proxy, factory->null_value());
363 global_proxy->map()->set_constructor(*factory->null_value());
Andrei Popescu74b3c142010-03-29 12:03:09 +0100364}
365
366
Steve Blocka7e24c12009-10-30 11:49:00 +0000367static Handle<JSFunction> InstallFunction(Handle<JSObject> target,
368 const char* name,
369 InstanceType type,
370 int instance_size,
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000371 MaybeHandle<JSObject> maybe_prototype,
372 Builtins::Name call) {
Ben Murdoch257744e2011-11-30 15:57:28 +0000373 Isolate* isolate = target->GetIsolate();
Steve Block44f0eee2011-05-26 01:26:41 +0100374 Factory* factory = isolate->factory();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000375 Handle<String> internalized_name = factory->InternalizeUtf8String(name);
Steve Block44f0eee2011-05-26 01:26:41 +0100376 Handle<Code> call_code = Handle<Code>(isolate->builtins()->builtin(call));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000377 Handle<JSObject> prototype;
378 Handle<JSFunction> function = maybe_prototype.ToHandle(&prototype)
379 ? factory->NewFunction(internalized_name, call_code, prototype,
380 type, instance_size)
381 : factory->NewFunctionWithoutPrototype(internalized_name, call_code);
Ben Murdoch589d6972011-11-30 16:04:58 +0000382 PropertyAttributes attributes;
383 if (target->IsJSBuiltinsObject()) {
384 attributes =
385 static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE | READ_ONLY);
386 } else {
387 attributes = DONT_ENUM;
388 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000389 JSObject::AddProperty(target, internalized_name, function, attributes);
390 if (target->IsJSGlobalObject()) {
391 function->shared()->set_instance_class_name(*internalized_name);
Steve Blocka7e24c12009-10-30 11:49:00 +0000392 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100393 function->shared()->set_native(true);
Steve Blocka7e24c12009-10-30 11:49:00 +0000394 return function;
395}
396
397
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000398void Genesis::SetFunctionInstanceDescriptor(
399 Handle<Map> map, FunctionMode function_mode) {
400 int size = IsFunctionModeWithPrototype(function_mode) ? 5 : 4;
401 Map::EnsureDescriptorSlack(map, size);
402
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100403 PropertyAttributes attribs = static_cast<PropertyAttributes>(
404 DONT_ENUM | DONT_DELETE | READ_ONLY);
405
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000406 Handle<AccessorInfo> length =
407 Accessors::FunctionLengthInfo(isolate(), attribs);
Steve Block44f0eee2011-05-26 01:26:41 +0100408 { // Add length.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000409 CallbacksDescriptor d(Handle<Name>(Name::cast(length->name())),
410 length, attribs);
411 map->AppendDescriptor(&d);
Steve Block44f0eee2011-05-26 01:26:41 +0100412 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000413 Handle<AccessorInfo> name =
414 Accessors::FunctionNameInfo(isolate(), attribs);
Steve Block44f0eee2011-05-26 01:26:41 +0100415 { // Add name.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000416 CallbacksDescriptor d(Handle<Name>(Name::cast(name->name())),
417 name, attribs);
418 map->AppendDescriptor(&d);
Steve Block44f0eee2011-05-26 01:26:41 +0100419 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000420 Handle<AccessorInfo> args =
421 Accessors::FunctionArgumentsInfo(isolate(), attribs);
Steve Block44f0eee2011-05-26 01:26:41 +0100422 { // Add arguments.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000423 CallbacksDescriptor d(Handle<Name>(Name::cast(args->name())),
424 args, attribs);
425 map->AppendDescriptor(&d);
Steve Block44f0eee2011-05-26 01:26:41 +0100426 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000427 Handle<AccessorInfo> caller =
428 Accessors::FunctionCallerInfo(isolate(), attribs);
Steve Block44f0eee2011-05-26 01:26:41 +0100429 { // Add caller.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000430 CallbacksDescriptor d(Handle<Name>(Name::cast(caller->name())),
431 caller, attribs);
432 map->AppendDescriptor(&d);
Steve Block44f0eee2011-05-26 01:26:41 +0100433 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000434 if (IsFunctionModeWithPrototype(function_mode)) {
435 if (function_mode == FUNCTION_WITH_WRITEABLE_PROTOTYPE) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100436 attribs = static_cast<PropertyAttributes>(attribs & ~READ_ONLY);
Steve Block44f0eee2011-05-26 01:26:41 +0100437 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000438 Handle<AccessorInfo> prototype =
439 Accessors::FunctionPrototypeInfo(isolate(), attribs);
440 CallbacksDescriptor d(Handle<Name>(Name::cast(prototype->name())),
441 prototype, attribs);
442 map->AppendDescriptor(&d);
Steve Block44f0eee2011-05-26 01:26:41 +0100443 }
Steve Block44f0eee2011-05-26 01:26:41 +0100444}
Steve Blocka7e24c12009-10-30 11:49:00 +0000445
Steve Blocka7e24c12009-10-30 11:49:00 +0000446
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000447Handle<Map> Genesis::CreateFunctionMap(FunctionMode function_mode) {
Ben Murdoch257744e2011-11-30 15:57:28 +0000448 Handle<Map> map = factory()->NewMap(JS_FUNCTION_TYPE, JSFunction::kSize);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000449 SetFunctionInstanceDescriptor(map, function_mode);
450 map->set_function_with_prototype(IsFunctionModeWithPrototype(function_mode));
Steve Block44f0eee2011-05-26 01:26:41 +0100451 return map;
Steve Blocka7e24c12009-10-30 11:49:00 +0000452}
453
454
Ben Murdoch257744e2011-11-30 15:57:28 +0000455Handle<JSFunction> Genesis::CreateEmptyFunction(Isolate* isolate) {
Steve Block44f0eee2011-05-26 01:26:41 +0100456 // Allocate the map for function instances. Maps are allocated first and their
457 // prototypes patched later, once empty function is created.
458
Steve Block6ded16b2010-05-10 14:33:55 +0100459 // Functions with this map will not have a 'prototype' property, and
460 // can not be used as constructors.
Ben Murdoch8b112d22011-06-08 16:22:53 +0100461 Handle<Map> function_without_prototype_map =
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000462 CreateFunctionMap(FUNCTION_WITHOUT_PROTOTYPE);
463 native_context()->set_sloppy_function_without_prototype_map(
Ben Murdoch8b112d22011-06-08 16:22:53 +0100464 *function_without_prototype_map);
Steve Blocka7e24c12009-10-30 11:49:00 +0000465
Steve Block44f0eee2011-05-26 01:26:41 +0100466 // Allocate the function map. This map is temporary, used only for processing
467 // of builtins.
468 // Later the map is replaced with writable prototype map, allocated below.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000469 Handle<Map> function_map =
470 CreateFunctionMap(FUNCTION_WITH_READONLY_PROTOTYPE);
471 native_context()->set_sloppy_function_map(*function_map);
472 native_context()->set_sloppy_function_with_readonly_prototype_map(
473 *function_map);
Steve Blocka7e24c12009-10-30 11:49:00 +0000474
Steve Block44f0eee2011-05-26 01:26:41 +0100475 // The final map for functions. Writeable prototype.
476 // This map is installed in MakeFunctionInstancePrototypeWritable.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000477 sloppy_function_map_writable_prototype_ =
478 CreateFunctionMap(FUNCTION_WITH_WRITEABLE_PROTOTYPE);
Steve Block44f0eee2011-05-26 01:26:41 +0100479
Steve Block44f0eee2011-05-26 01:26:41 +0100480 Factory* factory = isolate->factory();
Steve Block44f0eee2011-05-26 01:26:41 +0100481
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000482 Handle<String> object_name = factory->Object_string();
Steve Blocka7e24c12009-10-30 11:49:00 +0000483
484 { // --- O b j e c t ---
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000485 Handle<JSFunction> object_fun = factory->NewFunction(object_name);
486 int unused = JSObject::kInitialGlobalObjectUnusedPropertiesCount;
487 int instance_size = JSObject::kHeaderSize + kPointerSize * unused;
Steve Blocka7e24c12009-10-30 11:49:00 +0000488 Handle<Map> object_function_map =
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000489 factory->NewMap(JS_OBJECT_TYPE, instance_size);
490 object_function_map->set_inobject_properties(unused);
491 JSFunction::SetInitialMap(object_fun, object_function_map,
492 isolate->factory()->null_value());
493 object_function_map->set_unused_property_fields(unused);
Steve Blocka7e24c12009-10-30 11:49:00 +0000494
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000495 native_context()->set_object_function(*object_fun);
Steve Blocka7e24c12009-10-30 11:49:00 +0000496
497 // Allocate a new prototype for the object function.
Steve Block44f0eee2011-05-26 01:26:41 +0100498 Handle<JSObject> prototype = factory->NewJSObject(
499 isolate->object_function(),
500 TENURED);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000501 Handle<Map> map = Map::Copy(handle(prototype->map()));
502 map->set_is_prototype_map(true);
503 prototype->set_map(*map);
Steve Blocka7e24c12009-10-30 11:49:00 +0000504
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000505 native_context()->set_initial_object_prototype(*prototype);
506 // For bootstrapping set the array prototype to be the same as the object
507 // prototype, otherwise the missing initial_array_prototype will cause
508 // assertions during startup.
509 native_context()->set_initial_array_prototype(*prototype);
510 Accessors::FunctionSetPrototype(object_fun, prototype);
Steve Blocka7e24c12009-10-30 11:49:00 +0000511 }
512
513 // Allocate the empty function as the prototype for function ECMAScript
514 // 262 15.3.4.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000515 Handle<String> empty_string =
516 factory->InternalizeOneByteString(STATIC_CHAR_VECTOR("Empty"));
517 Handle<Code> code(isolate->builtins()->builtin(Builtins::kEmptyFunction));
518 Handle<JSFunction> empty_function = factory->NewFunctionWithoutPrototype(
519 empty_string, code);
520
521 // Allocate the function map first and then patch the prototype later
522 Handle<Map> empty_function_map =
523 CreateFunctionMap(FUNCTION_WITHOUT_PROTOTYPE);
524 DCHECK(!empty_function_map->is_dictionary_map());
525 empty_function_map->set_prototype(
526 native_context()->object_function()->prototype());
527 empty_function_map->set_is_prototype_map(true);
528 empty_function->set_map(*empty_function_map);
Steve Blocka7e24c12009-10-30 11:49:00 +0000529
Andrei Popescu31002712010-02-23 13:46:05 +0000530 // --- E m p t y ---
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000531 Handle<String> source = factory->NewStringFromStaticChars("() {}");
Steve Block44f0eee2011-05-26 01:26:41 +0100532 Handle<Script> script = factory->NewScript(source);
Andrei Popescu31002712010-02-23 13:46:05 +0000533 script->set_type(Smi::FromInt(Script::TYPE_NATIVE));
534 empty_function->shared()->set_script(*script);
535 empty_function->shared()->set_start_position(0);
536 empty_function->shared()->set_end_position(source->length());
537 empty_function->shared()->DontAdaptArguments();
Steve Block44f0eee2011-05-26 01:26:41 +0100538
539 // Set prototypes for the function maps.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000540 native_context()->sloppy_function_map()->set_prototype(*empty_function);
541 native_context()->sloppy_function_without_prototype_map()->
Steve Block6ded16b2010-05-10 14:33:55 +0100542 set_prototype(*empty_function);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000543 sloppy_function_map_writable_prototype_->set_prototype(*empty_function);
Andrei Popescu31002712010-02-23 13:46:05 +0000544 return empty_function;
545}
546
547
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000548void Genesis::SetStrictFunctionInstanceDescriptor(
549 Handle<Map> map, FunctionMode function_mode) {
550 int size = IsFunctionModeWithPrototype(function_mode) ? 5 : 4;
551 Map::EnsureDescriptorSlack(map, size);
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000552
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000553 Handle<AccessorPair> arguments(factory()->NewAccessorPair());
554 Handle<AccessorPair> caller(factory()->NewAccessorPair());
555 PropertyAttributes rw_attribs =
556 static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE);
557 PropertyAttributes ro_attribs =
558 static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE | READ_ONLY);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100559
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000560 // Add length.
561 if (function_mode == BOUND_FUNCTION) {
562 Handle<String> length_string = isolate()->factory()->length_string();
563 FieldDescriptor d(length_string, 0, ro_attribs, Representation::Tagged());
564 map->AppendDescriptor(&d);
565 } else {
566 DCHECK(function_mode == FUNCTION_WITH_WRITEABLE_PROTOTYPE ||
567 function_mode == FUNCTION_WITH_READONLY_PROTOTYPE ||
568 function_mode == FUNCTION_WITHOUT_PROTOTYPE);
569 Handle<AccessorInfo> length =
570 Accessors::FunctionLengthInfo(isolate(), ro_attribs);
571 CallbacksDescriptor d(Handle<Name>(Name::cast(length->name())),
572 length, ro_attribs);
573 map->AppendDescriptor(&d);
Steve Block44f0eee2011-05-26 01:26:41 +0100574 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000575 Handle<AccessorInfo> name =
576 Accessors::FunctionNameInfo(isolate(), ro_attribs);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100577 { // Add name.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000578 CallbacksDescriptor d(Handle<Name>(Name::cast(name->name())),
579 name, ro_attribs);
580 map->AppendDescriptor(&d);
Steve Block44f0eee2011-05-26 01:26:41 +0100581 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100582 { // Add arguments.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000583 CallbacksDescriptor d(factory()->arguments_string(), arguments,
584 rw_attribs);
585 map->AppendDescriptor(&d);
Steve Block44f0eee2011-05-26 01:26:41 +0100586 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100587 { // Add caller.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000588 CallbacksDescriptor d(factory()->caller_string(), caller, rw_attribs);
589 map->AppendDescriptor(&d);
Steve Block44f0eee2011-05-26 01:26:41 +0100590 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000591 if (IsFunctionModeWithPrototype(function_mode)) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100592 // Add prototype.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000593 PropertyAttributes attribs =
594 function_mode == FUNCTION_WITH_WRITEABLE_PROTOTYPE ? rw_attribs
595 : ro_attribs;
596 Handle<AccessorInfo> prototype =
597 Accessors::FunctionPrototypeInfo(isolate(), attribs);
598 CallbacksDescriptor d(Handle<Name>(Name::cast(prototype->name())),
599 prototype, attribs);
600 map->AppendDescriptor(&d);
Steve Block44f0eee2011-05-26 01:26:41 +0100601 }
Steve Block44f0eee2011-05-26 01:26:41 +0100602}
603
604
605// ECMAScript 5th Edition, 13.2.3
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000606Handle<JSFunction> Genesis::GetStrictPoisonFunction() {
607 if (strict_poison_function.is_null()) {
608 Handle<String> name = factory()->InternalizeOneByteString(
609 STATIC_CHAR_VECTOR("ThrowTypeError"));
Ben Murdoch257744e2011-11-30 15:57:28 +0000610 Handle<Code> code(isolate()->builtins()->builtin(
611 Builtins::kStrictModePoisonPill));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000612 strict_poison_function = factory()->NewFunctionWithoutPrototype(name, code);
613 strict_poison_function->set_map(native_context()->sloppy_function_map());
614 strict_poison_function->shared()->DontAdaptArguments();
Steve Block053d10c2011-06-13 19:13:29 +0100615
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000616 JSObject::PreventExtensions(strict_poison_function).Assert();
Ben Murdoch257744e2011-11-30 15:57:28 +0000617 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000618 return strict_poison_function;
Steve Block44f0eee2011-05-26 01:26:41 +0100619}
620
621
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000622Handle<JSFunction> Genesis::GetGeneratorPoisonFunction() {
623 if (generator_poison_function.is_null()) {
624 Handle<String> name = factory()->InternalizeOneByteString(
625 STATIC_CHAR_VECTOR("ThrowTypeError"));
626 Handle<Code> code(isolate()->builtins()->builtin(
627 Builtins::kGeneratorPoisonPill));
628 generator_poison_function = factory()->NewFunctionWithoutPrototype(
629 name, code);
630 generator_poison_function->set_map(native_context()->sloppy_function_map());
631 generator_poison_function->shared()->DontAdaptArguments();
632
633 JSObject::PreventExtensions(generator_poison_function).Assert();
634 }
635 return generator_poison_function;
636}
637
638
639Handle<Map> Genesis::CreateStrictFunctionMap(
640 FunctionMode function_mode,
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100641 Handle<JSFunction> empty_function) {
Ben Murdoch257744e2011-11-30 15:57:28 +0000642 Handle<Map> map = factory()->NewMap(JS_FUNCTION_TYPE, JSFunction::kSize);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000643 SetStrictFunctionInstanceDescriptor(map, function_mode);
644 map->set_function_with_prototype(IsFunctionModeWithPrototype(function_mode));
Steve Block44f0eee2011-05-26 01:26:41 +0100645 map->set_prototype(*empty_function);
646 return map;
647}
648
649
650void Genesis::CreateStrictModeFunctionMaps(Handle<JSFunction> empty) {
Steve Block44f0eee2011-05-26 01:26:41 +0100651 // Allocate map for the prototype-less strict mode instances.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000652 Handle<Map> strict_function_without_prototype_map =
653 CreateStrictFunctionMap(FUNCTION_WITHOUT_PROTOTYPE, empty);
654 native_context()->set_strict_function_without_prototype_map(
655 *strict_function_without_prototype_map);
Steve Block44f0eee2011-05-26 01:26:41 +0100656
657 // Allocate map for the strict mode functions. This map is temporary, used
658 // only for processing of builtins.
659 // Later the map is replaced with writable prototype map, allocated below.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000660 Handle<Map> strict_function_map =
661 CreateStrictFunctionMap(FUNCTION_WITH_READONLY_PROTOTYPE, empty);
662 native_context()->set_strict_function_map(*strict_function_map);
Steve Block44f0eee2011-05-26 01:26:41 +0100663
664 // The final map for the strict mode functions. Writeable prototype.
665 // This map is installed in MakeFunctionInstancePrototypeWritable.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000666 strict_function_map_writable_prototype_ =
667 CreateStrictFunctionMap(FUNCTION_WITH_WRITEABLE_PROTOTYPE, empty);
668 // Special map for bound functions.
669 Handle<Map> bound_function_map =
670 CreateStrictFunctionMap(BOUND_FUNCTION, empty);
671 native_context()->set_bound_function_map(*bound_function_map);
Steve Block44f0eee2011-05-26 01:26:41 +0100672
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100673 // Complete the callbacks.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000674 PoisonArgumentsAndCaller(strict_function_without_prototype_map);
675 PoisonArgumentsAndCaller(strict_function_map);
676 PoisonArgumentsAndCaller(strict_function_map_writable_prototype_);
677 PoisonArgumentsAndCaller(bound_function_map);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100678}
Ben Murdoch5d4cdbf2012-04-11 10:23:59 +0100679
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100680
681static void SetAccessors(Handle<Map> map,
682 Handle<String> name,
683 Handle<JSFunction> func) {
684 DescriptorArray* descs = map->instance_descriptors();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000685 int number = descs->SearchWithCache(*name, *map);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100686 AccessorPair* accessors = AccessorPair::cast(descs->GetValue(number));
687 accessors->set_getter(*func);
688 accessors->set_setter(*func);
689}
690
691
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000692static void ReplaceAccessors(Handle<Map> map,
693 Handle<String> name,
694 PropertyAttributes attributes,
695 Handle<AccessorPair> accessor_pair) {
696 DescriptorArray* descriptors = map->instance_descriptors();
697 int idx = descriptors->SearchWithCache(*name, *map);
698 CallbacksDescriptor descriptor(name, accessor_pair, attributes);
699 descriptors->Replace(idx, &descriptor);
Steve Block44f0eee2011-05-26 01:26:41 +0100700}
701
702
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000703void Genesis::PoisonArgumentsAndCaller(Handle<Map> map) {
704 SetAccessors(map, factory()->arguments_string(), GetStrictPoisonFunction());
705 SetAccessors(map, factory()->caller_string(), GetStrictPoisonFunction());
706}
707
708
709static void AddToWeakNativeContextList(Context* context) {
710 DCHECK(context->IsNativeContext());
Ben Murdoch257744e2011-11-30 15:57:28 +0000711 Heap* heap = context->GetIsolate()->heap();
Ben Murdochb0fe1622011-05-05 13:52:32 +0100712#ifdef DEBUG
713 { // NOLINT
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000714 DCHECK(context->get(Context::NEXT_CONTEXT_LINK)->IsUndefined());
Ben Murdochb0fe1622011-05-05 13:52:32 +0100715 // Check that context is not in the list yet.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000716 for (Object* current = heap->native_contexts_list();
Ben Murdochb0fe1622011-05-05 13:52:32 +0100717 !current->IsUndefined();
718 current = Context::cast(current)->get(Context::NEXT_CONTEXT_LINK)) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000719 DCHECK(current != context);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100720 }
721 }
722#endif
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000723 context->set(Context::NEXT_CONTEXT_LINK, heap->native_contexts_list());
724 heap->set_native_contexts_list(context);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100725}
726
727
Andrei Popescu31002712010-02-23 13:46:05 +0000728void Genesis::CreateRoots() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000729 // Allocate the native context FixedArray first and then patch the
Andrei Popescu31002712010-02-23 13:46:05 +0000730 // closure and extension object later (we need the empty function
731 // and the global object, but in order to create those, we need the
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000732 // native context).
733 native_context_ = factory()->NewNativeContext();
734 AddToWeakNativeContextList(*native_context());
735 isolate()->set_context(*native_context());
Andrei Popescu31002712010-02-23 13:46:05 +0000736
737 // Allocate the message listeners object.
738 {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000739 v8::NeanderArray listeners(isolate());
740 native_context()->set_message_listeners(*listeners.value());
Andrei Popescu31002712010-02-23 13:46:05 +0000741 }
742}
743
744
745Handle<JSGlobalProxy> Genesis::CreateNewGlobals(
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000746 v8::Handle<v8::ObjectTemplate> global_proxy_template,
747 MaybeHandle<JSGlobalProxy> maybe_global_proxy,
748 Handle<GlobalObject>* global_object_out) {
749 // The argument global_proxy_template aka data is an ObjectTemplateInfo.
Andrei Popescu31002712010-02-23 13:46:05 +0000750 // It has a constructor pointer that points at global_constructor which is a
751 // FunctionTemplateInfo.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000752 // The global_proxy_constructor is used to create or reinitialize the
753 // global_proxy. The global_proxy_constructor also has a prototype_template
754 // pointer that points at js_global_object_template which is an
755 // ObjectTemplateInfo.
Andrei Popescu31002712010-02-23 13:46:05 +0000756 // That in turn has a constructor pointer that points at
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000757 // js_global_object_constructor which is a FunctionTemplateInfo.
758 // js_global_object_constructor is used to make js_global_object_function
759 // js_global_object_function is used to make the new global_object.
Andrei Popescu31002712010-02-23 13:46:05 +0000760 //
761 // --- G l o b a l ---
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000762 // Step 1: Create a fresh JSGlobalObject.
763 Handle<JSFunction> js_global_object_function;
764 Handle<ObjectTemplateInfo> js_global_object_template;
765 if (!global_proxy_template.IsEmpty()) {
766 // Get prototype template of the global_proxy_template.
Andrei Popescu31002712010-02-23 13:46:05 +0000767 Handle<ObjectTemplateInfo> data =
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000768 v8::Utils::OpenHandle(*global_proxy_template);
Andrei Popescu31002712010-02-23 13:46:05 +0000769 Handle<FunctionTemplateInfo> global_constructor =
770 Handle<FunctionTemplateInfo>(
771 FunctionTemplateInfo::cast(data->constructor()));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000772 Handle<Object> proto_template(global_constructor->prototype_template(),
773 isolate());
Andrei Popescu31002712010-02-23 13:46:05 +0000774 if (!proto_template->IsUndefined()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000775 js_global_object_template =
Andrei Popescu31002712010-02-23 13:46:05 +0000776 Handle<ObjectTemplateInfo>::cast(proto_template);
777 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000778 }
779
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000780 if (js_global_object_template.is_null()) {
781 Handle<String> name = Handle<String>(heap()->empty_string());
Ben Murdoch257744e2011-11-30 15:57:28 +0000782 Handle<Code> code = Handle<Code>(isolate()->builtins()->builtin(
Steve Block44f0eee2011-05-26 01:26:41 +0100783 Builtins::kIllegal));
Andrei Popescu31002712010-02-23 13:46:05 +0000784 Handle<JSObject> prototype =
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000785 factory()->NewFunctionPrototype(isolate()->object_function());
786 js_global_object_function = factory()->NewFunction(
787 name, code, prototype, JS_GLOBAL_OBJECT_TYPE, JSGlobalObject::kSize);
788#ifdef DEBUG
789 LookupIterator it(prototype, factory()->constructor_string(),
790 LookupIterator::OWN_SKIP_INTERCEPTOR);
791 Handle<Object> value = JSReceiver::GetProperty(&it).ToHandleChecked();
792 DCHECK(it.IsFound());
793 DCHECK_EQ(*isolate()->object_function(), *value);
794#endif
Andrei Popescu31002712010-02-23 13:46:05 +0000795 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000796 Handle<FunctionTemplateInfo> js_global_object_constructor(
797 FunctionTemplateInfo::cast(js_global_object_template->constructor()));
798 js_global_object_function =
799 factory()->CreateApiFunction(js_global_object_constructor,
800 factory()->the_hole_value(),
801 factory()->GlobalObjectType);
Steve Blocka7e24c12009-10-30 11:49:00 +0000802 }
803
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000804 js_global_object_function->initial_map()->set_is_hidden_prototype();
805 js_global_object_function->initial_map()->set_dictionary_map(true);
806 Handle<GlobalObject> global_object =
807 factory()->NewGlobalObject(js_global_object_function);
808 if (global_object_out != NULL) {
809 *global_object_out = global_object;
Andrei Popescu31002712010-02-23 13:46:05 +0000810 }
811
812 // Step 2: create or re-initialize the global proxy object.
813 Handle<JSFunction> global_proxy_function;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000814 if (global_proxy_template.IsEmpty()) {
815 Handle<String> name = Handle<String>(heap()->empty_string());
Ben Murdoch257744e2011-11-30 15:57:28 +0000816 Handle<Code> code = Handle<Code>(isolate()->builtins()->builtin(
Steve Block44f0eee2011-05-26 01:26:41 +0100817 Builtins::kIllegal));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000818 global_proxy_function = factory()->NewFunction(
819 name, code, JS_GLOBAL_PROXY_TYPE, JSGlobalProxy::kSize);
Andrei Popescu31002712010-02-23 13:46:05 +0000820 } else {
821 Handle<ObjectTemplateInfo> data =
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000822 v8::Utils::OpenHandle(*global_proxy_template);
Andrei Popescu31002712010-02-23 13:46:05 +0000823 Handle<FunctionTemplateInfo> global_constructor(
824 FunctionTemplateInfo::cast(data->constructor()));
825 global_proxy_function =
Ben Murdoch257744e2011-11-30 15:57:28 +0000826 factory()->CreateApiFunction(global_constructor,
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000827 factory()->the_hole_value(),
828 factory()->GlobalProxyType);
Andrei Popescu31002712010-02-23 13:46:05 +0000829 }
830
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000831 Handle<String> global_name = factory()->global_string();
Andrei Popescu31002712010-02-23 13:46:05 +0000832 global_proxy_function->shared()->set_instance_class_name(*global_name);
833 global_proxy_function->initial_map()->set_is_access_check_needed(true);
834
835 // Set global_proxy.__proto__ to js_global after ConfigureGlobalObjects
836 // Return the global proxy.
837
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000838 Handle<JSGlobalProxy> global_proxy;
839 if (maybe_global_proxy.ToHandle(&global_proxy)) {
840 factory()->ReinitializeJSGlobalProxy(global_proxy, global_proxy_function);
Andrei Popescu31002712010-02-23 13:46:05 +0000841 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000842 global_proxy = Handle<JSGlobalProxy>::cast(
Ben Murdoch257744e2011-11-30 15:57:28 +0000843 factory()->NewJSObject(global_proxy_function, TENURED));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000844 global_proxy->set_hash(heap()->undefined_value());
Andrei Popescu31002712010-02-23 13:46:05 +0000845 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000846 return global_proxy;
Andrei Popescu31002712010-02-23 13:46:05 +0000847}
848
849
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000850void Genesis::HookUpGlobalProxy(Handle<GlobalObject> global_object,
Andrei Popescu31002712010-02-23 13:46:05 +0000851 Handle<JSGlobalProxy> global_proxy) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000852 // Set the native context for the global object.
853 global_object->set_native_context(*native_context());
854 global_object->set_global_context(*native_context());
855 global_object->set_global_proxy(*global_proxy);
856 global_proxy->set_native_context(*native_context());
857 native_context()->set_global_proxy(*global_proxy);
Andrei Popescu31002712010-02-23 13:46:05 +0000858}
859
860
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000861void Genesis::HookUpGlobalObject(Handle<GlobalObject> global_object) {
862 Handle<GlobalObject> global_object_from_snapshot(
863 GlobalObject::cast(native_context()->extension()));
864 Handle<JSBuiltinsObject> builtins_global(native_context()->builtins());
865 native_context()->set_extension(*global_object);
866 native_context()->set_global_object(*global_object);
867 native_context()->set_security_token(*global_object);
Andrei Popescu402d9372010-02-26 13:31:12 +0000868 static const PropertyAttributes attributes =
869 static_cast<PropertyAttributes>(READ_ONLY | DONT_DELETE);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000870 Runtime::DefineObjectProperty(builtins_global, factory()->global_string(),
871 global_object, attributes).Assert();
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100872 // Set up the reference from the global object to the builtins object.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000873 JSGlobalObject::cast(*global_object)->set_builtins(*builtins_global);
874 TransferNamedProperties(global_object_from_snapshot, global_object);
875 TransferIndexedProperties(global_object_from_snapshot, global_object);
Andrei Popescu402d9372010-02-26 13:31:12 +0000876}
877
878
879// This is only called if we are not using snapshots. The equivalent
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000880// work in the snapshot case is done in HookUpGlobalObject.
881void Genesis::InitializeGlobal(Handle<GlobalObject> global_object,
Andrei Popescu31002712010-02-23 13:46:05 +0000882 Handle<JSFunction> empty_function) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000883 // --- N a t i v e C o n t e x t ---
Andrei Popescu31002712010-02-23 13:46:05 +0000884 // Use the empty function as closure (no scope info).
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000885 native_context()->set_closure(*empty_function);
886 native_context()->set_previous(NULL);
Andrei Popescu31002712010-02-23 13:46:05 +0000887 // Set extension and global object.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000888 native_context()->set_extension(*global_object);
889 native_context()->set_global_object(*global_object);
890 // Security setup: Set the security token of the native context to the global
891 // object. This makes the security check between two different contexts fail
892 // by default even in case of global object reinitialization.
893 native_context()->set_security_token(*global_object);
Andrei Popescu31002712010-02-23 13:46:05 +0000894
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000895 Isolate* isolate = global_object->GetIsolate();
Steve Block44f0eee2011-05-26 01:26:41 +0100896 Factory* factory = isolate->factory();
897 Heap* heap = isolate->heap();
898
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000899 Handle<String> object_name = factory->Object_string();
900 JSObject::AddProperty(
901 global_object, object_name, isolate->object_function(), DONT_ENUM);
Andrei Popescu31002712010-02-23 13:46:05 +0000902
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000903 Handle<JSObject> global(native_context()->global_object());
Steve Blocka7e24c12009-10-30 11:49:00 +0000904
905 // Install global Function object
906 InstallFunction(global, "Function", JS_FUNCTION_TYPE, JSFunction::kSize,
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000907 empty_function, Builtins::kIllegal);
Steve Blocka7e24c12009-10-30 11:49:00 +0000908
909 { // --- A r r a y ---
910 Handle<JSFunction> array_function =
911 InstallFunction(global, "Array", JS_ARRAY_TYPE, JSArray::kSize,
Steve Block44f0eee2011-05-26 01:26:41 +0100912 isolate->initial_object_prototype(),
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000913 Builtins::kArrayCode);
Steve Blocka7e24c12009-10-30 11:49:00 +0000914 array_function->shared()->DontAdaptArguments();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000915 array_function->shared()->set_function_data(Smi::FromInt(kArrayCode));
Steve Blocka7e24c12009-10-30 11:49:00 +0000916
917 // This seems a bit hackish, but we need to make sure Array.length
918 // is 1.
919 array_function->shared()->set_length(1);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000920
921 Handle<Map> initial_map(array_function->initial_map());
922
923 // This assert protects an optimization in
924 // HGraphBuilder::JSArrayBuilder::EmitMapCode()
925 DCHECK(initial_map->elements_kind() == GetInitialFastElementsKind());
926 Map::EnsureDescriptorSlack(initial_map, 1);
927
928 PropertyAttributes attribs = static_cast<PropertyAttributes>(
929 DONT_ENUM | DONT_DELETE);
930
931 Handle<AccessorInfo> array_length =
932 Accessors::ArrayLengthInfo(isolate, attribs);
933 { // Add length.
934 CallbacksDescriptor d(
935 Handle<Name>(Name::cast(array_length->name())),
936 array_length, attribs);
937 array_function->initial_map()->AppendDescriptor(&d);
938 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000939
Steve Blocka7e24c12009-10-30 11:49:00 +0000940 // array_function is used internally. JS code creating array object should
941 // search for the 'Array' property on the global object and use that one
942 // as the constructor. 'Array' property on a global object can be
943 // overwritten by JS code.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000944 native_context()->set_array_function(*array_function);
945
946 // Cache the array maps, needed by ArrayConstructorStub
947 CacheInitialJSArrayMaps(native_context(), initial_map);
948 ArrayConstructorStub array_constructor_stub(isolate);
949 Handle<Code> code = array_constructor_stub.GetCode();
950 array_function->shared()->set_construct_stub(*code);
Steve Blocka7e24c12009-10-30 11:49:00 +0000951 }
952
953 { // --- N u m b e r ---
954 Handle<JSFunction> number_fun =
955 InstallFunction(global, "Number", JS_VALUE_TYPE, JSValue::kSize,
Steve Block44f0eee2011-05-26 01:26:41 +0100956 isolate->initial_object_prototype(),
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000957 Builtins::kIllegal);
958 native_context()->set_number_function(*number_fun);
Steve Blocka7e24c12009-10-30 11:49:00 +0000959 }
960
961 { // --- B o o l e a n ---
962 Handle<JSFunction> boolean_fun =
963 InstallFunction(global, "Boolean", JS_VALUE_TYPE, JSValue::kSize,
Steve Block44f0eee2011-05-26 01:26:41 +0100964 isolate->initial_object_prototype(),
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000965 Builtins::kIllegal);
966 native_context()->set_boolean_function(*boolean_fun);
Steve Blocka7e24c12009-10-30 11:49:00 +0000967 }
968
969 { // --- S t r i n g ---
970 Handle<JSFunction> string_fun =
971 InstallFunction(global, "String", JS_VALUE_TYPE, JSValue::kSize,
Steve Block44f0eee2011-05-26 01:26:41 +0100972 isolate->initial_object_prototype(),
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000973 Builtins::kIllegal);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100974 string_fun->shared()->set_construct_stub(
Steve Block44f0eee2011-05-26 01:26:41 +0100975 isolate->builtins()->builtin(Builtins::kStringConstructCode));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000976 native_context()->set_string_function(*string_fun);
Steve Blocka7e24c12009-10-30 11:49:00 +0000977
978 Handle<Map> string_map =
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000979 Handle<Map>(native_context()->string_function()->initial_map());
980 Map::EnsureDescriptorSlack(string_map, 1);
981
982 PropertyAttributes attribs = static_cast<PropertyAttributes>(
983 DONT_ENUM | DONT_DELETE | READ_ONLY);
984 Handle<AccessorInfo> string_length(
985 Accessors::StringLengthInfo(isolate, attribs));
986
987 { // Add length.
988 CallbacksDescriptor d(factory->length_string(), string_length, attribs);
989 string_map->AppendDescriptor(&d);
990 }
991 }
992
993 {
994 // --- S y m b o l ---
995 Handle<JSFunction> symbol_fun = InstallFunction(
996 global, "Symbol", JS_VALUE_TYPE, JSValue::kSize,
997 isolate->initial_object_prototype(), Builtins::kIllegal);
998 native_context()->set_symbol_function(*symbol_fun);
Steve Blocka7e24c12009-10-30 11:49:00 +0000999 }
1000
1001 { // --- D a t e ---
1002 // Builtin functions for Date.prototype.
1003 Handle<JSFunction> date_fun =
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001004 InstallFunction(global, "Date", JS_DATE_TYPE, JSDate::kSize,
Steve Block44f0eee2011-05-26 01:26:41 +01001005 isolate->initial_object_prototype(),
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001006 Builtins::kIllegal);
Steve Blocka7e24c12009-10-30 11:49:00 +00001007
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001008 native_context()->set_date_function(*date_fun);
Steve Blocka7e24c12009-10-30 11:49:00 +00001009 }
1010
1011
1012 { // -- R e g E x p
1013 // Builtin functions for RegExp.prototype.
1014 Handle<JSFunction> regexp_fun =
1015 InstallFunction(global, "RegExp", JS_REGEXP_TYPE, JSRegExp::kSize,
Steve Block44f0eee2011-05-26 01:26:41 +01001016 isolate->initial_object_prototype(),
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001017 Builtins::kIllegal);
1018 native_context()->set_regexp_function(*regexp_fun);
Steve Block6ded16b2010-05-10 14:33:55 +01001019
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001020 DCHECK(regexp_fun->has_initial_map());
Steve Block6ded16b2010-05-10 14:33:55 +01001021 Handle<Map> initial_map(regexp_fun->initial_map());
1022
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001023 DCHECK_EQ(0, initial_map->inobject_properties());
Steve Block6ded16b2010-05-10 14:33:55 +01001024
Steve Block6ded16b2010-05-10 14:33:55 +01001025 PropertyAttributes final =
1026 static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE | READ_ONLY);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001027 Map::EnsureDescriptorSlack(initial_map, 5);
1028
Steve Block6ded16b2010-05-10 14:33:55 +01001029 {
1030 // ECMA-262, section 15.10.7.1.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001031 FieldDescriptor field(factory->source_string(),
Steve Block6ded16b2010-05-10 14:33:55 +01001032 JSRegExp::kSourceFieldIndex,
1033 final,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001034 Representation::Tagged());
1035 initial_map->AppendDescriptor(&field);
Steve Block6ded16b2010-05-10 14:33:55 +01001036 }
1037 {
1038 // ECMA-262, section 15.10.7.2.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001039 FieldDescriptor field(factory->global_string(),
Steve Block6ded16b2010-05-10 14:33:55 +01001040 JSRegExp::kGlobalFieldIndex,
1041 final,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001042 Representation::Tagged());
1043 initial_map->AppendDescriptor(&field);
Steve Block6ded16b2010-05-10 14:33:55 +01001044 }
1045 {
1046 // ECMA-262, section 15.10.7.3.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001047 FieldDescriptor field(factory->ignore_case_string(),
Steve Block6ded16b2010-05-10 14:33:55 +01001048 JSRegExp::kIgnoreCaseFieldIndex,
1049 final,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001050 Representation::Tagged());
1051 initial_map->AppendDescriptor(&field);
Steve Block6ded16b2010-05-10 14:33:55 +01001052 }
1053 {
1054 // ECMA-262, section 15.10.7.4.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001055 FieldDescriptor field(factory->multiline_string(),
Steve Block6ded16b2010-05-10 14:33:55 +01001056 JSRegExp::kMultilineFieldIndex,
1057 final,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001058 Representation::Tagged());
1059 initial_map->AppendDescriptor(&field);
Steve Block6ded16b2010-05-10 14:33:55 +01001060 }
1061 {
1062 // ECMA-262, section 15.10.7.5.
1063 PropertyAttributes writable =
1064 static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001065 FieldDescriptor field(factory->last_index_string(),
Steve Block6ded16b2010-05-10 14:33:55 +01001066 JSRegExp::kLastIndexFieldIndex,
1067 writable,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001068 Representation::Tagged());
1069 initial_map->AppendDescriptor(&field);
Steve Block6ded16b2010-05-10 14:33:55 +01001070 }
Steve Block6ded16b2010-05-10 14:33:55 +01001071
1072 initial_map->set_inobject_properties(5);
1073 initial_map->set_pre_allocated_property_fields(5);
1074 initial_map->set_unused_property_fields(0);
1075 initial_map->set_instance_size(
1076 initial_map->instance_size() + 5 * kPointerSize);
Iain Merrick75681382010-08-19 15:07:18 +01001077 initial_map->set_visitor_id(StaticVisitorBase::GetVisitorId(*initial_map));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001078
1079 // RegExp prototype object is itself a RegExp.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001080 Handle<Map> proto_map = Map::Copy(initial_map);
1081 proto_map->set_prototype(native_context()->initial_object_prototype());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001082 Handle<JSObject> proto = factory->NewJSObjectFromMap(proto_map);
1083 proto->InObjectPropertyAtPut(JSRegExp::kSourceFieldIndex,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001084 heap->query_colon_string());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001085 proto->InObjectPropertyAtPut(JSRegExp::kGlobalFieldIndex,
1086 heap->false_value());
1087 proto->InObjectPropertyAtPut(JSRegExp::kIgnoreCaseFieldIndex,
1088 heap->false_value());
1089 proto->InObjectPropertyAtPut(JSRegExp::kMultilineFieldIndex,
1090 heap->false_value());
1091 proto->InObjectPropertyAtPut(JSRegExp::kLastIndexFieldIndex,
1092 Smi::FromInt(0),
1093 SKIP_WRITE_BARRIER); // It's a Smi.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001094 proto_map->set_is_prototype_map(true);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001095 initial_map->set_prototype(*proto);
1096 factory->SetRegExpIrregexpData(Handle<JSRegExp>::cast(proto),
1097 JSRegExp::IRREGEXP, factory->empty_string(),
1098 JSRegExp::Flags(0), 0);
Steve Blocka7e24c12009-10-30 11:49:00 +00001099 }
1100
1101 { // -- J S O N
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001102 Handle<String> name = factory->InternalizeUtf8String("JSON");
1103 Handle<JSFunction> cons = factory->NewFunction(name);
1104 JSFunction::SetInstancePrototype(cons,
1105 Handle<Object>(native_context()->initial_object_prototype(), isolate));
Steve Blocka7e24c12009-10-30 11:49:00 +00001106 cons->SetInstanceClassName(*name);
Steve Block44f0eee2011-05-26 01:26:41 +01001107 Handle<JSObject> json_object = factory->NewJSObject(cons, TENURED);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001108 DCHECK(json_object->IsJSObject());
1109 JSObject::AddProperty(global, name, json_object, DONT_ENUM);
1110 native_context()->set_json_object(*json_object);
Steve Blocka7e24c12009-10-30 11:49:00 +00001111 }
1112
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001113 { // -- A r r a y B u f f e r
1114 Handle<JSFunction> array_buffer_fun =
1115 InstallFunction(
1116 global, "ArrayBuffer", JS_ARRAY_BUFFER_TYPE,
1117 JSArrayBuffer::kSizeWithInternalFields,
1118 isolate->initial_object_prototype(),
1119 Builtins::kIllegal);
1120 native_context()->set_array_buffer_fun(*array_buffer_fun);
1121 }
1122
1123 { // -- T y p e d A r r a y s
1124#define INSTALL_TYPED_ARRAY(Type, type, TYPE, ctype, size) \
1125 { \
1126 Handle<JSFunction> fun; \
1127 Handle<Map> external_map; \
1128 InstallTypedArray(#Type "Array", \
1129 TYPE##_ELEMENTS, \
1130 &fun, \
1131 &external_map); \
1132 native_context()->set_##type##_array_fun(*fun); \
1133 native_context()->set_##type##_array_external_map(*external_map); \
1134 }
1135 TYPED_ARRAYS(INSTALL_TYPED_ARRAY)
1136#undef INSTALL_TYPED_ARRAY
1137
1138 Handle<JSFunction> data_view_fun =
1139 InstallFunction(
1140 global, "DataView", JS_DATA_VIEW_TYPE,
1141 JSDataView::kSizeWithInternalFields,
1142 isolate->initial_object_prototype(),
1143 Builtins::kIllegal);
1144 native_context()->set_data_view_fun(*data_view_fun);
1145 }
1146
1147 // -- M a p
1148 InstallFunction(global, "Map", JS_MAP_TYPE, JSMap::kSize,
1149 isolate->initial_object_prototype(), Builtins::kIllegal);
1150
1151 // -- S e t
1152 InstallFunction(global, "Set", JS_SET_TYPE, JSSet::kSize,
1153 isolate->initial_object_prototype(), Builtins::kIllegal);
1154
1155 { // Set up the iterator result object
1156 STATIC_ASSERT(JSGeneratorObject::kResultPropertyCount == 2);
1157 Handle<JSFunction> object_function(native_context()->object_function());
1158 Handle<Map> iterator_result_map =
1159 Map::Create(isolate, JSGeneratorObject::kResultPropertyCount);
1160 DCHECK_EQ(JSGeneratorObject::kResultSize,
1161 iterator_result_map->instance_size());
1162 DCHECK_EQ(JSGeneratorObject::kResultPropertyCount,
1163 iterator_result_map->inobject_properties());
1164 Map::EnsureDescriptorSlack(iterator_result_map,
1165 JSGeneratorObject::kResultPropertyCount);
1166
1167 FieldDescriptor value_descr(factory->value_string(),
1168 JSGeneratorObject::kResultValuePropertyIndex,
1169 NONE, Representation::Tagged());
1170 iterator_result_map->AppendDescriptor(&value_descr);
1171
1172 FieldDescriptor done_descr(factory->done_string(),
1173 JSGeneratorObject::kResultDonePropertyIndex,
1174 NONE, Representation::Tagged());
1175 iterator_result_map->AppendDescriptor(&done_descr);
1176
1177 iterator_result_map->set_unused_property_fields(0);
1178 iterator_result_map->set_pre_allocated_property_fields(
1179 JSGeneratorObject::kResultPropertyCount);
1180 DCHECK_EQ(JSGeneratorObject::kResultSize,
1181 iterator_result_map->instance_size());
1182 native_context()->set_iterator_result_map(*iterator_result_map);
1183 }
1184
1185 // -- W e a k M a p
1186 InstallFunction(global, "WeakMap", JS_WEAK_MAP_TYPE, JSWeakMap::kSize,
1187 isolate->initial_object_prototype(), Builtins::kIllegal);
1188 // -- W e a k S e t
1189 InstallFunction(global, "WeakSet", JS_WEAK_SET_TYPE, JSWeakSet::kSize,
1190 isolate->initial_object_prototype(), Builtins::kIllegal);
1191
1192 { // --- sloppy arguments map
Steve Blocka7e24c12009-10-30 11:49:00 +00001193 // Make sure we can recognize argument objects at runtime.
1194 // This is done by introducing an anonymous function with
1195 // class_name equals 'Arguments'.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001196 Handle<String> arguments_string = factory->Arguments_string();
1197 Handle<Code> code(isolate->builtins()->builtin(Builtins::kIllegal));
1198 Handle<JSFunction> function = factory->NewFunctionWithoutPrototype(
1199 arguments_string, code);
1200 function->shared()->set_instance_class_name(*arguments_string);
Steve Blocka7e24c12009-10-30 11:49:00 +00001201
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001202 Handle<Map> map =
1203 factory->NewMap(JS_OBJECT_TYPE, Heap::kSloppyArgumentsObjectSize);
1204 // Create the descriptor array for the arguments object.
1205 Map::EnsureDescriptorSlack(map, 2);
Steve Blocka7e24c12009-10-30 11:49:00 +00001206
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001207 { // length
1208 FieldDescriptor d(factory->length_string(), Heap::kArgumentsLengthIndex,
1209 DONT_ENUM, Representation::Tagged());
1210 map->AppendDescriptor(&d);
1211 }
1212 { // callee
1213 FieldDescriptor d(factory->callee_string(), Heap::kArgumentsCalleeIndex,
1214 DONT_ENUM, Representation::Tagged());
1215 map->AppendDescriptor(&d);
1216 }
1217 // @@iterator method is added later.
Steve Blocka7e24c12009-10-30 11:49:00 +00001218
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001219 map->set_function_with_prototype(true);
1220 map->set_pre_allocated_property_fields(2);
1221 map->set_inobject_properties(2);
1222 native_context()->set_sloppy_arguments_map(*map);
Steve Blocka7e24c12009-10-30 11:49:00 +00001223
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001224 DCHECK(!function->has_initial_map());
1225 JSFunction::SetInitialMap(function, map,
1226 isolate->initial_object_prototype());
Steve Blocka7e24c12009-10-30 11:49:00 +00001227
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001228 DCHECK(map->inobject_properties() > Heap::kArgumentsCalleeIndex);
1229 DCHECK(map->inobject_properties() > Heap::kArgumentsLengthIndex);
1230 DCHECK(!map->is_dictionary_map());
1231 DCHECK(IsFastObjectElementsKind(map->elements_kind()));
Steve Block44f0eee2011-05-26 01:26:41 +01001232 }
1233
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001234 { // --- aliased arguments map
1235 Handle<Map> map = Map::Copy(isolate->sloppy_arguments_map());
1236 map->set_elements_kind(SLOPPY_ARGUMENTS_ELEMENTS);
1237 DCHECK_EQ(2, map->pre_allocated_property_fields());
1238 native_context()->set_aliased_arguments_map(*map);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001239 }
1240
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001241 { // --- strict mode arguments map
Steve Block44f0eee2011-05-26 01:26:41 +01001242 const PropertyAttributes attributes =
1243 static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE | READ_ONLY);
1244
1245 // Create the ThrowTypeError functions.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001246 Handle<AccessorPair> callee = factory->NewAccessorPair();
1247 Handle<AccessorPair> caller = factory->NewAccessorPair();
Steve Block44f0eee2011-05-26 01:26:41 +01001248
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001249 Handle<JSFunction> poison = GetStrictPoisonFunction();
Steve Block44f0eee2011-05-26 01:26:41 +01001250
1251 // Install the ThrowTypeError functions.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001252 callee->set_getter(*poison);
1253 callee->set_setter(*poison);
1254 caller->set_getter(*poison);
1255 caller->set_setter(*poison);
Steve Block44f0eee2011-05-26 01:26:41 +01001256
1257 // Create the map. Allocate one in-object field for length.
1258 Handle<Map> map = factory->NewMap(JS_OBJECT_TYPE,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001259 Heap::kStrictArgumentsObjectSize);
1260 // Create the descriptor array for the arguments object.
1261 Map::EnsureDescriptorSlack(map, 3);
1262
1263 { // length
1264 FieldDescriptor d(factory->length_string(), Heap::kArgumentsLengthIndex,
1265 DONT_ENUM, Representation::Tagged());
1266 map->AppendDescriptor(&d);
1267 }
1268 { // callee
1269 CallbacksDescriptor d(factory->callee_string(), callee, attributes);
1270 map->AppendDescriptor(&d);
1271 }
1272 { // caller
1273 CallbacksDescriptor d(factory->caller_string(), caller, attributes);
1274 map->AppendDescriptor(&d);
1275 }
1276 // @@iterator method is added later.
1277
Steve Block44f0eee2011-05-26 01:26:41 +01001278 map->set_function_with_prototype(true);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001279 map->set_prototype(native_context()->object_function()->prototype());
Steve Block44f0eee2011-05-26 01:26:41 +01001280 map->set_pre_allocated_property_fields(1);
1281 map->set_inobject_properties(1);
1282
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001283 // Copy constructor from the sloppy arguments boilerplate.
Steve Block44f0eee2011-05-26 01:26:41 +01001284 map->set_constructor(
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001285 native_context()->sloppy_arguments_map()->constructor());
Steve Block44f0eee2011-05-26 01:26:41 +01001286
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001287 native_context()->set_strict_arguments_map(*map);
Steve Block44f0eee2011-05-26 01:26:41 +01001288
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001289 DCHECK(map->inobject_properties() > Heap::kArgumentsLengthIndex);
1290 DCHECK(!map->is_dictionary_map());
1291 DCHECK(IsFastObjectElementsKind(map->elements_kind()));
Steve Blocka7e24c12009-10-30 11:49:00 +00001292 }
1293
1294 { // --- context extension
1295 // Create a function for the context extension objects.
Steve Block44f0eee2011-05-26 01:26:41 +01001296 Handle<Code> code = Handle<Code>(
1297 isolate->builtins()->builtin(Builtins::kIllegal));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001298 Handle<JSFunction> context_extension_fun = factory->NewFunction(
1299 factory->empty_string(), code, JS_CONTEXT_EXTENSION_OBJECT_TYPE,
1300 JSObject::kHeaderSize);
Steve Blocka7e24c12009-10-30 11:49:00 +00001301
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001302 Handle<String> name = factory->InternalizeOneByteString(
1303 STATIC_CHAR_VECTOR("context_extension"));
Steve Blocka7e24c12009-10-30 11:49:00 +00001304 context_extension_fun->shared()->set_instance_class_name(*name);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001305 native_context()->set_context_extension_function(*context_extension_fun);
Steve Blocka7e24c12009-10-30 11:49:00 +00001306 }
1307
1308
1309 {
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001310 // Set up the call-as-function delegate.
Steve Blocka7e24c12009-10-30 11:49:00 +00001311 Handle<Code> code =
Steve Block44f0eee2011-05-26 01:26:41 +01001312 Handle<Code>(isolate->builtins()->builtin(
1313 Builtins::kHandleApiCallAsFunction));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001314 Handle<JSFunction> delegate = factory->NewFunction(
1315 factory->empty_string(), code, JS_OBJECT_TYPE, JSObject::kHeaderSize);
1316 native_context()->set_call_as_function_delegate(*delegate);
Steve Blocka7e24c12009-10-30 11:49:00 +00001317 delegate->shared()->DontAdaptArguments();
1318 }
1319
1320 {
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001321 // Set up the call-as-constructor delegate.
Steve Blocka7e24c12009-10-30 11:49:00 +00001322 Handle<Code> code =
Steve Block44f0eee2011-05-26 01:26:41 +01001323 Handle<Code>(isolate->builtins()->builtin(
1324 Builtins::kHandleApiCallAsConstructor));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001325 Handle<JSFunction> delegate = factory->NewFunction(
1326 factory->empty_string(), code, JS_OBJECT_TYPE, JSObject::kHeaderSize);
1327 native_context()->set_call_as_constructor_delegate(*delegate);
Steve Blocka7e24c12009-10-30 11:49:00 +00001328 delegate->shared()->DontAdaptArguments();
1329 }
1330
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001331 // Initialize the embedder data slot.
1332 Handle<FixedArray> embedder_data = factory->NewFixedArray(3);
1333 native_context()->set_embedder_data(*embedder_data);
1334}
Steve Blocka7e24c12009-10-30 11:49:00 +00001335
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001336
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001337void Genesis::InstallTypedArray(
1338 const char* name,
1339 ElementsKind elements_kind,
1340 Handle<JSFunction>* fun,
1341 Handle<Map>* external_map) {
1342 Handle<JSObject> global = Handle<JSObject>(native_context()->global_object());
1343 Handle<JSFunction> result = InstallFunction(
1344 global, name, JS_TYPED_ARRAY_TYPE, JSTypedArray::kSize,
1345 isolate()->initial_object_prototype(), Builtins::kIllegal);
1346
1347 Handle<Map> initial_map = isolate()->factory()->NewMap(
1348 JS_TYPED_ARRAY_TYPE,
1349 JSTypedArray::kSizeWithInternalFields,
1350 elements_kind);
1351 JSFunction::SetInitialMap(result, initial_map,
1352 handle(initial_map->prototype(), isolate()));
1353 *fun = result;
1354
1355 ElementsKind external_kind = GetNextTransitionElementsKind(elements_kind);
1356 *external_map = Map::AsElementsKind(initial_map, external_kind);
Steve Blocka7e24c12009-10-30 11:49:00 +00001357}
1358
1359
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001360void Genesis::InitializeExperimentalGlobal() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001361 // TODO(erikcorry): Move this into Genesis::InitializeGlobal once we no
1362 // longer need to live behind a flag.
1363 Handle<JSObject> builtins(native_context()->builtins());
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001364
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001365 Handle<HeapObject> flag(
1366 FLAG_harmony_regexps ? heap()->true_value() : heap()->false_value());
1367 PropertyAttributes attributes =
1368 static_cast<PropertyAttributes>(DONT_DELETE | READ_ONLY);
1369 Runtime::DefineObjectProperty(builtins, factory()->harmony_regexps_string(),
1370 flag, attributes).Assert();
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001371}
1372
1373
Ben Murdoch257744e2011-11-30 15:57:28 +00001374bool Genesis::CompileBuiltin(Isolate* isolate, int index) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001375 Vector<const char> name = Natives::GetScriptName(index);
Steve Block44f0eee2011-05-26 01:26:41 +01001376 Handle<String> source_code =
Ben Murdoch257744e2011-11-30 15:57:28 +00001377 isolate->bootstrapper()->NativesSourceLookup(index);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001378 return CompileNative(isolate, name, source_code);
Ben Murdoch257744e2011-11-30 15:57:28 +00001379}
1380
1381
1382bool Genesis::CompileExperimentalBuiltin(Isolate* isolate, int index) {
1383 Vector<const char> name = ExperimentalNatives::GetScriptName(index);
1384 Factory* factory = isolate->factory();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001385 Handle<String> source_code;
1386 ASSIGN_RETURN_ON_EXCEPTION_VALUE(
1387 isolate, source_code, factory->NewStringFromAscii(
1388 ExperimentalNatives::GetRawScriptSource(index)),
1389 false);
1390 return CompileNative(isolate, name, source_code);
Steve Blocka7e24c12009-10-30 11:49:00 +00001391}
1392
1393
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001394bool Genesis::CompileNative(Isolate* isolate,
1395 Vector<const char> name,
1396 Handle<String> source) {
1397 HandleScope scope(isolate);
1398 SuppressDebug compiling_natives(isolate->debug());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001399 // During genesis, the boilerplate for stack overflow won't work until the
1400 // environment has been at least partially initialized. Add a stack check
1401 // before entering JS code to catch overflow early.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001402 StackLimitCheck check(isolate);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001403 if (check.HasOverflowed()) return false;
1404
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001405 bool result = CompileScriptCached(isolate,
1406 name,
Andrei Popescu31002712010-02-23 13:46:05 +00001407 source,
1408 NULL,
1409 NULL,
Steve Block44f0eee2011-05-26 01:26:41 +01001410 Handle<Context>(isolate->context()),
Andrei Popescu31002712010-02-23 13:46:05 +00001411 true);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001412 DCHECK(isolate->has_pending_exception() != result);
Steve Block44f0eee2011-05-26 01:26:41 +01001413 if (!result) isolate->clear_pending_exception();
Steve Blocka7e24c12009-10-30 11:49:00 +00001414 return result;
1415}
1416
1417
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001418bool Genesis::CompileScriptCached(Isolate* isolate,
1419 Vector<const char> name,
Steve Blocka7e24c12009-10-30 11:49:00 +00001420 Handle<String> source,
1421 SourceCodeCache* cache,
1422 v8::Extension* extension,
Andrei Popescu31002712010-02-23 13:46:05 +00001423 Handle<Context> top_context,
Steve Blocka7e24c12009-10-30 11:49:00 +00001424 bool use_runtime_context) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001425 Factory* factory = isolate->factory();
1426 HandleScope scope(isolate);
Steve Block6ded16b2010-05-10 14:33:55 +01001427 Handle<SharedFunctionInfo> function_info;
Steve Blocka7e24c12009-10-30 11:49:00 +00001428
1429 // If we can't find the function in the cache, we compile a new
1430 // function and insert it into the cache.
Steve Block6ded16b2010-05-10 14:33:55 +01001431 if (cache == NULL || !cache->Lookup(name, &function_info)) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001432 DCHECK(source->IsOneByteRepresentation());
1433 Handle<String> script_name =
1434 factory->NewStringFromUtf8(name).ToHandleChecked();
1435 function_info = Compiler::CompileScript(
1436 source, script_name, 0, 0, false, top_context, extension, NULL,
1437 ScriptCompiler::kNoCompileOptions,
Andrei Popescu31002712010-02-23 13:46:05 +00001438 use_runtime_context ? NATIVES_CODE : NOT_NATIVES_CODE);
Steve Block6ded16b2010-05-10 14:33:55 +01001439 if (function_info.is_null()) return false;
1440 if (cache != NULL) cache->Add(name, function_info);
Steve Blocka7e24c12009-10-30 11:49:00 +00001441 }
1442
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001443 // Set up the function context. Conceptually, we should clone the
Steve Blocka7e24c12009-10-30 11:49:00 +00001444 // function before overwriting the context but since we're in a
1445 // single-threaded environment it is not strictly necessary.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001446 DCHECK(top_context->IsNativeContext());
Steve Blocka7e24c12009-10-30 11:49:00 +00001447 Handle<Context> context =
1448 Handle<Context>(use_runtime_context
Andrei Popescu31002712010-02-23 13:46:05 +00001449 ? Handle<Context>(top_context->runtime_context())
1450 : top_context);
Steve Blocka7e24c12009-10-30 11:49:00 +00001451 Handle<JSFunction> fun =
Steve Block44f0eee2011-05-26 01:26:41 +01001452 factory->NewFunctionFromSharedFunctionInfo(function_info, context);
Steve Blocka7e24c12009-10-30 11:49:00 +00001453
Leon Clarke4515c472010-02-03 11:58:03 +00001454 // Call function using either the runtime object or the global
Steve Blocka7e24c12009-10-30 11:49:00 +00001455 // object as the receiver. Provide no parameters.
1456 Handle<Object> receiver =
1457 Handle<Object>(use_runtime_context
Andrei Popescu31002712010-02-23 13:46:05 +00001458 ? top_context->builtins()
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001459 : top_context->global_object(),
1460 isolate);
1461 return !Execution::Call(
1462 isolate, fun, receiver, 0, NULL).is_null();
Steve Blocka7e24c12009-10-30 11:49:00 +00001463}
1464
1465
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001466static Handle<JSObject> ResolveBuiltinIdHolder(Handle<Context> native_context,
1467 const char* holder_expr) {
1468 Isolate* isolate = native_context->GetIsolate();
1469 Factory* factory = isolate->factory();
1470 Handle<GlobalObject> global(native_context->global_object());
1471 const char* period_pos = strchr(holder_expr, '.');
1472 if (period_pos == NULL) {
1473 return Handle<JSObject>::cast(
1474 Object::GetPropertyOrElement(
1475 global, factory->InternalizeUtf8String(holder_expr))
1476 .ToHandleChecked());
1477 }
1478 const char* inner = period_pos + 1;
1479 DCHECK_EQ(NULL, strchr(inner, '.'));
1480 Vector<const char> property(holder_expr,
1481 static_cast<int>(period_pos - holder_expr));
1482 Handle<String> property_string = factory->InternalizeUtf8String(property);
1483 DCHECK(!property_string.is_null());
1484 Handle<JSObject> object = Handle<JSObject>::cast(
1485 Object::GetProperty(global, property_string).ToHandleChecked());
1486 if (strcmp("prototype", inner) == 0) {
1487 Handle<JSFunction> function = Handle<JSFunction>::cast(object);
1488 return Handle<JSObject>(JSObject::cast(function->prototype()));
1489 }
1490 Handle<String> inner_string = factory->InternalizeUtf8String(inner);
1491 DCHECK(!inner_string.is_null());
1492 Handle<Object> value =
1493 Object::GetProperty(object, inner_string).ToHandleChecked();
1494 return Handle<JSObject>::cast(value);
1495}
Steve Blocka7e24c12009-10-30 11:49:00 +00001496
Steve Block44f0eee2011-05-26 01:26:41 +01001497
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001498#define INSTALL_NATIVE(Type, name, var) \
1499 Handle<String> var##_name = \
1500 factory()->InternalizeOneByteString(STATIC_CHAR_VECTOR(name)); \
1501 Handle<Object> var##_native = \
1502 Object::GetProperty(handle(native_context()->builtins()), var##_name) \
1503 .ToHandleChecked(); \
1504 native_context()->set_##var(Type::cast(*var##_native));
1505
1506#define INSTALL_NATIVE_MATH(name) \
1507 { \
1508 Handle<Object> fun = \
1509 ResolveBuiltinIdHolder(native_context(), "Math." #name); \
1510 native_context()->set_math_##name##_fun(JSFunction::cast(*fun)); \
1511 }
1512
Steve Blocka7e24c12009-10-30 11:49:00 +00001513void Genesis::InstallNativeFunctions() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001514 HandleScope scope(isolate());
Steve Blocka7e24c12009-10-30 11:49:00 +00001515 INSTALL_NATIVE(JSFunction, "CreateDate", create_date_fun);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001516
Steve Blocka7e24c12009-10-30 11:49:00 +00001517 INSTALL_NATIVE(JSFunction, "ToNumber", to_number_fun);
1518 INSTALL_NATIVE(JSFunction, "ToString", to_string_fun);
1519 INSTALL_NATIVE(JSFunction, "ToDetailString", to_detail_string_fun);
1520 INSTALL_NATIVE(JSFunction, "ToObject", to_object_fun);
1521 INSTALL_NATIVE(JSFunction, "ToInteger", to_integer_fun);
1522 INSTALL_NATIVE(JSFunction, "ToUint32", to_uint32_fun);
1523 INSTALL_NATIVE(JSFunction, "ToInt32", to_int32_fun);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001524
Leon Clarkee46be812010-01-19 14:06:41 +00001525 INSTALL_NATIVE(JSFunction, "GlobalEval", global_eval_fun);
Steve Blocka7e24c12009-10-30 11:49:00 +00001526 INSTALL_NATIVE(JSFunction, "Instantiate", instantiate_fun);
1527 INSTALL_NATIVE(JSFunction, "ConfigureTemplateInstance",
1528 configure_instance_fun);
Steve Blocka7e24c12009-10-30 11:49:00 +00001529 INSTALL_NATIVE(JSFunction, "GetStackTraceLine", get_stack_trace_line_fun);
1530 INSTALL_NATIVE(JSObject, "functionCache", function_cache);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001531 INSTALL_NATIVE(JSFunction, "ToCompletePropertyDescriptor",
1532 to_complete_property_descriptor);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001533
1534 INSTALL_NATIVE(JSFunction, "IsPromise", is_promise);
1535 INSTALL_NATIVE(JSFunction, "PromiseCreate", promise_create);
1536 INSTALL_NATIVE(JSFunction, "PromiseResolve", promise_resolve);
1537 INSTALL_NATIVE(JSFunction, "PromiseReject", promise_reject);
1538 INSTALL_NATIVE(JSFunction, "PromiseChain", promise_chain);
1539 INSTALL_NATIVE(JSFunction, "PromiseCatch", promise_catch);
1540 INSTALL_NATIVE(JSFunction, "PromiseThen", promise_then);
1541
1542 INSTALL_NATIVE(JSFunction, "NotifyChange", observers_notify_change);
1543 INSTALL_NATIVE(JSFunction, "EnqueueSpliceRecord", observers_enqueue_splice);
1544 INSTALL_NATIVE(JSFunction, "BeginPerformSplice",
1545 observers_begin_perform_splice);
1546 INSTALL_NATIVE(JSFunction, "EndPerformSplice",
1547 observers_end_perform_splice);
1548 INSTALL_NATIVE(JSFunction, "NativeObjectObserve",
1549 native_object_observe);
1550 INSTALL_NATIVE(JSFunction, "NativeObjectGetNotifier",
1551 native_object_get_notifier);
1552 INSTALL_NATIVE(JSFunction, "NativeObjectNotifierPerformChange",
1553 native_object_notifier_perform_change);
1554
1555 INSTALL_NATIVE(Symbol, "symbolIterator", iterator_symbol);
1556 INSTALL_NATIVE(Symbol, "symbolUnscopables", unscopables_symbol);
1557 INSTALL_NATIVE(JSFunction, "ArrayValues", array_values_iterator);
1558
1559 INSTALL_NATIVE_MATH(abs)
1560 INSTALL_NATIVE_MATH(acos)
1561 INSTALL_NATIVE_MATH(asin)
1562 INSTALL_NATIVE_MATH(atan)
1563 INSTALL_NATIVE_MATH(atan2)
1564 INSTALL_NATIVE_MATH(ceil)
1565 INSTALL_NATIVE_MATH(cos)
1566 INSTALL_NATIVE_MATH(exp)
1567 INSTALL_NATIVE_MATH(floor)
1568 INSTALL_NATIVE_MATH(imul)
1569 INSTALL_NATIVE_MATH(log)
1570 INSTALL_NATIVE_MATH(max)
1571 INSTALL_NATIVE_MATH(min)
1572 INSTALL_NATIVE_MATH(pow)
1573 INSTALL_NATIVE_MATH(random)
1574 INSTALL_NATIVE_MATH(round)
1575 INSTALL_NATIVE_MATH(sin)
1576 INSTALL_NATIVE_MATH(sqrt)
1577 INSTALL_NATIVE_MATH(tan)
Steve Blocka7e24c12009-10-30 11:49:00 +00001578}
1579
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001580
Ben Murdoch257744e2011-11-30 15:57:28 +00001581void Genesis::InstallExperimentalNativeFunctions() {
1582 if (FLAG_harmony_proxies) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001583 INSTALL_NATIVE(JSFunction, "DerivedHasTrap", derived_has_trap);
Ben Murdoch257744e2011-11-30 15:57:28 +00001584 INSTALL_NATIVE(JSFunction, "DerivedGetTrap", derived_get_trap);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001585 INSTALL_NATIVE(JSFunction, "DerivedSetTrap", derived_set_trap);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001586 INSTALL_NATIVE(JSFunction, "ProxyEnumerate", proxy_enumerate);
Ben Murdoch257744e2011-11-30 15:57:28 +00001587 }
1588}
1589
Steve Blocka7e24c12009-10-30 11:49:00 +00001590#undef INSTALL_NATIVE
1591
1592
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001593Handle<JSFunction> Genesis::InstallInternalArray(
1594 Handle<JSBuiltinsObject> builtins,
1595 const char* name,
1596 ElementsKind elements_kind) {
1597 // --- I n t e r n a l A r r a y ---
1598 // An array constructor on the builtins object that works like
1599 // the public Array constructor, except that its prototype
1600 // doesn't inherit from Object.prototype.
1601 // To be used only for internal work by builtins. Instances
1602 // must not be leaked to user code.
1603 Handle<JSObject> prototype =
1604 factory()->NewJSObject(isolate()->object_function(), TENURED);
1605 Handle<JSFunction> array_function = InstallFunction(
1606 builtins, name, JS_ARRAY_TYPE, JSArray::kSize,
1607 prototype, Builtins::kInternalArrayCode);
1608
1609 InternalArrayConstructorStub internal_array_constructor_stub(isolate());
1610 Handle<Code> code = internal_array_constructor_stub.GetCode();
1611 array_function->shared()->set_construct_stub(*code);
1612 array_function->shared()->DontAdaptArguments();
1613
1614 Handle<Map> original_map(array_function->initial_map());
1615 Handle<Map> initial_map = Map::Copy(original_map);
1616 initial_map->set_elements_kind(elements_kind);
1617 JSFunction::SetInitialMap(array_function, initial_map, prototype);
1618
1619 // Make "length" magic on instances.
1620 Map::EnsureDescriptorSlack(initial_map, 1);
1621
1622 PropertyAttributes attribs = static_cast<PropertyAttributes>(
1623 DONT_ENUM | DONT_DELETE);
1624
1625 Handle<AccessorInfo> array_length =
1626 Accessors::ArrayLengthInfo(isolate(), attribs);
1627 { // Add length.
1628 CallbacksDescriptor d(
1629 Handle<Name>(Name::cast(array_length->name())), array_length, attribs);
1630 array_function->initial_map()->AppendDescriptor(&d);
1631 }
1632
1633 return array_function;
1634}
1635
1636
Steve Blocka7e24c12009-10-30 11:49:00 +00001637bool Genesis::InstallNatives() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001638 HandleScope scope(isolate());
Steve Blocka7e24c12009-10-30 11:49:00 +00001639
1640 // Create a function for the builtins object. Allocate space for the
1641 // JavaScript builtins, a reference to the builtins object
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001642 // (itself) and a reference to the native_context directly in the object.
Steve Block44f0eee2011-05-26 01:26:41 +01001643 Handle<Code> code = Handle<Code>(
Ben Murdoch257744e2011-11-30 15:57:28 +00001644 isolate()->builtins()->builtin(Builtins::kIllegal));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001645 Handle<JSFunction> builtins_fun = factory()->NewFunction(
1646 factory()->empty_string(), code, JS_BUILTINS_OBJECT_TYPE,
1647 JSBuiltinsObject::kSize);
Steve Blocka7e24c12009-10-30 11:49:00 +00001648
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001649 Handle<String> name =
1650 factory()->InternalizeOneByteString(STATIC_CHAR_VECTOR("builtins"));
Steve Blocka7e24c12009-10-30 11:49:00 +00001651 builtins_fun->shared()->set_instance_class_name(*name);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001652 builtins_fun->initial_map()->set_dictionary_map(true);
1653 builtins_fun->initial_map()->set_prototype(heap()->null_value());
Steve Blocka7e24c12009-10-30 11:49:00 +00001654
1655 // Allocate the builtins object.
1656 Handle<JSBuiltinsObject> builtins =
Ben Murdoch257744e2011-11-30 15:57:28 +00001657 Handle<JSBuiltinsObject>::cast(factory()->NewGlobalObject(builtins_fun));
Steve Blocka7e24c12009-10-30 11:49:00 +00001658 builtins->set_builtins(*builtins);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001659 builtins->set_native_context(*native_context());
1660 builtins->set_global_context(*native_context());
1661 builtins->set_global_proxy(native_context()->global_proxy());
1662
Steve Blocka7e24c12009-10-30 11:49:00 +00001663
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001664 // Set up the 'global' properties of the builtins object. The
Steve Blocka7e24c12009-10-30 11:49:00 +00001665 // 'global' property that refers to the global object is the only
1666 // way to get from code running in the builtins context to the
1667 // global object.
1668 static const PropertyAttributes attributes =
1669 static_cast<PropertyAttributes>(READ_ONLY | DONT_DELETE);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001670 Handle<String> global_string =
1671 factory()->InternalizeOneByteString(STATIC_CHAR_VECTOR("global"));
1672 Handle<Object> global_obj(native_context()->global_object(), isolate());
1673 JSObject::AddProperty(builtins, global_string, global_obj, attributes);
1674 Handle<String> builtins_string =
1675 factory()->InternalizeOneByteString(STATIC_CHAR_VECTOR("builtins"));
1676 JSObject::AddProperty(builtins, builtins_string, builtins, attributes);
Steve Blocka7e24c12009-10-30 11:49:00 +00001677
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001678 // Set up the reference from the global object to the builtins object.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001679 JSGlobalObject::cast(native_context()->global_object())->
1680 set_builtins(*builtins);
Steve Blocka7e24c12009-10-30 11:49:00 +00001681
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001682 // Create a bridge function that has context in the native context.
1683 Handle<JSFunction> bridge = factory()->NewFunction(factory()->empty_string());
1684 DCHECK(bridge->context() == *isolate()->native_context());
Steve Blocka7e24c12009-10-30 11:49:00 +00001685
1686 // Allocate the builtins context.
1687 Handle<Context> context =
Ben Murdoch257744e2011-11-30 15:57:28 +00001688 factory()->NewFunctionContext(Context::MIN_CONTEXT_SLOTS, bridge);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001689 context->set_global_object(*builtins); // override builtins global object
Steve Blocka7e24c12009-10-30 11:49:00 +00001690
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001691 native_context()->set_runtime_context(*context);
Steve Blocka7e24c12009-10-30 11:49:00 +00001692
1693 { // -- S c r i p t
1694 // Builtin functions for Script.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001695 Handle<JSFunction> script_fun = InstallFunction(
1696 builtins, "Script", JS_VALUE_TYPE, JSValue::kSize,
1697 isolate()->initial_object_prototype(), Builtins::kIllegal);
Steve Blocka7e24c12009-10-30 11:49:00 +00001698 Handle<JSObject> prototype =
Ben Murdoch257744e2011-11-30 15:57:28 +00001699 factory()->NewJSObject(isolate()->object_function(), TENURED);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001700 Accessors::FunctionSetPrototype(script_fun, prototype);
1701 native_context()->set_script_function(*script_fun);
Steve Blocka7e24c12009-10-30 11:49:00 +00001702
1703 Handle<Map> script_map = Handle<Map>(script_fun->initial_map());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001704 Map::EnsureDescriptorSlack(script_map, 14);
1705
1706 PropertyAttributes attribs =
1707 static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE | READ_ONLY);
1708
1709 Handle<AccessorInfo> script_column =
1710 Accessors::ScriptColumnOffsetInfo(isolate(), attribs);
1711 {
1712 CallbacksDescriptor d(Handle<Name>(Name::cast(script_column->name())),
1713 script_column, attribs);
1714 script_map->AppendDescriptor(&d);
1715 }
1716
1717 Handle<AccessorInfo> script_id =
1718 Accessors::ScriptIdInfo(isolate(), attribs);
1719 {
1720 CallbacksDescriptor d(Handle<Name>(Name::cast(script_id->name())),
1721 script_id, attribs);
1722 script_map->AppendDescriptor(&d);
1723 }
1724
1725
1726 Handle<AccessorInfo> script_name =
1727 Accessors::ScriptNameInfo(isolate(), attribs);
1728 {
1729 CallbacksDescriptor d(Handle<Name>(Name::cast(script_name->name())),
1730 script_name, attribs);
1731 script_map->AppendDescriptor(&d);
1732 }
1733
1734 Handle<AccessorInfo> script_line =
1735 Accessors::ScriptLineOffsetInfo(isolate(), attribs);
1736 {
1737 CallbacksDescriptor d(Handle<Name>(Name::cast(script_line->name())),
1738 script_line, attribs);
1739 script_map->AppendDescriptor(&d);
1740 }
1741
1742 Handle<AccessorInfo> script_source =
1743 Accessors::ScriptSourceInfo(isolate(), attribs);
1744 {
1745 CallbacksDescriptor d(Handle<Name>(Name::cast(script_source->name())),
1746 script_source, attribs);
1747 script_map->AppendDescriptor(&d);
1748 }
1749
1750 Handle<AccessorInfo> script_type =
1751 Accessors::ScriptTypeInfo(isolate(), attribs);
1752 {
1753 CallbacksDescriptor d(Handle<Name>(Name::cast(script_type->name())),
1754 script_type, attribs);
1755 script_map->AppendDescriptor(&d);
1756 }
1757
1758 Handle<AccessorInfo> script_compilation_type =
1759 Accessors::ScriptCompilationTypeInfo(isolate(), attribs);
1760 {
1761 CallbacksDescriptor d(
1762 Handle<Name>(Name::cast(script_compilation_type->name())),
1763 script_compilation_type, attribs);
1764 script_map->AppendDescriptor(&d);
1765 }
1766
1767 Handle<AccessorInfo> script_line_ends =
1768 Accessors::ScriptLineEndsInfo(isolate(), attribs);
1769 {
1770 CallbacksDescriptor d(Handle<Name>(Name::cast(script_line_ends->name())),
1771 script_line_ends, attribs);
1772 script_map->AppendDescriptor(&d);
1773 }
1774
1775 Handle<AccessorInfo> script_context_data =
1776 Accessors::ScriptContextDataInfo(isolate(), attribs);
1777 {
1778 CallbacksDescriptor d(
1779 Handle<Name>(Name::cast(script_context_data->name())),
1780 script_context_data, attribs);
1781 script_map->AppendDescriptor(&d);
1782 }
1783
1784 Handle<AccessorInfo> script_eval_from_script =
1785 Accessors::ScriptEvalFromScriptInfo(isolate(), attribs);
1786 {
1787 CallbacksDescriptor d(
1788 Handle<Name>(Name::cast(script_eval_from_script->name())),
1789 script_eval_from_script, attribs);
1790 script_map->AppendDescriptor(&d);
1791 }
1792
1793 Handle<AccessorInfo> script_eval_from_script_position =
1794 Accessors::ScriptEvalFromScriptPositionInfo(isolate(), attribs);
1795 {
1796 CallbacksDescriptor d(
1797 Handle<Name>(Name::cast(script_eval_from_script_position->name())),
1798 script_eval_from_script_position, attribs);
1799 script_map->AppendDescriptor(&d);
1800 }
1801
1802 Handle<AccessorInfo> script_eval_from_function_name =
1803 Accessors::ScriptEvalFromFunctionNameInfo(isolate(), attribs);
1804 {
1805 CallbacksDescriptor d(
1806 Handle<Name>(Name::cast(script_eval_from_function_name->name())),
1807 script_eval_from_function_name, attribs);
1808 script_map->AppendDescriptor(&d);
1809 }
1810
1811 Handle<AccessorInfo> script_source_url =
1812 Accessors::ScriptSourceUrlInfo(isolate(), attribs);
1813 {
1814 CallbacksDescriptor d(Handle<Name>(Name::cast(script_source_url->name())),
1815 script_source_url, attribs);
1816 script_map->AppendDescriptor(&d);
1817 }
1818
1819 Handle<AccessorInfo> script_source_mapping_url =
1820 Accessors::ScriptSourceMappingUrlInfo(isolate(), attribs);
1821 {
1822 CallbacksDescriptor d(
1823 Handle<Name>(Name::cast(script_source_mapping_url->name())),
1824 script_source_mapping_url, attribs);
1825 script_map->AppendDescriptor(&d);
1826 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001827
1828 // Allocate the empty script.
Ben Murdoch257744e2011-11-30 15:57:28 +00001829 Handle<Script> script = factory()->NewScript(factory()->empty_string());
Steve Blocka7e24c12009-10-30 11:49:00 +00001830 script->set_type(Smi::FromInt(Script::TYPE_NATIVE));
Ben Murdoch257744e2011-11-30 15:57:28 +00001831 heap()->public_set_empty_script(*script);
Steve Blocka7e24c12009-10-30 11:49:00 +00001832 }
Steve Block6ded16b2010-05-10 14:33:55 +01001833 {
1834 // Builtin function for OpaqueReference -- a JSValue-based object,
1835 // that keeps its field isolated from JavaScript code. It may store
1836 // objects, that JavaScript code may not access.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001837 Handle<JSFunction> opaque_reference_fun = InstallFunction(
1838 builtins, "OpaqueReference", JS_VALUE_TYPE, JSValue::kSize,
1839 isolate()->initial_object_prototype(), Builtins::kIllegal);
Steve Block6ded16b2010-05-10 14:33:55 +01001840 Handle<JSObject> prototype =
Ben Murdoch257744e2011-11-30 15:57:28 +00001841 factory()->NewJSObject(isolate()->object_function(), TENURED);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001842 Accessors::FunctionSetPrototype(opaque_reference_fun, prototype);
1843 native_context()->set_opaque_reference_function(*opaque_reference_fun);
Steve Block6ded16b2010-05-10 14:33:55 +01001844 }
1845
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001846 // InternalArrays should not use Smi-Only array optimizations. There are too
1847 // many places in the C++ runtime code (e.g. RegEx) that assume that
1848 // elements in InternalArrays can be set to non-Smi values without going
1849 // through a common bottleneck that would make the SMI_ONLY -> FAST_ELEMENT
1850 // transition easy to trap. Moreover, they rarely are smi-only.
1851 {
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001852 Handle<JSFunction> array_function =
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001853 InstallInternalArray(builtins, "InternalArray", FAST_HOLEY_ELEMENTS);
1854 native_context()->set_internal_array_function(*array_function);
1855 }
1856
1857 {
1858 InstallInternalArray(builtins, "InternalPackedArray", FAST_ELEMENTS);
1859 }
1860
1861 { // -- S e t I t e r a t o r
1862 Handle<JSFunction> set_iterator_function = InstallFunction(
1863 builtins, "SetIterator", JS_SET_ITERATOR_TYPE, JSSetIterator::kSize,
1864 isolate()->initial_object_prototype(), Builtins::kIllegal);
1865 native_context()->set_set_iterator_map(
1866 set_iterator_function->initial_map());
1867 }
1868
1869 { // -- M a p I t e r a t o r
1870 Handle<JSFunction> map_iterator_function = InstallFunction(
1871 builtins, "MapIterator", JS_MAP_ITERATOR_TYPE, JSMapIterator::kSize,
1872 isolate()->initial_object_prototype(), Builtins::kIllegal);
1873 native_context()->set_map_iterator_map(
1874 map_iterator_function->initial_map());
1875 }
1876
1877 {
1878 // Create generator meta-objects and install them on the builtins object.
1879 Handle<JSObject> builtins(native_context()->builtins());
1880 Handle<JSObject> generator_object_prototype =
Ben Murdoch257744e2011-11-30 15:57:28 +00001881 factory()->NewJSObject(isolate()->object_function(), TENURED);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001882 Handle<JSFunction> generator_function_prototype =
1883 InstallFunction(builtins, "GeneratorFunctionPrototype",
1884 JS_FUNCTION_TYPE, JSFunction::kHeaderSize,
1885 generator_object_prototype, Builtins::kIllegal);
1886 InstallFunction(builtins, "GeneratorFunction", JS_FUNCTION_TYPE,
1887 JSFunction::kSize, generator_function_prototype,
1888 Builtins::kIllegal);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001889
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001890 // Create maps for generator functions and their prototypes. Store those
1891 // maps in the native context.
1892 Handle<Map> generator_function_map =
1893 Map::Copy(sloppy_function_map_writable_prototype_);
1894 generator_function_map->set_prototype(*generator_function_prototype);
1895 native_context()->set_sloppy_generator_function_map(
1896 *generator_function_map);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001897
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001898 // The "arguments" and "caller" instance properties aren't specified, so
1899 // technically we could leave them out. They make even less sense for
1900 // generators than for functions. Still, the same argument that it makes
1901 // sense to keep them around but poisoned in strict mode applies to
1902 // generators as well. With poisoned accessors, naive callers can still
1903 // iterate over the properties without accessing them.
1904 //
1905 // We can't use PoisonArgumentsAndCaller because that mutates accessor pairs
1906 // in place, and the initial state of the generator function map shares the
1907 // accessor pair with sloppy functions. Also the error message should be
1908 // different. Also unhappily, we can't use the API accessors to implement
1909 // poisoning, because API accessors present themselves as data properties,
1910 // not accessor properties, and so getOwnPropertyDescriptor raises an
1911 // exception as it tries to get the values. Sadness.
1912 Handle<AccessorPair> poison_pair(factory()->NewAccessorPair());
1913 PropertyAttributes rw_attribs =
1914 static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE);
1915 Handle<JSFunction> poison_function = GetGeneratorPoisonFunction();
1916 poison_pair->set_getter(*poison_function);
1917 poison_pair->set_setter(*poison_function);
1918 ReplaceAccessors(generator_function_map, factory()->arguments_string(),
1919 rw_attribs, poison_pair);
1920 ReplaceAccessors(generator_function_map, factory()->caller_string(),
1921 rw_attribs, poison_pair);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001922
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001923 Handle<Map> strict_function_map(native_context()->strict_function_map());
1924 Handle<Map> strict_generator_function_map = Map::Copy(strict_function_map);
1925 // "arguments" and "caller" already poisoned.
1926 strict_generator_function_map->set_prototype(*generator_function_prototype);
1927 native_context()->set_strict_generator_function_map(
1928 *strict_generator_function_map);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001929
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001930 Handle<JSFunction> object_function(native_context()->object_function());
1931 Handle<Map> generator_object_prototype_map = Map::Create(isolate(), 0);
1932 generator_object_prototype_map->set_prototype(*generator_object_prototype);
1933 native_context()->set_generator_object_prototype_map(
1934 *generator_object_prototype_map);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001935 }
1936
Steve Block6ded16b2010-05-10 14:33:55 +01001937 if (FLAG_disable_native_files) {
1938 PrintF("Warning: Running without installed natives!\n");
1939 return true;
1940 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001941
Andrei Popescu31002712010-02-23 13:46:05 +00001942 // Install natives.
1943 for (int i = Natives::GetDebuggerCount();
1944 i < Natives::GetBuiltinsCount();
1945 i++) {
Ben Murdoch257744e2011-11-30 15:57:28 +00001946 if (!CompileBuiltin(isolate(), i)) return false;
Andrei Popescu402d9372010-02-26 13:31:12 +00001947 // TODO(ager): We really only need to install the JS builtin
1948 // functions on the builtins object after compiling and running
1949 // runtime.js.
1950 if (!InstallJSBuiltins(builtins)) return false;
Steve Blocka7e24c12009-10-30 11:49:00 +00001951 }
1952
1953 InstallNativeFunctions();
1954
Iain Merrick75681382010-08-19 15:07:18 +01001955 // Store the map for the string prototype after the natives has been compiled
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001956 // and the String function has been set up.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001957 Handle<JSFunction> string_function(native_context()->string_function());
1958 DCHECK(JSObject::cast(
Iain Merrick75681382010-08-19 15:07:18 +01001959 string_function->initial_map()->prototype())->HasFastProperties());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001960 native_context()->set_string_function_prototype_map(
Iain Merrick75681382010-08-19 15:07:18 +01001961 HeapObject::cast(string_function->initial_map()->prototype())->map());
1962
Steve Blocka7e24c12009-10-30 11:49:00 +00001963 // Install Function.prototype.call and apply.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001964 {
1965 Handle<String> key = factory()->Function_string();
Steve Blocka7e24c12009-10-30 11:49:00 +00001966 Handle<JSFunction> function =
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001967 Handle<JSFunction>::cast(Object::GetProperty(
1968 handle(native_context()->global_object()), key).ToHandleChecked());
Steve Blocka7e24c12009-10-30 11:49:00 +00001969 Handle<JSObject> proto =
1970 Handle<JSObject>(JSObject::cast(function->instance_prototype()));
1971
1972 // Install the call and the apply functions.
1973 Handle<JSFunction> call =
1974 InstallFunction(proto, "call", JS_OBJECT_TYPE, JSObject::kHeaderSize,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001975 MaybeHandle<JSObject>(), Builtins::kFunctionCall);
Steve Blocka7e24c12009-10-30 11:49:00 +00001976 Handle<JSFunction> apply =
1977 InstallFunction(proto, "apply", JS_OBJECT_TYPE, JSObject::kHeaderSize,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001978 MaybeHandle<JSObject>(), Builtins::kFunctionApply);
1979 if (FLAG_vector_ics) {
1980 // Apply embeds an IC, so we need a type vector of size 1 in the shared
1981 // function info.
1982 Handle<TypeFeedbackVector> feedback_vector =
1983 factory()->NewTypeFeedbackVector(1);
1984 apply->shared()->set_feedback_vector(*feedback_vector);
1985 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001986
1987 // Make sure that Function.prototype.call appears to be compiled.
1988 // The code will never be called, but inline caching for call will
1989 // only work if it appears to be compiled.
1990 call->shared()->DontAdaptArguments();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001991 DCHECK(call->is_compiled());
Steve Blocka7e24c12009-10-30 11:49:00 +00001992
1993 // Set the expected parameters for apply to 2; required by builtin.
1994 apply->shared()->set_formal_parameter_count(2);
1995
1996 // Set the lengths for the functions to satisfy ECMA-262.
1997 call->shared()->set_length(1);
1998 apply->shared()->set_length(2);
1999 }
2000
Ben Murdoch42effa52011-08-19 16:40:31 +01002001 InstallBuiltinFunctionIds();
2002
Steve Block6ded16b2010-05-10 14:33:55 +01002003 // Create a constructor for RegExp results (a variant of Array that
2004 // predefines the two properties index and match).
2005 {
2006 // RegExpResult initial map.
2007
2008 // Find global.Array.prototype to inherit from.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002009 Handle<JSFunction> array_constructor(native_context()->array_function());
Steve Block6ded16b2010-05-10 14:33:55 +01002010 Handle<JSObject> array_prototype(
2011 JSObject::cast(array_constructor->instance_prototype()));
2012
2013 // Add initial map.
2014 Handle<Map> initial_map =
Ben Murdoch257744e2011-11-30 15:57:28 +00002015 factory()->NewMap(JS_ARRAY_TYPE, JSRegExpResult::kSize);
Steve Block6ded16b2010-05-10 14:33:55 +01002016 initial_map->set_constructor(*array_constructor);
2017
2018 // Set prototype on map.
2019 initial_map->set_non_instance_prototype(false);
2020 initial_map->set_prototype(*array_prototype);
2021
2022 // Update map with length accessor from Array and add "index" and "input".
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002023 Map::EnsureDescriptorSlack(initial_map, 3);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00002024
Steve Block6ded16b2010-05-10 14:33:55 +01002025 {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002026 JSFunction* array_function = native_context()->array_function();
2027 Handle<DescriptorArray> array_descriptors(
2028 array_function->initial_map()->instance_descriptors());
2029 Handle<String> length = factory()->length_string();
2030 int old = array_descriptors->SearchWithCache(
2031 *length, array_function->initial_map());
2032 DCHECK(old != DescriptorArray::kNotFound);
2033 CallbacksDescriptor desc(length,
2034 handle(array_descriptors->GetValue(old),
2035 isolate()),
2036 array_descriptors->GetDetails(old).attributes());
2037 initial_map->AppendDescriptor(&desc);
2038 }
2039 {
2040 FieldDescriptor index_field(factory()->index_string(),
Steve Block6ded16b2010-05-10 14:33:55 +01002041 JSRegExpResult::kIndexIndex,
2042 NONE,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002043 Representation::Tagged());
2044 initial_map->AppendDescriptor(&index_field);
Steve Block6ded16b2010-05-10 14:33:55 +01002045 }
2046
2047 {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002048 FieldDescriptor input_field(factory()->input_string(),
Steve Block6ded16b2010-05-10 14:33:55 +01002049 JSRegExpResult::kInputIndex,
2050 NONE,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002051 Representation::Tagged());
2052 initial_map->AppendDescriptor(&input_field);
Steve Block6ded16b2010-05-10 14:33:55 +01002053 }
Steve Block6ded16b2010-05-10 14:33:55 +01002054
2055 initial_map->set_inobject_properties(2);
2056 initial_map->set_pre_allocated_property_fields(2);
2057 initial_map->set_unused_property_fields(0);
Steve Block6ded16b2010-05-10 14:33:55 +01002058
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002059 native_context()->set_regexp_result_map(*initial_map);
Steve Block6ded16b2010-05-10 14:33:55 +01002060 }
2061
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002062 // Add @@iterator method to the arguments object maps.
2063 {
2064 PropertyAttributes attribs = DONT_ENUM;
2065 Handle<AccessorInfo> arguments_iterator =
2066 Accessors::ArgumentsIteratorInfo(isolate(), attribs);
2067 {
2068 CallbacksDescriptor d(Handle<Name>(native_context()->iterator_symbol()),
2069 arguments_iterator, attribs);
2070 Handle<Map> map(native_context()->sloppy_arguments_map());
2071 Map::EnsureDescriptorSlack(map, 1);
2072 map->AppendDescriptor(&d);
2073 }
2074 {
2075 CallbacksDescriptor d(Handle<Name>(native_context()->iterator_symbol()),
2076 arguments_iterator, attribs);
2077 Handle<Map> map(native_context()->aliased_arguments_map());
2078 Map::EnsureDescriptorSlack(map, 1);
2079 map->AppendDescriptor(&d);
2080 }
2081 {
2082 CallbacksDescriptor d(Handle<Name>(native_context()->iterator_symbol()),
2083 arguments_iterator, attribs);
2084 Handle<Map> map(native_context()->strict_arguments_map());
2085 Map::EnsureDescriptorSlack(map, 1);
2086 map->AppendDescriptor(&d);
2087 }
2088 }
2089
2090#ifdef VERIFY_HEAP
2091 builtins->ObjectVerify();
Steve Blocka7e24c12009-10-30 11:49:00 +00002092#endif
Andrei Popescu31002712010-02-23 13:46:05 +00002093
Steve Blocka7e24c12009-10-30 11:49:00 +00002094 return true;
2095}
2096
2097
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002098#define INSTALL_EXPERIMENTAL_NATIVE(i, flag, file) \
2099 if (FLAG_harmony_##flag && \
2100 strcmp(ExperimentalNatives::GetScriptName(i).start(), \
2101 "native " file) == 0) { \
2102 if (!CompileExperimentalBuiltin(isolate(), i)) return false; \
2103 }
2104
2105
Ben Murdoch257744e2011-11-30 15:57:28 +00002106bool Genesis::InstallExperimentalNatives() {
2107 for (int i = ExperimentalNatives::GetDebuggerCount();
2108 i < ExperimentalNatives::GetBuiltinsCount();
2109 i++) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002110 INSTALL_EXPERIMENTAL_NATIVE(i, proxies, "proxy.js")
2111 INSTALL_EXPERIMENTAL_NATIVE(i, strings, "harmony-string.js")
2112 INSTALL_EXPERIMENTAL_NATIVE(i, arrays, "harmony-array.js")
2113 INSTALL_EXPERIMENTAL_NATIVE(i, classes, "harmony-classes.js")
Ben Murdoch257744e2011-11-30 15:57:28 +00002114 }
2115
2116 InstallExperimentalNativeFunctions();
Ben Murdoch257744e2011-11-30 15:57:28 +00002117 return true;
2118}
2119
2120
Ben Murdochb0fe1622011-05-05 13:52:32 +01002121static void InstallBuiltinFunctionId(Handle<JSObject> holder,
2122 const char* function_name,
2123 BuiltinFunctionId id) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002124 Isolate* isolate = holder->GetIsolate();
2125 Handle<Object> function_object =
2126 Object::GetProperty(isolate, holder, function_name).ToHandleChecked();
2127 Handle<JSFunction> function = Handle<JSFunction>::cast(function_object);
Kristian Monsen25f61362010-05-21 11:50:48 +01002128 function->shared()->set_function_data(Smi::FromInt(id));
2129}
2130
2131
Ben Murdochb0fe1622011-05-05 13:52:32 +01002132void Genesis::InstallBuiltinFunctionIds() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002133 HandleScope scope(isolate());
Ben Murdochb0fe1622011-05-05 13:52:32 +01002134#define INSTALL_BUILTIN_ID(holder_expr, fun_name, name) \
2135 { \
2136 Handle<JSObject> holder = ResolveBuiltinIdHolder( \
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002137 native_context(), #holder_expr); \
Ben Murdochb0fe1622011-05-05 13:52:32 +01002138 BuiltinFunctionId id = k##name; \
2139 InstallBuiltinFunctionId(holder, #fun_name, id); \
Kristian Monsen25f61362010-05-21 11:50:48 +01002140 }
Ben Murdochb0fe1622011-05-05 13:52:32 +01002141 FUNCTIONS_WITH_ID_LIST(INSTALL_BUILTIN_ID)
2142#undef INSTALL_BUILTIN_ID
Kristian Monsen25f61362010-05-21 11:50:48 +01002143}
2144
2145
Steve Block6ded16b2010-05-10 14:33:55 +01002146// Do not forget to update macros.py with named constant
2147// of cache id.
2148#define JSFUNCTION_RESULT_CACHE_LIST(F) \
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002149 F(16, native_context()->regexp_function())
Steve Block6ded16b2010-05-10 14:33:55 +01002150
2151
Ben Murdoch257744e2011-11-30 15:57:28 +00002152static FixedArray* CreateCache(int size, Handle<JSFunction> factory_function) {
2153 Factory* factory = factory_function->GetIsolate()->factory();
Steve Block6ded16b2010-05-10 14:33:55 +01002154 // Caches are supposed to live for a long time, allocate in old space.
2155 int array_size = JSFunctionResultCache::kEntriesIndex + 2 * size;
2156 // Cannot use cast as object is not fully initialized yet.
2157 JSFunctionResultCache* cache = reinterpret_cast<JSFunctionResultCache*>(
Ben Murdoch257744e2011-11-30 15:57:28 +00002158 *factory->NewFixedArrayWithHoles(array_size, TENURED));
2159 cache->set(JSFunctionResultCache::kFactoryIndex, *factory_function);
Steve Block6ded16b2010-05-10 14:33:55 +01002160 cache->MakeZeroSize();
2161 return cache;
2162}
2163
2164
2165void Genesis::InstallJSFunctionResultCaches() {
2166 const int kNumberOfCaches = 0 +
2167#define F(size, func) + 1
2168 JSFUNCTION_RESULT_CACHE_LIST(F)
2169#undef F
2170 ;
2171
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002172 Handle<FixedArray> caches =
2173 factory()->NewFixedArray(kNumberOfCaches, TENURED);
Steve Block6ded16b2010-05-10 14:33:55 +01002174
2175 int index = 0;
Kristian Monsen0d5e1162010-09-30 15:31:59 +01002176
Ben Murdoch257744e2011-11-30 15:57:28 +00002177#define F(size, func) do { \
2178 FixedArray* cache = CreateCache((size), Handle<JSFunction>(func)); \
2179 caches->set(index++, cache); \
Kristian Monsen0d5e1162010-09-30 15:31:59 +01002180 } while (false)
2181
2182 JSFUNCTION_RESULT_CACHE_LIST(F);
2183
Steve Block6ded16b2010-05-10 14:33:55 +01002184#undef F
2185
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002186 native_context()->set_jsfunction_result_caches(*caches);
Steve Block6ded16b2010-05-10 14:33:55 +01002187}
2188
2189
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002190void Genesis::InitializeNormalizedMapCaches() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002191 Handle<NormalizedMapCache> cache = NormalizedMapCache::New(isolate());
2192 native_context()->set_normalized_map_cache(*cache);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002193}
2194
2195
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002196bool Bootstrapper::InstallExtensions(Handle<Context> native_context,
Andrei Popescu31002712010-02-23 13:46:05 +00002197 v8::ExtensionConfiguration* extensions) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002198 BootstrapperActive active(this);
2199 SaveContext saved_context(isolate_);
2200 isolate_->set_context(*native_context);
2201 return Genesis::InstallExtensions(native_context, extensions) &&
2202 Genesis::InstallSpecialObjects(native_context);
2203}
2204
2205
2206bool Genesis::InstallSpecialObjects(Handle<Context> native_context) {
2207 Isolate* isolate = native_context->GetIsolate();
2208 // Don't install extensions into the snapshot.
2209 if (isolate->serializer_enabled()) return true;
2210
2211 Factory* factory = isolate->factory();
2212 HandleScope scope(isolate);
2213 Handle<JSGlobalObject> global(JSGlobalObject::cast(
2214 native_context->global_object()));
2215
2216 Handle<JSObject> Error = Handle<JSObject>::cast(
2217 Object::GetProperty(isolate, global, "Error").ToHandleChecked());
2218 Handle<String> name =
2219 factory->InternalizeOneByteString(STATIC_CHAR_VECTOR("stackTraceLimit"));
2220 Handle<Smi> stack_trace_limit(Smi::FromInt(FLAG_stack_trace_limit), isolate);
2221 JSObject::AddProperty(Error, name, stack_trace_limit, NONE);
2222
2223 // Expose the natives in global if a name for it is specified.
2224 if (FLAG_expose_natives_as != NULL && strlen(FLAG_expose_natives_as) != 0) {
2225 Handle<String> natives =
2226 factory->InternalizeUtf8String(FLAG_expose_natives_as);
2227 uint32_t dummy_index;
2228 if (natives->AsArrayIndex(&dummy_index)) return true;
2229 JSObject::AddProperty(global, natives, handle(global->builtins()),
2230 DONT_ENUM);
2231 }
2232
2233 // Expose the stack trace symbol to native JS.
2234 RETURN_ON_EXCEPTION_VALUE(isolate,
2235 JSObject::SetOwnPropertyIgnoreAttributes(
2236 handle(native_context->builtins(), isolate),
2237 factory->InternalizeOneByteString(
2238 STATIC_CHAR_VECTOR("stack_trace_symbol")),
2239 factory->stack_trace_symbol(), NONE),
2240 false);
2241
2242 // Expose the debug global object in global if a name for it is specified.
2243 if (FLAG_expose_debug_as != NULL && strlen(FLAG_expose_debug_as) != 0) {
2244 // If loading fails we just bail out without installing the
2245 // debugger but without tanking the whole context.
2246 Debug* debug = isolate->debug();
2247 if (!debug->Load()) return true;
2248 Handle<Context> debug_context = debug->debug_context();
2249 // Set the security token for the debugger context to the same as
2250 // the shell native context to allow calling between these (otherwise
2251 // exposing debug global object doesn't make much sense).
2252 debug_context->set_security_token(native_context->security_token());
2253 Handle<String> debug_string =
2254 factory->InternalizeUtf8String(FLAG_expose_debug_as);
2255 uint32_t index;
2256 if (debug_string->AsArrayIndex(&index)) return true;
2257 Handle<Object> global_proxy(debug_context->global_proxy(), isolate);
2258 JSObject::AddProperty(global, debug_string, global_proxy, DONT_ENUM);
2259 }
Andrei Popescu31002712010-02-23 13:46:05 +00002260 return true;
2261}
2262
2263
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002264static uint32_t Hash(RegisteredExtension* extension) {
2265 return v8::internal::ComputePointerHash(extension);
2266}
2267
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002268
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002269Genesis::ExtensionStates::ExtensionStates() : map_(HashMap::PointersMatch, 8) {}
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002270
2271Genesis::ExtensionTraversalState Genesis::ExtensionStates::get_state(
2272 RegisteredExtension* extension) {
2273 i::HashMap::Entry* entry = map_.Lookup(extension, Hash(extension), false);
2274 if (entry == NULL) {
2275 return UNVISITED;
2276 }
2277 return static_cast<ExtensionTraversalState>(
2278 reinterpret_cast<intptr_t>(entry->value));
2279}
2280
2281void Genesis::ExtensionStates::set_state(RegisteredExtension* extension,
2282 ExtensionTraversalState state) {
2283 map_.Lookup(extension, Hash(extension), true)->value =
2284 reinterpret_cast<void*>(static_cast<intptr_t>(state));
2285}
Steve Blocka7e24c12009-10-30 11:49:00 +00002286
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002287
2288bool Genesis::InstallExtensions(Handle<Context> native_context,
Andrei Popescu31002712010-02-23 13:46:05 +00002289 v8::ExtensionConfiguration* extensions) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002290 Isolate* isolate = native_context->GetIsolate();
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002291 ExtensionStates extension_states; // All extensions have state UNVISITED.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002292 return InstallAutoExtensions(isolate, &extension_states) &&
2293 (!FLAG_expose_free_buffer ||
2294 InstallExtension(isolate, "v8/free-buffer", &extension_states)) &&
2295 (!FLAG_expose_gc ||
2296 InstallExtension(isolate, "v8/gc", &extension_states)) &&
2297 (!FLAG_expose_externalize_string ||
2298 InstallExtension(isolate, "v8/externalize", &extension_states)) &&
2299 (!FLAG_track_gc_object_stats ||
2300 InstallExtension(isolate, "v8/statistics", &extension_states)) &&
2301 (!FLAG_expose_trigger_failure ||
2302 InstallExtension(isolate, "v8/trigger-failure", &extension_states)) &&
2303 InstallRequestedExtensions(isolate, extensions, &extension_states);
2304}
Steve Blocka7e24c12009-10-30 11:49:00 +00002305
Steve Blocka7e24c12009-10-30 11:49:00 +00002306
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002307bool Genesis::InstallAutoExtensions(Isolate* isolate,
2308 ExtensionStates* extension_states) {
2309 for (v8::RegisteredExtension* it = v8::RegisteredExtension::first_extension();
2310 it != NULL;
2311 it = it->next()) {
2312 if (it->extension()->auto_enable() &&
2313 !InstallExtension(isolate, it, extension_states)) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002314 return false;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002315 }
Steve Blocka7e24c12009-10-30 11:49:00 +00002316 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002317 return true;
2318}
Steve Blocka7e24c12009-10-30 11:49:00 +00002319
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002320
2321bool Genesis::InstallRequestedExtensions(Isolate* isolate,
2322 v8::ExtensionConfiguration* extensions,
2323 ExtensionStates* extension_states) {
2324 for (const char** it = extensions->begin(); it != extensions->end(); ++it) {
2325 if (!InstallExtension(isolate, *it, extension_states)) return false;
2326 }
Steve Blocka7e24c12009-10-30 11:49:00 +00002327 return true;
2328}
2329
2330
2331// Installs a named extension. This methods is unoptimized and does
2332// not scale well if we want to support a large number of extensions.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002333bool Genesis::InstallExtension(Isolate* isolate,
2334 const char* name,
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002335 ExtensionStates* extension_states) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002336 for (v8::RegisteredExtension* it = v8::RegisteredExtension::first_extension();
2337 it != NULL;
2338 it = it->next()) {
2339 if (strcmp(name, it->extension()->name()) == 0) {
2340 return InstallExtension(isolate, it, extension_states);
2341 }
Steve Blocka7e24c12009-10-30 11:49:00 +00002342 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002343 return Utils::ApiCheck(false,
2344 "v8::Context::New()",
2345 "Cannot find required extension");
Steve Blocka7e24c12009-10-30 11:49:00 +00002346}
2347
2348
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002349bool Genesis::InstallExtension(Isolate* isolate,
2350 v8::RegisteredExtension* current,
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002351 ExtensionStates* extension_states) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002352 HandleScope scope(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00002353
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002354 if (extension_states->get_state(current) == INSTALLED) return true;
Steve Blocka7e24c12009-10-30 11:49:00 +00002355 // The current node has already been visited so there must be a
2356 // cycle in the dependency graph; fail.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002357 if (!Utils::ApiCheck(extension_states->get_state(current) != VISITED,
2358 "v8::Context::New()",
2359 "Circular extension dependency")) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002360 return false;
2361 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002362 DCHECK(extension_states->get_state(current) == UNVISITED);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002363 extension_states->set_state(current, VISITED);
Steve Blocka7e24c12009-10-30 11:49:00 +00002364 v8::Extension* extension = current->extension();
2365 // Install the extension's dependencies
2366 for (int i = 0; i < extension->dependency_count(); i++) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002367 if (!InstallExtension(isolate,
2368 extension->dependencies()[i],
2369 extension_states)) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002370 return false;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002371 }
Steve Blocka7e24c12009-10-30 11:49:00 +00002372 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002373 // We do not expect this to throw an exception. Change this if it does.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002374 Handle<String> source_code =
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002375 isolate->factory()
2376 ->NewExternalStringFromOneByte(extension->source())
2377 .ToHandleChecked();
2378 bool result = CompileScriptCached(isolate,
2379 CStrVector(extension->name()),
2380 source_code,
2381 isolate->bootstrapper()->extensions_cache(),
2382 extension,
2383 Handle<Context>(isolate->context()),
2384 false);
2385 DCHECK(isolate->has_pending_exception() != result);
Steve Blocka7e24c12009-10-30 11:49:00 +00002386 if (!result) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002387 // We print out the name of the extension that fail to install.
2388 // When an error is thrown during bootstrapping we automatically print
2389 // the line number at which this happened to the console in the isolate
2390 // error throwing functionality.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002391 base::OS::PrintError("Error installing extension '%s'.\n",
2392 current->extension()->name());
Steve Block44f0eee2011-05-26 01:26:41 +01002393 isolate->clear_pending_exception();
Steve Blocka7e24c12009-10-30 11:49:00 +00002394 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002395 extension_states->set_state(current, INSTALLED);
2396 isolate->NotifyExtensionInstalled();
Steve Blocka7e24c12009-10-30 11:49:00 +00002397 return result;
2398}
2399
2400
Andrei Popescu402d9372010-02-26 13:31:12 +00002401bool Genesis::InstallJSBuiltins(Handle<JSBuiltinsObject> builtins) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002402 HandleScope scope(isolate());
Andrei Popescu402d9372010-02-26 13:31:12 +00002403 for (int i = 0; i < Builtins::NumberOfJavaScriptBuiltins(); i++) {
2404 Builtins::JavaScript id = static_cast<Builtins::JavaScript>(i);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002405 Handle<Object> function_object = Object::GetProperty(
2406 isolate(), builtins, Builtins::GetName(id)).ToHandleChecked();
2407 Handle<JSFunction> function = Handle<JSFunction>::cast(function_object);
Andrei Popescu402d9372010-02-26 13:31:12 +00002408 builtins->set_javascript_builtin(id, *function);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002409 // TODO(mstarzinger): This is just a temporary hack to make TurboFan work,
2410 // the correct solution is to restore the context register after invoking
2411 // builtins from full-codegen.
2412 function->shared()->set_optimization_disabled(true);
2413 if (!Compiler::EnsureCompiled(function, CLEAR_EXCEPTION)) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002414 return false;
2415 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002416 builtins->set_javascript_builtin_code(id, function->shared()->code());
Andrei Popescu402d9372010-02-26 13:31:12 +00002417 }
2418 return true;
Andrei Popescu31002712010-02-23 13:46:05 +00002419}
2420
2421
Steve Blocka7e24c12009-10-30 11:49:00 +00002422bool Genesis::ConfigureGlobalObjects(
2423 v8::Handle<v8::ObjectTemplate> global_proxy_template) {
2424 Handle<JSObject> global_proxy(
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002425 JSObject::cast(native_context()->global_proxy()));
2426 Handle<JSObject> global_object(
2427 JSObject::cast(native_context()->global_object()));
Steve Blocka7e24c12009-10-30 11:49:00 +00002428
2429 if (!global_proxy_template.IsEmpty()) {
2430 // Configure the global proxy object.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002431 Handle<ObjectTemplateInfo> global_proxy_data =
Steve Blocka7e24c12009-10-30 11:49:00 +00002432 v8::Utils::OpenHandle(*global_proxy_template);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002433 if (!ConfigureApiObject(global_proxy, global_proxy_data)) return false;
Steve Blocka7e24c12009-10-30 11:49:00 +00002434
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002435 // Configure the global object.
Steve Blocka7e24c12009-10-30 11:49:00 +00002436 Handle<FunctionTemplateInfo> proxy_constructor(
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002437 FunctionTemplateInfo::cast(global_proxy_data->constructor()));
Steve Blocka7e24c12009-10-30 11:49:00 +00002438 if (!proxy_constructor->prototype_template()->IsUndefined()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002439 Handle<ObjectTemplateInfo> global_object_data(
Steve Blocka7e24c12009-10-30 11:49:00 +00002440 ObjectTemplateInfo::cast(proxy_constructor->prototype_template()));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002441 if (!ConfigureApiObject(global_object, global_object_data)) return false;
Steve Blocka7e24c12009-10-30 11:49:00 +00002442 }
2443 }
2444
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002445 SetObjectPrototype(global_proxy, global_object);
2446
2447 native_context()->set_initial_array_prototype(
2448 JSArray::cast(native_context()->array_function()->prototype()));
2449
Steve Blocka7e24c12009-10-30 11:49:00 +00002450 return true;
2451}
2452
2453
2454bool Genesis::ConfigureApiObject(Handle<JSObject> object,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002455 Handle<ObjectTemplateInfo> object_template) {
2456 DCHECK(!object_template.is_null());
2457 DCHECK(FunctionTemplateInfo::cast(object_template->constructor())
2458 ->IsTemplateFor(object->map()));;
Steve Blocka7e24c12009-10-30 11:49:00 +00002459
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002460 MaybeHandle<JSObject> maybe_obj =
2461 Execution::InstantiateObject(object_template);
2462 Handle<JSObject> obj;
2463 if (!maybe_obj.ToHandle(&obj)) {
2464 DCHECK(isolate()->has_pending_exception());
Ben Murdoch257744e2011-11-30 15:57:28 +00002465 isolate()->clear_pending_exception();
Steve Blocka7e24c12009-10-30 11:49:00 +00002466 return false;
2467 }
2468 TransferObject(obj, object);
2469 return true;
2470}
2471
2472
2473void Genesis::TransferNamedProperties(Handle<JSObject> from,
2474 Handle<JSObject> to) {
2475 if (from->HasFastProperties()) {
2476 Handle<DescriptorArray> descs =
2477 Handle<DescriptorArray>(from->map()->instance_descriptors());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002478 for (int i = 0; i < from->map()->NumberOfOwnDescriptors(); i++) {
2479 PropertyDetails details = descs->GetDetails(i);
Steve Blocka7e24c12009-10-30 11:49:00 +00002480 switch (details.type()) {
2481 case FIELD: {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002482 HandleScope inner(isolate());
2483 Handle<Name> key = Handle<Name>(descs->GetKey(i));
2484 FieldIndex index = FieldIndex::ForDescriptor(from->map(), i);
2485 DCHECK(!descs->GetDetails(i).representation().IsDouble());
2486 Handle<Object> value = Handle<Object>(from->RawFastPropertyAt(index),
2487 isolate());
2488 JSObject::AddProperty(to, key, value, details.attributes());
Steve Blocka7e24c12009-10-30 11:49:00 +00002489 break;
2490 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002491 case CONSTANT: {
2492 HandleScope inner(isolate());
2493 Handle<Name> key = Handle<Name>(descs->GetKey(i));
2494 Handle<Object> constant(descs->GetConstant(i), isolate());
2495 JSObject::AddProperty(to, key, constant, details.attributes());
Steve Blocka7e24c12009-10-30 11:49:00 +00002496 break;
2497 }
2498 case CALLBACKS: {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002499 Handle<Name> key(descs->GetKey(i));
2500 LookupIterator it(to, key, LookupIterator::OWN_SKIP_INTERCEPTOR);
2501 CHECK_NE(LookupIterator::ACCESS_CHECK, it.state());
Steve Blocka7e24c12009-10-30 11:49:00 +00002502 // If the property is already there we skip it
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002503 if (it.IsFound()) continue;
2504 HandleScope inner(isolate());
2505 DCHECK(!to->HasFastProperties());
Andrei Popescu31002712010-02-23 13:46:05 +00002506 // Add to dictionary.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002507 Handle<Object> callbacks(descs->GetCallbacksObject(i), isolate());
2508 PropertyDetails d = PropertyDetails(
2509 details.attributes(), CALLBACKS, i + 1);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002510 JSObject::SetNormalizedProperty(to, key, callbacks, d);
Steve Blocka7e24c12009-10-30 11:49:00 +00002511 break;
2512 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002513 // Do not occur since the from object has fast properties.
Steve Blocka7e24c12009-10-30 11:49:00 +00002514 case NORMAL:
Steve Blocka7e24c12009-10-30 11:49:00 +00002515 UNREACHABLE();
2516 break;
2517 }
2518 }
2519 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002520 Handle<NameDictionary> properties =
2521 Handle<NameDictionary>(from->property_dictionary());
Steve Blocka7e24c12009-10-30 11:49:00 +00002522 int capacity = properties->Capacity();
2523 for (int i = 0; i < capacity; i++) {
2524 Object* raw_key(properties->KeyAt(i));
2525 if (properties->IsKey(raw_key)) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002526 DCHECK(raw_key->IsName());
Steve Blocka7e24c12009-10-30 11:49:00 +00002527 // If the property is already there we skip it.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002528 Handle<Name> key(Name::cast(raw_key));
2529 LookupIterator it(to, key, LookupIterator::OWN_SKIP_INTERCEPTOR);
2530 CHECK_NE(LookupIterator::ACCESS_CHECK, it.state());
2531 if (it.IsFound()) continue;
Steve Blocka7e24c12009-10-30 11:49:00 +00002532 // Set the property.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002533 Handle<Object> value = Handle<Object>(properties->ValueAt(i),
2534 isolate());
2535 DCHECK(!value->IsCell());
2536 if (value->IsPropertyCell()) {
2537 value = Handle<Object>(PropertyCell::cast(*value)->value(),
2538 isolate());
Steve Blocka7e24c12009-10-30 11:49:00 +00002539 }
2540 PropertyDetails details = properties->DetailsAt(i);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002541 JSObject::AddProperty(to, key, value, details.attributes());
Steve Blocka7e24c12009-10-30 11:49:00 +00002542 }
2543 }
2544 }
2545}
2546
2547
2548void Genesis::TransferIndexedProperties(Handle<JSObject> from,
2549 Handle<JSObject> to) {
2550 // Cloning the elements array is sufficient.
2551 Handle<FixedArray> from_elements =
2552 Handle<FixedArray>(FixedArray::cast(from->elements()));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002553 Handle<FixedArray> to_elements = factory()->CopyFixedArray(from_elements);
Steve Blocka7e24c12009-10-30 11:49:00 +00002554 to->set_elements(*to_elements);
2555}
2556
2557
2558void Genesis::TransferObject(Handle<JSObject> from, Handle<JSObject> to) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002559 HandleScope outer(isolate());
Steve Blocka7e24c12009-10-30 11:49:00 +00002560
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002561 DCHECK(!from->IsJSArray());
2562 DCHECK(!to->IsJSArray());
Steve Blocka7e24c12009-10-30 11:49:00 +00002563
2564 TransferNamedProperties(from, to);
2565 TransferIndexedProperties(from, to);
2566
2567 // Transfer the prototype (new map is needed).
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002568 Handle<Object> proto(from->map()->prototype(), isolate());
2569 SetObjectPrototype(to, proto);
Steve Blocka7e24c12009-10-30 11:49:00 +00002570}
2571
2572
2573void Genesis::MakeFunctionInstancePrototypeWritable() {
Steve Block44f0eee2011-05-26 01:26:41 +01002574 // The maps with writable prototype are created in CreateEmptyFunction
2575 // and CreateStrictModeFunctionMaps respectively. Initially the maps are
2576 // created with read-only prototype for JS builtins processing.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002577 DCHECK(!sloppy_function_map_writable_prototype_.is_null());
2578 DCHECK(!strict_function_map_writable_prototype_.is_null());
Steve Blocka7e24c12009-10-30 11:49:00 +00002579
Steve Block44f0eee2011-05-26 01:26:41 +01002580 // Replace function instance maps to make prototype writable.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002581 native_context()->set_sloppy_function_map(
2582 *sloppy_function_map_writable_prototype_);
2583 native_context()->set_strict_function_map(
2584 *strict_function_map_writable_prototype_);
Steve Blocka7e24c12009-10-30 11:49:00 +00002585}
2586
2587
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002588class NoTrackDoubleFieldsForSerializerScope {
2589 public:
2590 explicit NoTrackDoubleFieldsForSerializerScope(Isolate* isolate)
2591 : flag_(FLAG_track_double_fields) {
2592 if (isolate->serializer_enabled()) {
2593 // Disable tracking double fields because heap numbers treated as
2594 // immutable by the serializer.
2595 FLAG_track_double_fields = false;
2596 }
2597 }
Steve Blocka7e24c12009-10-30 11:49:00 +00002598
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002599 ~NoTrackDoubleFieldsForSerializerScope() {
2600 FLAG_track_double_fields = flag_;
2601 }
2602
2603 private:
2604 bool flag_;
2605};
2606
2607
2608Genesis::Genesis(Isolate* isolate,
2609 MaybeHandle<JSGlobalProxy> maybe_global_proxy,
2610 v8::Handle<v8::ObjectTemplate> global_proxy_template,
2611 v8::ExtensionConfiguration* extensions)
2612 : isolate_(isolate),
2613 active_(isolate->bootstrapper()) {
2614 NoTrackDoubleFieldsForSerializerScope disable_scope(isolate);
2615 result_ = Handle<Context>::null();
Steve Blocka7e24c12009-10-30 11:49:00 +00002616 // Before creating the roots we must save the context and restore it
2617 // on all function exits.
Steve Block44f0eee2011-05-26 01:26:41 +01002618 SaveContext saved_context(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00002619
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002620 // During genesis, the boilerplate for stack overflow won't work until the
2621 // environment has been at least partially initialized. Add a stack check
2622 // before entering JS code to catch overflow early.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002623 StackLimitCheck check(isolate);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002624 if (check.HasOverflowed()) return;
2625
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002626 // We can only de-serialize a context if the isolate was initialized from
2627 // a snapshot. Otherwise we have to build the context from scratch.
2628 if (isolate->initialized_from_snapshot()) {
2629 native_context_ = Snapshot::NewContextFromSnapshot(isolate);
2630 } else {
2631 native_context_ = Handle<Context>();
2632 }
2633
2634 if (!native_context().is_null()) {
2635 AddToWeakNativeContextList(*native_context());
2636 isolate->set_context(*native_context());
Steve Block44f0eee2011-05-26 01:26:41 +01002637 isolate->counters()->contexts_created_by_snapshot()->Increment();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002638 Handle<GlobalObject> global_object;
2639 Handle<JSGlobalProxy> global_proxy = CreateNewGlobals(
2640 global_proxy_template, maybe_global_proxy, &global_object);
Andrei Popescu402d9372010-02-26 13:31:12 +00002641
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002642 HookUpGlobalProxy(global_object, global_proxy);
2643 HookUpGlobalObject(global_object);
2644 native_context()->builtins()->set_global_proxy(
2645 native_context()->global_proxy());
Andrei Popescu402d9372010-02-26 13:31:12 +00002646
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002647 if (!ConfigureGlobalObjects(global_proxy_template)) return;
Andrei Popescu31002712010-02-23 13:46:05 +00002648 } else {
2649 // We get here if there was no context snapshot.
2650 CreateRoots();
Ben Murdoch257744e2011-11-30 15:57:28 +00002651 Handle<JSFunction> empty_function = CreateEmptyFunction(isolate);
Steve Block44f0eee2011-05-26 01:26:41 +01002652 CreateStrictModeFunctionMaps(empty_function);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002653 Handle<GlobalObject> global_object;
2654 Handle<JSGlobalProxy> global_proxy = CreateNewGlobals(
2655 global_proxy_template, maybe_global_proxy, &global_object);
2656 HookUpGlobalProxy(global_object, global_proxy);
2657 InitializeGlobal(global_object, empty_function);
Steve Block6ded16b2010-05-10 14:33:55 +01002658 InstallJSFunctionResultCaches();
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002659 InitializeNormalizedMapCaches();
Kristian Monsen25f61362010-05-21 11:50:48 +01002660 if (!InstallNatives()) return;
Steve Blocka7e24c12009-10-30 11:49:00 +00002661
Andrei Popescu31002712010-02-23 13:46:05 +00002662 MakeFunctionInstancePrototypeWritable();
Steve Blocka7e24c12009-10-30 11:49:00 +00002663
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002664 if (!ConfigureGlobalObjects(global_proxy_template)) return;
Steve Block44f0eee2011-05-26 01:26:41 +01002665 isolate->counters()->contexts_created_from_scratch()->Increment();
Andrei Popescu31002712010-02-23 13:46:05 +00002666 }
Steve Blocka7e24c12009-10-30 11:49:00 +00002667
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002668 // Install experimental natives.
Ben Murdoch257744e2011-11-30 15:57:28 +00002669 if (!InstallExperimentalNatives()) return;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002670 InitializeExperimentalGlobal();
Ben Murdoch257744e2011-11-30 15:57:28 +00002671
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002672 // We can't (de-)serialize typed arrays currently, but we are lucky: The state
2673 // of the random number generator needs no initialization during snapshot
2674 // creation time and we don't need trigonometric functions then.
2675 if (!isolate->serializer_enabled()) {
2676 // Initially seed the per-context random number generator using the
2677 // per-isolate random number generator.
2678 const int num_elems = 2;
2679 const int num_bytes = num_elems * sizeof(uint32_t);
2680 uint32_t* state = reinterpret_cast<uint32_t*>(malloc(num_bytes));
2681
2682 do {
2683 isolate->random_number_generator()->NextBytes(state, num_bytes);
2684 } while (state[0] == 0 || state[1] == 0);
2685
2686 v8::Local<v8::ArrayBuffer> buffer = v8::ArrayBuffer::New(
2687 reinterpret_cast<v8::Isolate*>(isolate), state, num_bytes);
2688 Utils::OpenHandle(*buffer)->set_should_be_freed(true);
2689 v8::Local<v8::Uint32Array> ta = v8::Uint32Array::New(buffer, 0, num_elems);
2690 Handle<JSBuiltinsObject> builtins(native_context()->builtins());
2691 Runtime::DefineObjectProperty(builtins, factory()->InternalizeOneByteString(
2692 STATIC_CHAR_VECTOR("rngstate")),
2693 Utils::OpenHandle(*ta), NONE).Assert();
2694
2695 // Initialize trigonometric lookup tables and constants.
2696 const int constants_size = arraysize(fdlibm::MathConstants::constants);
2697 const int table_num_bytes = constants_size * kDoubleSize;
2698 v8::Local<v8::ArrayBuffer> trig_buffer = v8::ArrayBuffer::New(
2699 reinterpret_cast<v8::Isolate*>(isolate),
2700 const_cast<double*>(fdlibm::MathConstants::constants), table_num_bytes);
2701 v8::Local<v8::Float64Array> trig_table =
2702 v8::Float64Array::New(trig_buffer, 0, constants_size);
2703
2704 Runtime::DefineObjectProperty(
2705 builtins,
2706 factory()->InternalizeOneByteString(STATIC_CHAR_VECTOR("kMath")),
2707 Utils::OpenHandle(*trig_table), NONE).Assert();
2708 }
2709
2710 result_ = native_context();
Steve Blocka7e24c12009-10-30 11:49:00 +00002711}
2712
2713
2714// Support for thread preemption.
2715
2716// Reserve space for statics needing saving and restoring.
2717int Bootstrapper::ArchiveSpacePerThread() {
Steve Block44f0eee2011-05-26 01:26:41 +01002718 return sizeof(NestingCounterType);
Steve Blocka7e24c12009-10-30 11:49:00 +00002719}
2720
2721
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002722// Archive statics that are thread-local.
Steve Blocka7e24c12009-10-30 11:49:00 +00002723char* Bootstrapper::ArchiveState(char* to) {
Steve Block44f0eee2011-05-26 01:26:41 +01002724 *reinterpret_cast<NestingCounterType*>(to) = nesting_;
2725 nesting_ = 0;
2726 return to + sizeof(NestingCounterType);
Steve Blocka7e24c12009-10-30 11:49:00 +00002727}
2728
2729
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002730// Restore statics that are thread-local.
Steve Blocka7e24c12009-10-30 11:49:00 +00002731char* Bootstrapper::RestoreState(char* from) {
Steve Block44f0eee2011-05-26 01:26:41 +01002732 nesting_ = *reinterpret_cast<NestingCounterType*>(from);
2733 return from + sizeof(NestingCounterType);
Steve Blocka7e24c12009-10-30 11:49:00 +00002734}
2735
2736
2737// Called when the top-level V8 mutex is destroyed.
2738void Bootstrapper::FreeThreadResources() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002739 DCHECK(!IsActive());
Steve Blocka7e24c12009-10-30 11:49:00 +00002740}
2741
2742} } // namespace v8::internal