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