blob: 06d1655fbb8926f57ae47f66fd852bda8a4bf64f [file] [log] [blame]
Ben Murdochb0fe1622011-05-05 13:52:32 +01001// Copyright 2010 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"
Steve Blocka7e24c12009-10-30 11:49:00 +000037
38namespace v8 {
39namespace internal {
40
41
42Handle<FixedArray> Factory::NewFixedArray(int size, PretenureFlag pretenure) {
43 ASSERT(0 <= size);
Steve Block44f0eee2011-05-26 01:26:41 +010044 CALL_HEAP_FUNCTION(
45 isolate(),
46 isolate()->heap()->AllocateFixedArray(size, pretenure),
47 FixedArray);
Steve Blocka7e24c12009-10-30 11:49:00 +000048}
49
50
Steve Block6ded16b2010-05-10 14:33:55 +010051Handle<FixedArray> Factory::NewFixedArrayWithHoles(int size,
52 PretenureFlag pretenure) {
Steve Blocka7e24c12009-10-30 11:49:00 +000053 ASSERT(0 <= size);
Steve Block44f0eee2011-05-26 01:26:41 +010054 CALL_HEAP_FUNCTION(
55 isolate(),
56 isolate()->heap()->AllocateFixedArrayWithHoles(size, pretenure),
57 FixedArray);
Steve Blocka7e24c12009-10-30 11:49:00 +000058}
59
60
61Handle<StringDictionary> Factory::NewStringDictionary(int at_least_space_for) {
62 ASSERT(0 <= at_least_space_for);
Steve Block44f0eee2011-05-26 01:26:41 +010063 CALL_HEAP_FUNCTION(isolate(),
64 StringDictionary::Allocate(at_least_space_for),
Steve Blocka7e24c12009-10-30 11:49:00 +000065 StringDictionary);
66}
67
68
69Handle<NumberDictionary> Factory::NewNumberDictionary(int at_least_space_for) {
70 ASSERT(0 <= at_least_space_for);
Steve Block44f0eee2011-05-26 01:26:41 +010071 CALL_HEAP_FUNCTION(isolate(),
72 NumberDictionary::Allocate(at_least_space_for),
Steve Blocka7e24c12009-10-30 11:49:00 +000073 NumberDictionary);
74}
75
76
77Handle<DescriptorArray> Factory::NewDescriptorArray(int number_of_descriptors) {
78 ASSERT(0 <= number_of_descriptors);
Steve Block44f0eee2011-05-26 01:26:41 +010079 CALL_HEAP_FUNCTION(isolate(),
80 DescriptorArray::Allocate(number_of_descriptors),
Steve Blocka7e24c12009-10-30 11:49:00 +000081 DescriptorArray);
82}
83
84
Ben Murdochb0fe1622011-05-05 13:52:32 +010085Handle<DeoptimizationInputData> Factory::NewDeoptimizationInputData(
86 int deopt_entry_count,
87 PretenureFlag pretenure) {
88 ASSERT(deopt_entry_count > 0);
Steve Block44f0eee2011-05-26 01:26:41 +010089 CALL_HEAP_FUNCTION(isolate(),
90 DeoptimizationInputData::Allocate(deopt_entry_count,
Ben Murdochb0fe1622011-05-05 13:52:32 +010091 pretenure),
92 DeoptimizationInputData);
93}
94
95
96Handle<DeoptimizationOutputData> Factory::NewDeoptimizationOutputData(
97 int deopt_entry_count,
98 PretenureFlag pretenure) {
99 ASSERT(deopt_entry_count > 0);
Steve Block44f0eee2011-05-26 01:26:41 +0100100 CALL_HEAP_FUNCTION(isolate(),
101 DeoptimizationOutputData::Allocate(deopt_entry_count,
Ben Murdochb0fe1622011-05-05 13:52:32 +0100102 pretenure),
103 DeoptimizationOutputData);
104}
105
106
Steve Blocka7e24c12009-10-30 11:49:00 +0000107// Symbols are created in the old generation (data space).
108Handle<String> Factory::LookupSymbol(Vector<const char> string) {
Steve Block44f0eee2011-05-26 01:26:41 +0100109 CALL_HEAP_FUNCTION(isolate(),
110 isolate()->heap()->LookupSymbol(string),
111 String);
Steve Blocka7e24c12009-10-30 11:49:00 +0000112}
113
Ben Murdoch257744e2011-11-30 15:57:28 +0000114// Symbols are created in the old generation (data space).
115Handle<String> Factory::LookupSymbol(Handle<String> string) {
116 CALL_HEAP_FUNCTION(isolate(),
117 isolate()->heap()->LookupSymbol(*string),
118 String);
119}
120
Steve Block9fac8402011-05-12 15:51:54 +0100121Handle<String> Factory::LookupAsciiSymbol(Vector<const char> string) {
Steve Block44f0eee2011-05-26 01:26:41 +0100122 CALL_HEAP_FUNCTION(isolate(),
123 isolate()->heap()->LookupAsciiSymbol(string),
124 String);
Steve Block9fac8402011-05-12 15:51:54 +0100125}
126
Ben Murdoch257744e2011-11-30 15:57:28 +0000127
128Handle<String> Factory::LookupAsciiSymbol(Handle<SeqAsciiString> string,
129 int from,
130 int length) {
131 CALL_HEAP_FUNCTION(isolate(),
132 isolate()->heap()->LookupAsciiSymbol(string,
133 from,
134 length),
135 String);
136}
137
138
Steve Block9fac8402011-05-12 15:51:54 +0100139Handle<String> Factory::LookupTwoByteSymbol(Vector<const uc16> string) {
Steve Block44f0eee2011-05-26 01:26:41 +0100140 CALL_HEAP_FUNCTION(isolate(),
141 isolate()->heap()->LookupTwoByteSymbol(string),
142 String);
Steve Block9fac8402011-05-12 15:51:54 +0100143}
144
Steve Blocka7e24c12009-10-30 11:49:00 +0000145
146Handle<String> Factory::NewStringFromAscii(Vector<const char> string,
147 PretenureFlag pretenure) {
Steve Block44f0eee2011-05-26 01:26:41 +0100148 CALL_HEAP_FUNCTION(
149 isolate(),
150 isolate()->heap()->AllocateStringFromAscii(string, pretenure),
151 String);
Steve Blocka7e24c12009-10-30 11:49:00 +0000152}
153
154Handle<String> Factory::NewStringFromUtf8(Vector<const char> string,
155 PretenureFlag pretenure) {
Steve Block44f0eee2011-05-26 01:26:41 +0100156 CALL_HEAP_FUNCTION(
157 isolate(),
158 isolate()->heap()->AllocateStringFromUtf8(string, pretenure),
159 String);
Steve Blocka7e24c12009-10-30 11:49:00 +0000160}
161
162
163Handle<String> Factory::NewStringFromTwoByte(Vector<const uc16> string,
164 PretenureFlag pretenure) {
Steve Block44f0eee2011-05-26 01:26:41 +0100165 CALL_HEAP_FUNCTION(
166 isolate(),
167 isolate()->heap()->AllocateStringFromTwoByte(string, pretenure),
168 String);
Steve Blocka7e24c12009-10-30 11:49:00 +0000169}
170
171
Leon Clarkeac952652010-07-15 11:15:24 +0100172Handle<String> Factory::NewRawAsciiString(int length,
173 PretenureFlag pretenure) {
Steve Block44f0eee2011-05-26 01:26:41 +0100174 CALL_HEAP_FUNCTION(
175 isolate(),
176 isolate()->heap()->AllocateRawAsciiString(length, pretenure),
177 String);
Leon Clarkeac952652010-07-15 11:15:24 +0100178}
179
180
Steve Blocka7e24c12009-10-30 11:49:00 +0000181Handle<String> Factory::NewRawTwoByteString(int length,
182 PretenureFlag pretenure) {
Steve Block44f0eee2011-05-26 01:26:41 +0100183 CALL_HEAP_FUNCTION(
184 isolate(),
185 isolate()->heap()->AllocateRawTwoByteString(length, pretenure),
186 String);
Steve Blocka7e24c12009-10-30 11:49:00 +0000187}
188
189
190Handle<String> Factory::NewConsString(Handle<String> first,
191 Handle<String> second) {
Steve Block44f0eee2011-05-26 01:26:41 +0100192 CALL_HEAP_FUNCTION(isolate(),
193 isolate()->heap()->AllocateConsString(*first, *second),
194 String);
Steve Blocka7e24c12009-10-30 11:49:00 +0000195}
196
197
Steve Blockd0582a62009-12-15 09:54:21 +0000198Handle<String> Factory::NewSubString(Handle<String> str,
199 int begin,
200 int end) {
Steve Block44f0eee2011-05-26 01:26:41 +0100201 CALL_HEAP_FUNCTION(isolate(),
202 str->SubString(begin, end),
203 String);
Steve Blocka7e24c12009-10-30 11:49:00 +0000204}
205
206
207Handle<String> Factory::NewExternalStringFromAscii(
208 ExternalAsciiString::Resource* resource) {
Steve Block44f0eee2011-05-26 01:26:41 +0100209 CALL_HEAP_FUNCTION(
210 isolate(),
211 isolate()->heap()->AllocateExternalStringFromAscii(resource),
212 String);
Steve Blocka7e24c12009-10-30 11:49:00 +0000213}
214
215
216Handle<String> Factory::NewExternalStringFromTwoByte(
217 ExternalTwoByteString::Resource* resource) {
Steve Block44f0eee2011-05-26 01:26:41 +0100218 CALL_HEAP_FUNCTION(
219 isolate(),
220 isolate()->heap()->AllocateExternalStringFromTwoByte(resource),
221 String);
Steve Blocka7e24c12009-10-30 11:49:00 +0000222}
223
224
225Handle<Context> Factory::NewGlobalContext() {
Steve Block44f0eee2011-05-26 01:26:41 +0100226 CALL_HEAP_FUNCTION(
227 isolate(),
228 isolate()->heap()->AllocateGlobalContext(),
229 Context);
Steve Blocka7e24c12009-10-30 11:49:00 +0000230}
231
232
233Handle<Context> Factory::NewFunctionContext(int length,
234 Handle<JSFunction> closure) {
Steve Block44f0eee2011-05-26 01:26:41 +0100235 CALL_HEAP_FUNCTION(
236 isolate(),
237 isolate()->heap()->AllocateFunctionContext(length, *closure),
238 Context);
Steve Blocka7e24c12009-10-30 11:49:00 +0000239}
240
241
242Handle<Context> Factory::NewWithContext(Handle<Context> previous,
243 Handle<JSObject> extension,
244 bool is_catch_context) {
Steve Block44f0eee2011-05-26 01:26:41 +0100245 CALL_HEAP_FUNCTION(
246 isolate(),
247 isolate()->heap()->AllocateWithContext(*previous,
248 *extension,
249 is_catch_context),
250 Context);
Steve Blocka7e24c12009-10-30 11:49:00 +0000251}
252
253
254Handle<Struct> Factory::NewStruct(InstanceType type) {
Steve Block44f0eee2011-05-26 01:26:41 +0100255 CALL_HEAP_FUNCTION(
256 isolate(),
257 isolate()->heap()->AllocateStruct(type),
258 Struct);
Steve Blocka7e24c12009-10-30 11:49:00 +0000259}
260
261
262Handle<AccessorInfo> Factory::NewAccessorInfo() {
263 Handle<AccessorInfo> info =
264 Handle<AccessorInfo>::cast(NewStruct(ACCESSOR_INFO_TYPE));
265 info->set_flag(0); // Must clear the flag, it was initialized as undefined.
266 return info;
267}
268
269
270Handle<Script> Factory::NewScript(Handle<String> source) {
271 // Generate id for this script.
272 int id;
Steve Block44f0eee2011-05-26 01:26:41 +0100273 Heap* heap = isolate()->heap();
274 if (heap->last_script_id()->IsUndefined()) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000275 // Script ids start from one.
276 id = 1;
277 } else {
278 // Increment id, wrap when positive smi is exhausted.
Steve Block44f0eee2011-05-26 01:26:41 +0100279 id = Smi::cast(heap->last_script_id())->value();
Steve Blocka7e24c12009-10-30 11:49:00 +0000280 id++;
281 if (!Smi::IsValid(id)) {
282 id = 0;
283 }
284 }
Steve Block44f0eee2011-05-26 01:26:41 +0100285 heap->SetLastScriptId(Smi::FromInt(id));
Steve Blocka7e24c12009-10-30 11:49:00 +0000286
287 // Create and initialize script object.
Ben Murdoch257744e2011-11-30 15:57:28 +0000288 Handle<Foreign> wrapper = NewForeign(0, TENURED);
Steve Blocka7e24c12009-10-30 11:49:00 +0000289 Handle<Script> script = Handle<Script>::cast(NewStruct(SCRIPT_TYPE));
290 script->set_source(*source);
Steve Block44f0eee2011-05-26 01:26:41 +0100291 script->set_name(heap->undefined_value());
292 script->set_id(heap->last_script_id());
Steve Blocka7e24c12009-10-30 11:49:00 +0000293 script->set_line_offset(Smi::FromInt(0));
294 script->set_column_offset(Smi::FromInt(0));
Steve Block44f0eee2011-05-26 01:26:41 +0100295 script->set_data(heap->undefined_value());
296 script->set_context_data(heap->undefined_value());
Steve Blocka7e24c12009-10-30 11:49:00 +0000297 script->set_type(Smi::FromInt(Script::TYPE_NORMAL));
298 script->set_compilation_type(Smi::FromInt(Script::COMPILATION_TYPE_HOST));
299 script->set_wrapper(*wrapper);
Steve Block44f0eee2011-05-26 01:26:41 +0100300 script->set_line_ends(heap->undefined_value());
301 script->set_eval_from_shared(heap->undefined_value());
Steve Blocka7e24c12009-10-30 11:49:00 +0000302 script->set_eval_from_instructions_offset(Smi::FromInt(0));
303
304 return script;
305}
306
307
Ben Murdoch257744e2011-11-30 15:57:28 +0000308Handle<Foreign> Factory::NewForeign(Address addr, PretenureFlag pretenure) {
Steve Block44f0eee2011-05-26 01:26:41 +0100309 CALL_HEAP_FUNCTION(isolate(),
Ben Murdoch257744e2011-11-30 15:57:28 +0000310 isolate()->heap()->AllocateForeign(addr, pretenure),
311 Foreign);
Steve Blocka7e24c12009-10-30 11:49:00 +0000312}
313
314
Ben Murdoch257744e2011-11-30 15:57:28 +0000315Handle<Foreign> Factory::NewForeign(const AccessorDescriptor* desc) {
316 return NewForeign((Address) desc, TENURED);
Steve Blocka7e24c12009-10-30 11:49:00 +0000317}
318
319
320Handle<ByteArray> Factory::NewByteArray(int length, PretenureFlag pretenure) {
321 ASSERT(0 <= length);
Steve Block44f0eee2011-05-26 01:26:41 +0100322 CALL_HEAP_FUNCTION(
323 isolate(),
324 isolate()->heap()->AllocateByteArray(length, pretenure),
325 ByteArray);
Steve Blocka7e24c12009-10-30 11:49:00 +0000326}
327
328
Steve Block3ce2e202009-11-05 08:53:23 +0000329Handle<ExternalArray> Factory::NewExternalArray(int length,
330 ExternalArrayType array_type,
331 void* external_pointer,
332 PretenureFlag pretenure) {
333 ASSERT(0 <= length);
Steve Block44f0eee2011-05-26 01:26:41 +0100334 CALL_HEAP_FUNCTION(
335 isolate(),
336 isolate()->heap()->AllocateExternalArray(length,
337 array_type,
338 external_pointer,
339 pretenure),
340 ExternalArray);
Steve Block3ce2e202009-11-05 08:53:23 +0000341}
342
343
Ben Murdochb0fe1622011-05-05 13:52:32 +0100344Handle<JSGlobalPropertyCell> Factory::NewJSGlobalPropertyCell(
345 Handle<Object> value) {
Steve Block44f0eee2011-05-26 01:26:41 +0100346 CALL_HEAP_FUNCTION(
347 isolate(),
348 isolate()->heap()->AllocateJSGlobalPropertyCell(*value),
349 JSGlobalPropertyCell);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100350}
351
352
Steve Blocka7e24c12009-10-30 11:49:00 +0000353Handle<Map> Factory::NewMap(InstanceType type, int instance_size) {
Steve Block44f0eee2011-05-26 01:26:41 +0100354 CALL_HEAP_FUNCTION(
355 isolate(),
356 isolate()->heap()->AllocateMap(type, instance_size),
357 Map);
Steve Blocka7e24c12009-10-30 11:49:00 +0000358}
359
360
361Handle<JSObject> Factory::NewFunctionPrototype(Handle<JSFunction> function) {
Steve Block44f0eee2011-05-26 01:26:41 +0100362 CALL_HEAP_FUNCTION(
363 isolate(),
364 isolate()->heap()->AllocateFunctionPrototype(*function),
365 JSObject);
Steve Blocka7e24c12009-10-30 11:49:00 +0000366}
367
368
369Handle<Map> Factory::CopyMapDropDescriptors(Handle<Map> src) {
Steve Block44f0eee2011-05-26 01:26:41 +0100370 CALL_HEAP_FUNCTION(isolate(), src->CopyDropDescriptors(), Map);
Steve Blocka7e24c12009-10-30 11:49:00 +0000371}
372
373
374Handle<Map> Factory::CopyMap(Handle<Map> src,
375 int extra_inobject_properties) {
376 Handle<Map> copy = CopyMapDropDescriptors(src);
377 // Check that we do not overflow the instance size when adding the
378 // extra inobject properties.
379 int instance_size_delta = extra_inobject_properties * kPointerSize;
380 int max_instance_size_delta =
381 JSObject::kMaxInstanceSize - copy->instance_size();
382 if (instance_size_delta > max_instance_size_delta) {
383 // If the instance size overflows, we allocate as many properties
384 // as we can as inobject properties.
385 instance_size_delta = max_instance_size_delta;
386 extra_inobject_properties = max_instance_size_delta >> kPointerSizeLog2;
387 }
388 // Adjust the map with the extra inobject properties.
389 int inobject_properties =
390 copy->inobject_properties() + extra_inobject_properties;
391 copy->set_inobject_properties(inobject_properties);
392 copy->set_unused_property_fields(inobject_properties);
393 copy->set_instance_size(copy->instance_size() + instance_size_delta);
Iain Merrick75681382010-08-19 15:07:18 +0100394 copy->set_visitor_id(StaticVisitorBase::GetVisitorId(*copy));
Steve Blocka7e24c12009-10-30 11:49:00 +0000395 return copy;
396}
397
Steve Block8defd9f2010-07-08 12:39:36 +0100398
Steve Blocka7e24c12009-10-30 11:49:00 +0000399Handle<Map> Factory::CopyMapDropTransitions(Handle<Map> src) {
Steve Block44f0eee2011-05-26 01:26:41 +0100400 CALL_HEAP_FUNCTION(isolate(), src->CopyDropTransitions(), Map);
Steve Blocka7e24c12009-10-30 11:49:00 +0000401}
402
403
Steve Block8defd9f2010-07-08 12:39:36 +0100404Handle<Map> Factory::GetFastElementsMap(Handle<Map> src) {
Steve Block44f0eee2011-05-26 01:26:41 +0100405 CALL_HEAP_FUNCTION(isolate(), src->GetFastElementsMap(), Map);
Steve Block8defd9f2010-07-08 12:39:36 +0100406}
407
408
409Handle<Map> Factory::GetSlowElementsMap(Handle<Map> src) {
Steve Block44f0eee2011-05-26 01:26:41 +0100410 CALL_HEAP_FUNCTION(isolate(), src->GetSlowElementsMap(), Map);
Steve Block8defd9f2010-07-08 12:39:36 +0100411}
412
413
Steve Block44f0eee2011-05-26 01:26:41 +0100414Handle<Map> Factory::GetExternalArrayElementsMap(
415 Handle<Map> src,
416 ExternalArrayType array_type,
417 bool safe_to_add_transition) {
418 CALL_HEAP_FUNCTION(isolate(),
419 src->GetExternalArrayElementsMap(array_type,
420 safe_to_add_transition),
421 Map);
Steve Block1e0659c2011-05-24 12:43:12 +0100422}
423
424
Steve Blocka7e24c12009-10-30 11:49:00 +0000425Handle<FixedArray> Factory::CopyFixedArray(Handle<FixedArray> array) {
Steve Block44f0eee2011-05-26 01:26:41 +0100426 CALL_HEAP_FUNCTION(isolate(), array->Copy(), FixedArray);
Steve Blocka7e24c12009-10-30 11:49:00 +0000427}
428
429
Steve Block6ded16b2010-05-10 14:33:55 +0100430Handle<JSFunction> Factory::BaseNewFunctionFromSharedFunctionInfo(
431 Handle<SharedFunctionInfo> function_info,
Leon Clarkee46be812010-01-19 14:06:41 +0000432 Handle<Map> function_map,
433 PretenureFlag pretenure) {
Steve Block44f0eee2011-05-26 01:26:41 +0100434 CALL_HEAP_FUNCTION(
435 isolate(),
436 isolate()->heap()->AllocateFunction(*function_map,
437 *function_info,
438 isolate()->heap()->the_hole_value(),
439 pretenure),
Steve Blocka7e24c12009-10-30 11:49:00 +0000440 JSFunction);
441}
442
443
Steve Block6ded16b2010-05-10 14:33:55 +0100444Handle<JSFunction> Factory::NewFunctionFromSharedFunctionInfo(
445 Handle<SharedFunctionInfo> function_info,
Leon Clarkee46be812010-01-19 14:06:41 +0000446 Handle<Context> context,
447 PretenureFlag pretenure) {
Steve Block6ded16b2010-05-10 14:33:55 +0100448 Handle<JSFunction> result = BaseNewFunctionFromSharedFunctionInfo(
Steve Block44f0eee2011-05-26 01:26:41 +0100449 function_info,
450 function_info->strict_mode()
451 ? isolate()->strict_mode_function_map()
452 : isolate()->function_map(),
453 pretenure);
454
Steve Blocka7e24c12009-10-30 11:49:00 +0000455 result->set_context(*context);
Steve Block6ded16b2010-05-10 14:33:55 +0100456 int number_of_literals = function_info->num_literals();
Steve Block44f0eee2011-05-26 01:26:41 +0100457 Handle<FixedArray> literals = NewFixedArray(number_of_literals, pretenure);
Steve Blocka7e24c12009-10-30 11:49:00 +0000458 if (number_of_literals > 0) {
459 // Store the object, regexp and array functions in the literals
460 // array prefix. These functions will be used when creating
461 // object, regexp and array literals in this function.
462 literals->set(JSFunction::kLiteralGlobalContextIndex,
463 context->global_context());
464 }
465 result->set_literals(*literals);
Steve Block44f0eee2011-05-26 01:26:41 +0100466 result->set_next_function_link(isolate()->heap()->undefined_value());
Ben Murdochb0fe1622011-05-05 13:52:32 +0100467
468 if (V8::UseCrankshaft() &&
469 FLAG_always_opt &&
470 result->is_compiled() &&
471 !function_info->is_toplevel() &&
472 function_info->allows_lazy_compilation()) {
473 result->MarkForLazyRecompilation();
474 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000475 return result;
476}
477
478
479Handle<Object> Factory::NewNumber(double value,
480 PretenureFlag pretenure) {
Steve Block44f0eee2011-05-26 01:26:41 +0100481 CALL_HEAP_FUNCTION(
482 isolate(),
483 isolate()->heap()->NumberFromDouble(value, pretenure), Object);
Steve Blocka7e24c12009-10-30 11:49:00 +0000484}
485
486
487Handle<Object> Factory::NewNumberFromInt(int value) {
Steve Block44f0eee2011-05-26 01:26:41 +0100488 CALL_HEAP_FUNCTION(
489 isolate(),
490 isolate()->heap()->NumberFromInt32(value), Object);
Steve Blocka7e24c12009-10-30 11:49:00 +0000491}
492
493
494Handle<Object> Factory::NewNumberFromUint(uint32_t value) {
Steve Block44f0eee2011-05-26 01:26:41 +0100495 CALL_HEAP_FUNCTION(
496 isolate(),
497 isolate()->heap()->NumberFromUint32(value), Object);
Steve Blocka7e24c12009-10-30 11:49:00 +0000498}
499
500
501Handle<JSObject> Factory::NewNeanderObject() {
Steve Block44f0eee2011-05-26 01:26:41 +0100502 CALL_HEAP_FUNCTION(
503 isolate(),
504 isolate()->heap()->AllocateJSObjectFromMap(
505 isolate()->heap()->neander_map()),
506 JSObject);
Steve Blocka7e24c12009-10-30 11:49:00 +0000507}
508
509
510Handle<Object> Factory::NewTypeError(const char* type,
511 Vector< Handle<Object> > args) {
512 return NewError("MakeTypeError", type, args);
513}
514
515
516Handle<Object> Factory::NewTypeError(Handle<String> message) {
517 return NewError("$TypeError", message);
518}
519
520
521Handle<Object> Factory::NewRangeError(const char* type,
522 Vector< Handle<Object> > args) {
523 return NewError("MakeRangeError", type, args);
524}
525
526
527Handle<Object> Factory::NewRangeError(Handle<String> message) {
528 return NewError("$RangeError", message);
529}
530
531
532Handle<Object> Factory::NewSyntaxError(const char* type, Handle<JSArray> args) {
533 return NewError("MakeSyntaxError", type, args);
534}
535
536
537Handle<Object> Factory::NewSyntaxError(Handle<String> message) {
538 return NewError("$SyntaxError", message);
539}
540
541
542Handle<Object> Factory::NewReferenceError(const char* type,
543 Vector< Handle<Object> > args) {
544 return NewError("MakeReferenceError", type, args);
545}
546
547
548Handle<Object> Factory::NewReferenceError(Handle<String> message) {
549 return NewError("$ReferenceError", message);
550}
551
552
553Handle<Object> Factory::NewError(const char* maker, const char* type,
554 Vector< Handle<Object> > args) {
555 v8::HandleScope scope; // Instantiate a closeable HandleScope for EscapeFrom.
Steve Block44f0eee2011-05-26 01:26:41 +0100556 Handle<FixedArray> array = NewFixedArray(args.length());
Steve Blocka7e24c12009-10-30 11:49:00 +0000557 for (int i = 0; i < args.length(); i++) {
558 array->set(i, *args[i]);
559 }
Steve Block44f0eee2011-05-26 01:26:41 +0100560 Handle<JSArray> object = NewJSArrayWithElements(array);
Steve Blocka7e24c12009-10-30 11:49:00 +0000561 Handle<Object> result = NewError(maker, type, object);
562 return result.EscapeFrom(&scope);
563}
564
565
566Handle<Object> Factory::NewEvalError(const char* type,
567 Vector< Handle<Object> > args) {
568 return NewError("MakeEvalError", type, args);
569}
570
571
572Handle<Object> Factory::NewError(const char* type,
573 Vector< Handle<Object> > args) {
574 return NewError("MakeError", type, args);
575}
576
577
578Handle<Object> Factory::NewError(const char* maker,
579 const char* type,
580 Handle<JSArray> args) {
Steve Block44f0eee2011-05-26 01:26:41 +0100581 Handle<String> make_str = LookupAsciiSymbol(maker);
582 Handle<Object> fun_obj(
583 isolate()->js_builtins_object()->GetPropertyNoExceptionThrown(*make_str));
Steve Blocka7e24c12009-10-30 11:49:00 +0000584 // If the builtins haven't been properly configured yet this error
585 // constructor may not have been defined. Bail out.
586 if (!fun_obj->IsJSFunction())
Steve Block44f0eee2011-05-26 01:26:41 +0100587 return undefined_value();
Steve Blocka7e24c12009-10-30 11:49:00 +0000588 Handle<JSFunction> fun = Handle<JSFunction>::cast(fun_obj);
Steve Block44f0eee2011-05-26 01:26:41 +0100589 Handle<Object> type_obj = LookupAsciiSymbol(type);
Steve Blocka7e24c12009-10-30 11:49:00 +0000590 Object** argv[2] = { type_obj.location(),
591 Handle<Object>::cast(args).location() };
592
593 // Invoke the JavaScript factory method. If an exception is thrown while
594 // running the factory method, use the exception as the result.
595 bool caught_exception;
596 Handle<Object> result = Execution::TryCall(fun,
Steve Block44f0eee2011-05-26 01:26:41 +0100597 isolate()->js_builtins_object(), 2, argv, &caught_exception);
Steve Blocka7e24c12009-10-30 11:49:00 +0000598 return result;
599}
600
601
602Handle<Object> Factory::NewError(Handle<String> message) {
603 return NewError("$Error", message);
604}
605
606
607Handle<Object> Factory::NewError(const char* constructor,
608 Handle<String> message) {
Steve Block44f0eee2011-05-26 01:26:41 +0100609 Handle<String> constr = LookupAsciiSymbol(constructor);
610 Handle<JSFunction> fun = Handle<JSFunction>(
611 JSFunction::cast(isolate()->js_builtins_object()->
612 GetPropertyNoExceptionThrown(*constr)));
Steve Blocka7e24c12009-10-30 11:49:00 +0000613 Object** argv[1] = { Handle<Object>::cast(message).location() };
614
615 // Invoke the JavaScript factory method. If an exception is thrown while
616 // running the factory method, use the exception as the result.
617 bool caught_exception;
618 Handle<Object> result = Execution::TryCall(fun,
Steve Block44f0eee2011-05-26 01:26:41 +0100619 isolate()->js_builtins_object(), 1, argv, &caught_exception);
Steve Blocka7e24c12009-10-30 11:49:00 +0000620 return result;
621}
622
623
624Handle<JSFunction> Factory::NewFunction(Handle<String> name,
625 InstanceType type,
626 int instance_size,
627 Handle<Code> code,
628 bool force_initial_map) {
629 // Allocate the function
630 Handle<JSFunction> function = NewFunction(name, the_hole_value());
Iain Merrick75681382010-08-19 15:07:18 +0100631
632 // Setup the code pointer in both the shared function info and in
633 // the function itself.
634 function->shared()->set_code(*code);
Steve Blocka7e24c12009-10-30 11:49:00 +0000635 function->set_code(*code);
636
637 if (force_initial_map ||
638 type != JS_OBJECT_TYPE ||
639 instance_size != JSObject::kHeaderSize) {
640 Handle<Map> initial_map = NewMap(type, instance_size);
641 Handle<JSObject> prototype = NewFunctionPrototype(function);
642 initial_map->set_prototype(*prototype);
643 function->set_initial_map(*initial_map);
644 initial_map->set_constructor(*function);
645 } else {
646 ASSERT(!function->has_initial_map());
647 ASSERT(!function->has_prototype());
648 }
649
650 return function;
651}
652
653
Steve Blocka7e24c12009-10-30 11:49:00 +0000654Handle<JSFunction> Factory::NewFunctionWithPrototype(Handle<String> name,
655 InstanceType type,
656 int instance_size,
657 Handle<JSObject> prototype,
658 Handle<Code> code,
659 bool force_initial_map) {
Iain Merrick75681382010-08-19 15:07:18 +0100660 // Allocate the function.
Steve Blocka7e24c12009-10-30 11:49:00 +0000661 Handle<JSFunction> function = NewFunction(name, prototype);
662
Iain Merrick75681382010-08-19 15:07:18 +0100663 // Setup the code pointer in both the shared function info and in
664 // the function itself.
665 function->shared()->set_code(*code);
Steve Blocka7e24c12009-10-30 11:49:00 +0000666 function->set_code(*code);
667
668 if (force_initial_map ||
669 type != JS_OBJECT_TYPE ||
670 instance_size != JSObject::kHeaderSize) {
671 Handle<Map> initial_map = NewMap(type, instance_size);
672 function->set_initial_map(*initial_map);
673 initial_map->set_constructor(*function);
674 }
675
676 // Set function.prototype and give the prototype a constructor
677 // property that refers to the function.
678 SetPrototypeProperty(function, prototype);
Steve Block1e0659c2011-05-24 12:43:12 +0100679 // Currently safe because it is only invoked from Genesis.
Steve Block44f0eee2011-05-26 01:26:41 +0100680 SetLocalPropertyNoThrow(prototype, constructor_symbol(), function, DONT_ENUM);
Steve Blocka7e24c12009-10-30 11:49:00 +0000681 return function;
682}
683
684
Steve Block6ded16b2010-05-10 14:33:55 +0100685Handle<JSFunction> Factory::NewFunctionWithoutPrototype(Handle<String> name,
686 Handle<Code> code) {
Steve Block44f0eee2011-05-26 01:26:41 +0100687 Handle<JSFunction> function = NewFunctionWithoutPrototype(name,
688 kNonStrictMode);
Iain Merrick75681382010-08-19 15:07:18 +0100689 function->shared()->set_code(*code);
Steve Block6ded16b2010-05-10 14:33:55 +0100690 function->set_code(*code);
691 ASSERT(!function->has_initial_map());
692 ASSERT(!function->has_prototype());
693 return function;
694}
695
696
Steve Blocka7e24c12009-10-30 11:49:00 +0000697Handle<Code> Factory::NewCode(const CodeDesc& desc,
Steve Blocka7e24c12009-10-30 11:49:00 +0000698 Code::Flags flags,
Steve Block44f0eee2011-05-26 01:26:41 +0100699 Handle<Object> self_ref,
700 bool immovable) {
701 CALL_HEAP_FUNCTION(isolate(),
702 isolate()->heap()->CreateCode(
703 desc, flags, self_ref, immovable),
704 Code);
Steve Blocka7e24c12009-10-30 11:49:00 +0000705}
706
707
708Handle<Code> Factory::CopyCode(Handle<Code> code) {
Steve Block44f0eee2011-05-26 01:26:41 +0100709 CALL_HEAP_FUNCTION(isolate(),
710 isolate()->heap()->CopyCode(*code),
711 Code);
Steve Blocka7e24c12009-10-30 11:49:00 +0000712}
713
714
Steve Block6ded16b2010-05-10 14:33:55 +0100715Handle<Code> Factory::CopyCode(Handle<Code> code, Vector<byte> reloc_info) {
Steve Block44f0eee2011-05-26 01:26:41 +0100716 CALL_HEAP_FUNCTION(isolate(),
717 isolate()->heap()->CopyCode(*code, reloc_info),
718 Code);
Steve Block6ded16b2010-05-10 14:33:55 +0100719}
720
721
John Reck59135872010-11-02 12:39:01 -0700722MUST_USE_RESULT static inline MaybeObject* DoCopyInsert(
723 DescriptorArray* array,
724 String* key,
725 Object* value,
726 PropertyAttributes attributes) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000727 CallbacksDescriptor desc(key, value, attributes);
John Reck59135872010-11-02 12:39:01 -0700728 MaybeObject* obj = array->CopyInsert(&desc, REMOVE_TRANSITIONS);
Steve Blocka7e24c12009-10-30 11:49:00 +0000729 return obj;
730}
731
732
733// Allocate the new array.
Ben Murdoch257744e2011-11-30 15:57:28 +0000734Handle<DescriptorArray> Factory::CopyAppendForeignDescriptor(
Steve Blocka7e24c12009-10-30 11:49:00 +0000735 Handle<DescriptorArray> array,
736 Handle<String> key,
737 Handle<Object> value,
738 PropertyAttributes attributes) {
Steve Block44f0eee2011-05-26 01:26:41 +0100739 CALL_HEAP_FUNCTION(isolate(),
740 DoCopyInsert(*array, *key, *value, attributes),
Steve Blocka7e24c12009-10-30 11:49:00 +0000741 DescriptorArray);
742}
743
744
745Handle<String> Factory::SymbolFromString(Handle<String> value) {
Steve Block44f0eee2011-05-26 01:26:41 +0100746 CALL_HEAP_FUNCTION(isolate(),
747 isolate()->heap()->LookupSymbol(*value), String);
Steve Blocka7e24c12009-10-30 11:49:00 +0000748}
749
750
751Handle<DescriptorArray> Factory::CopyAppendCallbackDescriptors(
752 Handle<DescriptorArray> array,
753 Handle<Object> descriptors) {
754 v8::NeanderArray callbacks(descriptors);
755 int nof_callbacks = callbacks.length();
756 Handle<DescriptorArray> result =
757 NewDescriptorArray(array->number_of_descriptors() + nof_callbacks);
758
759 // Number of descriptors added to the result so far.
760 int descriptor_count = 0;
761
762 // Copy the descriptors from the array.
763 for (int i = 0; i < array->number_of_descriptors(); i++) {
764 if (array->GetType(i) != NULL_DESCRIPTOR) {
765 result->CopyFrom(descriptor_count++, *array, i);
766 }
767 }
768
769 // Number of duplicates detected.
770 int duplicates = 0;
771
772 // Fill in new callback descriptors. Process the callbacks from
773 // back to front so that the last callback with a given name takes
774 // precedence over previously added callbacks with that name.
775 for (int i = nof_callbacks - 1; i >= 0; i--) {
776 Handle<AccessorInfo> entry =
777 Handle<AccessorInfo>(AccessorInfo::cast(callbacks.get(i)));
778 // Ensure the key is a symbol before writing into the instance descriptor.
779 Handle<String> key =
780 SymbolFromString(Handle<String>(String::cast(entry->name())));
781 // Check if a descriptor with this name already exists before writing.
782 if (result->LinearSearch(*key, descriptor_count) ==
783 DescriptorArray::kNotFound) {
784 CallbacksDescriptor desc(*key, *entry, entry->property_attributes());
785 result->Set(descriptor_count, &desc);
786 descriptor_count++;
787 } else {
788 duplicates++;
789 }
790 }
791
792 // If duplicates were detected, allocate a result of the right size
793 // and transfer the elements.
794 if (duplicates > 0) {
795 int number_of_descriptors = result->number_of_descriptors() - duplicates;
796 Handle<DescriptorArray> new_result =
797 NewDescriptorArray(number_of_descriptors);
798 for (int i = 0; i < number_of_descriptors; i++) {
799 new_result->CopyFrom(i, *result, i);
800 }
801 result = new_result;
802 }
803
804 // Sort the result before returning.
805 result->Sort();
806 return result;
807}
808
809
810Handle<JSObject> Factory::NewJSObject(Handle<JSFunction> constructor,
811 PretenureFlag pretenure) {
Steve Block44f0eee2011-05-26 01:26:41 +0100812 CALL_HEAP_FUNCTION(
813 isolate(),
814 isolate()->heap()->AllocateJSObject(*constructor, pretenure), JSObject);
Steve Blocka7e24c12009-10-30 11:49:00 +0000815}
816
817
818Handle<GlobalObject> Factory::NewGlobalObject(
819 Handle<JSFunction> constructor) {
Steve Block44f0eee2011-05-26 01:26:41 +0100820 CALL_HEAP_FUNCTION(isolate(),
821 isolate()->heap()->AllocateGlobalObject(*constructor),
Steve Blocka7e24c12009-10-30 11:49:00 +0000822 GlobalObject);
823}
824
825
826
827Handle<JSObject> Factory::NewJSObjectFromMap(Handle<Map> map) {
Steve Block44f0eee2011-05-26 01:26:41 +0100828 CALL_HEAP_FUNCTION(
829 isolate(),
830 isolate()->heap()->AllocateJSObjectFromMap(*map, NOT_TENURED),
831 JSObject);
Steve Blocka7e24c12009-10-30 11:49:00 +0000832}
833
834
Steve Block44f0eee2011-05-26 01:26:41 +0100835Handle<JSArray> Factory::NewJSArray(int capacity,
Steve Blocka7e24c12009-10-30 11:49:00 +0000836 PretenureFlag pretenure) {
Steve Block44f0eee2011-05-26 01:26:41 +0100837 Handle<JSObject> obj = NewJSObject(isolate()->array_function(), pretenure);
838 CALL_HEAP_FUNCTION(isolate(),
839 Handle<JSArray>::cast(obj)->Initialize(capacity),
840 JSArray);
Steve Blocka7e24c12009-10-30 11:49:00 +0000841}
842
843
844Handle<JSArray> Factory::NewJSArrayWithElements(Handle<FixedArray> elements,
845 PretenureFlag pretenure) {
846 Handle<JSArray> result =
Steve Block44f0eee2011-05-26 01:26:41 +0100847 Handle<JSArray>::cast(NewJSObject(isolate()->array_function(),
848 pretenure));
Steve Blocka7e24c12009-10-30 11:49:00 +0000849 result->SetContent(*elements);
850 return result;
851}
852
853
Ben Murdoch257744e2011-11-30 15:57:28 +0000854Handle<JSProxy> Factory::NewJSProxy(Handle<Object> handler,
855 Handle<Object> prototype) {
856 CALL_HEAP_FUNCTION(
857 isolate(),
858 isolate()->heap()->AllocateJSProxy(*handler, *prototype),
859 JSProxy);
860}
861
862
Steve Block6ded16b2010-05-10 14:33:55 +0100863Handle<SharedFunctionInfo> Factory::NewSharedFunctionInfo(
Ben Murdoch3bec4d22010-07-22 14:51:16 +0100864 Handle<String> name,
865 int number_of_literals,
866 Handle<Code> code,
867 Handle<SerializedScopeInfo> scope_info) {
Steve Block6ded16b2010-05-10 14:33:55 +0100868 Handle<SharedFunctionInfo> shared = NewSharedFunctionInfo(name);
869 shared->set_code(*code);
Ben Murdoch3bec4d22010-07-22 14:51:16 +0100870 shared->set_scope_info(*scope_info);
Steve Block6ded16b2010-05-10 14:33:55 +0100871 int literals_array_size = number_of_literals;
872 // If the function contains object, regexp or array literals,
873 // allocate extra space for a literals array prefix containing the
874 // context.
875 if (number_of_literals > 0) {
876 literals_array_size += JSFunction::kLiteralsPrefixSize;
877 }
878 shared->set_num_literals(literals_array_size);
879 return shared;
880}
881
882
Steve Block1e0659c2011-05-24 12:43:12 +0100883Handle<JSMessageObject> Factory::NewJSMessageObject(
884 Handle<String> type,
885 Handle<JSArray> arguments,
886 int start_position,
887 int end_position,
888 Handle<Object> script,
889 Handle<Object> stack_trace,
890 Handle<Object> stack_frames) {
Steve Block44f0eee2011-05-26 01:26:41 +0100891 CALL_HEAP_FUNCTION(isolate(),
892 isolate()->heap()->AllocateJSMessageObject(*type,
893 *arguments,
894 start_position,
895 end_position,
896 *script,
897 *stack_trace,
898 *stack_frames),
Steve Block1e0659c2011-05-24 12:43:12 +0100899 JSMessageObject);
900}
901
Steve Blocka7e24c12009-10-30 11:49:00 +0000902Handle<SharedFunctionInfo> Factory::NewSharedFunctionInfo(Handle<String> name) {
Steve Block44f0eee2011-05-26 01:26:41 +0100903 CALL_HEAP_FUNCTION(isolate(),
904 isolate()->heap()->AllocateSharedFunctionInfo(*name),
Steve Blocka7e24c12009-10-30 11:49:00 +0000905 SharedFunctionInfo);
906}
907
908
909Handle<String> Factory::NumberToString(Handle<Object> number) {
Steve Block44f0eee2011-05-26 01:26:41 +0100910 CALL_HEAP_FUNCTION(isolate(),
911 isolate()->heap()->NumberToString(*number), String);
Steve Blocka7e24c12009-10-30 11:49:00 +0000912}
913
914
915Handle<NumberDictionary> Factory::DictionaryAtNumberPut(
916 Handle<NumberDictionary> dictionary,
917 uint32_t key,
918 Handle<Object> value) {
Steve Block44f0eee2011-05-26 01:26:41 +0100919 CALL_HEAP_FUNCTION(isolate(),
920 dictionary->AtNumberPut(key, *value),
921 NumberDictionary);
Steve Blocka7e24c12009-10-30 11:49:00 +0000922}
923
924
925Handle<JSFunction> Factory::NewFunctionHelper(Handle<String> name,
926 Handle<Object> prototype) {
927 Handle<SharedFunctionInfo> function_share = NewSharedFunctionInfo(name);
Steve Block44f0eee2011-05-26 01:26:41 +0100928 CALL_HEAP_FUNCTION(
929 isolate(),
930 isolate()->heap()->AllocateFunction(*isolate()->function_map(),
931 *function_share,
932 *prototype),
933 JSFunction);
Steve Blocka7e24c12009-10-30 11:49:00 +0000934}
935
936
937Handle<JSFunction> Factory::NewFunction(Handle<String> name,
938 Handle<Object> prototype) {
939 Handle<JSFunction> fun = NewFunctionHelper(name, prototype);
Steve Block44f0eee2011-05-26 01:26:41 +0100940 fun->set_context(isolate()->context()->global_context());
Steve Blocka7e24c12009-10-30 11:49:00 +0000941 return fun;
942}
943
944
Steve Block6ded16b2010-05-10 14:33:55 +0100945Handle<JSFunction> Factory::NewFunctionWithoutPrototypeHelper(
Steve Block44f0eee2011-05-26 01:26:41 +0100946 Handle<String> name,
947 StrictModeFlag strict_mode) {
Steve Block6ded16b2010-05-10 14:33:55 +0100948 Handle<SharedFunctionInfo> function_share = NewSharedFunctionInfo(name);
Steve Block44f0eee2011-05-26 01:26:41 +0100949 Handle<Map> map = strict_mode == kStrictMode
950 ? isolate()->strict_mode_function_without_prototype_map()
951 : isolate()->function_without_prototype_map();
952 CALL_HEAP_FUNCTION(isolate(),
953 isolate()->heap()->AllocateFunction(
954 *map,
Steve Block6ded16b2010-05-10 14:33:55 +0100955 *function_share,
956 *the_hole_value()),
957 JSFunction);
958}
959
960
Steve Block44f0eee2011-05-26 01:26:41 +0100961Handle<JSFunction> Factory::NewFunctionWithoutPrototype(
962 Handle<String> name,
963 StrictModeFlag strict_mode) {
964 Handle<JSFunction> fun = NewFunctionWithoutPrototypeHelper(name, strict_mode);
965 fun->set_context(isolate()->context()->global_context());
Steve Block6ded16b2010-05-10 14:33:55 +0100966 return fun;
967}
968
969
Leon Clarkee46be812010-01-19 14:06:41 +0000970Handle<Object> Factory::ToObject(Handle<Object> object) {
Steve Block44f0eee2011-05-26 01:26:41 +0100971 CALL_HEAP_FUNCTION(isolate(), object->ToObject(), Object);
Leon Clarkee46be812010-01-19 14:06:41 +0000972}
973
974
Steve Blocka7e24c12009-10-30 11:49:00 +0000975Handle<Object> Factory::ToObject(Handle<Object> object,
976 Handle<Context> global_context) {
Steve Block44f0eee2011-05-26 01:26:41 +0100977 CALL_HEAP_FUNCTION(isolate(), object->ToObject(*global_context), Object);
Steve Blocka7e24c12009-10-30 11:49:00 +0000978}
979
980
981#ifdef ENABLE_DEBUGGER_SUPPORT
982Handle<DebugInfo> Factory::NewDebugInfo(Handle<SharedFunctionInfo> shared) {
983 // Get the original code of the function.
984 Handle<Code> code(shared->code());
985
986 // Create a copy of the code before allocating the debug info object to avoid
987 // allocation while setting up the debug info object.
988 Handle<Code> original_code(*Factory::CopyCode(code));
989
990 // Allocate initial fixed array for active break points before allocating the
991 // debug info object to avoid allocation while setting up the debug info
992 // object.
993 Handle<FixedArray> break_points(
Steve Block44f0eee2011-05-26 01:26:41 +0100994 NewFixedArray(Debug::kEstimatedNofBreakPointsInFunction));
Steve Blocka7e24c12009-10-30 11:49:00 +0000995
996 // Create and set up the debug info object. Debug info contains function, a
997 // copy of the original code, the executing code and initial fixed array for
998 // active break points.
999 Handle<DebugInfo> debug_info =
Steve Block44f0eee2011-05-26 01:26:41 +01001000 Handle<DebugInfo>::cast(NewStruct(DEBUG_INFO_TYPE));
Steve Blocka7e24c12009-10-30 11:49:00 +00001001 debug_info->set_shared(*shared);
1002 debug_info->set_original_code(*original_code);
1003 debug_info->set_code(*code);
1004 debug_info->set_break_points(*break_points);
1005
1006 // Link debug info to function.
1007 shared->set_debug_info(*debug_info);
1008
1009 return debug_info;
1010}
1011#endif
1012
1013
1014Handle<JSObject> Factory::NewArgumentsObject(Handle<Object> callee,
1015 int length) {
Steve Block44f0eee2011-05-26 01:26:41 +01001016 CALL_HEAP_FUNCTION(
1017 isolate(),
1018 isolate()->heap()->AllocateArgumentsObject(*callee, length), JSObject);
Steve Blocka7e24c12009-10-30 11:49:00 +00001019}
1020
1021
1022Handle<JSFunction> Factory::CreateApiFunction(
1023 Handle<FunctionTemplateInfo> obj, ApiInstanceType instance_type) {
Steve Block44f0eee2011-05-26 01:26:41 +01001024 Handle<Code> code = isolate()->builtins()->HandleApiCall();
1025 Handle<Code> construct_stub = isolate()->builtins()->JSConstructStubApi();
Steve Blocka7e24c12009-10-30 11:49:00 +00001026
1027 int internal_field_count = 0;
1028 if (!obj->instance_template()->IsUndefined()) {
1029 Handle<ObjectTemplateInfo> instance_template =
1030 Handle<ObjectTemplateInfo>(
1031 ObjectTemplateInfo::cast(obj->instance_template()));
1032 internal_field_count =
1033 Smi::cast(instance_template->internal_field_count())->value();
1034 }
1035
1036 int instance_size = kPointerSize * internal_field_count;
1037 InstanceType type = INVALID_TYPE;
1038 switch (instance_type) {
1039 case JavaScriptObject:
1040 type = JS_OBJECT_TYPE;
1041 instance_size += JSObject::kHeaderSize;
1042 break;
1043 case InnerGlobalObject:
1044 type = JS_GLOBAL_OBJECT_TYPE;
1045 instance_size += JSGlobalObject::kSize;
1046 break;
1047 case OuterGlobalObject:
1048 type = JS_GLOBAL_PROXY_TYPE;
1049 instance_size += JSGlobalProxy::kSize;
1050 break;
1051 default:
1052 break;
1053 }
1054 ASSERT(type != INVALID_TYPE);
1055
1056 Handle<JSFunction> result =
Steve Block44f0eee2011-05-26 01:26:41 +01001057 NewFunction(Factory::empty_symbol(),
1058 type,
1059 instance_size,
1060 code,
1061 true);
Steve Blocka7e24c12009-10-30 11:49:00 +00001062 // Set class name.
1063 Handle<Object> class_name = Handle<Object>(obj->class_name());
1064 if (class_name->IsString()) {
1065 result->shared()->set_instance_class_name(*class_name);
1066 result->shared()->set_name(*class_name);
1067 }
1068
1069 Handle<Map> map = Handle<Map>(result->initial_map());
1070
1071 // Mark as undetectable if needed.
1072 if (obj->undetectable()) {
1073 map->set_is_undetectable();
1074 }
1075
1076 // Mark as hidden for the __proto__ accessor if needed.
1077 if (obj->hidden_prototype()) {
1078 map->set_is_hidden_prototype();
1079 }
1080
1081 // Mark as needs_access_check if needed.
1082 if (obj->needs_access_check()) {
1083 map->set_is_access_check_needed(true);
1084 }
1085
1086 // Set interceptor information in the map.
1087 if (!obj->named_property_handler()->IsUndefined()) {
1088 map->set_has_named_interceptor();
1089 }
1090 if (!obj->indexed_property_handler()->IsUndefined()) {
1091 map->set_has_indexed_interceptor();
1092 }
1093
1094 // Set instance call-as-function information in the map.
1095 if (!obj->instance_call_handler()->IsUndefined()) {
1096 map->set_has_instance_call_handler();
1097 }
1098
1099 result->shared()->set_function_data(*obj);
Leon Clarkee46be812010-01-19 14:06:41 +00001100 result->shared()->set_construct_stub(*construct_stub);
Steve Blocka7e24c12009-10-30 11:49:00 +00001101 result->shared()->DontAdaptArguments();
1102
1103 // Recursively copy parent templates' accessors, 'data' may be modified.
1104 Handle<DescriptorArray> array =
1105 Handle<DescriptorArray>(map->instance_descriptors());
1106 while (true) {
1107 Handle<Object> props = Handle<Object>(obj->property_accessors());
1108 if (!props->IsUndefined()) {
Steve Block44f0eee2011-05-26 01:26:41 +01001109 array = CopyAppendCallbackDescriptors(array, props);
Steve Blocka7e24c12009-10-30 11:49:00 +00001110 }
1111 Handle<Object> parent = Handle<Object>(obj->parent_template());
1112 if (parent->IsUndefined()) break;
1113 obj = Handle<FunctionTemplateInfo>::cast(parent);
1114 }
1115 if (!array->IsEmpty()) {
1116 map->set_instance_descriptors(*array);
1117 }
1118
Steve Block6ded16b2010-05-10 14:33:55 +01001119 ASSERT(result->shared()->IsApiFunction());
Steve Blocka7e24c12009-10-30 11:49:00 +00001120 return result;
1121}
1122
1123
1124Handle<MapCache> Factory::NewMapCache(int at_least_space_for) {
Steve Block44f0eee2011-05-26 01:26:41 +01001125 CALL_HEAP_FUNCTION(isolate(),
1126 MapCache::Allocate(at_least_space_for), MapCache);
Steve Blocka7e24c12009-10-30 11:49:00 +00001127}
1128
1129
John Reck59135872010-11-02 12:39:01 -07001130MUST_USE_RESULT static MaybeObject* UpdateMapCacheWith(Context* context,
1131 FixedArray* keys,
1132 Map* map) {
1133 Object* result;
1134 { MaybeObject* maybe_result =
1135 MapCache::cast(context->map_cache())->Put(keys, map);
1136 if (!maybe_result->ToObject(&result)) return maybe_result;
1137 }
1138 context->set_map_cache(MapCache::cast(result));
Steve Blocka7e24c12009-10-30 11:49:00 +00001139 return result;
1140}
1141
1142
1143Handle<MapCache> Factory::AddToMapCache(Handle<Context> context,
1144 Handle<FixedArray> keys,
1145 Handle<Map> map) {
Steve Block44f0eee2011-05-26 01:26:41 +01001146 CALL_HEAP_FUNCTION(isolate(),
1147 UpdateMapCacheWith(*context, *keys, *map), MapCache);
Steve Blocka7e24c12009-10-30 11:49:00 +00001148}
1149
1150
1151Handle<Map> Factory::ObjectLiteralMapFromCache(Handle<Context> context,
1152 Handle<FixedArray> keys) {
1153 if (context->map_cache()->IsUndefined()) {
1154 // Allocate the new map cache for the global context.
1155 Handle<MapCache> new_cache = NewMapCache(24);
1156 context->set_map_cache(*new_cache);
1157 }
1158 // Check to see whether there is a matching element in the cache.
1159 Handle<MapCache> cache =
1160 Handle<MapCache>(MapCache::cast(context->map_cache()));
1161 Handle<Object> result = Handle<Object>(cache->Lookup(*keys));
1162 if (result->IsMap()) return Handle<Map>::cast(result);
1163 // Create a new map and add it to the cache.
1164 Handle<Map> map =
1165 CopyMap(Handle<Map>(context->object_function()->initial_map()),
1166 keys->length());
1167 AddToMapCache(context, keys, map);
1168 return Handle<Map>(map);
1169}
1170
1171
1172void Factory::SetRegExpAtomData(Handle<JSRegExp> regexp,
1173 JSRegExp::Type type,
1174 Handle<String> source,
1175 JSRegExp::Flags flags,
1176 Handle<Object> data) {
1177 Handle<FixedArray> store = NewFixedArray(JSRegExp::kAtomDataSize);
1178
1179 store->set(JSRegExp::kTagIndex, Smi::FromInt(type));
1180 store->set(JSRegExp::kSourceIndex, *source);
1181 store->set(JSRegExp::kFlagsIndex, Smi::FromInt(flags.value()));
1182 store->set(JSRegExp::kAtomPatternIndex, *data);
1183 regexp->set_data(*store);
1184}
1185
1186void Factory::SetRegExpIrregexpData(Handle<JSRegExp> regexp,
1187 JSRegExp::Type type,
1188 Handle<String> source,
1189 JSRegExp::Flags flags,
1190 int capture_count) {
1191 Handle<FixedArray> store = NewFixedArray(JSRegExp::kIrregexpDataSize);
Ben Murdoch257744e2011-11-30 15:57:28 +00001192 Smi* uninitialized = Smi::FromInt(JSRegExp::kUninitializedValue);
Steve Blocka7e24c12009-10-30 11:49:00 +00001193 store->set(JSRegExp::kTagIndex, Smi::FromInt(type));
1194 store->set(JSRegExp::kSourceIndex, *source);
1195 store->set(JSRegExp::kFlagsIndex, Smi::FromInt(flags.value()));
Ben Murdoch257744e2011-11-30 15:57:28 +00001196 store->set(JSRegExp::kIrregexpASCIICodeIndex, uninitialized);
1197 store->set(JSRegExp::kIrregexpUC16CodeIndex, uninitialized);
1198 store->set(JSRegExp::kIrregexpASCIICodeSavedIndex, uninitialized);
1199 store->set(JSRegExp::kIrregexpUC16CodeSavedIndex, uninitialized);
Steve Blocka7e24c12009-10-30 11:49:00 +00001200 store->set(JSRegExp::kIrregexpMaxRegisterCountIndex, Smi::FromInt(0));
1201 store->set(JSRegExp::kIrregexpCaptureCountIndex,
1202 Smi::FromInt(capture_count));
1203 regexp->set_data(*store);
1204}
1205
1206
1207
1208void Factory::ConfigureInstance(Handle<FunctionTemplateInfo> desc,
1209 Handle<JSObject> instance,
1210 bool* pending_exception) {
1211 // Configure the instance by adding the properties specified by the
1212 // instance template.
1213 Handle<Object> instance_template = Handle<Object>(desc->instance_template());
1214 if (!instance_template->IsUndefined()) {
1215 Execution::ConfigureInstance(instance,
1216 instance_template,
1217 pending_exception);
1218 } else {
1219 *pending_exception = false;
1220 }
1221}
1222
1223
1224} } // namespace v8::internal