blob: 405cebe45fa9aed5ec3c7d386d6f33ac54531fd5 [file] [log] [blame]
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001// Copyright 2011 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"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000037
kasperl@chromium.org71affb52009-05-26 05:44:31 +000038namespace v8 {
39namespace internal {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000040
41
42Handle<FixedArray> Factory::NewFixedArray(int size, PretenureFlag pretenure) {
43 ASSERT(0 <= size);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000044 CALL_HEAP_FUNCTION(
45 isolate(),
46 isolate()->heap()->AllocateFixedArray(size, pretenure),
47 FixedArray);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000048}
49
50
ricow@chromium.orgc9c80822010-04-21 08:22:37 +000051Handle<FixedArray> Factory::NewFixedArrayWithHoles(int size,
52 PretenureFlag pretenure) {
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +000053 ASSERT(0 <= size);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000054 CALL_HEAP_FUNCTION(
55 isolate(),
56 isolate()->heap()->AllocateFixedArrayWithHoles(size, pretenure),
57 FixedArray);
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +000058}
59
60
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +000061Handle<FixedArray> Factory::NewFixedDoubleArray(int size,
62 PretenureFlag pretenure) {
63 ASSERT(0 <= size);
64 CALL_HEAP_FUNCTION(
65 isolate(),
66 isolate()->heap()->AllocateUninitializedFixedDoubleArray(size, pretenure),
67 FixedArray);
68}
69
70
kasperl@chromium.org86f77b72009-07-06 08:21:57 +000071Handle<StringDictionary> Factory::NewStringDictionary(int at_least_space_for) {
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +000072 ASSERT(0 <= at_least_space_for);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000073 CALL_HEAP_FUNCTION(isolate(),
74 StringDictionary::Allocate(at_least_space_for),
kasperl@chromium.org86f77b72009-07-06 08:21:57 +000075 StringDictionary);
76}
77
78
79Handle<NumberDictionary> Factory::NewNumberDictionary(int at_least_space_for) {
80 ASSERT(0 <= at_least_space_for);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000081 CALL_HEAP_FUNCTION(isolate(),
82 NumberDictionary::Allocate(at_least_space_for),
kasperl@chromium.org86f77b72009-07-06 08:21:57 +000083 NumberDictionary);
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +000084}
85
86
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000087Handle<DescriptorArray> Factory::NewDescriptorArray(int number_of_descriptors) {
88 ASSERT(0 <= number_of_descriptors);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000089 CALL_HEAP_FUNCTION(isolate(),
90 DescriptorArray::Allocate(number_of_descriptors),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000091 DescriptorArray);
92}
93
94
kasperl@chromium.orga5551262010-12-07 12:49:48 +000095Handle<DeoptimizationInputData> Factory::NewDeoptimizationInputData(
96 int deopt_entry_count,
97 PretenureFlag pretenure) {
98 ASSERT(deopt_entry_count > 0);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000099 CALL_HEAP_FUNCTION(isolate(),
100 DeoptimizationInputData::Allocate(deopt_entry_count,
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000101 pretenure),
102 DeoptimizationInputData);
103}
104
105
106Handle<DeoptimizationOutputData> Factory::NewDeoptimizationOutputData(
107 int deopt_entry_count,
108 PretenureFlag pretenure) {
109 ASSERT(deopt_entry_count > 0);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000110 CALL_HEAP_FUNCTION(isolate(),
111 DeoptimizationOutputData::Allocate(deopt_entry_count,
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000112 pretenure),
113 DeoptimizationOutputData);
114}
115
116
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000117// Symbols are created in the old generation (data space).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000118Handle<String> Factory::LookupSymbol(Vector<const char> string) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000119 CALL_HEAP_FUNCTION(isolate(),
120 isolate()->heap()->LookupSymbol(string),
121 String);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000122}
123
danno@chromium.org40cb8782011-05-25 07:58:50 +0000124// Symbols are created in the old generation (data space).
125Handle<String> Factory::LookupSymbol(Handle<String> string) {
126 CALL_HEAP_FUNCTION(isolate(),
127 isolate()->heap()->LookupSymbol(*string),
128 String);
129}
130
fschneider@chromium.org9e3e0b62011-01-03 10:16:46 +0000131Handle<String> Factory::LookupAsciiSymbol(Vector<const char> string) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000132 CALL_HEAP_FUNCTION(isolate(),
133 isolate()->heap()->LookupAsciiSymbol(string),
134 String);
fschneider@chromium.org9e3e0b62011-01-03 10:16:46 +0000135}
136
danno@chromium.org40cb8782011-05-25 07:58:50 +0000137
138Handle<String> Factory::LookupAsciiSymbol(Handle<SeqAsciiString> string,
139 int from,
140 int length) {
141 CALL_HEAP_FUNCTION(isolate(),
142 isolate()->heap()->LookupAsciiSymbol(string,
143 from,
144 length),
145 String);
146}
147
148
fschneider@chromium.org9e3e0b62011-01-03 10:16:46 +0000149Handle<String> Factory::LookupTwoByteSymbol(Vector<const uc16> string) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000150 CALL_HEAP_FUNCTION(isolate(),
151 isolate()->heap()->LookupTwoByteSymbol(string),
152 String);
fschneider@chromium.org9e3e0b62011-01-03 10:16:46 +0000153}
154
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000155
156Handle<String> Factory::NewStringFromAscii(Vector<const char> string,
157 PretenureFlag pretenure) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000158 CALL_HEAP_FUNCTION(
159 isolate(),
160 isolate()->heap()->AllocateStringFromAscii(string, pretenure),
161 String);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000162}
163
164Handle<String> Factory::NewStringFromUtf8(Vector<const char> string,
165 PretenureFlag pretenure) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000166 CALL_HEAP_FUNCTION(
167 isolate(),
168 isolate()->heap()->AllocateStringFromUtf8(string, pretenure),
169 String);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000170}
171
172
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000173Handle<String> Factory::NewStringFromTwoByte(Vector<const uc16> string,
174 PretenureFlag pretenure) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000175 CALL_HEAP_FUNCTION(
176 isolate(),
177 isolate()->heap()->AllocateStringFromTwoByte(string, pretenure),
178 String);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000179}
180
181
ager@chromium.org04921a82011-06-27 13:21:41 +0000182Handle<SeqAsciiString> Factory::NewRawAsciiString(int length,
183 PretenureFlag pretenure) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000184 CALL_HEAP_FUNCTION(
185 isolate(),
186 isolate()->heap()->AllocateRawAsciiString(length, pretenure),
ager@chromium.org04921a82011-06-27 13:21:41 +0000187 SeqAsciiString);
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +0000188}
189
190
ager@chromium.org04921a82011-06-27 13:21:41 +0000191Handle<SeqTwoByteString> Factory::NewRawTwoByteString(int length,
192 PretenureFlag pretenure) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000193 CALL_HEAP_FUNCTION(
194 isolate(),
195 isolate()->heap()->AllocateRawTwoByteString(length, pretenure),
ager@chromium.org04921a82011-06-27 13:21:41 +0000196 SeqTwoByteString);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000197}
198
199
200Handle<String> Factory::NewConsString(Handle<String> first,
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000201 Handle<String> second) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000202 CALL_HEAP_FUNCTION(isolate(),
203 isolate()->heap()->AllocateConsString(*first, *second),
204 String);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000205}
206
207
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000208Handle<String> Factory::NewSubString(Handle<String> str,
209 int begin,
210 int end) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000211 CALL_HEAP_FUNCTION(isolate(),
212 str->SubString(begin, end),
213 String);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000214}
215
216
ager@chromium.org04921a82011-06-27 13:21:41 +0000217Handle<String> Factory::NewProperSubString(Handle<String> str,
218 int begin,
219 int end) {
220 ASSERT(begin > 0 || end < str->length());
221 CALL_HEAP_FUNCTION(isolate(),
222 isolate()->heap()->AllocateSubString(*str, begin, end),
223 String);
224}
225
226
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000227Handle<String> Factory::NewExternalStringFromAscii(
228 ExternalAsciiString::Resource* resource) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000229 CALL_HEAP_FUNCTION(
230 isolate(),
231 isolate()->heap()->AllocateExternalStringFromAscii(resource),
232 String);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000233}
234
235
236Handle<String> Factory::NewExternalStringFromTwoByte(
237 ExternalTwoByteString::Resource* resource) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000238 CALL_HEAP_FUNCTION(
239 isolate(),
240 isolate()->heap()->AllocateExternalStringFromTwoByte(resource),
241 String);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000242}
243
244
245Handle<Context> Factory::NewGlobalContext() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000246 CALL_HEAP_FUNCTION(
247 isolate(),
248 isolate()->heap()->AllocateGlobalContext(),
249 Context);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000250}
251
252
253Handle<Context> Factory::NewFunctionContext(int length,
254 Handle<JSFunction> closure) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000255 CALL_HEAP_FUNCTION(
256 isolate(),
257 isolate()->heap()->AllocateFunctionContext(length, *closure),
258 Context);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000259}
260
261
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000262Handle<Context> Factory::NewCatchContext(Handle<Context> previous,
263 Handle<String> name,
264 Handle<Object> thrown_object) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000265 CALL_HEAP_FUNCTION(
266 isolate(),
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000267 isolate()->heap()->AllocateCatchContext(*previous, *name, *thrown_object),
268 Context);
269}
270
271
272Handle<Context> Factory::NewWithContext(Handle<Context> previous,
273 Handle<JSObject> extension) {
274 CALL_HEAP_FUNCTION(
275 isolate(),
276 isolate()->heap()->AllocateWithContext(*previous, *extension),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000277 Context);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000278}
279
280
281Handle<Struct> Factory::NewStruct(InstanceType type) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000282 CALL_HEAP_FUNCTION(
283 isolate(),
284 isolate()->heap()->AllocateStruct(type),
285 Struct);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000286}
287
288
289Handle<AccessorInfo> Factory::NewAccessorInfo() {
290 Handle<AccessorInfo> info =
291 Handle<AccessorInfo>::cast(NewStruct(ACCESSOR_INFO_TYPE));
292 info->set_flag(0); // Must clear the flag, it was initialized as undefined.
293 return info;
294}
295
296
297Handle<Script> Factory::NewScript(Handle<String> source) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000298 // Generate id for this script.
299 int id;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000300 Heap* heap = isolate()->heap();
301 if (heap->last_script_id()->IsUndefined()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000302 // Script ids start from one.
303 id = 1;
304 } else {
305 // Increment id, wrap when positive smi is exhausted.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000306 id = Smi::cast(heap->last_script_id())->value();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000307 id++;
308 if (!Smi::IsValid(id)) {
309 id = 0;
310 }
311 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000312 heap->SetLastScriptId(Smi::FromInt(id));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000313
314 // Create and initialize script object.
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000315 Handle<Foreign> wrapper = NewForeign(0, TENURED);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000316 Handle<Script> script = Handle<Script>::cast(NewStruct(SCRIPT_TYPE));
317 script->set_source(*source);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000318 script->set_name(heap->undefined_value());
319 script->set_id(heap->last_script_id());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000320 script->set_line_offset(Smi::FromInt(0));
321 script->set_column_offset(Smi::FromInt(0));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000322 script->set_data(heap->undefined_value());
323 script->set_context_data(heap->undefined_value());
ager@chromium.orge2902be2009-06-08 12:21:35 +0000324 script->set_type(Smi::FromInt(Script::TYPE_NORMAL));
325 script->set_compilation_type(Smi::FromInt(Script::COMPILATION_TYPE_HOST));
ager@chromium.org9085a012009-05-11 19:22:57 +0000326 script->set_wrapper(*wrapper);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000327 script->set_line_ends(heap->undefined_value());
328 script->set_eval_from_shared(heap->undefined_value());
ager@chromium.orge2902be2009-06-08 12:21:35 +0000329 script->set_eval_from_instructions_offset(Smi::FromInt(0));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000330
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000331 return script;
332}
333
334
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000335Handle<Foreign> Factory::NewForeign(Address addr, PretenureFlag pretenure) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000336 CALL_HEAP_FUNCTION(isolate(),
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000337 isolate()->heap()->AllocateForeign(addr, pretenure),
338 Foreign);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000339}
340
341
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000342Handle<Foreign> Factory::NewForeign(const AccessorDescriptor* desc) {
343 return NewForeign((Address) desc, TENURED);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000344}
345
346
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000347Handle<ByteArray> Factory::NewByteArray(int length, PretenureFlag pretenure) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000348 ASSERT(0 <= length);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000349 CALL_HEAP_FUNCTION(
350 isolate(),
351 isolate()->heap()->AllocateByteArray(length, pretenure),
352 ByteArray);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000353}
354
355
ager@chromium.org3811b432009-10-28 14:53:37 +0000356Handle<ExternalArray> Factory::NewExternalArray(int length,
357 ExternalArrayType array_type,
358 void* external_pointer,
359 PretenureFlag pretenure) {
360 ASSERT(0 <= length);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000361 CALL_HEAP_FUNCTION(
362 isolate(),
363 isolate()->heap()->AllocateExternalArray(length,
364 array_type,
365 external_pointer,
366 pretenure),
367 ExternalArray);
ager@chromium.org3811b432009-10-28 14:53:37 +0000368}
369
370
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000371Handle<JSGlobalPropertyCell> Factory::NewJSGlobalPropertyCell(
372 Handle<Object> value) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000373 CALL_HEAP_FUNCTION(
374 isolate(),
375 isolate()->heap()->AllocateJSGlobalPropertyCell(*value),
376 JSGlobalPropertyCell);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000377}
378
379
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000380Handle<Map> Factory::NewMap(InstanceType type, int instance_size) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000381 CALL_HEAP_FUNCTION(
382 isolate(),
383 isolate()->heap()->AllocateMap(type, instance_size),
384 Map);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000385}
386
387
388Handle<JSObject> Factory::NewFunctionPrototype(Handle<JSFunction> function) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000389 CALL_HEAP_FUNCTION(
390 isolate(),
391 isolate()->heap()->AllocateFunctionPrototype(*function),
392 JSObject);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000393}
394
395
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000396Handle<Map> Factory::CopyMapDropDescriptors(Handle<Map> src) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000397 CALL_HEAP_FUNCTION(isolate(), src->CopyDropDescriptors(), Map);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000398}
399
400
ager@chromium.org32912102009-01-16 10:38:43 +0000401Handle<Map> Factory::CopyMap(Handle<Map> src,
402 int extra_inobject_properties) {
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000403 Handle<Map> copy = CopyMapDropDescriptors(src);
ager@chromium.org32912102009-01-16 10:38:43 +0000404 // Check that we do not overflow the instance size when adding the
405 // extra inobject properties.
406 int instance_size_delta = extra_inobject_properties * kPointerSize;
407 int max_instance_size_delta =
408 JSObject::kMaxInstanceSize - copy->instance_size();
409 if (instance_size_delta > max_instance_size_delta) {
410 // If the instance size overflows, we allocate as many properties
411 // as we can as inobject properties.
412 instance_size_delta = max_instance_size_delta;
413 extra_inobject_properties = max_instance_size_delta >> kPointerSizeLog2;
414 }
415 // Adjust the map with the extra inobject properties.
416 int inobject_properties =
417 copy->inobject_properties() + extra_inobject_properties;
418 copy->set_inobject_properties(inobject_properties);
419 copy->set_unused_property_fields(inobject_properties);
420 copy->set_instance_size(copy->instance_size() + instance_size_delta);
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000421 copy->set_visitor_id(StaticVisitorBase::GetVisitorId(*copy));
ager@chromium.org32912102009-01-16 10:38:43 +0000422 return copy;
423}
424
fschneider@chromium.org40b9da32010-06-28 11:29:21 +0000425
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000426Handle<Map> Factory::CopyMapDropTransitions(Handle<Map> src) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000427 CALL_HEAP_FUNCTION(isolate(), src->CopyDropTransitions(), Map);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000428}
429
430
fschneider@chromium.org40b9da32010-06-28 11:29:21 +0000431Handle<Map> Factory::GetFastElementsMap(Handle<Map> src) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000432 CALL_HEAP_FUNCTION(isolate(), src->GetFastElementsMap(), Map);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +0000433}
434
435
436Handle<Map> Factory::GetSlowElementsMap(Handle<Map> src) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000437 CALL_HEAP_FUNCTION(isolate(), src->GetSlowElementsMap(), Map);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +0000438}
439
440
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000441Handle<Map> Factory::GetExternalArrayElementsMap(
442 Handle<Map> src,
443 ExternalArrayType array_type,
444 bool safe_to_add_transition) {
445 CALL_HEAP_FUNCTION(isolate(),
446 src->GetExternalArrayElementsMap(array_type,
447 safe_to_add_transition),
448 Map);
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +0000449}
450
451
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000452Handle<FixedArray> Factory::CopyFixedArray(Handle<FixedArray> array) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000453 CALL_HEAP_FUNCTION(isolate(), array->Copy(), FixedArray);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000454}
455
456
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000457Handle<JSFunction> Factory::BaseNewFunctionFromSharedFunctionInfo(
458 Handle<SharedFunctionInfo> function_info,
sgjesse@chromium.org846fb742009-12-18 08:56:33 +0000459 Handle<Map> function_map,
460 PretenureFlag pretenure) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000461 CALL_HEAP_FUNCTION(
462 isolate(),
463 isolate()->heap()->AllocateFunction(*function_map,
464 *function_info,
465 isolate()->heap()->the_hole_value(),
466 pretenure),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000467 JSFunction);
468}
469
470
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000471Handle<JSFunction> Factory::NewFunctionFromSharedFunctionInfo(
472 Handle<SharedFunctionInfo> function_info,
sgjesse@chromium.org846fb742009-12-18 08:56:33 +0000473 Handle<Context> context,
474 PretenureFlag pretenure) {
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000475 Handle<JSFunction> result = BaseNewFunctionFromSharedFunctionInfo(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000476 function_info,
477 function_info->strict_mode()
478 ? isolate()->strict_mode_function_map()
479 : isolate()->function_map(),
480 pretenure);
481
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000482 result->set_context(*context);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000483 int number_of_literals = function_info->num_literals();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000484 Handle<FixedArray> literals = NewFixedArray(number_of_literals, pretenure);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000485 if (number_of_literals > 0) {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000486 // Store the object, regexp and array functions in the literals
487 // array prefix. These functions will be used when creating
488 // object, regexp and array literals in this function.
ager@chromium.org236ad962008-09-25 09:45:57 +0000489 literals->set(JSFunction::kLiteralGlobalContextIndex,
490 context->global_context());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000491 }
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000492 result->set_literals(*literals);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000493 result->set_next_function_link(isolate()->heap()->undefined_value());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000494
495 if (V8::UseCrankshaft() &&
496 FLAG_always_opt &&
497 result->is_compiled() &&
498 !function_info->is_toplevel() &&
499 function_info->allows_lazy_compilation()) {
500 result->MarkForLazyRecompilation();
501 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000502 return result;
503}
504
505
506Handle<Object> Factory::NewNumber(double value,
507 PretenureFlag pretenure) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000508 CALL_HEAP_FUNCTION(
509 isolate(),
510 isolate()->heap()->NumberFromDouble(value, pretenure), Object);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000511}
512
513
514Handle<Object> Factory::NewNumberFromInt(int value) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000515 CALL_HEAP_FUNCTION(
516 isolate(),
517 isolate()->heap()->NumberFromInt32(value), Object);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000518}
519
520
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000521Handle<Object> Factory::NewNumberFromUint(uint32_t value) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000522 CALL_HEAP_FUNCTION(
523 isolate(),
524 isolate()->heap()->NumberFromUint32(value), Object);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000525}
526
527
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000528Handle<JSObject> Factory::NewNeanderObject() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000529 CALL_HEAP_FUNCTION(
530 isolate(),
531 isolate()->heap()->AllocateJSObjectFromMap(
532 isolate()->heap()->neander_map()),
533 JSObject);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000534}
535
536
537Handle<Object> Factory::NewTypeError(const char* type,
538 Vector< Handle<Object> > args) {
539 return NewError("MakeTypeError", type, args);
540}
541
542
543Handle<Object> Factory::NewTypeError(Handle<String> message) {
544 return NewError("$TypeError", message);
545}
546
547
548Handle<Object> Factory::NewRangeError(const char* type,
549 Vector< Handle<Object> > args) {
550 return NewError("MakeRangeError", type, args);
551}
552
553
554Handle<Object> Factory::NewRangeError(Handle<String> message) {
555 return NewError("$RangeError", message);
556}
557
558
559Handle<Object> Factory::NewSyntaxError(const char* type, Handle<JSArray> args) {
560 return NewError("MakeSyntaxError", type, args);
561}
562
563
564Handle<Object> Factory::NewSyntaxError(Handle<String> message) {
565 return NewError("$SyntaxError", message);
566}
567
568
569Handle<Object> Factory::NewReferenceError(const char* type,
570 Vector< Handle<Object> > args) {
571 return NewError("MakeReferenceError", type, args);
572}
573
574
575Handle<Object> Factory::NewReferenceError(Handle<String> message) {
576 return NewError("$ReferenceError", message);
577}
578
579
580Handle<Object> Factory::NewError(const char* maker, const char* type,
581 Vector< Handle<Object> > args) {
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000582 v8::HandleScope scope; // Instantiate a closeable HandleScope for EscapeFrom.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000583 Handle<FixedArray> array = NewFixedArray(args.length());
ager@chromium.org7c537e22008-10-16 08:43:32 +0000584 for (int i = 0; i < args.length(); i++) {
585 array->set(i, *args[i]);
586 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000587 Handle<JSArray> object = NewJSArrayWithElements(array);
ager@chromium.org7c537e22008-10-16 08:43:32 +0000588 Handle<Object> result = NewError(maker, type, object);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000589 return result.EscapeFrom(&scope);
590}
591
592
593Handle<Object> Factory::NewEvalError(const char* type,
594 Vector< Handle<Object> > args) {
595 return NewError("MakeEvalError", type, args);
596}
597
598
599Handle<Object> Factory::NewError(const char* type,
600 Vector< Handle<Object> > args) {
601 return NewError("MakeError", type, args);
602}
603
604
605Handle<Object> Factory::NewError(const char* maker,
606 const char* type,
607 Handle<JSArray> args) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000608 Handle<String> make_str = LookupAsciiSymbol(maker);
609 Handle<Object> fun_obj(
610 isolate()->js_builtins_object()->GetPropertyNoExceptionThrown(*make_str));
ager@chromium.org4af710e2009-09-15 12:20:11 +0000611 // If the builtins haven't been properly configured yet this error
612 // constructor may not have been defined. Bail out.
613 if (!fun_obj->IsJSFunction())
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000614 return undefined_value();
ager@chromium.org4af710e2009-09-15 12:20:11 +0000615 Handle<JSFunction> fun = Handle<JSFunction>::cast(fun_obj);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000616 Handle<Object> type_obj = LookupAsciiSymbol(type);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000617 Object** argv[2] = { type_obj.location(),
618 Handle<Object>::cast(args).location() };
619
620 // Invoke the JavaScript factory method. If an exception is thrown while
621 // running the factory method, use the exception as the result.
622 bool caught_exception;
623 Handle<Object> result = Execution::TryCall(fun,
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000624 isolate()->js_builtins_object(), 2, argv, &caught_exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000625 return result;
626}
627
628
629Handle<Object> Factory::NewError(Handle<String> message) {
630 return NewError("$Error", message);
631}
632
633
634Handle<Object> Factory::NewError(const char* constructor,
635 Handle<String> message) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000636 Handle<String> constr = LookupAsciiSymbol(constructor);
637 Handle<JSFunction> fun = Handle<JSFunction>(
638 JSFunction::cast(isolate()->js_builtins_object()->
639 GetPropertyNoExceptionThrown(*constr)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000640 Object** argv[1] = { Handle<Object>::cast(message).location() };
641
642 // Invoke the JavaScript factory method. If an exception is thrown while
643 // running the factory method, use the exception as the result.
644 bool caught_exception;
645 Handle<Object> result = Execution::TryCall(fun,
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000646 isolate()->js_builtins_object(), 1, argv, &caught_exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000647 return result;
648}
649
650
651Handle<JSFunction> Factory::NewFunction(Handle<String> name,
652 InstanceType type,
653 int instance_size,
654 Handle<Code> code,
655 bool force_initial_map) {
656 // Allocate the function
657 Handle<JSFunction> function = NewFunction(name, the_hole_value());
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000658
659 // Setup the code pointer in both the shared function info and in
660 // the function itself.
661 function->shared()->set_code(*code);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000662 function->set_code(*code);
663
664 if (force_initial_map ||
665 type != JS_OBJECT_TYPE ||
666 instance_size != JSObject::kHeaderSize) {
667 Handle<Map> initial_map = NewMap(type, instance_size);
668 Handle<JSObject> prototype = NewFunctionPrototype(function);
669 initial_map->set_prototype(*prototype);
670 function->set_initial_map(*initial_map);
671 initial_map->set_constructor(*function);
672 } else {
673 ASSERT(!function->has_initial_map());
674 ASSERT(!function->has_prototype());
675 }
676
677 return function;
678}
679
680
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000681Handle<JSFunction> Factory::NewFunctionWithPrototype(Handle<String> name,
682 InstanceType type,
683 int instance_size,
684 Handle<JSObject> prototype,
685 Handle<Code> code,
686 bool force_initial_map) {
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000687 // Allocate the function.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000688 Handle<JSFunction> function = NewFunction(name, prototype);
689
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000690 // Setup the code pointer in both the shared function info and in
691 // the function itself.
692 function->shared()->set_code(*code);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000693 function->set_code(*code);
694
695 if (force_initial_map ||
696 type != JS_OBJECT_TYPE ||
697 instance_size != JSObject::kHeaderSize) {
698 Handle<Map> initial_map = NewMap(type, instance_size);
699 function->set_initial_map(*initial_map);
700 initial_map->set_constructor(*function);
701 }
702
703 // Set function.prototype and give the prototype a constructor
704 // property that refers to the function.
705 SetPrototypeProperty(function, prototype);
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +0000706 // Currently safe because it is only invoked from Genesis.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000707 SetLocalPropertyNoThrow(prototype, constructor_symbol(), function, DONT_ENUM);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000708 return function;
709}
710
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000711
kmillikin@chromium.org4111b802010-05-03 10:34:42 +0000712Handle<JSFunction> Factory::NewFunctionWithoutPrototype(Handle<String> name,
713 Handle<Code> code) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000714 Handle<JSFunction> function = NewFunctionWithoutPrototype(name,
715 kNonStrictMode);
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000716 function->shared()->set_code(*code);
kmillikin@chromium.org4111b802010-05-03 10:34:42 +0000717 function->set_code(*code);
718 ASSERT(!function->has_initial_map());
719 ASSERT(!function->has_prototype());
720 return function;
721}
722
723
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000724Handle<Code> Factory::NewCode(const CodeDesc& desc,
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000725 Code::Flags flags,
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +0000726 Handle<Object> self_ref,
727 bool immovable) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000728 CALL_HEAP_FUNCTION(isolate(),
729 isolate()->heap()->CreateCode(
730 desc, flags, self_ref, immovable),
731 Code);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000732}
733
734
735Handle<Code> Factory::CopyCode(Handle<Code> code) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000736 CALL_HEAP_FUNCTION(isolate(),
737 isolate()->heap()->CopyCode(*code),
738 Code);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000739}
740
741
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000742Handle<Code> Factory::CopyCode(Handle<Code> code, Vector<byte> reloc_info) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000743 CALL_HEAP_FUNCTION(isolate(),
744 isolate()->heap()->CopyCode(*code, reloc_info),
745 Code);
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000746}
747
748
lrn@chromium.org303ada72010-10-27 09:33:13 +0000749MUST_USE_RESULT static inline MaybeObject* DoCopyInsert(
750 DescriptorArray* array,
751 String* key,
752 Object* value,
753 PropertyAttributes attributes) {
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000754 CallbacksDescriptor desc(key, value, attributes);
lrn@chromium.org303ada72010-10-27 09:33:13 +0000755 MaybeObject* obj = array->CopyInsert(&desc, REMOVE_TRANSITIONS);
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000756 return obj;
757}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000758
759
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000760// Allocate the new array.
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000761Handle<DescriptorArray> Factory::CopyAppendForeignDescriptor(
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000762 Handle<DescriptorArray> array,
763 Handle<String> key,
764 Handle<Object> value,
765 PropertyAttributes attributes) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000766 CALL_HEAP_FUNCTION(isolate(),
767 DoCopyInsert(*array, *key, *value, attributes),
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000768 DescriptorArray);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000769}
770
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000771
772Handle<String> Factory::SymbolFromString(Handle<String> value) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000773 CALL_HEAP_FUNCTION(isolate(),
774 isolate()->heap()->LookupSymbol(*value), String);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000775}
776
777
778Handle<DescriptorArray> Factory::CopyAppendCallbackDescriptors(
779 Handle<DescriptorArray> array,
780 Handle<Object> descriptors) {
781 v8::NeanderArray callbacks(descriptors);
782 int nof_callbacks = callbacks.length();
783 Handle<DescriptorArray> result =
784 NewDescriptorArray(array->number_of_descriptors() + nof_callbacks);
785
786 // Number of descriptors added to the result so far.
787 int descriptor_count = 0;
788
789 // Copy the descriptors from the array.
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000790 for (int i = 0; i < array->number_of_descriptors(); i++) {
791 if (array->GetType(i) != NULL_DESCRIPTOR) {
792 result->CopyFrom(descriptor_count++, *array, i);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000793 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000794 }
795
796 // Number of duplicates detected.
797 int duplicates = 0;
798
799 // Fill in new callback descriptors. Process the callbacks from
800 // back to front so that the last callback with a given name takes
801 // precedence over previously added callbacks with that name.
802 for (int i = nof_callbacks - 1; i >= 0; i--) {
803 Handle<AccessorInfo> entry =
804 Handle<AccessorInfo>(AccessorInfo::cast(callbacks.get(i)));
805 // Ensure the key is a symbol before writing into the instance descriptor.
806 Handle<String> key =
807 SymbolFromString(Handle<String>(String::cast(entry->name())));
808 // Check if a descriptor with this name already exists before writing.
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000809 if (result->LinearSearch(*key, descriptor_count) ==
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000810 DescriptorArray::kNotFound) {
811 CallbacksDescriptor desc(*key, *entry, entry->property_attributes());
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000812 result->Set(descriptor_count, &desc);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000813 descriptor_count++;
814 } else {
815 duplicates++;
816 }
817 }
818
819 // If duplicates were detected, allocate a result of the right size
820 // and transfer the elements.
821 if (duplicates > 0) {
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000822 int number_of_descriptors = result->number_of_descriptors() - duplicates;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000823 Handle<DescriptorArray> new_result =
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000824 NewDescriptorArray(number_of_descriptors);
825 for (int i = 0; i < number_of_descriptors; i++) {
826 new_result->CopyFrom(i, *result, i);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000827 }
828 result = new_result;
829 }
830
831 // Sort the result before returning.
832 result->Sort();
833 return result;
834}
835
836
837Handle<JSObject> Factory::NewJSObject(Handle<JSFunction> constructor,
838 PretenureFlag pretenure) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000839 CALL_HEAP_FUNCTION(
840 isolate(),
841 isolate()->heap()->AllocateJSObject(*constructor, pretenure), JSObject);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000842}
843
844
kasperl@chromium.org2abc4502009-07-02 07:00:29 +0000845Handle<GlobalObject> Factory::NewGlobalObject(
846 Handle<JSFunction> constructor) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000847 CALL_HEAP_FUNCTION(isolate(),
848 isolate()->heap()->AllocateGlobalObject(*constructor),
kasperl@chromium.org2abc4502009-07-02 07:00:29 +0000849 GlobalObject);
850}
851
852
853
ager@chromium.org236ad962008-09-25 09:45:57 +0000854Handle<JSObject> Factory::NewJSObjectFromMap(Handle<Map> map) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000855 CALL_HEAP_FUNCTION(
856 isolate(),
857 isolate()->heap()->AllocateJSObjectFromMap(*map, NOT_TENURED),
858 JSObject);
ager@chromium.org236ad962008-09-25 09:45:57 +0000859}
860
861
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +0000862Handle<JSArray> Factory::NewJSArray(int capacity,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000863 PretenureFlag pretenure) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000864 Handle<JSObject> obj = NewJSObject(isolate()->array_function(), pretenure);
865 CALL_HEAP_FUNCTION(isolate(),
866 Handle<JSArray>::cast(obj)->Initialize(capacity),
867 JSArray);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000868}
869
870
871Handle<JSArray> Factory::NewJSArrayWithElements(Handle<FixedArray> elements,
872 PretenureFlag pretenure) {
873 Handle<JSArray> result =
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000874 Handle<JSArray>::cast(NewJSObject(isolate()->array_function(),
875 pretenure));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000876 result->SetContent(*elements);
877 return result;
878}
879
880
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000881Handle<JSProxy> Factory::NewJSProxy(Handle<Object> handler,
882 Handle<Object> prototype) {
883 CALL_HEAP_FUNCTION(
884 isolate(),
885 isolate()->heap()->AllocateJSProxy(*handler, *prototype),
886 JSProxy);
887}
888
889
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000890Handle<SharedFunctionInfo> Factory::NewSharedFunctionInfo(
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +0000891 Handle<String> name,
892 int number_of_literals,
893 Handle<Code> code,
ager@chromium.orgb5737492010-07-15 09:29:43 +0000894 Handle<SerializedScopeInfo> scope_info) {
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000895 Handle<SharedFunctionInfo> shared = NewSharedFunctionInfo(name);
896 shared->set_code(*code);
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +0000897 shared->set_scope_info(*scope_info);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000898 int literals_array_size = number_of_literals;
899 // If the function contains object, regexp or array literals,
900 // allocate extra space for a literals array prefix containing the
901 // context.
902 if (number_of_literals > 0) {
903 literals_array_size += JSFunction::kLiteralsPrefixSize;
904 }
905 shared->set_num_literals(literals_array_size);
906 return shared;
907}
908
909
kmillikin@chromium.org31b12772011-02-02 16:08:26 +0000910Handle<JSMessageObject> Factory::NewJSMessageObject(
911 Handle<String> type,
912 Handle<JSArray> arguments,
913 int start_position,
914 int end_position,
915 Handle<Object> script,
916 Handle<Object> stack_trace,
917 Handle<Object> stack_frames) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000918 CALL_HEAP_FUNCTION(isolate(),
919 isolate()->heap()->AllocateJSMessageObject(*type,
920 *arguments,
921 start_position,
922 end_position,
923 *script,
924 *stack_trace,
925 *stack_frames),
kmillikin@chromium.org31b12772011-02-02 16:08:26 +0000926 JSMessageObject);
927}
928
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000929Handle<SharedFunctionInfo> Factory::NewSharedFunctionInfo(Handle<String> name) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000930 CALL_HEAP_FUNCTION(isolate(),
931 isolate()->heap()->AllocateSharedFunctionInfo(*name),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000932 SharedFunctionInfo);
933}
934
935
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000936Handle<String> Factory::NumberToString(Handle<Object> number) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000937 CALL_HEAP_FUNCTION(isolate(),
938 isolate()->heap()->NumberToString(*number), String);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000939}
940
941
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000942Handle<NumberDictionary> Factory::DictionaryAtNumberPut(
943 Handle<NumberDictionary> dictionary,
944 uint32_t key,
945 Handle<Object> value) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000946 CALL_HEAP_FUNCTION(isolate(),
947 dictionary->AtNumberPut(key, *value),
948 NumberDictionary);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000949}
950
951
952Handle<JSFunction> Factory::NewFunctionHelper(Handle<String> name,
953 Handle<Object> prototype) {
954 Handle<SharedFunctionInfo> function_share = NewSharedFunctionInfo(name);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000955 CALL_HEAP_FUNCTION(
956 isolate(),
957 isolate()->heap()->AllocateFunction(*isolate()->function_map(),
958 *function_share,
959 *prototype),
960 JSFunction);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000961}
962
963
964Handle<JSFunction> Factory::NewFunction(Handle<String> name,
965 Handle<Object> prototype) {
966 Handle<JSFunction> fun = NewFunctionHelper(name, prototype);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000967 fun->set_context(isolate()->context()->global_context());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000968 return fun;
969}
970
971
kmillikin@chromium.org4111b802010-05-03 10:34:42 +0000972Handle<JSFunction> Factory::NewFunctionWithoutPrototypeHelper(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000973 Handle<String> name,
974 StrictModeFlag strict_mode) {
kmillikin@chromium.org4111b802010-05-03 10:34:42 +0000975 Handle<SharedFunctionInfo> function_share = NewSharedFunctionInfo(name);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000976 Handle<Map> map = strict_mode == kStrictMode
977 ? isolate()->strict_mode_function_without_prototype_map()
978 : isolate()->function_without_prototype_map();
979 CALL_HEAP_FUNCTION(isolate(),
980 isolate()->heap()->AllocateFunction(
981 *map,
kmillikin@chromium.org4111b802010-05-03 10:34:42 +0000982 *function_share,
983 *the_hole_value()),
984 JSFunction);
985}
986
987
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000988Handle<JSFunction> Factory::NewFunctionWithoutPrototype(
989 Handle<String> name,
990 StrictModeFlag strict_mode) {
991 Handle<JSFunction> fun = NewFunctionWithoutPrototypeHelper(name, strict_mode);
992 fun->set_context(isolate()->context()->global_context());
kmillikin@chromium.org4111b802010-05-03 10:34:42 +0000993 return fun;
994}
995
996
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000997Handle<Object> Factory::ToObject(Handle<Object> object) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000998 CALL_HEAP_FUNCTION(isolate(), object->ToObject(), Object);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000999}
1000
1001
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001002Handle<Object> Factory::ToObject(Handle<Object> object,
1003 Handle<Context> global_context) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001004 CALL_HEAP_FUNCTION(isolate(), object->ToObject(*global_context), Object);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001005}
1006
1007
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001008#ifdef ENABLE_DEBUGGER_SUPPORT
v8.team.kasperl727e9952008-09-02 14:56:44 +00001009Handle<DebugInfo> Factory::NewDebugInfo(Handle<SharedFunctionInfo> shared) {
1010 // Get the original code of the function.
1011 Handle<Code> code(shared->code());
1012
1013 // Create a copy of the code before allocating the debug info object to avoid
1014 // allocation while setting up the debug info object.
1015 Handle<Code> original_code(*Factory::CopyCode(code));
1016
1017 // Allocate initial fixed array for active break points before allocating the
1018 // debug info object to avoid allocation while setting up the debug info
1019 // object.
1020 Handle<FixedArray> break_points(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001021 NewFixedArray(Debug::kEstimatedNofBreakPointsInFunction));
v8.team.kasperl727e9952008-09-02 14:56:44 +00001022
1023 // Create and set up the debug info object. Debug info contains function, a
1024 // copy of the original code, the executing code and initial fixed array for
1025 // active break points.
1026 Handle<DebugInfo> debug_info =
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001027 Handle<DebugInfo>::cast(NewStruct(DEBUG_INFO_TYPE));
v8.team.kasperl727e9952008-09-02 14:56:44 +00001028 debug_info->set_shared(*shared);
1029 debug_info->set_original_code(*original_code);
1030 debug_info->set_code(*code);
1031 debug_info->set_break_points(*break_points);
1032
1033 // Link debug info to function.
1034 shared->set_debug_info(*debug_info);
1035
1036 return debug_info;
1037}
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001038#endif
v8.team.kasperl727e9952008-09-02 14:56:44 +00001039
1040
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001041Handle<JSObject> Factory::NewArgumentsObject(Handle<Object> callee,
1042 int length) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001043 CALL_HEAP_FUNCTION(
1044 isolate(),
1045 isolate()->heap()->AllocateArgumentsObject(*callee, length), JSObject);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001046}
1047
1048
1049Handle<JSFunction> Factory::CreateApiFunction(
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001050 Handle<FunctionTemplateInfo> obj, ApiInstanceType instance_type) {
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00001051 Handle<Code> code = isolate()->builtins()->HandleApiCall();
1052 Handle<Code> construct_stub = isolate()->builtins()->JSConstructStubApi();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001053
kasper.lund212ac232008-07-16 07:07:30 +00001054 int internal_field_count = 0;
1055 if (!obj->instance_template()->IsUndefined()) {
1056 Handle<ObjectTemplateInfo> instance_template =
1057 Handle<ObjectTemplateInfo>(
1058 ObjectTemplateInfo::cast(obj->instance_template()));
1059 internal_field_count =
1060 Smi::cast(instance_template->internal_field_count())->value();
1061 }
1062
1063 int instance_size = kPointerSize * internal_field_count;
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001064 InstanceType type = INVALID_TYPE;
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001065 switch (instance_type) {
1066 case JavaScriptObject:
1067 type = JS_OBJECT_TYPE;
1068 instance_size += JSObject::kHeaderSize;
1069 break;
1070 case InnerGlobalObject:
1071 type = JS_GLOBAL_OBJECT_TYPE;
1072 instance_size += JSGlobalObject::kSize;
1073 break;
1074 case OuterGlobalObject:
1075 type = JS_GLOBAL_PROXY_TYPE;
1076 instance_size += JSGlobalProxy::kSize;
1077 break;
1078 default:
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001079 break;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001080 }
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001081 ASSERT(type != INVALID_TYPE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001082
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001083 Handle<JSFunction> result =
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001084 NewFunction(Factory::empty_symbol(),
1085 type,
1086 instance_size,
1087 code,
1088 true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001089 // Set class name.
1090 Handle<Object> class_name = Handle<Object>(obj->class_name());
1091 if (class_name->IsString()) {
1092 result->shared()->set_instance_class_name(*class_name);
1093 result->shared()->set_name(*class_name);
1094 }
1095
1096 Handle<Map> map = Handle<Map>(result->initial_map());
1097
1098 // Mark as undetectable if needed.
1099 if (obj->undetectable()) {
1100 map->set_is_undetectable();
1101 }
1102
1103 // Mark as hidden for the __proto__ accessor if needed.
1104 if (obj->hidden_prototype()) {
1105 map->set_is_hidden_prototype();
1106 }
1107
1108 // Mark as needs_access_check if needed.
1109 if (obj->needs_access_check()) {
ager@chromium.org870a0b62008-11-04 11:43:05 +00001110 map->set_is_access_check_needed(true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001111 }
1112
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001113 // Set interceptor information in the map.
1114 if (!obj->named_property_handler()->IsUndefined()) {
1115 map->set_has_named_interceptor();
1116 }
1117 if (!obj->indexed_property_handler()->IsUndefined()) {
1118 map->set_has_indexed_interceptor();
1119 }
1120
1121 // Set instance call-as-function information in the map.
1122 if (!obj->instance_call_handler()->IsUndefined()) {
1123 map->set_has_instance_call_handler();
1124 }
1125
1126 result->shared()->set_function_data(*obj);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001127 result->shared()->set_construct_stub(*construct_stub);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001128 result->shared()->DontAdaptArguments();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001129
1130 // Recursively copy parent templates' accessors, 'data' may be modified.
1131 Handle<DescriptorArray> array =
1132 Handle<DescriptorArray>(map->instance_descriptors());
1133 while (true) {
1134 Handle<Object> props = Handle<Object>(obj->property_accessors());
1135 if (!props->IsUndefined()) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001136 array = CopyAppendCallbackDescriptors(array, props);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001137 }
1138 Handle<Object> parent = Handle<Object>(obj->parent_template());
1139 if (parent->IsUndefined()) break;
1140 obj = Handle<FunctionTemplateInfo>::cast(parent);
1141 }
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001142 if (!array->IsEmpty()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001143 map->set_instance_descriptors(*array);
1144 }
1145
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001146 ASSERT(result->shared()->IsApiFunction());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001147 return result;
1148}
1149
1150
ager@chromium.org236ad962008-09-25 09:45:57 +00001151Handle<MapCache> Factory::NewMapCache(int at_least_space_for) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001152 CALL_HEAP_FUNCTION(isolate(),
1153 MapCache::Allocate(at_least_space_for), MapCache);
ager@chromium.org236ad962008-09-25 09:45:57 +00001154}
1155
1156
lrn@chromium.org303ada72010-10-27 09:33:13 +00001157MUST_USE_RESULT static MaybeObject* UpdateMapCacheWith(Context* context,
1158 FixedArray* keys,
1159 Map* map) {
1160 Object* result;
1161 { MaybeObject* maybe_result =
1162 MapCache::cast(context->map_cache())->Put(keys, map);
1163 if (!maybe_result->ToObject(&result)) return maybe_result;
1164 }
1165 context->set_map_cache(MapCache::cast(result));
ager@chromium.org236ad962008-09-25 09:45:57 +00001166 return result;
1167}
1168
1169
1170Handle<MapCache> Factory::AddToMapCache(Handle<Context> context,
1171 Handle<FixedArray> keys,
1172 Handle<Map> map) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001173 CALL_HEAP_FUNCTION(isolate(),
1174 UpdateMapCacheWith(*context, *keys, *map), MapCache);
ager@chromium.org236ad962008-09-25 09:45:57 +00001175}
1176
1177
1178Handle<Map> Factory::ObjectLiteralMapFromCache(Handle<Context> context,
1179 Handle<FixedArray> keys) {
1180 if (context->map_cache()->IsUndefined()) {
1181 // Allocate the new map cache for the global context.
1182 Handle<MapCache> new_cache = NewMapCache(24);
1183 context->set_map_cache(*new_cache);
1184 }
ager@chromium.org32912102009-01-16 10:38:43 +00001185 // Check to see whether there is a matching element in the cache.
ager@chromium.org236ad962008-09-25 09:45:57 +00001186 Handle<MapCache> cache =
1187 Handle<MapCache>(MapCache::cast(context->map_cache()));
1188 Handle<Object> result = Handle<Object>(cache->Lookup(*keys));
1189 if (result->IsMap()) return Handle<Map>::cast(result);
1190 // Create a new map and add it to the cache.
1191 Handle<Map> map =
ager@chromium.org32912102009-01-16 10:38:43 +00001192 CopyMap(Handle<Map>(context->object_function()->initial_map()),
1193 keys->length());
ager@chromium.org236ad962008-09-25 09:45:57 +00001194 AddToMapCache(context, keys, map);
1195 return Handle<Map>(map);
1196}
1197
1198
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001199void Factory::SetRegExpAtomData(Handle<JSRegExp> regexp,
1200 JSRegExp::Type type,
1201 Handle<String> source,
1202 JSRegExp::Flags flags,
1203 Handle<Object> data) {
1204 Handle<FixedArray> store = NewFixedArray(JSRegExp::kAtomDataSize);
1205
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001206 store->set(JSRegExp::kTagIndex, Smi::FromInt(type));
1207 store->set(JSRegExp::kSourceIndex, *source);
1208 store->set(JSRegExp::kFlagsIndex, Smi::FromInt(flags.value()));
1209 store->set(JSRegExp::kAtomPatternIndex, *data);
1210 regexp->set_data(*store);
1211}
1212
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001213void Factory::SetRegExpIrregexpData(Handle<JSRegExp> regexp,
1214 JSRegExp::Type type,
1215 Handle<String> source,
1216 JSRegExp::Flags flags,
1217 int capture_count) {
1218 Handle<FixedArray> store = NewFixedArray(JSRegExp::kIrregexpDataSize);
1219
1220 store->set(JSRegExp::kTagIndex, Smi::FromInt(type));
1221 store->set(JSRegExp::kSourceIndex, *source);
1222 store->set(JSRegExp::kFlagsIndex, Smi::FromInt(flags.value()));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001223 store->set(JSRegExp::kIrregexpASCIICodeIndex, HEAP->the_hole_value());
1224 store->set(JSRegExp::kIrregexpUC16CodeIndex, HEAP->the_hole_value());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001225 store->set(JSRegExp::kIrregexpMaxRegisterCountIndex, Smi::FromInt(0));
1226 store->set(JSRegExp::kIrregexpCaptureCountIndex,
1227 Smi::FromInt(capture_count));
1228 regexp->set_data(*store);
1229}
1230
1231
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001232
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001233void Factory::ConfigureInstance(Handle<FunctionTemplateInfo> desc,
1234 Handle<JSObject> instance,
1235 bool* pending_exception) {
1236 // Configure the instance by adding the properties specified by the
1237 // instance template.
1238 Handle<Object> instance_template = Handle<Object>(desc->instance_template());
1239 if (!instance_template->IsUndefined()) {
1240 Execution::ConfigureInstance(instance,
1241 instance_template,
1242 pending_exception);
1243 } else {
1244 *pending_exception = false;
1245 }
1246}
1247
1248
1249} } // namespace v8::internal