blob: 7105eb25e2380a347b93b2e94b6a04d3a79d99c2 [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.
Emily Bernierd0a1eb72015-03-24 16:35:39 -040051 Vector<const char> source = Natives::GetScriptSource(index);
Ben Murdoch3ef787d2012-04-12 10:51:47 +010052 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();
Emily Bernierd0a1eb72015-03-24 16:35:39 -040060 // Mark this external string with a special map.
61 source_code->set_map(isolate_->heap()->native_source_string_map());
Ben Murdoch3ef787d2012-04-12 10:51:47 +010062 heap->natives_source_cache()->set(index, *source_code);
Steve Blocka7e24c12009-10-30 11:49:00 +000063 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +000064 Handle<Object> cached_source(heap->natives_source_cache()->get(index),
65 isolate_);
Steve Blocka7e24c12009-10-30 11:49:00 +000066 return Handle<String>::cast(cached_source);
67}
68
69
Steve Blocka7e24c12009-10-30 11:49:00 +000070void Bootstrapper::Initialize(bool create_heap_objects) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000071 extensions_cache_.Initialize(isolate_, create_heap_objects);
72}
73
74
75static const char* GCFunctionName() {
76 bool flag_given = FLAG_expose_gc_as != NULL && strlen(FLAG_expose_gc_as) != 0;
77 return flag_given ? FLAG_expose_gc_as : "gc";
78}
79
80
81v8::Extension* Bootstrapper::free_buffer_extension_ = NULL;
82v8::Extension* Bootstrapper::gc_extension_ = NULL;
83v8::Extension* Bootstrapper::externalize_string_extension_ = NULL;
84v8::Extension* Bootstrapper::statistics_extension_ = NULL;
85v8::Extension* Bootstrapper::trigger_failure_extension_ = NULL;
86
87
88void Bootstrapper::InitializeOncePerProcess() {
89 free_buffer_extension_ = new FreeBufferExtension;
90 v8::RegisterExtension(free_buffer_extension_);
91 gc_extension_ = new GCExtension(GCFunctionName());
92 v8::RegisterExtension(gc_extension_);
93 externalize_string_extension_ = new ExternalizeStringExtension;
94 v8::RegisterExtension(externalize_string_extension_);
95 statistics_extension_ = new StatisticsExtension;
96 v8::RegisterExtension(statistics_extension_);
97 trigger_failure_extension_ = new TriggerFailureExtension;
98 v8::RegisterExtension(trigger_failure_extension_);
99}
100
101
102void Bootstrapper::TearDownExtensions() {
103 delete free_buffer_extension_;
104 free_buffer_extension_ = NULL;
105 delete gc_extension_;
106 gc_extension_ = NULL;
107 delete externalize_string_extension_;
108 externalize_string_extension_ = NULL;
109 delete statistics_extension_;
110 statistics_extension_ = NULL;
111 delete trigger_failure_extension_;
112 trigger_failure_extension_ = NULL;
Steve Blocka7e24c12009-10-30 11:49:00 +0000113}
114
115
Leon Clarkee46be812010-01-19 14:06:41 +0000116char* Bootstrapper::AllocateAutoDeletedArray(int bytes) {
117 char* memory = new char[bytes];
118 if (memory != NULL) {
Steve Block44f0eee2011-05-26 01:26:41 +0100119 if (delete_these_arrays_on_tear_down_ == NULL) {
120 delete_these_arrays_on_tear_down_ = new List<char*>(2);
Leon Clarkee46be812010-01-19 14:06:41 +0000121 }
Steve Block44f0eee2011-05-26 01:26:41 +0100122 delete_these_arrays_on_tear_down_->Add(memory);
Leon Clarkee46be812010-01-19 14:06:41 +0000123 }
124 return memory;
125}
126
127
Steve Blocka7e24c12009-10-30 11:49:00 +0000128void Bootstrapper::TearDown() {
Steve Block44f0eee2011-05-26 01:26:41 +0100129 if (delete_these_non_arrays_on_tear_down_ != NULL) {
130 int len = delete_these_non_arrays_on_tear_down_->length();
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400131 DCHECK(len < 1000); // Don't use this mechanism for unbounded allocations.
Steve Blockd0582a62009-12-15 09:54:21 +0000132 for (int i = 0; i < len; i++) {
Steve Block44f0eee2011-05-26 01:26:41 +0100133 delete delete_these_non_arrays_on_tear_down_->at(i);
134 delete_these_non_arrays_on_tear_down_->at(i) = NULL;
Steve Blockd0582a62009-12-15 09:54:21 +0000135 }
Steve Block44f0eee2011-05-26 01:26:41 +0100136 delete delete_these_non_arrays_on_tear_down_;
137 delete_these_non_arrays_on_tear_down_ = NULL;
Steve Blockd0582a62009-12-15 09:54:21 +0000138 }
139
Steve Block44f0eee2011-05-26 01:26:41 +0100140 if (delete_these_arrays_on_tear_down_ != NULL) {
141 int len = delete_these_arrays_on_tear_down_->length();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000142 DCHECK(len < 1000); // Don't use this mechanism for unbounded allocations.
Leon Clarkee46be812010-01-19 14:06:41 +0000143 for (int i = 0; i < len; i++) {
Steve Block44f0eee2011-05-26 01:26:41 +0100144 delete[] delete_these_arrays_on_tear_down_->at(i);
145 delete_these_arrays_on_tear_down_->at(i) = NULL;
Leon Clarkee46be812010-01-19 14:06:41 +0000146 }
Steve Block44f0eee2011-05-26 01:26:41 +0100147 delete delete_these_arrays_on_tear_down_;
148 delete_these_arrays_on_tear_down_ = NULL;
Leon Clarkee46be812010-01-19 14:06:41 +0000149 }
150
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000151 extensions_cache_.Initialize(isolate_, false); // Yes, symmetrical
Steve Blocka7e24c12009-10-30 11:49:00 +0000152}
153
154
Steve Blocka7e24c12009-10-30 11:49:00 +0000155class Genesis BASE_EMBEDDED {
156 public:
Ben Murdoch257744e2011-11-30 15:57:28 +0000157 Genesis(Isolate* isolate,
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000158 MaybeHandle<JSGlobalProxy> maybe_global_proxy,
159 v8::Handle<v8::ObjectTemplate> global_proxy_template,
Steve Blocka7e24c12009-10-30 11:49:00 +0000160 v8::ExtensionConfiguration* extensions);
Andrei Popescu31002712010-02-23 13:46:05 +0000161 ~Genesis() { }
Steve Blocka7e24c12009-10-30 11:49:00 +0000162
Ben Murdoch257744e2011-11-30 15:57:28 +0000163 Isolate* isolate() const { return isolate_; }
164 Factory* factory() const { return isolate_->factory(); }
165 Heap* heap() const { return isolate_->heap(); }
166
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000167 Handle<Context> result() { return result_; }
168
Steve Blocka7e24c12009-10-30 11:49:00 +0000169 private:
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000170 Handle<Context> native_context() { return native_context_; }
Steve Blocka7e24c12009-10-30 11:49:00 +0000171
Andrei Popescu31002712010-02-23 13:46:05 +0000172 // Creates some basic objects. Used for creating a context from scratch.
173 void CreateRoots();
174 // Creates the empty function. Used for creating a context from scratch.
Ben Murdoch257744e2011-11-30 15:57:28 +0000175 Handle<JSFunction> CreateEmptyFunction(Isolate* isolate);
Steve Block44f0eee2011-05-26 01:26:41 +0100176 // Creates the ThrowTypeError function. ECMA 5th Ed. 13.2.3
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000177 Handle<JSFunction> GetStrictPoisonFunction();
178 // Poison for sloppy generator function arguments/callee.
179 Handle<JSFunction> GetGeneratorPoisonFunction();
Steve Block44f0eee2011-05-26 01:26:41 +0100180
181 void CreateStrictModeFunctionMaps(Handle<JSFunction> empty);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100182
183 // Make the "arguments" and "caller" properties throw a TypeError on access.
184 void PoisonArgumentsAndCaller(Handle<Map> map);
185
Andrei Popescu31002712010-02-23 13:46:05 +0000186 // Creates the global objects using the global and the template passed in
187 // through the API. We call this regardless of whether we are building a
188 // context from scratch or using a deserialized one from the partial snapshot
189 // but in the latter case we don't use the objects it produces directly, as
190 // we have to used the deserialized ones that are linked together with the
191 // rest of the context snapshot.
192 Handle<JSGlobalProxy> CreateNewGlobals(
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000193 v8::Handle<v8::ObjectTemplate> global_proxy_template,
194 MaybeHandle<JSGlobalProxy> maybe_global_proxy,
195 Handle<GlobalObject>* global_object_out);
Andrei Popescu31002712010-02-23 13:46:05 +0000196 // Hooks the given global proxy into the context. If the context was created
197 // by deserialization then this will unhook the global proxy that was
198 // deserialized, leaving the GC to pick it up.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000199 void HookUpGlobalProxy(Handle<GlobalObject> global_object,
Andrei Popescu31002712010-02-23 13:46:05 +0000200 Handle<JSGlobalProxy> global_proxy);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000201 // Similarly, we want to use the global that has been created by the templates
202 // passed through the API. The global from the snapshot is detached from the
203 // other objects in the snapshot.
204 void HookUpGlobalObject(Handle<GlobalObject> global_object);
Andrei Popescu31002712010-02-23 13:46:05 +0000205 // New context initialization. Used for creating a context from scratch.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000206 void InitializeGlobal(Handle<GlobalObject> global_object,
Andrei Popescu31002712010-02-23 13:46:05 +0000207 Handle<JSFunction> empty_function);
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000208 void InitializeExperimentalGlobal();
Andrei Popescu31002712010-02-23 13:46:05 +0000209 // Installs the contents of the native .js files on the global objects.
210 // Used for creating a context from scratch.
Steve Blocka7e24c12009-10-30 11:49:00 +0000211 void InstallNativeFunctions();
Ben Murdoch257744e2011-11-30 15:57:28 +0000212 void InstallExperimentalNativeFunctions();
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400213
214#define DECLARE_FEATURE_INITIALIZATION(id, descr) \
215 void InstallNativeFunctions_##id(); \
216 void InitializeGlobal_##id();
217
218 HARMONY_INPROGRESS(DECLARE_FEATURE_INITIALIZATION)
219 HARMONY_STAGED(DECLARE_FEATURE_INITIALIZATION)
220 HARMONY_SHIPPING(DECLARE_FEATURE_INITIALIZATION)
221#undef DECLARE_FEATURE_INITIALIZATION
222
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000223 Handle<JSFunction> InstallInternalArray(Handle<JSBuiltinsObject> builtins,
224 const char* name,
225 ElementsKind elements_kind);
Steve Blocka7e24c12009-10-30 11:49:00 +0000226 bool InstallNatives();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000227
228 void InstallTypedArray(
229 const char* name,
230 ElementsKind elements_kind,
231 Handle<JSFunction>* fun,
232 Handle<Map>* external_map);
Ben Murdoch257744e2011-11-30 15:57:28 +0000233 bool InstallExperimentalNatives();
Ben Murdochb0fe1622011-05-05 13:52:32 +0100234 void InstallBuiltinFunctionIds();
Steve Block6ded16b2010-05-10 14:33:55 +0100235 void InstallJSFunctionResultCaches();
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100236 void InitializeNormalizedMapCaches();
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100237
238 enum ExtensionTraversalState {
239 UNVISITED, VISITED, INSTALLED
240 };
241
242 class ExtensionStates {
243 public:
244 ExtensionStates();
245 ExtensionTraversalState get_state(RegisteredExtension* extension);
246 void set_state(RegisteredExtension* extension,
247 ExtensionTraversalState state);
248 private:
249 HashMap map_;
250 DISALLOW_COPY_AND_ASSIGN(ExtensionStates);
251 };
252
Andrei Popescu31002712010-02-23 13:46:05 +0000253 // Used both for deserialized and from-scratch contexts to add the extensions
254 // provided.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000255 static bool InstallExtensions(Handle<Context> native_context,
Andrei Popescu31002712010-02-23 13:46:05 +0000256 v8::ExtensionConfiguration* extensions);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000257 static bool InstallAutoExtensions(Isolate* isolate,
258 ExtensionStates* extension_states);
259 static bool InstallRequestedExtensions(Isolate* isolate,
260 v8::ExtensionConfiguration* extensions,
261 ExtensionStates* extension_states);
262 static bool InstallExtension(Isolate* isolate,
263 const char* name,
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100264 ExtensionStates* extension_states);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000265 static bool InstallExtension(Isolate* isolate,
266 v8::RegisteredExtension* current,
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100267 ExtensionStates* extension_states);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000268 static bool InstallSpecialObjects(Handle<Context> native_context);
Andrei Popescu402d9372010-02-26 13:31:12 +0000269 bool InstallJSBuiltins(Handle<JSBuiltinsObject> builtins);
Steve Blocka7e24c12009-10-30 11:49:00 +0000270 bool ConfigureApiObject(Handle<JSObject> object,
271 Handle<ObjectTemplateInfo> object_template);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000272 bool ConfigureGlobalObjects(
273 v8::Handle<v8::ObjectTemplate> global_proxy_template);
Steve Blocka7e24c12009-10-30 11:49:00 +0000274
275 // Migrates all properties from the 'from' object to the 'to'
276 // object and overrides the prototype in 'to' with the one from
277 // 'from'.
278 void TransferObject(Handle<JSObject> from, Handle<JSObject> to);
279 void TransferNamedProperties(Handle<JSObject> from, Handle<JSObject> to);
280 void TransferIndexedProperties(Handle<JSObject> from, Handle<JSObject> to);
281
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000282 enum FunctionMode {
283 // With prototype.
284 FUNCTION_WITH_WRITEABLE_PROTOTYPE,
285 FUNCTION_WITH_READONLY_PROTOTYPE,
286 // Without prototype.
287 FUNCTION_WITHOUT_PROTOTYPE,
288 BOUND_FUNCTION
Steve Block6ded16b2010-05-10 14:33:55 +0100289 };
Steve Block44f0eee2011-05-26 01:26:41 +0100290
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000291 static bool IsFunctionModeWithPrototype(FunctionMode function_mode) {
292 return (function_mode == FUNCTION_WITH_WRITEABLE_PROTOTYPE ||
293 function_mode == FUNCTION_WITH_READONLY_PROTOTYPE);
294 }
Steve Block44f0eee2011-05-26 01:26:41 +0100295
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000296 Handle<Map> CreateFunctionMap(FunctionMode function_mode);
297
298 void SetFunctionInstanceDescriptor(Handle<Map> map,
299 FunctionMode function_mode);
Steve Blocka7e24c12009-10-30 11:49:00 +0000300 void MakeFunctionInstancePrototypeWritable();
301
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000302 Handle<Map> CreateStrictFunctionMap(
303 FunctionMode function_mode,
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100304 Handle<JSFunction> empty_function);
Steve Block44f0eee2011-05-26 01:26:41 +0100305
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000306 void SetStrictFunctionInstanceDescriptor(Handle<Map> map,
307 FunctionMode function_mode);
Steve Block44f0eee2011-05-26 01:26:41 +0100308
Ben Murdoch257744e2011-11-30 15:57:28 +0000309 static bool CompileBuiltin(Isolate* isolate, int index);
310 static bool CompileExperimentalBuiltin(Isolate* isolate, int index);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000311 static bool CompileNative(Isolate* isolate,
312 Vector<const char> name,
313 Handle<String> source);
314 static bool CompileScriptCached(Isolate* isolate,
315 Vector<const char> name,
Steve Blocka7e24c12009-10-30 11:49:00 +0000316 Handle<String> source,
317 SourceCodeCache* cache,
318 v8::Extension* extension,
Andrei Popescu31002712010-02-23 13:46:05 +0000319 Handle<Context> top_context,
Steve Blocka7e24c12009-10-30 11:49:00 +0000320 bool use_runtime_context);
321
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000322 Isolate* isolate_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000323 Handle<Context> result_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000324 Handle<Context> native_context_;
Steve Block44f0eee2011-05-26 01:26:41 +0100325
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000326 // Function maps. Function maps are created initially with a read only
327 // prototype for the processing of JS builtins. Later the function maps are
328 // replaced in order to make prototype writable. These are the final, writable
329 // prototype, maps.
330 Handle<Map> sloppy_function_map_writable_prototype_;
331 Handle<Map> strict_function_map_writable_prototype_;
332 Handle<JSFunction> strict_poison_function;
333 Handle<JSFunction> generator_poison_function;
Steve Block44f0eee2011-05-26 01:26:41 +0100334
Andrei Popescu31002712010-02-23 13:46:05 +0000335 BootstrapperActive active_;
336 friend class Bootstrapper;
Steve Blocka7e24c12009-10-30 11:49:00 +0000337};
338
Steve Blocka7e24c12009-10-30 11:49:00 +0000339
340void Bootstrapper::Iterate(ObjectVisitor* v) {
Steve Block44f0eee2011-05-26 01:26:41 +0100341 extensions_cache_.Iterate(v);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100342 v->Synchronize(VisitorSynchronization::kExtensions);
Steve Blocka7e24c12009-10-30 11:49:00 +0000343}
344
345
Steve Blocka7e24c12009-10-30 11:49:00 +0000346Handle<Context> Bootstrapper::CreateEnvironment(
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000347 MaybeHandle<JSGlobalProxy> maybe_global_proxy,
348 v8::Handle<v8::ObjectTemplate> global_proxy_template,
Steve Blocka7e24c12009-10-30 11:49:00 +0000349 v8::ExtensionConfiguration* extensions) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000350 HandleScope scope(isolate_);
351 Genesis genesis(
352 isolate_, maybe_global_proxy, global_proxy_template, extensions);
353 Handle<Context> env = genesis.result();
354 if (env.is_null() || !InstallExtensions(env, extensions)) {
355 return Handle<Context>();
Andrei Popescu31002712010-02-23 13:46:05 +0000356 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000357 return scope.CloseAndEscape(env);
Steve Blocka7e24c12009-10-30 11:49:00 +0000358}
359
360
361static void SetObjectPrototype(Handle<JSObject> object, Handle<Object> proto) {
362 // object.__proto__ = proto;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000363 Handle<Map> old_map = Handle<Map>(object->map());
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400364 Handle<Map> new_map = Map::Copy(old_map, "SetObjectPrototype");
365 new_map->SetPrototype(proto, FAST_PROTOTYPE);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000366 JSObject::MigrateToMap(object, new_map);
Steve Blocka7e24c12009-10-30 11:49:00 +0000367}
368
369
370void Bootstrapper::DetachGlobal(Handle<Context> env) {
Ben Murdoch257744e2011-11-30 15:57:28 +0000371 Factory* factory = env->GetIsolate()->factory();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000372 Handle<JSGlobalProxy> global_proxy(JSGlobalProxy::cast(env->global_proxy()));
373 global_proxy->set_native_context(*factory->null_value());
374 SetObjectPrototype(global_proxy, factory->null_value());
375 global_proxy->map()->set_constructor(*factory->null_value());
Andrei Popescu74b3c142010-03-29 12:03:09 +0100376}
377
378
Steve Blocka7e24c12009-10-30 11:49:00 +0000379static Handle<JSFunction> InstallFunction(Handle<JSObject> target,
380 const char* name,
381 InstanceType type,
382 int instance_size,
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000383 MaybeHandle<JSObject> maybe_prototype,
384 Builtins::Name call) {
Ben Murdoch257744e2011-11-30 15:57:28 +0000385 Isolate* isolate = target->GetIsolate();
Steve Block44f0eee2011-05-26 01:26:41 +0100386 Factory* factory = isolate->factory();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000387 Handle<String> internalized_name = factory->InternalizeUtf8String(name);
Steve Block44f0eee2011-05-26 01:26:41 +0100388 Handle<Code> call_code = Handle<Code>(isolate->builtins()->builtin(call));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000389 Handle<JSObject> prototype;
390 Handle<JSFunction> function = maybe_prototype.ToHandle(&prototype)
391 ? factory->NewFunction(internalized_name, call_code, prototype,
392 type, instance_size)
393 : factory->NewFunctionWithoutPrototype(internalized_name, call_code);
Ben Murdoch589d6972011-11-30 16:04:58 +0000394 PropertyAttributes attributes;
395 if (target->IsJSBuiltinsObject()) {
396 attributes =
397 static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE | READ_ONLY);
398 } else {
399 attributes = DONT_ENUM;
400 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000401 JSObject::AddProperty(target, internalized_name, function, attributes);
402 if (target->IsJSGlobalObject()) {
403 function->shared()->set_instance_class_name(*internalized_name);
Steve Blocka7e24c12009-10-30 11:49:00 +0000404 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100405 function->shared()->set_native(true);
Steve Blocka7e24c12009-10-30 11:49:00 +0000406 return function;
407}
408
409
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000410void Genesis::SetFunctionInstanceDescriptor(
411 Handle<Map> map, FunctionMode function_mode) {
412 int size = IsFunctionModeWithPrototype(function_mode) ? 5 : 4;
413 Map::EnsureDescriptorSlack(map, size);
414
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100415 PropertyAttributes attribs = static_cast<PropertyAttributes>(
416 DONT_ENUM | DONT_DELETE | READ_ONLY);
417
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000418 Handle<AccessorInfo> length =
419 Accessors::FunctionLengthInfo(isolate(), attribs);
Steve Block44f0eee2011-05-26 01:26:41 +0100420 { // Add length.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000421 CallbacksDescriptor d(Handle<Name>(Name::cast(length->name())),
422 length, attribs);
423 map->AppendDescriptor(&d);
Steve Block44f0eee2011-05-26 01:26:41 +0100424 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000425 Handle<AccessorInfo> name =
426 Accessors::FunctionNameInfo(isolate(), attribs);
Steve Block44f0eee2011-05-26 01:26:41 +0100427 { // Add name.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000428 CallbacksDescriptor d(Handle<Name>(Name::cast(name->name())),
429 name, attribs);
430 map->AppendDescriptor(&d);
Steve Block44f0eee2011-05-26 01:26:41 +0100431 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000432 Handle<AccessorInfo> args =
433 Accessors::FunctionArgumentsInfo(isolate(), attribs);
Steve Block44f0eee2011-05-26 01:26:41 +0100434 { // Add arguments.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000435 CallbacksDescriptor d(Handle<Name>(Name::cast(args->name())),
436 args, attribs);
437 map->AppendDescriptor(&d);
Steve Block44f0eee2011-05-26 01:26:41 +0100438 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000439 Handle<AccessorInfo> caller =
440 Accessors::FunctionCallerInfo(isolate(), attribs);
Steve Block44f0eee2011-05-26 01:26:41 +0100441 { // Add caller.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000442 CallbacksDescriptor d(Handle<Name>(Name::cast(caller->name())),
443 caller, attribs);
444 map->AppendDescriptor(&d);
Steve Block44f0eee2011-05-26 01:26:41 +0100445 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000446 if (IsFunctionModeWithPrototype(function_mode)) {
447 if (function_mode == FUNCTION_WITH_WRITEABLE_PROTOTYPE) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100448 attribs = static_cast<PropertyAttributes>(attribs & ~READ_ONLY);
Steve Block44f0eee2011-05-26 01:26:41 +0100449 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000450 Handle<AccessorInfo> prototype =
451 Accessors::FunctionPrototypeInfo(isolate(), attribs);
452 CallbacksDescriptor d(Handle<Name>(Name::cast(prototype->name())),
453 prototype, attribs);
454 map->AppendDescriptor(&d);
Steve Block44f0eee2011-05-26 01:26:41 +0100455 }
Steve Block44f0eee2011-05-26 01:26:41 +0100456}
Steve Blocka7e24c12009-10-30 11:49:00 +0000457
Steve Blocka7e24c12009-10-30 11:49:00 +0000458
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000459Handle<Map> Genesis::CreateFunctionMap(FunctionMode function_mode) {
Ben Murdoch257744e2011-11-30 15:57:28 +0000460 Handle<Map> map = factory()->NewMap(JS_FUNCTION_TYPE, JSFunction::kSize);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000461 SetFunctionInstanceDescriptor(map, function_mode);
462 map->set_function_with_prototype(IsFunctionModeWithPrototype(function_mode));
Steve Block44f0eee2011-05-26 01:26:41 +0100463 return map;
Steve Blocka7e24c12009-10-30 11:49:00 +0000464}
465
466
Ben Murdoch257744e2011-11-30 15:57:28 +0000467Handle<JSFunction> Genesis::CreateEmptyFunction(Isolate* isolate) {
Steve Block44f0eee2011-05-26 01:26:41 +0100468 // Allocate the map for function instances. Maps are allocated first and their
469 // prototypes patched later, once empty function is created.
470
Steve Block6ded16b2010-05-10 14:33:55 +0100471 // Functions with this map will not have a 'prototype' property, and
472 // can not be used as constructors.
Ben Murdoch8b112d22011-06-08 16:22:53 +0100473 Handle<Map> function_without_prototype_map =
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000474 CreateFunctionMap(FUNCTION_WITHOUT_PROTOTYPE);
475 native_context()->set_sloppy_function_without_prototype_map(
Ben Murdoch8b112d22011-06-08 16:22:53 +0100476 *function_without_prototype_map);
Steve Blocka7e24c12009-10-30 11:49:00 +0000477
Steve Block44f0eee2011-05-26 01:26:41 +0100478 // Allocate the function map. This map is temporary, used only for processing
479 // of builtins.
480 // Later the map is replaced with writable prototype map, allocated below.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000481 Handle<Map> function_map =
482 CreateFunctionMap(FUNCTION_WITH_READONLY_PROTOTYPE);
483 native_context()->set_sloppy_function_map(*function_map);
484 native_context()->set_sloppy_function_with_readonly_prototype_map(
485 *function_map);
Steve Blocka7e24c12009-10-30 11:49:00 +0000486
Steve Block44f0eee2011-05-26 01:26:41 +0100487 // The final map for functions. Writeable prototype.
488 // This map is installed in MakeFunctionInstancePrototypeWritable.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000489 sloppy_function_map_writable_prototype_ =
490 CreateFunctionMap(FUNCTION_WITH_WRITEABLE_PROTOTYPE);
Steve Block44f0eee2011-05-26 01:26:41 +0100491
Steve Block44f0eee2011-05-26 01:26:41 +0100492 Factory* factory = isolate->factory();
Steve Block44f0eee2011-05-26 01:26:41 +0100493
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000494 Handle<String> object_name = factory->Object_string();
Steve Blocka7e24c12009-10-30 11:49:00 +0000495
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400496 Handle<JSObject> object_function_prototype;
497
Steve Blocka7e24c12009-10-30 11:49:00 +0000498 { // --- O b j e c t ---
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000499 Handle<JSFunction> object_fun = factory->NewFunction(object_name);
500 int unused = JSObject::kInitialGlobalObjectUnusedPropertiesCount;
501 int instance_size = JSObject::kHeaderSize + kPointerSize * unused;
Steve Blocka7e24c12009-10-30 11:49:00 +0000502 Handle<Map> object_function_map =
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000503 factory->NewMap(JS_OBJECT_TYPE, instance_size);
504 object_function_map->set_inobject_properties(unused);
505 JSFunction::SetInitialMap(object_fun, object_function_map,
506 isolate->factory()->null_value());
507 object_function_map->set_unused_property_fields(unused);
Steve Blocka7e24c12009-10-30 11:49:00 +0000508
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000509 native_context()->set_object_function(*object_fun);
Steve Blocka7e24c12009-10-30 11:49:00 +0000510
511 // Allocate a new prototype for the object function.
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400512 object_function_prototype =
513 factory->NewJSObject(isolate->object_function(), TENURED);
514 Handle<Map> map = Map::Copy(handle(object_function_prototype->map()),
515 "EmptyObjectPrototype");
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000516 map->set_is_prototype_map(true);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400517 object_function_prototype->set_map(*map);
Steve Blocka7e24c12009-10-30 11:49:00 +0000518
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400519 native_context()->set_initial_object_prototype(*object_function_prototype);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000520 // For bootstrapping set the array prototype to be the same as the object
521 // prototype, otherwise the missing initial_array_prototype will cause
522 // assertions during startup.
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400523 native_context()->set_initial_array_prototype(*object_function_prototype);
524 Accessors::FunctionSetPrototype(object_fun, object_function_prototype)
525 .Assert();
Steve Blocka7e24c12009-10-30 11:49:00 +0000526 }
527
528 // Allocate the empty function as the prototype for function ECMAScript
529 // 262 15.3.4.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000530 Handle<String> empty_string =
531 factory->InternalizeOneByteString(STATIC_CHAR_VECTOR("Empty"));
532 Handle<Code> code(isolate->builtins()->builtin(Builtins::kEmptyFunction));
533 Handle<JSFunction> empty_function = factory->NewFunctionWithoutPrototype(
534 empty_string, code);
535
536 // Allocate the function map first and then patch the prototype later
537 Handle<Map> empty_function_map =
538 CreateFunctionMap(FUNCTION_WITHOUT_PROTOTYPE);
539 DCHECK(!empty_function_map->is_dictionary_map());
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400540 empty_function_map->SetPrototype(object_function_prototype);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000541 empty_function_map->set_is_prototype_map(true);
542 empty_function->set_map(*empty_function_map);
Steve Blocka7e24c12009-10-30 11:49:00 +0000543
Andrei Popescu31002712010-02-23 13:46:05 +0000544 // --- E m p t y ---
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000545 Handle<String> source = factory->NewStringFromStaticChars("() {}");
Steve Block44f0eee2011-05-26 01:26:41 +0100546 Handle<Script> script = factory->NewScript(source);
Andrei Popescu31002712010-02-23 13:46:05 +0000547 script->set_type(Smi::FromInt(Script::TYPE_NATIVE));
548 empty_function->shared()->set_script(*script);
549 empty_function->shared()->set_start_position(0);
550 empty_function->shared()->set_end_position(source->length());
551 empty_function->shared()->DontAdaptArguments();
Steve Block44f0eee2011-05-26 01:26:41 +0100552
553 // Set prototypes for the function maps.
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400554 native_context()->sloppy_function_map()->SetPrototype(empty_function);
555 native_context()->sloppy_function_without_prototype_map()->SetPrototype(
556 empty_function);
557 sloppy_function_map_writable_prototype_->SetPrototype(empty_function);
Andrei Popescu31002712010-02-23 13:46:05 +0000558 return empty_function;
559}
560
561
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000562void Genesis::SetStrictFunctionInstanceDescriptor(
563 Handle<Map> map, FunctionMode function_mode) {
564 int size = IsFunctionModeWithPrototype(function_mode) ? 5 : 4;
565 Map::EnsureDescriptorSlack(map, size);
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000566
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000567 Handle<AccessorPair> arguments(factory()->NewAccessorPair());
568 Handle<AccessorPair> caller(factory()->NewAccessorPair());
569 PropertyAttributes rw_attribs =
570 static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE);
571 PropertyAttributes ro_attribs =
572 static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE | READ_ONLY);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100573
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000574 // Add length.
575 if (function_mode == BOUND_FUNCTION) {
576 Handle<String> length_string = isolate()->factory()->length_string();
577 FieldDescriptor d(length_string, 0, ro_attribs, Representation::Tagged());
578 map->AppendDescriptor(&d);
579 } else {
580 DCHECK(function_mode == FUNCTION_WITH_WRITEABLE_PROTOTYPE ||
581 function_mode == FUNCTION_WITH_READONLY_PROTOTYPE ||
582 function_mode == FUNCTION_WITHOUT_PROTOTYPE);
583 Handle<AccessorInfo> length =
584 Accessors::FunctionLengthInfo(isolate(), ro_attribs);
585 CallbacksDescriptor d(Handle<Name>(Name::cast(length->name())),
586 length, ro_attribs);
587 map->AppendDescriptor(&d);
Steve Block44f0eee2011-05-26 01:26:41 +0100588 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000589 Handle<AccessorInfo> name =
590 Accessors::FunctionNameInfo(isolate(), ro_attribs);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100591 { // Add name.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000592 CallbacksDescriptor d(Handle<Name>(Name::cast(name->name())),
593 name, ro_attribs);
594 map->AppendDescriptor(&d);
Steve Block44f0eee2011-05-26 01:26:41 +0100595 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100596 { // Add arguments.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000597 CallbacksDescriptor d(factory()->arguments_string(), arguments,
598 rw_attribs);
599 map->AppendDescriptor(&d);
Steve Block44f0eee2011-05-26 01:26:41 +0100600 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100601 { // Add caller.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000602 CallbacksDescriptor d(factory()->caller_string(), caller, rw_attribs);
603 map->AppendDescriptor(&d);
Steve Block44f0eee2011-05-26 01:26:41 +0100604 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000605 if (IsFunctionModeWithPrototype(function_mode)) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100606 // Add prototype.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000607 PropertyAttributes attribs =
608 function_mode == FUNCTION_WITH_WRITEABLE_PROTOTYPE ? rw_attribs
609 : ro_attribs;
610 Handle<AccessorInfo> prototype =
611 Accessors::FunctionPrototypeInfo(isolate(), attribs);
612 CallbacksDescriptor d(Handle<Name>(Name::cast(prototype->name())),
613 prototype, attribs);
614 map->AppendDescriptor(&d);
Steve Block44f0eee2011-05-26 01:26:41 +0100615 }
Steve Block44f0eee2011-05-26 01:26:41 +0100616}
617
618
619// ECMAScript 5th Edition, 13.2.3
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000620Handle<JSFunction> Genesis::GetStrictPoisonFunction() {
621 if (strict_poison_function.is_null()) {
622 Handle<String> name = factory()->InternalizeOneByteString(
623 STATIC_CHAR_VECTOR("ThrowTypeError"));
Ben Murdoch257744e2011-11-30 15:57:28 +0000624 Handle<Code> code(isolate()->builtins()->builtin(
625 Builtins::kStrictModePoisonPill));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000626 strict_poison_function = factory()->NewFunctionWithoutPrototype(name, code);
627 strict_poison_function->set_map(native_context()->sloppy_function_map());
628 strict_poison_function->shared()->DontAdaptArguments();
Steve Block053d10c2011-06-13 19:13:29 +0100629
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000630 JSObject::PreventExtensions(strict_poison_function).Assert();
Ben Murdoch257744e2011-11-30 15:57:28 +0000631 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000632 return strict_poison_function;
Steve Block44f0eee2011-05-26 01:26:41 +0100633}
634
635
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000636Handle<JSFunction> Genesis::GetGeneratorPoisonFunction() {
637 if (generator_poison_function.is_null()) {
638 Handle<String> name = factory()->InternalizeOneByteString(
639 STATIC_CHAR_VECTOR("ThrowTypeError"));
640 Handle<Code> code(isolate()->builtins()->builtin(
641 Builtins::kGeneratorPoisonPill));
642 generator_poison_function = factory()->NewFunctionWithoutPrototype(
643 name, code);
644 generator_poison_function->set_map(native_context()->sloppy_function_map());
645 generator_poison_function->shared()->DontAdaptArguments();
646
647 JSObject::PreventExtensions(generator_poison_function).Assert();
648 }
649 return generator_poison_function;
650}
651
652
653Handle<Map> Genesis::CreateStrictFunctionMap(
654 FunctionMode function_mode,
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100655 Handle<JSFunction> empty_function) {
Ben Murdoch257744e2011-11-30 15:57:28 +0000656 Handle<Map> map = factory()->NewMap(JS_FUNCTION_TYPE, JSFunction::kSize);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000657 SetStrictFunctionInstanceDescriptor(map, function_mode);
658 map->set_function_with_prototype(IsFunctionModeWithPrototype(function_mode));
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400659 map->SetPrototype(empty_function);
Steve Block44f0eee2011-05-26 01:26:41 +0100660 return map;
661}
662
663
664void Genesis::CreateStrictModeFunctionMaps(Handle<JSFunction> empty) {
Steve Block44f0eee2011-05-26 01:26:41 +0100665 // Allocate map for the prototype-less strict mode instances.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000666 Handle<Map> strict_function_without_prototype_map =
667 CreateStrictFunctionMap(FUNCTION_WITHOUT_PROTOTYPE, empty);
668 native_context()->set_strict_function_without_prototype_map(
669 *strict_function_without_prototype_map);
Steve Block44f0eee2011-05-26 01:26:41 +0100670
671 // Allocate map for the strict mode functions. This map is temporary, used
672 // only for processing of builtins.
673 // Later the map is replaced with writable prototype map, allocated below.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000674 Handle<Map> strict_function_map =
675 CreateStrictFunctionMap(FUNCTION_WITH_READONLY_PROTOTYPE, empty);
676 native_context()->set_strict_function_map(*strict_function_map);
Steve Block44f0eee2011-05-26 01:26:41 +0100677
678 // The final map for the strict mode functions. Writeable prototype.
679 // This map is installed in MakeFunctionInstancePrototypeWritable.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000680 strict_function_map_writable_prototype_ =
681 CreateStrictFunctionMap(FUNCTION_WITH_WRITEABLE_PROTOTYPE, empty);
682 // Special map for bound functions.
683 Handle<Map> bound_function_map =
684 CreateStrictFunctionMap(BOUND_FUNCTION, empty);
685 native_context()->set_bound_function_map(*bound_function_map);
Steve Block44f0eee2011-05-26 01:26:41 +0100686
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100687 // Complete the callbacks.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000688 PoisonArgumentsAndCaller(strict_function_without_prototype_map);
689 PoisonArgumentsAndCaller(strict_function_map);
690 PoisonArgumentsAndCaller(strict_function_map_writable_prototype_);
691 PoisonArgumentsAndCaller(bound_function_map);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100692}
Ben Murdoch5d4cdbf2012-04-11 10:23:59 +0100693
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100694
695static void SetAccessors(Handle<Map> map,
696 Handle<String> name,
697 Handle<JSFunction> func) {
698 DescriptorArray* descs = map->instance_descriptors();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000699 int number = descs->SearchWithCache(*name, *map);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100700 AccessorPair* accessors = AccessorPair::cast(descs->GetValue(number));
701 accessors->set_getter(*func);
702 accessors->set_setter(*func);
703}
704
705
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000706static void ReplaceAccessors(Handle<Map> map,
707 Handle<String> name,
708 PropertyAttributes attributes,
709 Handle<AccessorPair> accessor_pair) {
710 DescriptorArray* descriptors = map->instance_descriptors();
711 int idx = descriptors->SearchWithCache(*name, *map);
712 CallbacksDescriptor descriptor(name, accessor_pair, attributes);
713 descriptors->Replace(idx, &descriptor);
Steve Block44f0eee2011-05-26 01:26:41 +0100714}
715
716
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000717void Genesis::PoisonArgumentsAndCaller(Handle<Map> map) {
718 SetAccessors(map, factory()->arguments_string(), GetStrictPoisonFunction());
719 SetAccessors(map, factory()->caller_string(), GetStrictPoisonFunction());
720}
721
722
723static void AddToWeakNativeContextList(Context* context) {
724 DCHECK(context->IsNativeContext());
Ben Murdoch257744e2011-11-30 15:57:28 +0000725 Heap* heap = context->GetIsolate()->heap();
Ben Murdochb0fe1622011-05-05 13:52:32 +0100726#ifdef DEBUG
727 { // NOLINT
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000728 DCHECK(context->get(Context::NEXT_CONTEXT_LINK)->IsUndefined());
Ben Murdochb0fe1622011-05-05 13:52:32 +0100729 // Check that context is not in the list yet.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000730 for (Object* current = heap->native_contexts_list();
Ben Murdochb0fe1622011-05-05 13:52:32 +0100731 !current->IsUndefined();
732 current = Context::cast(current)->get(Context::NEXT_CONTEXT_LINK)) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000733 DCHECK(current != context);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100734 }
735 }
736#endif
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000737 context->set(Context::NEXT_CONTEXT_LINK, heap->native_contexts_list());
738 heap->set_native_contexts_list(context);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100739}
740
741
Andrei Popescu31002712010-02-23 13:46:05 +0000742void Genesis::CreateRoots() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000743 // Allocate the native context FixedArray first and then patch the
Andrei Popescu31002712010-02-23 13:46:05 +0000744 // closure and extension object later (we need the empty function
745 // and the global object, but in order to create those, we need the
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000746 // native context).
747 native_context_ = factory()->NewNativeContext();
748 AddToWeakNativeContextList(*native_context());
749 isolate()->set_context(*native_context());
Andrei Popescu31002712010-02-23 13:46:05 +0000750
751 // Allocate the message listeners object.
752 {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000753 v8::NeanderArray listeners(isolate());
754 native_context()->set_message_listeners(*listeners.value());
Andrei Popescu31002712010-02-23 13:46:05 +0000755 }
756}
757
758
759Handle<JSGlobalProxy> Genesis::CreateNewGlobals(
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000760 v8::Handle<v8::ObjectTemplate> global_proxy_template,
761 MaybeHandle<JSGlobalProxy> maybe_global_proxy,
762 Handle<GlobalObject>* global_object_out) {
763 // The argument global_proxy_template aka data is an ObjectTemplateInfo.
Andrei Popescu31002712010-02-23 13:46:05 +0000764 // It has a constructor pointer that points at global_constructor which is a
765 // FunctionTemplateInfo.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000766 // The global_proxy_constructor is used to create or reinitialize the
767 // global_proxy. The global_proxy_constructor also has a prototype_template
768 // pointer that points at js_global_object_template which is an
769 // ObjectTemplateInfo.
Andrei Popescu31002712010-02-23 13:46:05 +0000770 // That in turn has a constructor pointer that points at
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000771 // js_global_object_constructor which is a FunctionTemplateInfo.
772 // js_global_object_constructor is used to make js_global_object_function
773 // js_global_object_function is used to make the new global_object.
Andrei Popescu31002712010-02-23 13:46:05 +0000774 //
775 // --- G l o b a l ---
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000776 // Step 1: Create a fresh JSGlobalObject.
777 Handle<JSFunction> js_global_object_function;
778 Handle<ObjectTemplateInfo> js_global_object_template;
779 if (!global_proxy_template.IsEmpty()) {
780 // Get prototype template of the global_proxy_template.
Andrei Popescu31002712010-02-23 13:46:05 +0000781 Handle<ObjectTemplateInfo> data =
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000782 v8::Utils::OpenHandle(*global_proxy_template);
Andrei Popescu31002712010-02-23 13:46:05 +0000783 Handle<FunctionTemplateInfo> global_constructor =
784 Handle<FunctionTemplateInfo>(
785 FunctionTemplateInfo::cast(data->constructor()));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000786 Handle<Object> proto_template(global_constructor->prototype_template(),
787 isolate());
Andrei Popescu31002712010-02-23 13:46:05 +0000788 if (!proto_template->IsUndefined()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000789 js_global_object_template =
Andrei Popescu31002712010-02-23 13:46:05 +0000790 Handle<ObjectTemplateInfo>::cast(proto_template);
791 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000792 }
793
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000794 if (js_global_object_template.is_null()) {
795 Handle<String> name = Handle<String>(heap()->empty_string());
Ben Murdoch257744e2011-11-30 15:57:28 +0000796 Handle<Code> code = Handle<Code>(isolate()->builtins()->builtin(
Steve Block44f0eee2011-05-26 01:26:41 +0100797 Builtins::kIllegal));
Andrei Popescu31002712010-02-23 13:46:05 +0000798 Handle<JSObject> prototype =
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000799 factory()->NewFunctionPrototype(isolate()->object_function());
800 js_global_object_function = factory()->NewFunction(
801 name, code, prototype, JS_GLOBAL_OBJECT_TYPE, JSGlobalObject::kSize);
802#ifdef DEBUG
803 LookupIterator it(prototype, factory()->constructor_string(),
804 LookupIterator::OWN_SKIP_INTERCEPTOR);
805 Handle<Object> value = JSReceiver::GetProperty(&it).ToHandleChecked();
806 DCHECK(it.IsFound());
807 DCHECK_EQ(*isolate()->object_function(), *value);
808#endif
Andrei Popescu31002712010-02-23 13:46:05 +0000809 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000810 Handle<FunctionTemplateInfo> js_global_object_constructor(
811 FunctionTemplateInfo::cast(js_global_object_template->constructor()));
812 js_global_object_function =
813 factory()->CreateApiFunction(js_global_object_constructor,
814 factory()->the_hole_value(),
815 factory()->GlobalObjectType);
Steve Blocka7e24c12009-10-30 11:49:00 +0000816 }
817
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000818 js_global_object_function->initial_map()->set_is_hidden_prototype();
819 js_global_object_function->initial_map()->set_dictionary_map(true);
820 Handle<GlobalObject> global_object =
821 factory()->NewGlobalObject(js_global_object_function);
822 if (global_object_out != NULL) {
823 *global_object_out = global_object;
Andrei Popescu31002712010-02-23 13:46:05 +0000824 }
825
826 // Step 2: create or re-initialize the global proxy object.
827 Handle<JSFunction> global_proxy_function;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000828 if (global_proxy_template.IsEmpty()) {
829 Handle<String> name = Handle<String>(heap()->empty_string());
Ben Murdoch257744e2011-11-30 15:57:28 +0000830 Handle<Code> code = Handle<Code>(isolate()->builtins()->builtin(
Steve Block44f0eee2011-05-26 01:26:41 +0100831 Builtins::kIllegal));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000832 global_proxy_function = factory()->NewFunction(
833 name, code, JS_GLOBAL_PROXY_TYPE, JSGlobalProxy::kSize);
Andrei Popescu31002712010-02-23 13:46:05 +0000834 } else {
835 Handle<ObjectTemplateInfo> data =
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000836 v8::Utils::OpenHandle(*global_proxy_template);
Andrei Popescu31002712010-02-23 13:46:05 +0000837 Handle<FunctionTemplateInfo> global_constructor(
838 FunctionTemplateInfo::cast(data->constructor()));
839 global_proxy_function =
Ben Murdoch257744e2011-11-30 15:57:28 +0000840 factory()->CreateApiFunction(global_constructor,
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000841 factory()->the_hole_value(),
842 factory()->GlobalProxyType);
Andrei Popescu31002712010-02-23 13:46:05 +0000843 }
844
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000845 Handle<String> global_name = factory()->global_string();
Andrei Popescu31002712010-02-23 13:46:05 +0000846 global_proxy_function->shared()->set_instance_class_name(*global_name);
847 global_proxy_function->initial_map()->set_is_access_check_needed(true);
848
849 // Set global_proxy.__proto__ to js_global after ConfigureGlobalObjects
850 // Return the global proxy.
851
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000852 Handle<JSGlobalProxy> global_proxy;
853 if (maybe_global_proxy.ToHandle(&global_proxy)) {
854 factory()->ReinitializeJSGlobalProxy(global_proxy, global_proxy_function);
Andrei Popescu31002712010-02-23 13:46:05 +0000855 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000856 global_proxy = Handle<JSGlobalProxy>::cast(
Ben Murdoch257744e2011-11-30 15:57:28 +0000857 factory()->NewJSObject(global_proxy_function, TENURED));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000858 global_proxy->set_hash(heap()->undefined_value());
Andrei Popescu31002712010-02-23 13:46:05 +0000859 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000860 return global_proxy;
Andrei Popescu31002712010-02-23 13:46:05 +0000861}
862
863
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000864void Genesis::HookUpGlobalProxy(Handle<GlobalObject> global_object,
Andrei Popescu31002712010-02-23 13:46:05 +0000865 Handle<JSGlobalProxy> global_proxy) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000866 // Set the native context for the global object.
867 global_object->set_native_context(*native_context());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000868 global_object->set_global_proxy(*global_proxy);
869 global_proxy->set_native_context(*native_context());
870 native_context()->set_global_proxy(*global_proxy);
Andrei Popescu31002712010-02-23 13:46:05 +0000871}
872
873
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000874void Genesis::HookUpGlobalObject(Handle<GlobalObject> global_object) {
875 Handle<GlobalObject> global_object_from_snapshot(
876 GlobalObject::cast(native_context()->extension()));
877 Handle<JSBuiltinsObject> builtins_global(native_context()->builtins());
878 native_context()->set_extension(*global_object);
879 native_context()->set_global_object(*global_object);
880 native_context()->set_security_token(*global_object);
Andrei Popescu402d9372010-02-26 13:31:12 +0000881 static const PropertyAttributes attributes =
882 static_cast<PropertyAttributes>(READ_ONLY | DONT_DELETE);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000883 Runtime::DefineObjectProperty(builtins_global, factory()->global_string(),
884 global_object, attributes).Assert();
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100885 // Set up the reference from the global object to the builtins object.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000886 JSGlobalObject::cast(*global_object)->set_builtins(*builtins_global);
887 TransferNamedProperties(global_object_from_snapshot, global_object);
888 TransferIndexedProperties(global_object_from_snapshot, global_object);
Andrei Popescu402d9372010-02-26 13:31:12 +0000889}
890
891
892// This is only called if we are not using snapshots. The equivalent
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000893// work in the snapshot case is done in HookUpGlobalObject.
894void Genesis::InitializeGlobal(Handle<GlobalObject> global_object,
Andrei Popescu31002712010-02-23 13:46:05 +0000895 Handle<JSFunction> empty_function) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000896 // --- N a t i v e C o n t e x t ---
Andrei Popescu31002712010-02-23 13:46:05 +0000897 // Use the empty function as closure (no scope info).
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000898 native_context()->set_closure(*empty_function);
899 native_context()->set_previous(NULL);
Andrei Popescu31002712010-02-23 13:46:05 +0000900 // Set extension and global object.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000901 native_context()->set_extension(*global_object);
902 native_context()->set_global_object(*global_object);
903 // Security setup: Set the security token of the native context to the global
904 // object. This makes the security check between two different contexts fail
905 // by default even in case of global object reinitialization.
906 native_context()->set_security_token(*global_object);
Andrei Popescu31002712010-02-23 13:46:05 +0000907
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000908 Isolate* isolate = global_object->GetIsolate();
Steve Block44f0eee2011-05-26 01:26:41 +0100909 Factory* factory = isolate->factory();
910 Heap* heap = isolate->heap();
911
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400912 Handle<ScriptContextTable> script_context_table =
913 factory->NewScriptContextTable();
914 native_context()->set_script_context_table(*script_context_table);
915
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000916 Handle<String> object_name = factory->Object_string();
917 JSObject::AddProperty(
918 global_object, object_name, isolate->object_function(), DONT_ENUM);
Andrei Popescu31002712010-02-23 13:46:05 +0000919
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000920 Handle<JSObject> global(native_context()->global_object());
Steve Blocka7e24c12009-10-30 11:49:00 +0000921
922 // Install global Function object
923 InstallFunction(global, "Function", JS_FUNCTION_TYPE, JSFunction::kSize,
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000924 empty_function, Builtins::kIllegal);
Steve Blocka7e24c12009-10-30 11:49:00 +0000925
926 { // --- A r r a y ---
927 Handle<JSFunction> array_function =
928 InstallFunction(global, "Array", JS_ARRAY_TYPE, JSArray::kSize,
Steve Block44f0eee2011-05-26 01:26:41 +0100929 isolate->initial_object_prototype(),
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000930 Builtins::kArrayCode);
Steve Blocka7e24c12009-10-30 11:49:00 +0000931 array_function->shared()->DontAdaptArguments();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000932 array_function->shared()->set_function_data(Smi::FromInt(kArrayCode));
Steve Blocka7e24c12009-10-30 11:49:00 +0000933
934 // This seems a bit hackish, but we need to make sure Array.length
935 // is 1.
936 array_function->shared()->set_length(1);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000937
938 Handle<Map> initial_map(array_function->initial_map());
939
940 // This assert protects an optimization in
941 // HGraphBuilder::JSArrayBuilder::EmitMapCode()
942 DCHECK(initial_map->elements_kind() == GetInitialFastElementsKind());
943 Map::EnsureDescriptorSlack(initial_map, 1);
944
945 PropertyAttributes attribs = static_cast<PropertyAttributes>(
946 DONT_ENUM | DONT_DELETE);
947
948 Handle<AccessorInfo> array_length =
949 Accessors::ArrayLengthInfo(isolate, attribs);
950 { // Add length.
951 CallbacksDescriptor d(
952 Handle<Name>(Name::cast(array_length->name())),
953 array_length, attribs);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400954 initial_map->AppendDescriptor(&d);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000955 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000956
Steve Blocka7e24c12009-10-30 11:49:00 +0000957 // array_function is used internally. JS code creating array object should
958 // search for the 'Array' property on the global object and use that one
959 // as the constructor. 'Array' property on a global object can be
960 // overwritten by JS code.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000961 native_context()->set_array_function(*array_function);
962
963 // Cache the array maps, needed by ArrayConstructorStub
964 CacheInitialJSArrayMaps(native_context(), initial_map);
965 ArrayConstructorStub array_constructor_stub(isolate);
966 Handle<Code> code = array_constructor_stub.GetCode();
967 array_function->shared()->set_construct_stub(*code);
Steve Blocka7e24c12009-10-30 11:49:00 +0000968 }
969
970 { // --- N u m b e r ---
971 Handle<JSFunction> number_fun =
972 InstallFunction(global, "Number", JS_VALUE_TYPE, JSValue::kSize,
Steve Block44f0eee2011-05-26 01:26:41 +0100973 isolate->initial_object_prototype(),
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000974 Builtins::kIllegal);
975 native_context()->set_number_function(*number_fun);
Steve Blocka7e24c12009-10-30 11:49:00 +0000976 }
977
978 { // --- B o o l e a n ---
979 Handle<JSFunction> boolean_fun =
980 InstallFunction(global, "Boolean", JS_VALUE_TYPE, JSValue::kSize,
Steve Block44f0eee2011-05-26 01:26:41 +0100981 isolate->initial_object_prototype(),
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000982 Builtins::kIllegal);
983 native_context()->set_boolean_function(*boolean_fun);
Steve Blocka7e24c12009-10-30 11:49:00 +0000984 }
985
986 { // --- S t r i n g ---
987 Handle<JSFunction> string_fun =
988 InstallFunction(global, "String", JS_VALUE_TYPE, JSValue::kSize,
Steve Block44f0eee2011-05-26 01:26:41 +0100989 isolate->initial_object_prototype(),
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000990 Builtins::kIllegal);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100991 string_fun->shared()->set_construct_stub(
Steve Block44f0eee2011-05-26 01:26:41 +0100992 isolate->builtins()->builtin(Builtins::kStringConstructCode));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000993 native_context()->set_string_function(*string_fun);
Steve Blocka7e24c12009-10-30 11:49:00 +0000994
995 Handle<Map> string_map =
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000996 Handle<Map>(native_context()->string_function()->initial_map());
997 Map::EnsureDescriptorSlack(string_map, 1);
998
999 PropertyAttributes attribs = static_cast<PropertyAttributes>(
1000 DONT_ENUM | DONT_DELETE | READ_ONLY);
1001 Handle<AccessorInfo> string_length(
1002 Accessors::StringLengthInfo(isolate, attribs));
1003
1004 { // Add length.
1005 CallbacksDescriptor d(factory->length_string(), string_length, attribs);
1006 string_map->AppendDescriptor(&d);
1007 }
1008 }
1009
1010 {
1011 // --- S y m b o l ---
1012 Handle<JSFunction> symbol_fun = InstallFunction(
1013 global, "Symbol", JS_VALUE_TYPE, JSValue::kSize,
1014 isolate->initial_object_prototype(), Builtins::kIllegal);
1015 native_context()->set_symbol_function(*symbol_fun);
Steve Blocka7e24c12009-10-30 11:49:00 +00001016 }
1017
1018 { // --- D a t e ---
1019 // Builtin functions for Date.prototype.
1020 Handle<JSFunction> date_fun =
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001021 InstallFunction(global, "Date", JS_DATE_TYPE, JSDate::kSize,
Steve Block44f0eee2011-05-26 01:26:41 +01001022 isolate->initial_object_prototype(),
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001023 Builtins::kIllegal);
Steve Blocka7e24c12009-10-30 11:49:00 +00001024
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001025 native_context()->set_date_function(*date_fun);
Steve Blocka7e24c12009-10-30 11:49:00 +00001026 }
1027
1028
1029 { // -- R e g E x p
1030 // Builtin functions for RegExp.prototype.
1031 Handle<JSFunction> regexp_fun =
1032 InstallFunction(global, "RegExp", JS_REGEXP_TYPE, JSRegExp::kSize,
Steve Block44f0eee2011-05-26 01:26:41 +01001033 isolate->initial_object_prototype(),
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001034 Builtins::kIllegal);
1035 native_context()->set_regexp_function(*regexp_fun);
Steve Block6ded16b2010-05-10 14:33:55 +01001036
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001037 DCHECK(regexp_fun->has_initial_map());
Steve Block6ded16b2010-05-10 14:33:55 +01001038 Handle<Map> initial_map(regexp_fun->initial_map());
1039
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001040 DCHECK_EQ(0, initial_map->inobject_properties());
Steve Block6ded16b2010-05-10 14:33:55 +01001041
Steve Block6ded16b2010-05-10 14:33:55 +01001042 PropertyAttributes final =
1043 static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE | READ_ONLY);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001044 Map::EnsureDescriptorSlack(initial_map, 5);
1045
Steve Block6ded16b2010-05-10 14:33:55 +01001046 {
1047 // ECMA-262, section 15.10.7.1.
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001048 Handle<AccessorInfo> regexp_source(
1049 Accessors::RegExpSourceInfo(isolate, final));
1050 CallbacksDescriptor d(factory->source_string(), regexp_source, final);
1051 initial_map->AppendDescriptor(&d);
Steve Block6ded16b2010-05-10 14:33:55 +01001052 }
1053 {
1054 // ECMA-262, section 15.10.7.2.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001055 FieldDescriptor field(factory->global_string(),
Steve Block6ded16b2010-05-10 14:33:55 +01001056 JSRegExp::kGlobalFieldIndex,
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.3.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001063 FieldDescriptor field(factory->ignore_case_string(),
Steve Block6ded16b2010-05-10 14:33:55 +01001064 JSRegExp::kIgnoreCaseFieldIndex,
1065 final,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001066 Representation::Tagged());
1067 initial_map->AppendDescriptor(&field);
Steve Block6ded16b2010-05-10 14:33:55 +01001068 }
1069 {
1070 // ECMA-262, section 15.10.7.4.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001071 FieldDescriptor field(factory->multiline_string(),
Steve Block6ded16b2010-05-10 14:33:55 +01001072 JSRegExp::kMultilineFieldIndex,
1073 final,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001074 Representation::Tagged());
1075 initial_map->AppendDescriptor(&field);
Steve Block6ded16b2010-05-10 14:33:55 +01001076 }
1077 {
1078 // ECMA-262, section 15.10.7.5.
1079 PropertyAttributes writable =
1080 static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001081 FieldDescriptor field(factory->last_index_string(),
Steve Block6ded16b2010-05-10 14:33:55 +01001082 JSRegExp::kLastIndexFieldIndex,
1083 writable,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001084 Representation::Tagged());
1085 initial_map->AppendDescriptor(&field);
Steve Block6ded16b2010-05-10 14:33:55 +01001086 }
Steve Block6ded16b2010-05-10 14:33:55 +01001087
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001088 static const int num_fields = JSRegExp::kInObjectFieldCount;
1089 initial_map->set_inobject_properties(num_fields);
1090 initial_map->set_pre_allocated_property_fields(num_fields);
Steve Block6ded16b2010-05-10 14:33:55 +01001091 initial_map->set_unused_property_fields(0);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001092 initial_map->set_instance_size(initial_map->instance_size() +
1093 num_fields * kPointerSize);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001094
1095 // RegExp prototype object is itself a RegExp.
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001096 Handle<Map> proto_map = Map::Copy(initial_map, "RegExpPrototype");
1097 DCHECK(proto_map->prototype() == *isolate->initial_object_prototype());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001098 Handle<JSObject> proto = factory->NewJSObjectFromMap(proto_map);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001099 proto->InObjectPropertyAtPut(JSRegExp::kGlobalFieldIndex,
1100 heap->false_value());
1101 proto->InObjectPropertyAtPut(JSRegExp::kIgnoreCaseFieldIndex,
1102 heap->false_value());
1103 proto->InObjectPropertyAtPut(JSRegExp::kMultilineFieldIndex,
1104 heap->false_value());
1105 proto->InObjectPropertyAtPut(JSRegExp::kLastIndexFieldIndex,
1106 Smi::FromInt(0),
1107 SKIP_WRITE_BARRIER); // It's a Smi.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001108 proto_map->set_is_prototype_map(true);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001109 initial_map->SetPrototype(proto);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001110 factory->SetRegExpIrregexpData(Handle<JSRegExp>::cast(proto),
1111 JSRegExp::IRREGEXP, factory->empty_string(),
1112 JSRegExp::Flags(0), 0);
Steve Blocka7e24c12009-10-30 11:49:00 +00001113 }
1114
1115 { // -- J S O N
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001116 Handle<String> name = factory->InternalizeUtf8String("JSON");
1117 Handle<JSFunction> cons = factory->NewFunction(name);
1118 JSFunction::SetInstancePrototype(cons,
1119 Handle<Object>(native_context()->initial_object_prototype(), isolate));
Steve Blocka7e24c12009-10-30 11:49:00 +00001120 cons->SetInstanceClassName(*name);
Steve Block44f0eee2011-05-26 01:26:41 +01001121 Handle<JSObject> json_object = factory->NewJSObject(cons, TENURED);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001122 DCHECK(json_object->IsJSObject());
1123 JSObject::AddProperty(global, name, json_object, DONT_ENUM);
1124 native_context()->set_json_object(*json_object);
Steve Blocka7e24c12009-10-30 11:49:00 +00001125 }
1126
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001127 { // -- A r r a y B u f f e r
1128 Handle<JSFunction> array_buffer_fun =
1129 InstallFunction(
1130 global, "ArrayBuffer", JS_ARRAY_BUFFER_TYPE,
1131 JSArrayBuffer::kSizeWithInternalFields,
1132 isolate->initial_object_prototype(),
1133 Builtins::kIllegal);
1134 native_context()->set_array_buffer_fun(*array_buffer_fun);
1135 }
1136
1137 { // -- T y p e d A r r a y s
1138#define INSTALL_TYPED_ARRAY(Type, type, TYPE, ctype, size) \
1139 { \
1140 Handle<JSFunction> fun; \
1141 Handle<Map> external_map; \
1142 InstallTypedArray(#Type "Array", \
1143 TYPE##_ELEMENTS, \
1144 &fun, \
1145 &external_map); \
1146 native_context()->set_##type##_array_fun(*fun); \
1147 native_context()->set_##type##_array_external_map(*external_map); \
1148 }
1149 TYPED_ARRAYS(INSTALL_TYPED_ARRAY)
1150#undef INSTALL_TYPED_ARRAY
1151
1152 Handle<JSFunction> data_view_fun =
1153 InstallFunction(
1154 global, "DataView", JS_DATA_VIEW_TYPE,
1155 JSDataView::kSizeWithInternalFields,
1156 isolate->initial_object_prototype(),
1157 Builtins::kIllegal);
1158 native_context()->set_data_view_fun(*data_view_fun);
1159 }
1160
1161 // -- M a p
1162 InstallFunction(global, "Map", JS_MAP_TYPE, JSMap::kSize,
1163 isolate->initial_object_prototype(), Builtins::kIllegal);
1164
1165 // -- S e t
1166 InstallFunction(global, "Set", JS_SET_TYPE, JSSet::kSize,
1167 isolate->initial_object_prototype(), Builtins::kIllegal);
1168
1169 { // Set up the iterator result object
1170 STATIC_ASSERT(JSGeneratorObject::kResultPropertyCount == 2);
1171 Handle<JSFunction> object_function(native_context()->object_function());
1172 Handle<Map> iterator_result_map =
1173 Map::Create(isolate, JSGeneratorObject::kResultPropertyCount);
1174 DCHECK_EQ(JSGeneratorObject::kResultSize,
1175 iterator_result_map->instance_size());
1176 DCHECK_EQ(JSGeneratorObject::kResultPropertyCount,
1177 iterator_result_map->inobject_properties());
1178 Map::EnsureDescriptorSlack(iterator_result_map,
1179 JSGeneratorObject::kResultPropertyCount);
1180
1181 FieldDescriptor value_descr(factory->value_string(),
1182 JSGeneratorObject::kResultValuePropertyIndex,
1183 NONE, Representation::Tagged());
1184 iterator_result_map->AppendDescriptor(&value_descr);
1185
1186 FieldDescriptor done_descr(factory->done_string(),
1187 JSGeneratorObject::kResultDonePropertyIndex,
1188 NONE, Representation::Tagged());
1189 iterator_result_map->AppendDescriptor(&done_descr);
1190
1191 iterator_result_map->set_unused_property_fields(0);
1192 iterator_result_map->set_pre_allocated_property_fields(
1193 JSGeneratorObject::kResultPropertyCount);
1194 DCHECK_EQ(JSGeneratorObject::kResultSize,
1195 iterator_result_map->instance_size());
1196 native_context()->set_iterator_result_map(*iterator_result_map);
1197 }
1198
1199 // -- W e a k M a p
1200 InstallFunction(global, "WeakMap", JS_WEAK_MAP_TYPE, JSWeakMap::kSize,
1201 isolate->initial_object_prototype(), Builtins::kIllegal);
1202 // -- W e a k S e t
1203 InstallFunction(global, "WeakSet", JS_WEAK_SET_TYPE, JSWeakSet::kSize,
1204 isolate->initial_object_prototype(), Builtins::kIllegal);
1205
1206 { // --- sloppy arguments map
Steve Blocka7e24c12009-10-30 11:49:00 +00001207 // Make sure we can recognize argument objects at runtime.
1208 // This is done by introducing an anonymous function with
1209 // class_name equals 'Arguments'.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001210 Handle<String> arguments_string = factory->Arguments_string();
1211 Handle<Code> code(isolate->builtins()->builtin(Builtins::kIllegal));
1212 Handle<JSFunction> function = factory->NewFunctionWithoutPrototype(
1213 arguments_string, code);
1214 function->shared()->set_instance_class_name(*arguments_string);
Steve Blocka7e24c12009-10-30 11:49:00 +00001215
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001216 Handle<Map> map =
1217 factory->NewMap(JS_OBJECT_TYPE, Heap::kSloppyArgumentsObjectSize);
1218 // Create the descriptor array for the arguments object.
1219 Map::EnsureDescriptorSlack(map, 2);
Steve Blocka7e24c12009-10-30 11:49:00 +00001220
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001221 { // length
1222 FieldDescriptor d(factory->length_string(), Heap::kArgumentsLengthIndex,
1223 DONT_ENUM, Representation::Tagged());
1224 map->AppendDescriptor(&d);
1225 }
1226 { // callee
1227 FieldDescriptor d(factory->callee_string(), Heap::kArgumentsCalleeIndex,
1228 DONT_ENUM, Representation::Tagged());
1229 map->AppendDescriptor(&d);
1230 }
1231 // @@iterator method is added later.
Steve Blocka7e24c12009-10-30 11:49:00 +00001232
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001233 map->set_function_with_prototype(true);
1234 map->set_pre_allocated_property_fields(2);
1235 map->set_inobject_properties(2);
1236 native_context()->set_sloppy_arguments_map(*map);
Steve Blocka7e24c12009-10-30 11:49:00 +00001237
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001238 DCHECK(!function->has_initial_map());
1239 JSFunction::SetInitialMap(function, map,
1240 isolate->initial_object_prototype());
Steve Blocka7e24c12009-10-30 11:49:00 +00001241
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001242 DCHECK(map->inobject_properties() > Heap::kArgumentsCalleeIndex);
1243 DCHECK(map->inobject_properties() > Heap::kArgumentsLengthIndex);
1244 DCHECK(!map->is_dictionary_map());
1245 DCHECK(IsFastObjectElementsKind(map->elements_kind()));
Steve Block44f0eee2011-05-26 01:26:41 +01001246 }
1247
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001248 { // --- aliased arguments map
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001249 Handle<Map> map =
1250 Map::Copy(isolate->sloppy_arguments_map(), "AliasedArguments");
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001251 map->set_elements_kind(SLOPPY_ARGUMENTS_ELEMENTS);
1252 DCHECK_EQ(2, map->pre_allocated_property_fields());
1253 native_context()->set_aliased_arguments_map(*map);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001254 }
1255
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001256 { // --- strict mode arguments map
Steve Block44f0eee2011-05-26 01:26:41 +01001257 const PropertyAttributes attributes =
1258 static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE | READ_ONLY);
1259
1260 // Create the ThrowTypeError functions.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001261 Handle<AccessorPair> callee = factory->NewAccessorPair();
1262 Handle<AccessorPair> caller = factory->NewAccessorPair();
Steve Block44f0eee2011-05-26 01:26:41 +01001263
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001264 Handle<JSFunction> poison = GetStrictPoisonFunction();
Steve Block44f0eee2011-05-26 01:26:41 +01001265
1266 // Install the ThrowTypeError functions.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001267 callee->set_getter(*poison);
1268 callee->set_setter(*poison);
1269 caller->set_getter(*poison);
1270 caller->set_setter(*poison);
Steve Block44f0eee2011-05-26 01:26:41 +01001271
1272 // Create the map. Allocate one in-object field for length.
1273 Handle<Map> map = factory->NewMap(JS_OBJECT_TYPE,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001274 Heap::kStrictArgumentsObjectSize);
1275 // Create the descriptor array for the arguments object.
1276 Map::EnsureDescriptorSlack(map, 3);
1277
1278 { // length
1279 FieldDescriptor d(factory->length_string(), Heap::kArgumentsLengthIndex,
1280 DONT_ENUM, Representation::Tagged());
1281 map->AppendDescriptor(&d);
1282 }
1283 { // callee
1284 CallbacksDescriptor d(factory->callee_string(), callee, attributes);
1285 map->AppendDescriptor(&d);
1286 }
1287 { // caller
1288 CallbacksDescriptor d(factory->caller_string(), caller, attributes);
1289 map->AppendDescriptor(&d);
1290 }
1291 // @@iterator method is added later.
1292
Steve Block44f0eee2011-05-26 01:26:41 +01001293 map->set_function_with_prototype(true);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001294 DCHECK_EQ(native_context()->object_function()->prototype(),
1295 *isolate->initial_object_prototype());
1296 map->SetPrototype(isolate->initial_object_prototype());
Steve Block44f0eee2011-05-26 01:26:41 +01001297 map->set_pre_allocated_property_fields(1);
1298 map->set_inobject_properties(1);
1299
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001300 // Copy constructor from the sloppy arguments boilerplate.
Steve Block44f0eee2011-05-26 01:26:41 +01001301 map->set_constructor(
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001302 native_context()->sloppy_arguments_map()->constructor());
Steve Block44f0eee2011-05-26 01:26:41 +01001303
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001304 native_context()->set_strict_arguments_map(*map);
Steve Block44f0eee2011-05-26 01:26:41 +01001305
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001306 DCHECK(map->inobject_properties() > Heap::kArgumentsLengthIndex);
1307 DCHECK(!map->is_dictionary_map());
1308 DCHECK(IsFastObjectElementsKind(map->elements_kind()));
Steve Blocka7e24c12009-10-30 11:49:00 +00001309 }
1310
1311 { // --- context extension
1312 // Create a function for the context extension objects.
Steve Block44f0eee2011-05-26 01:26:41 +01001313 Handle<Code> code = Handle<Code>(
1314 isolate->builtins()->builtin(Builtins::kIllegal));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001315 Handle<JSFunction> context_extension_fun = factory->NewFunction(
1316 factory->empty_string(), code, JS_CONTEXT_EXTENSION_OBJECT_TYPE,
1317 JSObject::kHeaderSize);
Steve Blocka7e24c12009-10-30 11:49:00 +00001318
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001319 Handle<String> name = factory->InternalizeOneByteString(
1320 STATIC_CHAR_VECTOR("context_extension"));
Steve Blocka7e24c12009-10-30 11:49:00 +00001321 context_extension_fun->shared()->set_instance_class_name(*name);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001322 native_context()->set_context_extension_function(*context_extension_fun);
Steve Blocka7e24c12009-10-30 11:49:00 +00001323 }
1324
1325
1326 {
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001327 // Set up the call-as-function delegate.
Steve Blocka7e24c12009-10-30 11:49:00 +00001328 Handle<Code> code =
Steve Block44f0eee2011-05-26 01:26:41 +01001329 Handle<Code>(isolate->builtins()->builtin(
1330 Builtins::kHandleApiCallAsFunction));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001331 Handle<JSFunction> delegate = factory->NewFunction(
1332 factory->empty_string(), code, JS_OBJECT_TYPE, JSObject::kHeaderSize);
1333 native_context()->set_call_as_function_delegate(*delegate);
Steve Blocka7e24c12009-10-30 11:49:00 +00001334 delegate->shared()->DontAdaptArguments();
1335 }
1336
1337 {
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001338 // Set up the call-as-constructor delegate.
Steve Blocka7e24c12009-10-30 11:49:00 +00001339 Handle<Code> code =
Steve Block44f0eee2011-05-26 01:26:41 +01001340 Handle<Code>(isolate->builtins()->builtin(
1341 Builtins::kHandleApiCallAsConstructor));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001342 Handle<JSFunction> delegate = factory->NewFunction(
1343 factory->empty_string(), code, JS_OBJECT_TYPE, JSObject::kHeaderSize);
1344 native_context()->set_call_as_constructor_delegate(*delegate);
Steve Blocka7e24c12009-10-30 11:49:00 +00001345 delegate->shared()->DontAdaptArguments();
1346 }
1347
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001348 // Initialize the embedder data slot.
1349 Handle<FixedArray> embedder_data = factory->NewFixedArray(3);
1350 native_context()->set_embedder_data(*embedder_data);
1351}
Steve Blocka7e24c12009-10-30 11:49:00 +00001352
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001353
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001354void Genesis::InstallTypedArray(
1355 const char* name,
1356 ElementsKind elements_kind,
1357 Handle<JSFunction>* fun,
1358 Handle<Map>* external_map) {
1359 Handle<JSObject> global = Handle<JSObject>(native_context()->global_object());
1360 Handle<JSFunction> result = InstallFunction(
1361 global, name, JS_TYPED_ARRAY_TYPE, JSTypedArray::kSize,
1362 isolate()->initial_object_prototype(), Builtins::kIllegal);
1363
1364 Handle<Map> initial_map = isolate()->factory()->NewMap(
1365 JS_TYPED_ARRAY_TYPE,
1366 JSTypedArray::kSizeWithInternalFields,
1367 elements_kind);
1368 JSFunction::SetInitialMap(result, initial_map,
1369 handle(initial_map->prototype(), isolate()));
1370 *fun = result;
1371
1372 ElementsKind external_kind = GetNextTransitionElementsKind(elements_kind);
1373 *external_map = Map::AsElementsKind(initial_map, external_kind);
Steve Blocka7e24c12009-10-30 11:49:00 +00001374}
1375
1376
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001377void Genesis::InitializeExperimentalGlobal() {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001378#define FEATURE_INITIALIZE_GLOBAL(id, descr) InitializeGlobal_##id();
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001379
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001380 HARMONY_INPROGRESS(FEATURE_INITIALIZE_GLOBAL)
1381 HARMONY_STAGED(FEATURE_INITIALIZE_GLOBAL)
1382 HARMONY_SHIPPING(FEATURE_INITIALIZE_GLOBAL)
1383#undef FEATURE_INITIALIZE_GLOBAL
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001384}
1385
1386
Ben Murdoch257744e2011-11-30 15:57:28 +00001387bool Genesis::CompileBuiltin(Isolate* isolate, int index) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001388 Vector<const char> name = Natives::GetScriptName(index);
Steve Block44f0eee2011-05-26 01:26:41 +01001389 Handle<String> source_code =
Ben Murdoch257744e2011-11-30 15:57:28 +00001390 isolate->bootstrapper()->NativesSourceLookup(index);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001391 return CompileNative(isolate, name, source_code);
Ben Murdoch257744e2011-11-30 15:57:28 +00001392}
1393
1394
1395bool Genesis::CompileExperimentalBuiltin(Isolate* isolate, int index) {
1396 Vector<const char> name = ExperimentalNatives::GetScriptName(index);
1397 Factory* factory = isolate->factory();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001398 Handle<String> source_code;
1399 ASSIGN_RETURN_ON_EXCEPTION_VALUE(
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001400 isolate, source_code,
1401 factory->NewStringFromAscii(ExperimentalNatives::GetScriptSource(index)),
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001402 false);
1403 return CompileNative(isolate, name, source_code);
Steve Blocka7e24c12009-10-30 11:49:00 +00001404}
1405
1406
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001407bool Genesis::CompileNative(Isolate* isolate,
1408 Vector<const char> name,
1409 Handle<String> source) {
1410 HandleScope scope(isolate);
1411 SuppressDebug compiling_natives(isolate->debug());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001412 // During genesis, the boilerplate for stack overflow won't work until the
1413 // environment has been at least partially initialized. Add a stack check
1414 // before entering JS code to catch overflow early.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001415 StackLimitCheck check(isolate);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001416 if (check.HasOverflowed()) return false;
1417
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001418 bool result = CompileScriptCached(isolate,
1419 name,
Andrei Popescu31002712010-02-23 13:46:05 +00001420 source,
1421 NULL,
1422 NULL,
Steve Block44f0eee2011-05-26 01:26:41 +01001423 Handle<Context>(isolate->context()),
Andrei Popescu31002712010-02-23 13:46:05 +00001424 true);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001425 DCHECK(isolate->has_pending_exception() != result);
Steve Block44f0eee2011-05-26 01:26:41 +01001426 if (!result) isolate->clear_pending_exception();
Steve Blocka7e24c12009-10-30 11:49:00 +00001427 return result;
1428}
1429
1430
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001431bool Genesis::CompileScriptCached(Isolate* isolate,
1432 Vector<const char> name,
Steve Blocka7e24c12009-10-30 11:49:00 +00001433 Handle<String> source,
1434 SourceCodeCache* cache,
1435 v8::Extension* extension,
Andrei Popescu31002712010-02-23 13:46:05 +00001436 Handle<Context> top_context,
Steve Blocka7e24c12009-10-30 11:49:00 +00001437 bool use_runtime_context) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001438 Factory* factory = isolate->factory();
1439 HandleScope scope(isolate);
Steve Block6ded16b2010-05-10 14:33:55 +01001440 Handle<SharedFunctionInfo> function_info;
Steve Blocka7e24c12009-10-30 11:49:00 +00001441
1442 // If we can't find the function in the cache, we compile a new
1443 // function and insert it into the cache.
Steve Block6ded16b2010-05-10 14:33:55 +01001444 if (cache == NULL || !cache->Lookup(name, &function_info)) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001445 DCHECK(source->IsOneByteRepresentation());
1446 Handle<String> script_name =
1447 factory->NewStringFromUtf8(name).ToHandleChecked();
1448 function_info = Compiler::CompileScript(
1449 source, script_name, 0, 0, false, top_context, extension, NULL,
1450 ScriptCompiler::kNoCompileOptions,
Andrei Popescu31002712010-02-23 13:46:05 +00001451 use_runtime_context ? NATIVES_CODE : NOT_NATIVES_CODE);
Steve Block6ded16b2010-05-10 14:33:55 +01001452 if (function_info.is_null()) return false;
1453 if (cache != NULL) cache->Add(name, function_info);
Steve Blocka7e24c12009-10-30 11:49:00 +00001454 }
1455
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001456 // Set up the function context. Conceptually, we should clone the
Steve Blocka7e24c12009-10-30 11:49:00 +00001457 // function before overwriting the context but since we're in a
1458 // single-threaded environment it is not strictly necessary.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001459 DCHECK(top_context->IsNativeContext());
Steve Blocka7e24c12009-10-30 11:49:00 +00001460 Handle<Context> context =
1461 Handle<Context>(use_runtime_context
Andrei Popescu31002712010-02-23 13:46:05 +00001462 ? Handle<Context>(top_context->runtime_context())
1463 : top_context);
Steve Blocka7e24c12009-10-30 11:49:00 +00001464 Handle<JSFunction> fun =
Steve Block44f0eee2011-05-26 01:26:41 +01001465 factory->NewFunctionFromSharedFunctionInfo(function_info, context);
Steve Blocka7e24c12009-10-30 11:49:00 +00001466
Leon Clarke4515c472010-02-03 11:58:03 +00001467 // Call function using either the runtime object or the global
Steve Blocka7e24c12009-10-30 11:49:00 +00001468 // object as the receiver. Provide no parameters.
1469 Handle<Object> receiver =
1470 Handle<Object>(use_runtime_context
Andrei Popescu31002712010-02-23 13:46:05 +00001471 ? top_context->builtins()
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001472 : top_context->global_object(),
1473 isolate);
1474 return !Execution::Call(
1475 isolate, fun, receiver, 0, NULL).is_null();
Steve Blocka7e24c12009-10-30 11:49:00 +00001476}
1477
1478
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001479static Handle<JSObject> ResolveBuiltinIdHolder(Handle<Context> native_context,
1480 const char* holder_expr) {
1481 Isolate* isolate = native_context->GetIsolate();
1482 Factory* factory = isolate->factory();
1483 Handle<GlobalObject> global(native_context->global_object());
1484 const char* period_pos = strchr(holder_expr, '.');
1485 if (period_pos == NULL) {
1486 return Handle<JSObject>::cast(
1487 Object::GetPropertyOrElement(
1488 global, factory->InternalizeUtf8String(holder_expr))
1489 .ToHandleChecked());
1490 }
1491 const char* inner = period_pos + 1;
1492 DCHECK_EQ(NULL, strchr(inner, '.'));
1493 Vector<const char> property(holder_expr,
1494 static_cast<int>(period_pos - holder_expr));
1495 Handle<String> property_string = factory->InternalizeUtf8String(property);
1496 DCHECK(!property_string.is_null());
1497 Handle<JSObject> object = Handle<JSObject>::cast(
1498 Object::GetProperty(global, property_string).ToHandleChecked());
1499 if (strcmp("prototype", inner) == 0) {
1500 Handle<JSFunction> function = Handle<JSFunction>::cast(object);
1501 return Handle<JSObject>(JSObject::cast(function->prototype()));
1502 }
1503 Handle<String> inner_string = factory->InternalizeUtf8String(inner);
1504 DCHECK(!inner_string.is_null());
1505 Handle<Object> value =
1506 Object::GetProperty(object, inner_string).ToHandleChecked();
1507 return Handle<JSObject>::cast(value);
1508}
Steve Blocka7e24c12009-10-30 11:49:00 +00001509
Steve Block44f0eee2011-05-26 01:26:41 +01001510
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001511#define INSTALL_NATIVE(Type, name, var) \
1512 Handle<String> var##_name = \
1513 factory()->InternalizeOneByteString(STATIC_CHAR_VECTOR(name)); \
1514 Handle<Object> var##_native = \
1515 Object::GetProperty(handle(native_context()->builtins()), var##_name) \
1516 .ToHandleChecked(); \
1517 native_context()->set_##var(Type::cast(*var##_native));
1518
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001519
Steve Blocka7e24c12009-10-30 11:49:00 +00001520void Genesis::InstallNativeFunctions() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001521 HandleScope scope(isolate());
Steve Blocka7e24c12009-10-30 11:49:00 +00001522 INSTALL_NATIVE(JSFunction, "CreateDate", create_date_fun);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001523
Steve Blocka7e24c12009-10-30 11:49:00 +00001524 INSTALL_NATIVE(JSFunction, "ToNumber", to_number_fun);
1525 INSTALL_NATIVE(JSFunction, "ToString", to_string_fun);
1526 INSTALL_NATIVE(JSFunction, "ToDetailString", to_detail_string_fun);
1527 INSTALL_NATIVE(JSFunction, "ToObject", to_object_fun);
1528 INSTALL_NATIVE(JSFunction, "ToInteger", to_integer_fun);
1529 INSTALL_NATIVE(JSFunction, "ToUint32", to_uint32_fun);
1530 INSTALL_NATIVE(JSFunction, "ToInt32", to_int32_fun);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001531 INSTALL_NATIVE(JSFunction, "ToLength", to_length_fun);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001532
Leon Clarkee46be812010-01-19 14:06:41 +00001533 INSTALL_NATIVE(JSFunction, "GlobalEval", global_eval_fun);
Steve Blocka7e24c12009-10-30 11:49:00 +00001534 INSTALL_NATIVE(JSFunction, "Instantiate", instantiate_fun);
1535 INSTALL_NATIVE(JSFunction, "ConfigureTemplateInstance",
1536 configure_instance_fun);
Steve Blocka7e24c12009-10-30 11:49:00 +00001537 INSTALL_NATIVE(JSFunction, "GetStackTraceLine", get_stack_trace_line_fun);
1538 INSTALL_NATIVE(JSObject, "functionCache", function_cache);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001539 INSTALL_NATIVE(JSFunction, "ToCompletePropertyDescriptor",
1540 to_complete_property_descriptor);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001541
1542 INSTALL_NATIVE(JSFunction, "IsPromise", is_promise);
1543 INSTALL_NATIVE(JSFunction, "PromiseCreate", promise_create);
1544 INSTALL_NATIVE(JSFunction, "PromiseResolve", promise_resolve);
1545 INSTALL_NATIVE(JSFunction, "PromiseReject", promise_reject);
1546 INSTALL_NATIVE(JSFunction, "PromiseChain", promise_chain);
1547 INSTALL_NATIVE(JSFunction, "PromiseCatch", promise_catch);
1548 INSTALL_NATIVE(JSFunction, "PromiseThen", promise_then);
1549
1550 INSTALL_NATIVE(JSFunction, "NotifyChange", observers_notify_change);
1551 INSTALL_NATIVE(JSFunction, "EnqueueSpliceRecord", observers_enqueue_splice);
1552 INSTALL_NATIVE(JSFunction, "BeginPerformSplice",
1553 observers_begin_perform_splice);
1554 INSTALL_NATIVE(JSFunction, "EndPerformSplice",
1555 observers_end_perform_splice);
1556 INSTALL_NATIVE(JSFunction, "NativeObjectObserve",
1557 native_object_observe);
1558 INSTALL_NATIVE(JSFunction, "NativeObjectGetNotifier",
1559 native_object_get_notifier);
1560 INSTALL_NATIVE(JSFunction, "NativeObjectNotifierPerformChange",
1561 native_object_notifier_perform_change);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001562 INSTALL_NATIVE(JSFunction, "ArrayValues", array_values_iterator);
Steve Blocka7e24c12009-10-30 11:49:00 +00001563}
1564
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001565
Ben Murdoch257744e2011-11-30 15:57:28 +00001566void Genesis::InstallExperimentalNativeFunctions() {
1567 if (FLAG_harmony_proxies) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001568 INSTALL_NATIVE(JSFunction, "DerivedHasTrap", derived_has_trap);
Ben Murdoch257744e2011-11-30 15:57:28 +00001569 INSTALL_NATIVE(JSFunction, "DerivedGetTrap", derived_get_trap);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001570 INSTALL_NATIVE(JSFunction, "DerivedSetTrap", derived_set_trap);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001571 INSTALL_NATIVE(JSFunction, "ProxyEnumerate", proxy_enumerate);
Ben Murdoch257744e2011-11-30 15:57:28 +00001572 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001573
1574#define INSTALL_NATIVE_FUNCTIONS_FOR(id, descr) InstallNativeFunctions_##id();
1575 HARMONY_INPROGRESS(INSTALL_NATIVE_FUNCTIONS_FOR)
1576 HARMONY_STAGED(INSTALL_NATIVE_FUNCTIONS_FOR)
1577 HARMONY_SHIPPING(INSTALL_NATIVE_FUNCTIONS_FOR)
1578#undef INSTALL_NATIVE_FUNCTIONS_FOR
1579}
1580
1581
1582#define EMPTY_NATIVE_FUNCTIONS_FOR_FEATURE(id) \
1583 void Genesis::InstallNativeFunctions_##id() {}
1584
1585EMPTY_NATIVE_FUNCTIONS_FOR_FEATURE(harmony_scoping)
1586EMPTY_NATIVE_FUNCTIONS_FOR_FEATURE(harmony_modules)
1587EMPTY_NATIVE_FUNCTIONS_FOR_FEATURE(harmony_strings)
1588EMPTY_NATIVE_FUNCTIONS_FOR_FEATURE(harmony_arrays)
1589EMPTY_NATIVE_FUNCTIONS_FOR_FEATURE(harmony_array_includes)
1590EMPTY_NATIVE_FUNCTIONS_FOR_FEATURE(harmony_classes)
1591EMPTY_NATIVE_FUNCTIONS_FOR_FEATURE(harmony_object_literals)
1592EMPTY_NATIVE_FUNCTIONS_FOR_FEATURE(harmony_regexps)
1593EMPTY_NATIVE_FUNCTIONS_FOR_FEATURE(harmony_arrow_functions)
1594EMPTY_NATIVE_FUNCTIONS_FOR_FEATURE(harmony_numeric_literals)
1595EMPTY_NATIVE_FUNCTIONS_FOR_FEATURE(harmony_tostring)
1596EMPTY_NATIVE_FUNCTIONS_FOR_FEATURE(harmony_templates)
1597EMPTY_NATIVE_FUNCTIONS_FOR_FEATURE(harmony_sloppy)
1598EMPTY_NATIVE_FUNCTIONS_FOR_FEATURE(harmony_unicode)
1599
1600
1601void Genesis::InstallNativeFunctions_harmony_proxies() {
1602 if (FLAG_harmony_proxies) {
1603 INSTALL_NATIVE(JSFunction, "DerivedHasTrap", derived_has_trap);
1604 INSTALL_NATIVE(JSFunction, "DerivedGetTrap", derived_get_trap);
1605 INSTALL_NATIVE(JSFunction, "DerivedSetTrap", derived_set_trap);
1606 INSTALL_NATIVE(JSFunction, "ProxyEnumerate", proxy_enumerate);
1607 }
Ben Murdoch257744e2011-11-30 15:57:28 +00001608}
1609
Steve Blocka7e24c12009-10-30 11:49:00 +00001610#undef INSTALL_NATIVE
1611
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001612#define EMPTY_INITIALIZE_GLOBAL_FOR_FEATURE(id) \
1613 void Genesis::InitializeGlobal_##id() {}
1614
1615EMPTY_INITIALIZE_GLOBAL_FOR_FEATURE(harmony_scoping)
1616EMPTY_INITIALIZE_GLOBAL_FOR_FEATURE(harmony_modules)
1617EMPTY_INITIALIZE_GLOBAL_FOR_FEATURE(harmony_strings)
1618EMPTY_INITIALIZE_GLOBAL_FOR_FEATURE(harmony_arrays)
1619EMPTY_INITIALIZE_GLOBAL_FOR_FEATURE(harmony_array_includes)
1620EMPTY_INITIALIZE_GLOBAL_FOR_FEATURE(harmony_classes)
1621EMPTY_INITIALIZE_GLOBAL_FOR_FEATURE(harmony_object_literals)
1622EMPTY_INITIALIZE_GLOBAL_FOR_FEATURE(harmony_arrow_functions)
1623EMPTY_INITIALIZE_GLOBAL_FOR_FEATURE(harmony_numeric_literals)
1624EMPTY_INITIALIZE_GLOBAL_FOR_FEATURE(harmony_tostring)
1625EMPTY_INITIALIZE_GLOBAL_FOR_FEATURE(harmony_proxies)
1626EMPTY_INITIALIZE_GLOBAL_FOR_FEATURE(harmony_templates)
1627EMPTY_INITIALIZE_GLOBAL_FOR_FEATURE(harmony_sloppy)
1628EMPTY_INITIALIZE_GLOBAL_FOR_FEATURE(harmony_unicode)
1629
1630void Genesis::InitializeGlobal_harmony_regexps() {
1631 Handle<JSObject> builtins(native_context()->builtins());
1632
1633 Handle<HeapObject> flag(FLAG_harmony_regexps ? heap()->true_value()
1634 : heap()->false_value());
1635 PropertyAttributes attributes =
1636 static_cast<PropertyAttributes>(DONT_DELETE | READ_ONLY);
1637 Runtime::DefineObjectProperty(builtins, factory()->harmony_regexps_string(),
1638 flag, attributes).Assert();
1639}
1640
Steve Blocka7e24c12009-10-30 11:49:00 +00001641
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001642Handle<JSFunction> Genesis::InstallInternalArray(
1643 Handle<JSBuiltinsObject> builtins,
1644 const char* name,
1645 ElementsKind elements_kind) {
1646 // --- I n t e r n a l A r r a y ---
1647 // An array constructor on the builtins object that works like
1648 // the public Array constructor, except that its prototype
1649 // doesn't inherit from Object.prototype.
1650 // To be used only for internal work by builtins. Instances
1651 // must not be leaked to user code.
1652 Handle<JSObject> prototype =
1653 factory()->NewJSObject(isolate()->object_function(), TENURED);
1654 Handle<JSFunction> array_function = InstallFunction(
1655 builtins, name, JS_ARRAY_TYPE, JSArray::kSize,
1656 prototype, Builtins::kInternalArrayCode);
1657
1658 InternalArrayConstructorStub internal_array_constructor_stub(isolate());
1659 Handle<Code> code = internal_array_constructor_stub.GetCode();
1660 array_function->shared()->set_construct_stub(*code);
1661 array_function->shared()->DontAdaptArguments();
1662
1663 Handle<Map> original_map(array_function->initial_map());
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001664 Handle<Map> initial_map = Map::Copy(original_map, "InternalArray");
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001665 initial_map->set_elements_kind(elements_kind);
1666 JSFunction::SetInitialMap(array_function, initial_map, prototype);
1667
1668 // Make "length" magic on instances.
1669 Map::EnsureDescriptorSlack(initial_map, 1);
1670
1671 PropertyAttributes attribs = static_cast<PropertyAttributes>(
1672 DONT_ENUM | DONT_DELETE);
1673
1674 Handle<AccessorInfo> array_length =
1675 Accessors::ArrayLengthInfo(isolate(), attribs);
1676 { // Add length.
1677 CallbacksDescriptor d(
1678 Handle<Name>(Name::cast(array_length->name())), array_length, attribs);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001679 initial_map->AppendDescriptor(&d);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001680 }
1681
1682 return array_function;
1683}
1684
1685
Steve Blocka7e24c12009-10-30 11:49:00 +00001686bool Genesis::InstallNatives() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001687 HandleScope scope(isolate());
Steve Blocka7e24c12009-10-30 11:49:00 +00001688
1689 // Create a function for the builtins object. Allocate space for the
1690 // JavaScript builtins, a reference to the builtins object
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001691 // (itself) and a reference to the native_context directly in the object.
Steve Block44f0eee2011-05-26 01:26:41 +01001692 Handle<Code> code = Handle<Code>(
Ben Murdoch257744e2011-11-30 15:57:28 +00001693 isolate()->builtins()->builtin(Builtins::kIllegal));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001694 Handle<JSFunction> builtins_fun = factory()->NewFunction(
1695 factory()->empty_string(), code, JS_BUILTINS_OBJECT_TYPE,
1696 JSBuiltinsObject::kSize);
Steve Blocka7e24c12009-10-30 11:49:00 +00001697
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001698 Handle<String> name =
1699 factory()->InternalizeOneByteString(STATIC_CHAR_VECTOR("builtins"));
Steve Blocka7e24c12009-10-30 11:49:00 +00001700 builtins_fun->shared()->set_instance_class_name(*name);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001701 builtins_fun->initial_map()->set_dictionary_map(true);
1702 builtins_fun->initial_map()->set_prototype(heap()->null_value());
Steve Blocka7e24c12009-10-30 11:49:00 +00001703
1704 // Allocate the builtins object.
1705 Handle<JSBuiltinsObject> builtins =
Ben Murdoch257744e2011-11-30 15:57:28 +00001706 Handle<JSBuiltinsObject>::cast(factory()->NewGlobalObject(builtins_fun));
Steve Blocka7e24c12009-10-30 11:49:00 +00001707 builtins->set_builtins(*builtins);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001708 builtins->set_native_context(*native_context());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001709 builtins->set_global_proxy(native_context()->global_proxy());
1710
Steve Blocka7e24c12009-10-30 11:49:00 +00001711
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001712 // Set up the 'global' properties of the builtins object. The
Steve Blocka7e24c12009-10-30 11:49:00 +00001713 // 'global' property that refers to the global object is the only
1714 // way to get from code running in the builtins context to the
1715 // global object.
1716 static const PropertyAttributes attributes =
1717 static_cast<PropertyAttributes>(READ_ONLY | DONT_DELETE);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001718 Handle<String> global_string =
1719 factory()->InternalizeOneByteString(STATIC_CHAR_VECTOR("global"));
1720 Handle<Object> global_obj(native_context()->global_object(), isolate());
1721 JSObject::AddProperty(builtins, global_string, global_obj, attributes);
1722 Handle<String> builtins_string =
1723 factory()->InternalizeOneByteString(STATIC_CHAR_VECTOR("builtins"));
1724 JSObject::AddProperty(builtins, builtins_string, builtins, attributes);
Steve Blocka7e24c12009-10-30 11:49:00 +00001725
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001726 // Set up the reference from the global object to the builtins object.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001727 JSGlobalObject::cast(native_context()->global_object())->
1728 set_builtins(*builtins);
Steve Blocka7e24c12009-10-30 11:49:00 +00001729
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001730 // Create a bridge function that has context in the native context.
1731 Handle<JSFunction> bridge = factory()->NewFunction(factory()->empty_string());
1732 DCHECK(bridge->context() == *isolate()->native_context());
Steve Blocka7e24c12009-10-30 11:49:00 +00001733
1734 // Allocate the builtins context.
1735 Handle<Context> context =
Ben Murdoch257744e2011-11-30 15:57:28 +00001736 factory()->NewFunctionContext(Context::MIN_CONTEXT_SLOTS, bridge);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001737 context->set_global_object(*builtins); // override builtins global object
Steve Blocka7e24c12009-10-30 11:49:00 +00001738
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001739 native_context()->set_runtime_context(*context);
Steve Blocka7e24c12009-10-30 11:49:00 +00001740
1741 { // -- S c r i p t
1742 // Builtin functions for Script.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001743 Handle<JSFunction> script_fun = InstallFunction(
1744 builtins, "Script", JS_VALUE_TYPE, JSValue::kSize,
1745 isolate()->initial_object_prototype(), Builtins::kIllegal);
Steve Blocka7e24c12009-10-30 11:49:00 +00001746 Handle<JSObject> prototype =
Ben Murdoch257744e2011-11-30 15:57:28 +00001747 factory()->NewJSObject(isolate()->object_function(), TENURED);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001748 Accessors::FunctionSetPrototype(script_fun, prototype).Assert();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001749 native_context()->set_script_function(*script_fun);
Steve Blocka7e24c12009-10-30 11:49:00 +00001750
1751 Handle<Map> script_map = Handle<Map>(script_fun->initial_map());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001752 Map::EnsureDescriptorSlack(script_map, 14);
1753
1754 PropertyAttributes attribs =
1755 static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE | READ_ONLY);
1756
1757 Handle<AccessorInfo> script_column =
1758 Accessors::ScriptColumnOffsetInfo(isolate(), attribs);
1759 {
1760 CallbacksDescriptor d(Handle<Name>(Name::cast(script_column->name())),
1761 script_column, attribs);
1762 script_map->AppendDescriptor(&d);
1763 }
1764
1765 Handle<AccessorInfo> script_id =
1766 Accessors::ScriptIdInfo(isolate(), attribs);
1767 {
1768 CallbacksDescriptor d(Handle<Name>(Name::cast(script_id->name())),
1769 script_id, attribs);
1770 script_map->AppendDescriptor(&d);
1771 }
1772
1773
1774 Handle<AccessorInfo> script_name =
1775 Accessors::ScriptNameInfo(isolate(), attribs);
1776 {
1777 CallbacksDescriptor d(Handle<Name>(Name::cast(script_name->name())),
1778 script_name, attribs);
1779 script_map->AppendDescriptor(&d);
1780 }
1781
1782 Handle<AccessorInfo> script_line =
1783 Accessors::ScriptLineOffsetInfo(isolate(), attribs);
1784 {
1785 CallbacksDescriptor d(Handle<Name>(Name::cast(script_line->name())),
1786 script_line, attribs);
1787 script_map->AppendDescriptor(&d);
1788 }
1789
1790 Handle<AccessorInfo> script_source =
1791 Accessors::ScriptSourceInfo(isolate(), attribs);
1792 {
1793 CallbacksDescriptor d(Handle<Name>(Name::cast(script_source->name())),
1794 script_source, attribs);
1795 script_map->AppendDescriptor(&d);
1796 }
1797
1798 Handle<AccessorInfo> script_type =
1799 Accessors::ScriptTypeInfo(isolate(), attribs);
1800 {
1801 CallbacksDescriptor d(Handle<Name>(Name::cast(script_type->name())),
1802 script_type, attribs);
1803 script_map->AppendDescriptor(&d);
1804 }
1805
1806 Handle<AccessorInfo> script_compilation_type =
1807 Accessors::ScriptCompilationTypeInfo(isolate(), attribs);
1808 {
1809 CallbacksDescriptor d(
1810 Handle<Name>(Name::cast(script_compilation_type->name())),
1811 script_compilation_type, attribs);
1812 script_map->AppendDescriptor(&d);
1813 }
1814
1815 Handle<AccessorInfo> script_line_ends =
1816 Accessors::ScriptLineEndsInfo(isolate(), attribs);
1817 {
1818 CallbacksDescriptor d(Handle<Name>(Name::cast(script_line_ends->name())),
1819 script_line_ends, attribs);
1820 script_map->AppendDescriptor(&d);
1821 }
1822
1823 Handle<AccessorInfo> script_context_data =
1824 Accessors::ScriptContextDataInfo(isolate(), attribs);
1825 {
1826 CallbacksDescriptor d(
1827 Handle<Name>(Name::cast(script_context_data->name())),
1828 script_context_data, attribs);
1829 script_map->AppendDescriptor(&d);
1830 }
1831
1832 Handle<AccessorInfo> script_eval_from_script =
1833 Accessors::ScriptEvalFromScriptInfo(isolate(), attribs);
1834 {
1835 CallbacksDescriptor d(
1836 Handle<Name>(Name::cast(script_eval_from_script->name())),
1837 script_eval_from_script, attribs);
1838 script_map->AppendDescriptor(&d);
1839 }
1840
1841 Handle<AccessorInfo> script_eval_from_script_position =
1842 Accessors::ScriptEvalFromScriptPositionInfo(isolate(), attribs);
1843 {
1844 CallbacksDescriptor d(
1845 Handle<Name>(Name::cast(script_eval_from_script_position->name())),
1846 script_eval_from_script_position, attribs);
1847 script_map->AppendDescriptor(&d);
1848 }
1849
1850 Handle<AccessorInfo> script_eval_from_function_name =
1851 Accessors::ScriptEvalFromFunctionNameInfo(isolate(), attribs);
1852 {
1853 CallbacksDescriptor d(
1854 Handle<Name>(Name::cast(script_eval_from_function_name->name())),
1855 script_eval_from_function_name, attribs);
1856 script_map->AppendDescriptor(&d);
1857 }
1858
1859 Handle<AccessorInfo> script_source_url =
1860 Accessors::ScriptSourceUrlInfo(isolate(), attribs);
1861 {
1862 CallbacksDescriptor d(Handle<Name>(Name::cast(script_source_url->name())),
1863 script_source_url, attribs);
1864 script_map->AppendDescriptor(&d);
1865 }
1866
1867 Handle<AccessorInfo> script_source_mapping_url =
1868 Accessors::ScriptSourceMappingUrlInfo(isolate(), attribs);
1869 {
1870 CallbacksDescriptor d(
1871 Handle<Name>(Name::cast(script_source_mapping_url->name())),
1872 script_source_mapping_url, attribs);
1873 script_map->AppendDescriptor(&d);
1874 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001875
1876 // Allocate the empty script.
Ben Murdoch257744e2011-11-30 15:57:28 +00001877 Handle<Script> script = factory()->NewScript(factory()->empty_string());
Steve Blocka7e24c12009-10-30 11:49:00 +00001878 script->set_type(Smi::FromInt(Script::TYPE_NATIVE));
Ben Murdoch257744e2011-11-30 15:57:28 +00001879 heap()->public_set_empty_script(*script);
Steve Blocka7e24c12009-10-30 11:49:00 +00001880 }
Steve Block6ded16b2010-05-10 14:33:55 +01001881 {
1882 // Builtin function for OpaqueReference -- a JSValue-based object,
1883 // that keeps its field isolated from JavaScript code. It may store
1884 // objects, that JavaScript code may not access.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001885 Handle<JSFunction> opaque_reference_fun = InstallFunction(
1886 builtins, "OpaqueReference", JS_VALUE_TYPE, JSValue::kSize,
1887 isolate()->initial_object_prototype(), Builtins::kIllegal);
Steve Block6ded16b2010-05-10 14:33:55 +01001888 Handle<JSObject> prototype =
Ben Murdoch257744e2011-11-30 15:57:28 +00001889 factory()->NewJSObject(isolate()->object_function(), TENURED);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001890 Accessors::FunctionSetPrototype(opaque_reference_fun, prototype).Assert();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001891 native_context()->set_opaque_reference_function(*opaque_reference_fun);
Steve Block6ded16b2010-05-10 14:33:55 +01001892 }
1893
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001894 // InternalArrays should not use Smi-Only array optimizations. There are too
1895 // many places in the C++ runtime code (e.g. RegEx) that assume that
1896 // elements in InternalArrays can be set to non-Smi values without going
1897 // through a common bottleneck that would make the SMI_ONLY -> FAST_ELEMENT
1898 // transition easy to trap. Moreover, they rarely are smi-only.
1899 {
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001900 Handle<JSFunction> array_function =
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001901 InstallInternalArray(builtins, "InternalArray", FAST_HOLEY_ELEMENTS);
1902 native_context()->set_internal_array_function(*array_function);
1903 }
1904
1905 {
1906 InstallInternalArray(builtins, "InternalPackedArray", FAST_ELEMENTS);
1907 }
1908
1909 { // -- S e t I t e r a t o r
1910 Handle<JSFunction> set_iterator_function = InstallFunction(
1911 builtins, "SetIterator", JS_SET_ITERATOR_TYPE, JSSetIterator::kSize,
1912 isolate()->initial_object_prototype(), Builtins::kIllegal);
1913 native_context()->set_set_iterator_map(
1914 set_iterator_function->initial_map());
1915 }
1916
1917 { // -- M a p I t e r a t o r
1918 Handle<JSFunction> map_iterator_function = InstallFunction(
1919 builtins, "MapIterator", JS_MAP_ITERATOR_TYPE, JSMapIterator::kSize,
1920 isolate()->initial_object_prototype(), Builtins::kIllegal);
1921 native_context()->set_map_iterator_map(
1922 map_iterator_function->initial_map());
1923 }
1924
1925 {
1926 // Create generator meta-objects and install them on the builtins object.
1927 Handle<JSObject> builtins(native_context()->builtins());
1928 Handle<JSObject> generator_object_prototype =
Ben Murdoch257744e2011-11-30 15:57:28 +00001929 factory()->NewJSObject(isolate()->object_function(), TENURED);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001930 Handle<JSFunction> generator_function_prototype =
1931 InstallFunction(builtins, "GeneratorFunctionPrototype",
1932 JS_FUNCTION_TYPE, JSFunction::kHeaderSize,
1933 generator_object_prototype, Builtins::kIllegal);
1934 InstallFunction(builtins, "GeneratorFunction", JS_FUNCTION_TYPE,
1935 JSFunction::kSize, generator_function_prototype,
1936 Builtins::kIllegal);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001937
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001938 // Create maps for generator functions and their prototypes. Store those
1939 // maps in the native context.
1940 Handle<Map> generator_function_map =
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001941 Map::Copy(sloppy_function_map_writable_prototype_, "GeneratorFunction");
1942 generator_function_map->SetPrototype(generator_function_prototype);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001943 native_context()->set_sloppy_generator_function_map(
1944 *generator_function_map);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001945
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001946 // The "arguments" and "caller" instance properties aren't specified, so
1947 // technically we could leave them out. They make even less sense for
1948 // generators than for functions. Still, the same argument that it makes
1949 // sense to keep them around but poisoned in strict mode applies to
1950 // generators as well. With poisoned accessors, naive callers can still
1951 // iterate over the properties without accessing them.
1952 //
1953 // We can't use PoisonArgumentsAndCaller because that mutates accessor pairs
1954 // in place, and the initial state of the generator function map shares the
1955 // accessor pair with sloppy functions. Also the error message should be
1956 // different. Also unhappily, we can't use the API accessors to implement
1957 // poisoning, because API accessors present themselves as data properties,
1958 // not accessor properties, and so getOwnPropertyDescriptor raises an
1959 // exception as it tries to get the values. Sadness.
1960 Handle<AccessorPair> poison_pair(factory()->NewAccessorPair());
1961 PropertyAttributes rw_attribs =
1962 static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE);
1963 Handle<JSFunction> poison_function = GetGeneratorPoisonFunction();
1964 poison_pair->set_getter(*poison_function);
1965 poison_pair->set_setter(*poison_function);
1966 ReplaceAccessors(generator_function_map, factory()->arguments_string(),
1967 rw_attribs, poison_pair);
1968 ReplaceAccessors(generator_function_map, factory()->caller_string(),
1969 rw_attribs, poison_pair);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001970
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001971 Handle<Map> strict_function_map(native_context()->strict_function_map());
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001972 Handle<Map> strict_generator_function_map =
1973 Map::Copy(strict_function_map, "StrictGeneratorFunction");
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001974 // "arguments" and "caller" already poisoned.
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001975 strict_generator_function_map->SetPrototype(generator_function_prototype);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001976 native_context()->set_strict_generator_function_map(
1977 *strict_generator_function_map);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001978
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001979 Handle<JSFunction> object_function(native_context()->object_function());
1980 Handle<Map> generator_object_prototype_map = Map::Create(isolate(), 0);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001981 generator_object_prototype_map->SetPrototype(generator_object_prototype);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001982 native_context()->set_generator_object_prototype_map(
1983 *generator_object_prototype_map);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001984 }
1985
Steve Block6ded16b2010-05-10 14:33:55 +01001986 if (FLAG_disable_native_files) {
1987 PrintF("Warning: Running without installed natives!\n");
1988 return true;
1989 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001990
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001991 // Install public symbols.
1992 {
1993 static const PropertyAttributes attributes =
1994 static_cast<PropertyAttributes>(READ_ONLY | DONT_DELETE);
1995#define INSTALL_PUBLIC_SYMBOL(name, varname, description) \
1996 Handle<String> varname = factory()->NewStringFromStaticChars(#varname); \
1997 JSObject::AddProperty(builtins, varname, factory()->name(), attributes);
1998 PUBLIC_SYMBOL_LIST(INSTALL_PUBLIC_SYMBOL)
1999#undef INSTALL_PUBLIC_SYMBOL
2000 }
2001
Andrei Popescu31002712010-02-23 13:46:05 +00002002 // Install natives.
2003 for (int i = Natives::GetDebuggerCount();
2004 i < Natives::GetBuiltinsCount();
2005 i++) {
Ben Murdoch257744e2011-11-30 15:57:28 +00002006 if (!CompileBuiltin(isolate(), i)) return false;
Andrei Popescu402d9372010-02-26 13:31:12 +00002007 // TODO(ager): We really only need to install the JS builtin
2008 // functions on the builtins object after compiling and running
2009 // runtime.js.
2010 if (!InstallJSBuiltins(builtins)) return false;
Steve Blocka7e24c12009-10-30 11:49:00 +00002011 }
2012
2013 InstallNativeFunctions();
2014
Iain Merrick75681382010-08-19 15:07:18 +01002015 // Store the map for the string prototype after the natives has been compiled
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002016 // and the String function has been set up.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002017 Handle<JSFunction> string_function(native_context()->string_function());
2018 DCHECK(JSObject::cast(
Iain Merrick75681382010-08-19 15:07:18 +01002019 string_function->initial_map()->prototype())->HasFastProperties());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002020 native_context()->set_string_function_prototype_map(
Iain Merrick75681382010-08-19 15:07:18 +01002021 HeapObject::cast(string_function->initial_map()->prototype())->map());
2022
Steve Blocka7e24c12009-10-30 11:49:00 +00002023 // Install Function.prototype.call and apply.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002024 {
2025 Handle<String> key = factory()->Function_string();
Steve Blocka7e24c12009-10-30 11:49:00 +00002026 Handle<JSFunction> function =
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002027 Handle<JSFunction>::cast(Object::GetProperty(
2028 handle(native_context()->global_object()), key).ToHandleChecked());
Steve Blocka7e24c12009-10-30 11:49:00 +00002029 Handle<JSObject> proto =
2030 Handle<JSObject>(JSObject::cast(function->instance_prototype()));
2031
2032 // Install the call and the apply functions.
2033 Handle<JSFunction> call =
2034 InstallFunction(proto, "call", JS_OBJECT_TYPE, JSObject::kHeaderSize,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002035 MaybeHandle<JSObject>(), Builtins::kFunctionCall);
Steve Blocka7e24c12009-10-30 11:49:00 +00002036 Handle<JSFunction> apply =
2037 InstallFunction(proto, "apply", JS_OBJECT_TYPE, JSObject::kHeaderSize,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002038 MaybeHandle<JSObject>(), Builtins::kFunctionApply);
2039 if (FLAG_vector_ics) {
2040 // Apply embeds an IC, so we need a type vector of size 1 in the shared
2041 // function info.
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002042 FeedbackVectorSpec spec(0, 1);
2043 spec.SetKind(0, Code::CALL_IC);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002044 Handle<TypeFeedbackVector> feedback_vector =
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002045 factory()->NewTypeFeedbackVector(spec);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002046 apply->shared()->set_feedback_vector(*feedback_vector);
2047 }
Steve Blocka7e24c12009-10-30 11:49:00 +00002048
2049 // Make sure that Function.prototype.call appears to be compiled.
2050 // The code will never be called, but inline caching for call will
2051 // only work if it appears to be compiled.
2052 call->shared()->DontAdaptArguments();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002053 DCHECK(call->is_compiled());
Steve Blocka7e24c12009-10-30 11:49:00 +00002054
2055 // Set the expected parameters for apply to 2; required by builtin.
2056 apply->shared()->set_formal_parameter_count(2);
2057
2058 // Set the lengths for the functions to satisfy ECMA-262.
2059 call->shared()->set_length(1);
2060 apply->shared()->set_length(2);
2061 }
2062
Ben Murdoch42effa52011-08-19 16:40:31 +01002063 InstallBuiltinFunctionIds();
2064
Steve Block6ded16b2010-05-10 14:33:55 +01002065 // Create a constructor for RegExp results (a variant of Array that
2066 // predefines the two properties index and match).
2067 {
2068 // RegExpResult initial map.
2069
2070 // Find global.Array.prototype to inherit from.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002071 Handle<JSFunction> array_constructor(native_context()->array_function());
Steve Block6ded16b2010-05-10 14:33:55 +01002072 Handle<JSObject> array_prototype(
2073 JSObject::cast(array_constructor->instance_prototype()));
2074
2075 // Add initial map.
2076 Handle<Map> initial_map =
Ben Murdoch257744e2011-11-30 15:57:28 +00002077 factory()->NewMap(JS_ARRAY_TYPE, JSRegExpResult::kSize);
Steve Block6ded16b2010-05-10 14:33:55 +01002078 initial_map->set_constructor(*array_constructor);
2079
2080 // Set prototype on map.
2081 initial_map->set_non_instance_prototype(false);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002082 initial_map->SetPrototype(array_prototype);
Steve Block6ded16b2010-05-10 14:33:55 +01002083
2084 // Update map with length accessor from Array and add "index" and "input".
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002085 Map::EnsureDescriptorSlack(initial_map, 3);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00002086
Steve Block6ded16b2010-05-10 14:33:55 +01002087 {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002088 JSFunction* array_function = native_context()->array_function();
2089 Handle<DescriptorArray> array_descriptors(
2090 array_function->initial_map()->instance_descriptors());
2091 Handle<String> length = factory()->length_string();
2092 int old = array_descriptors->SearchWithCache(
2093 *length, array_function->initial_map());
2094 DCHECK(old != DescriptorArray::kNotFound);
2095 CallbacksDescriptor desc(length,
2096 handle(array_descriptors->GetValue(old),
2097 isolate()),
2098 array_descriptors->GetDetails(old).attributes());
2099 initial_map->AppendDescriptor(&desc);
2100 }
2101 {
2102 FieldDescriptor index_field(factory()->index_string(),
Steve Block6ded16b2010-05-10 14:33:55 +01002103 JSRegExpResult::kIndexIndex,
2104 NONE,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002105 Representation::Tagged());
2106 initial_map->AppendDescriptor(&index_field);
Steve Block6ded16b2010-05-10 14:33:55 +01002107 }
2108
2109 {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002110 FieldDescriptor input_field(factory()->input_string(),
Steve Block6ded16b2010-05-10 14:33:55 +01002111 JSRegExpResult::kInputIndex,
2112 NONE,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002113 Representation::Tagged());
2114 initial_map->AppendDescriptor(&input_field);
Steve Block6ded16b2010-05-10 14:33:55 +01002115 }
Steve Block6ded16b2010-05-10 14:33:55 +01002116
2117 initial_map->set_inobject_properties(2);
2118 initial_map->set_pre_allocated_property_fields(2);
2119 initial_map->set_unused_property_fields(0);
Steve Block6ded16b2010-05-10 14:33:55 +01002120
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002121 native_context()->set_regexp_result_map(*initial_map);
Steve Block6ded16b2010-05-10 14:33:55 +01002122 }
2123
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002124 // Add @@iterator method to the arguments object maps.
2125 {
2126 PropertyAttributes attribs = DONT_ENUM;
2127 Handle<AccessorInfo> arguments_iterator =
2128 Accessors::ArgumentsIteratorInfo(isolate(), attribs);
2129 {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002130 CallbacksDescriptor d(factory()->iterator_symbol(), arguments_iterator,
2131 attribs);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002132 Handle<Map> map(native_context()->sloppy_arguments_map());
2133 Map::EnsureDescriptorSlack(map, 1);
2134 map->AppendDescriptor(&d);
2135 }
2136 {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002137 CallbacksDescriptor d(factory()->iterator_symbol(), arguments_iterator,
2138 attribs);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002139 Handle<Map> map(native_context()->aliased_arguments_map());
2140 Map::EnsureDescriptorSlack(map, 1);
2141 map->AppendDescriptor(&d);
2142 }
2143 {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002144 CallbacksDescriptor d(factory()->iterator_symbol(), arguments_iterator,
2145 attribs);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002146 Handle<Map> map(native_context()->strict_arguments_map());
2147 Map::EnsureDescriptorSlack(map, 1);
2148 map->AppendDescriptor(&d);
2149 }
2150 }
2151
2152#ifdef VERIFY_HEAP
2153 builtins->ObjectVerify();
Steve Blocka7e24c12009-10-30 11:49:00 +00002154#endif
Andrei Popescu31002712010-02-23 13:46:05 +00002155
Steve Blocka7e24c12009-10-30 11:49:00 +00002156 return true;
2157}
2158
2159
Ben Murdoch257744e2011-11-30 15:57:28 +00002160bool Genesis::InstallExperimentalNatives() {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002161 static const char* harmony_arrays_natives[] = {
2162 "native harmony-array.js", "native harmony-typedarray.js", NULL};
2163 static const char* harmony_array_includes_natives[] = {
2164 "native harmony-array-includes.js", NULL};
2165 static const char* harmony_proxies_natives[] = {"native proxy.js", NULL};
2166 static const char* harmony_strings_natives[] = {"native harmony-string.js",
2167 NULL};
2168 static const char* harmony_classes_natives[] = {"native harmony-classes.js",
2169 NULL};
2170 static const char* harmony_modules_natives[] = {NULL};
2171 static const char* harmony_scoping_natives[] = {NULL};
2172 static const char* harmony_object_literals_natives[] = {NULL};
2173 static const char* harmony_regexps_natives[] = {
2174 "native harmony-regexp.js", NULL};
2175 static const char* harmony_arrow_functions_natives[] = {NULL};
2176 static const char* harmony_numeric_literals_natives[] = {NULL};
2177 static const char* harmony_tostring_natives[] = {"native harmony-tostring.js",
2178 NULL};
2179 static const char* harmony_templates_natives[] = {
2180 "native harmony-templates.js", NULL};
2181 static const char* harmony_sloppy_natives[] = {NULL};
2182 static const char* harmony_unicode_natives[] = {NULL};
2183
Ben Murdoch257744e2011-11-30 15:57:28 +00002184 for (int i = ExperimentalNatives::GetDebuggerCount();
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002185 i < ExperimentalNatives::GetBuiltinsCount(); i++) {
2186#define INSTALL_EXPERIMENTAL_NATIVES(id, desc) \
2187 if (FLAG_##id) { \
2188 for (size_t j = 0; id##_natives[j] != NULL; j++) { \
2189 Vector<const char> script_name = ExperimentalNatives::GetScriptName(i); \
2190 if (strncmp(script_name.start(), id##_natives[j], \
2191 script_name.length()) == 0) { \
2192 if (!CompileExperimentalBuiltin(isolate(), i)) return false; \
2193 } \
2194 } \
2195 }
2196 HARMONY_INPROGRESS(INSTALL_EXPERIMENTAL_NATIVES);
2197 HARMONY_STAGED(INSTALL_EXPERIMENTAL_NATIVES);
2198 HARMONY_SHIPPING(INSTALL_EXPERIMENTAL_NATIVES);
2199#undef INSTALL_EXPERIMENTAL_NATIVES
Ben Murdoch257744e2011-11-30 15:57:28 +00002200 }
2201
2202 InstallExperimentalNativeFunctions();
Ben Murdoch257744e2011-11-30 15:57:28 +00002203 return true;
2204}
2205
2206
Ben Murdochb0fe1622011-05-05 13:52:32 +01002207static void InstallBuiltinFunctionId(Handle<JSObject> holder,
2208 const char* function_name,
2209 BuiltinFunctionId id) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002210 Isolate* isolate = holder->GetIsolate();
2211 Handle<Object> function_object =
2212 Object::GetProperty(isolate, holder, function_name).ToHandleChecked();
2213 Handle<JSFunction> function = Handle<JSFunction>::cast(function_object);
Kristian Monsen25f61362010-05-21 11:50:48 +01002214 function->shared()->set_function_data(Smi::FromInt(id));
2215}
2216
2217
Ben Murdochb0fe1622011-05-05 13:52:32 +01002218void Genesis::InstallBuiltinFunctionIds() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002219 HandleScope scope(isolate());
Ben Murdochb0fe1622011-05-05 13:52:32 +01002220#define INSTALL_BUILTIN_ID(holder_expr, fun_name, name) \
2221 { \
2222 Handle<JSObject> holder = ResolveBuiltinIdHolder( \
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002223 native_context(), #holder_expr); \
Ben Murdochb0fe1622011-05-05 13:52:32 +01002224 BuiltinFunctionId id = k##name; \
2225 InstallBuiltinFunctionId(holder, #fun_name, id); \
Kristian Monsen25f61362010-05-21 11:50:48 +01002226 }
Ben Murdochb0fe1622011-05-05 13:52:32 +01002227 FUNCTIONS_WITH_ID_LIST(INSTALL_BUILTIN_ID)
2228#undef INSTALL_BUILTIN_ID
Kristian Monsen25f61362010-05-21 11:50:48 +01002229}
2230
2231
Steve Block6ded16b2010-05-10 14:33:55 +01002232// Do not forget to update macros.py with named constant
2233// of cache id.
2234#define JSFUNCTION_RESULT_CACHE_LIST(F) \
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002235 F(16, native_context()->regexp_function())
Steve Block6ded16b2010-05-10 14:33:55 +01002236
2237
Ben Murdoch257744e2011-11-30 15:57:28 +00002238static FixedArray* CreateCache(int size, Handle<JSFunction> factory_function) {
2239 Factory* factory = factory_function->GetIsolate()->factory();
Steve Block6ded16b2010-05-10 14:33:55 +01002240 // Caches are supposed to live for a long time, allocate in old space.
2241 int array_size = JSFunctionResultCache::kEntriesIndex + 2 * size;
2242 // Cannot use cast as object is not fully initialized yet.
2243 JSFunctionResultCache* cache = reinterpret_cast<JSFunctionResultCache*>(
Ben Murdoch257744e2011-11-30 15:57:28 +00002244 *factory->NewFixedArrayWithHoles(array_size, TENURED));
2245 cache->set(JSFunctionResultCache::kFactoryIndex, *factory_function);
Steve Block6ded16b2010-05-10 14:33:55 +01002246 cache->MakeZeroSize();
2247 return cache;
2248}
2249
2250
2251void Genesis::InstallJSFunctionResultCaches() {
2252 const int kNumberOfCaches = 0 +
2253#define F(size, func) + 1
2254 JSFUNCTION_RESULT_CACHE_LIST(F)
2255#undef F
2256 ;
2257
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002258 Handle<FixedArray> caches =
2259 factory()->NewFixedArray(kNumberOfCaches, TENURED);
Steve Block6ded16b2010-05-10 14:33:55 +01002260
2261 int index = 0;
Kristian Monsen0d5e1162010-09-30 15:31:59 +01002262
Ben Murdoch257744e2011-11-30 15:57:28 +00002263#define F(size, func) do { \
2264 FixedArray* cache = CreateCache((size), Handle<JSFunction>(func)); \
2265 caches->set(index++, cache); \
Kristian Monsen0d5e1162010-09-30 15:31:59 +01002266 } while (false)
2267
2268 JSFUNCTION_RESULT_CACHE_LIST(F);
2269
Steve Block6ded16b2010-05-10 14:33:55 +01002270#undef F
2271
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002272 native_context()->set_jsfunction_result_caches(*caches);
Steve Block6ded16b2010-05-10 14:33:55 +01002273}
2274
2275
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002276void Genesis::InitializeNormalizedMapCaches() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002277 Handle<NormalizedMapCache> cache = NormalizedMapCache::New(isolate());
2278 native_context()->set_normalized_map_cache(*cache);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002279}
2280
2281
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002282bool Bootstrapper::InstallExtensions(Handle<Context> native_context,
Andrei Popescu31002712010-02-23 13:46:05 +00002283 v8::ExtensionConfiguration* extensions) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002284 BootstrapperActive active(this);
2285 SaveContext saved_context(isolate_);
2286 isolate_->set_context(*native_context);
2287 return Genesis::InstallExtensions(native_context, extensions) &&
2288 Genesis::InstallSpecialObjects(native_context);
2289}
2290
2291
2292bool Genesis::InstallSpecialObjects(Handle<Context> native_context) {
2293 Isolate* isolate = native_context->GetIsolate();
2294 // Don't install extensions into the snapshot.
2295 if (isolate->serializer_enabled()) return true;
2296
2297 Factory* factory = isolate->factory();
2298 HandleScope scope(isolate);
2299 Handle<JSGlobalObject> global(JSGlobalObject::cast(
2300 native_context->global_object()));
2301
2302 Handle<JSObject> Error = Handle<JSObject>::cast(
2303 Object::GetProperty(isolate, global, "Error").ToHandleChecked());
2304 Handle<String> name =
2305 factory->InternalizeOneByteString(STATIC_CHAR_VECTOR("stackTraceLimit"));
2306 Handle<Smi> stack_trace_limit(Smi::FromInt(FLAG_stack_trace_limit), isolate);
2307 JSObject::AddProperty(Error, name, stack_trace_limit, NONE);
2308
2309 // Expose the natives in global if a name for it is specified.
2310 if (FLAG_expose_natives_as != NULL && strlen(FLAG_expose_natives_as) != 0) {
2311 Handle<String> natives =
2312 factory->InternalizeUtf8String(FLAG_expose_natives_as);
2313 uint32_t dummy_index;
2314 if (natives->AsArrayIndex(&dummy_index)) return true;
2315 JSObject::AddProperty(global, natives, handle(global->builtins()),
2316 DONT_ENUM);
2317 }
2318
2319 // Expose the stack trace symbol to native JS.
2320 RETURN_ON_EXCEPTION_VALUE(isolate,
2321 JSObject::SetOwnPropertyIgnoreAttributes(
2322 handle(native_context->builtins(), isolate),
2323 factory->InternalizeOneByteString(
2324 STATIC_CHAR_VECTOR("stack_trace_symbol")),
2325 factory->stack_trace_symbol(), NONE),
2326 false);
2327
2328 // Expose the debug global object in global if a name for it is specified.
2329 if (FLAG_expose_debug_as != NULL && strlen(FLAG_expose_debug_as) != 0) {
2330 // If loading fails we just bail out without installing the
2331 // debugger but without tanking the whole context.
2332 Debug* debug = isolate->debug();
2333 if (!debug->Load()) return true;
2334 Handle<Context> debug_context = debug->debug_context();
2335 // Set the security token for the debugger context to the same as
2336 // the shell native context to allow calling between these (otherwise
2337 // exposing debug global object doesn't make much sense).
2338 debug_context->set_security_token(native_context->security_token());
2339 Handle<String> debug_string =
2340 factory->InternalizeUtf8String(FLAG_expose_debug_as);
2341 uint32_t index;
2342 if (debug_string->AsArrayIndex(&index)) return true;
2343 Handle<Object> global_proxy(debug_context->global_proxy(), isolate);
2344 JSObject::AddProperty(global, debug_string, global_proxy, DONT_ENUM);
2345 }
Andrei Popescu31002712010-02-23 13:46:05 +00002346 return true;
2347}
2348
2349
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002350static uint32_t Hash(RegisteredExtension* extension) {
2351 return v8::internal::ComputePointerHash(extension);
2352}
2353
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002354
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002355Genesis::ExtensionStates::ExtensionStates() : map_(HashMap::PointersMatch, 8) {}
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002356
2357Genesis::ExtensionTraversalState Genesis::ExtensionStates::get_state(
2358 RegisteredExtension* extension) {
2359 i::HashMap::Entry* entry = map_.Lookup(extension, Hash(extension), false);
2360 if (entry == NULL) {
2361 return UNVISITED;
2362 }
2363 return static_cast<ExtensionTraversalState>(
2364 reinterpret_cast<intptr_t>(entry->value));
2365}
2366
2367void Genesis::ExtensionStates::set_state(RegisteredExtension* extension,
2368 ExtensionTraversalState state) {
2369 map_.Lookup(extension, Hash(extension), true)->value =
2370 reinterpret_cast<void*>(static_cast<intptr_t>(state));
2371}
Steve Blocka7e24c12009-10-30 11:49:00 +00002372
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002373
2374bool Genesis::InstallExtensions(Handle<Context> native_context,
Andrei Popescu31002712010-02-23 13:46:05 +00002375 v8::ExtensionConfiguration* extensions) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002376 Isolate* isolate = native_context->GetIsolate();
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002377 ExtensionStates extension_states; // All extensions have state UNVISITED.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002378 return InstallAutoExtensions(isolate, &extension_states) &&
2379 (!FLAG_expose_free_buffer ||
2380 InstallExtension(isolate, "v8/free-buffer", &extension_states)) &&
2381 (!FLAG_expose_gc ||
2382 InstallExtension(isolate, "v8/gc", &extension_states)) &&
2383 (!FLAG_expose_externalize_string ||
2384 InstallExtension(isolate, "v8/externalize", &extension_states)) &&
2385 (!FLAG_track_gc_object_stats ||
2386 InstallExtension(isolate, "v8/statistics", &extension_states)) &&
2387 (!FLAG_expose_trigger_failure ||
2388 InstallExtension(isolate, "v8/trigger-failure", &extension_states)) &&
2389 InstallRequestedExtensions(isolate, extensions, &extension_states);
2390}
Steve Blocka7e24c12009-10-30 11:49:00 +00002391
Steve Blocka7e24c12009-10-30 11:49:00 +00002392
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002393bool Genesis::InstallAutoExtensions(Isolate* isolate,
2394 ExtensionStates* extension_states) {
2395 for (v8::RegisteredExtension* it = v8::RegisteredExtension::first_extension();
2396 it != NULL;
2397 it = it->next()) {
2398 if (it->extension()->auto_enable() &&
2399 !InstallExtension(isolate, it, extension_states)) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002400 return false;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002401 }
Steve Blocka7e24c12009-10-30 11:49:00 +00002402 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002403 return true;
2404}
Steve Blocka7e24c12009-10-30 11:49:00 +00002405
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002406
2407bool Genesis::InstallRequestedExtensions(Isolate* isolate,
2408 v8::ExtensionConfiguration* extensions,
2409 ExtensionStates* extension_states) {
2410 for (const char** it = extensions->begin(); it != extensions->end(); ++it) {
2411 if (!InstallExtension(isolate, *it, extension_states)) return false;
2412 }
Steve Blocka7e24c12009-10-30 11:49:00 +00002413 return true;
2414}
2415
2416
2417// Installs a named extension. This methods is unoptimized and does
2418// not scale well if we want to support a large number of extensions.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002419bool Genesis::InstallExtension(Isolate* isolate,
2420 const char* name,
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002421 ExtensionStates* extension_states) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002422 for (v8::RegisteredExtension* it = v8::RegisteredExtension::first_extension();
2423 it != NULL;
2424 it = it->next()) {
2425 if (strcmp(name, it->extension()->name()) == 0) {
2426 return InstallExtension(isolate, it, extension_states);
2427 }
Steve Blocka7e24c12009-10-30 11:49:00 +00002428 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002429 return Utils::ApiCheck(false,
2430 "v8::Context::New()",
2431 "Cannot find required extension");
Steve Blocka7e24c12009-10-30 11:49:00 +00002432}
2433
2434
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002435bool Genesis::InstallExtension(Isolate* isolate,
2436 v8::RegisteredExtension* current,
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002437 ExtensionStates* extension_states) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002438 HandleScope scope(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00002439
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002440 if (extension_states->get_state(current) == INSTALLED) return true;
Steve Blocka7e24c12009-10-30 11:49:00 +00002441 // The current node has already been visited so there must be a
2442 // cycle in the dependency graph; fail.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002443 if (!Utils::ApiCheck(extension_states->get_state(current) != VISITED,
2444 "v8::Context::New()",
2445 "Circular extension dependency")) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002446 return false;
2447 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002448 DCHECK(extension_states->get_state(current) == UNVISITED);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002449 extension_states->set_state(current, VISITED);
Steve Blocka7e24c12009-10-30 11:49:00 +00002450 v8::Extension* extension = current->extension();
2451 // Install the extension's dependencies
2452 for (int i = 0; i < extension->dependency_count(); i++) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002453 if (!InstallExtension(isolate,
2454 extension->dependencies()[i],
2455 extension_states)) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002456 return false;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002457 }
Steve Blocka7e24c12009-10-30 11:49:00 +00002458 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002459 // We do not expect this to throw an exception. Change this if it does.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002460 Handle<String> source_code =
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002461 isolate->factory()
2462 ->NewExternalStringFromOneByte(extension->source())
2463 .ToHandleChecked();
2464 bool result = CompileScriptCached(isolate,
2465 CStrVector(extension->name()),
2466 source_code,
2467 isolate->bootstrapper()->extensions_cache(),
2468 extension,
2469 Handle<Context>(isolate->context()),
2470 false);
2471 DCHECK(isolate->has_pending_exception() != result);
Steve Blocka7e24c12009-10-30 11:49:00 +00002472 if (!result) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002473 // We print out the name of the extension that fail to install.
2474 // When an error is thrown during bootstrapping we automatically print
2475 // the line number at which this happened to the console in the isolate
2476 // error throwing functionality.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002477 base::OS::PrintError("Error installing extension '%s'.\n",
2478 current->extension()->name());
Steve Block44f0eee2011-05-26 01:26:41 +01002479 isolate->clear_pending_exception();
Steve Blocka7e24c12009-10-30 11:49:00 +00002480 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002481 extension_states->set_state(current, INSTALLED);
2482 isolate->NotifyExtensionInstalled();
Steve Blocka7e24c12009-10-30 11:49:00 +00002483 return result;
2484}
2485
2486
Andrei Popescu402d9372010-02-26 13:31:12 +00002487bool Genesis::InstallJSBuiltins(Handle<JSBuiltinsObject> builtins) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002488 HandleScope scope(isolate());
Andrei Popescu402d9372010-02-26 13:31:12 +00002489 for (int i = 0; i < Builtins::NumberOfJavaScriptBuiltins(); i++) {
2490 Builtins::JavaScript id = static_cast<Builtins::JavaScript>(i);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002491 Handle<Object> function_object = Object::GetProperty(
2492 isolate(), builtins, Builtins::GetName(id)).ToHandleChecked();
2493 Handle<JSFunction> function = Handle<JSFunction>::cast(function_object);
Andrei Popescu402d9372010-02-26 13:31:12 +00002494 builtins->set_javascript_builtin(id, *function);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002495 // TODO(mstarzinger): This is just a temporary hack to make TurboFan work,
2496 // the correct solution is to restore the context register after invoking
2497 // builtins from full-codegen.
2498 function->shared()->set_optimization_disabled(true);
2499 if (!Compiler::EnsureCompiled(function, CLEAR_EXCEPTION)) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002500 return false;
2501 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002502 builtins->set_javascript_builtin_code(id, function->shared()->code());
Andrei Popescu402d9372010-02-26 13:31:12 +00002503 }
2504 return true;
Andrei Popescu31002712010-02-23 13:46:05 +00002505}
2506
2507
Steve Blocka7e24c12009-10-30 11:49:00 +00002508bool Genesis::ConfigureGlobalObjects(
2509 v8::Handle<v8::ObjectTemplate> global_proxy_template) {
2510 Handle<JSObject> global_proxy(
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002511 JSObject::cast(native_context()->global_proxy()));
2512 Handle<JSObject> global_object(
2513 JSObject::cast(native_context()->global_object()));
Steve Blocka7e24c12009-10-30 11:49:00 +00002514
2515 if (!global_proxy_template.IsEmpty()) {
2516 // Configure the global proxy object.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002517 Handle<ObjectTemplateInfo> global_proxy_data =
Steve Blocka7e24c12009-10-30 11:49:00 +00002518 v8::Utils::OpenHandle(*global_proxy_template);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002519 if (!ConfigureApiObject(global_proxy, global_proxy_data)) return false;
Steve Blocka7e24c12009-10-30 11:49:00 +00002520
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002521 // Configure the global object.
Steve Blocka7e24c12009-10-30 11:49:00 +00002522 Handle<FunctionTemplateInfo> proxy_constructor(
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002523 FunctionTemplateInfo::cast(global_proxy_data->constructor()));
Steve Blocka7e24c12009-10-30 11:49:00 +00002524 if (!proxy_constructor->prototype_template()->IsUndefined()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002525 Handle<ObjectTemplateInfo> global_object_data(
Steve Blocka7e24c12009-10-30 11:49:00 +00002526 ObjectTemplateInfo::cast(proxy_constructor->prototype_template()));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002527 if (!ConfigureApiObject(global_object, global_object_data)) return false;
Steve Blocka7e24c12009-10-30 11:49:00 +00002528 }
2529 }
2530
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002531 SetObjectPrototype(global_proxy, global_object);
2532
2533 native_context()->set_initial_array_prototype(
2534 JSArray::cast(native_context()->array_function()->prototype()));
2535
Steve Blocka7e24c12009-10-30 11:49:00 +00002536 return true;
2537}
2538
2539
2540bool Genesis::ConfigureApiObject(Handle<JSObject> object,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002541 Handle<ObjectTemplateInfo> object_template) {
2542 DCHECK(!object_template.is_null());
2543 DCHECK(FunctionTemplateInfo::cast(object_template->constructor())
2544 ->IsTemplateFor(object->map()));;
Steve Blocka7e24c12009-10-30 11:49:00 +00002545
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002546 MaybeHandle<JSObject> maybe_obj =
2547 Execution::InstantiateObject(object_template);
2548 Handle<JSObject> obj;
2549 if (!maybe_obj.ToHandle(&obj)) {
2550 DCHECK(isolate()->has_pending_exception());
Ben Murdoch257744e2011-11-30 15:57:28 +00002551 isolate()->clear_pending_exception();
Steve Blocka7e24c12009-10-30 11:49:00 +00002552 return false;
2553 }
2554 TransferObject(obj, object);
2555 return true;
2556}
2557
2558
2559void Genesis::TransferNamedProperties(Handle<JSObject> from,
2560 Handle<JSObject> to) {
2561 if (from->HasFastProperties()) {
2562 Handle<DescriptorArray> descs =
2563 Handle<DescriptorArray>(from->map()->instance_descriptors());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002564 for (int i = 0; i < from->map()->NumberOfOwnDescriptors(); i++) {
2565 PropertyDetails details = descs->GetDetails(i);
Steve Blocka7e24c12009-10-30 11:49:00 +00002566 switch (details.type()) {
2567 case FIELD: {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002568 HandleScope inner(isolate());
2569 Handle<Name> key = Handle<Name>(descs->GetKey(i));
2570 FieldIndex index = FieldIndex::ForDescriptor(from->map(), i);
2571 DCHECK(!descs->GetDetails(i).representation().IsDouble());
2572 Handle<Object> value = Handle<Object>(from->RawFastPropertyAt(index),
2573 isolate());
2574 JSObject::AddProperty(to, key, value, details.attributes());
Steve Blocka7e24c12009-10-30 11:49:00 +00002575 break;
2576 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002577 case CONSTANT: {
2578 HandleScope inner(isolate());
2579 Handle<Name> key = Handle<Name>(descs->GetKey(i));
2580 Handle<Object> constant(descs->GetConstant(i), isolate());
2581 JSObject::AddProperty(to, key, constant, details.attributes());
Steve Blocka7e24c12009-10-30 11:49:00 +00002582 break;
2583 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002584 case ACCESSOR_FIELD:
2585 UNREACHABLE();
Steve Blocka7e24c12009-10-30 11:49:00 +00002586 case CALLBACKS: {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002587 Handle<Name> key(descs->GetKey(i));
2588 LookupIterator it(to, key, LookupIterator::OWN_SKIP_INTERCEPTOR);
2589 CHECK_NE(LookupIterator::ACCESS_CHECK, it.state());
Steve Blocka7e24c12009-10-30 11:49:00 +00002590 // If the property is already there we skip it
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002591 if (it.IsFound()) continue;
2592 HandleScope inner(isolate());
2593 DCHECK(!to->HasFastProperties());
Andrei Popescu31002712010-02-23 13:46:05 +00002594 // Add to dictionary.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002595 Handle<Object> callbacks(descs->GetCallbacksObject(i), isolate());
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002596 PropertyDetails d(details.attributes(), CALLBACKS, i + 1);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002597 JSObject::SetNormalizedProperty(to, key, callbacks, d);
Steve Blocka7e24c12009-10-30 11:49:00 +00002598 break;
2599 }
Steve Blocka7e24c12009-10-30 11:49:00 +00002600 }
2601 }
2602 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002603 Handle<NameDictionary> properties =
2604 Handle<NameDictionary>(from->property_dictionary());
Steve Blocka7e24c12009-10-30 11:49:00 +00002605 int capacity = properties->Capacity();
2606 for (int i = 0; i < capacity; i++) {
2607 Object* raw_key(properties->KeyAt(i));
2608 if (properties->IsKey(raw_key)) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002609 DCHECK(raw_key->IsName());
Steve Blocka7e24c12009-10-30 11:49:00 +00002610 // If the property is already there we skip it.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002611 Handle<Name> key(Name::cast(raw_key));
2612 LookupIterator it(to, key, LookupIterator::OWN_SKIP_INTERCEPTOR);
2613 CHECK_NE(LookupIterator::ACCESS_CHECK, it.state());
2614 if (it.IsFound()) continue;
Steve Blocka7e24c12009-10-30 11:49:00 +00002615 // Set the property.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002616 Handle<Object> value = Handle<Object>(properties->ValueAt(i),
2617 isolate());
2618 DCHECK(!value->IsCell());
2619 if (value->IsPropertyCell()) {
2620 value = Handle<Object>(PropertyCell::cast(*value)->value(),
2621 isolate());
Steve Blocka7e24c12009-10-30 11:49:00 +00002622 }
2623 PropertyDetails details = properties->DetailsAt(i);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002624 DCHECK_EQ(DATA, details.kind());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002625 JSObject::AddProperty(to, key, value, details.attributes());
Steve Blocka7e24c12009-10-30 11:49:00 +00002626 }
2627 }
2628 }
2629}
2630
2631
2632void Genesis::TransferIndexedProperties(Handle<JSObject> from,
2633 Handle<JSObject> to) {
2634 // Cloning the elements array is sufficient.
2635 Handle<FixedArray> from_elements =
2636 Handle<FixedArray>(FixedArray::cast(from->elements()));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002637 Handle<FixedArray> to_elements = factory()->CopyFixedArray(from_elements);
Steve Blocka7e24c12009-10-30 11:49:00 +00002638 to->set_elements(*to_elements);
2639}
2640
2641
2642void Genesis::TransferObject(Handle<JSObject> from, Handle<JSObject> to) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002643 HandleScope outer(isolate());
Steve Blocka7e24c12009-10-30 11:49:00 +00002644
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002645 DCHECK(!from->IsJSArray());
2646 DCHECK(!to->IsJSArray());
Steve Blocka7e24c12009-10-30 11:49:00 +00002647
2648 TransferNamedProperties(from, to);
2649 TransferIndexedProperties(from, to);
2650
2651 // Transfer the prototype (new map is needed).
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002652 Handle<Object> proto(from->map()->prototype(), isolate());
2653 SetObjectPrototype(to, proto);
Steve Blocka7e24c12009-10-30 11:49:00 +00002654}
2655
2656
2657void Genesis::MakeFunctionInstancePrototypeWritable() {
Steve Block44f0eee2011-05-26 01:26:41 +01002658 // The maps with writable prototype are created in CreateEmptyFunction
2659 // and CreateStrictModeFunctionMaps respectively. Initially the maps are
2660 // created with read-only prototype for JS builtins processing.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002661 DCHECK(!sloppy_function_map_writable_prototype_.is_null());
2662 DCHECK(!strict_function_map_writable_prototype_.is_null());
Steve Blocka7e24c12009-10-30 11:49:00 +00002663
Steve Block44f0eee2011-05-26 01:26:41 +01002664 // Replace function instance maps to make prototype writable.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002665 native_context()->set_sloppy_function_map(
2666 *sloppy_function_map_writable_prototype_);
2667 native_context()->set_strict_function_map(
2668 *strict_function_map_writable_prototype_);
Steve Blocka7e24c12009-10-30 11:49:00 +00002669}
2670
2671
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002672class NoTrackDoubleFieldsForSerializerScope {
2673 public:
2674 explicit NoTrackDoubleFieldsForSerializerScope(Isolate* isolate)
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002675 : flag_(FLAG_track_double_fields), enabled_(false) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002676 if (isolate->serializer_enabled()) {
2677 // Disable tracking double fields because heap numbers treated as
2678 // immutable by the serializer.
2679 FLAG_track_double_fields = false;
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002680 enabled_ = true;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002681 }
2682 }
Steve Blocka7e24c12009-10-30 11:49:00 +00002683
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002684 ~NoTrackDoubleFieldsForSerializerScope() {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002685 if (enabled_) {
2686 FLAG_track_double_fields = flag_;
2687 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002688 }
2689
2690 private:
2691 bool flag_;
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002692 bool enabled_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002693};
2694
2695
2696Genesis::Genesis(Isolate* isolate,
2697 MaybeHandle<JSGlobalProxy> maybe_global_proxy,
2698 v8::Handle<v8::ObjectTemplate> global_proxy_template,
2699 v8::ExtensionConfiguration* extensions)
2700 : isolate_(isolate),
2701 active_(isolate->bootstrapper()) {
2702 NoTrackDoubleFieldsForSerializerScope disable_scope(isolate);
2703 result_ = Handle<Context>::null();
Steve Blocka7e24c12009-10-30 11:49:00 +00002704 // Before creating the roots we must save the context and restore it
2705 // on all function exits.
Steve Block44f0eee2011-05-26 01:26:41 +01002706 SaveContext saved_context(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00002707
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002708 // During genesis, the boilerplate for stack overflow won't work until the
2709 // environment has been at least partially initialized. Add a stack check
2710 // before entering JS code to catch overflow early.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002711 StackLimitCheck check(isolate);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002712 if (check.HasOverflowed()) return;
2713
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002714 // We can only de-serialize a context if the isolate was initialized from
2715 // a snapshot. Otherwise we have to build the context from scratch.
2716 if (isolate->initialized_from_snapshot()) {
2717 native_context_ = Snapshot::NewContextFromSnapshot(isolate);
2718 } else {
2719 native_context_ = Handle<Context>();
2720 }
2721
2722 if (!native_context().is_null()) {
2723 AddToWeakNativeContextList(*native_context());
2724 isolate->set_context(*native_context());
Steve Block44f0eee2011-05-26 01:26:41 +01002725 isolate->counters()->contexts_created_by_snapshot()->Increment();
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002726#if TRACE_MAPS
2727 if (FLAG_trace_maps) {
2728 Handle<JSFunction> object_fun = isolate->object_function();
2729 PrintF("[TraceMap: InitialMap map= %p SFI= %d_Object ]\n",
2730 reinterpret_cast<void*>(object_fun->initial_map()),
2731 object_fun->shared()->unique_id());
2732 Map::TraceAllTransitions(object_fun->initial_map());
2733 }
2734#endif
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002735 Handle<GlobalObject> global_object;
2736 Handle<JSGlobalProxy> global_proxy = CreateNewGlobals(
2737 global_proxy_template, maybe_global_proxy, &global_object);
Andrei Popescu402d9372010-02-26 13:31:12 +00002738
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002739 HookUpGlobalProxy(global_object, global_proxy);
2740 HookUpGlobalObject(global_object);
2741 native_context()->builtins()->set_global_proxy(
2742 native_context()->global_proxy());
Andrei Popescu402d9372010-02-26 13:31:12 +00002743
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002744 if (!ConfigureGlobalObjects(global_proxy_template)) return;
Andrei Popescu31002712010-02-23 13:46:05 +00002745 } else {
2746 // We get here if there was no context snapshot.
2747 CreateRoots();
Ben Murdoch257744e2011-11-30 15:57:28 +00002748 Handle<JSFunction> empty_function = CreateEmptyFunction(isolate);
Steve Block44f0eee2011-05-26 01:26:41 +01002749 CreateStrictModeFunctionMaps(empty_function);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002750 Handle<GlobalObject> global_object;
2751 Handle<JSGlobalProxy> global_proxy = CreateNewGlobals(
2752 global_proxy_template, maybe_global_proxy, &global_object);
2753 HookUpGlobalProxy(global_object, global_proxy);
2754 InitializeGlobal(global_object, empty_function);
Steve Block6ded16b2010-05-10 14:33:55 +01002755 InstallJSFunctionResultCaches();
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002756 InitializeNormalizedMapCaches();
Kristian Monsen25f61362010-05-21 11:50:48 +01002757 if (!InstallNatives()) return;
Steve Blocka7e24c12009-10-30 11:49:00 +00002758
Andrei Popescu31002712010-02-23 13:46:05 +00002759 MakeFunctionInstancePrototypeWritable();
Steve Blocka7e24c12009-10-30 11:49:00 +00002760
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002761 if (!ConfigureGlobalObjects(global_proxy_template)) return;
Steve Block44f0eee2011-05-26 01:26:41 +01002762 isolate->counters()->contexts_created_from_scratch()->Increment();
Andrei Popescu31002712010-02-23 13:46:05 +00002763 }
Steve Blocka7e24c12009-10-30 11:49:00 +00002764
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002765 // Install experimental natives.
Ben Murdoch257744e2011-11-30 15:57:28 +00002766 if (!InstallExperimentalNatives()) return;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002767 InitializeExperimentalGlobal();
Ben Murdoch257744e2011-11-30 15:57:28 +00002768
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002769 // We can't (de-)serialize typed arrays currently, but we are lucky: The state
2770 // of the random number generator needs no initialization during snapshot
2771 // creation time and we don't need trigonometric functions then.
2772 if (!isolate->serializer_enabled()) {
2773 // Initially seed the per-context random number generator using the
2774 // per-isolate random number generator.
2775 const int num_elems = 2;
2776 const int num_bytes = num_elems * sizeof(uint32_t);
2777 uint32_t* state = reinterpret_cast<uint32_t*>(malloc(num_bytes));
2778
2779 do {
2780 isolate->random_number_generator()->NextBytes(state, num_bytes);
2781 } while (state[0] == 0 || state[1] == 0);
2782
2783 v8::Local<v8::ArrayBuffer> buffer = v8::ArrayBuffer::New(
2784 reinterpret_cast<v8::Isolate*>(isolate), state, num_bytes);
2785 Utils::OpenHandle(*buffer)->set_should_be_freed(true);
2786 v8::Local<v8::Uint32Array> ta = v8::Uint32Array::New(buffer, 0, num_elems);
2787 Handle<JSBuiltinsObject> builtins(native_context()->builtins());
2788 Runtime::DefineObjectProperty(builtins, factory()->InternalizeOneByteString(
2789 STATIC_CHAR_VECTOR("rngstate")),
2790 Utils::OpenHandle(*ta), NONE).Assert();
2791
2792 // Initialize trigonometric lookup tables and constants.
2793 const int constants_size = arraysize(fdlibm::MathConstants::constants);
2794 const int table_num_bytes = constants_size * kDoubleSize;
2795 v8::Local<v8::ArrayBuffer> trig_buffer = v8::ArrayBuffer::New(
2796 reinterpret_cast<v8::Isolate*>(isolate),
2797 const_cast<double*>(fdlibm::MathConstants::constants), table_num_bytes);
2798 v8::Local<v8::Float64Array> trig_table =
2799 v8::Float64Array::New(trig_buffer, 0, constants_size);
2800
2801 Runtime::DefineObjectProperty(
2802 builtins,
2803 factory()->InternalizeOneByteString(STATIC_CHAR_VECTOR("kMath")),
2804 Utils::OpenHandle(*trig_table), NONE).Assert();
2805 }
2806
2807 result_ = native_context();
Steve Blocka7e24c12009-10-30 11:49:00 +00002808}
2809
2810
2811// Support for thread preemption.
2812
2813// Reserve space for statics needing saving and restoring.
2814int Bootstrapper::ArchiveSpacePerThread() {
Steve Block44f0eee2011-05-26 01:26:41 +01002815 return sizeof(NestingCounterType);
Steve Blocka7e24c12009-10-30 11:49:00 +00002816}
2817
2818
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002819// Archive statics that are thread-local.
Steve Blocka7e24c12009-10-30 11:49:00 +00002820char* Bootstrapper::ArchiveState(char* to) {
Steve Block44f0eee2011-05-26 01:26:41 +01002821 *reinterpret_cast<NestingCounterType*>(to) = nesting_;
2822 nesting_ = 0;
2823 return to + sizeof(NestingCounterType);
Steve Blocka7e24c12009-10-30 11:49:00 +00002824}
2825
2826
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002827// Restore statics that are thread-local.
Steve Blocka7e24c12009-10-30 11:49:00 +00002828char* Bootstrapper::RestoreState(char* from) {
Steve Block44f0eee2011-05-26 01:26:41 +01002829 nesting_ = *reinterpret_cast<NestingCounterType*>(from);
2830 return from + sizeof(NestingCounterType);
Steve Blocka7e24c12009-10-30 11:49:00 +00002831}
2832
2833
2834// Called when the top-level V8 mutex is destroyed.
2835void Bootstrapper::FreeThreadResources() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002836 DCHECK(!IsActive());
Steve Blocka7e24c12009-10-30 11:49:00 +00002837}
2838
2839} } // namespace v8::internal