blob: 9ba48ee5024fa1cb3a59c85864ff88604e8339b0 [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,
vegorov@chromium.org3cf47312011-06-29 13:20:01 +0000254 Handle<JSFunction> function) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000255 CALL_HEAP_FUNCTION(
256 isolate(),
vegorov@chromium.org3cf47312011-06-29 13:20:01 +0000257 isolate()->heap()->AllocateFunctionContext(length, *function),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000258 Context);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000259}
260
261
vegorov@chromium.org3cf47312011-06-29 13:20:01 +0000262Handle<Context> Factory::NewCatchContext(Handle<JSFunction> function,
263 Handle<Context> previous,
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000264 Handle<String> name,
265 Handle<Object> thrown_object) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000266 CALL_HEAP_FUNCTION(
267 isolate(),
vegorov@chromium.org3cf47312011-06-29 13:20:01 +0000268 isolate()->heap()->AllocateCatchContext(*function,
269 *previous,
270 *name,
271 *thrown_object),
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000272 Context);
273}
274
275
vegorov@chromium.org3cf47312011-06-29 13:20:01 +0000276Handle<Context> Factory::NewWithContext(Handle<JSFunction> function,
277 Handle<Context> previous,
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000278 Handle<JSObject> extension) {
279 CALL_HEAP_FUNCTION(
280 isolate(),
vegorov@chromium.org3cf47312011-06-29 13:20:01 +0000281 isolate()->heap()->AllocateWithContext(*function, *previous, *extension),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000282 Context);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000283}
284
285
286Handle<Struct> Factory::NewStruct(InstanceType type) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000287 CALL_HEAP_FUNCTION(
288 isolate(),
289 isolate()->heap()->AllocateStruct(type),
290 Struct);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000291}
292
293
294Handle<AccessorInfo> Factory::NewAccessorInfo() {
295 Handle<AccessorInfo> info =
296 Handle<AccessorInfo>::cast(NewStruct(ACCESSOR_INFO_TYPE));
297 info->set_flag(0); // Must clear the flag, it was initialized as undefined.
298 return info;
299}
300
301
302Handle<Script> Factory::NewScript(Handle<String> source) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000303 // Generate id for this script.
304 int id;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000305 Heap* heap = isolate()->heap();
306 if (heap->last_script_id()->IsUndefined()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000307 // Script ids start from one.
308 id = 1;
309 } else {
310 // Increment id, wrap when positive smi is exhausted.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000311 id = Smi::cast(heap->last_script_id())->value();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000312 id++;
313 if (!Smi::IsValid(id)) {
314 id = 0;
315 }
316 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000317 heap->SetLastScriptId(Smi::FromInt(id));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000318
319 // Create and initialize script object.
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000320 Handle<Foreign> wrapper = NewForeign(0, TENURED);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000321 Handle<Script> script = Handle<Script>::cast(NewStruct(SCRIPT_TYPE));
322 script->set_source(*source);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000323 script->set_name(heap->undefined_value());
324 script->set_id(heap->last_script_id());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000325 script->set_line_offset(Smi::FromInt(0));
326 script->set_column_offset(Smi::FromInt(0));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000327 script->set_data(heap->undefined_value());
328 script->set_context_data(heap->undefined_value());
ager@chromium.orge2902be2009-06-08 12:21:35 +0000329 script->set_type(Smi::FromInt(Script::TYPE_NORMAL));
330 script->set_compilation_type(Smi::FromInt(Script::COMPILATION_TYPE_HOST));
ager@chromium.org9085a012009-05-11 19:22:57 +0000331 script->set_wrapper(*wrapper);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000332 script->set_line_ends(heap->undefined_value());
333 script->set_eval_from_shared(heap->undefined_value());
ager@chromium.orge2902be2009-06-08 12:21:35 +0000334 script->set_eval_from_instructions_offset(Smi::FromInt(0));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000335
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000336 return script;
337}
338
339
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000340Handle<Foreign> Factory::NewForeign(Address addr, PretenureFlag pretenure) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000341 CALL_HEAP_FUNCTION(isolate(),
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000342 isolate()->heap()->AllocateForeign(addr, pretenure),
343 Foreign);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000344}
345
346
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000347Handle<Foreign> Factory::NewForeign(const AccessorDescriptor* desc) {
348 return NewForeign((Address) desc, TENURED);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000349}
350
351
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000352Handle<ByteArray> Factory::NewByteArray(int length, PretenureFlag pretenure) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000353 ASSERT(0 <= length);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000354 CALL_HEAP_FUNCTION(
355 isolate(),
356 isolate()->heap()->AllocateByteArray(length, pretenure),
357 ByteArray);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000358}
359
360
ager@chromium.org3811b432009-10-28 14:53:37 +0000361Handle<ExternalArray> Factory::NewExternalArray(int length,
362 ExternalArrayType array_type,
363 void* external_pointer,
364 PretenureFlag pretenure) {
365 ASSERT(0 <= length);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000366 CALL_HEAP_FUNCTION(
367 isolate(),
368 isolate()->heap()->AllocateExternalArray(length,
369 array_type,
370 external_pointer,
371 pretenure),
372 ExternalArray);
ager@chromium.org3811b432009-10-28 14:53:37 +0000373}
374
375
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000376Handle<JSGlobalPropertyCell> Factory::NewJSGlobalPropertyCell(
377 Handle<Object> value) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000378 CALL_HEAP_FUNCTION(
379 isolate(),
380 isolate()->heap()->AllocateJSGlobalPropertyCell(*value),
381 JSGlobalPropertyCell);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000382}
383
384
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000385Handle<Map> Factory::NewMap(InstanceType type, int instance_size) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000386 CALL_HEAP_FUNCTION(
387 isolate(),
388 isolate()->heap()->AllocateMap(type, instance_size),
389 Map);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000390}
391
392
393Handle<JSObject> Factory::NewFunctionPrototype(Handle<JSFunction> function) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000394 CALL_HEAP_FUNCTION(
395 isolate(),
396 isolate()->heap()->AllocateFunctionPrototype(*function),
397 JSObject);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000398}
399
400
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000401Handle<Map> Factory::CopyMapDropDescriptors(Handle<Map> src) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000402 CALL_HEAP_FUNCTION(isolate(), src->CopyDropDescriptors(), Map);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000403}
404
405
ager@chromium.org32912102009-01-16 10:38:43 +0000406Handle<Map> Factory::CopyMap(Handle<Map> src,
407 int extra_inobject_properties) {
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000408 Handle<Map> copy = CopyMapDropDescriptors(src);
ager@chromium.org32912102009-01-16 10:38:43 +0000409 // Check that we do not overflow the instance size when adding the
410 // extra inobject properties.
411 int instance_size_delta = extra_inobject_properties * kPointerSize;
412 int max_instance_size_delta =
413 JSObject::kMaxInstanceSize - copy->instance_size();
414 if (instance_size_delta > max_instance_size_delta) {
415 // If the instance size overflows, we allocate as many properties
416 // as we can as inobject properties.
417 instance_size_delta = max_instance_size_delta;
418 extra_inobject_properties = max_instance_size_delta >> kPointerSizeLog2;
419 }
420 // Adjust the map with the extra inobject properties.
421 int inobject_properties =
422 copy->inobject_properties() + extra_inobject_properties;
423 copy->set_inobject_properties(inobject_properties);
424 copy->set_unused_property_fields(inobject_properties);
425 copy->set_instance_size(copy->instance_size() + instance_size_delta);
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000426 copy->set_visitor_id(StaticVisitorBase::GetVisitorId(*copy));
ager@chromium.org32912102009-01-16 10:38:43 +0000427 return copy;
428}
429
fschneider@chromium.org40b9da32010-06-28 11:29:21 +0000430
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000431Handle<Map> Factory::CopyMapDropTransitions(Handle<Map> src) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000432 CALL_HEAP_FUNCTION(isolate(), src->CopyDropTransitions(), Map);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000433}
434
435
fschneider@chromium.org40b9da32010-06-28 11:29:21 +0000436Handle<Map> Factory::GetFastElementsMap(Handle<Map> src) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000437 CALL_HEAP_FUNCTION(isolate(), src->GetFastElementsMap(), Map);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +0000438}
439
440
441Handle<Map> Factory::GetSlowElementsMap(Handle<Map> src) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000442 CALL_HEAP_FUNCTION(isolate(), src->GetSlowElementsMap(), Map);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +0000443}
444
445
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000446Handle<Map> Factory::GetExternalArrayElementsMap(
447 Handle<Map> src,
448 ExternalArrayType array_type,
449 bool safe_to_add_transition) {
450 CALL_HEAP_FUNCTION(isolate(),
451 src->GetExternalArrayElementsMap(array_type,
452 safe_to_add_transition),
453 Map);
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +0000454}
455
456
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000457Handle<FixedArray> Factory::CopyFixedArray(Handle<FixedArray> array) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000458 CALL_HEAP_FUNCTION(isolate(), array->Copy(), FixedArray);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000459}
460
461
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000462Handle<JSFunction> Factory::BaseNewFunctionFromSharedFunctionInfo(
463 Handle<SharedFunctionInfo> function_info,
sgjesse@chromium.org846fb742009-12-18 08:56:33 +0000464 Handle<Map> function_map,
465 PretenureFlag pretenure) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000466 CALL_HEAP_FUNCTION(
467 isolate(),
468 isolate()->heap()->AllocateFunction(*function_map,
469 *function_info,
470 isolate()->heap()->the_hole_value(),
471 pretenure),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000472 JSFunction);
473}
474
475
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000476Handle<JSFunction> Factory::NewFunctionFromSharedFunctionInfo(
477 Handle<SharedFunctionInfo> function_info,
sgjesse@chromium.org846fb742009-12-18 08:56:33 +0000478 Handle<Context> context,
479 PretenureFlag pretenure) {
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000480 Handle<JSFunction> result = BaseNewFunctionFromSharedFunctionInfo(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000481 function_info,
482 function_info->strict_mode()
483 ? isolate()->strict_mode_function_map()
484 : isolate()->function_map(),
485 pretenure);
486
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000487 result->set_context(*context);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000488 int number_of_literals = function_info->num_literals();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000489 Handle<FixedArray> literals = NewFixedArray(number_of_literals, pretenure);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000490 if (number_of_literals > 0) {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000491 // Store the object, regexp and array functions in the literals
492 // array prefix. These functions will be used when creating
493 // object, regexp and array literals in this function.
ager@chromium.org236ad962008-09-25 09:45:57 +0000494 literals->set(JSFunction::kLiteralGlobalContextIndex,
495 context->global_context());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000496 }
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000497 result->set_literals(*literals);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000498 result->set_next_function_link(isolate()->heap()->undefined_value());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000499
500 if (V8::UseCrankshaft() &&
501 FLAG_always_opt &&
502 result->is_compiled() &&
503 !function_info->is_toplevel() &&
504 function_info->allows_lazy_compilation()) {
505 result->MarkForLazyRecompilation();
506 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000507 return result;
508}
509
510
511Handle<Object> Factory::NewNumber(double value,
512 PretenureFlag pretenure) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000513 CALL_HEAP_FUNCTION(
514 isolate(),
515 isolate()->heap()->NumberFromDouble(value, pretenure), Object);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000516}
517
518
519Handle<Object> Factory::NewNumberFromInt(int value) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000520 CALL_HEAP_FUNCTION(
521 isolate(),
522 isolate()->heap()->NumberFromInt32(value), Object);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000523}
524
525
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000526Handle<Object> Factory::NewNumberFromUint(uint32_t value) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000527 CALL_HEAP_FUNCTION(
528 isolate(),
529 isolate()->heap()->NumberFromUint32(value), Object);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000530}
531
532
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000533Handle<JSObject> Factory::NewNeanderObject() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000534 CALL_HEAP_FUNCTION(
535 isolate(),
536 isolate()->heap()->AllocateJSObjectFromMap(
537 isolate()->heap()->neander_map()),
538 JSObject);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000539}
540
541
542Handle<Object> Factory::NewTypeError(const char* type,
543 Vector< Handle<Object> > args) {
544 return NewError("MakeTypeError", type, args);
545}
546
547
548Handle<Object> Factory::NewTypeError(Handle<String> message) {
549 return NewError("$TypeError", message);
550}
551
552
553Handle<Object> Factory::NewRangeError(const char* type,
554 Vector< Handle<Object> > args) {
555 return NewError("MakeRangeError", type, args);
556}
557
558
559Handle<Object> Factory::NewRangeError(Handle<String> message) {
560 return NewError("$RangeError", message);
561}
562
563
564Handle<Object> Factory::NewSyntaxError(const char* type, Handle<JSArray> args) {
565 return NewError("MakeSyntaxError", type, args);
566}
567
568
569Handle<Object> Factory::NewSyntaxError(Handle<String> message) {
570 return NewError("$SyntaxError", message);
571}
572
573
574Handle<Object> Factory::NewReferenceError(const char* type,
575 Vector< Handle<Object> > args) {
576 return NewError("MakeReferenceError", type, args);
577}
578
579
580Handle<Object> Factory::NewReferenceError(Handle<String> message) {
581 return NewError("$ReferenceError", message);
582}
583
584
585Handle<Object> Factory::NewError(const char* maker, const char* type,
586 Vector< Handle<Object> > args) {
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000587 v8::HandleScope scope; // Instantiate a closeable HandleScope for EscapeFrom.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000588 Handle<FixedArray> array = NewFixedArray(args.length());
ager@chromium.org7c537e22008-10-16 08:43:32 +0000589 for (int i = 0; i < args.length(); i++) {
590 array->set(i, *args[i]);
591 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000592 Handle<JSArray> object = NewJSArrayWithElements(array);
ager@chromium.org7c537e22008-10-16 08:43:32 +0000593 Handle<Object> result = NewError(maker, type, object);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000594 return result.EscapeFrom(&scope);
595}
596
597
598Handle<Object> Factory::NewEvalError(const char* type,
599 Vector< Handle<Object> > args) {
600 return NewError("MakeEvalError", type, args);
601}
602
603
604Handle<Object> Factory::NewError(const char* type,
605 Vector< Handle<Object> > args) {
606 return NewError("MakeError", type, args);
607}
608
609
610Handle<Object> Factory::NewError(const char* maker,
611 const char* type,
612 Handle<JSArray> args) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000613 Handle<String> make_str = LookupAsciiSymbol(maker);
614 Handle<Object> fun_obj(
615 isolate()->js_builtins_object()->GetPropertyNoExceptionThrown(*make_str));
ager@chromium.org4af710e2009-09-15 12:20:11 +0000616 // If the builtins haven't been properly configured yet this error
617 // constructor may not have been defined. Bail out.
618 if (!fun_obj->IsJSFunction())
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000619 return undefined_value();
ager@chromium.org4af710e2009-09-15 12:20:11 +0000620 Handle<JSFunction> fun = Handle<JSFunction>::cast(fun_obj);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000621 Handle<Object> type_obj = LookupAsciiSymbol(type);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000622 Object** argv[2] = { type_obj.location(),
623 Handle<Object>::cast(args).location() };
624
625 // Invoke the JavaScript factory method. If an exception is thrown while
626 // running the factory method, use the exception as the result.
627 bool caught_exception;
628 Handle<Object> result = Execution::TryCall(fun,
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000629 isolate()->js_builtins_object(), 2, argv, &caught_exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000630 return result;
631}
632
633
634Handle<Object> Factory::NewError(Handle<String> message) {
635 return NewError("$Error", message);
636}
637
638
639Handle<Object> Factory::NewError(const char* constructor,
640 Handle<String> message) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000641 Handle<String> constr = LookupAsciiSymbol(constructor);
642 Handle<JSFunction> fun = Handle<JSFunction>(
643 JSFunction::cast(isolate()->js_builtins_object()->
644 GetPropertyNoExceptionThrown(*constr)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000645 Object** argv[1] = { Handle<Object>::cast(message).location() };
646
647 // Invoke the JavaScript factory method. If an exception is thrown while
648 // running the factory method, use the exception as the result.
649 bool caught_exception;
650 Handle<Object> result = Execution::TryCall(fun,
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000651 isolate()->js_builtins_object(), 1, argv, &caught_exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000652 return result;
653}
654
655
656Handle<JSFunction> Factory::NewFunction(Handle<String> name,
657 InstanceType type,
658 int instance_size,
659 Handle<Code> code,
660 bool force_initial_map) {
661 // Allocate the function
662 Handle<JSFunction> function = NewFunction(name, the_hole_value());
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000663
664 // Setup the code pointer in both the shared function info and in
665 // the function itself.
666 function->shared()->set_code(*code);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000667 function->set_code(*code);
668
669 if (force_initial_map ||
670 type != JS_OBJECT_TYPE ||
671 instance_size != JSObject::kHeaderSize) {
672 Handle<Map> initial_map = NewMap(type, instance_size);
673 Handle<JSObject> prototype = NewFunctionPrototype(function);
674 initial_map->set_prototype(*prototype);
675 function->set_initial_map(*initial_map);
676 initial_map->set_constructor(*function);
677 } else {
678 ASSERT(!function->has_initial_map());
679 ASSERT(!function->has_prototype());
680 }
681
682 return function;
683}
684
685
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000686Handle<JSFunction> Factory::NewFunctionWithPrototype(Handle<String> name,
687 InstanceType type,
688 int instance_size,
689 Handle<JSObject> prototype,
690 Handle<Code> code,
691 bool force_initial_map) {
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000692 // Allocate the function.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000693 Handle<JSFunction> function = NewFunction(name, prototype);
694
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000695 // Setup the code pointer in both the shared function info and in
696 // the function itself.
697 function->shared()->set_code(*code);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000698 function->set_code(*code);
699
700 if (force_initial_map ||
701 type != JS_OBJECT_TYPE ||
702 instance_size != JSObject::kHeaderSize) {
703 Handle<Map> initial_map = NewMap(type, instance_size);
704 function->set_initial_map(*initial_map);
705 initial_map->set_constructor(*function);
706 }
707
708 // Set function.prototype and give the prototype a constructor
709 // property that refers to the function.
710 SetPrototypeProperty(function, prototype);
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +0000711 // Currently safe because it is only invoked from Genesis.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000712 SetLocalPropertyNoThrow(prototype, constructor_symbol(), function, DONT_ENUM);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000713 return function;
714}
715
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000716
kmillikin@chromium.org4111b802010-05-03 10:34:42 +0000717Handle<JSFunction> Factory::NewFunctionWithoutPrototype(Handle<String> name,
718 Handle<Code> code) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000719 Handle<JSFunction> function = NewFunctionWithoutPrototype(name,
720 kNonStrictMode);
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000721 function->shared()->set_code(*code);
kmillikin@chromium.org4111b802010-05-03 10:34:42 +0000722 function->set_code(*code);
723 ASSERT(!function->has_initial_map());
724 ASSERT(!function->has_prototype());
725 return function;
726}
727
728
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000729Handle<Code> Factory::NewCode(const CodeDesc& desc,
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000730 Code::Flags flags,
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +0000731 Handle<Object> self_ref,
732 bool immovable) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000733 CALL_HEAP_FUNCTION(isolate(),
734 isolate()->heap()->CreateCode(
735 desc, flags, self_ref, immovable),
736 Code);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000737}
738
739
740Handle<Code> Factory::CopyCode(Handle<Code> code) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000741 CALL_HEAP_FUNCTION(isolate(),
742 isolate()->heap()->CopyCode(*code),
743 Code);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000744}
745
746
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000747Handle<Code> Factory::CopyCode(Handle<Code> code, Vector<byte> reloc_info) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000748 CALL_HEAP_FUNCTION(isolate(),
749 isolate()->heap()->CopyCode(*code, reloc_info),
750 Code);
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000751}
752
753
lrn@chromium.org303ada72010-10-27 09:33:13 +0000754MUST_USE_RESULT static inline MaybeObject* DoCopyInsert(
755 DescriptorArray* array,
756 String* key,
757 Object* value,
758 PropertyAttributes attributes) {
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000759 CallbacksDescriptor desc(key, value, attributes);
lrn@chromium.org303ada72010-10-27 09:33:13 +0000760 MaybeObject* obj = array->CopyInsert(&desc, REMOVE_TRANSITIONS);
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000761 return obj;
762}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000763
764
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000765// Allocate the new array.
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000766Handle<DescriptorArray> Factory::CopyAppendForeignDescriptor(
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000767 Handle<DescriptorArray> array,
768 Handle<String> key,
769 Handle<Object> value,
770 PropertyAttributes attributes) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000771 CALL_HEAP_FUNCTION(isolate(),
772 DoCopyInsert(*array, *key, *value, attributes),
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000773 DescriptorArray);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000774}
775
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000776
777Handle<String> Factory::SymbolFromString(Handle<String> value) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000778 CALL_HEAP_FUNCTION(isolate(),
779 isolate()->heap()->LookupSymbol(*value), String);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000780}
781
782
783Handle<DescriptorArray> Factory::CopyAppendCallbackDescriptors(
784 Handle<DescriptorArray> array,
785 Handle<Object> descriptors) {
786 v8::NeanderArray callbacks(descriptors);
787 int nof_callbacks = callbacks.length();
788 Handle<DescriptorArray> result =
789 NewDescriptorArray(array->number_of_descriptors() + nof_callbacks);
790
791 // Number of descriptors added to the result so far.
792 int descriptor_count = 0;
793
794 // Copy the descriptors from the array.
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000795 for (int i = 0; i < array->number_of_descriptors(); i++) {
796 if (array->GetType(i) != NULL_DESCRIPTOR) {
797 result->CopyFrom(descriptor_count++, *array, i);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000798 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000799 }
800
801 // Number of duplicates detected.
802 int duplicates = 0;
803
804 // Fill in new callback descriptors. Process the callbacks from
805 // back to front so that the last callback with a given name takes
806 // precedence over previously added callbacks with that name.
807 for (int i = nof_callbacks - 1; i >= 0; i--) {
808 Handle<AccessorInfo> entry =
809 Handle<AccessorInfo>(AccessorInfo::cast(callbacks.get(i)));
810 // Ensure the key is a symbol before writing into the instance descriptor.
811 Handle<String> key =
812 SymbolFromString(Handle<String>(String::cast(entry->name())));
813 // Check if a descriptor with this name already exists before writing.
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000814 if (result->LinearSearch(*key, descriptor_count) ==
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000815 DescriptorArray::kNotFound) {
816 CallbacksDescriptor desc(*key, *entry, entry->property_attributes());
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000817 result->Set(descriptor_count, &desc);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000818 descriptor_count++;
819 } else {
820 duplicates++;
821 }
822 }
823
824 // If duplicates were detected, allocate a result of the right size
825 // and transfer the elements.
826 if (duplicates > 0) {
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000827 int number_of_descriptors = result->number_of_descriptors() - duplicates;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000828 Handle<DescriptorArray> new_result =
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000829 NewDescriptorArray(number_of_descriptors);
830 for (int i = 0; i < number_of_descriptors; i++) {
831 new_result->CopyFrom(i, *result, i);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000832 }
833 result = new_result;
834 }
835
836 // Sort the result before returning.
837 result->Sort();
838 return result;
839}
840
841
842Handle<JSObject> Factory::NewJSObject(Handle<JSFunction> constructor,
843 PretenureFlag pretenure) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000844 CALL_HEAP_FUNCTION(
845 isolate(),
846 isolate()->heap()->AllocateJSObject(*constructor, pretenure), JSObject);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000847}
848
849
kasperl@chromium.org2abc4502009-07-02 07:00:29 +0000850Handle<GlobalObject> Factory::NewGlobalObject(
851 Handle<JSFunction> constructor) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000852 CALL_HEAP_FUNCTION(isolate(),
853 isolate()->heap()->AllocateGlobalObject(*constructor),
kasperl@chromium.org2abc4502009-07-02 07:00:29 +0000854 GlobalObject);
855}
856
857
858
ager@chromium.org236ad962008-09-25 09:45:57 +0000859Handle<JSObject> Factory::NewJSObjectFromMap(Handle<Map> map) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000860 CALL_HEAP_FUNCTION(
861 isolate(),
862 isolate()->heap()->AllocateJSObjectFromMap(*map, NOT_TENURED),
863 JSObject);
ager@chromium.org236ad962008-09-25 09:45:57 +0000864}
865
866
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +0000867Handle<JSArray> Factory::NewJSArray(int capacity,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000868 PretenureFlag pretenure) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000869 Handle<JSObject> obj = NewJSObject(isolate()->array_function(), pretenure);
870 CALL_HEAP_FUNCTION(isolate(),
871 Handle<JSArray>::cast(obj)->Initialize(capacity),
872 JSArray);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000873}
874
875
876Handle<JSArray> Factory::NewJSArrayWithElements(Handle<FixedArray> elements,
877 PretenureFlag pretenure) {
878 Handle<JSArray> result =
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000879 Handle<JSArray>::cast(NewJSObject(isolate()->array_function(),
880 pretenure));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000881 result->SetContent(*elements);
882 return result;
883}
884
885
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000886Handle<JSProxy> Factory::NewJSProxy(Handle<Object> handler,
887 Handle<Object> prototype) {
888 CALL_HEAP_FUNCTION(
889 isolate(),
890 isolate()->heap()->AllocateJSProxy(*handler, *prototype),
891 JSProxy);
892}
893
894
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000895Handle<SharedFunctionInfo> Factory::NewSharedFunctionInfo(
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +0000896 Handle<String> name,
897 int number_of_literals,
898 Handle<Code> code,
ager@chromium.orgb5737492010-07-15 09:29:43 +0000899 Handle<SerializedScopeInfo> scope_info) {
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000900 Handle<SharedFunctionInfo> shared = NewSharedFunctionInfo(name);
901 shared->set_code(*code);
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +0000902 shared->set_scope_info(*scope_info);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000903 int literals_array_size = number_of_literals;
904 // If the function contains object, regexp or array literals,
905 // allocate extra space for a literals array prefix containing the
906 // context.
907 if (number_of_literals > 0) {
908 literals_array_size += JSFunction::kLiteralsPrefixSize;
909 }
910 shared->set_num_literals(literals_array_size);
911 return shared;
912}
913
914
kmillikin@chromium.org31b12772011-02-02 16:08:26 +0000915Handle<JSMessageObject> Factory::NewJSMessageObject(
916 Handle<String> type,
917 Handle<JSArray> arguments,
918 int start_position,
919 int end_position,
920 Handle<Object> script,
921 Handle<Object> stack_trace,
922 Handle<Object> stack_frames) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000923 CALL_HEAP_FUNCTION(isolate(),
924 isolate()->heap()->AllocateJSMessageObject(*type,
925 *arguments,
926 start_position,
927 end_position,
928 *script,
929 *stack_trace,
930 *stack_frames),
kmillikin@chromium.org31b12772011-02-02 16:08:26 +0000931 JSMessageObject);
932}
933
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000934Handle<SharedFunctionInfo> Factory::NewSharedFunctionInfo(Handle<String> name) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000935 CALL_HEAP_FUNCTION(isolate(),
936 isolate()->heap()->AllocateSharedFunctionInfo(*name),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000937 SharedFunctionInfo);
938}
939
940
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000941Handle<String> Factory::NumberToString(Handle<Object> number) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000942 CALL_HEAP_FUNCTION(isolate(),
943 isolate()->heap()->NumberToString(*number), String);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000944}
945
946
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000947Handle<NumberDictionary> Factory::DictionaryAtNumberPut(
948 Handle<NumberDictionary> dictionary,
949 uint32_t key,
950 Handle<Object> value) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000951 CALL_HEAP_FUNCTION(isolate(),
952 dictionary->AtNumberPut(key, *value),
953 NumberDictionary);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000954}
955
956
957Handle<JSFunction> Factory::NewFunctionHelper(Handle<String> name,
958 Handle<Object> prototype) {
959 Handle<SharedFunctionInfo> function_share = NewSharedFunctionInfo(name);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000960 CALL_HEAP_FUNCTION(
961 isolate(),
962 isolate()->heap()->AllocateFunction(*isolate()->function_map(),
963 *function_share,
964 *prototype),
965 JSFunction);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000966}
967
968
969Handle<JSFunction> Factory::NewFunction(Handle<String> name,
970 Handle<Object> prototype) {
971 Handle<JSFunction> fun = NewFunctionHelper(name, prototype);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000972 fun->set_context(isolate()->context()->global_context());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000973 return fun;
974}
975
976
kmillikin@chromium.org4111b802010-05-03 10:34:42 +0000977Handle<JSFunction> Factory::NewFunctionWithoutPrototypeHelper(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000978 Handle<String> name,
979 StrictModeFlag strict_mode) {
kmillikin@chromium.org4111b802010-05-03 10:34:42 +0000980 Handle<SharedFunctionInfo> function_share = NewSharedFunctionInfo(name);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000981 Handle<Map> map = strict_mode == kStrictMode
982 ? isolate()->strict_mode_function_without_prototype_map()
983 : isolate()->function_without_prototype_map();
984 CALL_HEAP_FUNCTION(isolate(),
985 isolate()->heap()->AllocateFunction(
986 *map,
kmillikin@chromium.org4111b802010-05-03 10:34:42 +0000987 *function_share,
988 *the_hole_value()),
989 JSFunction);
990}
991
992
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000993Handle<JSFunction> Factory::NewFunctionWithoutPrototype(
994 Handle<String> name,
995 StrictModeFlag strict_mode) {
996 Handle<JSFunction> fun = NewFunctionWithoutPrototypeHelper(name, strict_mode);
997 fun->set_context(isolate()->context()->global_context());
kmillikin@chromium.org4111b802010-05-03 10:34:42 +0000998 return fun;
999}
1000
1001
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001002Handle<Object> Factory::ToObject(Handle<Object> object) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001003 CALL_HEAP_FUNCTION(isolate(), object->ToObject(), Object);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001004}
1005
1006
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001007Handle<Object> Factory::ToObject(Handle<Object> object,
1008 Handle<Context> global_context) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001009 CALL_HEAP_FUNCTION(isolate(), object->ToObject(*global_context), Object);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001010}
1011
1012
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001013#ifdef ENABLE_DEBUGGER_SUPPORT
v8.team.kasperl727e9952008-09-02 14:56:44 +00001014Handle<DebugInfo> Factory::NewDebugInfo(Handle<SharedFunctionInfo> shared) {
1015 // Get the original code of the function.
1016 Handle<Code> code(shared->code());
1017
1018 // Create a copy of the code before allocating the debug info object to avoid
1019 // allocation while setting up the debug info object.
1020 Handle<Code> original_code(*Factory::CopyCode(code));
1021
1022 // Allocate initial fixed array for active break points before allocating the
1023 // debug info object to avoid allocation while setting up the debug info
1024 // object.
1025 Handle<FixedArray> break_points(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001026 NewFixedArray(Debug::kEstimatedNofBreakPointsInFunction));
v8.team.kasperl727e9952008-09-02 14:56:44 +00001027
1028 // Create and set up the debug info object. Debug info contains function, a
1029 // copy of the original code, the executing code and initial fixed array for
1030 // active break points.
1031 Handle<DebugInfo> debug_info =
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001032 Handle<DebugInfo>::cast(NewStruct(DEBUG_INFO_TYPE));
v8.team.kasperl727e9952008-09-02 14:56:44 +00001033 debug_info->set_shared(*shared);
1034 debug_info->set_original_code(*original_code);
1035 debug_info->set_code(*code);
1036 debug_info->set_break_points(*break_points);
1037
1038 // Link debug info to function.
1039 shared->set_debug_info(*debug_info);
1040
1041 return debug_info;
1042}
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001043#endif
v8.team.kasperl727e9952008-09-02 14:56:44 +00001044
1045
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001046Handle<JSObject> Factory::NewArgumentsObject(Handle<Object> callee,
1047 int length) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001048 CALL_HEAP_FUNCTION(
1049 isolate(),
1050 isolate()->heap()->AllocateArgumentsObject(*callee, length), JSObject);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001051}
1052
1053
1054Handle<JSFunction> Factory::CreateApiFunction(
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001055 Handle<FunctionTemplateInfo> obj, ApiInstanceType instance_type) {
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00001056 Handle<Code> code = isolate()->builtins()->HandleApiCall();
1057 Handle<Code> construct_stub = isolate()->builtins()->JSConstructStubApi();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001058
kasper.lund212ac232008-07-16 07:07:30 +00001059 int internal_field_count = 0;
1060 if (!obj->instance_template()->IsUndefined()) {
1061 Handle<ObjectTemplateInfo> instance_template =
1062 Handle<ObjectTemplateInfo>(
1063 ObjectTemplateInfo::cast(obj->instance_template()));
1064 internal_field_count =
1065 Smi::cast(instance_template->internal_field_count())->value();
1066 }
1067
1068 int instance_size = kPointerSize * internal_field_count;
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001069 InstanceType type = INVALID_TYPE;
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001070 switch (instance_type) {
1071 case JavaScriptObject:
1072 type = JS_OBJECT_TYPE;
1073 instance_size += JSObject::kHeaderSize;
1074 break;
1075 case InnerGlobalObject:
1076 type = JS_GLOBAL_OBJECT_TYPE;
1077 instance_size += JSGlobalObject::kSize;
1078 break;
1079 case OuterGlobalObject:
1080 type = JS_GLOBAL_PROXY_TYPE;
1081 instance_size += JSGlobalProxy::kSize;
1082 break;
1083 default:
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001084 break;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001085 }
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001086 ASSERT(type != INVALID_TYPE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001087
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001088 Handle<JSFunction> result =
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001089 NewFunction(Factory::empty_symbol(),
1090 type,
1091 instance_size,
1092 code,
1093 true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001094 // Set class name.
1095 Handle<Object> class_name = Handle<Object>(obj->class_name());
1096 if (class_name->IsString()) {
1097 result->shared()->set_instance_class_name(*class_name);
1098 result->shared()->set_name(*class_name);
1099 }
1100
1101 Handle<Map> map = Handle<Map>(result->initial_map());
1102
1103 // Mark as undetectable if needed.
1104 if (obj->undetectable()) {
1105 map->set_is_undetectable();
1106 }
1107
1108 // Mark as hidden for the __proto__ accessor if needed.
1109 if (obj->hidden_prototype()) {
1110 map->set_is_hidden_prototype();
1111 }
1112
1113 // Mark as needs_access_check if needed.
1114 if (obj->needs_access_check()) {
ager@chromium.org870a0b62008-11-04 11:43:05 +00001115 map->set_is_access_check_needed(true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001116 }
1117
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001118 // Set interceptor information in the map.
1119 if (!obj->named_property_handler()->IsUndefined()) {
1120 map->set_has_named_interceptor();
1121 }
1122 if (!obj->indexed_property_handler()->IsUndefined()) {
1123 map->set_has_indexed_interceptor();
1124 }
1125
1126 // Set instance call-as-function information in the map.
1127 if (!obj->instance_call_handler()->IsUndefined()) {
1128 map->set_has_instance_call_handler();
1129 }
1130
1131 result->shared()->set_function_data(*obj);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001132 result->shared()->set_construct_stub(*construct_stub);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001133 result->shared()->DontAdaptArguments();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001134
1135 // Recursively copy parent templates' accessors, 'data' may be modified.
1136 Handle<DescriptorArray> array =
1137 Handle<DescriptorArray>(map->instance_descriptors());
1138 while (true) {
1139 Handle<Object> props = Handle<Object>(obj->property_accessors());
1140 if (!props->IsUndefined()) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001141 array = CopyAppendCallbackDescriptors(array, props);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001142 }
1143 Handle<Object> parent = Handle<Object>(obj->parent_template());
1144 if (parent->IsUndefined()) break;
1145 obj = Handle<FunctionTemplateInfo>::cast(parent);
1146 }
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001147 if (!array->IsEmpty()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001148 map->set_instance_descriptors(*array);
1149 }
1150
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001151 ASSERT(result->shared()->IsApiFunction());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001152 return result;
1153}
1154
1155
ager@chromium.org236ad962008-09-25 09:45:57 +00001156Handle<MapCache> Factory::NewMapCache(int at_least_space_for) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001157 CALL_HEAP_FUNCTION(isolate(),
1158 MapCache::Allocate(at_least_space_for), MapCache);
ager@chromium.org236ad962008-09-25 09:45:57 +00001159}
1160
1161
lrn@chromium.org303ada72010-10-27 09:33:13 +00001162MUST_USE_RESULT static MaybeObject* UpdateMapCacheWith(Context* context,
1163 FixedArray* keys,
1164 Map* map) {
1165 Object* result;
1166 { MaybeObject* maybe_result =
1167 MapCache::cast(context->map_cache())->Put(keys, map);
1168 if (!maybe_result->ToObject(&result)) return maybe_result;
1169 }
1170 context->set_map_cache(MapCache::cast(result));
ager@chromium.org236ad962008-09-25 09:45:57 +00001171 return result;
1172}
1173
1174
1175Handle<MapCache> Factory::AddToMapCache(Handle<Context> context,
1176 Handle<FixedArray> keys,
1177 Handle<Map> map) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001178 CALL_HEAP_FUNCTION(isolate(),
1179 UpdateMapCacheWith(*context, *keys, *map), MapCache);
ager@chromium.org236ad962008-09-25 09:45:57 +00001180}
1181
1182
1183Handle<Map> Factory::ObjectLiteralMapFromCache(Handle<Context> context,
1184 Handle<FixedArray> keys) {
1185 if (context->map_cache()->IsUndefined()) {
1186 // Allocate the new map cache for the global context.
1187 Handle<MapCache> new_cache = NewMapCache(24);
1188 context->set_map_cache(*new_cache);
1189 }
ager@chromium.org32912102009-01-16 10:38:43 +00001190 // Check to see whether there is a matching element in the cache.
ager@chromium.org236ad962008-09-25 09:45:57 +00001191 Handle<MapCache> cache =
1192 Handle<MapCache>(MapCache::cast(context->map_cache()));
1193 Handle<Object> result = Handle<Object>(cache->Lookup(*keys));
1194 if (result->IsMap()) return Handle<Map>::cast(result);
1195 // Create a new map and add it to the cache.
1196 Handle<Map> map =
ager@chromium.org32912102009-01-16 10:38:43 +00001197 CopyMap(Handle<Map>(context->object_function()->initial_map()),
1198 keys->length());
ager@chromium.org236ad962008-09-25 09:45:57 +00001199 AddToMapCache(context, keys, map);
1200 return Handle<Map>(map);
1201}
1202
1203
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001204void Factory::SetRegExpAtomData(Handle<JSRegExp> regexp,
1205 JSRegExp::Type type,
1206 Handle<String> source,
1207 JSRegExp::Flags flags,
1208 Handle<Object> data) {
1209 Handle<FixedArray> store = NewFixedArray(JSRegExp::kAtomDataSize);
1210
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001211 store->set(JSRegExp::kTagIndex, Smi::FromInt(type));
1212 store->set(JSRegExp::kSourceIndex, *source);
1213 store->set(JSRegExp::kFlagsIndex, Smi::FromInt(flags.value()));
1214 store->set(JSRegExp::kAtomPatternIndex, *data);
1215 regexp->set_data(*store);
1216}
1217
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001218void Factory::SetRegExpIrregexpData(Handle<JSRegExp> regexp,
1219 JSRegExp::Type type,
1220 Handle<String> source,
1221 JSRegExp::Flags flags,
1222 int capture_count) {
1223 Handle<FixedArray> store = NewFixedArray(JSRegExp::kIrregexpDataSize);
jkummerow@chromium.orgddda9e82011-07-06 11:27:02 +00001224 Smi* uninitialized = Smi::FromInt(JSRegExp::kUninitializedValue);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001225 store->set(JSRegExp::kTagIndex, Smi::FromInt(type));
1226 store->set(JSRegExp::kSourceIndex, *source);
1227 store->set(JSRegExp::kFlagsIndex, Smi::FromInt(flags.value()));
jkummerow@chromium.orgddda9e82011-07-06 11:27:02 +00001228 store->set(JSRegExp::kIrregexpASCIICodeIndex, uninitialized);
1229 store->set(JSRegExp::kIrregexpUC16CodeIndex, uninitialized);
1230 store->set(JSRegExp::kIrregexpASCIICodeSavedIndex, uninitialized);
1231 store->set(JSRegExp::kIrregexpUC16CodeSavedIndex, uninitialized);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001232 store->set(JSRegExp::kIrregexpMaxRegisterCountIndex, Smi::FromInt(0));
1233 store->set(JSRegExp::kIrregexpCaptureCountIndex,
1234 Smi::FromInt(capture_count));
1235 regexp->set_data(*store);
1236}
1237
1238
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001239
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001240void Factory::ConfigureInstance(Handle<FunctionTemplateInfo> desc,
1241 Handle<JSObject> instance,
1242 bool* pending_exception) {
1243 // Configure the instance by adding the properties specified by the
1244 // instance template.
1245 Handle<Object> instance_template = Handle<Object>(desc->instance_template());
1246 if (!instance_template->IsUndefined()) {
1247 Execution::ConfigureInstance(instance,
1248 instance_template,
1249 pending_exception);
1250 } else {
1251 *pending_exception = false;
1252 }
1253}
1254
1255
1256} } // namespace v8::internal