blob: 4b0b7f51f7ea5463650ed17a72fffc4ec6b2d778 [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
36namespace v8 { namespace internal {
37
38
39Handle<FixedArray> Factory::NewFixedArray(int size, PretenureFlag pretenure) {
40 ASSERT(0 <= size);
41 CALL_HEAP_FUNCTION(Heap::AllocateFixedArray(size, pretenure), FixedArray);
42}
43
44
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +000045Handle<FixedArray> Factory::NewFixedArrayWithHoles(int size) {
46 ASSERT(0 <= size);
47 CALL_HEAP_FUNCTION(Heap::AllocateFixedArrayWithHoles(size), FixedArray);
48}
49
50
51Handle<Dictionary> Factory::NewDictionary(int at_least_space_for) {
52 ASSERT(0 <= at_least_space_for);
53 CALL_HEAP_FUNCTION(Dictionary::Allocate(at_least_space_for), Dictionary);
54}
55
56
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000057Handle<DescriptorArray> Factory::NewDescriptorArray(int number_of_descriptors) {
58 ASSERT(0 <= number_of_descriptors);
59 CALL_HEAP_FUNCTION(DescriptorArray::Allocate(number_of_descriptors),
60 DescriptorArray);
61}
62
63
ager@chromium.org9258b6b2008-09-11 09:11:10 +000064// Symbols are created in the old generation (data space).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000065Handle<String> Factory::LookupSymbol(Vector<const char> string) {
66 CALL_HEAP_FUNCTION(Heap::LookupSymbol(string), String);
67}
68
69
70Handle<String> Factory::NewStringFromAscii(Vector<const char> string,
71 PretenureFlag pretenure) {
72 CALL_HEAP_FUNCTION(Heap::AllocateStringFromAscii(string, pretenure), String);
73}
74
75Handle<String> Factory::NewStringFromUtf8(Vector<const char> string,
76 PretenureFlag pretenure) {
77 CALL_HEAP_FUNCTION(Heap::AllocateStringFromUtf8(string, pretenure), String);
78}
79
80
81Handle<String> Factory::NewStringFromTwoByte(Vector<const uc16> string) {
82 CALL_HEAP_FUNCTION(Heap::AllocateStringFromTwoByte(string), String);
83}
84
85
86Handle<String> Factory::NewRawTwoByteString(int length,
87 PretenureFlag pretenure) {
88 CALL_HEAP_FUNCTION(Heap::AllocateRawTwoByteString(length, pretenure), String);
89}
90
91
92Handle<String> Factory::NewConsString(Handle<String> first,
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000093 Handle<String> second) {
94 if (first->length() == 0) return second;
95 if (second->length() == 0) return first;
ager@chromium.orgc3e50d82008-11-05 11:53:10 +000096 CALL_HEAP_FUNCTION(Heap::AllocateConsString(*first, *second), String);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000097}
98
99
ager@chromium.org870a0b62008-11-04 11:43:05 +0000100Handle<String> Factory::NewStringSlice(Handle<String> str,
ager@chromium.org870a0b62008-11-04 11:43:05 +0000101 int begin,
102 int end) {
ager@chromium.orgc3e50d82008-11-05 11:53:10 +0000103 CALL_HEAP_FUNCTION(str->Slice(begin, end), String);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000104}
105
106
107Handle<String> Factory::NewExternalStringFromAscii(
108 ExternalAsciiString::Resource* resource) {
109 CALL_HEAP_FUNCTION(Heap::AllocateExternalStringFromAscii(resource), String);
110}
111
112
113Handle<String> Factory::NewExternalStringFromTwoByte(
114 ExternalTwoByteString::Resource* resource) {
115 CALL_HEAP_FUNCTION(Heap::AllocateExternalStringFromTwoByte(resource), String);
116}
117
118
119Handle<Context> Factory::NewGlobalContext() {
120 CALL_HEAP_FUNCTION(Heap::AllocateGlobalContext(), Context);
121}
122
123
124Handle<Context> Factory::NewFunctionContext(int length,
125 Handle<JSFunction> closure) {
126 CALL_HEAP_FUNCTION(Heap::AllocateFunctionContext(length, *closure), Context);
127}
128
129
130Handle<Context> Factory::NewWithContext(Handle<Context> previous,
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000131 Handle<JSObject> extension,
132 bool is_catch_context) {
133 CALL_HEAP_FUNCTION(Heap::AllocateWithContext(*previous,
134 *extension,
135 is_catch_context),
136 Context);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000137}
138
139
140Handle<Struct> Factory::NewStruct(InstanceType type) {
141 CALL_HEAP_FUNCTION(Heap::AllocateStruct(type), Struct);
142}
143
144
145Handle<AccessorInfo> Factory::NewAccessorInfo() {
146 Handle<AccessorInfo> info =
147 Handle<AccessorInfo>::cast(NewStruct(ACCESSOR_INFO_TYPE));
148 info->set_flag(0); // Must clear the flag, it was initialized as undefined.
149 return info;
150}
151
152
153Handle<Script> Factory::NewScript(Handle<String> source) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000154 // Generate id for this script.
155 int id;
156 if (Heap::last_script_id()->IsUndefined()) {
157 // Script ids start from one.
158 id = 1;
159 } else {
160 // Increment id, wrap when positive smi is exhausted.
161 id = Smi::cast(Heap::last_script_id())->value();
162 id++;
163 if (!Smi::IsValid(id)) {
164 id = 0;
165 }
166 }
167 Heap::SetLastScriptId(Smi::FromInt(id));
168
169 // Create and initialize script object.
ager@chromium.org9085a012009-05-11 19:22:57 +0000170 Handle<Proxy> wrapper = Factory::NewProxy(0, TENURED);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000171 Handle<Script> script = Handle<Script>::cast(NewStruct(SCRIPT_TYPE));
172 script->set_source(*source);
173 script->set_name(Heap::undefined_value());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000174 script->set_id(Heap::last_script_id());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000175 script->set_line_offset(Smi::FromInt(0));
176 script->set_column_offset(Smi::FromInt(0));
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000177 script->set_data(Heap::undefined_value());
ager@chromium.org9085a012009-05-11 19:22:57 +0000178 script->set_context_data(Heap::undefined_value());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000179 script->set_type(Smi::FromInt(SCRIPT_TYPE_NORMAL));
ager@chromium.org9085a012009-05-11 19:22:57 +0000180 script->set_wrapper(*wrapper);
iposva@chromium.org245aa852009-02-10 00:49:54 +0000181 script->set_line_ends(Heap::undefined_value());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000182
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000183 return script;
184}
185
186
187Handle<Proxy> Factory::NewProxy(Address addr, PretenureFlag pretenure) {
188 CALL_HEAP_FUNCTION(Heap::AllocateProxy(addr, pretenure), Proxy);
189}
190
191
192Handle<Proxy> Factory::NewProxy(const AccessorDescriptor* desc) {
193 return NewProxy((Address) desc, TENURED);
194}
195
196
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000197Handle<ByteArray> Factory::NewByteArray(int length, PretenureFlag pretenure) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000198 ASSERT(0 <= length);
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000199 CALL_HEAP_FUNCTION(Heap::AllocateByteArray(length, pretenure), ByteArray);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000200}
201
202
203Handle<Map> Factory::NewMap(InstanceType type, int instance_size) {
204 CALL_HEAP_FUNCTION(Heap::AllocateMap(type, instance_size), Map);
205}
206
207
208Handle<JSObject> Factory::NewFunctionPrototype(Handle<JSFunction> function) {
209 CALL_HEAP_FUNCTION(Heap::AllocateFunctionPrototype(*function), JSObject);
210}
211
212
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000213Handle<Map> Factory::CopyMapDropDescriptors(Handle<Map> src) {
214 CALL_HEAP_FUNCTION(src->CopyDropDescriptors(), Map);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000215}
216
217
ager@chromium.org32912102009-01-16 10:38:43 +0000218Handle<Map> Factory::CopyMap(Handle<Map> src,
219 int extra_inobject_properties) {
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000220 Handle<Map> copy = CopyMapDropDescriptors(src);
ager@chromium.org32912102009-01-16 10:38:43 +0000221 // Check that we do not overflow the instance size when adding the
222 // extra inobject properties.
223 int instance_size_delta = extra_inobject_properties * kPointerSize;
224 int max_instance_size_delta =
225 JSObject::kMaxInstanceSize - copy->instance_size();
226 if (instance_size_delta > max_instance_size_delta) {
227 // If the instance size overflows, we allocate as many properties
228 // as we can as inobject properties.
229 instance_size_delta = max_instance_size_delta;
230 extra_inobject_properties = max_instance_size_delta >> kPointerSizeLog2;
231 }
232 // Adjust the map with the extra inobject properties.
233 int inobject_properties =
234 copy->inobject_properties() + extra_inobject_properties;
235 copy->set_inobject_properties(inobject_properties);
236 copy->set_unused_property_fields(inobject_properties);
237 copy->set_instance_size(copy->instance_size() + instance_size_delta);
238 return copy;
239}
240
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000241Handle<Map> Factory::CopyMapDropTransitions(Handle<Map> src) {
242 CALL_HEAP_FUNCTION(src->CopyDropTransitions(), Map);
243}
244
245
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000246Handle<FixedArray> Factory::CopyFixedArray(Handle<FixedArray> array) {
247 CALL_HEAP_FUNCTION(array->Copy(), FixedArray);
248}
249
250
251Handle<JSFunction> Factory::BaseNewFunctionFromBoilerplate(
252 Handle<JSFunction> boilerplate,
253 Handle<Map> function_map) {
254 ASSERT(boilerplate->IsBoilerplate());
255 ASSERT(!boilerplate->has_initial_map());
256 ASSERT(!boilerplate->has_prototype());
257 ASSERT(boilerplate->properties() == Heap::empty_fixed_array());
258 ASSERT(boilerplate->elements() == Heap::empty_fixed_array());
259 CALL_HEAP_FUNCTION(Heap::AllocateFunction(*function_map,
260 boilerplate->shared(),
261 Heap::the_hole_value()),
262 JSFunction);
263}
264
265
266Handle<JSFunction> Factory::NewFunctionFromBoilerplate(
267 Handle<JSFunction> boilerplate,
268 Handle<Context> context) {
269 Handle<JSFunction> result =
270 BaseNewFunctionFromBoilerplate(boilerplate, Top::function_map());
271 result->set_context(*context);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000272 int number_of_literals = boilerplate->NumberOfLiterals();
273 Handle<FixedArray> literals =
274 Factory::NewFixedArray(number_of_literals, TENURED);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000275 if (number_of_literals > 0) {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000276 // Store the object, regexp and array functions in the literals
277 // array prefix. These functions will be used when creating
278 // object, regexp and array literals in this function.
ager@chromium.org236ad962008-09-25 09:45:57 +0000279 literals->set(JSFunction::kLiteralGlobalContextIndex,
280 context->global_context());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000281 }
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000282 result->set_literals(*literals);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000283 ASSERT(!result->IsBoilerplate());
284 return result;
285}
286
287
288Handle<Object> Factory::NewNumber(double value,
289 PretenureFlag pretenure) {
290 CALL_HEAP_FUNCTION(Heap::NumberFromDouble(value, pretenure), Object);
291}
292
293
294Handle<Object> Factory::NewNumberFromInt(int value) {
295 CALL_HEAP_FUNCTION(Heap::NumberFromInt32(value), Object);
296}
297
298
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000299Handle<Object> Factory::NewNumberFromUint(uint32_t value) {
300 CALL_HEAP_FUNCTION(Heap::NumberFromUint32(value), Object);
301}
302
303
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000304Handle<JSObject> Factory::NewNeanderObject() {
305 CALL_HEAP_FUNCTION(Heap::AllocateJSObjectFromMap(Heap::neander_map()),
306 JSObject);
307}
308
309
310Handle<Object> Factory::NewTypeError(const char* type,
311 Vector< Handle<Object> > args) {
312 return NewError("MakeTypeError", type, args);
313}
314
315
316Handle<Object> Factory::NewTypeError(Handle<String> message) {
317 return NewError("$TypeError", message);
318}
319
320
321Handle<Object> Factory::NewRangeError(const char* type,
322 Vector< Handle<Object> > args) {
323 return NewError("MakeRangeError", type, args);
324}
325
326
327Handle<Object> Factory::NewRangeError(Handle<String> message) {
328 return NewError("$RangeError", message);
329}
330
331
332Handle<Object> Factory::NewSyntaxError(const char* type, Handle<JSArray> args) {
333 return NewError("MakeSyntaxError", type, args);
334}
335
336
337Handle<Object> Factory::NewSyntaxError(Handle<String> message) {
338 return NewError("$SyntaxError", message);
339}
340
341
342Handle<Object> Factory::NewReferenceError(const char* type,
343 Vector< Handle<Object> > args) {
344 return NewError("MakeReferenceError", type, args);
345}
346
347
348Handle<Object> Factory::NewReferenceError(Handle<String> message) {
349 return NewError("$ReferenceError", message);
350}
351
352
353Handle<Object> Factory::NewError(const char* maker, const char* type,
354 Vector< Handle<Object> > args) {
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000355 v8::HandleScope scope; // Instantiate a closeable HandleScope for EscapeFrom.
ager@chromium.org7c537e22008-10-16 08:43:32 +0000356 Handle<FixedArray> array = Factory::NewFixedArray(args.length());
357 for (int i = 0; i < args.length(); i++) {
358 array->set(i, *args[i]);
359 }
360 Handle<JSArray> object = Factory::NewJSArrayWithElements(array);
361 Handle<Object> result = NewError(maker, type, object);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000362 return result.EscapeFrom(&scope);
363}
364
365
366Handle<Object> Factory::NewEvalError(const char* type,
367 Vector< Handle<Object> > args) {
368 return NewError("MakeEvalError", type, args);
369}
370
371
372Handle<Object> Factory::NewError(const char* type,
373 Vector< Handle<Object> > args) {
374 return NewError("MakeError", type, args);
375}
376
377
378Handle<Object> Factory::NewError(const char* maker,
379 const char* type,
380 Handle<JSArray> args) {
381 Handle<String> make_str = Factory::LookupAsciiSymbol(maker);
382 Handle<JSFunction> fun =
383 Handle<JSFunction>(
384 JSFunction::cast(
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000385 Top::builtins()->GetProperty(*make_str)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000386 Handle<Object> type_obj = Factory::LookupAsciiSymbol(type);
387 Object** argv[2] = { type_obj.location(),
388 Handle<Object>::cast(args).location() };
389
390 // Invoke the JavaScript factory method. If an exception is thrown while
391 // running the factory method, use the exception as the result.
392 bool caught_exception;
393 Handle<Object> result = Execution::TryCall(fun,
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000394 Top::builtins(),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000395 2,
396 argv,
397 &caught_exception);
398 return result;
399}
400
401
402Handle<Object> Factory::NewError(Handle<String> message) {
403 return NewError("$Error", message);
404}
405
406
407Handle<Object> Factory::NewError(const char* constructor,
408 Handle<String> message) {
409 Handle<String> constr = Factory::LookupAsciiSymbol(constructor);
410 Handle<JSFunction> fun =
411 Handle<JSFunction>(
412 JSFunction::cast(
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000413 Top::builtins()->GetProperty(*constr)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000414 Object** argv[1] = { Handle<Object>::cast(message).location() };
415
416 // Invoke the JavaScript factory method. If an exception is thrown while
417 // running the factory method, use the exception as the result.
418 bool caught_exception;
419 Handle<Object> result = Execution::TryCall(fun,
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000420 Top::builtins(),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000421 1,
422 argv,
423 &caught_exception);
424 return result;
425}
426
427
428Handle<JSFunction> Factory::NewFunction(Handle<String> name,
429 InstanceType type,
430 int instance_size,
431 Handle<Code> code,
432 bool force_initial_map) {
433 // Allocate the function
434 Handle<JSFunction> function = NewFunction(name, the_hole_value());
435 function->set_code(*code);
436
437 if (force_initial_map ||
438 type != JS_OBJECT_TYPE ||
439 instance_size != JSObject::kHeaderSize) {
440 Handle<Map> initial_map = NewMap(type, instance_size);
441 Handle<JSObject> prototype = NewFunctionPrototype(function);
442 initial_map->set_prototype(*prototype);
443 function->set_initial_map(*initial_map);
444 initial_map->set_constructor(*function);
445 } else {
446 ASSERT(!function->has_initial_map());
447 ASSERT(!function->has_prototype());
448 }
449
450 return function;
451}
452
453
454Handle<JSFunction> Factory::NewFunctionBoilerplate(Handle<String> name,
455 int number_of_literals,
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000456 bool contains_array_literal,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000457 Handle<Code> code) {
458 Handle<JSFunction> function = NewFunctionBoilerplate(name);
459 function->set_code(*code);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000460 int literals_array_size = number_of_literals;
461 // If the function contains object, regexp or array literals,
462 // allocate extra space for a literals array prefix containing the
463 // object, regexp and array constructor functions.
464 if (number_of_literals > 0 || contains_array_literal) {
465 literals_array_size += JSFunction::kLiteralsPrefixSize;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000466 }
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000467 Handle<FixedArray> literals =
468 Factory::NewFixedArray(literals_array_size, TENURED);
469 function->set_literals(*literals);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000470 ASSERT(!function->has_initial_map());
471 ASSERT(!function->has_prototype());
472 return function;
473}
474
475
476Handle<JSFunction> Factory::NewFunctionBoilerplate(Handle<String> name) {
477 Handle<SharedFunctionInfo> shared = NewSharedFunctionInfo(name);
478 CALL_HEAP_FUNCTION(Heap::AllocateFunction(Heap::boilerplate_function_map(),
479 *shared,
480 Heap::the_hole_value()),
481 JSFunction);
482}
483
484
485Handle<JSFunction> Factory::NewFunctionWithPrototype(Handle<String> name,
486 InstanceType type,
487 int instance_size,
488 Handle<JSObject> prototype,
489 Handle<Code> code,
490 bool force_initial_map) {
491 // Allocate the function
492 Handle<JSFunction> function = NewFunction(name, prototype);
493
494 function->set_code(*code);
495
496 if (force_initial_map ||
497 type != JS_OBJECT_TYPE ||
498 instance_size != JSObject::kHeaderSize) {
499 Handle<Map> initial_map = NewMap(type, instance_size);
500 function->set_initial_map(*initial_map);
501 initial_map->set_constructor(*function);
502 }
503
504 // Set function.prototype and give the prototype a constructor
505 // property that refers to the function.
506 SetPrototypeProperty(function, prototype);
507 SetProperty(prototype, Factory::constructor_symbol(), function, DONT_ENUM);
508 return function;
509}
510
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000511
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000512Handle<Code> Factory::NewCode(const CodeDesc& desc, ScopeInfo<>* sinfo,
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000513 Code::Flags flags, Handle<Object> self_ref) {
kasperl@chromium.org061ef742009-02-27 12:16:20 +0000514 CALL_HEAP_FUNCTION(Heap::CreateCode(desc, sinfo, flags, self_ref), Code);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000515}
516
517
518Handle<Code> Factory::CopyCode(Handle<Code> code) {
519 CALL_HEAP_FUNCTION(Heap::CopyCode(*code), Code);
520}
521
522
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000523static inline Object* DoCopyInsert(DescriptorArray* array,
524 String* key,
525 Object* value,
526 PropertyAttributes attributes) {
527 CallbacksDescriptor desc(key, value, attributes);
528 Object* obj = array->CopyInsert(&desc, REMOVE_TRANSITIONS);
529 return obj;
530}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000531
532
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000533// Allocate the new array.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000534Handle<DescriptorArray> Factory::CopyAppendProxyDescriptor(
535 Handle<DescriptorArray> array,
536 Handle<String> key,
537 Handle<Object> value,
538 PropertyAttributes attributes) {
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000539 CALL_HEAP_FUNCTION(DoCopyInsert(*array, *key, *value, attributes),
540 DescriptorArray);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000541}
542
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000543
544Handle<String> Factory::SymbolFromString(Handle<String> value) {
545 CALL_HEAP_FUNCTION(Heap::LookupSymbol(*value), String);
546}
547
548
549Handle<DescriptorArray> Factory::CopyAppendCallbackDescriptors(
550 Handle<DescriptorArray> array,
551 Handle<Object> descriptors) {
552 v8::NeanderArray callbacks(descriptors);
553 int nof_callbacks = callbacks.length();
554 Handle<DescriptorArray> result =
555 NewDescriptorArray(array->number_of_descriptors() + nof_callbacks);
556
557 // Number of descriptors added to the result so far.
558 int descriptor_count = 0;
559
560 // Copy the descriptors from the array.
561 DescriptorWriter w(*result);
562 for (DescriptorReader r(*array); !r.eos(); r.advance()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000563 if (!r.IsNullDescriptor()) {
564 w.WriteFrom(&r);
565 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000566 descriptor_count++;
567 }
568
569 // Number of duplicates detected.
570 int duplicates = 0;
571
572 // Fill in new callback descriptors. Process the callbacks from
573 // back to front so that the last callback with a given name takes
574 // precedence over previously added callbacks with that name.
575 for (int i = nof_callbacks - 1; i >= 0; i--) {
576 Handle<AccessorInfo> entry =
577 Handle<AccessorInfo>(AccessorInfo::cast(callbacks.get(i)));
578 // Ensure the key is a symbol before writing into the instance descriptor.
579 Handle<String> key =
580 SymbolFromString(Handle<String>(String::cast(entry->name())));
581 // Check if a descriptor with this name already exists before writing.
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000582 if (result->LinearSearch(*key, descriptor_count) ==
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000583 DescriptorArray::kNotFound) {
584 CallbacksDescriptor desc(*key, *entry, entry->property_attributes());
585 w.Write(&desc);
586 descriptor_count++;
587 } else {
588 duplicates++;
589 }
590 }
591
592 // If duplicates were detected, allocate a result of the right size
593 // and transfer the elements.
594 if (duplicates > 0) {
595 Handle<DescriptorArray> new_result =
596 NewDescriptorArray(result->number_of_descriptors() - duplicates);
597 DescriptorWriter w(*new_result);
598 DescriptorReader r(*result);
599 while (!w.eos()) {
600 w.WriteFrom(&r);
601 r.advance();
602 }
603 result = new_result;
604 }
605
606 // Sort the result before returning.
607 result->Sort();
608 return result;
609}
610
611
612Handle<JSObject> Factory::NewJSObject(Handle<JSFunction> constructor,
613 PretenureFlag pretenure) {
614 CALL_HEAP_FUNCTION(Heap::AllocateJSObject(*constructor, pretenure), JSObject);
615}
616
617
ager@chromium.org236ad962008-09-25 09:45:57 +0000618Handle<JSObject> Factory::NewJSObjectFromMap(Handle<Map> map) {
619 CALL_HEAP_FUNCTION(Heap::AllocateJSObjectFromMap(*map, NOT_TENURED),
620 JSObject);
621}
622
623
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000624Handle<JSArray> Factory::NewJSArray(int length,
625 PretenureFlag pretenure) {
626 Handle<JSObject> obj = NewJSObject(Top::array_function(), pretenure);
627 CALL_HEAP_FUNCTION(Handle<JSArray>::cast(obj)->Initialize(length), JSArray);
628}
629
630
631Handle<JSArray> Factory::NewJSArrayWithElements(Handle<FixedArray> elements,
632 PretenureFlag pretenure) {
633 Handle<JSArray> result =
634 Handle<JSArray>::cast(NewJSObject(Top::array_function(), pretenure));
635 result->SetContent(*elements);
636 return result;
637}
638
639
640Handle<SharedFunctionInfo> Factory::NewSharedFunctionInfo(Handle<String> name) {
641 CALL_HEAP_FUNCTION(Heap::AllocateSharedFunctionInfo(*name),
642 SharedFunctionInfo);
643}
644
645
646Handle<Dictionary> Factory::DictionaryAtNumberPut(Handle<Dictionary> dictionary,
647 uint32_t key,
648 Handle<Object> value) {
649 CALL_HEAP_FUNCTION(dictionary->AtNumberPut(key, *value), Dictionary);
650}
651
652
653Handle<JSFunction> Factory::NewFunctionHelper(Handle<String> name,
654 Handle<Object> prototype) {
655 Handle<SharedFunctionInfo> function_share = NewSharedFunctionInfo(name);
656 CALL_HEAP_FUNCTION(Heap::AllocateFunction(*Top::function_map(),
657 *function_share,
658 *prototype),
659 JSFunction);
660}
661
662
663Handle<JSFunction> Factory::NewFunction(Handle<String> name,
664 Handle<Object> prototype) {
665 Handle<JSFunction> fun = NewFunctionHelper(name, prototype);
666 fun->set_context(Top::context()->global_context());
667 return fun;
668}
669
670
671Handle<Object> Factory::ToObject(Handle<Object> object,
672 Handle<Context> global_context) {
673 CALL_HEAP_FUNCTION(object->ToObject(*global_context), Object);
674}
675
676
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000677#ifdef ENABLE_DEBUGGER_SUPPORT
v8.team.kasperl727e9952008-09-02 14:56:44 +0000678Handle<DebugInfo> Factory::NewDebugInfo(Handle<SharedFunctionInfo> shared) {
679 // Get the original code of the function.
680 Handle<Code> code(shared->code());
681
682 // Create a copy of the code before allocating the debug info object to avoid
683 // allocation while setting up the debug info object.
684 Handle<Code> original_code(*Factory::CopyCode(code));
685
686 // Allocate initial fixed array for active break points before allocating the
687 // debug info object to avoid allocation while setting up the debug info
688 // object.
689 Handle<FixedArray> break_points(
690 Factory::NewFixedArray(Debug::kEstimatedNofBreakPointsInFunction));
691
692 // Create and set up the debug info object. Debug info contains function, a
693 // copy of the original code, the executing code and initial fixed array for
694 // active break points.
695 Handle<DebugInfo> debug_info =
696 Handle<DebugInfo>::cast(Factory::NewStruct(DEBUG_INFO_TYPE));
697 debug_info->set_shared(*shared);
698 debug_info->set_original_code(*original_code);
699 debug_info->set_code(*code);
700 debug_info->set_break_points(*break_points);
701
702 // Link debug info to function.
703 shared->set_debug_info(*debug_info);
704
705 return debug_info;
706}
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000707#endif
v8.team.kasperl727e9952008-09-02 14:56:44 +0000708
709
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000710Handle<JSObject> Factory::NewArgumentsObject(Handle<Object> callee,
711 int length) {
712 CALL_HEAP_FUNCTION(Heap::AllocateArgumentsObject(*callee, length), JSObject);
713}
714
715
716Handle<JSFunction> Factory::CreateApiFunction(
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000717 Handle<FunctionTemplateInfo> obj, ApiInstanceType instance_type) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000718 Handle<Code> code = Handle<Code>(Builtins::builtin(Builtins::HandleApiCall));
719
kasper.lund212ac232008-07-16 07:07:30 +0000720 int internal_field_count = 0;
721 if (!obj->instance_template()->IsUndefined()) {
722 Handle<ObjectTemplateInfo> instance_template =
723 Handle<ObjectTemplateInfo>(
724 ObjectTemplateInfo::cast(obj->instance_template()));
725 internal_field_count =
726 Smi::cast(instance_template->internal_field_count())->value();
727 }
728
729 int instance_size = kPointerSize * internal_field_count;
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000730 InstanceType type = INVALID_TYPE;
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000731 switch (instance_type) {
732 case JavaScriptObject:
733 type = JS_OBJECT_TYPE;
734 instance_size += JSObject::kHeaderSize;
735 break;
736 case InnerGlobalObject:
737 type = JS_GLOBAL_OBJECT_TYPE;
738 instance_size += JSGlobalObject::kSize;
739 break;
740 case OuterGlobalObject:
741 type = JS_GLOBAL_PROXY_TYPE;
742 instance_size += JSGlobalProxy::kSize;
743 break;
744 default:
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000745 break;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000746 }
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000747 ASSERT(type != INVALID_TYPE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000748
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000749 Handle<JSFunction> result =
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000750 Factory::NewFunction(Factory::empty_symbol(),
751 type,
752 instance_size,
753 code,
754 true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000755 // Set class name.
756 Handle<Object> class_name = Handle<Object>(obj->class_name());
757 if (class_name->IsString()) {
758 result->shared()->set_instance_class_name(*class_name);
759 result->shared()->set_name(*class_name);
760 }
761
762 Handle<Map> map = Handle<Map>(result->initial_map());
763
764 // Mark as undetectable if needed.
765 if (obj->undetectable()) {
766 map->set_is_undetectable();
767 }
768
769 // Mark as hidden for the __proto__ accessor if needed.
770 if (obj->hidden_prototype()) {
771 map->set_is_hidden_prototype();
772 }
773
774 // Mark as needs_access_check if needed.
775 if (obj->needs_access_check()) {
ager@chromium.org870a0b62008-11-04 11:43:05 +0000776 map->set_is_access_check_needed(true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000777 }
778
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000779 // Set interceptor information in the map.
780 if (!obj->named_property_handler()->IsUndefined()) {
781 map->set_has_named_interceptor();
782 }
783 if (!obj->indexed_property_handler()->IsUndefined()) {
784 map->set_has_indexed_interceptor();
785 }
786
787 // Set instance call-as-function information in the map.
788 if (!obj->instance_call_handler()->IsUndefined()) {
789 map->set_has_instance_call_handler();
790 }
791
792 result->shared()->set_function_data(*obj);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000793 result->shared()->DontAdaptArguments();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000794
795 // Recursively copy parent templates' accessors, 'data' may be modified.
796 Handle<DescriptorArray> array =
797 Handle<DescriptorArray>(map->instance_descriptors());
798 while (true) {
799 Handle<Object> props = Handle<Object>(obj->property_accessors());
800 if (!props->IsUndefined()) {
801 array = Factory::CopyAppendCallbackDescriptors(array, props);
802 }
803 Handle<Object> parent = Handle<Object>(obj->parent_template());
804 if (parent->IsUndefined()) break;
805 obj = Handle<FunctionTemplateInfo>::cast(parent);
806 }
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000807 if (!array->IsEmpty()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000808 map->set_instance_descriptors(*array);
809 }
810
811 return result;
812}
813
814
ager@chromium.org236ad962008-09-25 09:45:57 +0000815Handle<MapCache> Factory::NewMapCache(int at_least_space_for) {
816 CALL_HEAP_FUNCTION(MapCache::Allocate(at_least_space_for), MapCache);
817}
818
819
820static Object* UpdateMapCacheWith(Context* context,
821 FixedArray* keys,
822 Map* map) {
823 Object* result = MapCache::cast(context->map_cache())->Put(keys, map);
824 if (!result->IsFailure()) context->set_map_cache(MapCache::cast(result));
825 return result;
826}
827
828
829Handle<MapCache> Factory::AddToMapCache(Handle<Context> context,
830 Handle<FixedArray> keys,
831 Handle<Map> map) {
832 CALL_HEAP_FUNCTION(UpdateMapCacheWith(*context, *keys, *map), MapCache);
833}
834
835
836Handle<Map> Factory::ObjectLiteralMapFromCache(Handle<Context> context,
837 Handle<FixedArray> keys) {
838 if (context->map_cache()->IsUndefined()) {
839 // Allocate the new map cache for the global context.
840 Handle<MapCache> new_cache = NewMapCache(24);
841 context->set_map_cache(*new_cache);
842 }
ager@chromium.org32912102009-01-16 10:38:43 +0000843 // Check to see whether there is a matching element in the cache.
ager@chromium.org236ad962008-09-25 09:45:57 +0000844 Handle<MapCache> cache =
845 Handle<MapCache>(MapCache::cast(context->map_cache()));
846 Handle<Object> result = Handle<Object>(cache->Lookup(*keys));
847 if (result->IsMap()) return Handle<Map>::cast(result);
848 // Create a new map and add it to the cache.
849 Handle<Map> map =
ager@chromium.org32912102009-01-16 10:38:43 +0000850 CopyMap(Handle<Map>(context->object_function()->initial_map()),
851 keys->length());
ager@chromium.org236ad962008-09-25 09:45:57 +0000852 AddToMapCache(context, keys, map);
853 return Handle<Map>(map);
854}
855
856
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000857void Factory::SetRegExpAtomData(Handle<JSRegExp> regexp,
858 JSRegExp::Type type,
859 Handle<String> source,
860 JSRegExp::Flags flags,
861 Handle<Object> data) {
862 Handle<FixedArray> store = NewFixedArray(JSRegExp::kAtomDataSize);
863
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000864 store->set(JSRegExp::kTagIndex, Smi::FromInt(type));
865 store->set(JSRegExp::kSourceIndex, *source);
866 store->set(JSRegExp::kFlagsIndex, Smi::FromInt(flags.value()));
867 store->set(JSRegExp::kAtomPatternIndex, *data);
868 regexp->set_data(*store);
869}
870
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000871void Factory::SetRegExpIrregexpData(Handle<JSRegExp> regexp,
872 JSRegExp::Type type,
873 Handle<String> source,
874 JSRegExp::Flags flags,
875 int capture_count) {
876 Handle<FixedArray> store = NewFixedArray(JSRegExp::kIrregexpDataSize);
877
878 store->set(JSRegExp::kTagIndex, Smi::FromInt(type));
879 store->set(JSRegExp::kSourceIndex, *source);
880 store->set(JSRegExp::kFlagsIndex, Smi::FromInt(flags.value()));
881 store->set(JSRegExp::kIrregexpASCIICodeIndex, Heap::the_hole_value());
882 store->set(JSRegExp::kIrregexpUC16CodeIndex, Heap::the_hole_value());
883 store->set(JSRegExp::kIrregexpMaxRegisterCountIndex, Smi::FromInt(0));
884 store->set(JSRegExp::kIrregexpCaptureCountIndex,
885 Smi::FromInt(capture_count));
886 regexp->set_data(*store);
887}
888
889
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000890
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000891void Factory::ConfigureInstance(Handle<FunctionTemplateInfo> desc,
892 Handle<JSObject> instance,
893 bool* pending_exception) {
894 // Configure the instance by adding the properties specified by the
895 // instance template.
896 Handle<Object> instance_template = Handle<Object>(desc->instance_template());
897 if (!instance_template->IsUndefined()) {
898 Execution::ConfigureInstance(instance,
899 instance_template,
900 pending_exception);
901 } else {
902 *pending_exception = false;
903 }
904}
905
906
907} } // namespace v8::internal