blob: e29c84d2b1fcc2de65f989cbb270cb2285baeeab [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.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000170 Handle<Script> script = Handle<Script>::cast(NewStruct(SCRIPT_TYPE));
171 script->set_source(*source);
172 script->set_name(Heap::undefined_value());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000173 script->set_id(Heap::last_script_id());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000174 script->set_line_offset(Smi::FromInt(0));
175 script->set_column_offset(Smi::FromInt(0));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000176 script->set_type(Smi::FromInt(SCRIPT_TYPE_NORMAL));
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000177 script->set_wrapper(*Factory::NewProxy(0, TENURED));
iposva@chromium.org245aa852009-02-10 00:49:54 +0000178 script->set_line_ends(Heap::undefined_value());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000179
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000180 return script;
181}
182
183
184Handle<Proxy> Factory::NewProxy(Address addr, PretenureFlag pretenure) {
185 CALL_HEAP_FUNCTION(Heap::AllocateProxy(addr, pretenure), Proxy);
186}
187
188
189Handle<Proxy> Factory::NewProxy(const AccessorDescriptor* desc) {
190 return NewProxy((Address) desc, TENURED);
191}
192
193
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000194Handle<ByteArray> Factory::NewByteArray(int length, PretenureFlag pretenure) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000195 ASSERT(0 <= length);
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000196 CALL_HEAP_FUNCTION(Heap::AllocateByteArray(length, pretenure), ByteArray);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000197}
198
199
200Handle<Map> Factory::NewMap(InstanceType type, int instance_size) {
201 CALL_HEAP_FUNCTION(Heap::AllocateMap(type, instance_size), Map);
202}
203
204
205Handle<JSObject> Factory::NewFunctionPrototype(Handle<JSFunction> function) {
206 CALL_HEAP_FUNCTION(Heap::AllocateFunctionPrototype(*function), JSObject);
207}
208
209
210Handle<Map> Factory::CopyMap(Handle<Map> src) {
211 CALL_HEAP_FUNCTION(src->Copy(), Map);
212}
213
214
ager@chromium.org32912102009-01-16 10:38:43 +0000215Handle<Map> Factory::CopyMap(Handle<Map> src,
216 int extra_inobject_properties) {
217 Handle<Map> copy = CopyMap(src);
218 // Check that we do not overflow the instance size when adding the
219 // extra inobject properties.
220 int instance_size_delta = extra_inobject_properties * kPointerSize;
221 int max_instance_size_delta =
222 JSObject::kMaxInstanceSize - copy->instance_size();
223 if (instance_size_delta > max_instance_size_delta) {
224 // If the instance size overflows, we allocate as many properties
225 // as we can as inobject properties.
226 instance_size_delta = max_instance_size_delta;
227 extra_inobject_properties = max_instance_size_delta >> kPointerSizeLog2;
228 }
229 // Adjust the map with the extra inobject properties.
230 int inobject_properties =
231 copy->inobject_properties() + extra_inobject_properties;
232 copy->set_inobject_properties(inobject_properties);
233 copy->set_unused_property_fields(inobject_properties);
234 copy->set_instance_size(copy->instance_size() + instance_size_delta);
235 return copy;
236}
237
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000238Handle<Map> Factory::CopyMapDropTransitions(Handle<Map> src) {
239 CALL_HEAP_FUNCTION(src->CopyDropTransitions(), Map);
240}
241
242
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000243Handle<FixedArray> Factory::CopyFixedArray(Handle<FixedArray> array) {
244 CALL_HEAP_FUNCTION(array->Copy(), FixedArray);
245}
246
247
248Handle<JSFunction> Factory::BaseNewFunctionFromBoilerplate(
249 Handle<JSFunction> boilerplate,
250 Handle<Map> function_map) {
251 ASSERT(boilerplate->IsBoilerplate());
252 ASSERT(!boilerplate->has_initial_map());
253 ASSERT(!boilerplate->has_prototype());
254 ASSERT(boilerplate->properties() == Heap::empty_fixed_array());
255 ASSERT(boilerplate->elements() == Heap::empty_fixed_array());
256 CALL_HEAP_FUNCTION(Heap::AllocateFunction(*function_map,
257 boilerplate->shared(),
258 Heap::the_hole_value()),
259 JSFunction);
260}
261
262
263Handle<JSFunction> Factory::NewFunctionFromBoilerplate(
264 Handle<JSFunction> boilerplate,
265 Handle<Context> context) {
266 Handle<JSFunction> result =
267 BaseNewFunctionFromBoilerplate(boilerplate, Top::function_map());
268 result->set_context(*context);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000269 int number_of_literals = boilerplate->NumberOfLiterals();
270 Handle<FixedArray> literals =
271 Factory::NewFixedArray(number_of_literals, TENURED);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000272 if (number_of_literals > 0) {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000273 // Store the object, regexp and array functions in the literals
274 // array prefix. These functions will be used when creating
275 // object, regexp and array literals in this function.
ager@chromium.org236ad962008-09-25 09:45:57 +0000276 literals->set(JSFunction::kLiteralGlobalContextIndex,
277 context->global_context());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000278 }
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000279 result->set_literals(*literals);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000280 ASSERT(!result->IsBoilerplate());
281 return result;
282}
283
284
285Handle<Object> Factory::NewNumber(double value,
286 PretenureFlag pretenure) {
287 CALL_HEAP_FUNCTION(Heap::NumberFromDouble(value, pretenure), Object);
288}
289
290
291Handle<Object> Factory::NewNumberFromInt(int value) {
292 CALL_HEAP_FUNCTION(Heap::NumberFromInt32(value), Object);
293}
294
295
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000296Handle<Object> Factory::NewNumberFromUint(uint32_t value) {
297 CALL_HEAP_FUNCTION(Heap::NumberFromUint32(value), Object);
298}
299
300
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000301Handle<JSObject> Factory::NewNeanderObject() {
302 CALL_HEAP_FUNCTION(Heap::AllocateJSObjectFromMap(Heap::neander_map()),
303 JSObject);
304}
305
306
307Handle<Object> Factory::NewTypeError(const char* type,
308 Vector< Handle<Object> > args) {
309 return NewError("MakeTypeError", type, args);
310}
311
312
313Handle<Object> Factory::NewTypeError(Handle<String> message) {
314 return NewError("$TypeError", message);
315}
316
317
318Handle<Object> Factory::NewRangeError(const char* type,
319 Vector< Handle<Object> > args) {
320 return NewError("MakeRangeError", type, args);
321}
322
323
324Handle<Object> Factory::NewRangeError(Handle<String> message) {
325 return NewError("$RangeError", message);
326}
327
328
329Handle<Object> Factory::NewSyntaxError(const char* type, Handle<JSArray> args) {
330 return NewError("MakeSyntaxError", type, args);
331}
332
333
334Handle<Object> Factory::NewSyntaxError(Handle<String> message) {
335 return NewError("$SyntaxError", message);
336}
337
338
339Handle<Object> Factory::NewReferenceError(const char* type,
340 Vector< Handle<Object> > args) {
341 return NewError("MakeReferenceError", type, args);
342}
343
344
345Handle<Object> Factory::NewReferenceError(Handle<String> message) {
346 return NewError("$ReferenceError", message);
347}
348
349
350Handle<Object> Factory::NewError(const char* maker, const char* type,
351 Vector< Handle<Object> > args) {
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000352 v8::HandleScope scope; // Instantiate a closeable HandleScope for EscapeFrom.
ager@chromium.org7c537e22008-10-16 08:43:32 +0000353 Handle<FixedArray> array = Factory::NewFixedArray(args.length());
354 for (int i = 0; i < args.length(); i++) {
355 array->set(i, *args[i]);
356 }
357 Handle<JSArray> object = Factory::NewJSArrayWithElements(array);
358 Handle<Object> result = NewError(maker, type, object);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000359 return result.EscapeFrom(&scope);
360}
361
362
363Handle<Object> Factory::NewEvalError(const char* type,
364 Vector< Handle<Object> > args) {
365 return NewError("MakeEvalError", type, args);
366}
367
368
369Handle<Object> Factory::NewError(const char* type,
370 Vector< Handle<Object> > args) {
371 return NewError("MakeError", type, args);
372}
373
374
375Handle<Object> Factory::NewError(const char* maker,
376 const char* type,
377 Handle<JSArray> args) {
378 Handle<String> make_str = Factory::LookupAsciiSymbol(maker);
379 Handle<JSFunction> fun =
380 Handle<JSFunction>(
381 JSFunction::cast(
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000382 Top::builtins()->GetProperty(*make_str)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000383 Handle<Object> type_obj = Factory::LookupAsciiSymbol(type);
384 Object** argv[2] = { type_obj.location(),
385 Handle<Object>::cast(args).location() };
386
387 // Invoke the JavaScript factory method. If an exception is thrown while
388 // running the factory method, use the exception as the result.
389 bool caught_exception;
390 Handle<Object> result = Execution::TryCall(fun,
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000391 Top::builtins(),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000392 2,
393 argv,
394 &caught_exception);
395 return result;
396}
397
398
399Handle<Object> Factory::NewError(Handle<String> message) {
400 return NewError("$Error", message);
401}
402
403
404Handle<Object> Factory::NewError(const char* constructor,
405 Handle<String> message) {
406 Handle<String> constr = Factory::LookupAsciiSymbol(constructor);
407 Handle<JSFunction> fun =
408 Handle<JSFunction>(
409 JSFunction::cast(
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000410 Top::builtins()->GetProperty(*constr)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000411 Object** argv[1] = { Handle<Object>::cast(message).location() };
412
413 // Invoke the JavaScript factory method. If an exception is thrown while
414 // running the factory method, use the exception as the result.
415 bool caught_exception;
416 Handle<Object> result = Execution::TryCall(fun,
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000417 Top::builtins(),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000418 1,
419 argv,
420 &caught_exception);
421 return result;
422}
423
424
425Handle<JSFunction> Factory::NewFunction(Handle<String> name,
426 InstanceType type,
427 int instance_size,
428 Handle<Code> code,
429 bool force_initial_map) {
430 // Allocate the function
431 Handle<JSFunction> function = NewFunction(name, the_hole_value());
432 function->set_code(*code);
433
434 if (force_initial_map ||
435 type != JS_OBJECT_TYPE ||
436 instance_size != JSObject::kHeaderSize) {
437 Handle<Map> initial_map = NewMap(type, instance_size);
438 Handle<JSObject> prototype = NewFunctionPrototype(function);
439 initial_map->set_prototype(*prototype);
440 function->set_initial_map(*initial_map);
441 initial_map->set_constructor(*function);
442 } else {
443 ASSERT(!function->has_initial_map());
444 ASSERT(!function->has_prototype());
445 }
446
447 return function;
448}
449
450
451Handle<JSFunction> Factory::NewFunctionBoilerplate(Handle<String> name,
452 int number_of_literals,
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000453 bool contains_array_literal,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000454 Handle<Code> code) {
455 Handle<JSFunction> function = NewFunctionBoilerplate(name);
456 function->set_code(*code);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000457 int literals_array_size = number_of_literals;
458 // If the function contains object, regexp or array literals,
459 // allocate extra space for a literals array prefix containing the
460 // object, regexp and array constructor functions.
461 if (number_of_literals > 0 || contains_array_literal) {
462 literals_array_size += JSFunction::kLiteralsPrefixSize;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000463 }
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000464 Handle<FixedArray> literals =
465 Factory::NewFixedArray(literals_array_size, TENURED);
466 function->set_literals(*literals);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000467 ASSERT(!function->has_initial_map());
468 ASSERT(!function->has_prototype());
469 return function;
470}
471
472
473Handle<JSFunction> Factory::NewFunctionBoilerplate(Handle<String> name) {
474 Handle<SharedFunctionInfo> shared = NewSharedFunctionInfo(name);
475 CALL_HEAP_FUNCTION(Heap::AllocateFunction(Heap::boilerplate_function_map(),
476 *shared,
477 Heap::the_hole_value()),
478 JSFunction);
479}
480
481
482Handle<JSFunction> Factory::NewFunctionWithPrototype(Handle<String> name,
483 InstanceType type,
484 int instance_size,
485 Handle<JSObject> prototype,
486 Handle<Code> code,
487 bool force_initial_map) {
488 // Allocate the function
489 Handle<JSFunction> function = NewFunction(name, prototype);
490
491 function->set_code(*code);
492
493 if (force_initial_map ||
494 type != JS_OBJECT_TYPE ||
495 instance_size != JSObject::kHeaderSize) {
496 Handle<Map> initial_map = NewMap(type, instance_size);
497 function->set_initial_map(*initial_map);
498 initial_map->set_constructor(*function);
499 }
500
501 // Set function.prototype and give the prototype a constructor
502 // property that refers to the function.
503 SetPrototypeProperty(function, prototype);
504 SetProperty(prototype, Factory::constructor_symbol(), function, DONT_ENUM);
505 return function;
506}
507
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000508
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000509Handle<Code> Factory::NewCode(const CodeDesc& desc, ScopeInfo<>* sinfo,
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000510 Code::Flags flags, Handle<Object> self_ref) {
kasperl@chromium.org061ef742009-02-27 12:16:20 +0000511 CALL_HEAP_FUNCTION(Heap::CreateCode(desc, sinfo, flags, self_ref), Code);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000512}
513
514
515Handle<Code> Factory::CopyCode(Handle<Code> code) {
516 CALL_HEAP_FUNCTION(Heap::CopyCode(*code), Code);
517}
518
519
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000520static inline Object* DoCopyInsert(DescriptorArray* array,
521 String* key,
522 Object* value,
523 PropertyAttributes attributes) {
524 CallbacksDescriptor desc(key, value, attributes);
525 Object* obj = array->CopyInsert(&desc, REMOVE_TRANSITIONS);
526 return obj;
527}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000528
529
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000530// Allocate the new array.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000531Handle<DescriptorArray> Factory::CopyAppendProxyDescriptor(
532 Handle<DescriptorArray> array,
533 Handle<String> key,
534 Handle<Object> value,
535 PropertyAttributes attributes) {
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000536 CALL_HEAP_FUNCTION(DoCopyInsert(*array, *key, *value, attributes),
537 DescriptorArray);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000538}
539
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000540
541Handle<String> Factory::SymbolFromString(Handle<String> value) {
542 CALL_HEAP_FUNCTION(Heap::LookupSymbol(*value), String);
543}
544
545
546Handle<DescriptorArray> Factory::CopyAppendCallbackDescriptors(
547 Handle<DescriptorArray> array,
548 Handle<Object> descriptors) {
549 v8::NeanderArray callbacks(descriptors);
550 int nof_callbacks = callbacks.length();
551 Handle<DescriptorArray> result =
552 NewDescriptorArray(array->number_of_descriptors() + nof_callbacks);
553
554 // Number of descriptors added to the result so far.
555 int descriptor_count = 0;
556
557 // Copy the descriptors from the array.
558 DescriptorWriter w(*result);
559 for (DescriptorReader r(*array); !r.eos(); r.advance()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000560 if (!r.IsNullDescriptor()) {
561 w.WriteFrom(&r);
562 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000563 descriptor_count++;
564 }
565
566 // Number of duplicates detected.
567 int duplicates = 0;
568
569 // Fill in new callback descriptors. Process the callbacks from
570 // back to front so that the last callback with a given name takes
571 // precedence over previously added callbacks with that name.
572 for (int i = nof_callbacks - 1; i >= 0; i--) {
573 Handle<AccessorInfo> entry =
574 Handle<AccessorInfo>(AccessorInfo::cast(callbacks.get(i)));
575 // Ensure the key is a symbol before writing into the instance descriptor.
576 Handle<String> key =
577 SymbolFromString(Handle<String>(String::cast(entry->name())));
578 // Check if a descriptor with this name already exists before writing.
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000579 if (result->LinearSearch(*key, descriptor_count) ==
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000580 DescriptorArray::kNotFound) {
581 CallbacksDescriptor desc(*key, *entry, entry->property_attributes());
582 w.Write(&desc);
583 descriptor_count++;
584 } else {
585 duplicates++;
586 }
587 }
588
589 // If duplicates were detected, allocate a result of the right size
590 // and transfer the elements.
591 if (duplicates > 0) {
592 Handle<DescriptorArray> new_result =
593 NewDescriptorArray(result->number_of_descriptors() - duplicates);
594 DescriptorWriter w(*new_result);
595 DescriptorReader r(*result);
596 while (!w.eos()) {
597 w.WriteFrom(&r);
598 r.advance();
599 }
600 result = new_result;
601 }
602
603 // Sort the result before returning.
604 result->Sort();
605 return result;
606}
607
608
609Handle<JSObject> Factory::NewJSObject(Handle<JSFunction> constructor,
610 PretenureFlag pretenure) {
611 CALL_HEAP_FUNCTION(Heap::AllocateJSObject(*constructor, pretenure), JSObject);
612}
613
614
ager@chromium.org236ad962008-09-25 09:45:57 +0000615Handle<JSObject> Factory::NewJSObjectFromMap(Handle<Map> map) {
616 CALL_HEAP_FUNCTION(Heap::AllocateJSObjectFromMap(*map, NOT_TENURED),
617 JSObject);
618}
619
620
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000621Handle<JSArray> Factory::NewJSArray(int length,
622 PretenureFlag pretenure) {
623 Handle<JSObject> obj = NewJSObject(Top::array_function(), pretenure);
624 CALL_HEAP_FUNCTION(Handle<JSArray>::cast(obj)->Initialize(length), JSArray);
625}
626
627
628Handle<JSArray> Factory::NewJSArrayWithElements(Handle<FixedArray> elements,
629 PretenureFlag pretenure) {
630 Handle<JSArray> result =
631 Handle<JSArray>::cast(NewJSObject(Top::array_function(), pretenure));
632 result->SetContent(*elements);
633 return result;
634}
635
636
637Handle<SharedFunctionInfo> Factory::NewSharedFunctionInfo(Handle<String> name) {
638 CALL_HEAP_FUNCTION(Heap::AllocateSharedFunctionInfo(*name),
639 SharedFunctionInfo);
640}
641
642
643Handle<Dictionary> Factory::DictionaryAtNumberPut(Handle<Dictionary> dictionary,
644 uint32_t key,
645 Handle<Object> value) {
646 CALL_HEAP_FUNCTION(dictionary->AtNumberPut(key, *value), Dictionary);
647}
648
649
650Handle<JSFunction> Factory::NewFunctionHelper(Handle<String> name,
651 Handle<Object> prototype) {
652 Handle<SharedFunctionInfo> function_share = NewSharedFunctionInfo(name);
653 CALL_HEAP_FUNCTION(Heap::AllocateFunction(*Top::function_map(),
654 *function_share,
655 *prototype),
656 JSFunction);
657}
658
659
660Handle<JSFunction> Factory::NewFunction(Handle<String> name,
661 Handle<Object> prototype) {
662 Handle<JSFunction> fun = NewFunctionHelper(name, prototype);
663 fun->set_context(Top::context()->global_context());
664 return fun;
665}
666
667
668Handle<Object> Factory::ToObject(Handle<Object> object,
669 Handle<Context> global_context) {
670 CALL_HEAP_FUNCTION(object->ToObject(*global_context), Object);
671}
672
673
v8.team.kasperl727e9952008-09-02 14:56:44 +0000674Handle<DebugInfo> Factory::NewDebugInfo(Handle<SharedFunctionInfo> shared) {
675 // Get the original code of the function.
676 Handle<Code> code(shared->code());
677
678 // Create a copy of the code before allocating the debug info object to avoid
679 // allocation while setting up the debug info object.
680 Handle<Code> original_code(*Factory::CopyCode(code));
681
682 // Allocate initial fixed array for active break points before allocating the
683 // debug info object to avoid allocation while setting up the debug info
684 // object.
685 Handle<FixedArray> break_points(
686 Factory::NewFixedArray(Debug::kEstimatedNofBreakPointsInFunction));
687
688 // Create and set up the debug info object. Debug info contains function, a
689 // copy of the original code, the executing code and initial fixed array for
690 // active break points.
691 Handle<DebugInfo> debug_info =
692 Handle<DebugInfo>::cast(Factory::NewStruct(DEBUG_INFO_TYPE));
693 debug_info->set_shared(*shared);
694 debug_info->set_original_code(*original_code);
695 debug_info->set_code(*code);
696 debug_info->set_break_points(*break_points);
697
698 // Link debug info to function.
699 shared->set_debug_info(*debug_info);
700
701 return debug_info;
702}
703
704
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000705Handle<JSObject> Factory::NewArgumentsObject(Handle<Object> callee,
706 int length) {
707 CALL_HEAP_FUNCTION(Heap::AllocateArgumentsObject(*callee, length), JSObject);
708}
709
710
711Handle<JSFunction> Factory::CreateApiFunction(
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000712 Handle<FunctionTemplateInfo> obj, ApiInstanceType instance_type) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000713 Handle<Code> code = Handle<Code>(Builtins::builtin(Builtins::HandleApiCall));
714
kasper.lund212ac232008-07-16 07:07:30 +0000715 int internal_field_count = 0;
716 if (!obj->instance_template()->IsUndefined()) {
717 Handle<ObjectTemplateInfo> instance_template =
718 Handle<ObjectTemplateInfo>(
719 ObjectTemplateInfo::cast(obj->instance_template()));
720 internal_field_count =
721 Smi::cast(instance_template->internal_field_count())->value();
722 }
723
724 int instance_size = kPointerSize * internal_field_count;
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000725 InstanceType type = INVALID_TYPE;
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000726 switch (instance_type) {
727 case JavaScriptObject:
728 type = JS_OBJECT_TYPE;
729 instance_size += JSObject::kHeaderSize;
730 break;
731 case InnerGlobalObject:
732 type = JS_GLOBAL_OBJECT_TYPE;
733 instance_size += JSGlobalObject::kSize;
734 break;
735 case OuterGlobalObject:
736 type = JS_GLOBAL_PROXY_TYPE;
737 instance_size += JSGlobalProxy::kSize;
738 break;
739 default:
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000740 break;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000741 }
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000742 ASSERT(type != INVALID_TYPE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000743
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000744 Handle<JSFunction> result =
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000745 Factory::NewFunction(Factory::empty_symbol(),
746 type,
747 instance_size,
748 code,
749 true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000750 // Set class name.
751 Handle<Object> class_name = Handle<Object>(obj->class_name());
752 if (class_name->IsString()) {
753 result->shared()->set_instance_class_name(*class_name);
754 result->shared()->set_name(*class_name);
755 }
756
757 Handle<Map> map = Handle<Map>(result->initial_map());
758
759 // Mark as undetectable if needed.
760 if (obj->undetectable()) {
761 map->set_is_undetectable();
762 }
763
764 // Mark as hidden for the __proto__ accessor if needed.
765 if (obj->hidden_prototype()) {
766 map->set_is_hidden_prototype();
767 }
768
769 // Mark as needs_access_check if needed.
770 if (obj->needs_access_check()) {
ager@chromium.org870a0b62008-11-04 11:43:05 +0000771 map->set_is_access_check_needed(true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000772 }
773
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000774 // Set interceptor information in the map.
775 if (!obj->named_property_handler()->IsUndefined()) {
776 map->set_has_named_interceptor();
777 }
778 if (!obj->indexed_property_handler()->IsUndefined()) {
779 map->set_has_indexed_interceptor();
780 }
781
782 // Set instance call-as-function information in the map.
783 if (!obj->instance_call_handler()->IsUndefined()) {
784 map->set_has_instance_call_handler();
785 }
786
787 result->shared()->set_function_data(*obj);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000788 result->shared()->DontAdaptArguments();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000789
790 // Recursively copy parent templates' accessors, 'data' may be modified.
791 Handle<DescriptorArray> array =
792 Handle<DescriptorArray>(map->instance_descriptors());
793 while (true) {
794 Handle<Object> props = Handle<Object>(obj->property_accessors());
795 if (!props->IsUndefined()) {
796 array = Factory::CopyAppendCallbackDescriptors(array, props);
797 }
798 Handle<Object> parent = Handle<Object>(obj->parent_template());
799 if (parent->IsUndefined()) break;
800 obj = Handle<FunctionTemplateInfo>::cast(parent);
801 }
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000802 if (!array->IsEmpty()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000803 map->set_instance_descriptors(*array);
804 }
805
806 return result;
807}
808
809
ager@chromium.org236ad962008-09-25 09:45:57 +0000810Handle<MapCache> Factory::NewMapCache(int at_least_space_for) {
811 CALL_HEAP_FUNCTION(MapCache::Allocate(at_least_space_for), MapCache);
812}
813
814
815static Object* UpdateMapCacheWith(Context* context,
816 FixedArray* keys,
817 Map* map) {
818 Object* result = MapCache::cast(context->map_cache())->Put(keys, map);
819 if (!result->IsFailure()) context->set_map_cache(MapCache::cast(result));
820 return result;
821}
822
823
824Handle<MapCache> Factory::AddToMapCache(Handle<Context> context,
825 Handle<FixedArray> keys,
826 Handle<Map> map) {
827 CALL_HEAP_FUNCTION(UpdateMapCacheWith(*context, *keys, *map), MapCache);
828}
829
830
831Handle<Map> Factory::ObjectLiteralMapFromCache(Handle<Context> context,
832 Handle<FixedArray> keys) {
833 if (context->map_cache()->IsUndefined()) {
834 // Allocate the new map cache for the global context.
835 Handle<MapCache> new_cache = NewMapCache(24);
836 context->set_map_cache(*new_cache);
837 }
ager@chromium.org32912102009-01-16 10:38:43 +0000838 // Check to see whether there is a matching element in the cache.
ager@chromium.org236ad962008-09-25 09:45:57 +0000839 Handle<MapCache> cache =
840 Handle<MapCache>(MapCache::cast(context->map_cache()));
841 Handle<Object> result = Handle<Object>(cache->Lookup(*keys));
842 if (result->IsMap()) return Handle<Map>::cast(result);
843 // Create a new map and add it to the cache.
844 Handle<Map> map =
ager@chromium.org32912102009-01-16 10:38:43 +0000845 CopyMap(Handle<Map>(context->object_function()->initial_map()),
846 keys->length());
ager@chromium.org236ad962008-09-25 09:45:57 +0000847 AddToMapCache(context, keys, map);
848 return Handle<Map>(map);
849}
850
851
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000852void Factory::SetRegExpAtomData(Handle<JSRegExp> regexp,
853 JSRegExp::Type type,
854 Handle<String> source,
855 JSRegExp::Flags flags,
856 Handle<Object> data) {
857 Handle<FixedArray> store = NewFixedArray(JSRegExp::kAtomDataSize);
858
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000859 store->set(JSRegExp::kTagIndex, Smi::FromInt(type));
860 store->set(JSRegExp::kSourceIndex, *source);
861 store->set(JSRegExp::kFlagsIndex, Smi::FromInt(flags.value()));
862 store->set(JSRegExp::kAtomPatternIndex, *data);
863 regexp->set_data(*store);
864}
865
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000866void Factory::SetRegExpIrregexpData(Handle<JSRegExp> regexp,
867 JSRegExp::Type type,
868 Handle<String> source,
869 JSRegExp::Flags flags,
870 int capture_count) {
871 Handle<FixedArray> store = NewFixedArray(JSRegExp::kIrregexpDataSize);
872
873 store->set(JSRegExp::kTagIndex, Smi::FromInt(type));
874 store->set(JSRegExp::kSourceIndex, *source);
875 store->set(JSRegExp::kFlagsIndex, Smi::FromInt(flags.value()));
876 store->set(JSRegExp::kIrregexpASCIICodeIndex, Heap::the_hole_value());
877 store->set(JSRegExp::kIrregexpUC16CodeIndex, Heap::the_hole_value());
878 store->set(JSRegExp::kIrregexpMaxRegisterCountIndex, Smi::FromInt(0));
879 store->set(JSRegExp::kIrregexpCaptureCountIndex,
880 Smi::FromInt(capture_count));
881 regexp->set_data(*store);
882}
883
884
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000885
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000886void Factory::ConfigureInstance(Handle<FunctionTemplateInfo> desc,
887 Handle<JSObject> instance,
888 bool* pending_exception) {
889 // Configure the instance by adding the properties specified by the
890 // instance template.
891 Handle<Object> instance_template = Handle<Object>(desc->instance_template());
892 if (!instance_template->IsUndefined()) {
893 Execution::ConfigureInstance(instance,
894 instance_template,
895 pending_exception);
896 } else {
897 *pending_exception = false;
898 }
899}
900
901
902} } // namespace v8::internal