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