blob: 8dfab1803f165169b020a9c975cfb117b9826619 [file] [log] [blame]
mstarzinger@chromium.orgb228be02013-04-18 14:56:59 +00001// Copyright 2013 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.org6e196bf2013-03-13 09:38:22 +000076 NameDictionary::Allocate(isolate()->heap(),
77 at_least_space_for),
ulan@chromium.org750145a2013-03-07 15:14:13 +000078 NameDictionary);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +000079}
80
81
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +000082Handle<SeededNumberDictionary> Factory::NewSeededNumberDictionary(
83 int at_least_space_for) {
kasperl@chromium.org86f77b72009-07-06 08:21:57 +000084 ASSERT(0 <= at_least_space_for);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000085 CALL_HEAP_FUNCTION(isolate(),
ulan@chromium.org6e196bf2013-03-13 09:38:22 +000086 SeededNumberDictionary::Allocate(isolate()->heap(),
87 at_least_space_for),
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +000088 SeededNumberDictionary);
89}
90
91
92Handle<UnseededNumberDictionary> Factory::NewUnseededNumberDictionary(
93 int at_least_space_for) {
94 ASSERT(0 <= at_least_space_for);
95 CALL_HEAP_FUNCTION(isolate(),
ulan@chromium.org6e196bf2013-03-13 09:38:22 +000096 UnseededNumberDictionary::Allocate(isolate()->heap(),
97 at_least_space_for),
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +000098 UnseededNumberDictionary);
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +000099}
100
101
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000102Handle<ObjectHashSet> Factory::NewObjectHashSet(int at_least_space_for) {
103 ASSERT(0 <= at_least_space_for);
104 CALL_HEAP_FUNCTION(isolate(),
ulan@chromium.org6e196bf2013-03-13 09:38:22 +0000105 ObjectHashSet::Allocate(isolate()->heap(),
106 at_least_space_for),
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000107 ObjectHashSet);
108}
109
110
vegorov@chromium.org7943d462011-08-01 11:41:52 +0000111Handle<ObjectHashTable> Factory::NewObjectHashTable(int at_least_space_for) {
112 ASSERT(0 <= at_least_space_for);
113 CALL_HEAP_FUNCTION(isolate(),
ulan@chromium.org6e196bf2013-03-13 09:38:22 +0000114 ObjectHashTable::Allocate(isolate()->heap(),
115 at_least_space_for),
vegorov@chromium.org7943d462011-08-01 11:41:52 +0000116 ObjectHashTable);
117}
118
119
verwaest@chromium.org33e09c82012-10-10 17:07:22 +0000120Handle<DescriptorArray> Factory::NewDescriptorArray(int number_of_descriptors,
121 int slack) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000122 ASSERT(0 <= number_of_descriptors);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000123 CALL_HEAP_FUNCTION(isolate(),
verwaest@chromium.org33e09c82012-10-10 17:07:22 +0000124 DescriptorArray::Allocate(number_of_descriptors, slack),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000125 DescriptorArray);
126}
127
128
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000129Handle<DeoptimizationInputData> Factory::NewDeoptimizationInputData(
130 int deopt_entry_count,
131 PretenureFlag pretenure) {
132 ASSERT(deopt_entry_count > 0);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000133 CALL_HEAP_FUNCTION(isolate(),
134 DeoptimizationInputData::Allocate(deopt_entry_count,
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000135 pretenure),
136 DeoptimizationInputData);
137}
138
139
140Handle<DeoptimizationOutputData> Factory::NewDeoptimizationOutputData(
141 int deopt_entry_count,
142 PretenureFlag pretenure) {
143 ASSERT(deopt_entry_count > 0);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000144 CALL_HEAP_FUNCTION(isolate(),
145 DeoptimizationOutputData::Allocate(deopt_entry_count,
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000146 pretenure),
147 DeoptimizationOutputData);
148}
149
150
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000151Handle<AccessorPair> Factory::NewAccessorPair() {
152 CALL_HEAP_FUNCTION(isolate(),
153 isolate()->heap()->AllocateAccessorPair(),
154 AccessorPair);
155}
156
157
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000158Handle<TypeFeedbackInfo> Factory::NewTypeFeedbackInfo() {
159 CALL_HEAP_FUNCTION(isolate(),
160 isolate()->heap()->AllocateTypeFeedbackInfo(),
161 TypeFeedbackInfo);
162}
163
164
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000165// Internalized strings are created in the old generation (data space).
166Handle<String> Factory::InternalizeUtf8String(Vector<const char> string) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000167 CALL_HEAP_FUNCTION(isolate(),
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000168 isolate()->heap()->InternalizeUtf8String(string),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000169 String);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000170}
171
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000172// Internalized strings are created in the old generation (data space).
173Handle<String> Factory::InternalizeString(Handle<String> string) {
danno@chromium.org40cb8782011-05-25 07:58:50 +0000174 CALL_HEAP_FUNCTION(isolate(),
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000175 isolate()->heap()->InternalizeString(*string),
danno@chromium.org40cb8782011-05-25 07:58:50 +0000176 String);
177}
178
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000179Handle<String> Factory::InternalizeOneByteString(Vector<const uint8_t> string) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000180 CALL_HEAP_FUNCTION(isolate(),
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000181 isolate()->heap()->InternalizeOneByteString(string),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000182 String);
fschneider@chromium.org9e3e0b62011-01-03 10:16:46 +0000183}
184
danno@chromium.org40cb8782011-05-25 07:58:50 +0000185
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000186Handle<String> Factory::InternalizeOneByteString(
187 Handle<SeqOneByteString> string, int from, int length) {
danno@chromium.org40cb8782011-05-25 07:58:50 +0000188 CALL_HEAP_FUNCTION(isolate(),
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000189 isolate()->heap()->InternalizeOneByteString(
190 string, from, length),
danno@chromium.org40cb8782011-05-25 07:58:50 +0000191 String);
192}
193
194
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000195Handle<String> Factory::InternalizeTwoByteString(Vector<const uc16> string) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000196 CALL_HEAP_FUNCTION(isolate(),
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000197 isolate()->heap()->InternalizeTwoByteString(string),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000198 String);
fschneider@chromium.org9e3e0b62011-01-03 10:16:46 +0000199}
200
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000201
jkummerow@chromium.org59297c72013-01-09 16:32:23 +0000202Handle<String> Factory::NewStringFromOneByte(Vector<const uint8_t> string,
203 PretenureFlag pretenure) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000204 CALL_HEAP_FUNCTION(
205 isolate(),
ulan@chromium.org8e8d8822012-11-23 14:36:46 +0000206 isolate()->heap()->AllocateStringFromOneByte(string, pretenure),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000207 String);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000208}
209
210Handle<String> Factory::NewStringFromUtf8(Vector<const char> string,
211 PretenureFlag pretenure) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000212 CALL_HEAP_FUNCTION(
213 isolate(),
214 isolate()->heap()->AllocateStringFromUtf8(string, pretenure),
215 String);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000216}
217
218
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000219Handle<String> Factory::NewStringFromTwoByte(Vector<const uc16> string,
220 PretenureFlag pretenure) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000221 CALL_HEAP_FUNCTION(
222 isolate(),
223 isolate()->heap()->AllocateStringFromTwoByte(string, pretenure),
224 String);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000225}
226
227
yangguo@chromium.orgfb377212012-11-16 14:43:43 +0000228Handle<SeqOneByteString> Factory::NewRawOneByteString(int length,
ager@chromium.org04921a82011-06-27 13:21:41 +0000229 PretenureFlag pretenure) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000230 CALL_HEAP_FUNCTION(
231 isolate(),
ulan@chromium.org8e8d8822012-11-23 14:36:46 +0000232 isolate()->heap()->AllocateRawOneByteString(length, pretenure),
yangguo@chromium.orgfb377212012-11-16 14:43:43 +0000233 SeqOneByteString);
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +0000234}
235
236
ager@chromium.org04921a82011-06-27 13:21:41 +0000237Handle<SeqTwoByteString> Factory::NewRawTwoByteString(int length,
238 PretenureFlag pretenure) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000239 CALL_HEAP_FUNCTION(
240 isolate(),
241 isolate()->heap()->AllocateRawTwoByteString(length, pretenure),
ager@chromium.org04921a82011-06-27 13:21:41 +0000242 SeqTwoByteString);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000243}
244
245
246Handle<String> Factory::NewConsString(Handle<String> first,
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000247 Handle<String> second) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000248 CALL_HEAP_FUNCTION(isolate(),
249 isolate()->heap()->AllocateConsString(*first, *second),
250 String);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000251}
252
253
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000254Handle<String> Factory::NewSubString(Handle<String> str,
255 int begin,
256 int end) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000257 CALL_HEAP_FUNCTION(isolate(),
258 str->SubString(begin, end),
259 String);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000260}
261
262
ager@chromium.org04921a82011-06-27 13:21:41 +0000263Handle<String> Factory::NewProperSubString(Handle<String> str,
264 int begin,
265 int end) {
266 ASSERT(begin > 0 || end < str->length());
267 CALL_HEAP_FUNCTION(isolate(),
268 isolate()->heap()->AllocateSubString(*str, begin, end),
269 String);
270}
271
272
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000273Handle<String> Factory::NewExternalStringFromAscii(
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000274 const ExternalAsciiString::Resource* resource) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000275 CALL_HEAP_FUNCTION(
276 isolate(),
277 isolate()->heap()->AllocateExternalStringFromAscii(resource),
278 String);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000279}
280
281
282Handle<String> Factory::NewExternalStringFromTwoByte(
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000283 const ExternalTwoByteString::Resource* resource) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000284 CALL_HEAP_FUNCTION(
285 isolate(),
286 isolate()->heap()->AllocateExternalStringFromTwoByte(resource),
287 String);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000288}
289
290
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000291Handle<Symbol> Factory::NewSymbol() {
292 CALL_HEAP_FUNCTION(
293 isolate(),
294 isolate()->heap()->AllocateSymbol(),
295 Symbol);
296}
297
298
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000299Handle<Context> Factory::NewNativeContext() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000300 CALL_HEAP_FUNCTION(
301 isolate(),
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000302 isolate()->heap()->AllocateNativeContext(),
303 Context);
304}
305
306
307Handle<Context> Factory::NewGlobalContext(Handle<JSFunction> function,
308 Handle<ScopeInfo> scope_info) {
309 CALL_HEAP_FUNCTION(
310 isolate(),
311 isolate()->heap()->AllocateGlobalContext(*function, *scope_info),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000312 Context);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000313}
314
315
danno@chromium.org81cac2b2012-07-10 11:28:27 +0000316Handle<Context> Factory::NewModuleContext(Handle<ScopeInfo> scope_info) {
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000317 CALL_HEAP_FUNCTION(
318 isolate(),
danno@chromium.org81cac2b2012-07-10 11:28:27 +0000319 isolate()->heap()->AllocateModuleContext(*scope_info),
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000320 Context);
321}
322
323
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000324Handle<Context> Factory::NewFunctionContext(int length,
vegorov@chromium.org3cf47312011-06-29 13:20:01 +0000325 Handle<JSFunction> function) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000326 CALL_HEAP_FUNCTION(
327 isolate(),
vegorov@chromium.org3cf47312011-06-29 13:20:01 +0000328 isolate()->heap()->AllocateFunctionContext(length, *function),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000329 Context);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000330}
331
332
vegorov@chromium.org3cf47312011-06-29 13:20:01 +0000333Handle<Context> Factory::NewCatchContext(Handle<JSFunction> function,
334 Handle<Context> previous,
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000335 Handle<String> name,
336 Handle<Object> thrown_object) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000337 CALL_HEAP_FUNCTION(
338 isolate(),
vegorov@chromium.org3cf47312011-06-29 13:20:01 +0000339 isolate()->heap()->AllocateCatchContext(*function,
340 *previous,
341 *name,
342 *thrown_object),
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000343 Context);
344}
345
346
vegorov@chromium.org3cf47312011-06-29 13:20:01 +0000347Handle<Context> Factory::NewWithContext(Handle<JSFunction> function,
348 Handle<Context> previous,
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000349 Handle<JSObject> extension) {
350 CALL_HEAP_FUNCTION(
351 isolate(),
vegorov@chromium.org3cf47312011-06-29 13:20:01 +0000352 isolate()->heap()->AllocateWithContext(*function, *previous, *extension),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000353 Context);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000354}
355
356
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000357Handle<Context> Factory::NewBlockContext(Handle<JSFunction> function,
358 Handle<Context> previous,
359 Handle<ScopeInfo> scope_info) {
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000360 CALL_HEAP_FUNCTION(
361 isolate(),
362 isolate()->heap()->AllocateBlockContext(*function,
363 *previous,
364 *scope_info),
365 Context);
366}
367
368
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000369Handle<Struct> Factory::NewStruct(InstanceType type) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000370 CALL_HEAP_FUNCTION(
371 isolate(),
372 isolate()->heap()->AllocateStruct(type),
373 Struct);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000374}
375
376
ulan@chromium.org750145a2013-03-07 15:14:13 +0000377Handle<DeclaredAccessorDescriptor> Factory::NewDeclaredAccessorDescriptor() {
378 return Handle<DeclaredAccessorDescriptor>::cast(
379 NewStruct(DECLARED_ACCESSOR_DESCRIPTOR_TYPE));
380}
381
382
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +0000383Handle<DeclaredAccessorInfo> Factory::NewDeclaredAccessorInfo() {
384 Handle<DeclaredAccessorInfo> info =
385 Handle<DeclaredAccessorInfo>::cast(
386 NewStruct(DECLARED_ACCESSOR_INFO_TYPE));
387 info->set_flag(0); // Must clear the flag, it was initialized as undefined.
388 return info;
389}
390
391
392Handle<ExecutableAccessorInfo> Factory::NewExecutableAccessorInfo() {
393 Handle<ExecutableAccessorInfo> info =
394 Handle<ExecutableAccessorInfo>::cast(
395 NewStruct(EXECUTABLE_ACCESSOR_INFO_TYPE));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000396 info->set_flag(0); // Must clear the flag, it was initialized as undefined.
397 return info;
398}
399
400
401Handle<Script> Factory::NewScript(Handle<String> source) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000402 // Generate id for this script.
403 int id;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000404 Heap* heap = isolate()->heap();
405 if (heap->last_script_id()->IsUndefined()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000406 // Script ids start from one.
407 id = 1;
408 } else {
409 // Increment id, wrap when positive smi is exhausted.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000410 id = Smi::cast(heap->last_script_id())->value();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000411 id++;
412 if (!Smi::IsValid(id)) {
413 id = 0;
414 }
415 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000416 heap->SetLastScriptId(Smi::FromInt(id));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000417
418 // Create and initialize script object.
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000419 Handle<Foreign> wrapper = NewForeign(0, TENURED);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000420 Handle<Script> script = Handle<Script>::cast(NewStruct(SCRIPT_TYPE));
421 script->set_source(*source);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000422 script->set_name(heap->undefined_value());
423 script->set_id(heap->last_script_id());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000424 script->set_line_offset(Smi::FromInt(0));
425 script->set_column_offset(Smi::FromInt(0));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000426 script->set_data(heap->undefined_value());
427 script->set_context_data(heap->undefined_value());
ager@chromium.orge2902be2009-06-08 12:21:35 +0000428 script->set_type(Smi::FromInt(Script::TYPE_NORMAL));
429 script->set_compilation_type(Smi::FromInt(Script::COMPILATION_TYPE_HOST));
rossberg@chromium.org2c067b12012-03-19 11:01:52 +0000430 script->set_compilation_state(
431 Smi::FromInt(Script::COMPILATION_STATE_INITIAL));
ager@chromium.org9085a012009-05-11 19:22:57 +0000432 script->set_wrapper(*wrapper);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000433 script->set_line_ends(heap->undefined_value());
434 script->set_eval_from_shared(heap->undefined_value());
ager@chromium.orge2902be2009-06-08 12:21:35 +0000435 script->set_eval_from_instructions_offset(Smi::FromInt(0));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000436
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000437 return script;
438}
439
440
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000441Handle<Foreign> Factory::NewForeign(Address addr, PretenureFlag pretenure) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000442 CALL_HEAP_FUNCTION(isolate(),
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000443 isolate()->heap()->AllocateForeign(addr, pretenure),
444 Foreign);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000445}
446
447
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000448Handle<Foreign> Factory::NewForeign(const AccessorDescriptor* desc) {
449 return NewForeign((Address) desc, TENURED);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000450}
451
452
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000453Handle<ByteArray> Factory::NewByteArray(int length, PretenureFlag pretenure) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000454 ASSERT(0 <= length);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000455 CALL_HEAP_FUNCTION(
456 isolate(),
457 isolate()->heap()->AllocateByteArray(length, pretenure),
458 ByteArray);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000459}
460
461
ager@chromium.org3811b432009-10-28 14:53:37 +0000462Handle<ExternalArray> Factory::NewExternalArray(int length,
463 ExternalArrayType array_type,
464 void* external_pointer,
465 PretenureFlag pretenure) {
466 ASSERT(0 <= length);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000467 CALL_HEAP_FUNCTION(
468 isolate(),
469 isolate()->heap()->AllocateExternalArray(length,
470 array_type,
471 external_pointer,
472 pretenure),
473 ExternalArray);
ager@chromium.org3811b432009-10-28 14:53:37 +0000474}
475
476
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000477Handle<JSGlobalPropertyCell> Factory::NewJSGlobalPropertyCell(
478 Handle<Object> value) {
ulan@chromium.org32d7dba2013-04-24 10:59:06 +0000479 ALLOW_HANDLE_DEREF(isolate(),
480 "converting a handle into a global property cell");
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000481 CALL_HEAP_FUNCTION(
482 isolate(),
483 isolate()->heap()->AllocateJSGlobalPropertyCell(*value),
484 JSGlobalPropertyCell);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000485}
486
487
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000488Handle<Map> Factory::NewMap(InstanceType type,
489 int instance_size,
490 ElementsKind elements_kind) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000491 CALL_HEAP_FUNCTION(
492 isolate(),
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000493 isolate()->heap()->AllocateMap(type, instance_size, elements_kind),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000494 Map);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000495}
496
497
498Handle<JSObject> Factory::NewFunctionPrototype(Handle<JSFunction> function) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000499 CALL_HEAP_FUNCTION(
500 isolate(),
501 isolate()->heap()->AllocateFunctionPrototype(*function),
502 JSObject);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000503}
504
505
rossberg@chromium.org657d53b2012-07-12 11:06:03 +0000506Handle<Map> Factory::CopyWithPreallocatedFieldDescriptors(Handle<Map> src) {
507 CALL_HEAP_FUNCTION(
508 isolate(), src->CopyWithPreallocatedFieldDescriptors(), Map);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000509}
510
511
ager@chromium.org32912102009-01-16 10:38:43 +0000512Handle<Map> Factory::CopyMap(Handle<Map> src,
513 int extra_inobject_properties) {
rossberg@chromium.org657d53b2012-07-12 11:06:03 +0000514 Handle<Map> copy = CopyWithPreallocatedFieldDescriptors(src);
ager@chromium.org32912102009-01-16 10:38:43 +0000515 // Check that we do not overflow the instance size when adding the
516 // extra inobject properties.
517 int instance_size_delta = extra_inobject_properties * kPointerSize;
518 int max_instance_size_delta =
519 JSObject::kMaxInstanceSize - copy->instance_size();
520 if (instance_size_delta > max_instance_size_delta) {
521 // If the instance size overflows, we allocate as many properties
522 // as we can as inobject properties.
523 instance_size_delta = max_instance_size_delta;
524 extra_inobject_properties = max_instance_size_delta >> kPointerSizeLog2;
525 }
526 // Adjust the map with the extra inobject properties.
527 int inobject_properties =
528 copy->inobject_properties() + extra_inobject_properties;
529 copy->set_inobject_properties(inobject_properties);
530 copy->set_unused_property_fields(inobject_properties);
531 copy->set_instance_size(copy->instance_size() + instance_size_delta);
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000532 copy->set_visitor_id(StaticVisitorBase::GetVisitorId(*copy));
ager@chromium.org32912102009-01-16 10:38:43 +0000533 return copy;
534}
535
fschneider@chromium.org40b9da32010-06-28 11:29:21 +0000536
verwaest@chromium.org753aee42012-07-17 16:15:42 +0000537Handle<Map> Factory::CopyMap(Handle<Map> src) {
verwaest@chromium.orgde64f722012-08-16 15:44:54 +0000538 CALL_HEAP_FUNCTION(isolate(), src->Copy(), Map);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000539}
540
541
kmillikin@chromium.org83e16822011-09-13 08:21:47 +0000542Handle<Map> Factory::GetElementsTransitionMap(
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000543 Handle<JSObject> src,
544 ElementsKind elements_kind) {
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000545 Isolate* i = isolate();
546 CALL_HEAP_FUNCTION(i,
547 src->GetElementsTransitionMap(i, elements_kind),
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000548 Map);
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +0000549}
550
551
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000552Handle<FixedArray> Factory::CopyFixedArray(Handle<FixedArray> array) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000553 CALL_HEAP_FUNCTION(isolate(), array->Copy(), FixedArray);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000554}
555
556
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +0000557Handle<FixedArray> Factory::CopySizeFixedArray(Handle<FixedArray> array,
558 int new_length) {
559 CALL_HEAP_FUNCTION(isolate(), array->CopySize(new_length), FixedArray);
560}
561
562
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000563Handle<FixedDoubleArray> Factory::CopyFixedDoubleArray(
564 Handle<FixedDoubleArray> array) {
565 CALL_HEAP_FUNCTION(isolate(), array->Copy(), FixedDoubleArray);
566}
567
568
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000569Handle<JSFunction> Factory::BaseNewFunctionFromSharedFunctionInfo(
570 Handle<SharedFunctionInfo> function_info,
sgjesse@chromium.org846fb742009-12-18 08:56:33 +0000571 Handle<Map> function_map,
572 PretenureFlag pretenure) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000573 CALL_HEAP_FUNCTION(
574 isolate(),
575 isolate()->heap()->AllocateFunction(*function_map,
576 *function_info,
577 isolate()->heap()->the_hole_value(),
578 pretenure),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000579 JSFunction);
580}
581
582
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000583static Handle<Map> MapForNewFunction(Isolate *isolate,
584 Handle<SharedFunctionInfo> function_info) {
585 Context *context = isolate->context()->native_context();
586 int map_index = Context::FunctionMapIndex(function_info->language_mode(),
587 function_info->is_generator());
588 return Handle<Map>(Map::cast(context->get(map_index)));
589}
590
591
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000592Handle<JSFunction> Factory::NewFunctionFromSharedFunctionInfo(
593 Handle<SharedFunctionInfo> function_info,
sgjesse@chromium.org846fb742009-12-18 08:56:33 +0000594 Handle<Context> context,
595 PretenureFlag pretenure) {
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000596 Handle<JSFunction> result = BaseNewFunctionFromSharedFunctionInfo(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000597 function_info,
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000598 MapForNewFunction(isolate(), function_info),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000599 pretenure);
600
jkummerow@chromium.org1456e702012-03-30 08:38:13 +0000601 if (function_info->ic_age() != isolate()->heap()->global_ic_age()) {
602 function_info->ResetForNewContext(isolate()->heap()->global_ic_age());
603 }
604
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000605 result->set_context(*context);
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000606
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000607 int index = function_info->SearchOptimizedCodeMap(context->native_context());
ulan@chromium.orgd9e468a2012-06-25 09:47:40 +0000608 if (!function_info->bound() && index < 0) {
609 int number_of_literals = function_info->num_literals();
610 Handle<FixedArray> literals = NewFixedArray(number_of_literals, pretenure);
611 if (number_of_literals > 0) {
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000612 // Store the native context in the literals array prefix. This
ulan@chromium.orgd9e468a2012-06-25 09:47:40 +0000613 // context will be used when creating object, regexp and array
614 // literals in this function.
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000615 literals->set(JSFunction::kLiteralNativeContextIndex,
616 context->native_context());
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000617 }
ulan@chromium.orgd9e468a2012-06-25 09:47:40 +0000618 result->set_literals(*literals);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000619 }
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000620
621 if (index > 0) {
622 // Caching of optimized code enabled and optimized code found.
ulan@chromium.orgd9e468a2012-06-25 09:47:40 +0000623 function_info->InstallFromOptimizedCodeMap(*result, index);
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000624 return result;
625 }
626
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000627 if (V8::UseCrankshaft() &&
628 FLAG_always_opt &&
629 result->is_compiled() &&
630 !function_info->is_toplevel() &&
rossberg@chromium.org2c067b12012-03-19 11:01:52 +0000631 function_info->allows_lazy_compilation() &&
632 !function_info->optimization_disabled()) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000633 result->MarkForLazyRecompilation();
634 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000635 return result;
636}
637
638
639Handle<Object> Factory::NewNumber(double value,
640 PretenureFlag pretenure) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000641 CALL_HEAP_FUNCTION(
642 isolate(),
643 isolate()->heap()->NumberFromDouble(value, pretenure), Object);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000644}
645
646
erikcorry0ad885c2011-11-21 13:51:57 +0000647Handle<Object> Factory::NewNumberFromInt(int32_t value,
648 PretenureFlag pretenure) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000649 CALL_HEAP_FUNCTION(
650 isolate(),
erikcorry0ad885c2011-11-21 13:51:57 +0000651 isolate()->heap()->NumberFromInt32(value, pretenure), Object);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000652}
653
654
erikcorry0ad885c2011-11-21 13:51:57 +0000655Handle<Object> Factory::NewNumberFromUint(uint32_t value,
656 PretenureFlag pretenure) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000657 CALL_HEAP_FUNCTION(
658 isolate(),
erikcorry0ad885c2011-11-21 13:51:57 +0000659 isolate()->heap()->NumberFromUint32(value, pretenure), Object);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000660}
661
662
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000663Handle<JSObject> Factory::NewNeanderObject() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000664 CALL_HEAP_FUNCTION(
665 isolate(),
666 isolate()->heap()->AllocateJSObjectFromMap(
667 isolate()->heap()->neander_map()),
668 JSObject);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000669}
670
671
672Handle<Object> Factory::NewTypeError(const char* type,
673 Vector< Handle<Object> > args) {
674 return NewError("MakeTypeError", type, args);
675}
676
677
678Handle<Object> Factory::NewTypeError(Handle<String> message) {
679 return NewError("$TypeError", message);
680}
681
682
683Handle<Object> Factory::NewRangeError(const char* type,
684 Vector< Handle<Object> > args) {
685 return NewError("MakeRangeError", type, args);
686}
687
688
689Handle<Object> Factory::NewRangeError(Handle<String> message) {
690 return NewError("$RangeError", message);
691}
692
693
694Handle<Object> Factory::NewSyntaxError(const char* type, Handle<JSArray> args) {
695 return NewError("MakeSyntaxError", type, args);
696}
697
698
699Handle<Object> Factory::NewSyntaxError(Handle<String> message) {
700 return NewError("$SyntaxError", message);
701}
702
703
704Handle<Object> Factory::NewReferenceError(const char* type,
705 Vector< Handle<Object> > args) {
706 return NewError("MakeReferenceError", type, args);
707}
708
709
710Handle<Object> Factory::NewReferenceError(Handle<String> message) {
711 return NewError("$ReferenceError", message);
712}
713
714
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +0000715Handle<Object> Factory::NewError(const char* maker,
716 const char* type,
717 Vector< Handle<Object> > args) {
718 // Instantiate a closeable HandleScope for EscapeFrom.
719 v8::HandleScope scope(reinterpret_cast<v8::Isolate*>(isolate()));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000720 Handle<FixedArray> array = NewFixedArray(args.length());
ager@chromium.org7c537e22008-10-16 08:43:32 +0000721 for (int i = 0; i < args.length(); i++) {
722 array->set(i, *args[i]);
723 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000724 Handle<JSArray> object = NewJSArrayWithElements(array);
ager@chromium.org7c537e22008-10-16 08:43:32 +0000725 Handle<Object> result = NewError(maker, type, object);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000726 return result.EscapeFrom(&scope);
727}
728
729
730Handle<Object> Factory::NewEvalError(const char* type,
731 Vector< Handle<Object> > args) {
732 return NewError("MakeEvalError", type, args);
733}
734
735
736Handle<Object> Factory::NewError(const char* type,
737 Vector< Handle<Object> > args) {
738 return NewError("MakeError", type, args);
739}
740
741
verwaest@chromium.org37141392012-05-31 13:27:02 +0000742Handle<String> Factory::EmergencyNewError(const char* type,
743 Handle<JSArray> args) {
744 const int kBufferSize = 1000;
745 char buffer[kBufferSize];
746 size_t space = kBufferSize;
747 char* p = &buffer[0];
748
749 Vector<char> v(buffer, kBufferSize);
750 OS::StrNCpy(v, type, space);
751 space -= Min(space, strlen(type));
752 p = &buffer[kBufferSize] - space;
753
754 for (unsigned i = 0; i < ARRAY_SIZE(args); i++) {
755 if (space > 0) {
756 *p++ = ' ';
757 space--;
758 if (space > 0) {
759 MaybeObject* maybe_arg = args->GetElement(i);
760 Handle<String> arg_str(reinterpret_cast<String*>(maybe_arg));
761 const char* arg = *arg_str->ToCString();
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000762 Vector<char> v2(p, static_cast<int>(space));
verwaest@chromium.org37141392012-05-31 13:27:02 +0000763 OS::StrNCpy(v2, arg, space);
764 space -= Min(space, strlen(arg));
765 p = &buffer[kBufferSize] - space;
766 }
767 }
768 }
769 if (space > 0) {
770 *p = '\0';
771 } else {
772 buffer[kBufferSize - 1] = '\0';
773 }
774 Handle<String> error_string = NewStringFromUtf8(CStrVector(buffer), TENURED);
775 return error_string;
776}
777
778
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000779Handle<Object> Factory::NewError(const char* maker,
780 const char* type,
781 Handle<JSArray> args) {
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000782 Handle<String> make_str = InternalizeUtf8String(maker);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000783 Handle<Object> fun_obj(
ulan@chromium.org09d7ab52013-02-25 15:50:35 +0000784 isolate()->js_builtins_object()->GetPropertyNoExceptionThrown(*make_str),
785 isolate());
ager@chromium.org4af710e2009-09-15 12:20:11 +0000786 // If the builtins haven't been properly configured yet this error
787 // constructor may not have been defined. Bail out.
verwaest@chromium.org37141392012-05-31 13:27:02 +0000788 if (!fun_obj->IsJSFunction()) {
789 return EmergencyNewError(type, args);
790 }
ager@chromium.org4af710e2009-09-15 12:20:11 +0000791 Handle<JSFunction> fun = Handle<JSFunction>::cast(fun_obj);
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000792 Handle<Object> type_obj = InternalizeUtf8String(type);
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +0000793 Handle<Object> argv[] = { type_obj, args };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000794
795 // Invoke the JavaScript factory method. If an exception is thrown while
796 // running the factory method, use the exception as the result.
797 bool caught_exception;
798 Handle<Object> result = Execution::TryCall(fun,
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +0000799 isolate()->js_builtins_object(),
800 ARRAY_SIZE(argv),
801 argv,
802 &caught_exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000803 return result;
804}
805
806
807Handle<Object> Factory::NewError(Handle<String> message) {
808 return NewError("$Error", message);
809}
810
811
812Handle<Object> Factory::NewError(const char* constructor,
813 Handle<String> message) {
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000814 Handle<String> constr = InternalizeUtf8String(constructor);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000815 Handle<JSFunction> fun = Handle<JSFunction>(
816 JSFunction::cast(isolate()->js_builtins_object()->
817 GetPropertyNoExceptionThrown(*constr)));
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +0000818 Handle<Object> argv[] = { message };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000819
820 // Invoke the JavaScript factory method. If an exception is thrown while
821 // running the factory method, use the exception as the result.
822 bool caught_exception;
823 Handle<Object> result = Execution::TryCall(fun,
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +0000824 isolate()->js_builtins_object(),
825 ARRAY_SIZE(argv),
826 argv,
827 &caught_exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000828 return result;
829}
830
831
832Handle<JSFunction> Factory::NewFunction(Handle<String> name,
833 InstanceType type,
834 int instance_size,
835 Handle<Code> code,
836 bool force_initial_map) {
837 // Allocate the function
838 Handle<JSFunction> function = NewFunction(name, the_hole_value());
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000839
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000840 // Set up the code pointer in both the shared function info and in
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000841 // the function itself.
842 function->shared()->set_code(*code);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000843 function->set_code(*code);
844
845 if (force_initial_map ||
846 type != JS_OBJECT_TYPE ||
847 instance_size != JSObject::kHeaderSize) {
848 Handle<Map> initial_map = NewMap(type, instance_size);
849 Handle<JSObject> prototype = NewFunctionPrototype(function);
850 initial_map->set_prototype(*prototype);
851 function->set_initial_map(*initial_map);
852 initial_map->set_constructor(*function);
853 } else {
854 ASSERT(!function->has_initial_map());
855 ASSERT(!function->has_prototype());
856 }
857
858 return function;
859}
860
861
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000862Handle<JSFunction> Factory::NewFunctionWithPrototype(Handle<String> name,
863 InstanceType type,
864 int instance_size,
865 Handle<JSObject> prototype,
866 Handle<Code> code,
867 bool force_initial_map) {
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000868 // Allocate the function.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000869 Handle<JSFunction> function = NewFunction(name, prototype);
870
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000871 // Set up the code pointer in both the shared function info and in
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000872 // the function itself.
873 function->shared()->set_code(*code);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000874 function->set_code(*code);
875
876 if (force_initial_map ||
877 type != JS_OBJECT_TYPE ||
878 instance_size != JSObject::kHeaderSize) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000879 Handle<Map> initial_map = NewMap(type,
880 instance_size,
svenpanne@chromium.org830d30c2012-05-29 13:20:14 +0000881 GetInitialFastElementsKind());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000882 function->set_initial_map(*initial_map);
883 initial_map->set_constructor(*function);
884 }
885
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000886 SetPrototypeProperty(function, prototype);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000887 return function;
888}
889
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000890
kmillikin@chromium.org4111b802010-05-03 10:34:42 +0000891Handle<JSFunction> Factory::NewFunctionWithoutPrototype(Handle<String> name,
892 Handle<Code> code) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000893 Handle<JSFunction> function = NewFunctionWithoutPrototype(name,
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000894 CLASSIC_MODE);
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000895 function->shared()->set_code(*code);
kmillikin@chromium.org4111b802010-05-03 10:34:42 +0000896 function->set_code(*code);
897 ASSERT(!function->has_initial_map());
898 ASSERT(!function->has_prototype());
899 return function;
900}
901
902
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000903Handle<ScopeInfo> Factory::NewScopeInfo(int length) {
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000904 CALL_HEAP_FUNCTION(
905 isolate(),
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000906 isolate()->heap()->AllocateScopeInfo(length),
907 ScopeInfo);
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000908}
909
910
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +0000911Handle<JSObject> Factory::NewExternal(void* value) {
912 CALL_HEAP_FUNCTION(isolate(),
913 isolate()->heap()->AllocateExternal(value),
914 JSObject);
915}
916
917
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000918Handle<Code> Factory::NewCode(const CodeDesc& desc,
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000919 Code::Flags flags,
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +0000920 Handle<Object> self_ref,
mstarzinger@chromium.orgb228be02013-04-18 14:56:59 +0000921 bool immovable,
922 bool crankshafted) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000923 CALL_HEAP_FUNCTION(isolate(),
924 isolate()->heap()->CreateCode(
mstarzinger@chromium.orgb228be02013-04-18 14:56:59 +0000925 desc, flags, self_ref, immovable, crankshafted),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000926 Code);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000927}
928
929
930Handle<Code> Factory::CopyCode(Handle<Code> code) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000931 CALL_HEAP_FUNCTION(isolate(),
932 isolate()->heap()->CopyCode(*code),
933 Code);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000934}
935
936
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000937Handle<Code> Factory::CopyCode(Handle<Code> code, Vector<byte> reloc_info) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000938 CALL_HEAP_FUNCTION(isolate(),
939 isolate()->heap()->CopyCode(*code, reloc_info),
940 Code);
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000941}
942
943
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000944Handle<String> Factory::InternalizedStringFromString(Handle<String> value) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000945 CALL_HEAP_FUNCTION(isolate(),
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000946 isolate()->heap()->InternalizeString(*value), String);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000947}
948
949
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000950Handle<JSObject> Factory::NewJSObject(Handle<JSFunction> constructor,
951 PretenureFlag pretenure) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000952 CALL_HEAP_FUNCTION(
953 isolate(),
954 isolate()->heap()->AllocateJSObject(*constructor, pretenure), JSObject);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000955}
956
957
danno@chromium.org81cac2b2012-07-10 11:28:27 +0000958Handle<JSModule> Factory::NewJSModule(Handle<Context> context,
959 Handle<ScopeInfo> scope_info) {
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000960 CALL_HEAP_FUNCTION(
961 isolate(),
danno@chromium.org81cac2b2012-07-10 11:28:27 +0000962 isolate()->heap()->AllocateJSModule(*context, *scope_info), JSModule);
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000963}
964
965
kasperl@chromium.org2abc4502009-07-02 07:00:29 +0000966Handle<GlobalObject> Factory::NewGlobalObject(
967 Handle<JSFunction> constructor) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000968 CALL_HEAP_FUNCTION(isolate(),
969 isolate()->heap()->AllocateGlobalObject(*constructor),
kasperl@chromium.org2abc4502009-07-02 07:00:29 +0000970 GlobalObject);
971}
972
973
974
mstarzinger@chromium.org71fc3462013-02-27 09:34:27 +0000975Handle<JSObject> Factory::NewJSObjectFromMap(Handle<Map> map,
976 PretenureFlag pretenure) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000977 CALL_HEAP_FUNCTION(
978 isolate(),
mstarzinger@chromium.org71fc3462013-02-27 09:34:27 +0000979 isolate()->heap()->AllocateJSObjectFromMap(*map, pretenure),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000980 JSObject);
ager@chromium.org236ad962008-09-25 09:45:57 +0000981}
982
983
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +0000984Handle<JSArray> Factory::NewJSArray(int capacity,
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000985 ElementsKind elements_kind,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000986 PretenureFlag pretenure) {
ulan@chromium.org8e8d8822012-11-23 14:36:46 +0000987 if (capacity != 0) {
988 elements_kind = GetHoleyElementsKind(elements_kind);
989 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000990 CALL_HEAP_FUNCTION(isolate(),
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000991 isolate()->heap()->AllocateJSArrayAndStorage(
992 elements_kind,
993 0,
994 capacity,
995 INITIALIZE_ARRAY_ELEMENTS_WITH_HOLE,
996 pretenure),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000997 JSArray);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000998}
999
1000
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00001001Handle<JSArray> Factory::NewJSArrayWithElements(Handle<FixedArrayBase> elements,
danno@chromium.orgfa458e42012-02-01 10:48:36 +00001002 ElementsKind elements_kind,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001003 PretenureFlag pretenure) {
danno@chromium.orgfa458e42012-02-01 10:48:36 +00001004 CALL_HEAP_FUNCTION(
1005 isolate(),
1006 isolate()->heap()->AllocateJSArrayWithElements(*elements,
1007 elements_kind,
yangguo@chromium.orgfb377212012-11-16 14:43:43 +00001008 elements->length(),
danno@chromium.orgfa458e42012-02-01 10:48:36 +00001009 pretenure),
1010 JSArray);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001011}
1012
1013
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00001014void Factory::SetElementsCapacityAndLength(Handle<JSArray> array,
1015 int capacity,
1016 int length) {
1017 ElementsAccessor* accessor = array->GetElementsAccessor();
1018 CALL_HEAP_FUNCTION_VOID(
1019 isolate(),
1020 accessor->SetCapacityAndLength(*array, capacity, length));
1021}
1022
1023
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001024void Factory::SetContent(Handle<JSArray> array,
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00001025 Handle<FixedArrayBase> elements) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001026 CALL_HEAP_FUNCTION_VOID(
1027 isolate(),
1028 array->SetContent(*elements));
1029}
1030
1031
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00001032void Factory::EnsureCanContainHeapObjectElements(Handle<JSArray> array) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001033 CALL_HEAP_FUNCTION_VOID(
1034 isolate(),
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00001035 array->EnsureCanContainHeapObjectElements());
1036}
1037
1038
1039void Factory::EnsureCanContainElements(Handle<JSArray> array,
1040 Handle<FixedArrayBase> elements,
svenpanne@chromium.org830d30c2012-05-29 13:20:14 +00001041 uint32_t length,
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00001042 EnsureElementsMode mode) {
1043 CALL_HEAP_FUNCTION_VOID(
1044 isolate(),
svenpanne@chromium.org830d30c2012-05-29 13:20:14 +00001045 array->EnsureCanContainElements(*elements, length, mode));
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001046}
1047
1048
danno@chromium.orgca29dd82013-04-26 11:59:48 +00001049Handle<JSArrayBuffer> Factory::NewJSArrayBuffer() {
1050 JSFunction* array_buffer_fun =
1051 isolate()->context()->native_context()->array_buffer_fun();
1052 CALL_HEAP_FUNCTION(
1053 isolate(),
1054 isolate()->heap()->AllocateJSObject(array_buffer_fun),
1055 JSArrayBuffer);
1056}
1057
1058
danno@chromium.orgf005df62013-04-30 16:36:45 +00001059Handle<JSTypedArray> Factory::NewJSTypedArray(ExternalArrayType type) {
1060 JSFunction* typed_array_fun;
1061 Context* native_context = isolate()->context()->native_context();
1062 switch (type) {
1063 case kExternalUnsignedByteArray:
1064 typed_array_fun = native_context->uint8_array_fun();
1065 break;
1066
1067 case kExternalByteArray:
1068 typed_array_fun = native_context->int8_array_fun();
1069 break;
1070
1071 case kExternalUnsignedShortArray:
1072 typed_array_fun = native_context->uint16_array_fun();
1073 break;
1074
1075 case kExternalShortArray:
1076 typed_array_fun = native_context->int16_array_fun();
1077 break;
1078
1079 case kExternalUnsignedIntArray:
1080 typed_array_fun = native_context->uint32_array_fun();
1081 break;
1082
1083 case kExternalIntArray:
1084 typed_array_fun = native_context->int32_array_fun();
1085 break;
1086
1087 case kExternalFloatArray:
1088 typed_array_fun = native_context->float_array_fun();
1089 break;
1090
1091 case kExternalDoubleArray:
1092 typed_array_fun = native_context->double_array_fun();
1093 break;
1094
1095 default:
1096 UNREACHABLE();
1097 return Handle<JSTypedArray>();
1098 }
1099
1100 CALL_HEAP_FUNCTION(
1101 isolate(),
1102 isolate()->heap()->AllocateJSObject(typed_array_fun),
1103 JSTypedArray);
1104}
1105
1106
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001107Handle<JSProxy> Factory::NewJSProxy(Handle<Object> handler,
1108 Handle<Object> prototype) {
1109 CALL_HEAP_FUNCTION(
1110 isolate(),
1111 isolate()->heap()->AllocateJSProxy(*handler, *prototype),
1112 JSProxy);
1113}
1114
1115
lrn@chromium.org34e60782011-09-15 07:25:40 +00001116void Factory::BecomeJSObject(Handle<JSReceiver> object) {
rossberg@chromium.org717967f2011-07-20 13:44:42 +00001117 CALL_HEAP_FUNCTION_VOID(
1118 isolate(),
lrn@chromium.org34e60782011-09-15 07:25:40 +00001119 isolate()->heap()->ReinitializeJSReceiver(
1120 *object, JS_OBJECT_TYPE, JSObject::kHeaderSize));
1121}
1122
1123
1124void Factory::BecomeJSFunction(Handle<JSReceiver> object) {
1125 CALL_HEAP_FUNCTION_VOID(
1126 isolate(),
1127 isolate()->heap()->ReinitializeJSReceiver(
1128 *object, JS_FUNCTION_TYPE, JSFunction::kSize));
rossberg@chromium.org717967f2011-07-20 13:44:42 +00001129}
1130
1131
jkummerow@chromium.org000f7fb2012-08-01 11:14:42 +00001132void Factory::SetIdentityHash(Handle<JSObject> object, Smi* hash) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001133 CALL_HEAP_FUNCTION_VOID(
1134 isolate(),
1135 object->SetIdentityHash(hash, ALLOW_CREATION));
1136}
1137
1138
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001139Handle<SharedFunctionInfo> Factory::NewSharedFunctionInfo(
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00001140 Handle<String> name,
1141 int number_of_literals,
mstarzinger@chromium.orgb228be02013-04-18 14:56:59 +00001142 bool is_generator,
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00001143 Handle<Code> code,
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001144 Handle<ScopeInfo> scope_info) {
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001145 Handle<SharedFunctionInfo> shared = NewSharedFunctionInfo(name);
1146 shared->set_code(*code);
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00001147 shared->set_scope_info(*scope_info);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001148 int literals_array_size = number_of_literals;
1149 // If the function contains object, regexp or array literals,
1150 // allocate extra space for a literals array prefix containing the
1151 // context.
1152 if (number_of_literals > 0) {
1153 literals_array_size += JSFunction::kLiteralsPrefixSize;
1154 }
1155 shared->set_num_literals(literals_array_size);
mstarzinger@chromium.orgb228be02013-04-18 14:56:59 +00001156 if (is_generator) {
1157 shared->set_instance_class_name(isolate()->heap()->Generator_string());
1158 }
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001159 return shared;
1160}
1161
1162
kmillikin@chromium.org31b12772011-02-02 16:08:26 +00001163Handle<JSMessageObject> Factory::NewJSMessageObject(
1164 Handle<String> type,
1165 Handle<JSArray> arguments,
1166 int start_position,
1167 int end_position,
1168 Handle<Object> script,
1169 Handle<Object> stack_trace,
1170 Handle<Object> stack_frames) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001171 CALL_HEAP_FUNCTION(isolate(),
1172 isolate()->heap()->AllocateJSMessageObject(*type,
1173 *arguments,
1174 start_position,
1175 end_position,
1176 *script,
1177 *stack_trace,
1178 *stack_frames),
kmillikin@chromium.org31b12772011-02-02 16:08:26 +00001179 JSMessageObject);
1180}
1181
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001182Handle<SharedFunctionInfo> Factory::NewSharedFunctionInfo(Handle<String> name) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001183 CALL_HEAP_FUNCTION(isolate(),
1184 isolate()->heap()->AllocateSharedFunctionInfo(*name),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001185 SharedFunctionInfo);
1186}
1187
1188
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00001189Handle<String> Factory::NumberToString(Handle<Object> number) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001190 CALL_HEAP_FUNCTION(isolate(),
1191 isolate()->heap()->NumberToString(*number), String);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00001192}
1193
1194
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001195Handle<String> Factory::Uint32ToString(uint32_t value) {
1196 CALL_HEAP_FUNCTION(isolate(),
1197 isolate()->heap()->Uint32ToString(value), String);
1198}
1199
1200
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001201Handle<SeededNumberDictionary> Factory::DictionaryAtNumberPut(
1202 Handle<SeededNumberDictionary> dictionary,
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00001203 uint32_t key,
1204 Handle<Object> value) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001205 CALL_HEAP_FUNCTION(isolate(),
1206 dictionary->AtNumberPut(key, *value),
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001207 SeededNumberDictionary);
1208}
1209
1210
1211Handle<UnseededNumberDictionary> Factory::DictionaryAtNumberPut(
1212 Handle<UnseededNumberDictionary> dictionary,
1213 uint32_t key,
1214 Handle<Object> value) {
1215 CALL_HEAP_FUNCTION(isolate(),
1216 dictionary->AtNumberPut(key, *value),
1217 UnseededNumberDictionary);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001218}
1219
1220
1221Handle<JSFunction> Factory::NewFunctionHelper(Handle<String> name,
1222 Handle<Object> prototype) {
1223 Handle<SharedFunctionInfo> function_share = NewSharedFunctionInfo(name);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001224 CALL_HEAP_FUNCTION(
1225 isolate(),
1226 isolate()->heap()->AllocateFunction(*isolate()->function_map(),
1227 *function_share,
1228 *prototype),
1229 JSFunction);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001230}
1231
1232
1233Handle<JSFunction> Factory::NewFunction(Handle<String> name,
1234 Handle<Object> prototype) {
1235 Handle<JSFunction> fun = NewFunctionHelper(name, prototype);
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001236 fun->set_context(isolate()->context()->native_context());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001237 return fun;
1238}
1239
1240
kmillikin@chromium.org4111b802010-05-03 10:34:42 +00001241Handle<JSFunction> Factory::NewFunctionWithoutPrototypeHelper(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001242 Handle<String> name,
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00001243 LanguageMode language_mode) {
kmillikin@chromium.org4111b802010-05-03 10:34:42 +00001244 Handle<SharedFunctionInfo> function_share = NewSharedFunctionInfo(name);
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00001245 Handle<Map> map = (language_mode == CLASSIC_MODE)
1246 ? isolate()->function_without_prototype_map()
1247 : isolate()->strict_mode_function_without_prototype_map();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001248 CALL_HEAP_FUNCTION(isolate(),
1249 isolate()->heap()->AllocateFunction(
1250 *map,
kmillikin@chromium.org4111b802010-05-03 10:34:42 +00001251 *function_share,
1252 *the_hole_value()),
1253 JSFunction);
1254}
1255
1256
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001257Handle<JSFunction> Factory::NewFunctionWithoutPrototype(
1258 Handle<String> name,
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00001259 LanguageMode language_mode) {
1260 Handle<JSFunction> fun =
1261 NewFunctionWithoutPrototypeHelper(name, language_mode);
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001262 fun->set_context(isolate()->context()->native_context());
kmillikin@chromium.org4111b802010-05-03 10:34:42 +00001263 return fun;
1264}
1265
1266
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001267Handle<Object> Factory::ToObject(Handle<Object> object) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001268 CALL_HEAP_FUNCTION(isolate(), object->ToObject(), Object);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001269}
1270
1271
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001272Handle<Object> Factory::ToObject(Handle<Object> object,
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001273 Handle<Context> native_context) {
1274 CALL_HEAP_FUNCTION(isolate(), object->ToObject(*native_context), Object);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001275}
1276
1277
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001278#ifdef ENABLE_DEBUGGER_SUPPORT
v8.team.kasperl727e9952008-09-02 14:56:44 +00001279Handle<DebugInfo> Factory::NewDebugInfo(Handle<SharedFunctionInfo> shared) {
1280 // Get the original code of the function.
1281 Handle<Code> code(shared->code());
1282
1283 // Create a copy of the code before allocating the debug info object to avoid
1284 // allocation while setting up the debug info object.
1285 Handle<Code> original_code(*Factory::CopyCode(code));
1286
1287 // Allocate initial fixed array for active break points before allocating the
1288 // debug info object to avoid allocation while setting up the debug info
1289 // object.
1290 Handle<FixedArray> break_points(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001291 NewFixedArray(Debug::kEstimatedNofBreakPointsInFunction));
v8.team.kasperl727e9952008-09-02 14:56:44 +00001292
1293 // Create and set up the debug info object. Debug info contains function, a
1294 // copy of the original code, the executing code and initial fixed array for
1295 // active break points.
1296 Handle<DebugInfo> debug_info =
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001297 Handle<DebugInfo>::cast(NewStruct(DEBUG_INFO_TYPE));
v8.team.kasperl727e9952008-09-02 14:56:44 +00001298 debug_info->set_shared(*shared);
1299 debug_info->set_original_code(*original_code);
1300 debug_info->set_code(*code);
1301 debug_info->set_break_points(*break_points);
1302
1303 // Link debug info to function.
1304 shared->set_debug_info(*debug_info);
1305
1306 return debug_info;
1307}
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001308#endif
v8.team.kasperl727e9952008-09-02 14:56:44 +00001309
1310
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001311Handle<JSObject> Factory::NewArgumentsObject(Handle<Object> callee,
1312 int length) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001313 CALL_HEAP_FUNCTION(
1314 isolate(),
1315 isolate()->heap()->AllocateArgumentsObject(*callee, length), JSObject);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001316}
1317
1318
1319Handle<JSFunction> Factory::CreateApiFunction(
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001320 Handle<FunctionTemplateInfo> obj, ApiInstanceType instance_type) {
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00001321 Handle<Code> code = isolate()->builtins()->HandleApiCall();
1322 Handle<Code> construct_stub = isolate()->builtins()->JSConstructStubApi();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001323
kasper.lund212ac232008-07-16 07:07:30 +00001324 int internal_field_count = 0;
1325 if (!obj->instance_template()->IsUndefined()) {
1326 Handle<ObjectTemplateInfo> instance_template =
1327 Handle<ObjectTemplateInfo>(
1328 ObjectTemplateInfo::cast(obj->instance_template()));
1329 internal_field_count =
1330 Smi::cast(instance_template->internal_field_count())->value();
1331 }
1332
1333 int instance_size = kPointerSize * internal_field_count;
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001334 InstanceType type = INVALID_TYPE;
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001335 switch (instance_type) {
1336 case JavaScriptObject:
1337 type = JS_OBJECT_TYPE;
1338 instance_size += JSObject::kHeaderSize;
1339 break;
1340 case InnerGlobalObject:
1341 type = JS_GLOBAL_OBJECT_TYPE;
1342 instance_size += JSGlobalObject::kSize;
1343 break;
1344 case OuterGlobalObject:
1345 type = JS_GLOBAL_PROXY_TYPE;
1346 instance_size += JSGlobalProxy::kSize;
1347 break;
1348 default:
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001349 break;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001350 }
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001351 ASSERT(type != INVALID_TYPE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001352
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001353 Handle<JSFunction> result =
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001354 NewFunction(Factory::empty_string(),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001355 type,
1356 instance_size,
1357 code,
1358 true);
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00001359
1360 // Set length.
1361 result->shared()->set_length(obj->length());
1362
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001363 // Set class name.
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00001364 Handle<Object> class_name = Handle<Object>(obj->class_name(), isolate());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001365 if (class_name->IsString()) {
1366 result->shared()->set_instance_class_name(*class_name);
1367 result->shared()->set_name(*class_name);
1368 }
1369
1370 Handle<Map> map = Handle<Map>(result->initial_map());
1371
1372 // Mark as undetectable if needed.
1373 if (obj->undetectable()) {
1374 map->set_is_undetectable();
1375 }
1376
1377 // Mark as hidden for the __proto__ accessor if needed.
1378 if (obj->hidden_prototype()) {
1379 map->set_is_hidden_prototype();
1380 }
1381
1382 // Mark as needs_access_check if needed.
1383 if (obj->needs_access_check()) {
ager@chromium.org870a0b62008-11-04 11:43:05 +00001384 map->set_is_access_check_needed(true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001385 }
1386
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001387 // Set interceptor information in the map.
1388 if (!obj->named_property_handler()->IsUndefined()) {
1389 map->set_has_named_interceptor();
1390 }
1391 if (!obj->indexed_property_handler()->IsUndefined()) {
1392 map->set_has_indexed_interceptor();
1393 }
1394
1395 // Set instance call-as-function information in the map.
1396 if (!obj->instance_call_handler()->IsUndefined()) {
1397 map->set_has_instance_call_handler();
1398 }
1399
1400 result->shared()->set_function_data(*obj);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001401 result->shared()->set_construct_stub(*construct_stub);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001402 result->shared()->DontAdaptArguments();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001403
1404 // Recursively copy parent templates' accessors, 'data' may be modified.
verwaest@chromium.org33e09c82012-10-10 17:07:22 +00001405 int max_number_of_additional_properties = 0;
1406 FunctionTemplateInfo* info = *obj;
1407 while (true) {
1408 Object* props = info->property_accessors();
1409 if (!props->IsUndefined()) {
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00001410 Handle<Object> props_handle(props, isolate());
verwaest@chromium.org33e09c82012-10-10 17:07:22 +00001411 NeanderArray props_array(props_handle);
1412 max_number_of_additional_properties += props_array.length();
1413 }
1414 Object* parent = info->parent_template();
1415 if (parent->IsUndefined()) break;
1416 info = FunctionTemplateInfo::cast(parent);
1417 }
1418
1419 Map::EnsureDescriptorSlack(map, max_number_of_additional_properties);
1420
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001421 while (true) {
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00001422 Handle<Object> props = Handle<Object>(obj->property_accessors(),
1423 isolate());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001424 if (!props->IsUndefined()) {
verwaest@chromium.org33e09c82012-10-10 17:07:22 +00001425 Map::AppendCallbackDescriptors(map, props);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001426 }
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00001427 Handle<Object> parent = Handle<Object>(obj->parent_template(), isolate());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001428 if (parent->IsUndefined()) break;
1429 obj = Handle<FunctionTemplateInfo>::cast(parent);
1430 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001431
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001432 ASSERT(result->shared()->IsApiFunction());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001433 return result;
1434}
1435
1436
ager@chromium.org236ad962008-09-25 09:45:57 +00001437Handle<MapCache> Factory::NewMapCache(int at_least_space_for) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001438 CALL_HEAP_FUNCTION(isolate(),
ulan@chromium.org6e196bf2013-03-13 09:38:22 +00001439 MapCache::Allocate(isolate()->heap(),
1440 at_least_space_for),
1441 MapCache);
ager@chromium.org236ad962008-09-25 09:45:57 +00001442}
1443
1444
lrn@chromium.org303ada72010-10-27 09:33:13 +00001445MUST_USE_RESULT static MaybeObject* UpdateMapCacheWith(Context* context,
1446 FixedArray* keys,
1447 Map* map) {
1448 Object* result;
1449 { MaybeObject* maybe_result =
1450 MapCache::cast(context->map_cache())->Put(keys, map);
1451 if (!maybe_result->ToObject(&result)) return maybe_result;
1452 }
1453 context->set_map_cache(MapCache::cast(result));
ager@chromium.org236ad962008-09-25 09:45:57 +00001454 return result;
1455}
1456
1457
1458Handle<MapCache> Factory::AddToMapCache(Handle<Context> context,
1459 Handle<FixedArray> keys,
1460 Handle<Map> map) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001461 CALL_HEAP_FUNCTION(isolate(),
1462 UpdateMapCacheWith(*context, *keys, *map), MapCache);
ager@chromium.org236ad962008-09-25 09:45:57 +00001463}
1464
1465
1466Handle<Map> Factory::ObjectLiteralMapFromCache(Handle<Context> context,
1467 Handle<FixedArray> keys) {
1468 if (context->map_cache()->IsUndefined()) {
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001469 // Allocate the new map cache for the native context.
ager@chromium.org236ad962008-09-25 09:45:57 +00001470 Handle<MapCache> new_cache = NewMapCache(24);
1471 context->set_map_cache(*new_cache);
1472 }
ager@chromium.org32912102009-01-16 10:38:43 +00001473 // Check to see whether there is a matching element in the cache.
ager@chromium.org236ad962008-09-25 09:45:57 +00001474 Handle<MapCache> cache =
1475 Handle<MapCache>(MapCache::cast(context->map_cache()));
yangguo@chromium.orgfb377212012-11-16 14:43:43 +00001476 Handle<Object> result = Handle<Object>(cache->Lookup(*keys), isolate());
ager@chromium.org236ad962008-09-25 09:45:57 +00001477 if (result->IsMap()) return Handle<Map>::cast(result);
1478 // Create a new map and add it to the cache.
1479 Handle<Map> map =
ager@chromium.org32912102009-01-16 10:38:43 +00001480 CopyMap(Handle<Map>(context->object_function()->initial_map()),
1481 keys->length());
ager@chromium.org236ad962008-09-25 09:45:57 +00001482 AddToMapCache(context, keys, map);
1483 return Handle<Map>(map);
1484}
1485
1486
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001487void Factory::SetRegExpAtomData(Handle<JSRegExp> regexp,
1488 JSRegExp::Type type,
1489 Handle<String> source,
1490 JSRegExp::Flags flags,
1491 Handle<Object> data) {
1492 Handle<FixedArray> store = NewFixedArray(JSRegExp::kAtomDataSize);
1493
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001494 store->set(JSRegExp::kTagIndex, Smi::FromInt(type));
1495 store->set(JSRegExp::kSourceIndex, *source);
1496 store->set(JSRegExp::kFlagsIndex, Smi::FromInt(flags.value()));
1497 store->set(JSRegExp::kAtomPatternIndex, *data);
1498 regexp->set_data(*store);
1499}
1500
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001501void Factory::SetRegExpIrregexpData(Handle<JSRegExp> regexp,
1502 JSRegExp::Type type,
1503 Handle<String> source,
1504 JSRegExp::Flags flags,
1505 int capture_count) {
1506 Handle<FixedArray> store = NewFixedArray(JSRegExp::kIrregexpDataSize);
jkummerow@chromium.orgddda9e82011-07-06 11:27:02 +00001507 Smi* uninitialized = Smi::FromInt(JSRegExp::kUninitializedValue);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001508 store->set(JSRegExp::kTagIndex, Smi::FromInt(type));
1509 store->set(JSRegExp::kSourceIndex, *source);
1510 store->set(JSRegExp::kFlagsIndex, Smi::FromInt(flags.value()));
jkummerow@chromium.orgddda9e82011-07-06 11:27:02 +00001511 store->set(JSRegExp::kIrregexpASCIICodeIndex, uninitialized);
1512 store->set(JSRegExp::kIrregexpUC16CodeIndex, uninitialized);
1513 store->set(JSRegExp::kIrregexpASCIICodeSavedIndex, uninitialized);
1514 store->set(JSRegExp::kIrregexpUC16CodeSavedIndex, uninitialized);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001515 store->set(JSRegExp::kIrregexpMaxRegisterCountIndex, Smi::FromInt(0));
1516 store->set(JSRegExp::kIrregexpCaptureCountIndex,
1517 Smi::FromInt(capture_count));
1518 regexp->set_data(*store);
1519}
1520
1521
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001522
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001523void Factory::ConfigureInstance(Handle<FunctionTemplateInfo> desc,
1524 Handle<JSObject> instance,
1525 bool* pending_exception) {
1526 // Configure the instance by adding the properties specified by the
1527 // instance template.
yangguo@chromium.orgfb377212012-11-16 14:43:43 +00001528 Handle<Object> instance_template(desc->instance_template(), isolate());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001529 if (!instance_template->IsUndefined()) {
1530 Execution::ConfigureInstance(instance,
1531 instance_template,
1532 pending_exception);
1533 } else {
1534 *pending_exception = false;
1535 }
1536}
1537
1538
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001539Handle<Object> Factory::GlobalConstantFor(Handle<String> name) {
1540 Heap* h = isolate()->heap();
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001541 if (name->Equals(h->undefined_string())) return undefined_value();
1542 if (name->Equals(h->nan_string())) return nan_value();
1543 if (name->Equals(h->infinity_string())) return infinity_value();
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001544 return Handle<Object>::null();
1545}
1546
1547
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00001548Handle<Object> Factory::ToBoolean(bool value) {
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00001549 return value ? true_value() : false_value();
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00001550}
1551
1552
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001553} } // namespace v8::internal