blob: 971f9f9cee1b885fb3fc2cc7967f1ee8a118f9e5 [file] [log] [blame]
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001// Copyright 2011 the V8 project authors. All rights reserved.
Steve Blocka7e24c12009-10-30 11:49:00 +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"
31#include "debug.h"
32#include "execution.h"
33#include "factory.h"
34#include "macro-assembler.h"
Ben Murdochb0fe1622011-05-05 13:52:32 +010035#include "objects.h"
Iain Merrick75681382010-08-19 15:07:18 +010036#include "objects-visiting.h"
Ben Murdoch69a99ed2011-11-30 16:03:39 +000037#include "scopeinfo.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000038
39namespace v8 {
40namespace internal {
41
42
43Handle<FixedArray> Factory::NewFixedArray(int size, PretenureFlag pretenure) {
44 ASSERT(0 <= size);
Steve Block44f0eee2011-05-26 01:26:41 +010045 CALL_HEAP_FUNCTION(
46 isolate(),
47 isolate()->heap()->AllocateFixedArray(size, pretenure),
48 FixedArray);
Steve Blocka7e24c12009-10-30 11:49:00 +000049}
50
51
Steve Block6ded16b2010-05-10 14:33:55 +010052Handle<FixedArray> Factory::NewFixedArrayWithHoles(int size,
53 PretenureFlag pretenure) {
Steve Blocka7e24c12009-10-30 11:49:00 +000054 ASSERT(0 <= size);
Steve Block44f0eee2011-05-26 01:26:41 +010055 CALL_HEAP_FUNCTION(
56 isolate(),
57 isolate()->heap()->AllocateFixedArrayWithHoles(size, pretenure),
58 FixedArray);
Steve Blocka7e24c12009-10-30 11:49:00 +000059}
60
61
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000062Handle<FixedArray> Factory::NewFixedDoubleArray(int size,
63 PretenureFlag pretenure) {
64 ASSERT(0 <= size);
65 CALL_HEAP_FUNCTION(
66 isolate(),
67 isolate()->heap()->AllocateUninitializedFixedDoubleArray(size, pretenure),
68 FixedArray);
69}
70
71
Steve Blocka7e24c12009-10-30 11:49:00 +000072Handle<StringDictionary> Factory::NewStringDictionary(int at_least_space_for) {
73 ASSERT(0 <= at_least_space_for);
Steve Block44f0eee2011-05-26 01:26:41 +010074 CALL_HEAP_FUNCTION(isolate(),
75 StringDictionary::Allocate(at_least_space_for),
Steve Blocka7e24c12009-10-30 11:49:00 +000076 StringDictionary);
77}
78
79
Ben Murdoch2b4ba112012-01-20 14:57:15 +000080Handle<SeededNumberDictionary> Factory::NewSeededNumberDictionary(
81 int at_least_space_for) {
Steve Blocka7e24c12009-10-30 11:49:00 +000082 ASSERT(0 <= at_least_space_for);
Steve Block44f0eee2011-05-26 01:26:41 +010083 CALL_HEAP_FUNCTION(isolate(),
Ben Murdoch2b4ba112012-01-20 14:57:15 +000084 SeededNumberDictionary::Allocate(at_least_space_for),
85 SeededNumberDictionary);
86}
87
88
89Handle<UnseededNumberDictionary> Factory::NewUnseededNumberDictionary(
90 int at_least_space_for) {
91 ASSERT(0 <= at_least_space_for);
92 CALL_HEAP_FUNCTION(isolate(),
93 UnseededNumberDictionary::Allocate(at_least_space_for),
94 UnseededNumberDictionary);
Steve Blocka7e24c12009-10-30 11:49:00 +000095}
96
97
Ben Murdoch69a99ed2011-11-30 16:03:39 +000098Handle<ObjectHashTable> Factory::NewObjectHashTable(int at_least_space_for) {
99 ASSERT(0 <= at_least_space_for);
100 CALL_HEAP_FUNCTION(isolate(),
101 ObjectHashTable::Allocate(at_least_space_for),
102 ObjectHashTable);
103}
104
105
Steve Blocka7e24c12009-10-30 11:49:00 +0000106Handle<DescriptorArray> Factory::NewDescriptorArray(int number_of_descriptors) {
107 ASSERT(0 <= number_of_descriptors);
Steve Block44f0eee2011-05-26 01:26:41 +0100108 CALL_HEAP_FUNCTION(isolate(),
109 DescriptorArray::Allocate(number_of_descriptors),
Steve Blocka7e24c12009-10-30 11:49:00 +0000110 DescriptorArray);
111}
112
113
Ben Murdochb0fe1622011-05-05 13:52:32 +0100114Handle<DeoptimizationInputData> Factory::NewDeoptimizationInputData(
115 int deopt_entry_count,
116 PretenureFlag pretenure) {
117 ASSERT(deopt_entry_count > 0);
Steve Block44f0eee2011-05-26 01:26:41 +0100118 CALL_HEAP_FUNCTION(isolate(),
119 DeoptimizationInputData::Allocate(deopt_entry_count,
Ben Murdochb0fe1622011-05-05 13:52:32 +0100120 pretenure),
121 DeoptimizationInputData);
122}
123
124
125Handle<DeoptimizationOutputData> Factory::NewDeoptimizationOutputData(
126 int deopt_entry_count,
127 PretenureFlag pretenure) {
128 ASSERT(deopt_entry_count > 0);
Steve Block44f0eee2011-05-26 01:26:41 +0100129 CALL_HEAP_FUNCTION(isolate(),
130 DeoptimizationOutputData::Allocate(deopt_entry_count,
Ben Murdochb0fe1622011-05-05 13:52:32 +0100131 pretenure),
132 DeoptimizationOutputData);
133}
134
135
Steve Blocka7e24c12009-10-30 11:49:00 +0000136// Symbols are created in the old generation (data space).
137Handle<String> Factory::LookupSymbol(Vector<const char> string) {
Steve Block44f0eee2011-05-26 01:26:41 +0100138 CALL_HEAP_FUNCTION(isolate(),
139 isolate()->heap()->LookupSymbol(string),
140 String);
Steve Blocka7e24c12009-10-30 11:49:00 +0000141}
142
Ben Murdoch257744e2011-11-30 15:57:28 +0000143// Symbols are created in the old generation (data space).
144Handle<String> Factory::LookupSymbol(Handle<String> string) {
145 CALL_HEAP_FUNCTION(isolate(),
146 isolate()->heap()->LookupSymbol(*string),
147 String);
148}
149
Steve Block9fac8402011-05-12 15:51:54 +0100150Handle<String> Factory::LookupAsciiSymbol(Vector<const char> string) {
Steve Block44f0eee2011-05-26 01:26:41 +0100151 CALL_HEAP_FUNCTION(isolate(),
152 isolate()->heap()->LookupAsciiSymbol(string),
153 String);
Steve Block9fac8402011-05-12 15:51:54 +0100154}
155
Ben Murdoch257744e2011-11-30 15:57:28 +0000156
157Handle<String> Factory::LookupAsciiSymbol(Handle<SeqAsciiString> string,
158 int from,
159 int length) {
160 CALL_HEAP_FUNCTION(isolate(),
161 isolate()->heap()->LookupAsciiSymbol(string,
162 from,
163 length),
164 String);
165}
166
167
Steve Block9fac8402011-05-12 15:51:54 +0100168Handle<String> Factory::LookupTwoByteSymbol(Vector<const uc16> string) {
Steve Block44f0eee2011-05-26 01:26:41 +0100169 CALL_HEAP_FUNCTION(isolate(),
170 isolate()->heap()->LookupTwoByteSymbol(string),
171 String);
Steve Block9fac8402011-05-12 15:51:54 +0100172}
173
Steve Blocka7e24c12009-10-30 11:49:00 +0000174
175Handle<String> Factory::NewStringFromAscii(Vector<const char> string,
176 PretenureFlag pretenure) {
Steve Block44f0eee2011-05-26 01:26:41 +0100177 CALL_HEAP_FUNCTION(
178 isolate(),
179 isolate()->heap()->AllocateStringFromAscii(string, pretenure),
180 String);
Steve Blocka7e24c12009-10-30 11:49:00 +0000181}
182
183Handle<String> Factory::NewStringFromUtf8(Vector<const char> string,
184 PretenureFlag pretenure) {
Steve Block44f0eee2011-05-26 01:26:41 +0100185 CALL_HEAP_FUNCTION(
186 isolate(),
187 isolate()->heap()->AllocateStringFromUtf8(string, pretenure),
188 String);
Steve Blocka7e24c12009-10-30 11:49:00 +0000189}
190
191
192Handle<String> Factory::NewStringFromTwoByte(Vector<const uc16> string,
193 PretenureFlag pretenure) {
Steve Block44f0eee2011-05-26 01:26:41 +0100194 CALL_HEAP_FUNCTION(
195 isolate(),
196 isolate()->heap()->AllocateStringFromTwoByte(string, pretenure),
197 String);
Steve Blocka7e24c12009-10-30 11:49:00 +0000198}
199
200
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000201Handle<SeqAsciiString> Factory::NewRawAsciiString(int length,
202 PretenureFlag pretenure) {
Steve Block44f0eee2011-05-26 01:26:41 +0100203 CALL_HEAP_FUNCTION(
204 isolate(),
205 isolate()->heap()->AllocateRawAsciiString(length, pretenure),
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000206 SeqAsciiString);
Leon Clarkeac952652010-07-15 11:15:24 +0100207}
208
209
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000210Handle<SeqTwoByteString> Factory::NewRawTwoByteString(int length,
211 PretenureFlag pretenure) {
Steve Block44f0eee2011-05-26 01:26:41 +0100212 CALL_HEAP_FUNCTION(
213 isolate(),
214 isolate()->heap()->AllocateRawTwoByteString(length, pretenure),
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000215 SeqTwoByteString);
Steve Blocka7e24c12009-10-30 11:49:00 +0000216}
217
218
219Handle<String> Factory::NewConsString(Handle<String> first,
220 Handle<String> second) {
Steve Block44f0eee2011-05-26 01:26:41 +0100221 CALL_HEAP_FUNCTION(isolate(),
222 isolate()->heap()->AllocateConsString(*first, *second),
223 String);
Steve Blocka7e24c12009-10-30 11:49:00 +0000224}
225
226
Steve Blockd0582a62009-12-15 09:54:21 +0000227Handle<String> Factory::NewSubString(Handle<String> str,
228 int begin,
229 int end) {
Steve Block44f0eee2011-05-26 01:26:41 +0100230 CALL_HEAP_FUNCTION(isolate(),
231 str->SubString(begin, end),
232 String);
Steve Blocka7e24c12009-10-30 11:49:00 +0000233}
234
235
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000236Handle<String> Factory::NewProperSubString(Handle<String> str,
237 int begin,
238 int end) {
239 ASSERT(begin > 0 || end < str->length());
240 CALL_HEAP_FUNCTION(isolate(),
241 isolate()->heap()->AllocateSubString(*str, begin, end),
242 String);
243}
244
245
Steve Blocka7e24c12009-10-30 11:49:00 +0000246Handle<String> Factory::NewExternalStringFromAscii(
247 ExternalAsciiString::Resource* resource) {
Steve Block44f0eee2011-05-26 01:26:41 +0100248 CALL_HEAP_FUNCTION(
249 isolate(),
250 isolate()->heap()->AllocateExternalStringFromAscii(resource),
251 String);
Steve Blocka7e24c12009-10-30 11:49:00 +0000252}
253
254
255Handle<String> Factory::NewExternalStringFromTwoByte(
256 ExternalTwoByteString::Resource* resource) {
Steve Block44f0eee2011-05-26 01:26:41 +0100257 CALL_HEAP_FUNCTION(
258 isolate(),
259 isolate()->heap()->AllocateExternalStringFromTwoByte(resource),
260 String);
Steve Blocka7e24c12009-10-30 11:49:00 +0000261}
262
263
264Handle<Context> Factory::NewGlobalContext() {
Steve Block44f0eee2011-05-26 01:26:41 +0100265 CALL_HEAP_FUNCTION(
266 isolate(),
267 isolate()->heap()->AllocateGlobalContext(),
268 Context);
Steve Blocka7e24c12009-10-30 11:49:00 +0000269}
270
271
272Handle<Context> Factory::NewFunctionContext(int length,
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000273 Handle<JSFunction> function) {
Steve Block44f0eee2011-05-26 01:26:41 +0100274 CALL_HEAP_FUNCTION(
275 isolate(),
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000276 isolate()->heap()->AllocateFunctionContext(length, *function),
Steve Block44f0eee2011-05-26 01:26:41 +0100277 Context);
Steve Blocka7e24c12009-10-30 11:49:00 +0000278}
279
280
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000281Handle<Context> Factory::NewCatchContext(Handle<JSFunction> function,
282 Handle<Context> previous,
283 Handle<String> name,
284 Handle<Object> thrown_object) {
Steve Block44f0eee2011-05-26 01:26:41 +0100285 CALL_HEAP_FUNCTION(
286 isolate(),
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000287 isolate()->heap()->AllocateCatchContext(*function,
288 *previous,
289 *name,
290 *thrown_object),
291 Context);
292}
293
294
295Handle<Context> Factory::NewWithContext(Handle<JSFunction> function,
296 Handle<Context> previous,
297 Handle<JSObject> extension) {
298 CALL_HEAP_FUNCTION(
299 isolate(),
300 isolate()->heap()->AllocateWithContext(*function, *previous, *extension),
Steve Block44f0eee2011-05-26 01:26:41 +0100301 Context);
Steve Blocka7e24c12009-10-30 11:49:00 +0000302}
303
304
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000305Handle<Context> Factory::NewBlockContext(
306 Handle<JSFunction> function,
307 Handle<Context> previous,
308 Handle<SerializedScopeInfo> scope_info) {
309 CALL_HEAP_FUNCTION(
310 isolate(),
311 isolate()->heap()->AllocateBlockContext(*function,
312 *previous,
313 *scope_info),
314 Context);
315}
316
317
Steve Blocka7e24c12009-10-30 11:49:00 +0000318Handle<Struct> Factory::NewStruct(InstanceType type) {
Steve Block44f0eee2011-05-26 01:26:41 +0100319 CALL_HEAP_FUNCTION(
320 isolate(),
321 isolate()->heap()->AllocateStruct(type),
322 Struct);
Steve Blocka7e24c12009-10-30 11:49:00 +0000323}
324
325
326Handle<AccessorInfo> Factory::NewAccessorInfo() {
327 Handle<AccessorInfo> info =
328 Handle<AccessorInfo>::cast(NewStruct(ACCESSOR_INFO_TYPE));
329 info->set_flag(0); // Must clear the flag, it was initialized as undefined.
330 return info;
331}
332
333
334Handle<Script> Factory::NewScript(Handle<String> source) {
335 // Generate id for this script.
336 int id;
Steve Block44f0eee2011-05-26 01:26:41 +0100337 Heap* heap = isolate()->heap();
338 if (heap->last_script_id()->IsUndefined()) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000339 // Script ids start from one.
340 id = 1;
341 } else {
342 // Increment id, wrap when positive smi is exhausted.
Steve Block44f0eee2011-05-26 01:26:41 +0100343 id = Smi::cast(heap->last_script_id())->value();
Steve Blocka7e24c12009-10-30 11:49:00 +0000344 id++;
345 if (!Smi::IsValid(id)) {
346 id = 0;
347 }
348 }
Steve Block44f0eee2011-05-26 01:26:41 +0100349 heap->SetLastScriptId(Smi::FromInt(id));
Steve Blocka7e24c12009-10-30 11:49:00 +0000350
351 // Create and initialize script object.
Ben Murdoch257744e2011-11-30 15:57:28 +0000352 Handle<Foreign> wrapper = NewForeign(0, TENURED);
Steve Blocka7e24c12009-10-30 11:49:00 +0000353 Handle<Script> script = Handle<Script>::cast(NewStruct(SCRIPT_TYPE));
354 script->set_source(*source);
Steve Block44f0eee2011-05-26 01:26:41 +0100355 script->set_name(heap->undefined_value());
356 script->set_id(heap->last_script_id());
Steve Blocka7e24c12009-10-30 11:49:00 +0000357 script->set_line_offset(Smi::FromInt(0));
358 script->set_column_offset(Smi::FromInt(0));
Steve Block44f0eee2011-05-26 01:26:41 +0100359 script->set_data(heap->undefined_value());
360 script->set_context_data(heap->undefined_value());
Steve Blocka7e24c12009-10-30 11:49:00 +0000361 script->set_type(Smi::FromInt(Script::TYPE_NORMAL));
362 script->set_compilation_type(Smi::FromInt(Script::COMPILATION_TYPE_HOST));
363 script->set_wrapper(*wrapper);
Steve Block44f0eee2011-05-26 01:26:41 +0100364 script->set_line_ends(heap->undefined_value());
365 script->set_eval_from_shared(heap->undefined_value());
Steve Blocka7e24c12009-10-30 11:49:00 +0000366 script->set_eval_from_instructions_offset(Smi::FromInt(0));
367
368 return script;
369}
370
371
Ben Murdoch257744e2011-11-30 15:57:28 +0000372Handle<Foreign> Factory::NewForeign(Address addr, PretenureFlag pretenure) {
Steve Block44f0eee2011-05-26 01:26:41 +0100373 CALL_HEAP_FUNCTION(isolate(),
Ben Murdoch257744e2011-11-30 15:57:28 +0000374 isolate()->heap()->AllocateForeign(addr, pretenure),
375 Foreign);
Steve Blocka7e24c12009-10-30 11:49:00 +0000376}
377
378
Ben Murdoch257744e2011-11-30 15:57:28 +0000379Handle<Foreign> Factory::NewForeign(const AccessorDescriptor* desc) {
380 return NewForeign((Address) desc, TENURED);
Steve Blocka7e24c12009-10-30 11:49:00 +0000381}
382
383
384Handle<ByteArray> Factory::NewByteArray(int length, PretenureFlag pretenure) {
385 ASSERT(0 <= length);
Steve Block44f0eee2011-05-26 01:26:41 +0100386 CALL_HEAP_FUNCTION(
387 isolate(),
388 isolate()->heap()->AllocateByteArray(length, pretenure),
389 ByteArray);
Steve Blocka7e24c12009-10-30 11:49:00 +0000390}
391
392
Steve Block3ce2e202009-11-05 08:53:23 +0000393Handle<ExternalArray> Factory::NewExternalArray(int length,
394 ExternalArrayType array_type,
395 void* external_pointer,
396 PretenureFlag pretenure) {
397 ASSERT(0 <= length);
Steve Block44f0eee2011-05-26 01:26:41 +0100398 CALL_HEAP_FUNCTION(
399 isolate(),
400 isolate()->heap()->AllocateExternalArray(length,
401 array_type,
402 external_pointer,
403 pretenure),
404 ExternalArray);
Steve Block3ce2e202009-11-05 08:53:23 +0000405}
406
407
Ben Murdochb0fe1622011-05-05 13:52:32 +0100408Handle<JSGlobalPropertyCell> Factory::NewJSGlobalPropertyCell(
409 Handle<Object> value) {
Steve Block44f0eee2011-05-26 01:26:41 +0100410 CALL_HEAP_FUNCTION(
411 isolate(),
412 isolate()->heap()->AllocateJSGlobalPropertyCell(*value),
413 JSGlobalPropertyCell);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100414}
415
416
Steve Blocka7e24c12009-10-30 11:49:00 +0000417Handle<Map> Factory::NewMap(InstanceType type, int instance_size) {
Steve Block44f0eee2011-05-26 01:26:41 +0100418 CALL_HEAP_FUNCTION(
419 isolate(),
420 isolate()->heap()->AllocateMap(type, instance_size),
421 Map);
Steve Blocka7e24c12009-10-30 11:49:00 +0000422}
423
424
425Handle<JSObject> Factory::NewFunctionPrototype(Handle<JSFunction> function) {
Steve Block44f0eee2011-05-26 01:26:41 +0100426 CALL_HEAP_FUNCTION(
427 isolate(),
428 isolate()->heap()->AllocateFunctionPrototype(*function),
429 JSObject);
Steve Blocka7e24c12009-10-30 11:49:00 +0000430}
431
432
433Handle<Map> Factory::CopyMapDropDescriptors(Handle<Map> src) {
Steve Block44f0eee2011-05-26 01:26:41 +0100434 CALL_HEAP_FUNCTION(isolate(), src->CopyDropDescriptors(), Map);
Steve Blocka7e24c12009-10-30 11:49:00 +0000435}
436
437
438Handle<Map> Factory::CopyMap(Handle<Map> src,
439 int extra_inobject_properties) {
440 Handle<Map> copy = CopyMapDropDescriptors(src);
441 // Check that we do not overflow the instance size when adding the
442 // extra inobject properties.
443 int instance_size_delta = extra_inobject_properties * kPointerSize;
444 int max_instance_size_delta =
445 JSObject::kMaxInstanceSize - copy->instance_size();
446 if (instance_size_delta > max_instance_size_delta) {
447 // If the instance size overflows, we allocate as many properties
448 // as we can as inobject properties.
449 instance_size_delta = max_instance_size_delta;
450 extra_inobject_properties = max_instance_size_delta >> kPointerSizeLog2;
451 }
452 // Adjust the map with the extra inobject properties.
453 int inobject_properties =
454 copy->inobject_properties() + extra_inobject_properties;
455 copy->set_inobject_properties(inobject_properties);
456 copy->set_unused_property_fields(inobject_properties);
457 copy->set_instance_size(copy->instance_size() + instance_size_delta);
Iain Merrick75681382010-08-19 15:07:18 +0100458 copy->set_visitor_id(StaticVisitorBase::GetVisitorId(*copy));
Steve Blocka7e24c12009-10-30 11:49:00 +0000459 return copy;
460}
461
Steve Block8defd9f2010-07-08 12:39:36 +0100462
Steve Blocka7e24c12009-10-30 11:49:00 +0000463Handle<Map> Factory::CopyMapDropTransitions(Handle<Map> src) {
Steve Block44f0eee2011-05-26 01:26:41 +0100464 CALL_HEAP_FUNCTION(isolate(), src->CopyDropTransitions(), Map);
Steve Blocka7e24c12009-10-30 11:49:00 +0000465}
466
467
Steve Block8defd9f2010-07-08 12:39:36 +0100468Handle<Map> Factory::GetFastElementsMap(Handle<Map> src) {
Steve Block44f0eee2011-05-26 01:26:41 +0100469 CALL_HEAP_FUNCTION(isolate(), src->GetFastElementsMap(), Map);
Steve Block8defd9f2010-07-08 12:39:36 +0100470}
471
472
473Handle<Map> Factory::GetSlowElementsMap(Handle<Map> src) {
Steve Block44f0eee2011-05-26 01:26:41 +0100474 CALL_HEAP_FUNCTION(isolate(), src->GetSlowElementsMap(), Map);
Steve Block8defd9f2010-07-08 12:39:36 +0100475}
476
477
Ben Murdoch589d6972011-11-30 16:04:58 +0000478Handle<Map> Factory::GetElementsTransitionMap(
Steve Block44f0eee2011-05-26 01:26:41 +0100479 Handle<Map> src,
Ben Murdoch589d6972011-11-30 16:04:58 +0000480 ElementsKind elements_kind,
Steve Block44f0eee2011-05-26 01:26:41 +0100481 bool safe_to_add_transition) {
482 CALL_HEAP_FUNCTION(isolate(),
Ben Murdoch589d6972011-11-30 16:04:58 +0000483 src->GetElementsTransitionMap(elements_kind,
484 safe_to_add_transition),
Steve Block44f0eee2011-05-26 01:26:41 +0100485 Map);
Steve Block1e0659c2011-05-24 12:43:12 +0100486}
487
488
Steve Blocka7e24c12009-10-30 11:49:00 +0000489Handle<FixedArray> Factory::CopyFixedArray(Handle<FixedArray> array) {
Steve Block44f0eee2011-05-26 01:26:41 +0100490 CALL_HEAP_FUNCTION(isolate(), array->Copy(), FixedArray);
Steve Blocka7e24c12009-10-30 11:49:00 +0000491}
492
493
Steve Block6ded16b2010-05-10 14:33:55 +0100494Handle<JSFunction> Factory::BaseNewFunctionFromSharedFunctionInfo(
495 Handle<SharedFunctionInfo> function_info,
Leon Clarkee46be812010-01-19 14:06:41 +0000496 Handle<Map> function_map,
497 PretenureFlag pretenure) {
Steve Block44f0eee2011-05-26 01:26:41 +0100498 CALL_HEAP_FUNCTION(
499 isolate(),
500 isolate()->heap()->AllocateFunction(*function_map,
501 *function_info,
502 isolate()->heap()->the_hole_value(),
503 pretenure),
Steve Blocka7e24c12009-10-30 11:49:00 +0000504 JSFunction);
505}
506
507
Steve Block6ded16b2010-05-10 14:33:55 +0100508Handle<JSFunction> Factory::NewFunctionFromSharedFunctionInfo(
509 Handle<SharedFunctionInfo> function_info,
Leon Clarkee46be812010-01-19 14:06:41 +0000510 Handle<Context> context,
511 PretenureFlag pretenure) {
Steve Block6ded16b2010-05-10 14:33:55 +0100512 Handle<JSFunction> result = BaseNewFunctionFromSharedFunctionInfo(
Steve Block44f0eee2011-05-26 01:26:41 +0100513 function_info,
514 function_info->strict_mode()
515 ? isolate()->strict_mode_function_map()
516 : isolate()->function_map(),
517 pretenure);
518
Steve Blocka7e24c12009-10-30 11:49:00 +0000519 result->set_context(*context);
Steve Block6ded16b2010-05-10 14:33:55 +0100520 int number_of_literals = function_info->num_literals();
Steve Block44f0eee2011-05-26 01:26:41 +0100521 Handle<FixedArray> literals = NewFixedArray(number_of_literals, pretenure);
Steve Blocka7e24c12009-10-30 11:49:00 +0000522 if (number_of_literals > 0) {
523 // Store the object, regexp and array functions in the literals
524 // array prefix. These functions will be used when creating
525 // object, regexp and array literals in this function.
526 literals->set(JSFunction::kLiteralGlobalContextIndex,
527 context->global_context());
528 }
529 result->set_literals(*literals);
Steve Block44f0eee2011-05-26 01:26:41 +0100530 result->set_next_function_link(isolate()->heap()->undefined_value());
Ben Murdochb0fe1622011-05-05 13:52:32 +0100531
532 if (V8::UseCrankshaft() &&
533 FLAG_always_opt &&
534 result->is_compiled() &&
535 !function_info->is_toplevel() &&
536 function_info->allows_lazy_compilation()) {
537 result->MarkForLazyRecompilation();
538 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000539 return result;
540}
541
542
543Handle<Object> Factory::NewNumber(double value,
544 PretenureFlag pretenure) {
Steve Block44f0eee2011-05-26 01:26:41 +0100545 CALL_HEAP_FUNCTION(
546 isolate(),
547 isolate()->heap()->NumberFromDouble(value, pretenure), Object);
Steve Blocka7e24c12009-10-30 11:49:00 +0000548}
549
550
551Handle<Object> Factory::NewNumberFromInt(int value) {
Steve Block44f0eee2011-05-26 01:26:41 +0100552 CALL_HEAP_FUNCTION(
553 isolate(),
554 isolate()->heap()->NumberFromInt32(value), Object);
Steve Blocka7e24c12009-10-30 11:49:00 +0000555}
556
557
558Handle<Object> Factory::NewNumberFromUint(uint32_t value) {
Steve Block44f0eee2011-05-26 01:26:41 +0100559 CALL_HEAP_FUNCTION(
560 isolate(),
561 isolate()->heap()->NumberFromUint32(value), Object);
Steve Blocka7e24c12009-10-30 11:49:00 +0000562}
563
564
565Handle<JSObject> Factory::NewNeanderObject() {
Steve Block44f0eee2011-05-26 01:26:41 +0100566 CALL_HEAP_FUNCTION(
567 isolate(),
568 isolate()->heap()->AllocateJSObjectFromMap(
569 isolate()->heap()->neander_map()),
570 JSObject);
Steve Blocka7e24c12009-10-30 11:49:00 +0000571}
572
573
574Handle<Object> Factory::NewTypeError(const char* type,
575 Vector< Handle<Object> > args) {
576 return NewError("MakeTypeError", type, args);
577}
578
579
580Handle<Object> Factory::NewTypeError(Handle<String> message) {
581 return NewError("$TypeError", message);
582}
583
584
585Handle<Object> Factory::NewRangeError(const char* type,
586 Vector< Handle<Object> > args) {
587 return NewError("MakeRangeError", type, args);
588}
589
590
591Handle<Object> Factory::NewRangeError(Handle<String> message) {
592 return NewError("$RangeError", message);
593}
594
595
596Handle<Object> Factory::NewSyntaxError(const char* type, Handle<JSArray> args) {
597 return NewError("MakeSyntaxError", type, args);
598}
599
600
601Handle<Object> Factory::NewSyntaxError(Handle<String> message) {
602 return NewError("$SyntaxError", message);
603}
604
605
606Handle<Object> Factory::NewReferenceError(const char* type,
607 Vector< Handle<Object> > args) {
608 return NewError("MakeReferenceError", type, args);
609}
610
611
612Handle<Object> Factory::NewReferenceError(Handle<String> message) {
613 return NewError("$ReferenceError", message);
614}
615
616
617Handle<Object> Factory::NewError(const char* maker, const char* type,
618 Vector< Handle<Object> > args) {
619 v8::HandleScope scope; // Instantiate a closeable HandleScope for EscapeFrom.
Steve Block44f0eee2011-05-26 01:26:41 +0100620 Handle<FixedArray> array = NewFixedArray(args.length());
Steve Blocka7e24c12009-10-30 11:49:00 +0000621 for (int i = 0; i < args.length(); i++) {
622 array->set(i, *args[i]);
623 }
Steve Block44f0eee2011-05-26 01:26:41 +0100624 Handle<JSArray> object = NewJSArrayWithElements(array);
Steve Blocka7e24c12009-10-30 11:49:00 +0000625 Handle<Object> result = NewError(maker, type, object);
626 return result.EscapeFrom(&scope);
627}
628
629
630Handle<Object> Factory::NewEvalError(const char* type,
631 Vector< Handle<Object> > args) {
632 return NewError("MakeEvalError", type, args);
633}
634
635
636Handle<Object> Factory::NewError(const char* type,
637 Vector< Handle<Object> > args) {
638 return NewError("MakeError", type, args);
639}
640
641
642Handle<Object> Factory::NewError(const char* maker,
643 const char* type,
644 Handle<JSArray> args) {
Steve Block44f0eee2011-05-26 01:26:41 +0100645 Handle<String> make_str = LookupAsciiSymbol(maker);
646 Handle<Object> fun_obj(
647 isolate()->js_builtins_object()->GetPropertyNoExceptionThrown(*make_str));
Steve Blocka7e24c12009-10-30 11:49:00 +0000648 // If the builtins haven't been properly configured yet this error
649 // constructor may not have been defined. Bail out.
650 if (!fun_obj->IsJSFunction())
Steve Block44f0eee2011-05-26 01:26:41 +0100651 return undefined_value();
Steve Blocka7e24c12009-10-30 11:49:00 +0000652 Handle<JSFunction> fun = Handle<JSFunction>::cast(fun_obj);
Steve Block44f0eee2011-05-26 01:26:41 +0100653 Handle<Object> type_obj = LookupAsciiSymbol(type);
Steve Blocka7e24c12009-10-30 11:49:00 +0000654 Object** argv[2] = { type_obj.location(),
655 Handle<Object>::cast(args).location() };
656
657 // Invoke the JavaScript factory method. If an exception is thrown while
658 // running the factory method, use the exception as the result.
659 bool caught_exception;
660 Handle<Object> result = Execution::TryCall(fun,
Steve Block44f0eee2011-05-26 01:26:41 +0100661 isolate()->js_builtins_object(), 2, argv, &caught_exception);
Steve Blocka7e24c12009-10-30 11:49:00 +0000662 return result;
663}
664
665
666Handle<Object> Factory::NewError(Handle<String> message) {
667 return NewError("$Error", message);
668}
669
670
671Handle<Object> Factory::NewError(const char* constructor,
672 Handle<String> message) {
Steve Block44f0eee2011-05-26 01:26:41 +0100673 Handle<String> constr = LookupAsciiSymbol(constructor);
674 Handle<JSFunction> fun = Handle<JSFunction>(
675 JSFunction::cast(isolate()->js_builtins_object()->
676 GetPropertyNoExceptionThrown(*constr)));
Steve Blocka7e24c12009-10-30 11:49:00 +0000677 Object** argv[1] = { Handle<Object>::cast(message).location() };
678
679 // Invoke the JavaScript factory method. If an exception is thrown while
680 // running the factory method, use the exception as the result.
681 bool caught_exception;
682 Handle<Object> result = Execution::TryCall(fun,
Steve Block44f0eee2011-05-26 01:26:41 +0100683 isolate()->js_builtins_object(), 1, argv, &caught_exception);
Steve Blocka7e24c12009-10-30 11:49:00 +0000684 return result;
685}
686
687
688Handle<JSFunction> Factory::NewFunction(Handle<String> name,
689 InstanceType type,
690 int instance_size,
691 Handle<Code> code,
692 bool force_initial_map) {
693 // Allocate the function
694 Handle<JSFunction> function = NewFunction(name, the_hole_value());
Iain Merrick75681382010-08-19 15:07:18 +0100695
696 // Setup the code pointer in both the shared function info and in
697 // the function itself.
698 function->shared()->set_code(*code);
Steve Blocka7e24c12009-10-30 11:49:00 +0000699 function->set_code(*code);
700
701 if (force_initial_map ||
702 type != JS_OBJECT_TYPE ||
703 instance_size != JSObject::kHeaderSize) {
704 Handle<Map> initial_map = NewMap(type, instance_size);
705 Handle<JSObject> prototype = NewFunctionPrototype(function);
706 initial_map->set_prototype(*prototype);
707 function->set_initial_map(*initial_map);
708 initial_map->set_constructor(*function);
709 } else {
710 ASSERT(!function->has_initial_map());
711 ASSERT(!function->has_prototype());
712 }
713
714 return function;
715}
716
717
Steve Blocka7e24c12009-10-30 11:49:00 +0000718Handle<JSFunction> Factory::NewFunctionWithPrototype(Handle<String> name,
719 InstanceType type,
720 int instance_size,
721 Handle<JSObject> prototype,
722 Handle<Code> code,
723 bool force_initial_map) {
Iain Merrick75681382010-08-19 15:07:18 +0100724 // Allocate the function.
Steve Blocka7e24c12009-10-30 11:49:00 +0000725 Handle<JSFunction> function = NewFunction(name, prototype);
726
Iain Merrick75681382010-08-19 15:07:18 +0100727 // Setup the code pointer in both the shared function info and in
728 // the function itself.
729 function->shared()->set_code(*code);
Steve Blocka7e24c12009-10-30 11:49:00 +0000730 function->set_code(*code);
731
732 if (force_initial_map ||
733 type != JS_OBJECT_TYPE ||
734 instance_size != JSObject::kHeaderSize) {
735 Handle<Map> initial_map = NewMap(type, instance_size);
736 function->set_initial_map(*initial_map);
737 initial_map->set_constructor(*function);
738 }
739
740 // Set function.prototype and give the prototype a constructor
741 // property that refers to the function.
742 SetPrototypeProperty(function, prototype);
Steve Block1e0659c2011-05-24 12:43:12 +0100743 // Currently safe because it is only invoked from Genesis.
Steve Block44f0eee2011-05-26 01:26:41 +0100744 SetLocalPropertyNoThrow(prototype, constructor_symbol(), function, DONT_ENUM);
Steve Blocka7e24c12009-10-30 11:49:00 +0000745 return function;
746}
747
748
Steve Block6ded16b2010-05-10 14:33:55 +0100749Handle<JSFunction> Factory::NewFunctionWithoutPrototype(Handle<String> name,
750 Handle<Code> code) {
Steve Block44f0eee2011-05-26 01:26:41 +0100751 Handle<JSFunction> function = NewFunctionWithoutPrototype(name,
752 kNonStrictMode);
Iain Merrick75681382010-08-19 15:07:18 +0100753 function->shared()->set_code(*code);
Steve Block6ded16b2010-05-10 14:33:55 +0100754 function->set_code(*code);
755 ASSERT(!function->has_initial_map());
756 ASSERT(!function->has_prototype());
757 return function;
758}
759
760
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000761Handle<SerializedScopeInfo> Factory::NewSerializedScopeInfo(int length) {
762 CALL_HEAP_FUNCTION(
763 isolate(),
764 isolate()->heap()->AllocateSerializedScopeInfo(length),
765 SerializedScopeInfo);
766}
767
768
Steve Blocka7e24c12009-10-30 11:49:00 +0000769Handle<Code> Factory::NewCode(const CodeDesc& desc,
Steve Blocka7e24c12009-10-30 11:49:00 +0000770 Code::Flags flags,
Steve Block44f0eee2011-05-26 01:26:41 +0100771 Handle<Object> self_ref,
772 bool immovable) {
773 CALL_HEAP_FUNCTION(isolate(),
774 isolate()->heap()->CreateCode(
775 desc, flags, self_ref, immovable),
776 Code);
Steve Blocka7e24c12009-10-30 11:49:00 +0000777}
778
779
780Handle<Code> Factory::CopyCode(Handle<Code> code) {
Steve Block44f0eee2011-05-26 01:26:41 +0100781 CALL_HEAP_FUNCTION(isolate(),
782 isolate()->heap()->CopyCode(*code),
783 Code);
Steve Blocka7e24c12009-10-30 11:49:00 +0000784}
785
786
Steve Block6ded16b2010-05-10 14:33:55 +0100787Handle<Code> Factory::CopyCode(Handle<Code> code, Vector<byte> reloc_info) {
Steve Block44f0eee2011-05-26 01:26:41 +0100788 CALL_HEAP_FUNCTION(isolate(),
789 isolate()->heap()->CopyCode(*code, reloc_info),
790 Code);
Steve Block6ded16b2010-05-10 14:33:55 +0100791}
792
793
John Reck59135872010-11-02 12:39:01 -0700794MUST_USE_RESULT static inline MaybeObject* DoCopyInsert(
795 DescriptorArray* array,
796 String* key,
797 Object* value,
798 PropertyAttributes attributes) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000799 CallbacksDescriptor desc(key, value, attributes);
John Reck59135872010-11-02 12:39:01 -0700800 MaybeObject* obj = array->CopyInsert(&desc, REMOVE_TRANSITIONS);
Steve Blocka7e24c12009-10-30 11:49:00 +0000801 return obj;
802}
803
804
805// Allocate the new array.
Ben Murdoch257744e2011-11-30 15:57:28 +0000806Handle<DescriptorArray> Factory::CopyAppendForeignDescriptor(
Steve Blocka7e24c12009-10-30 11:49:00 +0000807 Handle<DescriptorArray> array,
808 Handle<String> key,
809 Handle<Object> value,
810 PropertyAttributes attributes) {
Steve Block44f0eee2011-05-26 01:26:41 +0100811 CALL_HEAP_FUNCTION(isolate(),
812 DoCopyInsert(*array, *key, *value, attributes),
Steve Blocka7e24c12009-10-30 11:49:00 +0000813 DescriptorArray);
814}
815
816
817Handle<String> Factory::SymbolFromString(Handle<String> value) {
Steve Block44f0eee2011-05-26 01:26:41 +0100818 CALL_HEAP_FUNCTION(isolate(),
819 isolate()->heap()->LookupSymbol(*value), String);
Steve Blocka7e24c12009-10-30 11:49:00 +0000820}
821
822
823Handle<DescriptorArray> Factory::CopyAppendCallbackDescriptors(
824 Handle<DescriptorArray> array,
825 Handle<Object> descriptors) {
826 v8::NeanderArray callbacks(descriptors);
827 int nof_callbacks = callbacks.length();
828 Handle<DescriptorArray> result =
829 NewDescriptorArray(array->number_of_descriptors() + nof_callbacks);
830
831 // Number of descriptors added to the result so far.
832 int descriptor_count = 0;
833
834 // Copy the descriptors from the array.
835 for (int i = 0; i < array->number_of_descriptors(); i++) {
836 if (array->GetType(i) != NULL_DESCRIPTOR) {
837 result->CopyFrom(descriptor_count++, *array, i);
838 }
839 }
840
841 // Number of duplicates detected.
842 int duplicates = 0;
843
844 // Fill in new callback descriptors. Process the callbacks from
845 // back to front so that the last callback with a given name takes
846 // precedence over previously added callbacks with that name.
847 for (int i = nof_callbacks - 1; i >= 0; i--) {
848 Handle<AccessorInfo> entry =
849 Handle<AccessorInfo>(AccessorInfo::cast(callbacks.get(i)));
850 // Ensure the key is a symbol before writing into the instance descriptor.
851 Handle<String> key =
852 SymbolFromString(Handle<String>(String::cast(entry->name())));
853 // Check if a descriptor with this name already exists before writing.
854 if (result->LinearSearch(*key, descriptor_count) ==
855 DescriptorArray::kNotFound) {
856 CallbacksDescriptor desc(*key, *entry, entry->property_attributes());
857 result->Set(descriptor_count, &desc);
858 descriptor_count++;
859 } else {
860 duplicates++;
861 }
862 }
863
864 // If duplicates were detected, allocate a result of the right size
865 // and transfer the elements.
866 if (duplicates > 0) {
867 int number_of_descriptors = result->number_of_descriptors() - duplicates;
868 Handle<DescriptorArray> new_result =
869 NewDescriptorArray(number_of_descriptors);
870 for (int i = 0; i < number_of_descriptors; i++) {
871 new_result->CopyFrom(i, *result, i);
872 }
873 result = new_result;
874 }
875
876 // Sort the result before returning.
877 result->Sort();
878 return result;
879}
880
881
882Handle<JSObject> Factory::NewJSObject(Handle<JSFunction> constructor,
883 PretenureFlag pretenure) {
Steve Block44f0eee2011-05-26 01:26:41 +0100884 CALL_HEAP_FUNCTION(
885 isolate(),
886 isolate()->heap()->AllocateJSObject(*constructor, pretenure), JSObject);
Steve Blocka7e24c12009-10-30 11:49:00 +0000887}
888
889
890Handle<GlobalObject> Factory::NewGlobalObject(
891 Handle<JSFunction> constructor) {
Steve Block44f0eee2011-05-26 01:26:41 +0100892 CALL_HEAP_FUNCTION(isolate(),
893 isolate()->heap()->AllocateGlobalObject(*constructor),
Steve Blocka7e24c12009-10-30 11:49:00 +0000894 GlobalObject);
895}
896
897
898
899Handle<JSObject> Factory::NewJSObjectFromMap(Handle<Map> map) {
Steve Block44f0eee2011-05-26 01:26:41 +0100900 CALL_HEAP_FUNCTION(
901 isolate(),
902 isolate()->heap()->AllocateJSObjectFromMap(*map, NOT_TENURED),
903 JSObject);
Steve Blocka7e24c12009-10-30 11:49:00 +0000904}
905
906
Steve Block44f0eee2011-05-26 01:26:41 +0100907Handle<JSArray> Factory::NewJSArray(int capacity,
Steve Blocka7e24c12009-10-30 11:49:00 +0000908 PretenureFlag pretenure) {
Steve Block44f0eee2011-05-26 01:26:41 +0100909 Handle<JSObject> obj = NewJSObject(isolate()->array_function(), pretenure);
910 CALL_HEAP_FUNCTION(isolate(),
911 Handle<JSArray>::cast(obj)->Initialize(capacity),
912 JSArray);
Steve Blocka7e24c12009-10-30 11:49:00 +0000913}
914
915
916Handle<JSArray> Factory::NewJSArrayWithElements(Handle<FixedArray> elements,
917 PretenureFlag pretenure) {
918 Handle<JSArray> result =
Steve Block44f0eee2011-05-26 01:26:41 +0100919 Handle<JSArray>::cast(NewJSObject(isolate()->array_function(),
920 pretenure));
Steve Blocka7e24c12009-10-30 11:49:00 +0000921 result->SetContent(*elements);
922 return result;
923}
924
925
Ben Murdoch257744e2011-11-30 15:57:28 +0000926Handle<JSProxy> Factory::NewJSProxy(Handle<Object> handler,
927 Handle<Object> prototype) {
928 CALL_HEAP_FUNCTION(
929 isolate(),
930 isolate()->heap()->AllocateJSProxy(*handler, *prototype),
931 JSProxy);
932}
933
934
Ben Murdoch589d6972011-11-30 16:04:58 +0000935void Factory::BecomeJSObject(Handle<JSReceiver> object) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000936 CALL_HEAP_FUNCTION_VOID(
937 isolate(),
Ben Murdoch589d6972011-11-30 16:04:58 +0000938 isolate()->heap()->ReinitializeJSReceiver(
939 *object, JS_OBJECT_TYPE, JSObject::kHeaderSize));
940}
941
942
943void Factory::BecomeJSFunction(Handle<JSReceiver> object) {
944 CALL_HEAP_FUNCTION_VOID(
945 isolate(),
946 isolate()->heap()->ReinitializeJSReceiver(
947 *object, JS_FUNCTION_TYPE, JSFunction::kSize));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000948}
949
950
Steve Block6ded16b2010-05-10 14:33:55 +0100951Handle<SharedFunctionInfo> Factory::NewSharedFunctionInfo(
Ben Murdoch3bec4d22010-07-22 14:51:16 +0100952 Handle<String> name,
953 int number_of_literals,
954 Handle<Code> code,
955 Handle<SerializedScopeInfo> scope_info) {
Steve Block6ded16b2010-05-10 14:33:55 +0100956 Handle<SharedFunctionInfo> shared = NewSharedFunctionInfo(name);
957 shared->set_code(*code);
Ben Murdoch3bec4d22010-07-22 14:51:16 +0100958 shared->set_scope_info(*scope_info);
Steve Block6ded16b2010-05-10 14:33:55 +0100959 int literals_array_size = number_of_literals;
960 // If the function contains object, regexp or array literals,
961 // allocate extra space for a literals array prefix containing the
962 // context.
963 if (number_of_literals > 0) {
964 literals_array_size += JSFunction::kLiteralsPrefixSize;
965 }
966 shared->set_num_literals(literals_array_size);
967 return shared;
968}
969
970
Steve Block1e0659c2011-05-24 12:43:12 +0100971Handle<JSMessageObject> Factory::NewJSMessageObject(
972 Handle<String> type,
973 Handle<JSArray> arguments,
974 int start_position,
975 int end_position,
976 Handle<Object> script,
977 Handle<Object> stack_trace,
978 Handle<Object> stack_frames) {
Steve Block44f0eee2011-05-26 01:26:41 +0100979 CALL_HEAP_FUNCTION(isolate(),
980 isolate()->heap()->AllocateJSMessageObject(*type,
981 *arguments,
982 start_position,
983 end_position,
984 *script,
985 *stack_trace,
986 *stack_frames),
Steve Block1e0659c2011-05-24 12:43:12 +0100987 JSMessageObject);
988}
989
Steve Blocka7e24c12009-10-30 11:49:00 +0000990Handle<SharedFunctionInfo> Factory::NewSharedFunctionInfo(Handle<String> name) {
Steve Block44f0eee2011-05-26 01:26:41 +0100991 CALL_HEAP_FUNCTION(isolate(),
992 isolate()->heap()->AllocateSharedFunctionInfo(*name),
Steve Blocka7e24c12009-10-30 11:49:00 +0000993 SharedFunctionInfo);
994}
995
996
997Handle<String> Factory::NumberToString(Handle<Object> number) {
Steve Block44f0eee2011-05-26 01:26:41 +0100998 CALL_HEAP_FUNCTION(isolate(),
999 isolate()->heap()->NumberToString(*number), String);
Steve Blocka7e24c12009-10-30 11:49:00 +00001000}
1001
1002
Ben Murdoch2b4ba112012-01-20 14:57:15 +00001003Handle<SeededNumberDictionary> Factory::DictionaryAtNumberPut(
1004 Handle<SeededNumberDictionary> dictionary,
Steve Blocka7e24c12009-10-30 11:49:00 +00001005 uint32_t key,
1006 Handle<Object> value) {
Steve Block44f0eee2011-05-26 01:26:41 +01001007 CALL_HEAP_FUNCTION(isolate(),
1008 dictionary->AtNumberPut(key, *value),
Ben Murdoch2b4ba112012-01-20 14:57:15 +00001009 SeededNumberDictionary);
1010}
1011
1012
1013Handle<UnseededNumberDictionary> Factory::DictionaryAtNumberPut(
1014 Handle<UnseededNumberDictionary> dictionary,
1015 uint32_t key,
1016 Handle<Object> value) {
1017 CALL_HEAP_FUNCTION(isolate(),
1018 dictionary->AtNumberPut(key, *value),
1019 UnseededNumberDictionary);
Steve Blocka7e24c12009-10-30 11:49:00 +00001020}
1021
1022
1023Handle<JSFunction> Factory::NewFunctionHelper(Handle<String> name,
1024 Handle<Object> prototype) {
1025 Handle<SharedFunctionInfo> function_share = NewSharedFunctionInfo(name);
Steve Block44f0eee2011-05-26 01:26:41 +01001026 CALL_HEAP_FUNCTION(
1027 isolate(),
1028 isolate()->heap()->AllocateFunction(*isolate()->function_map(),
1029 *function_share,
1030 *prototype),
1031 JSFunction);
Steve Blocka7e24c12009-10-30 11:49:00 +00001032}
1033
1034
1035Handle<JSFunction> Factory::NewFunction(Handle<String> name,
1036 Handle<Object> prototype) {
1037 Handle<JSFunction> fun = NewFunctionHelper(name, prototype);
Steve Block44f0eee2011-05-26 01:26:41 +01001038 fun->set_context(isolate()->context()->global_context());
Steve Blocka7e24c12009-10-30 11:49:00 +00001039 return fun;
1040}
1041
1042
Steve Block6ded16b2010-05-10 14:33:55 +01001043Handle<JSFunction> Factory::NewFunctionWithoutPrototypeHelper(
Steve Block44f0eee2011-05-26 01:26:41 +01001044 Handle<String> name,
1045 StrictModeFlag strict_mode) {
Steve Block6ded16b2010-05-10 14:33:55 +01001046 Handle<SharedFunctionInfo> function_share = NewSharedFunctionInfo(name);
Steve Block44f0eee2011-05-26 01:26:41 +01001047 Handle<Map> map = strict_mode == kStrictMode
1048 ? isolate()->strict_mode_function_without_prototype_map()
1049 : isolate()->function_without_prototype_map();
1050 CALL_HEAP_FUNCTION(isolate(),
1051 isolate()->heap()->AllocateFunction(
1052 *map,
Steve Block6ded16b2010-05-10 14:33:55 +01001053 *function_share,
1054 *the_hole_value()),
1055 JSFunction);
1056}
1057
1058
Steve Block44f0eee2011-05-26 01:26:41 +01001059Handle<JSFunction> Factory::NewFunctionWithoutPrototype(
1060 Handle<String> name,
1061 StrictModeFlag strict_mode) {
1062 Handle<JSFunction> fun = NewFunctionWithoutPrototypeHelper(name, strict_mode);
1063 fun->set_context(isolate()->context()->global_context());
Steve Block6ded16b2010-05-10 14:33:55 +01001064 return fun;
1065}
1066
1067
Leon Clarkee46be812010-01-19 14:06:41 +00001068Handle<Object> Factory::ToObject(Handle<Object> object) {
Steve Block44f0eee2011-05-26 01:26:41 +01001069 CALL_HEAP_FUNCTION(isolate(), object->ToObject(), Object);
Leon Clarkee46be812010-01-19 14:06:41 +00001070}
1071
1072
Steve Blocka7e24c12009-10-30 11:49:00 +00001073Handle<Object> Factory::ToObject(Handle<Object> object,
1074 Handle<Context> global_context) {
Steve Block44f0eee2011-05-26 01:26:41 +01001075 CALL_HEAP_FUNCTION(isolate(), object->ToObject(*global_context), Object);
Steve Blocka7e24c12009-10-30 11:49:00 +00001076}
1077
1078
1079#ifdef ENABLE_DEBUGGER_SUPPORT
1080Handle<DebugInfo> Factory::NewDebugInfo(Handle<SharedFunctionInfo> shared) {
1081 // Get the original code of the function.
1082 Handle<Code> code(shared->code());
1083
1084 // Create a copy of the code before allocating the debug info object to avoid
1085 // allocation while setting up the debug info object.
1086 Handle<Code> original_code(*Factory::CopyCode(code));
1087
1088 // Allocate initial fixed array for active break points before allocating the
1089 // debug info object to avoid allocation while setting up the debug info
1090 // object.
1091 Handle<FixedArray> break_points(
Steve Block44f0eee2011-05-26 01:26:41 +01001092 NewFixedArray(Debug::kEstimatedNofBreakPointsInFunction));
Steve Blocka7e24c12009-10-30 11:49:00 +00001093
1094 // Create and set up the debug info object. Debug info contains function, a
1095 // copy of the original code, the executing code and initial fixed array for
1096 // active break points.
1097 Handle<DebugInfo> debug_info =
Steve Block44f0eee2011-05-26 01:26:41 +01001098 Handle<DebugInfo>::cast(NewStruct(DEBUG_INFO_TYPE));
Steve Blocka7e24c12009-10-30 11:49:00 +00001099 debug_info->set_shared(*shared);
1100 debug_info->set_original_code(*original_code);
1101 debug_info->set_code(*code);
1102 debug_info->set_break_points(*break_points);
1103
1104 // Link debug info to function.
1105 shared->set_debug_info(*debug_info);
1106
1107 return debug_info;
1108}
1109#endif
1110
1111
1112Handle<JSObject> Factory::NewArgumentsObject(Handle<Object> callee,
1113 int length) {
Steve Block44f0eee2011-05-26 01:26:41 +01001114 CALL_HEAP_FUNCTION(
1115 isolate(),
1116 isolate()->heap()->AllocateArgumentsObject(*callee, length), JSObject);
Steve Blocka7e24c12009-10-30 11:49:00 +00001117}
1118
1119
1120Handle<JSFunction> Factory::CreateApiFunction(
1121 Handle<FunctionTemplateInfo> obj, ApiInstanceType instance_type) {
Steve Block44f0eee2011-05-26 01:26:41 +01001122 Handle<Code> code = isolate()->builtins()->HandleApiCall();
1123 Handle<Code> construct_stub = isolate()->builtins()->JSConstructStubApi();
Steve Blocka7e24c12009-10-30 11:49:00 +00001124
1125 int internal_field_count = 0;
1126 if (!obj->instance_template()->IsUndefined()) {
1127 Handle<ObjectTemplateInfo> instance_template =
1128 Handle<ObjectTemplateInfo>(
1129 ObjectTemplateInfo::cast(obj->instance_template()));
1130 internal_field_count =
1131 Smi::cast(instance_template->internal_field_count())->value();
1132 }
1133
1134 int instance_size = kPointerSize * internal_field_count;
1135 InstanceType type = INVALID_TYPE;
1136 switch (instance_type) {
1137 case JavaScriptObject:
1138 type = JS_OBJECT_TYPE;
1139 instance_size += JSObject::kHeaderSize;
1140 break;
1141 case InnerGlobalObject:
1142 type = JS_GLOBAL_OBJECT_TYPE;
1143 instance_size += JSGlobalObject::kSize;
1144 break;
1145 case OuterGlobalObject:
1146 type = JS_GLOBAL_PROXY_TYPE;
1147 instance_size += JSGlobalProxy::kSize;
1148 break;
1149 default:
1150 break;
1151 }
1152 ASSERT(type != INVALID_TYPE);
1153
1154 Handle<JSFunction> result =
Steve Block44f0eee2011-05-26 01:26:41 +01001155 NewFunction(Factory::empty_symbol(),
1156 type,
1157 instance_size,
1158 code,
1159 true);
Steve Blocka7e24c12009-10-30 11:49:00 +00001160 // Set class name.
1161 Handle<Object> class_name = Handle<Object>(obj->class_name());
1162 if (class_name->IsString()) {
1163 result->shared()->set_instance_class_name(*class_name);
1164 result->shared()->set_name(*class_name);
1165 }
1166
1167 Handle<Map> map = Handle<Map>(result->initial_map());
1168
1169 // Mark as undetectable if needed.
1170 if (obj->undetectable()) {
1171 map->set_is_undetectable();
1172 }
1173
1174 // Mark as hidden for the __proto__ accessor if needed.
1175 if (obj->hidden_prototype()) {
1176 map->set_is_hidden_prototype();
1177 }
1178
1179 // Mark as needs_access_check if needed.
1180 if (obj->needs_access_check()) {
1181 map->set_is_access_check_needed(true);
1182 }
1183
1184 // Set interceptor information in the map.
1185 if (!obj->named_property_handler()->IsUndefined()) {
1186 map->set_has_named_interceptor();
1187 }
1188 if (!obj->indexed_property_handler()->IsUndefined()) {
1189 map->set_has_indexed_interceptor();
1190 }
1191
1192 // Set instance call-as-function information in the map.
1193 if (!obj->instance_call_handler()->IsUndefined()) {
1194 map->set_has_instance_call_handler();
1195 }
1196
1197 result->shared()->set_function_data(*obj);
Leon Clarkee46be812010-01-19 14:06:41 +00001198 result->shared()->set_construct_stub(*construct_stub);
Steve Blocka7e24c12009-10-30 11:49:00 +00001199 result->shared()->DontAdaptArguments();
1200
1201 // Recursively copy parent templates' accessors, 'data' may be modified.
1202 Handle<DescriptorArray> array =
1203 Handle<DescriptorArray>(map->instance_descriptors());
1204 while (true) {
1205 Handle<Object> props = Handle<Object>(obj->property_accessors());
1206 if (!props->IsUndefined()) {
Steve Block44f0eee2011-05-26 01:26:41 +01001207 array = CopyAppendCallbackDescriptors(array, props);
Steve Blocka7e24c12009-10-30 11:49:00 +00001208 }
1209 Handle<Object> parent = Handle<Object>(obj->parent_template());
1210 if (parent->IsUndefined()) break;
1211 obj = Handle<FunctionTemplateInfo>::cast(parent);
1212 }
1213 if (!array->IsEmpty()) {
1214 map->set_instance_descriptors(*array);
1215 }
1216
Steve Block6ded16b2010-05-10 14:33:55 +01001217 ASSERT(result->shared()->IsApiFunction());
Steve Blocka7e24c12009-10-30 11:49:00 +00001218 return result;
1219}
1220
1221
1222Handle<MapCache> Factory::NewMapCache(int at_least_space_for) {
Steve Block44f0eee2011-05-26 01:26:41 +01001223 CALL_HEAP_FUNCTION(isolate(),
1224 MapCache::Allocate(at_least_space_for), MapCache);
Steve Blocka7e24c12009-10-30 11:49:00 +00001225}
1226
1227
John Reck59135872010-11-02 12:39:01 -07001228MUST_USE_RESULT static MaybeObject* UpdateMapCacheWith(Context* context,
1229 FixedArray* keys,
1230 Map* map) {
1231 Object* result;
1232 { MaybeObject* maybe_result =
1233 MapCache::cast(context->map_cache())->Put(keys, map);
1234 if (!maybe_result->ToObject(&result)) return maybe_result;
1235 }
1236 context->set_map_cache(MapCache::cast(result));
Steve Blocka7e24c12009-10-30 11:49:00 +00001237 return result;
1238}
1239
1240
1241Handle<MapCache> Factory::AddToMapCache(Handle<Context> context,
1242 Handle<FixedArray> keys,
1243 Handle<Map> map) {
Steve Block44f0eee2011-05-26 01:26:41 +01001244 CALL_HEAP_FUNCTION(isolate(),
1245 UpdateMapCacheWith(*context, *keys, *map), MapCache);
Steve Blocka7e24c12009-10-30 11:49:00 +00001246}
1247
1248
1249Handle<Map> Factory::ObjectLiteralMapFromCache(Handle<Context> context,
1250 Handle<FixedArray> keys) {
1251 if (context->map_cache()->IsUndefined()) {
1252 // Allocate the new map cache for the global context.
1253 Handle<MapCache> new_cache = NewMapCache(24);
1254 context->set_map_cache(*new_cache);
1255 }
1256 // Check to see whether there is a matching element in the cache.
1257 Handle<MapCache> cache =
1258 Handle<MapCache>(MapCache::cast(context->map_cache()));
1259 Handle<Object> result = Handle<Object>(cache->Lookup(*keys));
1260 if (result->IsMap()) return Handle<Map>::cast(result);
1261 // Create a new map and add it to the cache.
1262 Handle<Map> map =
1263 CopyMap(Handle<Map>(context->object_function()->initial_map()),
1264 keys->length());
1265 AddToMapCache(context, keys, map);
1266 return Handle<Map>(map);
1267}
1268
1269
1270void Factory::SetRegExpAtomData(Handle<JSRegExp> regexp,
1271 JSRegExp::Type type,
1272 Handle<String> source,
1273 JSRegExp::Flags flags,
1274 Handle<Object> data) {
1275 Handle<FixedArray> store = NewFixedArray(JSRegExp::kAtomDataSize);
1276
1277 store->set(JSRegExp::kTagIndex, Smi::FromInt(type));
1278 store->set(JSRegExp::kSourceIndex, *source);
1279 store->set(JSRegExp::kFlagsIndex, Smi::FromInt(flags.value()));
1280 store->set(JSRegExp::kAtomPatternIndex, *data);
1281 regexp->set_data(*store);
1282}
1283
1284void Factory::SetRegExpIrregexpData(Handle<JSRegExp> regexp,
1285 JSRegExp::Type type,
1286 Handle<String> source,
1287 JSRegExp::Flags flags,
1288 int capture_count) {
1289 Handle<FixedArray> store = NewFixedArray(JSRegExp::kIrregexpDataSize);
Ben Murdoch257744e2011-11-30 15:57:28 +00001290 Smi* uninitialized = Smi::FromInt(JSRegExp::kUninitializedValue);
Steve Blocka7e24c12009-10-30 11:49:00 +00001291 store->set(JSRegExp::kTagIndex, Smi::FromInt(type));
1292 store->set(JSRegExp::kSourceIndex, *source);
1293 store->set(JSRegExp::kFlagsIndex, Smi::FromInt(flags.value()));
Ben Murdoch257744e2011-11-30 15:57:28 +00001294 store->set(JSRegExp::kIrregexpASCIICodeIndex, uninitialized);
1295 store->set(JSRegExp::kIrregexpUC16CodeIndex, uninitialized);
1296 store->set(JSRegExp::kIrregexpASCIICodeSavedIndex, uninitialized);
1297 store->set(JSRegExp::kIrregexpUC16CodeSavedIndex, uninitialized);
Steve Blocka7e24c12009-10-30 11:49:00 +00001298 store->set(JSRegExp::kIrregexpMaxRegisterCountIndex, Smi::FromInt(0));
1299 store->set(JSRegExp::kIrregexpCaptureCountIndex,
1300 Smi::FromInt(capture_count));
1301 regexp->set_data(*store);
1302}
1303
1304
1305
1306void Factory::ConfigureInstance(Handle<FunctionTemplateInfo> desc,
1307 Handle<JSObject> instance,
1308 bool* pending_exception) {
1309 // Configure the instance by adding the properties specified by the
1310 // instance template.
1311 Handle<Object> instance_template = Handle<Object>(desc->instance_template());
1312 if (!instance_template->IsUndefined()) {
1313 Execution::ConfigureInstance(instance,
1314 instance_template,
1315 pending_exception);
1316 } else {
1317 *pending_exception = false;
1318 }
1319}
1320
1321
1322} } // namespace v8::internal