blob: 943902e153415da198c6d7b9088cd8729b302d33 [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
ulan@chromium.org750145a2013-03-07 15:14:13 +000073Handle<NameDictionary> Factory::NewNameDictionary(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(),
ulan@chromium.org750145a2013-03-07 15:14:13 +000076 NameDictionary::Allocate(at_least_space_for),
77 NameDictionary);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +000078}
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
ulan@chromium.org750145a2013-03-07 15:14:13 +0000372Handle<DeclaredAccessorDescriptor> Factory::NewDeclaredAccessorDescriptor() {
373 return Handle<DeclaredAccessorDescriptor>::cast(
374 NewStruct(DECLARED_ACCESSOR_DESCRIPTOR_TYPE));
375}
376
377
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +0000378Handle<DeclaredAccessorInfo> Factory::NewDeclaredAccessorInfo() {
379 Handle<DeclaredAccessorInfo> info =
380 Handle<DeclaredAccessorInfo>::cast(
381 NewStruct(DECLARED_ACCESSOR_INFO_TYPE));
382 info->set_flag(0); // Must clear the flag, it was initialized as undefined.
383 return info;
384}
385
386
387Handle<ExecutableAccessorInfo> Factory::NewExecutableAccessorInfo() {
388 Handle<ExecutableAccessorInfo> info =
389 Handle<ExecutableAccessorInfo>::cast(
390 NewStruct(EXECUTABLE_ACCESSOR_INFO_TYPE));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000391 info->set_flag(0); // Must clear the flag, it was initialized as undefined.
392 return info;
393}
394
395
396Handle<Script> Factory::NewScript(Handle<String> source) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000397 // Generate id for this script.
398 int id;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000399 Heap* heap = isolate()->heap();
400 if (heap->last_script_id()->IsUndefined()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000401 // Script ids start from one.
402 id = 1;
403 } else {
404 // Increment id, wrap when positive smi is exhausted.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000405 id = Smi::cast(heap->last_script_id())->value();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000406 id++;
407 if (!Smi::IsValid(id)) {
408 id = 0;
409 }
410 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000411 heap->SetLastScriptId(Smi::FromInt(id));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000412
413 // Create and initialize script object.
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000414 Handle<Foreign> wrapper = NewForeign(0, TENURED);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000415 Handle<Script> script = Handle<Script>::cast(NewStruct(SCRIPT_TYPE));
416 script->set_source(*source);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000417 script->set_name(heap->undefined_value());
418 script->set_id(heap->last_script_id());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000419 script->set_line_offset(Smi::FromInt(0));
420 script->set_column_offset(Smi::FromInt(0));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000421 script->set_data(heap->undefined_value());
422 script->set_context_data(heap->undefined_value());
ager@chromium.orge2902be2009-06-08 12:21:35 +0000423 script->set_type(Smi::FromInt(Script::TYPE_NORMAL));
424 script->set_compilation_type(Smi::FromInt(Script::COMPILATION_TYPE_HOST));
rossberg@chromium.org2c067b12012-03-19 11:01:52 +0000425 script->set_compilation_state(
426 Smi::FromInt(Script::COMPILATION_STATE_INITIAL));
ager@chromium.org9085a012009-05-11 19:22:57 +0000427 script->set_wrapper(*wrapper);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000428 script->set_line_ends(heap->undefined_value());
429 script->set_eval_from_shared(heap->undefined_value());
ager@chromium.orge2902be2009-06-08 12:21:35 +0000430 script->set_eval_from_instructions_offset(Smi::FromInt(0));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000431
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000432 return script;
433}
434
435
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000436Handle<Foreign> Factory::NewForeign(Address addr, PretenureFlag pretenure) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000437 CALL_HEAP_FUNCTION(isolate(),
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000438 isolate()->heap()->AllocateForeign(addr, pretenure),
439 Foreign);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000440}
441
442
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000443Handle<Foreign> Factory::NewForeign(const AccessorDescriptor* desc) {
444 return NewForeign((Address) desc, TENURED);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000445}
446
447
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000448Handle<ByteArray> Factory::NewByteArray(int length, PretenureFlag pretenure) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000449 ASSERT(0 <= length);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000450 CALL_HEAP_FUNCTION(
451 isolate(),
452 isolate()->heap()->AllocateByteArray(length, pretenure),
453 ByteArray);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000454}
455
456
ager@chromium.org3811b432009-10-28 14:53:37 +0000457Handle<ExternalArray> Factory::NewExternalArray(int length,
458 ExternalArrayType array_type,
459 void* external_pointer,
460 PretenureFlag pretenure) {
461 ASSERT(0 <= length);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000462 CALL_HEAP_FUNCTION(
463 isolate(),
464 isolate()->heap()->AllocateExternalArray(length,
465 array_type,
466 external_pointer,
467 pretenure),
468 ExternalArray);
ager@chromium.org3811b432009-10-28 14:53:37 +0000469}
470
471
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000472Handle<JSGlobalPropertyCell> Factory::NewJSGlobalPropertyCell(
473 Handle<Object> value) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000474 CALL_HEAP_FUNCTION(
475 isolate(),
476 isolate()->heap()->AllocateJSGlobalPropertyCell(*value),
477 JSGlobalPropertyCell);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000478}
479
480
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000481Handle<Map> Factory::NewMap(InstanceType type,
482 int instance_size,
483 ElementsKind elements_kind) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000484 CALL_HEAP_FUNCTION(
485 isolate(),
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000486 isolate()->heap()->AllocateMap(type, instance_size, elements_kind),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000487 Map);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000488}
489
490
491Handle<JSObject> Factory::NewFunctionPrototype(Handle<JSFunction> function) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000492 CALL_HEAP_FUNCTION(
493 isolate(),
494 isolate()->heap()->AllocateFunctionPrototype(*function),
495 JSObject);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000496}
497
498
rossberg@chromium.org657d53b2012-07-12 11:06:03 +0000499Handle<Map> Factory::CopyWithPreallocatedFieldDescriptors(Handle<Map> src) {
500 CALL_HEAP_FUNCTION(
501 isolate(), src->CopyWithPreallocatedFieldDescriptors(), Map);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000502}
503
504
ager@chromium.org32912102009-01-16 10:38:43 +0000505Handle<Map> Factory::CopyMap(Handle<Map> src,
506 int extra_inobject_properties) {
rossberg@chromium.org657d53b2012-07-12 11:06:03 +0000507 Handle<Map> copy = CopyWithPreallocatedFieldDescriptors(src);
ager@chromium.org32912102009-01-16 10:38:43 +0000508 // Check that we do not overflow the instance size when adding the
509 // extra inobject properties.
510 int instance_size_delta = extra_inobject_properties * kPointerSize;
511 int max_instance_size_delta =
512 JSObject::kMaxInstanceSize - copy->instance_size();
513 if (instance_size_delta > max_instance_size_delta) {
514 // If the instance size overflows, we allocate as many properties
515 // as we can as inobject properties.
516 instance_size_delta = max_instance_size_delta;
517 extra_inobject_properties = max_instance_size_delta >> kPointerSizeLog2;
518 }
519 // Adjust the map with the extra inobject properties.
520 int inobject_properties =
521 copy->inobject_properties() + extra_inobject_properties;
522 copy->set_inobject_properties(inobject_properties);
523 copy->set_unused_property_fields(inobject_properties);
524 copy->set_instance_size(copy->instance_size() + instance_size_delta);
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000525 copy->set_visitor_id(StaticVisitorBase::GetVisitorId(*copy));
ager@chromium.org32912102009-01-16 10:38:43 +0000526 return copy;
527}
528
fschneider@chromium.org40b9da32010-06-28 11:29:21 +0000529
verwaest@chromium.org753aee42012-07-17 16:15:42 +0000530Handle<Map> Factory::CopyMap(Handle<Map> src) {
verwaest@chromium.orgde64f722012-08-16 15:44:54 +0000531 CALL_HEAP_FUNCTION(isolate(), src->Copy(), Map);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000532}
533
534
kmillikin@chromium.org83e16822011-09-13 08:21:47 +0000535Handle<Map> Factory::GetElementsTransitionMap(
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000536 Handle<JSObject> src,
537 ElementsKind elements_kind) {
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000538 Isolate* i = isolate();
539 CALL_HEAP_FUNCTION(i,
540 src->GetElementsTransitionMap(i, elements_kind),
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000541 Map);
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +0000542}
543
544
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000545Handle<FixedArray> Factory::CopyFixedArray(Handle<FixedArray> array) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000546 CALL_HEAP_FUNCTION(isolate(), array->Copy(), FixedArray);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000547}
548
549
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +0000550Handle<FixedArray> Factory::CopySizeFixedArray(Handle<FixedArray> array,
551 int new_length) {
552 CALL_HEAP_FUNCTION(isolate(), array->CopySize(new_length), FixedArray);
553}
554
555
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000556Handle<FixedDoubleArray> Factory::CopyFixedDoubleArray(
557 Handle<FixedDoubleArray> array) {
558 CALL_HEAP_FUNCTION(isolate(), array->Copy(), FixedDoubleArray);
559}
560
561
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000562Handle<JSFunction> Factory::BaseNewFunctionFromSharedFunctionInfo(
563 Handle<SharedFunctionInfo> function_info,
sgjesse@chromium.org846fb742009-12-18 08:56:33 +0000564 Handle<Map> function_map,
565 PretenureFlag pretenure) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000566 CALL_HEAP_FUNCTION(
567 isolate(),
568 isolate()->heap()->AllocateFunction(*function_map,
569 *function_info,
570 isolate()->heap()->the_hole_value(),
571 pretenure),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000572 JSFunction);
573}
574
575
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000576Handle<JSFunction> Factory::NewFunctionFromSharedFunctionInfo(
577 Handle<SharedFunctionInfo> function_info,
sgjesse@chromium.org846fb742009-12-18 08:56:33 +0000578 Handle<Context> context,
579 PretenureFlag pretenure) {
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000580 Handle<JSFunction> result = BaseNewFunctionFromSharedFunctionInfo(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000581 function_info,
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000582 function_info->is_classic_mode()
583 ? isolate()->function_map()
584 : isolate()->strict_mode_function_map(),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000585 pretenure);
586
jkummerow@chromium.org1456e702012-03-30 08:38:13 +0000587 if (function_info->ic_age() != isolate()->heap()->global_ic_age()) {
588 function_info->ResetForNewContext(isolate()->heap()->global_ic_age());
589 }
590
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000591 result->set_context(*context);
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000592
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000593 int index = function_info->SearchOptimizedCodeMap(context->native_context());
ulan@chromium.orgd9e468a2012-06-25 09:47:40 +0000594 if (!function_info->bound() && index < 0) {
595 int number_of_literals = function_info->num_literals();
596 Handle<FixedArray> literals = NewFixedArray(number_of_literals, pretenure);
597 if (number_of_literals > 0) {
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000598 // Store the native context in the literals array prefix. This
ulan@chromium.orgd9e468a2012-06-25 09:47:40 +0000599 // context will be used when creating object, regexp and array
600 // literals in this function.
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000601 literals->set(JSFunction::kLiteralNativeContextIndex,
602 context->native_context());
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000603 }
ulan@chromium.orgd9e468a2012-06-25 09:47:40 +0000604 result->set_literals(*literals);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000605 }
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000606
607 if (index > 0) {
608 // Caching of optimized code enabled and optimized code found.
ulan@chromium.orgd9e468a2012-06-25 09:47:40 +0000609 function_info->InstallFromOptimizedCodeMap(*result, index);
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000610 return result;
611 }
612
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000613 if (V8::UseCrankshaft() &&
614 FLAG_always_opt &&
615 result->is_compiled() &&
616 !function_info->is_toplevel() &&
rossberg@chromium.org2c067b12012-03-19 11:01:52 +0000617 function_info->allows_lazy_compilation() &&
618 !function_info->optimization_disabled()) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000619 result->MarkForLazyRecompilation();
620 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000621 return result;
622}
623
624
625Handle<Object> Factory::NewNumber(double value,
626 PretenureFlag pretenure) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000627 CALL_HEAP_FUNCTION(
628 isolate(),
629 isolate()->heap()->NumberFromDouble(value, pretenure), Object);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000630}
631
632
erikcorry0ad885c2011-11-21 13:51:57 +0000633Handle<Object> Factory::NewNumberFromInt(int32_t value,
634 PretenureFlag pretenure) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000635 CALL_HEAP_FUNCTION(
636 isolate(),
erikcorry0ad885c2011-11-21 13:51:57 +0000637 isolate()->heap()->NumberFromInt32(value, pretenure), Object);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000638}
639
640
erikcorry0ad885c2011-11-21 13:51:57 +0000641Handle<Object> Factory::NewNumberFromUint(uint32_t value,
642 PretenureFlag pretenure) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000643 CALL_HEAP_FUNCTION(
644 isolate(),
erikcorry0ad885c2011-11-21 13:51:57 +0000645 isolate()->heap()->NumberFromUint32(value, pretenure), Object);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000646}
647
648
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000649Handle<JSObject> Factory::NewNeanderObject() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000650 CALL_HEAP_FUNCTION(
651 isolate(),
652 isolate()->heap()->AllocateJSObjectFromMap(
653 isolate()->heap()->neander_map()),
654 JSObject);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000655}
656
657
658Handle<Object> Factory::NewTypeError(const char* type,
659 Vector< Handle<Object> > args) {
660 return NewError("MakeTypeError", type, args);
661}
662
663
664Handle<Object> Factory::NewTypeError(Handle<String> message) {
665 return NewError("$TypeError", message);
666}
667
668
669Handle<Object> Factory::NewRangeError(const char* type,
670 Vector< Handle<Object> > args) {
671 return NewError("MakeRangeError", type, args);
672}
673
674
675Handle<Object> Factory::NewRangeError(Handle<String> message) {
676 return NewError("$RangeError", message);
677}
678
679
680Handle<Object> Factory::NewSyntaxError(const char* type, Handle<JSArray> args) {
681 return NewError("MakeSyntaxError", type, args);
682}
683
684
685Handle<Object> Factory::NewSyntaxError(Handle<String> message) {
686 return NewError("$SyntaxError", message);
687}
688
689
690Handle<Object> Factory::NewReferenceError(const char* type,
691 Vector< Handle<Object> > args) {
692 return NewError("MakeReferenceError", type, args);
693}
694
695
696Handle<Object> Factory::NewReferenceError(Handle<String> message) {
697 return NewError("$ReferenceError", message);
698}
699
700
701Handle<Object> Factory::NewError(const char* maker, const char* type,
702 Vector< Handle<Object> > args) {
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000703 v8::HandleScope scope; // Instantiate a closeable HandleScope for EscapeFrom.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000704 Handle<FixedArray> array = NewFixedArray(args.length());
ager@chromium.org7c537e22008-10-16 08:43:32 +0000705 for (int i = 0; i < args.length(); i++) {
706 array->set(i, *args[i]);
707 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000708 Handle<JSArray> object = NewJSArrayWithElements(array);
ager@chromium.org7c537e22008-10-16 08:43:32 +0000709 Handle<Object> result = NewError(maker, type, object);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000710 return result.EscapeFrom(&scope);
711}
712
713
714Handle<Object> Factory::NewEvalError(const char* type,
715 Vector< Handle<Object> > args) {
716 return NewError("MakeEvalError", type, args);
717}
718
719
720Handle<Object> Factory::NewError(const char* type,
721 Vector< Handle<Object> > args) {
722 return NewError("MakeError", type, args);
723}
724
725
verwaest@chromium.org37141392012-05-31 13:27:02 +0000726Handle<String> Factory::EmergencyNewError(const char* type,
727 Handle<JSArray> args) {
728 const int kBufferSize = 1000;
729 char buffer[kBufferSize];
730 size_t space = kBufferSize;
731 char* p = &buffer[0];
732
733 Vector<char> v(buffer, kBufferSize);
734 OS::StrNCpy(v, type, space);
735 space -= Min(space, strlen(type));
736 p = &buffer[kBufferSize] - space;
737
738 for (unsigned i = 0; i < ARRAY_SIZE(args); i++) {
739 if (space > 0) {
740 *p++ = ' ';
741 space--;
742 if (space > 0) {
743 MaybeObject* maybe_arg = args->GetElement(i);
744 Handle<String> arg_str(reinterpret_cast<String*>(maybe_arg));
745 const char* arg = *arg_str->ToCString();
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000746 Vector<char> v2(p, static_cast<int>(space));
verwaest@chromium.org37141392012-05-31 13:27:02 +0000747 OS::StrNCpy(v2, arg, space);
748 space -= Min(space, strlen(arg));
749 p = &buffer[kBufferSize] - space;
750 }
751 }
752 }
753 if (space > 0) {
754 *p = '\0';
755 } else {
756 buffer[kBufferSize - 1] = '\0';
757 }
758 Handle<String> error_string = NewStringFromUtf8(CStrVector(buffer), TENURED);
759 return error_string;
760}
761
762
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000763Handle<Object> Factory::NewError(const char* maker,
764 const char* type,
765 Handle<JSArray> args) {
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000766 Handle<String> make_str = InternalizeUtf8String(maker);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000767 Handle<Object> fun_obj(
ulan@chromium.org09d7ab52013-02-25 15:50:35 +0000768 isolate()->js_builtins_object()->GetPropertyNoExceptionThrown(*make_str),
769 isolate());
ager@chromium.org4af710e2009-09-15 12:20:11 +0000770 // If the builtins haven't been properly configured yet this error
771 // constructor may not have been defined. Bail out.
verwaest@chromium.org37141392012-05-31 13:27:02 +0000772 if (!fun_obj->IsJSFunction()) {
773 return EmergencyNewError(type, args);
774 }
ager@chromium.org4af710e2009-09-15 12:20:11 +0000775 Handle<JSFunction> fun = Handle<JSFunction>::cast(fun_obj);
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000776 Handle<Object> type_obj = InternalizeUtf8String(type);
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +0000777 Handle<Object> argv[] = { type_obj, args };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000778
779 // Invoke the JavaScript factory method. If an exception is thrown while
780 // running the factory method, use the exception as the result.
781 bool caught_exception;
782 Handle<Object> result = Execution::TryCall(fun,
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +0000783 isolate()->js_builtins_object(),
784 ARRAY_SIZE(argv),
785 argv,
786 &caught_exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000787 return result;
788}
789
790
791Handle<Object> Factory::NewError(Handle<String> message) {
792 return NewError("$Error", message);
793}
794
795
796Handle<Object> Factory::NewError(const char* constructor,
797 Handle<String> message) {
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000798 Handle<String> constr = InternalizeUtf8String(constructor);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000799 Handle<JSFunction> fun = Handle<JSFunction>(
800 JSFunction::cast(isolate()->js_builtins_object()->
801 GetPropertyNoExceptionThrown(*constr)));
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +0000802 Handle<Object> argv[] = { message };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000803
804 // Invoke the JavaScript factory method. If an exception is thrown while
805 // running the factory method, use the exception as the result.
806 bool caught_exception;
807 Handle<Object> result = Execution::TryCall(fun,
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +0000808 isolate()->js_builtins_object(),
809 ARRAY_SIZE(argv),
810 argv,
811 &caught_exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000812 return result;
813}
814
815
816Handle<JSFunction> Factory::NewFunction(Handle<String> name,
817 InstanceType type,
818 int instance_size,
819 Handle<Code> code,
820 bool force_initial_map) {
821 // Allocate the function
822 Handle<JSFunction> function = NewFunction(name, the_hole_value());
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000823
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000824 // Set up the code pointer in both the shared function info and in
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000825 // the function itself.
826 function->shared()->set_code(*code);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000827 function->set_code(*code);
828
829 if (force_initial_map ||
830 type != JS_OBJECT_TYPE ||
831 instance_size != JSObject::kHeaderSize) {
832 Handle<Map> initial_map = NewMap(type, instance_size);
833 Handle<JSObject> prototype = NewFunctionPrototype(function);
834 initial_map->set_prototype(*prototype);
835 function->set_initial_map(*initial_map);
836 initial_map->set_constructor(*function);
837 } else {
838 ASSERT(!function->has_initial_map());
839 ASSERT(!function->has_prototype());
840 }
841
842 return function;
843}
844
845
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000846Handle<JSFunction> Factory::NewFunctionWithPrototype(Handle<String> name,
847 InstanceType type,
848 int instance_size,
849 Handle<JSObject> prototype,
850 Handle<Code> code,
851 bool force_initial_map) {
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000852 // Allocate the function.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000853 Handle<JSFunction> function = NewFunction(name, prototype);
854
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000855 // Set up the code pointer in both the shared function info and in
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000856 // the function itself.
857 function->shared()->set_code(*code);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000858 function->set_code(*code);
859
860 if (force_initial_map ||
861 type != JS_OBJECT_TYPE ||
862 instance_size != JSObject::kHeaderSize) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000863 Handle<Map> initial_map = NewMap(type,
864 instance_size,
svenpanne@chromium.org830d30c2012-05-29 13:20:14 +0000865 GetInitialFastElementsKind());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000866 function->set_initial_map(*initial_map);
867 initial_map->set_constructor(*function);
868 }
869
870 // Set function.prototype and give the prototype a constructor
871 // property that refers to the function.
872 SetPrototypeProperty(function, prototype);
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +0000873 // Currently safe because it is only invoked from Genesis.
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000874 CHECK_NOT_EMPTY_HANDLE(isolate(),
875 JSObject::SetLocalPropertyIgnoreAttributes(
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000876 prototype, constructor_string(),
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000877 function, DONT_ENUM));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000878 return function;
879}
880
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000881
kmillikin@chromium.org4111b802010-05-03 10:34:42 +0000882Handle<JSFunction> Factory::NewFunctionWithoutPrototype(Handle<String> name,
883 Handle<Code> code) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000884 Handle<JSFunction> function = NewFunctionWithoutPrototype(name,
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000885 CLASSIC_MODE);
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000886 function->shared()->set_code(*code);
kmillikin@chromium.org4111b802010-05-03 10:34:42 +0000887 function->set_code(*code);
888 ASSERT(!function->has_initial_map());
889 ASSERT(!function->has_prototype());
890 return function;
891}
892
893
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000894Handle<ScopeInfo> Factory::NewScopeInfo(int length) {
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000895 CALL_HEAP_FUNCTION(
896 isolate(),
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000897 isolate()->heap()->AllocateScopeInfo(length),
898 ScopeInfo);
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000899}
900
901
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +0000902Handle<JSObject> Factory::NewExternal(void* value) {
903 CALL_HEAP_FUNCTION(isolate(),
904 isolate()->heap()->AllocateExternal(value),
905 JSObject);
906}
907
908
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000909Handle<Code> Factory::NewCode(const CodeDesc& desc,
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000910 Code::Flags flags,
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +0000911 Handle<Object> self_ref,
912 bool immovable) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000913 CALL_HEAP_FUNCTION(isolate(),
914 isolate()->heap()->CreateCode(
915 desc, flags, self_ref, immovable),
916 Code);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000917}
918
919
920Handle<Code> Factory::CopyCode(Handle<Code> code) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000921 CALL_HEAP_FUNCTION(isolate(),
922 isolate()->heap()->CopyCode(*code),
923 Code);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000924}
925
926
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000927Handle<Code> Factory::CopyCode(Handle<Code> code, Vector<byte> reloc_info) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000928 CALL_HEAP_FUNCTION(isolate(),
929 isolate()->heap()->CopyCode(*code, reloc_info),
930 Code);
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000931}
932
933
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000934Handle<String> Factory::InternalizedStringFromString(Handle<String> value) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000935 CALL_HEAP_FUNCTION(isolate(),
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000936 isolate()->heap()->InternalizeString(*value), String);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000937}
938
939
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000940Handle<JSObject> Factory::NewJSObject(Handle<JSFunction> constructor,
941 PretenureFlag pretenure) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000942 CALL_HEAP_FUNCTION(
943 isolate(),
944 isolate()->heap()->AllocateJSObject(*constructor, pretenure), JSObject);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000945}
946
947
danno@chromium.org81cac2b2012-07-10 11:28:27 +0000948Handle<JSModule> Factory::NewJSModule(Handle<Context> context,
949 Handle<ScopeInfo> scope_info) {
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000950 CALL_HEAP_FUNCTION(
951 isolate(),
danno@chromium.org81cac2b2012-07-10 11:28:27 +0000952 isolate()->heap()->AllocateJSModule(*context, *scope_info), JSModule);
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000953}
954
955
kasperl@chromium.org2abc4502009-07-02 07:00:29 +0000956Handle<GlobalObject> Factory::NewGlobalObject(
957 Handle<JSFunction> constructor) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000958 CALL_HEAP_FUNCTION(isolate(),
959 isolate()->heap()->AllocateGlobalObject(*constructor),
kasperl@chromium.org2abc4502009-07-02 07:00:29 +0000960 GlobalObject);
961}
962
963
964
mstarzinger@chromium.org71fc3462013-02-27 09:34:27 +0000965Handle<JSObject> Factory::NewJSObjectFromMap(Handle<Map> map,
966 PretenureFlag pretenure) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000967 CALL_HEAP_FUNCTION(
968 isolate(),
mstarzinger@chromium.org71fc3462013-02-27 09:34:27 +0000969 isolate()->heap()->AllocateJSObjectFromMap(*map, pretenure),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000970 JSObject);
ager@chromium.org236ad962008-09-25 09:45:57 +0000971}
972
973
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +0000974Handle<JSArray> Factory::NewJSArray(int capacity,
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000975 ElementsKind elements_kind,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000976 PretenureFlag pretenure) {
ulan@chromium.org8e8d8822012-11-23 14:36:46 +0000977 if (capacity != 0) {
978 elements_kind = GetHoleyElementsKind(elements_kind);
979 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000980 CALL_HEAP_FUNCTION(isolate(),
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000981 isolate()->heap()->AllocateJSArrayAndStorage(
982 elements_kind,
983 0,
984 capacity,
985 INITIALIZE_ARRAY_ELEMENTS_WITH_HOLE,
986 pretenure),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000987 JSArray);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000988}
989
990
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +0000991Handle<JSArray> Factory::NewJSArrayWithElements(Handle<FixedArrayBase> elements,
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000992 ElementsKind elements_kind,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000993 PretenureFlag pretenure) {
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000994 CALL_HEAP_FUNCTION(
995 isolate(),
996 isolate()->heap()->AllocateJSArrayWithElements(*elements,
997 elements_kind,
yangguo@chromium.orgfb377212012-11-16 14:43:43 +0000998 elements->length(),
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000999 pretenure),
1000 JSArray);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001001}
1002
1003
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00001004void Factory::SetElementsCapacityAndLength(Handle<JSArray> array,
1005 int capacity,
1006 int length) {
1007 ElementsAccessor* accessor = array->GetElementsAccessor();
1008 CALL_HEAP_FUNCTION_VOID(
1009 isolate(),
1010 accessor->SetCapacityAndLength(*array, capacity, length));
1011}
1012
1013
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001014void Factory::SetContent(Handle<JSArray> array,
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00001015 Handle<FixedArrayBase> elements) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001016 CALL_HEAP_FUNCTION_VOID(
1017 isolate(),
1018 array->SetContent(*elements));
1019}
1020
1021
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00001022void Factory::EnsureCanContainHeapObjectElements(Handle<JSArray> array) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001023 CALL_HEAP_FUNCTION_VOID(
1024 isolate(),
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00001025 array->EnsureCanContainHeapObjectElements());
1026}
1027
1028
1029void Factory::EnsureCanContainElements(Handle<JSArray> array,
1030 Handle<FixedArrayBase> elements,
svenpanne@chromium.org830d30c2012-05-29 13:20:14 +00001031 uint32_t length,
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00001032 EnsureElementsMode mode) {
1033 CALL_HEAP_FUNCTION_VOID(
1034 isolate(),
svenpanne@chromium.org830d30c2012-05-29 13:20:14 +00001035 array->EnsureCanContainElements(*elements, length, mode));
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001036}
1037
1038
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001039Handle<JSProxy> Factory::NewJSProxy(Handle<Object> handler,
1040 Handle<Object> prototype) {
1041 CALL_HEAP_FUNCTION(
1042 isolate(),
1043 isolate()->heap()->AllocateJSProxy(*handler, *prototype),
1044 JSProxy);
1045}
1046
1047
lrn@chromium.org34e60782011-09-15 07:25:40 +00001048void Factory::BecomeJSObject(Handle<JSReceiver> object) {
rossberg@chromium.org717967f2011-07-20 13:44:42 +00001049 CALL_HEAP_FUNCTION_VOID(
1050 isolate(),
lrn@chromium.org34e60782011-09-15 07:25:40 +00001051 isolate()->heap()->ReinitializeJSReceiver(
1052 *object, JS_OBJECT_TYPE, JSObject::kHeaderSize));
1053}
1054
1055
1056void Factory::BecomeJSFunction(Handle<JSReceiver> object) {
1057 CALL_HEAP_FUNCTION_VOID(
1058 isolate(),
1059 isolate()->heap()->ReinitializeJSReceiver(
1060 *object, JS_FUNCTION_TYPE, JSFunction::kSize));
rossberg@chromium.org717967f2011-07-20 13:44:42 +00001061}
1062
1063
jkummerow@chromium.org000f7fb2012-08-01 11:14:42 +00001064void Factory::SetIdentityHash(Handle<JSObject> object, Smi* hash) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001065 CALL_HEAP_FUNCTION_VOID(
1066 isolate(),
1067 object->SetIdentityHash(hash, ALLOW_CREATION));
1068}
1069
1070
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001071Handle<SharedFunctionInfo> Factory::NewSharedFunctionInfo(
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00001072 Handle<String> name,
1073 int number_of_literals,
1074 Handle<Code> code,
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001075 Handle<ScopeInfo> scope_info) {
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001076 Handle<SharedFunctionInfo> shared = NewSharedFunctionInfo(name);
1077 shared->set_code(*code);
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00001078 shared->set_scope_info(*scope_info);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001079 int literals_array_size = number_of_literals;
1080 // If the function contains object, regexp or array literals,
1081 // allocate extra space for a literals array prefix containing the
1082 // context.
1083 if (number_of_literals > 0) {
1084 literals_array_size += JSFunction::kLiteralsPrefixSize;
1085 }
1086 shared->set_num_literals(literals_array_size);
1087 return shared;
1088}
1089
1090
kmillikin@chromium.org31b12772011-02-02 16:08:26 +00001091Handle<JSMessageObject> Factory::NewJSMessageObject(
1092 Handle<String> type,
1093 Handle<JSArray> arguments,
1094 int start_position,
1095 int end_position,
1096 Handle<Object> script,
1097 Handle<Object> stack_trace,
1098 Handle<Object> stack_frames) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001099 CALL_HEAP_FUNCTION(isolate(),
1100 isolate()->heap()->AllocateJSMessageObject(*type,
1101 *arguments,
1102 start_position,
1103 end_position,
1104 *script,
1105 *stack_trace,
1106 *stack_frames),
kmillikin@chromium.org31b12772011-02-02 16:08:26 +00001107 JSMessageObject);
1108}
1109
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001110Handle<SharedFunctionInfo> Factory::NewSharedFunctionInfo(Handle<String> name) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001111 CALL_HEAP_FUNCTION(isolate(),
1112 isolate()->heap()->AllocateSharedFunctionInfo(*name),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001113 SharedFunctionInfo);
1114}
1115
1116
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00001117Handle<String> Factory::NumberToString(Handle<Object> number) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001118 CALL_HEAP_FUNCTION(isolate(),
1119 isolate()->heap()->NumberToString(*number), String);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00001120}
1121
1122
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001123Handle<String> Factory::Uint32ToString(uint32_t value) {
1124 CALL_HEAP_FUNCTION(isolate(),
1125 isolate()->heap()->Uint32ToString(value), String);
1126}
1127
1128
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001129Handle<SeededNumberDictionary> Factory::DictionaryAtNumberPut(
1130 Handle<SeededNumberDictionary> dictionary,
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00001131 uint32_t key,
1132 Handle<Object> value) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001133 CALL_HEAP_FUNCTION(isolate(),
1134 dictionary->AtNumberPut(key, *value),
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001135 SeededNumberDictionary);
1136}
1137
1138
1139Handle<UnseededNumberDictionary> Factory::DictionaryAtNumberPut(
1140 Handle<UnseededNumberDictionary> dictionary,
1141 uint32_t key,
1142 Handle<Object> value) {
1143 CALL_HEAP_FUNCTION(isolate(),
1144 dictionary->AtNumberPut(key, *value),
1145 UnseededNumberDictionary);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001146}
1147
1148
1149Handle<JSFunction> Factory::NewFunctionHelper(Handle<String> name,
1150 Handle<Object> prototype) {
1151 Handle<SharedFunctionInfo> function_share = NewSharedFunctionInfo(name);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001152 CALL_HEAP_FUNCTION(
1153 isolate(),
1154 isolate()->heap()->AllocateFunction(*isolate()->function_map(),
1155 *function_share,
1156 *prototype),
1157 JSFunction);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001158}
1159
1160
1161Handle<JSFunction> Factory::NewFunction(Handle<String> name,
1162 Handle<Object> prototype) {
1163 Handle<JSFunction> fun = NewFunctionHelper(name, prototype);
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001164 fun->set_context(isolate()->context()->native_context());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001165 return fun;
1166}
1167
1168
kmillikin@chromium.org4111b802010-05-03 10:34:42 +00001169Handle<JSFunction> Factory::NewFunctionWithoutPrototypeHelper(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001170 Handle<String> name,
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00001171 LanguageMode language_mode) {
kmillikin@chromium.org4111b802010-05-03 10:34:42 +00001172 Handle<SharedFunctionInfo> function_share = NewSharedFunctionInfo(name);
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00001173 Handle<Map> map = (language_mode == CLASSIC_MODE)
1174 ? isolate()->function_without_prototype_map()
1175 : isolate()->strict_mode_function_without_prototype_map();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001176 CALL_HEAP_FUNCTION(isolate(),
1177 isolate()->heap()->AllocateFunction(
1178 *map,
kmillikin@chromium.org4111b802010-05-03 10:34:42 +00001179 *function_share,
1180 *the_hole_value()),
1181 JSFunction);
1182}
1183
1184
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001185Handle<JSFunction> Factory::NewFunctionWithoutPrototype(
1186 Handle<String> name,
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00001187 LanguageMode language_mode) {
1188 Handle<JSFunction> fun =
1189 NewFunctionWithoutPrototypeHelper(name, language_mode);
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001190 fun->set_context(isolate()->context()->native_context());
kmillikin@chromium.org4111b802010-05-03 10:34:42 +00001191 return fun;
1192}
1193
1194
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001195Handle<Object> Factory::ToObject(Handle<Object> object) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001196 CALL_HEAP_FUNCTION(isolate(), object->ToObject(), Object);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001197}
1198
1199
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001200Handle<Object> Factory::ToObject(Handle<Object> object,
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001201 Handle<Context> native_context) {
1202 CALL_HEAP_FUNCTION(isolate(), object->ToObject(*native_context), Object);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001203}
1204
1205
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001206#ifdef ENABLE_DEBUGGER_SUPPORT
v8.team.kasperl727e9952008-09-02 14:56:44 +00001207Handle<DebugInfo> Factory::NewDebugInfo(Handle<SharedFunctionInfo> shared) {
1208 // Get the original code of the function.
1209 Handle<Code> code(shared->code());
1210
1211 // Create a copy of the code before allocating the debug info object to avoid
1212 // allocation while setting up the debug info object.
1213 Handle<Code> original_code(*Factory::CopyCode(code));
1214
1215 // Allocate initial fixed array for active break points before allocating the
1216 // debug info object to avoid allocation while setting up the debug info
1217 // object.
1218 Handle<FixedArray> break_points(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001219 NewFixedArray(Debug::kEstimatedNofBreakPointsInFunction));
v8.team.kasperl727e9952008-09-02 14:56:44 +00001220
1221 // Create and set up the debug info object. Debug info contains function, a
1222 // copy of the original code, the executing code and initial fixed array for
1223 // active break points.
1224 Handle<DebugInfo> debug_info =
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001225 Handle<DebugInfo>::cast(NewStruct(DEBUG_INFO_TYPE));
v8.team.kasperl727e9952008-09-02 14:56:44 +00001226 debug_info->set_shared(*shared);
1227 debug_info->set_original_code(*original_code);
1228 debug_info->set_code(*code);
1229 debug_info->set_break_points(*break_points);
1230
1231 // Link debug info to function.
1232 shared->set_debug_info(*debug_info);
1233
1234 return debug_info;
1235}
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001236#endif
v8.team.kasperl727e9952008-09-02 14:56:44 +00001237
1238
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001239Handle<JSObject> Factory::NewArgumentsObject(Handle<Object> callee,
1240 int length) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001241 CALL_HEAP_FUNCTION(
1242 isolate(),
1243 isolate()->heap()->AllocateArgumentsObject(*callee, length), JSObject);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001244}
1245
1246
1247Handle<JSFunction> Factory::CreateApiFunction(
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001248 Handle<FunctionTemplateInfo> obj, ApiInstanceType instance_type) {
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00001249 Handle<Code> code = isolate()->builtins()->HandleApiCall();
1250 Handle<Code> construct_stub = isolate()->builtins()->JSConstructStubApi();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001251
kasper.lund212ac232008-07-16 07:07:30 +00001252 int internal_field_count = 0;
1253 if (!obj->instance_template()->IsUndefined()) {
1254 Handle<ObjectTemplateInfo> instance_template =
1255 Handle<ObjectTemplateInfo>(
1256 ObjectTemplateInfo::cast(obj->instance_template()));
1257 internal_field_count =
1258 Smi::cast(instance_template->internal_field_count())->value();
1259 }
1260
1261 int instance_size = kPointerSize * internal_field_count;
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001262 InstanceType type = INVALID_TYPE;
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001263 switch (instance_type) {
1264 case JavaScriptObject:
1265 type = JS_OBJECT_TYPE;
1266 instance_size += JSObject::kHeaderSize;
1267 break;
1268 case InnerGlobalObject:
1269 type = JS_GLOBAL_OBJECT_TYPE;
1270 instance_size += JSGlobalObject::kSize;
1271 break;
1272 case OuterGlobalObject:
1273 type = JS_GLOBAL_PROXY_TYPE;
1274 instance_size += JSGlobalProxy::kSize;
1275 break;
1276 default:
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001277 break;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001278 }
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001279 ASSERT(type != INVALID_TYPE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001280
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001281 Handle<JSFunction> result =
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001282 NewFunction(Factory::empty_string(),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001283 type,
1284 instance_size,
1285 code,
1286 true);
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00001287
1288 // Set length.
1289 result->shared()->set_length(obj->length());
1290
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001291 // Set class name.
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00001292 Handle<Object> class_name = Handle<Object>(obj->class_name(), isolate());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001293 if (class_name->IsString()) {
1294 result->shared()->set_instance_class_name(*class_name);
1295 result->shared()->set_name(*class_name);
1296 }
1297
1298 Handle<Map> map = Handle<Map>(result->initial_map());
1299
1300 // Mark as undetectable if needed.
1301 if (obj->undetectable()) {
1302 map->set_is_undetectable();
1303 }
1304
1305 // Mark as hidden for the __proto__ accessor if needed.
1306 if (obj->hidden_prototype()) {
1307 map->set_is_hidden_prototype();
1308 }
1309
1310 // Mark as needs_access_check if needed.
1311 if (obj->needs_access_check()) {
ager@chromium.org870a0b62008-11-04 11:43:05 +00001312 map->set_is_access_check_needed(true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001313 }
1314
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001315 // Set interceptor information in the map.
1316 if (!obj->named_property_handler()->IsUndefined()) {
1317 map->set_has_named_interceptor();
1318 }
1319 if (!obj->indexed_property_handler()->IsUndefined()) {
1320 map->set_has_indexed_interceptor();
1321 }
1322
1323 // Set instance call-as-function information in the map.
1324 if (!obj->instance_call_handler()->IsUndefined()) {
1325 map->set_has_instance_call_handler();
1326 }
1327
1328 result->shared()->set_function_data(*obj);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001329 result->shared()->set_construct_stub(*construct_stub);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001330 result->shared()->DontAdaptArguments();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001331
1332 // Recursively copy parent templates' accessors, 'data' may be modified.
verwaest@chromium.org33e09c82012-10-10 17:07:22 +00001333 int max_number_of_additional_properties = 0;
1334 FunctionTemplateInfo* info = *obj;
1335 while (true) {
1336 Object* props = info->property_accessors();
1337 if (!props->IsUndefined()) {
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00001338 Handle<Object> props_handle(props, isolate());
verwaest@chromium.org33e09c82012-10-10 17:07:22 +00001339 NeanderArray props_array(props_handle);
1340 max_number_of_additional_properties += props_array.length();
1341 }
1342 Object* parent = info->parent_template();
1343 if (parent->IsUndefined()) break;
1344 info = FunctionTemplateInfo::cast(parent);
1345 }
1346
1347 Map::EnsureDescriptorSlack(map, max_number_of_additional_properties);
1348
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001349 while (true) {
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00001350 Handle<Object> props = Handle<Object>(obj->property_accessors(),
1351 isolate());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001352 if (!props->IsUndefined()) {
verwaest@chromium.org33e09c82012-10-10 17:07:22 +00001353 Map::AppendCallbackDescriptors(map, props);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001354 }
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00001355 Handle<Object> parent = Handle<Object>(obj->parent_template(), isolate());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001356 if (parent->IsUndefined()) break;
1357 obj = Handle<FunctionTemplateInfo>::cast(parent);
1358 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001359
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001360 ASSERT(result->shared()->IsApiFunction());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001361 return result;
1362}
1363
1364
ager@chromium.org236ad962008-09-25 09:45:57 +00001365Handle<MapCache> Factory::NewMapCache(int at_least_space_for) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001366 CALL_HEAP_FUNCTION(isolate(),
1367 MapCache::Allocate(at_least_space_for), MapCache);
ager@chromium.org236ad962008-09-25 09:45:57 +00001368}
1369
1370
lrn@chromium.org303ada72010-10-27 09:33:13 +00001371MUST_USE_RESULT static MaybeObject* UpdateMapCacheWith(Context* context,
1372 FixedArray* keys,
1373 Map* map) {
1374 Object* result;
1375 { MaybeObject* maybe_result =
1376 MapCache::cast(context->map_cache())->Put(keys, map);
1377 if (!maybe_result->ToObject(&result)) return maybe_result;
1378 }
1379 context->set_map_cache(MapCache::cast(result));
ager@chromium.org236ad962008-09-25 09:45:57 +00001380 return result;
1381}
1382
1383
1384Handle<MapCache> Factory::AddToMapCache(Handle<Context> context,
1385 Handle<FixedArray> keys,
1386 Handle<Map> map) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001387 CALL_HEAP_FUNCTION(isolate(),
1388 UpdateMapCacheWith(*context, *keys, *map), MapCache);
ager@chromium.org236ad962008-09-25 09:45:57 +00001389}
1390
1391
1392Handle<Map> Factory::ObjectLiteralMapFromCache(Handle<Context> context,
1393 Handle<FixedArray> keys) {
1394 if (context->map_cache()->IsUndefined()) {
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001395 // Allocate the new map cache for the native context.
ager@chromium.org236ad962008-09-25 09:45:57 +00001396 Handle<MapCache> new_cache = NewMapCache(24);
1397 context->set_map_cache(*new_cache);
1398 }
ager@chromium.org32912102009-01-16 10:38:43 +00001399 // Check to see whether there is a matching element in the cache.
ager@chromium.org236ad962008-09-25 09:45:57 +00001400 Handle<MapCache> cache =
1401 Handle<MapCache>(MapCache::cast(context->map_cache()));
yangguo@chromium.orgfb377212012-11-16 14:43:43 +00001402 Handle<Object> result = Handle<Object>(cache->Lookup(*keys), isolate());
ager@chromium.org236ad962008-09-25 09:45:57 +00001403 if (result->IsMap()) return Handle<Map>::cast(result);
1404 // Create a new map and add it to the cache.
1405 Handle<Map> map =
ager@chromium.org32912102009-01-16 10:38:43 +00001406 CopyMap(Handle<Map>(context->object_function()->initial_map()),
1407 keys->length());
ager@chromium.org236ad962008-09-25 09:45:57 +00001408 AddToMapCache(context, keys, map);
1409 return Handle<Map>(map);
1410}
1411
1412
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001413void Factory::SetRegExpAtomData(Handle<JSRegExp> regexp,
1414 JSRegExp::Type type,
1415 Handle<String> source,
1416 JSRegExp::Flags flags,
1417 Handle<Object> data) {
1418 Handle<FixedArray> store = NewFixedArray(JSRegExp::kAtomDataSize);
1419
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001420 store->set(JSRegExp::kTagIndex, Smi::FromInt(type));
1421 store->set(JSRegExp::kSourceIndex, *source);
1422 store->set(JSRegExp::kFlagsIndex, Smi::FromInt(flags.value()));
1423 store->set(JSRegExp::kAtomPatternIndex, *data);
1424 regexp->set_data(*store);
1425}
1426
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001427void Factory::SetRegExpIrregexpData(Handle<JSRegExp> regexp,
1428 JSRegExp::Type type,
1429 Handle<String> source,
1430 JSRegExp::Flags flags,
1431 int capture_count) {
1432 Handle<FixedArray> store = NewFixedArray(JSRegExp::kIrregexpDataSize);
jkummerow@chromium.orgddda9e82011-07-06 11:27:02 +00001433 Smi* uninitialized = Smi::FromInt(JSRegExp::kUninitializedValue);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001434 store->set(JSRegExp::kTagIndex, Smi::FromInt(type));
1435 store->set(JSRegExp::kSourceIndex, *source);
1436 store->set(JSRegExp::kFlagsIndex, Smi::FromInt(flags.value()));
jkummerow@chromium.orgddda9e82011-07-06 11:27:02 +00001437 store->set(JSRegExp::kIrregexpASCIICodeIndex, uninitialized);
1438 store->set(JSRegExp::kIrregexpUC16CodeIndex, uninitialized);
1439 store->set(JSRegExp::kIrregexpASCIICodeSavedIndex, uninitialized);
1440 store->set(JSRegExp::kIrregexpUC16CodeSavedIndex, uninitialized);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001441 store->set(JSRegExp::kIrregexpMaxRegisterCountIndex, Smi::FromInt(0));
1442 store->set(JSRegExp::kIrregexpCaptureCountIndex,
1443 Smi::FromInt(capture_count));
1444 regexp->set_data(*store);
1445}
1446
1447
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001448
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001449void Factory::ConfigureInstance(Handle<FunctionTemplateInfo> desc,
1450 Handle<JSObject> instance,
1451 bool* pending_exception) {
1452 // Configure the instance by adding the properties specified by the
1453 // instance template.
yangguo@chromium.orgfb377212012-11-16 14:43:43 +00001454 Handle<Object> instance_template(desc->instance_template(), isolate());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001455 if (!instance_template->IsUndefined()) {
1456 Execution::ConfigureInstance(instance,
1457 instance_template,
1458 pending_exception);
1459 } else {
1460 *pending_exception = false;
1461 }
1462}
1463
1464
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001465Handle<Object> Factory::GlobalConstantFor(Handle<String> name) {
1466 Heap* h = isolate()->heap();
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001467 if (name->Equals(h->undefined_string())) return undefined_value();
1468 if (name->Equals(h->nan_string())) return nan_value();
1469 if (name->Equals(h->infinity_string())) return infinity_value();
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001470 return Handle<Object>::null();
1471}
1472
1473
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00001474Handle<Object> Factory::ToBoolean(bool value) {
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00001475 return value ? true_value() : false_value();
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00001476}
1477
1478
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001479} } // namespace v8::internal