blob: 849a2e0c057526df8a93c76b7bd4117722b476db [file] [log] [blame]
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001// Copyright 2010 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
kasperl@chromium.org86f77b72009-07-06 08:21:57 +000061Handle<StringDictionary> Factory::NewStringDictionary(int at_least_space_for) {
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +000062 ASSERT(0 <= at_least_space_for);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000063 CALL_HEAP_FUNCTION(isolate(),
64 StringDictionary::Allocate(at_least_space_for),
kasperl@chromium.org86f77b72009-07-06 08:21:57 +000065 StringDictionary);
66}
67
68
69Handle<NumberDictionary> Factory::NewNumberDictionary(int at_least_space_for) {
70 ASSERT(0 <= at_least_space_for);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000071 CALL_HEAP_FUNCTION(isolate(),
72 NumberDictionary::Allocate(at_least_space_for),
kasperl@chromium.org86f77b72009-07-06 08:21:57 +000073 NumberDictionary);
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +000074}
75
76
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000077Handle<DescriptorArray> Factory::NewDescriptorArray(int number_of_descriptors) {
78 ASSERT(0 <= number_of_descriptors);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000079 CALL_HEAP_FUNCTION(isolate(),
80 DescriptorArray::Allocate(number_of_descriptors),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000081 DescriptorArray);
82}
83
84
kasperl@chromium.orga5551262010-12-07 12:49:48 +000085Handle<DeoptimizationInputData> Factory::NewDeoptimizationInputData(
86 int deopt_entry_count,
87 PretenureFlag pretenure) {
88 ASSERT(deopt_entry_count > 0);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000089 CALL_HEAP_FUNCTION(isolate(),
90 DeoptimizationInputData::Allocate(deopt_entry_count,
kasperl@chromium.orga5551262010-12-07 12:49:48 +000091 pretenure),
92 DeoptimizationInputData);
93}
94
95
96Handle<DeoptimizationOutputData> Factory::NewDeoptimizationOutputData(
97 int deopt_entry_count,
98 PretenureFlag pretenure) {
99 ASSERT(deopt_entry_count > 0);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000100 CALL_HEAP_FUNCTION(isolate(),
101 DeoptimizationOutputData::Allocate(deopt_entry_count,
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000102 pretenure),
103 DeoptimizationOutputData);
104}
105
106
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000107// Symbols are created in the old generation (data space).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000108Handle<String> Factory::LookupSymbol(Vector<const char> string) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000109 CALL_HEAP_FUNCTION(isolate(),
110 isolate()->heap()->LookupSymbol(string),
111 String);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000112}
113
fschneider@chromium.org9e3e0b62011-01-03 10:16:46 +0000114Handle<String> Factory::LookupAsciiSymbol(Vector<const char> string) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000115 CALL_HEAP_FUNCTION(isolate(),
116 isolate()->heap()->LookupAsciiSymbol(string),
117 String);
fschneider@chromium.org9e3e0b62011-01-03 10:16:46 +0000118}
119
120Handle<String> Factory::LookupTwoByteSymbol(Vector<const uc16> string) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000121 CALL_HEAP_FUNCTION(isolate(),
122 isolate()->heap()->LookupTwoByteSymbol(string),
123 String);
fschneider@chromium.org9e3e0b62011-01-03 10:16:46 +0000124}
125
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000126
127Handle<String> Factory::NewStringFromAscii(Vector<const char> string,
128 PretenureFlag pretenure) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000129 CALL_HEAP_FUNCTION(
130 isolate(),
131 isolate()->heap()->AllocateStringFromAscii(string, pretenure),
132 String);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000133}
134
135Handle<String> Factory::NewStringFromUtf8(Vector<const char> string,
136 PretenureFlag pretenure) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000137 CALL_HEAP_FUNCTION(
138 isolate(),
139 isolate()->heap()->AllocateStringFromUtf8(string, pretenure),
140 String);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000141}
142
143
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000144Handle<String> Factory::NewStringFromTwoByte(Vector<const uc16> string,
145 PretenureFlag pretenure) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000146 CALL_HEAP_FUNCTION(
147 isolate(),
148 isolate()->heap()->AllocateStringFromTwoByte(string, pretenure),
149 String);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000150}
151
152
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +0000153Handle<String> Factory::NewRawAsciiString(int length,
154 PretenureFlag pretenure) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000155 CALL_HEAP_FUNCTION(
156 isolate(),
157 isolate()->heap()->AllocateRawAsciiString(length, pretenure),
158 String);
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +0000159}
160
161
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000162Handle<String> Factory::NewRawTwoByteString(int length,
163 PretenureFlag pretenure) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000164 CALL_HEAP_FUNCTION(
165 isolate(),
166 isolate()->heap()->AllocateRawTwoByteString(length, pretenure),
167 String);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000168}
169
170
171Handle<String> Factory::NewConsString(Handle<String> first,
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000172 Handle<String> second) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000173 CALL_HEAP_FUNCTION(isolate(),
174 isolate()->heap()->AllocateConsString(*first, *second),
175 String);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000176}
177
178
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000179Handle<String> Factory::NewSubString(Handle<String> str,
180 int begin,
181 int end) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000182 CALL_HEAP_FUNCTION(isolate(),
183 str->SubString(begin, end),
184 String);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000185}
186
187
188Handle<String> Factory::NewExternalStringFromAscii(
189 ExternalAsciiString::Resource* resource) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000190 CALL_HEAP_FUNCTION(
191 isolate(),
192 isolate()->heap()->AllocateExternalStringFromAscii(resource),
193 String);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000194}
195
196
197Handle<String> Factory::NewExternalStringFromTwoByte(
198 ExternalTwoByteString::Resource* resource) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000199 CALL_HEAP_FUNCTION(
200 isolate(),
201 isolate()->heap()->AllocateExternalStringFromTwoByte(resource),
202 String);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000203}
204
205
206Handle<Context> Factory::NewGlobalContext() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000207 CALL_HEAP_FUNCTION(
208 isolate(),
209 isolate()->heap()->AllocateGlobalContext(),
210 Context);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000211}
212
213
214Handle<Context> Factory::NewFunctionContext(int length,
215 Handle<JSFunction> closure) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000216 CALL_HEAP_FUNCTION(
217 isolate(),
218 isolate()->heap()->AllocateFunctionContext(length, *closure),
219 Context);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000220}
221
222
223Handle<Context> Factory::NewWithContext(Handle<Context> previous,
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000224 Handle<JSObject> extension,
225 bool is_catch_context) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000226 CALL_HEAP_FUNCTION(
227 isolate(),
228 isolate()->heap()->AllocateWithContext(*previous,
229 *extension,
230 is_catch_context),
231 Context);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000232}
233
234
235Handle<Struct> Factory::NewStruct(InstanceType type) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000236 CALL_HEAP_FUNCTION(
237 isolate(),
238 isolate()->heap()->AllocateStruct(type),
239 Struct);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000240}
241
242
243Handle<AccessorInfo> Factory::NewAccessorInfo() {
244 Handle<AccessorInfo> info =
245 Handle<AccessorInfo>::cast(NewStruct(ACCESSOR_INFO_TYPE));
246 info->set_flag(0); // Must clear the flag, it was initialized as undefined.
247 return info;
248}
249
250
251Handle<Script> Factory::NewScript(Handle<String> source) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000252 // Generate id for this script.
253 int id;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000254 Heap* heap = isolate()->heap();
255 if (heap->last_script_id()->IsUndefined()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000256 // Script ids start from one.
257 id = 1;
258 } else {
259 // Increment id, wrap when positive smi is exhausted.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000260 id = Smi::cast(heap->last_script_id())->value();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000261 id++;
262 if (!Smi::IsValid(id)) {
263 id = 0;
264 }
265 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000266 heap->SetLastScriptId(Smi::FromInt(id));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000267
268 // Create and initialize script object.
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000269 Handle<Foreign> wrapper = NewForeign(0, TENURED);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000270 Handle<Script> script = Handle<Script>::cast(NewStruct(SCRIPT_TYPE));
271 script->set_source(*source);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000272 script->set_name(heap->undefined_value());
273 script->set_id(heap->last_script_id());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000274 script->set_line_offset(Smi::FromInt(0));
275 script->set_column_offset(Smi::FromInt(0));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000276 script->set_data(heap->undefined_value());
277 script->set_context_data(heap->undefined_value());
ager@chromium.orge2902be2009-06-08 12:21:35 +0000278 script->set_type(Smi::FromInt(Script::TYPE_NORMAL));
279 script->set_compilation_type(Smi::FromInt(Script::COMPILATION_TYPE_HOST));
ager@chromium.org9085a012009-05-11 19:22:57 +0000280 script->set_wrapper(*wrapper);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000281 script->set_line_ends(heap->undefined_value());
282 script->set_eval_from_shared(heap->undefined_value());
ager@chromium.orge2902be2009-06-08 12:21:35 +0000283 script->set_eval_from_instructions_offset(Smi::FromInt(0));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000284
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000285 return script;
286}
287
288
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000289Handle<Foreign> Factory::NewForeign(Address addr, PretenureFlag pretenure) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000290 CALL_HEAP_FUNCTION(isolate(),
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000291 isolate()->heap()->AllocateForeign(addr, pretenure),
292 Foreign);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000293}
294
295
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000296Handle<Foreign> Factory::NewForeign(const AccessorDescriptor* desc) {
297 return NewForeign((Address) desc, TENURED);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000298}
299
300
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000301Handle<ByteArray> Factory::NewByteArray(int length, PretenureFlag pretenure) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000302 ASSERT(0 <= length);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000303 CALL_HEAP_FUNCTION(
304 isolate(),
305 isolate()->heap()->AllocateByteArray(length, pretenure),
306 ByteArray);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000307}
308
309
ager@chromium.org3811b432009-10-28 14:53:37 +0000310Handle<ExternalArray> Factory::NewExternalArray(int length,
311 ExternalArrayType array_type,
312 void* external_pointer,
313 PretenureFlag pretenure) {
314 ASSERT(0 <= length);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000315 CALL_HEAP_FUNCTION(
316 isolate(),
317 isolate()->heap()->AllocateExternalArray(length,
318 array_type,
319 external_pointer,
320 pretenure),
321 ExternalArray);
ager@chromium.org3811b432009-10-28 14:53:37 +0000322}
323
324
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000325Handle<JSGlobalPropertyCell> Factory::NewJSGlobalPropertyCell(
326 Handle<Object> value) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000327 CALL_HEAP_FUNCTION(
328 isolate(),
329 isolate()->heap()->AllocateJSGlobalPropertyCell(*value),
330 JSGlobalPropertyCell);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000331}
332
333
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000334Handle<Map> Factory::NewMap(InstanceType type, int instance_size) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000335 CALL_HEAP_FUNCTION(
336 isolate(),
337 isolate()->heap()->AllocateMap(type, instance_size),
338 Map);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000339}
340
341
342Handle<JSObject> Factory::NewFunctionPrototype(Handle<JSFunction> function) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000343 CALL_HEAP_FUNCTION(
344 isolate(),
345 isolate()->heap()->AllocateFunctionPrototype(*function),
346 JSObject);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000347}
348
349
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000350Handle<Map> Factory::CopyMapDropDescriptors(Handle<Map> src) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000351 CALL_HEAP_FUNCTION(isolate(), src->CopyDropDescriptors(), Map);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000352}
353
354
ager@chromium.org32912102009-01-16 10:38:43 +0000355Handle<Map> Factory::CopyMap(Handle<Map> src,
356 int extra_inobject_properties) {
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000357 Handle<Map> copy = CopyMapDropDescriptors(src);
ager@chromium.org32912102009-01-16 10:38:43 +0000358 // Check that we do not overflow the instance size when adding the
359 // extra inobject properties.
360 int instance_size_delta = extra_inobject_properties * kPointerSize;
361 int max_instance_size_delta =
362 JSObject::kMaxInstanceSize - copy->instance_size();
363 if (instance_size_delta > max_instance_size_delta) {
364 // If the instance size overflows, we allocate as many properties
365 // as we can as inobject properties.
366 instance_size_delta = max_instance_size_delta;
367 extra_inobject_properties = max_instance_size_delta >> kPointerSizeLog2;
368 }
369 // Adjust the map with the extra inobject properties.
370 int inobject_properties =
371 copy->inobject_properties() + extra_inobject_properties;
372 copy->set_inobject_properties(inobject_properties);
373 copy->set_unused_property_fields(inobject_properties);
374 copy->set_instance_size(copy->instance_size() + instance_size_delta);
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000375 copy->set_visitor_id(StaticVisitorBase::GetVisitorId(*copy));
ager@chromium.org32912102009-01-16 10:38:43 +0000376 return copy;
377}
378
fschneider@chromium.org40b9da32010-06-28 11:29:21 +0000379
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000380Handle<Map> Factory::CopyMapDropTransitions(Handle<Map> src) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000381 CALL_HEAP_FUNCTION(isolate(), src->CopyDropTransitions(), Map);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000382}
383
384
fschneider@chromium.org40b9da32010-06-28 11:29:21 +0000385Handle<Map> Factory::GetFastElementsMap(Handle<Map> src) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000386 CALL_HEAP_FUNCTION(isolate(), src->GetFastElementsMap(), Map);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +0000387}
388
389
390Handle<Map> Factory::GetSlowElementsMap(Handle<Map> src) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000391 CALL_HEAP_FUNCTION(isolate(), src->GetSlowElementsMap(), Map);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +0000392}
393
394
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000395Handle<Map> Factory::GetExternalArrayElementsMap(
396 Handle<Map> src,
397 ExternalArrayType array_type,
398 bool safe_to_add_transition) {
399 CALL_HEAP_FUNCTION(isolate(),
400 src->GetExternalArrayElementsMap(array_type,
401 safe_to_add_transition),
402 Map);
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +0000403}
404
405
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000406Handle<FixedArray> Factory::CopyFixedArray(Handle<FixedArray> array) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000407 CALL_HEAP_FUNCTION(isolate(), array->Copy(), FixedArray);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000408}
409
410
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000411Handle<JSFunction> Factory::BaseNewFunctionFromSharedFunctionInfo(
412 Handle<SharedFunctionInfo> function_info,
sgjesse@chromium.org846fb742009-12-18 08:56:33 +0000413 Handle<Map> function_map,
414 PretenureFlag pretenure) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000415 CALL_HEAP_FUNCTION(
416 isolate(),
417 isolate()->heap()->AllocateFunction(*function_map,
418 *function_info,
419 isolate()->heap()->the_hole_value(),
420 pretenure),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000421 JSFunction);
422}
423
424
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000425Handle<JSFunction> Factory::NewFunctionFromSharedFunctionInfo(
426 Handle<SharedFunctionInfo> function_info,
sgjesse@chromium.org846fb742009-12-18 08:56:33 +0000427 Handle<Context> context,
428 PretenureFlag pretenure) {
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000429 Handle<JSFunction> result = BaseNewFunctionFromSharedFunctionInfo(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000430 function_info,
431 function_info->strict_mode()
432 ? isolate()->strict_mode_function_map()
433 : isolate()->function_map(),
434 pretenure);
435
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000436 result->set_context(*context);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000437 int number_of_literals = function_info->num_literals();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000438 Handle<FixedArray> literals = NewFixedArray(number_of_literals, pretenure);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000439 if (number_of_literals > 0) {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000440 // Store the object, regexp and array functions in the literals
441 // array prefix. These functions will be used when creating
442 // object, regexp and array literals in this function.
ager@chromium.org236ad962008-09-25 09:45:57 +0000443 literals->set(JSFunction::kLiteralGlobalContextIndex,
444 context->global_context());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000445 }
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000446 result->set_literals(*literals);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000447 result->set_next_function_link(isolate()->heap()->undefined_value());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000448
449 if (V8::UseCrankshaft() &&
450 FLAG_always_opt &&
451 result->is_compiled() &&
452 !function_info->is_toplevel() &&
453 function_info->allows_lazy_compilation()) {
454 result->MarkForLazyRecompilation();
455 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000456 return result;
457}
458
459
460Handle<Object> Factory::NewNumber(double value,
461 PretenureFlag pretenure) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000462 CALL_HEAP_FUNCTION(
463 isolate(),
464 isolate()->heap()->NumberFromDouble(value, pretenure), Object);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000465}
466
467
468Handle<Object> Factory::NewNumberFromInt(int value) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000469 CALL_HEAP_FUNCTION(
470 isolate(),
471 isolate()->heap()->NumberFromInt32(value), Object);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000472}
473
474
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000475Handle<Object> Factory::NewNumberFromUint(uint32_t value) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000476 CALL_HEAP_FUNCTION(
477 isolate(),
478 isolate()->heap()->NumberFromUint32(value), Object);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000479}
480
481
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000482Handle<JSObject> Factory::NewNeanderObject() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000483 CALL_HEAP_FUNCTION(
484 isolate(),
485 isolate()->heap()->AllocateJSObjectFromMap(
486 isolate()->heap()->neander_map()),
487 JSObject);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000488}
489
490
491Handle<Object> Factory::NewTypeError(const char* type,
492 Vector< Handle<Object> > args) {
493 return NewError("MakeTypeError", type, args);
494}
495
496
497Handle<Object> Factory::NewTypeError(Handle<String> message) {
498 return NewError("$TypeError", message);
499}
500
501
502Handle<Object> Factory::NewRangeError(const char* type,
503 Vector< Handle<Object> > args) {
504 return NewError("MakeRangeError", type, args);
505}
506
507
508Handle<Object> Factory::NewRangeError(Handle<String> message) {
509 return NewError("$RangeError", message);
510}
511
512
513Handle<Object> Factory::NewSyntaxError(const char* type, Handle<JSArray> args) {
514 return NewError("MakeSyntaxError", type, args);
515}
516
517
518Handle<Object> Factory::NewSyntaxError(Handle<String> message) {
519 return NewError("$SyntaxError", message);
520}
521
522
523Handle<Object> Factory::NewReferenceError(const char* type,
524 Vector< Handle<Object> > args) {
525 return NewError("MakeReferenceError", type, args);
526}
527
528
529Handle<Object> Factory::NewReferenceError(Handle<String> message) {
530 return NewError("$ReferenceError", message);
531}
532
533
534Handle<Object> Factory::NewError(const char* maker, const char* type,
535 Vector< Handle<Object> > args) {
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000536 v8::HandleScope scope; // Instantiate a closeable HandleScope for EscapeFrom.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000537 Handle<FixedArray> array = NewFixedArray(args.length());
ager@chromium.org7c537e22008-10-16 08:43:32 +0000538 for (int i = 0; i < args.length(); i++) {
539 array->set(i, *args[i]);
540 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000541 Handle<JSArray> object = NewJSArrayWithElements(array);
ager@chromium.org7c537e22008-10-16 08:43:32 +0000542 Handle<Object> result = NewError(maker, type, object);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000543 return result.EscapeFrom(&scope);
544}
545
546
547Handle<Object> Factory::NewEvalError(const char* type,
548 Vector< Handle<Object> > args) {
549 return NewError("MakeEvalError", type, args);
550}
551
552
553Handle<Object> Factory::NewError(const char* type,
554 Vector< Handle<Object> > args) {
555 return NewError("MakeError", type, args);
556}
557
558
559Handle<Object> Factory::NewError(const char* maker,
560 const char* type,
561 Handle<JSArray> args) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000562 Handle<String> make_str = LookupAsciiSymbol(maker);
563 Handle<Object> fun_obj(
564 isolate()->js_builtins_object()->GetPropertyNoExceptionThrown(*make_str));
ager@chromium.org4af710e2009-09-15 12:20:11 +0000565 // If the builtins haven't been properly configured yet this error
566 // constructor may not have been defined. Bail out.
567 if (!fun_obj->IsJSFunction())
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000568 return undefined_value();
ager@chromium.org4af710e2009-09-15 12:20:11 +0000569 Handle<JSFunction> fun = Handle<JSFunction>::cast(fun_obj);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000570 Handle<Object> type_obj = LookupAsciiSymbol(type);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000571 Object** argv[2] = { type_obj.location(),
572 Handle<Object>::cast(args).location() };
573
574 // Invoke the JavaScript factory method. If an exception is thrown while
575 // running the factory method, use the exception as the result.
576 bool caught_exception;
577 Handle<Object> result = Execution::TryCall(fun,
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000578 isolate()->js_builtins_object(), 2, argv, &caught_exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000579 return result;
580}
581
582
583Handle<Object> Factory::NewError(Handle<String> message) {
584 return NewError("$Error", message);
585}
586
587
588Handle<Object> Factory::NewError(const char* constructor,
589 Handle<String> message) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000590 Handle<String> constr = LookupAsciiSymbol(constructor);
591 Handle<JSFunction> fun = Handle<JSFunction>(
592 JSFunction::cast(isolate()->js_builtins_object()->
593 GetPropertyNoExceptionThrown(*constr)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000594 Object** argv[1] = { Handle<Object>::cast(message).location() };
595
596 // Invoke the JavaScript factory method. If an exception is thrown while
597 // running the factory method, use the exception as the result.
598 bool caught_exception;
599 Handle<Object> result = Execution::TryCall(fun,
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000600 isolate()->js_builtins_object(), 1, argv, &caught_exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000601 return result;
602}
603
604
605Handle<JSFunction> Factory::NewFunction(Handle<String> name,
606 InstanceType type,
607 int instance_size,
608 Handle<Code> code,
609 bool force_initial_map) {
610 // Allocate the function
611 Handle<JSFunction> function = NewFunction(name, the_hole_value());
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000612
613 // Setup the code pointer in both the shared function info and in
614 // the function itself.
615 function->shared()->set_code(*code);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000616 function->set_code(*code);
617
618 if (force_initial_map ||
619 type != JS_OBJECT_TYPE ||
620 instance_size != JSObject::kHeaderSize) {
621 Handle<Map> initial_map = NewMap(type, instance_size);
622 Handle<JSObject> prototype = NewFunctionPrototype(function);
623 initial_map->set_prototype(*prototype);
624 function->set_initial_map(*initial_map);
625 initial_map->set_constructor(*function);
626 } else {
627 ASSERT(!function->has_initial_map());
628 ASSERT(!function->has_prototype());
629 }
630
631 return function;
632}
633
634
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000635Handle<JSFunction> Factory::NewFunctionWithPrototype(Handle<String> name,
636 InstanceType type,
637 int instance_size,
638 Handle<JSObject> prototype,
639 Handle<Code> code,
640 bool force_initial_map) {
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000641 // Allocate the function.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000642 Handle<JSFunction> function = NewFunction(name, prototype);
643
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000644 // Setup the code pointer in both the shared function info and in
645 // the function itself.
646 function->shared()->set_code(*code);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000647 function->set_code(*code);
648
649 if (force_initial_map ||
650 type != JS_OBJECT_TYPE ||
651 instance_size != JSObject::kHeaderSize) {
652 Handle<Map> initial_map = NewMap(type, instance_size);
653 function->set_initial_map(*initial_map);
654 initial_map->set_constructor(*function);
655 }
656
657 // Set function.prototype and give the prototype a constructor
658 // property that refers to the function.
659 SetPrototypeProperty(function, prototype);
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +0000660 // Currently safe because it is only invoked from Genesis.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000661 SetLocalPropertyNoThrow(prototype, constructor_symbol(), function, DONT_ENUM);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000662 return function;
663}
664
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000665
kmillikin@chromium.org4111b802010-05-03 10:34:42 +0000666Handle<JSFunction> Factory::NewFunctionWithoutPrototype(Handle<String> name,
667 Handle<Code> code) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000668 Handle<JSFunction> function = NewFunctionWithoutPrototype(name,
669 kNonStrictMode);
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000670 function->shared()->set_code(*code);
kmillikin@chromium.org4111b802010-05-03 10:34:42 +0000671 function->set_code(*code);
672 ASSERT(!function->has_initial_map());
673 ASSERT(!function->has_prototype());
674 return function;
675}
676
677
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000678Handle<Code> Factory::NewCode(const CodeDesc& desc,
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000679 Code::Flags flags,
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +0000680 Handle<Object> self_ref,
681 bool immovable) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000682 CALL_HEAP_FUNCTION(isolate(),
683 isolate()->heap()->CreateCode(
684 desc, flags, self_ref, immovable),
685 Code);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000686}
687
688
689Handle<Code> Factory::CopyCode(Handle<Code> code) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000690 CALL_HEAP_FUNCTION(isolate(),
691 isolate()->heap()->CopyCode(*code),
692 Code);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000693}
694
695
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000696Handle<Code> Factory::CopyCode(Handle<Code> code, Vector<byte> reloc_info) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000697 CALL_HEAP_FUNCTION(isolate(),
698 isolate()->heap()->CopyCode(*code, reloc_info),
699 Code);
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000700}
701
702
lrn@chromium.org303ada72010-10-27 09:33:13 +0000703MUST_USE_RESULT static inline MaybeObject* DoCopyInsert(
704 DescriptorArray* array,
705 String* key,
706 Object* value,
707 PropertyAttributes attributes) {
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000708 CallbacksDescriptor desc(key, value, attributes);
lrn@chromium.org303ada72010-10-27 09:33:13 +0000709 MaybeObject* obj = array->CopyInsert(&desc, REMOVE_TRANSITIONS);
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000710 return obj;
711}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000712
713
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000714// Allocate the new array.
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000715Handle<DescriptorArray> Factory::CopyAppendForeignDescriptor(
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000716 Handle<DescriptorArray> array,
717 Handle<String> key,
718 Handle<Object> value,
719 PropertyAttributes attributes) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000720 CALL_HEAP_FUNCTION(isolate(),
721 DoCopyInsert(*array, *key, *value, attributes),
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000722 DescriptorArray);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000723}
724
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000725
726Handle<String> Factory::SymbolFromString(Handle<String> value) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000727 CALL_HEAP_FUNCTION(isolate(),
728 isolate()->heap()->LookupSymbol(*value), String);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000729}
730
731
732Handle<DescriptorArray> Factory::CopyAppendCallbackDescriptors(
733 Handle<DescriptorArray> array,
734 Handle<Object> descriptors) {
735 v8::NeanderArray callbacks(descriptors);
736 int nof_callbacks = callbacks.length();
737 Handle<DescriptorArray> result =
738 NewDescriptorArray(array->number_of_descriptors() + nof_callbacks);
739
740 // Number of descriptors added to the result so far.
741 int descriptor_count = 0;
742
743 // Copy the descriptors from the array.
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000744 for (int i = 0; i < array->number_of_descriptors(); i++) {
745 if (array->GetType(i) != NULL_DESCRIPTOR) {
746 result->CopyFrom(descriptor_count++, *array, i);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000747 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000748 }
749
750 // Number of duplicates detected.
751 int duplicates = 0;
752
753 // Fill in new callback descriptors. Process the callbacks from
754 // back to front so that the last callback with a given name takes
755 // precedence over previously added callbacks with that name.
756 for (int i = nof_callbacks - 1; i >= 0; i--) {
757 Handle<AccessorInfo> entry =
758 Handle<AccessorInfo>(AccessorInfo::cast(callbacks.get(i)));
759 // Ensure the key is a symbol before writing into the instance descriptor.
760 Handle<String> key =
761 SymbolFromString(Handle<String>(String::cast(entry->name())));
762 // Check if a descriptor with this name already exists before writing.
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000763 if (result->LinearSearch(*key, descriptor_count) ==
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000764 DescriptorArray::kNotFound) {
765 CallbacksDescriptor desc(*key, *entry, entry->property_attributes());
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000766 result->Set(descriptor_count, &desc);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000767 descriptor_count++;
768 } else {
769 duplicates++;
770 }
771 }
772
773 // If duplicates were detected, allocate a result of the right size
774 // and transfer the elements.
775 if (duplicates > 0) {
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000776 int number_of_descriptors = result->number_of_descriptors() - duplicates;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000777 Handle<DescriptorArray> new_result =
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000778 NewDescriptorArray(number_of_descriptors);
779 for (int i = 0; i < number_of_descriptors; i++) {
780 new_result->CopyFrom(i, *result, i);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000781 }
782 result = new_result;
783 }
784
785 // Sort the result before returning.
786 result->Sort();
787 return result;
788}
789
790
791Handle<JSObject> Factory::NewJSObject(Handle<JSFunction> constructor,
792 PretenureFlag pretenure) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000793 CALL_HEAP_FUNCTION(
794 isolate(),
795 isolate()->heap()->AllocateJSObject(*constructor, pretenure), JSObject);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000796}
797
798
kasperl@chromium.org2abc4502009-07-02 07:00:29 +0000799Handle<GlobalObject> Factory::NewGlobalObject(
800 Handle<JSFunction> constructor) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000801 CALL_HEAP_FUNCTION(isolate(),
802 isolate()->heap()->AllocateGlobalObject(*constructor),
kasperl@chromium.org2abc4502009-07-02 07:00:29 +0000803 GlobalObject);
804}
805
806
807
ager@chromium.org236ad962008-09-25 09:45:57 +0000808Handle<JSObject> Factory::NewJSObjectFromMap(Handle<Map> map) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000809 CALL_HEAP_FUNCTION(
810 isolate(),
811 isolate()->heap()->AllocateJSObjectFromMap(*map, NOT_TENURED),
812 JSObject);
ager@chromium.org236ad962008-09-25 09:45:57 +0000813}
814
815
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +0000816Handle<JSArray> Factory::NewJSArray(int capacity,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000817 PretenureFlag pretenure) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000818 Handle<JSObject> obj = NewJSObject(isolate()->array_function(), pretenure);
819 CALL_HEAP_FUNCTION(isolate(),
820 Handle<JSArray>::cast(obj)->Initialize(capacity),
821 JSArray);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000822}
823
824
825Handle<JSArray> Factory::NewJSArrayWithElements(Handle<FixedArray> elements,
826 PretenureFlag pretenure) {
827 Handle<JSArray> result =
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000828 Handle<JSArray>::cast(NewJSObject(isolate()->array_function(),
829 pretenure));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000830 result->SetContent(*elements);
831 return result;
832}
833
834
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000835Handle<JSProxy> Factory::NewJSProxy(Handle<Object> handler,
836 Handle<Object> prototype) {
837 CALL_HEAP_FUNCTION(
838 isolate(),
839 isolate()->heap()->AllocateJSProxy(*handler, *prototype),
840 JSProxy);
841}
842
843
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000844Handle<SharedFunctionInfo> Factory::NewSharedFunctionInfo(
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +0000845 Handle<String> name,
846 int number_of_literals,
847 Handle<Code> code,
ager@chromium.orgb5737492010-07-15 09:29:43 +0000848 Handle<SerializedScopeInfo> scope_info) {
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000849 Handle<SharedFunctionInfo> shared = NewSharedFunctionInfo(name);
850 shared->set_code(*code);
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +0000851 shared->set_scope_info(*scope_info);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000852 int literals_array_size = number_of_literals;
853 // If the function contains object, regexp or array literals,
854 // allocate extra space for a literals array prefix containing the
855 // context.
856 if (number_of_literals > 0) {
857 literals_array_size += JSFunction::kLiteralsPrefixSize;
858 }
859 shared->set_num_literals(literals_array_size);
860 return shared;
861}
862
863
kmillikin@chromium.org31b12772011-02-02 16:08:26 +0000864Handle<JSMessageObject> Factory::NewJSMessageObject(
865 Handle<String> type,
866 Handle<JSArray> arguments,
867 int start_position,
868 int end_position,
869 Handle<Object> script,
870 Handle<Object> stack_trace,
871 Handle<Object> stack_frames) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000872 CALL_HEAP_FUNCTION(isolate(),
873 isolate()->heap()->AllocateJSMessageObject(*type,
874 *arguments,
875 start_position,
876 end_position,
877 *script,
878 *stack_trace,
879 *stack_frames),
kmillikin@chromium.org31b12772011-02-02 16:08:26 +0000880 JSMessageObject);
881}
882
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000883Handle<SharedFunctionInfo> Factory::NewSharedFunctionInfo(Handle<String> name) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000884 CALL_HEAP_FUNCTION(isolate(),
885 isolate()->heap()->AllocateSharedFunctionInfo(*name),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000886 SharedFunctionInfo);
887}
888
889
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000890Handle<String> Factory::NumberToString(Handle<Object> number) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000891 CALL_HEAP_FUNCTION(isolate(),
892 isolate()->heap()->NumberToString(*number), String);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000893}
894
895
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000896Handle<NumberDictionary> Factory::DictionaryAtNumberPut(
897 Handle<NumberDictionary> dictionary,
898 uint32_t key,
899 Handle<Object> value) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000900 CALL_HEAP_FUNCTION(isolate(),
901 dictionary->AtNumberPut(key, *value),
902 NumberDictionary);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000903}
904
905
906Handle<JSFunction> Factory::NewFunctionHelper(Handle<String> name,
907 Handle<Object> prototype) {
908 Handle<SharedFunctionInfo> function_share = NewSharedFunctionInfo(name);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000909 CALL_HEAP_FUNCTION(
910 isolate(),
911 isolate()->heap()->AllocateFunction(*isolate()->function_map(),
912 *function_share,
913 *prototype),
914 JSFunction);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000915}
916
917
918Handle<JSFunction> Factory::NewFunction(Handle<String> name,
919 Handle<Object> prototype) {
920 Handle<JSFunction> fun = NewFunctionHelper(name, prototype);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000921 fun->set_context(isolate()->context()->global_context());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000922 return fun;
923}
924
925
kmillikin@chromium.org4111b802010-05-03 10:34:42 +0000926Handle<JSFunction> Factory::NewFunctionWithoutPrototypeHelper(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000927 Handle<String> name,
928 StrictModeFlag strict_mode) {
kmillikin@chromium.org4111b802010-05-03 10:34:42 +0000929 Handle<SharedFunctionInfo> function_share = NewSharedFunctionInfo(name);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000930 Handle<Map> map = strict_mode == kStrictMode
931 ? isolate()->strict_mode_function_without_prototype_map()
932 : isolate()->function_without_prototype_map();
933 CALL_HEAP_FUNCTION(isolate(),
934 isolate()->heap()->AllocateFunction(
935 *map,
kmillikin@chromium.org4111b802010-05-03 10:34:42 +0000936 *function_share,
937 *the_hole_value()),
938 JSFunction);
939}
940
941
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000942Handle<JSFunction> Factory::NewFunctionWithoutPrototype(
943 Handle<String> name,
944 StrictModeFlag strict_mode) {
945 Handle<JSFunction> fun = NewFunctionWithoutPrototypeHelper(name, strict_mode);
946 fun->set_context(isolate()->context()->global_context());
kmillikin@chromium.org4111b802010-05-03 10:34:42 +0000947 return fun;
948}
949
950
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000951Handle<Object> Factory::ToObject(Handle<Object> object) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000952 CALL_HEAP_FUNCTION(isolate(), object->ToObject(), Object);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000953}
954
955
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000956Handle<Object> Factory::ToObject(Handle<Object> object,
957 Handle<Context> global_context) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000958 CALL_HEAP_FUNCTION(isolate(), object->ToObject(*global_context), Object);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000959}
960
961
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000962#ifdef ENABLE_DEBUGGER_SUPPORT
v8.team.kasperl727e9952008-09-02 14:56:44 +0000963Handle<DebugInfo> Factory::NewDebugInfo(Handle<SharedFunctionInfo> shared) {
964 // Get the original code of the function.
965 Handle<Code> code(shared->code());
966
967 // Create a copy of the code before allocating the debug info object to avoid
968 // allocation while setting up the debug info object.
969 Handle<Code> original_code(*Factory::CopyCode(code));
970
971 // Allocate initial fixed array for active break points before allocating the
972 // debug info object to avoid allocation while setting up the debug info
973 // object.
974 Handle<FixedArray> break_points(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000975 NewFixedArray(Debug::kEstimatedNofBreakPointsInFunction));
v8.team.kasperl727e9952008-09-02 14:56:44 +0000976
977 // Create and set up the debug info object. Debug info contains function, a
978 // copy of the original code, the executing code and initial fixed array for
979 // active break points.
980 Handle<DebugInfo> debug_info =
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000981 Handle<DebugInfo>::cast(NewStruct(DEBUG_INFO_TYPE));
v8.team.kasperl727e9952008-09-02 14:56:44 +0000982 debug_info->set_shared(*shared);
983 debug_info->set_original_code(*original_code);
984 debug_info->set_code(*code);
985 debug_info->set_break_points(*break_points);
986
987 // Link debug info to function.
988 shared->set_debug_info(*debug_info);
989
990 return debug_info;
991}
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000992#endif
v8.team.kasperl727e9952008-09-02 14:56:44 +0000993
994
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000995Handle<JSObject> Factory::NewArgumentsObject(Handle<Object> callee,
996 int length) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000997 CALL_HEAP_FUNCTION(
998 isolate(),
999 isolate()->heap()->AllocateArgumentsObject(*callee, length), JSObject);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001000}
1001
1002
1003Handle<JSFunction> Factory::CreateApiFunction(
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001004 Handle<FunctionTemplateInfo> obj, ApiInstanceType instance_type) {
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00001005 Handle<Code> code = isolate()->builtins()->HandleApiCall();
1006 Handle<Code> construct_stub = isolate()->builtins()->JSConstructStubApi();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001007
kasper.lund212ac232008-07-16 07:07:30 +00001008 int internal_field_count = 0;
1009 if (!obj->instance_template()->IsUndefined()) {
1010 Handle<ObjectTemplateInfo> instance_template =
1011 Handle<ObjectTemplateInfo>(
1012 ObjectTemplateInfo::cast(obj->instance_template()));
1013 internal_field_count =
1014 Smi::cast(instance_template->internal_field_count())->value();
1015 }
1016
1017 int instance_size = kPointerSize * internal_field_count;
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001018 InstanceType type = INVALID_TYPE;
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001019 switch (instance_type) {
1020 case JavaScriptObject:
1021 type = JS_OBJECT_TYPE;
1022 instance_size += JSObject::kHeaderSize;
1023 break;
1024 case InnerGlobalObject:
1025 type = JS_GLOBAL_OBJECT_TYPE;
1026 instance_size += JSGlobalObject::kSize;
1027 break;
1028 case OuterGlobalObject:
1029 type = JS_GLOBAL_PROXY_TYPE;
1030 instance_size += JSGlobalProxy::kSize;
1031 break;
1032 default:
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001033 break;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001034 }
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001035 ASSERT(type != INVALID_TYPE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001036
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001037 Handle<JSFunction> result =
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001038 NewFunction(Factory::empty_symbol(),
1039 type,
1040 instance_size,
1041 code,
1042 true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001043 // Set class name.
1044 Handle<Object> class_name = Handle<Object>(obj->class_name());
1045 if (class_name->IsString()) {
1046 result->shared()->set_instance_class_name(*class_name);
1047 result->shared()->set_name(*class_name);
1048 }
1049
1050 Handle<Map> map = Handle<Map>(result->initial_map());
1051
1052 // Mark as undetectable if needed.
1053 if (obj->undetectable()) {
1054 map->set_is_undetectable();
1055 }
1056
1057 // Mark as hidden for the __proto__ accessor if needed.
1058 if (obj->hidden_prototype()) {
1059 map->set_is_hidden_prototype();
1060 }
1061
1062 // Mark as needs_access_check if needed.
1063 if (obj->needs_access_check()) {
ager@chromium.org870a0b62008-11-04 11:43:05 +00001064 map->set_is_access_check_needed(true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001065 }
1066
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001067 // Set interceptor information in the map.
1068 if (!obj->named_property_handler()->IsUndefined()) {
1069 map->set_has_named_interceptor();
1070 }
1071 if (!obj->indexed_property_handler()->IsUndefined()) {
1072 map->set_has_indexed_interceptor();
1073 }
1074
1075 // Set instance call-as-function information in the map.
1076 if (!obj->instance_call_handler()->IsUndefined()) {
1077 map->set_has_instance_call_handler();
1078 }
1079
1080 result->shared()->set_function_data(*obj);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001081 result->shared()->set_construct_stub(*construct_stub);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001082 result->shared()->DontAdaptArguments();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001083
1084 // Recursively copy parent templates' accessors, 'data' may be modified.
1085 Handle<DescriptorArray> array =
1086 Handle<DescriptorArray>(map->instance_descriptors());
1087 while (true) {
1088 Handle<Object> props = Handle<Object>(obj->property_accessors());
1089 if (!props->IsUndefined()) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001090 array = CopyAppendCallbackDescriptors(array, props);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001091 }
1092 Handle<Object> parent = Handle<Object>(obj->parent_template());
1093 if (parent->IsUndefined()) break;
1094 obj = Handle<FunctionTemplateInfo>::cast(parent);
1095 }
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001096 if (!array->IsEmpty()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001097 map->set_instance_descriptors(*array);
1098 }
1099
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001100 ASSERT(result->shared()->IsApiFunction());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001101 return result;
1102}
1103
1104
ager@chromium.org236ad962008-09-25 09:45:57 +00001105Handle<MapCache> Factory::NewMapCache(int at_least_space_for) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001106 CALL_HEAP_FUNCTION(isolate(),
1107 MapCache::Allocate(at_least_space_for), MapCache);
ager@chromium.org236ad962008-09-25 09:45:57 +00001108}
1109
1110
lrn@chromium.org303ada72010-10-27 09:33:13 +00001111MUST_USE_RESULT static MaybeObject* UpdateMapCacheWith(Context* context,
1112 FixedArray* keys,
1113 Map* map) {
1114 Object* result;
1115 { MaybeObject* maybe_result =
1116 MapCache::cast(context->map_cache())->Put(keys, map);
1117 if (!maybe_result->ToObject(&result)) return maybe_result;
1118 }
1119 context->set_map_cache(MapCache::cast(result));
ager@chromium.org236ad962008-09-25 09:45:57 +00001120 return result;
1121}
1122
1123
1124Handle<MapCache> Factory::AddToMapCache(Handle<Context> context,
1125 Handle<FixedArray> keys,
1126 Handle<Map> map) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001127 CALL_HEAP_FUNCTION(isolate(),
1128 UpdateMapCacheWith(*context, *keys, *map), MapCache);
ager@chromium.org236ad962008-09-25 09:45:57 +00001129}
1130
1131
1132Handle<Map> Factory::ObjectLiteralMapFromCache(Handle<Context> context,
1133 Handle<FixedArray> keys) {
1134 if (context->map_cache()->IsUndefined()) {
1135 // Allocate the new map cache for the global context.
1136 Handle<MapCache> new_cache = NewMapCache(24);
1137 context->set_map_cache(*new_cache);
1138 }
ager@chromium.org32912102009-01-16 10:38:43 +00001139 // Check to see whether there is a matching element in the cache.
ager@chromium.org236ad962008-09-25 09:45:57 +00001140 Handle<MapCache> cache =
1141 Handle<MapCache>(MapCache::cast(context->map_cache()));
1142 Handle<Object> result = Handle<Object>(cache->Lookup(*keys));
1143 if (result->IsMap()) return Handle<Map>::cast(result);
1144 // Create a new map and add it to the cache.
1145 Handle<Map> map =
ager@chromium.org32912102009-01-16 10:38:43 +00001146 CopyMap(Handle<Map>(context->object_function()->initial_map()),
1147 keys->length());
ager@chromium.org236ad962008-09-25 09:45:57 +00001148 AddToMapCache(context, keys, map);
1149 return Handle<Map>(map);
1150}
1151
1152
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001153void Factory::SetRegExpAtomData(Handle<JSRegExp> regexp,
1154 JSRegExp::Type type,
1155 Handle<String> source,
1156 JSRegExp::Flags flags,
1157 Handle<Object> data) {
1158 Handle<FixedArray> store = NewFixedArray(JSRegExp::kAtomDataSize);
1159
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001160 store->set(JSRegExp::kTagIndex, Smi::FromInt(type));
1161 store->set(JSRegExp::kSourceIndex, *source);
1162 store->set(JSRegExp::kFlagsIndex, Smi::FromInt(flags.value()));
1163 store->set(JSRegExp::kAtomPatternIndex, *data);
1164 regexp->set_data(*store);
1165}
1166
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001167void Factory::SetRegExpIrregexpData(Handle<JSRegExp> regexp,
1168 JSRegExp::Type type,
1169 Handle<String> source,
1170 JSRegExp::Flags flags,
1171 int capture_count) {
1172 Handle<FixedArray> store = NewFixedArray(JSRegExp::kIrregexpDataSize);
1173
1174 store->set(JSRegExp::kTagIndex, Smi::FromInt(type));
1175 store->set(JSRegExp::kSourceIndex, *source);
1176 store->set(JSRegExp::kFlagsIndex, Smi::FromInt(flags.value()));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001177 store->set(JSRegExp::kIrregexpASCIICodeIndex, HEAP->the_hole_value());
1178 store->set(JSRegExp::kIrregexpUC16CodeIndex, HEAP->the_hole_value());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001179 store->set(JSRegExp::kIrregexpMaxRegisterCountIndex, Smi::FromInt(0));
1180 store->set(JSRegExp::kIrregexpCaptureCountIndex,
1181 Smi::FromInt(capture_count));
1182 regexp->set_data(*store);
1183}
1184
1185
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001186
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001187void Factory::ConfigureInstance(Handle<FunctionTemplateInfo> desc,
1188 Handle<JSObject> instance,
1189 bool* pending_exception) {
1190 // Configure the instance by adding the properties specified by the
1191 // instance template.
1192 Handle<Object> instance_template = Handle<Object>(desc->instance_template());
1193 if (!instance_template->IsUndefined()) {
1194 Execution::ConfigureInstance(instance,
1195 instance_template,
1196 pending_exception);
1197 } else {
1198 *pending_exception = false;
1199 }
1200}
1201
1202
1203} } // namespace v8::internal