blob: f1042a4c66809d7ceec3972a755de406ec1c0b66 [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 Murdoch592a9fc2012-03-05 11:04:45 +000062Handle<FixedDoubleArray> Factory::NewFixedDoubleArray(int size,
63 PretenureFlag pretenure) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000064 ASSERT(0 <= size);
65 CALL_HEAP_FUNCTION(
66 isolate(),
67 isolate()->heap()->AllocateUninitializedFixedDoubleArray(size, pretenure),
Ben Murdoch592a9fc2012-03-05 11:04:45 +000068 FixedDoubleArray);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000069}
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 Murdoch592a9fc2012-03-05 11:04:45 +000080Handle<NumberDictionary> Factory::NewNumberDictionary(int at_least_space_for) {
Steve Blocka7e24c12009-10-30 11:49:00 +000081 ASSERT(0 <= at_least_space_for);
Steve Block44f0eee2011-05-26 01:26:41 +010082 CALL_HEAP_FUNCTION(isolate(),
Ben Murdoch592a9fc2012-03-05 11:04:45 +000083 NumberDictionary::Allocate(at_least_space_for),
84 NumberDictionary);
Ben Murdoch2b4ba112012-01-20 14:57:15 +000085}
86
87
Ben Murdoch592a9fc2012-03-05 11:04:45 +000088Handle<ObjectHashSet> Factory::NewObjectHashSet(int at_least_space_for) {
Ben Murdoch2b4ba112012-01-20 14:57:15 +000089 ASSERT(0 <= at_least_space_for);
90 CALL_HEAP_FUNCTION(isolate(),
Ben Murdoch592a9fc2012-03-05 11:04:45 +000091 ObjectHashSet::Allocate(at_least_space_for),
92 ObjectHashSet);
Steve Blocka7e24c12009-10-30 11:49:00 +000093}
94
95
Ben Murdoch69a99ed2011-11-30 16:03:39 +000096Handle<ObjectHashTable> Factory::NewObjectHashTable(int at_least_space_for) {
97 ASSERT(0 <= at_least_space_for);
98 CALL_HEAP_FUNCTION(isolate(),
99 ObjectHashTable::Allocate(at_least_space_for),
100 ObjectHashTable);
101}
102
103
Steve Blocka7e24c12009-10-30 11:49:00 +0000104Handle<DescriptorArray> Factory::NewDescriptorArray(int number_of_descriptors) {
105 ASSERT(0 <= number_of_descriptors);
Steve Block44f0eee2011-05-26 01:26:41 +0100106 CALL_HEAP_FUNCTION(isolate(),
107 DescriptorArray::Allocate(number_of_descriptors),
Steve Blocka7e24c12009-10-30 11:49:00 +0000108 DescriptorArray);
109}
110
111
Ben Murdochb0fe1622011-05-05 13:52:32 +0100112Handle<DeoptimizationInputData> Factory::NewDeoptimizationInputData(
113 int deopt_entry_count,
114 PretenureFlag pretenure) {
115 ASSERT(deopt_entry_count > 0);
Steve Block44f0eee2011-05-26 01:26:41 +0100116 CALL_HEAP_FUNCTION(isolate(),
117 DeoptimizationInputData::Allocate(deopt_entry_count,
Ben Murdochb0fe1622011-05-05 13:52:32 +0100118 pretenure),
119 DeoptimizationInputData);
120}
121
122
123Handle<DeoptimizationOutputData> Factory::NewDeoptimizationOutputData(
124 int deopt_entry_count,
125 PretenureFlag pretenure) {
126 ASSERT(deopt_entry_count > 0);
Steve Block44f0eee2011-05-26 01:26:41 +0100127 CALL_HEAP_FUNCTION(isolate(),
128 DeoptimizationOutputData::Allocate(deopt_entry_count,
Ben Murdochb0fe1622011-05-05 13:52:32 +0100129 pretenure),
130 DeoptimizationOutputData);
131}
132
133
Steve Blocka7e24c12009-10-30 11:49:00 +0000134// Symbols are created in the old generation (data space).
135Handle<String> Factory::LookupSymbol(Vector<const char> string) {
Steve Block44f0eee2011-05-26 01:26:41 +0100136 CALL_HEAP_FUNCTION(isolate(),
137 isolate()->heap()->LookupSymbol(string),
138 String);
Steve Blocka7e24c12009-10-30 11:49:00 +0000139}
140
Ben Murdoch257744e2011-11-30 15:57:28 +0000141// Symbols are created in the old generation (data space).
142Handle<String> Factory::LookupSymbol(Handle<String> string) {
143 CALL_HEAP_FUNCTION(isolate(),
144 isolate()->heap()->LookupSymbol(*string),
145 String);
146}
147
Steve Block9fac8402011-05-12 15:51:54 +0100148Handle<String> Factory::LookupAsciiSymbol(Vector<const char> string) {
Steve Block44f0eee2011-05-26 01:26:41 +0100149 CALL_HEAP_FUNCTION(isolate(),
150 isolate()->heap()->LookupAsciiSymbol(string),
151 String);
Steve Block9fac8402011-05-12 15:51:54 +0100152}
153
Ben Murdoch257744e2011-11-30 15:57:28 +0000154
155Handle<String> Factory::LookupAsciiSymbol(Handle<SeqAsciiString> string,
156 int from,
157 int length) {
158 CALL_HEAP_FUNCTION(isolate(),
159 isolate()->heap()->LookupAsciiSymbol(string,
160 from,
161 length),
162 String);
163}
164
165
Steve Block9fac8402011-05-12 15:51:54 +0100166Handle<String> Factory::LookupTwoByteSymbol(Vector<const uc16> string) {
Steve Block44f0eee2011-05-26 01:26:41 +0100167 CALL_HEAP_FUNCTION(isolate(),
168 isolate()->heap()->LookupTwoByteSymbol(string),
169 String);
Steve Block9fac8402011-05-12 15:51:54 +0100170}
171
Steve Blocka7e24c12009-10-30 11:49:00 +0000172
173Handle<String> Factory::NewStringFromAscii(Vector<const char> string,
174 PretenureFlag pretenure) {
Steve Block44f0eee2011-05-26 01:26:41 +0100175 CALL_HEAP_FUNCTION(
176 isolate(),
177 isolate()->heap()->AllocateStringFromAscii(string, pretenure),
178 String);
Steve Blocka7e24c12009-10-30 11:49:00 +0000179}
180
181Handle<String> Factory::NewStringFromUtf8(Vector<const char> string,
182 PretenureFlag pretenure) {
Steve Block44f0eee2011-05-26 01:26:41 +0100183 CALL_HEAP_FUNCTION(
184 isolate(),
185 isolate()->heap()->AllocateStringFromUtf8(string, pretenure),
186 String);
Steve Blocka7e24c12009-10-30 11:49:00 +0000187}
188
189
190Handle<String> Factory::NewStringFromTwoByte(Vector<const uc16> string,
191 PretenureFlag pretenure) {
Steve Block44f0eee2011-05-26 01:26:41 +0100192 CALL_HEAP_FUNCTION(
193 isolate(),
194 isolate()->heap()->AllocateStringFromTwoByte(string, pretenure),
195 String);
Steve Blocka7e24c12009-10-30 11:49:00 +0000196}
197
198
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000199Handle<SeqAsciiString> Factory::NewRawAsciiString(int length,
200 PretenureFlag pretenure) {
Steve Block44f0eee2011-05-26 01:26:41 +0100201 CALL_HEAP_FUNCTION(
202 isolate(),
203 isolate()->heap()->AllocateRawAsciiString(length, pretenure),
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000204 SeqAsciiString);
Leon Clarkeac952652010-07-15 11:15:24 +0100205}
206
207
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000208Handle<SeqTwoByteString> Factory::NewRawTwoByteString(int length,
209 PretenureFlag pretenure) {
Steve Block44f0eee2011-05-26 01:26:41 +0100210 CALL_HEAP_FUNCTION(
211 isolate(),
212 isolate()->heap()->AllocateRawTwoByteString(length, pretenure),
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000213 SeqTwoByteString);
Steve Blocka7e24c12009-10-30 11:49:00 +0000214}
215
216
217Handle<String> Factory::NewConsString(Handle<String> first,
218 Handle<String> second) {
Steve Block44f0eee2011-05-26 01:26:41 +0100219 CALL_HEAP_FUNCTION(isolate(),
220 isolate()->heap()->AllocateConsString(*first, *second),
221 String);
Steve Blocka7e24c12009-10-30 11:49:00 +0000222}
223
224
Steve Blockd0582a62009-12-15 09:54:21 +0000225Handle<String> Factory::NewSubString(Handle<String> str,
226 int begin,
227 int end) {
Steve Block44f0eee2011-05-26 01:26:41 +0100228 CALL_HEAP_FUNCTION(isolate(),
229 str->SubString(begin, end),
230 String);
Steve Blocka7e24c12009-10-30 11:49:00 +0000231}
232
233
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000234Handle<String> Factory::NewProperSubString(Handle<String> str,
235 int begin,
236 int end) {
237 ASSERT(begin > 0 || end < str->length());
238 CALL_HEAP_FUNCTION(isolate(),
239 isolate()->heap()->AllocateSubString(*str, begin, end),
240 String);
241}
242
243
Steve Blocka7e24c12009-10-30 11:49:00 +0000244Handle<String> Factory::NewExternalStringFromAscii(
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000245 const ExternalAsciiString::Resource* resource) {
Steve Block44f0eee2011-05-26 01:26:41 +0100246 CALL_HEAP_FUNCTION(
247 isolate(),
248 isolate()->heap()->AllocateExternalStringFromAscii(resource),
249 String);
Steve Blocka7e24c12009-10-30 11:49:00 +0000250}
251
252
253Handle<String> Factory::NewExternalStringFromTwoByte(
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000254 const ExternalTwoByteString::Resource* resource) {
Steve Block44f0eee2011-05-26 01:26:41 +0100255 CALL_HEAP_FUNCTION(
256 isolate(),
257 isolate()->heap()->AllocateExternalStringFromTwoByte(resource),
258 String);
Steve Blocka7e24c12009-10-30 11:49:00 +0000259}
260
261
262Handle<Context> Factory::NewGlobalContext() {
Steve Block44f0eee2011-05-26 01:26:41 +0100263 CALL_HEAP_FUNCTION(
264 isolate(),
265 isolate()->heap()->AllocateGlobalContext(),
266 Context);
Steve Blocka7e24c12009-10-30 11:49:00 +0000267}
268
269
270Handle<Context> Factory::NewFunctionContext(int length,
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000271 Handle<JSFunction> function) {
Steve Block44f0eee2011-05-26 01:26:41 +0100272 CALL_HEAP_FUNCTION(
273 isolate(),
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000274 isolate()->heap()->AllocateFunctionContext(length, *function),
Steve Block44f0eee2011-05-26 01:26:41 +0100275 Context);
Steve Blocka7e24c12009-10-30 11:49:00 +0000276}
277
278
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000279Handle<Context> Factory::NewCatchContext(Handle<JSFunction> function,
280 Handle<Context> previous,
281 Handle<String> name,
282 Handle<Object> thrown_object) {
Steve Block44f0eee2011-05-26 01:26:41 +0100283 CALL_HEAP_FUNCTION(
284 isolate(),
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000285 isolate()->heap()->AllocateCatchContext(*function,
286 *previous,
287 *name,
288 *thrown_object),
289 Context);
290}
291
292
293Handle<Context> Factory::NewWithContext(Handle<JSFunction> function,
294 Handle<Context> previous,
295 Handle<JSObject> extension) {
296 CALL_HEAP_FUNCTION(
297 isolate(),
298 isolate()->heap()->AllocateWithContext(*function, *previous, *extension),
Steve Block44f0eee2011-05-26 01:26:41 +0100299 Context);
Steve Blocka7e24c12009-10-30 11:49:00 +0000300}
301
302
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000303Handle<Context> Factory::NewBlockContext(
304 Handle<JSFunction> function,
305 Handle<Context> previous,
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000306 Handle<ScopeInfo> scope_info) {
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000307 CALL_HEAP_FUNCTION(
308 isolate(),
309 isolate()->heap()->AllocateBlockContext(*function,
310 *previous,
311 *scope_info),
312 Context);
313}
314
315
Steve Blocka7e24c12009-10-30 11:49:00 +0000316Handle<Struct> Factory::NewStruct(InstanceType type) {
Steve Block44f0eee2011-05-26 01:26:41 +0100317 CALL_HEAP_FUNCTION(
318 isolate(),
319 isolate()->heap()->AllocateStruct(type),
320 Struct);
Steve Blocka7e24c12009-10-30 11:49:00 +0000321}
322
323
324Handle<AccessorInfo> Factory::NewAccessorInfo() {
325 Handle<AccessorInfo> info =
326 Handle<AccessorInfo>::cast(NewStruct(ACCESSOR_INFO_TYPE));
327 info->set_flag(0); // Must clear the flag, it was initialized as undefined.
328 return info;
329}
330
331
332Handle<Script> Factory::NewScript(Handle<String> source) {
333 // Generate id for this script.
334 int id;
Steve Block44f0eee2011-05-26 01:26:41 +0100335 Heap* heap = isolate()->heap();
336 if (heap->last_script_id()->IsUndefined()) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000337 // Script ids start from one.
338 id = 1;
339 } else {
340 // Increment id, wrap when positive smi is exhausted.
Steve Block44f0eee2011-05-26 01:26:41 +0100341 id = Smi::cast(heap->last_script_id())->value();
Steve Blocka7e24c12009-10-30 11:49:00 +0000342 id++;
343 if (!Smi::IsValid(id)) {
344 id = 0;
345 }
346 }
Steve Block44f0eee2011-05-26 01:26:41 +0100347 heap->SetLastScriptId(Smi::FromInt(id));
Steve Blocka7e24c12009-10-30 11:49:00 +0000348
349 // Create and initialize script object.
Ben Murdoch257744e2011-11-30 15:57:28 +0000350 Handle<Foreign> wrapper = NewForeign(0, TENURED);
Steve Blocka7e24c12009-10-30 11:49:00 +0000351 Handle<Script> script = Handle<Script>::cast(NewStruct(SCRIPT_TYPE));
352 script->set_source(*source);
Steve Block44f0eee2011-05-26 01:26:41 +0100353 script->set_name(heap->undefined_value());
354 script->set_id(heap->last_script_id());
Steve Blocka7e24c12009-10-30 11:49:00 +0000355 script->set_line_offset(Smi::FromInt(0));
356 script->set_column_offset(Smi::FromInt(0));
Steve Block44f0eee2011-05-26 01:26:41 +0100357 script->set_data(heap->undefined_value());
358 script->set_context_data(heap->undefined_value());
Steve Blocka7e24c12009-10-30 11:49:00 +0000359 script->set_type(Smi::FromInt(Script::TYPE_NORMAL));
360 script->set_compilation_type(Smi::FromInt(Script::COMPILATION_TYPE_HOST));
361 script->set_wrapper(*wrapper);
Steve Block44f0eee2011-05-26 01:26:41 +0100362 script->set_line_ends(heap->undefined_value());
363 script->set_eval_from_shared(heap->undefined_value());
Steve Blocka7e24c12009-10-30 11:49:00 +0000364 script->set_eval_from_instructions_offset(Smi::FromInt(0));
365
366 return script;
367}
368
369
Ben Murdoch257744e2011-11-30 15:57:28 +0000370Handle<Foreign> Factory::NewForeign(Address addr, PretenureFlag pretenure) {
Steve Block44f0eee2011-05-26 01:26:41 +0100371 CALL_HEAP_FUNCTION(isolate(),
Ben Murdoch257744e2011-11-30 15:57:28 +0000372 isolate()->heap()->AllocateForeign(addr, pretenure),
373 Foreign);
Steve Blocka7e24c12009-10-30 11:49:00 +0000374}
375
376
Ben Murdoch257744e2011-11-30 15:57:28 +0000377Handle<Foreign> Factory::NewForeign(const AccessorDescriptor* desc) {
378 return NewForeign((Address) desc, TENURED);
Steve Blocka7e24c12009-10-30 11:49:00 +0000379}
380
381
382Handle<ByteArray> Factory::NewByteArray(int length, PretenureFlag pretenure) {
383 ASSERT(0 <= length);
Steve Block44f0eee2011-05-26 01:26:41 +0100384 CALL_HEAP_FUNCTION(
385 isolate(),
386 isolate()->heap()->AllocateByteArray(length, pretenure),
387 ByteArray);
Steve Blocka7e24c12009-10-30 11:49:00 +0000388}
389
390
Steve Block3ce2e202009-11-05 08:53:23 +0000391Handle<ExternalArray> Factory::NewExternalArray(int length,
392 ExternalArrayType array_type,
393 void* external_pointer,
394 PretenureFlag pretenure) {
395 ASSERT(0 <= length);
Steve Block44f0eee2011-05-26 01:26:41 +0100396 CALL_HEAP_FUNCTION(
397 isolate(),
398 isolate()->heap()->AllocateExternalArray(length,
399 array_type,
400 external_pointer,
401 pretenure),
402 ExternalArray);
Steve Block3ce2e202009-11-05 08:53:23 +0000403}
404
405
Ben Murdochb0fe1622011-05-05 13:52:32 +0100406Handle<JSGlobalPropertyCell> Factory::NewJSGlobalPropertyCell(
407 Handle<Object> value) {
Steve Block44f0eee2011-05-26 01:26:41 +0100408 CALL_HEAP_FUNCTION(
409 isolate(),
410 isolate()->heap()->AllocateJSGlobalPropertyCell(*value),
411 JSGlobalPropertyCell);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100412}
413
414
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000415Handle<Map> Factory::NewMap(InstanceType type,
416 int instance_size,
417 ElementsKind elements_kind) {
Steve Block44f0eee2011-05-26 01:26:41 +0100418 CALL_HEAP_FUNCTION(
419 isolate(),
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000420 isolate()->heap()->AllocateMap(type, instance_size, elements_kind),
Steve Block44f0eee2011-05-26 01:26:41 +0100421 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
Ben Murdoch589d6972011-11-30 16:04:58 +0000468Handle<Map> Factory::GetElementsTransitionMap(
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000469 Handle<JSObject> src,
470 ElementsKind elements_kind) {
Steve Block44f0eee2011-05-26 01:26:41 +0100471 CALL_HEAP_FUNCTION(isolate(),
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000472 src->GetElementsTransitionMap(elements_kind),
Steve Block44f0eee2011-05-26 01:26:41 +0100473 Map);
Steve Block1e0659c2011-05-24 12:43:12 +0100474}
475
476
Steve Blocka7e24c12009-10-30 11:49:00 +0000477Handle<FixedArray> Factory::CopyFixedArray(Handle<FixedArray> array) {
Steve Block44f0eee2011-05-26 01:26:41 +0100478 CALL_HEAP_FUNCTION(isolate(), array->Copy(), FixedArray);
Steve Blocka7e24c12009-10-30 11:49:00 +0000479}
480
481
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000482Handle<FixedDoubleArray> Factory::CopyFixedDoubleArray(
483 Handle<FixedDoubleArray> array) {
484 CALL_HEAP_FUNCTION(isolate(), array->Copy(), FixedDoubleArray);
485}
486
487
Steve Block6ded16b2010-05-10 14:33:55 +0100488Handle<JSFunction> Factory::BaseNewFunctionFromSharedFunctionInfo(
489 Handle<SharedFunctionInfo> function_info,
Leon Clarkee46be812010-01-19 14:06:41 +0000490 Handle<Map> function_map,
491 PretenureFlag pretenure) {
Steve Block44f0eee2011-05-26 01:26:41 +0100492 CALL_HEAP_FUNCTION(
493 isolate(),
494 isolate()->heap()->AllocateFunction(*function_map,
495 *function_info,
496 isolate()->heap()->the_hole_value(),
497 pretenure),
Steve Blocka7e24c12009-10-30 11:49:00 +0000498 JSFunction);
499}
500
501
Steve Block6ded16b2010-05-10 14:33:55 +0100502Handle<JSFunction> Factory::NewFunctionFromSharedFunctionInfo(
503 Handle<SharedFunctionInfo> function_info,
Leon Clarkee46be812010-01-19 14:06:41 +0000504 Handle<Context> context,
505 PretenureFlag pretenure) {
Steve Block6ded16b2010-05-10 14:33:55 +0100506 Handle<JSFunction> result = BaseNewFunctionFromSharedFunctionInfo(
Steve Block44f0eee2011-05-26 01:26:41 +0100507 function_info,
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000508 function_info->is_classic_mode()
509 ? isolate()->function_map()
510 : isolate()->strict_mode_function_map(),
Steve Block44f0eee2011-05-26 01:26:41 +0100511 pretenure);
512
Steve Blocka7e24c12009-10-30 11:49:00 +0000513 result->set_context(*context);
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000514 if (!function_info->bound()) {
515 int number_of_literals = function_info->num_literals();
516 Handle<FixedArray> literals = NewFixedArray(number_of_literals, pretenure);
517 if (number_of_literals > 0) {
518 // Store the object, regexp and array functions in the literals
519 // array prefix. These functions will be used when creating
520 // object, regexp and array literals in this function.
521 literals->set(JSFunction::kLiteralGlobalContextIndex,
522 context->global_context());
523 }
524 result->set_literals(*literals);
525 } else {
526 result->set_function_bindings(isolate()->heap()->empty_fixed_array());
Steve Blocka7e24c12009-10-30 11:49:00 +0000527 }
Steve Block44f0eee2011-05-26 01:26:41 +0100528 result->set_next_function_link(isolate()->heap()->undefined_value());
Ben Murdochb0fe1622011-05-05 13:52:32 +0100529
530 if (V8::UseCrankshaft() &&
531 FLAG_always_opt &&
532 result->is_compiled() &&
533 !function_info->is_toplevel() &&
534 function_info->allows_lazy_compilation()) {
535 result->MarkForLazyRecompilation();
536 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000537 return result;
538}
539
540
541Handle<Object> Factory::NewNumber(double value,
542 PretenureFlag pretenure) {
Steve Block44f0eee2011-05-26 01:26:41 +0100543 CALL_HEAP_FUNCTION(
544 isolate(),
545 isolate()->heap()->NumberFromDouble(value, pretenure), Object);
Steve Blocka7e24c12009-10-30 11:49:00 +0000546}
547
548
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000549Handle<Object> Factory::NewNumberFromInt(int32_t value,
550 PretenureFlag pretenure) {
Steve Block44f0eee2011-05-26 01:26:41 +0100551 CALL_HEAP_FUNCTION(
552 isolate(),
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000553 isolate()->heap()->NumberFromInt32(value, pretenure), Object);
Steve Blocka7e24c12009-10-30 11:49:00 +0000554}
555
556
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000557Handle<Object> Factory::NewNumberFromUint(uint32_t value,
558 PretenureFlag pretenure) {
Steve Block44f0eee2011-05-26 01:26:41 +0100559 CALL_HEAP_FUNCTION(
560 isolate(),
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000561 isolate()->heap()->NumberFromUint32(value, pretenure), 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);
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000654 Handle<Object> argv[] = { type_obj, args };
Steve Blocka7e24c12009-10-30 11:49:00 +0000655
656 // Invoke the JavaScript factory method. If an exception is thrown while
657 // running the factory method, use the exception as the result.
658 bool caught_exception;
659 Handle<Object> result = Execution::TryCall(fun,
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000660 isolate()->js_builtins_object(),
661 ARRAY_SIZE(argv),
662 argv,
663 &caught_exception);
Steve Blocka7e24c12009-10-30 11:49:00 +0000664 return result;
665}
666
667
668Handle<Object> Factory::NewError(Handle<String> message) {
669 return NewError("$Error", message);
670}
671
672
673Handle<Object> Factory::NewError(const char* constructor,
674 Handle<String> message) {
Steve Block44f0eee2011-05-26 01:26:41 +0100675 Handle<String> constr = LookupAsciiSymbol(constructor);
676 Handle<JSFunction> fun = Handle<JSFunction>(
677 JSFunction::cast(isolate()->js_builtins_object()->
678 GetPropertyNoExceptionThrown(*constr)));
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000679 Handle<Object> argv[] = { message };
Steve Blocka7e24c12009-10-30 11:49:00 +0000680
681 // Invoke the JavaScript factory method. If an exception is thrown while
682 // running the factory method, use the exception as the result.
683 bool caught_exception;
684 Handle<Object> result = Execution::TryCall(fun,
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000685 isolate()->js_builtins_object(),
686 ARRAY_SIZE(argv),
687 argv,
688 &caught_exception);
Steve Blocka7e24c12009-10-30 11:49:00 +0000689 return result;
690}
691
692
693Handle<JSFunction> Factory::NewFunction(Handle<String> name,
694 InstanceType type,
695 int instance_size,
696 Handle<Code> code,
697 bool force_initial_map) {
698 // Allocate the function
699 Handle<JSFunction> function = NewFunction(name, the_hole_value());
Iain Merrick75681382010-08-19 15:07:18 +0100700
701 // Setup the code pointer in both the shared function info and in
702 // the function itself.
703 function->shared()->set_code(*code);
Steve Blocka7e24c12009-10-30 11:49:00 +0000704 function->set_code(*code);
705
706 if (force_initial_map ||
707 type != JS_OBJECT_TYPE ||
708 instance_size != JSObject::kHeaderSize) {
709 Handle<Map> initial_map = NewMap(type, instance_size);
710 Handle<JSObject> prototype = NewFunctionPrototype(function);
711 initial_map->set_prototype(*prototype);
712 function->set_initial_map(*initial_map);
713 initial_map->set_constructor(*function);
714 } else {
715 ASSERT(!function->has_initial_map());
716 ASSERT(!function->has_prototype());
717 }
718
719 return function;
720}
721
722
Steve Blocka7e24c12009-10-30 11:49:00 +0000723Handle<JSFunction> Factory::NewFunctionWithPrototype(Handle<String> name,
724 InstanceType type,
725 int instance_size,
726 Handle<JSObject> prototype,
727 Handle<Code> code,
728 bool force_initial_map) {
Iain Merrick75681382010-08-19 15:07:18 +0100729 // Allocate the function.
Steve Blocka7e24c12009-10-30 11:49:00 +0000730 Handle<JSFunction> function = NewFunction(name, prototype);
731
Iain Merrick75681382010-08-19 15:07:18 +0100732 // Setup the code pointer in both the shared function info and in
733 // the function itself.
734 function->shared()->set_code(*code);
Steve Blocka7e24c12009-10-30 11:49:00 +0000735 function->set_code(*code);
736
737 if (force_initial_map ||
738 type != JS_OBJECT_TYPE ||
739 instance_size != JSObject::kHeaderSize) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000740 ElementsKind default_elements_kind = FLAG_smi_only_arrays
741 ? FAST_SMI_ONLY_ELEMENTS
742 : FAST_ELEMENTS;
743 Handle<Map> initial_map = NewMap(type,
744 instance_size,
745 default_elements_kind);
Steve Blocka7e24c12009-10-30 11:49:00 +0000746 function->set_initial_map(*initial_map);
747 initial_map->set_constructor(*function);
748 }
749
750 // Set function.prototype and give the prototype a constructor
751 // property that refers to the function.
752 SetPrototypeProperty(function, prototype);
Steve Block1e0659c2011-05-24 12:43:12 +0100753 // Currently safe because it is only invoked from Genesis.
Steve Block44f0eee2011-05-26 01:26:41 +0100754 SetLocalPropertyNoThrow(prototype, constructor_symbol(), function, DONT_ENUM);
Steve Blocka7e24c12009-10-30 11:49:00 +0000755 return function;
756}
757
758
Steve Block6ded16b2010-05-10 14:33:55 +0100759Handle<JSFunction> Factory::NewFunctionWithoutPrototype(Handle<String> name,
760 Handle<Code> code) {
Steve Block44f0eee2011-05-26 01:26:41 +0100761 Handle<JSFunction> function = NewFunctionWithoutPrototype(name,
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000762 CLASSIC_MODE);
Iain Merrick75681382010-08-19 15:07:18 +0100763 function->shared()->set_code(*code);
Steve Block6ded16b2010-05-10 14:33:55 +0100764 function->set_code(*code);
765 ASSERT(!function->has_initial_map());
766 ASSERT(!function->has_prototype());
767 return function;
768}
769
770
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000771Handle<ScopeInfo> Factory::NewScopeInfo(int length) {
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000772 CALL_HEAP_FUNCTION(
773 isolate(),
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000774 isolate()->heap()->AllocateScopeInfo(length),
775 ScopeInfo);
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000776}
777
778
Steve Blocka7e24c12009-10-30 11:49:00 +0000779Handle<Code> Factory::NewCode(const CodeDesc& desc,
Steve Blocka7e24c12009-10-30 11:49:00 +0000780 Code::Flags flags,
Steve Block44f0eee2011-05-26 01:26:41 +0100781 Handle<Object> self_ref,
782 bool immovable) {
783 CALL_HEAP_FUNCTION(isolate(),
784 isolate()->heap()->CreateCode(
785 desc, flags, self_ref, immovable),
786 Code);
Steve Blocka7e24c12009-10-30 11:49:00 +0000787}
788
789
790Handle<Code> Factory::CopyCode(Handle<Code> code) {
Steve Block44f0eee2011-05-26 01:26:41 +0100791 CALL_HEAP_FUNCTION(isolate(),
792 isolate()->heap()->CopyCode(*code),
793 Code);
Steve Blocka7e24c12009-10-30 11:49:00 +0000794}
795
796
Steve Block6ded16b2010-05-10 14:33:55 +0100797Handle<Code> Factory::CopyCode(Handle<Code> code, Vector<byte> reloc_info) {
Steve Block44f0eee2011-05-26 01:26:41 +0100798 CALL_HEAP_FUNCTION(isolate(),
799 isolate()->heap()->CopyCode(*code, reloc_info),
800 Code);
Steve Block6ded16b2010-05-10 14:33:55 +0100801}
802
803
John Reck59135872010-11-02 12:39:01 -0700804MUST_USE_RESULT static inline MaybeObject* DoCopyInsert(
805 DescriptorArray* array,
806 String* key,
807 Object* value,
808 PropertyAttributes attributes) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000809 CallbacksDescriptor desc(key, value, attributes);
John Reck59135872010-11-02 12:39:01 -0700810 MaybeObject* obj = array->CopyInsert(&desc, REMOVE_TRANSITIONS);
Steve Blocka7e24c12009-10-30 11:49:00 +0000811 return obj;
812}
813
814
815// Allocate the new array.
Ben Murdoch257744e2011-11-30 15:57:28 +0000816Handle<DescriptorArray> Factory::CopyAppendForeignDescriptor(
Steve Blocka7e24c12009-10-30 11:49:00 +0000817 Handle<DescriptorArray> array,
818 Handle<String> key,
819 Handle<Object> value,
820 PropertyAttributes attributes) {
Steve Block44f0eee2011-05-26 01:26:41 +0100821 CALL_HEAP_FUNCTION(isolate(),
822 DoCopyInsert(*array, *key, *value, attributes),
Steve Blocka7e24c12009-10-30 11:49:00 +0000823 DescriptorArray);
824}
825
826
827Handle<String> Factory::SymbolFromString(Handle<String> value) {
Steve Block44f0eee2011-05-26 01:26:41 +0100828 CALL_HEAP_FUNCTION(isolate(),
829 isolate()->heap()->LookupSymbol(*value), String);
Steve Blocka7e24c12009-10-30 11:49:00 +0000830}
831
832
833Handle<DescriptorArray> Factory::CopyAppendCallbackDescriptors(
834 Handle<DescriptorArray> array,
835 Handle<Object> descriptors) {
836 v8::NeanderArray callbacks(descriptors);
837 int nof_callbacks = callbacks.length();
838 Handle<DescriptorArray> result =
839 NewDescriptorArray(array->number_of_descriptors() + nof_callbacks);
840
841 // Number of descriptors added to the result so far.
842 int descriptor_count = 0;
843
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000844 // Ensure that marking will not progress and change color of objects.
845 DescriptorArray::WhitenessWitness witness(*result);
846
Steve Blocka7e24c12009-10-30 11:49:00 +0000847 // Copy the descriptors from the array.
848 for (int i = 0; i < array->number_of_descriptors(); i++) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000849 if (!array->IsNullDescriptor(i)) {
850 result->CopyFrom(descriptor_count++, *array, i, witness);
Steve Blocka7e24c12009-10-30 11:49:00 +0000851 }
852 }
853
854 // Number of duplicates detected.
855 int duplicates = 0;
856
857 // Fill in new callback descriptors. Process the callbacks from
858 // back to front so that the last callback with a given name takes
859 // precedence over previously added callbacks with that name.
860 for (int i = nof_callbacks - 1; i >= 0; i--) {
861 Handle<AccessorInfo> entry =
862 Handle<AccessorInfo>(AccessorInfo::cast(callbacks.get(i)));
863 // Ensure the key is a symbol before writing into the instance descriptor.
864 Handle<String> key =
865 SymbolFromString(Handle<String>(String::cast(entry->name())));
866 // Check if a descriptor with this name already exists before writing.
867 if (result->LinearSearch(*key, descriptor_count) ==
868 DescriptorArray::kNotFound) {
869 CallbacksDescriptor desc(*key, *entry, entry->property_attributes());
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000870 result->Set(descriptor_count, &desc, witness);
Steve Blocka7e24c12009-10-30 11:49:00 +0000871 descriptor_count++;
872 } else {
873 duplicates++;
874 }
875 }
876
877 // If duplicates were detected, allocate a result of the right size
878 // and transfer the elements.
879 if (duplicates > 0) {
880 int number_of_descriptors = result->number_of_descriptors() - duplicates;
881 Handle<DescriptorArray> new_result =
882 NewDescriptorArray(number_of_descriptors);
883 for (int i = 0; i < number_of_descriptors; i++) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000884 new_result->CopyFrom(i, *result, i, witness);
Steve Blocka7e24c12009-10-30 11:49:00 +0000885 }
886 result = new_result;
887 }
888
889 // Sort the result before returning.
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000890 result->Sort(witness);
Steve Blocka7e24c12009-10-30 11:49:00 +0000891 return result;
892}
893
894
895Handle<JSObject> Factory::NewJSObject(Handle<JSFunction> constructor,
896 PretenureFlag pretenure) {
Steve Block44f0eee2011-05-26 01:26:41 +0100897 CALL_HEAP_FUNCTION(
898 isolate(),
899 isolate()->heap()->AllocateJSObject(*constructor, pretenure), JSObject);
Steve Blocka7e24c12009-10-30 11:49:00 +0000900}
901
902
903Handle<GlobalObject> Factory::NewGlobalObject(
904 Handle<JSFunction> constructor) {
Steve Block44f0eee2011-05-26 01:26:41 +0100905 CALL_HEAP_FUNCTION(isolate(),
906 isolate()->heap()->AllocateGlobalObject(*constructor),
Steve Blocka7e24c12009-10-30 11:49:00 +0000907 GlobalObject);
908}
909
910
911
912Handle<JSObject> Factory::NewJSObjectFromMap(Handle<Map> map) {
Steve Block44f0eee2011-05-26 01:26:41 +0100913 CALL_HEAP_FUNCTION(
914 isolate(),
915 isolate()->heap()->AllocateJSObjectFromMap(*map, NOT_TENURED),
916 JSObject);
Steve Blocka7e24c12009-10-30 11:49:00 +0000917}
918
919
Steve Block44f0eee2011-05-26 01:26:41 +0100920Handle<JSArray> Factory::NewJSArray(int capacity,
Steve Blocka7e24c12009-10-30 11:49:00 +0000921 PretenureFlag pretenure) {
Steve Block44f0eee2011-05-26 01:26:41 +0100922 Handle<JSObject> obj = NewJSObject(isolate()->array_function(), pretenure);
923 CALL_HEAP_FUNCTION(isolate(),
924 Handle<JSArray>::cast(obj)->Initialize(capacity),
925 JSArray);
Steve Blocka7e24c12009-10-30 11:49:00 +0000926}
927
928
929Handle<JSArray> Factory::NewJSArrayWithElements(Handle<FixedArray> elements,
930 PretenureFlag pretenure) {
931 Handle<JSArray> result =
Steve Block44f0eee2011-05-26 01:26:41 +0100932 Handle<JSArray>::cast(NewJSObject(isolate()->array_function(),
933 pretenure));
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000934 SetContent(result, elements);
Steve Blocka7e24c12009-10-30 11:49:00 +0000935 return result;
936}
937
938
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000939void Factory::SetContent(Handle<JSArray> array,
940 Handle<FixedArray> elements) {
941 CALL_HEAP_FUNCTION_VOID(
942 isolate(),
943 array->SetContent(*elements));
944}
945
946
947void Factory::EnsureCanContainNonSmiElements(Handle<JSArray> array) {
948 CALL_HEAP_FUNCTION_VOID(
949 isolate(),
950 array->EnsureCanContainNonSmiElements());
951}
952
953
Ben Murdoch257744e2011-11-30 15:57:28 +0000954Handle<JSProxy> Factory::NewJSProxy(Handle<Object> handler,
955 Handle<Object> prototype) {
956 CALL_HEAP_FUNCTION(
957 isolate(),
958 isolate()->heap()->AllocateJSProxy(*handler, *prototype),
959 JSProxy);
960}
961
962
Ben Murdoch589d6972011-11-30 16:04:58 +0000963void Factory::BecomeJSObject(Handle<JSReceiver> object) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000964 CALL_HEAP_FUNCTION_VOID(
965 isolate(),
Ben Murdoch589d6972011-11-30 16:04:58 +0000966 isolate()->heap()->ReinitializeJSReceiver(
967 *object, JS_OBJECT_TYPE, JSObject::kHeaderSize));
968}
969
970
971void Factory::BecomeJSFunction(Handle<JSReceiver> object) {
972 CALL_HEAP_FUNCTION_VOID(
973 isolate(),
974 isolate()->heap()->ReinitializeJSReceiver(
975 *object, JS_FUNCTION_TYPE, JSFunction::kSize));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000976}
977
978
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000979void Factory::SetIdentityHash(Handle<JSObject> object, Object* hash) {
980 CALL_HEAP_FUNCTION_VOID(
981 isolate(),
982 object->SetIdentityHash(hash, ALLOW_CREATION));
983}
984
985
Steve Block6ded16b2010-05-10 14:33:55 +0100986Handle<SharedFunctionInfo> Factory::NewSharedFunctionInfo(
Ben Murdoch3bec4d22010-07-22 14:51:16 +0100987 Handle<String> name,
988 int number_of_literals,
989 Handle<Code> code,
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000990 Handle<ScopeInfo> scope_info) {
Steve Block6ded16b2010-05-10 14:33:55 +0100991 Handle<SharedFunctionInfo> shared = NewSharedFunctionInfo(name);
992 shared->set_code(*code);
Ben Murdoch3bec4d22010-07-22 14:51:16 +0100993 shared->set_scope_info(*scope_info);
Steve Block6ded16b2010-05-10 14:33:55 +0100994 int literals_array_size = number_of_literals;
995 // If the function contains object, regexp or array literals,
996 // allocate extra space for a literals array prefix containing the
997 // context.
998 if (number_of_literals > 0) {
999 literals_array_size += JSFunction::kLiteralsPrefixSize;
1000 }
1001 shared->set_num_literals(literals_array_size);
1002 return shared;
1003}
1004
1005
Steve Block1e0659c2011-05-24 12:43:12 +01001006Handle<JSMessageObject> Factory::NewJSMessageObject(
1007 Handle<String> type,
1008 Handle<JSArray> arguments,
1009 int start_position,
1010 int end_position,
1011 Handle<Object> script,
1012 Handle<Object> stack_trace,
1013 Handle<Object> stack_frames) {
Steve Block44f0eee2011-05-26 01:26:41 +01001014 CALL_HEAP_FUNCTION(isolate(),
1015 isolate()->heap()->AllocateJSMessageObject(*type,
1016 *arguments,
1017 start_position,
1018 end_position,
1019 *script,
1020 *stack_trace,
1021 *stack_frames),
Steve Block1e0659c2011-05-24 12:43:12 +01001022 JSMessageObject);
1023}
1024
Steve Blocka7e24c12009-10-30 11:49:00 +00001025Handle<SharedFunctionInfo> Factory::NewSharedFunctionInfo(Handle<String> name) {
Steve Block44f0eee2011-05-26 01:26:41 +01001026 CALL_HEAP_FUNCTION(isolate(),
1027 isolate()->heap()->AllocateSharedFunctionInfo(*name),
Steve Blocka7e24c12009-10-30 11:49:00 +00001028 SharedFunctionInfo);
1029}
1030
1031
1032Handle<String> Factory::NumberToString(Handle<Object> number) {
Steve Block44f0eee2011-05-26 01:26:41 +01001033 CALL_HEAP_FUNCTION(isolate(),
1034 isolate()->heap()->NumberToString(*number), String);
Steve Blocka7e24c12009-10-30 11:49:00 +00001035}
1036
1037
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001038Handle<String> Factory::Uint32ToString(uint32_t value) {
Steve Block44f0eee2011-05-26 01:26:41 +01001039 CALL_HEAP_FUNCTION(isolate(),
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001040 isolate()->heap()->Uint32ToString(value), String);
Ben Murdoch2b4ba112012-01-20 14:57:15 +00001041}
1042
1043
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001044Handle<NumberDictionary> Factory::DictionaryAtNumberPut(
1045 Handle<NumberDictionary> dictionary,
Ben Murdoch2b4ba112012-01-20 14:57:15 +00001046 uint32_t key,
1047 Handle<Object> value) {
1048 CALL_HEAP_FUNCTION(isolate(),
1049 dictionary->AtNumberPut(key, *value),
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001050 NumberDictionary);
Steve Blocka7e24c12009-10-30 11:49:00 +00001051}
1052
1053
1054Handle<JSFunction> Factory::NewFunctionHelper(Handle<String> name,
1055 Handle<Object> prototype) {
1056 Handle<SharedFunctionInfo> function_share = NewSharedFunctionInfo(name);
Steve Block44f0eee2011-05-26 01:26:41 +01001057 CALL_HEAP_FUNCTION(
1058 isolate(),
1059 isolate()->heap()->AllocateFunction(*isolate()->function_map(),
1060 *function_share,
1061 *prototype),
1062 JSFunction);
Steve Blocka7e24c12009-10-30 11:49:00 +00001063}
1064
1065
1066Handle<JSFunction> Factory::NewFunction(Handle<String> name,
1067 Handle<Object> prototype) {
1068 Handle<JSFunction> fun = NewFunctionHelper(name, prototype);
Steve Block44f0eee2011-05-26 01:26:41 +01001069 fun->set_context(isolate()->context()->global_context());
Steve Blocka7e24c12009-10-30 11:49:00 +00001070 return fun;
1071}
1072
1073
Steve Block6ded16b2010-05-10 14:33:55 +01001074Handle<JSFunction> Factory::NewFunctionWithoutPrototypeHelper(
Steve Block44f0eee2011-05-26 01:26:41 +01001075 Handle<String> name,
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001076 LanguageMode language_mode) {
Steve Block6ded16b2010-05-10 14:33:55 +01001077 Handle<SharedFunctionInfo> function_share = NewSharedFunctionInfo(name);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001078 Handle<Map> map = (language_mode == CLASSIC_MODE)
1079 ? isolate()->function_without_prototype_map()
1080 : isolate()->strict_mode_function_without_prototype_map();
Steve Block44f0eee2011-05-26 01:26:41 +01001081 CALL_HEAP_FUNCTION(isolate(),
1082 isolate()->heap()->AllocateFunction(
1083 *map,
Steve Block6ded16b2010-05-10 14:33:55 +01001084 *function_share,
1085 *the_hole_value()),
1086 JSFunction);
1087}
1088
1089
Steve Block44f0eee2011-05-26 01:26:41 +01001090Handle<JSFunction> Factory::NewFunctionWithoutPrototype(
1091 Handle<String> name,
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001092 LanguageMode language_mode) {
1093 Handle<JSFunction> fun =
1094 NewFunctionWithoutPrototypeHelper(name, language_mode);
Steve Block44f0eee2011-05-26 01:26:41 +01001095 fun->set_context(isolate()->context()->global_context());
Steve Block6ded16b2010-05-10 14:33:55 +01001096 return fun;
1097}
1098
1099
Leon Clarkee46be812010-01-19 14:06:41 +00001100Handle<Object> Factory::ToObject(Handle<Object> object) {
Steve Block44f0eee2011-05-26 01:26:41 +01001101 CALL_HEAP_FUNCTION(isolate(), object->ToObject(), Object);
Leon Clarkee46be812010-01-19 14:06:41 +00001102}
1103
1104
Steve Blocka7e24c12009-10-30 11:49:00 +00001105Handle<Object> Factory::ToObject(Handle<Object> object,
1106 Handle<Context> global_context) {
Steve Block44f0eee2011-05-26 01:26:41 +01001107 CALL_HEAP_FUNCTION(isolate(), object->ToObject(*global_context), Object);
Steve Blocka7e24c12009-10-30 11:49:00 +00001108}
1109
1110
1111#ifdef ENABLE_DEBUGGER_SUPPORT
1112Handle<DebugInfo> Factory::NewDebugInfo(Handle<SharedFunctionInfo> shared) {
1113 // Get the original code of the function.
1114 Handle<Code> code(shared->code());
1115
1116 // Create a copy of the code before allocating the debug info object to avoid
1117 // allocation while setting up the debug info object.
1118 Handle<Code> original_code(*Factory::CopyCode(code));
1119
1120 // Allocate initial fixed array for active break points before allocating the
1121 // debug info object to avoid allocation while setting up the debug info
1122 // object.
1123 Handle<FixedArray> break_points(
Steve Block44f0eee2011-05-26 01:26:41 +01001124 NewFixedArray(Debug::kEstimatedNofBreakPointsInFunction));
Steve Blocka7e24c12009-10-30 11:49:00 +00001125
1126 // Create and set up the debug info object. Debug info contains function, a
1127 // copy of the original code, the executing code and initial fixed array for
1128 // active break points.
1129 Handle<DebugInfo> debug_info =
Steve Block44f0eee2011-05-26 01:26:41 +01001130 Handle<DebugInfo>::cast(NewStruct(DEBUG_INFO_TYPE));
Steve Blocka7e24c12009-10-30 11:49:00 +00001131 debug_info->set_shared(*shared);
1132 debug_info->set_original_code(*original_code);
1133 debug_info->set_code(*code);
1134 debug_info->set_break_points(*break_points);
1135
1136 // Link debug info to function.
1137 shared->set_debug_info(*debug_info);
1138
1139 return debug_info;
1140}
1141#endif
1142
1143
1144Handle<JSObject> Factory::NewArgumentsObject(Handle<Object> callee,
1145 int length) {
Steve Block44f0eee2011-05-26 01:26:41 +01001146 CALL_HEAP_FUNCTION(
1147 isolate(),
1148 isolate()->heap()->AllocateArgumentsObject(*callee, length), JSObject);
Steve Blocka7e24c12009-10-30 11:49:00 +00001149}
1150
1151
1152Handle<JSFunction> Factory::CreateApiFunction(
1153 Handle<FunctionTemplateInfo> obj, ApiInstanceType instance_type) {
Steve Block44f0eee2011-05-26 01:26:41 +01001154 Handle<Code> code = isolate()->builtins()->HandleApiCall();
1155 Handle<Code> construct_stub = isolate()->builtins()->JSConstructStubApi();
Steve Blocka7e24c12009-10-30 11:49:00 +00001156
1157 int internal_field_count = 0;
1158 if (!obj->instance_template()->IsUndefined()) {
1159 Handle<ObjectTemplateInfo> instance_template =
1160 Handle<ObjectTemplateInfo>(
1161 ObjectTemplateInfo::cast(obj->instance_template()));
1162 internal_field_count =
1163 Smi::cast(instance_template->internal_field_count())->value();
1164 }
1165
1166 int instance_size = kPointerSize * internal_field_count;
1167 InstanceType type = INVALID_TYPE;
1168 switch (instance_type) {
1169 case JavaScriptObject:
1170 type = JS_OBJECT_TYPE;
1171 instance_size += JSObject::kHeaderSize;
1172 break;
1173 case InnerGlobalObject:
1174 type = JS_GLOBAL_OBJECT_TYPE;
1175 instance_size += JSGlobalObject::kSize;
1176 break;
1177 case OuterGlobalObject:
1178 type = JS_GLOBAL_PROXY_TYPE;
1179 instance_size += JSGlobalProxy::kSize;
1180 break;
1181 default:
1182 break;
1183 }
1184 ASSERT(type != INVALID_TYPE);
1185
1186 Handle<JSFunction> result =
Steve Block44f0eee2011-05-26 01:26:41 +01001187 NewFunction(Factory::empty_symbol(),
1188 type,
1189 instance_size,
1190 code,
1191 true);
Steve Blocka7e24c12009-10-30 11:49:00 +00001192 // Set class name.
1193 Handle<Object> class_name = Handle<Object>(obj->class_name());
1194 if (class_name->IsString()) {
1195 result->shared()->set_instance_class_name(*class_name);
1196 result->shared()->set_name(*class_name);
1197 }
1198
1199 Handle<Map> map = Handle<Map>(result->initial_map());
1200
1201 // Mark as undetectable if needed.
1202 if (obj->undetectable()) {
1203 map->set_is_undetectable();
1204 }
1205
1206 // Mark as hidden for the __proto__ accessor if needed.
1207 if (obj->hidden_prototype()) {
1208 map->set_is_hidden_prototype();
1209 }
1210
1211 // Mark as needs_access_check if needed.
1212 if (obj->needs_access_check()) {
1213 map->set_is_access_check_needed(true);
1214 }
1215
1216 // Set interceptor information in the map.
1217 if (!obj->named_property_handler()->IsUndefined()) {
1218 map->set_has_named_interceptor();
1219 }
1220 if (!obj->indexed_property_handler()->IsUndefined()) {
1221 map->set_has_indexed_interceptor();
1222 }
1223
1224 // Set instance call-as-function information in the map.
1225 if (!obj->instance_call_handler()->IsUndefined()) {
1226 map->set_has_instance_call_handler();
1227 }
1228
1229 result->shared()->set_function_data(*obj);
Leon Clarkee46be812010-01-19 14:06:41 +00001230 result->shared()->set_construct_stub(*construct_stub);
Steve Blocka7e24c12009-10-30 11:49:00 +00001231 result->shared()->DontAdaptArguments();
1232
1233 // Recursively copy parent templates' accessors, 'data' may be modified.
1234 Handle<DescriptorArray> array =
1235 Handle<DescriptorArray>(map->instance_descriptors());
1236 while (true) {
1237 Handle<Object> props = Handle<Object>(obj->property_accessors());
1238 if (!props->IsUndefined()) {
Steve Block44f0eee2011-05-26 01:26:41 +01001239 array = CopyAppendCallbackDescriptors(array, props);
Steve Blocka7e24c12009-10-30 11:49:00 +00001240 }
1241 Handle<Object> parent = Handle<Object>(obj->parent_template());
1242 if (parent->IsUndefined()) break;
1243 obj = Handle<FunctionTemplateInfo>::cast(parent);
1244 }
1245 if (!array->IsEmpty()) {
1246 map->set_instance_descriptors(*array);
1247 }
1248
Steve Block6ded16b2010-05-10 14:33:55 +01001249 ASSERT(result->shared()->IsApiFunction());
Steve Blocka7e24c12009-10-30 11:49:00 +00001250 return result;
1251}
1252
1253
1254Handle<MapCache> Factory::NewMapCache(int at_least_space_for) {
Steve Block44f0eee2011-05-26 01:26:41 +01001255 CALL_HEAP_FUNCTION(isolate(),
1256 MapCache::Allocate(at_least_space_for), MapCache);
Steve Blocka7e24c12009-10-30 11:49:00 +00001257}
1258
1259
John Reck59135872010-11-02 12:39:01 -07001260MUST_USE_RESULT static MaybeObject* UpdateMapCacheWith(Context* context,
1261 FixedArray* keys,
1262 Map* map) {
1263 Object* result;
1264 { MaybeObject* maybe_result =
1265 MapCache::cast(context->map_cache())->Put(keys, map);
1266 if (!maybe_result->ToObject(&result)) return maybe_result;
1267 }
1268 context->set_map_cache(MapCache::cast(result));
Steve Blocka7e24c12009-10-30 11:49:00 +00001269 return result;
1270}
1271
1272
1273Handle<MapCache> Factory::AddToMapCache(Handle<Context> context,
1274 Handle<FixedArray> keys,
1275 Handle<Map> map) {
Steve Block44f0eee2011-05-26 01:26:41 +01001276 CALL_HEAP_FUNCTION(isolate(),
1277 UpdateMapCacheWith(*context, *keys, *map), MapCache);
Steve Blocka7e24c12009-10-30 11:49:00 +00001278}
1279
1280
1281Handle<Map> Factory::ObjectLiteralMapFromCache(Handle<Context> context,
1282 Handle<FixedArray> keys) {
1283 if (context->map_cache()->IsUndefined()) {
1284 // Allocate the new map cache for the global context.
1285 Handle<MapCache> new_cache = NewMapCache(24);
1286 context->set_map_cache(*new_cache);
1287 }
1288 // Check to see whether there is a matching element in the cache.
1289 Handle<MapCache> cache =
1290 Handle<MapCache>(MapCache::cast(context->map_cache()));
1291 Handle<Object> result = Handle<Object>(cache->Lookup(*keys));
1292 if (result->IsMap()) return Handle<Map>::cast(result);
1293 // Create a new map and add it to the cache.
1294 Handle<Map> map =
1295 CopyMap(Handle<Map>(context->object_function()->initial_map()),
1296 keys->length());
1297 AddToMapCache(context, keys, map);
1298 return Handle<Map>(map);
1299}
1300
1301
1302void Factory::SetRegExpAtomData(Handle<JSRegExp> regexp,
1303 JSRegExp::Type type,
1304 Handle<String> source,
1305 JSRegExp::Flags flags,
1306 Handle<Object> data) {
1307 Handle<FixedArray> store = NewFixedArray(JSRegExp::kAtomDataSize);
1308
1309 store->set(JSRegExp::kTagIndex, Smi::FromInt(type));
1310 store->set(JSRegExp::kSourceIndex, *source);
1311 store->set(JSRegExp::kFlagsIndex, Smi::FromInt(flags.value()));
1312 store->set(JSRegExp::kAtomPatternIndex, *data);
1313 regexp->set_data(*store);
1314}
1315
1316void Factory::SetRegExpIrregexpData(Handle<JSRegExp> regexp,
1317 JSRegExp::Type type,
1318 Handle<String> source,
1319 JSRegExp::Flags flags,
1320 int capture_count) {
1321 Handle<FixedArray> store = NewFixedArray(JSRegExp::kIrregexpDataSize);
Ben Murdoch257744e2011-11-30 15:57:28 +00001322 Smi* uninitialized = Smi::FromInt(JSRegExp::kUninitializedValue);
Steve Blocka7e24c12009-10-30 11:49:00 +00001323 store->set(JSRegExp::kTagIndex, Smi::FromInt(type));
1324 store->set(JSRegExp::kSourceIndex, *source);
1325 store->set(JSRegExp::kFlagsIndex, Smi::FromInt(flags.value()));
Ben Murdoch257744e2011-11-30 15:57:28 +00001326 store->set(JSRegExp::kIrregexpASCIICodeIndex, uninitialized);
1327 store->set(JSRegExp::kIrregexpUC16CodeIndex, uninitialized);
1328 store->set(JSRegExp::kIrregexpASCIICodeSavedIndex, uninitialized);
1329 store->set(JSRegExp::kIrregexpUC16CodeSavedIndex, uninitialized);
Steve Blocka7e24c12009-10-30 11:49:00 +00001330 store->set(JSRegExp::kIrregexpMaxRegisterCountIndex, Smi::FromInt(0));
1331 store->set(JSRegExp::kIrregexpCaptureCountIndex,
1332 Smi::FromInt(capture_count));
1333 regexp->set_data(*store);
1334}
1335
1336
1337
1338void Factory::ConfigureInstance(Handle<FunctionTemplateInfo> desc,
1339 Handle<JSObject> instance,
1340 bool* pending_exception) {
1341 // Configure the instance by adding the properties specified by the
1342 // instance template.
1343 Handle<Object> instance_template = Handle<Object>(desc->instance_template());
1344 if (!instance_template->IsUndefined()) {
1345 Execution::ConfigureInstance(instance,
1346 instance_template,
1347 pending_exception);
1348 } else {
1349 *pending_exception = false;
1350 }
1351}
1352
1353
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001354Handle<Object> Factory::GlobalConstantFor(Handle<String> name) {
1355 Heap* h = isolate()->heap();
1356 if (name->Equals(h->undefined_symbol())) return undefined_value();
1357 if (name->Equals(h->nan_symbol())) return nan_value();
1358 if (name->Equals(h->infinity_symbol())) return infinity_value();
1359 return Handle<Object>::null();
1360}
1361
1362
1363Handle<Object> Factory::ToBoolean(bool value) {
1364 return Handle<Object>(value
1365 ? isolate()->heap()->true_value()
1366 : isolate()->heap()->false_value());
1367}
1368
1369
Steve Blocka7e24c12009-10-30 11:49:00 +00001370} } // namespace v8::internal