blob: ac96668d995b396bcc997f90a601e6e7d99c16f0 [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"
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
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000061Handle<FixedArray> Factory::NewFixedDoubleArray(int size,
62 PretenureFlag pretenure) {
63 ASSERT(0 <= size);
64 CALL_HEAP_FUNCTION(
65 isolate(),
66 isolate()->heap()->AllocateUninitializedFixedDoubleArray(size, pretenure),
67 FixedArray);
68}
69
70
Steve Blocka7e24c12009-10-30 11:49:00 +000071Handle<StringDictionary> Factory::NewStringDictionary(int at_least_space_for) {
72 ASSERT(0 <= at_least_space_for);
Steve Block44f0eee2011-05-26 01:26:41 +010073 CALL_HEAP_FUNCTION(isolate(),
74 StringDictionary::Allocate(at_least_space_for),
Steve Blocka7e24c12009-10-30 11:49:00 +000075 StringDictionary);
76}
77
78
79Handle<NumberDictionary> Factory::NewNumberDictionary(int at_least_space_for) {
80 ASSERT(0 <= at_least_space_for);
Steve Block44f0eee2011-05-26 01:26:41 +010081 CALL_HEAP_FUNCTION(isolate(),
82 NumberDictionary::Allocate(at_least_space_for),
Steve Blocka7e24c12009-10-30 11:49:00 +000083 NumberDictionary);
84}
85
86
87Handle<DescriptorArray> Factory::NewDescriptorArray(int number_of_descriptors) {
88 ASSERT(0 <= number_of_descriptors);
Steve Block44f0eee2011-05-26 01:26:41 +010089 CALL_HEAP_FUNCTION(isolate(),
90 DescriptorArray::Allocate(number_of_descriptors),
Steve Blocka7e24c12009-10-30 11:49:00 +000091 DescriptorArray);
92}
93
94
Ben Murdochb0fe1622011-05-05 13:52:32 +010095Handle<DeoptimizationInputData> Factory::NewDeoptimizationInputData(
96 int deopt_entry_count,
97 PretenureFlag pretenure) {
98 ASSERT(deopt_entry_count > 0);
Steve Block44f0eee2011-05-26 01:26:41 +010099 CALL_HEAP_FUNCTION(isolate(),
100 DeoptimizationInputData::Allocate(deopt_entry_count,
Ben Murdochb0fe1622011-05-05 13:52:32 +0100101 pretenure),
102 DeoptimizationInputData);
103}
104
105
106Handle<DeoptimizationOutputData> Factory::NewDeoptimizationOutputData(
107 int deopt_entry_count,
108 PretenureFlag pretenure) {
109 ASSERT(deopt_entry_count > 0);
Steve Block44f0eee2011-05-26 01:26:41 +0100110 CALL_HEAP_FUNCTION(isolate(),
111 DeoptimizationOutputData::Allocate(deopt_entry_count,
Ben Murdochb0fe1622011-05-05 13:52:32 +0100112 pretenure),
113 DeoptimizationOutputData);
114}
115
116
Steve Blocka7e24c12009-10-30 11:49:00 +0000117// Symbols are created in the old generation (data space).
118Handle<String> Factory::LookupSymbol(Vector<const char> string) {
Steve Block44f0eee2011-05-26 01:26:41 +0100119 CALL_HEAP_FUNCTION(isolate(),
120 isolate()->heap()->LookupSymbol(string),
121 String);
Steve Blocka7e24c12009-10-30 11:49:00 +0000122}
123
Ben Murdoch257744e2011-11-30 15:57:28 +0000124// Symbols are created in the old generation (data space).
125Handle<String> Factory::LookupSymbol(Handle<String> string) {
126 CALL_HEAP_FUNCTION(isolate(),
127 isolate()->heap()->LookupSymbol(*string),
128 String);
129}
130
Steve Block9fac8402011-05-12 15:51:54 +0100131Handle<String> Factory::LookupAsciiSymbol(Vector<const char> string) {
Steve Block44f0eee2011-05-26 01:26:41 +0100132 CALL_HEAP_FUNCTION(isolate(),
133 isolate()->heap()->LookupAsciiSymbol(string),
134 String);
Steve Block9fac8402011-05-12 15:51:54 +0100135}
136
Ben Murdoch257744e2011-11-30 15:57:28 +0000137
138Handle<String> Factory::LookupAsciiSymbol(Handle<SeqAsciiString> string,
139 int from,
140 int length) {
141 CALL_HEAP_FUNCTION(isolate(),
142 isolate()->heap()->LookupAsciiSymbol(string,
143 from,
144 length),
145 String);
146}
147
148
Steve Block9fac8402011-05-12 15:51:54 +0100149Handle<String> Factory::LookupTwoByteSymbol(Vector<const uc16> string) {
Steve Block44f0eee2011-05-26 01:26:41 +0100150 CALL_HEAP_FUNCTION(isolate(),
151 isolate()->heap()->LookupTwoByteSymbol(string),
152 String);
Steve Block9fac8402011-05-12 15:51:54 +0100153}
154
Steve Blocka7e24c12009-10-30 11:49:00 +0000155
156Handle<String> Factory::NewStringFromAscii(Vector<const char> string,
157 PretenureFlag pretenure) {
Steve Block44f0eee2011-05-26 01:26:41 +0100158 CALL_HEAP_FUNCTION(
159 isolate(),
160 isolate()->heap()->AllocateStringFromAscii(string, pretenure),
161 String);
Steve Blocka7e24c12009-10-30 11:49:00 +0000162}
163
164Handle<String> Factory::NewStringFromUtf8(Vector<const char> string,
165 PretenureFlag pretenure) {
Steve Block44f0eee2011-05-26 01:26:41 +0100166 CALL_HEAP_FUNCTION(
167 isolate(),
168 isolate()->heap()->AllocateStringFromUtf8(string, pretenure),
169 String);
Steve Blocka7e24c12009-10-30 11:49:00 +0000170}
171
172
173Handle<String> Factory::NewStringFromTwoByte(Vector<const uc16> string,
174 PretenureFlag pretenure) {
Steve Block44f0eee2011-05-26 01:26:41 +0100175 CALL_HEAP_FUNCTION(
176 isolate(),
177 isolate()->heap()->AllocateStringFromTwoByte(string, pretenure),
178 String);
Steve Blocka7e24c12009-10-30 11:49:00 +0000179}
180
181
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000182Handle<SeqAsciiString> Factory::NewRawAsciiString(int length,
183 PretenureFlag pretenure) {
Steve Block44f0eee2011-05-26 01:26:41 +0100184 CALL_HEAP_FUNCTION(
185 isolate(),
186 isolate()->heap()->AllocateRawAsciiString(length, pretenure),
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000187 SeqAsciiString);
Leon Clarkeac952652010-07-15 11:15:24 +0100188}
189
190
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000191Handle<SeqTwoByteString> Factory::NewRawTwoByteString(int length,
192 PretenureFlag pretenure) {
Steve Block44f0eee2011-05-26 01:26:41 +0100193 CALL_HEAP_FUNCTION(
194 isolate(),
195 isolate()->heap()->AllocateRawTwoByteString(length, pretenure),
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000196 SeqTwoByteString);
Steve Blocka7e24c12009-10-30 11:49:00 +0000197}
198
199
200Handle<String> Factory::NewConsString(Handle<String> first,
201 Handle<String> second) {
Steve Block44f0eee2011-05-26 01:26:41 +0100202 CALL_HEAP_FUNCTION(isolate(),
203 isolate()->heap()->AllocateConsString(*first, *second),
204 String);
Steve Blocka7e24c12009-10-30 11:49:00 +0000205}
206
207
Steve Blockd0582a62009-12-15 09:54:21 +0000208Handle<String> Factory::NewSubString(Handle<String> str,
209 int begin,
210 int end) {
Steve Block44f0eee2011-05-26 01:26:41 +0100211 CALL_HEAP_FUNCTION(isolate(),
212 str->SubString(begin, end),
213 String);
Steve Blocka7e24c12009-10-30 11:49:00 +0000214}
215
216
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000217Handle<String> Factory::NewProperSubString(Handle<String> str,
218 int begin,
219 int end) {
220 ASSERT(begin > 0 || end < str->length());
221 CALL_HEAP_FUNCTION(isolate(),
222 isolate()->heap()->AllocateSubString(*str, begin, end),
223 String);
224}
225
226
Steve Blocka7e24c12009-10-30 11:49:00 +0000227Handle<String> Factory::NewExternalStringFromAscii(
228 ExternalAsciiString::Resource* resource) {
Steve Block44f0eee2011-05-26 01:26:41 +0100229 CALL_HEAP_FUNCTION(
230 isolate(),
231 isolate()->heap()->AllocateExternalStringFromAscii(resource),
232 String);
Steve Blocka7e24c12009-10-30 11:49:00 +0000233}
234
235
236Handle<String> Factory::NewExternalStringFromTwoByte(
237 ExternalTwoByteString::Resource* resource) {
Steve Block44f0eee2011-05-26 01:26:41 +0100238 CALL_HEAP_FUNCTION(
239 isolate(),
240 isolate()->heap()->AllocateExternalStringFromTwoByte(resource),
241 String);
Steve Blocka7e24c12009-10-30 11:49:00 +0000242}
243
244
245Handle<Context> Factory::NewGlobalContext() {
Steve Block44f0eee2011-05-26 01:26:41 +0100246 CALL_HEAP_FUNCTION(
247 isolate(),
248 isolate()->heap()->AllocateGlobalContext(),
249 Context);
Steve Blocka7e24c12009-10-30 11:49:00 +0000250}
251
252
253Handle<Context> Factory::NewFunctionContext(int length,
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000254 Handle<JSFunction> function) {
Steve Block44f0eee2011-05-26 01:26:41 +0100255 CALL_HEAP_FUNCTION(
256 isolate(),
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000257 isolate()->heap()->AllocateFunctionContext(length, *function),
Steve Block44f0eee2011-05-26 01:26:41 +0100258 Context);
Steve Blocka7e24c12009-10-30 11:49:00 +0000259}
260
261
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000262Handle<Context> Factory::NewCatchContext(Handle<JSFunction> function,
263 Handle<Context> previous,
264 Handle<String> name,
265 Handle<Object> thrown_object) {
Steve Block44f0eee2011-05-26 01:26:41 +0100266 CALL_HEAP_FUNCTION(
267 isolate(),
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000268 isolate()->heap()->AllocateCatchContext(*function,
269 *previous,
270 *name,
271 *thrown_object),
272 Context);
273}
274
275
276Handle<Context> Factory::NewWithContext(Handle<JSFunction> function,
277 Handle<Context> previous,
278 Handle<JSObject> extension) {
279 CALL_HEAP_FUNCTION(
280 isolate(),
281 isolate()->heap()->AllocateWithContext(*function, *previous, *extension),
Steve Block44f0eee2011-05-26 01:26:41 +0100282 Context);
Steve Blocka7e24c12009-10-30 11:49:00 +0000283}
284
285
286Handle<Struct> Factory::NewStruct(InstanceType type) {
Steve Block44f0eee2011-05-26 01:26:41 +0100287 CALL_HEAP_FUNCTION(
288 isolate(),
289 isolate()->heap()->AllocateStruct(type),
290 Struct);
Steve Blocka7e24c12009-10-30 11:49:00 +0000291}
292
293
294Handle<AccessorInfo> Factory::NewAccessorInfo() {
295 Handle<AccessorInfo> info =
296 Handle<AccessorInfo>::cast(NewStruct(ACCESSOR_INFO_TYPE));
297 info->set_flag(0); // Must clear the flag, it was initialized as undefined.
298 return info;
299}
300
301
302Handle<Script> Factory::NewScript(Handle<String> source) {
303 // Generate id for this script.
304 int id;
Steve Block44f0eee2011-05-26 01:26:41 +0100305 Heap* heap = isolate()->heap();
306 if (heap->last_script_id()->IsUndefined()) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000307 // Script ids start from one.
308 id = 1;
309 } else {
310 // Increment id, wrap when positive smi is exhausted.
Steve Block44f0eee2011-05-26 01:26:41 +0100311 id = Smi::cast(heap->last_script_id())->value();
Steve Blocka7e24c12009-10-30 11:49:00 +0000312 id++;
313 if (!Smi::IsValid(id)) {
314 id = 0;
315 }
316 }
Steve Block44f0eee2011-05-26 01:26:41 +0100317 heap->SetLastScriptId(Smi::FromInt(id));
Steve Blocka7e24c12009-10-30 11:49:00 +0000318
319 // Create and initialize script object.
Ben Murdoch257744e2011-11-30 15:57:28 +0000320 Handle<Foreign> wrapper = NewForeign(0, TENURED);
Steve Blocka7e24c12009-10-30 11:49:00 +0000321 Handle<Script> script = Handle<Script>::cast(NewStruct(SCRIPT_TYPE));
322 script->set_source(*source);
Steve Block44f0eee2011-05-26 01:26:41 +0100323 script->set_name(heap->undefined_value());
324 script->set_id(heap->last_script_id());
Steve Blocka7e24c12009-10-30 11:49:00 +0000325 script->set_line_offset(Smi::FromInt(0));
326 script->set_column_offset(Smi::FromInt(0));
Steve Block44f0eee2011-05-26 01:26:41 +0100327 script->set_data(heap->undefined_value());
328 script->set_context_data(heap->undefined_value());
Steve Blocka7e24c12009-10-30 11:49:00 +0000329 script->set_type(Smi::FromInt(Script::TYPE_NORMAL));
330 script->set_compilation_type(Smi::FromInt(Script::COMPILATION_TYPE_HOST));
331 script->set_wrapper(*wrapper);
Steve Block44f0eee2011-05-26 01:26:41 +0100332 script->set_line_ends(heap->undefined_value());
333 script->set_eval_from_shared(heap->undefined_value());
Steve Blocka7e24c12009-10-30 11:49:00 +0000334 script->set_eval_from_instructions_offset(Smi::FromInt(0));
335
336 return script;
337}
338
339
Ben Murdoch257744e2011-11-30 15:57:28 +0000340Handle<Foreign> Factory::NewForeign(Address addr, PretenureFlag pretenure) {
Steve Block44f0eee2011-05-26 01:26:41 +0100341 CALL_HEAP_FUNCTION(isolate(),
Ben Murdoch257744e2011-11-30 15:57:28 +0000342 isolate()->heap()->AllocateForeign(addr, pretenure),
343 Foreign);
Steve Blocka7e24c12009-10-30 11:49:00 +0000344}
345
346
Ben Murdoch257744e2011-11-30 15:57:28 +0000347Handle<Foreign> Factory::NewForeign(const AccessorDescriptor* desc) {
348 return NewForeign((Address) desc, TENURED);
Steve Blocka7e24c12009-10-30 11:49:00 +0000349}
350
351
352Handle<ByteArray> Factory::NewByteArray(int length, PretenureFlag pretenure) {
353 ASSERT(0 <= length);
Steve Block44f0eee2011-05-26 01:26:41 +0100354 CALL_HEAP_FUNCTION(
355 isolate(),
356 isolate()->heap()->AllocateByteArray(length, pretenure),
357 ByteArray);
Steve Blocka7e24c12009-10-30 11:49:00 +0000358}
359
360
Steve Block3ce2e202009-11-05 08:53:23 +0000361Handle<ExternalArray> Factory::NewExternalArray(int length,
362 ExternalArrayType array_type,
363 void* external_pointer,
364 PretenureFlag pretenure) {
365 ASSERT(0 <= length);
Steve Block44f0eee2011-05-26 01:26:41 +0100366 CALL_HEAP_FUNCTION(
367 isolate(),
368 isolate()->heap()->AllocateExternalArray(length,
369 array_type,
370 external_pointer,
371 pretenure),
372 ExternalArray);
Steve Block3ce2e202009-11-05 08:53:23 +0000373}
374
375
Ben Murdochb0fe1622011-05-05 13:52:32 +0100376Handle<JSGlobalPropertyCell> Factory::NewJSGlobalPropertyCell(
377 Handle<Object> value) {
Steve Block44f0eee2011-05-26 01:26:41 +0100378 CALL_HEAP_FUNCTION(
379 isolate(),
380 isolate()->heap()->AllocateJSGlobalPropertyCell(*value),
381 JSGlobalPropertyCell);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100382}
383
384
Steve Blocka7e24c12009-10-30 11:49:00 +0000385Handle<Map> Factory::NewMap(InstanceType type, int instance_size) {
Steve Block44f0eee2011-05-26 01:26:41 +0100386 CALL_HEAP_FUNCTION(
387 isolate(),
388 isolate()->heap()->AllocateMap(type, instance_size),
389 Map);
Steve Blocka7e24c12009-10-30 11:49:00 +0000390}
391
392
393Handle<JSObject> Factory::NewFunctionPrototype(Handle<JSFunction> function) {
Steve Block44f0eee2011-05-26 01:26:41 +0100394 CALL_HEAP_FUNCTION(
395 isolate(),
396 isolate()->heap()->AllocateFunctionPrototype(*function),
397 JSObject);
Steve Blocka7e24c12009-10-30 11:49:00 +0000398}
399
400
401Handle<Map> Factory::CopyMapDropDescriptors(Handle<Map> src) {
Steve Block44f0eee2011-05-26 01:26:41 +0100402 CALL_HEAP_FUNCTION(isolate(), src->CopyDropDescriptors(), Map);
Steve Blocka7e24c12009-10-30 11:49:00 +0000403}
404
405
406Handle<Map> Factory::CopyMap(Handle<Map> src,
407 int extra_inobject_properties) {
408 Handle<Map> copy = CopyMapDropDescriptors(src);
409 // Check that we do not overflow the instance size when adding the
410 // extra inobject properties.
411 int instance_size_delta = extra_inobject_properties * kPointerSize;
412 int max_instance_size_delta =
413 JSObject::kMaxInstanceSize - copy->instance_size();
414 if (instance_size_delta > max_instance_size_delta) {
415 // If the instance size overflows, we allocate as many properties
416 // as we can as inobject properties.
417 instance_size_delta = max_instance_size_delta;
418 extra_inobject_properties = max_instance_size_delta >> kPointerSizeLog2;
419 }
420 // Adjust the map with the extra inobject properties.
421 int inobject_properties =
422 copy->inobject_properties() + extra_inobject_properties;
423 copy->set_inobject_properties(inobject_properties);
424 copy->set_unused_property_fields(inobject_properties);
425 copy->set_instance_size(copy->instance_size() + instance_size_delta);
Iain Merrick75681382010-08-19 15:07:18 +0100426 copy->set_visitor_id(StaticVisitorBase::GetVisitorId(*copy));
Steve Blocka7e24c12009-10-30 11:49:00 +0000427 return copy;
428}
429
Steve Block8defd9f2010-07-08 12:39:36 +0100430
Steve Blocka7e24c12009-10-30 11:49:00 +0000431Handle<Map> Factory::CopyMapDropTransitions(Handle<Map> src) {
Steve Block44f0eee2011-05-26 01:26:41 +0100432 CALL_HEAP_FUNCTION(isolate(), src->CopyDropTransitions(), Map);
Steve Blocka7e24c12009-10-30 11:49:00 +0000433}
434
435
Steve Block8defd9f2010-07-08 12:39:36 +0100436Handle<Map> Factory::GetFastElementsMap(Handle<Map> src) {
Steve Block44f0eee2011-05-26 01:26:41 +0100437 CALL_HEAP_FUNCTION(isolate(), src->GetFastElementsMap(), Map);
Steve Block8defd9f2010-07-08 12:39:36 +0100438}
439
440
441Handle<Map> Factory::GetSlowElementsMap(Handle<Map> src) {
Steve Block44f0eee2011-05-26 01:26:41 +0100442 CALL_HEAP_FUNCTION(isolate(), src->GetSlowElementsMap(), Map);
Steve Block8defd9f2010-07-08 12:39:36 +0100443}
444
445
Steve Block44f0eee2011-05-26 01:26:41 +0100446Handle<Map> Factory::GetExternalArrayElementsMap(
447 Handle<Map> src,
448 ExternalArrayType array_type,
449 bool safe_to_add_transition) {
450 CALL_HEAP_FUNCTION(isolate(),
451 src->GetExternalArrayElementsMap(array_type,
452 safe_to_add_transition),
453 Map);
Steve Block1e0659c2011-05-24 12:43:12 +0100454}
455
456
Steve Blocka7e24c12009-10-30 11:49:00 +0000457Handle<FixedArray> Factory::CopyFixedArray(Handle<FixedArray> array) {
Steve Block44f0eee2011-05-26 01:26:41 +0100458 CALL_HEAP_FUNCTION(isolate(), array->Copy(), FixedArray);
Steve Blocka7e24c12009-10-30 11:49:00 +0000459}
460
461
Steve Block6ded16b2010-05-10 14:33:55 +0100462Handle<JSFunction> Factory::BaseNewFunctionFromSharedFunctionInfo(
463 Handle<SharedFunctionInfo> function_info,
Leon Clarkee46be812010-01-19 14:06:41 +0000464 Handle<Map> function_map,
465 PretenureFlag pretenure) {
Steve Block44f0eee2011-05-26 01:26:41 +0100466 CALL_HEAP_FUNCTION(
467 isolate(),
468 isolate()->heap()->AllocateFunction(*function_map,
469 *function_info,
470 isolate()->heap()->the_hole_value(),
471 pretenure),
Steve Blocka7e24c12009-10-30 11:49:00 +0000472 JSFunction);
473}
474
475
Steve Block6ded16b2010-05-10 14:33:55 +0100476Handle<JSFunction> Factory::NewFunctionFromSharedFunctionInfo(
477 Handle<SharedFunctionInfo> function_info,
Leon Clarkee46be812010-01-19 14:06:41 +0000478 Handle<Context> context,
479 PretenureFlag pretenure) {
Steve Block6ded16b2010-05-10 14:33:55 +0100480 Handle<JSFunction> result = BaseNewFunctionFromSharedFunctionInfo(
Steve Block44f0eee2011-05-26 01:26:41 +0100481 function_info,
482 function_info->strict_mode()
483 ? isolate()->strict_mode_function_map()
484 : isolate()->function_map(),
485 pretenure);
486
Steve Blocka7e24c12009-10-30 11:49:00 +0000487 result->set_context(*context);
Steve Block6ded16b2010-05-10 14:33:55 +0100488 int number_of_literals = function_info->num_literals();
Steve Block44f0eee2011-05-26 01:26:41 +0100489 Handle<FixedArray> literals = NewFixedArray(number_of_literals, pretenure);
Steve Blocka7e24c12009-10-30 11:49:00 +0000490 if (number_of_literals > 0) {
491 // Store the object, regexp and array functions in the literals
492 // array prefix. These functions will be used when creating
493 // object, regexp and array literals in this function.
494 literals->set(JSFunction::kLiteralGlobalContextIndex,
495 context->global_context());
496 }
497 result->set_literals(*literals);
Steve Block44f0eee2011-05-26 01:26:41 +0100498 result->set_next_function_link(isolate()->heap()->undefined_value());
Ben Murdochb0fe1622011-05-05 13:52:32 +0100499
500 if (V8::UseCrankshaft() &&
501 FLAG_always_opt &&
502 result->is_compiled() &&
503 !function_info->is_toplevel() &&
504 function_info->allows_lazy_compilation()) {
505 result->MarkForLazyRecompilation();
506 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000507 return result;
508}
509
510
511Handle<Object> Factory::NewNumber(double value,
512 PretenureFlag pretenure) {
Steve Block44f0eee2011-05-26 01:26:41 +0100513 CALL_HEAP_FUNCTION(
514 isolate(),
515 isolate()->heap()->NumberFromDouble(value, pretenure), Object);
Steve Blocka7e24c12009-10-30 11:49:00 +0000516}
517
518
519Handle<Object> Factory::NewNumberFromInt(int value) {
Steve Block44f0eee2011-05-26 01:26:41 +0100520 CALL_HEAP_FUNCTION(
521 isolate(),
522 isolate()->heap()->NumberFromInt32(value), Object);
Steve Blocka7e24c12009-10-30 11:49:00 +0000523}
524
525
526Handle<Object> Factory::NewNumberFromUint(uint32_t value) {
Steve Block44f0eee2011-05-26 01:26:41 +0100527 CALL_HEAP_FUNCTION(
528 isolate(),
529 isolate()->heap()->NumberFromUint32(value), Object);
Steve Blocka7e24c12009-10-30 11:49:00 +0000530}
531
532
533Handle<JSObject> Factory::NewNeanderObject() {
Steve Block44f0eee2011-05-26 01:26:41 +0100534 CALL_HEAP_FUNCTION(
535 isolate(),
536 isolate()->heap()->AllocateJSObjectFromMap(
537 isolate()->heap()->neander_map()),
538 JSObject);
Steve Blocka7e24c12009-10-30 11:49:00 +0000539}
540
541
542Handle<Object> Factory::NewTypeError(const char* type,
543 Vector< Handle<Object> > args) {
544 return NewError("MakeTypeError", type, args);
545}
546
547
548Handle<Object> Factory::NewTypeError(Handle<String> message) {
549 return NewError("$TypeError", message);
550}
551
552
553Handle<Object> Factory::NewRangeError(const char* type,
554 Vector< Handle<Object> > args) {
555 return NewError("MakeRangeError", type, args);
556}
557
558
559Handle<Object> Factory::NewRangeError(Handle<String> message) {
560 return NewError("$RangeError", message);
561}
562
563
564Handle<Object> Factory::NewSyntaxError(const char* type, Handle<JSArray> args) {
565 return NewError("MakeSyntaxError", type, args);
566}
567
568
569Handle<Object> Factory::NewSyntaxError(Handle<String> message) {
570 return NewError("$SyntaxError", message);
571}
572
573
574Handle<Object> Factory::NewReferenceError(const char* type,
575 Vector< Handle<Object> > args) {
576 return NewError("MakeReferenceError", type, args);
577}
578
579
580Handle<Object> Factory::NewReferenceError(Handle<String> message) {
581 return NewError("$ReferenceError", message);
582}
583
584
585Handle<Object> Factory::NewError(const char* maker, const char* type,
586 Vector< Handle<Object> > args) {
587 v8::HandleScope scope; // Instantiate a closeable HandleScope for EscapeFrom.
Steve Block44f0eee2011-05-26 01:26:41 +0100588 Handle<FixedArray> array = NewFixedArray(args.length());
Steve Blocka7e24c12009-10-30 11:49:00 +0000589 for (int i = 0; i < args.length(); i++) {
590 array->set(i, *args[i]);
591 }
Steve Block44f0eee2011-05-26 01:26:41 +0100592 Handle<JSArray> object = NewJSArrayWithElements(array);
Steve Blocka7e24c12009-10-30 11:49:00 +0000593 Handle<Object> result = NewError(maker, type, object);
594 return result.EscapeFrom(&scope);
595}
596
597
598Handle<Object> Factory::NewEvalError(const char* type,
599 Vector< Handle<Object> > args) {
600 return NewError("MakeEvalError", type, args);
601}
602
603
604Handle<Object> Factory::NewError(const char* type,
605 Vector< Handle<Object> > args) {
606 return NewError("MakeError", type, args);
607}
608
609
610Handle<Object> Factory::NewError(const char* maker,
611 const char* type,
612 Handle<JSArray> args) {
Steve Block44f0eee2011-05-26 01:26:41 +0100613 Handle<String> make_str = LookupAsciiSymbol(maker);
614 Handle<Object> fun_obj(
615 isolate()->js_builtins_object()->GetPropertyNoExceptionThrown(*make_str));
Steve Blocka7e24c12009-10-30 11:49:00 +0000616 // If the builtins haven't been properly configured yet this error
617 // constructor may not have been defined. Bail out.
618 if (!fun_obj->IsJSFunction())
Steve Block44f0eee2011-05-26 01:26:41 +0100619 return undefined_value();
Steve Blocka7e24c12009-10-30 11:49:00 +0000620 Handle<JSFunction> fun = Handle<JSFunction>::cast(fun_obj);
Steve Block44f0eee2011-05-26 01:26:41 +0100621 Handle<Object> type_obj = LookupAsciiSymbol(type);
Steve Blocka7e24c12009-10-30 11:49:00 +0000622 Object** argv[2] = { type_obj.location(),
623 Handle<Object>::cast(args).location() };
624
625 // Invoke the JavaScript factory method. If an exception is thrown while
626 // running the factory method, use the exception as the result.
627 bool caught_exception;
628 Handle<Object> result = Execution::TryCall(fun,
Steve Block44f0eee2011-05-26 01:26:41 +0100629 isolate()->js_builtins_object(), 2, argv, &caught_exception);
Steve Blocka7e24c12009-10-30 11:49:00 +0000630 return result;
631}
632
633
634Handle<Object> Factory::NewError(Handle<String> message) {
635 return NewError("$Error", message);
636}
637
638
639Handle<Object> Factory::NewError(const char* constructor,
640 Handle<String> message) {
Steve Block44f0eee2011-05-26 01:26:41 +0100641 Handle<String> constr = LookupAsciiSymbol(constructor);
642 Handle<JSFunction> fun = Handle<JSFunction>(
643 JSFunction::cast(isolate()->js_builtins_object()->
644 GetPropertyNoExceptionThrown(*constr)));
Steve Blocka7e24c12009-10-30 11:49:00 +0000645 Object** argv[1] = { Handle<Object>::cast(message).location() };
646
647 // Invoke the JavaScript factory method. If an exception is thrown while
648 // running the factory method, use the exception as the result.
649 bool caught_exception;
650 Handle<Object> result = Execution::TryCall(fun,
Steve Block44f0eee2011-05-26 01:26:41 +0100651 isolate()->js_builtins_object(), 1, argv, &caught_exception);
Steve Blocka7e24c12009-10-30 11:49:00 +0000652 return result;
653}
654
655
656Handle<JSFunction> Factory::NewFunction(Handle<String> name,
657 InstanceType type,
658 int instance_size,
659 Handle<Code> code,
660 bool force_initial_map) {
661 // Allocate the function
662 Handle<JSFunction> function = NewFunction(name, the_hole_value());
Iain Merrick75681382010-08-19 15:07:18 +0100663
664 // Setup the code pointer in both the shared function info and in
665 // the function itself.
666 function->shared()->set_code(*code);
Steve Blocka7e24c12009-10-30 11:49:00 +0000667 function->set_code(*code);
668
669 if (force_initial_map ||
670 type != JS_OBJECT_TYPE ||
671 instance_size != JSObject::kHeaderSize) {
672 Handle<Map> initial_map = NewMap(type, instance_size);
673 Handle<JSObject> prototype = NewFunctionPrototype(function);
674 initial_map->set_prototype(*prototype);
675 function->set_initial_map(*initial_map);
676 initial_map->set_constructor(*function);
677 } else {
678 ASSERT(!function->has_initial_map());
679 ASSERT(!function->has_prototype());
680 }
681
682 return function;
683}
684
685
Steve Blocka7e24c12009-10-30 11:49:00 +0000686Handle<JSFunction> Factory::NewFunctionWithPrototype(Handle<String> name,
687 InstanceType type,
688 int instance_size,
689 Handle<JSObject> prototype,
690 Handle<Code> code,
691 bool force_initial_map) {
Iain Merrick75681382010-08-19 15:07:18 +0100692 // Allocate the function.
Steve Blocka7e24c12009-10-30 11:49:00 +0000693 Handle<JSFunction> function = NewFunction(name, prototype);
694
Iain Merrick75681382010-08-19 15:07:18 +0100695 // Setup the code pointer in both the shared function info and in
696 // the function itself.
697 function->shared()->set_code(*code);
Steve Blocka7e24c12009-10-30 11:49:00 +0000698 function->set_code(*code);
699
700 if (force_initial_map ||
701 type != JS_OBJECT_TYPE ||
702 instance_size != JSObject::kHeaderSize) {
703 Handle<Map> initial_map = NewMap(type, instance_size);
704 function->set_initial_map(*initial_map);
705 initial_map->set_constructor(*function);
706 }
707
708 // Set function.prototype and give the prototype a constructor
709 // property that refers to the function.
710 SetPrototypeProperty(function, prototype);
Steve Block1e0659c2011-05-24 12:43:12 +0100711 // Currently safe because it is only invoked from Genesis.
Steve Block44f0eee2011-05-26 01:26:41 +0100712 SetLocalPropertyNoThrow(prototype, constructor_symbol(), function, DONT_ENUM);
Steve Blocka7e24c12009-10-30 11:49:00 +0000713 return function;
714}
715
716
Steve Block6ded16b2010-05-10 14:33:55 +0100717Handle<JSFunction> Factory::NewFunctionWithoutPrototype(Handle<String> name,
718 Handle<Code> code) {
Steve Block44f0eee2011-05-26 01:26:41 +0100719 Handle<JSFunction> function = NewFunctionWithoutPrototype(name,
720 kNonStrictMode);
Iain Merrick75681382010-08-19 15:07:18 +0100721 function->shared()->set_code(*code);
Steve Block6ded16b2010-05-10 14:33:55 +0100722 function->set_code(*code);
723 ASSERT(!function->has_initial_map());
724 ASSERT(!function->has_prototype());
725 return function;
726}
727
728
Steve Blocka7e24c12009-10-30 11:49:00 +0000729Handle<Code> Factory::NewCode(const CodeDesc& desc,
Steve Blocka7e24c12009-10-30 11:49:00 +0000730 Code::Flags flags,
Steve Block44f0eee2011-05-26 01:26:41 +0100731 Handle<Object> self_ref,
732 bool immovable) {
733 CALL_HEAP_FUNCTION(isolate(),
734 isolate()->heap()->CreateCode(
735 desc, flags, self_ref, immovable),
736 Code);
Steve Blocka7e24c12009-10-30 11:49:00 +0000737}
738
739
740Handle<Code> Factory::CopyCode(Handle<Code> code) {
Steve Block44f0eee2011-05-26 01:26:41 +0100741 CALL_HEAP_FUNCTION(isolate(),
742 isolate()->heap()->CopyCode(*code),
743 Code);
Steve Blocka7e24c12009-10-30 11:49:00 +0000744}
745
746
Steve Block6ded16b2010-05-10 14:33:55 +0100747Handle<Code> Factory::CopyCode(Handle<Code> code, Vector<byte> reloc_info) {
Steve Block44f0eee2011-05-26 01:26:41 +0100748 CALL_HEAP_FUNCTION(isolate(),
749 isolate()->heap()->CopyCode(*code, reloc_info),
750 Code);
Steve Block6ded16b2010-05-10 14:33:55 +0100751}
752
753
John Reck59135872010-11-02 12:39:01 -0700754MUST_USE_RESULT static inline MaybeObject* DoCopyInsert(
755 DescriptorArray* array,
756 String* key,
757 Object* value,
758 PropertyAttributes attributes) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000759 CallbacksDescriptor desc(key, value, attributes);
John Reck59135872010-11-02 12:39:01 -0700760 MaybeObject* obj = array->CopyInsert(&desc, REMOVE_TRANSITIONS);
Steve Blocka7e24c12009-10-30 11:49:00 +0000761 return obj;
762}
763
764
765// Allocate the new array.
Ben Murdoch257744e2011-11-30 15:57:28 +0000766Handle<DescriptorArray> Factory::CopyAppendForeignDescriptor(
Steve Blocka7e24c12009-10-30 11:49:00 +0000767 Handle<DescriptorArray> array,
768 Handle<String> key,
769 Handle<Object> value,
770 PropertyAttributes attributes) {
Steve Block44f0eee2011-05-26 01:26:41 +0100771 CALL_HEAP_FUNCTION(isolate(),
772 DoCopyInsert(*array, *key, *value, attributes),
Steve Blocka7e24c12009-10-30 11:49:00 +0000773 DescriptorArray);
774}
775
776
777Handle<String> Factory::SymbolFromString(Handle<String> value) {
Steve Block44f0eee2011-05-26 01:26:41 +0100778 CALL_HEAP_FUNCTION(isolate(),
779 isolate()->heap()->LookupSymbol(*value), String);
Steve Blocka7e24c12009-10-30 11:49:00 +0000780}
781
782
783Handle<DescriptorArray> Factory::CopyAppendCallbackDescriptors(
784 Handle<DescriptorArray> array,
785 Handle<Object> descriptors) {
786 v8::NeanderArray callbacks(descriptors);
787 int nof_callbacks = callbacks.length();
788 Handle<DescriptorArray> result =
789 NewDescriptorArray(array->number_of_descriptors() + nof_callbacks);
790
791 // Number of descriptors added to the result so far.
792 int descriptor_count = 0;
793
794 // Copy the descriptors from the array.
795 for (int i = 0; i < array->number_of_descriptors(); i++) {
796 if (array->GetType(i) != NULL_DESCRIPTOR) {
797 result->CopyFrom(descriptor_count++, *array, i);
798 }
799 }
800
801 // Number of duplicates detected.
802 int duplicates = 0;
803
804 // Fill in new callback descriptors. Process the callbacks from
805 // back to front so that the last callback with a given name takes
806 // precedence over previously added callbacks with that name.
807 for (int i = nof_callbacks - 1; i >= 0; i--) {
808 Handle<AccessorInfo> entry =
809 Handle<AccessorInfo>(AccessorInfo::cast(callbacks.get(i)));
810 // Ensure the key is a symbol before writing into the instance descriptor.
811 Handle<String> key =
812 SymbolFromString(Handle<String>(String::cast(entry->name())));
813 // Check if a descriptor with this name already exists before writing.
814 if (result->LinearSearch(*key, descriptor_count) ==
815 DescriptorArray::kNotFound) {
816 CallbacksDescriptor desc(*key, *entry, entry->property_attributes());
817 result->Set(descriptor_count, &desc);
818 descriptor_count++;
819 } else {
820 duplicates++;
821 }
822 }
823
824 // If duplicates were detected, allocate a result of the right size
825 // and transfer the elements.
826 if (duplicates > 0) {
827 int number_of_descriptors = result->number_of_descriptors() - duplicates;
828 Handle<DescriptorArray> new_result =
829 NewDescriptorArray(number_of_descriptors);
830 for (int i = 0; i < number_of_descriptors; i++) {
831 new_result->CopyFrom(i, *result, i);
832 }
833 result = new_result;
834 }
835
836 // Sort the result before returning.
837 result->Sort();
838 return result;
839}
840
841
842Handle<JSObject> Factory::NewJSObject(Handle<JSFunction> constructor,
843 PretenureFlag pretenure) {
Steve Block44f0eee2011-05-26 01:26:41 +0100844 CALL_HEAP_FUNCTION(
845 isolate(),
846 isolate()->heap()->AllocateJSObject(*constructor, pretenure), JSObject);
Steve Blocka7e24c12009-10-30 11:49:00 +0000847}
848
849
850Handle<GlobalObject> Factory::NewGlobalObject(
851 Handle<JSFunction> constructor) {
Steve Block44f0eee2011-05-26 01:26:41 +0100852 CALL_HEAP_FUNCTION(isolate(),
853 isolate()->heap()->AllocateGlobalObject(*constructor),
Steve Blocka7e24c12009-10-30 11:49:00 +0000854 GlobalObject);
855}
856
857
858
859Handle<JSObject> Factory::NewJSObjectFromMap(Handle<Map> map) {
Steve Block44f0eee2011-05-26 01:26:41 +0100860 CALL_HEAP_FUNCTION(
861 isolate(),
862 isolate()->heap()->AllocateJSObjectFromMap(*map, NOT_TENURED),
863 JSObject);
Steve Blocka7e24c12009-10-30 11:49:00 +0000864}
865
866
Steve Block44f0eee2011-05-26 01:26:41 +0100867Handle<JSArray> Factory::NewJSArray(int capacity,
Steve Blocka7e24c12009-10-30 11:49:00 +0000868 PretenureFlag pretenure) {
Steve Block44f0eee2011-05-26 01:26:41 +0100869 Handle<JSObject> obj = NewJSObject(isolate()->array_function(), pretenure);
870 CALL_HEAP_FUNCTION(isolate(),
871 Handle<JSArray>::cast(obj)->Initialize(capacity),
872 JSArray);
Steve Blocka7e24c12009-10-30 11:49:00 +0000873}
874
875
876Handle<JSArray> Factory::NewJSArrayWithElements(Handle<FixedArray> elements,
877 PretenureFlag pretenure) {
878 Handle<JSArray> result =
Steve Block44f0eee2011-05-26 01:26:41 +0100879 Handle<JSArray>::cast(NewJSObject(isolate()->array_function(),
880 pretenure));
Steve Blocka7e24c12009-10-30 11:49:00 +0000881 result->SetContent(*elements);
882 return result;
883}
884
885
Ben Murdoch257744e2011-11-30 15:57:28 +0000886Handle<JSProxy> Factory::NewJSProxy(Handle<Object> handler,
887 Handle<Object> prototype) {
888 CALL_HEAP_FUNCTION(
889 isolate(),
890 isolate()->heap()->AllocateJSProxy(*handler, *prototype),
891 JSProxy);
892}
893
894
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000895void Factory::BecomeJSObject(Handle<JSProxy> object) {
896 CALL_HEAP_FUNCTION_VOID(
897 isolate(),
898 isolate()->heap()->ReinitializeJSProxyAsJSObject(*object));
899}
900
901
Steve Block6ded16b2010-05-10 14:33:55 +0100902Handle<SharedFunctionInfo> Factory::NewSharedFunctionInfo(
Ben Murdoch3bec4d22010-07-22 14:51:16 +0100903 Handle<String> name,
904 int number_of_literals,
905 Handle<Code> code,
906 Handle<SerializedScopeInfo> scope_info) {
Steve Block6ded16b2010-05-10 14:33:55 +0100907 Handle<SharedFunctionInfo> shared = NewSharedFunctionInfo(name);
908 shared->set_code(*code);
Ben Murdoch3bec4d22010-07-22 14:51:16 +0100909 shared->set_scope_info(*scope_info);
Steve Block6ded16b2010-05-10 14:33:55 +0100910 int literals_array_size = number_of_literals;
911 // If the function contains object, regexp or array literals,
912 // allocate extra space for a literals array prefix containing the
913 // context.
914 if (number_of_literals > 0) {
915 literals_array_size += JSFunction::kLiteralsPrefixSize;
916 }
917 shared->set_num_literals(literals_array_size);
918 return shared;
919}
920
921
Steve Block1e0659c2011-05-24 12:43:12 +0100922Handle<JSMessageObject> Factory::NewJSMessageObject(
923 Handle<String> type,
924 Handle<JSArray> arguments,
925 int start_position,
926 int end_position,
927 Handle<Object> script,
928 Handle<Object> stack_trace,
929 Handle<Object> stack_frames) {
Steve Block44f0eee2011-05-26 01:26:41 +0100930 CALL_HEAP_FUNCTION(isolate(),
931 isolate()->heap()->AllocateJSMessageObject(*type,
932 *arguments,
933 start_position,
934 end_position,
935 *script,
936 *stack_trace,
937 *stack_frames),
Steve Block1e0659c2011-05-24 12:43:12 +0100938 JSMessageObject);
939}
940
Steve Blocka7e24c12009-10-30 11:49:00 +0000941Handle<SharedFunctionInfo> Factory::NewSharedFunctionInfo(Handle<String> name) {
Steve Block44f0eee2011-05-26 01:26:41 +0100942 CALL_HEAP_FUNCTION(isolate(),
943 isolate()->heap()->AllocateSharedFunctionInfo(*name),
Steve Blocka7e24c12009-10-30 11:49:00 +0000944 SharedFunctionInfo);
945}
946
947
948Handle<String> Factory::NumberToString(Handle<Object> number) {
Steve Block44f0eee2011-05-26 01:26:41 +0100949 CALL_HEAP_FUNCTION(isolate(),
950 isolate()->heap()->NumberToString(*number), String);
Steve Blocka7e24c12009-10-30 11:49:00 +0000951}
952
953
954Handle<NumberDictionary> Factory::DictionaryAtNumberPut(
955 Handle<NumberDictionary> dictionary,
956 uint32_t key,
957 Handle<Object> value) {
Steve Block44f0eee2011-05-26 01:26:41 +0100958 CALL_HEAP_FUNCTION(isolate(),
959 dictionary->AtNumberPut(key, *value),
960 NumberDictionary);
Steve Blocka7e24c12009-10-30 11:49:00 +0000961}
962
963
964Handle<JSFunction> Factory::NewFunctionHelper(Handle<String> name,
965 Handle<Object> prototype) {
966 Handle<SharedFunctionInfo> function_share = NewSharedFunctionInfo(name);
Steve Block44f0eee2011-05-26 01:26:41 +0100967 CALL_HEAP_FUNCTION(
968 isolate(),
969 isolate()->heap()->AllocateFunction(*isolate()->function_map(),
970 *function_share,
971 *prototype),
972 JSFunction);
Steve Blocka7e24c12009-10-30 11:49:00 +0000973}
974
975
976Handle<JSFunction> Factory::NewFunction(Handle<String> name,
977 Handle<Object> prototype) {
978 Handle<JSFunction> fun = NewFunctionHelper(name, prototype);
Steve Block44f0eee2011-05-26 01:26:41 +0100979 fun->set_context(isolate()->context()->global_context());
Steve Blocka7e24c12009-10-30 11:49:00 +0000980 return fun;
981}
982
983
Steve Block6ded16b2010-05-10 14:33:55 +0100984Handle<JSFunction> Factory::NewFunctionWithoutPrototypeHelper(
Steve Block44f0eee2011-05-26 01:26:41 +0100985 Handle<String> name,
986 StrictModeFlag strict_mode) {
Steve Block6ded16b2010-05-10 14:33:55 +0100987 Handle<SharedFunctionInfo> function_share = NewSharedFunctionInfo(name);
Steve Block44f0eee2011-05-26 01:26:41 +0100988 Handle<Map> map = strict_mode == kStrictMode
989 ? isolate()->strict_mode_function_without_prototype_map()
990 : isolate()->function_without_prototype_map();
991 CALL_HEAP_FUNCTION(isolate(),
992 isolate()->heap()->AllocateFunction(
993 *map,
Steve Block6ded16b2010-05-10 14:33:55 +0100994 *function_share,
995 *the_hole_value()),
996 JSFunction);
997}
998
999
Steve Block44f0eee2011-05-26 01:26:41 +01001000Handle<JSFunction> Factory::NewFunctionWithoutPrototype(
1001 Handle<String> name,
1002 StrictModeFlag strict_mode) {
1003 Handle<JSFunction> fun = NewFunctionWithoutPrototypeHelper(name, strict_mode);
1004 fun->set_context(isolate()->context()->global_context());
Steve Block6ded16b2010-05-10 14:33:55 +01001005 return fun;
1006}
1007
1008
Leon Clarkee46be812010-01-19 14:06:41 +00001009Handle<Object> Factory::ToObject(Handle<Object> object) {
Steve Block44f0eee2011-05-26 01:26:41 +01001010 CALL_HEAP_FUNCTION(isolate(), object->ToObject(), Object);
Leon Clarkee46be812010-01-19 14:06:41 +00001011}
1012
1013
Steve Blocka7e24c12009-10-30 11:49:00 +00001014Handle<Object> Factory::ToObject(Handle<Object> object,
1015 Handle<Context> global_context) {
Steve Block44f0eee2011-05-26 01:26:41 +01001016 CALL_HEAP_FUNCTION(isolate(), object->ToObject(*global_context), Object);
Steve Blocka7e24c12009-10-30 11:49:00 +00001017}
1018
1019
1020#ifdef ENABLE_DEBUGGER_SUPPORT
1021Handle<DebugInfo> Factory::NewDebugInfo(Handle<SharedFunctionInfo> shared) {
1022 // Get the original code of the function.
1023 Handle<Code> code(shared->code());
1024
1025 // Create a copy of the code before allocating the debug info object to avoid
1026 // allocation while setting up the debug info object.
1027 Handle<Code> original_code(*Factory::CopyCode(code));
1028
1029 // Allocate initial fixed array for active break points before allocating the
1030 // debug info object to avoid allocation while setting up the debug info
1031 // object.
1032 Handle<FixedArray> break_points(
Steve Block44f0eee2011-05-26 01:26:41 +01001033 NewFixedArray(Debug::kEstimatedNofBreakPointsInFunction));
Steve Blocka7e24c12009-10-30 11:49:00 +00001034
1035 // Create and set up the debug info object. Debug info contains function, a
1036 // copy of the original code, the executing code and initial fixed array for
1037 // active break points.
1038 Handle<DebugInfo> debug_info =
Steve Block44f0eee2011-05-26 01:26:41 +01001039 Handle<DebugInfo>::cast(NewStruct(DEBUG_INFO_TYPE));
Steve Blocka7e24c12009-10-30 11:49:00 +00001040 debug_info->set_shared(*shared);
1041 debug_info->set_original_code(*original_code);
1042 debug_info->set_code(*code);
1043 debug_info->set_break_points(*break_points);
1044
1045 // Link debug info to function.
1046 shared->set_debug_info(*debug_info);
1047
1048 return debug_info;
1049}
1050#endif
1051
1052
1053Handle<JSObject> Factory::NewArgumentsObject(Handle<Object> callee,
1054 int length) {
Steve Block44f0eee2011-05-26 01:26:41 +01001055 CALL_HEAP_FUNCTION(
1056 isolate(),
1057 isolate()->heap()->AllocateArgumentsObject(*callee, length), JSObject);
Steve Blocka7e24c12009-10-30 11:49:00 +00001058}
1059
1060
1061Handle<JSFunction> Factory::CreateApiFunction(
1062 Handle<FunctionTemplateInfo> obj, ApiInstanceType instance_type) {
Steve Block44f0eee2011-05-26 01:26:41 +01001063 Handle<Code> code = isolate()->builtins()->HandleApiCall();
1064 Handle<Code> construct_stub = isolate()->builtins()->JSConstructStubApi();
Steve Blocka7e24c12009-10-30 11:49:00 +00001065
1066 int internal_field_count = 0;
1067 if (!obj->instance_template()->IsUndefined()) {
1068 Handle<ObjectTemplateInfo> instance_template =
1069 Handle<ObjectTemplateInfo>(
1070 ObjectTemplateInfo::cast(obj->instance_template()));
1071 internal_field_count =
1072 Smi::cast(instance_template->internal_field_count())->value();
1073 }
1074
1075 int instance_size = kPointerSize * internal_field_count;
1076 InstanceType type = INVALID_TYPE;
1077 switch (instance_type) {
1078 case JavaScriptObject:
1079 type = JS_OBJECT_TYPE;
1080 instance_size += JSObject::kHeaderSize;
1081 break;
1082 case InnerGlobalObject:
1083 type = JS_GLOBAL_OBJECT_TYPE;
1084 instance_size += JSGlobalObject::kSize;
1085 break;
1086 case OuterGlobalObject:
1087 type = JS_GLOBAL_PROXY_TYPE;
1088 instance_size += JSGlobalProxy::kSize;
1089 break;
1090 default:
1091 break;
1092 }
1093 ASSERT(type != INVALID_TYPE);
1094
1095 Handle<JSFunction> result =
Steve Block44f0eee2011-05-26 01:26:41 +01001096 NewFunction(Factory::empty_symbol(),
1097 type,
1098 instance_size,
1099 code,
1100 true);
Steve Blocka7e24c12009-10-30 11:49:00 +00001101 // Set class name.
1102 Handle<Object> class_name = Handle<Object>(obj->class_name());
1103 if (class_name->IsString()) {
1104 result->shared()->set_instance_class_name(*class_name);
1105 result->shared()->set_name(*class_name);
1106 }
1107
1108 Handle<Map> map = Handle<Map>(result->initial_map());
1109
1110 // Mark as undetectable if needed.
1111 if (obj->undetectable()) {
1112 map->set_is_undetectable();
1113 }
1114
1115 // Mark as hidden for the __proto__ accessor if needed.
1116 if (obj->hidden_prototype()) {
1117 map->set_is_hidden_prototype();
1118 }
1119
1120 // Mark as needs_access_check if needed.
1121 if (obj->needs_access_check()) {
1122 map->set_is_access_check_needed(true);
1123 }
1124
1125 // Set interceptor information in the map.
1126 if (!obj->named_property_handler()->IsUndefined()) {
1127 map->set_has_named_interceptor();
1128 }
1129 if (!obj->indexed_property_handler()->IsUndefined()) {
1130 map->set_has_indexed_interceptor();
1131 }
1132
1133 // Set instance call-as-function information in the map.
1134 if (!obj->instance_call_handler()->IsUndefined()) {
1135 map->set_has_instance_call_handler();
1136 }
1137
1138 result->shared()->set_function_data(*obj);
Leon Clarkee46be812010-01-19 14:06:41 +00001139 result->shared()->set_construct_stub(*construct_stub);
Steve Blocka7e24c12009-10-30 11:49:00 +00001140 result->shared()->DontAdaptArguments();
1141
1142 // Recursively copy parent templates' accessors, 'data' may be modified.
1143 Handle<DescriptorArray> array =
1144 Handle<DescriptorArray>(map->instance_descriptors());
1145 while (true) {
1146 Handle<Object> props = Handle<Object>(obj->property_accessors());
1147 if (!props->IsUndefined()) {
Steve Block44f0eee2011-05-26 01:26:41 +01001148 array = CopyAppendCallbackDescriptors(array, props);
Steve Blocka7e24c12009-10-30 11:49:00 +00001149 }
1150 Handle<Object> parent = Handle<Object>(obj->parent_template());
1151 if (parent->IsUndefined()) break;
1152 obj = Handle<FunctionTemplateInfo>::cast(parent);
1153 }
1154 if (!array->IsEmpty()) {
1155 map->set_instance_descriptors(*array);
1156 }
1157
Steve Block6ded16b2010-05-10 14:33:55 +01001158 ASSERT(result->shared()->IsApiFunction());
Steve Blocka7e24c12009-10-30 11:49:00 +00001159 return result;
1160}
1161
1162
1163Handle<MapCache> Factory::NewMapCache(int at_least_space_for) {
Steve Block44f0eee2011-05-26 01:26:41 +01001164 CALL_HEAP_FUNCTION(isolate(),
1165 MapCache::Allocate(at_least_space_for), MapCache);
Steve Blocka7e24c12009-10-30 11:49:00 +00001166}
1167
1168
John Reck59135872010-11-02 12:39:01 -07001169MUST_USE_RESULT static MaybeObject* UpdateMapCacheWith(Context* context,
1170 FixedArray* keys,
1171 Map* map) {
1172 Object* result;
1173 { MaybeObject* maybe_result =
1174 MapCache::cast(context->map_cache())->Put(keys, map);
1175 if (!maybe_result->ToObject(&result)) return maybe_result;
1176 }
1177 context->set_map_cache(MapCache::cast(result));
Steve Blocka7e24c12009-10-30 11:49:00 +00001178 return result;
1179}
1180
1181
1182Handle<MapCache> Factory::AddToMapCache(Handle<Context> context,
1183 Handle<FixedArray> keys,
1184 Handle<Map> map) {
Steve Block44f0eee2011-05-26 01:26:41 +01001185 CALL_HEAP_FUNCTION(isolate(),
1186 UpdateMapCacheWith(*context, *keys, *map), MapCache);
Steve Blocka7e24c12009-10-30 11:49:00 +00001187}
1188
1189
1190Handle<Map> Factory::ObjectLiteralMapFromCache(Handle<Context> context,
1191 Handle<FixedArray> keys) {
1192 if (context->map_cache()->IsUndefined()) {
1193 // Allocate the new map cache for the global context.
1194 Handle<MapCache> new_cache = NewMapCache(24);
1195 context->set_map_cache(*new_cache);
1196 }
1197 // Check to see whether there is a matching element in the cache.
1198 Handle<MapCache> cache =
1199 Handle<MapCache>(MapCache::cast(context->map_cache()));
1200 Handle<Object> result = Handle<Object>(cache->Lookup(*keys));
1201 if (result->IsMap()) return Handle<Map>::cast(result);
1202 // Create a new map and add it to the cache.
1203 Handle<Map> map =
1204 CopyMap(Handle<Map>(context->object_function()->initial_map()),
1205 keys->length());
1206 AddToMapCache(context, keys, map);
1207 return Handle<Map>(map);
1208}
1209
1210
1211void Factory::SetRegExpAtomData(Handle<JSRegExp> regexp,
1212 JSRegExp::Type type,
1213 Handle<String> source,
1214 JSRegExp::Flags flags,
1215 Handle<Object> data) {
1216 Handle<FixedArray> store = NewFixedArray(JSRegExp::kAtomDataSize);
1217
1218 store->set(JSRegExp::kTagIndex, Smi::FromInt(type));
1219 store->set(JSRegExp::kSourceIndex, *source);
1220 store->set(JSRegExp::kFlagsIndex, Smi::FromInt(flags.value()));
1221 store->set(JSRegExp::kAtomPatternIndex, *data);
1222 regexp->set_data(*store);
1223}
1224
1225void Factory::SetRegExpIrregexpData(Handle<JSRegExp> regexp,
1226 JSRegExp::Type type,
1227 Handle<String> source,
1228 JSRegExp::Flags flags,
1229 int capture_count) {
1230 Handle<FixedArray> store = NewFixedArray(JSRegExp::kIrregexpDataSize);
Ben Murdoch257744e2011-11-30 15:57:28 +00001231 Smi* uninitialized = Smi::FromInt(JSRegExp::kUninitializedValue);
Steve Blocka7e24c12009-10-30 11:49:00 +00001232 store->set(JSRegExp::kTagIndex, Smi::FromInt(type));
1233 store->set(JSRegExp::kSourceIndex, *source);
1234 store->set(JSRegExp::kFlagsIndex, Smi::FromInt(flags.value()));
Ben Murdoch257744e2011-11-30 15:57:28 +00001235 store->set(JSRegExp::kIrregexpASCIICodeIndex, uninitialized);
1236 store->set(JSRegExp::kIrregexpUC16CodeIndex, uninitialized);
1237 store->set(JSRegExp::kIrregexpASCIICodeSavedIndex, uninitialized);
1238 store->set(JSRegExp::kIrregexpUC16CodeSavedIndex, uninitialized);
Steve Blocka7e24c12009-10-30 11:49:00 +00001239 store->set(JSRegExp::kIrregexpMaxRegisterCountIndex, Smi::FromInt(0));
1240 store->set(JSRegExp::kIrregexpCaptureCountIndex,
1241 Smi::FromInt(capture_count));
1242 regexp->set_data(*store);
1243}
1244
1245
1246
1247void Factory::ConfigureInstance(Handle<FunctionTemplateInfo> desc,
1248 Handle<JSObject> instance,
1249 bool* pending_exception) {
1250 // Configure the instance by adding the properties specified by the
1251 // instance template.
1252 Handle<Object> instance_template = Handle<Object>(desc->instance_template());
1253 if (!instance_template->IsUndefined()) {
1254 Execution::ConfigureInstance(instance,
1255 instance_template,
1256 pending_exception);
1257 } else {
1258 *pending_exception = false;
1259 }
1260}
1261
1262
1263} } // namespace v8::internal