blob: 8b842a733928f7425f73870b8c61523bc03ac84c [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
dslomov@chromium.orgb752d402013-06-18 11:54:54 +0000494Handle<PropertyCell> Factory::NewPropertyCell(Handle<Object> value) {
rossberg@chromium.org79e79022013-06-03 15:43:46 +0000495 AllowDeferredHandleDereference convert_to_cell;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000496 CALL_HEAP_FUNCTION(
497 isolate(),
dslomov@chromium.orgb752d402013-06-18 11:54:54 +0000498 isolate()->heap()->AllocatePropertyCell(*value),
499 PropertyCell);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000500}
501
502
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000503Handle<Map> Factory::NewMap(InstanceType type,
504 int instance_size,
505 ElementsKind elements_kind) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000506 CALL_HEAP_FUNCTION(
507 isolate(),
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000508 isolate()->heap()->AllocateMap(type, instance_size, elements_kind),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000509 Map);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000510}
511
512
513Handle<JSObject> Factory::NewFunctionPrototype(Handle<JSFunction> function) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000514 CALL_HEAP_FUNCTION(
515 isolate(),
516 isolate()->heap()->AllocateFunctionPrototype(*function),
517 JSObject);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000518}
519
520
rossberg@chromium.org657d53b2012-07-12 11:06:03 +0000521Handle<Map> Factory::CopyWithPreallocatedFieldDescriptors(Handle<Map> src) {
522 CALL_HEAP_FUNCTION(
523 isolate(), src->CopyWithPreallocatedFieldDescriptors(), Map);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000524}
525
526
ager@chromium.org32912102009-01-16 10:38:43 +0000527Handle<Map> Factory::CopyMap(Handle<Map> src,
528 int extra_inobject_properties) {
rossberg@chromium.org657d53b2012-07-12 11:06:03 +0000529 Handle<Map> copy = CopyWithPreallocatedFieldDescriptors(src);
ager@chromium.org32912102009-01-16 10:38:43 +0000530 // Check that we do not overflow the instance size when adding the
531 // extra inobject properties.
532 int instance_size_delta = extra_inobject_properties * kPointerSize;
533 int max_instance_size_delta =
534 JSObject::kMaxInstanceSize - copy->instance_size();
535 if (instance_size_delta > max_instance_size_delta) {
536 // If the instance size overflows, we allocate as many properties
537 // as we can as inobject properties.
538 instance_size_delta = max_instance_size_delta;
539 extra_inobject_properties = max_instance_size_delta >> kPointerSizeLog2;
540 }
541 // Adjust the map with the extra inobject properties.
542 int inobject_properties =
543 copy->inobject_properties() + extra_inobject_properties;
544 copy->set_inobject_properties(inobject_properties);
545 copy->set_unused_property_fields(inobject_properties);
546 copy->set_instance_size(copy->instance_size() + instance_size_delta);
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000547 copy->set_visitor_id(StaticVisitorBase::GetVisitorId(*copy));
ager@chromium.org32912102009-01-16 10:38:43 +0000548 return copy;
549}
550
fschneider@chromium.org40b9da32010-06-28 11:29:21 +0000551
verwaest@chromium.org753aee42012-07-17 16:15:42 +0000552Handle<Map> Factory::CopyMap(Handle<Map> src) {
verwaest@chromium.orgde64f722012-08-16 15:44:54 +0000553 CALL_HEAP_FUNCTION(isolate(), src->Copy(), Map);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000554}
555
556
kmillikin@chromium.org83e16822011-09-13 08:21:47 +0000557Handle<Map> Factory::GetElementsTransitionMap(
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000558 Handle<JSObject> src,
559 ElementsKind elements_kind) {
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000560 Isolate* i = isolate();
561 CALL_HEAP_FUNCTION(i,
562 src->GetElementsTransitionMap(i, elements_kind),
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000563 Map);
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +0000564}
565
566
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000567Handle<FixedArray> Factory::CopyFixedArray(Handle<FixedArray> array) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000568 CALL_HEAP_FUNCTION(isolate(), array->Copy(), FixedArray);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000569}
570
571
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +0000572Handle<FixedArray> Factory::CopySizeFixedArray(Handle<FixedArray> array,
573 int new_length) {
574 CALL_HEAP_FUNCTION(isolate(), array->CopySize(new_length), FixedArray);
575}
576
577
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000578Handle<FixedDoubleArray> Factory::CopyFixedDoubleArray(
579 Handle<FixedDoubleArray> array) {
580 CALL_HEAP_FUNCTION(isolate(), array->Copy(), FixedDoubleArray);
581}
582
583
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000584Handle<JSFunction> Factory::BaseNewFunctionFromSharedFunctionInfo(
585 Handle<SharedFunctionInfo> function_info,
sgjesse@chromium.org846fb742009-12-18 08:56:33 +0000586 Handle<Map> function_map,
587 PretenureFlag pretenure) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000588 CALL_HEAP_FUNCTION(
589 isolate(),
590 isolate()->heap()->AllocateFunction(*function_map,
591 *function_info,
592 isolate()->heap()->the_hole_value(),
593 pretenure),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000594 JSFunction);
595}
596
597
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000598static Handle<Map> MapForNewFunction(Isolate *isolate,
599 Handle<SharedFunctionInfo> function_info) {
600 Context *context = isolate->context()->native_context();
601 int map_index = Context::FunctionMapIndex(function_info->language_mode(),
602 function_info->is_generator());
603 return Handle<Map>(Map::cast(context->get(map_index)));
604}
605
606
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000607Handle<JSFunction> Factory::NewFunctionFromSharedFunctionInfo(
608 Handle<SharedFunctionInfo> function_info,
sgjesse@chromium.org846fb742009-12-18 08:56:33 +0000609 Handle<Context> context,
610 PretenureFlag pretenure) {
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000611 Handle<JSFunction> result = BaseNewFunctionFromSharedFunctionInfo(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000612 function_info,
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000613 MapForNewFunction(isolate(), function_info),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000614 pretenure);
615
jkummerow@chromium.org1456e702012-03-30 08:38:13 +0000616 if (function_info->ic_age() != isolate()->heap()->global_ic_age()) {
617 function_info->ResetForNewContext(isolate()->heap()->global_ic_age());
618 }
619
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000620 result->set_context(*context);
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000621
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000622 int index = function_info->SearchOptimizedCodeMap(context->native_context());
ulan@chromium.orgd9e468a2012-06-25 09:47:40 +0000623 if (!function_info->bound() && index < 0) {
624 int number_of_literals = function_info->num_literals();
625 Handle<FixedArray> literals = NewFixedArray(number_of_literals, pretenure);
626 if (number_of_literals > 0) {
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000627 // Store the native context in the literals array prefix. This
ulan@chromium.orgd9e468a2012-06-25 09:47:40 +0000628 // context will be used when creating object, regexp and array
629 // literals in this function.
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000630 literals->set(JSFunction::kLiteralNativeContextIndex,
631 context->native_context());
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000632 }
ulan@chromium.orgd9e468a2012-06-25 09:47:40 +0000633 result->set_literals(*literals);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000634 }
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000635
636 if (index > 0) {
637 // Caching of optimized code enabled and optimized code found.
ulan@chromium.orgd9e468a2012-06-25 09:47:40 +0000638 function_info->InstallFromOptimizedCodeMap(*result, index);
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000639 return result;
640 }
641
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000642 if (V8::UseCrankshaft() &&
643 FLAG_always_opt &&
644 result->is_compiled() &&
645 !function_info->is_toplevel() &&
rossberg@chromium.org2c067b12012-03-19 11:01:52 +0000646 function_info->allows_lazy_compilation() &&
647 !function_info->optimization_disabled()) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000648 result->MarkForLazyRecompilation();
649 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000650 return result;
651}
652
653
654Handle<Object> Factory::NewNumber(double value,
655 PretenureFlag pretenure) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000656 CALL_HEAP_FUNCTION(
657 isolate(),
658 isolate()->heap()->NumberFromDouble(value, pretenure), Object);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000659}
660
661
erikcorry0ad885c2011-11-21 13:51:57 +0000662Handle<Object> Factory::NewNumberFromInt(int32_t value,
663 PretenureFlag pretenure) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000664 CALL_HEAP_FUNCTION(
665 isolate(),
erikcorry0ad885c2011-11-21 13:51:57 +0000666 isolate()->heap()->NumberFromInt32(value, pretenure), Object);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000667}
668
669
erikcorry0ad885c2011-11-21 13:51:57 +0000670Handle<Object> Factory::NewNumberFromUint(uint32_t value,
671 PretenureFlag pretenure) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000672 CALL_HEAP_FUNCTION(
673 isolate(),
erikcorry0ad885c2011-11-21 13:51:57 +0000674 isolate()->heap()->NumberFromUint32(value, pretenure), Object);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000675}
676
677
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000678Handle<HeapNumber> Factory::NewHeapNumber(double value,
679 PretenureFlag pretenure) {
680 CALL_HEAP_FUNCTION(
681 isolate(),
682 isolate()->heap()->AllocateHeapNumber(value, pretenure), HeapNumber);
683}
684
685
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000686Handle<JSObject> Factory::NewNeanderObject() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000687 CALL_HEAP_FUNCTION(
688 isolate(),
689 isolate()->heap()->AllocateJSObjectFromMap(
690 isolate()->heap()->neander_map()),
691 JSObject);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000692}
693
694
ulan@chromium.orgdfe53072013-06-06 14:14:51 +0000695Handle<Object> Factory::NewTypeError(const char* message,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000696 Vector< Handle<Object> > args) {
ulan@chromium.orgdfe53072013-06-06 14:14:51 +0000697 return NewError("MakeTypeError", message, args);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000698}
699
700
701Handle<Object> Factory::NewTypeError(Handle<String> message) {
702 return NewError("$TypeError", message);
703}
704
705
ulan@chromium.orgdfe53072013-06-06 14:14:51 +0000706Handle<Object> Factory::NewRangeError(const char* message,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000707 Vector< Handle<Object> > args) {
ulan@chromium.orgdfe53072013-06-06 14:14:51 +0000708 return NewError("MakeRangeError", message, args);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000709}
710
711
712Handle<Object> Factory::NewRangeError(Handle<String> message) {
713 return NewError("$RangeError", message);
714}
715
716
ulan@chromium.orgdfe53072013-06-06 14:14:51 +0000717Handle<Object> Factory::NewSyntaxError(const char* message,
718 Handle<JSArray> args) {
719 return NewError("MakeSyntaxError", message, args);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000720}
721
722
723Handle<Object> Factory::NewSyntaxError(Handle<String> message) {
724 return NewError("$SyntaxError", message);
725}
726
727
ulan@chromium.orgdfe53072013-06-06 14:14:51 +0000728Handle<Object> Factory::NewReferenceError(const char* message,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000729 Vector< Handle<Object> > args) {
ulan@chromium.orgdfe53072013-06-06 14:14:51 +0000730 return NewError("MakeReferenceError", message, args);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000731}
732
733
734Handle<Object> Factory::NewReferenceError(Handle<String> message) {
735 return NewError("$ReferenceError", message);
736}
737
738
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +0000739Handle<Object> Factory::NewError(const char* maker,
ulan@chromium.orgdfe53072013-06-06 14:14:51 +0000740 const char* message,
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +0000741 Vector< Handle<Object> > args) {
742 // Instantiate a closeable HandleScope for EscapeFrom.
743 v8::HandleScope scope(reinterpret_cast<v8::Isolate*>(isolate()));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000744 Handle<FixedArray> array = NewFixedArray(args.length());
ager@chromium.org7c537e22008-10-16 08:43:32 +0000745 for (int i = 0; i < args.length(); i++) {
746 array->set(i, *args[i]);
747 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000748 Handle<JSArray> object = NewJSArrayWithElements(array);
ulan@chromium.orgdfe53072013-06-06 14:14:51 +0000749 Handle<Object> result = NewError(maker, message, object);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000750 return result.EscapeFrom(&scope);
751}
752
753
ulan@chromium.orgdfe53072013-06-06 14:14:51 +0000754Handle<Object> Factory::NewEvalError(const char* message,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000755 Vector< Handle<Object> > args) {
ulan@chromium.orgdfe53072013-06-06 14:14:51 +0000756 return NewError("MakeEvalError", message, args);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000757}
758
759
ulan@chromium.orgdfe53072013-06-06 14:14:51 +0000760Handle<Object> Factory::NewError(const char* message,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000761 Vector< Handle<Object> > args) {
ulan@chromium.orgdfe53072013-06-06 14:14:51 +0000762 return NewError("MakeError", message, args);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000763}
764
765
ulan@chromium.orgdfe53072013-06-06 14:14:51 +0000766Handle<String> Factory::EmergencyNewError(const char* message,
verwaest@chromium.org37141392012-05-31 13:27:02 +0000767 Handle<JSArray> args) {
768 const int kBufferSize = 1000;
769 char buffer[kBufferSize];
770 size_t space = kBufferSize;
771 char* p = &buffer[0];
772
773 Vector<char> v(buffer, kBufferSize);
ulan@chromium.orgdfe53072013-06-06 14:14:51 +0000774 OS::StrNCpy(v, message, space);
775 space -= Min(space, strlen(message));
verwaest@chromium.org37141392012-05-31 13:27:02 +0000776 p = &buffer[kBufferSize] - space;
777
778 for (unsigned i = 0; i < ARRAY_SIZE(args); i++) {
779 if (space > 0) {
780 *p++ = ' ';
781 space--;
782 if (space > 0) {
783 MaybeObject* maybe_arg = args->GetElement(i);
784 Handle<String> arg_str(reinterpret_cast<String*>(maybe_arg));
785 const char* arg = *arg_str->ToCString();
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000786 Vector<char> v2(p, static_cast<int>(space));
verwaest@chromium.org37141392012-05-31 13:27:02 +0000787 OS::StrNCpy(v2, arg, space);
788 space -= Min(space, strlen(arg));
789 p = &buffer[kBufferSize] - space;
790 }
791 }
792 }
793 if (space > 0) {
794 *p = '\0';
795 } else {
796 buffer[kBufferSize - 1] = '\0';
797 }
798 Handle<String> error_string = NewStringFromUtf8(CStrVector(buffer), TENURED);
799 return error_string;
800}
801
802
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000803Handle<Object> Factory::NewError(const char* maker,
ulan@chromium.orgdfe53072013-06-06 14:14:51 +0000804 const char* message,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000805 Handle<JSArray> args) {
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000806 Handle<String> make_str = InternalizeUtf8String(maker);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000807 Handle<Object> fun_obj(
ulan@chromium.org09d7ab52013-02-25 15:50:35 +0000808 isolate()->js_builtins_object()->GetPropertyNoExceptionThrown(*make_str),
809 isolate());
ager@chromium.org4af710e2009-09-15 12:20:11 +0000810 // If the builtins haven't been properly configured yet this error
811 // constructor may not have been defined. Bail out.
verwaest@chromium.org37141392012-05-31 13:27:02 +0000812 if (!fun_obj->IsJSFunction()) {
ulan@chromium.orgdfe53072013-06-06 14:14:51 +0000813 return EmergencyNewError(message, args);
verwaest@chromium.org37141392012-05-31 13:27:02 +0000814 }
ager@chromium.org4af710e2009-09-15 12:20:11 +0000815 Handle<JSFunction> fun = Handle<JSFunction>::cast(fun_obj);
ulan@chromium.orgdfe53072013-06-06 14:14:51 +0000816 Handle<Object> message_obj = InternalizeUtf8String(message);
817 Handle<Object> argv[] = { message_obj, args };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000818
819 // Invoke the JavaScript factory method. If an exception is thrown while
820 // running the factory method, use the exception as the result.
821 bool caught_exception;
822 Handle<Object> result = Execution::TryCall(fun,
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +0000823 isolate()->js_builtins_object(),
824 ARRAY_SIZE(argv),
825 argv,
826 &caught_exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000827 return result;
828}
829
830
831Handle<Object> Factory::NewError(Handle<String> message) {
832 return NewError("$Error", message);
833}
834
835
836Handle<Object> Factory::NewError(const char* constructor,
837 Handle<String> message) {
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000838 Handle<String> constr = InternalizeUtf8String(constructor);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000839 Handle<JSFunction> fun = Handle<JSFunction>(
840 JSFunction::cast(isolate()->js_builtins_object()->
841 GetPropertyNoExceptionThrown(*constr)));
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +0000842 Handle<Object> argv[] = { message };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000843
844 // Invoke the JavaScript factory method. If an exception is thrown while
845 // running the factory method, use the exception as the result.
846 bool caught_exception;
847 Handle<Object> result = Execution::TryCall(fun,
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +0000848 isolate()->js_builtins_object(),
849 ARRAY_SIZE(argv),
850 argv,
851 &caught_exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000852 return result;
853}
854
855
856Handle<JSFunction> Factory::NewFunction(Handle<String> name,
857 InstanceType type,
858 int instance_size,
859 Handle<Code> code,
860 bool force_initial_map) {
861 // Allocate the function
862 Handle<JSFunction> function = NewFunction(name, the_hole_value());
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000863
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000864 // Set up the code pointer in both the shared function info and in
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000865 // the function itself.
866 function->shared()->set_code(*code);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000867 function->set_code(*code);
868
869 if (force_initial_map ||
870 type != JS_OBJECT_TYPE ||
871 instance_size != JSObject::kHeaderSize) {
872 Handle<Map> initial_map = NewMap(type, instance_size);
873 Handle<JSObject> prototype = NewFunctionPrototype(function);
874 initial_map->set_prototype(*prototype);
875 function->set_initial_map(*initial_map);
876 initial_map->set_constructor(*function);
877 } else {
878 ASSERT(!function->has_initial_map());
879 ASSERT(!function->has_prototype());
880 }
881
882 return function;
883}
884
885
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000886Handle<JSFunction> Factory::NewFunctionWithPrototype(Handle<String> name,
887 InstanceType type,
888 int instance_size,
889 Handle<JSObject> prototype,
890 Handle<Code> code,
891 bool force_initial_map) {
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000892 // Allocate the function.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000893 Handle<JSFunction> function = NewFunction(name, prototype);
894
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000895 // Set up the code pointer in both the shared function info and in
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000896 // the function itself.
897 function->shared()->set_code(*code);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000898 function->set_code(*code);
899
900 if (force_initial_map ||
901 type != JS_OBJECT_TYPE ||
902 instance_size != JSObject::kHeaderSize) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000903 Handle<Map> initial_map = NewMap(type,
904 instance_size,
svenpanne@chromium.org830d30c2012-05-29 13:20:14 +0000905 GetInitialFastElementsKind());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000906 function->set_initial_map(*initial_map);
907 initial_map->set_constructor(*function);
908 }
909
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000910 SetPrototypeProperty(function, prototype);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000911 return function;
912}
913
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000914
kmillikin@chromium.org4111b802010-05-03 10:34:42 +0000915Handle<JSFunction> Factory::NewFunctionWithoutPrototype(Handle<String> name,
916 Handle<Code> code) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000917 Handle<JSFunction> function = NewFunctionWithoutPrototype(name,
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000918 CLASSIC_MODE);
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000919 function->shared()->set_code(*code);
kmillikin@chromium.org4111b802010-05-03 10:34:42 +0000920 function->set_code(*code);
921 ASSERT(!function->has_initial_map());
922 ASSERT(!function->has_prototype());
923 return function;
924}
925
926
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000927Handle<ScopeInfo> Factory::NewScopeInfo(int length) {
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000928 CALL_HEAP_FUNCTION(
929 isolate(),
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000930 isolate()->heap()->AllocateScopeInfo(length),
931 ScopeInfo);
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000932}
933
934
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +0000935Handle<JSObject> Factory::NewExternal(void* value) {
936 CALL_HEAP_FUNCTION(isolate(),
937 isolate()->heap()->AllocateExternal(value),
938 JSObject);
939}
940
941
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000942Handle<Code> Factory::NewCode(const CodeDesc& desc,
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000943 Code::Flags flags,
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +0000944 Handle<Object> self_ref,
mstarzinger@chromium.orgb228be02013-04-18 14:56:59 +0000945 bool immovable,
946 bool crankshafted) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000947 CALL_HEAP_FUNCTION(isolate(),
948 isolate()->heap()->CreateCode(
mstarzinger@chromium.orgb228be02013-04-18 14:56:59 +0000949 desc, flags, self_ref, immovable, crankshafted),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000950 Code);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000951}
952
953
954Handle<Code> Factory::CopyCode(Handle<Code> code) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000955 CALL_HEAP_FUNCTION(isolate(),
956 isolate()->heap()->CopyCode(*code),
957 Code);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000958}
959
960
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000961Handle<Code> Factory::CopyCode(Handle<Code> code, Vector<byte> reloc_info) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000962 CALL_HEAP_FUNCTION(isolate(),
963 isolate()->heap()->CopyCode(*code, reloc_info),
964 Code);
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000965}
966
967
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000968Handle<String> Factory::InternalizedStringFromString(Handle<String> value) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000969 CALL_HEAP_FUNCTION(isolate(),
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000970 isolate()->heap()->InternalizeString(*value), String);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000971}
972
973
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000974Handle<JSObject> Factory::NewJSObject(Handle<JSFunction> constructor,
975 PretenureFlag pretenure) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000976 CALL_HEAP_FUNCTION(
977 isolate(),
978 isolate()->heap()->AllocateJSObject(*constructor, pretenure), JSObject);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000979}
980
981
danno@chromium.org81cac2b2012-07-10 11:28:27 +0000982Handle<JSModule> Factory::NewJSModule(Handle<Context> context,
983 Handle<ScopeInfo> scope_info) {
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000984 CALL_HEAP_FUNCTION(
985 isolate(),
danno@chromium.org81cac2b2012-07-10 11:28:27 +0000986 isolate()->heap()->AllocateJSModule(*context, *scope_info), JSModule);
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000987}
988
989
kasperl@chromium.org2abc4502009-07-02 07:00:29 +0000990Handle<GlobalObject> Factory::NewGlobalObject(
991 Handle<JSFunction> constructor) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000992 CALL_HEAP_FUNCTION(isolate(),
993 isolate()->heap()->AllocateGlobalObject(*constructor),
kasperl@chromium.org2abc4502009-07-02 07:00:29 +0000994 GlobalObject);
995}
996
997
998
mstarzinger@chromium.org71fc3462013-02-27 09:34:27 +0000999Handle<JSObject> Factory::NewJSObjectFromMap(Handle<Map> map,
1000 PretenureFlag pretenure) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001001 CALL_HEAP_FUNCTION(
1002 isolate(),
mstarzinger@chromium.org71fc3462013-02-27 09:34:27 +00001003 isolate()->heap()->AllocateJSObjectFromMap(*map, pretenure),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001004 JSObject);
ager@chromium.org236ad962008-09-25 09:45:57 +00001005}
1006
1007
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +00001008Handle<JSArray> Factory::NewJSArray(int capacity,
danno@chromium.orgfa458e42012-02-01 10:48:36 +00001009 ElementsKind elements_kind,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001010 PretenureFlag pretenure) {
ulan@chromium.org8e8d8822012-11-23 14:36:46 +00001011 if (capacity != 0) {
1012 elements_kind = GetHoleyElementsKind(elements_kind);
1013 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001014 CALL_HEAP_FUNCTION(isolate(),
danno@chromium.orgfa458e42012-02-01 10:48:36 +00001015 isolate()->heap()->AllocateJSArrayAndStorage(
1016 elements_kind,
1017 0,
1018 capacity,
1019 INITIALIZE_ARRAY_ELEMENTS_WITH_HOLE,
1020 pretenure),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001021 JSArray);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001022}
1023
1024
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00001025Handle<JSArray> Factory::NewJSArrayWithElements(Handle<FixedArrayBase> elements,
danno@chromium.orgfa458e42012-02-01 10:48:36 +00001026 ElementsKind elements_kind,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001027 PretenureFlag pretenure) {
danno@chromium.orgfa458e42012-02-01 10:48:36 +00001028 CALL_HEAP_FUNCTION(
1029 isolate(),
1030 isolate()->heap()->AllocateJSArrayWithElements(*elements,
1031 elements_kind,
yangguo@chromium.orgfb377212012-11-16 14:43:43 +00001032 elements->length(),
danno@chromium.orgfa458e42012-02-01 10:48:36 +00001033 pretenure),
1034 JSArray);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001035}
1036
1037
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00001038void Factory::SetElementsCapacityAndLength(Handle<JSArray> array,
1039 int capacity,
1040 int length) {
1041 ElementsAccessor* accessor = array->GetElementsAccessor();
1042 CALL_HEAP_FUNCTION_VOID(
1043 isolate(),
1044 accessor->SetCapacityAndLength(*array, capacity, length));
1045}
1046
1047
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001048void Factory::SetContent(Handle<JSArray> array,
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00001049 Handle<FixedArrayBase> elements) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001050 CALL_HEAP_FUNCTION_VOID(
1051 isolate(),
1052 array->SetContent(*elements));
1053}
1054
1055
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00001056void Factory::EnsureCanContainHeapObjectElements(Handle<JSArray> array) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001057 CALL_HEAP_FUNCTION_VOID(
1058 isolate(),
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00001059 array->EnsureCanContainHeapObjectElements());
1060}
1061
1062
1063void Factory::EnsureCanContainElements(Handle<JSArray> array,
1064 Handle<FixedArrayBase> elements,
svenpanne@chromium.org830d30c2012-05-29 13:20:14 +00001065 uint32_t length,
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00001066 EnsureElementsMode mode) {
1067 CALL_HEAP_FUNCTION_VOID(
1068 isolate(),
svenpanne@chromium.org830d30c2012-05-29 13:20:14 +00001069 array->EnsureCanContainElements(*elements, length, mode));
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001070}
1071
1072
danno@chromium.orgca29dd82013-04-26 11:59:48 +00001073Handle<JSArrayBuffer> Factory::NewJSArrayBuffer() {
1074 JSFunction* array_buffer_fun =
1075 isolate()->context()->native_context()->array_buffer_fun();
1076 CALL_HEAP_FUNCTION(
1077 isolate(),
1078 isolate()->heap()->AllocateJSObject(array_buffer_fun),
1079 JSArrayBuffer);
1080}
1081
1082
danno@chromium.orgf005df62013-04-30 16:36:45 +00001083Handle<JSTypedArray> Factory::NewJSTypedArray(ExternalArrayType type) {
1084 JSFunction* typed_array_fun;
1085 Context* native_context = isolate()->context()->native_context();
1086 switch (type) {
1087 case kExternalUnsignedByteArray:
1088 typed_array_fun = native_context->uint8_array_fun();
1089 break;
1090
1091 case kExternalByteArray:
1092 typed_array_fun = native_context->int8_array_fun();
1093 break;
1094
1095 case kExternalUnsignedShortArray:
1096 typed_array_fun = native_context->uint16_array_fun();
1097 break;
1098
1099 case kExternalShortArray:
1100 typed_array_fun = native_context->int16_array_fun();
1101 break;
1102
1103 case kExternalUnsignedIntArray:
1104 typed_array_fun = native_context->uint32_array_fun();
1105 break;
1106
1107 case kExternalIntArray:
1108 typed_array_fun = native_context->int32_array_fun();
1109 break;
1110
1111 case kExternalFloatArray:
1112 typed_array_fun = native_context->float_array_fun();
1113 break;
1114
1115 case kExternalDoubleArray:
1116 typed_array_fun = native_context->double_array_fun();
1117 break;
1118
ulan@chromium.org57ff8812013-05-10 08:16:55 +00001119 case kExternalPixelArray:
1120 typed_array_fun = native_context->uint8c_array_fun();
1121 break;
1122
danno@chromium.orgf005df62013-04-30 16:36:45 +00001123 default:
1124 UNREACHABLE();
1125 return Handle<JSTypedArray>();
1126 }
1127
1128 CALL_HEAP_FUNCTION(
1129 isolate(),
1130 isolate()->heap()->AllocateJSObject(typed_array_fun),
1131 JSTypedArray);
1132}
1133
1134
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001135Handle<JSProxy> Factory::NewJSProxy(Handle<Object> handler,
1136 Handle<Object> prototype) {
1137 CALL_HEAP_FUNCTION(
1138 isolate(),
1139 isolate()->heap()->AllocateJSProxy(*handler, *prototype),
1140 JSProxy);
1141}
1142
1143
lrn@chromium.org34e60782011-09-15 07:25:40 +00001144void Factory::BecomeJSObject(Handle<JSReceiver> object) {
rossberg@chromium.org717967f2011-07-20 13:44:42 +00001145 CALL_HEAP_FUNCTION_VOID(
1146 isolate(),
lrn@chromium.org34e60782011-09-15 07:25:40 +00001147 isolate()->heap()->ReinitializeJSReceiver(
1148 *object, JS_OBJECT_TYPE, JSObject::kHeaderSize));
1149}
1150
1151
1152void Factory::BecomeJSFunction(Handle<JSReceiver> object) {
1153 CALL_HEAP_FUNCTION_VOID(
1154 isolate(),
1155 isolate()->heap()->ReinitializeJSReceiver(
1156 *object, JS_FUNCTION_TYPE, JSFunction::kSize));
rossberg@chromium.org717967f2011-07-20 13:44:42 +00001157}
1158
1159
jkummerow@chromium.org000f7fb2012-08-01 11:14:42 +00001160void Factory::SetIdentityHash(Handle<JSObject> object, Smi* hash) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001161 CALL_HEAP_FUNCTION_VOID(
1162 isolate(),
1163 object->SetIdentityHash(hash, ALLOW_CREATION));
1164}
1165
1166
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001167Handle<SharedFunctionInfo> Factory::NewSharedFunctionInfo(
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00001168 Handle<String> name,
1169 int number_of_literals,
mstarzinger@chromium.orgb228be02013-04-18 14:56:59 +00001170 bool is_generator,
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00001171 Handle<Code> code,
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001172 Handle<ScopeInfo> scope_info) {
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001173 Handle<SharedFunctionInfo> shared = NewSharedFunctionInfo(name);
1174 shared->set_code(*code);
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00001175 shared->set_scope_info(*scope_info);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001176 int literals_array_size = number_of_literals;
1177 // If the function contains object, regexp or array literals,
1178 // allocate extra space for a literals array prefix containing the
1179 // context.
1180 if (number_of_literals > 0) {
1181 literals_array_size += JSFunction::kLiteralsPrefixSize;
1182 }
1183 shared->set_num_literals(literals_array_size);
mstarzinger@chromium.orgb228be02013-04-18 14:56:59 +00001184 if (is_generator) {
1185 shared->set_instance_class_name(isolate()->heap()->Generator_string());
1186 }
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001187 return shared;
1188}
1189
1190
kmillikin@chromium.org31b12772011-02-02 16:08:26 +00001191Handle<JSMessageObject> Factory::NewJSMessageObject(
1192 Handle<String> type,
1193 Handle<JSArray> arguments,
1194 int start_position,
1195 int end_position,
1196 Handle<Object> script,
1197 Handle<Object> stack_trace,
1198 Handle<Object> stack_frames) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001199 CALL_HEAP_FUNCTION(isolate(),
1200 isolate()->heap()->AllocateJSMessageObject(*type,
1201 *arguments,
1202 start_position,
1203 end_position,
1204 *script,
1205 *stack_trace,
1206 *stack_frames),
kmillikin@chromium.org31b12772011-02-02 16:08:26 +00001207 JSMessageObject);
1208}
1209
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001210Handle<SharedFunctionInfo> Factory::NewSharedFunctionInfo(Handle<String> name) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001211 CALL_HEAP_FUNCTION(isolate(),
1212 isolate()->heap()->AllocateSharedFunctionInfo(*name),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001213 SharedFunctionInfo);
1214}
1215
1216
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00001217Handle<String> Factory::NumberToString(Handle<Object> number) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001218 CALL_HEAP_FUNCTION(isolate(),
1219 isolate()->heap()->NumberToString(*number), String);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00001220}
1221
1222
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001223Handle<String> Factory::Uint32ToString(uint32_t value) {
1224 CALL_HEAP_FUNCTION(isolate(),
1225 isolate()->heap()->Uint32ToString(value), String);
1226}
1227
1228
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001229Handle<SeededNumberDictionary> Factory::DictionaryAtNumberPut(
1230 Handle<SeededNumberDictionary> dictionary,
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00001231 uint32_t key,
1232 Handle<Object> value) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001233 CALL_HEAP_FUNCTION(isolate(),
1234 dictionary->AtNumberPut(key, *value),
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001235 SeededNumberDictionary);
1236}
1237
1238
1239Handle<UnseededNumberDictionary> Factory::DictionaryAtNumberPut(
1240 Handle<UnseededNumberDictionary> dictionary,
1241 uint32_t key,
1242 Handle<Object> value) {
1243 CALL_HEAP_FUNCTION(isolate(),
1244 dictionary->AtNumberPut(key, *value),
1245 UnseededNumberDictionary);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001246}
1247
1248
1249Handle<JSFunction> Factory::NewFunctionHelper(Handle<String> name,
1250 Handle<Object> prototype) {
1251 Handle<SharedFunctionInfo> function_share = NewSharedFunctionInfo(name);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001252 CALL_HEAP_FUNCTION(
1253 isolate(),
1254 isolate()->heap()->AllocateFunction(*isolate()->function_map(),
1255 *function_share,
1256 *prototype),
1257 JSFunction);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001258}
1259
1260
1261Handle<JSFunction> Factory::NewFunction(Handle<String> name,
1262 Handle<Object> prototype) {
1263 Handle<JSFunction> fun = NewFunctionHelper(name, prototype);
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001264 fun->set_context(isolate()->context()->native_context());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001265 return fun;
1266}
1267
1268
kmillikin@chromium.org4111b802010-05-03 10:34:42 +00001269Handle<JSFunction> Factory::NewFunctionWithoutPrototypeHelper(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001270 Handle<String> name,
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00001271 LanguageMode language_mode) {
kmillikin@chromium.org4111b802010-05-03 10:34:42 +00001272 Handle<SharedFunctionInfo> function_share = NewSharedFunctionInfo(name);
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00001273 Handle<Map> map = (language_mode == CLASSIC_MODE)
1274 ? isolate()->function_without_prototype_map()
1275 : isolate()->strict_mode_function_without_prototype_map();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001276 CALL_HEAP_FUNCTION(isolate(),
1277 isolate()->heap()->AllocateFunction(
1278 *map,
kmillikin@chromium.org4111b802010-05-03 10:34:42 +00001279 *function_share,
1280 *the_hole_value()),
1281 JSFunction);
1282}
1283
1284
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001285Handle<JSFunction> Factory::NewFunctionWithoutPrototype(
1286 Handle<String> name,
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00001287 LanguageMode language_mode) {
1288 Handle<JSFunction> fun =
1289 NewFunctionWithoutPrototypeHelper(name, language_mode);
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001290 fun->set_context(isolate()->context()->native_context());
kmillikin@chromium.org4111b802010-05-03 10:34:42 +00001291 return fun;
1292}
1293
1294
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001295Handle<Object> Factory::ToObject(Handle<Object> object) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001296 CALL_HEAP_FUNCTION(isolate(), object->ToObject(), Object);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001297}
1298
1299
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001300Handle<Object> Factory::ToObject(Handle<Object> object,
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001301 Handle<Context> native_context) {
1302 CALL_HEAP_FUNCTION(isolate(), object->ToObject(*native_context), Object);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001303}
1304
1305
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001306#ifdef ENABLE_DEBUGGER_SUPPORT
v8.team.kasperl727e9952008-09-02 14:56:44 +00001307Handle<DebugInfo> Factory::NewDebugInfo(Handle<SharedFunctionInfo> shared) {
1308 // Get the original code of the function.
1309 Handle<Code> code(shared->code());
1310
1311 // Create a copy of the code before allocating the debug info object to avoid
1312 // allocation while setting up the debug info object.
1313 Handle<Code> original_code(*Factory::CopyCode(code));
1314
1315 // Allocate initial fixed array for active break points before allocating the
1316 // debug info object to avoid allocation while setting up the debug info
1317 // object.
1318 Handle<FixedArray> break_points(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001319 NewFixedArray(Debug::kEstimatedNofBreakPointsInFunction));
v8.team.kasperl727e9952008-09-02 14:56:44 +00001320
1321 // Create and set up the debug info object. Debug info contains function, a
1322 // copy of the original code, the executing code and initial fixed array for
1323 // active break points.
1324 Handle<DebugInfo> debug_info =
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001325 Handle<DebugInfo>::cast(NewStruct(DEBUG_INFO_TYPE));
v8.team.kasperl727e9952008-09-02 14:56:44 +00001326 debug_info->set_shared(*shared);
1327 debug_info->set_original_code(*original_code);
1328 debug_info->set_code(*code);
1329 debug_info->set_break_points(*break_points);
1330
1331 // Link debug info to function.
1332 shared->set_debug_info(*debug_info);
1333
1334 return debug_info;
1335}
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001336#endif
v8.team.kasperl727e9952008-09-02 14:56:44 +00001337
1338
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001339Handle<JSObject> Factory::NewArgumentsObject(Handle<Object> callee,
1340 int length) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001341 CALL_HEAP_FUNCTION(
1342 isolate(),
1343 isolate()->heap()->AllocateArgumentsObject(*callee, length), JSObject);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001344}
1345
1346
1347Handle<JSFunction> Factory::CreateApiFunction(
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001348 Handle<FunctionTemplateInfo> obj, ApiInstanceType instance_type) {
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00001349 Handle<Code> code = isolate()->builtins()->HandleApiCall();
1350 Handle<Code> construct_stub = isolate()->builtins()->JSConstructStubApi();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001351
kasper.lund212ac232008-07-16 07:07:30 +00001352 int internal_field_count = 0;
1353 if (!obj->instance_template()->IsUndefined()) {
1354 Handle<ObjectTemplateInfo> instance_template =
1355 Handle<ObjectTemplateInfo>(
1356 ObjectTemplateInfo::cast(obj->instance_template()));
1357 internal_field_count =
1358 Smi::cast(instance_template->internal_field_count())->value();
1359 }
1360
1361 int instance_size = kPointerSize * internal_field_count;
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001362 InstanceType type = INVALID_TYPE;
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001363 switch (instance_type) {
1364 case JavaScriptObject:
1365 type = JS_OBJECT_TYPE;
1366 instance_size += JSObject::kHeaderSize;
1367 break;
1368 case InnerGlobalObject:
1369 type = JS_GLOBAL_OBJECT_TYPE;
1370 instance_size += JSGlobalObject::kSize;
1371 break;
1372 case OuterGlobalObject:
1373 type = JS_GLOBAL_PROXY_TYPE;
1374 instance_size += JSGlobalProxy::kSize;
1375 break;
1376 default:
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001377 break;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001378 }
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001379 ASSERT(type != INVALID_TYPE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001380
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001381 Handle<JSFunction> result =
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001382 NewFunction(Factory::empty_string(),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001383 type,
1384 instance_size,
1385 code,
1386 true);
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00001387
1388 // Set length.
1389 result->shared()->set_length(obj->length());
1390
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001391 // Set class name.
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00001392 Handle<Object> class_name = Handle<Object>(obj->class_name(), isolate());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001393 if (class_name->IsString()) {
1394 result->shared()->set_instance_class_name(*class_name);
1395 result->shared()->set_name(*class_name);
1396 }
1397
1398 Handle<Map> map = Handle<Map>(result->initial_map());
1399
1400 // Mark as undetectable if needed.
1401 if (obj->undetectable()) {
1402 map->set_is_undetectable();
1403 }
1404
1405 // Mark as hidden for the __proto__ accessor if needed.
1406 if (obj->hidden_prototype()) {
1407 map->set_is_hidden_prototype();
1408 }
1409
1410 // Mark as needs_access_check if needed.
1411 if (obj->needs_access_check()) {
ager@chromium.org870a0b62008-11-04 11:43:05 +00001412 map->set_is_access_check_needed(true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001413 }
1414
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001415 // Set interceptor information in the map.
1416 if (!obj->named_property_handler()->IsUndefined()) {
1417 map->set_has_named_interceptor();
1418 }
1419 if (!obj->indexed_property_handler()->IsUndefined()) {
1420 map->set_has_indexed_interceptor();
1421 }
1422
1423 // Set instance call-as-function information in the map.
1424 if (!obj->instance_call_handler()->IsUndefined()) {
1425 map->set_has_instance_call_handler();
1426 }
1427
1428 result->shared()->set_function_data(*obj);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001429 result->shared()->set_construct_stub(*construct_stub);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001430 result->shared()->DontAdaptArguments();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001431
1432 // Recursively copy parent templates' accessors, 'data' may be modified.
verwaest@chromium.org33e09c82012-10-10 17:07:22 +00001433 int max_number_of_additional_properties = 0;
1434 FunctionTemplateInfo* info = *obj;
1435 while (true) {
1436 Object* props = info->property_accessors();
1437 if (!props->IsUndefined()) {
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00001438 Handle<Object> props_handle(props, isolate());
verwaest@chromium.org33e09c82012-10-10 17:07:22 +00001439 NeanderArray props_array(props_handle);
1440 max_number_of_additional_properties += props_array.length();
1441 }
1442 Object* parent = info->parent_template();
1443 if (parent->IsUndefined()) break;
1444 info = FunctionTemplateInfo::cast(parent);
1445 }
1446
1447 Map::EnsureDescriptorSlack(map, max_number_of_additional_properties);
1448
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001449 while (true) {
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00001450 Handle<Object> props = Handle<Object>(obj->property_accessors(),
1451 isolate());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001452 if (!props->IsUndefined()) {
verwaest@chromium.org33e09c82012-10-10 17:07:22 +00001453 Map::AppendCallbackDescriptors(map, props);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001454 }
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00001455 Handle<Object> parent = Handle<Object>(obj->parent_template(), isolate());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001456 if (parent->IsUndefined()) break;
1457 obj = Handle<FunctionTemplateInfo>::cast(parent);
1458 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001459
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001460 ASSERT(result->shared()->IsApiFunction());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001461 return result;
1462}
1463
1464
ager@chromium.org236ad962008-09-25 09:45:57 +00001465Handle<MapCache> Factory::NewMapCache(int at_least_space_for) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001466 CALL_HEAP_FUNCTION(isolate(),
ulan@chromium.org6e196bf2013-03-13 09:38:22 +00001467 MapCache::Allocate(isolate()->heap(),
1468 at_least_space_for),
1469 MapCache);
ager@chromium.org236ad962008-09-25 09:45:57 +00001470}
1471
1472
lrn@chromium.org303ada72010-10-27 09:33:13 +00001473MUST_USE_RESULT static MaybeObject* UpdateMapCacheWith(Context* context,
1474 FixedArray* keys,
1475 Map* map) {
1476 Object* result;
1477 { MaybeObject* maybe_result =
1478 MapCache::cast(context->map_cache())->Put(keys, map);
1479 if (!maybe_result->ToObject(&result)) return maybe_result;
1480 }
1481 context->set_map_cache(MapCache::cast(result));
ager@chromium.org236ad962008-09-25 09:45:57 +00001482 return result;
1483}
1484
1485
1486Handle<MapCache> Factory::AddToMapCache(Handle<Context> context,
1487 Handle<FixedArray> keys,
1488 Handle<Map> map) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001489 CALL_HEAP_FUNCTION(isolate(),
1490 UpdateMapCacheWith(*context, *keys, *map), MapCache);
ager@chromium.org236ad962008-09-25 09:45:57 +00001491}
1492
1493
1494Handle<Map> Factory::ObjectLiteralMapFromCache(Handle<Context> context,
1495 Handle<FixedArray> keys) {
1496 if (context->map_cache()->IsUndefined()) {
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001497 // Allocate the new map cache for the native context.
ager@chromium.org236ad962008-09-25 09:45:57 +00001498 Handle<MapCache> new_cache = NewMapCache(24);
1499 context->set_map_cache(*new_cache);
1500 }
ager@chromium.org32912102009-01-16 10:38:43 +00001501 // Check to see whether there is a matching element in the cache.
ager@chromium.org236ad962008-09-25 09:45:57 +00001502 Handle<MapCache> cache =
1503 Handle<MapCache>(MapCache::cast(context->map_cache()));
yangguo@chromium.orgfb377212012-11-16 14:43:43 +00001504 Handle<Object> result = Handle<Object>(cache->Lookup(*keys), isolate());
ager@chromium.org236ad962008-09-25 09:45:57 +00001505 if (result->IsMap()) return Handle<Map>::cast(result);
1506 // Create a new map and add it to the cache.
1507 Handle<Map> map =
ager@chromium.org32912102009-01-16 10:38:43 +00001508 CopyMap(Handle<Map>(context->object_function()->initial_map()),
1509 keys->length());
ager@chromium.org236ad962008-09-25 09:45:57 +00001510 AddToMapCache(context, keys, map);
1511 return Handle<Map>(map);
1512}
1513
1514
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001515void Factory::SetRegExpAtomData(Handle<JSRegExp> regexp,
1516 JSRegExp::Type type,
1517 Handle<String> source,
1518 JSRegExp::Flags flags,
1519 Handle<Object> data) {
1520 Handle<FixedArray> store = NewFixedArray(JSRegExp::kAtomDataSize);
1521
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001522 store->set(JSRegExp::kTagIndex, Smi::FromInt(type));
1523 store->set(JSRegExp::kSourceIndex, *source);
1524 store->set(JSRegExp::kFlagsIndex, Smi::FromInt(flags.value()));
1525 store->set(JSRegExp::kAtomPatternIndex, *data);
1526 regexp->set_data(*store);
1527}
1528
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001529void Factory::SetRegExpIrregexpData(Handle<JSRegExp> regexp,
1530 JSRegExp::Type type,
1531 Handle<String> source,
1532 JSRegExp::Flags flags,
1533 int capture_count) {
1534 Handle<FixedArray> store = NewFixedArray(JSRegExp::kIrregexpDataSize);
jkummerow@chromium.orgddda9e82011-07-06 11:27:02 +00001535 Smi* uninitialized = Smi::FromInt(JSRegExp::kUninitializedValue);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001536 store->set(JSRegExp::kTagIndex, Smi::FromInt(type));
1537 store->set(JSRegExp::kSourceIndex, *source);
1538 store->set(JSRegExp::kFlagsIndex, Smi::FromInt(flags.value()));
jkummerow@chromium.orgddda9e82011-07-06 11:27:02 +00001539 store->set(JSRegExp::kIrregexpASCIICodeIndex, uninitialized);
1540 store->set(JSRegExp::kIrregexpUC16CodeIndex, uninitialized);
1541 store->set(JSRegExp::kIrregexpASCIICodeSavedIndex, uninitialized);
1542 store->set(JSRegExp::kIrregexpUC16CodeSavedIndex, uninitialized);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001543 store->set(JSRegExp::kIrregexpMaxRegisterCountIndex, Smi::FromInt(0));
1544 store->set(JSRegExp::kIrregexpCaptureCountIndex,
1545 Smi::FromInt(capture_count));
1546 regexp->set_data(*store);
1547}
1548
1549
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001550
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001551void Factory::ConfigureInstance(Handle<FunctionTemplateInfo> desc,
1552 Handle<JSObject> instance,
1553 bool* pending_exception) {
1554 // Configure the instance by adding the properties specified by the
1555 // instance template.
yangguo@chromium.orgfb377212012-11-16 14:43:43 +00001556 Handle<Object> instance_template(desc->instance_template(), isolate());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001557 if (!instance_template->IsUndefined()) {
1558 Execution::ConfigureInstance(instance,
1559 instance_template,
1560 pending_exception);
1561 } else {
1562 *pending_exception = false;
1563 }
1564}
1565
1566
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001567Handle<Object> Factory::GlobalConstantFor(Handle<String> name) {
1568 Heap* h = isolate()->heap();
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001569 if (name->Equals(h->undefined_string())) return undefined_value();
1570 if (name->Equals(h->nan_string())) return nan_value();
1571 if (name->Equals(h->infinity_string())) return infinity_value();
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001572 return Handle<Object>::null();
1573}
1574
1575
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00001576Handle<Object> Factory::ToBoolean(bool value) {
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00001577 return value ? true_value() : false_value();
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00001578}
1579
1580
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001581} } // namespace v8::internal