blob: f27ca7ab8d687ac4f9de190edbdf6f247a7f7c21 [file] [log] [blame]
danno@chromium.orgfa458e42012-02-01 10:48:36 +00001// Copyright 2012 the V8 project authors. All rights reserved.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28#include "v8.h"
29
30#include "accessors.h"
31#include "api.h"
32#include "bootstrapper.h"
33#include "compiler.h"
34#include "debug.h"
35#include "execution.h"
36#include "global-handles.h"
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +000037#include "isolate-inl.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000038#include "macro-assembler.h"
39#include "natives.h"
ager@chromium.orgea4f62e2010-08-16 16:28:43 +000040#include "objects-visiting.h"
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +000041#include "platform.h"
ager@chromium.orgc4c92722009-11-18 14:12:51 +000042#include "snapshot.h"
machenbach@chromium.org90dca012013-11-22 10:04:21 +000043#include "trig-table.h"
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +000044#include "extensions/externalize-string-extension.h"
machenbach@chromium.orgafbdadc2013-12-09 16:12:18 +000045#include "extensions/free-buffer-extension.h"
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +000046#include "extensions/gc-extension.h"
yangguo@chromium.org304cc332012-07-24 07:59:48 +000047#include "extensions/statistics-extension.h"
machenbach@chromium.orgafbdadc2013-12-09 16:12:18 +000048#include "extensions/trigger-failure-extension.h"
danno@chromium.orgca29dd82013-04-26 11:59:48 +000049#include "code-stubs.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000050
kasperl@chromium.org71affb52009-05-26 05:44:31 +000051namespace v8 {
52namespace internal {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000053
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000054
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000055NativesExternalStringResource::NativesExternalStringResource(
56 Bootstrapper* bootstrapper,
jkummerow@chromium.orge297f592011-06-08 10:05:15 +000057 const char* source,
58 size_t length)
59 : data_(source), length_(length) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000060 if (bootstrapper->delete_these_non_arrays_on_tear_down_ == NULL) {
61 bootstrapper->delete_these_non_arrays_on_tear_down_ = new List<char*>(2);
ager@chromium.orgc4c92722009-11-18 14:12:51 +000062 }
63 // The resources are small objects and we only make a fixed number of
64 // them, but let's clean them up on exit for neatness.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000065 bootstrapper->delete_these_non_arrays_on_tear_down_->
ager@chromium.orgc4c92722009-11-18 14:12:51 +000066 Add(reinterpret_cast<char*>(this));
67}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000068
69
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +000070Bootstrapper::Bootstrapper(Isolate* isolate)
71 : isolate_(isolate),
72 nesting_(0),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000073 extensions_cache_(Script::TYPE_EXTENSION),
74 delete_these_non_arrays_on_tear_down_(NULL),
75 delete_these_arrays_on_tear_down_(NULL) {
76}
77
78
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000079Handle<String> Bootstrapper::NativesSourceLookup(int index) {
80 ASSERT(0 <= index && index < Natives::GetBuiltinsCount());
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +000081 Heap* heap = isolate_->heap();
lrn@chromium.org7516f052011-03-30 08:52:27 +000082 if (heap->natives_source_cache()->get(index)->IsUndefined()) {
danno@chromium.orgfa458e42012-02-01 10:48:36 +000083 // We can use external strings for the natives.
84 Vector<const char> source = Natives::GetRawScriptSource(index);
85 NativesExternalStringResource* resource =
86 new NativesExternalStringResource(this,
87 source.start(),
88 source.length());
89 Handle<String> source_code =
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +000090 isolate_->factory()->NewExternalStringFromAscii(resource);
danno@chromium.orgfa458e42012-02-01 10:48:36 +000091 heap->natives_source_cache()->set(index, *source_code);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000092 }
ulan@chromium.org09d7ab52013-02-25 15:50:35 +000093 Handle<Object> cached_source(heap->natives_source_cache()->get(index),
94 isolate_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000095 return Handle<String>::cast(cached_source);
96}
97
98
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000099void Bootstrapper::Initialize(bool create_heap_objects) {
dslomov@chromium.orge97852d2013-09-12 09:02:59 +0000100 extensions_cache_.Initialize(isolate_, create_heap_objects);
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +0000101}
102
103
104void Bootstrapper::InitializeOncePerProcess() {
machenbach@chromium.orgc86e8c22013-11-27 15:11:04 +0000105#ifdef ADDRESS_SANITIZER
106 FreeBufferExtension::Register();
107#endif
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +0000108 GCExtension::Register();
109 ExternalizeStringExtension::Register();
yangguo@chromium.org304cc332012-07-24 07:59:48 +0000110 StatisticsExtension::Register();
machenbach@chromium.orgafbdadc2013-12-09 16:12:18 +0000111 TriggerFailureExtension::Register();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000112}
113
114
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000115char* Bootstrapper::AllocateAutoDeletedArray(int bytes) {
116 char* memory = new char[bytes];
117 if (memory != NULL) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000118 if (delete_these_arrays_on_tear_down_ == NULL) {
119 delete_these_arrays_on_tear_down_ = new List<char*>(2);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000120 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000121 delete_these_arrays_on_tear_down_->Add(memory);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000122 }
123 return memory;
124}
125
126
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000127void Bootstrapper::TearDown() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000128 if (delete_these_non_arrays_on_tear_down_ != NULL) {
129 int len = delete_these_non_arrays_on_tear_down_->length();
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000130 ASSERT(len < 20); // Don't use this mechanism for unbounded allocations.
131 for (int i = 0; i < len; i++) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000132 delete delete_these_non_arrays_on_tear_down_->at(i);
133 delete_these_non_arrays_on_tear_down_->at(i) = NULL;
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000134 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000135 delete delete_these_non_arrays_on_tear_down_;
136 delete_these_non_arrays_on_tear_down_ = NULL;
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000137 }
138
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000139 if (delete_these_arrays_on_tear_down_ != NULL) {
140 int len = delete_these_arrays_on_tear_down_->length();
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000141 ASSERT(len < 1000); // Don't use this mechanism for unbounded allocations.
142 for (int i = 0; i < len; i++) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000143 delete[] delete_these_arrays_on_tear_down_->at(i);
144 delete_these_arrays_on_tear_down_->at(i) = NULL;
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000145 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000146 delete delete_these_arrays_on_tear_down_;
147 delete_these_arrays_on_tear_down_ = NULL;
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000148 }
149
dslomov@chromium.orge97852d2013-09-12 09:02:59 +0000150 extensions_cache_.Initialize(isolate_, false); // Yes, symmetrical
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000151}
152
153
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000154class Genesis BASE_EMBEDDED {
155 public:
danno@chromium.org160a7b02011-04-18 15:51:38 +0000156 Genesis(Isolate* isolate,
157 Handle<Object> global_object,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000158 v8::Handle<v8::ObjectTemplate> global_template,
159 v8::ExtensionConfiguration* extensions);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000160 ~Genesis() { }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000161
danno@chromium.org160a7b02011-04-18 15:51:38 +0000162 Isolate* isolate() const { return isolate_; }
163 Factory* factory() const { return isolate_->factory(); }
164 Heap* heap() const { return isolate_->heap(); }
165
jkummerow@chromium.org7bd87f02013-03-20 18:06:29 +0000166 Handle<Context> result() { return result_; }
jkummerow@chromium.org8718d732013-03-19 22:53:30 +0000167
jkummerow@chromium.org7bd87f02013-03-20 18:06:29 +0000168 private:
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000169 Handle<Context> native_context() { return native_context_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000170
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000171 // Creates some basic objects. Used for creating a context from scratch.
172 void CreateRoots();
173 // Creates the empty function. Used for creating a context from scratch.
danno@chromium.org160a7b02011-04-18 15:51:38 +0000174 Handle<JSFunction> CreateEmptyFunction(Isolate* isolate);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000175 // Creates the ThrowTypeError function. ECMA 5th Ed. 13.2.3
danno@chromium.org40cb8782011-05-25 07:58:50 +0000176 Handle<JSFunction> GetThrowTypeErrorFunction();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000177
178 void CreateStrictModeFunctionMaps(Handle<JSFunction> empty);
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000179
180 // Make the "arguments" and "caller" properties throw a TypeError on access.
181 void PoisonArgumentsAndCaller(Handle<Map> map);
182
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000183 // Creates the global objects using the global and the template passed in
184 // through the API. We call this regardless of whether we are building a
185 // context from scratch or using a deserialized one from the partial snapshot
186 // but in the latter case we don't use the objects it produces directly, as
187 // we have to used the deserialized ones that are linked together with the
188 // rest of the context snapshot.
189 Handle<JSGlobalProxy> CreateNewGlobals(
190 v8::Handle<v8::ObjectTemplate> global_template,
191 Handle<Object> global_object,
192 Handle<GlobalObject>* global_proxy_out);
193 // Hooks the given global proxy into the context. If the context was created
194 // by deserialization then this will unhook the global proxy that was
195 // deserialized, leaving the GC to pick it up.
196 void HookUpGlobalProxy(Handle<GlobalObject> inner_global,
197 Handle<JSGlobalProxy> global_proxy);
198 // Similarly, we want to use the inner global that has been created by the
199 // templates passed through the API. The inner global from the snapshot is
200 // detached from the other objects in the snapshot.
201 void HookUpInnerGlobal(Handle<GlobalObject> inner_global);
202 // New context initialization. Used for creating a context from scratch.
jkummerow@chromium.orgba72ec82013-07-22 09:21:20 +0000203 void InitializeGlobal(Handle<GlobalObject> inner_global,
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000204 Handle<JSFunction> empty_function);
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +0000205 void InitializeExperimentalGlobal();
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000206 // Installs the contents of the native .js files on the global objects.
207 // Used for creating a context from scratch.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000208 void InstallNativeFunctions();
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000209 void InstallExperimentalNativeFunctions();
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +0000210 Handle<JSFunction> InstallInternalArray(Handle<JSBuiltinsObject> builtins,
211 const char* name,
212 ElementsKind elements_kind);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000213 bool InstallNatives();
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000214
jkummerow@chromium.org4e308cf2013-05-17 13:39:16 +0000215 Handle<JSFunction> InstallTypedArray(const char* name,
216 ElementsKind elementsKind);
danno@chromium.org160a7b02011-04-18 15:51:38 +0000217 bool InstallExperimentalNatives();
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000218 void InstallBuiltinFunctionIds();
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000219 void InstallJSFunctionResultCaches();
ricow@chromium.org65fae842010-08-25 15:26:24 +0000220 void InitializeNormalizedMapCaches();
ricow@chromium.org27bf2882011-11-17 08:34:43 +0000221
222 enum ExtensionTraversalState {
223 UNVISITED, VISITED, INSTALLED
224 };
225
226 class ExtensionStates {
yangguo@chromium.orgab30bb82012-02-24 14:41:46 +0000227 public:
ricow@chromium.org27bf2882011-11-17 08:34:43 +0000228 ExtensionStates();
229 ExtensionTraversalState get_state(RegisteredExtension* extension);
230 void set_state(RegisteredExtension* extension,
231 ExtensionTraversalState state);
yangguo@chromium.orgab30bb82012-02-24 14:41:46 +0000232 private:
ricow@chromium.org27bf2882011-11-17 08:34:43 +0000233 HashMap map_;
234 DISALLOW_COPY_AND_ASSIGN(ExtensionStates);
235 };
236
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000237 // Used both for deserialized and from-scratch contexts to add the extensions
238 // provided.
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000239 static bool InstallExtensions(Handle<Context> native_context,
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000240 v8::ExtensionConfiguration* extensions);
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +0000241 static bool InstallExtension(Isolate* isolate,
242 const char* name,
ricow@chromium.org27bf2882011-11-17 08:34:43 +0000243 ExtensionStates* extension_states);
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +0000244 static bool InstallExtension(Isolate* isolate,
245 v8::RegisteredExtension* current,
ricow@chromium.org27bf2882011-11-17 08:34:43 +0000246 ExtensionStates* extension_states);
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000247 static void InstallSpecialObjects(Handle<Context> native_context);
ager@chromium.org5c838252010-02-19 08:53:10 +0000248 bool InstallJSBuiltins(Handle<JSBuiltinsObject> builtins);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000249 bool ConfigureApiObject(Handle<JSObject> object,
250 Handle<ObjectTemplateInfo> object_template);
251 bool ConfigureGlobalObjects(v8::Handle<v8::ObjectTemplate> global_template);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000252
253 // Migrates all properties from the 'from' object to the 'to'
254 // object and overrides the prototype in 'to' with the one from
255 // 'from'.
256 void TransferObject(Handle<JSObject> from, Handle<JSObject> to);
257 void TransferNamedProperties(Handle<JSObject> from, Handle<JSObject> to);
258 void TransferIndexedProperties(Handle<JSObject> from, Handle<JSObject> to);
259
kmillikin@chromium.org4111b802010-05-03 10:34:42 +0000260 enum PrototypePropertyMode {
261 DONT_ADD_PROTOTYPE,
262 ADD_READONLY_PROTOTYPE,
263 ADD_WRITEABLE_PROTOTYPE
264 };
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000265
266 Handle<Map> CreateFunctionMap(PrototypePropertyMode prototype_mode);
267
yangguo@chromium.org304cc332012-07-24 07:59:48 +0000268 void SetFunctionInstanceDescriptor(Handle<Map> map,
269 PrototypePropertyMode prototypeMode);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000270 void MakeFunctionInstancePrototypeWritable();
271
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000272 Handle<Map> CreateStrictModeFunctionMap(
273 PrototypePropertyMode prototype_mode,
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000274 Handle<JSFunction> empty_function);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000275
yangguo@chromium.org304cc332012-07-24 07:59:48 +0000276 void SetStrictFunctionInstanceDescriptor(Handle<Map> map,
277 PrototypePropertyMode propertyMode);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000278
danno@chromium.org160a7b02011-04-18 15:51:38 +0000279 static bool CompileBuiltin(Isolate* isolate, int index);
280 static bool CompileExperimentalBuiltin(Isolate* isolate, int index);
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +0000281 static bool CompileNative(Isolate* isolate,
282 Vector<const char> name,
283 Handle<String> source);
284 static bool CompileScriptCached(Isolate* isolate,
285 Vector<const char> name,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000286 Handle<String> source,
287 SourceCodeCache* cache,
288 v8::Extension* extension,
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000289 Handle<Context> top_context,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000290 bool use_runtime_context);
291
jkummerow@chromium.org7bd87f02013-03-20 18:06:29 +0000292 Isolate* isolate_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000293 Handle<Context> result_;
jkummerow@chromium.org7bd87f02013-03-20 18:06:29 +0000294 Handle<Context> native_context_;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000295
ulan@chromium.org906e2fb2013-05-14 08:14:38 +0000296 // Function maps. Function maps are created initially with a read only
297 // prototype for the processing of JS builtins. Later the function maps are
298 // replaced in order to make prototype writable. These are the final, writable
299 // prototype, maps.
300 Handle<Map> function_map_writable_prototype_;
301 Handle<Map> strict_mode_function_map_writable_prototype_;
danno@chromium.org40cb8782011-05-25 07:58:50 +0000302 Handle<JSFunction> throw_type_error_function;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000303
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000304 BootstrapperActive active_;
305 friend class Bootstrapper;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000306};
307
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000308
309void Bootstrapper::Iterate(ObjectVisitor* v) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000310 extensions_cache_.Iterate(v);
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +0000311 v->Synchronize(VisitorSynchronization::kExtensions);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000312}
313
314
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000315Handle<Context> Bootstrapper::CreateEnvironment(
316 Handle<Object> global_object,
317 v8::Handle<v8::ObjectTemplate> global_template,
318 v8::ExtensionConfiguration* extensions) {
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +0000319 HandleScope scope(isolate_);
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +0000320 Genesis genesis(isolate_, global_object, global_template, extensions);
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000321 Handle<Context> env = genesis.result();
322 if (env.is_null() || !InstallExtensions(env, extensions)) {
323 return Handle<Context>();
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000324 }
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000325 return scope.CloseAndEscape(env);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000326}
327
328
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000329static void SetObjectPrototype(Handle<JSObject> object, Handle<Object> proto) {
330 // object.__proto__ = proto;
danno@chromium.org160a7b02011-04-18 15:51:38 +0000331 Factory* factory = object->GetIsolate()->factory();
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000332 Handle<Map> old_to_map = Handle<Map>(object->map());
verwaest@chromium.org753aee42012-07-17 16:15:42 +0000333 Handle<Map> new_to_map = factory->CopyMap(old_to_map);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000334 new_to_map->set_prototype(*proto);
335 object->set_map(*new_to_map);
336}
337
338
339void Bootstrapper::DetachGlobal(Handle<Context> env) {
danno@chromium.org160a7b02011-04-18 15:51:38 +0000340 Factory* factory = env->GetIsolate()->factory();
yangguo@chromium.org355cfd12012-08-29 15:32:24 +0000341 Handle<JSGlobalProxy> global_proxy(JSGlobalProxy::cast(env->global_proxy()));
342 global_proxy->set_native_context(*factory->null_value());
343 SetObjectPrototype(global_proxy, factory->null_value());
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +0000344}
345
346
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000347static Handle<JSFunction> InstallFunction(Handle<JSObject> target,
348 const char* name,
349 InstanceType type,
350 int instance_size,
351 Handle<JSObject> prototype,
352 Builtins::Name call,
jkummerow@chromium.org4e308cf2013-05-17 13:39:16 +0000353 bool install_initial_map,
354 bool set_instance_class_name) {
danno@chromium.org160a7b02011-04-18 15:51:38 +0000355 Isolate* isolate = target->GetIsolate();
lrn@chromium.org7516f052011-03-30 08:52:27 +0000356 Factory* factory = isolate->factory();
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000357 Handle<String> internalized_name = factory->InternalizeUtf8String(name);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000358 Handle<Code> call_code = Handle<Code>(isolate->builtins()->builtin(call));
kmillikin@chromium.org4111b802010-05-03 10:34:42 +0000359 Handle<JSFunction> function = prototype.is_null() ?
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000360 factory->NewFunctionWithoutPrototype(internalized_name, call_code) :
361 factory->NewFunctionWithPrototype(internalized_name,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000362 type,
363 instance_size,
364 prototype,
365 call_code,
jkummerow@chromium.org4e308cf2013-05-17 13:39:16 +0000366 install_initial_map);
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000367 PropertyAttributes attributes;
368 if (target->IsJSBuiltinsObject()) {
369 attributes =
370 static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE | READ_ONLY);
371 } else {
372 attributes = DONT_ENUM;
373 }
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000374 CHECK_NOT_EMPTY_HANDLE(isolate,
375 JSObject::SetLocalPropertyIgnoreAttributes(
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000376 target, internalized_name, function, attributes));
jkummerow@chromium.org4e308cf2013-05-17 13:39:16 +0000377 if (set_instance_class_name) {
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000378 function->shared()->set_instance_class_name(*internalized_name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000379 }
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000380 function->shared()->set_native(true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000381 return function;
382}
383
384
yangguo@chromium.org304cc332012-07-24 07:59:48 +0000385void Genesis::SetFunctionInstanceDescriptor(
386 Handle<Map> map, PrototypePropertyMode prototypeMode) {
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000387 int size = (prototypeMode == DONT_ADD_PROTOTYPE) ? 4 : 5;
verwaest@chromium.org33e09c82012-10-10 17:07:22 +0000388 Handle<DescriptorArray> descriptors(factory()->NewDescriptorArray(0, size));
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000389 DescriptorArray::WhitenessWitness witness(*descriptors);
390
yangguo@chromium.org304cc332012-07-24 07:59:48 +0000391 Handle<Foreign> length(factory()->NewForeign(&Accessors::FunctionLength));
392 Handle<Foreign> name(factory()->NewForeign(&Accessors::FunctionName));
393 Handle<Foreign> args(factory()->NewForeign(&Accessors::FunctionArguments));
394 Handle<Foreign> caller(factory()->NewForeign(&Accessors::FunctionCaller));
395 Handle<Foreign> prototype;
396 if (prototypeMode != DONT_ADD_PROTOTYPE) {
397 prototype = factory()->NewForeign(&Accessors::FunctionPrototype);
398 }
399 PropertyAttributes attribs = static_cast<PropertyAttributes>(
400 DONT_ENUM | DONT_DELETE | READ_ONLY);
rossberg@chromium.org89e18f52012-10-22 13:09:53 +0000401 map->set_instance_descriptors(*descriptors);
yangguo@chromium.org304cc332012-07-24 07:59:48 +0000402
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000403 { // Add length.
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000404 CallbacksDescriptor d(*factory()->length_string(), *length, attribs);
yangguo@chromium.org304cc332012-07-24 07:59:48 +0000405 map->AppendDescriptor(&d, witness);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000406 }
407 { // Add name.
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000408 CallbacksDescriptor d(*factory()->name_string(), *name, attribs);
yangguo@chromium.org304cc332012-07-24 07:59:48 +0000409 map->AppendDescriptor(&d, witness);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000410 }
411 { // Add arguments.
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000412 CallbacksDescriptor d(*factory()->arguments_string(), *args, attribs);
yangguo@chromium.org304cc332012-07-24 07:59:48 +0000413 map->AppendDescriptor(&d, witness);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000414 }
415 { // Add caller.
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000416 CallbacksDescriptor d(*factory()->caller_string(), *caller, attribs);
yangguo@chromium.org304cc332012-07-24 07:59:48 +0000417 map->AppendDescriptor(&d, witness);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000418 }
419 if (prototypeMode != DONT_ADD_PROTOTYPE) {
420 // Add prototype.
421 if (prototypeMode == ADD_WRITEABLE_PROTOTYPE) {
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000422 attribs = static_cast<PropertyAttributes>(attribs & ~READ_ONLY);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000423 }
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000424 CallbacksDescriptor d(*factory()->prototype_string(), *prototype, attribs);
yangguo@chromium.org304cc332012-07-24 07:59:48 +0000425 map->AppendDescriptor(&d, witness);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000426 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000427}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000428
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000429
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000430Handle<Map> Genesis::CreateFunctionMap(PrototypePropertyMode prototype_mode) {
danno@chromium.org160a7b02011-04-18 15:51:38 +0000431 Handle<Map> map = factory()->NewMap(JS_FUNCTION_TYPE, JSFunction::kSize);
yangguo@chromium.org304cc332012-07-24 07:59:48 +0000432 SetFunctionInstanceDescriptor(map, prototype_mode);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000433 map->set_function_with_prototype(prototype_mode != DONT_ADD_PROTOTYPE);
434 return map;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000435}
436
437
danno@chromium.org160a7b02011-04-18 15:51:38 +0000438Handle<JSFunction> Genesis::CreateEmptyFunction(Isolate* isolate) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000439 // Allocate the map for function instances. Maps are allocated first and their
440 // prototypes patched later, once empty function is created.
441
kmillikin@chromium.org4111b802010-05-03 10:34:42 +0000442 // Functions with this map will not have a 'prototype' property, and
443 // can not be used as constructors.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000444 Handle<Map> function_without_prototype_map =
445 CreateFunctionMap(DONT_ADD_PROTOTYPE);
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000446 native_context()->set_function_without_prototype_map(
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000447 *function_without_prototype_map);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000448
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000449 // Allocate the function map. This map is temporary, used only for processing
450 // of builtins.
451 // Later the map is replaced with writable prototype map, allocated below.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000452 Handle<Map> function_map = CreateFunctionMap(ADD_READONLY_PROTOTYPE);
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000453 native_context()->set_function_map(*function_map);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000454
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000455 // The final map for functions. Writeable prototype.
456 // This map is installed in MakeFunctionInstancePrototypeWritable.
ulan@chromium.org906e2fb2013-05-14 08:14:38 +0000457 function_map_writable_prototype_ = CreateFunctionMap(ADD_WRITEABLE_PROTOTYPE);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000458
lrn@chromium.org7516f052011-03-30 08:52:27 +0000459 Factory* factory = isolate->factory();
lrn@chromium.org7516f052011-03-30 08:52:27 +0000460
ulan@chromium.org906e2fb2013-05-14 08:14:38 +0000461 Handle<String> object_name = factory->Object_string();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000462
463 { // --- O b j e c t ---
464 Handle<JSFunction> object_fun =
lrn@chromium.org7516f052011-03-30 08:52:27 +0000465 factory->NewFunction(object_name, factory->null_value());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000466 Handle<Map> object_function_map =
lrn@chromium.org7516f052011-03-30 08:52:27 +0000467 factory->NewMap(JS_OBJECT_TYPE, JSObject::kHeaderSize);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000468 object_fun->set_initial_map(*object_function_map);
469 object_function_map->set_constructor(*object_fun);
470
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000471 native_context()->set_object_function(*object_fun);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000472
473 // Allocate a new prototype for the object function.
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000474 Handle<JSObject> prototype = factory->NewJSObject(
475 isolate->object_function(),
mstarzinger@chromium.org71fc3462013-02-27 09:34:27 +0000476 TENURED);
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000477
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000478 native_context()->set_initial_object_prototype(*prototype);
ulan@chromium.org906e2fb2013-05-14 08:14:38 +0000479 // For bootstrapping set the array prototype to be the same as the object
480 // prototype, otherwise the missing initial_array_prototype will cause
481 // assertions during startup.
482 native_context()->set_initial_array_prototype(*prototype);
rossberg@chromium.orgebeba022013-08-19 09:36:44 +0000483 Accessors::FunctionSetPrototype(object_fun, prototype);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000484 }
485
486 // Allocate the empty function as the prototype for function ECMAScript
487 // 262 15.3.4.
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000488 Handle<String> empty_string =
489 factory->InternalizeOneByteString(STATIC_ASCII_VECTOR("Empty"));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000490 Handle<JSFunction> empty_function =
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000491 factory->NewFunctionWithoutPrototype(empty_string, CLASSIC_MODE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000492
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000493 // --- E m p t y ---
494 Handle<Code> code =
lrn@chromium.org7516f052011-03-30 08:52:27 +0000495 Handle<Code>(isolate->builtins()->builtin(
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000496 Builtins::kEmptyFunction));
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000497 empty_function->set_code(*code);
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000498 empty_function->shared()->set_code(*code);
jkummerow@chromium.org59297c72013-01-09 16:32:23 +0000499 Handle<String> source =
500 factory->NewStringFromOneByte(STATIC_ASCII_VECTOR("() {}"));
lrn@chromium.org7516f052011-03-30 08:52:27 +0000501 Handle<Script> script = factory->NewScript(source);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000502 script->set_type(Smi::FromInt(Script::TYPE_NATIVE));
503 empty_function->shared()->set_script(*script);
504 empty_function->shared()->set_start_position(0);
505 empty_function->shared()->set_end_position(source->length());
506 empty_function->shared()->DontAdaptArguments();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000507
508 // Set prototypes for the function maps.
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000509 native_context()->function_map()->set_prototype(*empty_function);
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000510 native_context()->function_without_prototype_map()->
kmillikin@chromium.org4111b802010-05-03 10:34:42 +0000511 set_prototype(*empty_function);
ulan@chromium.org906e2fb2013-05-14 08:14:38 +0000512 function_map_writable_prototype_->set_prototype(*empty_function);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000513
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000514 // Allocate the function map first and then patch the prototype later
yangguo@chromium.org5f0b8ea2012-05-16 12:37:04 +0000515 Handle<Map> empty_function_map = CreateFunctionMap(DONT_ADD_PROTOTYPE);
516 empty_function_map->set_prototype(
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000517 native_context()->object_function()->prototype());
yangguo@chromium.org5f0b8ea2012-05-16 12:37:04 +0000518 empty_function->set_map(*empty_function_map);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000519 return empty_function;
520}
521
522
yangguo@chromium.org304cc332012-07-24 07:59:48 +0000523void Genesis::SetStrictFunctionInstanceDescriptor(
524 Handle<Map> map, PrototypePropertyMode prototypeMode) {
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000525 int size = (prototypeMode == DONT_ADD_PROTOTYPE) ? 4 : 5;
verwaest@chromium.org33e09c82012-10-10 17:07:22 +0000526 Handle<DescriptorArray> descriptors(factory()->NewDescriptorArray(0, size));
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000527 DescriptorArray::WhitenessWitness witness(*descriptors);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000528
yangguo@chromium.org304cc332012-07-24 07:59:48 +0000529 Handle<Foreign> length(factory()->NewForeign(&Accessors::FunctionLength));
530 Handle<Foreign> name(factory()->NewForeign(&Accessors::FunctionName));
531 Handle<AccessorPair> arguments(factory()->NewAccessorPair());
532 Handle<AccessorPair> caller(factory()->NewAccessorPair());
533 Handle<Foreign> prototype;
534 if (prototypeMode != DONT_ADD_PROTOTYPE) {
535 prototype = factory()->NewForeign(&Accessors::FunctionPrototype);
536 }
dslomov@chromium.orgb752d402013-06-18 11:54:54 +0000537 PropertyAttributes rw_attribs =
538 static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE);
539 PropertyAttributes ro_attribs =
540 static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE | READ_ONLY);
rossberg@chromium.org89e18f52012-10-22 13:09:53 +0000541 map->set_instance_descriptors(*descriptors);
yangguo@chromium.org304cc332012-07-24 07:59:48 +0000542
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000543 { // Add length.
dslomov@chromium.orgb752d402013-06-18 11:54:54 +0000544 CallbacksDescriptor d(*factory()->length_string(), *length, ro_attribs);
yangguo@chromium.org304cc332012-07-24 07:59:48 +0000545 map->AppendDescriptor(&d, witness);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000546 }
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000547 { // Add name.
hpayer@chromium.orgea9b8ba2013-12-20 19:22:39 +0000548 CallbacksDescriptor d(*factory()->name_string(), *name, ro_attribs);
yangguo@chromium.org304cc332012-07-24 07:59:48 +0000549 map->AppendDescriptor(&d, witness);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000550 }
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000551 { // Add arguments.
dslomov@chromium.orgb752d402013-06-18 11:54:54 +0000552 CallbacksDescriptor d(*factory()->arguments_string(), *arguments,
553 rw_attribs);
yangguo@chromium.org304cc332012-07-24 07:59:48 +0000554 map->AppendDescriptor(&d, witness);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000555 }
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000556 { // Add caller.
dslomov@chromium.orgb752d402013-06-18 11:54:54 +0000557 CallbacksDescriptor d(*factory()->caller_string(), *caller, rw_attribs);
yangguo@chromium.org304cc332012-07-24 07:59:48 +0000558 map->AppendDescriptor(&d, witness);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000559 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000560 if (prototypeMode != DONT_ADD_PROTOTYPE) {
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000561 // Add prototype.
dslomov@chromium.orgb752d402013-06-18 11:54:54 +0000562 PropertyAttributes attribs =
563 prototypeMode == ADD_WRITEABLE_PROTOTYPE ? rw_attribs : ro_attribs;
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000564 CallbacksDescriptor d(*factory()->prototype_string(), *prototype, attribs);
yangguo@chromium.org304cc332012-07-24 07:59:48 +0000565 map->AppendDescriptor(&d, witness);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000566 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000567}
568
569
570// ECMAScript 5th Edition, 13.2.3
danno@chromium.org40cb8782011-05-25 07:58:50 +0000571Handle<JSFunction> Genesis::GetThrowTypeErrorFunction() {
572 if (throw_type_error_function.is_null()) {
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000573 Handle<String> name = factory()->InternalizeOneByteString(
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +0000574 STATIC_ASCII_VECTOR("ThrowTypeError"));
danno@chromium.org40cb8782011-05-25 07:58:50 +0000575 throw_type_error_function =
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000576 factory()->NewFunctionWithoutPrototype(name, CLASSIC_MODE);
danno@chromium.org40cb8782011-05-25 07:58:50 +0000577 Handle<Code> code(isolate()->builtins()->builtin(
578 Builtins::kStrictModePoisonPill));
579 throw_type_error_function->set_map(
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000580 native_context()->function_map());
danno@chromium.org40cb8782011-05-25 07:58:50 +0000581 throw_type_error_function->set_code(*code);
582 throw_type_error_function->shared()->set_code(*code);
583 throw_type_error_function->shared()->DontAdaptArguments();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000584
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000585 JSObject::PreventExtensions(throw_type_error_function);
danno@chromium.org40cb8782011-05-25 07:58:50 +0000586 }
587 return throw_type_error_function;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000588}
589
590
591Handle<Map> Genesis::CreateStrictModeFunctionMap(
592 PrototypePropertyMode prototype_mode,
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000593 Handle<JSFunction> empty_function) {
danno@chromium.org160a7b02011-04-18 15:51:38 +0000594 Handle<Map> map = factory()->NewMap(JS_FUNCTION_TYPE, JSFunction::kSize);
yangguo@chromium.org304cc332012-07-24 07:59:48 +0000595 SetStrictFunctionInstanceDescriptor(map, prototype_mode);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000596 map->set_function_with_prototype(prototype_mode != DONT_ADD_PROTOTYPE);
597 map->set_prototype(*empty_function);
598 return map;
599}
600
601
602void Genesis::CreateStrictModeFunctionMaps(Handle<JSFunction> empty) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000603 // Allocate map for the prototype-less strict mode instances.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000604 Handle<Map> strict_mode_function_without_prototype_map =
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000605 CreateStrictModeFunctionMap(DONT_ADD_PROTOTYPE, empty);
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000606 native_context()->set_strict_mode_function_without_prototype_map(
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000607 *strict_mode_function_without_prototype_map);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000608
609 // Allocate map for the strict mode functions. This map is temporary, used
610 // only for processing of builtins.
611 // Later the map is replaced with writable prototype map, allocated below.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000612 Handle<Map> strict_mode_function_map =
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000613 CreateStrictModeFunctionMap(ADD_READONLY_PROTOTYPE, empty);
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000614 native_context()->set_strict_mode_function_map(
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000615 *strict_mode_function_map);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000616
617 // The final map for the strict mode functions. Writeable prototype.
618 // This map is installed in MakeFunctionInstancePrototypeWritable.
ulan@chromium.org906e2fb2013-05-14 08:14:38 +0000619 strict_mode_function_map_writable_prototype_ =
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000620 CreateStrictModeFunctionMap(ADD_WRITEABLE_PROTOTYPE, empty);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000621
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000622 // Complete the callbacks.
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000623 PoisonArgumentsAndCaller(strict_mode_function_without_prototype_map);
624 PoisonArgumentsAndCaller(strict_mode_function_map);
ulan@chromium.org906e2fb2013-05-14 08:14:38 +0000625 PoisonArgumentsAndCaller(strict_mode_function_map_writable_prototype_);
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000626}
627
628
629static void SetAccessors(Handle<Map> map,
630 Handle<String> name,
631 Handle<JSFunction> func) {
632 DescriptorArray* descs = map->instance_descriptors();
verwaest@chromium.org06ab2ec2012-10-09 17:00:13 +0000633 int number = descs->SearchWithCache(*name, *map);
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000634 AccessorPair* accessors = AccessorPair::cast(descs->GetValue(number));
635 accessors->set_getter(*func);
636 accessors->set_setter(*func);
637}
638
639
640void Genesis::PoisonArgumentsAndCaller(Handle<Map> map) {
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000641 SetAccessors(map, factory()->arguments_string(), GetThrowTypeErrorFunction());
642 SetAccessors(map, factory()->caller_string(), GetThrowTypeErrorFunction());
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000643}
644
645
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000646static void AddToWeakNativeContextList(Context* context) {
647 ASSERT(context->IsNativeContext());
danno@chromium.org160a7b02011-04-18 15:51:38 +0000648 Heap* heap = context->GetIsolate()->heap();
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000649#ifdef DEBUG
650 { // NOLINT
651 ASSERT(context->get(Context::NEXT_CONTEXT_LINK)->IsUndefined());
652 // Check that context is not in the list yet.
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000653 for (Object* current = heap->native_contexts_list();
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000654 !current->IsUndefined();
655 current = Context::cast(current)->get(Context::NEXT_CONTEXT_LINK)) {
656 ASSERT(current != context);
657 }
658 }
659#endif
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000660 context->set(Context::NEXT_CONTEXT_LINK, heap->native_contexts_list());
661 heap->set_native_contexts_list(context);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000662}
663
664
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000665void Genesis::CreateRoots() {
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000666 // Allocate the native context FixedArray first and then patch the
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000667 // closure and extension object later (we need the empty function
668 // and the global object, but in order to create those, we need the
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000669 // native context).
jkummerow@chromium.org7bd87f02013-03-20 18:06:29 +0000670 native_context_ = factory()->NewNativeContext();
671 AddToWeakNativeContextList(*native_context());
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000672 isolate()->set_context(*native_context());
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000673
674 // Allocate the message listeners object.
675 {
676 v8::NeanderArray listeners;
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000677 native_context()->set_message_listeners(*listeners.value());
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000678 }
679}
680
681
682Handle<JSGlobalProxy> Genesis::CreateNewGlobals(
683 v8::Handle<v8::ObjectTemplate> global_template,
684 Handle<Object> global_object,
685 Handle<GlobalObject>* inner_global_out) {
686 // The argument global_template aka data is an ObjectTemplateInfo.
687 // It has a constructor pointer that points at global_constructor which is a
688 // FunctionTemplateInfo.
689 // The global_constructor is used to create or reinitialize the global_proxy.
690 // The global_constructor also has a prototype_template pointer that points at
691 // js_global_template which is an ObjectTemplateInfo.
692 // That in turn has a constructor pointer that points at
693 // js_global_constructor which is a FunctionTemplateInfo.
694 // js_global_constructor is used to make js_global_function
695 // js_global_function is used to make the new inner_global.
696 //
697 // --- G l o b a l ---
698 // Step 1: Create a fresh inner JSGlobalObject.
699 Handle<JSFunction> js_global_function;
700 Handle<ObjectTemplateInfo> js_global_template;
701 if (!global_template.IsEmpty()) {
702 // Get prototype template of the global_template.
703 Handle<ObjectTemplateInfo> data =
704 v8::Utils::OpenHandle(*global_template);
705 Handle<FunctionTemplateInfo> global_constructor =
706 Handle<FunctionTemplateInfo>(
707 FunctionTemplateInfo::cast(data->constructor()));
ulan@chromium.org09d7ab52013-02-25 15:50:35 +0000708 Handle<Object> proto_template(global_constructor->prototype_template(),
709 isolate());
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000710 if (!proto_template->IsUndefined()) {
711 js_global_template =
712 Handle<ObjectTemplateInfo>::cast(proto_template);
713 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000714 }
715
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000716 if (js_global_template.is_null()) {
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000717 Handle<String> name = Handle<String>(heap()->empty_string());
danno@chromium.org160a7b02011-04-18 15:51:38 +0000718 Handle<Code> code = Handle<Code>(isolate()->builtins()->builtin(
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000719 Builtins::kIllegal));
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000720 js_global_function =
danno@chromium.org160a7b02011-04-18 15:51:38 +0000721 factory()->NewFunction(name, JS_GLOBAL_OBJECT_TYPE,
722 JSGlobalObject::kSize, code, true);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000723 // Change the constructor property of the prototype of the
724 // hidden global function to refer to the Object function.
725 Handle<JSObject> prototype =
726 Handle<JSObject>(
727 JSObject::cast(js_global_function->instance_prototype()));
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000728 CHECK_NOT_EMPTY_HANDLE(isolate(),
729 JSObject::SetLocalPropertyIgnoreAttributes(
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000730 prototype, factory()->constructor_string(),
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000731 isolate()->object_function(), NONE));
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000732 } else {
733 Handle<FunctionTemplateInfo> js_global_constructor(
734 FunctionTemplateInfo::cast(js_global_template->constructor()));
735 js_global_function =
danno@chromium.org160a7b02011-04-18 15:51:38 +0000736 factory()->CreateApiFunction(js_global_constructor,
737 factory()->InnerGlobalObject);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000738 }
739
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000740 js_global_function->initial_map()->set_is_hidden_prototype();
erik.corry@gmail.com88767242012-08-08 14:43:45 +0000741 js_global_function->initial_map()->set_dictionary_map(true);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000742 Handle<GlobalObject> inner_global =
danno@chromium.org160a7b02011-04-18 15:51:38 +0000743 factory()->NewGlobalObject(js_global_function);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000744 if (inner_global_out != NULL) {
745 *inner_global_out = inner_global;
746 }
747
748 // Step 2: create or re-initialize the global proxy object.
749 Handle<JSFunction> global_proxy_function;
750 if (global_template.IsEmpty()) {
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000751 Handle<String> name = Handle<String>(heap()->empty_string());
danno@chromium.org160a7b02011-04-18 15:51:38 +0000752 Handle<Code> code = Handle<Code>(isolate()->builtins()->builtin(
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000753 Builtins::kIllegal));
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000754 global_proxy_function =
danno@chromium.org160a7b02011-04-18 15:51:38 +0000755 factory()->NewFunction(name, JS_GLOBAL_PROXY_TYPE,
756 JSGlobalProxy::kSize, code, true);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000757 } else {
758 Handle<ObjectTemplateInfo> data =
759 v8::Utils::OpenHandle(*global_template);
760 Handle<FunctionTemplateInfo> global_constructor(
761 FunctionTemplateInfo::cast(data->constructor()));
762 global_proxy_function =
danno@chromium.org160a7b02011-04-18 15:51:38 +0000763 factory()->CreateApiFunction(global_constructor,
764 factory()->OuterGlobalObject);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000765 }
766
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000767 Handle<String> global_name = factory()->InternalizeOneByteString(
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +0000768 STATIC_ASCII_VECTOR("global"));
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000769 global_proxy_function->shared()->set_instance_class_name(*global_name);
770 global_proxy_function->initial_map()->set_is_access_check_needed(true);
771
772 // Set global_proxy.__proto__ to js_global after ConfigureGlobalObjects
773 // Return the global proxy.
774
775 if (global_object.location() != NULL) {
776 ASSERT(global_object->IsJSGlobalProxy());
777 return ReinitializeJSGlobalProxy(
778 global_proxy_function,
779 Handle<JSGlobalProxy>::cast(global_object));
780 } else {
781 return Handle<JSGlobalProxy>::cast(
danno@chromium.org160a7b02011-04-18 15:51:38 +0000782 factory()->NewJSObject(global_proxy_function, TENURED));
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000783 }
784}
785
786
787void Genesis::HookUpGlobalProxy(Handle<GlobalObject> inner_global,
788 Handle<JSGlobalProxy> global_proxy) {
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000789 // Set the native context for the global object.
790 inner_global->set_native_context(*native_context());
yangguo@chromium.org355cfd12012-08-29 15:32:24 +0000791 inner_global->set_global_context(*native_context());
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000792 inner_global->set_global_receiver(*global_proxy);
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000793 global_proxy->set_native_context(*native_context());
794 native_context()->set_global_proxy(*global_proxy);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000795}
796
797
798void Genesis::HookUpInnerGlobal(Handle<GlobalObject> inner_global) {
799 Handle<GlobalObject> inner_global_from_snapshot(
jkummerow@chromium.org7bd87f02013-03-20 18:06:29 +0000800 GlobalObject::cast(native_context()->extension()));
801 Handle<JSBuiltinsObject> builtins_global(native_context()->builtins());
802 native_context()->set_extension(*inner_global);
803 native_context()->set_global_object(*inner_global);
804 native_context()->set_security_token(*inner_global);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000805 static const PropertyAttributes attributes =
806 static_cast<PropertyAttributes>(READ_ONLY | DONT_DELETE);
807 ForceSetProperty(builtins_global,
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000808 factory()->InternalizeOneByteString(
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +0000809 STATIC_ASCII_VECTOR("global")),
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000810 inner_global,
811 attributes);
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000812 // Set up the reference from the global object to the builtins object.
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000813 JSGlobalObject::cast(*inner_global)->set_builtins(*builtins_global);
814 TransferNamedProperties(inner_global_from_snapshot, inner_global);
815 TransferIndexedProperties(inner_global_from_snapshot, inner_global);
816}
817
818
819// This is only called if we are not using snapshots. The equivalent
820// work in the snapshot case is done in HookUpInnerGlobal.
jkummerow@chromium.orgba72ec82013-07-22 09:21:20 +0000821void Genesis::InitializeGlobal(Handle<GlobalObject> inner_global,
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000822 Handle<JSFunction> empty_function) {
mstarzinger@chromium.orga2e1a402013-10-15 08:25:05 +0000823 // --- N a t i v e C o n t e x t ---
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000824 // Use the empty function as closure (no scope info).
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000825 native_context()->set_closure(*empty_function);
826 native_context()->set_previous(NULL);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000827 // Set extension and global object.
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000828 native_context()->set_extension(*inner_global);
829 native_context()->set_global_object(*inner_global);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000830 // Security setup: Set the security token of the global object to
831 // its the inner global. This makes the security check between two
832 // different contexts fail by default even in case of global
833 // object reinitialization.
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000834 native_context()->set_security_token(*inner_global);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000835
danno@chromium.org160a7b02011-04-18 15:51:38 +0000836 Isolate* isolate = inner_global->GetIsolate();
lrn@chromium.org7516f052011-03-30 08:52:27 +0000837 Factory* factory = isolate->factory();
838 Heap* heap = isolate->heap();
839
ulan@chromium.org906e2fb2013-05-14 08:14:38 +0000840 Handle<String> object_name = factory->Object_string();
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000841 CHECK_NOT_EMPTY_HANDLE(isolate,
842 JSObject::SetLocalPropertyIgnoreAttributes(
843 inner_global, object_name,
844 isolate->object_function(), DONT_ENUM));
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000845
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000846 Handle<JSObject> global = Handle<JSObject>(native_context()->global_object());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000847
848 // Install global Function object
849 InstallFunction(global, "Function", JS_FUNCTION_TYPE, JSFunction::kSize,
jkummerow@chromium.org4e308cf2013-05-17 13:39:16 +0000850 empty_function, Builtins::kIllegal, true, true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000851
852 { // --- A r r a y ---
853 Handle<JSFunction> array_function =
854 InstallFunction(global, "Array", JS_ARRAY_TYPE, JSArray::kSize,
lrn@chromium.org7516f052011-03-30 08:52:27 +0000855 isolate->initial_object_prototype(),
jkummerow@chromium.org4e308cf2013-05-17 13:39:16 +0000856 Builtins::kArrayCode, true, true);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000857 array_function->shared()->DontAdaptArguments();
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +0000858 array_function->shared()->set_function_data(Smi::FromInt(kArrayCode));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000859
860 // This seems a bit hackish, but we need to make sure Array.length
861 // is 1.
862 array_function->shared()->set_length(1);
yangguo@chromium.org304cc332012-07-24 07:59:48 +0000863
verwaest@chromium.orgde64f722012-08-16 15:44:54 +0000864 Handle<Map> initial_map(array_function->initial_map());
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +0000865
866 // This assert protects an optimization in
867 // HGraphBuilder::JSArrayBuilder::EmitMapCode()
868 ASSERT(initial_map->elements_kind() == GetInitialFastElementsKind());
869
verwaest@chromium.org33e09c82012-10-10 17:07:22 +0000870 Handle<DescriptorArray> array_descriptors(
871 factory->NewDescriptorArray(0, 1));
yangguo@chromium.org304cc332012-07-24 07:59:48 +0000872 DescriptorArray::WhitenessWitness witness(*array_descriptors);
873
874 Handle<Foreign> array_length(factory->NewForeign(&Accessors::ArrayLength));
875 PropertyAttributes attribs = static_cast<PropertyAttributes>(
876 DONT_ENUM | DONT_DELETE);
rossberg@chromium.org89e18f52012-10-22 13:09:53 +0000877 initial_map->set_instance_descriptors(*array_descriptors);
yangguo@chromium.org304cc332012-07-24 07:59:48 +0000878
879 { // Add length.
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000880 CallbacksDescriptor d(*factory->length_string(), *array_length, attribs);
yangguo@chromium.org304cc332012-07-24 07:59:48 +0000881 array_function->initial_map()->AppendDescriptor(&d, witness);
882 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000883
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000884 // array_function is used internally. JS code creating array object should
885 // search for the 'Array' property on the global object and use that one
886 // as the constructor. 'Array' property on a global object can be
887 // overwritten by JS code.
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000888 native_context()->set_array_function(*array_function);
danno@chromium.orgca29dd82013-04-26 11:59:48 +0000889
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +0000890 // Cache the array maps, needed by ArrayConstructorStub
891 CacheInitialJSArrayMaps(native_context(), initial_map);
892 ArrayConstructorStub array_constructor_stub(isolate);
893 Handle<Code> code = array_constructor_stub.GetCode(isolate);
894 array_function->shared()->set_construct_stub(*code);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000895 }
896
897 { // --- N u m b e r ---
898 Handle<JSFunction> number_fun =
899 InstallFunction(global, "Number", JS_VALUE_TYPE, JSValue::kSize,
lrn@chromium.org7516f052011-03-30 08:52:27 +0000900 isolate->initial_object_prototype(),
jkummerow@chromium.org4e308cf2013-05-17 13:39:16 +0000901 Builtins::kIllegal, true, true);
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000902 native_context()->set_number_function(*number_fun);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000903 }
904
905 { // --- B o o l e a n ---
906 Handle<JSFunction> boolean_fun =
907 InstallFunction(global, "Boolean", JS_VALUE_TYPE, JSValue::kSize,
lrn@chromium.org7516f052011-03-30 08:52:27 +0000908 isolate->initial_object_prototype(),
jkummerow@chromium.org4e308cf2013-05-17 13:39:16 +0000909 Builtins::kIllegal, true, true);
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000910 native_context()->set_boolean_function(*boolean_fun);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000911 }
912
913 { // --- S t r i n g ---
914 Handle<JSFunction> string_fun =
915 InstallFunction(global, "String", JS_VALUE_TYPE, JSValue::kSize,
lrn@chromium.org7516f052011-03-30 08:52:27 +0000916 isolate->initial_object_prototype(),
jkummerow@chromium.org4e308cf2013-05-17 13:39:16 +0000917 Builtins::kIllegal, true, true);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000918 string_fun->shared()->set_construct_stub(
lrn@chromium.org7516f052011-03-30 08:52:27 +0000919 isolate->builtins()->builtin(Builtins::kStringConstructCode));
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000920 native_context()->set_string_function(*string_fun);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000921
922 Handle<Map> string_map =
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000923 Handle<Map>(native_context()->string_function()->initial_map());
verwaest@chromium.org33e09c82012-10-10 17:07:22 +0000924 Handle<DescriptorArray> string_descriptors(
925 factory->NewDescriptorArray(0, 1));
yangguo@chromium.org304cc332012-07-24 07:59:48 +0000926 DescriptorArray::WhitenessWitness witness(*string_descriptors);
927
928 Handle<Foreign> string_length(
929 factory->NewForeign(&Accessors::StringLength));
930 PropertyAttributes attribs = static_cast<PropertyAttributes>(
931 DONT_ENUM | DONT_DELETE | READ_ONLY);
rossberg@chromium.org89e18f52012-10-22 13:09:53 +0000932 string_map->set_instance_descriptors(*string_descriptors);
yangguo@chromium.org304cc332012-07-24 07:59:48 +0000933
934 { // Add length.
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000935 CallbacksDescriptor d(*factory->length_string(), *string_length, attribs);
yangguo@chromium.org304cc332012-07-24 07:59:48 +0000936 string_map->AppendDescriptor(&d, witness);
937 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000938 }
939
940 { // --- D a t e ---
941 // Builtin functions for Date.prototype.
942 Handle<JSFunction> date_fun =
svenpanne@chromium.org4efbdb12012-03-12 08:18:42 +0000943 InstallFunction(global, "Date", JS_DATE_TYPE, JSDate::kSize,
lrn@chromium.org7516f052011-03-30 08:52:27 +0000944 isolate->initial_object_prototype(),
jkummerow@chromium.org4e308cf2013-05-17 13:39:16 +0000945 Builtins::kIllegal, true, true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000946
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000947 native_context()->set_date_function(*date_fun);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000948 }
949
950
951 { // -- R e g E x p
952 // Builtin functions for RegExp.prototype.
953 Handle<JSFunction> regexp_fun =
ager@chromium.org236ad962008-09-25 09:45:57 +0000954 InstallFunction(global, "RegExp", JS_REGEXP_TYPE, JSRegExp::kSize,
lrn@chromium.org7516f052011-03-30 08:52:27 +0000955 isolate->initial_object_prototype(),
jkummerow@chromium.org4e308cf2013-05-17 13:39:16 +0000956 Builtins::kIllegal, true, true);
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000957 native_context()->set_regexp_function(*regexp_fun);
lrn@chromium.org25156de2010-04-06 13:10:27 +0000958
959 ASSERT(regexp_fun->has_initial_map());
960 Handle<Map> initial_map(regexp_fun->initial_map());
961
962 ASSERT_EQ(0, initial_map->inobject_properties());
963
lrn@chromium.org25156de2010-04-06 13:10:27 +0000964 PropertyAttributes final =
965 static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE | READ_ONLY);
verwaest@chromium.org33e09c82012-10-10 17:07:22 +0000966 Handle<DescriptorArray> descriptors = factory->NewDescriptorArray(0, 5);
yangguo@chromium.org304cc332012-07-24 07:59:48 +0000967 DescriptorArray::WhitenessWitness witness(*descriptors);
rossberg@chromium.org89e18f52012-10-22 13:09:53 +0000968 initial_map->set_instance_descriptors(*descriptors);
yangguo@chromium.org304cc332012-07-24 07:59:48 +0000969
lrn@chromium.org25156de2010-04-06 13:10:27 +0000970 {
971 // ECMA-262, section 15.10.7.1.
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000972 FieldDescriptor field(heap->source_string(),
lrn@chromium.org25156de2010-04-06 13:10:27 +0000973 JSRegExp::kSourceFieldIndex,
danno@chromium.orgf005df62013-04-30 16:36:45 +0000974 final,
975 Representation::Tagged());
yangguo@chromium.org304cc332012-07-24 07:59:48 +0000976 initial_map->AppendDescriptor(&field, witness);
lrn@chromium.org25156de2010-04-06 13:10:27 +0000977 }
978 {
979 // ECMA-262, section 15.10.7.2.
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000980 FieldDescriptor field(heap->global_string(),
lrn@chromium.org25156de2010-04-06 13:10:27 +0000981 JSRegExp::kGlobalFieldIndex,
danno@chromium.orgf005df62013-04-30 16:36:45 +0000982 final,
983 Representation::Tagged());
yangguo@chromium.org304cc332012-07-24 07:59:48 +0000984 initial_map->AppendDescriptor(&field, witness);
lrn@chromium.org25156de2010-04-06 13:10:27 +0000985 }
986 {
987 // ECMA-262, section 15.10.7.3.
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000988 FieldDescriptor field(heap->ignore_case_string(),
lrn@chromium.org25156de2010-04-06 13:10:27 +0000989 JSRegExp::kIgnoreCaseFieldIndex,
danno@chromium.orgf005df62013-04-30 16:36:45 +0000990 final,
991 Representation::Tagged());
yangguo@chromium.org304cc332012-07-24 07:59:48 +0000992 initial_map->AppendDescriptor(&field, witness);
lrn@chromium.org25156de2010-04-06 13:10:27 +0000993 }
994 {
995 // ECMA-262, section 15.10.7.4.
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000996 FieldDescriptor field(heap->multiline_string(),
lrn@chromium.org25156de2010-04-06 13:10:27 +0000997 JSRegExp::kMultilineFieldIndex,
danno@chromium.orgf005df62013-04-30 16:36:45 +0000998 final,
999 Representation::Tagged());
yangguo@chromium.org304cc332012-07-24 07:59:48 +00001000 initial_map->AppendDescriptor(&field, witness);
lrn@chromium.org25156de2010-04-06 13:10:27 +00001001 }
1002 {
1003 // ECMA-262, section 15.10.7.5.
1004 PropertyAttributes writable =
1005 static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE);
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001006 FieldDescriptor field(heap->last_index_string(),
lrn@chromium.org25156de2010-04-06 13:10:27 +00001007 JSRegExp::kLastIndexFieldIndex,
danno@chromium.orgf005df62013-04-30 16:36:45 +00001008 writable,
1009 Representation::Tagged());
yangguo@chromium.org304cc332012-07-24 07:59:48 +00001010 initial_map->AppendDescriptor(&field, witness);
lrn@chromium.org25156de2010-04-06 13:10:27 +00001011 }
lrn@chromium.org25156de2010-04-06 13:10:27 +00001012
1013 initial_map->set_inobject_properties(5);
1014 initial_map->set_pre_allocated_property_fields(5);
1015 initial_map->set_unused_property_fields(0);
1016 initial_map->set_instance_size(
1017 initial_map->instance_size() + 5 * kPointerSize);
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00001018 initial_map->set_visitor_id(StaticVisitorBase::GetVisitorId(*initial_map));
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001019
1020 // RegExp prototype object is itself a RegExp.
verwaest@chromium.org753aee42012-07-17 16:15:42 +00001021 Handle<Map> proto_map = factory->CopyMap(initial_map);
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001022 proto_map->set_prototype(native_context()->initial_object_prototype());
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001023 Handle<JSObject> proto = factory->NewJSObjectFromMap(proto_map);
1024 proto->InObjectPropertyAtPut(JSRegExp::kSourceFieldIndex,
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001025 heap->query_colon_string());
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001026 proto->InObjectPropertyAtPut(JSRegExp::kGlobalFieldIndex,
1027 heap->false_value());
1028 proto->InObjectPropertyAtPut(JSRegExp::kIgnoreCaseFieldIndex,
1029 heap->false_value());
1030 proto->InObjectPropertyAtPut(JSRegExp::kMultilineFieldIndex,
1031 heap->false_value());
1032 proto->InObjectPropertyAtPut(JSRegExp::kLastIndexFieldIndex,
1033 Smi::FromInt(0),
1034 SKIP_WRITE_BARRIER); // It's a Smi.
1035 initial_map->set_prototype(*proto);
1036 factory->SetRegExpIrregexpData(Handle<JSRegExp>::cast(proto),
1037 JSRegExp::IRREGEXP, factory->empty_string(),
1038 JSRegExp::Flags(0), 0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001039 }
1040
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00001041 { // -- J S O N
machenbach@chromium.org8e36b5b2013-09-26 07:36:30 +00001042 Handle<String> name = factory->InternalizeUtf8String("JSON");
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001043 Handle<JSFunction> cons = factory->NewFunction(name,
1044 factory->the_hole_value());
jkummerow@chromium.orgba72ec82013-07-22 09:21:20 +00001045 JSFunction::SetInstancePrototype(cons,
1046 Handle<Object>(native_context()->initial_object_prototype(), isolate));
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00001047 cons->SetInstanceClassName(*name);
lrn@chromium.org7516f052011-03-30 08:52:27 +00001048 Handle<JSObject> json_object = factory->NewJSObject(cons, TENURED);
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00001049 ASSERT(json_object->IsJSObject());
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001050 CHECK_NOT_EMPTY_HANDLE(isolate,
1051 JSObject::SetLocalPropertyIgnoreAttributes(
1052 global, name, json_object, DONT_ENUM));
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001053 native_context()->set_json_object(*json_object);
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00001054 }
1055
verwaest@chromium.org32cb9b22013-08-21 11:18:12 +00001056 { // -- A r r a y B u f f e r
1057 Handle<JSFunction> array_buffer_fun =
1058 InstallFunction(
1059 global, "ArrayBuffer", JS_ARRAY_BUFFER_TYPE,
1060 JSArrayBuffer::kSizeWithInternalFields,
1061 isolate->initial_object_prototype(),
1062 Builtins::kIllegal, true, true);
1063 native_context()->set_array_buffer_fun(*array_buffer_fun);
1064 }
1065
1066 { // -- T y p e d A r r a y s
1067 Handle<JSFunction> int8_fun = InstallTypedArray("Int8Array",
1068 EXTERNAL_BYTE_ELEMENTS);
1069 native_context()->set_int8_array_fun(*int8_fun);
1070 Handle<JSFunction> uint8_fun = InstallTypedArray("Uint8Array",
1071 EXTERNAL_UNSIGNED_BYTE_ELEMENTS);
1072 native_context()->set_uint8_array_fun(*uint8_fun);
1073 Handle<JSFunction> int16_fun = InstallTypedArray("Int16Array",
1074 EXTERNAL_SHORT_ELEMENTS);
1075 native_context()->set_int16_array_fun(*int16_fun);
1076 Handle<JSFunction> uint16_fun = InstallTypedArray("Uint16Array",
1077 EXTERNAL_UNSIGNED_SHORT_ELEMENTS);
1078 native_context()->set_uint16_array_fun(*uint16_fun);
1079 Handle<JSFunction> int32_fun = InstallTypedArray("Int32Array",
1080 EXTERNAL_INT_ELEMENTS);
1081 native_context()->set_int32_array_fun(*int32_fun);
1082 Handle<JSFunction> uint32_fun = InstallTypedArray("Uint32Array",
1083 EXTERNAL_UNSIGNED_INT_ELEMENTS);
1084 native_context()->set_uint32_array_fun(*uint32_fun);
1085 Handle<JSFunction> float_fun = InstallTypedArray("Float32Array",
1086 EXTERNAL_FLOAT_ELEMENTS);
1087 native_context()->set_float_array_fun(*float_fun);
1088 Handle<JSFunction> double_fun = InstallTypedArray("Float64Array",
1089 EXTERNAL_DOUBLE_ELEMENTS);
1090 native_context()->set_double_array_fun(*double_fun);
1091 Handle<JSFunction> uint8c_fun = InstallTypedArray("Uint8ClampedArray",
1092 EXTERNAL_PIXEL_ELEMENTS);
1093 native_context()->set_uint8c_array_fun(*uint8c_fun);
1094
1095 Handle<JSFunction> data_view_fun =
1096 InstallFunction(
1097 global, "DataView", JS_DATA_VIEW_TYPE,
1098 JSDataView::kSizeWithInternalFields,
1099 isolate->initial_object_prototype(),
1100 Builtins::kIllegal, true, true);
1101 native_context()->set_data_view_fun(*data_view_fun);
1102 }
1103
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001104 { // --- arguments_boilerplate_
1105 // Make sure we can recognize argument objects at runtime.
1106 // This is done by introducing an anonymous function with
1107 // class_name equals 'Arguments'.
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001108 Handle<String> arguments_string = factory->InternalizeOneByteString(
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00001109 STATIC_ASCII_VECTOR("Arguments"));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001110 Handle<Code> code = Handle<Code>(
lrn@chromium.org7516f052011-03-30 08:52:27 +00001111 isolate->builtins()->builtin(Builtins::kIllegal));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001112 Handle<JSObject> prototype =
1113 Handle<JSObject>(
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001114 JSObject::cast(native_context()->object_function()->prototype()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001115
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001116 Handle<JSFunction> function =
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001117 factory->NewFunctionWithPrototype(arguments_string,
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001118 JS_OBJECT_TYPE,
1119 JSObject::kHeaderSize,
1120 prototype,
1121 code,
1122 false);
1123 ASSERT(!function->has_initial_map());
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001124 function->shared()->set_instance_class_name(*arguments_string);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001125 function->shared()->set_expected_nof_properties(2);
lrn@chromium.org7516f052011-03-30 08:52:27 +00001126 Handle<JSObject> result = factory->NewJSObject(function);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001127
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001128 native_context()->set_arguments_boilerplate(*result);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001129 // Note: length must be added as the first property and
1130 // callee must be added as the second property.
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001131 CHECK_NOT_EMPTY_HANDLE(isolate,
1132 JSObject::SetLocalPropertyIgnoreAttributes(
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001133 result, factory->length_string(),
danno@chromium.org1fd77d52013-06-07 16:01:45 +00001134 factory->undefined_value(), DONT_ENUM,
rossberg@chromium.org92597162013-08-23 13:28:00 +00001135 Object::FORCE_TAGGED, FORCE_FIELD));
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001136 CHECK_NOT_EMPTY_HANDLE(isolate,
1137 JSObject::SetLocalPropertyIgnoreAttributes(
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001138 result, factory->callee_string(),
danno@chromium.org1fd77d52013-06-07 16:01:45 +00001139 factory->undefined_value(), DONT_ENUM,
rossberg@chromium.org92597162013-08-23 13:28:00 +00001140 Object::FORCE_TAGGED, FORCE_FIELD));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001141
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001142#ifdef DEBUG
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001143 LookupResult lookup(isolate);
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001144 result->LocalLookup(heap->callee_string(), &lookup);
yangguo@chromium.orgde0db002012-06-22 13:44:28 +00001145 ASSERT(lookup.IsField());
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00001146 ASSERT(lookup.GetFieldIndex().field_index() == Heap::kArgumentsCalleeIndex);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001147
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001148 result->LocalLookup(heap->length_string(), &lookup);
yangguo@chromium.orgde0db002012-06-22 13:44:28 +00001149 ASSERT(lookup.IsField());
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00001150 ASSERT(lookup.GetFieldIndex().field_index() == Heap::kArgumentsLengthIndex);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001151
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001152 ASSERT(result->map()->inobject_properties() > Heap::kArgumentsCalleeIndex);
1153 ASSERT(result->map()->inobject_properties() > Heap::kArgumentsLengthIndex);
1154
1155 // Check the state of the object.
1156 ASSERT(result->HasFastProperties());
svenpanne@chromium.org830d30c2012-05-29 13:20:14 +00001157 ASSERT(result->HasFastObjectElements());
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001158#endif
1159 }
1160
whesse@chromium.org7b260152011-06-20 15:33:18 +00001161 { // --- aliased_arguments_boilerplate_
whesse@chromium.org7b260152011-06-20 15:33:18 +00001162 // Set up a well-formed parameter map to make assertions happy.
1163 Handle<FixedArray> elements = factory->NewFixedArray(2);
1164 elements->set_map(heap->non_strict_arguments_elements_map());
1165 Handle<FixedArray> array;
1166 array = factory->NewFixedArray(0);
1167 elements->set(0, *array);
1168 array = factory->NewFixedArray(0);
1169 elements->set(1, *array);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001170
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001171 Handle<Map> old_map(native_context()->arguments_boilerplate()->map());
verwaest@chromium.org753aee42012-07-17 16:15:42 +00001172 Handle<Map> new_map = factory->CopyMap(old_map);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001173 new_map->set_pre_allocated_property_fields(2);
1174 Handle<JSObject> result = factory->NewJSObjectFromMap(new_map);
1175 // Set elements kind after allocating the object because
1176 // NewJSObjectFromMap assumes a fast elements map.
1177 new_map->set_elements_kind(NON_STRICT_ARGUMENTS_ELEMENTS);
whesse@chromium.org7b260152011-06-20 15:33:18 +00001178 result->set_elements(*elements);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001179 ASSERT(result->HasNonStrictArgumentsElements());
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001180 native_context()->set_aliased_arguments_boilerplate(*result);
whesse@chromium.org7b260152011-06-20 15:33:18 +00001181 }
1182
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001183 { // --- strict mode arguments boilerplate
1184 const PropertyAttributes attributes =
1185 static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE | READ_ONLY);
1186
1187 // Create the ThrowTypeError functions.
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001188 Handle<AccessorPair> callee = factory->NewAccessorPair();
1189 Handle<AccessorPair> caller = factory->NewAccessorPair();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001190
danno@chromium.org40cb8782011-05-25 07:58:50 +00001191 Handle<JSFunction> throw_function =
1192 GetThrowTypeErrorFunction();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001193
1194 // Install the ThrowTypeError functions.
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001195 callee->set_getter(*throw_function);
1196 callee->set_setter(*throw_function);
1197 caller->set_getter(*throw_function);
1198 caller->set_setter(*throw_function);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001199
yangguo@chromium.org304cc332012-07-24 07:59:48 +00001200 // Create the map. Allocate one in-object field for length.
1201 Handle<Map> map = factory->NewMap(JS_OBJECT_TYPE,
1202 Heap::kArgumentsObjectSizeStrict);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001203 // Create the descriptor array for the arguments object.
verwaest@chromium.org33e09c82012-10-10 17:07:22 +00001204 Handle<DescriptorArray> descriptors = factory->NewDescriptorArray(0, 3);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001205 DescriptorArray::WhitenessWitness witness(*descriptors);
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00001206 map->set_instance_descriptors(*descriptors);
yangguo@chromium.org304cc332012-07-24 07:59:48 +00001207
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001208 { // length
danno@chromium.orgf005df62013-04-30 16:36:45 +00001209 FieldDescriptor d(
1210 *factory->length_string(), 0, DONT_ENUM, Representation::Tagged());
yangguo@chromium.org304cc332012-07-24 07:59:48 +00001211 map->AppendDescriptor(&d, witness);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001212 }
1213 { // callee
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001214 CallbacksDescriptor d(*factory->callee_string(),
rossberg@chromium.org657d53b2012-07-12 11:06:03 +00001215 *callee,
jkummerow@chromium.org28583c92012-07-16 11:31:55 +00001216 attributes);
yangguo@chromium.org304cc332012-07-24 07:59:48 +00001217 map->AppendDescriptor(&d, witness);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001218 }
1219 { // caller
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001220 CallbacksDescriptor d(*factory->caller_string(),
rossberg@chromium.org657d53b2012-07-12 11:06:03 +00001221 *caller,
jkummerow@chromium.org28583c92012-07-16 11:31:55 +00001222 attributes);
yangguo@chromium.org304cc332012-07-24 07:59:48 +00001223 map->AppendDescriptor(&d, witness);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001224 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001225
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001226 map->set_function_with_prototype(true);
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001227 map->set_prototype(native_context()->object_function()->prototype());
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001228 map->set_pre_allocated_property_fields(1);
1229 map->set_inobject_properties(1);
1230
1231 // Copy constructor from the non-strict arguments boilerplate.
1232 map->set_constructor(
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001233 native_context()->arguments_boilerplate()->map()->constructor());
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001234
1235 // Allocate the arguments boilerplate object.
lrn@chromium.org7516f052011-03-30 08:52:27 +00001236 Handle<JSObject> result = factory->NewJSObjectFromMap(map);
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001237 native_context()->set_strict_mode_arguments_boilerplate(*result);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001238
1239 // Add length property only for strict mode boilerplate.
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001240 CHECK_NOT_EMPTY_HANDLE(isolate,
1241 JSObject::SetLocalPropertyIgnoreAttributes(
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001242 result, factory->length_string(),
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001243 factory->undefined_value(), DONT_ENUM));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001244
1245#ifdef DEBUG
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001246 LookupResult lookup(isolate);
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001247 result->LocalLookup(heap->length_string(), &lookup);
yangguo@chromium.orgde0db002012-06-22 13:44:28 +00001248 ASSERT(lookup.IsField());
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00001249 ASSERT(lookup.GetFieldIndex().field_index() == Heap::kArgumentsLengthIndex);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001250
1251 ASSERT(result->map()->inobject_properties() > Heap::kArgumentsLengthIndex);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001252
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001253 // Check the state of the object.
1254 ASSERT(result->HasFastProperties());
svenpanne@chromium.org830d30c2012-05-29 13:20:14 +00001255 ASSERT(result->HasFastObjectElements());
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001256#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001257 }
1258
1259 { // --- context extension
1260 // Create a function for the context extension objects.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001261 Handle<Code> code = Handle<Code>(
lrn@chromium.org7516f052011-03-30 08:52:27 +00001262 isolate->builtins()->builtin(Builtins::kIllegal));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001263 Handle<JSFunction> context_extension_fun =
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001264 factory->NewFunction(factory->empty_string(),
ager@chromium.org32912102009-01-16 10:38:43 +00001265 JS_CONTEXT_EXTENSION_OBJECT_TYPE,
1266 JSObject::kHeaderSize,
1267 code,
1268 true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001269
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001270 Handle<String> name = factory->InternalizeOneByteString(
1271 STATIC_ASCII_VECTOR("context_extension"));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001272 context_extension_fun->shared()->set_instance_class_name(*name);
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001273 native_context()->set_context_extension_function(*context_extension_fun);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001274 }
1275
sgjesse@chromium.org05521fc2009-05-21 07:37:44 +00001276
1277 {
yangguo@chromium.org80c42ed2011-08-31 09:03:56 +00001278 // Set up the call-as-function delegate.
sgjesse@chromium.org05521fc2009-05-21 07:37:44 +00001279 Handle<Code> code =
lrn@chromium.org7516f052011-03-30 08:52:27 +00001280 Handle<Code>(isolate->builtins()->builtin(
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00001281 Builtins::kHandleApiCallAsFunction));
sgjesse@chromium.org05521fc2009-05-21 07:37:44 +00001282 Handle<JSFunction> delegate =
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001283 factory->NewFunction(factory->empty_string(), JS_OBJECT_TYPE,
sgjesse@chromium.org05521fc2009-05-21 07:37:44 +00001284 JSObject::kHeaderSize, code, true);
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001285 native_context()->set_call_as_function_delegate(*delegate);
sgjesse@chromium.org05521fc2009-05-21 07:37:44 +00001286 delegate->shared()->DontAdaptArguments();
1287 }
1288
1289 {
yangguo@chromium.org80c42ed2011-08-31 09:03:56 +00001290 // Set up the call-as-constructor delegate.
sgjesse@chromium.org05521fc2009-05-21 07:37:44 +00001291 Handle<Code> code =
lrn@chromium.org7516f052011-03-30 08:52:27 +00001292 Handle<Code>(isolate->builtins()->builtin(
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00001293 Builtins::kHandleApiCallAsConstructor));
sgjesse@chromium.org05521fc2009-05-21 07:37:44 +00001294 Handle<JSFunction> delegate =
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001295 factory->NewFunction(factory->empty_string(), JS_OBJECT_TYPE,
sgjesse@chromium.org05521fc2009-05-21 07:37:44 +00001296 JSObject::kHeaderSize, code, true);
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001297 native_context()->set_call_as_constructor_delegate(*delegate);
sgjesse@chromium.org05521fc2009-05-21 07:37:44 +00001298 delegate->shared()->DontAdaptArguments();
1299 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001300
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001301 // Initialize the out of memory slot.
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001302 native_context()->set_out_of_memory(heap->false_value());
ager@chromium.org9085a012009-05-11 19:22:57 +00001303
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00001304 // Initialize the embedder data slot.
hpayer@chromium.org4f99be92013-12-18 16:23:55 +00001305 Handle<FixedArray> embedder_data = factory->NewFixedArray(3);
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00001306 native_context()->set_embedder_data(*embedder_data);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001307}
1308
1309
jkummerow@chromium.org4e308cf2013-05-17 13:39:16 +00001310Handle<JSFunction> Genesis::InstallTypedArray(
1311 const char* name, ElementsKind elementsKind) {
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00001312 Handle<JSObject> global = Handle<JSObject>(native_context()->global_object());
jkummerow@chromium.org4e308cf2013-05-17 13:39:16 +00001313 Handle<JSFunction> result = InstallFunction(global, name, JS_TYPED_ARRAY_TYPE,
danno@chromium.orgf005df62013-04-30 16:36:45 +00001314 JSTypedArray::kSize, isolate()->initial_object_prototype(),
jkummerow@chromium.org4e308cf2013-05-17 13:39:16 +00001315 Builtins::kIllegal, false, true);
1316
1317 Handle<Map> initial_map = isolate()->factory()->NewMap(
mstarzinger@chromium.orge0e1b0d2013-07-08 08:38:06 +00001318 JS_TYPED_ARRAY_TYPE, JSTypedArray::kSizeWithInternalFields, elementsKind);
jkummerow@chromium.org4e308cf2013-05-17 13:39:16 +00001319 result->set_initial_map(*initial_map);
1320 initial_map->set_constructor(*result);
1321 return result;
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00001322}
1323
1324
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +00001325void Genesis::InitializeExperimentalGlobal() {
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001326 Handle<JSObject> global = Handle<JSObject>(native_context()->global_object());
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +00001327
1328 // TODO(mstarzinger): Move this into Genesis::InitializeGlobal once we no
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +00001329 // longer need to live behind flags, so functions get added to the snapshot.
1330
1331 if (FLAG_harmony_symbols) {
1332 // --- S y m b o l ---
1333 Handle<JSFunction> symbol_fun =
1334 InstallFunction(global, "Symbol", JS_VALUE_TYPE, JSValue::kSize,
1335 isolate()->initial_object_prototype(),
jkummerow@chromium.org4e308cf2013-05-17 13:39:16 +00001336 Builtins::kIllegal, true, true);
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +00001337 native_context()->set_symbol_function(*symbol_fun);
1338 }
1339
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001340 if (FLAG_harmony_collections) {
1341 { // -- S e t
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001342 InstallFunction(global, "Set", JS_SET_TYPE, JSSet::kSize,
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00001343 isolate()->initial_object_prototype(),
jkummerow@chromium.org4e308cf2013-05-17 13:39:16 +00001344 Builtins::kIllegal, true, true);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001345 }
1346 { // -- M a p
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001347 InstallFunction(global, "Map", JS_MAP_TYPE, JSMap::kSize,
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00001348 isolate()->initial_object_prototype(),
jkummerow@chromium.org4e308cf2013-05-17 13:39:16 +00001349 Builtins::kIllegal, true, true);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001350 }
1351 { // -- W e a k M a p
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001352 InstallFunction(global, "WeakMap", JS_WEAK_MAP_TYPE, JSWeakMap::kSize,
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00001353 isolate()->initial_object_prototype(),
jkummerow@chromium.org4e308cf2013-05-17 13:39:16 +00001354 Builtins::kIllegal, true, true);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001355 }
jkummerow@chromium.orgba72ec82013-07-22 09:21:20 +00001356 { // -- W e a k S e t
1357 InstallFunction(global, "WeakSet", JS_WEAK_SET_TYPE, JSWeakSet::kSize,
1358 isolate()->initial_object_prototype(),
1359 Builtins::kIllegal, true, true);
1360 }
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +00001361 }
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +00001362
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00001363 if (FLAG_harmony_generators) {
1364 // Create generator meta-objects and install them on the builtins object.
1365 Handle<JSObject> builtins(native_context()->builtins());
1366 Handle<JSObject> generator_object_prototype =
1367 factory()->NewJSObject(isolate()->object_function(), TENURED);
1368 Handle<JSFunction> generator_function_prototype =
1369 InstallFunction(builtins, "GeneratorFunctionPrototype",
1370 JS_FUNCTION_TYPE, JSFunction::kHeaderSize,
1371 generator_object_prototype, Builtins::kIllegal,
jkummerow@chromium.org4e308cf2013-05-17 13:39:16 +00001372 false, false);
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00001373 InstallFunction(builtins, "GeneratorFunction",
1374 JS_FUNCTION_TYPE, JSFunction::kSize,
1375 generator_function_prototype, Builtins::kIllegal,
jkummerow@chromium.org4e308cf2013-05-17 13:39:16 +00001376 false, false);
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00001377
1378 // Create maps for generator functions and their prototypes. Store those
1379 // maps in the native context.
1380 Handle<Map> function_map(native_context()->function_map());
1381 Handle<Map> generator_function_map = factory()->CopyMap(function_map);
1382 generator_function_map->set_prototype(*generator_function_prototype);
1383 native_context()->set_generator_function_map(*generator_function_map);
1384
1385 Handle<Map> strict_mode_function_map(
1386 native_context()->strict_mode_function_map());
1387 Handle<Map> strict_mode_generator_function_map = factory()->CopyMap(
1388 strict_mode_function_map);
1389 strict_mode_generator_function_map->set_prototype(
1390 *generator_function_prototype);
1391 native_context()->set_strict_mode_generator_function_map(
1392 *strict_mode_generator_function_map);
1393
1394 Handle<Map> object_map(native_context()->object_function()->initial_map());
1395 Handle<Map> generator_object_prototype_map = factory()->CopyMap(
1396 object_map, 0);
1397 generator_object_prototype_map->set_prototype(
1398 *generator_object_prototype);
1399 native_context()->set_generator_object_prototype_map(
1400 *generator_object_prototype_map);
ulan@chromium.org57ff8812013-05-10 08:16:55 +00001401
1402 // Create a map for generator result objects.
1403 ASSERT(object_map->inobject_properties() == 0);
1404 STATIC_ASSERT(JSGeneratorObject::kResultPropertyCount == 2);
1405 Handle<Map> generator_result_map = factory()->CopyMap(object_map,
1406 JSGeneratorObject::kResultPropertyCount);
1407 ASSERT(generator_result_map->inobject_properties() ==
1408 JSGeneratorObject::kResultPropertyCount);
1409
1410 Handle<DescriptorArray> descriptors = factory()->NewDescriptorArray(0,
1411 JSGeneratorObject::kResultPropertyCount);
1412 DescriptorArray::WhitenessWitness witness(*descriptors);
1413 generator_result_map->set_instance_descriptors(*descriptors);
1414
1415 Handle<String> value_string = factory()->InternalizeOneByteString(
1416 STATIC_ASCII_VECTOR("value"));
1417 FieldDescriptor value_descr(*value_string,
1418 JSGeneratorObject::kResultValuePropertyIndex,
1419 NONE,
1420 Representation::Tagged());
1421 generator_result_map->AppendDescriptor(&value_descr, witness);
1422
1423 Handle<String> done_string = factory()->InternalizeOneByteString(
1424 STATIC_ASCII_VECTOR("done"));
1425 FieldDescriptor done_descr(*done_string,
1426 JSGeneratorObject::kResultDonePropertyIndex,
1427 NONE,
1428 Representation::Tagged());
1429 generator_result_map->AppendDescriptor(&done_descr, witness);
1430
1431 generator_result_map->set_unused_property_fields(0);
1432 ASSERT_EQ(JSGeneratorObject::kResultSize,
1433 generator_result_map->instance_size());
1434 native_context()->set_generator_result_map(*generator_result_map);
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +00001435 }
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +00001436}
1437
1438
danno@chromium.org160a7b02011-04-18 15:51:38 +00001439bool Genesis::CompileBuiltin(Isolate* isolate, int index) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001440 Vector<const char> name = Natives::GetScriptName(index);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001441 Handle<String> source_code =
danno@chromium.org160a7b02011-04-18 15:51:38 +00001442 isolate->bootstrapper()->NativesSourceLookup(index);
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00001443 return CompileNative(isolate, name, source_code);
danno@chromium.org160a7b02011-04-18 15:51:38 +00001444}
1445
1446
1447bool Genesis::CompileExperimentalBuiltin(Isolate* isolate, int index) {
1448 Vector<const char> name = ExperimentalNatives::GetScriptName(index);
1449 Factory* factory = isolate->factory();
1450 Handle<String> source_code =
jkummerow@chromium.orge297f592011-06-08 10:05:15 +00001451 factory->NewStringFromAscii(
1452 ExperimentalNatives::GetRawScriptSource(index));
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00001453 return CompileNative(isolate, name, source_code);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001454}
1455
1456
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00001457bool Genesis::CompileNative(Isolate* isolate,
1458 Vector<const char> name,
1459 Handle<String> source) {
1460 HandleScope scope(isolate);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001461#ifdef ENABLE_DEBUGGER_SUPPORT
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001462 isolate->debugger()->set_compiling_natives(true);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001463#endif
fschneider@chromium.org7d10be52012-04-10 12:30:14 +00001464 // During genesis, the boilerplate for stack overflow won't work until the
1465 // environment has been at least partially initialized. Add a stack check
1466 // before entering JS code to catch overflow early.
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00001467 StackLimitCheck check(isolate);
fschneider@chromium.org7d10be52012-04-10 12:30:14 +00001468 if (check.HasOverflowed()) return false;
1469
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00001470 bool result = CompileScriptCached(isolate,
1471 name,
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001472 source,
1473 NULL,
1474 NULL,
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001475 Handle<Context>(isolate->context()),
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001476 true);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001477 ASSERT(isolate->has_pending_exception() != result);
1478 if (!result) isolate->clear_pending_exception();
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001479#ifdef ENABLE_DEBUGGER_SUPPORT
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001480 isolate->debugger()->set_compiling_natives(false);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001481#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001482 return result;
1483}
1484
1485
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00001486bool Genesis::CompileScriptCached(Isolate* isolate,
1487 Vector<const char> name,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001488 Handle<String> source,
1489 SourceCodeCache* cache,
1490 v8::Extension* extension,
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001491 Handle<Context> top_context,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001492 bool use_runtime_context) {
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00001493 Factory* factory = isolate->factory();
1494 HandleScope scope(isolate);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001495 Handle<SharedFunctionInfo> function_info;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001496
1497 // If we can't find the function in the cache, we compile a new
1498 // function and insert it into the cache.
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001499 if (cache == NULL || !cache->Lookup(name, &function_info)) {
ulan@chromium.org8e8d8822012-11-23 14:36:46 +00001500 ASSERT(source->IsOneByteRepresentation());
lrn@chromium.org7516f052011-03-30 08:52:27 +00001501 Handle<String> script_name = factory->NewStringFromUtf8(name);
yangguo@chromium.org49546742013-12-23 16:17:49 +00001502 function_info = Compiler::CompileScript(
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001503 source,
1504 script_name,
1505 0,
1506 0,
danno@chromium.orgd3c42102013-08-01 16:58:23 +00001507 false,
yangguo@chromium.org355cfd12012-08-29 15:32:24 +00001508 top_context,
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001509 extension,
1510 NULL,
1511 Handle<String>::null(),
1512 use_runtime_context ? NATIVES_CODE : NOT_NATIVES_CODE);
1513 if (function_info.is_null()) return false;
1514 if (cache != NULL) cache->Add(name, function_info);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001515 }
1516
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001517 // Set up the function context. Conceptually, we should clone the
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001518 // function before overwriting the context but since we're in a
1519 // single-threaded environment it is not strictly necessary.
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001520 ASSERT(top_context->IsNativeContext());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001521 Handle<Context> context =
1522 Handle<Context>(use_runtime_context
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001523 ? Handle<Context>(top_context->runtime_context())
1524 : top_context);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001525 Handle<JSFunction> fun =
lrn@chromium.org7516f052011-03-30 08:52:27 +00001526 factory->NewFunctionFromSharedFunctionInfo(function_info, context);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001527
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001528 // Call function using either the runtime object or the global
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001529 // object as the receiver. Provide no parameters.
1530 Handle<Object> receiver =
1531 Handle<Object>(use_runtime_context
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001532 ? top_context->builtins()
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00001533 : top_context->global_object(),
1534 isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001535 bool has_pending_exception;
jkummerow@chromium.org2c9426b2013-09-05 16:31:13 +00001536 Execution::Call(isolate, fun, receiver, 0, NULL, &has_pending_exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001537 if (has_pending_exception) return false;
ager@chromium.org5c838252010-02-19 08:53:10 +00001538 return true;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001539}
1540
1541
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00001542#define INSTALL_NATIVE(Type, name, var) \
1543 Handle<String> var##_name = \
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001544 factory()->InternalizeOneByteString(STATIC_ASCII_VECTOR(name)); \
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00001545 Object* var##_native = \
1546 native_context()->builtins()->GetPropertyNoExceptionThrown( \
1547 *var##_name); \
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001548 native_context()->set_##var(Type::cast(var##_native));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001549
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001550
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001551void Genesis::InstallNativeFunctions() {
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00001552 HandleScope scope(isolate());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001553 INSTALL_NATIVE(JSFunction, "CreateDate", create_date_fun);
1554 INSTALL_NATIVE(JSFunction, "ToNumber", to_number_fun);
1555 INSTALL_NATIVE(JSFunction, "ToString", to_string_fun);
1556 INSTALL_NATIVE(JSFunction, "ToDetailString", to_detail_string_fun);
1557 INSTALL_NATIVE(JSFunction, "ToObject", to_object_fun);
1558 INSTALL_NATIVE(JSFunction, "ToInteger", to_integer_fun);
1559 INSTALL_NATIVE(JSFunction, "ToUint32", to_uint32_fun);
1560 INSTALL_NATIVE(JSFunction, "ToInt32", to_int32_fun);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00001561 INSTALL_NATIVE(JSFunction, "GlobalEval", global_eval_fun);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001562 INSTALL_NATIVE(JSFunction, "Instantiate", instantiate_fun);
1563 INSTALL_NATIVE(JSFunction, "ConfigureTemplateInstance",
1564 configure_instance_fun);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001565 INSTALL_NATIVE(JSFunction, "GetStackTraceLine", get_stack_trace_line_fun);
1566 INSTALL_NATIVE(JSObject, "functionCache", function_cache);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001567 INSTALL_NATIVE(JSFunction, "ToCompletePropertyDescriptor",
1568 to_complete_property_descriptor);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001569}
1570
mstarzinger@chromium.orge0e1b0d2013-07-08 08:38:06 +00001571
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001572void Genesis::InstallExperimentalNativeFunctions() {
machenbach@chromium.org9f18d912013-11-28 13:42:41 +00001573 INSTALL_NATIVE(JSFunction, "RunMicrotasks", run_microtasks);
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001574 if (FLAG_harmony_proxies) {
rossberg@chromium.org717967f2011-07-20 13:44:42 +00001575 INSTALL_NATIVE(JSFunction, "DerivedHasTrap", derived_has_trap);
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001576 INSTALL_NATIVE(JSFunction, "DerivedGetTrap", derived_get_trap);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00001577 INSTALL_NATIVE(JSFunction, "DerivedSetTrap", derived_set_trap);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001578 INSTALL_NATIVE(JSFunction, "ProxyEnumerate", proxy_enumerate);
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001579 }
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +00001580 if (FLAG_harmony_observation) {
1581 INSTALL_NATIVE(JSFunction, "NotifyChange", observers_notify_change);
verwaest@chromium.orgd4be0f02013-06-05 13:39:03 +00001582 INSTALL_NATIVE(JSFunction, "EnqueueSpliceRecord", observers_enqueue_splice);
1583 INSTALL_NATIVE(JSFunction, "BeginPerformSplice",
1584 observers_begin_perform_splice);
1585 INSTALL_NATIVE(JSFunction, "EndPerformSplice",
1586 observers_end_perform_splice);
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +00001587 }
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001588}
1589
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001590#undef INSTALL_NATIVE
1591
1592
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00001593Handle<JSFunction> Genesis::InstallInternalArray(
1594 Handle<JSBuiltinsObject> builtins,
1595 const char* name,
1596 ElementsKind elements_kind) {
1597 // --- I n t e r n a l A r r a y ---
1598 // An array constructor on the builtins object that works like
1599 // the public Array constructor, except that its prototype
1600 // doesn't inherit from Object.prototype.
1601 // To be used only for internal work by builtins. Instances
1602 // must not be leaked to user code.
1603 Handle<JSFunction> array_function =
1604 InstallFunction(builtins,
1605 name,
1606 JS_ARRAY_TYPE,
1607 JSArray::kSize,
1608 isolate()->initial_object_prototype(),
1609 Builtins::kInternalArrayCode,
jkummerow@chromium.org4e308cf2013-05-17 13:39:16 +00001610 true, true);
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00001611 Handle<JSObject> prototype =
1612 factory()->NewJSObject(isolate()->object_function(), TENURED);
rossberg@chromium.orgebeba022013-08-19 09:36:44 +00001613 Accessors::FunctionSetPrototype(array_function, prototype);
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00001614
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +00001615 InternalArrayConstructorStub internal_array_constructor_stub(isolate());
1616 Handle<Code> code = internal_array_constructor_stub.GetCode(isolate());
1617 array_function->shared()->set_construct_stub(*code);
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00001618 array_function->shared()->DontAdaptArguments();
1619
rossberg@chromium.orgb99c7542013-05-31 11:40:45 +00001620 Handle<Map> original_map(array_function->initial_map());
1621 Handle<Map> initial_map = factory()->CopyMap(original_map);
1622 initial_map->set_elements_kind(elements_kind);
1623 array_function->set_initial_map(*initial_map);
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00001624
1625 // Make "length" magic on instances.
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00001626 Handle<DescriptorArray> array_descriptors(
1627 factory()->NewDescriptorArray(0, 1));
1628 DescriptorArray::WhitenessWitness witness(*array_descriptors);
1629
1630 Handle<Foreign> array_length(factory()->NewForeign(
1631 &Accessors::ArrayLength));
1632 PropertyAttributes attribs = static_cast<PropertyAttributes>(
1633 DONT_ENUM | DONT_DELETE);
1634 initial_map->set_instance_descriptors(*array_descriptors);
1635
1636 { // Add length.
1637 CallbacksDescriptor d(
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001638 *factory()->length_string(), *array_length, attribs);
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00001639 array_function->initial_map()->AppendDescriptor(&d, witness);
1640 }
1641
1642 return array_function;
1643}
1644
1645
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001646bool Genesis::InstallNatives() {
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00001647 HandleScope scope(isolate());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001648
1649 // Create a function for the builtins object. Allocate space for the
1650 // JavaScript builtins, a reference to the builtins object
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001651 // (itself) and a reference to the native_context directly in the object.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001652 Handle<Code> code = Handle<Code>(
danno@chromium.org160a7b02011-04-18 15:51:38 +00001653 isolate()->builtins()->builtin(Builtins::kIllegal));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001654 Handle<JSFunction> builtins_fun =
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001655 factory()->NewFunction(factory()->empty_string(),
danno@chromium.org160a7b02011-04-18 15:51:38 +00001656 JS_BUILTINS_OBJECT_TYPE,
1657 JSBuiltinsObject::kSize, code, true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001658
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00001659 Handle<String> name =
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001660 factory()->InternalizeOneByteString(STATIC_ASCII_VECTOR("builtins"));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001661 builtins_fun->shared()->set_instance_class_name(*name);
erik.corry@gmail.com88767242012-08-08 14:43:45 +00001662 builtins_fun->initial_map()->set_dictionary_map(true);
verwaest@chromium.orgde64f722012-08-16 15:44:54 +00001663 builtins_fun->initial_map()->set_prototype(heap()->null_value());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001664
1665 // Allocate the builtins object.
1666 Handle<JSBuiltinsObject> builtins =
danno@chromium.org160a7b02011-04-18 15:51:38 +00001667 Handle<JSBuiltinsObject>::cast(factory()->NewGlobalObject(builtins_fun));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001668 builtins->set_builtins(*builtins);
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001669 builtins->set_native_context(*native_context());
yangguo@chromium.org355cfd12012-08-29 15:32:24 +00001670 builtins->set_global_context(*native_context());
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001671 builtins->set_global_receiver(*builtins);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001672
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001673 // Set up the 'global' properties of the builtins object. The
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001674 // 'global' property that refers to the global object is the only
1675 // way to get from code running in the builtins context to the
1676 // global object.
1677 static const PropertyAttributes attributes =
1678 static_cast<PropertyAttributes>(READ_ONLY | DONT_DELETE);
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001679 Handle<String> global_string =
1680 factory()->InternalizeOneByteString(STATIC_ASCII_VECTOR("global"));
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00001681 Handle<Object> global_obj(native_context()->global_object(), isolate());
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001682 CHECK_NOT_EMPTY_HANDLE(isolate(),
1683 JSObject::SetLocalPropertyIgnoreAttributes(
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001684 builtins, global_string, global_obj, attributes));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001685
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001686 // Set up the reference from the global object to the builtins object.
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001687 JSGlobalObject::cast(native_context()->global_object())->
1688 set_builtins(*builtins);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001689
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001690 // Create a bridge function that has context in the native context.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001691 Handle<JSFunction> bridge =
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001692 factory()->NewFunction(factory()->empty_string(),
danno@chromium.org160a7b02011-04-18 15:51:38 +00001693 factory()->undefined_value());
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001694 ASSERT(bridge->context() == *isolate()->native_context());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001695
1696 // Allocate the builtins context.
1697 Handle<Context> context =
danno@chromium.org160a7b02011-04-18 15:51:38 +00001698 factory()->NewFunctionContext(Context::MIN_CONTEXT_SLOTS, bridge);
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001699 context->set_global_object(*builtins); // override builtins global object
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001700
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001701 native_context()->set_runtime_context(*context);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001702
1703 { // -- S c r i p t
1704 // Builtin functions for Script.
1705 Handle<JSFunction> script_fun =
1706 InstallFunction(builtins, "Script", JS_VALUE_TYPE, JSValue::kSize,
danno@chromium.org160a7b02011-04-18 15:51:38 +00001707 isolate()->initial_object_prototype(),
jkummerow@chromium.org4e308cf2013-05-17 13:39:16 +00001708 Builtins::kIllegal, false, false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001709 Handle<JSObject> prototype =
danno@chromium.org160a7b02011-04-18 15:51:38 +00001710 factory()->NewJSObject(isolate()->object_function(), TENURED);
rossberg@chromium.orgebeba022013-08-19 09:36:44 +00001711 Accessors::FunctionSetPrototype(script_fun, prototype);
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001712 native_context()->set_script_function(*script_fun);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001713
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001714 Handle<Map> script_map = Handle<Map>(script_fun->initial_map());
yangguo@chromium.org304cc332012-07-24 07:59:48 +00001715
1716 Handle<DescriptorArray> script_descriptors(
verwaest@chromium.org33e09c82012-10-10 17:07:22 +00001717 factory()->NewDescriptorArray(0, 13));
yangguo@chromium.org304cc332012-07-24 07:59:48 +00001718 DescriptorArray::WhitenessWitness witness(*script_descriptors);
1719
1720 Handle<Foreign> script_source(
1721 factory()->NewForeign(&Accessors::ScriptSource));
1722 Handle<Foreign> script_name(factory()->NewForeign(&Accessors::ScriptName));
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001723 Handle<String> id_string(factory()->InternalizeOneByteString(
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00001724 STATIC_ASCII_VECTOR("id")));
yangguo@chromium.org304cc332012-07-24 07:59:48 +00001725 Handle<Foreign> script_id(factory()->NewForeign(&Accessors::ScriptId));
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001726 Handle<String> line_offset_string(
1727 factory()->InternalizeOneByteString(
1728 STATIC_ASCII_VECTOR("line_offset")));
yangguo@chromium.org304cc332012-07-24 07:59:48 +00001729 Handle<Foreign> script_line_offset(
1730 factory()->NewForeign(&Accessors::ScriptLineOffset));
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001731 Handle<String> column_offset_string(
1732 factory()->InternalizeOneByteString(
1733 STATIC_ASCII_VECTOR("column_offset")));
yangguo@chromium.org304cc332012-07-24 07:59:48 +00001734 Handle<Foreign> script_column_offset(
1735 factory()->NewForeign(&Accessors::ScriptColumnOffset));
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001736 Handle<String> data_string(factory()->InternalizeOneByteString(
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00001737 STATIC_ASCII_VECTOR("data")));
yangguo@chromium.org304cc332012-07-24 07:59:48 +00001738 Handle<Foreign> script_data(factory()->NewForeign(&Accessors::ScriptData));
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001739 Handle<String> type_string(factory()->InternalizeOneByteString(
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00001740 STATIC_ASCII_VECTOR("type")));
yangguo@chromium.org304cc332012-07-24 07:59:48 +00001741 Handle<Foreign> script_type(factory()->NewForeign(&Accessors::ScriptType));
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001742 Handle<String> compilation_type_string(
1743 factory()->InternalizeOneByteString(
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00001744 STATIC_ASCII_VECTOR("compilation_type")));
yangguo@chromium.org304cc332012-07-24 07:59:48 +00001745 Handle<Foreign> script_compilation_type(
1746 factory()->NewForeign(&Accessors::ScriptCompilationType));
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001747 Handle<String> line_ends_string(factory()->InternalizeOneByteString(
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00001748 STATIC_ASCII_VECTOR("line_ends")));
yangguo@chromium.org304cc332012-07-24 07:59:48 +00001749 Handle<Foreign> script_line_ends(
1750 factory()->NewForeign(&Accessors::ScriptLineEnds));
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001751 Handle<String> context_data_string(
1752 factory()->InternalizeOneByteString(
1753 STATIC_ASCII_VECTOR("context_data")));
yangguo@chromium.org304cc332012-07-24 07:59:48 +00001754 Handle<Foreign> script_context_data(
1755 factory()->NewForeign(&Accessors::ScriptContextData));
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001756 Handle<String> eval_from_script_string(
1757 factory()->InternalizeOneByteString(
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00001758 STATIC_ASCII_VECTOR("eval_from_script")));
yangguo@chromium.org304cc332012-07-24 07:59:48 +00001759 Handle<Foreign> script_eval_from_script(
1760 factory()->NewForeign(&Accessors::ScriptEvalFromScript));
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001761 Handle<String> eval_from_script_position_string(
1762 factory()->InternalizeOneByteString(
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00001763 STATIC_ASCII_VECTOR("eval_from_script_position")));
yangguo@chromium.org304cc332012-07-24 07:59:48 +00001764 Handle<Foreign> script_eval_from_script_position(
1765 factory()->NewForeign(&Accessors::ScriptEvalFromScriptPosition));
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001766 Handle<String> eval_from_function_name_string(
1767 factory()->InternalizeOneByteString(
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00001768 STATIC_ASCII_VECTOR("eval_from_function_name")));
yangguo@chromium.org304cc332012-07-24 07:59:48 +00001769 Handle<Foreign> script_eval_from_function_name(
1770 factory()->NewForeign(&Accessors::ScriptEvalFromFunctionName));
1771 PropertyAttributes attribs =
1772 static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE | READ_ONLY);
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00001773 script_map->set_instance_descriptors(*script_descriptors);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001774
yangguo@chromium.org304cc332012-07-24 07:59:48 +00001775 {
1776 CallbacksDescriptor d(
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001777 *factory()->source_string(), *script_source, attribs);
yangguo@chromium.org304cc332012-07-24 07:59:48 +00001778 script_map->AppendDescriptor(&d, witness);
1779 }
1780
1781 {
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001782 CallbacksDescriptor d(*factory()->name_string(), *script_name, attribs);
yangguo@chromium.org304cc332012-07-24 07:59:48 +00001783 script_map->AppendDescriptor(&d, witness);
1784 }
1785
1786 {
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001787 CallbacksDescriptor d(*id_string, *script_id, attribs);
yangguo@chromium.org304cc332012-07-24 07:59:48 +00001788 script_map->AppendDescriptor(&d, witness);
1789 }
1790
1791 {
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001792 CallbacksDescriptor d(*line_offset_string, *script_line_offset, attribs);
yangguo@chromium.org304cc332012-07-24 07:59:48 +00001793 script_map->AppendDescriptor(&d, witness);
1794 }
1795
1796 {
1797 CallbacksDescriptor d(
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001798 *column_offset_string, *script_column_offset, attribs);
yangguo@chromium.org304cc332012-07-24 07:59:48 +00001799 script_map->AppendDescriptor(&d, witness);
1800 }
1801
1802 {
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001803 CallbacksDescriptor d(*data_string, *script_data, attribs);
yangguo@chromium.org304cc332012-07-24 07:59:48 +00001804 script_map->AppendDescriptor(&d, witness);
1805 }
1806
1807 {
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001808 CallbacksDescriptor d(*type_string, *script_type, attribs);
yangguo@chromium.org304cc332012-07-24 07:59:48 +00001809 script_map->AppendDescriptor(&d, witness);
1810 }
1811
1812 {
1813 CallbacksDescriptor d(
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001814 *compilation_type_string, *script_compilation_type, attribs);
yangguo@chromium.org304cc332012-07-24 07:59:48 +00001815 script_map->AppendDescriptor(&d, witness);
1816 }
1817
1818 {
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001819 CallbacksDescriptor d(*line_ends_string, *script_line_ends, attribs);
yangguo@chromium.org304cc332012-07-24 07:59:48 +00001820 script_map->AppendDescriptor(&d, witness);
1821 }
1822
1823 {
1824 CallbacksDescriptor d(
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001825 *context_data_string, *script_context_data, attribs);
yangguo@chromium.org304cc332012-07-24 07:59:48 +00001826 script_map->AppendDescriptor(&d, witness);
1827 }
1828
1829 {
1830 CallbacksDescriptor d(
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001831 *eval_from_script_string, *script_eval_from_script, attribs);
yangguo@chromium.org304cc332012-07-24 07:59:48 +00001832 script_map->AppendDescriptor(&d, witness);
1833 }
1834
1835 {
1836 CallbacksDescriptor d(
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001837 *eval_from_script_position_string,
yangguo@chromium.org304cc332012-07-24 07:59:48 +00001838 *script_eval_from_script_position,
1839 attribs);
1840 script_map->AppendDescriptor(&d, witness);
1841 }
1842
1843 {
1844 CallbacksDescriptor d(
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001845 *eval_from_function_name_string,
yangguo@chromium.org304cc332012-07-24 07:59:48 +00001846 *script_eval_from_function_name,
1847 attribs);
1848 script_map->AppendDescriptor(&d, witness);
1849 }
1850
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001851 // Allocate the empty script.
danno@chromium.org160a7b02011-04-18 15:51:38 +00001852 Handle<Script> script = factory()->NewScript(factory()->empty_string());
ager@chromium.orge2902be2009-06-08 12:21:35 +00001853 script->set_type(Smi::FromInt(Script::TYPE_NATIVE));
danno@chromium.org160a7b02011-04-18 15:51:38 +00001854 heap()->public_set_empty_script(*script);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001855 }
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001856 {
1857 // Builtin function for OpaqueReference -- a JSValue-based object,
1858 // that keeps its field isolated from JavaScript code. It may store
1859 // objects, that JavaScript code may not access.
1860 Handle<JSFunction> opaque_reference_fun =
1861 InstallFunction(builtins, "OpaqueReference", JS_VALUE_TYPE,
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001862 JSValue::kSize,
danno@chromium.org160a7b02011-04-18 15:51:38 +00001863 isolate()->initial_object_prototype(),
jkummerow@chromium.org4e308cf2013-05-17 13:39:16 +00001864 Builtins::kIllegal, false, false);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001865 Handle<JSObject> prototype =
danno@chromium.org160a7b02011-04-18 15:51:38 +00001866 factory()->NewJSObject(isolate()->object_function(), TENURED);
rossberg@chromium.orgebeba022013-08-19 09:36:44 +00001867 Accessors::FunctionSetPrototype(opaque_reference_fun, prototype);
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001868 native_context()->set_opaque_reference_function(*opaque_reference_fun);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001869 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001870
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00001871 // InternalArrays should not use Smi-Only array optimizations. There are too
1872 // many places in the C++ runtime code (e.g. RegEx) that assume that
1873 // elements in InternalArrays can be set to non-Smi values without going
1874 // through a common bottleneck that would make the SMI_ONLY -> FAST_ELEMENT
1875 // transition easy to trap. Moreover, they rarely are smi-only.
1876 {
karlklose@chromium.org8f806e82011-03-07 14:06:08 +00001877 Handle<JSFunction> array_function =
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00001878 InstallInternalArray(builtins, "InternalArray", FAST_HOLEY_ELEMENTS);
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001879 native_context()->set_internal_array_function(*array_function);
karlklose@chromium.org8f806e82011-03-07 14:06:08 +00001880 }
1881
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00001882 {
rossberg@chromium.orgb99c7542013-05-31 11:40:45 +00001883 InstallInternalArray(builtins, "InternalPackedArray", FAST_ELEMENTS);
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00001884 }
1885
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001886 if (FLAG_disable_native_files) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001887 PrintF("Warning: Running without installed natives!\n");
1888 return true;
1889 }
1890
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001891 // Install natives.
1892 for (int i = Natives::GetDebuggerCount();
1893 i < Natives::GetBuiltinsCount();
1894 i++) {
danno@chromium.org160a7b02011-04-18 15:51:38 +00001895 if (!CompileBuiltin(isolate(), i)) return false;
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001896 // TODO(ager): We really only need to install the JS builtin
1897 // functions on the builtins object after compiling and running
1898 // runtime.js.
1899 if (!InstallJSBuiltins(builtins)) return false;
1900 }
1901
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001902 InstallNativeFunctions();
1903
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00001904 // Store the map for the string prototype after the natives has been compiled
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001905 // and the String function has been set up.
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001906 Handle<JSFunction> string_function(native_context()->string_function());
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00001907 ASSERT(JSObject::cast(
1908 string_function->initial_map()->prototype())->HasFastProperties());
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001909 native_context()->set_string_function_prototype_map(
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00001910 HeapObject::cast(string_function->initial_map()->prototype())->map());
1911
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001912 // Install Function.prototype.call and apply.
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001913 { Handle<String> key = factory()->function_class_string();
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001914 Handle<JSFunction> function =
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00001915 Handle<JSFunction>::cast(
1916 GetProperty(isolate(), isolate()->global_object(), key));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001917 Handle<JSObject> proto =
1918 Handle<JSObject>(JSObject::cast(function->instance_prototype()));
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001919
1920 // Install the call and the apply functions.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001921 Handle<JSFunction> call =
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001922 InstallFunction(proto, "call", JS_OBJECT_TYPE, JSObject::kHeaderSize,
kmillikin@chromium.org4111b802010-05-03 10:34:42 +00001923 Handle<JSObject>::null(),
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00001924 Builtins::kFunctionCall,
jkummerow@chromium.org4e308cf2013-05-17 13:39:16 +00001925 false, false);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001926 Handle<JSFunction> apply =
1927 InstallFunction(proto, "apply", JS_OBJECT_TYPE, JSObject::kHeaderSize,
kmillikin@chromium.org4111b802010-05-03 10:34:42 +00001928 Handle<JSObject>::null(),
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00001929 Builtins::kFunctionApply,
jkummerow@chromium.org4e308cf2013-05-17 13:39:16 +00001930 false, false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001931
1932 // Make sure that Function.prototype.call appears to be compiled.
1933 // The code will never be called, but inline caching for call will
1934 // only work if it appears to be compiled.
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001935 call->shared()->DontAdaptArguments();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001936 ASSERT(call->is_compiled());
1937
ager@chromium.org32912102009-01-16 10:38:43 +00001938 // Set the expected parameters for apply to 2; required by builtin.
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001939 apply->shared()->set_formal_parameter_count(2);
1940
1941 // Set the lengths for the functions to satisfy ECMA-262.
1942 call->shared()->set_length(1);
1943 apply->shared()->set_length(2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001944 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001945
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001946 InstallBuiltinFunctionIds();
1947
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00001948 // Create a constructor for RegExp results (a variant of Array that
1949 // predefines the two properties index and match).
1950 {
1951 // RegExpResult initial map.
1952
1953 // Find global.Array.prototype to inherit from.
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001954 Handle<JSFunction> array_constructor(native_context()->array_function());
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00001955 Handle<JSObject> array_prototype(
1956 JSObject::cast(array_constructor->instance_prototype()));
1957
1958 // Add initial map.
1959 Handle<Map> initial_map =
danno@chromium.org160a7b02011-04-18 15:51:38 +00001960 factory()->NewMap(JS_ARRAY_TYPE, JSRegExpResult::kSize);
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00001961 initial_map->set_constructor(*array_constructor);
1962
1963 // Set prototype on map.
1964 initial_map->set_non_instance_prototype(false);
1965 initial_map->set_prototype(*array_prototype);
1966
1967 // Update map with length accessor from Array and add "index" and "input".
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00001968 Handle<DescriptorArray> reresult_descriptors =
verwaest@chromium.org33e09c82012-10-10 17:07:22 +00001969 factory()->NewDescriptorArray(0, 3);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001970 DescriptorArray::WhitenessWitness witness(*reresult_descriptors);
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00001971 initial_map->set_instance_descriptors(*reresult_descriptors);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001972
yangguo@chromium.org304cc332012-07-24 07:59:48 +00001973 {
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001974 JSFunction* array_function = native_context()->array_function();
yangguo@chromium.org304cc332012-07-24 07:59:48 +00001975 Handle<DescriptorArray> array_descriptors(
1976 array_function->initial_map()->instance_descriptors());
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001977 String* length = heap()->length_string();
verwaest@chromium.org06ab2ec2012-10-09 17:00:13 +00001978 int old = array_descriptors->SearchWithCache(
1979 length, array_function->initial_map());
yangguo@chromium.org304cc332012-07-24 07:59:48 +00001980 ASSERT(old != DescriptorArray::kNotFound);
1981 CallbacksDescriptor desc(length,
1982 array_descriptors->GetValue(old),
1983 array_descriptors->GetDetails(old).attributes());
1984 initial_map->AppendDescriptor(&desc, witness);
1985 }
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00001986 {
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001987 FieldDescriptor index_field(heap()->index_string(),
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00001988 JSRegExpResult::kIndexIndex,
danno@chromium.orgf005df62013-04-30 16:36:45 +00001989 NONE,
1990 Representation::Tagged());
yangguo@chromium.org304cc332012-07-24 07:59:48 +00001991 initial_map->AppendDescriptor(&index_field, witness);
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00001992 }
1993
1994 {
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001995 FieldDescriptor input_field(heap()->input_string(),
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00001996 JSRegExpResult::kInputIndex,
danno@chromium.orgf005df62013-04-30 16:36:45 +00001997 NONE,
1998 Representation::Tagged());
yangguo@chromium.org304cc332012-07-24 07:59:48 +00001999 initial_map->AppendDescriptor(&input_field, witness);
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00002000 }
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00002001
2002 initial_map->set_inobject_properties(2);
2003 initial_map->set_pre_allocated_property_fields(2);
2004 initial_map->set_unused_property_fields(0);
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00002005
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00002006 native_context()->set_regexp_result_map(*initial_map);
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00002007 }
2008
svenpanne@chromium.orgc859c4f2012-10-15 11:51:39 +00002009#ifdef VERIFY_HEAP
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002010 builtins->Verify();
2011#endif
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002012
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002013 return true;
2014}
2015
2016
machenbach@chromium.org9f18d912013-11-28 13:42:41 +00002017#define INSTALL_EXPERIMENTAL_NATIVE(i, flag, file) \
2018 if (FLAG_harmony_##flag && \
2019 strcmp(ExperimentalNatives::GetScriptName(i).start(), \
2020 "native " file) == 0) { \
2021 if (!CompileExperimentalBuiltin(isolate(), i)) return false; \
2022 }
2023
2024
danno@chromium.org160a7b02011-04-18 15:51:38 +00002025bool Genesis::InstallExperimentalNatives() {
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002026 for (int i = ExperimentalNatives::GetDebuggerCount();
2027 i < ExperimentalNatives::GetBuiltinsCount();
2028 i++) {
machenbach@chromium.org9f18d912013-11-28 13:42:41 +00002029 INSTALL_EXPERIMENTAL_NATIVE(i, symbols, "symbol.js")
2030 INSTALL_EXPERIMENTAL_NATIVE(i, proxies, "proxy.js")
2031 INSTALL_EXPERIMENTAL_NATIVE(i, collections, "collection.js")
2032 INSTALL_EXPERIMENTAL_NATIVE(i, observation, "object-observe.js")
2033 INSTALL_EXPERIMENTAL_NATIVE(i, promises, "promise.js")
2034 INSTALL_EXPERIMENTAL_NATIVE(i, generators, "generator.js")
2035 INSTALL_EXPERIMENTAL_NATIVE(i, iteration, "array-iterator.js")
2036 INSTALL_EXPERIMENTAL_NATIVE(i, strings, "harmony-string.js")
2037 INSTALL_EXPERIMENTAL_NATIVE(i, arrays, "harmony-array.js")
2038 INSTALL_EXPERIMENTAL_NATIVE(i, maths, "harmony-math.js")
danno@chromium.org160a7b02011-04-18 15:51:38 +00002039 }
ager@chromium.orgea91cc52011-05-23 06:06:11 +00002040
2041 InstallExperimentalNativeFunctions();
2042
danno@chromium.org160a7b02011-04-18 15:51:38 +00002043 return true;
2044}
2045
2046
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00002047static Handle<JSObject> ResolveBuiltinIdHolder(
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00002048 Handle<Context> native_context,
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00002049 const char* holder_expr) {
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00002050 Isolate* isolate = native_context->GetIsolate();
2051 Factory* factory = isolate->factory();
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00002052 Handle<GlobalObject> global(native_context->global_object());
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00002053 const char* period_pos = strchr(holder_expr, '.');
2054 if (period_pos == NULL) {
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00002055 return Handle<JSObject>::cast(GetProperty(
2056 isolate, global, factory->InternalizeUtf8String(holder_expr)));
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +00002057 }
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00002058 ASSERT_EQ(".prototype", period_pos);
2059 Vector<const char> property(holder_expr,
2060 static_cast<int>(period_pos - holder_expr));
2061 Handle<JSFunction> function = Handle<JSFunction>::cast(
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00002062 GetProperty(isolate, global, factory->InternalizeUtf8String(property)));
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00002063 return Handle<JSObject>(JSObject::cast(function->prototype()));
2064}
2065
2066
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00002067static void InstallBuiltinFunctionId(Handle<JSObject> holder,
2068 const char* function_name,
2069 BuiltinFunctionId id) {
danno@chromium.org160a7b02011-04-18 15:51:38 +00002070 Factory* factory = holder->GetIsolate()->factory();
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00002071 Handle<String> name = factory->InternalizeUtf8String(function_name);
lrn@chromium.org303ada72010-10-27 09:33:13 +00002072 Object* function_object = holder->GetProperty(*name)->ToObjectUnchecked();
2073 Handle<JSFunction> function(JSFunction::cast(function_object));
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00002074 function->shared()->set_function_data(Smi::FromInt(id));
2075}
2076
2077
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00002078void Genesis::InstallBuiltinFunctionIds() {
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00002079 HandleScope scope(isolate());
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00002080#define INSTALL_BUILTIN_ID(holder_expr, fun_name, name) \
2081 { \
2082 Handle<JSObject> holder = ResolveBuiltinIdHolder( \
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00002083 native_context(), #holder_expr); \
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00002084 BuiltinFunctionId id = k##name; \
2085 InstallBuiltinFunctionId(holder, #fun_name, id); \
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00002086 }
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00002087 FUNCTIONS_WITH_ID_LIST(INSTALL_BUILTIN_ID)
2088#undef INSTALL_BUILTIN_ID
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00002089}
2090
2091
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00002092// Do not forget to update macros.py with named constant
2093// of cache id.
2094#define JSFUNCTION_RESULT_CACHE_LIST(F) \
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00002095 F(16, native_context()->regexp_function())
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00002096
2097
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002098static FixedArray* CreateCache(int size, Handle<JSFunction> factory_function) {
danno@chromium.org160a7b02011-04-18 15:51:38 +00002099 Factory* factory = factory_function->GetIsolate()->factory();
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00002100 // Caches are supposed to live for a long time, allocate in old space.
2101 int array_size = JSFunctionResultCache::kEntriesIndex + 2 * size;
ager@chromium.orgac091b72010-05-05 07:34:42 +00002102 // Cannot use cast as object is not fully initialized yet.
2103 JSFunctionResultCache* cache = reinterpret_cast<JSFunctionResultCache*>(
danno@chromium.org160a7b02011-04-18 15:51:38 +00002104 *factory->NewFixedArrayWithHoles(array_size, TENURED));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002105 cache->set(JSFunctionResultCache::kFactoryIndex, *factory_function);
ager@chromium.orgac091b72010-05-05 07:34:42 +00002106 cache->MakeZeroSize();
2107 return cache;
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00002108}
2109
2110
2111void Genesis::InstallJSFunctionResultCaches() {
2112 const int kNumberOfCaches = 0 +
2113#define F(size, func) + 1
2114 JSFUNCTION_RESULT_CACHE_LIST(F)
2115#undef F
2116 ;
2117
verwaest@chromium.orgd4be0f02013-06-05 13:39:03 +00002118 Handle<FixedArray> caches =
2119 factory()->NewFixedArray(kNumberOfCaches, TENURED);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00002120
2121 int index = 0;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002122
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002123#define F(size, func) do { \
2124 FixedArray* cache = CreateCache((size), Handle<JSFunction>(func)); \
2125 caches->set(index++, cache); \
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002126 } while (false)
2127
2128 JSFUNCTION_RESULT_CACHE_LIST(F);
2129
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00002130#undef F
2131
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00002132 native_context()->set_jsfunction_result_caches(*caches);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00002133}
2134
2135
ricow@chromium.org65fae842010-08-25 15:26:24 +00002136void Genesis::InitializeNormalizedMapCaches() {
2137 Handle<FixedArray> array(
verwaest@chromium.orgd4be0f02013-06-05 13:39:03 +00002138 factory()->NewFixedArray(NormalizedMapCache::kEntries, TENURED));
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00002139 native_context()->set_normalized_map_cache(NormalizedMapCache::cast(*array));
ricow@chromium.org65fae842010-08-25 15:26:24 +00002140}
2141
2142
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00002143bool Bootstrapper::InstallExtensions(Handle<Context> native_context,
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002144 v8::ExtensionConfiguration* extensions) {
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00002145 BootstrapperActive active(this);
2146 SaveContext saved_context(isolate_);
2147 isolate_->set_context(*native_context);
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00002148 if (!Genesis::InstallExtensions(native_context, extensions)) return false;
2149 Genesis::InstallSpecialObjects(native_context);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002150 return true;
2151}
2152
2153
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00002154void Genesis::InstallSpecialObjects(Handle<Context> native_context) {
2155 Isolate* isolate = native_context->GetIsolate();
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00002156 Factory* factory = isolate->factory();
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00002157 HandleScope scope(isolate);
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00002158 Handle<JSGlobalObject> global(JSGlobalObject::cast(
2159 native_context->global_object()));
mads.s.agercbaa0602008-08-14 13:41:48 +00002160 // Expose the natives in global if a name for it is specified.
2161 if (FLAG_expose_natives_as != NULL && strlen(FLAG_expose_natives_as) != 0) {
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00002162 Handle<String> natives =
2163 factory->InternalizeUtf8String(FLAG_expose_natives_as);
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00002164 CHECK_NOT_EMPTY_HANDLE(isolate,
2165 JSObject::SetLocalPropertyIgnoreAttributes(
2166 global, natives,
2167 Handle<JSObject>(global->builtins()),
2168 DONT_ENUM));
mads.s.agercbaa0602008-08-14 13:41:48 +00002169 }
2170
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00002171 Handle<Object> Error = GetProperty(global, "Error");
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00002172 if (Error->IsJSObject()) {
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00002173 Handle<String> name = factory->InternalizeOneByteString(
2174 STATIC_ASCII_VECTOR("stackTraceLimit"));
2175 Handle<Smi> stack_trace_limit(
2176 Smi::FromInt(FLAG_stack_trace_limit), isolate);
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00002177 CHECK_NOT_EMPTY_HANDLE(isolate,
2178 JSObject::SetLocalPropertyIgnoreAttributes(
2179 Handle<JSObject>::cast(Error), name,
2180 stack_trace_limit, NONE));
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002181 }
2182
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002183#ifdef ENABLE_DEBUGGER_SUPPORT
mads.s.agercbaa0602008-08-14 13:41:48 +00002184 // Expose the debug global object in global if a name for it is specified.
2185 if (FLAG_expose_debug_as != NULL && strlen(FLAG_expose_debug_as) != 0) {
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00002186 Debug* debug = isolate->debug();
mads.s.agercbaa0602008-08-14 13:41:48 +00002187 // If loading fails we just bail out without installing the
2188 // debugger but without tanking the whole context.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002189 if (!debug->Load()) return;
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002190 // Set the security token for the debugger context to the same as
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00002191 // the shell native context to allow calling between these (otherwise
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002192 // exposing debug global object doesn't make much sense).
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002193 debug->debug_context()->set_security_token(
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00002194 native_context->security_token());
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002195
mads.s.agercbaa0602008-08-14 13:41:48 +00002196 Handle<String> debug_string =
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00002197 factory->InternalizeUtf8String(FLAG_expose_debug_as);
2198 Handle<Object> global_proxy(
2199 debug->debug_context()->global_proxy(), isolate);
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00002200 CHECK_NOT_EMPTY_HANDLE(isolate,
2201 JSObject::SetLocalPropertyIgnoreAttributes(
2202 global, debug_string, global_proxy, DONT_ENUM));
mads.s.agercbaa0602008-08-14 13:41:48 +00002203 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002204#endif
mads.s.agercbaa0602008-08-14 13:41:48 +00002205}
2206
mstarzinger@chromium.orge0e1b0d2013-07-08 08:38:06 +00002207
ricow@chromium.org27bf2882011-11-17 08:34:43 +00002208static uint32_t Hash(RegisteredExtension* extension) {
2209 return v8::internal::ComputePointerHash(extension);
2210}
2211
mstarzinger@chromium.orge0e1b0d2013-07-08 08:38:06 +00002212
ricow@chromium.org27bf2882011-11-17 08:34:43 +00002213static bool MatchRegisteredExtensions(void* key1, void* key2) {
2214 return key1 == key2;
2215}
2216
2217Genesis::ExtensionStates::ExtensionStates()
yangguo@chromium.orgab30bb82012-02-24 14:41:46 +00002218 : map_(MatchRegisteredExtensions, 8) { }
ricow@chromium.org27bf2882011-11-17 08:34:43 +00002219
2220Genesis::ExtensionTraversalState Genesis::ExtensionStates::get_state(
2221 RegisteredExtension* extension) {
2222 i::HashMap::Entry* entry = map_.Lookup(extension, Hash(extension), false);
2223 if (entry == NULL) {
2224 return UNVISITED;
2225 }
2226 return static_cast<ExtensionTraversalState>(
2227 reinterpret_cast<intptr_t>(entry->value));
2228}
2229
2230void Genesis::ExtensionStates::set_state(RegisteredExtension* extension,
2231 ExtensionTraversalState state) {
2232 map_.Lookup(extension, Hash(extension), true)->value =
2233 reinterpret_cast<void*>(static_cast<intptr_t>(state));
2234}
mads.s.agercbaa0602008-08-14 13:41:48 +00002235
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00002236bool Genesis::InstallExtensions(Handle<Context> native_context,
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002237 v8::ExtensionConfiguration* extensions) {
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00002238 Isolate* isolate = native_context->GetIsolate();
ricow@chromium.org27bf2882011-11-17 08:34:43 +00002239 ExtensionStates extension_states; // All extensions have state UNVISITED.
2240 // Install auto extensions.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002241 v8::RegisteredExtension* current = v8::RegisteredExtension::first_extension();
2242 while (current != NULL) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002243 if (current->extension()->auto_enable())
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00002244 InstallExtension(isolate, current, &extension_states);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002245 current = current->next();
2246 }
2247
machenbach@chromium.orgc86e8c22013-11-27 15:11:04 +00002248#ifdef ADDRESS_SANITIZER
2249 if (FLAG_expose_free_buffer) {
2250 InstallExtension(isolate, "v8/free-buffer", &extension_states);
2251 }
2252#endif
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00002253 if (FLAG_expose_gc) InstallExtension(isolate, "v8/gc", &extension_states);
ricow@chromium.org27bf2882011-11-17 08:34:43 +00002254 if (FLAG_expose_externalize_string) {
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00002255 InstallExtension(isolate, "v8/externalize", &extension_states);
ricow@chromium.org27bf2882011-11-17 08:34:43 +00002256 }
yangguo@chromium.org304cc332012-07-24 07:59:48 +00002257 if (FLAG_track_gc_object_stats) {
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00002258 InstallExtension(isolate, "v8/statistics", &extension_states);
yangguo@chromium.org304cc332012-07-24 07:59:48 +00002259 }
machenbach@chromium.orgafbdadc2013-12-09 16:12:18 +00002260 if (FLAG_expose_trigger_failure) {
2261 InstallExtension(isolate, "v8/trigger-failure", &extension_states);
2262 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002263
2264 if (extensions == NULL) return true;
2265 // Install required extensions
2266 int count = v8::ImplementationUtilities::GetNameCount(extensions);
2267 const char** names = v8::ImplementationUtilities::GetNames(extensions);
2268 for (int i = 0; i < count; i++) {
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00002269 if (!InstallExtension(isolate, names[i], &extension_states))
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002270 return false;
2271 }
2272
2273 return true;
2274}
2275
2276
2277// Installs a named extension. This methods is unoptimized and does
2278// not scale well if we want to support a large number of extensions.
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00002279bool Genesis::InstallExtension(Isolate* isolate,
2280 const char* name,
ricow@chromium.org27bf2882011-11-17 08:34:43 +00002281 ExtensionStates* extension_states) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002282 v8::RegisteredExtension* current = v8::RegisteredExtension::first_extension();
2283 // Loop until we find the relevant extension
2284 while (current != NULL) {
2285 if (strcmp(name, current->extension()->name()) == 0) break;
2286 current = current->next();
2287 }
2288 // Didn't find the extension; fail.
2289 if (current == NULL) {
2290 v8::Utils::ReportApiFailure(
2291 "v8::Context::New()", "Cannot find required extension");
2292 return false;
2293 }
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00002294 return InstallExtension(isolate, current, extension_states);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002295}
2296
2297
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00002298bool Genesis::InstallExtension(Isolate* isolate,
2299 v8::RegisteredExtension* current,
ricow@chromium.org27bf2882011-11-17 08:34:43 +00002300 ExtensionStates* extension_states) {
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00002301 HandleScope scope(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002302
ricow@chromium.org27bf2882011-11-17 08:34:43 +00002303 if (extension_states->get_state(current) == INSTALLED) return true;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002304 // The current node has already been visited so there must be a
2305 // cycle in the dependency graph; fail.
ricow@chromium.org27bf2882011-11-17 08:34:43 +00002306 if (extension_states->get_state(current) == VISITED) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002307 v8::Utils::ReportApiFailure(
2308 "v8::Context::New()", "Circular extension dependency");
2309 return false;
2310 }
ricow@chromium.org27bf2882011-11-17 08:34:43 +00002311 ASSERT(extension_states->get_state(current) == UNVISITED);
2312 extension_states->set_state(current, VISITED);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002313 v8::Extension* extension = current->extension();
2314 // Install the extension's dependencies
2315 for (int i = 0; i < extension->dependency_count(); i++) {
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00002316 if (!InstallExtension(isolate,
2317 extension->dependencies()[i],
2318 extension_states)) {
ricow@chromium.org27bf2882011-11-17 08:34:43 +00002319 return false;
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00002320 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002321 }
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002322 Handle<String> source_code =
2323 isolate->factory()->NewExternalStringFromAscii(extension->source());
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00002324 bool result = CompileScriptCached(isolate,
2325 CStrVector(extension->name()),
2326 source_code,
2327 isolate->bootstrapper()->extensions_cache(),
2328 extension,
2329 Handle<Context>(isolate->context()),
2330 false);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002331 ASSERT(isolate->has_pending_exception() != result);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002332 if (!result) {
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002333 // We print out the name of the extension that fail to install.
2334 // When an error is thrown during bootstrapping we automatically print
2335 // the line number at which this happened to the console in the isolate
2336 // error throwing functionality.
2337 OS::PrintError("Error installing extension '%s'.\n",
2338 current->extension()->name());
lrn@chromium.org7516f052011-03-30 08:52:27 +00002339 isolate->clear_pending_exception();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002340 }
ricow@chromium.org27bf2882011-11-17 08:34:43 +00002341 extension_states->set_state(current, INSTALLED);
2342 isolate->NotifyExtensionInstalled();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002343 return result;
2344}
2345
2346
ager@chromium.org5c838252010-02-19 08:53:10 +00002347bool Genesis::InstallJSBuiltins(Handle<JSBuiltinsObject> builtins) {
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00002348 HandleScope scope(isolate());
ager@chromium.org5c838252010-02-19 08:53:10 +00002349 for (int i = 0; i < Builtins::NumberOfJavaScriptBuiltins(); i++) {
2350 Builtins::JavaScript id = static_cast<Builtins::JavaScript>(i);
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00002351 Handle<String> name =
2352 factory()->InternalizeUtf8String(Builtins::GetName(id));
lrn@chromium.org303ada72010-10-27 09:33:13 +00002353 Object* function_object = builtins->GetPropertyNoExceptionThrown(*name);
ager@chromium.org5c838252010-02-19 08:53:10 +00002354 Handle<JSFunction> function
lrn@chromium.org303ada72010-10-27 09:33:13 +00002355 = Handle<JSFunction>(JSFunction::cast(function_object));
ager@chromium.org5c838252010-02-19 08:53:10 +00002356 builtins->set_javascript_builtin(id, *function);
yangguo@chromium.org49546742013-12-23 16:17:49 +00002357 if (!Compiler::EnsureCompiled(function, CLEAR_EXCEPTION)) {
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002358 return false;
2359 }
rossberg@chromium.org400388e2012-06-06 09:29:22 +00002360 builtins->set_javascript_builtin_code(id, function->shared()->code());
ager@chromium.org5c838252010-02-19 08:53:10 +00002361 }
2362 return true;
2363}
2364
2365
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002366bool Genesis::ConfigureGlobalObjects(
2367 v8::Handle<v8::ObjectTemplate> global_proxy_template) {
2368 Handle<JSObject> global_proxy(
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00002369 JSObject::cast(native_context()->global_proxy()));
2370 Handle<JSObject> inner_global(
2371 JSObject::cast(native_context()->global_object()));
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002372
2373 if (!global_proxy_template.IsEmpty()) {
2374 // Configure the global proxy object.
2375 Handle<ObjectTemplateInfo> proxy_data =
2376 v8::Utils::OpenHandle(*global_proxy_template);
2377 if (!ConfigureApiObject(global_proxy, proxy_data)) return false;
2378
2379 // Configure the inner global object.
2380 Handle<FunctionTemplateInfo> proxy_constructor(
2381 FunctionTemplateInfo::cast(proxy_data->constructor()));
2382 if (!proxy_constructor->prototype_template()->IsUndefined()) {
2383 Handle<ObjectTemplateInfo> inner_data(
2384 ObjectTemplateInfo::cast(proxy_constructor->prototype_template()));
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002385 if (!ConfigureApiObject(inner_global, inner_data)) return false;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002386 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002387 }
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002388
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002389 SetObjectPrototype(global_proxy, inner_global);
ulan@chromium.org906e2fb2013-05-14 08:14:38 +00002390
2391 native_context()->set_initial_array_prototype(
2392 JSArray::cast(native_context()->array_function()->prototype()));
2393
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002394 return true;
2395}
2396
2397
2398bool Genesis::ConfigureApiObject(Handle<JSObject> object,
2399 Handle<ObjectTemplateInfo> object_template) {
2400 ASSERT(!object_template.is_null());
machenbach@chromium.org9af454f2013-11-20 09:25:57 +00002401 ASSERT(FunctionTemplateInfo::cast(object_template->constructor())
2402 ->IsTemplateFor(object->map()));;
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002403
2404 bool pending_exception = false;
2405 Handle<JSObject> obj =
2406 Execution::InstantiateObject(object_template, &pending_exception);
2407 if (pending_exception) {
danno@chromium.org160a7b02011-04-18 15:51:38 +00002408 ASSERT(isolate()->has_pending_exception());
2409 isolate()->clear_pending_exception();
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002410 return false;
2411 }
2412 TransferObject(obj, object);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002413 return true;
2414}
2415
2416
2417void Genesis::TransferNamedProperties(Handle<JSObject> from,
2418 Handle<JSObject> to) {
2419 if (from->HasFastProperties()) {
2420 Handle<DescriptorArray> descs =
2421 Handle<DescriptorArray>(from->map()->instance_descriptors());
ulan@chromium.org57ff8812013-05-10 08:16:55 +00002422 for (int i = 0; i < from->map()->NumberOfOwnDescriptors(); i++) {
erik.corry@gmail.comed49e962012-04-17 11:57:53 +00002423 PropertyDetails details = descs->GetDetails(i);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002424 switch (details.type()) {
2425 case FIELD: {
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00002426 HandleScope inner(isolate());
ulan@chromium.org750145a2013-03-07 15:14:13 +00002427 Handle<Name> key = Handle<Name>(descs->GetKey(i));
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002428 int index = descs->GetFieldIndex(i);
ulan@chromium.org57ff8812013-05-10 08:16:55 +00002429 ASSERT(!descs->GetDetails(i).representation().IsDouble());
2430 Handle<Object> value = Handle<Object>(from->RawFastPropertyAt(index),
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00002431 isolate());
2432 CHECK_NOT_EMPTY_HANDLE(isolate(),
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00002433 JSObject::SetLocalPropertyIgnoreAttributes(
2434 to, key, value, details.attributes()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002435 break;
2436 }
jkummerow@chromium.orgfb732b12013-07-26 10:27:09 +00002437 case CONSTANT: {
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00002438 HandleScope inner(isolate());
ulan@chromium.org750145a2013-03-07 15:14:13 +00002439 Handle<Name> key = Handle<Name>(descs->GetKey(i));
jkummerow@chromium.orgfb732b12013-07-26 10:27:09 +00002440 Handle<Object> constant(descs->GetConstant(i), isolate());
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00002441 CHECK_NOT_EMPTY_HANDLE(isolate(),
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00002442 JSObject::SetLocalPropertyIgnoreAttributes(
jkummerow@chromium.orgfb732b12013-07-26 10:27:09 +00002443 to, key, constant, details.attributes()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002444 break;
2445 }
2446 case CALLBACKS: {
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002447 LookupResult result(isolate());
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002448 to->LocalLookup(descs->GetKey(i), &result);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002449 // If the property is already there we skip it
verwaest@chromium.org753aee42012-07-17 16:15:42 +00002450 if (result.IsFound()) continue;
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00002451 HandleScope inner(isolate());
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002452 ASSERT(!to->HasFastProperties());
2453 // Add to dictionary.
ulan@chromium.org750145a2013-03-07 15:14:13 +00002454 Handle<Name> key = Handle<Name>(descs->GetKey(i));
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00002455 Handle<Object> callbacks(descs->GetCallbacksObject(i), isolate());
ulan@chromium.org57ff8812013-05-10 08:16:55 +00002456 PropertyDetails d = PropertyDetails(
2457 details.attributes(), CALLBACKS, i + 1);
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00002458 JSObject::SetNormalizedProperty(to, key, callbacks, d);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002459 break;
2460 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002461 case NORMAL:
2462 // Do not occur since the from object has fast properties.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002463 case HANDLER:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002464 case INTERCEPTOR:
yangguo@chromium.org99aa4902012-07-06 16:21:55 +00002465 case TRANSITION:
jkummerow@chromium.org7a6fc812012-06-27 11:12:38 +00002466 case NONEXISTENT:
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002467 // No element in instance descriptors have proxy or interceptor type.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002468 UNREACHABLE();
2469 break;
2470 }
2471 }
2472 } else {
ulan@chromium.org750145a2013-03-07 15:14:13 +00002473 Handle<NameDictionary> properties =
2474 Handle<NameDictionary>(from->property_dictionary());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002475 int capacity = properties->Capacity();
2476 for (int i = 0; i < capacity; i++) {
2477 Object* raw_key(properties->KeyAt(i));
2478 if (properties->IsKey(raw_key)) {
ulan@chromium.org750145a2013-03-07 15:14:13 +00002479 ASSERT(raw_key->IsName());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002480 // If the property is already there we skip it.
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002481 LookupResult result(isolate());
ulan@chromium.org750145a2013-03-07 15:14:13 +00002482 to->LocalLookup(Name::cast(raw_key), &result);
verwaest@chromium.org753aee42012-07-17 16:15:42 +00002483 if (result.IsFound()) continue;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002484 // Set the property.
ulan@chromium.org750145a2013-03-07 15:14:13 +00002485 Handle<Name> key = Handle<Name>(Name::cast(raw_key));
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00002486 Handle<Object> value = Handle<Object>(properties->ValueAt(i),
2487 isolate());
danno@chromium.org41728482013-06-12 22:31:22 +00002488 ASSERT(!value->IsCell());
dslomov@chromium.orgb752d402013-06-18 11:54:54 +00002489 if (value->IsPropertyCell()) {
2490 value = Handle<Object>(PropertyCell::cast(*value)->value(),
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00002491 isolate());
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002492 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002493 PropertyDetails details = properties->DetailsAt(i);
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00002494 CHECK_NOT_EMPTY_HANDLE(isolate(),
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00002495 JSObject::SetLocalPropertyIgnoreAttributes(
2496 to, key, value, details.attributes()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002497 }
2498 }
2499 }
2500}
2501
2502
2503void Genesis::TransferIndexedProperties(Handle<JSObject> from,
2504 Handle<JSObject> to) {
2505 // Cloning the elements array is sufficient.
2506 Handle<FixedArray> from_elements =
2507 Handle<FixedArray>(FixedArray::cast(from->elements()));
verwaest@chromium.orgd4be0f02013-06-05 13:39:03 +00002508 Handle<FixedArray> to_elements = factory()->CopyFixedArray(from_elements);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002509 to->set_elements(*to_elements);
2510}
2511
2512
2513void Genesis::TransferObject(Handle<JSObject> from, Handle<JSObject> to) {
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00002514 HandleScope outer(isolate());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002515
2516 ASSERT(!from->IsJSArray());
2517 ASSERT(!to->IsJSArray());
2518
2519 TransferNamedProperties(from, to);
2520 TransferIndexedProperties(from, to);
2521
2522 // Transfer the prototype (new map is needed).
2523 Handle<Map> old_to_map = Handle<Map>(to->map());
verwaest@chromium.orgd4be0f02013-06-05 13:39:03 +00002524 Handle<Map> new_to_map = factory()->CopyMap(old_to_map);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002525 new_to_map->set_prototype(from->map()->prototype());
2526 to->set_map(*new_to_map);
2527}
2528
2529
2530void Genesis::MakeFunctionInstancePrototypeWritable() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002531 // The maps with writable prototype are created in CreateEmptyFunction
2532 // and CreateStrictModeFunctionMaps respectively. Initially the maps are
2533 // created with read-only prototype for JS builtins processing.
ulan@chromium.org906e2fb2013-05-14 08:14:38 +00002534 ASSERT(!function_map_writable_prototype_.is_null());
2535 ASSERT(!strict_mode_function_map_writable_prototype_.is_null());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002536
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002537 // Replace function instance maps to make prototype writable.
ulan@chromium.org906e2fb2013-05-14 08:14:38 +00002538 native_context()->set_function_map(*function_map_writable_prototype_);
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00002539 native_context()->set_strict_mode_function_map(
ulan@chromium.org906e2fb2013-05-14 08:14:38 +00002540 *strict_mode_function_map_writable_prototype_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002541}
2542
2543
danno@chromium.org160a7b02011-04-18 15:51:38 +00002544Genesis::Genesis(Isolate* isolate,
2545 Handle<Object> global_object,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002546 v8::Handle<v8::ObjectTemplate> global_template,
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00002547 v8::ExtensionConfiguration* extensions)
2548 : isolate_(isolate),
2549 active_(isolate->bootstrapper()) {
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00002550 result_ = Handle<Context>::null();
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +00002551 // If V8 cannot be initialized, just return.
2552 if (!V8::Initialize(NULL)) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002553
2554 // Before creating the roots we must save the context and restore it
2555 // on all function exits.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002556 SaveContext saved_context(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002557
fschneider@chromium.org7d10be52012-04-10 12:30:14 +00002558 // During genesis, the boilerplate for stack overflow won't work until the
2559 // environment has been at least partially initialized. Add a stack check
2560 // before entering JS code to catch overflow early.
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00002561 StackLimitCheck check(isolate);
fschneider@chromium.org7d10be52012-04-10 12:30:14 +00002562 if (check.HasOverflowed()) return;
2563
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +00002564 // We can only de-serialize a context if the isolate was initialized from
2565 // a snapshot. Otherwise we have to build the context from scratch.
2566 if (isolate->initialized_from_snapshot()) {
jkummerow@chromium.org3d00d0a2013-09-04 13:57:32 +00002567 native_context_ = Snapshot::NewContextFromSnapshot(isolate);
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +00002568 } else {
2569 native_context_ = Handle<Context>();
2570 }
2571
jkummerow@chromium.org7bd87f02013-03-20 18:06:29 +00002572 if (!native_context().is_null()) {
2573 AddToWeakNativeContextList(*native_context());
2574 isolate->set_context(*native_context());
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002575 isolate->counters()->contexts_created_by_snapshot()->Increment();
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002576 Handle<GlobalObject> inner_global;
2577 Handle<JSGlobalProxy> global_proxy =
2578 CreateNewGlobals(global_template,
2579 global_object,
2580 &inner_global);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002581
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002582 HookUpGlobalProxy(inner_global, global_proxy);
2583 HookUpInnerGlobal(inner_global);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002584
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002585 if (!ConfigureGlobalObjects(global_template)) return;
2586 } else {
2587 // We get here if there was no context snapshot.
2588 CreateRoots();
danno@chromium.org160a7b02011-04-18 15:51:38 +00002589 Handle<JSFunction> empty_function = CreateEmptyFunction(isolate);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002590 CreateStrictModeFunctionMaps(empty_function);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002591 Handle<GlobalObject> inner_global;
2592 Handle<JSGlobalProxy> global_proxy =
2593 CreateNewGlobals(global_template, global_object, &inner_global);
2594 HookUpGlobalProxy(inner_global, global_proxy);
jkummerow@chromium.orgba72ec82013-07-22 09:21:20 +00002595 InitializeGlobal(inner_global, empty_function);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00002596 InstallJSFunctionResultCaches();
ricow@chromium.org65fae842010-08-25 15:26:24 +00002597 InitializeNormalizedMapCaches();
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00002598 if (!InstallNatives()) return;
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002599
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002600 MakeFunctionInstancePrototypeWritable();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002601
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002602 if (!ConfigureGlobalObjects(global_template)) return;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002603 isolate->counters()->contexts_created_from_scratch()->Increment();
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002604 }
mads.s.agercbaa0602008-08-14 13:41:48 +00002605
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +00002606 // Initialize experimental globals and install experimental natives.
2607 InitializeExperimentalGlobal();
danno@chromium.org160a7b02011-04-18 15:51:38 +00002608 if (!InstallExperimentalNatives()) return;
2609
machenbach@chromium.orgf9841892013-11-25 12:01:13 +00002610 // We can't (de-)serialize typed arrays currently, but we are lucky: The state
2611 // of the random number generator needs no initialization during snapshot
2612 // creation time and we don't need trigonometric functions then.
machenbach@chromium.org90dca012013-11-22 10:04:21 +00002613 if (!Serializer::enabled()) {
machenbach@chromium.orgf9841892013-11-25 12:01:13 +00002614 // Initially seed the per-context random number generator using the
2615 // per-isolate random number generator.
2616 const int num_elems = 2;
2617 const int num_bytes = num_elems * sizeof(uint32_t);
2618 uint32_t* state = reinterpret_cast<uint32_t*>(malloc(num_bytes));
2619
2620 do {
2621 isolate->random_number_generator()->NextBytes(state, num_bytes);
2622 } while (state[0] == 0 || state[1] == 0);
2623
machenbach@chromium.org9f18d912013-11-28 13:42:41 +00002624 v8::Local<v8::ArrayBuffer> buffer = v8::ArrayBuffer::New(
2625 reinterpret_cast<v8::Isolate*>(isolate), state, num_bytes);
machenbach@chromium.orgf9841892013-11-25 12:01:13 +00002626 Utils::OpenHandle(*buffer)->set_should_be_freed(true);
2627 v8::Local<v8::Uint32Array> ta = v8::Uint32Array::New(buffer, 0, num_elems);
machenbach@chromium.org90dca012013-11-22 10:04:21 +00002628 Handle<JSBuiltinsObject> builtins(native_context()->builtins());
machenbach@chromium.orgf9841892013-11-25 12:01:13 +00002629 ForceSetProperty(builtins,
2630 factory()->InternalizeOneByteString(
2631 STATIC_ASCII_VECTOR("rngstate")),
2632 Utils::OpenHandle(*ta),
2633 NONE);
2634
machenbach@chromium.org90dca012013-11-22 10:04:21 +00002635 // Initialize trigonometric lookup tables and constants.
machenbach@chromium.org90dca012013-11-22 10:04:21 +00002636 const int table_num_bytes = TrigonometricLookupTable::table_num_bytes();
2637 v8::Local<v8::ArrayBuffer> sin_buffer = v8::ArrayBuffer::New(
machenbach@chromium.org9f18d912013-11-28 13:42:41 +00002638 reinterpret_cast<v8::Isolate*>(isolate),
machenbach@chromium.org90dca012013-11-22 10:04:21 +00002639 TrigonometricLookupTable::sin_table(), table_num_bytes);
2640 v8::Local<v8::ArrayBuffer> cos_buffer = v8::ArrayBuffer::New(
machenbach@chromium.org9f18d912013-11-28 13:42:41 +00002641 reinterpret_cast<v8::Isolate*>(isolate),
machenbach@chromium.org90dca012013-11-22 10:04:21 +00002642 TrigonometricLookupTable::cos_x_interval_table(), table_num_bytes);
2643 v8::Local<v8::Float64Array> sin_table = v8::Float64Array::New(
2644 sin_buffer, 0, TrigonometricLookupTable::table_size());
2645 v8::Local<v8::Float64Array> cos_table = v8::Float64Array::New(
2646 cos_buffer, 0, TrigonometricLookupTable::table_size());
2647
2648 ForceSetProperty(builtins,
2649 factory()->InternalizeOneByteString(
2650 STATIC_ASCII_VECTOR("kSinTable")),
2651 Utils::OpenHandle(*sin_table),
2652 NONE);
2653 ForceSetProperty(builtins,
2654 factory()->InternalizeOneByteString(
2655 STATIC_ASCII_VECTOR("kCosXIntervalTable")),
2656 Utils::OpenHandle(*cos_table),
2657 NONE);
2658 ForceSetProperty(builtins,
2659 factory()->InternalizeOneByteString(
2660 STATIC_ASCII_VECTOR("kSamples")),
2661 factory()->NewHeapNumber(
2662 TrigonometricLookupTable::samples()),
2663 NONE);
2664 ForceSetProperty(builtins,
2665 factory()->InternalizeOneByteString(
2666 STATIC_ASCII_VECTOR("kIndexConvert")),
2667 factory()->NewHeapNumber(
2668 TrigonometricLookupTable::samples_over_pi_half()),
2669 NONE);
2670 }
2671
jkummerow@chromium.org7bd87f02013-03-20 18:06:29 +00002672 result_ = native_context();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002673}
2674
ager@chromium.orgddb913d2009-01-27 10:01:48 +00002675
2676// Support for thread preemption.
2677
2678// Reserve space for statics needing saving and restoring.
2679int Bootstrapper::ArchiveSpacePerThread() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002680 return sizeof(NestingCounterType);
ager@chromium.orgddb913d2009-01-27 10:01:48 +00002681}
2682
2683
2684// Archive statics that are thread local.
2685char* Bootstrapper::ArchiveState(char* to) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002686 *reinterpret_cast<NestingCounterType*>(to) = nesting_;
2687 nesting_ = 0;
2688 return to + sizeof(NestingCounterType);
ager@chromium.orgddb913d2009-01-27 10:01:48 +00002689}
2690
2691
2692// Restore statics that are thread local.
2693char* Bootstrapper::RestoreState(char* from) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002694 nesting_ = *reinterpret_cast<NestingCounterType*>(from);
2695 return from + sizeof(NestingCounterType);
ager@chromium.orgddb913d2009-01-27 10:01:48 +00002696}
2697
2698
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002699// Called when the top-level V8 mutex is destroyed.
2700void Bootstrapper::FreeThreadResources() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002701 ASSERT(!IsActive());
ager@chromium.orgddb913d2009-01-27 10:01:48 +00002702}
2703
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002704} } // namespace v8::internal