blob: 3d9cd7a110a0e4ba6ab4d2868dc39cb7e60f9d73 [file] [log] [blame]
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001// Copyright 2006-2008 the V8 project authors. All rights reserved.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +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"
v8.team.kasperl727e9952008-09-02 14:56:44 +000031#include "debug.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000032#include "execution.h"
33#include "factory.h"
34#include "macro-assembler.h"
35
kasperl@chromium.org71affb52009-05-26 05:44:31 +000036namespace v8 {
37namespace internal {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000038
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
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +000046Handle<FixedArray> Factory::NewFixedArrayWithHoles(int size) {
47 ASSERT(0 <= size);
48 CALL_HEAP_FUNCTION(Heap::AllocateFixedArrayWithHoles(size), FixedArray);
49}
50
51
kasperl@chromium.org86f77b72009-07-06 08:21:57 +000052Handle<StringDictionary> Factory::NewStringDictionary(int at_least_space_for) {
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +000053 ASSERT(0 <= at_least_space_for);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +000054 CALL_HEAP_FUNCTION(StringDictionary::Allocate(at_least_space_for),
55 StringDictionary);
56}
57
58
59Handle<NumberDictionary> Factory::NewNumberDictionary(int at_least_space_for) {
60 ASSERT(0 <= at_least_space_for);
61 CALL_HEAP_FUNCTION(NumberDictionary::Allocate(at_least_space_for),
62 NumberDictionary);
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +000063}
64
65
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000066Handle<DescriptorArray> Factory::NewDescriptorArray(int number_of_descriptors) {
67 ASSERT(0 <= number_of_descriptors);
68 CALL_HEAP_FUNCTION(DescriptorArray::Allocate(number_of_descriptors),
69 DescriptorArray);
70}
71
72
ager@chromium.org9258b6b2008-09-11 09:11:10 +000073// Symbols are created in the old generation (data space).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000074Handle<String> Factory::LookupSymbol(Vector<const char> string) {
75 CALL_HEAP_FUNCTION(Heap::LookupSymbol(string), String);
76}
77
78
79Handle<String> Factory::NewStringFromAscii(Vector<const char> string,
80 PretenureFlag pretenure) {
81 CALL_HEAP_FUNCTION(Heap::AllocateStringFromAscii(string, pretenure), String);
82}
83
84Handle<String> Factory::NewStringFromUtf8(Vector<const char> string,
85 PretenureFlag pretenure) {
86 CALL_HEAP_FUNCTION(Heap::AllocateStringFromUtf8(string, pretenure), String);
87}
88
89
sgjesse@chromium.org911335c2009-08-19 12:59:44 +000090Handle<String> Factory::NewStringFromTwoByte(Vector<const uc16> string,
91 PretenureFlag pretenure) {
92 CALL_HEAP_FUNCTION(Heap::AllocateStringFromTwoByte(string, pretenure),
93 String);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000094}
95
96
97Handle<String> Factory::NewRawTwoByteString(int length,
98 PretenureFlag pretenure) {
99 CALL_HEAP_FUNCTION(Heap::AllocateRawTwoByteString(length, pretenure), String);
100}
101
102
103Handle<String> Factory::NewConsString(Handle<String> first,
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000104 Handle<String> second) {
ager@chromium.orgc3e50d82008-11-05 11:53:10 +0000105 CALL_HEAP_FUNCTION(Heap::AllocateConsString(*first, *second), String);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000106}
107
108
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000109Handle<String> Factory::NewSubString(Handle<String> str,
110 int begin,
111 int end) {
112 CALL_HEAP_FUNCTION(str->SubString(begin, end), String);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000113}
114
115
116Handle<String> Factory::NewExternalStringFromAscii(
117 ExternalAsciiString::Resource* resource) {
118 CALL_HEAP_FUNCTION(Heap::AllocateExternalStringFromAscii(resource), String);
119}
120
121
122Handle<String> Factory::NewExternalStringFromTwoByte(
123 ExternalTwoByteString::Resource* resource) {
124 CALL_HEAP_FUNCTION(Heap::AllocateExternalStringFromTwoByte(resource), String);
125}
126
127
128Handle<Context> Factory::NewGlobalContext() {
129 CALL_HEAP_FUNCTION(Heap::AllocateGlobalContext(), Context);
130}
131
132
133Handle<Context> Factory::NewFunctionContext(int length,
134 Handle<JSFunction> closure) {
135 CALL_HEAP_FUNCTION(Heap::AllocateFunctionContext(length, *closure), Context);
136}
137
138
139Handle<Context> Factory::NewWithContext(Handle<Context> previous,
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000140 Handle<JSObject> extension,
141 bool is_catch_context) {
142 CALL_HEAP_FUNCTION(Heap::AllocateWithContext(*previous,
143 *extension,
144 is_catch_context),
145 Context);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000146}
147
148
149Handle<Struct> Factory::NewStruct(InstanceType type) {
150 CALL_HEAP_FUNCTION(Heap::AllocateStruct(type), Struct);
151}
152
153
154Handle<AccessorInfo> Factory::NewAccessorInfo() {
155 Handle<AccessorInfo> info =
156 Handle<AccessorInfo>::cast(NewStruct(ACCESSOR_INFO_TYPE));
157 info->set_flag(0); // Must clear the flag, it was initialized as undefined.
158 return info;
159}
160
161
162Handle<Script> Factory::NewScript(Handle<String> source) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000163 // Generate id for this script.
164 int id;
165 if (Heap::last_script_id()->IsUndefined()) {
166 // Script ids start from one.
167 id = 1;
168 } else {
169 // Increment id, wrap when positive smi is exhausted.
170 id = Smi::cast(Heap::last_script_id())->value();
171 id++;
172 if (!Smi::IsValid(id)) {
173 id = 0;
174 }
175 }
176 Heap::SetLastScriptId(Smi::FromInt(id));
177
178 // Create and initialize script object.
ager@chromium.org9085a012009-05-11 19:22:57 +0000179 Handle<Proxy> wrapper = Factory::NewProxy(0, TENURED);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000180 Handle<Script> script = Handle<Script>::cast(NewStruct(SCRIPT_TYPE));
181 script->set_source(*source);
182 script->set_name(Heap::undefined_value());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000183 script->set_id(Heap::last_script_id());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000184 script->set_line_offset(Smi::FromInt(0));
185 script->set_column_offset(Smi::FromInt(0));
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000186 script->set_data(Heap::undefined_value());
ager@chromium.org9085a012009-05-11 19:22:57 +0000187 script->set_context_data(Heap::undefined_value());
ager@chromium.orge2902be2009-06-08 12:21:35 +0000188 script->set_type(Smi::FromInt(Script::TYPE_NORMAL));
189 script->set_compilation_type(Smi::FromInt(Script::COMPILATION_TYPE_HOST));
ager@chromium.org9085a012009-05-11 19:22:57 +0000190 script->set_wrapper(*wrapper);
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000191 script->set_line_ends_fixed_array(Heap::undefined_value());
192 script->set_line_ends_js_array(Heap::undefined_value());
ager@chromium.orge2902be2009-06-08 12:21:35 +0000193 script->set_eval_from_function(Heap::undefined_value());
194 script->set_eval_from_instructions_offset(Smi::FromInt(0));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000195
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000196 return script;
197}
198
199
200Handle<Proxy> Factory::NewProxy(Address addr, PretenureFlag pretenure) {
201 CALL_HEAP_FUNCTION(Heap::AllocateProxy(addr, pretenure), Proxy);
202}
203
204
205Handle<Proxy> Factory::NewProxy(const AccessorDescriptor* desc) {
206 return NewProxy((Address) desc, TENURED);
207}
208
209
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000210Handle<ByteArray> Factory::NewByteArray(int length, PretenureFlag pretenure) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000211 ASSERT(0 <= length);
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000212 CALL_HEAP_FUNCTION(Heap::AllocateByteArray(length, pretenure), ByteArray);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000213}
214
215
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000216Handle<PixelArray> Factory::NewPixelArray(int length,
217 uint8_t* external_pointer,
218 PretenureFlag pretenure) {
219 ASSERT(0 <= length);
220 CALL_HEAP_FUNCTION(Heap::AllocatePixelArray(length,
221 external_pointer,
222 pretenure), PixelArray);
223}
224
225
ager@chromium.org3811b432009-10-28 14:53:37 +0000226Handle<ExternalArray> Factory::NewExternalArray(int length,
227 ExternalArrayType array_type,
228 void* external_pointer,
229 PretenureFlag pretenure) {
230 ASSERT(0 <= length);
231 CALL_HEAP_FUNCTION(Heap::AllocateExternalArray(length,
232 array_type,
233 external_pointer,
234 pretenure), ExternalArray);
235}
236
237
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000238Handle<Map> Factory::NewMap(InstanceType type, int instance_size) {
239 CALL_HEAP_FUNCTION(Heap::AllocateMap(type, instance_size), Map);
240}
241
242
243Handle<JSObject> Factory::NewFunctionPrototype(Handle<JSFunction> function) {
244 CALL_HEAP_FUNCTION(Heap::AllocateFunctionPrototype(*function), JSObject);
245}
246
247
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000248Handle<Map> Factory::CopyMapDropDescriptors(Handle<Map> src) {
249 CALL_HEAP_FUNCTION(src->CopyDropDescriptors(), Map);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000250}
251
252
ager@chromium.org32912102009-01-16 10:38:43 +0000253Handle<Map> Factory::CopyMap(Handle<Map> src,
254 int extra_inobject_properties) {
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000255 Handle<Map> copy = CopyMapDropDescriptors(src);
ager@chromium.org32912102009-01-16 10:38:43 +0000256 // Check that we do not overflow the instance size when adding the
257 // extra inobject properties.
258 int instance_size_delta = extra_inobject_properties * kPointerSize;
259 int max_instance_size_delta =
260 JSObject::kMaxInstanceSize - copy->instance_size();
261 if (instance_size_delta > max_instance_size_delta) {
262 // If the instance size overflows, we allocate as many properties
263 // as we can as inobject properties.
264 instance_size_delta = max_instance_size_delta;
265 extra_inobject_properties = max_instance_size_delta >> kPointerSizeLog2;
266 }
267 // Adjust the map with the extra inobject properties.
268 int inobject_properties =
269 copy->inobject_properties() + extra_inobject_properties;
270 copy->set_inobject_properties(inobject_properties);
271 copy->set_unused_property_fields(inobject_properties);
272 copy->set_instance_size(copy->instance_size() + instance_size_delta);
273 return copy;
274}
275
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000276Handle<Map> Factory::CopyMapDropTransitions(Handle<Map> src) {
277 CALL_HEAP_FUNCTION(src->CopyDropTransitions(), Map);
278}
279
280
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000281Handle<FixedArray> Factory::CopyFixedArray(Handle<FixedArray> array) {
282 CALL_HEAP_FUNCTION(array->Copy(), FixedArray);
283}
284
285
286Handle<JSFunction> Factory::BaseNewFunctionFromBoilerplate(
287 Handle<JSFunction> boilerplate,
288 Handle<Map> function_map) {
289 ASSERT(boilerplate->IsBoilerplate());
290 ASSERT(!boilerplate->has_initial_map());
291 ASSERT(!boilerplate->has_prototype());
292 ASSERT(boilerplate->properties() == Heap::empty_fixed_array());
293 ASSERT(boilerplate->elements() == Heap::empty_fixed_array());
294 CALL_HEAP_FUNCTION(Heap::AllocateFunction(*function_map,
295 boilerplate->shared(),
296 Heap::the_hole_value()),
297 JSFunction);
298}
299
300
301Handle<JSFunction> Factory::NewFunctionFromBoilerplate(
302 Handle<JSFunction> boilerplate,
303 Handle<Context> context) {
304 Handle<JSFunction> result =
305 BaseNewFunctionFromBoilerplate(boilerplate, Top::function_map());
306 result->set_context(*context);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000307 int number_of_literals = boilerplate->NumberOfLiterals();
308 Handle<FixedArray> literals =
309 Factory::NewFixedArray(number_of_literals, TENURED);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000310 if (number_of_literals > 0) {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000311 // Store the object, regexp and array functions in the literals
312 // array prefix. These functions will be used when creating
313 // object, regexp and array literals in this function.
ager@chromium.org236ad962008-09-25 09:45:57 +0000314 literals->set(JSFunction::kLiteralGlobalContextIndex,
315 context->global_context());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000316 }
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000317 result->set_literals(*literals);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000318 ASSERT(!result->IsBoilerplate());
319 return result;
320}
321
322
323Handle<Object> Factory::NewNumber(double value,
324 PretenureFlag pretenure) {
325 CALL_HEAP_FUNCTION(Heap::NumberFromDouble(value, pretenure), Object);
326}
327
328
329Handle<Object> Factory::NewNumberFromInt(int value) {
330 CALL_HEAP_FUNCTION(Heap::NumberFromInt32(value), Object);
331}
332
333
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000334Handle<Object> Factory::NewNumberFromUint(uint32_t value) {
335 CALL_HEAP_FUNCTION(Heap::NumberFromUint32(value), Object);
336}
337
338
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000339Handle<JSObject> Factory::NewNeanderObject() {
340 CALL_HEAP_FUNCTION(Heap::AllocateJSObjectFromMap(Heap::neander_map()),
341 JSObject);
342}
343
344
345Handle<Object> Factory::NewTypeError(const char* type,
346 Vector< Handle<Object> > args) {
347 return NewError("MakeTypeError", type, args);
348}
349
350
351Handle<Object> Factory::NewTypeError(Handle<String> message) {
352 return NewError("$TypeError", message);
353}
354
355
356Handle<Object> Factory::NewRangeError(const char* type,
357 Vector< Handle<Object> > args) {
358 return NewError("MakeRangeError", type, args);
359}
360
361
362Handle<Object> Factory::NewRangeError(Handle<String> message) {
363 return NewError("$RangeError", message);
364}
365
366
367Handle<Object> Factory::NewSyntaxError(const char* type, Handle<JSArray> args) {
368 return NewError("MakeSyntaxError", type, args);
369}
370
371
372Handle<Object> Factory::NewSyntaxError(Handle<String> message) {
373 return NewError("$SyntaxError", message);
374}
375
376
377Handle<Object> Factory::NewReferenceError(const char* type,
378 Vector< Handle<Object> > args) {
379 return NewError("MakeReferenceError", type, args);
380}
381
382
383Handle<Object> Factory::NewReferenceError(Handle<String> message) {
384 return NewError("$ReferenceError", message);
385}
386
387
388Handle<Object> Factory::NewError(const char* maker, const char* type,
389 Vector< Handle<Object> > args) {
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000390 v8::HandleScope scope; // Instantiate a closeable HandleScope for EscapeFrom.
ager@chromium.org7c537e22008-10-16 08:43:32 +0000391 Handle<FixedArray> array = Factory::NewFixedArray(args.length());
392 for (int i = 0; i < args.length(); i++) {
393 array->set(i, *args[i]);
394 }
395 Handle<JSArray> object = Factory::NewJSArrayWithElements(array);
396 Handle<Object> result = NewError(maker, type, object);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000397 return result.EscapeFrom(&scope);
398}
399
400
401Handle<Object> Factory::NewEvalError(const char* type,
402 Vector< Handle<Object> > args) {
403 return NewError("MakeEvalError", type, args);
404}
405
406
407Handle<Object> Factory::NewError(const char* type,
408 Vector< Handle<Object> > args) {
409 return NewError("MakeError", type, args);
410}
411
412
413Handle<Object> Factory::NewError(const char* maker,
414 const char* type,
415 Handle<JSArray> args) {
416 Handle<String> make_str = Factory::LookupAsciiSymbol(maker);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000417 Handle<Object> fun_obj(Top::builtins()->GetProperty(*make_str));
418 // If the builtins haven't been properly configured yet this error
419 // constructor may not have been defined. Bail out.
420 if (!fun_obj->IsJSFunction())
421 return Factory::undefined_value();
422 Handle<JSFunction> fun = Handle<JSFunction>::cast(fun_obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000423 Handle<Object> type_obj = Factory::LookupAsciiSymbol(type);
424 Object** argv[2] = { type_obj.location(),
425 Handle<Object>::cast(args).location() };
426
427 // Invoke the JavaScript factory method. If an exception is thrown while
428 // running the factory method, use the exception as the result.
429 bool caught_exception;
430 Handle<Object> result = Execution::TryCall(fun,
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000431 Top::builtins(),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000432 2,
433 argv,
434 &caught_exception);
435 return result;
436}
437
438
439Handle<Object> Factory::NewError(Handle<String> message) {
440 return NewError("$Error", message);
441}
442
443
444Handle<Object> Factory::NewError(const char* constructor,
445 Handle<String> message) {
446 Handle<String> constr = Factory::LookupAsciiSymbol(constructor);
447 Handle<JSFunction> fun =
448 Handle<JSFunction>(
449 JSFunction::cast(
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000450 Top::builtins()->GetProperty(*constr)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000451 Object** argv[1] = { Handle<Object>::cast(message).location() };
452
453 // Invoke the JavaScript factory method. If an exception is thrown while
454 // running the factory method, use the exception as the result.
455 bool caught_exception;
456 Handle<Object> result = Execution::TryCall(fun,
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000457 Top::builtins(),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000458 1,
459 argv,
460 &caught_exception);
461 return result;
462}
463
464
465Handle<JSFunction> Factory::NewFunction(Handle<String> name,
466 InstanceType type,
467 int instance_size,
468 Handle<Code> code,
469 bool force_initial_map) {
470 // Allocate the function
471 Handle<JSFunction> function = NewFunction(name, the_hole_value());
472 function->set_code(*code);
473
474 if (force_initial_map ||
475 type != JS_OBJECT_TYPE ||
476 instance_size != JSObject::kHeaderSize) {
477 Handle<Map> initial_map = NewMap(type, instance_size);
478 Handle<JSObject> prototype = NewFunctionPrototype(function);
479 initial_map->set_prototype(*prototype);
480 function->set_initial_map(*initial_map);
481 initial_map->set_constructor(*function);
482 } else {
483 ASSERT(!function->has_initial_map());
484 ASSERT(!function->has_prototype());
485 }
486
487 return function;
488}
489
490
491Handle<JSFunction> Factory::NewFunctionBoilerplate(Handle<String> name,
492 int number_of_literals,
493 Handle<Code> code) {
494 Handle<JSFunction> function = NewFunctionBoilerplate(name);
495 function->set_code(*code);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000496 int literals_array_size = number_of_literals;
497 // If the function contains object, regexp or array literals,
498 // allocate extra space for a literals array prefix containing the
499 // object, regexp and array constructor functions.
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000500 if (number_of_literals > 0) {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000501 literals_array_size += JSFunction::kLiteralsPrefixSize;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000502 }
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000503 Handle<FixedArray> literals =
504 Factory::NewFixedArray(literals_array_size, TENURED);
505 function->set_literals(*literals);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000506 ASSERT(!function->has_initial_map());
507 ASSERT(!function->has_prototype());
508 return function;
509}
510
511
512Handle<JSFunction> Factory::NewFunctionBoilerplate(Handle<String> name) {
513 Handle<SharedFunctionInfo> shared = NewSharedFunctionInfo(name);
514 CALL_HEAP_FUNCTION(Heap::AllocateFunction(Heap::boilerplate_function_map(),
515 *shared,
516 Heap::the_hole_value()),
517 JSFunction);
518}
519
520
521Handle<JSFunction> Factory::NewFunctionWithPrototype(Handle<String> name,
522 InstanceType type,
523 int instance_size,
524 Handle<JSObject> prototype,
525 Handle<Code> code,
526 bool force_initial_map) {
527 // Allocate the function
528 Handle<JSFunction> function = NewFunction(name, prototype);
529
530 function->set_code(*code);
531
532 if (force_initial_map ||
533 type != JS_OBJECT_TYPE ||
534 instance_size != JSObject::kHeaderSize) {
535 Handle<Map> initial_map = NewMap(type, instance_size);
536 function->set_initial_map(*initial_map);
537 initial_map->set_constructor(*function);
538 }
539
540 // Set function.prototype and give the prototype a constructor
541 // property that refers to the function.
542 SetPrototypeProperty(function, prototype);
543 SetProperty(prototype, Factory::constructor_symbol(), function, DONT_ENUM);
544 return function;
545}
546
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000547
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000548Handle<Code> Factory::NewCode(const CodeDesc& desc,
549 ZoneScopeInfo* sinfo,
550 Code::Flags flags,
551 Handle<Object> self_ref) {
kasperl@chromium.org061ef742009-02-27 12:16:20 +0000552 CALL_HEAP_FUNCTION(Heap::CreateCode(desc, sinfo, flags, self_ref), Code);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000553}
554
555
556Handle<Code> Factory::CopyCode(Handle<Code> code) {
557 CALL_HEAP_FUNCTION(Heap::CopyCode(*code), Code);
558}
559
560
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +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}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000569
570
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000571// Allocate the new array.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000572Handle<DescriptorArray> Factory::CopyAppendProxyDescriptor(
573 Handle<DescriptorArray> array,
574 Handle<String> key,
575 Handle<Object> value,
576 PropertyAttributes attributes) {
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000577 CALL_HEAP_FUNCTION(DoCopyInsert(*array, *key, *value, attributes),
578 DescriptorArray);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000579}
580
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000581
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.
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000599 for (int i = 0; i < array->number_of_descriptors(); i++) {
600 if (array->GetType(i) != NULL_DESCRIPTOR) {
601 result->CopyFrom(descriptor_count++, *array, i);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000602 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000603 }
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.
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000618 if (result->LinearSearch(*key, descriptor_count) ==
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000619 DescriptorArray::kNotFound) {
620 CallbacksDescriptor desc(*key, *entry, entry->property_attributes());
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000621 result->Set(descriptor_count, &desc);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000622 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) {
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000631 int number_of_descriptors = result->number_of_descriptors() - duplicates;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000632 Handle<DescriptorArray> new_result =
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000633 NewDescriptorArray(number_of_descriptors);
634 for (int i = 0; i < number_of_descriptors; i++) {
635 new_result->CopyFrom(i, *result, i);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000636 }
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
kasperl@chromium.org2abc4502009-07-02 07:00:29 +0000652Handle<GlobalObject> Factory::NewGlobalObject(
653 Handle<JSFunction> constructor) {
654 CALL_HEAP_FUNCTION(Heap::AllocateGlobalObject(*constructor),
655 GlobalObject);
656}
657
658
659
ager@chromium.org236ad962008-09-25 09:45:57 +0000660Handle<JSObject> Factory::NewJSObjectFromMap(Handle<Map> map) {
661 CALL_HEAP_FUNCTION(Heap::AllocateJSObjectFromMap(*map, NOT_TENURED),
662 JSObject);
663}
664
665
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000666Handle<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
682Handle<SharedFunctionInfo> Factory::NewSharedFunctionInfo(Handle<String> name) {
683 CALL_HEAP_FUNCTION(Heap::AllocateSharedFunctionInfo(*name),
684 SharedFunctionInfo);
685}
686
687
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000688Handle<String> Factory::NumberToString(Handle<Object> number) {
689 CALL_HEAP_FUNCTION(Heap::NumberToString(*number), String);
690}
691
692
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000693Handle<NumberDictionary> Factory::DictionaryAtNumberPut(
694 Handle<NumberDictionary> dictionary,
695 uint32_t key,
696 Handle<Object> value) {
697 CALL_HEAP_FUNCTION(dictionary->AtNumberPut(key, *value), NumberDictionary);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000698}
699
700
701Handle<JSFunction> Factory::NewFunctionHelper(Handle<String> name,
702 Handle<Object> prototype) {
703 Handle<SharedFunctionInfo> function_share = NewSharedFunctionInfo(name);
704 CALL_HEAP_FUNCTION(Heap::AllocateFunction(*Top::function_map(),
705 *function_share,
706 *prototype),
707 JSFunction);
708}
709
710
711Handle<JSFunction> Factory::NewFunction(Handle<String> name,
712 Handle<Object> prototype) {
713 Handle<JSFunction> fun = NewFunctionHelper(name, prototype);
714 fun->set_context(Top::context()->global_context());
715 return fun;
716}
717
718
719Handle<Object> Factory::ToObject(Handle<Object> object,
720 Handle<Context> global_context) {
721 CALL_HEAP_FUNCTION(object->ToObject(*global_context), Object);
722}
723
724
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000725#ifdef ENABLE_DEBUGGER_SUPPORT
v8.team.kasperl727e9952008-09-02 14:56:44 +0000726Handle<DebugInfo> Factory::NewDebugInfo(Handle<SharedFunctionInfo> shared) {
727 // Get the original code of the function.
728 Handle<Code> code(shared->code());
729
730 // Create a copy of the code before allocating the debug info object to avoid
731 // allocation while setting up the debug info object.
732 Handle<Code> original_code(*Factory::CopyCode(code));
733
734 // Allocate initial fixed array for active break points before allocating the
735 // debug info object to avoid allocation while setting up the debug info
736 // object.
737 Handle<FixedArray> break_points(
738 Factory::NewFixedArray(Debug::kEstimatedNofBreakPointsInFunction));
739
740 // Create and set up the debug info object. Debug info contains function, a
741 // copy of the original code, the executing code and initial fixed array for
742 // active break points.
743 Handle<DebugInfo> debug_info =
744 Handle<DebugInfo>::cast(Factory::NewStruct(DEBUG_INFO_TYPE));
745 debug_info->set_shared(*shared);
746 debug_info->set_original_code(*original_code);
747 debug_info->set_code(*code);
748 debug_info->set_break_points(*break_points);
749
750 // Link debug info to function.
751 shared->set_debug_info(*debug_info);
752
753 return debug_info;
754}
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000755#endif
v8.team.kasperl727e9952008-09-02 14:56:44 +0000756
757
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000758Handle<JSObject> Factory::NewArgumentsObject(Handle<Object> callee,
759 int length) {
760 CALL_HEAP_FUNCTION(Heap::AllocateArgumentsObject(*callee, length), JSObject);
761}
762
763
764Handle<JSFunction> Factory::CreateApiFunction(
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000765 Handle<FunctionTemplateInfo> obj, ApiInstanceType instance_type) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000766 Handle<Code> code = Handle<Code>(Builtins::builtin(Builtins::HandleApiCall));
767
kasper.lund212ac232008-07-16 07:07:30 +0000768 int internal_field_count = 0;
769 if (!obj->instance_template()->IsUndefined()) {
770 Handle<ObjectTemplateInfo> instance_template =
771 Handle<ObjectTemplateInfo>(
772 ObjectTemplateInfo::cast(obj->instance_template()));
773 internal_field_count =
774 Smi::cast(instance_template->internal_field_count())->value();
775 }
776
777 int instance_size = kPointerSize * internal_field_count;
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000778 InstanceType type = INVALID_TYPE;
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000779 switch (instance_type) {
780 case JavaScriptObject:
781 type = JS_OBJECT_TYPE;
782 instance_size += JSObject::kHeaderSize;
783 break;
784 case InnerGlobalObject:
785 type = JS_GLOBAL_OBJECT_TYPE;
786 instance_size += JSGlobalObject::kSize;
787 break;
788 case OuterGlobalObject:
789 type = JS_GLOBAL_PROXY_TYPE;
790 instance_size += JSGlobalProxy::kSize;
791 break;
792 default:
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000793 break;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000794 }
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000795 ASSERT(type != INVALID_TYPE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000796
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000797 Handle<JSFunction> result =
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000798 Factory::NewFunction(Factory::empty_symbol(),
799 type,
800 instance_size,
801 code,
802 true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000803 // Set class name.
804 Handle<Object> class_name = Handle<Object>(obj->class_name());
805 if (class_name->IsString()) {
806 result->shared()->set_instance_class_name(*class_name);
807 result->shared()->set_name(*class_name);
808 }
809
810 Handle<Map> map = Handle<Map>(result->initial_map());
811
812 // Mark as undetectable if needed.
813 if (obj->undetectable()) {
814 map->set_is_undetectable();
815 }
816
817 // Mark as hidden for the __proto__ accessor if needed.
818 if (obj->hidden_prototype()) {
819 map->set_is_hidden_prototype();
820 }
821
822 // Mark as needs_access_check if needed.
823 if (obj->needs_access_check()) {
ager@chromium.org870a0b62008-11-04 11:43:05 +0000824 map->set_is_access_check_needed(true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000825 }
826
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000827 // Set interceptor information in the map.
828 if (!obj->named_property_handler()->IsUndefined()) {
829 map->set_has_named_interceptor();
830 }
831 if (!obj->indexed_property_handler()->IsUndefined()) {
832 map->set_has_indexed_interceptor();
833 }
834
835 // Set instance call-as-function information in the map.
836 if (!obj->instance_call_handler()->IsUndefined()) {
837 map->set_has_instance_call_handler();
838 }
839
840 result->shared()->set_function_data(*obj);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000841 result->shared()->DontAdaptArguments();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000842
843 // Recursively copy parent templates' accessors, 'data' may be modified.
844 Handle<DescriptorArray> array =
845 Handle<DescriptorArray>(map->instance_descriptors());
846 while (true) {
847 Handle<Object> props = Handle<Object>(obj->property_accessors());
848 if (!props->IsUndefined()) {
849 array = Factory::CopyAppendCallbackDescriptors(array, props);
850 }
851 Handle<Object> parent = Handle<Object>(obj->parent_template());
852 if (parent->IsUndefined()) break;
853 obj = Handle<FunctionTemplateInfo>::cast(parent);
854 }
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000855 if (!array->IsEmpty()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000856 map->set_instance_descriptors(*array);
857 }
858
859 return result;
860}
861
862
ager@chromium.org236ad962008-09-25 09:45:57 +0000863Handle<MapCache> Factory::NewMapCache(int at_least_space_for) {
864 CALL_HEAP_FUNCTION(MapCache::Allocate(at_least_space_for), MapCache);
865}
866
867
868static Object* UpdateMapCacheWith(Context* context,
869 FixedArray* keys,
870 Map* map) {
871 Object* result = MapCache::cast(context->map_cache())->Put(keys, map);
872 if (!result->IsFailure()) context->set_map_cache(MapCache::cast(result));
873 return result;
874}
875
876
877Handle<MapCache> Factory::AddToMapCache(Handle<Context> context,
878 Handle<FixedArray> keys,
879 Handle<Map> map) {
880 CALL_HEAP_FUNCTION(UpdateMapCacheWith(*context, *keys, *map), MapCache);
881}
882
883
884Handle<Map> Factory::ObjectLiteralMapFromCache(Handle<Context> context,
885 Handle<FixedArray> keys) {
886 if (context->map_cache()->IsUndefined()) {
887 // Allocate the new map cache for the global context.
888 Handle<MapCache> new_cache = NewMapCache(24);
889 context->set_map_cache(*new_cache);
890 }
ager@chromium.org32912102009-01-16 10:38:43 +0000891 // Check to see whether there is a matching element in the cache.
ager@chromium.org236ad962008-09-25 09:45:57 +0000892 Handle<MapCache> cache =
893 Handle<MapCache>(MapCache::cast(context->map_cache()));
894 Handle<Object> result = Handle<Object>(cache->Lookup(*keys));
895 if (result->IsMap()) return Handle<Map>::cast(result);
896 // Create a new map and add it to the cache.
897 Handle<Map> map =
ager@chromium.org32912102009-01-16 10:38:43 +0000898 CopyMap(Handle<Map>(context->object_function()->initial_map()),
899 keys->length());
ager@chromium.org236ad962008-09-25 09:45:57 +0000900 AddToMapCache(context, keys, map);
901 return Handle<Map>(map);
902}
903
904
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000905void Factory::SetRegExpAtomData(Handle<JSRegExp> regexp,
906 JSRegExp::Type type,
907 Handle<String> source,
908 JSRegExp::Flags flags,
909 Handle<Object> data) {
910 Handle<FixedArray> store = NewFixedArray(JSRegExp::kAtomDataSize);
911
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000912 store->set(JSRegExp::kTagIndex, Smi::FromInt(type));
913 store->set(JSRegExp::kSourceIndex, *source);
914 store->set(JSRegExp::kFlagsIndex, Smi::FromInt(flags.value()));
915 store->set(JSRegExp::kAtomPatternIndex, *data);
916 regexp->set_data(*store);
917}
918
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000919void Factory::SetRegExpIrregexpData(Handle<JSRegExp> regexp,
920 JSRegExp::Type type,
921 Handle<String> source,
922 JSRegExp::Flags flags,
923 int capture_count) {
924 Handle<FixedArray> store = NewFixedArray(JSRegExp::kIrregexpDataSize);
925
926 store->set(JSRegExp::kTagIndex, Smi::FromInt(type));
927 store->set(JSRegExp::kSourceIndex, *source);
928 store->set(JSRegExp::kFlagsIndex, Smi::FromInt(flags.value()));
929 store->set(JSRegExp::kIrregexpASCIICodeIndex, Heap::the_hole_value());
930 store->set(JSRegExp::kIrregexpUC16CodeIndex, Heap::the_hole_value());
931 store->set(JSRegExp::kIrregexpMaxRegisterCountIndex, Smi::FromInt(0));
932 store->set(JSRegExp::kIrregexpCaptureCountIndex,
933 Smi::FromInt(capture_count));
934 regexp->set_data(*store);
935}
936
937
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000938
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000939void Factory::ConfigureInstance(Handle<FunctionTemplateInfo> desc,
940 Handle<JSObject> instance,
941 bool* pending_exception) {
942 // Configure the instance by adding the properties specified by the
943 // instance template.
944 Handle<Object> instance_template = Handle<Object>(desc->instance_template());
945 if (!instance_template->IsUndefined()) {
946 Execution::ConfigureInstance(instance,
947 instance_template,
948 pending_exception);
949 } else {
950 *pending_exception = false;
951 }
952}
953
954
955} } // namespace v8::internal