blob: 2c100a4940e69124aace22c35c8636d9c88a4400 [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
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +0000182Handle<String> 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),
187 String);
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +0000188}
189
190
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000191Handle<String> 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),
196 String);
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
217Handle<String> Factory::NewExternalStringFromAscii(
218 ExternalAsciiString::Resource* resource) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000219 CALL_HEAP_FUNCTION(
220 isolate(),
221 isolate()->heap()->AllocateExternalStringFromAscii(resource),
222 String);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000223}
224
225
226Handle<String> Factory::NewExternalStringFromTwoByte(
227 ExternalTwoByteString::Resource* resource) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000228 CALL_HEAP_FUNCTION(
229 isolate(),
230 isolate()->heap()->AllocateExternalStringFromTwoByte(resource),
231 String);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000232}
233
234
235Handle<Context> Factory::NewGlobalContext() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000236 CALL_HEAP_FUNCTION(
237 isolate(),
238 isolate()->heap()->AllocateGlobalContext(),
239 Context);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000240}
241
242
243Handle<Context> Factory::NewFunctionContext(int length,
244 Handle<JSFunction> closure) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000245 CALL_HEAP_FUNCTION(
246 isolate(),
247 isolate()->heap()->AllocateFunctionContext(length, *closure),
248 Context);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000249}
250
251
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000252Handle<Context> Factory::NewCatchContext(Handle<Context> previous,
253 Handle<String> name,
254 Handle<Object> thrown_object) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000255 CALL_HEAP_FUNCTION(
256 isolate(),
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000257 isolate()->heap()->AllocateCatchContext(*previous, *name, *thrown_object),
258 Context);
259}
260
261
262Handle<Context> Factory::NewWithContext(Handle<Context> previous,
263 Handle<JSObject> extension) {
264 CALL_HEAP_FUNCTION(
265 isolate(),
266 isolate()->heap()->AllocateWithContext(*previous, *extension),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000267 Context);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000268}
269
270
271Handle<Struct> Factory::NewStruct(InstanceType type) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000272 CALL_HEAP_FUNCTION(
273 isolate(),
274 isolate()->heap()->AllocateStruct(type),
275 Struct);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000276}
277
278
279Handle<AccessorInfo> Factory::NewAccessorInfo() {
280 Handle<AccessorInfo> info =
281 Handle<AccessorInfo>::cast(NewStruct(ACCESSOR_INFO_TYPE));
282 info->set_flag(0); // Must clear the flag, it was initialized as undefined.
283 return info;
284}
285
286
287Handle<Script> Factory::NewScript(Handle<String> source) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000288 // Generate id for this script.
289 int id;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000290 Heap* heap = isolate()->heap();
291 if (heap->last_script_id()->IsUndefined()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000292 // Script ids start from one.
293 id = 1;
294 } else {
295 // Increment id, wrap when positive smi is exhausted.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000296 id = Smi::cast(heap->last_script_id())->value();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000297 id++;
298 if (!Smi::IsValid(id)) {
299 id = 0;
300 }
301 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000302 heap->SetLastScriptId(Smi::FromInt(id));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000303
304 // Create and initialize script object.
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000305 Handle<Foreign> wrapper = NewForeign(0, TENURED);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000306 Handle<Script> script = Handle<Script>::cast(NewStruct(SCRIPT_TYPE));
307 script->set_source(*source);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000308 script->set_name(heap->undefined_value());
309 script->set_id(heap->last_script_id());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000310 script->set_line_offset(Smi::FromInt(0));
311 script->set_column_offset(Smi::FromInt(0));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000312 script->set_data(heap->undefined_value());
313 script->set_context_data(heap->undefined_value());
ager@chromium.orge2902be2009-06-08 12:21:35 +0000314 script->set_type(Smi::FromInt(Script::TYPE_NORMAL));
315 script->set_compilation_type(Smi::FromInt(Script::COMPILATION_TYPE_HOST));
ager@chromium.org9085a012009-05-11 19:22:57 +0000316 script->set_wrapper(*wrapper);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000317 script->set_line_ends(heap->undefined_value());
318 script->set_eval_from_shared(heap->undefined_value());
ager@chromium.orge2902be2009-06-08 12:21:35 +0000319 script->set_eval_from_instructions_offset(Smi::FromInt(0));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000320
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000321 return script;
322}
323
324
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000325Handle<Foreign> Factory::NewForeign(Address addr, PretenureFlag pretenure) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000326 CALL_HEAP_FUNCTION(isolate(),
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000327 isolate()->heap()->AllocateForeign(addr, pretenure),
328 Foreign);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000329}
330
331
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000332Handle<Foreign> Factory::NewForeign(const AccessorDescriptor* desc) {
333 return NewForeign((Address) desc, TENURED);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000334}
335
336
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000337Handle<ByteArray> Factory::NewByteArray(int length, PretenureFlag pretenure) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000338 ASSERT(0 <= length);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000339 CALL_HEAP_FUNCTION(
340 isolate(),
341 isolate()->heap()->AllocateByteArray(length, pretenure),
342 ByteArray);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000343}
344
345
ager@chromium.org3811b432009-10-28 14:53:37 +0000346Handle<ExternalArray> Factory::NewExternalArray(int length,
347 ExternalArrayType array_type,
348 void* external_pointer,
349 PretenureFlag pretenure) {
350 ASSERT(0 <= length);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000351 CALL_HEAP_FUNCTION(
352 isolate(),
353 isolate()->heap()->AllocateExternalArray(length,
354 array_type,
355 external_pointer,
356 pretenure),
357 ExternalArray);
ager@chromium.org3811b432009-10-28 14:53:37 +0000358}
359
360
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000361Handle<JSGlobalPropertyCell> Factory::NewJSGlobalPropertyCell(
362 Handle<Object> value) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000363 CALL_HEAP_FUNCTION(
364 isolate(),
365 isolate()->heap()->AllocateJSGlobalPropertyCell(*value),
366 JSGlobalPropertyCell);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000367}
368
369
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000370Handle<Map> Factory::NewMap(InstanceType type, int instance_size) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000371 CALL_HEAP_FUNCTION(
372 isolate(),
373 isolate()->heap()->AllocateMap(type, instance_size),
374 Map);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000375}
376
377
378Handle<JSObject> Factory::NewFunctionPrototype(Handle<JSFunction> function) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000379 CALL_HEAP_FUNCTION(
380 isolate(),
381 isolate()->heap()->AllocateFunctionPrototype(*function),
382 JSObject);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000383}
384
385
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000386Handle<Map> Factory::CopyMapDropDescriptors(Handle<Map> src) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000387 CALL_HEAP_FUNCTION(isolate(), src->CopyDropDescriptors(), Map);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000388}
389
390
ager@chromium.org32912102009-01-16 10:38:43 +0000391Handle<Map> Factory::CopyMap(Handle<Map> src,
392 int extra_inobject_properties) {
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000393 Handle<Map> copy = CopyMapDropDescriptors(src);
ager@chromium.org32912102009-01-16 10:38:43 +0000394 // Check that we do not overflow the instance size when adding the
395 // extra inobject properties.
396 int instance_size_delta = extra_inobject_properties * kPointerSize;
397 int max_instance_size_delta =
398 JSObject::kMaxInstanceSize - copy->instance_size();
399 if (instance_size_delta > max_instance_size_delta) {
400 // If the instance size overflows, we allocate as many properties
401 // as we can as inobject properties.
402 instance_size_delta = max_instance_size_delta;
403 extra_inobject_properties = max_instance_size_delta >> kPointerSizeLog2;
404 }
405 // Adjust the map with the extra inobject properties.
406 int inobject_properties =
407 copy->inobject_properties() + extra_inobject_properties;
408 copy->set_inobject_properties(inobject_properties);
409 copy->set_unused_property_fields(inobject_properties);
410 copy->set_instance_size(copy->instance_size() + instance_size_delta);
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000411 copy->set_visitor_id(StaticVisitorBase::GetVisitorId(*copy));
ager@chromium.org32912102009-01-16 10:38:43 +0000412 return copy;
413}
414
fschneider@chromium.org40b9da32010-06-28 11:29:21 +0000415
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000416Handle<Map> Factory::CopyMapDropTransitions(Handle<Map> src) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000417 CALL_HEAP_FUNCTION(isolate(), src->CopyDropTransitions(), Map);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000418}
419
420
fschneider@chromium.org40b9da32010-06-28 11:29:21 +0000421Handle<Map> Factory::GetFastElementsMap(Handle<Map> src) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000422 CALL_HEAP_FUNCTION(isolate(), src->GetFastElementsMap(), Map);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +0000423}
424
425
426Handle<Map> Factory::GetSlowElementsMap(Handle<Map> src) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000427 CALL_HEAP_FUNCTION(isolate(), src->GetSlowElementsMap(), Map);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +0000428}
429
430
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000431Handle<Map> Factory::GetExternalArrayElementsMap(
432 Handle<Map> src,
433 ExternalArrayType array_type,
434 bool safe_to_add_transition) {
435 CALL_HEAP_FUNCTION(isolate(),
436 src->GetExternalArrayElementsMap(array_type,
437 safe_to_add_transition),
438 Map);
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +0000439}
440
441
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000442Handle<FixedArray> Factory::CopyFixedArray(Handle<FixedArray> array) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000443 CALL_HEAP_FUNCTION(isolate(), array->Copy(), FixedArray);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000444}
445
446
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000447Handle<JSFunction> Factory::BaseNewFunctionFromSharedFunctionInfo(
448 Handle<SharedFunctionInfo> function_info,
sgjesse@chromium.org846fb742009-12-18 08:56:33 +0000449 Handle<Map> function_map,
450 PretenureFlag pretenure) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000451 CALL_HEAP_FUNCTION(
452 isolate(),
453 isolate()->heap()->AllocateFunction(*function_map,
454 *function_info,
455 isolate()->heap()->the_hole_value(),
456 pretenure),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000457 JSFunction);
458}
459
460
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000461Handle<JSFunction> Factory::NewFunctionFromSharedFunctionInfo(
462 Handle<SharedFunctionInfo> function_info,
sgjesse@chromium.org846fb742009-12-18 08:56:33 +0000463 Handle<Context> context,
464 PretenureFlag pretenure) {
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000465 Handle<JSFunction> result = BaseNewFunctionFromSharedFunctionInfo(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000466 function_info,
467 function_info->strict_mode()
468 ? isolate()->strict_mode_function_map()
469 : isolate()->function_map(),
470 pretenure);
471
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000472 result->set_context(*context);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000473 int number_of_literals = function_info->num_literals();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000474 Handle<FixedArray> literals = NewFixedArray(number_of_literals, pretenure);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000475 if (number_of_literals > 0) {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000476 // Store the object, regexp and array functions in the literals
477 // array prefix. These functions will be used when creating
478 // object, regexp and array literals in this function.
ager@chromium.org236ad962008-09-25 09:45:57 +0000479 literals->set(JSFunction::kLiteralGlobalContextIndex,
480 context->global_context());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000481 }
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000482 result->set_literals(*literals);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000483 result->set_next_function_link(isolate()->heap()->undefined_value());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000484
485 if (V8::UseCrankshaft() &&
486 FLAG_always_opt &&
487 result->is_compiled() &&
488 !function_info->is_toplevel() &&
489 function_info->allows_lazy_compilation()) {
490 result->MarkForLazyRecompilation();
491 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000492 return result;
493}
494
495
496Handle<Object> Factory::NewNumber(double value,
497 PretenureFlag pretenure) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000498 CALL_HEAP_FUNCTION(
499 isolate(),
500 isolate()->heap()->NumberFromDouble(value, pretenure), Object);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000501}
502
503
504Handle<Object> Factory::NewNumberFromInt(int value) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000505 CALL_HEAP_FUNCTION(
506 isolate(),
507 isolate()->heap()->NumberFromInt32(value), Object);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000508}
509
510
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000511Handle<Object> Factory::NewNumberFromUint(uint32_t value) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000512 CALL_HEAP_FUNCTION(
513 isolate(),
514 isolate()->heap()->NumberFromUint32(value), Object);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000515}
516
517
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000518Handle<JSObject> Factory::NewNeanderObject() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000519 CALL_HEAP_FUNCTION(
520 isolate(),
521 isolate()->heap()->AllocateJSObjectFromMap(
522 isolate()->heap()->neander_map()),
523 JSObject);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000524}
525
526
527Handle<Object> Factory::NewTypeError(const char* type,
528 Vector< Handle<Object> > args) {
529 return NewError("MakeTypeError", type, args);
530}
531
532
533Handle<Object> Factory::NewTypeError(Handle<String> message) {
534 return NewError("$TypeError", message);
535}
536
537
538Handle<Object> Factory::NewRangeError(const char* type,
539 Vector< Handle<Object> > args) {
540 return NewError("MakeRangeError", type, args);
541}
542
543
544Handle<Object> Factory::NewRangeError(Handle<String> message) {
545 return NewError("$RangeError", message);
546}
547
548
549Handle<Object> Factory::NewSyntaxError(const char* type, Handle<JSArray> args) {
550 return NewError("MakeSyntaxError", type, args);
551}
552
553
554Handle<Object> Factory::NewSyntaxError(Handle<String> message) {
555 return NewError("$SyntaxError", message);
556}
557
558
559Handle<Object> Factory::NewReferenceError(const char* type,
560 Vector< Handle<Object> > args) {
561 return NewError("MakeReferenceError", type, args);
562}
563
564
565Handle<Object> Factory::NewReferenceError(Handle<String> message) {
566 return NewError("$ReferenceError", message);
567}
568
569
570Handle<Object> Factory::NewError(const char* maker, const char* type,
571 Vector< Handle<Object> > args) {
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000572 v8::HandleScope scope; // Instantiate a closeable HandleScope for EscapeFrom.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000573 Handle<FixedArray> array = NewFixedArray(args.length());
ager@chromium.org7c537e22008-10-16 08:43:32 +0000574 for (int i = 0; i < args.length(); i++) {
575 array->set(i, *args[i]);
576 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000577 Handle<JSArray> object = NewJSArrayWithElements(array);
ager@chromium.org7c537e22008-10-16 08:43:32 +0000578 Handle<Object> result = NewError(maker, type, object);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000579 return result.EscapeFrom(&scope);
580}
581
582
583Handle<Object> Factory::NewEvalError(const char* type,
584 Vector< Handle<Object> > args) {
585 return NewError("MakeEvalError", type, args);
586}
587
588
589Handle<Object> Factory::NewError(const char* type,
590 Vector< Handle<Object> > args) {
591 return NewError("MakeError", type, args);
592}
593
594
595Handle<Object> Factory::NewError(const char* maker,
596 const char* type,
597 Handle<JSArray> args) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000598 Handle<String> make_str = LookupAsciiSymbol(maker);
599 Handle<Object> fun_obj(
600 isolate()->js_builtins_object()->GetPropertyNoExceptionThrown(*make_str));
ager@chromium.org4af710e2009-09-15 12:20:11 +0000601 // If the builtins haven't been properly configured yet this error
602 // constructor may not have been defined. Bail out.
603 if (!fun_obj->IsJSFunction())
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000604 return undefined_value();
ager@chromium.org4af710e2009-09-15 12:20:11 +0000605 Handle<JSFunction> fun = Handle<JSFunction>::cast(fun_obj);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000606 Handle<Object> type_obj = LookupAsciiSymbol(type);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000607 Object** argv[2] = { type_obj.location(),
608 Handle<Object>::cast(args).location() };
609
610 // Invoke the JavaScript factory method. If an exception is thrown while
611 // running the factory method, use the exception as the result.
612 bool caught_exception;
613 Handle<Object> result = Execution::TryCall(fun,
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000614 isolate()->js_builtins_object(), 2, argv, &caught_exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000615 return result;
616}
617
618
619Handle<Object> Factory::NewError(Handle<String> message) {
620 return NewError("$Error", message);
621}
622
623
624Handle<Object> Factory::NewError(const char* constructor,
625 Handle<String> message) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000626 Handle<String> constr = LookupAsciiSymbol(constructor);
627 Handle<JSFunction> fun = Handle<JSFunction>(
628 JSFunction::cast(isolate()->js_builtins_object()->
629 GetPropertyNoExceptionThrown(*constr)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000630 Object** argv[1] = { Handle<Object>::cast(message).location() };
631
632 // Invoke the JavaScript factory method. If an exception is thrown while
633 // running the factory method, use the exception as the result.
634 bool caught_exception;
635 Handle<Object> result = Execution::TryCall(fun,
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000636 isolate()->js_builtins_object(), 1, argv, &caught_exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000637 return result;
638}
639
640
641Handle<JSFunction> Factory::NewFunction(Handle<String> name,
642 InstanceType type,
643 int instance_size,
644 Handle<Code> code,
645 bool force_initial_map) {
646 // Allocate the function
647 Handle<JSFunction> function = NewFunction(name, the_hole_value());
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000648
649 // Setup the code pointer in both the shared function info and in
650 // the function itself.
651 function->shared()->set_code(*code);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000652 function->set_code(*code);
653
654 if (force_initial_map ||
655 type != JS_OBJECT_TYPE ||
656 instance_size != JSObject::kHeaderSize) {
657 Handle<Map> initial_map = NewMap(type, instance_size);
658 Handle<JSObject> prototype = NewFunctionPrototype(function);
659 initial_map->set_prototype(*prototype);
660 function->set_initial_map(*initial_map);
661 initial_map->set_constructor(*function);
662 } else {
663 ASSERT(!function->has_initial_map());
664 ASSERT(!function->has_prototype());
665 }
666
667 return function;
668}
669
670
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000671Handle<JSFunction> Factory::NewFunctionWithPrototype(Handle<String> name,
672 InstanceType type,
673 int instance_size,
674 Handle<JSObject> prototype,
675 Handle<Code> code,
676 bool force_initial_map) {
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000677 // Allocate the function.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000678 Handle<JSFunction> function = NewFunction(name, prototype);
679
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000680 // Setup the code pointer in both the shared function info and in
681 // the function itself.
682 function->shared()->set_code(*code);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000683 function->set_code(*code);
684
685 if (force_initial_map ||
686 type != JS_OBJECT_TYPE ||
687 instance_size != JSObject::kHeaderSize) {
688 Handle<Map> initial_map = NewMap(type, instance_size);
689 function->set_initial_map(*initial_map);
690 initial_map->set_constructor(*function);
691 }
692
693 // Set function.prototype and give the prototype a constructor
694 // property that refers to the function.
695 SetPrototypeProperty(function, prototype);
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +0000696 // Currently safe because it is only invoked from Genesis.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000697 SetLocalPropertyNoThrow(prototype, constructor_symbol(), function, DONT_ENUM);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000698 return function;
699}
700
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000701
kmillikin@chromium.org4111b802010-05-03 10:34:42 +0000702Handle<JSFunction> Factory::NewFunctionWithoutPrototype(Handle<String> name,
703 Handle<Code> code) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000704 Handle<JSFunction> function = NewFunctionWithoutPrototype(name,
705 kNonStrictMode);
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000706 function->shared()->set_code(*code);
kmillikin@chromium.org4111b802010-05-03 10:34:42 +0000707 function->set_code(*code);
708 ASSERT(!function->has_initial_map());
709 ASSERT(!function->has_prototype());
710 return function;
711}
712
713
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000714Handle<Code> Factory::NewCode(const CodeDesc& desc,
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000715 Code::Flags flags,
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +0000716 Handle<Object> self_ref,
717 bool immovable) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000718 CALL_HEAP_FUNCTION(isolate(),
719 isolate()->heap()->CreateCode(
720 desc, flags, self_ref, immovable),
721 Code);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000722}
723
724
725Handle<Code> Factory::CopyCode(Handle<Code> code) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000726 CALL_HEAP_FUNCTION(isolate(),
727 isolate()->heap()->CopyCode(*code),
728 Code);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000729}
730
731
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000732Handle<Code> Factory::CopyCode(Handle<Code> code, Vector<byte> reloc_info) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000733 CALL_HEAP_FUNCTION(isolate(),
734 isolate()->heap()->CopyCode(*code, reloc_info),
735 Code);
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000736}
737
738
lrn@chromium.org303ada72010-10-27 09:33:13 +0000739MUST_USE_RESULT static inline MaybeObject* DoCopyInsert(
740 DescriptorArray* array,
741 String* key,
742 Object* value,
743 PropertyAttributes attributes) {
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000744 CallbacksDescriptor desc(key, value, attributes);
lrn@chromium.org303ada72010-10-27 09:33:13 +0000745 MaybeObject* obj = array->CopyInsert(&desc, REMOVE_TRANSITIONS);
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000746 return obj;
747}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000748
749
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000750// Allocate the new array.
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000751Handle<DescriptorArray> Factory::CopyAppendForeignDescriptor(
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000752 Handle<DescriptorArray> array,
753 Handle<String> key,
754 Handle<Object> value,
755 PropertyAttributes attributes) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000756 CALL_HEAP_FUNCTION(isolate(),
757 DoCopyInsert(*array, *key, *value, attributes),
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000758 DescriptorArray);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000759}
760
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000761
762Handle<String> Factory::SymbolFromString(Handle<String> value) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000763 CALL_HEAP_FUNCTION(isolate(),
764 isolate()->heap()->LookupSymbol(*value), String);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000765}
766
767
768Handle<DescriptorArray> Factory::CopyAppendCallbackDescriptors(
769 Handle<DescriptorArray> array,
770 Handle<Object> descriptors) {
771 v8::NeanderArray callbacks(descriptors);
772 int nof_callbacks = callbacks.length();
773 Handle<DescriptorArray> result =
774 NewDescriptorArray(array->number_of_descriptors() + nof_callbacks);
775
776 // Number of descriptors added to the result so far.
777 int descriptor_count = 0;
778
779 // Copy the descriptors from the array.
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000780 for (int i = 0; i < array->number_of_descriptors(); i++) {
781 if (array->GetType(i) != NULL_DESCRIPTOR) {
782 result->CopyFrom(descriptor_count++, *array, i);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000783 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000784 }
785
786 // Number of duplicates detected.
787 int duplicates = 0;
788
789 // Fill in new callback descriptors. Process the callbacks from
790 // back to front so that the last callback with a given name takes
791 // precedence over previously added callbacks with that name.
792 for (int i = nof_callbacks - 1; i >= 0; i--) {
793 Handle<AccessorInfo> entry =
794 Handle<AccessorInfo>(AccessorInfo::cast(callbacks.get(i)));
795 // Ensure the key is a symbol before writing into the instance descriptor.
796 Handle<String> key =
797 SymbolFromString(Handle<String>(String::cast(entry->name())));
798 // Check if a descriptor with this name already exists before writing.
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000799 if (result->LinearSearch(*key, descriptor_count) ==
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000800 DescriptorArray::kNotFound) {
801 CallbacksDescriptor desc(*key, *entry, entry->property_attributes());
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000802 result->Set(descriptor_count, &desc);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000803 descriptor_count++;
804 } else {
805 duplicates++;
806 }
807 }
808
809 // If duplicates were detected, allocate a result of the right size
810 // and transfer the elements.
811 if (duplicates > 0) {
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000812 int number_of_descriptors = result->number_of_descriptors() - duplicates;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000813 Handle<DescriptorArray> new_result =
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000814 NewDescriptorArray(number_of_descriptors);
815 for (int i = 0; i < number_of_descriptors; i++) {
816 new_result->CopyFrom(i, *result, i);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000817 }
818 result = new_result;
819 }
820
821 // Sort the result before returning.
822 result->Sort();
823 return result;
824}
825
826
827Handle<JSObject> Factory::NewJSObject(Handle<JSFunction> constructor,
828 PretenureFlag pretenure) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000829 CALL_HEAP_FUNCTION(
830 isolate(),
831 isolate()->heap()->AllocateJSObject(*constructor, pretenure), JSObject);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000832}
833
834
kasperl@chromium.org2abc4502009-07-02 07:00:29 +0000835Handle<GlobalObject> Factory::NewGlobalObject(
836 Handle<JSFunction> constructor) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000837 CALL_HEAP_FUNCTION(isolate(),
838 isolate()->heap()->AllocateGlobalObject(*constructor),
kasperl@chromium.org2abc4502009-07-02 07:00:29 +0000839 GlobalObject);
840}
841
842
843
ager@chromium.org236ad962008-09-25 09:45:57 +0000844Handle<JSObject> Factory::NewJSObjectFromMap(Handle<Map> map) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000845 CALL_HEAP_FUNCTION(
846 isolate(),
847 isolate()->heap()->AllocateJSObjectFromMap(*map, NOT_TENURED),
848 JSObject);
ager@chromium.org236ad962008-09-25 09:45:57 +0000849}
850
851
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +0000852Handle<JSArray> Factory::NewJSArray(int capacity,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000853 PretenureFlag pretenure) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000854 Handle<JSObject> obj = NewJSObject(isolate()->array_function(), pretenure);
855 CALL_HEAP_FUNCTION(isolate(),
856 Handle<JSArray>::cast(obj)->Initialize(capacity),
857 JSArray);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000858}
859
860
861Handle<JSArray> Factory::NewJSArrayWithElements(Handle<FixedArray> elements,
862 PretenureFlag pretenure) {
863 Handle<JSArray> result =
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000864 Handle<JSArray>::cast(NewJSObject(isolate()->array_function(),
865 pretenure));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000866 result->SetContent(*elements);
867 return result;
868}
869
870
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000871Handle<JSProxy> Factory::NewJSProxy(Handle<Object> handler,
872 Handle<Object> prototype) {
873 CALL_HEAP_FUNCTION(
874 isolate(),
875 isolate()->heap()->AllocateJSProxy(*handler, *prototype),
876 JSProxy);
877}
878
879
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000880Handle<SharedFunctionInfo> Factory::NewSharedFunctionInfo(
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +0000881 Handle<String> name,
882 int number_of_literals,
883 Handle<Code> code,
ager@chromium.orgb5737492010-07-15 09:29:43 +0000884 Handle<SerializedScopeInfo> scope_info) {
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000885 Handle<SharedFunctionInfo> shared = NewSharedFunctionInfo(name);
886 shared->set_code(*code);
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +0000887 shared->set_scope_info(*scope_info);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000888 int literals_array_size = number_of_literals;
889 // If the function contains object, regexp or array literals,
890 // allocate extra space for a literals array prefix containing the
891 // context.
892 if (number_of_literals > 0) {
893 literals_array_size += JSFunction::kLiteralsPrefixSize;
894 }
895 shared->set_num_literals(literals_array_size);
896 return shared;
897}
898
899
kmillikin@chromium.org31b12772011-02-02 16:08:26 +0000900Handle<JSMessageObject> Factory::NewJSMessageObject(
901 Handle<String> type,
902 Handle<JSArray> arguments,
903 int start_position,
904 int end_position,
905 Handle<Object> script,
906 Handle<Object> stack_trace,
907 Handle<Object> stack_frames) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000908 CALL_HEAP_FUNCTION(isolate(),
909 isolate()->heap()->AllocateJSMessageObject(*type,
910 *arguments,
911 start_position,
912 end_position,
913 *script,
914 *stack_trace,
915 *stack_frames),
kmillikin@chromium.org31b12772011-02-02 16:08:26 +0000916 JSMessageObject);
917}
918
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000919Handle<SharedFunctionInfo> Factory::NewSharedFunctionInfo(Handle<String> name) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000920 CALL_HEAP_FUNCTION(isolate(),
921 isolate()->heap()->AllocateSharedFunctionInfo(*name),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000922 SharedFunctionInfo);
923}
924
925
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000926Handle<String> Factory::NumberToString(Handle<Object> number) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000927 CALL_HEAP_FUNCTION(isolate(),
928 isolate()->heap()->NumberToString(*number), String);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000929}
930
931
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000932Handle<NumberDictionary> Factory::DictionaryAtNumberPut(
933 Handle<NumberDictionary> dictionary,
934 uint32_t key,
935 Handle<Object> value) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000936 CALL_HEAP_FUNCTION(isolate(),
937 dictionary->AtNumberPut(key, *value),
938 NumberDictionary);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000939}
940
941
942Handle<JSFunction> Factory::NewFunctionHelper(Handle<String> name,
943 Handle<Object> prototype) {
944 Handle<SharedFunctionInfo> function_share = NewSharedFunctionInfo(name);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000945 CALL_HEAP_FUNCTION(
946 isolate(),
947 isolate()->heap()->AllocateFunction(*isolate()->function_map(),
948 *function_share,
949 *prototype),
950 JSFunction);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000951}
952
953
954Handle<JSFunction> Factory::NewFunction(Handle<String> name,
955 Handle<Object> prototype) {
956 Handle<JSFunction> fun = NewFunctionHelper(name, prototype);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000957 fun->set_context(isolate()->context()->global_context());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000958 return fun;
959}
960
961
kmillikin@chromium.org4111b802010-05-03 10:34:42 +0000962Handle<JSFunction> Factory::NewFunctionWithoutPrototypeHelper(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000963 Handle<String> name,
964 StrictModeFlag strict_mode) {
kmillikin@chromium.org4111b802010-05-03 10:34:42 +0000965 Handle<SharedFunctionInfo> function_share = NewSharedFunctionInfo(name);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000966 Handle<Map> map = strict_mode == kStrictMode
967 ? isolate()->strict_mode_function_without_prototype_map()
968 : isolate()->function_without_prototype_map();
969 CALL_HEAP_FUNCTION(isolate(),
970 isolate()->heap()->AllocateFunction(
971 *map,
kmillikin@chromium.org4111b802010-05-03 10:34:42 +0000972 *function_share,
973 *the_hole_value()),
974 JSFunction);
975}
976
977
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000978Handle<JSFunction> Factory::NewFunctionWithoutPrototype(
979 Handle<String> name,
980 StrictModeFlag strict_mode) {
981 Handle<JSFunction> fun = NewFunctionWithoutPrototypeHelper(name, strict_mode);
982 fun->set_context(isolate()->context()->global_context());
kmillikin@chromium.org4111b802010-05-03 10:34:42 +0000983 return fun;
984}
985
986
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000987Handle<Object> Factory::ToObject(Handle<Object> object) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000988 CALL_HEAP_FUNCTION(isolate(), object->ToObject(), Object);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000989}
990
991
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000992Handle<Object> Factory::ToObject(Handle<Object> object,
993 Handle<Context> global_context) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000994 CALL_HEAP_FUNCTION(isolate(), object->ToObject(*global_context), Object);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000995}
996
997
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000998#ifdef ENABLE_DEBUGGER_SUPPORT
v8.team.kasperl727e9952008-09-02 14:56:44 +0000999Handle<DebugInfo> Factory::NewDebugInfo(Handle<SharedFunctionInfo> shared) {
1000 // Get the original code of the function.
1001 Handle<Code> code(shared->code());
1002
1003 // Create a copy of the code before allocating the debug info object to avoid
1004 // allocation while setting up the debug info object.
1005 Handle<Code> original_code(*Factory::CopyCode(code));
1006
1007 // Allocate initial fixed array for active break points before allocating the
1008 // debug info object to avoid allocation while setting up the debug info
1009 // object.
1010 Handle<FixedArray> break_points(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001011 NewFixedArray(Debug::kEstimatedNofBreakPointsInFunction));
v8.team.kasperl727e9952008-09-02 14:56:44 +00001012
1013 // Create and set up the debug info object. Debug info contains function, a
1014 // copy of the original code, the executing code and initial fixed array for
1015 // active break points.
1016 Handle<DebugInfo> debug_info =
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001017 Handle<DebugInfo>::cast(NewStruct(DEBUG_INFO_TYPE));
v8.team.kasperl727e9952008-09-02 14:56:44 +00001018 debug_info->set_shared(*shared);
1019 debug_info->set_original_code(*original_code);
1020 debug_info->set_code(*code);
1021 debug_info->set_break_points(*break_points);
1022
1023 // Link debug info to function.
1024 shared->set_debug_info(*debug_info);
1025
1026 return debug_info;
1027}
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001028#endif
v8.team.kasperl727e9952008-09-02 14:56:44 +00001029
1030
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001031Handle<JSObject> Factory::NewArgumentsObject(Handle<Object> callee,
1032 int length) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001033 CALL_HEAP_FUNCTION(
1034 isolate(),
1035 isolate()->heap()->AllocateArgumentsObject(*callee, length), JSObject);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001036}
1037
1038
1039Handle<JSFunction> Factory::CreateApiFunction(
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001040 Handle<FunctionTemplateInfo> obj, ApiInstanceType instance_type) {
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00001041 Handle<Code> code = isolate()->builtins()->HandleApiCall();
1042 Handle<Code> construct_stub = isolate()->builtins()->JSConstructStubApi();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001043
kasper.lund212ac232008-07-16 07:07:30 +00001044 int internal_field_count = 0;
1045 if (!obj->instance_template()->IsUndefined()) {
1046 Handle<ObjectTemplateInfo> instance_template =
1047 Handle<ObjectTemplateInfo>(
1048 ObjectTemplateInfo::cast(obj->instance_template()));
1049 internal_field_count =
1050 Smi::cast(instance_template->internal_field_count())->value();
1051 }
1052
1053 int instance_size = kPointerSize * internal_field_count;
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001054 InstanceType type = INVALID_TYPE;
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001055 switch (instance_type) {
1056 case JavaScriptObject:
1057 type = JS_OBJECT_TYPE;
1058 instance_size += JSObject::kHeaderSize;
1059 break;
1060 case InnerGlobalObject:
1061 type = JS_GLOBAL_OBJECT_TYPE;
1062 instance_size += JSGlobalObject::kSize;
1063 break;
1064 case OuterGlobalObject:
1065 type = JS_GLOBAL_PROXY_TYPE;
1066 instance_size += JSGlobalProxy::kSize;
1067 break;
1068 default:
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001069 break;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001070 }
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001071 ASSERT(type != INVALID_TYPE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001072
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001073 Handle<JSFunction> result =
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001074 NewFunction(Factory::empty_symbol(),
1075 type,
1076 instance_size,
1077 code,
1078 true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001079 // Set class name.
1080 Handle<Object> class_name = Handle<Object>(obj->class_name());
1081 if (class_name->IsString()) {
1082 result->shared()->set_instance_class_name(*class_name);
1083 result->shared()->set_name(*class_name);
1084 }
1085
1086 Handle<Map> map = Handle<Map>(result->initial_map());
1087
1088 // Mark as undetectable if needed.
1089 if (obj->undetectable()) {
1090 map->set_is_undetectable();
1091 }
1092
1093 // Mark as hidden for the __proto__ accessor if needed.
1094 if (obj->hidden_prototype()) {
1095 map->set_is_hidden_prototype();
1096 }
1097
1098 // Mark as needs_access_check if needed.
1099 if (obj->needs_access_check()) {
ager@chromium.org870a0b62008-11-04 11:43:05 +00001100 map->set_is_access_check_needed(true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001101 }
1102
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001103 // Set interceptor information in the map.
1104 if (!obj->named_property_handler()->IsUndefined()) {
1105 map->set_has_named_interceptor();
1106 }
1107 if (!obj->indexed_property_handler()->IsUndefined()) {
1108 map->set_has_indexed_interceptor();
1109 }
1110
1111 // Set instance call-as-function information in the map.
1112 if (!obj->instance_call_handler()->IsUndefined()) {
1113 map->set_has_instance_call_handler();
1114 }
1115
1116 result->shared()->set_function_data(*obj);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001117 result->shared()->set_construct_stub(*construct_stub);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001118 result->shared()->DontAdaptArguments();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001119
1120 // Recursively copy parent templates' accessors, 'data' may be modified.
1121 Handle<DescriptorArray> array =
1122 Handle<DescriptorArray>(map->instance_descriptors());
1123 while (true) {
1124 Handle<Object> props = Handle<Object>(obj->property_accessors());
1125 if (!props->IsUndefined()) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001126 array = CopyAppendCallbackDescriptors(array, props);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001127 }
1128 Handle<Object> parent = Handle<Object>(obj->parent_template());
1129 if (parent->IsUndefined()) break;
1130 obj = Handle<FunctionTemplateInfo>::cast(parent);
1131 }
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001132 if (!array->IsEmpty()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001133 map->set_instance_descriptors(*array);
1134 }
1135
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001136 ASSERT(result->shared()->IsApiFunction());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001137 return result;
1138}
1139
1140
ager@chromium.org236ad962008-09-25 09:45:57 +00001141Handle<MapCache> Factory::NewMapCache(int at_least_space_for) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001142 CALL_HEAP_FUNCTION(isolate(),
1143 MapCache::Allocate(at_least_space_for), MapCache);
ager@chromium.org236ad962008-09-25 09:45:57 +00001144}
1145
1146
lrn@chromium.org303ada72010-10-27 09:33:13 +00001147MUST_USE_RESULT static MaybeObject* UpdateMapCacheWith(Context* context,
1148 FixedArray* keys,
1149 Map* map) {
1150 Object* result;
1151 { MaybeObject* maybe_result =
1152 MapCache::cast(context->map_cache())->Put(keys, map);
1153 if (!maybe_result->ToObject(&result)) return maybe_result;
1154 }
1155 context->set_map_cache(MapCache::cast(result));
ager@chromium.org236ad962008-09-25 09:45:57 +00001156 return result;
1157}
1158
1159
1160Handle<MapCache> Factory::AddToMapCache(Handle<Context> context,
1161 Handle<FixedArray> keys,
1162 Handle<Map> map) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001163 CALL_HEAP_FUNCTION(isolate(),
1164 UpdateMapCacheWith(*context, *keys, *map), MapCache);
ager@chromium.org236ad962008-09-25 09:45:57 +00001165}
1166
1167
1168Handle<Map> Factory::ObjectLiteralMapFromCache(Handle<Context> context,
1169 Handle<FixedArray> keys) {
1170 if (context->map_cache()->IsUndefined()) {
1171 // Allocate the new map cache for the global context.
1172 Handle<MapCache> new_cache = NewMapCache(24);
1173 context->set_map_cache(*new_cache);
1174 }
ager@chromium.org32912102009-01-16 10:38:43 +00001175 // Check to see whether there is a matching element in the cache.
ager@chromium.org236ad962008-09-25 09:45:57 +00001176 Handle<MapCache> cache =
1177 Handle<MapCache>(MapCache::cast(context->map_cache()));
1178 Handle<Object> result = Handle<Object>(cache->Lookup(*keys));
1179 if (result->IsMap()) return Handle<Map>::cast(result);
1180 // Create a new map and add it to the cache.
1181 Handle<Map> map =
ager@chromium.org32912102009-01-16 10:38:43 +00001182 CopyMap(Handle<Map>(context->object_function()->initial_map()),
1183 keys->length());
ager@chromium.org236ad962008-09-25 09:45:57 +00001184 AddToMapCache(context, keys, map);
1185 return Handle<Map>(map);
1186}
1187
1188
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001189void Factory::SetRegExpAtomData(Handle<JSRegExp> regexp,
1190 JSRegExp::Type type,
1191 Handle<String> source,
1192 JSRegExp::Flags flags,
1193 Handle<Object> data) {
1194 Handle<FixedArray> store = NewFixedArray(JSRegExp::kAtomDataSize);
1195
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001196 store->set(JSRegExp::kTagIndex, Smi::FromInt(type));
1197 store->set(JSRegExp::kSourceIndex, *source);
1198 store->set(JSRegExp::kFlagsIndex, Smi::FromInt(flags.value()));
1199 store->set(JSRegExp::kAtomPatternIndex, *data);
1200 regexp->set_data(*store);
1201}
1202
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001203void Factory::SetRegExpIrregexpData(Handle<JSRegExp> regexp,
1204 JSRegExp::Type type,
1205 Handle<String> source,
1206 JSRegExp::Flags flags,
1207 int capture_count) {
1208 Handle<FixedArray> store = NewFixedArray(JSRegExp::kIrregexpDataSize);
1209
1210 store->set(JSRegExp::kTagIndex, Smi::FromInt(type));
1211 store->set(JSRegExp::kSourceIndex, *source);
1212 store->set(JSRegExp::kFlagsIndex, Smi::FromInt(flags.value()));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001213 store->set(JSRegExp::kIrregexpASCIICodeIndex, HEAP->the_hole_value());
1214 store->set(JSRegExp::kIrregexpUC16CodeIndex, HEAP->the_hole_value());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001215 store->set(JSRegExp::kIrregexpMaxRegisterCountIndex, Smi::FromInt(0));
1216 store->set(JSRegExp::kIrregexpCaptureCountIndex,
1217 Smi::FromInt(capture_count));
1218 regexp->set_data(*store);
1219}
1220
1221
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001222
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001223void Factory::ConfigureInstance(Handle<FunctionTemplateInfo> desc,
1224 Handle<JSObject> instance,
1225 bool* pending_exception) {
1226 // Configure the instance by adding the properties specified by the
1227 // instance template.
1228 Handle<Object> instance_template = Handle<Object>(desc->instance_template());
1229 if (!instance_template->IsUndefined()) {
1230 Execution::ConfigureInstance(instance,
1231 instance_template,
1232 pending_exception);
1233 } else {
1234 *pending_exception = false;
1235 }
1236}
1237
1238
1239} } // namespace v8::internal