blob: 913b75601551593eb93bf5f61a5b4e9567dd4b07 [file] [log] [blame]
danno@chromium.orgfa458e42012-02-01 10:48:36 +00001// Copyright 2012 the V8 project authors. All rights reserved.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28#include "v8.h"
29
30#include "api.h"
v8.team.kasperl727e9952008-09-02 14:56:44 +000031#include "debug.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000032#include "execution.h"
33#include "factory.h"
34#include "macro-assembler.h"
kasperl@chromium.orga5551262010-12-07 12:49:48 +000035#include "objects.h"
ager@chromium.orgea4f62e2010-08-16 16:28:43 +000036#include "objects-visiting.h"
verwaest@chromium.org37141392012-05-31 13:27:02 +000037#include "platform.h"
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +000038#include "scopeinfo.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000039
kasperl@chromium.org71affb52009-05-26 05:44:31 +000040namespace v8 {
41namespace internal {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000042
43
44Handle<FixedArray> Factory::NewFixedArray(int size, PretenureFlag pretenure) {
45 ASSERT(0 <= size);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000046 CALL_HEAP_FUNCTION(
47 isolate(),
48 isolate()->heap()->AllocateFixedArray(size, pretenure),
49 FixedArray);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000050}
51
52
ricow@chromium.orgc9c80822010-04-21 08:22:37 +000053Handle<FixedArray> Factory::NewFixedArrayWithHoles(int size,
54 PretenureFlag pretenure) {
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +000055 ASSERT(0 <= size);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000056 CALL_HEAP_FUNCTION(
57 isolate(),
58 isolate()->heap()->AllocateFixedArrayWithHoles(size, pretenure),
59 FixedArray);
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +000060}
61
62
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +000063Handle<FixedDoubleArray> Factory::NewFixedDoubleArray(int size,
64 PretenureFlag pretenure) {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +000065 ASSERT(0 <= size);
66 CALL_HEAP_FUNCTION(
67 isolate(),
68 isolate()->heap()->AllocateUninitializedFixedDoubleArray(size, pretenure),
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +000069 FixedDoubleArray);
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +000070}
71
72
kasperl@chromium.org86f77b72009-07-06 08:21:57 +000073Handle<StringDictionary> Factory::NewStringDictionary(int at_least_space_for) {
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +000074 ASSERT(0 <= at_least_space_for);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000075 CALL_HEAP_FUNCTION(isolate(),
76 StringDictionary::Allocate(at_least_space_for),
kasperl@chromium.org86f77b72009-07-06 08:21:57 +000077 StringDictionary);
78}
79
80
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +000081Handle<SeededNumberDictionary> Factory::NewSeededNumberDictionary(
82 int at_least_space_for) {
kasperl@chromium.org86f77b72009-07-06 08:21:57 +000083 ASSERT(0 <= at_least_space_for);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000084 CALL_HEAP_FUNCTION(isolate(),
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +000085 SeededNumberDictionary::Allocate(at_least_space_for),
86 SeededNumberDictionary);
87}
88
89
90Handle<UnseededNumberDictionary> Factory::NewUnseededNumberDictionary(
91 int at_least_space_for) {
92 ASSERT(0 <= at_least_space_for);
93 CALL_HEAP_FUNCTION(isolate(),
94 UnseededNumberDictionary::Allocate(at_least_space_for),
95 UnseededNumberDictionary);
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +000096}
97
98
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +000099Handle<ObjectHashSet> Factory::NewObjectHashSet(int at_least_space_for) {
100 ASSERT(0 <= at_least_space_for);
101 CALL_HEAP_FUNCTION(isolate(),
102 ObjectHashSet::Allocate(at_least_space_for),
103 ObjectHashSet);
104}
105
106
vegorov@chromium.org7943d462011-08-01 11:41:52 +0000107Handle<ObjectHashTable> Factory::NewObjectHashTable(int at_least_space_for) {
108 ASSERT(0 <= at_least_space_for);
109 CALL_HEAP_FUNCTION(isolate(),
110 ObjectHashTable::Allocate(at_least_space_for),
111 ObjectHashTable);
112}
113
114
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000115Handle<DescriptorArray> Factory::NewDescriptorArray(int number_of_descriptors) {
116 ASSERT(0 <= number_of_descriptors);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000117 CALL_HEAP_FUNCTION(isolate(),
verwaest@chromium.orgde64f722012-08-16 15:44:54 +0000118 DescriptorArray::Allocate(number_of_descriptors),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000119 DescriptorArray);
120}
121
122
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000123Handle<DeoptimizationInputData> Factory::NewDeoptimizationInputData(
124 int deopt_entry_count,
125 PretenureFlag pretenure) {
126 ASSERT(deopt_entry_count > 0);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000127 CALL_HEAP_FUNCTION(isolate(),
128 DeoptimizationInputData::Allocate(deopt_entry_count,
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000129 pretenure),
130 DeoptimizationInputData);
131}
132
133
134Handle<DeoptimizationOutputData> Factory::NewDeoptimizationOutputData(
135 int deopt_entry_count,
136 PretenureFlag pretenure) {
137 ASSERT(deopt_entry_count > 0);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000138 CALL_HEAP_FUNCTION(isolate(),
139 DeoptimizationOutputData::Allocate(deopt_entry_count,
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000140 pretenure),
141 DeoptimizationOutputData);
142}
143
144
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000145Handle<AccessorPair> Factory::NewAccessorPair() {
146 CALL_HEAP_FUNCTION(isolate(),
147 isolate()->heap()->AllocateAccessorPair(),
148 AccessorPair);
149}
150
151
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000152Handle<TypeFeedbackInfo> Factory::NewTypeFeedbackInfo() {
153 CALL_HEAP_FUNCTION(isolate(),
154 isolate()->heap()->AllocateTypeFeedbackInfo(),
155 TypeFeedbackInfo);
156}
157
158
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000159// Symbols are created in the old generation (data space).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000160Handle<String> Factory::LookupSymbol(Vector<const char> string) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000161 CALL_HEAP_FUNCTION(isolate(),
162 isolate()->heap()->LookupSymbol(string),
163 String);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000164}
165
danno@chromium.org40cb8782011-05-25 07:58:50 +0000166// Symbols are created in the old generation (data space).
167Handle<String> Factory::LookupSymbol(Handle<String> string) {
168 CALL_HEAP_FUNCTION(isolate(),
169 isolate()->heap()->LookupSymbol(*string),
170 String);
171}
172
fschneider@chromium.org9e3e0b62011-01-03 10:16:46 +0000173Handle<String> Factory::LookupAsciiSymbol(Vector<const char> string) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000174 CALL_HEAP_FUNCTION(isolate(),
175 isolate()->heap()->LookupAsciiSymbol(string),
176 String);
fschneider@chromium.org9e3e0b62011-01-03 10:16:46 +0000177}
178
danno@chromium.org40cb8782011-05-25 07:58:50 +0000179
180Handle<String> Factory::LookupAsciiSymbol(Handle<SeqAsciiString> string,
181 int from,
182 int length) {
183 CALL_HEAP_FUNCTION(isolate(),
184 isolate()->heap()->LookupAsciiSymbol(string,
185 from,
186 length),
187 String);
188}
189
190
fschneider@chromium.org9e3e0b62011-01-03 10:16:46 +0000191Handle<String> Factory::LookupTwoByteSymbol(Vector<const uc16> string) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000192 CALL_HEAP_FUNCTION(isolate(),
193 isolate()->heap()->LookupTwoByteSymbol(string),
194 String);
fschneider@chromium.org9e3e0b62011-01-03 10:16:46 +0000195}
196
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000197
198Handle<String> Factory::NewStringFromAscii(Vector<const char> string,
199 PretenureFlag pretenure) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000200 CALL_HEAP_FUNCTION(
201 isolate(),
202 isolate()->heap()->AllocateStringFromAscii(string, pretenure),
203 String);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000204}
205
206Handle<String> Factory::NewStringFromUtf8(Vector<const char> string,
207 PretenureFlag pretenure) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000208 CALL_HEAP_FUNCTION(
209 isolate(),
210 isolate()->heap()->AllocateStringFromUtf8(string, pretenure),
211 String);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000212}
213
214
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000215Handle<String> Factory::NewStringFromTwoByte(Vector<const uc16> string,
216 PretenureFlag pretenure) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000217 CALL_HEAP_FUNCTION(
218 isolate(),
219 isolate()->heap()->AllocateStringFromTwoByte(string, pretenure),
220 String);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000221}
222
223
ager@chromium.org04921a82011-06-27 13:21:41 +0000224Handle<SeqAsciiString> Factory::NewRawAsciiString(int length,
225 PretenureFlag pretenure) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000226 CALL_HEAP_FUNCTION(
227 isolate(),
228 isolate()->heap()->AllocateRawAsciiString(length, pretenure),
ager@chromium.org04921a82011-06-27 13:21:41 +0000229 SeqAsciiString);
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +0000230}
231
232
ager@chromium.org04921a82011-06-27 13:21:41 +0000233Handle<SeqTwoByteString> Factory::NewRawTwoByteString(int length,
234 PretenureFlag pretenure) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000235 CALL_HEAP_FUNCTION(
236 isolate(),
237 isolate()->heap()->AllocateRawTwoByteString(length, pretenure),
ager@chromium.org04921a82011-06-27 13:21:41 +0000238 SeqTwoByteString);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000239}
240
241
242Handle<String> Factory::NewConsString(Handle<String> first,
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000243 Handle<String> second) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000244 CALL_HEAP_FUNCTION(isolate(),
245 isolate()->heap()->AllocateConsString(*first, *second),
246 String);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000247}
248
249
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000250Handle<String> Factory::NewSubString(Handle<String> str,
251 int begin,
252 int end) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000253 CALL_HEAP_FUNCTION(isolate(),
254 str->SubString(begin, end),
255 String);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000256}
257
258
ager@chromium.org04921a82011-06-27 13:21:41 +0000259Handle<String> Factory::NewProperSubString(Handle<String> str,
260 int begin,
261 int end) {
262 ASSERT(begin > 0 || end < str->length());
263 CALL_HEAP_FUNCTION(isolate(),
264 isolate()->heap()->AllocateSubString(*str, begin, end),
265 String);
266}
267
268
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000269Handle<String> Factory::NewExternalStringFromAscii(
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000270 const ExternalAsciiString::Resource* resource) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000271 CALL_HEAP_FUNCTION(
272 isolate(),
273 isolate()->heap()->AllocateExternalStringFromAscii(resource),
274 String);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000275}
276
277
278Handle<String> Factory::NewExternalStringFromTwoByte(
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000279 const ExternalTwoByteString::Resource* resource) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000280 CALL_HEAP_FUNCTION(
281 isolate(),
282 isolate()->heap()->AllocateExternalStringFromTwoByte(resource),
283 String);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000284}
285
286
287Handle<Context> Factory::NewGlobalContext() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000288 CALL_HEAP_FUNCTION(
289 isolate(),
290 isolate()->heap()->AllocateGlobalContext(),
291 Context);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000292}
293
294
danno@chromium.org81cac2b2012-07-10 11:28:27 +0000295Handle<Context> Factory::NewModuleContext(Handle<ScopeInfo> scope_info) {
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000296 CALL_HEAP_FUNCTION(
297 isolate(),
danno@chromium.org81cac2b2012-07-10 11:28:27 +0000298 isolate()->heap()->AllocateModuleContext(*scope_info),
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000299 Context);
300}
301
302
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000303Handle<Context> Factory::NewFunctionContext(int length,
vegorov@chromium.org3cf47312011-06-29 13:20:01 +0000304 Handle<JSFunction> function) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000305 CALL_HEAP_FUNCTION(
306 isolate(),
vegorov@chromium.org3cf47312011-06-29 13:20:01 +0000307 isolate()->heap()->AllocateFunctionContext(length, *function),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000308 Context);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000309}
310
311
vegorov@chromium.org3cf47312011-06-29 13:20:01 +0000312Handle<Context> Factory::NewCatchContext(Handle<JSFunction> function,
313 Handle<Context> previous,
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000314 Handle<String> name,
315 Handle<Object> thrown_object) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000316 CALL_HEAP_FUNCTION(
317 isolate(),
vegorov@chromium.org3cf47312011-06-29 13:20:01 +0000318 isolate()->heap()->AllocateCatchContext(*function,
319 *previous,
320 *name,
321 *thrown_object),
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000322 Context);
323}
324
325
vegorov@chromium.org3cf47312011-06-29 13:20:01 +0000326Handle<Context> Factory::NewWithContext(Handle<JSFunction> function,
327 Handle<Context> previous,
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000328 Handle<JSObject> extension) {
329 CALL_HEAP_FUNCTION(
330 isolate(),
vegorov@chromium.org3cf47312011-06-29 13:20:01 +0000331 isolate()->heap()->AllocateWithContext(*function, *previous, *extension),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000332 Context);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000333}
334
335
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000336Handle<Context> Factory::NewBlockContext(Handle<JSFunction> function,
337 Handle<Context> previous,
338 Handle<ScopeInfo> scope_info) {
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000339 CALL_HEAP_FUNCTION(
340 isolate(),
341 isolate()->heap()->AllocateBlockContext(*function,
342 *previous,
343 *scope_info),
344 Context);
345}
346
347
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000348Handle<Struct> Factory::NewStruct(InstanceType type) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000349 CALL_HEAP_FUNCTION(
350 isolate(),
351 isolate()->heap()->AllocateStruct(type),
352 Struct);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000353}
354
355
356Handle<AccessorInfo> Factory::NewAccessorInfo() {
357 Handle<AccessorInfo> info =
358 Handle<AccessorInfo>::cast(NewStruct(ACCESSOR_INFO_TYPE));
359 info->set_flag(0); // Must clear the flag, it was initialized as undefined.
360 return info;
361}
362
363
364Handle<Script> Factory::NewScript(Handle<String> source) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000365 // Generate id for this script.
366 int id;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000367 Heap* heap = isolate()->heap();
368 if (heap->last_script_id()->IsUndefined()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000369 // Script ids start from one.
370 id = 1;
371 } else {
372 // Increment id, wrap when positive smi is exhausted.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000373 id = Smi::cast(heap->last_script_id())->value();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000374 id++;
375 if (!Smi::IsValid(id)) {
376 id = 0;
377 }
378 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000379 heap->SetLastScriptId(Smi::FromInt(id));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000380
381 // Create and initialize script object.
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000382 Handle<Foreign> wrapper = NewForeign(0, TENURED);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000383 Handle<Script> script = Handle<Script>::cast(NewStruct(SCRIPT_TYPE));
384 script->set_source(*source);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000385 script->set_name(heap->undefined_value());
386 script->set_id(heap->last_script_id());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000387 script->set_line_offset(Smi::FromInt(0));
388 script->set_column_offset(Smi::FromInt(0));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000389 script->set_data(heap->undefined_value());
390 script->set_context_data(heap->undefined_value());
ager@chromium.orge2902be2009-06-08 12:21:35 +0000391 script->set_type(Smi::FromInt(Script::TYPE_NORMAL));
392 script->set_compilation_type(Smi::FromInt(Script::COMPILATION_TYPE_HOST));
rossberg@chromium.org2c067b12012-03-19 11:01:52 +0000393 script->set_compilation_state(
394 Smi::FromInt(Script::COMPILATION_STATE_INITIAL));
ager@chromium.org9085a012009-05-11 19:22:57 +0000395 script->set_wrapper(*wrapper);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000396 script->set_line_ends(heap->undefined_value());
397 script->set_eval_from_shared(heap->undefined_value());
ager@chromium.orge2902be2009-06-08 12:21:35 +0000398 script->set_eval_from_instructions_offset(Smi::FromInt(0));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000399
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000400 return script;
401}
402
403
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000404Handle<Foreign> Factory::NewForeign(Address addr, PretenureFlag pretenure) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000405 CALL_HEAP_FUNCTION(isolate(),
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000406 isolate()->heap()->AllocateForeign(addr, pretenure),
407 Foreign);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000408}
409
410
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000411Handle<Foreign> Factory::NewForeign(const AccessorDescriptor* desc) {
412 return NewForeign((Address) desc, TENURED);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000413}
414
415
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000416Handle<ByteArray> Factory::NewByteArray(int length, PretenureFlag pretenure) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000417 ASSERT(0 <= length);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000418 CALL_HEAP_FUNCTION(
419 isolate(),
420 isolate()->heap()->AllocateByteArray(length, pretenure),
421 ByteArray);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000422}
423
424
ager@chromium.org3811b432009-10-28 14:53:37 +0000425Handle<ExternalArray> Factory::NewExternalArray(int length,
426 ExternalArrayType array_type,
427 void* external_pointer,
428 PretenureFlag pretenure) {
429 ASSERT(0 <= length);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000430 CALL_HEAP_FUNCTION(
431 isolate(),
432 isolate()->heap()->AllocateExternalArray(length,
433 array_type,
434 external_pointer,
435 pretenure),
436 ExternalArray);
ager@chromium.org3811b432009-10-28 14:53:37 +0000437}
438
439
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000440Handle<JSGlobalPropertyCell> Factory::NewJSGlobalPropertyCell(
441 Handle<Object> value) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000442 CALL_HEAP_FUNCTION(
443 isolate(),
444 isolate()->heap()->AllocateJSGlobalPropertyCell(*value),
445 JSGlobalPropertyCell);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000446}
447
448
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000449Handle<Map> Factory::NewMap(InstanceType type,
450 int instance_size,
451 ElementsKind elements_kind) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000452 CALL_HEAP_FUNCTION(
453 isolate(),
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000454 isolate()->heap()->AllocateMap(type, instance_size, elements_kind),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000455 Map);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000456}
457
458
459Handle<JSObject> Factory::NewFunctionPrototype(Handle<JSFunction> function) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000460 CALL_HEAP_FUNCTION(
461 isolate(),
462 isolate()->heap()->AllocateFunctionPrototype(*function),
463 JSObject);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000464}
465
466
rossberg@chromium.org657d53b2012-07-12 11:06:03 +0000467Handle<Map> Factory::CopyWithPreallocatedFieldDescriptors(Handle<Map> src) {
468 CALL_HEAP_FUNCTION(
469 isolate(), src->CopyWithPreallocatedFieldDescriptors(), Map);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000470}
471
472
ager@chromium.org32912102009-01-16 10:38:43 +0000473Handle<Map> Factory::CopyMap(Handle<Map> src,
474 int extra_inobject_properties) {
rossberg@chromium.org657d53b2012-07-12 11:06:03 +0000475 Handle<Map> copy = CopyWithPreallocatedFieldDescriptors(src);
ager@chromium.org32912102009-01-16 10:38:43 +0000476 // Check that we do not overflow the instance size when adding the
477 // extra inobject properties.
478 int instance_size_delta = extra_inobject_properties * kPointerSize;
479 int max_instance_size_delta =
480 JSObject::kMaxInstanceSize - copy->instance_size();
481 if (instance_size_delta > max_instance_size_delta) {
482 // If the instance size overflows, we allocate as many properties
483 // as we can as inobject properties.
484 instance_size_delta = max_instance_size_delta;
485 extra_inobject_properties = max_instance_size_delta >> kPointerSizeLog2;
486 }
487 // Adjust the map with the extra inobject properties.
488 int inobject_properties =
489 copy->inobject_properties() + extra_inobject_properties;
490 copy->set_inobject_properties(inobject_properties);
491 copy->set_unused_property_fields(inobject_properties);
492 copy->set_instance_size(copy->instance_size() + instance_size_delta);
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000493 copy->set_visitor_id(StaticVisitorBase::GetVisitorId(*copy));
ager@chromium.org32912102009-01-16 10:38:43 +0000494 return copy;
495}
496
fschneider@chromium.org40b9da32010-06-28 11:29:21 +0000497
verwaest@chromium.org753aee42012-07-17 16:15:42 +0000498Handle<Map> Factory::CopyMap(Handle<Map> src) {
verwaest@chromium.orgde64f722012-08-16 15:44:54 +0000499 CALL_HEAP_FUNCTION(isolate(), src->Copy(), Map);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000500}
501
502
kmillikin@chromium.org83e16822011-09-13 08:21:47 +0000503Handle<Map> Factory::GetElementsTransitionMap(
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000504 Handle<JSObject> src,
505 ElementsKind elements_kind) {
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000506 Isolate* i = isolate();
507 CALL_HEAP_FUNCTION(i,
508 src->GetElementsTransitionMap(i, elements_kind),
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000509 Map);
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +0000510}
511
512
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000513Handle<FixedArray> Factory::CopyFixedArray(Handle<FixedArray> array) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000514 CALL_HEAP_FUNCTION(isolate(), array->Copy(), FixedArray);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000515}
516
517
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000518Handle<FixedDoubleArray> Factory::CopyFixedDoubleArray(
519 Handle<FixedDoubleArray> array) {
520 CALL_HEAP_FUNCTION(isolate(), array->Copy(), FixedDoubleArray);
521}
522
523
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000524Handle<JSFunction> Factory::BaseNewFunctionFromSharedFunctionInfo(
525 Handle<SharedFunctionInfo> function_info,
sgjesse@chromium.org846fb742009-12-18 08:56:33 +0000526 Handle<Map> function_map,
527 PretenureFlag pretenure) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000528 CALL_HEAP_FUNCTION(
529 isolate(),
530 isolate()->heap()->AllocateFunction(*function_map,
531 *function_info,
532 isolate()->heap()->the_hole_value(),
533 pretenure),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000534 JSFunction);
535}
536
537
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000538Handle<JSFunction> Factory::NewFunctionFromSharedFunctionInfo(
539 Handle<SharedFunctionInfo> function_info,
sgjesse@chromium.org846fb742009-12-18 08:56:33 +0000540 Handle<Context> context,
541 PretenureFlag pretenure) {
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000542 Handle<JSFunction> result = BaseNewFunctionFromSharedFunctionInfo(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000543 function_info,
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000544 function_info->is_classic_mode()
545 ? isolate()->function_map()
546 : isolate()->strict_mode_function_map(),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000547 pretenure);
548
jkummerow@chromium.org1456e702012-03-30 08:38:13 +0000549 if (function_info->ic_age() != isolate()->heap()->global_ic_age()) {
550 function_info->ResetForNewContext(isolate()->heap()->global_ic_age());
551 }
552
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000553 result->set_context(*context);
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000554
ulan@chromium.orgd9e468a2012-06-25 09:47:40 +0000555 int index = function_info->SearchOptimizedCodeMap(context->global_context());
556 if (!function_info->bound() && index < 0) {
557 int number_of_literals = function_info->num_literals();
558 Handle<FixedArray> literals = NewFixedArray(number_of_literals, pretenure);
559 if (number_of_literals > 0) {
560 // Store the global context in the literals array prefix. This
561 // context will be used when creating object, regexp and array
562 // literals in this function.
563 literals->set(JSFunction::kLiteralGlobalContextIndex,
564 context->global_context());
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000565 }
ulan@chromium.orgd9e468a2012-06-25 09:47:40 +0000566 result->set_literals(*literals);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000567 }
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000568
569 if (index > 0) {
570 // Caching of optimized code enabled and optimized code found.
ulan@chromium.orgd9e468a2012-06-25 09:47:40 +0000571 function_info->InstallFromOptimizedCodeMap(*result, index);
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000572 return result;
573 }
574
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000575 if (V8::UseCrankshaft() &&
576 FLAG_always_opt &&
577 result->is_compiled() &&
578 !function_info->is_toplevel() &&
rossberg@chromium.org2c067b12012-03-19 11:01:52 +0000579 function_info->allows_lazy_compilation() &&
580 !function_info->optimization_disabled()) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000581 result->MarkForLazyRecompilation();
582 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000583 return result;
584}
585
586
587Handle<Object> Factory::NewNumber(double value,
588 PretenureFlag pretenure) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000589 CALL_HEAP_FUNCTION(
590 isolate(),
591 isolate()->heap()->NumberFromDouble(value, pretenure), Object);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000592}
593
594
erikcorry0ad885c2011-11-21 13:51:57 +0000595Handle<Object> Factory::NewNumberFromInt(int32_t value,
596 PretenureFlag pretenure) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000597 CALL_HEAP_FUNCTION(
598 isolate(),
erikcorry0ad885c2011-11-21 13:51:57 +0000599 isolate()->heap()->NumberFromInt32(value, pretenure), Object);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000600}
601
602
erikcorry0ad885c2011-11-21 13:51:57 +0000603Handle<Object> Factory::NewNumberFromUint(uint32_t value,
604 PretenureFlag pretenure) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000605 CALL_HEAP_FUNCTION(
606 isolate(),
erikcorry0ad885c2011-11-21 13:51:57 +0000607 isolate()->heap()->NumberFromUint32(value, pretenure), Object);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000608}
609
610
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000611Handle<JSObject> Factory::NewNeanderObject() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000612 CALL_HEAP_FUNCTION(
613 isolate(),
614 isolate()->heap()->AllocateJSObjectFromMap(
615 isolate()->heap()->neander_map()),
616 JSObject);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000617}
618
619
620Handle<Object> Factory::NewTypeError(const char* type,
621 Vector< Handle<Object> > args) {
622 return NewError("MakeTypeError", type, args);
623}
624
625
626Handle<Object> Factory::NewTypeError(Handle<String> message) {
627 return NewError("$TypeError", message);
628}
629
630
631Handle<Object> Factory::NewRangeError(const char* type,
632 Vector< Handle<Object> > args) {
633 return NewError("MakeRangeError", type, args);
634}
635
636
637Handle<Object> Factory::NewRangeError(Handle<String> message) {
638 return NewError("$RangeError", message);
639}
640
641
642Handle<Object> Factory::NewSyntaxError(const char* type, Handle<JSArray> args) {
643 return NewError("MakeSyntaxError", type, args);
644}
645
646
647Handle<Object> Factory::NewSyntaxError(Handle<String> message) {
648 return NewError("$SyntaxError", message);
649}
650
651
652Handle<Object> Factory::NewReferenceError(const char* type,
653 Vector< Handle<Object> > args) {
654 return NewError("MakeReferenceError", type, args);
655}
656
657
658Handle<Object> Factory::NewReferenceError(Handle<String> message) {
659 return NewError("$ReferenceError", message);
660}
661
662
663Handle<Object> Factory::NewError(const char* maker, const char* type,
664 Vector< Handle<Object> > args) {
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000665 v8::HandleScope scope; // Instantiate a closeable HandleScope for EscapeFrom.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000666 Handle<FixedArray> array = NewFixedArray(args.length());
ager@chromium.org7c537e22008-10-16 08:43:32 +0000667 for (int i = 0; i < args.length(); i++) {
668 array->set(i, *args[i]);
669 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000670 Handle<JSArray> object = NewJSArrayWithElements(array);
ager@chromium.org7c537e22008-10-16 08:43:32 +0000671 Handle<Object> result = NewError(maker, type, object);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000672 return result.EscapeFrom(&scope);
673}
674
675
676Handle<Object> Factory::NewEvalError(const char* type,
677 Vector< Handle<Object> > args) {
678 return NewError("MakeEvalError", type, args);
679}
680
681
682Handle<Object> Factory::NewError(const char* type,
683 Vector< Handle<Object> > args) {
684 return NewError("MakeError", type, args);
685}
686
687
verwaest@chromium.org37141392012-05-31 13:27:02 +0000688Handle<String> Factory::EmergencyNewError(const char* type,
689 Handle<JSArray> args) {
690 const int kBufferSize = 1000;
691 char buffer[kBufferSize];
692 size_t space = kBufferSize;
693 char* p = &buffer[0];
694
695 Vector<char> v(buffer, kBufferSize);
696 OS::StrNCpy(v, type, space);
697 space -= Min(space, strlen(type));
698 p = &buffer[kBufferSize] - space;
699
700 for (unsigned i = 0; i < ARRAY_SIZE(args); i++) {
701 if (space > 0) {
702 *p++ = ' ';
703 space--;
704 if (space > 0) {
705 MaybeObject* maybe_arg = args->GetElement(i);
706 Handle<String> arg_str(reinterpret_cast<String*>(maybe_arg));
707 const char* arg = *arg_str->ToCString();
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000708 Vector<char> v2(p, static_cast<int>(space));
verwaest@chromium.org37141392012-05-31 13:27:02 +0000709 OS::StrNCpy(v2, arg, space);
710 space -= Min(space, strlen(arg));
711 p = &buffer[kBufferSize] - space;
712 }
713 }
714 }
715 if (space > 0) {
716 *p = '\0';
717 } else {
718 buffer[kBufferSize - 1] = '\0';
719 }
720 Handle<String> error_string = NewStringFromUtf8(CStrVector(buffer), TENURED);
721 return error_string;
722}
723
724
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000725Handle<Object> Factory::NewError(const char* maker,
726 const char* type,
727 Handle<JSArray> args) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000728 Handle<String> make_str = LookupAsciiSymbol(maker);
729 Handle<Object> fun_obj(
730 isolate()->js_builtins_object()->GetPropertyNoExceptionThrown(*make_str));
ager@chromium.org4af710e2009-09-15 12:20:11 +0000731 // If the builtins haven't been properly configured yet this error
732 // constructor may not have been defined. Bail out.
verwaest@chromium.org37141392012-05-31 13:27:02 +0000733 if (!fun_obj->IsJSFunction()) {
734 return EmergencyNewError(type, args);
735 }
ager@chromium.org4af710e2009-09-15 12:20:11 +0000736 Handle<JSFunction> fun = Handle<JSFunction>::cast(fun_obj);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000737 Handle<Object> type_obj = LookupAsciiSymbol(type);
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +0000738 Handle<Object> argv[] = { type_obj, args };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000739
740 // Invoke the JavaScript factory method. If an exception is thrown while
741 // running the factory method, use the exception as the result.
742 bool caught_exception;
743 Handle<Object> result = Execution::TryCall(fun,
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +0000744 isolate()->js_builtins_object(),
745 ARRAY_SIZE(argv),
746 argv,
747 &caught_exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000748 return result;
749}
750
751
752Handle<Object> Factory::NewError(Handle<String> message) {
753 return NewError("$Error", message);
754}
755
756
757Handle<Object> Factory::NewError(const char* constructor,
758 Handle<String> message) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000759 Handle<String> constr = LookupAsciiSymbol(constructor);
760 Handle<JSFunction> fun = Handle<JSFunction>(
761 JSFunction::cast(isolate()->js_builtins_object()->
762 GetPropertyNoExceptionThrown(*constr)));
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +0000763 Handle<Object> argv[] = { message };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000764
765 // Invoke the JavaScript factory method. If an exception is thrown while
766 // running the factory method, use the exception as the result.
767 bool caught_exception;
768 Handle<Object> result = Execution::TryCall(fun,
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +0000769 isolate()->js_builtins_object(),
770 ARRAY_SIZE(argv),
771 argv,
772 &caught_exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000773 return result;
774}
775
776
777Handle<JSFunction> Factory::NewFunction(Handle<String> name,
778 InstanceType type,
779 int instance_size,
780 Handle<Code> code,
781 bool force_initial_map) {
782 // Allocate the function
783 Handle<JSFunction> function = NewFunction(name, the_hole_value());
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000784
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000785 // Set up the code pointer in both the shared function info and in
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000786 // the function itself.
787 function->shared()->set_code(*code);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000788 function->set_code(*code);
789
790 if (force_initial_map ||
791 type != JS_OBJECT_TYPE ||
792 instance_size != JSObject::kHeaderSize) {
793 Handle<Map> initial_map = NewMap(type, instance_size);
794 Handle<JSObject> prototype = NewFunctionPrototype(function);
795 initial_map->set_prototype(*prototype);
796 function->set_initial_map(*initial_map);
797 initial_map->set_constructor(*function);
798 } else {
799 ASSERT(!function->has_initial_map());
800 ASSERT(!function->has_prototype());
801 }
802
803 return function;
804}
805
806
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000807Handle<JSFunction> Factory::NewFunctionWithPrototype(Handle<String> name,
808 InstanceType type,
809 int instance_size,
810 Handle<JSObject> prototype,
811 Handle<Code> code,
812 bool force_initial_map) {
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000813 // Allocate the function.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000814 Handle<JSFunction> function = NewFunction(name, prototype);
815
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000816 // Set up the code pointer in both the shared function info and in
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000817 // the function itself.
818 function->shared()->set_code(*code);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000819 function->set_code(*code);
820
821 if (force_initial_map ||
822 type != JS_OBJECT_TYPE ||
823 instance_size != JSObject::kHeaderSize) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000824 Handle<Map> initial_map = NewMap(type,
825 instance_size,
svenpanne@chromium.org830d30c2012-05-29 13:20:14 +0000826 GetInitialFastElementsKind());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000827 function->set_initial_map(*initial_map);
828 initial_map->set_constructor(*function);
829 }
830
831 // Set function.prototype and give the prototype a constructor
832 // property that refers to the function.
833 SetPrototypeProperty(function, prototype);
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +0000834 // Currently safe because it is only invoked from Genesis.
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000835 CHECK_NOT_EMPTY_HANDLE(isolate(),
836 JSObject::SetLocalPropertyIgnoreAttributes(
837 prototype, constructor_symbol(),
838 function, DONT_ENUM));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000839 return function;
840}
841
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000842
kmillikin@chromium.org4111b802010-05-03 10:34:42 +0000843Handle<JSFunction> Factory::NewFunctionWithoutPrototype(Handle<String> name,
844 Handle<Code> code) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000845 Handle<JSFunction> function = NewFunctionWithoutPrototype(name,
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000846 CLASSIC_MODE);
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000847 function->shared()->set_code(*code);
kmillikin@chromium.org4111b802010-05-03 10:34:42 +0000848 function->set_code(*code);
849 ASSERT(!function->has_initial_map());
850 ASSERT(!function->has_prototype());
851 return function;
852}
853
854
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000855Handle<ScopeInfo> Factory::NewScopeInfo(int length) {
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000856 CALL_HEAP_FUNCTION(
857 isolate(),
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000858 isolate()->heap()->AllocateScopeInfo(length),
859 ScopeInfo);
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000860}
861
862
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000863Handle<Code> Factory::NewCode(const CodeDesc& desc,
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000864 Code::Flags flags,
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +0000865 Handle<Object> self_ref,
866 bool immovable) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000867 CALL_HEAP_FUNCTION(isolate(),
868 isolate()->heap()->CreateCode(
869 desc, flags, self_ref, immovable),
870 Code);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000871}
872
873
874Handle<Code> Factory::CopyCode(Handle<Code> code) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000875 CALL_HEAP_FUNCTION(isolate(),
876 isolate()->heap()->CopyCode(*code),
877 Code);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000878}
879
880
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000881Handle<Code> Factory::CopyCode(Handle<Code> code, Vector<byte> reloc_info) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000882 CALL_HEAP_FUNCTION(isolate(),
883 isolate()->heap()->CopyCode(*code, reloc_info),
884 Code);
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000885}
886
887
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000888Handle<String> Factory::SymbolFromString(Handle<String> value) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000889 CALL_HEAP_FUNCTION(isolate(),
890 isolate()->heap()->LookupSymbol(*value), String);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000891}
892
893
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000894Handle<JSObject> Factory::NewJSObject(Handle<JSFunction> constructor,
895 PretenureFlag pretenure) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000896 CALL_HEAP_FUNCTION(
897 isolate(),
898 isolate()->heap()->AllocateJSObject(*constructor, pretenure), JSObject);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000899}
900
901
danno@chromium.org81cac2b2012-07-10 11:28:27 +0000902Handle<JSModule> Factory::NewJSModule(Handle<Context> context,
903 Handle<ScopeInfo> scope_info) {
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000904 CALL_HEAP_FUNCTION(
905 isolate(),
danno@chromium.org81cac2b2012-07-10 11:28:27 +0000906 isolate()->heap()->AllocateJSModule(*context, *scope_info), JSModule);
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000907}
908
909
kasperl@chromium.org2abc4502009-07-02 07:00:29 +0000910Handle<GlobalObject> Factory::NewGlobalObject(
911 Handle<JSFunction> constructor) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000912 CALL_HEAP_FUNCTION(isolate(),
913 isolate()->heap()->AllocateGlobalObject(*constructor),
kasperl@chromium.org2abc4502009-07-02 07:00:29 +0000914 GlobalObject);
915}
916
917
918
ager@chromium.org236ad962008-09-25 09:45:57 +0000919Handle<JSObject> Factory::NewJSObjectFromMap(Handle<Map> map) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000920 CALL_HEAP_FUNCTION(
921 isolate(),
922 isolate()->heap()->AllocateJSObjectFromMap(*map, NOT_TENURED),
923 JSObject);
ager@chromium.org236ad962008-09-25 09:45:57 +0000924}
925
926
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +0000927Handle<JSArray> Factory::NewJSArray(int capacity,
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000928 ElementsKind elements_kind,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000929 PretenureFlag pretenure) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000930 CALL_HEAP_FUNCTION(isolate(),
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000931 isolate()->heap()->AllocateJSArrayAndStorage(
932 elements_kind,
933 0,
934 capacity,
935 INITIALIZE_ARRAY_ELEMENTS_WITH_HOLE,
936 pretenure),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000937 JSArray);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000938}
939
940
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +0000941Handle<JSArray> Factory::NewJSArrayWithElements(Handle<FixedArrayBase> elements,
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000942 ElementsKind elements_kind,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000943 PretenureFlag pretenure) {
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000944 CALL_HEAP_FUNCTION(
945 isolate(),
946 isolate()->heap()->AllocateJSArrayWithElements(*elements,
947 elements_kind,
948 pretenure),
949 JSArray);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000950}
951
952
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +0000953void Factory::SetElementsCapacityAndLength(Handle<JSArray> array,
954 int capacity,
955 int length) {
956 ElementsAccessor* accessor = array->GetElementsAccessor();
957 CALL_HEAP_FUNCTION_VOID(
958 isolate(),
959 accessor->SetCapacityAndLength(*array, capacity, length));
960}
961
962
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000963void Factory::SetContent(Handle<JSArray> array,
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +0000964 Handle<FixedArrayBase> elements) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000965 CALL_HEAP_FUNCTION_VOID(
966 isolate(),
967 array->SetContent(*elements));
968}
969
970
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +0000971void Factory::EnsureCanContainHeapObjectElements(Handle<JSArray> array) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000972 CALL_HEAP_FUNCTION_VOID(
973 isolate(),
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +0000974 array->EnsureCanContainHeapObjectElements());
975}
976
977
978void Factory::EnsureCanContainElements(Handle<JSArray> array,
979 Handle<FixedArrayBase> elements,
svenpanne@chromium.org830d30c2012-05-29 13:20:14 +0000980 uint32_t length,
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +0000981 EnsureElementsMode mode) {
982 CALL_HEAP_FUNCTION_VOID(
983 isolate(),
svenpanne@chromium.org830d30c2012-05-29 13:20:14 +0000984 array->EnsureCanContainElements(*elements, length, mode));
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000985}
986
987
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000988Handle<JSProxy> Factory::NewJSProxy(Handle<Object> handler,
989 Handle<Object> prototype) {
990 CALL_HEAP_FUNCTION(
991 isolate(),
992 isolate()->heap()->AllocateJSProxy(*handler, *prototype),
993 JSProxy);
994}
995
996
lrn@chromium.org34e60782011-09-15 07:25:40 +0000997void Factory::BecomeJSObject(Handle<JSReceiver> object) {
rossberg@chromium.org717967f2011-07-20 13:44:42 +0000998 CALL_HEAP_FUNCTION_VOID(
999 isolate(),
lrn@chromium.org34e60782011-09-15 07:25:40 +00001000 isolate()->heap()->ReinitializeJSReceiver(
1001 *object, JS_OBJECT_TYPE, JSObject::kHeaderSize));
1002}
1003
1004
1005void Factory::BecomeJSFunction(Handle<JSReceiver> object) {
1006 CALL_HEAP_FUNCTION_VOID(
1007 isolate(),
1008 isolate()->heap()->ReinitializeJSReceiver(
1009 *object, JS_FUNCTION_TYPE, JSFunction::kSize));
rossberg@chromium.org717967f2011-07-20 13:44:42 +00001010}
1011
1012
jkummerow@chromium.org000f7fb2012-08-01 11:14:42 +00001013void Factory::SetIdentityHash(Handle<JSObject> object, Smi* hash) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001014 CALL_HEAP_FUNCTION_VOID(
1015 isolate(),
1016 object->SetIdentityHash(hash, ALLOW_CREATION));
1017}
1018
1019
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001020Handle<SharedFunctionInfo> Factory::NewSharedFunctionInfo(
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00001021 Handle<String> name,
1022 int number_of_literals,
1023 Handle<Code> code,
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001024 Handle<ScopeInfo> scope_info) {
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001025 Handle<SharedFunctionInfo> shared = NewSharedFunctionInfo(name);
1026 shared->set_code(*code);
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00001027 shared->set_scope_info(*scope_info);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001028 int literals_array_size = number_of_literals;
1029 // If the function contains object, regexp or array literals,
1030 // allocate extra space for a literals array prefix containing the
1031 // context.
1032 if (number_of_literals > 0) {
1033 literals_array_size += JSFunction::kLiteralsPrefixSize;
1034 }
1035 shared->set_num_literals(literals_array_size);
1036 return shared;
1037}
1038
1039
kmillikin@chromium.org31b12772011-02-02 16:08:26 +00001040Handle<JSMessageObject> Factory::NewJSMessageObject(
1041 Handle<String> type,
1042 Handle<JSArray> arguments,
1043 int start_position,
1044 int end_position,
1045 Handle<Object> script,
1046 Handle<Object> stack_trace,
1047 Handle<Object> stack_frames) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001048 CALL_HEAP_FUNCTION(isolate(),
1049 isolate()->heap()->AllocateJSMessageObject(*type,
1050 *arguments,
1051 start_position,
1052 end_position,
1053 *script,
1054 *stack_trace,
1055 *stack_frames),
kmillikin@chromium.org31b12772011-02-02 16:08:26 +00001056 JSMessageObject);
1057}
1058
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001059Handle<SharedFunctionInfo> Factory::NewSharedFunctionInfo(Handle<String> name) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001060 CALL_HEAP_FUNCTION(isolate(),
1061 isolate()->heap()->AllocateSharedFunctionInfo(*name),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001062 SharedFunctionInfo);
1063}
1064
1065
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00001066Handle<String> Factory::NumberToString(Handle<Object> number) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001067 CALL_HEAP_FUNCTION(isolate(),
1068 isolate()->heap()->NumberToString(*number), String);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00001069}
1070
1071
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001072Handle<String> Factory::Uint32ToString(uint32_t value) {
1073 CALL_HEAP_FUNCTION(isolate(),
1074 isolate()->heap()->Uint32ToString(value), String);
1075}
1076
1077
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001078Handle<SeededNumberDictionary> Factory::DictionaryAtNumberPut(
1079 Handle<SeededNumberDictionary> dictionary,
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00001080 uint32_t key,
1081 Handle<Object> value) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001082 CALL_HEAP_FUNCTION(isolate(),
1083 dictionary->AtNumberPut(key, *value),
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001084 SeededNumberDictionary);
1085}
1086
1087
1088Handle<UnseededNumberDictionary> Factory::DictionaryAtNumberPut(
1089 Handle<UnseededNumberDictionary> dictionary,
1090 uint32_t key,
1091 Handle<Object> value) {
1092 CALL_HEAP_FUNCTION(isolate(),
1093 dictionary->AtNumberPut(key, *value),
1094 UnseededNumberDictionary);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001095}
1096
1097
1098Handle<JSFunction> Factory::NewFunctionHelper(Handle<String> name,
1099 Handle<Object> prototype) {
1100 Handle<SharedFunctionInfo> function_share = NewSharedFunctionInfo(name);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001101 CALL_HEAP_FUNCTION(
1102 isolate(),
1103 isolate()->heap()->AllocateFunction(*isolate()->function_map(),
1104 *function_share,
1105 *prototype),
1106 JSFunction);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001107}
1108
1109
1110Handle<JSFunction> Factory::NewFunction(Handle<String> name,
1111 Handle<Object> prototype) {
1112 Handle<JSFunction> fun = NewFunctionHelper(name, prototype);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001113 fun->set_context(isolate()->context()->global_context());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001114 return fun;
1115}
1116
1117
kmillikin@chromium.org4111b802010-05-03 10:34:42 +00001118Handle<JSFunction> Factory::NewFunctionWithoutPrototypeHelper(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001119 Handle<String> name,
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00001120 LanguageMode language_mode) {
kmillikin@chromium.org4111b802010-05-03 10:34:42 +00001121 Handle<SharedFunctionInfo> function_share = NewSharedFunctionInfo(name);
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00001122 Handle<Map> map = (language_mode == CLASSIC_MODE)
1123 ? isolate()->function_without_prototype_map()
1124 : isolate()->strict_mode_function_without_prototype_map();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001125 CALL_HEAP_FUNCTION(isolate(),
1126 isolate()->heap()->AllocateFunction(
1127 *map,
kmillikin@chromium.org4111b802010-05-03 10:34:42 +00001128 *function_share,
1129 *the_hole_value()),
1130 JSFunction);
1131}
1132
1133
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001134Handle<JSFunction> Factory::NewFunctionWithoutPrototype(
1135 Handle<String> name,
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00001136 LanguageMode language_mode) {
1137 Handle<JSFunction> fun =
1138 NewFunctionWithoutPrototypeHelper(name, language_mode);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001139 fun->set_context(isolate()->context()->global_context());
kmillikin@chromium.org4111b802010-05-03 10:34:42 +00001140 return fun;
1141}
1142
1143
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001144Handle<Object> Factory::ToObject(Handle<Object> object) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001145 CALL_HEAP_FUNCTION(isolate(), object->ToObject(), Object);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001146}
1147
1148
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001149Handle<Object> Factory::ToObject(Handle<Object> object,
1150 Handle<Context> global_context) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001151 CALL_HEAP_FUNCTION(isolate(), object->ToObject(*global_context), Object);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001152}
1153
1154
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001155#ifdef ENABLE_DEBUGGER_SUPPORT
v8.team.kasperl727e9952008-09-02 14:56:44 +00001156Handle<DebugInfo> Factory::NewDebugInfo(Handle<SharedFunctionInfo> shared) {
1157 // Get the original code of the function.
1158 Handle<Code> code(shared->code());
1159
1160 // Create a copy of the code before allocating the debug info object to avoid
1161 // allocation while setting up the debug info object.
1162 Handle<Code> original_code(*Factory::CopyCode(code));
1163
1164 // Allocate initial fixed array for active break points before allocating the
1165 // debug info object to avoid allocation while setting up the debug info
1166 // object.
1167 Handle<FixedArray> break_points(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001168 NewFixedArray(Debug::kEstimatedNofBreakPointsInFunction));
v8.team.kasperl727e9952008-09-02 14:56:44 +00001169
1170 // Create and set up the debug info object. Debug info contains function, a
1171 // copy of the original code, the executing code and initial fixed array for
1172 // active break points.
1173 Handle<DebugInfo> debug_info =
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001174 Handle<DebugInfo>::cast(NewStruct(DEBUG_INFO_TYPE));
v8.team.kasperl727e9952008-09-02 14:56:44 +00001175 debug_info->set_shared(*shared);
1176 debug_info->set_original_code(*original_code);
1177 debug_info->set_code(*code);
1178 debug_info->set_break_points(*break_points);
1179
1180 // Link debug info to function.
1181 shared->set_debug_info(*debug_info);
1182
1183 return debug_info;
1184}
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001185#endif
v8.team.kasperl727e9952008-09-02 14:56:44 +00001186
1187
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001188Handle<JSObject> Factory::NewArgumentsObject(Handle<Object> callee,
1189 int length) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001190 CALL_HEAP_FUNCTION(
1191 isolate(),
1192 isolate()->heap()->AllocateArgumentsObject(*callee, length), JSObject);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001193}
1194
1195
1196Handle<JSFunction> Factory::CreateApiFunction(
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001197 Handle<FunctionTemplateInfo> obj, ApiInstanceType instance_type) {
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00001198 Handle<Code> code = isolate()->builtins()->HandleApiCall();
1199 Handle<Code> construct_stub = isolate()->builtins()->JSConstructStubApi();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001200
kasper.lund212ac232008-07-16 07:07:30 +00001201 int internal_field_count = 0;
1202 if (!obj->instance_template()->IsUndefined()) {
1203 Handle<ObjectTemplateInfo> instance_template =
1204 Handle<ObjectTemplateInfo>(
1205 ObjectTemplateInfo::cast(obj->instance_template()));
1206 internal_field_count =
1207 Smi::cast(instance_template->internal_field_count())->value();
1208 }
1209
1210 int instance_size = kPointerSize * internal_field_count;
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001211 InstanceType type = INVALID_TYPE;
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001212 switch (instance_type) {
1213 case JavaScriptObject:
1214 type = JS_OBJECT_TYPE;
1215 instance_size += JSObject::kHeaderSize;
1216 break;
1217 case InnerGlobalObject:
1218 type = JS_GLOBAL_OBJECT_TYPE;
1219 instance_size += JSGlobalObject::kSize;
1220 break;
1221 case OuterGlobalObject:
1222 type = JS_GLOBAL_PROXY_TYPE;
1223 instance_size += JSGlobalProxy::kSize;
1224 break;
1225 default:
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001226 break;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001227 }
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001228 ASSERT(type != INVALID_TYPE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001229
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001230 Handle<JSFunction> result =
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001231 NewFunction(Factory::empty_symbol(),
1232 type,
1233 instance_size,
1234 code,
1235 true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001236 // Set class name.
1237 Handle<Object> class_name = Handle<Object>(obj->class_name());
1238 if (class_name->IsString()) {
1239 result->shared()->set_instance_class_name(*class_name);
1240 result->shared()->set_name(*class_name);
1241 }
1242
1243 Handle<Map> map = Handle<Map>(result->initial_map());
1244
1245 // Mark as undetectable if needed.
1246 if (obj->undetectable()) {
1247 map->set_is_undetectable();
1248 }
1249
1250 // Mark as hidden for the __proto__ accessor if needed.
1251 if (obj->hidden_prototype()) {
1252 map->set_is_hidden_prototype();
1253 }
1254
1255 // Mark as needs_access_check if needed.
1256 if (obj->needs_access_check()) {
ager@chromium.org870a0b62008-11-04 11:43:05 +00001257 map->set_is_access_check_needed(true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001258 }
1259
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001260 // Set interceptor information in the map.
1261 if (!obj->named_property_handler()->IsUndefined()) {
1262 map->set_has_named_interceptor();
1263 }
1264 if (!obj->indexed_property_handler()->IsUndefined()) {
1265 map->set_has_indexed_interceptor();
1266 }
1267
1268 // Set instance call-as-function information in the map.
1269 if (!obj->instance_call_handler()->IsUndefined()) {
1270 map->set_has_instance_call_handler();
1271 }
1272
1273 result->shared()->set_function_data(*obj);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001274 result->shared()->set_construct_stub(*construct_stub);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001275 result->shared()->DontAdaptArguments();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001276
1277 // Recursively copy parent templates' accessors, 'data' may be modified.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001278 while (true) {
1279 Handle<Object> props = Handle<Object>(obj->property_accessors());
1280 if (!props->IsUndefined()) {
danno@chromium.org129d3982012-07-25 15:01:47 +00001281 Map::CopyAppendCallbackDescriptors(map, props);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001282 }
1283 Handle<Object> parent = Handle<Object>(obj->parent_template());
1284 if (parent->IsUndefined()) break;
1285 obj = Handle<FunctionTemplateInfo>::cast(parent);
1286 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001287
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001288 ASSERT(result->shared()->IsApiFunction());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001289 return result;
1290}
1291
1292
ager@chromium.org236ad962008-09-25 09:45:57 +00001293Handle<MapCache> Factory::NewMapCache(int at_least_space_for) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001294 CALL_HEAP_FUNCTION(isolate(),
1295 MapCache::Allocate(at_least_space_for), MapCache);
ager@chromium.org236ad962008-09-25 09:45:57 +00001296}
1297
1298
lrn@chromium.org303ada72010-10-27 09:33:13 +00001299MUST_USE_RESULT static MaybeObject* UpdateMapCacheWith(Context* context,
1300 FixedArray* keys,
1301 Map* map) {
1302 Object* result;
1303 { MaybeObject* maybe_result =
1304 MapCache::cast(context->map_cache())->Put(keys, map);
1305 if (!maybe_result->ToObject(&result)) return maybe_result;
1306 }
1307 context->set_map_cache(MapCache::cast(result));
ager@chromium.org236ad962008-09-25 09:45:57 +00001308 return result;
1309}
1310
1311
1312Handle<MapCache> Factory::AddToMapCache(Handle<Context> context,
1313 Handle<FixedArray> keys,
1314 Handle<Map> map) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001315 CALL_HEAP_FUNCTION(isolate(),
1316 UpdateMapCacheWith(*context, *keys, *map), MapCache);
ager@chromium.org236ad962008-09-25 09:45:57 +00001317}
1318
1319
1320Handle<Map> Factory::ObjectLiteralMapFromCache(Handle<Context> context,
1321 Handle<FixedArray> keys) {
1322 if (context->map_cache()->IsUndefined()) {
1323 // Allocate the new map cache for the global context.
1324 Handle<MapCache> new_cache = NewMapCache(24);
1325 context->set_map_cache(*new_cache);
1326 }
ager@chromium.org32912102009-01-16 10:38:43 +00001327 // Check to see whether there is a matching element in the cache.
ager@chromium.org236ad962008-09-25 09:45:57 +00001328 Handle<MapCache> cache =
1329 Handle<MapCache>(MapCache::cast(context->map_cache()));
1330 Handle<Object> result = Handle<Object>(cache->Lookup(*keys));
1331 if (result->IsMap()) return Handle<Map>::cast(result);
1332 // Create a new map and add it to the cache.
1333 Handle<Map> map =
ager@chromium.org32912102009-01-16 10:38:43 +00001334 CopyMap(Handle<Map>(context->object_function()->initial_map()),
1335 keys->length());
ager@chromium.org236ad962008-09-25 09:45:57 +00001336 AddToMapCache(context, keys, map);
1337 return Handle<Map>(map);
1338}
1339
1340
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001341void Factory::SetRegExpAtomData(Handle<JSRegExp> regexp,
1342 JSRegExp::Type type,
1343 Handle<String> source,
1344 JSRegExp::Flags flags,
1345 Handle<Object> data) {
1346 Handle<FixedArray> store = NewFixedArray(JSRegExp::kAtomDataSize);
1347
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001348 store->set(JSRegExp::kTagIndex, Smi::FromInt(type));
1349 store->set(JSRegExp::kSourceIndex, *source);
1350 store->set(JSRegExp::kFlagsIndex, Smi::FromInt(flags.value()));
1351 store->set(JSRegExp::kAtomPatternIndex, *data);
1352 regexp->set_data(*store);
1353}
1354
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001355void Factory::SetRegExpIrregexpData(Handle<JSRegExp> regexp,
1356 JSRegExp::Type type,
1357 Handle<String> source,
1358 JSRegExp::Flags flags,
1359 int capture_count) {
1360 Handle<FixedArray> store = NewFixedArray(JSRegExp::kIrregexpDataSize);
jkummerow@chromium.orgddda9e82011-07-06 11:27:02 +00001361 Smi* uninitialized = Smi::FromInt(JSRegExp::kUninitializedValue);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001362 store->set(JSRegExp::kTagIndex, Smi::FromInt(type));
1363 store->set(JSRegExp::kSourceIndex, *source);
1364 store->set(JSRegExp::kFlagsIndex, Smi::FromInt(flags.value()));
jkummerow@chromium.orgddda9e82011-07-06 11:27:02 +00001365 store->set(JSRegExp::kIrregexpASCIICodeIndex, uninitialized);
1366 store->set(JSRegExp::kIrregexpUC16CodeIndex, uninitialized);
1367 store->set(JSRegExp::kIrregexpASCIICodeSavedIndex, uninitialized);
1368 store->set(JSRegExp::kIrregexpUC16CodeSavedIndex, uninitialized);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001369 store->set(JSRegExp::kIrregexpMaxRegisterCountIndex, Smi::FromInt(0));
1370 store->set(JSRegExp::kIrregexpCaptureCountIndex,
1371 Smi::FromInt(capture_count));
1372 regexp->set_data(*store);
1373}
1374
1375
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001376
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001377void Factory::ConfigureInstance(Handle<FunctionTemplateInfo> desc,
1378 Handle<JSObject> instance,
1379 bool* pending_exception) {
1380 // Configure the instance by adding the properties specified by the
1381 // instance template.
1382 Handle<Object> instance_template = Handle<Object>(desc->instance_template());
1383 if (!instance_template->IsUndefined()) {
1384 Execution::ConfigureInstance(instance,
1385 instance_template,
1386 pending_exception);
1387 } else {
1388 *pending_exception = false;
1389 }
1390}
1391
1392
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001393Handle<Object> Factory::GlobalConstantFor(Handle<String> name) {
1394 Heap* h = isolate()->heap();
1395 if (name->Equals(h->undefined_symbol())) return undefined_value();
1396 if (name->Equals(h->nan_symbol())) return nan_value();
1397 if (name->Equals(h->infinity_symbol())) return infinity_value();
1398 return Handle<Object>::null();
1399}
1400
1401
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00001402Handle<Object> Factory::ToBoolean(bool value) {
1403 return Handle<Object>(value
1404 ? isolate()->heap()->true_value()
1405 : isolate()->heap()->false_value());
1406}
1407
1408
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001409} } // namespace v8::internal