blob: fbe6cb860b144a20fa1816af24f61e22118bae7e [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
danno@chromium.org1fd77d52013-06-07 16:01:45 +000044Handle<Box> Factory::NewBox(Handle<Object> value, PretenureFlag pretenure) {
45 CALL_HEAP_FUNCTION(
46 isolate(),
47 isolate()->heap()->AllocateBox(*value, pretenure),
48 Box);
49}
50
51
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000052Handle<FixedArray> Factory::NewFixedArray(int size, PretenureFlag pretenure) {
53 ASSERT(0 <= size);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000054 CALL_HEAP_FUNCTION(
55 isolate(),
56 isolate()->heap()->AllocateFixedArray(size, pretenure),
57 FixedArray);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000058}
59
60
ricow@chromium.orgc9c80822010-04-21 08:22:37 +000061Handle<FixedArray> Factory::NewFixedArrayWithHoles(int size,
62 PretenureFlag pretenure) {
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +000063 ASSERT(0 <= size);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000064 CALL_HEAP_FUNCTION(
65 isolate(),
66 isolate()->heap()->AllocateFixedArrayWithHoles(size, pretenure),
67 FixedArray);
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +000068}
69
70
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +000071Handle<FixedDoubleArray> Factory::NewFixedDoubleArray(int size,
72 PretenureFlag pretenure) {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +000073 ASSERT(0 <= size);
74 CALL_HEAP_FUNCTION(
75 isolate(),
76 isolate()->heap()->AllocateUninitializedFixedDoubleArray(size, pretenure),
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +000077 FixedDoubleArray);
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +000078}
79
80
ulan@chromium.org750145a2013-03-07 15:14:13 +000081Handle<NameDictionary> Factory::NewNameDictionary(int at_least_space_for) {
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +000082 ASSERT(0 <= at_least_space_for);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000083 CALL_HEAP_FUNCTION(isolate(),
ulan@chromium.org6e196bf2013-03-13 09:38:22 +000084 NameDictionary::Allocate(isolate()->heap(),
85 at_least_space_for),
ulan@chromium.org750145a2013-03-07 15:14:13 +000086 NameDictionary);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +000087}
88
89
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +000090Handle<SeededNumberDictionary> Factory::NewSeededNumberDictionary(
91 int at_least_space_for) {
kasperl@chromium.org86f77b72009-07-06 08:21:57 +000092 ASSERT(0 <= at_least_space_for);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000093 CALL_HEAP_FUNCTION(isolate(),
ulan@chromium.org6e196bf2013-03-13 09:38:22 +000094 SeededNumberDictionary::Allocate(isolate()->heap(),
95 at_least_space_for),
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +000096 SeededNumberDictionary);
97}
98
99
100Handle<UnseededNumberDictionary> Factory::NewUnseededNumberDictionary(
101 int at_least_space_for) {
102 ASSERT(0 <= at_least_space_for);
103 CALL_HEAP_FUNCTION(isolate(),
ulan@chromium.org6e196bf2013-03-13 09:38:22 +0000104 UnseededNumberDictionary::Allocate(isolate()->heap(),
105 at_least_space_for),
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000106 UnseededNumberDictionary);
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000107}
108
109
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000110Handle<ObjectHashSet> Factory::NewObjectHashSet(int at_least_space_for) {
111 ASSERT(0 <= at_least_space_for);
112 CALL_HEAP_FUNCTION(isolate(),
ulan@chromium.org6e196bf2013-03-13 09:38:22 +0000113 ObjectHashSet::Allocate(isolate()->heap(),
114 at_least_space_for),
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000115 ObjectHashSet);
116}
117
118
vegorov@chromium.org7943d462011-08-01 11:41:52 +0000119Handle<ObjectHashTable> Factory::NewObjectHashTable(int at_least_space_for) {
120 ASSERT(0 <= at_least_space_for);
121 CALL_HEAP_FUNCTION(isolate(),
ulan@chromium.org6e196bf2013-03-13 09:38:22 +0000122 ObjectHashTable::Allocate(isolate()->heap(),
123 at_least_space_for),
vegorov@chromium.org7943d462011-08-01 11:41:52 +0000124 ObjectHashTable);
125}
126
127
verwaest@chromium.org33e09c82012-10-10 17:07:22 +0000128Handle<DescriptorArray> Factory::NewDescriptorArray(int number_of_descriptors,
129 int slack) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000130 ASSERT(0 <= number_of_descriptors);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000131 CALL_HEAP_FUNCTION(isolate(),
verwaest@chromium.org33e09c82012-10-10 17:07:22 +0000132 DescriptorArray::Allocate(number_of_descriptors, slack),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000133 DescriptorArray);
134}
135
136
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000137Handle<DeoptimizationInputData> Factory::NewDeoptimizationInputData(
138 int deopt_entry_count,
139 PretenureFlag pretenure) {
140 ASSERT(deopt_entry_count > 0);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000141 CALL_HEAP_FUNCTION(isolate(),
142 DeoptimizationInputData::Allocate(deopt_entry_count,
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000143 pretenure),
144 DeoptimizationInputData);
145}
146
147
148Handle<DeoptimizationOutputData> Factory::NewDeoptimizationOutputData(
149 int deopt_entry_count,
150 PretenureFlag pretenure) {
151 ASSERT(deopt_entry_count > 0);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000152 CALL_HEAP_FUNCTION(isolate(),
153 DeoptimizationOutputData::Allocate(deopt_entry_count,
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000154 pretenure),
155 DeoptimizationOutputData);
156}
157
158
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000159Handle<AccessorPair> Factory::NewAccessorPair() {
160 CALL_HEAP_FUNCTION(isolate(),
161 isolate()->heap()->AllocateAccessorPair(),
162 AccessorPair);
163}
164
165
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000166Handle<TypeFeedbackInfo> Factory::NewTypeFeedbackInfo() {
167 CALL_HEAP_FUNCTION(isolate(),
168 isolate()->heap()->AllocateTypeFeedbackInfo(),
169 TypeFeedbackInfo);
170}
171
172
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000173// Internalized strings are created in the old generation (data space).
174Handle<String> Factory::InternalizeUtf8String(Vector<const char> 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()->InternalizeUtf8String(string),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000177 String);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000178}
179
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000180// Internalized strings are created in the old generation (data space).
181Handle<String> Factory::InternalizeString(Handle<String> string) {
danno@chromium.org40cb8782011-05-25 07:58:50 +0000182 CALL_HEAP_FUNCTION(isolate(),
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000183 isolate()->heap()->InternalizeString(*string),
danno@chromium.org40cb8782011-05-25 07:58:50 +0000184 String);
185}
186
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000187Handle<String> Factory::InternalizeOneByteString(Vector<const uint8_t> string) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000188 CALL_HEAP_FUNCTION(isolate(),
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000189 isolate()->heap()->InternalizeOneByteString(string),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000190 String);
fschneider@chromium.org9e3e0b62011-01-03 10:16:46 +0000191}
192
danno@chromium.org40cb8782011-05-25 07:58:50 +0000193
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000194Handle<String> Factory::InternalizeOneByteString(
195 Handle<SeqOneByteString> string, int from, int length) {
danno@chromium.org40cb8782011-05-25 07:58:50 +0000196 CALL_HEAP_FUNCTION(isolate(),
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000197 isolate()->heap()->InternalizeOneByteString(
198 string, from, length),
danno@chromium.org40cb8782011-05-25 07:58:50 +0000199 String);
200}
201
202
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000203Handle<String> Factory::InternalizeTwoByteString(Vector<const uc16> string) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000204 CALL_HEAP_FUNCTION(isolate(),
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000205 isolate()->heap()->InternalizeTwoByteString(string),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000206 String);
fschneider@chromium.org9e3e0b62011-01-03 10:16:46 +0000207}
208
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000209
jkummerow@chromium.org59297c72013-01-09 16:32:23 +0000210Handle<String> Factory::NewStringFromOneByte(Vector<const uint8_t> string,
211 PretenureFlag pretenure) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000212 CALL_HEAP_FUNCTION(
213 isolate(),
ulan@chromium.org8e8d8822012-11-23 14:36:46 +0000214 isolate()->heap()->AllocateStringFromOneByte(string, pretenure),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000215 String);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000216}
217
218Handle<String> Factory::NewStringFromUtf8(Vector<const char> string,
219 PretenureFlag pretenure) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000220 CALL_HEAP_FUNCTION(
221 isolate(),
222 isolate()->heap()->AllocateStringFromUtf8(string, pretenure),
223 String);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000224}
225
226
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000227Handle<String> Factory::NewStringFromTwoByte(Vector<const uc16> string,
228 PretenureFlag pretenure) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000229 CALL_HEAP_FUNCTION(
230 isolate(),
231 isolate()->heap()->AllocateStringFromTwoByte(string, pretenure),
232 String);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000233}
234
235
yangguo@chromium.orgfb377212012-11-16 14:43:43 +0000236Handle<SeqOneByteString> Factory::NewRawOneByteString(int length,
ager@chromium.org04921a82011-06-27 13:21:41 +0000237 PretenureFlag pretenure) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000238 CALL_HEAP_FUNCTION(
239 isolate(),
ulan@chromium.org8e8d8822012-11-23 14:36:46 +0000240 isolate()->heap()->AllocateRawOneByteString(length, pretenure),
yangguo@chromium.orgfb377212012-11-16 14:43:43 +0000241 SeqOneByteString);
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +0000242}
243
244
ager@chromium.org04921a82011-06-27 13:21:41 +0000245Handle<SeqTwoByteString> Factory::NewRawTwoByteString(int length,
246 PretenureFlag pretenure) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000247 CALL_HEAP_FUNCTION(
248 isolate(),
249 isolate()->heap()->AllocateRawTwoByteString(length, pretenure),
ager@chromium.org04921a82011-06-27 13:21:41 +0000250 SeqTwoByteString);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000251}
252
253
254Handle<String> Factory::NewConsString(Handle<String> first,
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000255 Handle<String> second) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000256 CALL_HEAP_FUNCTION(isolate(),
257 isolate()->heap()->AllocateConsString(*first, *second),
258 String);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000259}
260
261
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000262Handle<String> Factory::NewSubString(Handle<String> str,
263 int begin,
264 int end) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000265 CALL_HEAP_FUNCTION(isolate(),
266 str->SubString(begin, end),
267 String);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000268}
269
270
ager@chromium.org04921a82011-06-27 13:21:41 +0000271Handle<String> Factory::NewProperSubString(Handle<String> str,
272 int begin,
273 int end) {
274 ASSERT(begin > 0 || end < str->length());
275 CALL_HEAP_FUNCTION(isolate(),
276 isolate()->heap()->AllocateSubString(*str, begin, end),
277 String);
278}
279
280
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000281Handle<String> Factory::NewExternalStringFromAscii(
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000282 const ExternalAsciiString::Resource* resource) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000283 CALL_HEAP_FUNCTION(
284 isolate(),
285 isolate()->heap()->AllocateExternalStringFromAscii(resource),
286 String);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000287}
288
289
290Handle<String> Factory::NewExternalStringFromTwoByte(
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000291 const ExternalTwoByteString::Resource* resource) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000292 CALL_HEAP_FUNCTION(
293 isolate(),
294 isolate()->heap()->AllocateExternalStringFromTwoByte(resource),
295 String);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000296}
297
298
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000299Handle<Symbol> Factory::NewSymbol() {
300 CALL_HEAP_FUNCTION(
301 isolate(),
302 isolate()->heap()->AllocateSymbol(),
303 Symbol);
304}
305
306
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000307Handle<Context> Factory::NewNativeContext() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000308 CALL_HEAP_FUNCTION(
309 isolate(),
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000310 isolate()->heap()->AllocateNativeContext(),
311 Context);
312}
313
314
315Handle<Context> Factory::NewGlobalContext(Handle<JSFunction> function,
316 Handle<ScopeInfo> scope_info) {
317 CALL_HEAP_FUNCTION(
318 isolate(),
319 isolate()->heap()->AllocateGlobalContext(*function, *scope_info),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000320 Context);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000321}
322
323
danno@chromium.org81cac2b2012-07-10 11:28:27 +0000324Handle<Context> Factory::NewModuleContext(Handle<ScopeInfo> scope_info) {
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000325 CALL_HEAP_FUNCTION(
326 isolate(),
danno@chromium.org81cac2b2012-07-10 11:28:27 +0000327 isolate()->heap()->AllocateModuleContext(*scope_info),
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000328 Context);
329}
330
331
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000332Handle<Context> Factory::NewFunctionContext(int length,
vegorov@chromium.org3cf47312011-06-29 13:20:01 +0000333 Handle<JSFunction> function) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000334 CALL_HEAP_FUNCTION(
335 isolate(),
vegorov@chromium.org3cf47312011-06-29 13:20:01 +0000336 isolate()->heap()->AllocateFunctionContext(length, *function),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000337 Context);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000338}
339
340
vegorov@chromium.org3cf47312011-06-29 13:20:01 +0000341Handle<Context> Factory::NewCatchContext(Handle<JSFunction> function,
342 Handle<Context> previous,
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000343 Handle<String> name,
344 Handle<Object> thrown_object) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000345 CALL_HEAP_FUNCTION(
346 isolate(),
vegorov@chromium.org3cf47312011-06-29 13:20:01 +0000347 isolate()->heap()->AllocateCatchContext(*function,
348 *previous,
349 *name,
350 *thrown_object),
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000351 Context);
352}
353
354
vegorov@chromium.org3cf47312011-06-29 13:20:01 +0000355Handle<Context> Factory::NewWithContext(Handle<JSFunction> function,
356 Handle<Context> previous,
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000357 Handle<JSObject> extension) {
358 CALL_HEAP_FUNCTION(
359 isolate(),
vegorov@chromium.org3cf47312011-06-29 13:20:01 +0000360 isolate()->heap()->AllocateWithContext(*function, *previous, *extension),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000361 Context);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000362}
363
364
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000365Handle<Context> Factory::NewBlockContext(Handle<JSFunction> function,
366 Handle<Context> previous,
367 Handle<ScopeInfo> scope_info) {
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000368 CALL_HEAP_FUNCTION(
369 isolate(),
370 isolate()->heap()->AllocateBlockContext(*function,
371 *previous,
372 *scope_info),
373 Context);
374}
375
376
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000377Handle<Struct> Factory::NewStruct(InstanceType type) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000378 CALL_HEAP_FUNCTION(
379 isolate(),
380 isolate()->heap()->AllocateStruct(type),
381 Struct);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000382}
383
384
ulan@chromium.org750145a2013-03-07 15:14:13 +0000385Handle<DeclaredAccessorDescriptor> Factory::NewDeclaredAccessorDescriptor() {
386 return Handle<DeclaredAccessorDescriptor>::cast(
387 NewStruct(DECLARED_ACCESSOR_DESCRIPTOR_TYPE));
388}
389
390
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +0000391Handle<DeclaredAccessorInfo> Factory::NewDeclaredAccessorInfo() {
392 Handle<DeclaredAccessorInfo> info =
393 Handle<DeclaredAccessorInfo>::cast(
394 NewStruct(DECLARED_ACCESSOR_INFO_TYPE));
395 info->set_flag(0); // Must clear the flag, it was initialized as undefined.
396 return info;
397}
398
399
400Handle<ExecutableAccessorInfo> Factory::NewExecutableAccessorInfo() {
401 Handle<ExecutableAccessorInfo> info =
402 Handle<ExecutableAccessorInfo>::cast(
403 NewStruct(EXECUTABLE_ACCESSOR_INFO_TYPE));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000404 info->set_flag(0); // Must clear the flag, it was initialized as undefined.
405 return info;
406}
407
408
409Handle<Script> Factory::NewScript(Handle<String> source) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000410 // Generate id for this script.
411 int id;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000412 Heap* heap = isolate()->heap();
413 if (heap->last_script_id()->IsUndefined()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000414 // Script ids start from one.
415 id = 1;
416 } else {
417 // Increment id, wrap when positive smi is exhausted.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000418 id = Smi::cast(heap->last_script_id())->value();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000419 id++;
420 if (!Smi::IsValid(id)) {
421 id = 0;
422 }
423 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000424 heap->SetLastScriptId(Smi::FromInt(id));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000425
426 // Create and initialize script object.
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000427 Handle<Foreign> wrapper = NewForeign(0, TENURED);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000428 Handle<Script> script = Handle<Script>::cast(NewStruct(SCRIPT_TYPE));
429 script->set_source(*source);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000430 script->set_name(heap->undefined_value());
431 script->set_id(heap->last_script_id());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000432 script->set_line_offset(Smi::FromInt(0));
433 script->set_column_offset(Smi::FromInt(0));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000434 script->set_data(heap->undefined_value());
435 script->set_context_data(heap->undefined_value());
ager@chromium.orge2902be2009-06-08 12:21:35 +0000436 script->set_type(Smi::FromInt(Script::TYPE_NORMAL));
437 script->set_compilation_type(Smi::FromInt(Script::COMPILATION_TYPE_HOST));
rossberg@chromium.org2c067b12012-03-19 11:01:52 +0000438 script->set_compilation_state(
439 Smi::FromInt(Script::COMPILATION_STATE_INITIAL));
ager@chromium.org9085a012009-05-11 19:22:57 +0000440 script->set_wrapper(*wrapper);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000441 script->set_line_ends(heap->undefined_value());
442 script->set_eval_from_shared(heap->undefined_value());
ager@chromium.orge2902be2009-06-08 12:21:35 +0000443 script->set_eval_from_instructions_offset(Smi::FromInt(0));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000444
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000445 return script;
446}
447
448
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000449Handle<Foreign> Factory::NewForeign(Address addr, PretenureFlag pretenure) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000450 CALL_HEAP_FUNCTION(isolate(),
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000451 isolate()->heap()->AllocateForeign(addr, pretenure),
452 Foreign);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000453}
454
455
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000456Handle<Foreign> Factory::NewForeign(const AccessorDescriptor* desc) {
457 return NewForeign((Address) desc, TENURED);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000458}
459
460
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000461Handle<ByteArray> Factory::NewByteArray(int length, PretenureFlag pretenure) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000462 ASSERT(0 <= length);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000463 CALL_HEAP_FUNCTION(
464 isolate(),
465 isolate()->heap()->AllocateByteArray(length, pretenure),
466 ByteArray);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000467}
468
469
ager@chromium.org3811b432009-10-28 14:53:37 +0000470Handle<ExternalArray> Factory::NewExternalArray(int length,
471 ExternalArrayType array_type,
472 void* external_pointer,
473 PretenureFlag pretenure) {
474 ASSERT(0 <= length);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000475 CALL_HEAP_FUNCTION(
476 isolate(),
477 isolate()->heap()->AllocateExternalArray(length,
478 array_type,
479 external_pointer,
480 pretenure),
481 ExternalArray);
ager@chromium.org3811b432009-10-28 14:53:37 +0000482}
483
484
danno@chromium.org41728482013-06-12 22:31:22 +0000485Handle<Cell> Factory::NewCell(Handle<Object> value) {
486 AllowDeferredHandleDereference convert_to_cell;
487 CALL_HEAP_FUNCTION(
488 isolate(),
489 isolate()->heap()->AllocateCell(*value),
490 Cell);
491}
492
493
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000494Handle<JSGlobalPropertyCell> Factory::NewJSGlobalPropertyCell(
495 Handle<Object> value) {
rossberg@chromium.org79e79022013-06-03 15:43:46 +0000496 AllowDeferredHandleDereference convert_to_cell;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000497 CALL_HEAP_FUNCTION(
498 isolate(),
499 isolate()->heap()->AllocateJSGlobalPropertyCell(*value),
500 JSGlobalPropertyCell);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000501}
502
503
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000504Handle<Map> Factory::NewMap(InstanceType type,
505 int instance_size,
506 ElementsKind elements_kind) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000507 CALL_HEAP_FUNCTION(
508 isolate(),
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000509 isolate()->heap()->AllocateMap(type, instance_size, elements_kind),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000510 Map);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000511}
512
513
514Handle<JSObject> Factory::NewFunctionPrototype(Handle<JSFunction> function) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000515 CALL_HEAP_FUNCTION(
516 isolate(),
517 isolate()->heap()->AllocateFunctionPrototype(*function),
518 JSObject);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000519}
520
521
rossberg@chromium.org657d53b2012-07-12 11:06:03 +0000522Handle<Map> Factory::CopyWithPreallocatedFieldDescriptors(Handle<Map> src) {
523 CALL_HEAP_FUNCTION(
524 isolate(), src->CopyWithPreallocatedFieldDescriptors(), Map);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000525}
526
527
ager@chromium.org32912102009-01-16 10:38:43 +0000528Handle<Map> Factory::CopyMap(Handle<Map> src,
529 int extra_inobject_properties) {
rossberg@chromium.org657d53b2012-07-12 11:06:03 +0000530 Handle<Map> copy = CopyWithPreallocatedFieldDescriptors(src);
ager@chromium.org32912102009-01-16 10:38:43 +0000531 // Check that we do not overflow the instance size when adding the
532 // extra inobject properties.
533 int instance_size_delta = extra_inobject_properties * kPointerSize;
534 int max_instance_size_delta =
535 JSObject::kMaxInstanceSize - copy->instance_size();
536 if (instance_size_delta > max_instance_size_delta) {
537 // If the instance size overflows, we allocate as many properties
538 // as we can as inobject properties.
539 instance_size_delta = max_instance_size_delta;
540 extra_inobject_properties = max_instance_size_delta >> kPointerSizeLog2;
541 }
542 // Adjust the map with the extra inobject properties.
543 int inobject_properties =
544 copy->inobject_properties() + extra_inobject_properties;
545 copy->set_inobject_properties(inobject_properties);
546 copy->set_unused_property_fields(inobject_properties);
547 copy->set_instance_size(copy->instance_size() + instance_size_delta);
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000548 copy->set_visitor_id(StaticVisitorBase::GetVisitorId(*copy));
ager@chromium.org32912102009-01-16 10:38:43 +0000549 return copy;
550}
551
fschneider@chromium.org40b9da32010-06-28 11:29:21 +0000552
verwaest@chromium.org753aee42012-07-17 16:15:42 +0000553Handle<Map> Factory::CopyMap(Handle<Map> src) {
verwaest@chromium.orgde64f722012-08-16 15:44:54 +0000554 CALL_HEAP_FUNCTION(isolate(), src->Copy(), Map);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000555}
556
557
kmillikin@chromium.org83e16822011-09-13 08:21:47 +0000558Handle<Map> Factory::GetElementsTransitionMap(
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000559 Handle<JSObject> src,
560 ElementsKind elements_kind) {
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000561 Isolate* i = isolate();
562 CALL_HEAP_FUNCTION(i,
563 src->GetElementsTransitionMap(i, elements_kind),
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000564 Map);
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +0000565}
566
567
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000568Handle<FixedArray> Factory::CopyFixedArray(Handle<FixedArray> array) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000569 CALL_HEAP_FUNCTION(isolate(), array->Copy(), FixedArray);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000570}
571
572
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +0000573Handle<FixedArray> Factory::CopySizeFixedArray(Handle<FixedArray> array,
574 int new_length) {
575 CALL_HEAP_FUNCTION(isolate(), array->CopySize(new_length), FixedArray);
576}
577
578
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000579Handle<FixedDoubleArray> Factory::CopyFixedDoubleArray(
580 Handle<FixedDoubleArray> array) {
581 CALL_HEAP_FUNCTION(isolate(), array->Copy(), FixedDoubleArray);
582}
583
584
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000585Handle<JSFunction> Factory::BaseNewFunctionFromSharedFunctionInfo(
586 Handle<SharedFunctionInfo> function_info,
sgjesse@chromium.org846fb742009-12-18 08:56:33 +0000587 Handle<Map> function_map,
588 PretenureFlag pretenure) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000589 CALL_HEAP_FUNCTION(
590 isolate(),
591 isolate()->heap()->AllocateFunction(*function_map,
592 *function_info,
593 isolate()->heap()->the_hole_value(),
594 pretenure),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000595 JSFunction);
596}
597
598
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000599static Handle<Map> MapForNewFunction(Isolate *isolate,
600 Handle<SharedFunctionInfo> function_info) {
601 Context *context = isolate->context()->native_context();
602 int map_index = Context::FunctionMapIndex(function_info->language_mode(),
603 function_info->is_generator());
604 return Handle<Map>(Map::cast(context->get(map_index)));
605}
606
607
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000608Handle<JSFunction> Factory::NewFunctionFromSharedFunctionInfo(
609 Handle<SharedFunctionInfo> function_info,
sgjesse@chromium.org846fb742009-12-18 08:56:33 +0000610 Handle<Context> context,
611 PretenureFlag pretenure) {
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000612 Handle<JSFunction> result = BaseNewFunctionFromSharedFunctionInfo(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000613 function_info,
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000614 MapForNewFunction(isolate(), function_info),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000615 pretenure);
616
jkummerow@chromium.org1456e702012-03-30 08:38:13 +0000617 if (function_info->ic_age() != isolate()->heap()->global_ic_age()) {
618 function_info->ResetForNewContext(isolate()->heap()->global_ic_age());
619 }
620
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000621 result->set_context(*context);
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000622
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000623 int index = function_info->SearchOptimizedCodeMap(context->native_context());
ulan@chromium.orgd9e468a2012-06-25 09:47:40 +0000624 if (!function_info->bound() && index < 0) {
625 int number_of_literals = function_info->num_literals();
626 Handle<FixedArray> literals = NewFixedArray(number_of_literals, pretenure);
627 if (number_of_literals > 0) {
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000628 // Store the native context in the literals array prefix. This
ulan@chromium.orgd9e468a2012-06-25 09:47:40 +0000629 // context will be used when creating object, regexp and array
630 // literals in this function.
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000631 literals->set(JSFunction::kLiteralNativeContextIndex,
632 context->native_context());
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000633 }
ulan@chromium.orgd9e468a2012-06-25 09:47:40 +0000634 result->set_literals(*literals);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000635 }
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000636
637 if (index > 0) {
638 // Caching of optimized code enabled and optimized code found.
ulan@chromium.orgd9e468a2012-06-25 09:47:40 +0000639 function_info->InstallFromOptimizedCodeMap(*result, index);
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000640 return result;
641 }
642
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000643 if (V8::UseCrankshaft() &&
644 FLAG_always_opt &&
645 result->is_compiled() &&
646 !function_info->is_toplevel() &&
rossberg@chromium.org2c067b12012-03-19 11:01:52 +0000647 function_info->allows_lazy_compilation() &&
648 !function_info->optimization_disabled()) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000649 result->MarkForLazyRecompilation();
650 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000651 return result;
652}
653
654
655Handle<Object> Factory::NewNumber(double value,
656 PretenureFlag pretenure) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000657 CALL_HEAP_FUNCTION(
658 isolate(),
659 isolate()->heap()->NumberFromDouble(value, pretenure), Object);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000660}
661
662
erikcorry0ad885c2011-11-21 13:51:57 +0000663Handle<Object> Factory::NewNumberFromInt(int32_t value,
664 PretenureFlag pretenure) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000665 CALL_HEAP_FUNCTION(
666 isolate(),
erikcorry0ad885c2011-11-21 13:51:57 +0000667 isolate()->heap()->NumberFromInt32(value, pretenure), Object);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000668}
669
670
erikcorry0ad885c2011-11-21 13:51:57 +0000671Handle<Object> Factory::NewNumberFromUint(uint32_t value,
672 PretenureFlag pretenure) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000673 CALL_HEAP_FUNCTION(
674 isolate(),
erikcorry0ad885c2011-11-21 13:51:57 +0000675 isolate()->heap()->NumberFromUint32(value, pretenure), Object);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000676}
677
678
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000679Handle<HeapNumber> Factory::NewHeapNumber(double value,
680 PretenureFlag pretenure) {
681 CALL_HEAP_FUNCTION(
682 isolate(),
683 isolate()->heap()->AllocateHeapNumber(value, pretenure), HeapNumber);
684}
685
686
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000687Handle<JSObject> Factory::NewNeanderObject() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000688 CALL_HEAP_FUNCTION(
689 isolate(),
690 isolate()->heap()->AllocateJSObjectFromMap(
691 isolate()->heap()->neander_map()),
692 JSObject);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000693}
694
695
ulan@chromium.orgdfe53072013-06-06 14:14:51 +0000696Handle<Object> Factory::NewTypeError(const char* message,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000697 Vector< Handle<Object> > args) {
ulan@chromium.orgdfe53072013-06-06 14:14:51 +0000698 return NewError("MakeTypeError", message, args);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000699}
700
701
702Handle<Object> Factory::NewTypeError(Handle<String> message) {
703 return NewError("$TypeError", message);
704}
705
706
ulan@chromium.orgdfe53072013-06-06 14:14:51 +0000707Handle<Object> Factory::NewRangeError(const char* message,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000708 Vector< Handle<Object> > args) {
ulan@chromium.orgdfe53072013-06-06 14:14:51 +0000709 return NewError("MakeRangeError", message, args);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000710}
711
712
713Handle<Object> Factory::NewRangeError(Handle<String> message) {
714 return NewError("$RangeError", message);
715}
716
717
ulan@chromium.orgdfe53072013-06-06 14:14:51 +0000718Handle<Object> Factory::NewSyntaxError(const char* message,
719 Handle<JSArray> args) {
720 return NewError("MakeSyntaxError", message, args);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000721}
722
723
724Handle<Object> Factory::NewSyntaxError(Handle<String> message) {
725 return NewError("$SyntaxError", message);
726}
727
728
ulan@chromium.orgdfe53072013-06-06 14:14:51 +0000729Handle<Object> Factory::NewReferenceError(const char* message,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000730 Vector< Handle<Object> > args) {
ulan@chromium.orgdfe53072013-06-06 14:14:51 +0000731 return NewError("MakeReferenceError", message, args);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000732}
733
734
735Handle<Object> Factory::NewReferenceError(Handle<String> message) {
736 return NewError("$ReferenceError", message);
737}
738
739
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +0000740Handle<Object> Factory::NewError(const char* maker,
ulan@chromium.orgdfe53072013-06-06 14:14:51 +0000741 const char* message,
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +0000742 Vector< Handle<Object> > args) {
743 // Instantiate a closeable HandleScope for EscapeFrom.
744 v8::HandleScope scope(reinterpret_cast<v8::Isolate*>(isolate()));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000745 Handle<FixedArray> array = NewFixedArray(args.length());
ager@chromium.org7c537e22008-10-16 08:43:32 +0000746 for (int i = 0; i < args.length(); i++) {
747 array->set(i, *args[i]);
748 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000749 Handle<JSArray> object = NewJSArrayWithElements(array);
ulan@chromium.orgdfe53072013-06-06 14:14:51 +0000750 Handle<Object> result = NewError(maker, message, object);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000751 return result.EscapeFrom(&scope);
752}
753
754
ulan@chromium.orgdfe53072013-06-06 14:14:51 +0000755Handle<Object> Factory::NewEvalError(const char* message,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000756 Vector< Handle<Object> > args) {
ulan@chromium.orgdfe53072013-06-06 14:14:51 +0000757 return NewError("MakeEvalError", message, args);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000758}
759
760
ulan@chromium.orgdfe53072013-06-06 14:14:51 +0000761Handle<Object> Factory::NewError(const char* message,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000762 Vector< Handle<Object> > args) {
ulan@chromium.orgdfe53072013-06-06 14:14:51 +0000763 return NewError("MakeError", message, args);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000764}
765
766
ulan@chromium.orgdfe53072013-06-06 14:14:51 +0000767Handle<String> Factory::EmergencyNewError(const char* message,
verwaest@chromium.org37141392012-05-31 13:27:02 +0000768 Handle<JSArray> args) {
769 const int kBufferSize = 1000;
770 char buffer[kBufferSize];
771 size_t space = kBufferSize;
772 char* p = &buffer[0];
773
774 Vector<char> v(buffer, kBufferSize);
ulan@chromium.orgdfe53072013-06-06 14:14:51 +0000775 OS::StrNCpy(v, message, space);
776 space -= Min(space, strlen(message));
verwaest@chromium.org37141392012-05-31 13:27:02 +0000777 p = &buffer[kBufferSize] - space;
778
779 for (unsigned i = 0; i < ARRAY_SIZE(args); i++) {
780 if (space > 0) {
781 *p++ = ' ';
782 space--;
783 if (space > 0) {
784 MaybeObject* maybe_arg = args->GetElement(i);
785 Handle<String> arg_str(reinterpret_cast<String*>(maybe_arg));
786 const char* arg = *arg_str->ToCString();
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000787 Vector<char> v2(p, static_cast<int>(space));
verwaest@chromium.org37141392012-05-31 13:27:02 +0000788 OS::StrNCpy(v2, arg, space);
789 space -= Min(space, strlen(arg));
790 p = &buffer[kBufferSize] - space;
791 }
792 }
793 }
794 if (space > 0) {
795 *p = '\0';
796 } else {
797 buffer[kBufferSize - 1] = '\0';
798 }
799 Handle<String> error_string = NewStringFromUtf8(CStrVector(buffer), TENURED);
800 return error_string;
801}
802
803
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000804Handle<Object> Factory::NewError(const char* maker,
ulan@chromium.orgdfe53072013-06-06 14:14:51 +0000805 const char* message,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000806 Handle<JSArray> args) {
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000807 Handle<String> make_str = InternalizeUtf8String(maker);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000808 Handle<Object> fun_obj(
ulan@chromium.org09d7ab52013-02-25 15:50:35 +0000809 isolate()->js_builtins_object()->GetPropertyNoExceptionThrown(*make_str),
810 isolate());
ager@chromium.org4af710e2009-09-15 12:20:11 +0000811 // If the builtins haven't been properly configured yet this error
812 // constructor may not have been defined. Bail out.
verwaest@chromium.org37141392012-05-31 13:27:02 +0000813 if (!fun_obj->IsJSFunction()) {
ulan@chromium.orgdfe53072013-06-06 14:14:51 +0000814 return EmergencyNewError(message, args);
verwaest@chromium.org37141392012-05-31 13:27:02 +0000815 }
ager@chromium.org4af710e2009-09-15 12:20:11 +0000816 Handle<JSFunction> fun = Handle<JSFunction>::cast(fun_obj);
ulan@chromium.orgdfe53072013-06-06 14:14:51 +0000817 Handle<Object> message_obj = InternalizeUtf8String(message);
818 Handle<Object> argv[] = { message_obj, args };
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<Object> Factory::NewError(Handle<String> message) {
833 return NewError("$Error", message);
834}
835
836
837Handle<Object> Factory::NewError(const char* constructor,
838 Handle<String> message) {
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000839 Handle<String> constr = InternalizeUtf8String(constructor);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000840 Handle<JSFunction> fun = Handle<JSFunction>(
841 JSFunction::cast(isolate()->js_builtins_object()->
842 GetPropertyNoExceptionThrown(*constr)));
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +0000843 Handle<Object> argv[] = { message };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000844
845 // Invoke the JavaScript factory method. If an exception is thrown while
846 // running the factory method, use the exception as the result.
847 bool caught_exception;
848 Handle<Object> result = Execution::TryCall(fun,
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +0000849 isolate()->js_builtins_object(),
850 ARRAY_SIZE(argv),
851 argv,
852 &caught_exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000853 return result;
854}
855
856
857Handle<JSFunction> Factory::NewFunction(Handle<String> name,
858 InstanceType type,
859 int instance_size,
860 Handle<Code> code,
861 bool force_initial_map) {
862 // Allocate the function
863 Handle<JSFunction> function = NewFunction(name, the_hole_value());
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000864
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000865 // Set up the code pointer in both the shared function info and in
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000866 // the function itself.
867 function->shared()->set_code(*code);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000868 function->set_code(*code);
869
870 if (force_initial_map ||
871 type != JS_OBJECT_TYPE ||
872 instance_size != JSObject::kHeaderSize) {
873 Handle<Map> initial_map = NewMap(type, instance_size);
874 Handle<JSObject> prototype = NewFunctionPrototype(function);
875 initial_map->set_prototype(*prototype);
876 function->set_initial_map(*initial_map);
877 initial_map->set_constructor(*function);
878 } else {
879 ASSERT(!function->has_initial_map());
880 ASSERT(!function->has_prototype());
881 }
882
883 return function;
884}
885
886
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000887Handle<JSFunction> Factory::NewFunctionWithPrototype(Handle<String> name,
888 InstanceType type,
889 int instance_size,
890 Handle<JSObject> prototype,
891 Handle<Code> code,
892 bool force_initial_map) {
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000893 // Allocate the function.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000894 Handle<JSFunction> function = NewFunction(name, prototype);
895
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000896 // Set up the code pointer in both the shared function info and in
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000897 // the function itself.
898 function->shared()->set_code(*code);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000899 function->set_code(*code);
900
901 if (force_initial_map ||
902 type != JS_OBJECT_TYPE ||
903 instance_size != JSObject::kHeaderSize) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000904 Handle<Map> initial_map = NewMap(type,
905 instance_size,
svenpanne@chromium.org830d30c2012-05-29 13:20:14 +0000906 GetInitialFastElementsKind());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000907 function->set_initial_map(*initial_map);
908 initial_map->set_constructor(*function);
909 }
910
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000911 SetPrototypeProperty(function, prototype);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000912 return function;
913}
914
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000915
kmillikin@chromium.org4111b802010-05-03 10:34:42 +0000916Handle<JSFunction> Factory::NewFunctionWithoutPrototype(Handle<String> name,
917 Handle<Code> code) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000918 Handle<JSFunction> function = NewFunctionWithoutPrototype(name,
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000919 CLASSIC_MODE);
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000920 function->shared()->set_code(*code);
kmillikin@chromium.org4111b802010-05-03 10:34:42 +0000921 function->set_code(*code);
922 ASSERT(!function->has_initial_map());
923 ASSERT(!function->has_prototype());
924 return function;
925}
926
927
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000928Handle<ScopeInfo> Factory::NewScopeInfo(int length) {
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000929 CALL_HEAP_FUNCTION(
930 isolate(),
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000931 isolate()->heap()->AllocateScopeInfo(length),
932 ScopeInfo);
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000933}
934
935
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +0000936Handle<JSObject> Factory::NewExternal(void* value) {
937 CALL_HEAP_FUNCTION(isolate(),
938 isolate()->heap()->AllocateExternal(value),
939 JSObject);
940}
941
942
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000943Handle<Code> Factory::NewCode(const CodeDesc& desc,
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000944 Code::Flags flags,
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +0000945 Handle<Object> self_ref,
mstarzinger@chromium.orgb228be02013-04-18 14:56:59 +0000946 bool immovable,
947 bool crankshafted) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000948 CALL_HEAP_FUNCTION(isolate(),
949 isolate()->heap()->CreateCode(
mstarzinger@chromium.orgb228be02013-04-18 14:56:59 +0000950 desc, flags, self_ref, immovable, crankshafted),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000951 Code);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000952}
953
954
955Handle<Code> Factory::CopyCode(Handle<Code> code) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000956 CALL_HEAP_FUNCTION(isolate(),
957 isolate()->heap()->CopyCode(*code),
958 Code);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000959}
960
961
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000962Handle<Code> Factory::CopyCode(Handle<Code> code, Vector<byte> reloc_info) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000963 CALL_HEAP_FUNCTION(isolate(),
964 isolate()->heap()->CopyCode(*code, reloc_info),
965 Code);
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000966}
967
968
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000969Handle<String> Factory::InternalizedStringFromString(Handle<String> value) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000970 CALL_HEAP_FUNCTION(isolate(),
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000971 isolate()->heap()->InternalizeString(*value), String);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000972}
973
974
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000975Handle<JSObject> Factory::NewJSObject(Handle<JSFunction> constructor,
976 PretenureFlag pretenure) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000977 CALL_HEAP_FUNCTION(
978 isolate(),
979 isolate()->heap()->AllocateJSObject(*constructor, pretenure), JSObject);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000980}
981
982
danno@chromium.org81cac2b2012-07-10 11:28:27 +0000983Handle<JSModule> Factory::NewJSModule(Handle<Context> context,
984 Handle<ScopeInfo> scope_info) {
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000985 CALL_HEAP_FUNCTION(
986 isolate(),
danno@chromium.org81cac2b2012-07-10 11:28:27 +0000987 isolate()->heap()->AllocateJSModule(*context, *scope_info), JSModule);
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000988}
989
990
kasperl@chromium.org2abc4502009-07-02 07:00:29 +0000991Handle<GlobalObject> Factory::NewGlobalObject(
992 Handle<JSFunction> constructor) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000993 CALL_HEAP_FUNCTION(isolate(),
994 isolate()->heap()->AllocateGlobalObject(*constructor),
kasperl@chromium.org2abc4502009-07-02 07:00:29 +0000995 GlobalObject);
996}
997
998
999
mstarzinger@chromium.org71fc3462013-02-27 09:34:27 +00001000Handle<JSObject> Factory::NewJSObjectFromMap(Handle<Map> map,
1001 PretenureFlag pretenure) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001002 CALL_HEAP_FUNCTION(
1003 isolate(),
mstarzinger@chromium.org71fc3462013-02-27 09:34:27 +00001004 isolate()->heap()->AllocateJSObjectFromMap(*map, pretenure),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001005 JSObject);
ager@chromium.org236ad962008-09-25 09:45:57 +00001006}
1007
1008
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +00001009Handle<JSArray> Factory::NewJSArray(int capacity,
danno@chromium.orgfa458e42012-02-01 10:48:36 +00001010 ElementsKind elements_kind,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001011 PretenureFlag pretenure) {
ulan@chromium.org8e8d8822012-11-23 14:36:46 +00001012 if (capacity != 0) {
1013 elements_kind = GetHoleyElementsKind(elements_kind);
1014 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001015 CALL_HEAP_FUNCTION(isolate(),
danno@chromium.orgfa458e42012-02-01 10:48:36 +00001016 isolate()->heap()->AllocateJSArrayAndStorage(
1017 elements_kind,
1018 0,
1019 capacity,
1020 INITIALIZE_ARRAY_ELEMENTS_WITH_HOLE,
1021 pretenure),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001022 JSArray);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001023}
1024
1025
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00001026Handle<JSArray> Factory::NewJSArrayWithElements(Handle<FixedArrayBase> elements,
danno@chromium.orgfa458e42012-02-01 10:48:36 +00001027 ElementsKind elements_kind,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001028 PretenureFlag pretenure) {
danno@chromium.orgfa458e42012-02-01 10:48:36 +00001029 CALL_HEAP_FUNCTION(
1030 isolate(),
1031 isolate()->heap()->AllocateJSArrayWithElements(*elements,
1032 elements_kind,
yangguo@chromium.orgfb377212012-11-16 14:43:43 +00001033 elements->length(),
danno@chromium.orgfa458e42012-02-01 10:48:36 +00001034 pretenure),
1035 JSArray);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001036}
1037
1038
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00001039void Factory::SetElementsCapacityAndLength(Handle<JSArray> array,
1040 int capacity,
1041 int length) {
1042 ElementsAccessor* accessor = array->GetElementsAccessor();
1043 CALL_HEAP_FUNCTION_VOID(
1044 isolate(),
1045 accessor->SetCapacityAndLength(*array, capacity, length));
1046}
1047
1048
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001049void Factory::SetContent(Handle<JSArray> array,
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00001050 Handle<FixedArrayBase> elements) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001051 CALL_HEAP_FUNCTION_VOID(
1052 isolate(),
1053 array->SetContent(*elements));
1054}
1055
1056
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00001057void Factory::EnsureCanContainHeapObjectElements(Handle<JSArray> array) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001058 CALL_HEAP_FUNCTION_VOID(
1059 isolate(),
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00001060 array->EnsureCanContainHeapObjectElements());
1061}
1062
1063
1064void Factory::EnsureCanContainElements(Handle<JSArray> array,
1065 Handle<FixedArrayBase> elements,
svenpanne@chromium.org830d30c2012-05-29 13:20:14 +00001066 uint32_t length,
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00001067 EnsureElementsMode mode) {
1068 CALL_HEAP_FUNCTION_VOID(
1069 isolate(),
svenpanne@chromium.org830d30c2012-05-29 13:20:14 +00001070 array->EnsureCanContainElements(*elements, length, mode));
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001071}
1072
1073
danno@chromium.orgca29dd82013-04-26 11:59:48 +00001074Handle<JSArrayBuffer> Factory::NewJSArrayBuffer() {
1075 JSFunction* array_buffer_fun =
1076 isolate()->context()->native_context()->array_buffer_fun();
1077 CALL_HEAP_FUNCTION(
1078 isolate(),
1079 isolate()->heap()->AllocateJSObject(array_buffer_fun),
1080 JSArrayBuffer);
1081}
1082
1083
danno@chromium.orgf005df62013-04-30 16:36:45 +00001084Handle<JSTypedArray> Factory::NewJSTypedArray(ExternalArrayType type) {
1085 JSFunction* typed_array_fun;
1086 Context* native_context = isolate()->context()->native_context();
1087 switch (type) {
1088 case kExternalUnsignedByteArray:
1089 typed_array_fun = native_context->uint8_array_fun();
1090 break;
1091
1092 case kExternalByteArray:
1093 typed_array_fun = native_context->int8_array_fun();
1094 break;
1095
1096 case kExternalUnsignedShortArray:
1097 typed_array_fun = native_context->uint16_array_fun();
1098 break;
1099
1100 case kExternalShortArray:
1101 typed_array_fun = native_context->int16_array_fun();
1102 break;
1103
1104 case kExternalUnsignedIntArray:
1105 typed_array_fun = native_context->uint32_array_fun();
1106 break;
1107
1108 case kExternalIntArray:
1109 typed_array_fun = native_context->int32_array_fun();
1110 break;
1111
1112 case kExternalFloatArray:
1113 typed_array_fun = native_context->float_array_fun();
1114 break;
1115
1116 case kExternalDoubleArray:
1117 typed_array_fun = native_context->double_array_fun();
1118 break;
1119
ulan@chromium.org57ff8812013-05-10 08:16:55 +00001120 case kExternalPixelArray:
1121 typed_array_fun = native_context->uint8c_array_fun();
1122 break;
1123
danno@chromium.orgf005df62013-04-30 16:36:45 +00001124 default:
1125 UNREACHABLE();
1126 return Handle<JSTypedArray>();
1127 }
1128
1129 CALL_HEAP_FUNCTION(
1130 isolate(),
1131 isolate()->heap()->AllocateJSObject(typed_array_fun),
1132 JSTypedArray);
1133}
1134
1135
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001136Handle<JSProxy> Factory::NewJSProxy(Handle<Object> handler,
1137 Handle<Object> prototype) {
1138 CALL_HEAP_FUNCTION(
1139 isolate(),
1140 isolate()->heap()->AllocateJSProxy(*handler, *prototype),
1141 JSProxy);
1142}
1143
1144
lrn@chromium.org34e60782011-09-15 07:25:40 +00001145void Factory::BecomeJSObject(Handle<JSReceiver> object) {
rossberg@chromium.org717967f2011-07-20 13:44:42 +00001146 CALL_HEAP_FUNCTION_VOID(
1147 isolate(),
lrn@chromium.org34e60782011-09-15 07:25:40 +00001148 isolate()->heap()->ReinitializeJSReceiver(
1149 *object, JS_OBJECT_TYPE, JSObject::kHeaderSize));
1150}
1151
1152
1153void Factory::BecomeJSFunction(Handle<JSReceiver> object) {
1154 CALL_HEAP_FUNCTION_VOID(
1155 isolate(),
1156 isolate()->heap()->ReinitializeJSReceiver(
1157 *object, JS_FUNCTION_TYPE, JSFunction::kSize));
rossberg@chromium.org717967f2011-07-20 13:44:42 +00001158}
1159
1160
jkummerow@chromium.org000f7fb2012-08-01 11:14:42 +00001161void Factory::SetIdentityHash(Handle<JSObject> object, Smi* hash) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001162 CALL_HEAP_FUNCTION_VOID(
1163 isolate(),
1164 object->SetIdentityHash(hash, ALLOW_CREATION));
1165}
1166
1167
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001168Handle<SharedFunctionInfo> Factory::NewSharedFunctionInfo(
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00001169 Handle<String> name,
1170 int number_of_literals,
mstarzinger@chromium.orgb228be02013-04-18 14:56:59 +00001171 bool is_generator,
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00001172 Handle<Code> code,
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001173 Handle<ScopeInfo> scope_info) {
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001174 Handle<SharedFunctionInfo> shared = NewSharedFunctionInfo(name);
1175 shared->set_code(*code);
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00001176 shared->set_scope_info(*scope_info);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001177 int literals_array_size = number_of_literals;
1178 // If the function contains object, regexp or array literals,
1179 // allocate extra space for a literals array prefix containing the
1180 // context.
1181 if (number_of_literals > 0) {
1182 literals_array_size += JSFunction::kLiteralsPrefixSize;
1183 }
1184 shared->set_num_literals(literals_array_size);
mstarzinger@chromium.orgb228be02013-04-18 14:56:59 +00001185 if (is_generator) {
1186 shared->set_instance_class_name(isolate()->heap()->Generator_string());
1187 }
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001188 return shared;
1189}
1190
1191
kmillikin@chromium.org31b12772011-02-02 16:08:26 +00001192Handle<JSMessageObject> Factory::NewJSMessageObject(
1193 Handle<String> type,
1194 Handle<JSArray> arguments,
1195 int start_position,
1196 int end_position,
1197 Handle<Object> script,
1198 Handle<Object> stack_trace,
1199 Handle<Object> stack_frames) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001200 CALL_HEAP_FUNCTION(isolate(),
1201 isolate()->heap()->AllocateJSMessageObject(*type,
1202 *arguments,
1203 start_position,
1204 end_position,
1205 *script,
1206 *stack_trace,
1207 *stack_frames),
kmillikin@chromium.org31b12772011-02-02 16:08:26 +00001208 JSMessageObject);
1209}
1210
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001211Handle<SharedFunctionInfo> Factory::NewSharedFunctionInfo(Handle<String> name) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001212 CALL_HEAP_FUNCTION(isolate(),
1213 isolate()->heap()->AllocateSharedFunctionInfo(*name),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001214 SharedFunctionInfo);
1215}
1216
1217
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00001218Handle<String> Factory::NumberToString(Handle<Object> number) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001219 CALL_HEAP_FUNCTION(isolate(),
1220 isolate()->heap()->NumberToString(*number), String);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00001221}
1222
1223
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001224Handle<String> Factory::Uint32ToString(uint32_t value) {
1225 CALL_HEAP_FUNCTION(isolate(),
1226 isolate()->heap()->Uint32ToString(value), String);
1227}
1228
1229
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001230Handle<SeededNumberDictionary> Factory::DictionaryAtNumberPut(
1231 Handle<SeededNumberDictionary> dictionary,
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00001232 uint32_t key,
1233 Handle<Object> value) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001234 CALL_HEAP_FUNCTION(isolate(),
1235 dictionary->AtNumberPut(key, *value),
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001236 SeededNumberDictionary);
1237}
1238
1239
1240Handle<UnseededNumberDictionary> Factory::DictionaryAtNumberPut(
1241 Handle<UnseededNumberDictionary> dictionary,
1242 uint32_t key,
1243 Handle<Object> value) {
1244 CALL_HEAP_FUNCTION(isolate(),
1245 dictionary->AtNumberPut(key, *value),
1246 UnseededNumberDictionary);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001247}
1248
1249
1250Handle<JSFunction> Factory::NewFunctionHelper(Handle<String> name,
1251 Handle<Object> prototype) {
1252 Handle<SharedFunctionInfo> function_share = NewSharedFunctionInfo(name);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001253 CALL_HEAP_FUNCTION(
1254 isolate(),
1255 isolate()->heap()->AllocateFunction(*isolate()->function_map(),
1256 *function_share,
1257 *prototype),
1258 JSFunction);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001259}
1260
1261
1262Handle<JSFunction> Factory::NewFunction(Handle<String> name,
1263 Handle<Object> prototype) {
1264 Handle<JSFunction> fun = NewFunctionHelper(name, prototype);
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001265 fun->set_context(isolate()->context()->native_context());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001266 return fun;
1267}
1268
1269
kmillikin@chromium.org4111b802010-05-03 10:34:42 +00001270Handle<JSFunction> Factory::NewFunctionWithoutPrototypeHelper(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001271 Handle<String> name,
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00001272 LanguageMode language_mode) {
kmillikin@chromium.org4111b802010-05-03 10:34:42 +00001273 Handle<SharedFunctionInfo> function_share = NewSharedFunctionInfo(name);
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00001274 Handle<Map> map = (language_mode == CLASSIC_MODE)
1275 ? isolate()->function_without_prototype_map()
1276 : isolate()->strict_mode_function_without_prototype_map();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001277 CALL_HEAP_FUNCTION(isolate(),
1278 isolate()->heap()->AllocateFunction(
1279 *map,
kmillikin@chromium.org4111b802010-05-03 10:34:42 +00001280 *function_share,
1281 *the_hole_value()),
1282 JSFunction);
1283}
1284
1285
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001286Handle<JSFunction> Factory::NewFunctionWithoutPrototype(
1287 Handle<String> name,
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00001288 LanguageMode language_mode) {
1289 Handle<JSFunction> fun =
1290 NewFunctionWithoutPrototypeHelper(name, language_mode);
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001291 fun->set_context(isolate()->context()->native_context());
kmillikin@chromium.org4111b802010-05-03 10:34:42 +00001292 return fun;
1293}
1294
1295
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001296Handle<Object> Factory::ToObject(Handle<Object> object) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001297 CALL_HEAP_FUNCTION(isolate(), object->ToObject(), Object);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001298}
1299
1300
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001301Handle<Object> Factory::ToObject(Handle<Object> object,
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001302 Handle<Context> native_context) {
1303 CALL_HEAP_FUNCTION(isolate(), object->ToObject(*native_context), Object);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001304}
1305
1306
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001307#ifdef ENABLE_DEBUGGER_SUPPORT
v8.team.kasperl727e9952008-09-02 14:56:44 +00001308Handle<DebugInfo> Factory::NewDebugInfo(Handle<SharedFunctionInfo> shared) {
1309 // Get the original code of the function.
1310 Handle<Code> code(shared->code());
1311
1312 // Create a copy of the code before allocating the debug info object to avoid
1313 // allocation while setting up the debug info object.
1314 Handle<Code> original_code(*Factory::CopyCode(code));
1315
1316 // Allocate initial fixed array for active break points before allocating the
1317 // debug info object to avoid allocation while setting up the debug info
1318 // object.
1319 Handle<FixedArray> break_points(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001320 NewFixedArray(Debug::kEstimatedNofBreakPointsInFunction));
v8.team.kasperl727e9952008-09-02 14:56:44 +00001321
1322 // Create and set up the debug info object. Debug info contains function, a
1323 // copy of the original code, the executing code and initial fixed array for
1324 // active break points.
1325 Handle<DebugInfo> debug_info =
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001326 Handle<DebugInfo>::cast(NewStruct(DEBUG_INFO_TYPE));
v8.team.kasperl727e9952008-09-02 14:56:44 +00001327 debug_info->set_shared(*shared);
1328 debug_info->set_original_code(*original_code);
1329 debug_info->set_code(*code);
1330 debug_info->set_break_points(*break_points);
1331
1332 // Link debug info to function.
1333 shared->set_debug_info(*debug_info);
1334
1335 return debug_info;
1336}
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001337#endif
v8.team.kasperl727e9952008-09-02 14:56:44 +00001338
1339
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001340Handle<JSObject> Factory::NewArgumentsObject(Handle<Object> callee,
1341 int length) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001342 CALL_HEAP_FUNCTION(
1343 isolate(),
1344 isolate()->heap()->AllocateArgumentsObject(*callee, length), JSObject);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001345}
1346
1347
1348Handle<JSFunction> Factory::CreateApiFunction(
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001349 Handle<FunctionTemplateInfo> obj, ApiInstanceType instance_type) {
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00001350 Handle<Code> code = isolate()->builtins()->HandleApiCall();
1351 Handle<Code> construct_stub = isolate()->builtins()->JSConstructStubApi();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001352
kasper.lund212ac232008-07-16 07:07:30 +00001353 int internal_field_count = 0;
1354 if (!obj->instance_template()->IsUndefined()) {
1355 Handle<ObjectTemplateInfo> instance_template =
1356 Handle<ObjectTemplateInfo>(
1357 ObjectTemplateInfo::cast(obj->instance_template()));
1358 internal_field_count =
1359 Smi::cast(instance_template->internal_field_count())->value();
1360 }
1361
1362 int instance_size = kPointerSize * internal_field_count;
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001363 InstanceType type = INVALID_TYPE;
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001364 switch (instance_type) {
1365 case JavaScriptObject:
1366 type = JS_OBJECT_TYPE;
1367 instance_size += JSObject::kHeaderSize;
1368 break;
1369 case InnerGlobalObject:
1370 type = JS_GLOBAL_OBJECT_TYPE;
1371 instance_size += JSGlobalObject::kSize;
1372 break;
1373 case OuterGlobalObject:
1374 type = JS_GLOBAL_PROXY_TYPE;
1375 instance_size += JSGlobalProxy::kSize;
1376 break;
1377 default:
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001378 break;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001379 }
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001380 ASSERT(type != INVALID_TYPE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001381
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001382 Handle<JSFunction> result =
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001383 NewFunction(Factory::empty_string(),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001384 type,
1385 instance_size,
1386 code,
1387 true);
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00001388
1389 // Set length.
1390 result->shared()->set_length(obj->length());
1391
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001392 // Set class name.
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00001393 Handle<Object> class_name = Handle<Object>(obj->class_name(), isolate());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001394 if (class_name->IsString()) {
1395 result->shared()->set_instance_class_name(*class_name);
1396 result->shared()->set_name(*class_name);
1397 }
1398
1399 Handle<Map> map = Handle<Map>(result->initial_map());
1400
1401 // Mark as undetectable if needed.
1402 if (obj->undetectable()) {
1403 map->set_is_undetectable();
1404 }
1405
1406 // Mark as hidden for the __proto__ accessor if needed.
1407 if (obj->hidden_prototype()) {
1408 map->set_is_hidden_prototype();
1409 }
1410
1411 // Mark as needs_access_check if needed.
1412 if (obj->needs_access_check()) {
ager@chromium.org870a0b62008-11-04 11:43:05 +00001413 map->set_is_access_check_needed(true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001414 }
1415
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001416 // Set interceptor information in the map.
1417 if (!obj->named_property_handler()->IsUndefined()) {
1418 map->set_has_named_interceptor();
1419 }
1420 if (!obj->indexed_property_handler()->IsUndefined()) {
1421 map->set_has_indexed_interceptor();
1422 }
1423
1424 // Set instance call-as-function information in the map.
1425 if (!obj->instance_call_handler()->IsUndefined()) {
1426 map->set_has_instance_call_handler();
1427 }
1428
1429 result->shared()->set_function_data(*obj);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001430 result->shared()->set_construct_stub(*construct_stub);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001431 result->shared()->DontAdaptArguments();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001432
1433 // Recursively copy parent templates' accessors, 'data' may be modified.
verwaest@chromium.org33e09c82012-10-10 17:07:22 +00001434 int max_number_of_additional_properties = 0;
1435 FunctionTemplateInfo* info = *obj;
1436 while (true) {
1437 Object* props = info->property_accessors();
1438 if (!props->IsUndefined()) {
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00001439 Handle<Object> props_handle(props, isolate());
verwaest@chromium.org33e09c82012-10-10 17:07:22 +00001440 NeanderArray props_array(props_handle);
1441 max_number_of_additional_properties += props_array.length();
1442 }
1443 Object* parent = info->parent_template();
1444 if (parent->IsUndefined()) break;
1445 info = FunctionTemplateInfo::cast(parent);
1446 }
1447
1448 Map::EnsureDescriptorSlack(map, max_number_of_additional_properties);
1449
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001450 while (true) {
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00001451 Handle<Object> props = Handle<Object>(obj->property_accessors(),
1452 isolate());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001453 if (!props->IsUndefined()) {
verwaest@chromium.org33e09c82012-10-10 17:07:22 +00001454 Map::AppendCallbackDescriptors(map, props);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001455 }
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00001456 Handle<Object> parent = Handle<Object>(obj->parent_template(), isolate());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001457 if (parent->IsUndefined()) break;
1458 obj = Handle<FunctionTemplateInfo>::cast(parent);
1459 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001460
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001461 ASSERT(result->shared()->IsApiFunction());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001462 return result;
1463}
1464
1465
ager@chromium.org236ad962008-09-25 09:45:57 +00001466Handle<MapCache> Factory::NewMapCache(int at_least_space_for) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001467 CALL_HEAP_FUNCTION(isolate(),
ulan@chromium.org6e196bf2013-03-13 09:38:22 +00001468 MapCache::Allocate(isolate()->heap(),
1469 at_least_space_for),
1470 MapCache);
ager@chromium.org236ad962008-09-25 09:45:57 +00001471}
1472
1473
lrn@chromium.org303ada72010-10-27 09:33:13 +00001474MUST_USE_RESULT static MaybeObject* UpdateMapCacheWith(Context* context,
1475 FixedArray* keys,
1476 Map* map) {
1477 Object* result;
1478 { MaybeObject* maybe_result =
1479 MapCache::cast(context->map_cache())->Put(keys, map);
1480 if (!maybe_result->ToObject(&result)) return maybe_result;
1481 }
1482 context->set_map_cache(MapCache::cast(result));
ager@chromium.org236ad962008-09-25 09:45:57 +00001483 return result;
1484}
1485
1486
1487Handle<MapCache> Factory::AddToMapCache(Handle<Context> context,
1488 Handle<FixedArray> keys,
1489 Handle<Map> map) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001490 CALL_HEAP_FUNCTION(isolate(),
1491 UpdateMapCacheWith(*context, *keys, *map), MapCache);
ager@chromium.org236ad962008-09-25 09:45:57 +00001492}
1493
1494
1495Handle<Map> Factory::ObjectLiteralMapFromCache(Handle<Context> context,
1496 Handle<FixedArray> keys) {
1497 if (context->map_cache()->IsUndefined()) {
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001498 // Allocate the new map cache for the native context.
ager@chromium.org236ad962008-09-25 09:45:57 +00001499 Handle<MapCache> new_cache = NewMapCache(24);
1500 context->set_map_cache(*new_cache);
1501 }
ager@chromium.org32912102009-01-16 10:38:43 +00001502 // Check to see whether there is a matching element in the cache.
ager@chromium.org236ad962008-09-25 09:45:57 +00001503 Handle<MapCache> cache =
1504 Handle<MapCache>(MapCache::cast(context->map_cache()));
yangguo@chromium.orgfb377212012-11-16 14:43:43 +00001505 Handle<Object> result = Handle<Object>(cache->Lookup(*keys), isolate());
ager@chromium.org236ad962008-09-25 09:45:57 +00001506 if (result->IsMap()) return Handle<Map>::cast(result);
1507 // Create a new map and add it to the cache.
1508 Handle<Map> map =
ager@chromium.org32912102009-01-16 10:38:43 +00001509 CopyMap(Handle<Map>(context->object_function()->initial_map()),
1510 keys->length());
ager@chromium.org236ad962008-09-25 09:45:57 +00001511 AddToMapCache(context, keys, map);
1512 return Handle<Map>(map);
1513}
1514
1515
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001516void Factory::SetRegExpAtomData(Handle<JSRegExp> regexp,
1517 JSRegExp::Type type,
1518 Handle<String> source,
1519 JSRegExp::Flags flags,
1520 Handle<Object> data) {
1521 Handle<FixedArray> store = NewFixedArray(JSRegExp::kAtomDataSize);
1522
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001523 store->set(JSRegExp::kTagIndex, Smi::FromInt(type));
1524 store->set(JSRegExp::kSourceIndex, *source);
1525 store->set(JSRegExp::kFlagsIndex, Smi::FromInt(flags.value()));
1526 store->set(JSRegExp::kAtomPatternIndex, *data);
1527 regexp->set_data(*store);
1528}
1529
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001530void Factory::SetRegExpIrregexpData(Handle<JSRegExp> regexp,
1531 JSRegExp::Type type,
1532 Handle<String> source,
1533 JSRegExp::Flags flags,
1534 int capture_count) {
1535 Handle<FixedArray> store = NewFixedArray(JSRegExp::kIrregexpDataSize);
jkummerow@chromium.orgddda9e82011-07-06 11:27:02 +00001536 Smi* uninitialized = Smi::FromInt(JSRegExp::kUninitializedValue);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001537 store->set(JSRegExp::kTagIndex, Smi::FromInt(type));
1538 store->set(JSRegExp::kSourceIndex, *source);
1539 store->set(JSRegExp::kFlagsIndex, Smi::FromInt(flags.value()));
jkummerow@chromium.orgddda9e82011-07-06 11:27:02 +00001540 store->set(JSRegExp::kIrregexpASCIICodeIndex, uninitialized);
1541 store->set(JSRegExp::kIrregexpUC16CodeIndex, uninitialized);
1542 store->set(JSRegExp::kIrregexpASCIICodeSavedIndex, uninitialized);
1543 store->set(JSRegExp::kIrregexpUC16CodeSavedIndex, uninitialized);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001544 store->set(JSRegExp::kIrregexpMaxRegisterCountIndex, Smi::FromInt(0));
1545 store->set(JSRegExp::kIrregexpCaptureCountIndex,
1546 Smi::FromInt(capture_count));
1547 regexp->set_data(*store);
1548}
1549
1550
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001551
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001552void Factory::ConfigureInstance(Handle<FunctionTemplateInfo> desc,
1553 Handle<JSObject> instance,
1554 bool* pending_exception) {
1555 // Configure the instance by adding the properties specified by the
1556 // instance template.
yangguo@chromium.orgfb377212012-11-16 14:43:43 +00001557 Handle<Object> instance_template(desc->instance_template(), isolate());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001558 if (!instance_template->IsUndefined()) {
1559 Execution::ConfigureInstance(instance,
1560 instance_template,
1561 pending_exception);
1562 } else {
1563 *pending_exception = false;
1564 }
1565}
1566
1567
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001568Handle<Object> Factory::GlobalConstantFor(Handle<String> name) {
1569 Heap* h = isolate()->heap();
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001570 if (name->Equals(h->undefined_string())) return undefined_value();
1571 if (name->Equals(h->nan_string())) return nan_value();
1572 if (name->Equals(h->infinity_string())) return infinity_value();
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001573 return Handle<Object>::null();
1574}
1575
1576
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00001577Handle<Object> Factory::ToBoolean(bool value) {
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00001578 return value ? true_value() : false_value();
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00001579}
1580
1581
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001582} } // namespace v8::internal