blob: 70eec2be34871e316ad8df8af6eb896ba1dee370 [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"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008#include "src/api-natives.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009#include "src/code-stubs.h"
10#include "src/extensions/externalize-string-extension.h"
11#include "src/extensions/free-buffer-extension.h"
12#include "src/extensions/gc-extension.h"
Ben Murdochc5610432016-08-08 18:44:38 +010013#include "src/extensions/ignition-statistics-extension.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000014#include "src/extensions/statistics-extension.h"
15#include "src/extensions/trigger-failure-extension.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000016#include "src/heap/heap.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000017#include "src/isolate-inl.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000018#include "src/snapshot/natives.h"
19#include "src/snapshot/snapshot.h"
20#include "src/wasm/wasm-js.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000021
22namespace v8 {
23namespace internal {
24
Ben Murdochb8a8cc12014-11-26 15:28:44 +000025Bootstrapper::Bootstrapper(Isolate* isolate)
26 : isolate_(isolate),
27 nesting_(0),
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000028 extensions_cache_(Script::TYPE_EXTENSION) {}
Steve Block44f0eee2011-05-26 01:26:41 +010029
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000030template <class Source>
31Handle<String> Bootstrapper::SourceLookup(int index) {
32 DCHECK(0 <= index && index < Source::GetBuiltinsCount());
Ben Murdochb8a8cc12014-11-26 15:28:44 +000033 Heap* heap = isolate_->heap();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000034 if (Source::GetSourceCache(heap)->get(index)->IsUndefined()) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +010035 // We can use external strings for the natives.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000036 Vector<const char> source = Source::GetScriptSource(index);
Ben Murdoch3ef787d2012-04-12 10:51:47 +010037 NativesExternalStringResource* resource =
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000038 new NativesExternalStringResource(source.start(), source.length());
Ben Murdochb8a8cc12014-11-26 15:28:44 +000039 // We do not expect this to throw an exception. Change this if it does.
40 Handle<String> source_code = isolate_->factory()
41 ->NewExternalStringFromOneByte(resource)
42 .ToHandleChecked();
Emily Bernierd0a1eb72015-03-24 16:35:39 -040043 // Mark this external string with a special map.
44 source_code->set_map(isolate_->heap()->native_source_string_map());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000045 Source::GetSourceCache(heap)->set(index, *source_code);
Steve Blocka7e24c12009-10-30 11:49:00 +000046 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000047 Handle<Object> cached_source(Source::GetSourceCache(heap)->get(index),
Ben Murdochb8a8cc12014-11-26 15:28:44 +000048 isolate_);
Steve Blocka7e24c12009-10-30 11:49:00 +000049 return Handle<String>::cast(cached_source);
50}
51
52
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000053template Handle<String> Bootstrapper::SourceLookup<Natives>(int index);
54template Handle<String> Bootstrapper::SourceLookup<ExperimentalNatives>(
55 int index);
56template Handle<String> Bootstrapper::SourceLookup<ExperimentalExtraNatives>(
57 int index);
58template Handle<String> Bootstrapper::SourceLookup<ExtraNatives>(int index);
59
60
Steve Blocka7e24c12009-10-30 11:49:00 +000061void Bootstrapper::Initialize(bool create_heap_objects) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000062 extensions_cache_.Initialize(isolate_, create_heap_objects);
63}
64
65
66static const char* GCFunctionName() {
67 bool flag_given = FLAG_expose_gc_as != NULL && strlen(FLAG_expose_gc_as) != 0;
68 return flag_given ? FLAG_expose_gc_as : "gc";
69}
70
71
72v8::Extension* Bootstrapper::free_buffer_extension_ = NULL;
73v8::Extension* Bootstrapper::gc_extension_ = NULL;
74v8::Extension* Bootstrapper::externalize_string_extension_ = NULL;
75v8::Extension* Bootstrapper::statistics_extension_ = NULL;
76v8::Extension* Bootstrapper::trigger_failure_extension_ = NULL;
Ben Murdochc5610432016-08-08 18:44:38 +010077v8::Extension* Bootstrapper::ignition_statistics_extension_ = NULL;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000078
79void Bootstrapper::InitializeOncePerProcess() {
80 free_buffer_extension_ = new FreeBufferExtension;
81 v8::RegisterExtension(free_buffer_extension_);
82 gc_extension_ = new GCExtension(GCFunctionName());
83 v8::RegisterExtension(gc_extension_);
84 externalize_string_extension_ = new ExternalizeStringExtension;
85 v8::RegisterExtension(externalize_string_extension_);
86 statistics_extension_ = new StatisticsExtension;
87 v8::RegisterExtension(statistics_extension_);
88 trigger_failure_extension_ = new TriggerFailureExtension;
89 v8::RegisterExtension(trigger_failure_extension_);
Ben Murdochc5610432016-08-08 18:44:38 +010090 ignition_statistics_extension_ = new IgnitionStatisticsExtension;
91 v8::RegisterExtension(ignition_statistics_extension_);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000092}
93
94
95void Bootstrapper::TearDownExtensions() {
96 delete free_buffer_extension_;
97 free_buffer_extension_ = NULL;
98 delete gc_extension_;
99 gc_extension_ = NULL;
100 delete externalize_string_extension_;
101 externalize_string_extension_ = NULL;
102 delete statistics_extension_;
103 statistics_extension_ = NULL;
104 delete trigger_failure_extension_;
105 trigger_failure_extension_ = NULL;
Ben Murdochc5610432016-08-08 18:44:38 +0100106 delete ignition_statistics_extension_;
107 ignition_statistics_extension_ = NULL;
Steve Blocka7e24c12009-10-30 11:49:00 +0000108}
109
110
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000111void DeleteNativeSources(Object* maybe_array) {
112 if (maybe_array->IsFixedArray()) {
113 FixedArray* array = FixedArray::cast(maybe_array);
114 for (int i = 0; i < array->length(); i++) {
115 Object* natives_source = array->get(i);
116 if (!natives_source->IsUndefined()) {
117 const NativesExternalStringResource* resource =
118 reinterpret_cast<const NativesExternalStringResource*>(
119 ExternalOneByteString::cast(natives_source)->resource());
120 delete resource;
121 }
Leon Clarkee46be812010-01-19 14:06:41 +0000122 }
Leon Clarkee46be812010-01-19 14:06:41 +0000123 }
Leon Clarkee46be812010-01-19 14:06:41 +0000124}
125
126
Steve Blocka7e24c12009-10-30 11:49:00 +0000127void Bootstrapper::TearDown() {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000128 DeleteNativeSources(Natives::GetSourceCache(isolate_->heap()));
129 DeleteNativeSources(ExperimentalNatives::GetSourceCache(isolate_->heap()));
130 DeleteNativeSources(ExtraNatives::GetSourceCache(isolate_->heap()));
131 DeleteNativeSources(
132 ExperimentalExtraNatives::GetSourceCache(isolate_->heap()));
Leon Clarkee46be812010-01-19 14:06:41 +0000133
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000134 extensions_cache_.Initialize(isolate_, false); // Yes, symmetrical
Steve Blocka7e24c12009-10-30 11:49:00 +0000135}
136
137
Steve Blocka7e24c12009-10-30 11:49:00 +0000138class Genesis BASE_EMBEDDED {
139 public:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000140 Genesis(Isolate* isolate, MaybeHandle<JSGlobalProxy> maybe_global_proxy,
141 v8::Local<v8::ObjectTemplate> global_proxy_template,
Ben Murdoch097c5b22016-05-18 11:27:45 +0100142 v8::ExtensionConfiguration* extensions,
143 GlobalContextType context_type);
Andrei Popescu31002712010-02-23 13:46:05 +0000144 ~Genesis() { }
Steve Blocka7e24c12009-10-30 11:49:00 +0000145
Ben Murdoch257744e2011-11-30 15:57:28 +0000146 Isolate* isolate() const { return isolate_; }
147 Factory* factory() const { return isolate_->factory(); }
148 Heap* heap() const { return isolate_->heap(); }
149
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000150 Handle<Context> result() { return result_; }
151
Steve Blocka7e24c12009-10-30 11:49:00 +0000152 private:
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000153 Handle<Context> native_context() { return native_context_; }
Steve Blocka7e24c12009-10-30 11:49:00 +0000154
Andrei Popescu31002712010-02-23 13:46:05 +0000155 // Creates some basic objects. Used for creating a context from scratch.
156 void CreateRoots();
157 // Creates the empty function. Used for creating a context from scratch.
Ben Murdoch257744e2011-11-30 15:57:28 +0000158 Handle<JSFunction> CreateEmptyFunction(Isolate* isolate);
Steve Block44f0eee2011-05-26 01:26:41 +0100159 // Creates the ThrowTypeError function. ECMA 5th Ed. 13.2.3
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000160 Handle<JSFunction> GetRestrictedFunctionPropertiesThrower();
161 Handle<JSFunction> GetStrictArgumentsPoisonFunction();
162 Handle<JSFunction> GetThrowTypeErrorIntrinsic(Builtins::Name builtin_name);
Steve Block44f0eee2011-05-26 01:26:41 +0100163
164 void CreateStrictModeFunctionMaps(Handle<JSFunction> empty);
Ben Murdochc5610432016-08-08 18:44:38 +0100165 void CreateIteratorMaps(Handle<JSFunction> empty);
166 void CreateAsyncFunctionMaps(Handle<JSFunction> empty);
Ben Murdochda12d292016-06-02 14:46:10 +0100167 void CreateJSProxyMaps();
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100168
169 // Make the "arguments" and "caller" properties throw a TypeError on access.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000170 void AddRestrictedFunctionProperties(Handle<Map> map);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100171
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000172 // Creates the global objects using the global proxy and the template passed
173 // in through the API. We call this regardless of whether we are building a
Andrei Popescu31002712010-02-23 13:46:05 +0000174 // context from scratch or using a deserialized one from the partial snapshot
175 // but in the latter case we don't use the objects it produces directly, as
176 // we have to used the deserialized ones that are linked together with the
177 // rest of the context snapshot.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000178 Handle<JSGlobalObject> CreateNewGlobals(
179 v8::Local<v8::ObjectTemplate> global_proxy_template,
180 Handle<JSGlobalProxy> global_proxy);
Andrei Popescu31002712010-02-23 13:46:05 +0000181 // Hooks the given global proxy into the context. If the context was created
182 // by deserialization then this will unhook the global proxy that was
183 // deserialized, leaving the GC to pick it up.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000184 void HookUpGlobalProxy(Handle<JSGlobalObject> global_object,
Andrei Popescu31002712010-02-23 13:46:05 +0000185 Handle<JSGlobalProxy> global_proxy);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000186 // Similarly, we want to use the global that has been created by the templates
187 // passed through the API. The global from the snapshot is detached from the
188 // other objects in the snapshot.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000189 void HookUpGlobalObject(Handle<JSGlobalObject> global_object);
190 // The native context has a ScriptContextTable that store declarative bindings
191 // made in script scopes. Add a "this" binding to that table pointing to the
192 // global proxy.
193 void InstallGlobalThisBinding();
Andrei Popescu31002712010-02-23 13:46:05 +0000194 // New context initialization. Used for creating a context from scratch.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000195 void InitializeGlobal(Handle<JSGlobalObject> global_object,
196 Handle<JSFunction> empty_function,
Ben Murdoch097c5b22016-05-18 11:27:45 +0100197 GlobalContextType context_type);
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000198 void InitializeExperimentalGlobal();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000199 // Depending on the situation, expose and/or get rid of the utils object.
Ben Murdoch097c5b22016-05-18 11:27:45 +0100200 void ConfigureUtilsObject(GlobalContextType context_type);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400201
202#define DECLARE_FEATURE_INITIALIZATION(id, descr) \
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400203 void InitializeGlobal_##id();
204
205 HARMONY_INPROGRESS(DECLARE_FEATURE_INITIALIZATION)
206 HARMONY_STAGED(DECLARE_FEATURE_INITIALIZATION)
207 HARMONY_SHIPPING(DECLARE_FEATURE_INITIALIZATION)
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000208 DECLARE_FEATURE_INITIALIZATION(promise_extra, "")
Ben Murdochc5610432016-08-08 18:44:38 +0100209 DECLARE_FEATURE_INITIALIZATION(intl_extra, "")
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400210#undef DECLARE_FEATURE_INITIALIZATION
211
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000212 Handle<JSFunction> InstallArrayBuffer(Handle<JSObject> target,
213 const char* name);
214 Handle<JSFunction> InstallInternalArray(Handle<JSObject> target,
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000215 const char* name,
216 ElementsKind elements_kind);
Ben Murdoch097c5b22016-05-18 11:27:45 +0100217 bool InstallNatives(GlobalContextType context_type);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000218
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000219 void InstallTypedArray(const char* name, ElementsKind elements_kind,
220 Handle<JSFunction>* fun);
Ben Murdoch257744e2011-11-30 15:57:28 +0000221 bool InstallExperimentalNatives();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000222 bool InstallExtraNatives();
223 bool InstallExperimentalExtraNatives();
224 bool InstallDebuggerNatives();
Ben Murdochb0fe1622011-05-05 13:52:32 +0100225 void InstallBuiltinFunctionIds();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000226 void InstallExperimentalBuiltinFunctionIds();
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100227 void InitializeNormalizedMapCaches();
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100228
229 enum ExtensionTraversalState {
230 UNVISITED, VISITED, INSTALLED
231 };
232
233 class ExtensionStates {
234 public:
235 ExtensionStates();
236 ExtensionTraversalState get_state(RegisteredExtension* extension);
237 void set_state(RegisteredExtension* extension,
238 ExtensionTraversalState state);
239 private:
240 HashMap map_;
241 DISALLOW_COPY_AND_ASSIGN(ExtensionStates);
242 };
243
Andrei Popescu31002712010-02-23 13:46:05 +0000244 // Used both for deserialized and from-scratch contexts to add the extensions
245 // provided.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000246 static bool InstallExtensions(Handle<Context> native_context,
Andrei Popescu31002712010-02-23 13:46:05 +0000247 v8::ExtensionConfiguration* extensions);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000248 static bool InstallAutoExtensions(Isolate* isolate,
249 ExtensionStates* extension_states);
250 static bool InstallRequestedExtensions(Isolate* isolate,
251 v8::ExtensionConfiguration* extensions,
252 ExtensionStates* extension_states);
253 static bool InstallExtension(Isolate* isolate,
254 const char* name,
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100255 ExtensionStates* extension_states);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000256 static bool InstallExtension(Isolate* isolate,
257 v8::RegisteredExtension* current,
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100258 ExtensionStates* extension_states);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000259 static bool InstallSpecialObjects(Handle<Context> native_context);
Steve Blocka7e24c12009-10-30 11:49:00 +0000260 bool ConfigureApiObject(Handle<JSObject> object,
261 Handle<ObjectTemplateInfo> object_template);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000262 bool ConfigureGlobalObjects(
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000263 v8::Local<v8::ObjectTemplate> global_proxy_template);
Steve Blocka7e24c12009-10-30 11:49:00 +0000264
265 // Migrates all properties from the 'from' object to the 'to'
266 // object and overrides the prototype in 'to' with the one from
267 // 'from'.
268 void TransferObject(Handle<JSObject> from, Handle<JSObject> to);
269 void TransferNamedProperties(Handle<JSObject> from, Handle<JSObject> to);
270 void TransferIndexedProperties(Handle<JSObject> from, Handle<JSObject> to);
271
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000272 enum FunctionMode {
273 // With prototype.
274 FUNCTION_WITH_WRITEABLE_PROTOTYPE,
275 FUNCTION_WITH_READONLY_PROTOTYPE,
276 // Without prototype.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000277 FUNCTION_WITHOUT_PROTOTYPE
Steve Block6ded16b2010-05-10 14:33:55 +0100278 };
Steve Block44f0eee2011-05-26 01:26:41 +0100279
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000280 static bool IsFunctionModeWithPrototype(FunctionMode function_mode) {
281 return (function_mode == FUNCTION_WITH_WRITEABLE_PROTOTYPE ||
282 function_mode == FUNCTION_WITH_READONLY_PROTOTYPE);
283 }
Steve Block44f0eee2011-05-26 01:26:41 +0100284
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000285 Handle<Map> CreateSloppyFunctionMap(FunctionMode function_mode);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000286
287 void SetFunctionInstanceDescriptor(Handle<Map> map,
288 FunctionMode function_mode);
Steve Blocka7e24c12009-10-30 11:49:00 +0000289 void MakeFunctionInstancePrototypeWritable();
290
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000291 Handle<Map> CreateStrictFunctionMap(FunctionMode function_mode,
292 Handle<JSFunction> empty_function);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000293
Steve Block44f0eee2011-05-26 01:26:41 +0100294
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000295 void SetStrictFunctionInstanceDescriptor(Handle<Map> map,
296 FunctionMode function_mode);
Steve Block44f0eee2011-05-26 01:26:41 +0100297
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000298 static bool CallUtilsFunction(Isolate* isolate, const char* name);
299
300 static bool CompileExtension(Isolate* isolate, v8::Extension* extension);
Steve Blocka7e24c12009-10-30 11:49:00 +0000301
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000302 Isolate* isolate_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000303 Handle<Context> result_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000304 Handle<Context> native_context_;
Steve Block44f0eee2011-05-26 01:26:41 +0100305
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000306 // Function maps. Function maps are created initially with a read only
307 // prototype for the processing of JS builtins. Later the function maps are
308 // replaced in order to make prototype writable. These are the final, writable
309 // prototype, maps.
310 Handle<Map> sloppy_function_map_writable_prototype_;
311 Handle<Map> strict_function_map_writable_prototype_;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000312 Handle<JSFunction> strict_poison_function_;
313 Handle<JSFunction> restricted_function_properties_thrower_;
Steve Block44f0eee2011-05-26 01:26:41 +0100314
Andrei Popescu31002712010-02-23 13:46:05 +0000315 BootstrapperActive active_;
316 friend class Bootstrapper;
Steve Blocka7e24c12009-10-30 11:49:00 +0000317};
318
Steve Blocka7e24c12009-10-30 11:49:00 +0000319
320void Bootstrapper::Iterate(ObjectVisitor* v) {
Steve Block44f0eee2011-05-26 01:26:41 +0100321 extensions_cache_.Iterate(v);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100322 v->Synchronize(VisitorSynchronization::kExtensions);
Steve Blocka7e24c12009-10-30 11:49:00 +0000323}
324
Steve Blocka7e24c12009-10-30 11:49:00 +0000325Handle<Context> Bootstrapper::CreateEnvironment(
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000326 MaybeHandle<JSGlobalProxy> maybe_global_proxy,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000327 v8::Local<v8::ObjectTemplate> global_proxy_template,
Ben Murdoch097c5b22016-05-18 11:27:45 +0100328 v8::ExtensionConfiguration* extensions, GlobalContextType context_type) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000329 HandleScope scope(isolate_);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000330 Genesis genesis(isolate_, maybe_global_proxy, global_proxy_template,
331 extensions, context_type);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000332 Handle<Context> env = genesis.result();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000333 if (env.is_null() ||
334 (context_type != THIN_CONTEXT && !InstallExtensions(env, extensions))) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000335 return Handle<Context>();
Andrei Popescu31002712010-02-23 13:46:05 +0000336 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000337 return scope.CloseAndEscape(env);
Steve Blocka7e24c12009-10-30 11:49:00 +0000338}
339
340
341static void SetObjectPrototype(Handle<JSObject> object, Handle<Object> proto) {
342 // object.__proto__ = proto;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000343 Handle<Map> old_map = Handle<Map>(object->map());
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400344 Handle<Map> new_map = Map::Copy(old_map, "SetObjectPrototype");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000345 Map::SetPrototype(new_map, proto, FAST_PROTOTYPE);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000346 JSObject::MigrateToMap(object, new_map);
Steve Blocka7e24c12009-10-30 11:49:00 +0000347}
348
349
350void Bootstrapper::DetachGlobal(Handle<Context> env) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000351 env->GetIsolate()->counters()->errors_thrown_per_context()->AddSample(
352 env->GetErrorsThrown());
353
Ben Murdoch257744e2011-11-30 15:57:28 +0000354 Factory* factory = env->GetIsolate()->factory();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000355 Handle<JSGlobalProxy> global_proxy(JSGlobalProxy::cast(env->global_proxy()));
356 global_proxy->set_native_context(*factory->null_value());
357 SetObjectPrototype(global_proxy, factory->null_value());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000358 global_proxy->map()->SetConstructor(*factory->null_value());
359 if (FLAG_track_detached_contexts) {
360 env->GetIsolate()->AddDetachedContext(env);
361 }
Andrei Popescu74b3c142010-03-29 12:03:09 +0100362}
363
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000364namespace {
365
366void InstallFunction(Handle<JSObject> target, Handle<Name> property_name,
367 Handle<JSFunction> function, Handle<String> function_name,
368 PropertyAttributes attributes = DONT_ENUM) {
369 JSObject::AddProperty(target, property_name, function, attributes);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000370 if (target->IsJSGlobalObject()) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000371 function->shared()->set_instance_class_name(*function_name);
Steve Blocka7e24c12009-10-30 11:49:00 +0000372 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100373 function->shared()->set_native(true);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000374}
375
Ben Murdochc5610432016-08-08 18:44:38 +0100376void InstallFunction(Handle<JSObject> target, Handle<JSFunction> function,
377 Handle<Name> name,
378 PropertyAttributes attributes = DONT_ENUM) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000379 Handle<String> name_string = Name::ToFunctionName(name).ToHandleChecked();
380 InstallFunction(target, name, function, name_string, attributes);
381}
382
Ben Murdochc5610432016-08-08 18:44:38 +0100383Handle<JSFunction> CreateFunction(Isolate* isolate, Handle<String> name,
384 InstanceType type, int instance_size,
385 MaybeHandle<JSObject> maybe_prototype,
386 Builtins::Name call,
387 bool strict_function_map = false) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000388 Factory* factory = isolate->factory();
389 Handle<Code> call_code(isolate->builtins()->builtin(call));
390 Handle<JSObject> prototype;
391 static const bool kReadOnlyPrototype = false;
392 static const bool kInstallConstructor = false;
393 return maybe_prototype.ToHandle(&prototype)
394 ? factory->NewFunction(name, call_code, prototype, type,
395 instance_size, kReadOnlyPrototype,
396 kInstallConstructor, strict_function_map)
397 : factory->NewFunctionWithoutPrototype(name, call_code,
398 strict_function_map);
399}
400
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000401Handle<JSFunction> InstallFunction(Handle<JSObject> target, Handle<Name> name,
402 InstanceType type, int instance_size,
403 MaybeHandle<JSObject> maybe_prototype,
404 Builtins::Name call,
405 PropertyAttributes attributes,
406 bool strict_function_map = false) {
407 Handle<String> name_string = Name::ToFunctionName(name).ToHandleChecked();
408 Handle<JSFunction> function =
409 CreateFunction(target->GetIsolate(), name_string, type, instance_size,
410 maybe_prototype, call, strict_function_map);
411 InstallFunction(target, name, function, name_string, attributes);
Steve Blocka7e24c12009-10-30 11:49:00 +0000412 return function;
413}
414
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000415Handle<JSFunction> InstallFunction(Handle<JSObject> target, const char* name,
416 InstanceType type, int instance_size,
417 MaybeHandle<JSObject> maybe_prototype,
418 Builtins::Name call,
419 bool strict_function_map = false) {
420 Factory* const factory = target->GetIsolate()->factory();
421 PropertyAttributes attributes = DONT_ENUM;
422 return InstallFunction(target, factory->InternalizeUtf8String(name), type,
423 instance_size, maybe_prototype, call, attributes,
424 strict_function_map);
425}
426
Ben Murdochc5610432016-08-08 18:44:38 +0100427Handle<JSFunction> SimpleCreateFunction(Isolate* isolate, Handle<String> name,
428 Builtins::Name call, int len,
429 bool adapt) {
430 Handle<JSFunction> fun =
431 CreateFunction(isolate, name, JS_OBJECT_TYPE, JSObject::kHeaderSize,
432 MaybeHandle<JSObject>(), call);
433 if (adapt) {
434 fun->shared()->set_internal_formal_parameter_count(len);
435 } else {
436 fun->shared()->DontAdaptArguments();
437 }
438 fun->shared()->set_length(len);
439 return fun;
440}
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000441
Ben Murdochc5610432016-08-08 18:44:38 +0100442Handle<JSFunction> SimpleInstallFunction(Handle<JSObject> base,
443 Handle<String> name,
444 Builtins::Name call, int len,
445 bool adapt) {
446 Handle<JSFunction> fun =
447 SimpleCreateFunction(base->GetIsolate(), name, call, len, adapt);
448 InstallFunction(base, fun, name, DONT_ENUM);
449 return fun;
450}
451
452Handle<JSFunction> SimpleInstallFunction(Handle<JSObject> base,
453 const char* name, Builtins::Name call,
454 int len, bool adapt) {
455 Factory* const factory = base->GetIsolate()->factory();
456 return SimpleInstallFunction(base, factory->InternalizeUtf8String(name), call,
457 len, adapt);
458}
459
460} // namespace
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000461
462void Genesis::SetFunctionInstanceDescriptor(Handle<Map> map,
463 FunctionMode function_mode) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000464 int size = IsFunctionModeWithPrototype(function_mode) ? 5 : 4;
465 Map::EnsureDescriptorSlack(map, size);
466
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000467 PropertyAttributes ro_attribs =
468 static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE | READ_ONLY);
469 PropertyAttributes roc_attribs =
470 static_cast<PropertyAttributes>(DONT_ENUM | READ_ONLY);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100471
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000472 Handle<AccessorInfo> length =
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000473 Accessors::FunctionLengthInfo(isolate(), roc_attribs);
Steve Block44f0eee2011-05-26 01:26:41 +0100474 { // Add length.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000475 AccessorConstantDescriptor d(Handle<Name>(Name::cast(length->name())),
476 length, roc_attribs);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000477 map->AppendDescriptor(&d);
Steve Block44f0eee2011-05-26 01:26:41 +0100478 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000479 Handle<AccessorInfo> name =
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000480 Accessors::FunctionNameInfo(isolate(), ro_attribs);
Steve Block44f0eee2011-05-26 01:26:41 +0100481 { // Add name.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000482 AccessorConstantDescriptor d(Handle<Name>(Name::cast(name->name())), name,
483 roc_attribs);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000484 map->AppendDescriptor(&d);
Steve Block44f0eee2011-05-26 01:26:41 +0100485 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000486 Handle<AccessorInfo> args =
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000487 Accessors::FunctionArgumentsInfo(isolate(), ro_attribs);
Steve Block44f0eee2011-05-26 01:26:41 +0100488 { // Add arguments.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000489 AccessorConstantDescriptor d(Handle<Name>(Name::cast(args->name())), args,
490 ro_attribs);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000491 map->AppendDescriptor(&d);
Steve Block44f0eee2011-05-26 01:26:41 +0100492 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000493 Handle<AccessorInfo> caller =
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000494 Accessors::FunctionCallerInfo(isolate(), ro_attribs);
Steve Block44f0eee2011-05-26 01:26:41 +0100495 { // Add caller.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000496 AccessorConstantDescriptor d(Handle<Name>(Name::cast(caller->name())),
497 caller, ro_attribs);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000498 map->AppendDescriptor(&d);
Steve Block44f0eee2011-05-26 01:26:41 +0100499 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000500 if (IsFunctionModeWithPrototype(function_mode)) {
501 if (function_mode == FUNCTION_WITH_WRITEABLE_PROTOTYPE) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000502 ro_attribs = static_cast<PropertyAttributes>(ro_attribs & ~READ_ONLY);
Steve Block44f0eee2011-05-26 01:26:41 +0100503 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000504 Handle<AccessorInfo> prototype =
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000505 Accessors::FunctionPrototypeInfo(isolate(), ro_attribs);
506 AccessorConstantDescriptor d(Handle<Name>(Name::cast(prototype->name())),
507 prototype, ro_attribs);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000508 map->AppendDescriptor(&d);
Steve Block44f0eee2011-05-26 01:26:41 +0100509 }
Steve Block44f0eee2011-05-26 01:26:41 +0100510}
Steve Blocka7e24c12009-10-30 11:49:00 +0000511
Steve Blocka7e24c12009-10-30 11:49:00 +0000512
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000513Handle<Map> Genesis::CreateSloppyFunctionMap(FunctionMode function_mode) {
Ben Murdoch257744e2011-11-30 15:57:28 +0000514 Handle<Map> map = factory()->NewMap(JS_FUNCTION_TYPE, JSFunction::kSize);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000515 SetFunctionInstanceDescriptor(map, function_mode);
Ben Murdoch097c5b22016-05-18 11:27:45 +0100516 map->set_is_constructor(IsFunctionModeWithPrototype(function_mode));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000517 map->set_is_callable();
Steve Block44f0eee2011-05-26 01:26:41 +0100518 return map;
Steve Blocka7e24c12009-10-30 11:49:00 +0000519}
520
521
Ben Murdoch257744e2011-11-30 15:57:28 +0000522Handle<JSFunction> Genesis::CreateEmptyFunction(Isolate* isolate) {
Steve Block44f0eee2011-05-26 01:26:41 +0100523 // Allocate the map for function instances. Maps are allocated first and their
524 // prototypes patched later, once empty function is created.
525
Steve Block6ded16b2010-05-10 14:33:55 +0100526 // Functions with this map will not have a 'prototype' property, and
527 // can not be used as constructors.
Ben Murdoch8b112d22011-06-08 16:22:53 +0100528 Handle<Map> function_without_prototype_map =
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000529 CreateSloppyFunctionMap(FUNCTION_WITHOUT_PROTOTYPE);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000530 native_context()->set_sloppy_function_without_prototype_map(
Ben Murdoch8b112d22011-06-08 16:22:53 +0100531 *function_without_prototype_map);
Steve Blocka7e24c12009-10-30 11:49:00 +0000532
Steve Block44f0eee2011-05-26 01:26:41 +0100533 // Allocate the function map. This map is temporary, used only for processing
534 // of builtins.
535 // Later the map is replaced with writable prototype map, allocated below.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000536 Handle<Map> function_map =
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000537 CreateSloppyFunctionMap(FUNCTION_WITH_READONLY_PROTOTYPE);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000538 native_context()->set_sloppy_function_map(*function_map);
539 native_context()->set_sloppy_function_with_readonly_prototype_map(
540 *function_map);
Steve Blocka7e24c12009-10-30 11:49:00 +0000541
Steve Block44f0eee2011-05-26 01:26:41 +0100542 // The final map for functions. Writeable prototype.
543 // This map is installed in MakeFunctionInstancePrototypeWritable.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000544 sloppy_function_map_writable_prototype_ =
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000545 CreateSloppyFunctionMap(FUNCTION_WITH_WRITEABLE_PROTOTYPE);
Steve Block44f0eee2011-05-26 01:26:41 +0100546 Factory* factory = isolate->factory();
Steve Block44f0eee2011-05-26 01:26:41 +0100547
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000548 Handle<String> object_name = factory->Object_string();
Steve Blocka7e24c12009-10-30 11:49:00 +0000549
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400550 Handle<JSObject> object_function_prototype;
551
Steve Blocka7e24c12009-10-30 11:49:00 +0000552 { // --- O b j e c t ---
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000553 Handle<JSFunction> object_fun = factory->NewFunction(object_name);
554 int unused = JSObject::kInitialGlobalObjectUnusedPropertiesCount;
555 int instance_size = JSObject::kHeaderSize + kPointerSize * unused;
Steve Blocka7e24c12009-10-30 11:49:00 +0000556 Handle<Map> object_function_map =
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000557 factory->NewMap(JS_OBJECT_TYPE, instance_size);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000558 object_function_map->SetInObjectProperties(unused);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000559 JSFunction::SetInitialMap(object_fun, object_function_map,
560 isolate->factory()->null_value());
561 object_function_map->set_unused_property_fields(unused);
Steve Blocka7e24c12009-10-30 11:49:00 +0000562
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000563 native_context()->set_object_function(*object_fun);
Steve Blocka7e24c12009-10-30 11:49:00 +0000564
565 // Allocate a new prototype for the object function.
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400566 object_function_prototype =
567 factory->NewJSObject(isolate->object_function(), TENURED);
568 Handle<Map> map = Map::Copy(handle(object_function_prototype->map()),
569 "EmptyObjectPrototype");
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000570 map->set_is_prototype_map(true);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400571 object_function_prototype->set_map(*map);
Steve Blocka7e24c12009-10-30 11:49:00 +0000572
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400573 native_context()->set_initial_object_prototype(*object_function_prototype);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000574 // For bootstrapping set the array prototype to be the same as the object
575 // prototype, otherwise the missing initial_array_prototype will cause
576 // assertions during startup.
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400577 native_context()->set_initial_array_prototype(*object_function_prototype);
578 Accessors::FunctionSetPrototype(object_fun, object_function_prototype)
579 .Assert();
Steve Blocka7e24c12009-10-30 11:49:00 +0000580 }
581
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000582 // Allocate the empty function as the prototype for function - ES6 19.2.3
583 Handle<Code> code(isolate->builtins()->EmptyFunction());
584 Handle<JSFunction> empty_function =
585 factory->NewFunctionWithoutPrototype(factory->empty_string(), code);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000586
587 // Allocate the function map first and then patch the prototype later
588 Handle<Map> empty_function_map =
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000589 CreateSloppyFunctionMap(FUNCTION_WITHOUT_PROTOTYPE);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000590 DCHECK(!empty_function_map->is_dictionary_map());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000591 Map::SetPrototype(empty_function_map, object_function_prototype);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000592 empty_function_map->set_is_prototype_map(true);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000593
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000594 empty_function->set_map(*empty_function_map);
Steve Blocka7e24c12009-10-30 11:49:00 +0000595
Andrei Popescu31002712010-02-23 13:46:05 +0000596 // --- E m p t y ---
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000597 Handle<String> source = factory->NewStringFromStaticChars("() {}");
Steve Block44f0eee2011-05-26 01:26:41 +0100598 Handle<Script> script = factory->NewScript(source);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000599 script->set_type(Script::TYPE_NATIVE);
Andrei Popescu31002712010-02-23 13:46:05 +0000600 empty_function->shared()->set_start_position(0);
601 empty_function->shared()->set_end_position(source->length());
602 empty_function->shared()->DontAdaptArguments();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000603 SharedFunctionInfo::SetScript(handle(empty_function->shared()), script);
Steve Block44f0eee2011-05-26 01:26:41 +0100604
605 // Set prototypes for the function maps.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000606 Handle<Map> sloppy_function_map(native_context()->sloppy_function_map(),
607 isolate);
608 Handle<Map> sloppy_function_without_prototype_map(
609 native_context()->sloppy_function_without_prototype_map(), isolate);
610 Map::SetPrototype(sloppy_function_map, empty_function);
611 Map::SetPrototype(sloppy_function_without_prototype_map, empty_function);
612 Map::SetPrototype(sloppy_function_map_writable_prototype_, empty_function);
613
614 // ES6 draft 03-17-2015, section 8.2.2 step 12
615 AddRestrictedFunctionProperties(empty_function_map);
616
Andrei Popescu31002712010-02-23 13:46:05 +0000617 return empty_function;
618}
619
620
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000621void Genesis::SetStrictFunctionInstanceDescriptor(Handle<Map> map,
622 FunctionMode function_mode) {
623 int size = IsFunctionModeWithPrototype(function_mode) ? 3 : 2;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000624 Map::EnsureDescriptorSlack(map, size);
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000625
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000626 PropertyAttributes rw_attribs =
627 static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE);
628 PropertyAttributes ro_attribs =
629 static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE | READ_ONLY);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000630 PropertyAttributes roc_attribs =
631 static_cast<PropertyAttributes>(DONT_ENUM | READ_ONLY);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100632
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000633 DCHECK(function_mode == FUNCTION_WITH_WRITEABLE_PROTOTYPE ||
634 function_mode == FUNCTION_WITH_READONLY_PROTOTYPE ||
635 function_mode == FUNCTION_WITHOUT_PROTOTYPE);
636 { // Add length.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000637 Handle<AccessorInfo> length =
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000638 Accessors::FunctionLengthInfo(isolate(), roc_attribs);
639 AccessorConstantDescriptor d(Handle<Name>(Name::cast(length->name())),
640 length, roc_attribs);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000641 map->AppendDescriptor(&d);
Steve Block44f0eee2011-05-26 01:26:41 +0100642 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100643 { // Add name.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000644 Handle<AccessorInfo> name =
645 Accessors::FunctionNameInfo(isolate(), roc_attribs);
646 AccessorConstantDescriptor d(Handle<Name>(Name::cast(name->name())), name,
647 roc_attribs);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000648 map->AppendDescriptor(&d);
Steve Block44f0eee2011-05-26 01:26:41 +0100649 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000650 if (IsFunctionModeWithPrototype(function_mode)) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100651 // Add prototype.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000652 PropertyAttributes attribs =
653 function_mode == FUNCTION_WITH_WRITEABLE_PROTOTYPE ? rw_attribs
654 : ro_attribs;
655 Handle<AccessorInfo> prototype =
656 Accessors::FunctionPrototypeInfo(isolate(), attribs);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000657 AccessorConstantDescriptor d(Handle<Name>(Name::cast(prototype->name())),
658 prototype, attribs);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000659 map->AppendDescriptor(&d);
Steve Block44f0eee2011-05-26 01:26:41 +0100660 }
Steve Block44f0eee2011-05-26 01:26:41 +0100661}
662
663
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000664// Creates the %ThrowTypeError% function.
665Handle<JSFunction> Genesis::GetThrowTypeErrorIntrinsic(
666 Builtins::Name builtin_name) {
667 Handle<String> name =
668 factory()->InternalizeOneByteString(STATIC_CHAR_VECTOR("ThrowTypeError"));
669 Handle<Code> code(isolate()->builtins()->builtin(builtin_name));
670 Handle<JSFunction> function =
671 factory()->NewFunctionWithoutPrototype(name, code);
672 function->shared()->DontAdaptArguments();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000673
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000674 // %ThrowTypeError% must not have a name property.
675 if (JSReceiver::DeleteProperty(function, factory()->name_string())
676 .IsNothing()) {
677 DCHECK(false);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000678 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000679
680 // length needs to be non configurable.
681 Handle<Object> value(Smi::FromInt(function->shared()->length()), isolate());
682 JSObject::SetOwnPropertyIgnoreAttributes(
683 function, factory()->length_string(), value,
684 static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE | READ_ONLY))
685 .Assert();
686
687 if (JSObject::PreventExtensions(function, Object::THROW_ON_ERROR)
688 .IsNothing()) {
689 DCHECK(false);
690 }
691
692 return function;
693}
694
695
696// ECMAScript 5th Edition, 13.2.3
697Handle<JSFunction> Genesis::GetRestrictedFunctionPropertiesThrower() {
698 if (restricted_function_properties_thrower_.is_null()) {
699 restricted_function_properties_thrower_ = GetThrowTypeErrorIntrinsic(
700 Builtins::kRestrictedFunctionPropertiesThrower);
701 }
702 return restricted_function_properties_thrower_;
703}
704
705
706Handle<JSFunction> Genesis::GetStrictArgumentsPoisonFunction() {
707 if (strict_poison_function_.is_null()) {
708 strict_poison_function_ = GetThrowTypeErrorIntrinsic(
709 Builtins::kRestrictedStrictArgumentsPropertiesThrower);
710 }
711 return strict_poison_function_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000712}
713
714
715Handle<Map> Genesis::CreateStrictFunctionMap(
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000716 FunctionMode function_mode, Handle<JSFunction> empty_function) {
Ben Murdoch257744e2011-11-30 15:57:28 +0000717 Handle<Map> map = factory()->NewMap(JS_FUNCTION_TYPE, JSFunction::kSize);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000718 SetStrictFunctionInstanceDescriptor(map, function_mode);
Ben Murdoch097c5b22016-05-18 11:27:45 +0100719 map->set_is_constructor(IsFunctionModeWithPrototype(function_mode));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000720 map->set_is_callable();
721 Map::SetPrototype(map, empty_function);
722 return map;
723}
724
725
Steve Block44f0eee2011-05-26 01:26:41 +0100726void Genesis::CreateStrictModeFunctionMaps(Handle<JSFunction> empty) {
Steve Block44f0eee2011-05-26 01:26:41 +0100727 // Allocate map for the prototype-less strict mode instances.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000728 Handle<Map> strict_function_without_prototype_map =
729 CreateStrictFunctionMap(FUNCTION_WITHOUT_PROTOTYPE, empty);
730 native_context()->set_strict_function_without_prototype_map(
731 *strict_function_without_prototype_map);
Steve Block44f0eee2011-05-26 01:26:41 +0100732
733 // Allocate map for the strict mode functions. This map is temporary, used
734 // only for processing of builtins.
735 // Later the map is replaced with writable prototype map, allocated below.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000736 Handle<Map> strict_function_map =
737 CreateStrictFunctionMap(FUNCTION_WITH_READONLY_PROTOTYPE, empty);
738 native_context()->set_strict_function_map(*strict_function_map);
Steve Block44f0eee2011-05-26 01:26:41 +0100739
740 // The final map for the strict mode functions. Writeable prototype.
741 // This map is installed in MakeFunctionInstancePrototypeWritable.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000742 strict_function_map_writable_prototype_ =
743 CreateStrictFunctionMap(FUNCTION_WITH_WRITEABLE_PROTOTYPE, empty);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100744}
Ben Murdoch5d4cdbf2012-04-11 10:23:59 +0100745
Ben Murdochc5610432016-08-08 18:44:38 +0100746void Genesis::CreateIteratorMaps(Handle<JSFunction> empty) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000747 // Create iterator-related meta-objects.
748 Handle<JSObject> iterator_prototype =
749 factory()->NewJSObject(isolate()->object_function(), TENURED);
750 Handle<JSObject> generator_object_prototype =
751 factory()->NewJSObject(isolate()->object_function(), TENURED);
Ben Murdochc5610432016-08-08 18:44:38 +0100752 native_context()->set_initial_generator_prototype(
753 *generator_object_prototype);
754 SetObjectPrototype(generator_object_prototype, iterator_prototype);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000755 Handle<JSObject> generator_function_prototype =
756 factory()->NewJSObject(isolate()->object_function(), TENURED);
Ben Murdochc5610432016-08-08 18:44:38 +0100757 SetObjectPrototype(generator_function_prototype, empty);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000758
Ben Murdochc5610432016-08-08 18:44:38 +0100759 JSObject::AddProperty(
760 generator_function_prototype, factory()->to_string_tag_symbol(),
761 factory()->NewStringFromAsciiChecked("GeneratorFunction"),
762 static_cast<PropertyAttributes>(DONT_ENUM | READ_ONLY));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000763 JSObject::AddProperty(generator_function_prototype,
Ben Murdochc5610432016-08-08 18:44:38 +0100764 factory()->prototype_string(),
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000765 generator_object_prototype,
766 static_cast<PropertyAttributes>(DONT_ENUM | READ_ONLY));
767
Ben Murdochc5610432016-08-08 18:44:38 +0100768 JSObject::AddProperty(generator_object_prototype,
769 factory()->constructor_string(),
770 generator_function_prototype,
771 static_cast<PropertyAttributes>(DONT_ENUM | READ_ONLY));
772 JSObject::AddProperty(generator_object_prototype,
773 factory()->to_string_tag_symbol(),
774 factory()->NewStringFromAsciiChecked("Generator"),
775 static_cast<PropertyAttributes>(DONT_ENUM | READ_ONLY));
776 SimpleInstallFunction(generator_object_prototype, "next",
777 Builtins::kGeneratorPrototypeNext, 1, true);
778 SimpleInstallFunction(generator_object_prototype, "return",
779 Builtins::kGeneratorPrototypeReturn, 1, true);
780 SimpleInstallFunction(generator_object_prototype, "throw",
781 Builtins::kGeneratorPrototypeThrow, 1, true);
782
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000783 // Create maps for generator functions and their prototypes. Store those
784 // maps in the native context. The "prototype" property descriptor is
785 // writable, non-enumerable, and non-configurable (as per ES6 draft
786 // 04-14-15, section 25.2.4.3).
787 Handle<Map> strict_function_map(strict_function_map_writable_prototype_);
788 // Generator functions do not have "caller" or "arguments" accessors.
789 Handle<Map> sloppy_generator_function_map =
790 Map::Copy(strict_function_map, "SloppyGeneratorFunction");
Ben Murdoch097c5b22016-05-18 11:27:45 +0100791 sloppy_generator_function_map->set_is_constructor(false);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000792 Map::SetPrototype(sloppy_generator_function_map,
793 generator_function_prototype);
794 native_context()->set_sloppy_generator_function_map(
795 *sloppy_generator_function_map);
796
797 Handle<Map> strict_generator_function_map =
798 Map::Copy(strict_function_map, "StrictGeneratorFunction");
Ben Murdoch097c5b22016-05-18 11:27:45 +0100799 strict_generator_function_map->set_is_constructor(false);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000800 Map::SetPrototype(strict_generator_function_map,
801 generator_function_prototype);
802 native_context()->set_strict_generator_function_map(
803 *strict_generator_function_map);
804
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000805 Handle<JSFunction> object_function(native_context()->object_function());
806 Handle<Map> generator_object_prototype_map = Map::Create(isolate(), 0);
807 Map::SetPrototype(generator_object_prototype_map, generator_object_prototype);
808 native_context()->set_generator_object_prototype_map(
809 *generator_object_prototype_map);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100810}
811
Ben Murdochc5610432016-08-08 18:44:38 +0100812void Genesis::CreateAsyncFunctionMaps(Handle<JSFunction> empty) {
813 // %AsyncFunctionPrototype% intrinsic
814 Handle<JSObject> async_function_prototype =
815 factory()->NewJSObject(isolate()->object_function(), TENURED);
816 SetObjectPrototype(async_function_prototype, empty);
817
818 JSObject::AddProperty(async_function_prototype,
819 factory()->to_string_tag_symbol(),
820 factory()->NewStringFromAsciiChecked("AsyncFunction"),
821 static_cast<PropertyAttributes>(DONT_ENUM | READ_ONLY));
822
823 Handle<Map> strict_function_map(
824 native_context()->strict_function_without_prototype_map());
825 Handle<Map> sloppy_async_function_map =
826 Map::Copy(strict_function_map, "SloppyAsyncFunction");
827 sloppy_async_function_map->set_is_constructor(false);
828 Map::SetPrototype(sloppy_async_function_map, async_function_prototype);
829 native_context()->set_sloppy_async_function_map(*sloppy_async_function_map);
830
831 Handle<Map> strict_async_function_map =
832 Map::Copy(strict_function_map, "StrictAsyncFunction");
833 strict_async_function_map->set_is_constructor(false);
834 Map::SetPrototype(strict_async_function_map, async_function_prototype);
835 native_context()->set_strict_async_function_map(*strict_async_function_map);
836}
837
Ben Murdochda12d292016-06-02 14:46:10 +0100838void Genesis::CreateJSProxyMaps() {
839 // Allocate the different maps for all Proxy types.
840 // Next to the default proxy, we need maps indicating callable and
841 // constructable proxies.
842 Handle<Map> proxy_function_map =
843 Map::Copy(isolate()->sloppy_function_without_prototype_map(), "Proxy");
844 proxy_function_map->set_is_constructor(true);
845 native_context()->set_proxy_function_map(*proxy_function_map);
846
847 Handle<Map> proxy_map =
848 factory()->NewMap(JS_PROXY_TYPE, JSProxy::kSize, FAST_ELEMENTS);
849 proxy_map->set_dictionary_map(true);
850 native_context()->set_proxy_map(*proxy_map);
851
852 Handle<Map> proxy_callable_map = Map::Copy(proxy_map, "callable Proxy");
853 proxy_callable_map->set_is_callable();
854 native_context()->set_proxy_callable_map(*proxy_callable_map);
855 proxy_callable_map->SetConstructor(native_context()->function_function());
856
857 Handle<Map> proxy_constructor_map =
858 Map::Copy(proxy_callable_map, "constructor Proxy");
859 proxy_constructor_map->set_is_constructor(true);
860 native_context()->set_proxy_constructor_map(*proxy_constructor_map);
861}
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100862
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000863static void ReplaceAccessors(Handle<Map> map,
864 Handle<String> name,
865 PropertyAttributes attributes,
866 Handle<AccessorPair> accessor_pair) {
867 DescriptorArray* descriptors = map->instance_descriptors();
Ben Murdoch097c5b22016-05-18 11:27:45 +0100868 int idx = descriptors->SearchWithCache(map->GetIsolate(), *name, *map);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000869 AccessorConstantDescriptor descriptor(name, accessor_pair, attributes);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000870 descriptors->Replace(idx, &descriptor);
Steve Block44f0eee2011-05-26 01:26:41 +0100871}
872
873
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000874void Genesis::AddRestrictedFunctionProperties(Handle<Map> map) {
875 PropertyAttributes rw_attribs = static_cast<PropertyAttributes>(DONT_ENUM);
876 Handle<JSFunction> thrower = GetRestrictedFunctionPropertiesThrower();
877 Handle<AccessorPair> accessors = factory()->NewAccessorPair();
878 accessors->set_getter(*thrower);
879 accessors->set_setter(*thrower);
880
881 ReplaceAccessors(map, factory()->arguments_string(), rw_attribs, accessors);
882 ReplaceAccessors(map, factory()->caller_string(), rw_attribs, accessors);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000883}
884
885
886static void AddToWeakNativeContextList(Context* context) {
887 DCHECK(context->IsNativeContext());
Ben Murdoch257744e2011-11-30 15:57:28 +0000888 Heap* heap = context->GetIsolate()->heap();
Ben Murdochb0fe1622011-05-05 13:52:32 +0100889#ifdef DEBUG
890 { // NOLINT
Ben Murdochc5610432016-08-08 18:44:38 +0100891 DCHECK(context->next_context_link()->IsUndefined());
Ben Murdochb0fe1622011-05-05 13:52:32 +0100892 // Check that context is not in the list yet.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000893 for (Object* current = heap->native_contexts_list();
Ben Murdochb0fe1622011-05-05 13:52:32 +0100894 !current->IsUndefined();
Ben Murdochc5610432016-08-08 18:44:38 +0100895 current = Context::cast(current)->next_context_link()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000896 DCHECK(current != context);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100897 }
898 }
899#endif
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000900 context->set(Context::NEXT_CONTEXT_LINK, heap->native_contexts_list(),
901 UPDATE_WEAK_WRITE_BARRIER);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000902 heap->set_native_contexts_list(context);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100903}
904
905
Andrei Popescu31002712010-02-23 13:46:05 +0000906void Genesis::CreateRoots() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000907 // Allocate the native context FixedArray first and then patch the
Andrei Popescu31002712010-02-23 13:46:05 +0000908 // closure and extension object later (we need the empty function
909 // and the global object, but in order to create those, we need the
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000910 // native context).
911 native_context_ = factory()->NewNativeContext();
912 AddToWeakNativeContextList(*native_context());
913 isolate()->set_context(*native_context());
Andrei Popescu31002712010-02-23 13:46:05 +0000914
915 // Allocate the message listeners object.
916 {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000917 v8::NeanderArray listeners(isolate());
918 native_context()->set_message_listeners(*listeners.value());
Andrei Popescu31002712010-02-23 13:46:05 +0000919 }
920}
921
922
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000923void Genesis::InstallGlobalThisBinding() {
924 Handle<ScriptContextTable> script_contexts(
925 native_context()->script_context_table());
926 Handle<ScopeInfo> scope_info = ScopeInfo::CreateGlobalThisBinding(isolate());
927 Handle<JSFunction> closure(native_context()->closure());
928 Handle<Context> context = factory()->NewScriptContext(closure, scope_info);
929
930 // Go ahead and hook it up while we're at it.
931 int slot = scope_info->ReceiverContextSlotIndex();
932 DCHECK_EQ(slot, Context::MIN_CONTEXT_SLOTS);
933 context->set(slot, native_context()->global_proxy());
934
935 Handle<ScriptContextTable> new_script_contexts =
936 ScriptContextTable::Extend(script_contexts, context);
937 native_context()->set_script_context_table(*new_script_contexts);
938}
939
940
941Handle<JSGlobalObject> Genesis::CreateNewGlobals(
942 v8::Local<v8::ObjectTemplate> global_proxy_template,
943 Handle<JSGlobalProxy> global_proxy) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000944 // The argument global_proxy_template aka data is an ObjectTemplateInfo.
Andrei Popescu31002712010-02-23 13:46:05 +0000945 // It has a constructor pointer that points at global_constructor which is a
946 // FunctionTemplateInfo.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000947 // The global_proxy_constructor is used to (re)initialize the
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000948 // global_proxy. The global_proxy_constructor also has a prototype_template
949 // pointer that points at js_global_object_template which is an
950 // ObjectTemplateInfo.
Andrei Popescu31002712010-02-23 13:46:05 +0000951 // That in turn has a constructor pointer that points at
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000952 // js_global_object_constructor which is a FunctionTemplateInfo.
953 // js_global_object_constructor is used to make js_global_object_function
954 // js_global_object_function is used to make the new global_object.
Andrei Popescu31002712010-02-23 13:46:05 +0000955 //
956 // --- G l o b a l ---
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000957 // Step 1: Create a fresh JSGlobalObject.
958 Handle<JSFunction> js_global_object_function;
959 Handle<ObjectTemplateInfo> js_global_object_template;
960 if (!global_proxy_template.IsEmpty()) {
961 // Get prototype template of the global_proxy_template.
Andrei Popescu31002712010-02-23 13:46:05 +0000962 Handle<ObjectTemplateInfo> data =
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000963 v8::Utils::OpenHandle(*global_proxy_template);
Andrei Popescu31002712010-02-23 13:46:05 +0000964 Handle<FunctionTemplateInfo> global_constructor =
965 Handle<FunctionTemplateInfo>(
966 FunctionTemplateInfo::cast(data->constructor()));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000967 Handle<Object> proto_template(global_constructor->prototype_template(),
968 isolate());
Andrei Popescu31002712010-02-23 13:46:05 +0000969 if (!proto_template->IsUndefined()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000970 js_global_object_template =
Andrei Popescu31002712010-02-23 13:46:05 +0000971 Handle<ObjectTemplateInfo>::cast(proto_template);
972 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000973 }
974
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000975 if (js_global_object_template.is_null()) {
976 Handle<String> name = Handle<String>(heap()->empty_string());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000977 Handle<Code> code = isolate()->builtins()->Illegal();
Andrei Popescu31002712010-02-23 13:46:05 +0000978 Handle<JSObject> prototype =
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000979 factory()->NewFunctionPrototype(isolate()->object_function());
980 js_global_object_function = factory()->NewFunction(
981 name, code, prototype, JS_GLOBAL_OBJECT_TYPE, JSGlobalObject::kSize);
982#ifdef DEBUG
983 LookupIterator it(prototype, factory()->constructor_string(),
984 LookupIterator::OWN_SKIP_INTERCEPTOR);
Ben Murdochda12d292016-06-02 14:46:10 +0100985 Handle<Object> value = Object::GetProperty(&it).ToHandleChecked();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000986 DCHECK(it.IsFound());
987 DCHECK_EQ(*isolate()->object_function(), *value);
988#endif
Andrei Popescu31002712010-02-23 13:46:05 +0000989 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000990 Handle<FunctionTemplateInfo> js_global_object_constructor(
991 FunctionTemplateInfo::cast(js_global_object_template->constructor()));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000992 js_global_object_function = ApiNatives::CreateApiFunction(
993 isolate(), js_global_object_constructor, factory()->the_hole_value(),
994 ApiNatives::GlobalObjectType);
Steve Blocka7e24c12009-10-30 11:49:00 +0000995 }
996
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000997 js_global_object_function->initial_map()->set_is_prototype_map(true);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000998 js_global_object_function->initial_map()->set_dictionary_map(true);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000999 Handle<JSGlobalObject> global_object =
1000 factory()->NewJSGlobalObject(js_global_object_function);
Andrei Popescu31002712010-02-23 13:46:05 +00001001
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001002 // Step 2: (re)initialize the global proxy object.
Andrei Popescu31002712010-02-23 13:46:05 +00001003 Handle<JSFunction> global_proxy_function;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001004 if (global_proxy_template.IsEmpty()) {
1005 Handle<String> name = Handle<String>(heap()->empty_string());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001006 Handle<Code> code = isolate()->builtins()->Illegal();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001007 global_proxy_function = factory()->NewFunction(
1008 name, code, JS_GLOBAL_PROXY_TYPE, JSGlobalProxy::kSize);
Andrei Popescu31002712010-02-23 13:46:05 +00001009 } else {
1010 Handle<ObjectTemplateInfo> data =
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001011 v8::Utils::OpenHandle(*global_proxy_template);
Andrei Popescu31002712010-02-23 13:46:05 +00001012 Handle<FunctionTemplateInfo> global_constructor(
1013 FunctionTemplateInfo::cast(data->constructor()));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001014 global_proxy_function = ApiNatives::CreateApiFunction(
1015 isolate(), global_constructor, factory()->the_hole_value(),
1016 ApiNatives::GlobalProxyType);
Andrei Popescu31002712010-02-23 13:46:05 +00001017 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001018 Handle<String> global_name = factory()->global_string();
Andrei Popescu31002712010-02-23 13:46:05 +00001019 global_proxy_function->shared()->set_instance_class_name(*global_name);
1020 global_proxy_function->initial_map()->set_is_access_check_needed(true);
Ben Murdoch097c5b22016-05-18 11:27:45 +01001021 global_proxy_function->initial_map()->set_has_hidden_prototype(true);
Andrei Popescu31002712010-02-23 13:46:05 +00001022
1023 // Set global_proxy.__proto__ to js_global after ConfigureGlobalObjects
1024 // Return the global proxy.
1025
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001026 factory()->ReinitializeJSGlobalProxy(global_proxy, global_proxy_function);
1027 return global_object;
Andrei Popescu31002712010-02-23 13:46:05 +00001028}
1029
1030
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001031void Genesis::HookUpGlobalProxy(Handle<JSGlobalObject> global_object,
Andrei Popescu31002712010-02-23 13:46:05 +00001032 Handle<JSGlobalProxy> global_proxy) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001033 // Set the native context for the global object.
1034 global_object->set_native_context(*native_context());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001035 global_object->set_global_proxy(*global_proxy);
1036 global_proxy->set_native_context(*native_context());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001037 // If we deserialized the context, the global proxy is already
1038 // correctly set up. Otherwise it's undefined.
1039 DCHECK(native_context()->get(Context::GLOBAL_PROXY_INDEX)->IsUndefined() ||
1040 native_context()->global_proxy() == *global_proxy);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001041 native_context()->set_global_proxy(*global_proxy);
Andrei Popescu31002712010-02-23 13:46:05 +00001042}
1043
1044
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001045void Genesis::HookUpGlobalObject(Handle<JSGlobalObject> global_object) {
1046 Handle<JSGlobalObject> global_object_from_snapshot(
1047 JSGlobalObject::cast(native_context()->extension()));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001048 native_context()->set_extension(*global_object);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001049 native_context()->set_security_token(*global_object);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001050
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001051 TransferNamedProperties(global_object_from_snapshot, global_object);
1052 TransferIndexedProperties(global_object_from_snapshot, global_object);
Andrei Popescu402d9372010-02-26 13:31:12 +00001053}
1054
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001055static void InstallWithIntrinsicDefaultProto(Isolate* isolate,
1056 Handle<JSFunction> function,
1057 int context_index) {
1058 Handle<Smi> index(Smi::FromInt(context_index), isolate);
1059 JSObject::AddProperty(
1060 function, isolate->factory()->native_context_index_symbol(), index, NONE);
1061 isolate->native_context()->set(context_index, *function);
1062}
1063
1064
Andrei Popescu402d9372010-02-26 13:31:12 +00001065// This is only called if we are not using snapshots. The equivalent
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001066// work in the snapshot case is done in HookUpGlobalObject.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001067void Genesis::InitializeGlobal(Handle<JSGlobalObject> global_object,
1068 Handle<JSFunction> empty_function,
Ben Murdoch097c5b22016-05-18 11:27:45 +01001069 GlobalContextType context_type) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001070 // --- N a t i v e C o n t e x t ---
Andrei Popescu31002712010-02-23 13:46:05 +00001071 // Use the empty function as closure (no scope info).
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001072 native_context()->set_closure(*empty_function);
1073 native_context()->set_previous(NULL);
Andrei Popescu31002712010-02-23 13:46:05 +00001074 // Set extension and global object.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001075 native_context()->set_extension(*global_object);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001076 // Security setup: Set the security token of the native context to the global
1077 // object. This makes the security check between two different contexts fail
1078 // by default even in case of global object reinitialization.
1079 native_context()->set_security_token(*global_object);
Andrei Popescu31002712010-02-23 13:46:05 +00001080
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001081 Isolate* isolate = global_object->GetIsolate();
Steve Block44f0eee2011-05-26 01:26:41 +01001082 Factory* factory = isolate->factory();
Steve Block44f0eee2011-05-26 01:26:41 +01001083
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001084 Handle<ScriptContextTable> script_context_table =
1085 factory->NewScriptContextTable();
1086 native_context()->set_script_context_table(*script_context_table);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001087 InstallGlobalThisBinding();
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001088
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001089 { // --- O b j e c t ---
1090 Handle<String> object_name = factory->Object_string();
1091 Handle<JSFunction> object_function = isolate->object_function();
1092 JSObject::AddProperty(global_object, object_name, object_function,
1093 DONT_ENUM);
Ben Murdochc5610432016-08-08 18:44:38 +01001094
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001095 SimpleInstallFunction(object_function, factory->assign_string(),
1096 Builtins::kObjectAssign, 2, false);
1097 SimpleInstallFunction(object_function, factory->create_string(),
1098 Builtins::kObjectCreate, 2, false);
Ben Murdoch097c5b22016-05-18 11:27:45 +01001099 SimpleInstallFunction(object_function, "getOwnPropertyDescriptor",
1100 Builtins::kObjectGetOwnPropertyDescriptor, 2, false);
1101 SimpleInstallFunction(object_function, "getOwnPropertyNames",
1102 Builtins::kObjectGetOwnPropertyNames, 1, false);
1103 SimpleInstallFunction(object_function, "getOwnPropertySymbols",
1104 Builtins::kObjectGetOwnPropertySymbols, 1, false);
Ben Murdochc5610432016-08-08 18:44:38 +01001105 SimpleInstallFunction(object_function, "is",
1106 Builtins::kObjectIs, 2, true);
1107 SimpleInstallFunction(object_function, "preventExtensions",
1108 Builtins::kObjectPreventExtensions, 1, false);
1109 SimpleInstallFunction(object_function, "seal",
1110 Builtins::kObjectSeal, 1, false);
1111
1112 Handle<JSFunction> object_define_properties = SimpleInstallFunction(
1113 object_function, "defineProperties",
1114 Builtins::kObjectDefineProperties, 2, true);
1115 native_context()->set_object_define_properties(*object_define_properties);
1116
1117 Handle<JSFunction> object_define_property = SimpleInstallFunction(
1118 object_function, factory->defineProperty_string(),
1119 Builtins::kObjectDefineProperty, 3, true);
1120 native_context()->set_object_define_property(*object_define_property);
1121
1122 Handle<JSFunction> object_freeze = SimpleInstallFunction(
1123 object_function, "freeze", Builtins::kObjectFreeze, 1, false);
1124 native_context()->set_object_freeze(*object_freeze);
1125
1126 Handle<JSFunction> object_get_prototype_of = SimpleInstallFunction(
1127 object_function, "getPrototypeOf", Builtins::kObjectGetPrototypeOf,
1128 1, false);
1129 native_context()->set_object_get_prototype_of(*object_get_prototype_of);
1130
1131 Handle<JSFunction> object_is_extensible = SimpleInstallFunction(
1132 object_function, "isExtensible", Builtins::kObjectIsExtensible,
1133 1, false);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001134 native_context()->set_object_is_extensible(*object_is_extensible);
Ben Murdochc5610432016-08-08 18:44:38 +01001135
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001136 Handle<JSFunction> object_is_frozen = SimpleInstallFunction(
1137 object_function, "isFrozen", Builtins::kObjectIsFrozen, 1, false);
1138 native_context()->set_object_is_frozen(*object_is_frozen);
Ben Murdochc5610432016-08-08 18:44:38 +01001139
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001140 Handle<JSFunction> object_is_sealed = SimpleInstallFunction(
1141 object_function, "isSealed", Builtins::kObjectIsSealed, 1, false);
1142 native_context()->set_object_is_sealed(*object_is_sealed);
Ben Murdochc5610432016-08-08 18:44:38 +01001143
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001144 Handle<JSFunction> object_keys = SimpleInstallFunction(
1145 object_function, "keys", Builtins::kObjectKeys, 1, false);
1146 native_context()->set_object_keys(*object_keys);
Ben Murdochda12d292016-06-02 14:46:10 +01001147
Ben Murdochc5610432016-08-08 18:44:38 +01001148 SimpleInstallFunction(isolate->initial_object_prototype(),
1149 "__defineGetter__", Builtins::kObjectDefineGetter, 2,
1150 true);
1151 SimpleInstallFunction(isolate->initial_object_prototype(),
1152 "__defineSetter__", Builtins::kObjectDefineSetter, 2,
1153 true);
Ben Murdochda12d292016-06-02 14:46:10 +01001154 SimpleInstallFunction(isolate->initial_object_prototype(), "hasOwnProperty",
1155 Builtins::kObjectHasOwnProperty, 1, true);
Ben Murdochc5610432016-08-08 18:44:38 +01001156 SimpleInstallFunction(isolate->initial_object_prototype(),
1157 "__lookupGetter__", Builtins::kObjectLookupGetter, 1,
1158 true);
1159 SimpleInstallFunction(isolate->initial_object_prototype(),
1160 "__lookupSetter__", Builtins::kObjectLookupSetter, 1,
1161 true);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001162 }
Andrei Popescu31002712010-02-23 13:46:05 +00001163
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001164 Handle<JSObject> global(native_context()->global_object());
Steve Blocka7e24c12009-10-30 11:49:00 +00001165
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001166 { // --- F u n c t i o n ---
1167 Handle<JSFunction> prototype = empty_function;
1168 Handle<JSFunction> function_fun =
1169 InstallFunction(global, "Function", JS_FUNCTION_TYPE, JSFunction::kSize,
1170 prototype, Builtins::kFunctionConstructor);
1171 function_fun->set_prototype_or_initial_map(
1172 *sloppy_function_map_writable_prototype_);
1173 function_fun->shared()->DontAdaptArguments();
1174 function_fun->shared()->set_construct_stub(
1175 *isolate->builtins()->FunctionConstructor());
1176 function_fun->shared()->set_length(1);
1177 InstallWithIntrinsicDefaultProto(isolate, function_fun,
1178 Context::FUNCTION_FUNCTION_INDEX);
1179
1180 // Setup the methods on the %FunctionPrototype%.
1181 SimpleInstallFunction(prototype, factory->apply_string(),
1182 Builtins::kFunctionPrototypeApply, 2, false);
1183 SimpleInstallFunction(prototype, factory->bind_string(),
1184 Builtins::kFunctionPrototypeBind, 1, false);
1185 SimpleInstallFunction(prototype, factory->call_string(),
1186 Builtins::kFunctionPrototypeCall, 1, false);
1187 SimpleInstallFunction(prototype, factory->toString_string(),
1188 Builtins::kFunctionPrototypeToString, 0, false);
1189
Ben Murdoch097c5b22016-05-18 11:27:45 +01001190 // Install the @@hasInstance function.
1191 Handle<JSFunction> has_instance = InstallFunction(
1192 prototype, factory->has_instance_symbol(), JS_OBJECT_TYPE,
1193 JSObject::kHeaderSize, MaybeHandle<JSObject>(),
Ben Murdochc5610432016-08-08 18:44:38 +01001194 Builtins::kFunctionPrototypeHasInstance,
Ben Murdoch097c5b22016-05-18 11:27:45 +01001195 static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE | READ_ONLY));
1196
1197 // Set the expected parameters for @@hasInstance to 1; required by builtin.
1198 has_instance->shared()->set_internal_formal_parameter_count(1);
1199
1200 // Set the length for the function to satisfy ECMA-262.
1201 has_instance->shared()->set_length(1);
1202
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001203 // Install the "constructor" property on the %FunctionPrototype%.
1204 JSObject::AddProperty(prototype, factory->constructor_string(),
1205 function_fun, DONT_ENUM);
1206
1207 sloppy_function_map_writable_prototype_->SetConstructor(*function_fun);
1208 strict_function_map_writable_prototype_->SetConstructor(*function_fun);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001209 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001210
1211 { // --- A r r a y ---
1212 Handle<JSFunction> array_function =
1213 InstallFunction(global, "Array", JS_ARRAY_TYPE, JSArray::kSize,
Steve Block44f0eee2011-05-26 01:26:41 +01001214 isolate->initial_object_prototype(),
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001215 Builtins::kArrayCode);
Steve Blocka7e24c12009-10-30 11:49:00 +00001216 array_function->shared()->DontAdaptArguments();
Ben Murdochda12d292016-06-02 14:46:10 +01001217 array_function->shared()->set_builtin_function_id(kArrayCode);
Steve Blocka7e24c12009-10-30 11:49:00 +00001218
1219 // This seems a bit hackish, but we need to make sure Array.length
1220 // is 1.
1221 array_function->shared()->set_length(1);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001222
1223 Handle<Map> initial_map(array_function->initial_map());
1224
1225 // This assert protects an optimization in
1226 // HGraphBuilder::JSArrayBuilder::EmitMapCode()
1227 DCHECK(initial_map->elements_kind() == GetInitialFastElementsKind());
1228 Map::EnsureDescriptorSlack(initial_map, 1);
1229
1230 PropertyAttributes attribs = static_cast<PropertyAttributes>(
1231 DONT_ENUM | DONT_DELETE);
1232
1233 Handle<AccessorInfo> array_length =
1234 Accessors::ArrayLengthInfo(isolate, attribs);
1235 { // Add length.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001236 AccessorConstantDescriptor d(
1237 Handle<Name>(Name::cast(array_length->name())), array_length,
1238 attribs);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001239 initial_map->AppendDescriptor(&d);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001240 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001241
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001242 InstallWithIntrinsicDefaultProto(isolate, array_function,
1243 Context::ARRAY_FUNCTION_INDEX);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001244
1245 // Cache the array maps, needed by ArrayConstructorStub
1246 CacheInitialJSArrayMaps(native_context(), initial_map);
1247 ArrayConstructorStub array_constructor_stub(isolate);
1248 Handle<Code> code = array_constructor_stub.GetCode();
1249 array_function->shared()->set_construct_stub(*code);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001250
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001251 Handle<JSFunction> is_arraylike = SimpleInstallFunction(
1252 array_function, isolate->factory()->InternalizeUtf8String("isArray"),
1253 Builtins::kArrayIsArray, 1, true);
1254 native_context()->set_is_arraylike(*is_arraylike);
Steve Blocka7e24c12009-10-30 11:49:00 +00001255 }
1256
1257 { // --- N u m b e r ---
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001258 Handle<JSFunction> number_fun = InstallFunction(
1259 global, "Number", JS_VALUE_TYPE, JSValue::kSize,
1260 isolate->initial_object_prototype(), Builtins::kNumberConstructor);
1261 number_fun->shared()->DontAdaptArguments();
1262 number_fun->shared()->set_construct_stub(
1263 *isolate->builtins()->NumberConstructor_ConstructStub());
1264 number_fun->shared()->set_length(1);
1265 InstallWithIntrinsicDefaultProto(isolate, number_fun,
1266 Context::NUMBER_FUNCTION_INDEX);
Steve Blocka7e24c12009-10-30 11:49:00 +00001267 }
1268
1269 { // --- B o o l e a n ---
1270 Handle<JSFunction> boolean_fun =
1271 InstallFunction(global, "Boolean", JS_VALUE_TYPE, JSValue::kSize,
Steve Block44f0eee2011-05-26 01:26:41 +01001272 isolate->initial_object_prototype(),
Ben Murdoch097c5b22016-05-18 11:27:45 +01001273 Builtins::kBooleanConstructor);
1274 boolean_fun->shared()->DontAdaptArguments();
1275 boolean_fun->shared()->set_construct_stub(
1276 *isolate->builtins()->BooleanConstructor_ConstructStub());
1277 boolean_fun->shared()->set_length(1);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001278 InstallWithIntrinsicDefaultProto(isolate, boolean_fun,
1279 Context::BOOLEAN_FUNCTION_INDEX);
Ben Murdoch097c5b22016-05-18 11:27:45 +01001280
1281 // Create the %BooleanPrototype%
1282 Handle<JSValue> prototype =
1283 Handle<JSValue>::cast(factory->NewJSObject(boolean_fun, TENURED));
1284 prototype->set_value(isolate->heap()->false_value());
1285 Accessors::FunctionSetPrototype(boolean_fun, prototype).Assert();
1286
1287 // Install the "constructor" property on the {prototype}.
1288 JSObject::AddProperty(prototype, factory->constructor_string(), boolean_fun,
1289 DONT_ENUM);
1290
1291 // Install the Boolean.prototype methods.
1292 SimpleInstallFunction(prototype, "toString",
1293 Builtins::kBooleanPrototypeToString, 0, false);
1294 SimpleInstallFunction(prototype, "valueOf",
1295 Builtins::kBooleanPrototypeValueOf, 0, false);
Steve Blocka7e24c12009-10-30 11:49:00 +00001296 }
1297
1298 { // --- S t r i n g ---
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001299 Handle<JSFunction> string_fun = InstallFunction(
1300 global, "String", JS_VALUE_TYPE, JSValue::kSize,
1301 isolate->initial_object_prototype(), Builtins::kStringConstructor);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001302 string_fun->shared()->set_construct_stub(
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001303 *isolate->builtins()->StringConstructor_ConstructStub());
1304 string_fun->shared()->DontAdaptArguments();
1305 string_fun->shared()->set_length(1);
1306 InstallWithIntrinsicDefaultProto(isolate, string_fun,
1307 Context::STRING_FUNCTION_INDEX);
Steve Blocka7e24c12009-10-30 11:49:00 +00001308
1309 Handle<Map> string_map =
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001310 Handle<Map>(native_context()->string_function()->initial_map());
Ben Murdoch097c5b22016-05-18 11:27:45 +01001311 string_map->set_elements_kind(FAST_STRING_WRAPPER_ELEMENTS);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001312 Map::EnsureDescriptorSlack(string_map, 1);
1313
1314 PropertyAttributes attribs = static_cast<PropertyAttributes>(
1315 DONT_ENUM | DONT_DELETE | READ_ONLY);
1316 Handle<AccessorInfo> string_length(
1317 Accessors::StringLengthInfo(isolate, attribs));
1318
1319 { // Add length.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001320 AccessorConstantDescriptor d(factory->length_string(), string_length,
1321 attribs);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001322 string_map->AppendDescriptor(&d);
1323 }
Ben Murdochda12d292016-06-02 14:46:10 +01001324
1325 // Install the String.fromCharCode function.
1326 SimpleInstallFunction(string_fun, "fromCharCode",
1327 Builtins::kStringFromCharCode, 1, false);
Ben Murdochc5610432016-08-08 18:44:38 +01001328
1329 // Create the %StringPrototype%
1330 Handle<JSValue> prototype =
1331 Handle<JSValue>::cast(factory->NewJSObject(string_fun, TENURED));
1332 prototype->set_value(isolate->heap()->empty_string());
1333 Accessors::FunctionSetPrototype(string_fun, prototype).Assert();
1334
1335 // Install the "constructor" property on the {prototype}.
1336 JSObject::AddProperty(prototype, factory->constructor_string(), string_fun,
1337 DONT_ENUM);
1338
1339 // Install the String.prototype methods.
1340 SimpleInstallFunction(prototype, "charAt", Builtins::kStringPrototypeCharAt,
1341 1, true);
1342 SimpleInstallFunction(prototype, "charCodeAt",
1343 Builtins::kStringPrototypeCharCodeAt, 1, true);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001344 }
1345
1346 {
1347 // --- S y m b o l ---
Ben Murdoch097c5b22016-05-18 11:27:45 +01001348 Handle<JSObject> prototype =
1349 factory->NewJSObject(isolate->object_function(), TENURED);
1350 Handle<JSFunction> symbol_fun =
1351 InstallFunction(global, "Symbol", JS_VALUE_TYPE, JSValue::kSize,
1352 prototype, Builtins::kSymbolConstructor);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001353 symbol_fun->shared()->set_construct_stub(
1354 *isolate->builtins()->SymbolConstructor_ConstructStub());
Ben Murdochda12d292016-06-02 14:46:10 +01001355 symbol_fun->shared()->set_length(0);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001356 symbol_fun->shared()->DontAdaptArguments();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001357 native_context()->set_symbol_function(*symbol_fun);
Ben Murdoch097c5b22016-05-18 11:27:45 +01001358
1359 // Install the "constructor" property on the {prototype}.
1360 JSObject::AddProperty(prototype, factory->constructor_string(), symbol_fun,
1361 DONT_ENUM);
Steve Blocka7e24c12009-10-30 11:49:00 +00001362 }
1363
1364 { // --- D a t e ---
1365 // Builtin functions for Date.prototype.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001366 Handle<JSObject> prototype =
1367 factory->NewJSObject(isolate->object_function(), TENURED);
Steve Blocka7e24c12009-10-30 11:49:00 +00001368 Handle<JSFunction> date_fun =
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001369 InstallFunction(global, "Date", JS_DATE_TYPE, JSDate::kSize, prototype,
1370 Builtins::kDateConstructor);
1371 InstallWithIntrinsicDefaultProto(isolate, date_fun,
1372 Context::DATE_FUNCTION_INDEX);
1373 date_fun->shared()->set_construct_stub(
1374 *isolate->builtins()->DateConstructor_ConstructStub());
1375 date_fun->shared()->set_length(7);
1376 date_fun->shared()->DontAdaptArguments();
Steve Blocka7e24c12009-10-30 11:49:00 +00001377
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001378 // Install the Date.now, Date.parse and Date.UTC functions.
1379 SimpleInstallFunction(date_fun, "now", Builtins::kDateNow, 0, false);
1380 SimpleInstallFunction(date_fun, "parse", Builtins::kDateParse, 1, false);
1381 SimpleInstallFunction(date_fun, "UTC", Builtins::kDateUTC, 7, false);
1382
1383 // Install the "constructor" property on the {prototype}.
1384 JSObject::AddProperty(prototype, factory->constructor_string(), date_fun,
1385 DONT_ENUM);
1386
1387 // Install the Date.prototype methods.
1388 SimpleInstallFunction(prototype, "toString",
1389 Builtins::kDatePrototypeToString, 0, false);
1390 SimpleInstallFunction(prototype, "toDateString",
1391 Builtins::kDatePrototypeToDateString, 0, false);
1392 SimpleInstallFunction(prototype, "toTimeString",
1393 Builtins::kDatePrototypeToTimeString, 0, false);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001394 SimpleInstallFunction(prototype, "toISOString",
1395 Builtins::kDatePrototypeToISOString, 0, false);
Ben Murdoch097c5b22016-05-18 11:27:45 +01001396 Handle<JSFunction> to_utc_string =
1397 SimpleInstallFunction(prototype, "toUTCString",
1398 Builtins::kDatePrototypeToUTCString, 0, false);
1399 InstallFunction(prototype, to_utc_string,
1400 factory->InternalizeUtf8String("toGMTString"), DONT_ENUM);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001401 SimpleInstallFunction(prototype, "getDate", Builtins::kDatePrototypeGetDate,
1402 0, true);
1403 SimpleInstallFunction(prototype, "setDate", Builtins::kDatePrototypeSetDate,
1404 1, false);
1405 SimpleInstallFunction(prototype, "getDay", Builtins::kDatePrototypeGetDay,
1406 0, true);
1407 SimpleInstallFunction(prototype, "getFullYear",
1408 Builtins::kDatePrototypeGetFullYear, 0, true);
1409 SimpleInstallFunction(prototype, "setFullYear",
1410 Builtins::kDatePrototypeSetFullYear, 3, false);
1411 SimpleInstallFunction(prototype, "getHours",
1412 Builtins::kDatePrototypeGetHours, 0, true);
1413 SimpleInstallFunction(prototype, "setHours",
1414 Builtins::kDatePrototypeSetHours, 4, false);
1415 SimpleInstallFunction(prototype, "getMilliseconds",
1416 Builtins::kDatePrototypeGetMilliseconds, 0, true);
1417 SimpleInstallFunction(prototype, "setMilliseconds",
1418 Builtins::kDatePrototypeSetMilliseconds, 1, false);
1419 SimpleInstallFunction(prototype, "getMinutes",
1420 Builtins::kDatePrototypeGetMinutes, 0, true);
1421 SimpleInstallFunction(prototype, "setMinutes",
1422 Builtins::kDatePrototypeSetMinutes, 3, false);
1423 SimpleInstallFunction(prototype, "getMonth",
1424 Builtins::kDatePrototypeGetMonth, 0, true);
1425 SimpleInstallFunction(prototype, "setMonth",
1426 Builtins::kDatePrototypeSetMonth, 2, false);
1427 SimpleInstallFunction(prototype, "getSeconds",
1428 Builtins::kDatePrototypeGetSeconds, 0, true);
1429 SimpleInstallFunction(prototype, "setSeconds",
1430 Builtins::kDatePrototypeSetSeconds, 2, false);
1431 SimpleInstallFunction(prototype, "getTime", Builtins::kDatePrototypeGetTime,
1432 0, true);
1433 SimpleInstallFunction(prototype, "setTime", Builtins::kDatePrototypeSetTime,
1434 1, false);
1435 SimpleInstallFunction(prototype, "getTimezoneOffset",
1436 Builtins::kDatePrototypeGetTimezoneOffset, 0, true);
1437 SimpleInstallFunction(prototype, "getUTCDate",
1438 Builtins::kDatePrototypeGetUTCDate, 0, true);
1439 SimpleInstallFunction(prototype, "setUTCDate",
1440 Builtins::kDatePrototypeSetUTCDate, 1, false);
1441 SimpleInstallFunction(prototype, "getUTCDay",
1442 Builtins::kDatePrototypeGetUTCDay, 0, true);
1443 SimpleInstallFunction(prototype, "getUTCFullYear",
1444 Builtins::kDatePrototypeGetUTCFullYear, 0, true);
1445 SimpleInstallFunction(prototype, "setUTCFullYear",
1446 Builtins::kDatePrototypeSetUTCFullYear, 3, false);
1447 SimpleInstallFunction(prototype, "getUTCHours",
1448 Builtins::kDatePrototypeGetUTCHours, 0, true);
1449 SimpleInstallFunction(prototype, "setUTCHours",
1450 Builtins::kDatePrototypeSetUTCHours, 4, false);
1451 SimpleInstallFunction(prototype, "getUTCMilliseconds",
1452 Builtins::kDatePrototypeGetUTCMilliseconds, 0, true);
1453 SimpleInstallFunction(prototype, "setUTCMilliseconds",
1454 Builtins::kDatePrototypeSetUTCMilliseconds, 1, false);
1455 SimpleInstallFunction(prototype, "getUTCMinutes",
1456 Builtins::kDatePrototypeGetUTCMinutes, 0, true);
1457 SimpleInstallFunction(prototype, "setUTCMinutes",
1458 Builtins::kDatePrototypeSetUTCMinutes, 3, false);
1459 SimpleInstallFunction(prototype, "getUTCMonth",
1460 Builtins::kDatePrototypeGetUTCMonth, 0, true);
1461 SimpleInstallFunction(prototype, "setUTCMonth",
1462 Builtins::kDatePrototypeSetUTCMonth, 2, false);
1463 SimpleInstallFunction(prototype, "getUTCSeconds",
1464 Builtins::kDatePrototypeGetUTCSeconds, 0, true);
1465 SimpleInstallFunction(prototype, "setUTCSeconds",
1466 Builtins::kDatePrototypeSetUTCSeconds, 2, false);
1467 SimpleInstallFunction(prototype, "valueOf", Builtins::kDatePrototypeValueOf,
1468 0, false);
1469 SimpleInstallFunction(prototype, "getYear", Builtins::kDatePrototypeGetYear,
1470 0, true);
1471 SimpleInstallFunction(prototype, "setYear", Builtins::kDatePrototypeSetYear,
1472 1, false);
1473
1474 // Install i18n fallback functions.
1475 SimpleInstallFunction(prototype, "toLocaleString",
1476 Builtins::kDatePrototypeToString, 0, false);
1477 SimpleInstallFunction(prototype, "toLocaleDateString",
1478 Builtins::kDatePrototypeToDateString, 0, false);
1479 SimpleInstallFunction(prototype, "toLocaleTimeString",
1480 Builtins::kDatePrototypeToTimeString, 0, false);
1481
1482 // Install the @@toPrimitive function.
1483 Handle<JSFunction> to_primitive = InstallFunction(
1484 prototype, factory->to_primitive_symbol(), JS_OBJECT_TYPE,
1485 JSObject::kHeaderSize, MaybeHandle<JSObject>(),
1486 Builtins::kDatePrototypeToPrimitive,
1487 static_cast<PropertyAttributes>(DONT_ENUM | READ_ONLY));
1488
1489 // Set the expected parameters for @@toPrimitive to 1; required by builtin.
1490 to_primitive->shared()->set_internal_formal_parameter_count(1);
1491
1492 // Set the length for the function to satisfy ECMA-262.
1493 to_primitive->shared()->set_length(1);
Steve Blocka7e24c12009-10-30 11:49:00 +00001494 }
1495
Steve Blocka7e24c12009-10-30 11:49:00 +00001496 { // -- R e g E x p
1497 // Builtin functions for RegExp.prototype.
1498 Handle<JSFunction> regexp_fun =
1499 InstallFunction(global, "RegExp", JS_REGEXP_TYPE, JSRegExp::kSize,
Steve Block44f0eee2011-05-26 01:26:41 +01001500 isolate->initial_object_prototype(),
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001501 Builtins::kIllegal);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001502 InstallWithIntrinsicDefaultProto(isolate, regexp_fun,
1503 Context::REGEXP_FUNCTION_INDEX);
1504 regexp_fun->shared()->set_construct_stub(
1505 *isolate->builtins()->JSBuiltinsConstructStub());
Steve Block6ded16b2010-05-10 14:33:55 +01001506
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001507 DCHECK(regexp_fun->has_initial_map());
Steve Block6ded16b2010-05-10 14:33:55 +01001508 Handle<Map> initial_map(regexp_fun->initial_map());
1509
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001510 DCHECK_EQ(0, initial_map->GetInObjectProperties());
Steve Block6ded16b2010-05-10 14:33:55 +01001511
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001512 Map::EnsureDescriptorSlack(initial_map, 1);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001513
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001514 // ECMA-262, section 15.10.7.5.
1515 PropertyAttributes writable =
1516 static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE);
1517 DataDescriptor field(factory->last_index_string(),
1518 JSRegExp::kLastIndexFieldIndex, writable,
1519 Representation::Tagged());
1520 initial_map->AppendDescriptor(&field);
Steve Block6ded16b2010-05-10 14:33:55 +01001521
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001522 static const int num_fields = JSRegExp::kInObjectFieldCount;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001523 initial_map->SetInObjectProperties(num_fields);
Steve Block6ded16b2010-05-10 14:33:55 +01001524 initial_map->set_unused_property_fields(0);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001525 initial_map->set_instance_size(initial_map->instance_size() +
1526 num_fields * kPointerSize);
Steve Blocka7e24c12009-10-30 11:49:00 +00001527 }
1528
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001529 { // -- E r r o r
1530 Handle<JSFunction> error_fun = InstallFunction(
1531 global, "Error", JS_OBJECT_TYPE, JSObject::kHeaderSize,
1532 isolate->initial_object_prototype(), Builtins::kIllegal);
1533 InstallWithIntrinsicDefaultProto(isolate, error_fun,
1534 Context::ERROR_FUNCTION_INDEX);
1535 }
1536
1537 { // -- E v a l E r r o r
1538 Handle<JSFunction> eval_error_fun = InstallFunction(
1539 global, "EvalError", JS_OBJECT_TYPE, JSObject::kHeaderSize,
1540 isolate->initial_object_prototype(), Builtins::kIllegal);
1541 InstallWithIntrinsicDefaultProto(isolate, eval_error_fun,
1542 Context::EVAL_ERROR_FUNCTION_INDEX);
1543 }
1544
1545 { // -- R a n g e E r r o r
1546 Handle<JSFunction> range_error_fun = InstallFunction(
1547 global, "RangeError", JS_OBJECT_TYPE, JSObject::kHeaderSize,
1548 isolate->initial_object_prototype(), Builtins::kIllegal);
1549 InstallWithIntrinsicDefaultProto(isolate, range_error_fun,
1550 Context::RANGE_ERROR_FUNCTION_INDEX);
1551 }
1552
1553 { // -- R e f e r e n c e E r r o r
1554 Handle<JSFunction> reference_error_fun = InstallFunction(
1555 global, "ReferenceError", JS_OBJECT_TYPE, JSObject::kHeaderSize,
1556 isolate->initial_object_prototype(), Builtins::kIllegal);
1557 InstallWithIntrinsicDefaultProto(isolate, reference_error_fun,
1558 Context::REFERENCE_ERROR_FUNCTION_INDEX);
1559 }
1560
1561 { // -- S y n t a x E r r o r
1562 Handle<JSFunction> syntax_error_fun = InstallFunction(
1563 global, "SyntaxError", JS_OBJECT_TYPE, JSObject::kHeaderSize,
1564 isolate->initial_object_prototype(), Builtins::kIllegal);
1565 InstallWithIntrinsicDefaultProto(isolate, syntax_error_fun,
1566 Context::SYNTAX_ERROR_FUNCTION_INDEX);
1567 }
1568
1569 { // -- T y p e E r r o r
1570 Handle<JSFunction> type_error_fun = InstallFunction(
1571 global, "TypeError", JS_OBJECT_TYPE, JSObject::kHeaderSize,
1572 isolate->initial_object_prototype(), Builtins::kIllegal);
1573 InstallWithIntrinsicDefaultProto(isolate, type_error_fun,
1574 Context::TYPE_ERROR_FUNCTION_INDEX);
1575 }
1576
1577 { // -- U R I E r r o r
1578 Handle<JSFunction> uri_error_fun = InstallFunction(
1579 global, "URIError", JS_OBJECT_TYPE, JSObject::kHeaderSize,
1580 isolate->initial_object_prototype(), Builtins::kIllegal);
1581 InstallWithIntrinsicDefaultProto(isolate, uri_error_fun,
1582 Context::URI_ERROR_FUNCTION_INDEX);
1583 }
1584
1585 // Initialize the embedder data slot.
1586 Handle<FixedArray> embedder_data = factory->NewFixedArray(3);
1587 native_context()->set_embedder_data(*embedder_data);
1588
1589 if (context_type == THIN_CONTEXT) return;
1590
Steve Blocka7e24c12009-10-30 11:49:00 +00001591 { // -- J S O N
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001592 Handle<String> name = factory->InternalizeUtf8String("JSON");
1593 Handle<JSFunction> cons = factory->NewFunction(name);
1594 JSFunction::SetInstancePrototype(cons,
1595 Handle<Object>(native_context()->initial_object_prototype(), isolate));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001596 cons->shared()->set_instance_class_name(*name);
Steve Block44f0eee2011-05-26 01:26:41 +01001597 Handle<JSObject> json_object = factory->NewJSObject(cons, TENURED);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001598 DCHECK(json_object->IsJSObject());
1599 JSObject::AddProperty(global, name, json_object, DONT_ENUM);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001600 }
1601
1602 { // -- M a t h
1603 Handle<String> name = factory->InternalizeUtf8String("Math");
1604 Handle<JSFunction> cons = factory->NewFunction(name);
1605 JSFunction::SetInstancePrototype(
1606 cons,
1607 Handle<Object>(native_context()->initial_object_prototype(), isolate));
1608 cons->shared()->set_instance_class_name(*name);
Ben Murdoch097c5b22016-05-18 11:27:45 +01001609 Handle<JSObject> math = factory->NewJSObject(cons, TENURED);
1610 DCHECK(math->IsJSObject());
1611 JSObject::AddProperty(global, name, math, DONT_ENUM);
Ben Murdochda12d292016-06-02 14:46:10 +01001612 SimpleInstallFunction(math, "acos", Builtins::kMathAcos, 1, true);
1613 SimpleInstallFunction(math, "asin", Builtins::kMathAsin, 1, true);
1614 SimpleInstallFunction(math, "atan", Builtins::kMathAtan, 1, true);
1615 SimpleInstallFunction(math, "ceil", Builtins::kMathCeil, 1, true);
1616 SimpleInstallFunction(math, "clz32", Builtins::kMathClz32, 1, true);
1617 Handle<JSFunction> math_floor =
1618 SimpleInstallFunction(math, "floor", Builtins::kMathFloor, 1, true);
1619 native_context()->set_math_floor(*math_floor);
1620 SimpleInstallFunction(math, "fround", Builtins::kMathFround, 1, true);
1621 SimpleInstallFunction(math, "imul", Builtins::kMathImul, 2, true);
Ben Murdoch097c5b22016-05-18 11:27:45 +01001622 SimpleInstallFunction(math, "max", Builtins::kMathMax, 2, false);
1623 SimpleInstallFunction(math, "min", Builtins::kMathMin, 2, false);
Ben Murdochda12d292016-06-02 14:46:10 +01001624 SimpleInstallFunction(math, "round", Builtins::kMathRound, 1, true);
1625 Handle<JSFunction> math_sqrt =
1626 SimpleInstallFunction(math, "sqrt", Builtins::kMathSqrt, 1, true);
1627 native_context()->set_math_sqrt(*math_sqrt);
1628 SimpleInstallFunction(math, "trunc", Builtins::kMathTrunc, 1, true);
Steve Blocka7e24c12009-10-30 11:49:00 +00001629 }
1630
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001631 { // -- A r r a y B u f f e r
1632 Handle<JSFunction> array_buffer_fun =
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001633 InstallArrayBuffer(global, "ArrayBuffer");
1634 InstallWithIntrinsicDefaultProto(isolate, array_buffer_fun,
1635 Context::ARRAY_BUFFER_FUN_INDEX);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001636 }
1637
1638 { // -- T y p e d A r r a y s
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001639#define INSTALL_TYPED_ARRAY(Type, type, TYPE, ctype, size) \
1640 { \
1641 Handle<JSFunction> fun; \
1642 InstallTypedArray(#Type "Array", TYPE##_ELEMENTS, &fun); \
1643 InstallWithIntrinsicDefaultProto(isolate, fun, \
1644 Context::TYPE##_ARRAY_FUN_INDEX); \
1645 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001646 TYPED_ARRAYS(INSTALL_TYPED_ARRAY)
1647#undef INSTALL_TYPED_ARRAY
1648
Ben Murdoch097c5b22016-05-18 11:27:45 +01001649 Handle<JSFunction> data_view_fun = InstallFunction(
1650 global, "DataView", JS_DATA_VIEW_TYPE,
1651 JSDataView::kSizeWithInternalFields,
1652 isolate->initial_object_prototype(), Builtins::kDataViewConstructor);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001653 InstallWithIntrinsicDefaultProto(isolate, data_view_fun,
1654 Context::DATA_VIEW_FUN_INDEX);
1655 data_view_fun->shared()->set_construct_stub(
Ben Murdoch097c5b22016-05-18 11:27:45 +01001656 *isolate->builtins()->DataViewConstructor_ConstructStub());
1657 data_view_fun->shared()->set_length(3);
1658 data_view_fun->shared()->DontAdaptArguments();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001659 }
1660
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001661 { // -- M a p
1662 Handle<JSFunction> js_map_fun = InstallFunction(
1663 global, "Map", JS_MAP_TYPE, JSMap::kSize,
1664 isolate->initial_object_prototype(), Builtins::kIllegal);
1665 InstallWithIntrinsicDefaultProto(isolate, js_map_fun,
1666 Context::JS_MAP_FUN_INDEX);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001667 }
1668
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001669 { // -- S e t
1670 Handle<JSFunction> js_set_fun = InstallFunction(
1671 global, "Set", JS_SET_TYPE, JSSet::kSize,
1672 isolate->initial_object_prototype(), Builtins::kIllegal);
1673 InstallWithIntrinsicDefaultProto(isolate, js_set_fun,
1674 Context::JS_SET_FUN_INDEX);
1675 }
1676
1677 { // -- I t e r a t o r R e s u l t
1678 Handle<Map> map =
Ben Murdoch097c5b22016-05-18 11:27:45 +01001679 factory->NewMap(JS_OBJECT_TYPE, JSIteratorResult::kSize);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001680 Map::SetPrototype(map, isolate->initial_object_prototype());
1681 Map::EnsureDescriptorSlack(map, 2);
1682
1683 { // value
1684 DataDescriptor d(factory->value_string(), JSIteratorResult::kValueIndex,
1685 NONE, Representation::Tagged());
1686 map->AppendDescriptor(&d);
1687 }
1688
1689 { // done
1690 DataDescriptor d(factory->done_string(), JSIteratorResult::kDoneIndex,
1691 NONE, Representation::Tagged());
1692 map->AppendDescriptor(&d);
1693 }
1694
Ben Murdoch097c5b22016-05-18 11:27:45 +01001695 map->SetConstructor(native_context()->object_function());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001696 map->SetInObjectProperties(2);
1697 native_context()->set_iterator_result_map(*map);
1698 }
1699
1700 { // -- W e a k M a p
1701 Handle<JSFunction> js_weak_map_fun = InstallFunction(
1702 global, "WeakMap", JS_WEAK_MAP_TYPE, JSWeakMap::kSize,
1703 isolate->initial_object_prototype(), Builtins::kIllegal);
1704 InstallWithIntrinsicDefaultProto(isolate, js_weak_map_fun,
1705 Context::JS_WEAK_MAP_FUN_INDEX);
1706 }
1707
1708 { // -- W e a k S e t
1709 Handle<JSFunction> js_weak_set_fun = InstallFunction(
1710 global, "WeakSet", JS_WEAK_SET_TYPE, JSWeakSet::kSize,
1711 isolate->initial_object_prototype(), Builtins::kIllegal);
1712 InstallWithIntrinsicDefaultProto(isolate, js_weak_set_fun,
1713 Context::JS_WEAK_SET_FUN_INDEX);
1714 }
1715
Ben Murdochda12d292016-06-02 14:46:10 +01001716 { // -- P r o x y
1717 CreateJSProxyMaps();
1718
1719 Handle<String> name = factory->Proxy_string();
1720 Handle<Code> code(isolate->builtins()->ProxyConstructor());
1721
1722 Handle<JSFunction> proxy_function =
1723 factory->NewFunction(isolate->proxy_function_map(),
1724 factory->Proxy_string(), MaybeHandle<Code>(code));
1725
1726 JSFunction::SetInitialMap(
1727 proxy_function, Handle<Map>(native_context()->proxy_map(), isolate),
1728 factory->null_value());
1729
1730 proxy_function->shared()->set_construct_stub(
1731 *isolate->builtins()->ProxyConstructor_ConstructStub());
1732 proxy_function->shared()->set_internal_formal_parameter_count(2);
1733 proxy_function->shared()->set_length(2);
1734
1735 native_context()->set_proxy_function(*proxy_function);
1736 InstallFunction(global, name, proxy_function, factory->Object_string());
1737 }
1738
1739 { // -- R e f l e c t
1740 Handle<String> reflect_string = factory->InternalizeUtf8String("Reflect");
1741 Handle<JSObject> reflect =
1742 factory->NewJSObject(isolate->object_function(), TENURED);
1743 JSObject::AddProperty(global, reflect_string, reflect, DONT_ENUM);
1744
1745 Handle<JSFunction> define_property =
1746 SimpleInstallFunction(reflect, factory->defineProperty_string(),
1747 Builtins::kReflectDefineProperty, 3, true);
1748 native_context()->set_reflect_define_property(*define_property);
1749
1750 Handle<JSFunction> delete_property =
1751 SimpleInstallFunction(reflect, factory->deleteProperty_string(),
1752 Builtins::kReflectDeleteProperty, 2, true);
1753 native_context()->set_reflect_delete_property(*delete_property);
1754
1755 Handle<JSFunction> apply = SimpleInstallFunction(
1756 reflect, factory->apply_string(), Builtins::kReflectApply, 3, false);
1757 native_context()->set_reflect_apply(*apply);
1758
1759 Handle<JSFunction> construct =
1760 SimpleInstallFunction(reflect, factory->construct_string(),
1761 Builtins::kReflectConstruct, 2, false);
1762 native_context()->set_reflect_construct(*construct);
1763
1764 SimpleInstallFunction(reflect, factory->get_string(), Builtins::kReflectGet,
1765 2, false);
1766 SimpleInstallFunction(reflect, factory->getOwnPropertyDescriptor_string(),
1767 Builtins::kReflectGetOwnPropertyDescriptor, 2, true);
1768 SimpleInstallFunction(reflect, factory->getPrototypeOf_string(),
1769 Builtins::kReflectGetPrototypeOf, 1, true);
1770 SimpleInstallFunction(reflect, factory->has_string(), Builtins::kReflectHas,
1771 2, true);
1772 SimpleInstallFunction(reflect, factory->isExtensible_string(),
1773 Builtins::kReflectIsExtensible, 1, true);
1774 SimpleInstallFunction(reflect, factory->ownKeys_string(),
1775 Builtins::kReflectOwnKeys, 1, true);
1776 SimpleInstallFunction(reflect, factory->preventExtensions_string(),
1777 Builtins::kReflectPreventExtensions, 1, true);
1778 SimpleInstallFunction(reflect, factory->set_string(), Builtins::kReflectSet,
1779 3, false);
1780 SimpleInstallFunction(reflect, factory->setPrototypeOf_string(),
1781 Builtins::kReflectSetPrototypeOf, 2, true);
1782 }
1783
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001784 { // --- B o u n d F u n c t i o n
1785 Handle<Map> map =
1786 factory->NewMap(JS_BOUND_FUNCTION_TYPE, JSBoundFunction::kSize);
1787 map->set_is_callable();
1788 Map::SetPrototype(map, empty_function);
1789
1790 PropertyAttributes roc_attribs =
1791 static_cast<PropertyAttributes>(DONT_ENUM | READ_ONLY);
1792 Map::EnsureDescriptorSlack(map, 2);
1793
Ben Murdochc5610432016-08-08 18:44:38 +01001794 Handle<AccessorInfo> bound_length =
1795 Accessors::BoundFunctionLengthInfo(isolate, roc_attribs);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001796 { // length
Ben Murdochc5610432016-08-08 18:44:38 +01001797 AccessorConstantDescriptor d(factory->length_string(), bound_length,
1798 roc_attribs);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001799 map->AppendDescriptor(&d);
1800 }
Ben Murdochc5610432016-08-08 18:44:38 +01001801 Handle<AccessorInfo> bound_name =
1802 Accessors::BoundFunctionNameInfo(isolate, roc_attribs);
1803 { // length
1804 AccessorConstantDescriptor d(factory->name_string(), bound_name,
1805 roc_attribs);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001806 map->AppendDescriptor(&d);
1807 }
Ben Murdochc5610432016-08-08 18:44:38 +01001808 map->SetInObjectProperties(0);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001809 native_context()->set_bound_function_without_constructor_map(*map);
1810
1811 map = Map::Copy(map, "IsConstructor");
Ben Murdoch097c5b22016-05-18 11:27:45 +01001812 map->set_is_constructor(true);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001813 native_context()->set_bound_function_with_constructor_map(*map);
1814 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001815
1816 { // --- sloppy arguments map
Steve Blocka7e24c12009-10-30 11:49:00 +00001817 // Make sure we can recognize argument objects at runtime.
1818 // This is done by introducing an anonymous function with
1819 // class_name equals 'Arguments'.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001820 Handle<String> arguments_string = factory->Arguments_string();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001821 Handle<Code> code = isolate->builtins()->Illegal();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001822 Handle<JSFunction> function = factory->NewFunctionWithoutPrototype(
1823 arguments_string, code);
1824 function->shared()->set_instance_class_name(*arguments_string);
Steve Blocka7e24c12009-10-30 11:49:00 +00001825
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001826 Handle<Map> map = factory->NewMap(
Ben Murdoch097c5b22016-05-18 11:27:45 +01001827 JS_OBJECT_TYPE, JSSloppyArgumentsObject::kSize, FAST_ELEMENTS);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001828 // Create the descriptor array for the arguments object.
1829 Map::EnsureDescriptorSlack(map, 2);
Steve Blocka7e24c12009-10-30 11:49:00 +00001830
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001831 { // length
Ben Murdoch097c5b22016-05-18 11:27:45 +01001832 DataDescriptor d(factory->length_string(),
1833 JSSloppyArgumentsObject::kLengthIndex, DONT_ENUM,
1834 Representation::Tagged());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001835 map->AppendDescriptor(&d);
1836 }
1837 { // callee
Ben Murdoch097c5b22016-05-18 11:27:45 +01001838 DataDescriptor d(factory->callee_string(),
1839 JSSloppyArgumentsObject::kCalleeIndex, DONT_ENUM,
1840 Representation::Tagged());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001841 map->AppendDescriptor(&d);
1842 }
1843 // @@iterator method is added later.
Steve Blocka7e24c12009-10-30 11:49:00 +00001844
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001845 map->SetInObjectProperties(2);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001846 native_context()->set_sloppy_arguments_map(*map);
Steve Blocka7e24c12009-10-30 11:49:00 +00001847
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001848 DCHECK(!function->has_initial_map());
1849 JSFunction::SetInitialMap(function, map,
1850 isolate->initial_object_prototype());
Steve Blocka7e24c12009-10-30 11:49:00 +00001851
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001852 DCHECK(!map->is_dictionary_map());
1853 DCHECK(IsFastObjectElementsKind(map->elements_kind()));
Steve Block44f0eee2011-05-26 01:26:41 +01001854 }
1855
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001856 { // --- fast and slow aliased arguments map
1857 Handle<Map> map = isolate->sloppy_arguments_map();
1858 map = Map::Copy(map, "FastAliasedArguments");
1859 map->set_elements_kind(FAST_SLOPPY_ARGUMENTS_ELEMENTS);
1860 DCHECK_EQ(2, map->GetInObjectProperties());
1861 native_context()->set_fast_aliased_arguments_map(*map);
1862
1863 map = Map::Copy(map, "SlowAliasedArguments");
1864 map->set_elements_kind(SLOW_SLOPPY_ARGUMENTS_ELEMENTS);
1865 DCHECK_EQ(2, map->GetInObjectProperties());
1866 native_context()->set_slow_aliased_arguments_map(*map);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001867 }
1868
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001869 { // --- strict mode arguments map
Steve Block44f0eee2011-05-26 01:26:41 +01001870 const PropertyAttributes attributes =
1871 static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE | READ_ONLY);
1872
1873 // Create the ThrowTypeError functions.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001874 Handle<AccessorPair> callee = factory->NewAccessorPair();
1875 Handle<AccessorPair> caller = factory->NewAccessorPair();
Steve Block44f0eee2011-05-26 01:26:41 +01001876
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001877 Handle<JSFunction> poison = GetStrictArgumentsPoisonFunction();
Steve Block44f0eee2011-05-26 01:26:41 +01001878
1879 // Install the ThrowTypeError functions.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001880 callee->set_getter(*poison);
1881 callee->set_setter(*poison);
1882 caller->set_getter(*poison);
1883 caller->set_setter(*poison);
Steve Block44f0eee2011-05-26 01:26:41 +01001884
1885 // Create the map. Allocate one in-object field for length.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001886 Handle<Map> map = factory->NewMap(
Ben Murdoch097c5b22016-05-18 11:27:45 +01001887 JS_OBJECT_TYPE, JSStrictArgumentsObject::kSize, FAST_ELEMENTS);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001888 // Create the descriptor array for the arguments object.
1889 Map::EnsureDescriptorSlack(map, 3);
1890
1891 { // length
Ben Murdoch097c5b22016-05-18 11:27:45 +01001892 DataDescriptor d(factory->length_string(),
1893 JSStrictArgumentsObject::kLengthIndex, DONT_ENUM,
1894 Representation::Tagged());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001895 map->AppendDescriptor(&d);
1896 }
1897 { // callee
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001898 AccessorConstantDescriptor d(factory->callee_string(), callee,
1899 attributes);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001900 map->AppendDescriptor(&d);
1901 }
1902 { // caller
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001903 AccessorConstantDescriptor d(factory->caller_string(), caller,
1904 attributes);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001905 map->AppendDescriptor(&d);
1906 }
1907 // @@iterator method is added later.
1908
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001909 DCHECK_EQ(native_context()->object_function()->prototype(),
1910 *isolate->initial_object_prototype());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001911 Map::SetPrototype(map, isolate->initial_object_prototype());
1912 map->SetInObjectProperties(1);
Steve Block44f0eee2011-05-26 01:26:41 +01001913
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001914 // Copy constructor from the sloppy arguments boilerplate.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001915 map->SetConstructor(
1916 native_context()->sloppy_arguments_map()->GetConstructor());
Steve Block44f0eee2011-05-26 01:26:41 +01001917
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001918 native_context()->set_strict_arguments_map(*map);
Steve Block44f0eee2011-05-26 01:26:41 +01001919
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001920 DCHECK(!map->is_dictionary_map());
1921 DCHECK(IsFastObjectElementsKind(map->elements_kind()));
Steve Blocka7e24c12009-10-30 11:49:00 +00001922 }
1923
1924 { // --- context extension
1925 // Create a function for the context extension objects.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001926 Handle<Code> code = isolate->builtins()->Illegal();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001927 Handle<JSFunction> context_extension_fun = factory->NewFunction(
1928 factory->empty_string(), code, JS_CONTEXT_EXTENSION_OBJECT_TYPE,
1929 JSObject::kHeaderSize);
Steve Blocka7e24c12009-10-30 11:49:00 +00001930
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001931 Handle<String> name = factory->InternalizeOneByteString(
1932 STATIC_CHAR_VECTOR("context_extension"));
Steve Blocka7e24c12009-10-30 11:49:00 +00001933 context_extension_fun->shared()->set_instance_class_name(*name);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001934 native_context()->set_context_extension_function(*context_extension_fun);
Steve Blocka7e24c12009-10-30 11:49:00 +00001935 }
1936
1937
1938 {
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001939 // Set up the call-as-function delegate.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001940 Handle<Code> code = isolate->builtins()->HandleApiCallAsFunction();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001941 Handle<JSFunction> delegate = factory->NewFunction(
1942 factory->empty_string(), code, JS_OBJECT_TYPE, JSObject::kHeaderSize);
1943 native_context()->set_call_as_function_delegate(*delegate);
Steve Blocka7e24c12009-10-30 11:49:00 +00001944 delegate->shared()->DontAdaptArguments();
1945 }
1946
1947 {
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001948 // Set up the call-as-constructor delegate.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001949 Handle<Code> code = isolate->builtins()->HandleApiCallAsConstructor();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001950 Handle<JSFunction> delegate = factory->NewFunction(
1951 factory->empty_string(), code, JS_OBJECT_TYPE, JSObject::kHeaderSize);
1952 native_context()->set_call_as_constructor_delegate(*delegate);
Steve Blocka7e24c12009-10-30 11:49:00 +00001953 delegate->shared()->DontAdaptArguments();
1954 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001955} // NOLINT(readability/fn_size)
Steve Blocka7e24c12009-10-30 11:49:00 +00001956
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001957
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001958void Genesis::InstallTypedArray(const char* name, ElementsKind elements_kind,
1959 Handle<JSFunction>* fun) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001960 Handle<JSObject> global = Handle<JSObject>(native_context()->global_object());
1961 Handle<JSFunction> result = InstallFunction(
1962 global, name, JS_TYPED_ARRAY_TYPE, JSTypedArray::kSize,
1963 isolate()->initial_object_prototype(), Builtins::kIllegal);
1964
1965 Handle<Map> initial_map = isolate()->factory()->NewMap(
1966 JS_TYPED_ARRAY_TYPE,
1967 JSTypedArray::kSizeWithInternalFields,
1968 elements_kind);
1969 JSFunction::SetInitialMap(result, initial_map,
1970 handle(initial_map->prototype(), isolate()));
1971 *fun = result;
Steve Blocka7e24c12009-10-30 11:49:00 +00001972}
1973
1974
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001975void Genesis::InitializeExperimentalGlobal() {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001976#define FEATURE_INITIALIZE_GLOBAL(id, descr) InitializeGlobal_##id();
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001977
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001978 HARMONY_INPROGRESS(FEATURE_INITIALIZE_GLOBAL)
1979 HARMONY_STAGED(FEATURE_INITIALIZE_GLOBAL)
1980 HARMONY_SHIPPING(FEATURE_INITIALIZE_GLOBAL)
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001981 FEATURE_INITIALIZE_GLOBAL(promise_extra, "")
Ben Murdochc5610432016-08-08 18:44:38 +01001982 FEATURE_INITIALIZE_GLOBAL(intl_extra, "")
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001983#undef FEATURE_INITIALIZE_GLOBAL
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001984}
1985
1986
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001987bool Bootstrapper::CompileBuiltin(Isolate* isolate, int index) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001988 Vector<const char> name = Natives::GetScriptName(index);
Steve Block44f0eee2011-05-26 01:26:41 +01001989 Handle<String> source_code =
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001990 isolate->bootstrapper()->SourceLookup<Natives>(index);
1991
1992 // We pass in extras_utils so that builtin code can set it up for later use
1993 // by actual extras code, compiled with CompileExtraBuiltin.
1994 Handle<Object> global = isolate->global_object();
1995 Handle<Object> utils = isolate->natives_utils_object();
1996 Handle<Object> extras_utils = isolate->extras_utils_object();
1997 Handle<Object> args[] = {global, utils, extras_utils};
1998
1999 return Bootstrapper::CompileNative(isolate, name, source_code,
Ben Murdoch097c5b22016-05-18 11:27:45 +01002000 arraysize(args), args, NATIVES_CODE);
Ben Murdoch257744e2011-11-30 15:57:28 +00002001}
2002
2003
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002004bool Bootstrapper::CompileExperimentalBuiltin(Isolate* isolate, int index) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002005 HandleScope scope(isolate);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002006 Vector<const char> name = ExperimentalNatives::GetScriptName(index);
2007 Handle<String> source_code =
2008 isolate->bootstrapper()->SourceLookup<ExperimentalNatives>(index);
2009 Handle<Object> global = isolate->global_object();
2010 Handle<Object> utils = isolate->natives_utils_object();
2011 Handle<Object> args[] = {global, utils};
2012 return Bootstrapper::CompileNative(isolate, name, source_code,
Ben Murdoch097c5b22016-05-18 11:27:45 +01002013 arraysize(args), args, NATIVES_CODE);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002014}
2015
2016
2017bool Bootstrapper::CompileExtraBuiltin(Isolate* isolate, int index) {
2018 HandleScope scope(isolate);
2019 Vector<const char> name = ExtraNatives::GetScriptName(index);
2020 Handle<String> source_code =
2021 isolate->bootstrapper()->SourceLookup<ExtraNatives>(index);
2022 Handle<Object> global = isolate->global_object();
2023 Handle<Object> binding = isolate->extras_binding_object();
2024 Handle<Object> extras_utils = isolate->extras_utils_object();
2025 Handle<Object> args[] = {global, binding, extras_utils};
2026 return Bootstrapper::CompileNative(isolate, name, source_code,
Ben Murdoch097c5b22016-05-18 11:27:45 +01002027 arraysize(args), args, EXTENSION_CODE);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002028}
2029
2030
2031bool Bootstrapper::CompileExperimentalExtraBuiltin(Isolate* isolate,
2032 int index) {
2033 HandleScope scope(isolate);
2034 Vector<const char> name = ExperimentalExtraNatives::GetScriptName(index);
2035 Handle<String> source_code =
2036 isolate->bootstrapper()->SourceLookup<ExperimentalExtraNatives>(index);
2037 Handle<Object> global = isolate->global_object();
2038 Handle<Object> binding = isolate->extras_binding_object();
2039 Handle<Object> extras_utils = isolate->extras_utils_object();
2040 Handle<Object> args[] = {global, binding, extras_utils};
2041 return Bootstrapper::CompileNative(isolate, name, source_code,
Ben Murdoch097c5b22016-05-18 11:27:45 +01002042 arraysize(args), args, EXTENSION_CODE);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002043}
2044
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002045bool Bootstrapper::CompileNative(Isolate* isolate, Vector<const char> name,
2046 Handle<String> source, int argc,
Ben Murdoch097c5b22016-05-18 11:27:45 +01002047 Handle<Object> argv[],
2048 NativesFlag natives_flag) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002049 SuppressDebug compiling_natives(isolate->debug());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002050 // During genesis, the boilerplate for stack overflow won't work until the
2051 // environment has been at least partially initialized. Add a stack check
2052 // before entering JS code to catch overflow early.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002053 StackLimitCheck check(isolate);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002054 if (check.JsHasOverflowed(1 * KB)) {
2055 isolate->StackOverflow();
2056 return false;
2057 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002058
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002059 Handle<Context> context(isolate->context());
2060
2061 Handle<String> script_name =
2062 isolate->factory()->NewStringFromUtf8(name).ToHandleChecked();
Ben Murdochda12d292016-06-02 14:46:10 +01002063 Handle<SharedFunctionInfo> function_info =
2064 Compiler::GetSharedFunctionInfoForScript(
2065 source, script_name, 0, 0, ScriptOriginOptions(), Handle<Object>(),
2066 context, NULL, NULL, ScriptCompiler::kNoCompileOptions, natives_flag,
2067 false);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002068 if (function_info.is_null()) return false;
2069
2070 DCHECK(context->IsNativeContext());
2071
2072 Handle<JSFunction> fun =
2073 isolate->factory()->NewFunctionFromSharedFunctionInfo(function_info,
2074 context);
2075 Handle<Object> receiver = isolate->factory()->undefined_value();
2076
2077 // For non-extension scripts, run script to get the function wrapper.
2078 Handle<Object> wrapper;
2079 if (!Execution::Call(isolate, fun, receiver, 0, NULL).ToHandle(&wrapper)) {
2080 return false;
2081 }
2082 // Then run the function wrapper.
2083 return !Execution::Call(isolate, Handle<JSFunction>::cast(wrapper), receiver,
2084 argc, argv).is_null();
Steve Blocka7e24c12009-10-30 11:49:00 +00002085}
2086
2087
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002088bool Genesis::CallUtilsFunction(Isolate* isolate, const char* name) {
2089 Handle<JSObject> utils =
2090 Handle<JSObject>::cast(isolate->natives_utils_object());
2091 Handle<String> name_string =
2092 isolate->factory()->NewStringFromAsciiChecked(name);
2093 Handle<Object> fun = JSObject::GetDataProperty(utils, name_string);
2094 Handle<Object> receiver = isolate->factory()->undefined_value();
2095 Handle<Object> args[] = {utils};
2096 return !Execution::Call(isolate, fun, receiver, 1, args).is_null();
2097}
2098
2099
2100bool Genesis::CompileExtension(Isolate* isolate, v8::Extension* extension) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002101 Factory* factory = isolate->factory();
2102 HandleScope scope(isolate);
Steve Block6ded16b2010-05-10 14:33:55 +01002103 Handle<SharedFunctionInfo> function_info;
Steve Blocka7e24c12009-10-30 11:49:00 +00002104
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002105 Handle<String> source =
2106 isolate->factory()
2107 ->NewExternalStringFromOneByte(extension->source())
2108 .ToHandleChecked();
2109 DCHECK(source->IsOneByteRepresentation());
2110
Steve Blocka7e24c12009-10-30 11:49:00 +00002111 // If we can't find the function in the cache, we compile a new
2112 // function and insert it into the cache.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002113 Vector<const char> name = CStrVector(extension->name());
2114 SourceCodeCache* cache = isolate->bootstrapper()->extensions_cache();
2115 Handle<Context> context(isolate->context());
2116 DCHECK(context->IsNativeContext());
2117
2118 if (!cache->Lookup(name, &function_info)) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002119 Handle<String> script_name =
2120 factory->NewStringFromUtf8(name).ToHandleChecked();
Ben Murdochda12d292016-06-02 14:46:10 +01002121 function_info = Compiler::GetSharedFunctionInfoForScript(
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002122 source, script_name, 0, 0, ScriptOriginOptions(), Handle<Object>(),
2123 context, extension, NULL, ScriptCompiler::kNoCompileOptions,
Ben Murdoch097c5b22016-05-18 11:27:45 +01002124 EXTENSION_CODE, false);
Steve Block6ded16b2010-05-10 14:33:55 +01002125 if (function_info.is_null()) return false;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002126 cache->Add(name, function_info);
Steve Blocka7e24c12009-10-30 11:49:00 +00002127 }
2128
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002129 // Set up the function context. Conceptually, we should clone the
Steve Blocka7e24c12009-10-30 11:49:00 +00002130 // function before overwriting the context but since we're in a
2131 // single-threaded environment it is not strictly necessary.
Steve Blocka7e24c12009-10-30 11:49:00 +00002132 Handle<JSFunction> fun =
Steve Block44f0eee2011-05-26 01:26:41 +01002133 factory->NewFunctionFromSharedFunctionInfo(function_info, context);
Steve Blocka7e24c12009-10-30 11:49:00 +00002134
Leon Clarke4515c472010-02-03 11:58:03 +00002135 // Call function using either the runtime object or the global
Steve Blocka7e24c12009-10-30 11:49:00 +00002136 // object as the receiver. Provide no parameters.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002137 Handle<Object> receiver = isolate->global_object();
2138 return !Execution::Call(isolate, fun, receiver, 0, NULL).is_null();
Steve Blocka7e24c12009-10-30 11:49:00 +00002139}
2140
2141
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002142static Handle<JSObject> ResolveBuiltinIdHolder(Handle<Context> native_context,
2143 const char* holder_expr) {
2144 Isolate* isolate = native_context->GetIsolate();
2145 Factory* factory = isolate->factory();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002146 Handle<JSGlobalObject> global(native_context->global_object());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002147 const char* period_pos = strchr(holder_expr, '.');
2148 if (period_pos == NULL) {
2149 return Handle<JSObject>::cast(
2150 Object::GetPropertyOrElement(
2151 global, factory->InternalizeUtf8String(holder_expr))
2152 .ToHandleChecked());
2153 }
2154 const char* inner = period_pos + 1;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002155 DCHECK(!strchr(inner, '.'));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002156 Vector<const char> property(holder_expr,
2157 static_cast<int>(period_pos - holder_expr));
2158 Handle<String> property_string = factory->InternalizeUtf8String(property);
2159 DCHECK(!property_string.is_null());
2160 Handle<JSObject> object = Handle<JSObject>::cast(
Ben Murdochda12d292016-06-02 14:46:10 +01002161 JSReceiver::GetProperty(global, property_string).ToHandleChecked());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002162 if (strcmp("prototype", inner) == 0) {
2163 Handle<JSFunction> function = Handle<JSFunction>::cast(object);
2164 return Handle<JSObject>(JSObject::cast(function->prototype()));
2165 }
2166 Handle<String> inner_string = factory->InternalizeUtf8String(inner);
2167 DCHECK(!inner_string.is_null());
2168 Handle<Object> value =
Ben Murdochda12d292016-06-02 14:46:10 +01002169 JSReceiver::GetProperty(object, inner_string).ToHandleChecked();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002170 return Handle<JSObject>::cast(value);
2171}
Steve Blocka7e24c12009-10-30 11:49:00 +00002172
Ben Murdoch097c5b22016-05-18 11:27:45 +01002173void Genesis::ConfigureUtilsObject(GlobalContextType context_type) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002174 switch (context_type) {
2175 // We still need the utils object to find debug functions.
2176 case DEBUG_CONTEXT:
2177 return;
2178 // Expose the natives in global if a valid name for it is specified.
2179 case FULL_CONTEXT: {
2180 // We still need the utils object after deserialization.
2181 if (isolate()->serializer_enabled()) return;
2182 if (FLAG_expose_natives_as == NULL) break;
2183 if (strlen(FLAG_expose_natives_as) == 0) break;
2184 HandleScope scope(isolate());
2185 Handle<String> natives_key =
2186 factory()->InternalizeUtf8String(FLAG_expose_natives_as);
2187 uint32_t dummy_index;
2188 if (natives_key->AsArrayIndex(&dummy_index)) break;
2189 Handle<Object> utils = isolate()->natives_utils_object();
2190 Handle<JSObject> global = isolate()->global_object();
2191 JSObject::AddProperty(global, natives_key, utils, DONT_ENUM);
2192 break;
2193 }
2194 case THIN_CONTEXT:
2195 break;
Ben Murdoch257744e2011-11-30 15:57:28 +00002196 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002197
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002198 // The utils object can be removed for cases that reach this point.
2199 native_context()->set_natives_utils_object(heap()->undefined_value());
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002200}
2201
2202
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002203void Bootstrapper::ExportFromRuntime(Isolate* isolate,
2204 Handle<JSObject> container) {
2205 Factory* factory = isolate->factory();
2206 HandleScope scope(isolate);
2207 Handle<Context> native_context = isolate->native_context();
2208#define EXPORT_PRIVATE_SYMBOL(NAME) \
2209 Handle<String> NAME##_name = factory->NewStringFromAsciiChecked(#NAME); \
2210 JSObject::AddProperty(container, NAME##_name, factory->NAME(), NONE);
2211 PRIVATE_SYMBOL_LIST(EXPORT_PRIVATE_SYMBOL)
2212#undef EXPORT_PRIVATE_SYMBOL
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002213
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002214#define EXPORT_PUBLIC_SYMBOL(NAME, DESCRIPTION) \
2215 Handle<String> NAME##_name = factory->NewStringFromAsciiChecked(#NAME); \
2216 JSObject::AddProperty(container, NAME##_name, factory->NAME(), NONE);
2217 PUBLIC_SYMBOL_LIST(EXPORT_PUBLIC_SYMBOL)
2218 WELL_KNOWN_SYMBOL_LIST(EXPORT_PUBLIC_SYMBOL)
2219#undef EXPORT_PUBLIC_SYMBOL
2220
2221 {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002222 Handle<JSFunction> to_string = InstallFunction(
2223 container, "object_to_string", JS_OBJECT_TYPE, JSObject::kHeaderSize,
2224 MaybeHandle<JSObject>(), Builtins::kObjectProtoToString);
2225 to_string->shared()->DontAdaptArguments();
2226 to_string->shared()->set_length(0);
2227 native_context->set_object_to_string(*to_string);
2228 }
2229
2230 Handle<JSObject> iterator_prototype;
2231
2232 {
2233 PrototypeIterator iter(native_context->generator_object_prototype_map());
2234 iter.Advance(); // Advance to the prototype of generator_object_prototype.
2235 iterator_prototype = Handle<JSObject>(iter.GetCurrent<JSObject>());
2236
2237 JSObject::AddProperty(container,
2238 factory->InternalizeUtf8String("IteratorPrototype"),
2239 iterator_prototype, NONE);
2240 }
2241
2242 {
2243 PrototypeIterator iter(native_context->sloppy_generator_function_map());
2244 Handle<JSObject> generator_function_prototype(iter.GetCurrent<JSObject>());
2245
2246 JSObject::AddProperty(
2247 container, factory->InternalizeUtf8String("GeneratorFunctionPrototype"),
2248 generator_function_prototype, NONE);
2249
2250 static const bool kUseStrictFunctionMap = true;
2251 Handle<JSFunction> generator_function_function = InstallFunction(
2252 container, "GeneratorFunction", JS_FUNCTION_TYPE, JSFunction::kSize,
2253 generator_function_prototype, Builtins::kGeneratorFunctionConstructor,
2254 kUseStrictFunctionMap);
2255 generator_function_function->set_prototype_or_initial_map(
2256 native_context->sloppy_generator_function_map());
2257 generator_function_function->shared()->DontAdaptArguments();
2258 generator_function_function->shared()->set_construct_stub(
2259 *isolate->builtins()->GeneratorFunctionConstructor());
2260 generator_function_function->shared()->set_length(1);
2261 InstallWithIntrinsicDefaultProto(
2262 isolate, generator_function_function,
2263 Context::GENERATOR_FUNCTION_FUNCTION_INDEX);
2264
Ben Murdochc5610432016-08-08 18:44:38 +01002265 SetObjectPrototype(generator_function_function,
2266 isolate->function_function());
2267 JSObject::AddProperty(
2268 generator_function_prototype, factory->constructor_string(),
2269 generator_function_function,
2270 static_cast<PropertyAttributes>(DONT_ENUM | READ_ONLY));
2271
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002272 native_context->sloppy_generator_function_map()->SetConstructor(
2273 *generator_function_function);
2274 native_context->strict_generator_function_map()->SetConstructor(
2275 *generator_function_function);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002276 }
2277
2278 { // -- S e t I t e r a t o r
2279 Handle<JSObject> set_iterator_prototype =
2280 isolate->factory()->NewJSObject(isolate->object_function(), TENURED);
2281 SetObjectPrototype(set_iterator_prototype, iterator_prototype);
2282 Handle<JSFunction> set_iterator_function = InstallFunction(
2283 container, "SetIterator", JS_SET_ITERATOR_TYPE, JSSetIterator::kSize,
2284 set_iterator_prototype, Builtins::kIllegal);
2285 native_context->set_set_iterator_map(set_iterator_function->initial_map());
2286 }
2287
2288 { // -- M a p I t e r a t o r
2289 Handle<JSObject> map_iterator_prototype =
2290 isolate->factory()->NewJSObject(isolate->object_function(), TENURED);
2291 SetObjectPrototype(map_iterator_prototype, iterator_prototype);
2292 Handle<JSFunction> map_iterator_function = InstallFunction(
2293 container, "MapIterator", JS_MAP_ITERATOR_TYPE, JSMapIterator::kSize,
2294 map_iterator_prototype, Builtins::kIllegal);
2295 native_context->set_map_iterator_map(map_iterator_function->initial_map());
2296 }
2297
2298 { // -- S c r i p t
2299 // Builtin functions for Script.
2300 Handle<JSFunction> script_fun = InstallFunction(
2301 container, "Script", JS_VALUE_TYPE, JSValue::kSize,
2302 isolate->initial_object_prototype(), Builtins::kIllegal);
2303 Handle<JSObject> prototype =
2304 factory->NewJSObject(isolate->object_function(), TENURED);
2305 Accessors::FunctionSetPrototype(script_fun, prototype).Assert();
2306 native_context->set_script_function(*script_fun);
2307
2308 Handle<Map> script_map = Handle<Map>(script_fun->initial_map());
2309 Map::EnsureDescriptorSlack(script_map, 15);
2310
2311 PropertyAttributes attribs =
2312 static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE | READ_ONLY);
2313
2314 Handle<AccessorInfo> script_column =
2315 Accessors::ScriptColumnOffsetInfo(isolate, attribs);
2316 {
2317 AccessorConstantDescriptor d(
2318 Handle<Name>(Name::cast(script_column->name())), script_column,
2319 attribs);
2320 script_map->AppendDescriptor(&d);
2321 }
2322
2323 Handle<AccessorInfo> script_id = Accessors::ScriptIdInfo(isolate, attribs);
2324 {
2325 AccessorConstantDescriptor d(Handle<Name>(Name::cast(script_id->name())),
2326 script_id, attribs);
2327 script_map->AppendDescriptor(&d);
2328 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002329
2330
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002331 Handle<AccessorInfo> script_name =
2332 Accessors::ScriptNameInfo(isolate, attribs);
2333 {
2334 AccessorConstantDescriptor d(
2335 Handle<Name>(Name::cast(script_name->name())), script_name, attribs);
2336 script_map->AppendDescriptor(&d);
2337 }
2338
2339 Handle<AccessorInfo> script_line =
2340 Accessors::ScriptLineOffsetInfo(isolate, attribs);
2341 {
2342 AccessorConstantDescriptor d(
2343 Handle<Name>(Name::cast(script_line->name())), script_line, attribs);
2344 script_map->AppendDescriptor(&d);
2345 }
2346
2347 Handle<AccessorInfo> script_source =
2348 Accessors::ScriptSourceInfo(isolate, attribs);
2349 {
2350 AccessorConstantDescriptor d(
2351 Handle<Name>(Name::cast(script_source->name())), script_source,
2352 attribs);
2353 script_map->AppendDescriptor(&d);
2354 }
2355
2356 Handle<AccessorInfo> script_type =
2357 Accessors::ScriptTypeInfo(isolate, attribs);
2358 {
2359 AccessorConstantDescriptor d(
2360 Handle<Name>(Name::cast(script_type->name())), script_type, attribs);
2361 script_map->AppendDescriptor(&d);
2362 }
2363
2364 Handle<AccessorInfo> script_compilation_type =
2365 Accessors::ScriptCompilationTypeInfo(isolate, attribs);
2366 {
2367 AccessorConstantDescriptor d(
2368 Handle<Name>(Name::cast(script_compilation_type->name())),
2369 script_compilation_type, attribs);
2370 script_map->AppendDescriptor(&d);
2371 }
2372
2373 Handle<AccessorInfo> script_line_ends =
2374 Accessors::ScriptLineEndsInfo(isolate, attribs);
2375 {
2376 AccessorConstantDescriptor d(
2377 Handle<Name>(Name::cast(script_line_ends->name())), script_line_ends,
2378 attribs);
2379 script_map->AppendDescriptor(&d);
2380 }
2381
2382 Handle<AccessorInfo> script_context_data =
2383 Accessors::ScriptContextDataInfo(isolate, attribs);
2384 {
2385 AccessorConstantDescriptor d(
2386 Handle<Name>(Name::cast(script_context_data->name())),
2387 script_context_data, attribs);
2388 script_map->AppendDescriptor(&d);
2389 }
2390
2391 Handle<AccessorInfo> script_eval_from_script =
2392 Accessors::ScriptEvalFromScriptInfo(isolate, attribs);
2393 {
2394 AccessorConstantDescriptor d(
2395 Handle<Name>(Name::cast(script_eval_from_script->name())),
2396 script_eval_from_script, attribs);
2397 script_map->AppendDescriptor(&d);
2398 }
2399
2400 Handle<AccessorInfo> script_eval_from_script_position =
2401 Accessors::ScriptEvalFromScriptPositionInfo(isolate, attribs);
2402 {
2403 AccessorConstantDescriptor d(
2404 Handle<Name>(Name::cast(script_eval_from_script_position->name())),
2405 script_eval_from_script_position, attribs);
2406 script_map->AppendDescriptor(&d);
2407 }
2408
2409 Handle<AccessorInfo> script_eval_from_function_name =
2410 Accessors::ScriptEvalFromFunctionNameInfo(isolate, attribs);
2411 {
2412 AccessorConstantDescriptor d(
2413 Handle<Name>(Name::cast(script_eval_from_function_name->name())),
2414 script_eval_from_function_name, attribs);
2415 script_map->AppendDescriptor(&d);
2416 }
2417
2418 Handle<AccessorInfo> script_source_url =
2419 Accessors::ScriptSourceUrlInfo(isolate, attribs);
2420 {
2421 AccessorConstantDescriptor d(
2422 Handle<Name>(Name::cast(script_source_url->name())),
2423 script_source_url, attribs);
2424 script_map->AppendDescriptor(&d);
2425 }
2426
2427 Handle<AccessorInfo> script_source_mapping_url =
2428 Accessors::ScriptSourceMappingUrlInfo(isolate, attribs);
2429 {
2430 AccessorConstantDescriptor d(
2431 Handle<Name>(Name::cast(script_source_mapping_url->name())),
2432 script_source_mapping_url, attribs);
2433 script_map->AppendDescriptor(&d);
2434 }
2435
2436 Handle<AccessorInfo> script_is_embedder_debug_script =
2437 Accessors::ScriptIsEmbedderDebugScriptInfo(isolate, attribs);
2438 {
2439 AccessorConstantDescriptor d(
2440 Handle<Name>(Name::cast(script_is_embedder_debug_script->name())),
2441 script_is_embedder_debug_script, attribs);
2442 script_map->AppendDescriptor(&d);
2443 }
Ben Murdochc5610432016-08-08 18:44:38 +01002444
2445 {
2446 PrototypeIterator iter(native_context->sloppy_async_function_map());
2447 Handle<JSObject> async_function_prototype(iter.GetCurrent<JSObject>());
2448
2449 static const bool kUseStrictFunctionMap = true;
2450 Handle<JSFunction> async_function_constructor = InstallFunction(
2451 container, "AsyncFunction", JS_FUNCTION_TYPE, JSFunction::kSize,
2452 async_function_prototype, Builtins::kAsyncFunctionConstructor,
2453 kUseStrictFunctionMap);
2454 async_function_constructor->set_prototype_or_initial_map(
2455 native_context->sloppy_async_function_map());
2456 async_function_constructor->shared()->DontAdaptArguments();
2457 async_function_constructor->shared()->set_construct_stub(
2458 *isolate->builtins()->AsyncFunctionConstructor());
2459 async_function_constructor->shared()->set_length(1);
2460 InstallWithIntrinsicDefaultProto(isolate, async_function_constructor,
2461 Context::ASYNC_FUNCTION_FUNCTION_INDEX);
2462
2463 JSObject::AddProperty(
2464 async_function_prototype, factory->constructor_string(),
2465 async_function_constructor,
2466 static_cast<PropertyAttributes>(DONT_ENUM | READ_ONLY));
2467
2468 JSFunction::SetPrototype(async_function_constructor,
2469 async_function_prototype);
2470
2471 Handle<JSFunction> async_function_next =
2472 SimpleInstallFunction(container, "AsyncFunctionNext",
2473 Builtins::kGeneratorPrototypeNext, 2, false);
2474 Handle<JSFunction> async_function_throw =
2475 SimpleInstallFunction(container, "AsyncFunctionThrow",
2476 Builtins::kGeneratorPrototypeThrow, 2, false);
2477 async_function_next->shared()->set_native(true);
2478 async_function_throw->shared()->set_native(true);
2479 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002480 }
Ben Murdoch257744e2011-11-30 15:57:28 +00002481}
2482
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002483
2484void Bootstrapper::ExportExperimentalFromRuntime(Isolate* isolate,
2485 Handle<JSObject> container) {
2486 HandleScope scope(isolate);
2487
2488#define INITIALIZE_FLAG(FLAG) \
2489 { \
2490 Handle<String> name = \
2491 isolate->factory()->NewStringFromAsciiChecked(#FLAG); \
2492 JSObject::AddProperty(container, name, \
2493 isolate->factory()->ToBoolean(FLAG), NONE); \
2494 }
2495
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002496 INITIALIZE_FLAG(FLAG_harmony_species)
Ben Murdochc5610432016-08-08 18:44:38 +01002497 INITIALIZE_FLAG(FLAG_intl_extra)
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002498
2499#undef INITIALIZE_FLAG
2500}
2501
Steve Blocka7e24c12009-10-30 11:49:00 +00002502
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002503#define EMPTY_INITIALIZE_GLOBAL_FOR_FEATURE(id) \
2504 void Genesis::InitializeGlobal_##id() {}
2505
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002506EMPTY_INITIALIZE_GLOBAL_FOR_FEATURE(harmony_unicode_regexps)
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002507EMPTY_INITIALIZE_GLOBAL_FOR_FEATURE(harmony_do_expressions)
Ben Murdochc5610432016-08-08 18:44:38 +01002508EMPTY_INITIALIZE_GLOBAL_FOR_FEATURE(harmony_for_in)
Ben Murdoch097c5b22016-05-18 11:27:45 +01002509EMPTY_INITIALIZE_GLOBAL_FOR_FEATURE(harmony_iterator_close)
Ben Murdochda12d292016-06-02 14:46:10 +01002510EMPTY_INITIALIZE_GLOBAL_FOR_FEATURE(harmony_regexp_exec)
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002511EMPTY_INITIALIZE_GLOBAL_FOR_FEATURE(harmony_regexp_lookbehind)
Ben Murdoch097c5b22016-05-18 11:27:45 +01002512EMPTY_INITIALIZE_GLOBAL_FOR_FEATURE(harmony_regexp_property)
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002513EMPTY_INITIALIZE_GLOBAL_FOR_FEATURE(harmony_function_name)
Ben Murdoch097c5b22016-05-18 11:27:45 +01002514EMPTY_INITIALIZE_GLOBAL_FOR_FEATURE(harmony_function_sent)
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002515EMPTY_INITIALIZE_GLOBAL_FOR_FEATURE(promise_extra)
Ben Murdochc5610432016-08-08 18:44:38 +01002516EMPTY_INITIALIZE_GLOBAL_FOR_FEATURE(intl_extra)
2517EMPTY_INITIALIZE_GLOBAL_FOR_FEATURE(harmony_explicit_tailcalls)
Ben Murdoch097c5b22016-05-18 11:27:45 +01002518EMPTY_INITIALIZE_GLOBAL_FOR_FEATURE(harmony_tailcalls)
2519EMPTY_INITIALIZE_GLOBAL_FOR_FEATURE(harmony_instanceof)
Ben Murdochda12d292016-06-02 14:46:10 +01002520EMPTY_INITIALIZE_GLOBAL_FOR_FEATURE(harmony_restrictive_declarations)
2521EMPTY_INITIALIZE_GLOBAL_FOR_FEATURE(harmony_exponentiation_operator)
2522EMPTY_INITIALIZE_GLOBAL_FOR_FEATURE(harmony_string_padding)
Ben Murdochc5610432016-08-08 18:44:38 +01002523#ifdef V8_I18N_SUPPORT
2524EMPTY_INITIALIZE_GLOBAL_FOR_FEATURE(icu_case_mapping)
2525#endif
2526EMPTY_INITIALIZE_GLOBAL_FOR_FEATURE(harmony_async_await)
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002527
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002528void InstallPublicSymbol(Factory* factory, Handle<Context> native_context,
2529 const char* name, Handle<Symbol> value) {
2530 Handle<JSGlobalObject> global(
2531 JSGlobalObject::cast(native_context->global_object()));
2532 Handle<String> symbol_string = factory->InternalizeUtf8String("Symbol");
2533 Handle<JSObject> symbol = Handle<JSObject>::cast(
2534 JSObject::GetProperty(global, symbol_string).ToHandleChecked());
2535 Handle<String> name_string = factory->InternalizeUtf8String(name);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002536 PropertyAttributes attributes =
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002537 static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE | READ_ONLY);
2538 JSObject::AddProperty(symbol, name_string, value, attributes);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002539}
2540
Steve Blocka7e24c12009-10-30 11:49:00 +00002541
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002542void Genesis::InitializeGlobal_harmony_regexp_subclass() {
2543 if (!FLAG_harmony_regexp_subclass) return;
2544 InstallPublicSymbol(factory(), native_context(), "match",
2545 factory()->match_symbol());
2546 InstallPublicSymbol(factory(), native_context(), "replace",
2547 factory()->replace_symbol());
2548 InstallPublicSymbol(factory(), native_context(), "search",
2549 factory()->search_symbol());
2550 InstallPublicSymbol(factory(), native_context(), "split",
2551 factory()->split_symbol());
2552}
2553
2554
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002555void Genesis::InitializeGlobal_harmony_sharedarraybuffer() {
2556 if (!FLAG_harmony_sharedarraybuffer) return;
2557
2558 Handle<JSGlobalObject> global(native_context()->global_object());
Ben Murdochc5610432016-08-08 18:44:38 +01002559 Isolate* isolate = global->GetIsolate();
2560 Factory* factory = isolate->factory();
2561
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002562 Handle<JSFunction> shared_array_buffer_fun =
2563 InstallArrayBuffer(global, "SharedArrayBuffer");
2564 native_context()->set_shared_array_buffer_fun(*shared_array_buffer_fun);
Ben Murdochc5610432016-08-08 18:44:38 +01002565
2566 Handle<String> name = factory->InternalizeUtf8String("Atomics");
2567 Handle<JSFunction> cons = factory->NewFunction(name);
2568 JSFunction::SetInstancePrototype(
2569 cons,
2570 Handle<Object>(native_context()->initial_object_prototype(), isolate));
2571 Handle<JSObject> atomics_object = factory->NewJSObject(cons, TENURED);
2572 DCHECK(atomics_object->IsJSObject());
2573 JSObject::AddProperty(global, name, atomics_object, DONT_ENUM);
2574
2575 SimpleInstallFunction(atomics_object, factory->InternalizeUtf8String("load"),
2576 Builtins::kAtomicsLoad, 2, true);
2577 SimpleInstallFunction(atomics_object, factory->InternalizeUtf8String("store"),
2578 Builtins::kAtomicsStore, 3, true);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002579}
2580
2581
2582void Genesis::InitializeGlobal_harmony_simd() {
2583 if (!FLAG_harmony_simd) return;
2584
2585 Handle<JSGlobalObject> global(
2586 JSGlobalObject::cast(native_context()->global_object()));
2587 Isolate* isolate = global->GetIsolate();
2588 Factory* factory = isolate->factory();
2589
2590 Handle<String> name = factory->InternalizeUtf8String("SIMD");
2591 Handle<JSFunction> cons = factory->NewFunction(name);
2592 JSFunction::SetInstancePrototype(
2593 cons,
2594 Handle<Object>(native_context()->initial_object_prototype(), isolate));
2595 cons->shared()->set_instance_class_name(*name);
2596 Handle<JSObject> simd_object = factory->NewJSObject(cons, TENURED);
2597 DCHECK(simd_object->IsJSObject());
2598 JSObject::AddProperty(global, name, simd_object, DONT_ENUM);
2599
2600// Install SIMD type functions. Set the instance class names since
2601// InstallFunction only does this when we install on the JSGlobalObject.
2602#define SIMD128_INSTALL_FUNCTION(TYPE, Type, type, lane_count, lane_type) \
2603 Handle<JSFunction> type##_function = InstallFunction( \
2604 simd_object, #Type, JS_VALUE_TYPE, JSValue::kSize, \
2605 isolate->initial_object_prototype(), Builtins::kIllegal); \
2606 native_context()->set_##type##_function(*type##_function); \
2607 type##_function->shared()->set_instance_class_name(*factory->Type##_string());
2608 SIMD128_TYPES(SIMD128_INSTALL_FUNCTION)
2609#undef SIMD128_INSTALL_FUNCTION
2610}
2611
2612
Ben Murdoch097c5b22016-05-18 11:27:45 +01002613void Genesis::InitializeGlobal_harmony_object_values_entries() {
2614 if (!FLAG_harmony_object_values_entries) return;
2615
2616 Handle<JSGlobalObject> global(
2617 JSGlobalObject::cast(native_context()->global_object()));
2618 Isolate* isolate = global->GetIsolate();
2619 Factory* factory = isolate->factory();
2620
2621 Handle<JSFunction> object_function = isolate->object_function();
2622 SimpleInstallFunction(object_function, factory->entries_string(),
2623 Builtins::kObjectEntries, 1, false);
2624 SimpleInstallFunction(object_function, factory->values_string(),
2625 Builtins::kObjectValues, 1, false);
2626}
2627
2628void Genesis::InitializeGlobal_harmony_object_own_property_descriptors() {
2629 if (!FLAG_harmony_object_own_property_descriptors) return;
2630
2631 Handle<JSGlobalObject> global(
2632 JSGlobalObject::cast(native_context()->global_object()));
2633 Isolate* isolate = global->GetIsolate();
2634 Factory* factory = isolate->factory();
2635
2636 Handle<JSFunction> object_function = isolate->object_function();
2637 SimpleInstallFunction(object_function,
2638 factory->getOwnPropertyDescriptors_string(),
2639 Builtins::kObjectGetOwnPropertyDescriptors, 1, false);
2640}
2641
Ben Murdochda12d292016-06-02 14:46:10 +01002642void Genesis::InitializeGlobal_harmony_array_prototype_values() {
2643 if (!FLAG_harmony_array_prototype_values) return;
2644 Handle<JSFunction> array_constructor(native_context()->array_function());
2645 Handle<JSObject> array_prototype(
2646 JSObject::cast(array_constructor->instance_prototype()));
2647 Handle<Object> values_iterator =
2648 JSObject::GetProperty(array_prototype, factory()->iterator_symbol())
2649 .ToHandleChecked();
2650 DCHECK(values_iterator->IsJSFunction());
2651 JSObject::AddProperty(array_prototype, factory()->values_string(),
2652 values_iterator, DONT_ENUM);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002653
Ben Murdochda12d292016-06-02 14:46:10 +01002654 Handle<Object> unscopables =
2655 JSObject::GetProperty(array_prototype, factory()->unscopables_symbol())
2656 .ToHandleChecked();
2657 DCHECK(unscopables->IsJSObject());
2658 JSObject::AddProperty(Handle<JSObject>::cast(unscopables),
2659 factory()->values_string(), factory()->true_value(),
2660 NONE);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002661}
2662
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002663Handle<JSFunction> Genesis::InstallArrayBuffer(Handle<JSObject> target,
2664 const char* name) {
2665 // Setup the {prototype} with the given {name} for @@toStringTag.
2666 Handle<JSObject> prototype =
2667 factory()->NewJSObject(isolate()->object_function(), TENURED);
2668 JSObject::AddProperty(prototype, factory()->to_string_tag_symbol(),
2669 factory()->NewStringFromAsciiChecked(name),
2670 static_cast<PropertyAttributes>(DONT_ENUM | READ_ONLY));
2671
2672 // Allocate the constructor with the given {prototype}.
2673 Handle<JSFunction> array_buffer_fun =
2674 InstallFunction(target, name, JS_ARRAY_BUFFER_TYPE,
2675 JSArrayBuffer::kSizeWithInternalFields, prototype,
2676 Builtins::kArrayBufferConstructor);
2677 array_buffer_fun->shared()->set_construct_stub(
2678 *isolate()->builtins()->ArrayBufferConstructor_ConstructStub());
2679 array_buffer_fun->shared()->DontAdaptArguments();
2680 array_buffer_fun->shared()->set_length(1);
2681
2682 // Install the "constructor" property on the {prototype}.
2683 JSObject::AddProperty(prototype, factory()->constructor_string(),
2684 array_buffer_fun, DONT_ENUM);
2685
2686 SimpleInstallFunction(array_buffer_fun, factory()->isView_string(),
2687 Builtins::kArrayBufferIsView, 1, true);
2688
2689 return array_buffer_fun;
2690}
2691
2692
2693void Genesis::InitializeGlobal_harmony_species() {
2694 if (!FLAG_harmony_species) return;
2695 InstallPublicSymbol(factory(), native_context(), "species",
2696 factory()->species_symbol());
2697}
2698
2699
2700Handle<JSFunction> Genesis::InstallInternalArray(Handle<JSObject> target,
2701 const char* name,
2702 ElementsKind elements_kind) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002703 // --- I n t e r n a l A r r a y ---
2704 // An array constructor on the builtins object that works like
2705 // the public Array constructor, except that its prototype
2706 // doesn't inherit from Object.prototype.
2707 // To be used only for internal work by builtins. Instances
2708 // must not be leaked to user code.
2709 Handle<JSObject> prototype =
2710 factory()->NewJSObject(isolate()->object_function(), TENURED);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002711 Handle<JSFunction> array_function =
2712 InstallFunction(target, name, JS_ARRAY_TYPE, JSArray::kSize, prototype,
2713 Builtins::kInternalArrayCode);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002714
2715 InternalArrayConstructorStub internal_array_constructor_stub(isolate());
2716 Handle<Code> code = internal_array_constructor_stub.GetCode();
2717 array_function->shared()->set_construct_stub(*code);
2718 array_function->shared()->DontAdaptArguments();
2719
2720 Handle<Map> original_map(array_function->initial_map());
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002721 Handle<Map> initial_map = Map::Copy(original_map, "InternalArray");
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002722 initial_map->set_elements_kind(elements_kind);
2723 JSFunction::SetInitialMap(array_function, initial_map, prototype);
2724
2725 // Make "length" magic on instances.
2726 Map::EnsureDescriptorSlack(initial_map, 1);
2727
2728 PropertyAttributes attribs = static_cast<PropertyAttributes>(
2729 DONT_ENUM | DONT_DELETE);
2730
2731 Handle<AccessorInfo> array_length =
2732 Accessors::ArrayLengthInfo(isolate(), attribs);
2733 { // Add length.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002734 AccessorConstantDescriptor d(Handle<Name>(Name::cast(array_length->name())),
2735 array_length, attribs);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002736 initial_map->AppendDescriptor(&d);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002737 }
2738
2739 return array_function;
2740}
2741
Ben Murdoch097c5b22016-05-18 11:27:45 +01002742bool Genesis::InstallNatives(GlobalContextType context_type) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002743 HandleScope scope(isolate());
Steve Blocka7e24c12009-10-30 11:49:00 +00002744
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002745 // Set up the utils object as shared container between native scripts.
2746 Handle<JSObject> utils = factory()->NewJSObject(isolate()->object_function());
2747 JSObject::NormalizeProperties(utils, CLEAR_INOBJECT_PROPERTIES, 16,
2748 "utils container for native scripts");
2749 native_context()->set_natives_utils_object(*utils);
Steve Blocka7e24c12009-10-30 11:49:00 +00002750
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002751 // Set up the extras utils object as a shared container between native
2752 // scripts and extras. (Extras consume things added there by native scripts.)
2753 Handle<JSObject> extras_utils =
2754 factory()->NewJSObject(isolate()->object_function());
2755 native_context()->set_extras_utils_object(*extras_utils);
Steve Blocka7e24c12009-10-30 11:49:00 +00002756
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002757 InstallInternalArray(extras_utils, "InternalPackedArray", FAST_ELEMENTS);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002758
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002759 int builtin_index = Natives::GetDebuggerCount();
2760 // Only run prologue.js and runtime.js at this point.
2761 DCHECK_EQ(builtin_index, Natives::GetIndex("prologue"));
2762 if (!Bootstrapper::CompileBuiltin(isolate(), builtin_index++)) return false;
2763 DCHECK_EQ(builtin_index, Natives::GetIndex("runtime"));
2764 if (!Bootstrapper::CompileBuiltin(isolate(), builtin_index++)) return false;
Steve Blocka7e24c12009-10-30 11:49:00 +00002765
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002766 // A thin context is ready at this point.
2767 if (context_type == THIN_CONTEXT) return true;
Steve Blocka7e24c12009-10-30 11:49:00 +00002768
Steve Block6ded16b2010-05-10 14:33:55 +01002769 {
2770 // Builtin function for OpaqueReference -- a JSValue-based object,
2771 // that keeps its field isolated from JavaScript code. It may store
2772 // objects, that JavaScript code may not access.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002773 Handle<JSFunction> opaque_reference_fun = factory()->NewFunction(
2774 factory()->empty_string(), isolate()->builtins()->Illegal(),
2775 isolate()->initial_object_prototype(), JS_VALUE_TYPE, JSValue::kSize);
Steve Block6ded16b2010-05-10 14:33:55 +01002776 Handle<JSObject> prototype =
Ben Murdoch257744e2011-11-30 15:57:28 +00002777 factory()->NewJSObject(isolate()->object_function(), TENURED);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002778 Accessors::FunctionSetPrototype(opaque_reference_fun, prototype).Assert();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002779 native_context()->set_opaque_reference_function(*opaque_reference_fun);
Steve Block6ded16b2010-05-10 14:33:55 +01002780 }
2781
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002782 // InternalArrays should not use Smi-Only array optimizations. There are too
2783 // many places in the C++ runtime code (e.g. RegEx) that assume that
2784 // elements in InternalArrays can be set to non-Smi values without going
2785 // through a common bottleneck that would make the SMI_ONLY -> FAST_ELEMENT
2786 // transition easy to trap. Moreover, they rarely are smi-only.
2787 {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002788 HandleScope scope(isolate());
2789 Handle<JSObject> utils =
2790 Handle<JSObject>::cast(isolate()->natives_utils_object());
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002791 Handle<JSFunction> array_function =
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002792 InstallInternalArray(utils, "InternalArray", FAST_HOLEY_ELEMENTS);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002793 native_context()->set_internal_array_function(*array_function);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002794 InstallInternalArray(utils, "InternalPackedArray", FAST_ELEMENTS);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002795 }
2796
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002797 // Run the rest of the native scripts.
2798 while (builtin_index < Natives::GetBuiltinsCount()) {
2799 if (!Bootstrapper::CompileBuiltin(isolate(), builtin_index++)) return false;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002800 }
2801
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002802 if (!CallUtilsFunction(isolate(), "PostNatives")) return false;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002803
Ben Murdochda12d292016-06-02 14:46:10 +01002804 auto template_instantiations_cache = UnseededNumberDictionary::New(
2805 isolate(), ApiNatives::kInitialFunctionCacheSize);
Ben Murdoch097c5b22016-05-18 11:27:45 +01002806 native_context()->set_template_instantiations_cache(
2807 *template_instantiations_cache);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002808
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002809 // Store the map for the %ObjectPrototype% after the natives has been compiled
2810 // and the Object function has been set up.
2811 Handle<JSFunction> object_function(native_context()->object_function());
2812 DCHECK(JSObject::cast(object_function->initial_map()->prototype())
2813 ->HasFastProperties());
2814 native_context()->set_object_function_prototype_map(
2815 HeapObject::cast(object_function->initial_map()->prototype())->map());
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002816
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002817 // Store the map for the %StringPrototype% after the natives has been compiled
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002818 // and the String function has been set up.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002819 Handle<JSFunction> string_function(native_context()->string_function());
2820 DCHECK(JSObject::cast(
Iain Merrick75681382010-08-19 15:07:18 +01002821 string_function->initial_map()->prototype())->HasFastProperties());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002822 native_context()->set_string_function_prototype_map(
Iain Merrick75681382010-08-19 15:07:18 +01002823 HeapObject::cast(string_function->initial_map()->prototype())->map());
2824
Ben Murdochc5610432016-08-08 18:44:38 +01002825 Handle<JSGlobalObject> global_object =
2826 handle(native_context()->global_object());
2827
2828 // Install Global.encodeURI.
2829 SimpleInstallFunction(global_object, "encodeURI", Builtins::kGlobalEncodeURI,
2830 1, false);
2831
2832 // Install Global.encodeURIComponent.
2833 SimpleInstallFunction(global_object, "encodeURIComponent",
2834 Builtins::kGlobalEncodeURIComponent, 1, false);
2835
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002836 // Install Global.eval.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002837 {
Ben Murdochc5610432016-08-08 18:44:38 +01002838 Handle<JSFunction> eval =
2839 SimpleInstallFunction(global_object, factory()->eval_string(),
2840 Builtins::kGlobalEval, 1, false);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002841 native_context()->set_global_eval_fun(*eval);
2842 }
Steve Blocka7e24c12009-10-30 11:49:00 +00002843
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002844 // Install Array.prototype.concat
2845 {
2846 Handle<JSFunction> array_constructor(native_context()->array_function());
2847 Handle<JSObject> proto(JSObject::cast(array_constructor->prototype()));
2848 Handle<JSFunction> concat =
2849 InstallFunction(proto, "concat", JS_OBJECT_TYPE, JSObject::kHeaderSize,
2850 MaybeHandle<JSObject>(), Builtins::kArrayConcat);
Steve Blocka7e24c12009-10-30 11:49:00 +00002851
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002852 // Make sure that Array.prototype.concat appears to be compiled.
Steve Blocka7e24c12009-10-30 11:49:00 +00002853 // The code will never be called, but inline caching for call will
2854 // only work if it appears to be compiled.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002855 concat->shared()->DontAdaptArguments();
2856 DCHECK(concat->is_compiled());
Steve Blocka7e24c12009-10-30 11:49:00 +00002857 // Set the lengths for the functions to satisfy ECMA-262.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002858 concat->shared()->set_length(1);
2859 }
2860
2861 // Install InternalArray.prototype.concat
2862 {
2863 Handle<JSFunction> array_constructor(
2864 native_context()->internal_array_function());
2865 Handle<JSObject> proto(JSObject::cast(array_constructor->prototype()));
2866 Handle<JSFunction> concat =
2867 InstallFunction(proto, "concat", JS_OBJECT_TYPE, JSObject::kHeaderSize,
2868 MaybeHandle<JSObject>(), Builtins::kArrayConcat);
2869
2870 // Make sure that InternalArray.prototype.concat appears to be compiled.
2871 // The code will never be called, but inline caching for call will
2872 // only work if it appears to be compiled.
2873 concat->shared()->DontAdaptArguments();
2874 DCHECK(concat->is_compiled());
2875 // Set the lengths for the functions to satisfy ECMA-262.
2876 concat->shared()->set_length(1);
2877 }
2878
2879 // Set up the Promise constructor.
2880 {
2881 Handle<String> key = factory()->Promise_string();
2882 Handle<JSFunction> function = Handle<JSFunction>::cast(
Ben Murdochc5610432016-08-08 18:44:38 +01002883 JSReceiver::GetProperty(global_object, key).ToHandleChecked());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002884 JSFunction::EnsureHasInitialMap(function);
2885 function->initial_map()->set_instance_type(JS_PROMISE_TYPE);
2886 function->shared()->set_construct_stub(
2887 *isolate()->builtins()->JSBuiltinsConstructStub());
2888 InstallWithIntrinsicDefaultProto(isolate(), function,
2889 Context::PROMISE_FUNCTION_INDEX);
Steve Blocka7e24c12009-10-30 11:49:00 +00002890 }
2891
Ben Murdoch42effa52011-08-19 16:40:31 +01002892 InstallBuiltinFunctionIds();
2893
Ben Murdoch097c5b22016-05-18 11:27:45 +01002894 // Create a map for accessor property descriptors (a variant of JSObject
2895 // that predefines four properties get, set, configurable and enumerable).
2896 {
2897 // AccessorPropertyDescriptor initial map.
2898 Handle<Map> map =
2899 factory()->NewMap(JS_OBJECT_TYPE, JSAccessorPropertyDescriptor::kSize);
2900 // Create the descriptor array for the property descriptor object.
2901 Map::EnsureDescriptorSlack(map, 4);
2902
2903 { // get
2904 DataDescriptor d(factory()->get_string(),
2905 JSAccessorPropertyDescriptor::kGetIndex, NONE,
2906 Representation::Tagged());
2907 map->AppendDescriptor(&d);
2908 }
2909 { // set
2910 DataDescriptor d(factory()->set_string(),
2911 JSAccessorPropertyDescriptor::kSetIndex, NONE,
2912 Representation::Tagged());
2913 map->AppendDescriptor(&d);
2914 }
2915 { // enumerable
2916 DataDescriptor d(factory()->enumerable_string(),
2917 JSAccessorPropertyDescriptor::kEnumerableIndex, NONE,
2918 Representation::Tagged());
2919 map->AppendDescriptor(&d);
2920 }
2921 { // configurable
2922 DataDescriptor d(factory()->configurable_string(),
2923 JSAccessorPropertyDescriptor::kConfigurableIndex, NONE,
2924 Representation::Tagged());
2925 map->AppendDescriptor(&d);
2926 }
2927
2928 Map::SetPrototype(map, isolate()->initial_object_prototype());
2929 map->SetConstructor(native_context()->object_function());
2930 map->SetInObjectProperties(4);
2931 map->set_unused_property_fields(0);
2932
2933 native_context()->set_accessor_property_descriptor_map(*map);
2934 }
2935
2936 // Create a map for data property descriptors (a variant of JSObject
2937 // that predefines four properties value, writable, configurable and
2938 // enumerable).
2939 {
2940 // DataPropertyDescriptor initial map.
2941 Handle<Map> map =
2942 factory()->NewMap(JS_OBJECT_TYPE, JSDataPropertyDescriptor::kSize);
2943 // Create the descriptor array for the property descriptor object.
2944 Map::EnsureDescriptorSlack(map, 4);
2945
2946 { // value
2947 DataDescriptor d(factory()->value_string(),
2948 JSDataPropertyDescriptor::kValueIndex, NONE,
2949 Representation::Tagged());
2950 map->AppendDescriptor(&d);
2951 }
2952 { // writable
2953 DataDescriptor d(factory()->writable_string(),
2954 JSDataPropertyDescriptor::kWritableIndex, NONE,
2955 Representation::Tagged());
2956 map->AppendDescriptor(&d);
2957 }
2958 { // enumerable
2959 DataDescriptor d(factory()->enumerable_string(),
2960 JSDataPropertyDescriptor::kEnumerableIndex, NONE,
2961 Representation::Tagged());
2962 map->AppendDescriptor(&d);
2963 }
2964 { // configurable
2965 DataDescriptor d(factory()->configurable_string(),
2966 JSDataPropertyDescriptor::kConfigurableIndex, NONE,
2967 Representation::Tagged());
2968 map->AppendDescriptor(&d);
2969 }
2970
2971 Map::SetPrototype(map, isolate()->initial_object_prototype());
2972 map->SetConstructor(native_context()->object_function());
2973 map->SetInObjectProperties(4);
2974 map->set_unused_property_fields(0);
2975
2976 native_context()->set_data_property_descriptor_map(*map);
2977 }
2978
Steve Block6ded16b2010-05-10 14:33:55 +01002979 // Create a constructor for RegExp results (a variant of Array that
2980 // predefines the two properties index and match).
2981 {
2982 // RegExpResult initial map.
2983
2984 // Find global.Array.prototype to inherit from.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002985 Handle<JSFunction> array_constructor(native_context()->array_function());
Steve Block6ded16b2010-05-10 14:33:55 +01002986 Handle<JSObject> array_prototype(
2987 JSObject::cast(array_constructor->instance_prototype()));
2988
2989 // Add initial map.
2990 Handle<Map> initial_map =
Ben Murdoch257744e2011-11-30 15:57:28 +00002991 factory()->NewMap(JS_ARRAY_TYPE, JSRegExpResult::kSize);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002992 initial_map->SetConstructor(*array_constructor);
Steve Block6ded16b2010-05-10 14:33:55 +01002993
2994 // Set prototype on map.
2995 initial_map->set_non_instance_prototype(false);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002996 Map::SetPrototype(initial_map, array_prototype);
Steve Block6ded16b2010-05-10 14:33:55 +01002997
2998 // Update map with length accessor from Array and add "index" and "input".
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002999 Map::EnsureDescriptorSlack(initial_map, 3);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00003000
Steve Block6ded16b2010-05-10 14:33:55 +01003001 {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003002 JSFunction* array_function = native_context()->array_function();
3003 Handle<DescriptorArray> array_descriptors(
3004 array_function->initial_map()->instance_descriptors());
3005 Handle<String> length = factory()->length_string();
3006 int old = array_descriptors->SearchWithCache(
Ben Murdoch097c5b22016-05-18 11:27:45 +01003007 isolate(), *length, array_function->initial_map());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003008 DCHECK(old != DescriptorArray::kNotFound);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003009 AccessorConstantDescriptor desc(
3010 length, handle(array_descriptors->GetValue(old), isolate()),
3011 array_descriptors->GetDetails(old).attributes());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003012 initial_map->AppendDescriptor(&desc);
3013 }
3014 {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003015 DataDescriptor index_field(factory()->index_string(),
3016 JSRegExpResult::kIndexIndex, NONE,
3017 Representation::Tagged());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003018 initial_map->AppendDescriptor(&index_field);
Steve Block6ded16b2010-05-10 14:33:55 +01003019 }
3020
3021 {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003022 DataDescriptor input_field(factory()->input_string(),
3023 JSRegExpResult::kInputIndex, NONE,
3024 Representation::Tagged());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003025 initial_map->AppendDescriptor(&input_field);
Steve Block6ded16b2010-05-10 14:33:55 +01003026 }
Steve Block6ded16b2010-05-10 14:33:55 +01003027
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003028 initial_map->SetInObjectProperties(2);
Steve Block6ded16b2010-05-10 14:33:55 +01003029 initial_map->set_unused_property_fields(0);
Steve Block6ded16b2010-05-10 14:33:55 +01003030
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003031 native_context()->set_regexp_result_map(*initial_map);
Steve Block6ded16b2010-05-10 14:33:55 +01003032 }
3033
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003034 // Add @@iterator method to the arguments object maps.
3035 {
3036 PropertyAttributes attribs = DONT_ENUM;
3037 Handle<AccessorInfo> arguments_iterator =
3038 Accessors::ArgumentsIteratorInfo(isolate(), attribs);
3039 {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003040 AccessorConstantDescriptor d(factory()->iterator_symbol(),
3041 arguments_iterator, attribs);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003042 Handle<Map> map(native_context()->sloppy_arguments_map());
3043 Map::EnsureDescriptorSlack(map, 1);
3044 map->AppendDescriptor(&d);
3045 }
3046 {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003047 AccessorConstantDescriptor d(factory()->iterator_symbol(),
3048 arguments_iterator, attribs);
3049 Handle<Map> map(native_context()->fast_aliased_arguments_map());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003050 Map::EnsureDescriptorSlack(map, 1);
3051 map->AppendDescriptor(&d);
3052 }
3053 {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003054 AccessorConstantDescriptor d(factory()->iterator_symbol(),
3055 arguments_iterator, attribs);
3056 Handle<Map> map(native_context()->slow_aliased_arguments_map());
3057 Map::EnsureDescriptorSlack(map, 1);
3058 map->AppendDescriptor(&d);
3059 }
3060 {
3061 AccessorConstantDescriptor d(factory()->iterator_symbol(),
3062 arguments_iterator, attribs);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003063 Handle<Map> map(native_context()->strict_arguments_map());
3064 Map::EnsureDescriptorSlack(map, 1);
3065 map->AppendDescriptor(&d);
3066 }
3067 }
3068
Steve Blocka7e24c12009-10-30 11:49:00 +00003069 return true;
3070}
3071
3072
Ben Murdoch257744e2011-11-30 15:57:28 +00003073bool Genesis::InstallExperimentalNatives() {
Ben Murdoch097c5b22016-05-18 11:27:45 +01003074 static const char* harmony_iterator_close_natives[] = {nullptr};
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003075 static const char* harmony_species_natives[] = {"native harmony-species.js",
3076 nullptr};
Ben Murdochc5610432016-08-08 18:44:38 +01003077 static const char* harmony_explicit_tailcalls_natives[] = {nullptr};
Ben Murdoch097c5b22016-05-18 11:27:45 +01003078 static const char* harmony_tailcalls_natives[] = {nullptr};
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003079 static const char* harmony_unicode_regexps_natives[] = {
3080 "native harmony-unicode-regexps.js", nullptr};
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003081 static const char* harmony_sharedarraybuffer_natives[] = {
3082 "native harmony-sharedarraybuffer.js", "native harmony-atomics.js", NULL};
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003083 static const char* harmony_simd_natives[] = {"native harmony-simd.js",
3084 nullptr};
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003085 static const char* harmony_do_expressions_natives[] = {nullptr};
Ben Murdochc5610432016-08-08 18:44:38 +01003086 static const char* harmony_for_in_natives[] = {nullptr};
Ben Murdochda12d292016-06-02 14:46:10 +01003087 static const char* harmony_regexp_exec_natives[] = {
3088 "native harmony-regexp-exec.js", nullptr};
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003089 static const char* harmony_regexp_subclass_natives[] = {nullptr};
3090 static const char* harmony_regexp_lookbehind_natives[] = {nullptr};
Ben Murdoch097c5b22016-05-18 11:27:45 +01003091 static const char* harmony_instanceof_natives[] = {nullptr};
Ben Murdochda12d292016-06-02 14:46:10 +01003092 static const char* harmony_restrictive_declarations_natives[] = {nullptr};
Ben Murdoch097c5b22016-05-18 11:27:45 +01003093 static const char* harmony_regexp_property_natives[] = {nullptr};
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003094 static const char* harmony_function_name_natives[] = {nullptr};
Ben Murdoch097c5b22016-05-18 11:27:45 +01003095 static const char* harmony_function_sent_natives[] = {nullptr};
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003096 static const char* promise_extra_natives[] = {"native promise-extra.js",
3097 nullptr};
Ben Murdochc5610432016-08-08 18:44:38 +01003098 static const char* intl_extra_natives[] = {"native intl-extra.js", nullptr};
Ben Murdoch097c5b22016-05-18 11:27:45 +01003099 static const char* harmony_object_values_entries_natives[] = {nullptr};
3100 static const char* harmony_object_own_property_descriptors_natives[] = {
3101 nullptr};
Ben Murdochda12d292016-06-02 14:46:10 +01003102 static const char* harmony_array_prototype_values_natives[] = {nullptr};
3103 static const char* harmony_exponentiation_operator_natives[] = {nullptr};
3104 static const char* harmony_string_padding_natives[] = {
3105 "native harmony-string-padding.js", nullptr};
Ben Murdochc5610432016-08-08 18:44:38 +01003106#ifdef V8_I18N_SUPPORT
3107 static const char* icu_case_mapping_natives[] = {"native icu-case-mapping.js",
3108 nullptr};
3109#endif
3110 static const char* harmony_async_await_natives[] = {
3111 "native harmony-async-await.js", nullptr};
Emily Bernierd0a1eb72015-03-24 16:35:39 -04003112
Ben Murdoch257744e2011-11-30 15:57:28 +00003113 for (int i = ExperimentalNatives::GetDebuggerCount();
Emily Bernierd0a1eb72015-03-24 16:35:39 -04003114 i < ExperimentalNatives::GetBuiltinsCount(); i++) {
3115#define INSTALL_EXPERIMENTAL_NATIVES(id, desc) \
3116 if (FLAG_##id) { \
3117 for (size_t j = 0; id##_natives[j] != NULL; j++) { \
3118 Vector<const char> script_name = ExperimentalNatives::GetScriptName(i); \
3119 if (strncmp(script_name.start(), id##_natives[j], \
3120 script_name.length()) == 0) { \
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003121 if (!Bootstrapper::CompileExperimentalBuiltin(isolate(), i)) { \
3122 return false; \
3123 } \
Emily Bernierd0a1eb72015-03-24 16:35:39 -04003124 } \
3125 } \
3126 }
3127 HARMONY_INPROGRESS(INSTALL_EXPERIMENTAL_NATIVES);
3128 HARMONY_STAGED(INSTALL_EXPERIMENTAL_NATIVES);
3129 HARMONY_SHIPPING(INSTALL_EXPERIMENTAL_NATIVES);
Ben Murdochc5610432016-08-08 18:44:38 +01003130 INSTALL_EXPERIMENTAL_NATIVES(intl_extra, "");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003131 INSTALL_EXPERIMENTAL_NATIVES(promise_extra, "");
Emily Bernierd0a1eb72015-03-24 16:35:39 -04003132#undef INSTALL_EXPERIMENTAL_NATIVES
Ben Murdoch257744e2011-11-30 15:57:28 +00003133 }
3134
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003135 if (!CallUtilsFunction(isolate(), "PostExperimentals")) return false;
3136
3137 InstallExperimentalBuiltinFunctionIds();
Ben Murdoch257744e2011-11-30 15:57:28 +00003138 return true;
3139}
3140
3141
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003142bool Genesis::InstallExtraNatives() {
3143 HandleScope scope(isolate());
3144
3145 Handle<JSObject> extras_binding =
3146 factory()->NewJSObject(isolate()->object_function());
3147 native_context()->set_extras_binding_object(*extras_binding);
3148
3149 for (int i = ExtraNatives::GetDebuggerCount();
3150 i < ExtraNatives::GetBuiltinsCount(); i++) {
3151 if (!Bootstrapper::CompileExtraBuiltin(isolate(), i)) return false;
3152 }
3153
3154 return true;
3155}
3156
3157
3158bool Genesis::InstallExperimentalExtraNatives() {
3159 for (int i = ExperimentalExtraNatives::GetDebuggerCount();
3160 i < ExperimentalExtraNatives::GetBuiltinsCount(); i++) {
3161 if (!Bootstrapper::CompileExperimentalExtraBuiltin(isolate(), i))
3162 return false;
3163 }
3164
3165 return true;
3166}
3167
3168
3169bool Genesis::InstallDebuggerNatives() {
3170 for (int i = 0; i < Natives::GetDebuggerCount(); ++i) {
3171 if (!Bootstrapper::CompileBuiltin(isolate(), i)) return false;
3172 }
3173 return CallUtilsFunction(isolate(), "PostDebug");
3174}
3175
3176
Ben Murdochb0fe1622011-05-05 13:52:32 +01003177static void InstallBuiltinFunctionId(Handle<JSObject> holder,
3178 const char* function_name,
3179 BuiltinFunctionId id) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003180 Isolate* isolate = holder->GetIsolate();
3181 Handle<Object> function_object =
Ben Murdochda12d292016-06-02 14:46:10 +01003182 JSReceiver::GetProperty(isolate, holder, function_name).ToHandleChecked();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003183 Handle<JSFunction> function = Handle<JSFunction>::cast(function_object);
Ben Murdochda12d292016-06-02 14:46:10 +01003184 function->shared()->set_builtin_function_id(id);
Kristian Monsen25f61362010-05-21 11:50:48 +01003185}
3186
3187
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003188#define INSTALL_BUILTIN_ID(holder_expr, fun_name, name) \
3189 { #holder_expr, #fun_name, k##name } \
3190 ,
3191
3192
Ben Murdochb0fe1622011-05-05 13:52:32 +01003193void Genesis::InstallBuiltinFunctionIds() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003194 HandleScope scope(isolate());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003195 struct BuiltinFunctionIds {
3196 const char* holder_expr;
3197 const char* fun_name;
3198 BuiltinFunctionId id;
3199 };
3200
3201 const BuiltinFunctionIds builtins[] = {
3202 FUNCTIONS_WITH_ID_LIST(INSTALL_BUILTIN_ID)};
3203
3204 for (const BuiltinFunctionIds& builtin : builtins) {
3205 Handle<JSObject> holder =
3206 ResolveBuiltinIdHolder(native_context(), builtin.holder_expr);
3207 InstallBuiltinFunctionId(holder, builtin.fun_name, builtin.id);
Kristian Monsen25f61362010-05-21 11:50:48 +01003208 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003209}
3210
3211
3212void Genesis::InstallExperimentalBuiltinFunctionIds() {
3213 if (FLAG_harmony_sharedarraybuffer) {
3214 struct BuiltinFunctionIds {
3215 const char* holder_expr;
3216 const char* fun_name;
3217 BuiltinFunctionId id;
3218 };
3219
3220 const BuiltinFunctionIds atomic_builtins[] = {
3221 ATOMIC_FUNCTIONS_WITH_ID_LIST(INSTALL_BUILTIN_ID)};
3222
3223 for (const BuiltinFunctionIds& builtin : atomic_builtins) {
3224 Handle<JSObject> holder =
3225 ResolveBuiltinIdHolder(native_context(), builtin.holder_expr);
3226 InstallBuiltinFunctionId(holder, builtin.fun_name, builtin.id);
3227 }
3228 }
3229}
3230
3231
Ben Murdochb0fe1622011-05-05 13:52:32 +01003232#undef INSTALL_BUILTIN_ID
Steve Block6ded16b2010-05-10 14:33:55 +01003233
3234
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003235void Genesis::InitializeNormalizedMapCaches() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003236 Handle<NormalizedMapCache> cache = NormalizedMapCache::New(isolate());
3237 native_context()->set_normalized_map_cache(*cache);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003238}
3239
3240
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003241bool Bootstrapper::InstallExtensions(Handle<Context> native_context,
Andrei Popescu31002712010-02-23 13:46:05 +00003242 v8::ExtensionConfiguration* extensions) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003243 BootstrapperActive active(this);
3244 SaveContext saved_context(isolate_);
3245 isolate_->set_context(*native_context);
3246 return Genesis::InstallExtensions(native_context, extensions) &&
3247 Genesis::InstallSpecialObjects(native_context);
3248}
3249
3250
3251bool Genesis::InstallSpecialObjects(Handle<Context> native_context) {
3252 Isolate* isolate = native_context->GetIsolate();
3253 // Don't install extensions into the snapshot.
3254 if (isolate->serializer_enabled()) return true;
3255
3256 Factory* factory = isolate->factory();
3257 HandleScope scope(isolate);
3258 Handle<JSGlobalObject> global(JSGlobalObject::cast(
3259 native_context->global_object()));
3260
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003261 Handle<JSObject> Error = isolate->error_function();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003262 Handle<String> name =
3263 factory->InternalizeOneByteString(STATIC_CHAR_VECTOR("stackTraceLimit"));
3264 Handle<Smi> stack_trace_limit(Smi::FromInt(FLAG_stack_trace_limit), isolate);
3265 JSObject::AddProperty(Error, name, stack_trace_limit, NONE);
3266
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003267 // Expose the debug global object in global if a name for it is specified.
3268 if (FLAG_expose_debug_as != NULL && strlen(FLAG_expose_debug_as) != 0) {
3269 // If loading fails we just bail out without installing the
3270 // debugger but without tanking the whole context.
3271 Debug* debug = isolate->debug();
3272 if (!debug->Load()) return true;
3273 Handle<Context> debug_context = debug->debug_context();
3274 // Set the security token for the debugger context to the same as
3275 // the shell native context to allow calling between these (otherwise
3276 // exposing debug global object doesn't make much sense).
3277 debug_context->set_security_token(native_context->security_token());
3278 Handle<String> debug_string =
3279 factory->InternalizeUtf8String(FLAG_expose_debug_as);
3280 uint32_t index;
3281 if (debug_string->AsArrayIndex(&index)) return true;
3282 Handle<Object> global_proxy(debug_context->global_proxy(), isolate);
3283 JSObject::AddProperty(global, debug_string, global_proxy, DONT_ENUM);
3284 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003285
3286 if (FLAG_expose_wasm) {
3287 WasmJs::Install(isolate, global);
3288 }
3289
Andrei Popescu31002712010-02-23 13:46:05 +00003290 return true;
3291}
3292
3293
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003294static uint32_t Hash(RegisteredExtension* extension) {
3295 return v8::internal::ComputePointerHash(extension);
3296}
3297
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003298
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003299Genesis::ExtensionStates::ExtensionStates() : map_(HashMap::PointersMatch, 8) {}
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003300
3301Genesis::ExtensionTraversalState Genesis::ExtensionStates::get_state(
3302 RegisteredExtension* extension) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003303 i::HashMap::Entry* entry = map_.Lookup(extension, Hash(extension));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003304 if (entry == NULL) {
3305 return UNVISITED;
3306 }
3307 return static_cast<ExtensionTraversalState>(
3308 reinterpret_cast<intptr_t>(entry->value));
3309}
3310
3311void Genesis::ExtensionStates::set_state(RegisteredExtension* extension,
3312 ExtensionTraversalState state) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003313 map_.LookupOrInsert(extension, Hash(extension))->value =
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003314 reinterpret_cast<void*>(static_cast<intptr_t>(state));
3315}
Steve Blocka7e24c12009-10-30 11:49:00 +00003316
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003317
3318bool Genesis::InstallExtensions(Handle<Context> native_context,
Andrei Popescu31002712010-02-23 13:46:05 +00003319 v8::ExtensionConfiguration* extensions) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003320 Isolate* isolate = native_context->GetIsolate();
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003321 ExtensionStates extension_states; // All extensions have state UNVISITED.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003322 return InstallAutoExtensions(isolate, &extension_states) &&
Ben Murdochc5610432016-08-08 18:44:38 +01003323 (!FLAG_expose_free_buffer ||
3324 InstallExtension(isolate, "v8/free-buffer", &extension_states)) &&
3325 (!FLAG_expose_gc ||
3326 InstallExtension(isolate, "v8/gc", &extension_states)) &&
3327 (!FLAG_expose_externalize_string ||
3328 InstallExtension(isolate, "v8/externalize", &extension_states)) &&
3329 (!FLAG_track_gc_object_stats ||
3330 InstallExtension(isolate, "v8/statistics", &extension_states)) &&
3331 (!FLAG_expose_trigger_failure ||
3332 InstallExtension(isolate, "v8/trigger-failure", &extension_states)) &&
3333 (!(FLAG_ignition && FLAG_trace_ignition_dispatches) ||
3334 InstallExtension(isolate, "v8/ignition-statistics",
3335 &extension_states)) &&
3336 InstallRequestedExtensions(isolate, extensions, &extension_states);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003337}
Steve Blocka7e24c12009-10-30 11:49:00 +00003338
Steve Blocka7e24c12009-10-30 11:49:00 +00003339
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003340bool Genesis::InstallAutoExtensions(Isolate* isolate,
3341 ExtensionStates* extension_states) {
3342 for (v8::RegisteredExtension* it = v8::RegisteredExtension::first_extension();
3343 it != NULL;
3344 it = it->next()) {
3345 if (it->extension()->auto_enable() &&
3346 !InstallExtension(isolate, it, extension_states)) {
Steve Blocka7e24c12009-10-30 11:49:00 +00003347 return false;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003348 }
Steve Blocka7e24c12009-10-30 11:49:00 +00003349 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003350 return true;
3351}
Steve Blocka7e24c12009-10-30 11:49:00 +00003352
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003353
3354bool Genesis::InstallRequestedExtensions(Isolate* isolate,
3355 v8::ExtensionConfiguration* extensions,
3356 ExtensionStates* extension_states) {
3357 for (const char** it = extensions->begin(); it != extensions->end(); ++it) {
3358 if (!InstallExtension(isolate, *it, extension_states)) return false;
3359 }
Steve Blocka7e24c12009-10-30 11:49:00 +00003360 return true;
3361}
3362
3363
3364// Installs a named extension. This methods is unoptimized and does
3365// not scale well if we want to support a large number of extensions.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003366bool Genesis::InstallExtension(Isolate* isolate,
3367 const char* name,
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003368 ExtensionStates* extension_states) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003369 for (v8::RegisteredExtension* it = v8::RegisteredExtension::first_extension();
3370 it != NULL;
3371 it = it->next()) {
3372 if (strcmp(name, it->extension()->name()) == 0) {
3373 return InstallExtension(isolate, it, extension_states);
3374 }
Steve Blocka7e24c12009-10-30 11:49:00 +00003375 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003376 return Utils::ApiCheck(false,
3377 "v8::Context::New()",
3378 "Cannot find required extension");
Steve Blocka7e24c12009-10-30 11:49:00 +00003379}
3380
3381
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003382bool Genesis::InstallExtension(Isolate* isolate,
3383 v8::RegisteredExtension* current,
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003384 ExtensionStates* extension_states) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003385 HandleScope scope(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00003386
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003387 if (extension_states->get_state(current) == INSTALLED) return true;
Steve Blocka7e24c12009-10-30 11:49:00 +00003388 // The current node has already been visited so there must be a
3389 // cycle in the dependency graph; fail.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003390 if (!Utils::ApiCheck(extension_states->get_state(current) != VISITED,
3391 "v8::Context::New()",
3392 "Circular extension dependency")) {
Steve Blocka7e24c12009-10-30 11:49:00 +00003393 return false;
3394 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003395 DCHECK(extension_states->get_state(current) == UNVISITED);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003396 extension_states->set_state(current, VISITED);
Steve Blocka7e24c12009-10-30 11:49:00 +00003397 v8::Extension* extension = current->extension();
3398 // Install the extension's dependencies
3399 for (int i = 0; i < extension->dependency_count(); i++) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003400 if (!InstallExtension(isolate,
3401 extension->dependencies()[i],
3402 extension_states)) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003403 return false;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003404 }
Steve Blocka7e24c12009-10-30 11:49:00 +00003405 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003406 // We do not expect this to throw an exception. Change this if it does.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003407 bool result = CompileExtension(isolate, extension);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003408 DCHECK(isolate->has_pending_exception() != result);
Steve Blocka7e24c12009-10-30 11:49:00 +00003409 if (!result) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003410 // We print out the name of the extension that fail to install.
3411 // When an error is thrown during bootstrapping we automatically print
3412 // the line number at which this happened to the console in the isolate
3413 // error throwing functionality.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003414 base::OS::PrintError("Error installing extension '%s'.\n",
3415 current->extension()->name());
Steve Block44f0eee2011-05-26 01:26:41 +01003416 isolate->clear_pending_exception();
Steve Blocka7e24c12009-10-30 11:49:00 +00003417 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003418 extension_states->set_state(current, INSTALLED);
3419 isolate->NotifyExtensionInstalled();
Steve Blocka7e24c12009-10-30 11:49:00 +00003420 return result;
3421}
3422
3423
3424bool Genesis::ConfigureGlobalObjects(
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003425 v8::Local<v8::ObjectTemplate> global_proxy_template) {
Steve Blocka7e24c12009-10-30 11:49:00 +00003426 Handle<JSObject> global_proxy(
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003427 JSObject::cast(native_context()->global_proxy()));
3428 Handle<JSObject> global_object(
3429 JSObject::cast(native_context()->global_object()));
Steve Blocka7e24c12009-10-30 11:49:00 +00003430
3431 if (!global_proxy_template.IsEmpty()) {
3432 // Configure the global proxy object.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003433 Handle<ObjectTemplateInfo> global_proxy_data =
Steve Blocka7e24c12009-10-30 11:49:00 +00003434 v8::Utils::OpenHandle(*global_proxy_template);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003435 if (!ConfigureApiObject(global_proxy, global_proxy_data)) return false;
Steve Blocka7e24c12009-10-30 11:49:00 +00003436
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003437 // Configure the global object.
Steve Blocka7e24c12009-10-30 11:49:00 +00003438 Handle<FunctionTemplateInfo> proxy_constructor(
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003439 FunctionTemplateInfo::cast(global_proxy_data->constructor()));
Steve Blocka7e24c12009-10-30 11:49:00 +00003440 if (!proxy_constructor->prototype_template()->IsUndefined()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003441 Handle<ObjectTemplateInfo> global_object_data(
Steve Blocka7e24c12009-10-30 11:49:00 +00003442 ObjectTemplateInfo::cast(proxy_constructor->prototype_template()));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003443 if (!ConfigureApiObject(global_object, global_object_data)) return false;
Steve Blocka7e24c12009-10-30 11:49:00 +00003444 }
3445 }
3446
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003447 SetObjectPrototype(global_proxy, global_object);
3448
3449 native_context()->set_initial_array_prototype(
3450 JSArray::cast(native_context()->array_function()->prototype()));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003451 native_context()->set_array_buffer_map(
3452 native_context()->array_buffer_fun()->initial_map());
3453 native_context()->set_js_map_map(
3454 native_context()->js_map_fun()->initial_map());
3455 native_context()->set_js_set_map(
3456 native_context()->js_set_fun()->initial_map());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003457
Steve Blocka7e24c12009-10-30 11:49:00 +00003458 return true;
3459}
3460
3461
3462bool Genesis::ConfigureApiObject(Handle<JSObject> object,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003463 Handle<ObjectTemplateInfo> object_template) {
3464 DCHECK(!object_template.is_null());
3465 DCHECK(FunctionTemplateInfo::cast(object_template->constructor())
3466 ->IsTemplateFor(object->map()));;
Steve Blocka7e24c12009-10-30 11:49:00 +00003467
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003468 MaybeHandle<JSObject> maybe_obj =
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003469 ApiNatives::InstantiateObject(object_template);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003470 Handle<JSObject> obj;
3471 if (!maybe_obj.ToHandle(&obj)) {
3472 DCHECK(isolate()->has_pending_exception());
Ben Murdoch257744e2011-11-30 15:57:28 +00003473 isolate()->clear_pending_exception();
Steve Blocka7e24c12009-10-30 11:49:00 +00003474 return false;
3475 }
3476 TransferObject(obj, object);
3477 return true;
3478}
3479
3480
3481void Genesis::TransferNamedProperties(Handle<JSObject> from,
3482 Handle<JSObject> to) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003483 // If JSObject::AddProperty asserts due to already existing property,
3484 // it is likely due to both global objects sharing property name(s).
3485 // Merging those two global objects is impossible.
3486 // The global template must not create properties that already exist
3487 // in the snapshotted global object.
Steve Blocka7e24c12009-10-30 11:49:00 +00003488 if (from->HasFastProperties()) {
3489 Handle<DescriptorArray> descs =
3490 Handle<DescriptorArray>(from->map()->instance_descriptors());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003491 for (int i = 0; i < from->map()->NumberOfOwnDescriptors(); i++) {
3492 PropertyDetails details = descs->GetDetails(i);
Steve Blocka7e24c12009-10-30 11:49:00 +00003493 switch (details.type()) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003494 case DATA: {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003495 HandleScope inner(isolate());
3496 Handle<Name> key = Handle<Name>(descs->GetKey(i));
3497 FieldIndex index = FieldIndex::ForDescriptor(from->map(), i);
3498 DCHECK(!descs->GetDetails(i).representation().IsDouble());
3499 Handle<Object> value = Handle<Object>(from->RawFastPropertyAt(index),
3500 isolate());
3501 JSObject::AddProperty(to, key, value, details.attributes());
Steve Blocka7e24c12009-10-30 11:49:00 +00003502 break;
3503 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003504 case DATA_CONSTANT: {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003505 HandleScope inner(isolate());
3506 Handle<Name> key = Handle<Name>(descs->GetKey(i));
3507 Handle<Object> constant(descs->GetConstant(i), isolate());
3508 JSObject::AddProperty(to, key, constant, details.attributes());
Steve Blocka7e24c12009-10-30 11:49:00 +00003509 break;
3510 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003511 case ACCESSOR:
Emily Bernierd0a1eb72015-03-24 16:35:39 -04003512 UNREACHABLE();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003513 case ACCESSOR_CONSTANT: {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003514 Handle<Name> key(descs->GetKey(i));
3515 LookupIterator it(to, key, LookupIterator::OWN_SKIP_INTERCEPTOR);
3516 CHECK_NE(LookupIterator::ACCESS_CHECK, it.state());
Steve Blocka7e24c12009-10-30 11:49:00 +00003517 // If the property is already there we skip it
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003518 if (it.IsFound()) continue;
3519 HandleScope inner(isolate());
3520 DCHECK(!to->HasFastProperties());
Andrei Popescu31002712010-02-23 13:46:05 +00003521 // Add to dictionary.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003522 Handle<Object> callbacks(descs->GetCallbacksObject(i), isolate());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003523 PropertyDetails d(details.attributes(), ACCESSOR_CONSTANT, i + 1,
3524 PropertyCellType::kMutable);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003525 JSObject::SetNormalizedProperty(to, key, callbacks, d);
Steve Blocka7e24c12009-10-30 11:49:00 +00003526 break;
3527 }
Steve Blocka7e24c12009-10-30 11:49:00 +00003528 }
3529 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003530 } else if (from->IsJSGlobalObject()) {
3531 Handle<GlobalDictionary> properties =
3532 Handle<GlobalDictionary>(from->global_dictionary());
3533 int capacity = properties->Capacity();
3534 for (int i = 0; i < capacity; i++) {
3535 Object* raw_key(properties->KeyAt(i));
3536 if (properties->IsKey(raw_key)) {
3537 DCHECK(raw_key->IsName());
3538 // If the property is already there we skip it.
3539 Handle<Name> key(Name::cast(raw_key));
3540 LookupIterator it(to, key, LookupIterator::OWN_SKIP_INTERCEPTOR);
3541 CHECK_NE(LookupIterator::ACCESS_CHECK, it.state());
3542 if (it.IsFound()) continue;
3543 // Set the property.
3544 DCHECK(properties->ValueAt(i)->IsPropertyCell());
3545 Handle<PropertyCell> cell(PropertyCell::cast(properties->ValueAt(i)));
3546 Handle<Object> value(cell->value(), isolate());
3547 if (value->IsTheHole()) continue;
3548 PropertyDetails details = cell->property_details();
3549 DCHECK_EQ(kData, details.kind());
3550 JSObject::AddProperty(to, key, value, details.attributes());
3551 }
3552 }
Steve Blocka7e24c12009-10-30 11:49:00 +00003553 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003554 Handle<NameDictionary> properties =
3555 Handle<NameDictionary>(from->property_dictionary());
Steve Blocka7e24c12009-10-30 11:49:00 +00003556 int capacity = properties->Capacity();
3557 for (int i = 0; i < capacity; i++) {
3558 Object* raw_key(properties->KeyAt(i));
3559 if (properties->IsKey(raw_key)) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003560 DCHECK(raw_key->IsName());
Steve Blocka7e24c12009-10-30 11:49:00 +00003561 // If the property is already there we skip it.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003562 Handle<Name> key(Name::cast(raw_key));
3563 LookupIterator it(to, key, LookupIterator::OWN_SKIP_INTERCEPTOR);
3564 CHECK_NE(LookupIterator::ACCESS_CHECK, it.state());
3565 if (it.IsFound()) continue;
Steve Blocka7e24c12009-10-30 11:49:00 +00003566 // Set the property.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003567 Handle<Object> value = Handle<Object>(properties->ValueAt(i),
3568 isolate());
3569 DCHECK(!value->IsCell());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003570 DCHECK(!value->IsTheHole());
Steve Blocka7e24c12009-10-30 11:49:00 +00003571 PropertyDetails details = properties->DetailsAt(i);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003572 DCHECK_EQ(kData, details.kind());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003573 JSObject::AddProperty(to, key, value, details.attributes());
Steve Blocka7e24c12009-10-30 11:49:00 +00003574 }
3575 }
3576 }
3577}
3578
3579
3580void Genesis::TransferIndexedProperties(Handle<JSObject> from,
3581 Handle<JSObject> to) {
3582 // Cloning the elements array is sufficient.
3583 Handle<FixedArray> from_elements =
3584 Handle<FixedArray>(FixedArray::cast(from->elements()));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003585 Handle<FixedArray> to_elements = factory()->CopyFixedArray(from_elements);
Steve Blocka7e24c12009-10-30 11:49:00 +00003586 to->set_elements(*to_elements);
3587}
3588
3589
3590void Genesis::TransferObject(Handle<JSObject> from, Handle<JSObject> to) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003591 HandleScope outer(isolate());
Steve Blocka7e24c12009-10-30 11:49:00 +00003592
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003593 DCHECK(!from->IsJSArray());
3594 DCHECK(!to->IsJSArray());
Steve Blocka7e24c12009-10-30 11:49:00 +00003595
3596 TransferNamedProperties(from, to);
3597 TransferIndexedProperties(from, to);
3598
3599 // Transfer the prototype (new map is needed).
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003600 Handle<Object> proto(from->map()->prototype(), isolate());
3601 SetObjectPrototype(to, proto);
Steve Blocka7e24c12009-10-30 11:49:00 +00003602}
3603
3604
3605void Genesis::MakeFunctionInstancePrototypeWritable() {
Steve Block44f0eee2011-05-26 01:26:41 +01003606 // The maps with writable prototype are created in CreateEmptyFunction
3607 // and CreateStrictModeFunctionMaps respectively. Initially the maps are
3608 // created with read-only prototype for JS builtins processing.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003609 DCHECK(!sloppy_function_map_writable_prototype_.is_null());
3610 DCHECK(!strict_function_map_writable_prototype_.is_null());
Steve Blocka7e24c12009-10-30 11:49:00 +00003611
Steve Block44f0eee2011-05-26 01:26:41 +01003612 // Replace function instance maps to make prototype writable.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003613 native_context()->set_sloppy_function_map(
3614 *sloppy_function_map_writable_prototype_);
3615 native_context()->set_strict_function_map(
3616 *strict_function_map_writable_prototype_);
Steve Blocka7e24c12009-10-30 11:49:00 +00003617}
3618
3619
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003620class NoTrackDoubleFieldsForSerializerScope {
3621 public:
3622 explicit NoTrackDoubleFieldsForSerializerScope(Isolate* isolate)
Emily Bernierd0a1eb72015-03-24 16:35:39 -04003623 : flag_(FLAG_track_double_fields), enabled_(false) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003624 if (isolate->serializer_enabled()) {
3625 // Disable tracking double fields because heap numbers treated as
3626 // immutable by the serializer.
3627 FLAG_track_double_fields = false;
Emily Bernierd0a1eb72015-03-24 16:35:39 -04003628 enabled_ = true;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003629 }
3630 }
Steve Blocka7e24c12009-10-30 11:49:00 +00003631
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003632 ~NoTrackDoubleFieldsForSerializerScope() {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04003633 if (enabled_) {
3634 FLAG_track_double_fields = flag_;
3635 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003636 }
3637
3638 private:
3639 bool flag_;
Emily Bernierd0a1eb72015-03-24 16:35:39 -04003640 bool enabled_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003641};
3642
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003643Genesis::Genesis(Isolate* isolate,
3644 MaybeHandle<JSGlobalProxy> maybe_global_proxy,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003645 v8::Local<v8::ObjectTemplate> global_proxy_template,
3646 v8::ExtensionConfiguration* extensions,
Ben Murdoch097c5b22016-05-18 11:27:45 +01003647 GlobalContextType context_type)
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003648 : isolate_(isolate), active_(isolate->bootstrapper()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003649 NoTrackDoubleFieldsForSerializerScope disable_scope(isolate);
3650 result_ = Handle<Context>::null();
Steve Blocka7e24c12009-10-30 11:49:00 +00003651 // Before creating the roots we must save the context and restore it
3652 // on all function exits.
Steve Block44f0eee2011-05-26 01:26:41 +01003653 SaveContext saved_context(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00003654
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003655 // During genesis, the boilerplate for stack overflow won't work until the
3656 // environment has been at least partially initialized. Add a stack check
3657 // before entering JS code to catch overflow early.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003658 StackLimitCheck check(isolate);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003659 if (check.HasOverflowed()) {
3660 isolate->StackOverflow();
3661 return;
3662 }
3663
3664 // The deserializer needs to hook up references to the global proxy.
3665 // Create an uninitialized global proxy now if we don't have one
3666 // and initialize it later in CreateNewGlobals.
3667 Handle<JSGlobalProxy> global_proxy;
3668 if (!maybe_global_proxy.ToHandle(&global_proxy)) {
3669 global_proxy = isolate->factory()->NewUninitializedJSGlobalProxy();
3670 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003671
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003672 // We can only de-serialize a context if the isolate was initialized from
3673 // a snapshot. Otherwise we have to build the context from scratch.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003674 // Also create a context from scratch to expose natives, if required by flag.
3675 if (!isolate->initialized_from_snapshot() ||
3676 !Snapshot::NewContextFromSnapshot(isolate, global_proxy)
3677 .ToHandle(&native_context_)) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003678 native_context_ = Handle<Context>();
3679 }
3680
3681 if (!native_context().is_null()) {
3682 AddToWeakNativeContextList(*native_context());
3683 isolate->set_context(*native_context());
Steve Block44f0eee2011-05-26 01:26:41 +01003684 isolate->counters()->contexts_created_by_snapshot()->Increment();
Emily Bernierd0a1eb72015-03-24 16:35:39 -04003685#if TRACE_MAPS
3686 if (FLAG_trace_maps) {
3687 Handle<JSFunction> object_fun = isolate->object_function();
3688 PrintF("[TraceMap: InitialMap map= %p SFI= %d_Object ]\n",
3689 reinterpret_cast<void*>(object_fun->initial_map()),
3690 object_fun->shared()->unique_id());
3691 Map::TraceAllTransitions(object_fun->initial_map());
3692 }
3693#endif
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003694 Handle<JSGlobalObject> global_object =
3695 CreateNewGlobals(global_proxy_template, global_proxy);
Andrei Popescu402d9372010-02-26 13:31:12 +00003696
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003697 HookUpGlobalProxy(global_object, global_proxy);
3698 HookUpGlobalObject(global_object);
Andrei Popescu402d9372010-02-26 13:31:12 +00003699
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003700 if (!ConfigureGlobalObjects(global_proxy_template)) return;
Andrei Popescu31002712010-02-23 13:46:05 +00003701 } else {
3702 // We get here if there was no context snapshot.
3703 CreateRoots();
Ben Murdoch257744e2011-11-30 15:57:28 +00003704 Handle<JSFunction> empty_function = CreateEmptyFunction(isolate);
Steve Block44f0eee2011-05-26 01:26:41 +01003705 CreateStrictModeFunctionMaps(empty_function);
Ben Murdochc5610432016-08-08 18:44:38 +01003706 CreateIteratorMaps(empty_function);
3707 CreateAsyncFunctionMaps(empty_function);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003708 Handle<JSGlobalObject> global_object =
3709 CreateNewGlobals(global_proxy_template, global_proxy);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003710 HookUpGlobalProxy(global_object, global_proxy);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003711 InitializeGlobal(global_object, empty_function, context_type);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003712 InitializeNormalizedMapCaches();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003713
3714 if (!InstallNatives(context_type)) return;
Steve Blocka7e24c12009-10-30 11:49:00 +00003715
Andrei Popescu31002712010-02-23 13:46:05 +00003716 MakeFunctionInstancePrototypeWritable();
Steve Blocka7e24c12009-10-30 11:49:00 +00003717
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003718 if (context_type != THIN_CONTEXT) {
3719 if (!InstallExtraNatives()) return;
3720 if (!ConfigureGlobalObjects(global_proxy_template)) return;
3721 }
Steve Block44f0eee2011-05-26 01:26:41 +01003722 isolate->counters()->contexts_created_from_scratch()->Increment();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003723 // Re-initialize the counter because it got incremented during snapshot
3724 // creation.
3725 isolate->native_context()->set_errors_thrown(Smi::FromInt(0));
Andrei Popescu31002712010-02-23 13:46:05 +00003726 }
Steve Blocka7e24c12009-10-30 11:49:00 +00003727
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003728 // Install experimental natives. Do not include them into the
3729 // snapshot as we should be able to turn them off at runtime. Re-installing
3730 // them after they have already been deserialized would also fail.
3731 if (context_type == FULL_CONTEXT) {
3732 if (!isolate->serializer_enabled()) {
3733 InitializeExperimentalGlobal();
3734 if (!InstallExperimentalNatives()) return;
Ben Murdoch257744e2011-11-30 15:57:28 +00003735
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003736 if (FLAG_experimental_extras) {
3737 if (!InstallExperimentalExtraNatives()) return;
3738 }
3739 }
3740 // The serializer cannot serialize typed arrays. Reset those typed arrays
3741 // for each new context.
3742 } else if (context_type == DEBUG_CONTEXT) {
3743 DCHECK(!isolate->serializer_enabled());
3744 InitializeExperimentalGlobal();
3745 if (!InstallDebuggerNatives()) return;
3746 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003747
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003748 ConfigureUtilsObject(context_type);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003749
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003750 // Check that the script context table is empty except for the 'this' binding.
3751 // We do not need script contexts for native scripts.
3752 if (!FLAG_global_var_shortcuts) {
3753 DCHECK_EQ(1, native_context()->script_context_table()->used());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003754 }
3755
3756 result_ = native_context();
Steve Blocka7e24c12009-10-30 11:49:00 +00003757}
3758
3759
3760// Support for thread preemption.
3761
3762// Reserve space for statics needing saving and restoring.
3763int Bootstrapper::ArchiveSpacePerThread() {
Steve Block44f0eee2011-05-26 01:26:41 +01003764 return sizeof(NestingCounterType);
Steve Blocka7e24c12009-10-30 11:49:00 +00003765}
3766
3767
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003768// Archive statics that are thread-local.
Steve Blocka7e24c12009-10-30 11:49:00 +00003769char* Bootstrapper::ArchiveState(char* to) {
Steve Block44f0eee2011-05-26 01:26:41 +01003770 *reinterpret_cast<NestingCounterType*>(to) = nesting_;
3771 nesting_ = 0;
3772 return to + sizeof(NestingCounterType);
Steve Blocka7e24c12009-10-30 11:49:00 +00003773}
3774
3775
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003776// Restore statics that are thread-local.
Steve Blocka7e24c12009-10-30 11:49:00 +00003777char* Bootstrapper::RestoreState(char* from) {
Steve Block44f0eee2011-05-26 01:26:41 +01003778 nesting_ = *reinterpret_cast<NestingCounterType*>(from);
3779 return from + sizeof(NestingCounterType);
Steve Blocka7e24c12009-10-30 11:49:00 +00003780}
3781
3782
3783// Called when the top-level V8 mutex is destroyed.
3784void Bootstrapper::FreeThreadResources() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003785 DCHECK(!IsActive());
Steve Blocka7e24c12009-10-30 11:49:00 +00003786}
3787
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003788} // namespace internal
3789} // namespace v8