blob: d65338385e08111fc06b51309d859249ef17973f [file] [log] [blame]
Steve Blocka7e24c12009-10-30 11:49:00 +00001// Copyright 2006-2008 the V8 project authors. All rights reserved.
2// 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"
35
36namespace v8 {
37namespace internal {
38
39
40Handle<FixedArray> Factory::NewFixedArray(int size, PretenureFlag pretenure) {
41 ASSERT(0 <= size);
42 CALL_HEAP_FUNCTION(Heap::AllocateFixedArray(size, pretenure), FixedArray);
43}
44
45
Steve Block6ded16b2010-05-10 14:33:55 +010046Handle<FixedArray> Factory::NewFixedArrayWithHoles(int size,
47 PretenureFlag pretenure) {
Steve Blocka7e24c12009-10-30 11:49:00 +000048 ASSERT(0 <= size);
Steve Block6ded16b2010-05-10 14:33:55 +010049 CALL_HEAP_FUNCTION(Heap::AllocateFixedArrayWithHoles(size, pretenure),
50 FixedArray);
Steve Blocka7e24c12009-10-30 11:49:00 +000051}
52
53
54Handle<StringDictionary> Factory::NewStringDictionary(int at_least_space_for) {
55 ASSERT(0 <= at_least_space_for);
56 CALL_HEAP_FUNCTION(StringDictionary::Allocate(at_least_space_for),
57 StringDictionary);
58}
59
60
61Handle<NumberDictionary> Factory::NewNumberDictionary(int at_least_space_for) {
62 ASSERT(0 <= at_least_space_for);
63 CALL_HEAP_FUNCTION(NumberDictionary::Allocate(at_least_space_for),
64 NumberDictionary);
65}
66
67
68Handle<DescriptorArray> Factory::NewDescriptorArray(int number_of_descriptors) {
69 ASSERT(0 <= number_of_descriptors);
70 CALL_HEAP_FUNCTION(DescriptorArray::Allocate(number_of_descriptors),
71 DescriptorArray);
72}
73
74
75// Symbols are created in the old generation (data space).
76Handle<String> Factory::LookupSymbol(Vector<const char> string) {
77 CALL_HEAP_FUNCTION(Heap::LookupSymbol(string), String);
78}
79
80
81Handle<String> Factory::NewStringFromAscii(Vector<const char> string,
82 PretenureFlag pretenure) {
83 CALL_HEAP_FUNCTION(Heap::AllocateStringFromAscii(string, pretenure), String);
84}
85
86Handle<String> Factory::NewStringFromUtf8(Vector<const char> string,
87 PretenureFlag pretenure) {
88 CALL_HEAP_FUNCTION(Heap::AllocateStringFromUtf8(string, pretenure), String);
89}
90
91
92Handle<String> Factory::NewStringFromTwoByte(Vector<const uc16> string,
93 PretenureFlag pretenure) {
94 CALL_HEAP_FUNCTION(Heap::AllocateStringFromTwoByte(string, pretenure),
95 String);
96}
97
98
Leon Clarkeac952652010-07-15 11:15:24 +010099Handle<String> Factory::NewRawAsciiString(int length,
100 PretenureFlag pretenure) {
101 CALL_HEAP_FUNCTION(Heap::AllocateRawAsciiString(length, pretenure), String);
102}
103
104
Steve Blocka7e24c12009-10-30 11:49:00 +0000105Handle<String> Factory::NewRawTwoByteString(int length,
106 PretenureFlag pretenure) {
107 CALL_HEAP_FUNCTION(Heap::AllocateRawTwoByteString(length, pretenure), String);
108}
109
110
111Handle<String> Factory::NewConsString(Handle<String> first,
112 Handle<String> second) {
113 CALL_HEAP_FUNCTION(Heap::AllocateConsString(*first, *second), String);
114}
115
116
Steve Blockd0582a62009-12-15 09:54:21 +0000117Handle<String> Factory::NewSubString(Handle<String> str,
118 int begin,
119 int end) {
120 CALL_HEAP_FUNCTION(str->SubString(begin, end), String);
Steve Blocka7e24c12009-10-30 11:49:00 +0000121}
122
123
124Handle<String> Factory::NewExternalStringFromAscii(
125 ExternalAsciiString::Resource* resource) {
126 CALL_HEAP_FUNCTION(Heap::AllocateExternalStringFromAscii(resource), String);
127}
128
129
130Handle<String> Factory::NewExternalStringFromTwoByte(
131 ExternalTwoByteString::Resource* resource) {
132 CALL_HEAP_FUNCTION(Heap::AllocateExternalStringFromTwoByte(resource), String);
133}
134
135
136Handle<Context> Factory::NewGlobalContext() {
137 CALL_HEAP_FUNCTION(Heap::AllocateGlobalContext(), Context);
138}
139
140
141Handle<Context> Factory::NewFunctionContext(int length,
142 Handle<JSFunction> closure) {
143 CALL_HEAP_FUNCTION(Heap::AllocateFunctionContext(length, *closure), Context);
144}
145
146
147Handle<Context> Factory::NewWithContext(Handle<Context> previous,
148 Handle<JSObject> extension,
149 bool is_catch_context) {
150 CALL_HEAP_FUNCTION(Heap::AllocateWithContext(*previous,
151 *extension,
152 is_catch_context),
153 Context);
154}
155
156
157Handle<Struct> Factory::NewStruct(InstanceType type) {
158 CALL_HEAP_FUNCTION(Heap::AllocateStruct(type), Struct);
159}
160
161
162Handle<AccessorInfo> Factory::NewAccessorInfo() {
163 Handle<AccessorInfo> info =
164 Handle<AccessorInfo>::cast(NewStruct(ACCESSOR_INFO_TYPE));
165 info->set_flag(0); // Must clear the flag, it was initialized as undefined.
166 return info;
167}
168
169
170Handle<Script> Factory::NewScript(Handle<String> source) {
171 // Generate id for this script.
172 int id;
173 if (Heap::last_script_id()->IsUndefined()) {
174 // Script ids start from one.
175 id = 1;
176 } else {
177 // Increment id, wrap when positive smi is exhausted.
178 id = Smi::cast(Heap::last_script_id())->value();
179 id++;
180 if (!Smi::IsValid(id)) {
181 id = 0;
182 }
183 }
184 Heap::SetLastScriptId(Smi::FromInt(id));
185
186 // Create and initialize script object.
187 Handle<Proxy> wrapper = Factory::NewProxy(0, TENURED);
188 Handle<Script> script = Handle<Script>::cast(NewStruct(SCRIPT_TYPE));
189 script->set_source(*source);
190 script->set_name(Heap::undefined_value());
191 script->set_id(Heap::last_script_id());
192 script->set_line_offset(Smi::FromInt(0));
193 script->set_column_offset(Smi::FromInt(0));
194 script->set_data(Heap::undefined_value());
195 script->set_context_data(Heap::undefined_value());
196 script->set_type(Smi::FromInt(Script::TYPE_NORMAL));
197 script->set_compilation_type(Smi::FromInt(Script::COMPILATION_TYPE_HOST));
198 script->set_wrapper(*wrapper);
199 script->set_line_ends(Heap::undefined_value());
Steve Blockd0582a62009-12-15 09:54:21 +0000200 script->set_eval_from_shared(Heap::undefined_value());
Steve Blocka7e24c12009-10-30 11:49:00 +0000201 script->set_eval_from_instructions_offset(Smi::FromInt(0));
202
203 return script;
204}
205
206
207Handle<Proxy> Factory::NewProxy(Address addr, PretenureFlag pretenure) {
208 CALL_HEAP_FUNCTION(Heap::AllocateProxy(addr, pretenure), Proxy);
209}
210
211
212Handle<Proxy> Factory::NewProxy(const AccessorDescriptor* desc) {
213 return NewProxy((Address) desc, TENURED);
214}
215
216
217Handle<ByteArray> Factory::NewByteArray(int length, PretenureFlag pretenure) {
218 ASSERT(0 <= length);
219 CALL_HEAP_FUNCTION(Heap::AllocateByteArray(length, pretenure), ByteArray);
220}
221
222
223Handle<PixelArray> Factory::NewPixelArray(int length,
224 uint8_t* external_pointer,
225 PretenureFlag pretenure) {
226 ASSERT(0 <= length);
227 CALL_HEAP_FUNCTION(Heap::AllocatePixelArray(length,
228 external_pointer,
229 pretenure), PixelArray);
230}
231
232
Steve Block3ce2e202009-11-05 08:53:23 +0000233Handle<ExternalArray> Factory::NewExternalArray(int length,
234 ExternalArrayType array_type,
235 void* external_pointer,
236 PretenureFlag pretenure) {
237 ASSERT(0 <= length);
238 CALL_HEAP_FUNCTION(Heap::AllocateExternalArray(length,
239 array_type,
240 external_pointer,
241 pretenure), ExternalArray);
242}
243
244
Steve Blocka7e24c12009-10-30 11:49:00 +0000245Handle<Map> Factory::NewMap(InstanceType type, int instance_size) {
246 CALL_HEAP_FUNCTION(Heap::AllocateMap(type, instance_size), Map);
247}
248
249
250Handle<JSObject> Factory::NewFunctionPrototype(Handle<JSFunction> function) {
251 CALL_HEAP_FUNCTION(Heap::AllocateFunctionPrototype(*function), JSObject);
252}
253
254
255Handle<Map> Factory::CopyMapDropDescriptors(Handle<Map> src) {
256 CALL_HEAP_FUNCTION(src->CopyDropDescriptors(), Map);
257}
258
259
260Handle<Map> Factory::CopyMap(Handle<Map> src,
261 int extra_inobject_properties) {
262 Handle<Map> copy = CopyMapDropDescriptors(src);
263 // Check that we do not overflow the instance size when adding the
264 // extra inobject properties.
265 int instance_size_delta = extra_inobject_properties * kPointerSize;
266 int max_instance_size_delta =
267 JSObject::kMaxInstanceSize - copy->instance_size();
268 if (instance_size_delta > max_instance_size_delta) {
269 // If the instance size overflows, we allocate as many properties
270 // as we can as inobject properties.
271 instance_size_delta = max_instance_size_delta;
272 extra_inobject_properties = max_instance_size_delta >> kPointerSizeLog2;
273 }
274 // Adjust the map with the extra inobject properties.
275 int inobject_properties =
276 copy->inobject_properties() + extra_inobject_properties;
277 copy->set_inobject_properties(inobject_properties);
278 copy->set_unused_property_fields(inobject_properties);
279 copy->set_instance_size(copy->instance_size() + instance_size_delta);
Ben Murdoch3bec4d22010-07-22 14:51:16 +0100280 copy->set_scavenger(Heap::GetScavenger(copy->instance_type(),
281 copy->instance_size()));
Steve Blocka7e24c12009-10-30 11:49:00 +0000282 return copy;
283}
284
Steve Block8defd9f2010-07-08 12:39:36 +0100285
Steve Blocka7e24c12009-10-30 11:49:00 +0000286Handle<Map> Factory::CopyMapDropTransitions(Handle<Map> src) {
287 CALL_HEAP_FUNCTION(src->CopyDropTransitions(), Map);
288}
289
290
Steve Block8defd9f2010-07-08 12:39:36 +0100291Handle<Map> Factory::GetFastElementsMap(Handle<Map> src) {
292 CALL_HEAP_FUNCTION(src->GetFastElementsMap(), Map);
293}
294
295
296Handle<Map> Factory::GetSlowElementsMap(Handle<Map> src) {
297 CALL_HEAP_FUNCTION(src->GetSlowElementsMap(), Map);
298}
299
300
Steve Blocka7e24c12009-10-30 11:49:00 +0000301Handle<FixedArray> Factory::CopyFixedArray(Handle<FixedArray> array) {
302 CALL_HEAP_FUNCTION(array->Copy(), FixedArray);
303}
304
305
Steve Block6ded16b2010-05-10 14:33:55 +0100306Handle<JSFunction> Factory::BaseNewFunctionFromSharedFunctionInfo(
307 Handle<SharedFunctionInfo> function_info,
Leon Clarkee46be812010-01-19 14:06:41 +0000308 Handle<Map> function_map,
309 PretenureFlag pretenure) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000310 CALL_HEAP_FUNCTION(Heap::AllocateFunction(*function_map,
Steve Block6ded16b2010-05-10 14:33:55 +0100311 *function_info,
Leon Clarkee46be812010-01-19 14:06:41 +0000312 Heap::the_hole_value(),
313 pretenure),
Steve Blocka7e24c12009-10-30 11:49:00 +0000314 JSFunction);
315}
316
317
Steve Block6ded16b2010-05-10 14:33:55 +0100318Handle<JSFunction> Factory::NewFunctionFromSharedFunctionInfo(
319 Handle<SharedFunctionInfo> function_info,
Leon Clarkee46be812010-01-19 14:06:41 +0000320 Handle<Context> context,
321 PretenureFlag pretenure) {
Steve Block6ded16b2010-05-10 14:33:55 +0100322 Handle<JSFunction> result = BaseNewFunctionFromSharedFunctionInfo(
323 function_info, Top::function_map(), pretenure);
Steve Blocka7e24c12009-10-30 11:49:00 +0000324 result->set_context(*context);
Steve Block6ded16b2010-05-10 14:33:55 +0100325 int number_of_literals = function_info->num_literals();
Steve Blocka7e24c12009-10-30 11:49:00 +0000326 Handle<FixedArray> literals =
Leon Clarkee46be812010-01-19 14:06:41 +0000327 Factory::NewFixedArray(number_of_literals, pretenure);
Steve Blocka7e24c12009-10-30 11:49:00 +0000328 if (number_of_literals > 0) {
329 // Store the object, regexp and array functions in the literals
330 // array prefix. These functions will be used when creating
331 // object, regexp and array literals in this function.
332 literals->set(JSFunction::kLiteralGlobalContextIndex,
333 context->global_context());
334 }
335 result->set_literals(*literals);
Steve Blocka7e24c12009-10-30 11:49:00 +0000336 return result;
337}
338
339
340Handle<Object> Factory::NewNumber(double value,
341 PretenureFlag pretenure) {
342 CALL_HEAP_FUNCTION(Heap::NumberFromDouble(value, pretenure), Object);
343}
344
345
346Handle<Object> Factory::NewNumberFromInt(int value) {
347 CALL_HEAP_FUNCTION(Heap::NumberFromInt32(value), Object);
348}
349
350
351Handle<Object> Factory::NewNumberFromUint(uint32_t value) {
352 CALL_HEAP_FUNCTION(Heap::NumberFromUint32(value), Object);
353}
354
355
356Handle<JSObject> Factory::NewNeanderObject() {
357 CALL_HEAP_FUNCTION(Heap::AllocateJSObjectFromMap(Heap::neander_map()),
358 JSObject);
359}
360
361
362Handle<Object> Factory::NewTypeError(const char* type,
363 Vector< Handle<Object> > args) {
364 return NewError("MakeTypeError", type, args);
365}
366
367
368Handle<Object> Factory::NewTypeError(Handle<String> message) {
369 return NewError("$TypeError", message);
370}
371
372
373Handle<Object> Factory::NewRangeError(const char* type,
374 Vector< Handle<Object> > args) {
375 return NewError("MakeRangeError", type, args);
376}
377
378
379Handle<Object> Factory::NewRangeError(Handle<String> message) {
380 return NewError("$RangeError", message);
381}
382
383
384Handle<Object> Factory::NewSyntaxError(const char* type, Handle<JSArray> args) {
385 return NewError("MakeSyntaxError", type, args);
386}
387
388
389Handle<Object> Factory::NewSyntaxError(Handle<String> message) {
390 return NewError("$SyntaxError", message);
391}
392
393
394Handle<Object> Factory::NewReferenceError(const char* type,
395 Vector< Handle<Object> > args) {
396 return NewError("MakeReferenceError", type, args);
397}
398
399
400Handle<Object> Factory::NewReferenceError(Handle<String> message) {
401 return NewError("$ReferenceError", message);
402}
403
404
405Handle<Object> Factory::NewError(const char* maker, const char* type,
406 Vector< Handle<Object> > args) {
407 v8::HandleScope scope; // Instantiate a closeable HandleScope for EscapeFrom.
408 Handle<FixedArray> array = Factory::NewFixedArray(args.length());
409 for (int i = 0; i < args.length(); i++) {
410 array->set(i, *args[i]);
411 }
412 Handle<JSArray> object = Factory::NewJSArrayWithElements(array);
413 Handle<Object> result = NewError(maker, type, object);
414 return result.EscapeFrom(&scope);
415}
416
417
418Handle<Object> Factory::NewEvalError(const char* type,
419 Vector< Handle<Object> > args) {
420 return NewError("MakeEvalError", type, args);
421}
422
423
424Handle<Object> Factory::NewError(const char* type,
425 Vector< Handle<Object> > args) {
426 return NewError("MakeError", type, args);
427}
428
429
430Handle<Object> Factory::NewError(const char* maker,
431 const char* type,
432 Handle<JSArray> args) {
433 Handle<String> make_str = Factory::LookupAsciiSymbol(maker);
434 Handle<Object> fun_obj(Top::builtins()->GetProperty(*make_str));
435 // If the builtins haven't been properly configured yet this error
436 // constructor may not have been defined. Bail out.
437 if (!fun_obj->IsJSFunction())
438 return Factory::undefined_value();
439 Handle<JSFunction> fun = Handle<JSFunction>::cast(fun_obj);
440 Handle<Object> type_obj = Factory::LookupAsciiSymbol(type);
441 Object** argv[2] = { type_obj.location(),
442 Handle<Object>::cast(args).location() };
443
444 // Invoke the JavaScript factory method. If an exception is thrown while
445 // running the factory method, use the exception as the result.
446 bool caught_exception;
447 Handle<Object> result = Execution::TryCall(fun,
448 Top::builtins(),
449 2,
450 argv,
451 &caught_exception);
452 return result;
453}
454
455
456Handle<Object> Factory::NewError(Handle<String> message) {
457 return NewError("$Error", message);
458}
459
460
461Handle<Object> Factory::NewError(const char* constructor,
462 Handle<String> message) {
463 Handle<String> constr = Factory::LookupAsciiSymbol(constructor);
464 Handle<JSFunction> fun =
465 Handle<JSFunction>(
466 JSFunction::cast(
467 Top::builtins()->GetProperty(*constr)));
468 Object** argv[1] = { Handle<Object>::cast(message).location() };
469
470 // Invoke the JavaScript factory method. If an exception is thrown while
471 // running the factory method, use the exception as the result.
472 bool caught_exception;
473 Handle<Object> result = Execution::TryCall(fun,
474 Top::builtins(),
475 1,
476 argv,
477 &caught_exception);
478 return result;
479}
480
481
482Handle<JSFunction> Factory::NewFunction(Handle<String> name,
483 InstanceType type,
484 int instance_size,
485 Handle<Code> code,
486 bool force_initial_map) {
487 // Allocate the function
488 Handle<JSFunction> function = NewFunction(name, the_hole_value());
489 function->set_code(*code);
490
491 if (force_initial_map ||
492 type != JS_OBJECT_TYPE ||
493 instance_size != JSObject::kHeaderSize) {
494 Handle<Map> initial_map = NewMap(type, instance_size);
495 Handle<JSObject> prototype = NewFunctionPrototype(function);
496 initial_map->set_prototype(*prototype);
497 function->set_initial_map(*initial_map);
498 initial_map->set_constructor(*function);
499 } else {
500 ASSERT(!function->has_initial_map());
501 ASSERT(!function->has_prototype());
502 }
503
504 return function;
505}
506
507
Steve Blocka7e24c12009-10-30 11:49:00 +0000508Handle<JSFunction> Factory::NewFunctionWithPrototype(Handle<String> name,
509 InstanceType type,
510 int instance_size,
511 Handle<JSObject> prototype,
512 Handle<Code> code,
513 bool force_initial_map) {
514 // Allocate the function
515 Handle<JSFunction> function = NewFunction(name, prototype);
516
517 function->set_code(*code);
518
519 if (force_initial_map ||
520 type != JS_OBJECT_TYPE ||
521 instance_size != JSObject::kHeaderSize) {
522 Handle<Map> initial_map = NewMap(type, instance_size);
523 function->set_initial_map(*initial_map);
524 initial_map->set_constructor(*function);
525 }
526
527 // Set function.prototype and give the prototype a constructor
528 // property that refers to the function.
529 SetPrototypeProperty(function, prototype);
530 SetProperty(prototype, Factory::constructor_symbol(), function, DONT_ENUM);
531 return function;
532}
533
534
Steve Block6ded16b2010-05-10 14:33:55 +0100535Handle<JSFunction> Factory::NewFunctionWithoutPrototype(Handle<String> name,
536 Handle<Code> code) {
537 Handle<JSFunction> function = NewFunctionWithoutPrototype(name);
538 function->set_code(*code);
539 ASSERT(!function->has_initial_map());
540 ASSERT(!function->has_prototype());
541 return function;
542}
543
544
Steve Blocka7e24c12009-10-30 11:49:00 +0000545Handle<Code> Factory::NewCode(const CodeDesc& desc,
Steve Blocka7e24c12009-10-30 11:49:00 +0000546 Code::Flags flags,
547 Handle<Object> self_ref) {
Ben Murdoch3bec4d22010-07-22 14:51:16 +0100548 CALL_HEAP_FUNCTION(Heap::CreateCode(desc, flags, self_ref), Code);
Steve Blocka7e24c12009-10-30 11:49:00 +0000549}
550
551
552Handle<Code> Factory::CopyCode(Handle<Code> code) {
553 CALL_HEAP_FUNCTION(Heap::CopyCode(*code), Code);
554}
555
556
Steve Block6ded16b2010-05-10 14:33:55 +0100557Handle<Code> Factory::CopyCode(Handle<Code> code, Vector<byte> reloc_info) {
558 CALL_HEAP_FUNCTION(Heap::CopyCode(*code, reloc_info), Code);
559}
560
561
Steve Blocka7e24c12009-10-30 11:49:00 +0000562static inline Object* DoCopyInsert(DescriptorArray* array,
563 String* key,
564 Object* value,
565 PropertyAttributes attributes) {
566 CallbacksDescriptor desc(key, value, attributes);
567 Object* obj = array->CopyInsert(&desc, REMOVE_TRANSITIONS);
568 return obj;
569}
570
571
572// Allocate the new array.
573Handle<DescriptorArray> Factory::CopyAppendProxyDescriptor(
574 Handle<DescriptorArray> array,
575 Handle<String> key,
576 Handle<Object> value,
577 PropertyAttributes attributes) {
578 CALL_HEAP_FUNCTION(DoCopyInsert(*array, *key, *value, attributes),
579 DescriptorArray);
580}
581
582
583Handle<String> Factory::SymbolFromString(Handle<String> value) {
584 CALL_HEAP_FUNCTION(Heap::LookupSymbol(*value), String);
585}
586
587
588Handle<DescriptorArray> Factory::CopyAppendCallbackDescriptors(
589 Handle<DescriptorArray> array,
590 Handle<Object> descriptors) {
591 v8::NeanderArray callbacks(descriptors);
592 int nof_callbacks = callbacks.length();
593 Handle<DescriptorArray> result =
594 NewDescriptorArray(array->number_of_descriptors() + nof_callbacks);
595
596 // Number of descriptors added to the result so far.
597 int descriptor_count = 0;
598
599 // Copy the descriptors from the array.
600 for (int i = 0; i < array->number_of_descriptors(); i++) {
601 if (array->GetType(i) != NULL_DESCRIPTOR) {
602 result->CopyFrom(descriptor_count++, *array, i);
603 }
604 }
605
606 // Number of duplicates detected.
607 int duplicates = 0;
608
609 // Fill in new callback descriptors. Process the callbacks from
610 // back to front so that the last callback with a given name takes
611 // precedence over previously added callbacks with that name.
612 for (int i = nof_callbacks - 1; i >= 0; i--) {
613 Handle<AccessorInfo> entry =
614 Handle<AccessorInfo>(AccessorInfo::cast(callbacks.get(i)));
615 // Ensure the key is a symbol before writing into the instance descriptor.
616 Handle<String> key =
617 SymbolFromString(Handle<String>(String::cast(entry->name())));
618 // Check if a descriptor with this name already exists before writing.
619 if (result->LinearSearch(*key, descriptor_count) ==
620 DescriptorArray::kNotFound) {
621 CallbacksDescriptor desc(*key, *entry, entry->property_attributes());
622 result->Set(descriptor_count, &desc);
623 descriptor_count++;
624 } else {
625 duplicates++;
626 }
627 }
628
629 // If duplicates were detected, allocate a result of the right size
630 // and transfer the elements.
631 if (duplicates > 0) {
632 int number_of_descriptors = result->number_of_descriptors() - duplicates;
633 Handle<DescriptorArray> new_result =
634 NewDescriptorArray(number_of_descriptors);
635 for (int i = 0; i < number_of_descriptors; i++) {
636 new_result->CopyFrom(i, *result, i);
637 }
638 result = new_result;
639 }
640
641 // Sort the result before returning.
642 result->Sort();
643 return result;
644}
645
646
647Handle<JSObject> Factory::NewJSObject(Handle<JSFunction> constructor,
648 PretenureFlag pretenure) {
649 CALL_HEAP_FUNCTION(Heap::AllocateJSObject(*constructor, pretenure), JSObject);
650}
651
652
653Handle<GlobalObject> Factory::NewGlobalObject(
654 Handle<JSFunction> constructor) {
655 CALL_HEAP_FUNCTION(Heap::AllocateGlobalObject(*constructor),
656 GlobalObject);
657}
658
659
660
661Handle<JSObject> Factory::NewJSObjectFromMap(Handle<Map> map) {
662 CALL_HEAP_FUNCTION(Heap::AllocateJSObjectFromMap(*map, NOT_TENURED),
663 JSObject);
664}
665
666
667Handle<JSArray> Factory::NewJSArray(int length,
668 PretenureFlag pretenure) {
669 Handle<JSObject> obj = NewJSObject(Top::array_function(), pretenure);
670 CALL_HEAP_FUNCTION(Handle<JSArray>::cast(obj)->Initialize(length), JSArray);
671}
672
673
674Handle<JSArray> Factory::NewJSArrayWithElements(Handle<FixedArray> elements,
675 PretenureFlag pretenure) {
676 Handle<JSArray> result =
677 Handle<JSArray>::cast(NewJSObject(Top::array_function(), pretenure));
678 result->SetContent(*elements);
679 return result;
680}
681
682
Steve Block6ded16b2010-05-10 14:33:55 +0100683Handle<SharedFunctionInfo> Factory::NewSharedFunctionInfo(
Ben Murdoch3bec4d22010-07-22 14:51:16 +0100684 Handle<String> name,
685 int number_of_literals,
686 Handle<Code> code,
687 Handle<SerializedScopeInfo> scope_info) {
Steve Block6ded16b2010-05-10 14:33:55 +0100688 Handle<SharedFunctionInfo> shared = NewSharedFunctionInfo(name);
689 shared->set_code(*code);
Ben Murdoch3bec4d22010-07-22 14:51:16 +0100690 shared->set_scope_info(*scope_info);
Steve Block6ded16b2010-05-10 14:33:55 +0100691 int literals_array_size = number_of_literals;
692 // If the function contains object, regexp or array literals,
693 // allocate extra space for a literals array prefix containing the
694 // context.
695 if (number_of_literals > 0) {
696 literals_array_size += JSFunction::kLiteralsPrefixSize;
697 }
698 shared->set_num_literals(literals_array_size);
699 return shared;
700}
701
702
Steve Blocka7e24c12009-10-30 11:49:00 +0000703Handle<SharedFunctionInfo> Factory::NewSharedFunctionInfo(Handle<String> name) {
704 CALL_HEAP_FUNCTION(Heap::AllocateSharedFunctionInfo(*name),
705 SharedFunctionInfo);
706}
707
708
709Handle<String> Factory::NumberToString(Handle<Object> number) {
710 CALL_HEAP_FUNCTION(Heap::NumberToString(*number), String);
711}
712
713
714Handle<NumberDictionary> Factory::DictionaryAtNumberPut(
715 Handle<NumberDictionary> dictionary,
716 uint32_t key,
717 Handle<Object> value) {
718 CALL_HEAP_FUNCTION(dictionary->AtNumberPut(key, *value), NumberDictionary);
719}
720
721
722Handle<JSFunction> Factory::NewFunctionHelper(Handle<String> name,
723 Handle<Object> prototype) {
724 Handle<SharedFunctionInfo> function_share = NewSharedFunctionInfo(name);
725 CALL_HEAP_FUNCTION(Heap::AllocateFunction(*Top::function_map(),
726 *function_share,
727 *prototype),
728 JSFunction);
729}
730
731
732Handle<JSFunction> Factory::NewFunction(Handle<String> name,
733 Handle<Object> prototype) {
734 Handle<JSFunction> fun = NewFunctionHelper(name, prototype);
735 fun->set_context(Top::context()->global_context());
736 return fun;
737}
738
739
Steve Block6ded16b2010-05-10 14:33:55 +0100740Handle<JSFunction> Factory::NewFunctionWithoutPrototypeHelper(
741 Handle<String> name) {
742 Handle<SharedFunctionInfo> function_share = NewSharedFunctionInfo(name);
743 CALL_HEAP_FUNCTION(Heap::AllocateFunction(
744 *Top::function_without_prototype_map(),
745 *function_share,
746 *the_hole_value()),
747 JSFunction);
748}
749
750
751Handle<JSFunction> Factory::NewFunctionWithoutPrototype(Handle<String> name) {
752 Handle<JSFunction> fun = NewFunctionWithoutPrototypeHelper(name);
753 fun->set_context(Top::context()->global_context());
754 return fun;
755}
756
757
Leon Clarkee46be812010-01-19 14:06:41 +0000758Handle<Object> Factory::ToObject(Handle<Object> object) {
759 CALL_HEAP_FUNCTION(object->ToObject(), Object);
760}
761
762
Steve Blocka7e24c12009-10-30 11:49:00 +0000763Handle<Object> Factory::ToObject(Handle<Object> object,
764 Handle<Context> global_context) {
765 CALL_HEAP_FUNCTION(object->ToObject(*global_context), Object);
766}
767
768
769#ifdef ENABLE_DEBUGGER_SUPPORT
770Handle<DebugInfo> Factory::NewDebugInfo(Handle<SharedFunctionInfo> shared) {
771 // Get the original code of the function.
772 Handle<Code> code(shared->code());
773
774 // Create a copy of the code before allocating the debug info object to avoid
775 // allocation while setting up the debug info object.
776 Handle<Code> original_code(*Factory::CopyCode(code));
777
778 // Allocate initial fixed array for active break points before allocating the
779 // debug info object to avoid allocation while setting up the debug info
780 // object.
781 Handle<FixedArray> break_points(
782 Factory::NewFixedArray(Debug::kEstimatedNofBreakPointsInFunction));
783
784 // Create and set up the debug info object. Debug info contains function, a
785 // copy of the original code, the executing code and initial fixed array for
786 // active break points.
787 Handle<DebugInfo> debug_info =
788 Handle<DebugInfo>::cast(Factory::NewStruct(DEBUG_INFO_TYPE));
789 debug_info->set_shared(*shared);
790 debug_info->set_original_code(*original_code);
791 debug_info->set_code(*code);
792 debug_info->set_break_points(*break_points);
793
794 // Link debug info to function.
795 shared->set_debug_info(*debug_info);
796
797 return debug_info;
798}
799#endif
800
801
802Handle<JSObject> Factory::NewArgumentsObject(Handle<Object> callee,
803 int length) {
804 CALL_HEAP_FUNCTION(Heap::AllocateArgumentsObject(*callee, length), JSObject);
805}
806
807
808Handle<JSFunction> Factory::CreateApiFunction(
809 Handle<FunctionTemplateInfo> obj, ApiInstanceType instance_type) {
810 Handle<Code> code = Handle<Code>(Builtins::builtin(Builtins::HandleApiCall));
Leon Clarkee46be812010-01-19 14:06:41 +0000811 Handle<Code> construct_stub =
812 Handle<Code>(Builtins::builtin(Builtins::JSConstructStubApi));
Steve Blocka7e24c12009-10-30 11:49:00 +0000813
814 int internal_field_count = 0;
815 if (!obj->instance_template()->IsUndefined()) {
816 Handle<ObjectTemplateInfo> instance_template =
817 Handle<ObjectTemplateInfo>(
818 ObjectTemplateInfo::cast(obj->instance_template()));
819 internal_field_count =
820 Smi::cast(instance_template->internal_field_count())->value();
821 }
822
823 int instance_size = kPointerSize * internal_field_count;
824 InstanceType type = INVALID_TYPE;
825 switch (instance_type) {
826 case JavaScriptObject:
827 type = JS_OBJECT_TYPE;
828 instance_size += JSObject::kHeaderSize;
829 break;
830 case InnerGlobalObject:
831 type = JS_GLOBAL_OBJECT_TYPE;
832 instance_size += JSGlobalObject::kSize;
833 break;
834 case OuterGlobalObject:
835 type = JS_GLOBAL_PROXY_TYPE;
836 instance_size += JSGlobalProxy::kSize;
837 break;
838 default:
839 break;
840 }
841 ASSERT(type != INVALID_TYPE);
842
843 Handle<JSFunction> result =
844 Factory::NewFunction(Factory::empty_symbol(),
845 type,
846 instance_size,
847 code,
848 true);
849 // Set class name.
850 Handle<Object> class_name = Handle<Object>(obj->class_name());
851 if (class_name->IsString()) {
852 result->shared()->set_instance_class_name(*class_name);
853 result->shared()->set_name(*class_name);
854 }
855
856 Handle<Map> map = Handle<Map>(result->initial_map());
857
858 // Mark as undetectable if needed.
859 if (obj->undetectable()) {
860 map->set_is_undetectable();
861 }
862
863 // Mark as hidden for the __proto__ accessor if needed.
864 if (obj->hidden_prototype()) {
865 map->set_is_hidden_prototype();
866 }
867
868 // Mark as needs_access_check if needed.
869 if (obj->needs_access_check()) {
870 map->set_is_access_check_needed(true);
871 }
872
873 // Set interceptor information in the map.
874 if (!obj->named_property_handler()->IsUndefined()) {
875 map->set_has_named_interceptor();
876 }
877 if (!obj->indexed_property_handler()->IsUndefined()) {
878 map->set_has_indexed_interceptor();
879 }
880
881 // Set instance call-as-function information in the map.
882 if (!obj->instance_call_handler()->IsUndefined()) {
883 map->set_has_instance_call_handler();
884 }
885
886 result->shared()->set_function_data(*obj);
Leon Clarkee46be812010-01-19 14:06:41 +0000887 result->shared()->set_construct_stub(*construct_stub);
Steve Blocka7e24c12009-10-30 11:49:00 +0000888 result->shared()->DontAdaptArguments();
889
890 // Recursively copy parent templates' accessors, 'data' may be modified.
891 Handle<DescriptorArray> array =
892 Handle<DescriptorArray>(map->instance_descriptors());
893 while (true) {
894 Handle<Object> props = Handle<Object>(obj->property_accessors());
895 if (!props->IsUndefined()) {
896 array = Factory::CopyAppendCallbackDescriptors(array, props);
897 }
898 Handle<Object> parent = Handle<Object>(obj->parent_template());
899 if (parent->IsUndefined()) break;
900 obj = Handle<FunctionTemplateInfo>::cast(parent);
901 }
902 if (!array->IsEmpty()) {
903 map->set_instance_descriptors(*array);
904 }
905
Steve Block6ded16b2010-05-10 14:33:55 +0100906 ASSERT(result->shared()->IsApiFunction());
Steve Blocka7e24c12009-10-30 11:49:00 +0000907 return result;
908}
909
910
911Handle<MapCache> Factory::NewMapCache(int at_least_space_for) {
912 CALL_HEAP_FUNCTION(MapCache::Allocate(at_least_space_for), MapCache);
913}
914
915
916static Object* UpdateMapCacheWith(Context* context,
917 FixedArray* keys,
918 Map* map) {
919 Object* result = MapCache::cast(context->map_cache())->Put(keys, map);
920 if (!result->IsFailure()) context->set_map_cache(MapCache::cast(result));
921 return result;
922}
923
924
925Handle<MapCache> Factory::AddToMapCache(Handle<Context> context,
926 Handle<FixedArray> keys,
927 Handle<Map> map) {
928 CALL_HEAP_FUNCTION(UpdateMapCacheWith(*context, *keys, *map), MapCache);
929}
930
931
932Handle<Map> Factory::ObjectLiteralMapFromCache(Handle<Context> context,
933 Handle<FixedArray> keys) {
934 if (context->map_cache()->IsUndefined()) {
935 // Allocate the new map cache for the global context.
936 Handle<MapCache> new_cache = NewMapCache(24);
937 context->set_map_cache(*new_cache);
938 }
939 // Check to see whether there is a matching element in the cache.
940 Handle<MapCache> cache =
941 Handle<MapCache>(MapCache::cast(context->map_cache()));
942 Handle<Object> result = Handle<Object>(cache->Lookup(*keys));
943 if (result->IsMap()) return Handle<Map>::cast(result);
944 // Create a new map and add it to the cache.
945 Handle<Map> map =
946 CopyMap(Handle<Map>(context->object_function()->initial_map()),
947 keys->length());
948 AddToMapCache(context, keys, map);
949 return Handle<Map>(map);
950}
951
952
953void Factory::SetRegExpAtomData(Handle<JSRegExp> regexp,
954 JSRegExp::Type type,
955 Handle<String> source,
956 JSRegExp::Flags flags,
957 Handle<Object> data) {
958 Handle<FixedArray> store = NewFixedArray(JSRegExp::kAtomDataSize);
959
960 store->set(JSRegExp::kTagIndex, Smi::FromInt(type));
961 store->set(JSRegExp::kSourceIndex, *source);
962 store->set(JSRegExp::kFlagsIndex, Smi::FromInt(flags.value()));
963 store->set(JSRegExp::kAtomPatternIndex, *data);
964 regexp->set_data(*store);
965}
966
967void Factory::SetRegExpIrregexpData(Handle<JSRegExp> regexp,
968 JSRegExp::Type type,
969 Handle<String> source,
970 JSRegExp::Flags flags,
971 int capture_count) {
972 Handle<FixedArray> store = NewFixedArray(JSRegExp::kIrregexpDataSize);
973
974 store->set(JSRegExp::kTagIndex, Smi::FromInt(type));
975 store->set(JSRegExp::kSourceIndex, *source);
976 store->set(JSRegExp::kFlagsIndex, Smi::FromInt(flags.value()));
977 store->set(JSRegExp::kIrregexpASCIICodeIndex, Heap::the_hole_value());
978 store->set(JSRegExp::kIrregexpUC16CodeIndex, Heap::the_hole_value());
979 store->set(JSRegExp::kIrregexpMaxRegisterCountIndex, Smi::FromInt(0));
980 store->set(JSRegExp::kIrregexpCaptureCountIndex,
981 Smi::FromInt(capture_count));
982 regexp->set_data(*store);
983}
984
985
986
987void Factory::ConfigureInstance(Handle<FunctionTemplateInfo> desc,
988 Handle<JSObject> instance,
989 bool* pending_exception) {
990 // Configure the instance by adding the properties specified by the
991 // instance template.
992 Handle<Object> instance_template = Handle<Object>(desc->instance_template());
993 if (!instance_template->IsUndefined()) {
994 Execution::ConfigureInstance(instance,
995 instance_template,
996 pending_exception);
997 } else {
998 *pending_exception = false;
999 }
1000}
1001
1002
1003} } // namespace v8::internal