blob: 39e881ac3d567b5fbdc3d354325cc62b4ea994c5 [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);
280 return copy;
281}
282
Steve Block8defd9f2010-07-08 12:39:36 +0100283
Steve Blocka7e24c12009-10-30 11:49:00 +0000284Handle<Map> Factory::CopyMapDropTransitions(Handle<Map> src) {
285 CALL_HEAP_FUNCTION(src->CopyDropTransitions(), Map);
286}
287
288
Steve Block8defd9f2010-07-08 12:39:36 +0100289Handle<Map> Factory::GetFastElementsMap(Handle<Map> src) {
290 CALL_HEAP_FUNCTION(src->GetFastElementsMap(), Map);
291}
292
293
294Handle<Map> Factory::GetSlowElementsMap(Handle<Map> src) {
295 CALL_HEAP_FUNCTION(src->GetSlowElementsMap(), Map);
296}
297
298
Steve Blocka7e24c12009-10-30 11:49:00 +0000299Handle<FixedArray> Factory::CopyFixedArray(Handle<FixedArray> array) {
300 CALL_HEAP_FUNCTION(array->Copy(), FixedArray);
301}
302
303
Steve Block6ded16b2010-05-10 14:33:55 +0100304Handle<JSFunction> Factory::BaseNewFunctionFromSharedFunctionInfo(
305 Handle<SharedFunctionInfo> function_info,
Leon Clarkee46be812010-01-19 14:06:41 +0000306 Handle<Map> function_map,
307 PretenureFlag pretenure) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000308 CALL_HEAP_FUNCTION(Heap::AllocateFunction(*function_map,
Steve Block6ded16b2010-05-10 14:33:55 +0100309 *function_info,
Leon Clarkee46be812010-01-19 14:06:41 +0000310 Heap::the_hole_value(),
311 pretenure),
Steve Blocka7e24c12009-10-30 11:49:00 +0000312 JSFunction);
313}
314
315
Steve Block6ded16b2010-05-10 14:33:55 +0100316Handle<JSFunction> Factory::NewFunctionFromSharedFunctionInfo(
317 Handle<SharedFunctionInfo> function_info,
Leon Clarkee46be812010-01-19 14:06:41 +0000318 Handle<Context> context,
319 PretenureFlag pretenure) {
Steve Block6ded16b2010-05-10 14:33:55 +0100320 Handle<JSFunction> result = BaseNewFunctionFromSharedFunctionInfo(
321 function_info, Top::function_map(), pretenure);
Steve Blocka7e24c12009-10-30 11:49:00 +0000322 result->set_context(*context);
Steve Block6ded16b2010-05-10 14:33:55 +0100323 int number_of_literals = function_info->num_literals();
Steve Blocka7e24c12009-10-30 11:49:00 +0000324 Handle<FixedArray> literals =
Leon Clarkee46be812010-01-19 14:06:41 +0000325 Factory::NewFixedArray(number_of_literals, pretenure);
Steve Blocka7e24c12009-10-30 11:49:00 +0000326 if (number_of_literals > 0) {
327 // Store the object, regexp and array functions in the literals
328 // array prefix. These functions will be used when creating
329 // object, regexp and array literals in this function.
330 literals->set(JSFunction::kLiteralGlobalContextIndex,
331 context->global_context());
332 }
333 result->set_literals(*literals);
Steve Blocka7e24c12009-10-30 11:49:00 +0000334 return result;
335}
336
337
338Handle<Object> Factory::NewNumber(double value,
339 PretenureFlag pretenure) {
340 CALL_HEAP_FUNCTION(Heap::NumberFromDouble(value, pretenure), Object);
341}
342
343
344Handle<Object> Factory::NewNumberFromInt(int value) {
345 CALL_HEAP_FUNCTION(Heap::NumberFromInt32(value), Object);
346}
347
348
349Handle<Object> Factory::NewNumberFromUint(uint32_t value) {
350 CALL_HEAP_FUNCTION(Heap::NumberFromUint32(value), Object);
351}
352
353
354Handle<JSObject> Factory::NewNeanderObject() {
355 CALL_HEAP_FUNCTION(Heap::AllocateJSObjectFromMap(Heap::neander_map()),
356 JSObject);
357}
358
359
360Handle<Object> Factory::NewTypeError(const char* type,
361 Vector< Handle<Object> > args) {
362 return NewError("MakeTypeError", type, args);
363}
364
365
366Handle<Object> Factory::NewTypeError(Handle<String> message) {
367 return NewError("$TypeError", message);
368}
369
370
371Handle<Object> Factory::NewRangeError(const char* type,
372 Vector< Handle<Object> > args) {
373 return NewError("MakeRangeError", type, args);
374}
375
376
377Handle<Object> Factory::NewRangeError(Handle<String> message) {
378 return NewError("$RangeError", message);
379}
380
381
382Handle<Object> Factory::NewSyntaxError(const char* type, Handle<JSArray> args) {
383 return NewError("MakeSyntaxError", type, args);
384}
385
386
387Handle<Object> Factory::NewSyntaxError(Handle<String> message) {
388 return NewError("$SyntaxError", message);
389}
390
391
392Handle<Object> Factory::NewReferenceError(const char* type,
393 Vector< Handle<Object> > args) {
394 return NewError("MakeReferenceError", type, args);
395}
396
397
398Handle<Object> Factory::NewReferenceError(Handle<String> message) {
399 return NewError("$ReferenceError", message);
400}
401
402
403Handle<Object> Factory::NewError(const char* maker, const char* type,
404 Vector< Handle<Object> > args) {
405 v8::HandleScope scope; // Instantiate a closeable HandleScope for EscapeFrom.
406 Handle<FixedArray> array = Factory::NewFixedArray(args.length());
407 for (int i = 0; i < args.length(); i++) {
408 array->set(i, *args[i]);
409 }
410 Handle<JSArray> object = Factory::NewJSArrayWithElements(array);
411 Handle<Object> result = NewError(maker, type, object);
412 return result.EscapeFrom(&scope);
413}
414
415
416Handle<Object> Factory::NewEvalError(const char* type,
417 Vector< Handle<Object> > args) {
418 return NewError("MakeEvalError", type, args);
419}
420
421
422Handle<Object> Factory::NewError(const char* type,
423 Vector< Handle<Object> > args) {
424 return NewError("MakeError", type, args);
425}
426
427
428Handle<Object> Factory::NewError(const char* maker,
429 const char* type,
430 Handle<JSArray> args) {
431 Handle<String> make_str = Factory::LookupAsciiSymbol(maker);
432 Handle<Object> fun_obj(Top::builtins()->GetProperty(*make_str));
433 // If the builtins haven't been properly configured yet this error
434 // constructor may not have been defined. Bail out.
435 if (!fun_obj->IsJSFunction())
436 return Factory::undefined_value();
437 Handle<JSFunction> fun = Handle<JSFunction>::cast(fun_obj);
438 Handle<Object> type_obj = Factory::LookupAsciiSymbol(type);
439 Object** argv[2] = { type_obj.location(),
440 Handle<Object>::cast(args).location() };
441
442 // Invoke the JavaScript factory method. If an exception is thrown while
443 // running the factory method, use the exception as the result.
444 bool caught_exception;
445 Handle<Object> result = Execution::TryCall(fun,
446 Top::builtins(),
447 2,
448 argv,
449 &caught_exception);
450 return result;
451}
452
453
454Handle<Object> Factory::NewError(Handle<String> message) {
455 return NewError("$Error", message);
456}
457
458
459Handle<Object> Factory::NewError(const char* constructor,
460 Handle<String> message) {
461 Handle<String> constr = Factory::LookupAsciiSymbol(constructor);
462 Handle<JSFunction> fun =
463 Handle<JSFunction>(
464 JSFunction::cast(
465 Top::builtins()->GetProperty(*constr)));
466 Object** argv[1] = { Handle<Object>::cast(message).location() };
467
468 // Invoke the JavaScript factory method. If an exception is thrown while
469 // running the factory method, use the exception as the result.
470 bool caught_exception;
471 Handle<Object> result = Execution::TryCall(fun,
472 Top::builtins(),
473 1,
474 argv,
475 &caught_exception);
476 return result;
477}
478
479
480Handle<JSFunction> Factory::NewFunction(Handle<String> name,
481 InstanceType type,
482 int instance_size,
483 Handle<Code> code,
484 bool force_initial_map) {
485 // Allocate the function
486 Handle<JSFunction> function = NewFunction(name, the_hole_value());
487 function->set_code(*code);
488
489 if (force_initial_map ||
490 type != JS_OBJECT_TYPE ||
491 instance_size != JSObject::kHeaderSize) {
492 Handle<Map> initial_map = NewMap(type, instance_size);
493 Handle<JSObject> prototype = NewFunctionPrototype(function);
494 initial_map->set_prototype(*prototype);
495 function->set_initial_map(*initial_map);
496 initial_map->set_constructor(*function);
497 } else {
498 ASSERT(!function->has_initial_map());
499 ASSERT(!function->has_prototype());
500 }
501
502 return function;
503}
504
505
Steve Blocka7e24c12009-10-30 11:49:00 +0000506Handle<JSFunction> Factory::NewFunctionWithPrototype(Handle<String> name,
507 InstanceType type,
508 int instance_size,
509 Handle<JSObject> prototype,
510 Handle<Code> code,
511 bool force_initial_map) {
512 // Allocate the function
513 Handle<JSFunction> function = NewFunction(name, prototype);
514
515 function->set_code(*code);
516
517 if (force_initial_map ||
518 type != JS_OBJECT_TYPE ||
519 instance_size != JSObject::kHeaderSize) {
520 Handle<Map> initial_map = NewMap(type, instance_size);
521 function->set_initial_map(*initial_map);
522 initial_map->set_constructor(*function);
523 }
524
525 // Set function.prototype and give the prototype a constructor
526 // property that refers to the function.
527 SetPrototypeProperty(function, prototype);
528 SetProperty(prototype, Factory::constructor_symbol(), function, DONT_ENUM);
529 return function;
530}
531
532
Steve Block6ded16b2010-05-10 14:33:55 +0100533Handle<JSFunction> Factory::NewFunctionWithoutPrototype(Handle<String> name,
534 Handle<Code> code) {
535 Handle<JSFunction> function = NewFunctionWithoutPrototype(name);
536 function->set_code(*code);
537 ASSERT(!function->has_initial_map());
538 ASSERT(!function->has_prototype());
539 return function;
540}
541
542
Steve Blocka7e24c12009-10-30 11:49:00 +0000543Handle<Code> Factory::NewCode(const CodeDesc& desc,
544 ZoneScopeInfo* sinfo,
545 Code::Flags flags,
546 Handle<Object> self_ref) {
547 CALL_HEAP_FUNCTION(Heap::CreateCode(desc, sinfo, flags, self_ref), Code);
548}
549
550
551Handle<Code> Factory::CopyCode(Handle<Code> code) {
552 CALL_HEAP_FUNCTION(Heap::CopyCode(*code), Code);
553}
554
555
Steve Block6ded16b2010-05-10 14:33:55 +0100556Handle<Code> Factory::CopyCode(Handle<Code> code, Vector<byte> reloc_info) {
557 CALL_HEAP_FUNCTION(Heap::CopyCode(*code, reloc_info), Code);
558}
559
560
Steve Blocka7e24c12009-10-30 11:49:00 +0000561static inline Object* DoCopyInsert(DescriptorArray* array,
562 String* key,
563 Object* value,
564 PropertyAttributes attributes) {
565 CallbacksDescriptor desc(key, value, attributes);
566 Object* obj = array->CopyInsert(&desc, REMOVE_TRANSITIONS);
567 return obj;
568}
569
570
571// Allocate the new array.
572Handle<DescriptorArray> Factory::CopyAppendProxyDescriptor(
573 Handle<DescriptorArray> array,
574 Handle<String> key,
575 Handle<Object> value,
576 PropertyAttributes attributes) {
577 CALL_HEAP_FUNCTION(DoCopyInsert(*array, *key, *value, attributes),
578 DescriptorArray);
579}
580
581
582Handle<String> Factory::SymbolFromString(Handle<String> value) {
583 CALL_HEAP_FUNCTION(Heap::LookupSymbol(*value), String);
584}
585
586
587Handle<DescriptorArray> Factory::CopyAppendCallbackDescriptors(
588 Handle<DescriptorArray> array,
589 Handle<Object> descriptors) {
590 v8::NeanderArray callbacks(descriptors);
591 int nof_callbacks = callbacks.length();
592 Handle<DescriptorArray> result =
593 NewDescriptorArray(array->number_of_descriptors() + nof_callbacks);
594
595 // Number of descriptors added to the result so far.
596 int descriptor_count = 0;
597
598 // Copy the descriptors from the array.
599 for (int i = 0; i < array->number_of_descriptors(); i++) {
600 if (array->GetType(i) != NULL_DESCRIPTOR) {
601 result->CopyFrom(descriptor_count++, *array, i);
602 }
603 }
604
605 // Number of duplicates detected.
606 int duplicates = 0;
607
608 // Fill in new callback descriptors. Process the callbacks from
609 // back to front so that the last callback with a given name takes
610 // precedence over previously added callbacks with that name.
611 for (int i = nof_callbacks - 1; i >= 0; i--) {
612 Handle<AccessorInfo> entry =
613 Handle<AccessorInfo>(AccessorInfo::cast(callbacks.get(i)));
614 // Ensure the key is a symbol before writing into the instance descriptor.
615 Handle<String> key =
616 SymbolFromString(Handle<String>(String::cast(entry->name())));
617 // Check if a descriptor with this name already exists before writing.
618 if (result->LinearSearch(*key, descriptor_count) ==
619 DescriptorArray::kNotFound) {
620 CallbacksDescriptor desc(*key, *entry, entry->property_attributes());
621 result->Set(descriptor_count, &desc);
622 descriptor_count++;
623 } else {
624 duplicates++;
625 }
626 }
627
628 // If duplicates were detected, allocate a result of the right size
629 // and transfer the elements.
630 if (duplicates > 0) {
631 int number_of_descriptors = result->number_of_descriptors() - duplicates;
632 Handle<DescriptorArray> new_result =
633 NewDescriptorArray(number_of_descriptors);
634 for (int i = 0; i < number_of_descriptors; i++) {
635 new_result->CopyFrom(i, *result, i);
636 }
637 result = new_result;
638 }
639
640 // Sort the result before returning.
641 result->Sort();
642 return result;
643}
644
645
646Handle<JSObject> Factory::NewJSObject(Handle<JSFunction> constructor,
647 PretenureFlag pretenure) {
648 CALL_HEAP_FUNCTION(Heap::AllocateJSObject(*constructor, pretenure), JSObject);
649}
650
651
652Handle<GlobalObject> Factory::NewGlobalObject(
653 Handle<JSFunction> constructor) {
654 CALL_HEAP_FUNCTION(Heap::AllocateGlobalObject(*constructor),
655 GlobalObject);
656}
657
658
659
660Handle<JSObject> Factory::NewJSObjectFromMap(Handle<Map> map) {
661 CALL_HEAP_FUNCTION(Heap::AllocateJSObjectFromMap(*map, NOT_TENURED),
662 JSObject);
663}
664
665
666Handle<JSArray> Factory::NewJSArray(int length,
667 PretenureFlag pretenure) {
668 Handle<JSObject> obj = NewJSObject(Top::array_function(), pretenure);
669 CALL_HEAP_FUNCTION(Handle<JSArray>::cast(obj)->Initialize(length), JSArray);
670}
671
672
673Handle<JSArray> Factory::NewJSArrayWithElements(Handle<FixedArray> elements,
674 PretenureFlag pretenure) {
675 Handle<JSArray> result =
676 Handle<JSArray>::cast(NewJSObject(Top::array_function(), pretenure));
677 result->SetContent(*elements);
678 return result;
679}
680
681
Steve Block6ded16b2010-05-10 14:33:55 +0100682Handle<SharedFunctionInfo> Factory::NewSharedFunctionInfo(
683 Handle<String> name, int number_of_literals, Handle<Code> code) {
684 Handle<SharedFunctionInfo> shared = NewSharedFunctionInfo(name);
685 shared->set_code(*code);
686 int literals_array_size = number_of_literals;
687 // If the function contains object, regexp or array literals,
688 // allocate extra space for a literals array prefix containing the
689 // context.
690 if (number_of_literals > 0) {
691 literals_array_size += JSFunction::kLiteralsPrefixSize;
692 }
693 shared->set_num_literals(literals_array_size);
694 return shared;
695}
696
697
Steve Blocka7e24c12009-10-30 11:49:00 +0000698Handle<SharedFunctionInfo> Factory::NewSharedFunctionInfo(Handle<String> name) {
699 CALL_HEAP_FUNCTION(Heap::AllocateSharedFunctionInfo(*name),
700 SharedFunctionInfo);
701}
702
703
704Handle<String> Factory::NumberToString(Handle<Object> number) {
705 CALL_HEAP_FUNCTION(Heap::NumberToString(*number), String);
706}
707
708
709Handle<NumberDictionary> Factory::DictionaryAtNumberPut(
710 Handle<NumberDictionary> dictionary,
711 uint32_t key,
712 Handle<Object> value) {
713 CALL_HEAP_FUNCTION(dictionary->AtNumberPut(key, *value), NumberDictionary);
714}
715
716
717Handle<JSFunction> Factory::NewFunctionHelper(Handle<String> name,
718 Handle<Object> prototype) {
719 Handle<SharedFunctionInfo> function_share = NewSharedFunctionInfo(name);
720 CALL_HEAP_FUNCTION(Heap::AllocateFunction(*Top::function_map(),
721 *function_share,
722 *prototype),
723 JSFunction);
724}
725
726
727Handle<JSFunction> Factory::NewFunction(Handle<String> name,
728 Handle<Object> prototype) {
729 Handle<JSFunction> fun = NewFunctionHelper(name, prototype);
730 fun->set_context(Top::context()->global_context());
731 return fun;
732}
733
734
Steve Block6ded16b2010-05-10 14:33:55 +0100735Handle<JSFunction> Factory::NewFunctionWithoutPrototypeHelper(
736 Handle<String> name) {
737 Handle<SharedFunctionInfo> function_share = NewSharedFunctionInfo(name);
738 CALL_HEAP_FUNCTION(Heap::AllocateFunction(
739 *Top::function_without_prototype_map(),
740 *function_share,
741 *the_hole_value()),
742 JSFunction);
743}
744
745
746Handle<JSFunction> Factory::NewFunctionWithoutPrototype(Handle<String> name) {
747 Handle<JSFunction> fun = NewFunctionWithoutPrototypeHelper(name);
748 fun->set_context(Top::context()->global_context());
749 return fun;
750}
751
752
Leon Clarkee46be812010-01-19 14:06:41 +0000753Handle<Object> Factory::ToObject(Handle<Object> object) {
754 CALL_HEAP_FUNCTION(object->ToObject(), Object);
755}
756
757
Steve Blocka7e24c12009-10-30 11:49:00 +0000758Handle<Object> Factory::ToObject(Handle<Object> object,
759 Handle<Context> global_context) {
760 CALL_HEAP_FUNCTION(object->ToObject(*global_context), Object);
761}
762
763
764#ifdef ENABLE_DEBUGGER_SUPPORT
765Handle<DebugInfo> Factory::NewDebugInfo(Handle<SharedFunctionInfo> shared) {
766 // Get the original code of the function.
767 Handle<Code> code(shared->code());
768
769 // Create a copy of the code before allocating the debug info object to avoid
770 // allocation while setting up the debug info object.
771 Handle<Code> original_code(*Factory::CopyCode(code));
772
773 // Allocate initial fixed array for active break points before allocating the
774 // debug info object to avoid allocation while setting up the debug info
775 // object.
776 Handle<FixedArray> break_points(
777 Factory::NewFixedArray(Debug::kEstimatedNofBreakPointsInFunction));
778
779 // Create and set up the debug info object. Debug info contains function, a
780 // copy of the original code, the executing code and initial fixed array for
781 // active break points.
782 Handle<DebugInfo> debug_info =
783 Handle<DebugInfo>::cast(Factory::NewStruct(DEBUG_INFO_TYPE));
784 debug_info->set_shared(*shared);
785 debug_info->set_original_code(*original_code);
786 debug_info->set_code(*code);
787 debug_info->set_break_points(*break_points);
788
789 // Link debug info to function.
790 shared->set_debug_info(*debug_info);
791
792 return debug_info;
793}
794#endif
795
796
797Handle<JSObject> Factory::NewArgumentsObject(Handle<Object> callee,
798 int length) {
799 CALL_HEAP_FUNCTION(Heap::AllocateArgumentsObject(*callee, length), JSObject);
800}
801
802
803Handle<JSFunction> Factory::CreateApiFunction(
804 Handle<FunctionTemplateInfo> obj, ApiInstanceType instance_type) {
805 Handle<Code> code = Handle<Code>(Builtins::builtin(Builtins::HandleApiCall));
Leon Clarkee46be812010-01-19 14:06:41 +0000806 Handle<Code> construct_stub =
807 Handle<Code>(Builtins::builtin(Builtins::JSConstructStubApi));
Steve Blocka7e24c12009-10-30 11:49:00 +0000808
809 int internal_field_count = 0;
810 if (!obj->instance_template()->IsUndefined()) {
811 Handle<ObjectTemplateInfo> instance_template =
812 Handle<ObjectTemplateInfo>(
813 ObjectTemplateInfo::cast(obj->instance_template()));
814 internal_field_count =
815 Smi::cast(instance_template->internal_field_count())->value();
816 }
817
818 int instance_size = kPointerSize * internal_field_count;
819 InstanceType type = INVALID_TYPE;
820 switch (instance_type) {
821 case JavaScriptObject:
822 type = JS_OBJECT_TYPE;
823 instance_size += JSObject::kHeaderSize;
824 break;
825 case InnerGlobalObject:
826 type = JS_GLOBAL_OBJECT_TYPE;
827 instance_size += JSGlobalObject::kSize;
828 break;
829 case OuterGlobalObject:
830 type = JS_GLOBAL_PROXY_TYPE;
831 instance_size += JSGlobalProxy::kSize;
832 break;
833 default:
834 break;
835 }
836 ASSERT(type != INVALID_TYPE);
837
838 Handle<JSFunction> result =
839 Factory::NewFunction(Factory::empty_symbol(),
840 type,
841 instance_size,
842 code,
843 true);
844 // Set class name.
845 Handle<Object> class_name = Handle<Object>(obj->class_name());
846 if (class_name->IsString()) {
847 result->shared()->set_instance_class_name(*class_name);
848 result->shared()->set_name(*class_name);
849 }
850
851 Handle<Map> map = Handle<Map>(result->initial_map());
852
853 // Mark as undetectable if needed.
854 if (obj->undetectable()) {
855 map->set_is_undetectable();
856 }
857
858 // Mark as hidden for the __proto__ accessor if needed.
859 if (obj->hidden_prototype()) {
860 map->set_is_hidden_prototype();
861 }
862
863 // Mark as needs_access_check if needed.
864 if (obj->needs_access_check()) {
865 map->set_is_access_check_needed(true);
866 }
867
868 // Set interceptor information in the map.
869 if (!obj->named_property_handler()->IsUndefined()) {
870 map->set_has_named_interceptor();
871 }
872 if (!obj->indexed_property_handler()->IsUndefined()) {
873 map->set_has_indexed_interceptor();
874 }
875
876 // Set instance call-as-function information in the map.
877 if (!obj->instance_call_handler()->IsUndefined()) {
878 map->set_has_instance_call_handler();
879 }
880
881 result->shared()->set_function_data(*obj);
Leon Clarkee46be812010-01-19 14:06:41 +0000882 result->shared()->set_construct_stub(*construct_stub);
Steve Blocka7e24c12009-10-30 11:49:00 +0000883 result->shared()->DontAdaptArguments();
884
885 // Recursively copy parent templates' accessors, 'data' may be modified.
886 Handle<DescriptorArray> array =
887 Handle<DescriptorArray>(map->instance_descriptors());
888 while (true) {
889 Handle<Object> props = Handle<Object>(obj->property_accessors());
890 if (!props->IsUndefined()) {
891 array = Factory::CopyAppendCallbackDescriptors(array, props);
892 }
893 Handle<Object> parent = Handle<Object>(obj->parent_template());
894 if (parent->IsUndefined()) break;
895 obj = Handle<FunctionTemplateInfo>::cast(parent);
896 }
897 if (!array->IsEmpty()) {
898 map->set_instance_descriptors(*array);
899 }
900
Steve Block6ded16b2010-05-10 14:33:55 +0100901 ASSERT(result->shared()->IsApiFunction());
Steve Blocka7e24c12009-10-30 11:49:00 +0000902 return result;
903}
904
905
906Handle<MapCache> Factory::NewMapCache(int at_least_space_for) {
907 CALL_HEAP_FUNCTION(MapCache::Allocate(at_least_space_for), MapCache);
908}
909
910
911static Object* UpdateMapCacheWith(Context* context,
912 FixedArray* keys,
913 Map* map) {
914 Object* result = MapCache::cast(context->map_cache())->Put(keys, map);
915 if (!result->IsFailure()) context->set_map_cache(MapCache::cast(result));
916 return result;
917}
918
919
920Handle<MapCache> Factory::AddToMapCache(Handle<Context> context,
921 Handle<FixedArray> keys,
922 Handle<Map> map) {
923 CALL_HEAP_FUNCTION(UpdateMapCacheWith(*context, *keys, *map), MapCache);
924}
925
926
927Handle<Map> Factory::ObjectLiteralMapFromCache(Handle<Context> context,
928 Handle<FixedArray> keys) {
929 if (context->map_cache()->IsUndefined()) {
930 // Allocate the new map cache for the global context.
931 Handle<MapCache> new_cache = NewMapCache(24);
932 context->set_map_cache(*new_cache);
933 }
934 // Check to see whether there is a matching element in the cache.
935 Handle<MapCache> cache =
936 Handle<MapCache>(MapCache::cast(context->map_cache()));
937 Handle<Object> result = Handle<Object>(cache->Lookup(*keys));
938 if (result->IsMap()) return Handle<Map>::cast(result);
939 // Create a new map and add it to the cache.
940 Handle<Map> map =
941 CopyMap(Handle<Map>(context->object_function()->initial_map()),
942 keys->length());
943 AddToMapCache(context, keys, map);
944 return Handle<Map>(map);
945}
946
947
948void Factory::SetRegExpAtomData(Handle<JSRegExp> regexp,
949 JSRegExp::Type type,
950 Handle<String> source,
951 JSRegExp::Flags flags,
952 Handle<Object> data) {
953 Handle<FixedArray> store = NewFixedArray(JSRegExp::kAtomDataSize);
954
955 store->set(JSRegExp::kTagIndex, Smi::FromInt(type));
956 store->set(JSRegExp::kSourceIndex, *source);
957 store->set(JSRegExp::kFlagsIndex, Smi::FromInt(flags.value()));
958 store->set(JSRegExp::kAtomPatternIndex, *data);
959 regexp->set_data(*store);
960}
961
962void Factory::SetRegExpIrregexpData(Handle<JSRegExp> regexp,
963 JSRegExp::Type type,
964 Handle<String> source,
965 JSRegExp::Flags flags,
966 int capture_count) {
967 Handle<FixedArray> store = NewFixedArray(JSRegExp::kIrregexpDataSize);
968
969 store->set(JSRegExp::kTagIndex, Smi::FromInt(type));
970 store->set(JSRegExp::kSourceIndex, *source);
971 store->set(JSRegExp::kFlagsIndex, Smi::FromInt(flags.value()));
972 store->set(JSRegExp::kIrregexpASCIICodeIndex, Heap::the_hole_value());
973 store->set(JSRegExp::kIrregexpUC16CodeIndex, Heap::the_hole_value());
974 store->set(JSRegExp::kIrregexpMaxRegisterCountIndex, Smi::FromInt(0));
975 store->set(JSRegExp::kIrregexpCaptureCountIndex,
976 Smi::FromInt(capture_count));
977 regexp->set_data(*store);
978}
979
980
981
982void Factory::ConfigureInstance(Handle<FunctionTemplateInfo> desc,
983 Handle<JSObject> instance,
984 bool* pending_exception) {
985 // Configure the instance by adding the properties specified by the
986 // instance template.
987 Handle<Object> instance_template = Handle<Object>(desc->instance_template());
988 if (!instance_template->IsUndefined()) {
989 Execution::ConfigureInstance(instance,
990 instance_template,
991 pending_exception);
992 } else {
993 *pending_exception = false;
994 }
995}
996
997
998} } // namespace v8::internal