blob: bb0e85f94c4bd38e11bc6c0a1a429067411c1821 [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 "api.h"
v8.team.kasperl727e9952008-09-02 14:56:44 +000031#include "debug.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000032#include "execution.h"
33#include "factory.h"
34#include "macro-assembler.h"
kasperl@chromium.orga5551262010-12-07 12:49:48 +000035#include "objects.h"
ager@chromium.orgea4f62e2010-08-16 16:28:43 +000036#include "objects-visiting.h"
verwaest@chromium.org37141392012-05-31 13:27:02 +000037#include "platform.h"
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +000038#include "scopeinfo.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000039
kasperl@chromium.org71affb52009-05-26 05:44:31 +000040namespace v8 {
41namespace internal {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000042
43
44Handle<FixedArray> Factory::NewFixedArray(int size, PretenureFlag pretenure) {
45 ASSERT(0 <= size);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000046 CALL_HEAP_FUNCTION(
47 isolate(),
48 isolate()->heap()->AllocateFixedArray(size, pretenure),
49 FixedArray);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000050}
51
52
ricow@chromium.orgc9c80822010-04-21 08:22:37 +000053Handle<FixedArray> Factory::NewFixedArrayWithHoles(int size,
54 PretenureFlag pretenure) {
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +000055 ASSERT(0 <= size);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000056 CALL_HEAP_FUNCTION(
57 isolate(),
58 isolate()->heap()->AllocateFixedArrayWithHoles(size, pretenure),
59 FixedArray);
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +000060}
61
62
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +000063Handle<FixedDoubleArray> Factory::NewFixedDoubleArray(int size,
64 PretenureFlag pretenure) {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +000065 ASSERT(0 <= size);
66 CALL_HEAP_FUNCTION(
67 isolate(),
68 isolate()->heap()->AllocateUninitializedFixedDoubleArray(size, pretenure),
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +000069 FixedDoubleArray);
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +000070}
71
72
kasperl@chromium.org86f77b72009-07-06 08:21:57 +000073Handle<StringDictionary> Factory::NewStringDictionary(int at_least_space_for) {
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +000074 ASSERT(0 <= at_least_space_for);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000075 CALL_HEAP_FUNCTION(isolate(),
76 StringDictionary::Allocate(at_least_space_for),
kasperl@chromium.org86f77b72009-07-06 08:21:57 +000077 StringDictionary);
78}
79
80
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +000081Handle<SeededNumberDictionary> Factory::NewSeededNumberDictionary(
82 int at_least_space_for) {
kasperl@chromium.org86f77b72009-07-06 08:21:57 +000083 ASSERT(0 <= at_least_space_for);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000084 CALL_HEAP_FUNCTION(isolate(),
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +000085 SeededNumberDictionary::Allocate(at_least_space_for),
86 SeededNumberDictionary);
87}
88
89
90Handle<UnseededNumberDictionary> Factory::NewUnseededNumberDictionary(
91 int at_least_space_for) {
92 ASSERT(0 <= at_least_space_for);
93 CALL_HEAP_FUNCTION(isolate(),
94 UnseededNumberDictionary::Allocate(at_least_space_for),
95 UnseededNumberDictionary);
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +000096}
97
98
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +000099Handle<ObjectHashSet> Factory::NewObjectHashSet(int at_least_space_for) {
100 ASSERT(0 <= at_least_space_for);
101 CALL_HEAP_FUNCTION(isolate(),
102 ObjectHashSet::Allocate(at_least_space_for),
103 ObjectHashSet);
104}
105
106
vegorov@chromium.org7943d462011-08-01 11:41:52 +0000107Handle<ObjectHashTable> Factory::NewObjectHashTable(int at_least_space_for) {
108 ASSERT(0 <= at_least_space_for);
109 CALL_HEAP_FUNCTION(isolate(),
110 ObjectHashTable::Allocate(at_least_space_for),
111 ObjectHashTable);
112}
113
114
verwaest@chromium.org33e09c82012-10-10 17:07:22 +0000115Handle<DescriptorArray> Factory::NewDescriptorArray(int number_of_descriptors,
116 int slack) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000117 ASSERT(0 <= number_of_descriptors);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000118 CALL_HEAP_FUNCTION(isolate(),
verwaest@chromium.org33e09c82012-10-10 17:07:22 +0000119 DescriptorArray::Allocate(number_of_descriptors, slack),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000120 DescriptorArray);
121}
122
123
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000124Handle<DeoptimizationInputData> Factory::NewDeoptimizationInputData(
125 int deopt_entry_count,
126 PretenureFlag pretenure) {
127 ASSERT(deopt_entry_count > 0);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000128 CALL_HEAP_FUNCTION(isolate(),
129 DeoptimizationInputData::Allocate(deopt_entry_count,
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000130 pretenure),
131 DeoptimizationInputData);
132}
133
134
135Handle<DeoptimizationOutputData> Factory::NewDeoptimizationOutputData(
136 int deopt_entry_count,
137 PretenureFlag pretenure) {
138 ASSERT(deopt_entry_count > 0);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000139 CALL_HEAP_FUNCTION(isolate(),
140 DeoptimizationOutputData::Allocate(deopt_entry_count,
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000141 pretenure),
142 DeoptimizationOutputData);
143}
144
145
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000146Handle<AccessorPair> Factory::NewAccessorPair() {
147 CALL_HEAP_FUNCTION(isolate(),
148 isolate()->heap()->AllocateAccessorPair(),
149 AccessorPair);
150}
151
152
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000153Handle<TypeFeedbackInfo> Factory::NewTypeFeedbackInfo() {
154 CALL_HEAP_FUNCTION(isolate(),
155 isolate()->heap()->AllocateTypeFeedbackInfo(),
156 TypeFeedbackInfo);
157}
158
159
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000160// Internalized strings are created in the old generation (data space).
161Handle<String> Factory::InternalizeUtf8String(Vector<const char> string) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000162 CALL_HEAP_FUNCTION(isolate(),
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000163 isolate()->heap()->InternalizeUtf8String(string),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000164 String);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000165}
166
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000167// Internalized strings are created in the old generation (data space).
168Handle<String> Factory::InternalizeString(Handle<String> string) {
danno@chromium.org40cb8782011-05-25 07:58:50 +0000169 CALL_HEAP_FUNCTION(isolate(),
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000170 isolate()->heap()->InternalizeString(*string),
danno@chromium.org40cb8782011-05-25 07:58:50 +0000171 String);
172}
173
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000174Handle<String> Factory::InternalizeOneByteString(Vector<const uint8_t> string) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000175 CALL_HEAP_FUNCTION(isolate(),
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000176 isolate()->heap()->InternalizeOneByteString(string),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000177 String);
fschneider@chromium.org9e3e0b62011-01-03 10:16:46 +0000178}
179
danno@chromium.org40cb8782011-05-25 07:58:50 +0000180
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000181Handle<String> Factory::InternalizeOneByteString(
182 Handle<SeqOneByteString> string, int from, int length) {
danno@chromium.org40cb8782011-05-25 07:58:50 +0000183 CALL_HEAP_FUNCTION(isolate(),
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000184 isolate()->heap()->InternalizeOneByteString(
185 string, from, length),
danno@chromium.org40cb8782011-05-25 07:58:50 +0000186 String);
187}
188
189
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000190Handle<String> Factory::InternalizeTwoByteString(Vector<const uc16> string) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000191 CALL_HEAP_FUNCTION(isolate(),
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000192 isolate()->heap()->InternalizeTwoByteString(string),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000193 String);
fschneider@chromium.org9e3e0b62011-01-03 10:16:46 +0000194}
195
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000196
jkummerow@chromium.org59297c72013-01-09 16:32:23 +0000197Handle<String> Factory::NewStringFromOneByte(Vector<const uint8_t> string,
198 PretenureFlag pretenure) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000199 CALL_HEAP_FUNCTION(
200 isolate(),
ulan@chromium.org8e8d8822012-11-23 14:36:46 +0000201 isolate()->heap()->AllocateStringFromOneByte(string, pretenure),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000202 String);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000203}
204
205Handle<String> Factory::NewStringFromUtf8(Vector<const char> string,
206 PretenureFlag pretenure) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000207 CALL_HEAP_FUNCTION(
208 isolate(),
209 isolate()->heap()->AllocateStringFromUtf8(string, pretenure),
210 String);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000211}
212
213
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000214Handle<String> Factory::NewStringFromTwoByte(Vector<const uc16> string,
215 PretenureFlag pretenure) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000216 CALL_HEAP_FUNCTION(
217 isolate(),
218 isolate()->heap()->AllocateStringFromTwoByte(string, pretenure),
219 String);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000220}
221
222
yangguo@chromium.orgfb377212012-11-16 14:43:43 +0000223Handle<SeqOneByteString> Factory::NewRawOneByteString(int length,
ager@chromium.org04921a82011-06-27 13:21:41 +0000224 PretenureFlag pretenure) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000225 CALL_HEAP_FUNCTION(
226 isolate(),
ulan@chromium.org8e8d8822012-11-23 14:36:46 +0000227 isolate()->heap()->AllocateRawOneByteString(length, pretenure),
yangguo@chromium.orgfb377212012-11-16 14:43:43 +0000228 SeqOneByteString);
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +0000229}
230
231
ager@chromium.org04921a82011-06-27 13:21:41 +0000232Handle<SeqTwoByteString> Factory::NewRawTwoByteString(int length,
233 PretenureFlag pretenure) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000234 CALL_HEAP_FUNCTION(
235 isolate(),
236 isolate()->heap()->AllocateRawTwoByteString(length, pretenure),
ager@chromium.org04921a82011-06-27 13:21:41 +0000237 SeqTwoByteString);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000238}
239
240
241Handle<String> Factory::NewConsString(Handle<String> first,
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000242 Handle<String> second) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000243 CALL_HEAP_FUNCTION(isolate(),
244 isolate()->heap()->AllocateConsString(*first, *second),
245 String);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000246}
247
248
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000249Handle<String> Factory::NewSubString(Handle<String> str,
250 int begin,
251 int end) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000252 CALL_HEAP_FUNCTION(isolate(),
253 str->SubString(begin, end),
254 String);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000255}
256
257
ager@chromium.org04921a82011-06-27 13:21:41 +0000258Handle<String> Factory::NewProperSubString(Handle<String> str,
259 int begin,
260 int end) {
261 ASSERT(begin > 0 || end < str->length());
262 CALL_HEAP_FUNCTION(isolate(),
263 isolate()->heap()->AllocateSubString(*str, begin, end),
264 String);
265}
266
267
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000268Handle<String> Factory::NewExternalStringFromAscii(
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000269 const ExternalAsciiString::Resource* resource) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000270 CALL_HEAP_FUNCTION(
271 isolate(),
272 isolate()->heap()->AllocateExternalStringFromAscii(resource),
273 String);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000274}
275
276
277Handle<String> Factory::NewExternalStringFromTwoByte(
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000278 const ExternalTwoByteString::Resource* resource) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000279 CALL_HEAP_FUNCTION(
280 isolate(),
281 isolate()->heap()->AllocateExternalStringFromTwoByte(resource),
282 String);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000283}
284
285
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000286Handle<Symbol> Factory::NewSymbol() {
287 CALL_HEAP_FUNCTION(
288 isolate(),
289 isolate()->heap()->AllocateSymbol(),
290 Symbol);
291}
292
293
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000294Handle<Context> Factory::NewNativeContext() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000295 CALL_HEAP_FUNCTION(
296 isolate(),
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000297 isolate()->heap()->AllocateNativeContext(),
298 Context);
299}
300
301
302Handle<Context> Factory::NewGlobalContext(Handle<JSFunction> function,
303 Handle<ScopeInfo> scope_info) {
304 CALL_HEAP_FUNCTION(
305 isolate(),
306 isolate()->heap()->AllocateGlobalContext(*function, *scope_info),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000307 Context);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000308}
309
310
danno@chromium.org81cac2b2012-07-10 11:28:27 +0000311Handle<Context> Factory::NewModuleContext(Handle<ScopeInfo> scope_info) {
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000312 CALL_HEAP_FUNCTION(
313 isolate(),
danno@chromium.org81cac2b2012-07-10 11:28:27 +0000314 isolate()->heap()->AllocateModuleContext(*scope_info),
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000315 Context);
316}
317
318
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000319Handle<Context> Factory::NewFunctionContext(int length,
vegorov@chromium.org3cf47312011-06-29 13:20:01 +0000320 Handle<JSFunction> function) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000321 CALL_HEAP_FUNCTION(
322 isolate(),
vegorov@chromium.org3cf47312011-06-29 13:20:01 +0000323 isolate()->heap()->AllocateFunctionContext(length, *function),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000324 Context);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000325}
326
327
vegorov@chromium.org3cf47312011-06-29 13:20:01 +0000328Handle<Context> Factory::NewCatchContext(Handle<JSFunction> function,
329 Handle<Context> previous,
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000330 Handle<String> name,
331 Handle<Object> thrown_object) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000332 CALL_HEAP_FUNCTION(
333 isolate(),
vegorov@chromium.org3cf47312011-06-29 13:20:01 +0000334 isolate()->heap()->AllocateCatchContext(*function,
335 *previous,
336 *name,
337 *thrown_object),
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000338 Context);
339}
340
341
vegorov@chromium.org3cf47312011-06-29 13:20:01 +0000342Handle<Context> Factory::NewWithContext(Handle<JSFunction> function,
343 Handle<Context> previous,
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000344 Handle<JSObject> extension) {
345 CALL_HEAP_FUNCTION(
346 isolate(),
vegorov@chromium.org3cf47312011-06-29 13:20:01 +0000347 isolate()->heap()->AllocateWithContext(*function, *previous, *extension),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000348 Context);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000349}
350
351
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000352Handle<Context> Factory::NewBlockContext(Handle<JSFunction> function,
353 Handle<Context> previous,
354 Handle<ScopeInfo> scope_info) {
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000355 CALL_HEAP_FUNCTION(
356 isolate(),
357 isolate()->heap()->AllocateBlockContext(*function,
358 *previous,
359 *scope_info),
360 Context);
361}
362
363
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000364Handle<Struct> Factory::NewStruct(InstanceType type) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000365 CALL_HEAP_FUNCTION(
366 isolate(),
367 isolate()->heap()->AllocateStruct(type),
368 Struct);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000369}
370
371
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +0000372Handle<DeclaredAccessorInfo> Factory::NewDeclaredAccessorInfo() {
373 Handle<DeclaredAccessorInfo> info =
374 Handle<DeclaredAccessorInfo>::cast(
375 NewStruct(DECLARED_ACCESSOR_INFO_TYPE));
376 info->set_flag(0); // Must clear the flag, it was initialized as undefined.
377 return info;
378}
379
380
381Handle<ExecutableAccessorInfo> Factory::NewExecutableAccessorInfo() {
382 Handle<ExecutableAccessorInfo> info =
383 Handle<ExecutableAccessorInfo>::cast(
384 NewStruct(EXECUTABLE_ACCESSOR_INFO_TYPE));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000385 info->set_flag(0); // Must clear the flag, it was initialized as undefined.
386 return info;
387}
388
389
390Handle<Script> Factory::NewScript(Handle<String> source) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000391 // Generate id for this script.
392 int id;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000393 Heap* heap = isolate()->heap();
394 if (heap->last_script_id()->IsUndefined()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000395 // Script ids start from one.
396 id = 1;
397 } else {
398 // Increment id, wrap when positive smi is exhausted.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000399 id = Smi::cast(heap->last_script_id())->value();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000400 id++;
401 if (!Smi::IsValid(id)) {
402 id = 0;
403 }
404 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000405 heap->SetLastScriptId(Smi::FromInt(id));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000406
407 // Create and initialize script object.
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000408 Handle<Foreign> wrapper = NewForeign(0, TENURED);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000409 Handle<Script> script = Handle<Script>::cast(NewStruct(SCRIPT_TYPE));
410 script->set_source(*source);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000411 script->set_name(heap->undefined_value());
412 script->set_id(heap->last_script_id());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000413 script->set_line_offset(Smi::FromInt(0));
414 script->set_column_offset(Smi::FromInt(0));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000415 script->set_data(heap->undefined_value());
416 script->set_context_data(heap->undefined_value());
ager@chromium.orge2902be2009-06-08 12:21:35 +0000417 script->set_type(Smi::FromInt(Script::TYPE_NORMAL));
418 script->set_compilation_type(Smi::FromInt(Script::COMPILATION_TYPE_HOST));
rossberg@chromium.org2c067b12012-03-19 11:01:52 +0000419 script->set_compilation_state(
420 Smi::FromInt(Script::COMPILATION_STATE_INITIAL));
ager@chromium.org9085a012009-05-11 19:22:57 +0000421 script->set_wrapper(*wrapper);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000422 script->set_line_ends(heap->undefined_value());
423 script->set_eval_from_shared(heap->undefined_value());
ager@chromium.orge2902be2009-06-08 12:21:35 +0000424 script->set_eval_from_instructions_offset(Smi::FromInt(0));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000425
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000426 return script;
427}
428
429
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000430Handle<Foreign> Factory::NewForeign(Address addr, PretenureFlag pretenure) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000431 CALL_HEAP_FUNCTION(isolate(),
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000432 isolate()->heap()->AllocateForeign(addr, pretenure),
433 Foreign);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000434}
435
436
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000437Handle<Foreign> Factory::NewForeign(const AccessorDescriptor* desc) {
438 return NewForeign((Address) desc, TENURED);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000439}
440
441
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000442Handle<ByteArray> Factory::NewByteArray(int length, PretenureFlag pretenure) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000443 ASSERT(0 <= length);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000444 CALL_HEAP_FUNCTION(
445 isolate(),
446 isolate()->heap()->AllocateByteArray(length, pretenure),
447 ByteArray);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000448}
449
450
ager@chromium.org3811b432009-10-28 14:53:37 +0000451Handle<ExternalArray> Factory::NewExternalArray(int length,
452 ExternalArrayType array_type,
453 void* external_pointer,
454 PretenureFlag pretenure) {
455 ASSERT(0 <= length);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000456 CALL_HEAP_FUNCTION(
457 isolate(),
458 isolate()->heap()->AllocateExternalArray(length,
459 array_type,
460 external_pointer,
461 pretenure),
462 ExternalArray);
ager@chromium.org3811b432009-10-28 14:53:37 +0000463}
464
465
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000466Handle<JSGlobalPropertyCell> Factory::NewJSGlobalPropertyCell(
467 Handle<Object> value) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000468 CALL_HEAP_FUNCTION(
469 isolate(),
470 isolate()->heap()->AllocateJSGlobalPropertyCell(*value),
471 JSGlobalPropertyCell);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000472}
473
474
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000475Handle<Map> Factory::NewMap(InstanceType type,
476 int instance_size,
477 ElementsKind elements_kind) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000478 CALL_HEAP_FUNCTION(
479 isolate(),
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000480 isolate()->heap()->AllocateMap(type, instance_size, elements_kind),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000481 Map);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000482}
483
484
485Handle<JSObject> Factory::NewFunctionPrototype(Handle<JSFunction> function) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000486 CALL_HEAP_FUNCTION(
487 isolate(),
488 isolate()->heap()->AllocateFunctionPrototype(*function),
489 JSObject);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000490}
491
492
rossberg@chromium.org657d53b2012-07-12 11:06:03 +0000493Handle<Map> Factory::CopyWithPreallocatedFieldDescriptors(Handle<Map> src) {
494 CALL_HEAP_FUNCTION(
495 isolate(), src->CopyWithPreallocatedFieldDescriptors(), Map);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000496}
497
498
ager@chromium.org32912102009-01-16 10:38:43 +0000499Handle<Map> Factory::CopyMap(Handle<Map> src,
500 int extra_inobject_properties) {
rossberg@chromium.org657d53b2012-07-12 11:06:03 +0000501 Handle<Map> copy = CopyWithPreallocatedFieldDescriptors(src);
ager@chromium.org32912102009-01-16 10:38:43 +0000502 // Check that we do not overflow the instance size when adding the
503 // extra inobject properties.
504 int instance_size_delta = extra_inobject_properties * kPointerSize;
505 int max_instance_size_delta =
506 JSObject::kMaxInstanceSize - copy->instance_size();
507 if (instance_size_delta > max_instance_size_delta) {
508 // If the instance size overflows, we allocate as many properties
509 // as we can as inobject properties.
510 instance_size_delta = max_instance_size_delta;
511 extra_inobject_properties = max_instance_size_delta >> kPointerSizeLog2;
512 }
513 // Adjust the map with the extra inobject properties.
514 int inobject_properties =
515 copy->inobject_properties() + extra_inobject_properties;
516 copy->set_inobject_properties(inobject_properties);
517 copy->set_unused_property_fields(inobject_properties);
518 copy->set_instance_size(copy->instance_size() + instance_size_delta);
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000519 copy->set_visitor_id(StaticVisitorBase::GetVisitorId(*copy));
ager@chromium.org32912102009-01-16 10:38:43 +0000520 return copy;
521}
522
fschneider@chromium.org40b9da32010-06-28 11:29:21 +0000523
verwaest@chromium.org753aee42012-07-17 16:15:42 +0000524Handle<Map> Factory::CopyMap(Handle<Map> src) {
verwaest@chromium.orgde64f722012-08-16 15:44:54 +0000525 CALL_HEAP_FUNCTION(isolate(), src->Copy(), Map);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000526}
527
528
kmillikin@chromium.org83e16822011-09-13 08:21:47 +0000529Handle<Map> Factory::GetElementsTransitionMap(
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000530 Handle<JSObject> src,
531 ElementsKind elements_kind) {
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000532 Isolate* i = isolate();
533 CALL_HEAP_FUNCTION(i,
534 src->GetElementsTransitionMap(i, elements_kind),
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000535 Map);
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +0000536}
537
538
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000539Handle<FixedArray> Factory::CopyFixedArray(Handle<FixedArray> array) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000540 CALL_HEAP_FUNCTION(isolate(), array->Copy(), FixedArray);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000541}
542
543
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +0000544Handle<FixedArray> Factory::CopySizeFixedArray(Handle<FixedArray> array,
545 int new_length) {
546 CALL_HEAP_FUNCTION(isolate(), array->CopySize(new_length), FixedArray);
547}
548
549
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000550Handle<FixedDoubleArray> Factory::CopyFixedDoubleArray(
551 Handle<FixedDoubleArray> array) {
552 CALL_HEAP_FUNCTION(isolate(), array->Copy(), FixedDoubleArray);
553}
554
555
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000556Handle<JSFunction> Factory::BaseNewFunctionFromSharedFunctionInfo(
557 Handle<SharedFunctionInfo> function_info,
sgjesse@chromium.org846fb742009-12-18 08:56:33 +0000558 Handle<Map> function_map,
559 PretenureFlag pretenure) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000560 CALL_HEAP_FUNCTION(
561 isolate(),
562 isolate()->heap()->AllocateFunction(*function_map,
563 *function_info,
564 isolate()->heap()->the_hole_value(),
565 pretenure),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000566 JSFunction);
567}
568
569
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000570Handle<JSFunction> Factory::NewFunctionFromSharedFunctionInfo(
571 Handle<SharedFunctionInfo> function_info,
sgjesse@chromium.org846fb742009-12-18 08:56:33 +0000572 Handle<Context> context,
573 PretenureFlag pretenure) {
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000574 Handle<JSFunction> result = BaseNewFunctionFromSharedFunctionInfo(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000575 function_info,
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000576 function_info->is_classic_mode()
577 ? isolate()->function_map()
578 : isolate()->strict_mode_function_map(),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000579 pretenure);
580
jkummerow@chromium.org1456e702012-03-30 08:38:13 +0000581 if (function_info->ic_age() != isolate()->heap()->global_ic_age()) {
582 function_info->ResetForNewContext(isolate()->heap()->global_ic_age());
583 }
584
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000585 result->set_context(*context);
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000586
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000587 int index = function_info->SearchOptimizedCodeMap(context->native_context());
ulan@chromium.orgd9e468a2012-06-25 09:47:40 +0000588 if (!function_info->bound() && index < 0) {
589 int number_of_literals = function_info->num_literals();
590 Handle<FixedArray> literals = NewFixedArray(number_of_literals, pretenure);
591 if (number_of_literals > 0) {
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000592 // Store the native context in the literals array prefix. This
ulan@chromium.orgd9e468a2012-06-25 09:47:40 +0000593 // context will be used when creating object, regexp and array
594 // literals in this function.
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000595 literals->set(JSFunction::kLiteralNativeContextIndex,
596 context->native_context());
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000597 }
ulan@chromium.orgd9e468a2012-06-25 09:47:40 +0000598 result->set_literals(*literals);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000599 }
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000600
601 if (index > 0) {
602 // Caching of optimized code enabled and optimized code found.
ulan@chromium.orgd9e468a2012-06-25 09:47:40 +0000603 function_info->InstallFromOptimizedCodeMap(*result, index);
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000604 return result;
605 }
606
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000607 if (V8::UseCrankshaft() &&
608 FLAG_always_opt &&
609 result->is_compiled() &&
610 !function_info->is_toplevel() &&
rossberg@chromium.org2c067b12012-03-19 11:01:52 +0000611 function_info->allows_lazy_compilation() &&
612 !function_info->optimization_disabled()) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000613 result->MarkForLazyRecompilation();
614 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000615 return result;
616}
617
618
619Handle<Object> Factory::NewNumber(double value,
620 PretenureFlag pretenure) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000621 CALL_HEAP_FUNCTION(
622 isolate(),
623 isolate()->heap()->NumberFromDouble(value, pretenure), Object);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000624}
625
626
erikcorry0ad885c2011-11-21 13:51:57 +0000627Handle<Object> Factory::NewNumberFromInt(int32_t value,
628 PretenureFlag pretenure) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000629 CALL_HEAP_FUNCTION(
630 isolate(),
erikcorry0ad885c2011-11-21 13:51:57 +0000631 isolate()->heap()->NumberFromInt32(value, pretenure), Object);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000632}
633
634
erikcorry0ad885c2011-11-21 13:51:57 +0000635Handle<Object> Factory::NewNumberFromUint(uint32_t value,
636 PretenureFlag pretenure) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000637 CALL_HEAP_FUNCTION(
638 isolate(),
erikcorry0ad885c2011-11-21 13:51:57 +0000639 isolate()->heap()->NumberFromUint32(value, pretenure), Object);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000640}
641
642
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000643Handle<JSObject> Factory::NewNeanderObject() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000644 CALL_HEAP_FUNCTION(
645 isolate(),
646 isolate()->heap()->AllocateJSObjectFromMap(
647 isolate()->heap()->neander_map()),
648 JSObject);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000649}
650
651
652Handle<Object> Factory::NewTypeError(const char* type,
653 Vector< Handle<Object> > args) {
654 return NewError("MakeTypeError", type, args);
655}
656
657
658Handle<Object> Factory::NewTypeError(Handle<String> message) {
659 return NewError("$TypeError", message);
660}
661
662
663Handle<Object> Factory::NewRangeError(const char* type,
664 Vector< Handle<Object> > args) {
665 return NewError("MakeRangeError", type, args);
666}
667
668
669Handle<Object> Factory::NewRangeError(Handle<String> message) {
670 return NewError("$RangeError", message);
671}
672
673
674Handle<Object> Factory::NewSyntaxError(const char* type, Handle<JSArray> args) {
675 return NewError("MakeSyntaxError", type, args);
676}
677
678
679Handle<Object> Factory::NewSyntaxError(Handle<String> message) {
680 return NewError("$SyntaxError", message);
681}
682
683
684Handle<Object> Factory::NewReferenceError(const char* type,
685 Vector< Handle<Object> > args) {
686 return NewError("MakeReferenceError", type, args);
687}
688
689
690Handle<Object> Factory::NewReferenceError(Handle<String> message) {
691 return NewError("$ReferenceError", message);
692}
693
694
695Handle<Object> Factory::NewError(const char* maker, const char* type,
696 Vector< Handle<Object> > args) {
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000697 v8::HandleScope scope; // Instantiate a closeable HandleScope for EscapeFrom.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000698 Handle<FixedArray> array = NewFixedArray(args.length());
ager@chromium.org7c537e22008-10-16 08:43:32 +0000699 for (int i = 0; i < args.length(); i++) {
700 array->set(i, *args[i]);
701 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000702 Handle<JSArray> object = NewJSArrayWithElements(array);
ager@chromium.org7c537e22008-10-16 08:43:32 +0000703 Handle<Object> result = NewError(maker, type, object);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000704 return result.EscapeFrom(&scope);
705}
706
707
708Handle<Object> Factory::NewEvalError(const char* type,
709 Vector< Handle<Object> > args) {
710 return NewError("MakeEvalError", type, args);
711}
712
713
714Handle<Object> Factory::NewError(const char* type,
715 Vector< Handle<Object> > args) {
716 return NewError("MakeError", type, args);
717}
718
719
verwaest@chromium.org37141392012-05-31 13:27:02 +0000720Handle<String> Factory::EmergencyNewError(const char* type,
721 Handle<JSArray> args) {
722 const int kBufferSize = 1000;
723 char buffer[kBufferSize];
724 size_t space = kBufferSize;
725 char* p = &buffer[0];
726
727 Vector<char> v(buffer, kBufferSize);
728 OS::StrNCpy(v, type, space);
729 space -= Min(space, strlen(type));
730 p = &buffer[kBufferSize] - space;
731
732 for (unsigned i = 0; i < ARRAY_SIZE(args); i++) {
733 if (space > 0) {
734 *p++ = ' ';
735 space--;
736 if (space > 0) {
737 MaybeObject* maybe_arg = args->GetElement(i);
738 Handle<String> arg_str(reinterpret_cast<String*>(maybe_arg));
739 const char* arg = *arg_str->ToCString();
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000740 Vector<char> v2(p, static_cast<int>(space));
verwaest@chromium.org37141392012-05-31 13:27:02 +0000741 OS::StrNCpy(v2, arg, space);
742 space -= Min(space, strlen(arg));
743 p = &buffer[kBufferSize] - space;
744 }
745 }
746 }
747 if (space > 0) {
748 *p = '\0';
749 } else {
750 buffer[kBufferSize - 1] = '\0';
751 }
752 Handle<String> error_string = NewStringFromUtf8(CStrVector(buffer), TENURED);
753 return error_string;
754}
755
756
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000757Handle<Object> Factory::NewError(const char* maker,
758 const char* type,
759 Handle<JSArray> args) {
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000760 Handle<String> make_str = InternalizeUtf8String(maker);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000761 Handle<Object> fun_obj(
ulan@chromium.org09d7ab52013-02-25 15:50:35 +0000762 isolate()->js_builtins_object()->GetPropertyNoExceptionThrown(*make_str),
763 isolate());
ager@chromium.org4af710e2009-09-15 12:20:11 +0000764 // If the builtins haven't been properly configured yet this error
765 // constructor may not have been defined. Bail out.
verwaest@chromium.org37141392012-05-31 13:27:02 +0000766 if (!fun_obj->IsJSFunction()) {
767 return EmergencyNewError(type, args);
768 }
ager@chromium.org4af710e2009-09-15 12:20:11 +0000769 Handle<JSFunction> fun = Handle<JSFunction>::cast(fun_obj);
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000770 Handle<Object> type_obj = InternalizeUtf8String(type);
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +0000771 Handle<Object> argv[] = { type_obj, args };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000772
773 // Invoke the JavaScript factory method. If an exception is thrown while
774 // running the factory method, use the exception as the result.
775 bool caught_exception;
776 Handle<Object> result = Execution::TryCall(fun,
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +0000777 isolate()->js_builtins_object(),
778 ARRAY_SIZE(argv),
779 argv,
780 &caught_exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000781 return result;
782}
783
784
785Handle<Object> Factory::NewError(Handle<String> message) {
786 return NewError("$Error", message);
787}
788
789
790Handle<Object> Factory::NewError(const char* constructor,
791 Handle<String> message) {
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000792 Handle<String> constr = InternalizeUtf8String(constructor);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000793 Handle<JSFunction> fun = Handle<JSFunction>(
794 JSFunction::cast(isolate()->js_builtins_object()->
795 GetPropertyNoExceptionThrown(*constr)));
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +0000796 Handle<Object> argv[] = { message };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000797
798 // Invoke the JavaScript factory method. If an exception is thrown while
799 // running the factory method, use the exception as the result.
800 bool caught_exception;
801 Handle<Object> result = Execution::TryCall(fun,
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +0000802 isolate()->js_builtins_object(),
803 ARRAY_SIZE(argv),
804 argv,
805 &caught_exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000806 return result;
807}
808
809
810Handle<JSFunction> Factory::NewFunction(Handle<String> name,
811 InstanceType type,
812 int instance_size,
813 Handle<Code> code,
814 bool force_initial_map) {
815 // Allocate the function
816 Handle<JSFunction> function = NewFunction(name, the_hole_value());
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000817
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000818 // Set up the code pointer in both the shared function info and in
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000819 // the function itself.
820 function->shared()->set_code(*code);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000821 function->set_code(*code);
822
823 if (force_initial_map ||
824 type != JS_OBJECT_TYPE ||
825 instance_size != JSObject::kHeaderSize) {
826 Handle<Map> initial_map = NewMap(type, instance_size);
827 Handle<JSObject> prototype = NewFunctionPrototype(function);
828 initial_map->set_prototype(*prototype);
829 function->set_initial_map(*initial_map);
830 initial_map->set_constructor(*function);
831 } else {
832 ASSERT(!function->has_initial_map());
833 ASSERT(!function->has_prototype());
834 }
835
836 return function;
837}
838
839
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000840Handle<JSFunction> Factory::NewFunctionWithPrototype(Handle<String> name,
841 InstanceType type,
842 int instance_size,
843 Handle<JSObject> prototype,
844 Handle<Code> code,
845 bool force_initial_map) {
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000846 // Allocate the function.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000847 Handle<JSFunction> function = NewFunction(name, prototype);
848
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000849 // Set up the code pointer in both the shared function info and in
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000850 // the function itself.
851 function->shared()->set_code(*code);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000852 function->set_code(*code);
853
854 if (force_initial_map ||
855 type != JS_OBJECT_TYPE ||
856 instance_size != JSObject::kHeaderSize) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000857 Handle<Map> initial_map = NewMap(type,
858 instance_size,
svenpanne@chromium.org830d30c2012-05-29 13:20:14 +0000859 GetInitialFastElementsKind());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000860 function->set_initial_map(*initial_map);
861 initial_map->set_constructor(*function);
862 }
863
864 // Set function.prototype and give the prototype a constructor
865 // property that refers to the function.
866 SetPrototypeProperty(function, prototype);
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +0000867 // Currently safe because it is only invoked from Genesis.
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000868 CHECK_NOT_EMPTY_HANDLE(isolate(),
869 JSObject::SetLocalPropertyIgnoreAttributes(
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000870 prototype, constructor_string(),
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000871 function, DONT_ENUM));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000872 return function;
873}
874
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000875
kmillikin@chromium.org4111b802010-05-03 10:34:42 +0000876Handle<JSFunction> Factory::NewFunctionWithoutPrototype(Handle<String> name,
877 Handle<Code> code) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000878 Handle<JSFunction> function = NewFunctionWithoutPrototype(name,
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000879 CLASSIC_MODE);
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000880 function->shared()->set_code(*code);
kmillikin@chromium.org4111b802010-05-03 10:34:42 +0000881 function->set_code(*code);
882 ASSERT(!function->has_initial_map());
883 ASSERT(!function->has_prototype());
884 return function;
885}
886
887
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000888Handle<ScopeInfo> Factory::NewScopeInfo(int length) {
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000889 CALL_HEAP_FUNCTION(
890 isolate(),
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000891 isolate()->heap()->AllocateScopeInfo(length),
892 ScopeInfo);
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000893}
894
895
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +0000896Handle<JSObject> Factory::NewExternal(void* value) {
897 CALL_HEAP_FUNCTION(isolate(),
898 isolate()->heap()->AllocateExternal(value),
899 JSObject);
900}
901
902
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000903Handle<Code> Factory::NewCode(const CodeDesc& desc,
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000904 Code::Flags flags,
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +0000905 Handle<Object> self_ref,
906 bool immovable) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000907 CALL_HEAP_FUNCTION(isolate(),
908 isolate()->heap()->CreateCode(
909 desc, flags, self_ref, immovable),
910 Code);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000911}
912
913
914Handle<Code> Factory::CopyCode(Handle<Code> code) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000915 CALL_HEAP_FUNCTION(isolate(),
916 isolate()->heap()->CopyCode(*code),
917 Code);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000918}
919
920
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000921Handle<Code> Factory::CopyCode(Handle<Code> code, Vector<byte> reloc_info) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000922 CALL_HEAP_FUNCTION(isolate(),
923 isolate()->heap()->CopyCode(*code, reloc_info),
924 Code);
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000925}
926
927
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000928Handle<String> Factory::InternalizedStringFromString(Handle<String> value) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000929 CALL_HEAP_FUNCTION(isolate(),
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000930 isolate()->heap()->InternalizeString(*value), String);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000931}
932
933
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000934Handle<JSObject> Factory::NewJSObject(Handle<JSFunction> constructor,
935 PretenureFlag pretenure) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000936 CALL_HEAP_FUNCTION(
937 isolate(),
938 isolate()->heap()->AllocateJSObject(*constructor, pretenure), JSObject);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000939}
940
941
danno@chromium.org81cac2b2012-07-10 11:28:27 +0000942Handle<JSModule> Factory::NewJSModule(Handle<Context> context,
943 Handle<ScopeInfo> scope_info) {
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000944 CALL_HEAP_FUNCTION(
945 isolate(),
danno@chromium.org81cac2b2012-07-10 11:28:27 +0000946 isolate()->heap()->AllocateJSModule(*context, *scope_info), JSModule);
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000947}
948
949
kasperl@chromium.org2abc4502009-07-02 07:00:29 +0000950Handle<GlobalObject> Factory::NewGlobalObject(
951 Handle<JSFunction> constructor) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000952 CALL_HEAP_FUNCTION(isolate(),
953 isolate()->heap()->AllocateGlobalObject(*constructor),
kasperl@chromium.org2abc4502009-07-02 07:00:29 +0000954 GlobalObject);
955}
956
957
958
mstarzinger@chromium.org71fc3462013-02-27 09:34:27 +0000959Handle<JSObject> Factory::NewJSObjectFromMap(Handle<Map> map,
960 PretenureFlag pretenure) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000961 CALL_HEAP_FUNCTION(
962 isolate(),
mstarzinger@chromium.org71fc3462013-02-27 09:34:27 +0000963 isolate()->heap()->AllocateJSObjectFromMap(*map, pretenure),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000964 JSObject);
ager@chromium.org236ad962008-09-25 09:45:57 +0000965}
966
967
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +0000968Handle<JSArray> Factory::NewJSArray(int capacity,
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000969 ElementsKind elements_kind,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000970 PretenureFlag pretenure) {
ulan@chromium.org8e8d8822012-11-23 14:36:46 +0000971 if (capacity != 0) {
972 elements_kind = GetHoleyElementsKind(elements_kind);
973 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000974 CALL_HEAP_FUNCTION(isolate(),
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000975 isolate()->heap()->AllocateJSArrayAndStorage(
976 elements_kind,
977 0,
978 capacity,
979 INITIALIZE_ARRAY_ELEMENTS_WITH_HOLE,
980 pretenure),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000981 JSArray);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000982}
983
984
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +0000985Handle<JSArray> Factory::NewJSArrayWithElements(Handle<FixedArrayBase> elements,
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000986 ElementsKind elements_kind,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000987 PretenureFlag pretenure) {
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000988 CALL_HEAP_FUNCTION(
989 isolate(),
990 isolate()->heap()->AllocateJSArrayWithElements(*elements,
991 elements_kind,
yangguo@chromium.orgfb377212012-11-16 14:43:43 +0000992 elements->length(),
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000993 pretenure),
994 JSArray);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000995}
996
997
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +0000998void Factory::SetElementsCapacityAndLength(Handle<JSArray> array,
999 int capacity,
1000 int length) {
1001 ElementsAccessor* accessor = array->GetElementsAccessor();
1002 CALL_HEAP_FUNCTION_VOID(
1003 isolate(),
1004 accessor->SetCapacityAndLength(*array, capacity, length));
1005}
1006
1007
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001008void Factory::SetContent(Handle<JSArray> array,
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00001009 Handle<FixedArrayBase> elements) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001010 CALL_HEAP_FUNCTION_VOID(
1011 isolate(),
1012 array->SetContent(*elements));
1013}
1014
1015
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00001016void Factory::EnsureCanContainHeapObjectElements(Handle<JSArray> array) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001017 CALL_HEAP_FUNCTION_VOID(
1018 isolate(),
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00001019 array->EnsureCanContainHeapObjectElements());
1020}
1021
1022
1023void Factory::EnsureCanContainElements(Handle<JSArray> array,
1024 Handle<FixedArrayBase> elements,
svenpanne@chromium.org830d30c2012-05-29 13:20:14 +00001025 uint32_t length,
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00001026 EnsureElementsMode mode) {
1027 CALL_HEAP_FUNCTION_VOID(
1028 isolate(),
svenpanne@chromium.org830d30c2012-05-29 13:20:14 +00001029 array->EnsureCanContainElements(*elements, length, mode));
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001030}
1031
1032
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001033Handle<JSProxy> Factory::NewJSProxy(Handle<Object> handler,
1034 Handle<Object> prototype) {
1035 CALL_HEAP_FUNCTION(
1036 isolate(),
1037 isolate()->heap()->AllocateJSProxy(*handler, *prototype),
1038 JSProxy);
1039}
1040
1041
lrn@chromium.org34e60782011-09-15 07:25:40 +00001042void Factory::BecomeJSObject(Handle<JSReceiver> object) {
rossberg@chromium.org717967f2011-07-20 13:44:42 +00001043 CALL_HEAP_FUNCTION_VOID(
1044 isolate(),
lrn@chromium.org34e60782011-09-15 07:25:40 +00001045 isolate()->heap()->ReinitializeJSReceiver(
1046 *object, JS_OBJECT_TYPE, JSObject::kHeaderSize));
1047}
1048
1049
1050void Factory::BecomeJSFunction(Handle<JSReceiver> object) {
1051 CALL_HEAP_FUNCTION_VOID(
1052 isolate(),
1053 isolate()->heap()->ReinitializeJSReceiver(
1054 *object, JS_FUNCTION_TYPE, JSFunction::kSize));
rossberg@chromium.org717967f2011-07-20 13:44:42 +00001055}
1056
1057
jkummerow@chromium.org000f7fb2012-08-01 11:14:42 +00001058void Factory::SetIdentityHash(Handle<JSObject> object, Smi* hash) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001059 CALL_HEAP_FUNCTION_VOID(
1060 isolate(),
1061 object->SetIdentityHash(hash, ALLOW_CREATION));
1062}
1063
1064
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001065Handle<SharedFunctionInfo> Factory::NewSharedFunctionInfo(
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00001066 Handle<String> name,
1067 int number_of_literals,
1068 Handle<Code> code,
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001069 Handle<ScopeInfo> scope_info) {
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001070 Handle<SharedFunctionInfo> shared = NewSharedFunctionInfo(name);
1071 shared->set_code(*code);
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00001072 shared->set_scope_info(*scope_info);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001073 int literals_array_size = number_of_literals;
1074 // If the function contains object, regexp or array literals,
1075 // allocate extra space for a literals array prefix containing the
1076 // context.
1077 if (number_of_literals > 0) {
1078 literals_array_size += JSFunction::kLiteralsPrefixSize;
1079 }
1080 shared->set_num_literals(literals_array_size);
1081 return shared;
1082}
1083
1084
kmillikin@chromium.org31b12772011-02-02 16:08:26 +00001085Handle<JSMessageObject> Factory::NewJSMessageObject(
1086 Handle<String> type,
1087 Handle<JSArray> arguments,
1088 int start_position,
1089 int end_position,
1090 Handle<Object> script,
1091 Handle<Object> stack_trace,
1092 Handle<Object> stack_frames) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001093 CALL_HEAP_FUNCTION(isolate(),
1094 isolate()->heap()->AllocateJSMessageObject(*type,
1095 *arguments,
1096 start_position,
1097 end_position,
1098 *script,
1099 *stack_trace,
1100 *stack_frames),
kmillikin@chromium.org31b12772011-02-02 16:08:26 +00001101 JSMessageObject);
1102}
1103
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001104Handle<SharedFunctionInfo> Factory::NewSharedFunctionInfo(Handle<String> name) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001105 CALL_HEAP_FUNCTION(isolate(),
1106 isolate()->heap()->AllocateSharedFunctionInfo(*name),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001107 SharedFunctionInfo);
1108}
1109
1110
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00001111Handle<String> Factory::NumberToString(Handle<Object> number) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001112 CALL_HEAP_FUNCTION(isolate(),
1113 isolate()->heap()->NumberToString(*number), String);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00001114}
1115
1116
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001117Handle<String> Factory::Uint32ToString(uint32_t value) {
1118 CALL_HEAP_FUNCTION(isolate(),
1119 isolate()->heap()->Uint32ToString(value), String);
1120}
1121
1122
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001123Handle<SeededNumberDictionary> Factory::DictionaryAtNumberPut(
1124 Handle<SeededNumberDictionary> dictionary,
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00001125 uint32_t key,
1126 Handle<Object> value) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001127 CALL_HEAP_FUNCTION(isolate(),
1128 dictionary->AtNumberPut(key, *value),
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001129 SeededNumberDictionary);
1130}
1131
1132
1133Handle<UnseededNumberDictionary> Factory::DictionaryAtNumberPut(
1134 Handle<UnseededNumberDictionary> dictionary,
1135 uint32_t key,
1136 Handle<Object> value) {
1137 CALL_HEAP_FUNCTION(isolate(),
1138 dictionary->AtNumberPut(key, *value),
1139 UnseededNumberDictionary);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001140}
1141
1142
1143Handle<JSFunction> Factory::NewFunctionHelper(Handle<String> name,
1144 Handle<Object> prototype) {
1145 Handle<SharedFunctionInfo> function_share = NewSharedFunctionInfo(name);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001146 CALL_HEAP_FUNCTION(
1147 isolate(),
1148 isolate()->heap()->AllocateFunction(*isolate()->function_map(),
1149 *function_share,
1150 *prototype),
1151 JSFunction);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001152}
1153
1154
1155Handle<JSFunction> Factory::NewFunction(Handle<String> name,
1156 Handle<Object> prototype) {
1157 Handle<JSFunction> fun = NewFunctionHelper(name, prototype);
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001158 fun->set_context(isolate()->context()->native_context());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001159 return fun;
1160}
1161
1162
kmillikin@chromium.org4111b802010-05-03 10:34:42 +00001163Handle<JSFunction> Factory::NewFunctionWithoutPrototypeHelper(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001164 Handle<String> name,
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00001165 LanguageMode language_mode) {
kmillikin@chromium.org4111b802010-05-03 10:34:42 +00001166 Handle<SharedFunctionInfo> function_share = NewSharedFunctionInfo(name);
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00001167 Handle<Map> map = (language_mode == CLASSIC_MODE)
1168 ? isolate()->function_without_prototype_map()
1169 : isolate()->strict_mode_function_without_prototype_map();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001170 CALL_HEAP_FUNCTION(isolate(),
1171 isolate()->heap()->AllocateFunction(
1172 *map,
kmillikin@chromium.org4111b802010-05-03 10:34:42 +00001173 *function_share,
1174 *the_hole_value()),
1175 JSFunction);
1176}
1177
1178
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001179Handle<JSFunction> Factory::NewFunctionWithoutPrototype(
1180 Handle<String> name,
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00001181 LanguageMode language_mode) {
1182 Handle<JSFunction> fun =
1183 NewFunctionWithoutPrototypeHelper(name, language_mode);
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001184 fun->set_context(isolate()->context()->native_context());
kmillikin@chromium.org4111b802010-05-03 10:34:42 +00001185 return fun;
1186}
1187
1188
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001189Handle<Object> Factory::ToObject(Handle<Object> object) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001190 CALL_HEAP_FUNCTION(isolate(), object->ToObject(), Object);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001191}
1192
1193
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001194Handle<Object> Factory::ToObject(Handle<Object> object,
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001195 Handle<Context> native_context) {
1196 CALL_HEAP_FUNCTION(isolate(), object->ToObject(*native_context), Object);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001197}
1198
1199
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001200#ifdef ENABLE_DEBUGGER_SUPPORT
v8.team.kasperl727e9952008-09-02 14:56:44 +00001201Handle<DebugInfo> Factory::NewDebugInfo(Handle<SharedFunctionInfo> shared) {
1202 // Get the original code of the function.
1203 Handle<Code> code(shared->code());
1204
1205 // Create a copy of the code before allocating the debug info object to avoid
1206 // allocation while setting up the debug info object.
1207 Handle<Code> original_code(*Factory::CopyCode(code));
1208
1209 // Allocate initial fixed array for active break points before allocating the
1210 // debug info object to avoid allocation while setting up the debug info
1211 // object.
1212 Handle<FixedArray> break_points(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001213 NewFixedArray(Debug::kEstimatedNofBreakPointsInFunction));
v8.team.kasperl727e9952008-09-02 14:56:44 +00001214
1215 // Create and set up the debug info object. Debug info contains function, a
1216 // copy of the original code, the executing code and initial fixed array for
1217 // active break points.
1218 Handle<DebugInfo> debug_info =
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001219 Handle<DebugInfo>::cast(NewStruct(DEBUG_INFO_TYPE));
v8.team.kasperl727e9952008-09-02 14:56:44 +00001220 debug_info->set_shared(*shared);
1221 debug_info->set_original_code(*original_code);
1222 debug_info->set_code(*code);
1223 debug_info->set_break_points(*break_points);
1224
1225 // Link debug info to function.
1226 shared->set_debug_info(*debug_info);
1227
1228 return debug_info;
1229}
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001230#endif
v8.team.kasperl727e9952008-09-02 14:56:44 +00001231
1232
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001233Handle<JSObject> Factory::NewArgumentsObject(Handle<Object> callee,
1234 int length) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001235 CALL_HEAP_FUNCTION(
1236 isolate(),
1237 isolate()->heap()->AllocateArgumentsObject(*callee, length), JSObject);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001238}
1239
1240
1241Handle<JSFunction> Factory::CreateApiFunction(
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001242 Handle<FunctionTemplateInfo> obj, ApiInstanceType instance_type) {
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00001243 Handle<Code> code = isolate()->builtins()->HandleApiCall();
1244 Handle<Code> construct_stub = isolate()->builtins()->JSConstructStubApi();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001245
kasper.lund212ac232008-07-16 07:07:30 +00001246 int internal_field_count = 0;
1247 if (!obj->instance_template()->IsUndefined()) {
1248 Handle<ObjectTemplateInfo> instance_template =
1249 Handle<ObjectTemplateInfo>(
1250 ObjectTemplateInfo::cast(obj->instance_template()));
1251 internal_field_count =
1252 Smi::cast(instance_template->internal_field_count())->value();
1253 }
1254
1255 int instance_size = kPointerSize * internal_field_count;
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001256 InstanceType type = INVALID_TYPE;
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001257 switch (instance_type) {
1258 case JavaScriptObject:
1259 type = JS_OBJECT_TYPE;
1260 instance_size += JSObject::kHeaderSize;
1261 break;
1262 case InnerGlobalObject:
1263 type = JS_GLOBAL_OBJECT_TYPE;
1264 instance_size += JSGlobalObject::kSize;
1265 break;
1266 case OuterGlobalObject:
1267 type = JS_GLOBAL_PROXY_TYPE;
1268 instance_size += JSGlobalProxy::kSize;
1269 break;
1270 default:
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001271 break;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001272 }
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001273 ASSERT(type != INVALID_TYPE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001274
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001275 Handle<JSFunction> result =
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001276 NewFunction(Factory::empty_string(),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001277 type,
1278 instance_size,
1279 code,
1280 true);
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00001281
1282 // Set length.
1283 result->shared()->set_length(obj->length());
1284
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001285 // Set class name.
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00001286 Handle<Object> class_name = Handle<Object>(obj->class_name(), isolate());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001287 if (class_name->IsString()) {
1288 result->shared()->set_instance_class_name(*class_name);
1289 result->shared()->set_name(*class_name);
1290 }
1291
1292 Handle<Map> map = Handle<Map>(result->initial_map());
1293
1294 // Mark as undetectable if needed.
1295 if (obj->undetectable()) {
1296 map->set_is_undetectable();
1297 }
1298
1299 // Mark as hidden for the __proto__ accessor if needed.
1300 if (obj->hidden_prototype()) {
1301 map->set_is_hidden_prototype();
1302 }
1303
1304 // Mark as needs_access_check if needed.
1305 if (obj->needs_access_check()) {
ager@chromium.org870a0b62008-11-04 11:43:05 +00001306 map->set_is_access_check_needed(true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001307 }
1308
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001309 // Set interceptor information in the map.
1310 if (!obj->named_property_handler()->IsUndefined()) {
1311 map->set_has_named_interceptor();
1312 }
1313 if (!obj->indexed_property_handler()->IsUndefined()) {
1314 map->set_has_indexed_interceptor();
1315 }
1316
1317 // Set instance call-as-function information in the map.
1318 if (!obj->instance_call_handler()->IsUndefined()) {
1319 map->set_has_instance_call_handler();
1320 }
1321
1322 result->shared()->set_function_data(*obj);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001323 result->shared()->set_construct_stub(*construct_stub);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001324 result->shared()->DontAdaptArguments();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001325
1326 // Recursively copy parent templates' accessors, 'data' may be modified.
verwaest@chromium.org33e09c82012-10-10 17:07:22 +00001327 int max_number_of_additional_properties = 0;
1328 FunctionTemplateInfo* info = *obj;
1329 while (true) {
1330 Object* props = info->property_accessors();
1331 if (!props->IsUndefined()) {
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00001332 Handle<Object> props_handle(props, isolate());
verwaest@chromium.org33e09c82012-10-10 17:07:22 +00001333 NeanderArray props_array(props_handle);
1334 max_number_of_additional_properties += props_array.length();
1335 }
1336 Object* parent = info->parent_template();
1337 if (parent->IsUndefined()) break;
1338 info = FunctionTemplateInfo::cast(parent);
1339 }
1340
1341 Map::EnsureDescriptorSlack(map, max_number_of_additional_properties);
1342
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001343 while (true) {
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00001344 Handle<Object> props = Handle<Object>(obj->property_accessors(),
1345 isolate());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001346 if (!props->IsUndefined()) {
verwaest@chromium.org33e09c82012-10-10 17:07:22 +00001347 Map::AppendCallbackDescriptors(map, props);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001348 }
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00001349 Handle<Object> parent = Handle<Object>(obj->parent_template(), isolate());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001350 if (parent->IsUndefined()) break;
1351 obj = Handle<FunctionTemplateInfo>::cast(parent);
1352 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001353
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001354 ASSERT(result->shared()->IsApiFunction());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001355 return result;
1356}
1357
1358
ager@chromium.org236ad962008-09-25 09:45:57 +00001359Handle<MapCache> Factory::NewMapCache(int at_least_space_for) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001360 CALL_HEAP_FUNCTION(isolate(),
1361 MapCache::Allocate(at_least_space_for), MapCache);
ager@chromium.org236ad962008-09-25 09:45:57 +00001362}
1363
1364
lrn@chromium.org303ada72010-10-27 09:33:13 +00001365MUST_USE_RESULT static MaybeObject* UpdateMapCacheWith(Context* context,
1366 FixedArray* keys,
1367 Map* map) {
1368 Object* result;
1369 { MaybeObject* maybe_result =
1370 MapCache::cast(context->map_cache())->Put(keys, map);
1371 if (!maybe_result->ToObject(&result)) return maybe_result;
1372 }
1373 context->set_map_cache(MapCache::cast(result));
ager@chromium.org236ad962008-09-25 09:45:57 +00001374 return result;
1375}
1376
1377
1378Handle<MapCache> Factory::AddToMapCache(Handle<Context> context,
1379 Handle<FixedArray> keys,
1380 Handle<Map> map) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001381 CALL_HEAP_FUNCTION(isolate(),
1382 UpdateMapCacheWith(*context, *keys, *map), MapCache);
ager@chromium.org236ad962008-09-25 09:45:57 +00001383}
1384
1385
1386Handle<Map> Factory::ObjectLiteralMapFromCache(Handle<Context> context,
1387 Handle<FixedArray> keys) {
1388 if (context->map_cache()->IsUndefined()) {
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001389 // Allocate the new map cache for the native context.
ager@chromium.org236ad962008-09-25 09:45:57 +00001390 Handle<MapCache> new_cache = NewMapCache(24);
1391 context->set_map_cache(*new_cache);
1392 }
ager@chromium.org32912102009-01-16 10:38:43 +00001393 // Check to see whether there is a matching element in the cache.
ager@chromium.org236ad962008-09-25 09:45:57 +00001394 Handle<MapCache> cache =
1395 Handle<MapCache>(MapCache::cast(context->map_cache()));
yangguo@chromium.orgfb377212012-11-16 14:43:43 +00001396 Handle<Object> result = Handle<Object>(cache->Lookup(*keys), isolate());
ager@chromium.org236ad962008-09-25 09:45:57 +00001397 if (result->IsMap()) return Handle<Map>::cast(result);
1398 // Create a new map and add it to the cache.
1399 Handle<Map> map =
ager@chromium.org32912102009-01-16 10:38:43 +00001400 CopyMap(Handle<Map>(context->object_function()->initial_map()),
1401 keys->length());
ager@chromium.org236ad962008-09-25 09:45:57 +00001402 AddToMapCache(context, keys, map);
1403 return Handle<Map>(map);
1404}
1405
1406
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001407void Factory::SetRegExpAtomData(Handle<JSRegExp> regexp,
1408 JSRegExp::Type type,
1409 Handle<String> source,
1410 JSRegExp::Flags flags,
1411 Handle<Object> data) {
1412 Handle<FixedArray> store = NewFixedArray(JSRegExp::kAtomDataSize);
1413
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001414 store->set(JSRegExp::kTagIndex, Smi::FromInt(type));
1415 store->set(JSRegExp::kSourceIndex, *source);
1416 store->set(JSRegExp::kFlagsIndex, Smi::FromInt(flags.value()));
1417 store->set(JSRegExp::kAtomPatternIndex, *data);
1418 regexp->set_data(*store);
1419}
1420
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001421void Factory::SetRegExpIrregexpData(Handle<JSRegExp> regexp,
1422 JSRegExp::Type type,
1423 Handle<String> source,
1424 JSRegExp::Flags flags,
1425 int capture_count) {
1426 Handle<FixedArray> store = NewFixedArray(JSRegExp::kIrregexpDataSize);
jkummerow@chromium.orgddda9e82011-07-06 11:27:02 +00001427 Smi* uninitialized = Smi::FromInt(JSRegExp::kUninitializedValue);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001428 store->set(JSRegExp::kTagIndex, Smi::FromInt(type));
1429 store->set(JSRegExp::kSourceIndex, *source);
1430 store->set(JSRegExp::kFlagsIndex, Smi::FromInt(flags.value()));
jkummerow@chromium.orgddda9e82011-07-06 11:27:02 +00001431 store->set(JSRegExp::kIrregexpASCIICodeIndex, uninitialized);
1432 store->set(JSRegExp::kIrregexpUC16CodeIndex, uninitialized);
1433 store->set(JSRegExp::kIrregexpASCIICodeSavedIndex, uninitialized);
1434 store->set(JSRegExp::kIrregexpUC16CodeSavedIndex, uninitialized);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001435 store->set(JSRegExp::kIrregexpMaxRegisterCountIndex, Smi::FromInt(0));
1436 store->set(JSRegExp::kIrregexpCaptureCountIndex,
1437 Smi::FromInt(capture_count));
1438 regexp->set_data(*store);
1439}
1440
1441
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001442
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001443void Factory::ConfigureInstance(Handle<FunctionTemplateInfo> desc,
1444 Handle<JSObject> instance,
1445 bool* pending_exception) {
1446 // Configure the instance by adding the properties specified by the
1447 // instance template.
yangguo@chromium.orgfb377212012-11-16 14:43:43 +00001448 Handle<Object> instance_template(desc->instance_template(), isolate());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001449 if (!instance_template->IsUndefined()) {
1450 Execution::ConfigureInstance(instance,
1451 instance_template,
1452 pending_exception);
1453 } else {
1454 *pending_exception = false;
1455 }
1456}
1457
1458
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001459Handle<Object> Factory::GlobalConstantFor(Handle<String> name) {
1460 Heap* h = isolate()->heap();
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001461 if (name->Equals(h->undefined_string())) return undefined_value();
1462 if (name->Equals(h->nan_string())) return nan_value();
1463 if (name->Equals(h->infinity_string())) return infinity_value();
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001464 return Handle<Object>::null();
1465}
1466
1467
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00001468Handle<Object> Factory::ToBoolean(bool value) {
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00001469 return value ? true_value() : false_value();
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00001470}
1471
1472
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001473} } // namespace v8::internal