blob: 2bc878cc4dab0a532fc72ddae785779c155689cc [file] [log] [blame]
Ben Murdochb0fe1622011-05-05 13:52:32 +01001// Copyright 2010 the V8 project authors. All rights reserved.
Steve Blocka7e24c12009-10-30 11:49:00 +00002// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28#include "v8.h"
29
30#include "api.h"
31#include "debug.h"
32#include "execution.h"
33#include "factory.h"
34#include "macro-assembler.h"
Ben Murdochb0fe1622011-05-05 13:52:32 +010035#include "objects.h"
Iain Merrick75681382010-08-19 15:07:18 +010036#include "objects-visiting.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000037
38namespace v8 {
39namespace internal {
40
41
42Handle<FixedArray> Factory::NewFixedArray(int size, PretenureFlag pretenure) {
43 ASSERT(0 <= size);
44 CALL_HEAP_FUNCTION(Heap::AllocateFixedArray(size, pretenure), FixedArray);
45}
46
47
Steve Block6ded16b2010-05-10 14:33:55 +010048Handle<FixedArray> Factory::NewFixedArrayWithHoles(int size,
49 PretenureFlag pretenure) {
Steve Blocka7e24c12009-10-30 11:49:00 +000050 ASSERT(0 <= size);
Steve Block6ded16b2010-05-10 14:33:55 +010051 CALL_HEAP_FUNCTION(Heap::AllocateFixedArrayWithHoles(size, pretenure),
52 FixedArray);
Steve Blocka7e24c12009-10-30 11:49:00 +000053}
54
55
56Handle<StringDictionary> Factory::NewStringDictionary(int at_least_space_for) {
57 ASSERT(0 <= at_least_space_for);
58 CALL_HEAP_FUNCTION(StringDictionary::Allocate(at_least_space_for),
59 StringDictionary);
60}
61
62
63Handle<NumberDictionary> Factory::NewNumberDictionary(int at_least_space_for) {
64 ASSERT(0 <= at_least_space_for);
65 CALL_HEAP_FUNCTION(NumberDictionary::Allocate(at_least_space_for),
66 NumberDictionary);
67}
68
69
70Handle<DescriptorArray> Factory::NewDescriptorArray(int number_of_descriptors) {
71 ASSERT(0 <= number_of_descriptors);
72 CALL_HEAP_FUNCTION(DescriptorArray::Allocate(number_of_descriptors),
73 DescriptorArray);
74}
75
76
Ben Murdochb0fe1622011-05-05 13:52:32 +010077Handle<DeoptimizationInputData> Factory::NewDeoptimizationInputData(
78 int deopt_entry_count,
79 PretenureFlag pretenure) {
80 ASSERT(deopt_entry_count > 0);
81 CALL_HEAP_FUNCTION(DeoptimizationInputData::Allocate(deopt_entry_count,
82 pretenure),
83 DeoptimizationInputData);
84}
85
86
87Handle<DeoptimizationOutputData> Factory::NewDeoptimizationOutputData(
88 int deopt_entry_count,
89 PretenureFlag pretenure) {
90 ASSERT(deopt_entry_count > 0);
91 CALL_HEAP_FUNCTION(DeoptimizationOutputData::Allocate(deopt_entry_count,
92 pretenure),
93 DeoptimizationOutputData);
94}
95
96
Steve Blocka7e24c12009-10-30 11:49:00 +000097// Symbols are created in the old generation (data space).
98Handle<String> Factory::LookupSymbol(Vector<const char> string) {
99 CALL_HEAP_FUNCTION(Heap::LookupSymbol(string), String);
100}
101
Steve Block9fac8402011-05-12 15:51:54 +0100102Handle<String> Factory::LookupAsciiSymbol(Vector<const char> string) {
103 CALL_HEAP_FUNCTION(Heap::LookupAsciiSymbol(string), String);
104}
105
106Handle<String> Factory::LookupTwoByteSymbol(Vector<const uc16> string) {
107 CALL_HEAP_FUNCTION(Heap::LookupTwoByteSymbol(string), String);
108}
109
Steve Blocka7e24c12009-10-30 11:49:00 +0000110
111Handle<String> Factory::NewStringFromAscii(Vector<const char> string,
112 PretenureFlag pretenure) {
113 CALL_HEAP_FUNCTION(Heap::AllocateStringFromAscii(string, pretenure), String);
114}
115
116Handle<String> Factory::NewStringFromUtf8(Vector<const char> string,
117 PretenureFlag pretenure) {
118 CALL_HEAP_FUNCTION(Heap::AllocateStringFromUtf8(string, pretenure), String);
119}
120
121
122Handle<String> Factory::NewStringFromTwoByte(Vector<const uc16> string,
123 PretenureFlag pretenure) {
124 CALL_HEAP_FUNCTION(Heap::AllocateStringFromTwoByte(string, pretenure),
125 String);
126}
127
128
Leon Clarkeac952652010-07-15 11:15:24 +0100129Handle<String> Factory::NewRawAsciiString(int length,
130 PretenureFlag pretenure) {
131 CALL_HEAP_FUNCTION(Heap::AllocateRawAsciiString(length, pretenure), String);
132}
133
134
Steve Blocka7e24c12009-10-30 11:49:00 +0000135Handle<String> Factory::NewRawTwoByteString(int length,
136 PretenureFlag pretenure) {
137 CALL_HEAP_FUNCTION(Heap::AllocateRawTwoByteString(length, pretenure), String);
138}
139
140
141Handle<String> Factory::NewConsString(Handle<String> first,
142 Handle<String> second) {
143 CALL_HEAP_FUNCTION(Heap::AllocateConsString(*first, *second), String);
144}
145
146
Steve Blockd0582a62009-12-15 09:54:21 +0000147Handle<String> Factory::NewSubString(Handle<String> str,
148 int begin,
149 int end) {
150 CALL_HEAP_FUNCTION(str->SubString(begin, end), String);
Steve Blocka7e24c12009-10-30 11:49:00 +0000151}
152
153
154Handle<String> Factory::NewExternalStringFromAscii(
155 ExternalAsciiString::Resource* resource) {
156 CALL_HEAP_FUNCTION(Heap::AllocateExternalStringFromAscii(resource), String);
157}
158
159
160Handle<String> Factory::NewExternalStringFromTwoByte(
161 ExternalTwoByteString::Resource* resource) {
162 CALL_HEAP_FUNCTION(Heap::AllocateExternalStringFromTwoByte(resource), String);
163}
164
165
166Handle<Context> Factory::NewGlobalContext() {
167 CALL_HEAP_FUNCTION(Heap::AllocateGlobalContext(), Context);
168}
169
170
171Handle<Context> Factory::NewFunctionContext(int length,
172 Handle<JSFunction> closure) {
173 CALL_HEAP_FUNCTION(Heap::AllocateFunctionContext(length, *closure), Context);
174}
175
176
177Handle<Context> Factory::NewWithContext(Handle<Context> previous,
178 Handle<JSObject> extension,
179 bool is_catch_context) {
180 CALL_HEAP_FUNCTION(Heap::AllocateWithContext(*previous,
181 *extension,
182 is_catch_context),
183 Context);
184}
185
186
187Handle<Struct> Factory::NewStruct(InstanceType type) {
188 CALL_HEAP_FUNCTION(Heap::AllocateStruct(type), Struct);
189}
190
191
192Handle<AccessorInfo> Factory::NewAccessorInfo() {
193 Handle<AccessorInfo> info =
194 Handle<AccessorInfo>::cast(NewStruct(ACCESSOR_INFO_TYPE));
195 info->set_flag(0); // Must clear the flag, it was initialized as undefined.
196 return info;
197}
198
199
200Handle<Script> Factory::NewScript(Handle<String> source) {
201 // Generate id for this script.
202 int id;
203 if (Heap::last_script_id()->IsUndefined()) {
204 // Script ids start from one.
205 id = 1;
206 } else {
207 // Increment id, wrap when positive smi is exhausted.
208 id = Smi::cast(Heap::last_script_id())->value();
209 id++;
210 if (!Smi::IsValid(id)) {
211 id = 0;
212 }
213 }
214 Heap::SetLastScriptId(Smi::FromInt(id));
215
216 // Create and initialize script object.
217 Handle<Proxy> wrapper = Factory::NewProxy(0, TENURED);
218 Handle<Script> script = Handle<Script>::cast(NewStruct(SCRIPT_TYPE));
219 script->set_source(*source);
220 script->set_name(Heap::undefined_value());
221 script->set_id(Heap::last_script_id());
222 script->set_line_offset(Smi::FromInt(0));
223 script->set_column_offset(Smi::FromInt(0));
224 script->set_data(Heap::undefined_value());
225 script->set_context_data(Heap::undefined_value());
226 script->set_type(Smi::FromInt(Script::TYPE_NORMAL));
227 script->set_compilation_type(Smi::FromInt(Script::COMPILATION_TYPE_HOST));
228 script->set_wrapper(*wrapper);
229 script->set_line_ends(Heap::undefined_value());
Steve Blockd0582a62009-12-15 09:54:21 +0000230 script->set_eval_from_shared(Heap::undefined_value());
Steve Blocka7e24c12009-10-30 11:49:00 +0000231 script->set_eval_from_instructions_offset(Smi::FromInt(0));
232
233 return script;
234}
235
236
237Handle<Proxy> Factory::NewProxy(Address addr, PretenureFlag pretenure) {
238 CALL_HEAP_FUNCTION(Heap::AllocateProxy(addr, pretenure), Proxy);
239}
240
241
242Handle<Proxy> Factory::NewProxy(const AccessorDescriptor* desc) {
243 return NewProxy((Address) desc, TENURED);
244}
245
246
247Handle<ByteArray> Factory::NewByteArray(int length, PretenureFlag pretenure) {
248 ASSERT(0 <= length);
249 CALL_HEAP_FUNCTION(Heap::AllocateByteArray(length, pretenure), ByteArray);
250}
251
252
253Handle<PixelArray> Factory::NewPixelArray(int length,
254 uint8_t* external_pointer,
255 PretenureFlag pretenure) {
256 ASSERT(0 <= length);
257 CALL_HEAP_FUNCTION(Heap::AllocatePixelArray(length,
258 external_pointer,
259 pretenure), PixelArray);
260}
261
262
Steve Block3ce2e202009-11-05 08:53:23 +0000263Handle<ExternalArray> Factory::NewExternalArray(int length,
264 ExternalArrayType array_type,
265 void* external_pointer,
266 PretenureFlag pretenure) {
267 ASSERT(0 <= length);
268 CALL_HEAP_FUNCTION(Heap::AllocateExternalArray(length,
269 array_type,
270 external_pointer,
271 pretenure), ExternalArray);
272}
273
274
Ben Murdochb0fe1622011-05-05 13:52:32 +0100275Handle<JSGlobalPropertyCell> Factory::NewJSGlobalPropertyCell(
276 Handle<Object> value) {
277 CALL_HEAP_FUNCTION(Heap::AllocateJSGlobalPropertyCell(*value),
278 JSGlobalPropertyCell);
279}
280
281
Steve Blocka7e24c12009-10-30 11:49:00 +0000282Handle<Map> Factory::NewMap(InstanceType type, int instance_size) {
283 CALL_HEAP_FUNCTION(Heap::AllocateMap(type, instance_size), Map);
284}
285
286
287Handle<JSObject> Factory::NewFunctionPrototype(Handle<JSFunction> function) {
288 CALL_HEAP_FUNCTION(Heap::AllocateFunctionPrototype(*function), JSObject);
289}
290
291
292Handle<Map> Factory::CopyMapDropDescriptors(Handle<Map> src) {
293 CALL_HEAP_FUNCTION(src->CopyDropDescriptors(), Map);
294}
295
296
297Handle<Map> Factory::CopyMap(Handle<Map> src,
298 int extra_inobject_properties) {
299 Handle<Map> copy = CopyMapDropDescriptors(src);
300 // Check that we do not overflow the instance size when adding the
301 // extra inobject properties.
302 int instance_size_delta = extra_inobject_properties * kPointerSize;
303 int max_instance_size_delta =
304 JSObject::kMaxInstanceSize - copy->instance_size();
305 if (instance_size_delta > max_instance_size_delta) {
306 // If the instance size overflows, we allocate as many properties
307 // as we can as inobject properties.
308 instance_size_delta = max_instance_size_delta;
309 extra_inobject_properties = max_instance_size_delta >> kPointerSizeLog2;
310 }
311 // Adjust the map with the extra inobject properties.
312 int inobject_properties =
313 copy->inobject_properties() + extra_inobject_properties;
314 copy->set_inobject_properties(inobject_properties);
315 copy->set_unused_property_fields(inobject_properties);
316 copy->set_instance_size(copy->instance_size() + instance_size_delta);
Iain Merrick75681382010-08-19 15:07:18 +0100317 copy->set_visitor_id(StaticVisitorBase::GetVisitorId(*copy));
Steve Blocka7e24c12009-10-30 11:49:00 +0000318 return copy;
319}
320
Steve Block8defd9f2010-07-08 12:39:36 +0100321
Steve Blocka7e24c12009-10-30 11:49:00 +0000322Handle<Map> Factory::CopyMapDropTransitions(Handle<Map> src) {
323 CALL_HEAP_FUNCTION(src->CopyDropTransitions(), Map);
324}
325
326
Steve Block8defd9f2010-07-08 12:39:36 +0100327Handle<Map> Factory::GetFastElementsMap(Handle<Map> src) {
328 CALL_HEAP_FUNCTION(src->GetFastElementsMap(), Map);
329}
330
331
332Handle<Map> Factory::GetSlowElementsMap(Handle<Map> src) {
333 CALL_HEAP_FUNCTION(src->GetSlowElementsMap(), Map);
334}
335
336
Steve Blocka7e24c12009-10-30 11:49:00 +0000337Handle<FixedArray> Factory::CopyFixedArray(Handle<FixedArray> array) {
338 CALL_HEAP_FUNCTION(array->Copy(), FixedArray);
339}
340
341
Steve Block6ded16b2010-05-10 14:33:55 +0100342Handle<JSFunction> Factory::BaseNewFunctionFromSharedFunctionInfo(
343 Handle<SharedFunctionInfo> function_info,
Leon Clarkee46be812010-01-19 14:06:41 +0000344 Handle<Map> function_map,
345 PretenureFlag pretenure) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000346 CALL_HEAP_FUNCTION(Heap::AllocateFunction(*function_map,
Steve Block6ded16b2010-05-10 14:33:55 +0100347 *function_info,
Leon Clarkee46be812010-01-19 14:06:41 +0000348 Heap::the_hole_value(),
349 pretenure),
Steve Blocka7e24c12009-10-30 11:49:00 +0000350 JSFunction);
351}
352
353
Steve Block6ded16b2010-05-10 14:33:55 +0100354Handle<JSFunction> Factory::NewFunctionFromSharedFunctionInfo(
355 Handle<SharedFunctionInfo> function_info,
Leon Clarkee46be812010-01-19 14:06:41 +0000356 Handle<Context> context,
357 PretenureFlag pretenure) {
Steve Block6ded16b2010-05-10 14:33:55 +0100358 Handle<JSFunction> result = BaseNewFunctionFromSharedFunctionInfo(
359 function_info, Top::function_map(), pretenure);
Steve Blocka7e24c12009-10-30 11:49:00 +0000360 result->set_context(*context);
Steve Block6ded16b2010-05-10 14:33:55 +0100361 int number_of_literals = function_info->num_literals();
Steve Blocka7e24c12009-10-30 11:49:00 +0000362 Handle<FixedArray> literals =
Leon Clarkee46be812010-01-19 14:06:41 +0000363 Factory::NewFixedArray(number_of_literals, pretenure);
Steve Blocka7e24c12009-10-30 11:49:00 +0000364 if (number_of_literals > 0) {
365 // Store the object, regexp and array functions in the literals
366 // array prefix. These functions will be used when creating
367 // object, regexp and array literals in this function.
368 literals->set(JSFunction::kLiteralGlobalContextIndex,
369 context->global_context());
370 }
371 result->set_literals(*literals);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100372 result->set_next_function_link(Heap::undefined_value());
373
374 if (V8::UseCrankshaft() &&
375 FLAG_always_opt &&
376 result->is_compiled() &&
377 !function_info->is_toplevel() &&
378 function_info->allows_lazy_compilation()) {
379 result->MarkForLazyRecompilation();
380 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000381 return result;
382}
383
384
385Handle<Object> Factory::NewNumber(double value,
386 PretenureFlag pretenure) {
387 CALL_HEAP_FUNCTION(Heap::NumberFromDouble(value, pretenure), Object);
388}
389
390
391Handle<Object> Factory::NewNumberFromInt(int value) {
392 CALL_HEAP_FUNCTION(Heap::NumberFromInt32(value), Object);
393}
394
395
396Handle<Object> Factory::NewNumberFromUint(uint32_t value) {
397 CALL_HEAP_FUNCTION(Heap::NumberFromUint32(value), Object);
398}
399
400
401Handle<JSObject> Factory::NewNeanderObject() {
402 CALL_HEAP_FUNCTION(Heap::AllocateJSObjectFromMap(Heap::neander_map()),
403 JSObject);
404}
405
406
407Handle<Object> Factory::NewTypeError(const char* type,
408 Vector< Handle<Object> > args) {
409 return NewError("MakeTypeError", type, args);
410}
411
412
413Handle<Object> Factory::NewTypeError(Handle<String> message) {
414 return NewError("$TypeError", message);
415}
416
417
418Handle<Object> Factory::NewRangeError(const char* type,
419 Vector< Handle<Object> > args) {
420 return NewError("MakeRangeError", type, args);
421}
422
423
424Handle<Object> Factory::NewRangeError(Handle<String> message) {
425 return NewError("$RangeError", message);
426}
427
428
429Handle<Object> Factory::NewSyntaxError(const char* type, Handle<JSArray> args) {
430 return NewError("MakeSyntaxError", type, args);
431}
432
433
434Handle<Object> Factory::NewSyntaxError(Handle<String> message) {
435 return NewError("$SyntaxError", message);
436}
437
438
439Handle<Object> Factory::NewReferenceError(const char* type,
440 Vector< Handle<Object> > args) {
441 return NewError("MakeReferenceError", type, args);
442}
443
444
445Handle<Object> Factory::NewReferenceError(Handle<String> message) {
446 return NewError("$ReferenceError", message);
447}
448
449
450Handle<Object> Factory::NewError(const char* maker, const char* type,
451 Vector< Handle<Object> > args) {
452 v8::HandleScope scope; // Instantiate a closeable HandleScope for EscapeFrom.
453 Handle<FixedArray> array = Factory::NewFixedArray(args.length());
454 for (int i = 0; i < args.length(); i++) {
455 array->set(i, *args[i]);
456 }
457 Handle<JSArray> object = Factory::NewJSArrayWithElements(array);
458 Handle<Object> result = NewError(maker, type, object);
459 return result.EscapeFrom(&scope);
460}
461
462
463Handle<Object> Factory::NewEvalError(const char* type,
464 Vector< Handle<Object> > args) {
465 return NewError("MakeEvalError", type, args);
466}
467
468
469Handle<Object> Factory::NewError(const char* type,
470 Vector< Handle<Object> > args) {
471 return NewError("MakeError", type, args);
472}
473
474
475Handle<Object> Factory::NewError(const char* maker,
476 const char* type,
477 Handle<JSArray> args) {
478 Handle<String> make_str = Factory::LookupAsciiSymbol(maker);
John Reck59135872010-11-02 12:39:01 -0700479 Handle<Object> fun_obj(Top::builtins()->GetPropertyNoExceptionThrown(
480 *make_str));
Steve Blocka7e24c12009-10-30 11:49:00 +0000481 // If the builtins haven't been properly configured yet this error
482 // constructor may not have been defined. Bail out.
483 if (!fun_obj->IsJSFunction())
484 return Factory::undefined_value();
485 Handle<JSFunction> fun = Handle<JSFunction>::cast(fun_obj);
486 Handle<Object> type_obj = Factory::LookupAsciiSymbol(type);
487 Object** argv[2] = { type_obj.location(),
488 Handle<Object>::cast(args).location() };
489
490 // Invoke the JavaScript factory method. If an exception is thrown while
491 // running the factory method, use the exception as the result.
492 bool caught_exception;
493 Handle<Object> result = Execution::TryCall(fun,
494 Top::builtins(),
495 2,
496 argv,
497 &caught_exception);
498 return result;
499}
500
501
502Handle<Object> Factory::NewError(Handle<String> message) {
503 return NewError("$Error", message);
504}
505
506
507Handle<Object> Factory::NewError(const char* constructor,
508 Handle<String> message) {
509 Handle<String> constr = Factory::LookupAsciiSymbol(constructor);
510 Handle<JSFunction> fun =
511 Handle<JSFunction>(
512 JSFunction::cast(
John Reck59135872010-11-02 12:39:01 -0700513 Top::builtins()->GetPropertyNoExceptionThrown(*constr)));
Steve Blocka7e24c12009-10-30 11:49:00 +0000514 Object** argv[1] = { Handle<Object>::cast(message).location() };
515
516 // Invoke the JavaScript factory method. If an exception is thrown while
517 // running the factory method, use the exception as the result.
518 bool caught_exception;
519 Handle<Object> result = Execution::TryCall(fun,
520 Top::builtins(),
521 1,
522 argv,
523 &caught_exception);
524 return result;
525}
526
527
528Handle<JSFunction> Factory::NewFunction(Handle<String> name,
529 InstanceType type,
530 int instance_size,
531 Handle<Code> code,
532 bool force_initial_map) {
533 // Allocate the function
534 Handle<JSFunction> function = NewFunction(name, the_hole_value());
Iain Merrick75681382010-08-19 15:07:18 +0100535
536 // Setup the code pointer in both the shared function info and in
537 // the function itself.
538 function->shared()->set_code(*code);
Steve Blocka7e24c12009-10-30 11:49:00 +0000539 function->set_code(*code);
540
541 if (force_initial_map ||
542 type != JS_OBJECT_TYPE ||
543 instance_size != JSObject::kHeaderSize) {
544 Handle<Map> initial_map = NewMap(type, instance_size);
545 Handle<JSObject> prototype = NewFunctionPrototype(function);
546 initial_map->set_prototype(*prototype);
547 function->set_initial_map(*initial_map);
548 initial_map->set_constructor(*function);
549 } else {
550 ASSERT(!function->has_initial_map());
551 ASSERT(!function->has_prototype());
552 }
553
554 return function;
555}
556
557
Steve Blocka7e24c12009-10-30 11:49:00 +0000558Handle<JSFunction> Factory::NewFunctionWithPrototype(Handle<String> name,
559 InstanceType type,
560 int instance_size,
561 Handle<JSObject> prototype,
562 Handle<Code> code,
563 bool force_initial_map) {
Iain Merrick75681382010-08-19 15:07:18 +0100564 // Allocate the function.
Steve Blocka7e24c12009-10-30 11:49:00 +0000565 Handle<JSFunction> function = NewFunction(name, prototype);
566
Iain Merrick75681382010-08-19 15:07:18 +0100567 // Setup the code pointer in both the shared function info and in
568 // the function itself.
569 function->shared()->set_code(*code);
Steve Blocka7e24c12009-10-30 11:49:00 +0000570 function->set_code(*code);
571
572 if (force_initial_map ||
573 type != JS_OBJECT_TYPE ||
574 instance_size != JSObject::kHeaderSize) {
575 Handle<Map> initial_map = NewMap(type, instance_size);
576 function->set_initial_map(*initial_map);
577 initial_map->set_constructor(*function);
578 }
579
580 // Set function.prototype and give the prototype a constructor
581 // property that refers to the function.
582 SetPrototypeProperty(function, prototype);
583 SetProperty(prototype, Factory::constructor_symbol(), function, DONT_ENUM);
584 return function;
585}
586
587
Steve Block6ded16b2010-05-10 14:33:55 +0100588Handle<JSFunction> Factory::NewFunctionWithoutPrototype(Handle<String> name,
589 Handle<Code> code) {
590 Handle<JSFunction> function = NewFunctionWithoutPrototype(name);
Iain Merrick75681382010-08-19 15:07:18 +0100591 function->shared()->set_code(*code);
Steve Block6ded16b2010-05-10 14:33:55 +0100592 function->set_code(*code);
593 ASSERT(!function->has_initial_map());
594 ASSERT(!function->has_prototype());
595 return function;
596}
597
598
Steve Blocka7e24c12009-10-30 11:49:00 +0000599Handle<Code> Factory::NewCode(const CodeDesc& desc,
Steve Blocka7e24c12009-10-30 11:49:00 +0000600 Code::Flags flags,
601 Handle<Object> self_ref) {
Ben Murdoch3bec4d22010-07-22 14:51:16 +0100602 CALL_HEAP_FUNCTION(Heap::CreateCode(desc, flags, self_ref), Code);
Steve Blocka7e24c12009-10-30 11:49:00 +0000603}
604
605
606Handle<Code> Factory::CopyCode(Handle<Code> code) {
607 CALL_HEAP_FUNCTION(Heap::CopyCode(*code), Code);
608}
609
610
Steve Block6ded16b2010-05-10 14:33:55 +0100611Handle<Code> Factory::CopyCode(Handle<Code> code, Vector<byte> reloc_info) {
612 CALL_HEAP_FUNCTION(Heap::CopyCode(*code, reloc_info), Code);
613}
614
615
John Reck59135872010-11-02 12:39:01 -0700616MUST_USE_RESULT static inline MaybeObject* DoCopyInsert(
617 DescriptorArray* array,
618 String* key,
619 Object* value,
620 PropertyAttributes attributes) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000621 CallbacksDescriptor desc(key, value, attributes);
John Reck59135872010-11-02 12:39:01 -0700622 MaybeObject* obj = array->CopyInsert(&desc, REMOVE_TRANSITIONS);
Steve Blocka7e24c12009-10-30 11:49:00 +0000623 return obj;
624}
625
626
627// Allocate the new array.
628Handle<DescriptorArray> Factory::CopyAppendProxyDescriptor(
629 Handle<DescriptorArray> array,
630 Handle<String> key,
631 Handle<Object> value,
632 PropertyAttributes attributes) {
633 CALL_HEAP_FUNCTION(DoCopyInsert(*array, *key, *value, attributes),
634 DescriptorArray);
635}
636
637
638Handle<String> Factory::SymbolFromString(Handle<String> value) {
639 CALL_HEAP_FUNCTION(Heap::LookupSymbol(*value), String);
640}
641
642
643Handle<DescriptorArray> Factory::CopyAppendCallbackDescriptors(
644 Handle<DescriptorArray> array,
645 Handle<Object> descriptors) {
646 v8::NeanderArray callbacks(descriptors);
647 int nof_callbacks = callbacks.length();
648 Handle<DescriptorArray> result =
649 NewDescriptorArray(array->number_of_descriptors() + nof_callbacks);
650
651 // Number of descriptors added to the result so far.
652 int descriptor_count = 0;
653
654 // Copy the descriptors from the array.
655 for (int i = 0; i < array->number_of_descriptors(); i++) {
656 if (array->GetType(i) != NULL_DESCRIPTOR) {
657 result->CopyFrom(descriptor_count++, *array, i);
658 }
659 }
660
661 // Number of duplicates detected.
662 int duplicates = 0;
663
664 // Fill in new callback descriptors. Process the callbacks from
665 // back to front so that the last callback with a given name takes
666 // precedence over previously added callbacks with that name.
667 for (int i = nof_callbacks - 1; i >= 0; i--) {
668 Handle<AccessorInfo> entry =
669 Handle<AccessorInfo>(AccessorInfo::cast(callbacks.get(i)));
670 // Ensure the key is a symbol before writing into the instance descriptor.
671 Handle<String> key =
672 SymbolFromString(Handle<String>(String::cast(entry->name())));
673 // Check if a descriptor with this name already exists before writing.
674 if (result->LinearSearch(*key, descriptor_count) ==
675 DescriptorArray::kNotFound) {
676 CallbacksDescriptor desc(*key, *entry, entry->property_attributes());
677 result->Set(descriptor_count, &desc);
678 descriptor_count++;
679 } else {
680 duplicates++;
681 }
682 }
683
684 // If duplicates were detected, allocate a result of the right size
685 // and transfer the elements.
686 if (duplicates > 0) {
687 int number_of_descriptors = result->number_of_descriptors() - duplicates;
688 Handle<DescriptorArray> new_result =
689 NewDescriptorArray(number_of_descriptors);
690 for (int i = 0; i < number_of_descriptors; i++) {
691 new_result->CopyFrom(i, *result, i);
692 }
693 result = new_result;
694 }
695
696 // Sort the result before returning.
697 result->Sort();
698 return result;
699}
700
701
702Handle<JSObject> Factory::NewJSObject(Handle<JSFunction> constructor,
703 PretenureFlag pretenure) {
704 CALL_HEAP_FUNCTION(Heap::AllocateJSObject(*constructor, pretenure), JSObject);
705}
706
707
708Handle<GlobalObject> Factory::NewGlobalObject(
709 Handle<JSFunction> constructor) {
710 CALL_HEAP_FUNCTION(Heap::AllocateGlobalObject(*constructor),
711 GlobalObject);
712}
713
714
715
716Handle<JSObject> Factory::NewJSObjectFromMap(Handle<Map> map) {
717 CALL_HEAP_FUNCTION(Heap::AllocateJSObjectFromMap(*map, NOT_TENURED),
718 JSObject);
719}
720
721
722Handle<JSArray> Factory::NewJSArray(int length,
723 PretenureFlag pretenure) {
724 Handle<JSObject> obj = NewJSObject(Top::array_function(), pretenure);
725 CALL_HEAP_FUNCTION(Handle<JSArray>::cast(obj)->Initialize(length), JSArray);
726}
727
728
729Handle<JSArray> Factory::NewJSArrayWithElements(Handle<FixedArray> elements,
730 PretenureFlag pretenure) {
731 Handle<JSArray> result =
732 Handle<JSArray>::cast(NewJSObject(Top::array_function(), pretenure));
733 result->SetContent(*elements);
734 return result;
735}
736
737
Steve Block6ded16b2010-05-10 14:33:55 +0100738Handle<SharedFunctionInfo> Factory::NewSharedFunctionInfo(
Ben Murdoch3bec4d22010-07-22 14:51:16 +0100739 Handle<String> name,
740 int number_of_literals,
741 Handle<Code> code,
742 Handle<SerializedScopeInfo> scope_info) {
Steve Block6ded16b2010-05-10 14:33:55 +0100743 Handle<SharedFunctionInfo> shared = NewSharedFunctionInfo(name);
744 shared->set_code(*code);
Ben Murdoch3bec4d22010-07-22 14:51:16 +0100745 shared->set_scope_info(*scope_info);
Steve Block6ded16b2010-05-10 14:33:55 +0100746 int literals_array_size = number_of_literals;
747 // If the function contains object, regexp or array literals,
748 // allocate extra space for a literals array prefix containing the
749 // context.
750 if (number_of_literals > 0) {
751 literals_array_size += JSFunction::kLiteralsPrefixSize;
752 }
753 shared->set_num_literals(literals_array_size);
754 return shared;
755}
756
757
Steve Blocka7e24c12009-10-30 11:49:00 +0000758Handle<SharedFunctionInfo> Factory::NewSharedFunctionInfo(Handle<String> name) {
759 CALL_HEAP_FUNCTION(Heap::AllocateSharedFunctionInfo(*name),
760 SharedFunctionInfo);
761}
762
763
764Handle<String> Factory::NumberToString(Handle<Object> number) {
765 CALL_HEAP_FUNCTION(Heap::NumberToString(*number), String);
766}
767
768
769Handle<NumberDictionary> Factory::DictionaryAtNumberPut(
770 Handle<NumberDictionary> dictionary,
771 uint32_t key,
772 Handle<Object> value) {
773 CALL_HEAP_FUNCTION(dictionary->AtNumberPut(key, *value), NumberDictionary);
774}
775
776
777Handle<JSFunction> Factory::NewFunctionHelper(Handle<String> name,
778 Handle<Object> prototype) {
779 Handle<SharedFunctionInfo> function_share = NewSharedFunctionInfo(name);
780 CALL_HEAP_FUNCTION(Heap::AllocateFunction(*Top::function_map(),
781 *function_share,
782 *prototype),
783 JSFunction);
784}
785
786
787Handle<JSFunction> Factory::NewFunction(Handle<String> name,
788 Handle<Object> prototype) {
789 Handle<JSFunction> fun = NewFunctionHelper(name, prototype);
790 fun->set_context(Top::context()->global_context());
791 return fun;
792}
793
794
Steve Block6ded16b2010-05-10 14:33:55 +0100795Handle<JSFunction> Factory::NewFunctionWithoutPrototypeHelper(
796 Handle<String> name) {
797 Handle<SharedFunctionInfo> function_share = NewSharedFunctionInfo(name);
798 CALL_HEAP_FUNCTION(Heap::AllocateFunction(
799 *Top::function_without_prototype_map(),
800 *function_share,
801 *the_hole_value()),
802 JSFunction);
803}
804
805
806Handle<JSFunction> Factory::NewFunctionWithoutPrototype(Handle<String> name) {
807 Handle<JSFunction> fun = NewFunctionWithoutPrototypeHelper(name);
808 fun->set_context(Top::context()->global_context());
809 return fun;
810}
811
812
Leon Clarkee46be812010-01-19 14:06:41 +0000813Handle<Object> Factory::ToObject(Handle<Object> object) {
814 CALL_HEAP_FUNCTION(object->ToObject(), Object);
815}
816
817
Steve Blocka7e24c12009-10-30 11:49:00 +0000818Handle<Object> Factory::ToObject(Handle<Object> object,
819 Handle<Context> global_context) {
820 CALL_HEAP_FUNCTION(object->ToObject(*global_context), Object);
821}
822
823
824#ifdef ENABLE_DEBUGGER_SUPPORT
825Handle<DebugInfo> Factory::NewDebugInfo(Handle<SharedFunctionInfo> shared) {
826 // Get the original code of the function.
827 Handle<Code> code(shared->code());
828
829 // Create a copy of the code before allocating the debug info object to avoid
830 // allocation while setting up the debug info object.
831 Handle<Code> original_code(*Factory::CopyCode(code));
832
833 // Allocate initial fixed array for active break points before allocating the
834 // debug info object to avoid allocation while setting up the debug info
835 // object.
836 Handle<FixedArray> break_points(
837 Factory::NewFixedArray(Debug::kEstimatedNofBreakPointsInFunction));
838
839 // Create and set up the debug info object. Debug info contains function, a
840 // copy of the original code, the executing code and initial fixed array for
841 // active break points.
842 Handle<DebugInfo> debug_info =
843 Handle<DebugInfo>::cast(Factory::NewStruct(DEBUG_INFO_TYPE));
844 debug_info->set_shared(*shared);
845 debug_info->set_original_code(*original_code);
846 debug_info->set_code(*code);
847 debug_info->set_break_points(*break_points);
848
849 // Link debug info to function.
850 shared->set_debug_info(*debug_info);
851
852 return debug_info;
853}
854#endif
855
856
857Handle<JSObject> Factory::NewArgumentsObject(Handle<Object> callee,
858 int length) {
859 CALL_HEAP_FUNCTION(Heap::AllocateArgumentsObject(*callee, length), JSObject);
860}
861
862
863Handle<JSFunction> Factory::CreateApiFunction(
864 Handle<FunctionTemplateInfo> obj, ApiInstanceType instance_type) {
865 Handle<Code> code = Handle<Code>(Builtins::builtin(Builtins::HandleApiCall));
Leon Clarkee46be812010-01-19 14:06:41 +0000866 Handle<Code> construct_stub =
867 Handle<Code>(Builtins::builtin(Builtins::JSConstructStubApi));
Steve Blocka7e24c12009-10-30 11:49:00 +0000868
869 int internal_field_count = 0;
870 if (!obj->instance_template()->IsUndefined()) {
871 Handle<ObjectTemplateInfo> instance_template =
872 Handle<ObjectTemplateInfo>(
873 ObjectTemplateInfo::cast(obj->instance_template()));
874 internal_field_count =
875 Smi::cast(instance_template->internal_field_count())->value();
876 }
877
878 int instance_size = kPointerSize * internal_field_count;
879 InstanceType type = INVALID_TYPE;
880 switch (instance_type) {
881 case JavaScriptObject:
882 type = JS_OBJECT_TYPE;
883 instance_size += JSObject::kHeaderSize;
884 break;
885 case InnerGlobalObject:
886 type = JS_GLOBAL_OBJECT_TYPE;
887 instance_size += JSGlobalObject::kSize;
888 break;
889 case OuterGlobalObject:
890 type = JS_GLOBAL_PROXY_TYPE;
891 instance_size += JSGlobalProxy::kSize;
892 break;
893 default:
894 break;
895 }
896 ASSERT(type != INVALID_TYPE);
897
898 Handle<JSFunction> result =
899 Factory::NewFunction(Factory::empty_symbol(),
900 type,
901 instance_size,
902 code,
903 true);
904 // Set class name.
905 Handle<Object> class_name = Handle<Object>(obj->class_name());
906 if (class_name->IsString()) {
907 result->shared()->set_instance_class_name(*class_name);
908 result->shared()->set_name(*class_name);
909 }
910
911 Handle<Map> map = Handle<Map>(result->initial_map());
912
913 // Mark as undetectable if needed.
914 if (obj->undetectable()) {
915 map->set_is_undetectable();
916 }
917
918 // Mark as hidden for the __proto__ accessor if needed.
919 if (obj->hidden_prototype()) {
920 map->set_is_hidden_prototype();
921 }
922
923 // Mark as needs_access_check if needed.
924 if (obj->needs_access_check()) {
925 map->set_is_access_check_needed(true);
926 }
927
928 // Set interceptor information in the map.
929 if (!obj->named_property_handler()->IsUndefined()) {
930 map->set_has_named_interceptor();
931 }
932 if (!obj->indexed_property_handler()->IsUndefined()) {
933 map->set_has_indexed_interceptor();
934 }
935
936 // Set instance call-as-function information in the map.
937 if (!obj->instance_call_handler()->IsUndefined()) {
938 map->set_has_instance_call_handler();
939 }
940
941 result->shared()->set_function_data(*obj);
Leon Clarkee46be812010-01-19 14:06:41 +0000942 result->shared()->set_construct_stub(*construct_stub);
Steve Blocka7e24c12009-10-30 11:49:00 +0000943 result->shared()->DontAdaptArguments();
944
945 // Recursively copy parent templates' accessors, 'data' may be modified.
946 Handle<DescriptorArray> array =
947 Handle<DescriptorArray>(map->instance_descriptors());
948 while (true) {
949 Handle<Object> props = Handle<Object>(obj->property_accessors());
950 if (!props->IsUndefined()) {
951 array = Factory::CopyAppendCallbackDescriptors(array, props);
952 }
953 Handle<Object> parent = Handle<Object>(obj->parent_template());
954 if (parent->IsUndefined()) break;
955 obj = Handle<FunctionTemplateInfo>::cast(parent);
956 }
957 if (!array->IsEmpty()) {
958 map->set_instance_descriptors(*array);
959 }
960
Steve Block6ded16b2010-05-10 14:33:55 +0100961 ASSERT(result->shared()->IsApiFunction());
Steve Blocka7e24c12009-10-30 11:49:00 +0000962 return result;
963}
964
965
966Handle<MapCache> Factory::NewMapCache(int at_least_space_for) {
967 CALL_HEAP_FUNCTION(MapCache::Allocate(at_least_space_for), MapCache);
968}
969
970
John Reck59135872010-11-02 12:39:01 -0700971MUST_USE_RESULT static MaybeObject* UpdateMapCacheWith(Context* context,
972 FixedArray* keys,
973 Map* map) {
974 Object* result;
975 { MaybeObject* maybe_result =
976 MapCache::cast(context->map_cache())->Put(keys, map);
977 if (!maybe_result->ToObject(&result)) return maybe_result;
978 }
979 context->set_map_cache(MapCache::cast(result));
Steve Blocka7e24c12009-10-30 11:49:00 +0000980 return result;
981}
982
983
984Handle<MapCache> Factory::AddToMapCache(Handle<Context> context,
985 Handle<FixedArray> keys,
986 Handle<Map> map) {
987 CALL_HEAP_FUNCTION(UpdateMapCacheWith(*context, *keys, *map), MapCache);
988}
989
990
991Handle<Map> Factory::ObjectLiteralMapFromCache(Handle<Context> context,
992 Handle<FixedArray> keys) {
993 if (context->map_cache()->IsUndefined()) {
994 // Allocate the new map cache for the global context.
995 Handle<MapCache> new_cache = NewMapCache(24);
996 context->set_map_cache(*new_cache);
997 }
998 // Check to see whether there is a matching element in the cache.
999 Handle<MapCache> cache =
1000 Handle<MapCache>(MapCache::cast(context->map_cache()));
1001 Handle<Object> result = Handle<Object>(cache->Lookup(*keys));
1002 if (result->IsMap()) return Handle<Map>::cast(result);
1003 // Create a new map and add it to the cache.
1004 Handle<Map> map =
1005 CopyMap(Handle<Map>(context->object_function()->initial_map()),
1006 keys->length());
1007 AddToMapCache(context, keys, map);
1008 return Handle<Map>(map);
1009}
1010
1011
1012void Factory::SetRegExpAtomData(Handle<JSRegExp> regexp,
1013 JSRegExp::Type type,
1014 Handle<String> source,
1015 JSRegExp::Flags flags,
1016 Handle<Object> data) {
1017 Handle<FixedArray> store = NewFixedArray(JSRegExp::kAtomDataSize);
1018
1019 store->set(JSRegExp::kTagIndex, Smi::FromInt(type));
1020 store->set(JSRegExp::kSourceIndex, *source);
1021 store->set(JSRegExp::kFlagsIndex, Smi::FromInt(flags.value()));
1022 store->set(JSRegExp::kAtomPatternIndex, *data);
1023 regexp->set_data(*store);
1024}
1025
1026void Factory::SetRegExpIrregexpData(Handle<JSRegExp> regexp,
1027 JSRegExp::Type type,
1028 Handle<String> source,
1029 JSRegExp::Flags flags,
1030 int capture_count) {
1031 Handle<FixedArray> store = NewFixedArray(JSRegExp::kIrregexpDataSize);
1032
1033 store->set(JSRegExp::kTagIndex, Smi::FromInt(type));
1034 store->set(JSRegExp::kSourceIndex, *source);
1035 store->set(JSRegExp::kFlagsIndex, Smi::FromInt(flags.value()));
1036 store->set(JSRegExp::kIrregexpASCIICodeIndex, Heap::the_hole_value());
1037 store->set(JSRegExp::kIrregexpUC16CodeIndex, Heap::the_hole_value());
1038 store->set(JSRegExp::kIrregexpMaxRegisterCountIndex, Smi::FromInt(0));
1039 store->set(JSRegExp::kIrregexpCaptureCountIndex,
1040 Smi::FromInt(capture_count));
1041 regexp->set_data(*store);
1042}
1043
1044
1045
1046void Factory::ConfigureInstance(Handle<FunctionTemplateInfo> desc,
1047 Handle<JSObject> instance,
1048 bool* pending_exception) {
1049 // Configure the instance by adding the properties specified by the
1050 // instance template.
1051 Handle<Object> instance_template = Handle<Object>(desc->instance_template());
1052 if (!instance_template->IsUndefined()) {
1053 Execution::ConfigureInstance(instance,
1054 instance_template,
1055 pending_exception);
1056 } else {
1057 *pending_exception = false;
1058 }
1059}
1060
1061
1062} } // namespace v8::internal