blob: c49f58a9388d22996a9f3034e7b6a30323fcb482 [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.org870a0b62008-11-04 11:43:05 +000093 StringShape first_shape,
94 Handle<String> second,
95 StringShape second_shape) {
96 if (first->length(first_shape) == 0) return second;
97 if (second->length(second_shape) == 0) return first;
ager@chromium.orgc3e50d82008-11-05 11:53:10 +000098 CALL_HEAP_FUNCTION(Heap::AllocateConsString(*first, *second), String);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000099}
100
101
ager@chromium.org870a0b62008-11-04 11:43:05 +0000102Handle<String> Factory::NewStringSlice(Handle<String> str,
ager@chromium.org870a0b62008-11-04 11:43:05 +0000103 int begin,
104 int end) {
ager@chromium.orgc3e50d82008-11-05 11:53:10 +0000105 CALL_HEAP_FUNCTION(str->Slice(begin, end), String);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000106}
107
108
109Handle<String> Factory::NewExternalStringFromAscii(
110 ExternalAsciiString::Resource* resource) {
111 CALL_HEAP_FUNCTION(Heap::AllocateExternalStringFromAscii(resource), String);
112}
113
114
115Handle<String> Factory::NewExternalStringFromTwoByte(
116 ExternalTwoByteString::Resource* resource) {
117 CALL_HEAP_FUNCTION(Heap::AllocateExternalStringFromTwoByte(resource), String);
118}
119
120
121Handle<Context> Factory::NewGlobalContext() {
122 CALL_HEAP_FUNCTION(Heap::AllocateGlobalContext(), Context);
123}
124
125
126Handle<Context> Factory::NewFunctionContext(int length,
127 Handle<JSFunction> closure) {
128 CALL_HEAP_FUNCTION(Heap::AllocateFunctionContext(length, *closure), Context);
129}
130
131
132Handle<Context> Factory::NewWithContext(Handle<Context> previous,
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000133 Handle<JSObject> extension,
134 bool is_catch_context) {
135 CALL_HEAP_FUNCTION(Heap::AllocateWithContext(*previous,
136 *extension,
137 is_catch_context),
138 Context);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000139}
140
141
142Handle<Struct> Factory::NewStruct(InstanceType type) {
143 CALL_HEAP_FUNCTION(Heap::AllocateStruct(type), Struct);
144}
145
146
147Handle<AccessorInfo> Factory::NewAccessorInfo() {
148 Handle<AccessorInfo> info =
149 Handle<AccessorInfo>::cast(NewStruct(ACCESSOR_INFO_TYPE));
150 info->set_flag(0); // Must clear the flag, it was initialized as undefined.
151 return info;
152}
153
154
155Handle<Script> Factory::NewScript(Handle<String> source) {
156 Handle<Script> script = Handle<Script>::cast(NewStruct(SCRIPT_TYPE));
157 script->set_source(*source);
158 script->set_name(Heap::undefined_value());
159 script->set_line_offset(Smi::FromInt(0));
160 script->set_column_offset(Smi::FromInt(0));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000161 script->set_type(Smi::FromInt(SCRIPT_TYPE_NORMAL));
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000162 script->set_wrapper(*Factory::NewProxy(0, TENURED));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000163 return script;
164}
165
166
167Handle<Proxy> Factory::NewProxy(Address addr, PretenureFlag pretenure) {
168 CALL_HEAP_FUNCTION(Heap::AllocateProxy(addr, pretenure), Proxy);
169}
170
171
172Handle<Proxy> Factory::NewProxy(const AccessorDescriptor* desc) {
173 return NewProxy((Address) desc, TENURED);
174}
175
176
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000177Handle<ByteArray> Factory::NewByteArray(int length, PretenureFlag pretenure) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000178 ASSERT(0 <= length);
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000179 CALL_HEAP_FUNCTION(Heap::AllocateByteArray(length, pretenure), ByteArray);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000180}
181
182
183Handle<Map> Factory::NewMap(InstanceType type, int instance_size) {
184 CALL_HEAP_FUNCTION(Heap::AllocateMap(type, instance_size), Map);
185}
186
187
188Handle<JSObject> Factory::NewFunctionPrototype(Handle<JSFunction> function) {
189 CALL_HEAP_FUNCTION(Heap::AllocateFunctionPrototype(*function), JSObject);
190}
191
192
193Handle<Map> Factory::CopyMap(Handle<Map> src) {
194 CALL_HEAP_FUNCTION(src->Copy(), Map);
195}
196
197
ager@chromium.org32912102009-01-16 10:38:43 +0000198Handle<Map> Factory::CopyMap(Handle<Map> src,
199 int extra_inobject_properties) {
200 Handle<Map> copy = CopyMap(src);
201 // Check that we do not overflow the instance size when adding the
202 // extra inobject properties.
203 int instance_size_delta = extra_inobject_properties * kPointerSize;
204 int max_instance_size_delta =
205 JSObject::kMaxInstanceSize - copy->instance_size();
206 if (instance_size_delta > max_instance_size_delta) {
207 // If the instance size overflows, we allocate as many properties
208 // as we can as inobject properties.
209 instance_size_delta = max_instance_size_delta;
210 extra_inobject_properties = max_instance_size_delta >> kPointerSizeLog2;
211 }
212 // Adjust the map with the extra inobject properties.
213 int inobject_properties =
214 copy->inobject_properties() + extra_inobject_properties;
215 copy->set_inobject_properties(inobject_properties);
216 copy->set_unused_property_fields(inobject_properties);
217 copy->set_instance_size(copy->instance_size() + instance_size_delta);
218 return copy;
219}
220
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000221Handle<Map> Factory::CopyMapDropTransitions(Handle<Map> src) {
222 CALL_HEAP_FUNCTION(src->CopyDropTransitions(), Map);
223}
224
225
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000226Handle<FixedArray> Factory::CopyFixedArray(Handle<FixedArray> array) {
227 CALL_HEAP_FUNCTION(array->Copy(), FixedArray);
228}
229
230
231Handle<JSFunction> Factory::BaseNewFunctionFromBoilerplate(
232 Handle<JSFunction> boilerplate,
233 Handle<Map> function_map) {
234 ASSERT(boilerplate->IsBoilerplate());
235 ASSERT(!boilerplate->has_initial_map());
236 ASSERT(!boilerplate->has_prototype());
237 ASSERT(boilerplate->properties() == Heap::empty_fixed_array());
238 ASSERT(boilerplate->elements() == Heap::empty_fixed_array());
239 CALL_HEAP_FUNCTION(Heap::AllocateFunction(*function_map,
240 boilerplate->shared(),
241 Heap::the_hole_value()),
242 JSFunction);
243}
244
245
246Handle<JSFunction> Factory::NewFunctionFromBoilerplate(
247 Handle<JSFunction> boilerplate,
248 Handle<Context> context) {
249 Handle<JSFunction> result =
250 BaseNewFunctionFromBoilerplate(boilerplate, Top::function_map());
251 result->set_context(*context);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000252 int number_of_literals = boilerplate->NumberOfLiterals();
253 Handle<FixedArray> literals =
254 Factory::NewFixedArray(number_of_literals, TENURED);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000255 if (number_of_literals > 0) {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000256 // Store the object, regexp and array functions in the literals
257 // array prefix. These functions will be used when creating
258 // object, regexp and array literals in this function.
ager@chromium.org236ad962008-09-25 09:45:57 +0000259 literals->set(JSFunction::kLiteralGlobalContextIndex,
260 context->global_context());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000261 }
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000262 result->set_literals(*literals);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000263 ASSERT(!result->IsBoilerplate());
264 return result;
265}
266
267
268Handle<Object> Factory::NewNumber(double value,
269 PretenureFlag pretenure) {
270 CALL_HEAP_FUNCTION(Heap::NumberFromDouble(value, pretenure), Object);
271}
272
273
274Handle<Object> Factory::NewNumberFromInt(int value) {
275 CALL_HEAP_FUNCTION(Heap::NumberFromInt32(value), Object);
276}
277
278
279Handle<JSObject> Factory::NewNeanderObject() {
280 CALL_HEAP_FUNCTION(Heap::AllocateJSObjectFromMap(Heap::neander_map()),
281 JSObject);
282}
283
284
285Handle<Object> Factory::NewTypeError(const char* type,
286 Vector< Handle<Object> > args) {
287 return NewError("MakeTypeError", type, args);
288}
289
290
291Handle<Object> Factory::NewTypeError(Handle<String> message) {
292 return NewError("$TypeError", message);
293}
294
295
296Handle<Object> Factory::NewRangeError(const char* type,
297 Vector< Handle<Object> > args) {
298 return NewError("MakeRangeError", type, args);
299}
300
301
302Handle<Object> Factory::NewRangeError(Handle<String> message) {
303 return NewError("$RangeError", message);
304}
305
306
307Handle<Object> Factory::NewSyntaxError(const char* type, Handle<JSArray> args) {
308 return NewError("MakeSyntaxError", type, args);
309}
310
311
312Handle<Object> Factory::NewSyntaxError(Handle<String> message) {
313 return NewError("$SyntaxError", message);
314}
315
316
317Handle<Object> Factory::NewReferenceError(const char* type,
318 Vector< Handle<Object> > args) {
319 return NewError("MakeReferenceError", type, args);
320}
321
322
323Handle<Object> Factory::NewReferenceError(Handle<String> message) {
324 return NewError("$ReferenceError", message);
325}
326
327
328Handle<Object> Factory::NewError(const char* maker, const char* type,
329 Vector< Handle<Object> > args) {
330 HandleScope scope;
ager@chromium.org7c537e22008-10-16 08:43:32 +0000331 Handle<FixedArray> array = Factory::NewFixedArray(args.length());
332 for (int i = 0; i < args.length(); i++) {
333 array->set(i, *args[i]);
334 }
335 Handle<JSArray> object = Factory::NewJSArrayWithElements(array);
336 Handle<Object> result = NewError(maker, type, object);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000337 return result.EscapeFrom(&scope);
338}
339
340
341Handle<Object> Factory::NewEvalError(const char* type,
342 Vector< Handle<Object> > args) {
343 return NewError("MakeEvalError", type, args);
344}
345
346
347Handle<Object> Factory::NewError(const char* type,
348 Vector< Handle<Object> > args) {
349 return NewError("MakeError", type, args);
350}
351
352
353Handle<Object> Factory::NewError(const char* maker,
354 const char* type,
355 Handle<JSArray> args) {
356 Handle<String> make_str = Factory::LookupAsciiSymbol(maker);
357 Handle<JSFunction> fun =
358 Handle<JSFunction>(
359 JSFunction::cast(
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000360 Top::builtins()->GetProperty(*make_str)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000361 Handle<Object> type_obj = Factory::LookupAsciiSymbol(type);
362 Object** argv[2] = { type_obj.location(),
363 Handle<Object>::cast(args).location() };
364
365 // Invoke the JavaScript factory method. If an exception is thrown while
366 // running the factory method, use the exception as the result.
367 bool caught_exception;
368 Handle<Object> result = Execution::TryCall(fun,
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000369 Top::builtins(),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000370 2,
371 argv,
372 &caught_exception);
373 return result;
374}
375
376
377Handle<Object> Factory::NewError(Handle<String> message) {
378 return NewError("$Error", message);
379}
380
381
382Handle<Object> Factory::NewError(const char* constructor,
383 Handle<String> message) {
384 Handle<String> constr = Factory::LookupAsciiSymbol(constructor);
385 Handle<JSFunction> fun =
386 Handle<JSFunction>(
387 JSFunction::cast(
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000388 Top::builtins()->GetProperty(*constr)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000389 Object** argv[1] = { Handle<Object>::cast(message).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 1,
397 argv,
398 &caught_exception);
399 return result;
400}
401
402
403Handle<JSFunction> Factory::NewFunction(Handle<String> name,
404 InstanceType type,
405 int instance_size,
406 Handle<Code> code,
407 bool force_initial_map) {
408 // Allocate the function
409 Handle<JSFunction> function = NewFunction(name, the_hole_value());
410 function->set_code(*code);
411
412 if (force_initial_map ||
413 type != JS_OBJECT_TYPE ||
414 instance_size != JSObject::kHeaderSize) {
415 Handle<Map> initial_map = NewMap(type, instance_size);
416 Handle<JSObject> prototype = NewFunctionPrototype(function);
417 initial_map->set_prototype(*prototype);
418 function->set_initial_map(*initial_map);
419 initial_map->set_constructor(*function);
420 } else {
421 ASSERT(!function->has_initial_map());
422 ASSERT(!function->has_prototype());
423 }
424
425 return function;
426}
427
428
429Handle<JSFunction> Factory::NewFunctionBoilerplate(Handle<String> name,
430 int number_of_literals,
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000431 bool contains_array_literal,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000432 Handle<Code> code) {
433 Handle<JSFunction> function = NewFunctionBoilerplate(name);
434 function->set_code(*code);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000435 int literals_array_size = number_of_literals;
436 // If the function contains object, regexp or array literals,
437 // allocate extra space for a literals array prefix containing the
438 // object, regexp and array constructor functions.
439 if (number_of_literals > 0 || contains_array_literal) {
440 literals_array_size += JSFunction::kLiteralsPrefixSize;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000441 }
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000442 Handle<FixedArray> literals =
443 Factory::NewFixedArray(literals_array_size, TENURED);
444 function->set_literals(*literals);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000445 ASSERT(!function->has_initial_map());
446 ASSERT(!function->has_prototype());
447 return function;
448}
449
450
451Handle<JSFunction> Factory::NewFunctionBoilerplate(Handle<String> name) {
452 Handle<SharedFunctionInfo> shared = NewSharedFunctionInfo(name);
453 CALL_HEAP_FUNCTION(Heap::AllocateFunction(Heap::boilerplate_function_map(),
454 *shared,
455 Heap::the_hole_value()),
456 JSFunction);
457}
458
459
460Handle<JSFunction> Factory::NewFunctionWithPrototype(Handle<String> name,
461 InstanceType type,
462 int instance_size,
463 Handle<JSObject> prototype,
464 Handle<Code> code,
465 bool force_initial_map) {
466 // Allocate the function
467 Handle<JSFunction> function = NewFunction(name, prototype);
468
469 function->set_code(*code);
470
471 if (force_initial_map ||
472 type != JS_OBJECT_TYPE ||
473 instance_size != JSObject::kHeaderSize) {
474 Handle<Map> initial_map = NewMap(type, instance_size);
475 function->set_initial_map(*initial_map);
476 initial_map->set_constructor(*function);
477 }
478
479 // Set function.prototype and give the prototype a constructor
480 // property that refers to the function.
481 SetPrototypeProperty(function, prototype);
482 SetProperty(prototype, Factory::constructor_symbol(), function, DONT_ENUM);
483 return function;
484}
485
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000486
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000487Handle<Code> Factory::NewCode(const CodeDesc& desc, ScopeInfo<>* sinfo,
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000488 Code::Flags flags, Handle<Object> self_ref) {
489 CALL_HEAP_FUNCTION(Heap::CreateCode(
490 desc, sinfo, flags, reinterpret_cast<Code**>(self_ref.location())), Code);
491}
492
493Handle<Code> Factory::NewCode(const CodeDesc& desc, ScopeInfo<>* sinfo,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000494 Code::Flags flags) {
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000495 CALL_HEAP_FUNCTION(Heap::CreateCode(desc, sinfo, flags, NULL), Code);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000496}
497
498
499Handle<Code> Factory::CopyCode(Handle<Code> code) {
500 CALL_HEAP_FUNCTION(Heap::CopyCode(*code), Code);
501}
502
503
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000504static inline Object* DoCopyInsert(DescriptorArray* array,
505 String* key,
506 Object* value,
507 PropertyAttributes attributes) {
508 CallbacksDescriptor desc(key, value, attributes);
509 Object* obj = array->CopyInsert(&desc, REMOVE_TRANSITIONS);
510 return obj;
511}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000512
513
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000514// Allocate the new array.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000515Handle<DescriptorArray> Factory::CopyAppendProxyDescriptor(
516 Handle<DescriptorArray> array,
517 Handle<String> key,
518 Handle<Object> value,
519 PropertyAttributes attributes) {
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000520 CALL_HEAP_FUNCTION(DoCopyInsert(*array, *key, *value, attributes),
521 DescriptorArray);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000522}
523
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000524
525Handle<String> Factory::SymbolFromString(Handle<String> value) {
526 CALL_HEAP_FUNCTION(Heap::LookupSymbol(*value), String);
527}
528
529
530Handle<DescriptorArray> Factory::CopyAppendCallbackDescriptors(
531 Handle<DescriptorArray> array,
532 Handle<Object> descriptors) {
533 v8::NeanderArray callbacks(descriptors);
534 int nof_callbacks = callbacks.length();
535 Handle<DescriptorArray> result =
536 NewDescriptorArray(array->number_of_descriptors() + nof_callbacks);
537
538 // Number of descriptors added to the result so far.
539 int descriptor_count = 0;
540
541 // Copy the descriptors from the array.
542 DescriptorWriter w(*result);
543 for (DescriptorReader r(*array); !r.eos(); r.advance()) {
544 w.WriteFrom(&r);
545 descriptor_count++;
546 }
547
548 // Number of duplicates detected.
549 int duplicates = 0;
550
551 // Fill in new callback descriptors. Process the callbacks from
552 // back to front so that the last callback with a given name takes
553 // precedence over previously added callbacks with that name.
554 for (int i = nof_callbacks - 1; i >= 0; i--) {
555 Handle<AccessorInfo> entry =
556 Handle<AccessorInfo>(AccessorInfo::cast(callbacks.get(i)));
557 // Ensure the key is a symbol before writing into the instance descriptor.
558 Handle<String> key =
559 SymbolFromString(Handle<String>(String::cast(entry->name())));
560 // Check if a descriptor with this name already exists before writing.
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000561 if (result->LinearSearch(*key, descriptor_count) ==
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000562 DescriptorArray::kNotFound) {
563 CallbacksDescriptor desc(*key, *entry, entry->property_attributes());
564 w.Write(&desc);
565 descriptor_count++;
566 } else {
567 duplicates++;
568 }
569 }
570
571 // If duplicates were detected, allocate a result of the right size
572 // and transfer the elements.
573 if (duplicates > 0) {
574 Handle<DescriptorArray> new_result =
575 NewDescriptorArray(result->number_of_descriptors() - duplicates);
576 DescriptorWriter w(*new_result);
577 DescriptorReader r(*result);
578 while (!w.eos()) {
579 w.WriteFrom(&r);
580 r.advance();
581 }
582 result = new_result;
583 }
584
585 // Sort the result before returning.
586 result->Sort();
587 return result;
588}
589
590
591Handle<JSObject> Factory::NewJSObject(Handle<JSFunction> constructor,
592 PretenureFlag pretenure) {
593 CALL_HEAP_FUNCTION(Heap::AllocateJSObject(*constructor, pretenure), JSObject);
594}
595
596
ager@chromium.org236ad962008-09-25 09:45:57 +0000597Handle<JSObject> Factory::NewJSObjectFromMap(Handle<Map> map) {
598 CALL_HEAP_FUNCTION(Heap::AllocateJSObjectFromMap(*map, NOT_TENURED),
599 JSObject);
600}
601
602
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000603Handle<JSArray> Factory::NewArrayLiteral(int length) {
604 return NewJSArrayWithElements(NewFixedArray(length), TENURED);
605}
606
607
608Handle<JSArray> Factory::NewJSArray(int length,
609 PretenureFlag pretenure) {
610 Handle<JSObject> obj = NewJSObject(Top::array_function(), pretenure);
611 CALL_HEAP_FUNCTION(Handle<JSArray>::cast(obj)->Initialize(length), JSArray);
612}
613
614
615Handle<JSArray> Factory::NewJSArrayWithElements(Handle<FixedArray> elements,
616 PretenureFlag pretenure) {
617 Handle<JSArray> result =
618 Handle<JSArray>::cast(NewJSObject(Top::array_function(), pretenure));
619 result->SetContent(*elements);
620 return result;
621}
622
623
624Handle<SharedFunctionInfo> Factory::NewSharedFunctionInfo(Handle<String> name) {
625 CALL_HEAP_FUNCTION(Heap::AllocateSharedFunctionInfo(*name),
626 SharedFunctionInfo);
627}
628
629
630Handle<Dictionary> Factory::DictionaryAtNumberPut(Handle<Dictionary> dictionary,
631 uint32_t key,
632 Handle<Object> value) {
633 CALL_HEAP_FUNCTION(dictionary->AtNumberPut(key, *value), Dictionary);
634}
635
636
637Handle<JSFunction> Factory::NewFunctionHelper(Handle<String> name,
638 Handle<Object> prototype) {
639 Handle<SharedFunctionInfo> function_share = NewSharedFunctionInfo(name);
640 CALL_HEAP_FUNCTION(Heap::AllocateFunction(*Top::function_map(),
641 *function_share,
642 *prototype),
643 JSFunction);
644}
645
646
647Handle<JSFunction> Factory::NewFunction(Handle<String> name,
648 Handle<Object> prototype) {
649 Handle<JSFunction> fun = NewFunctionHelper(name, prototype);
650 fun->set_context(Top::context()->global_context());
651 return fun;
652}
653
654
655Handle<Object> Factory::ToObject(Handle<Object> object,
656 Handle<Context> global_context) {
657 CALL_HEAP_FUNCTION(object->ToObject(*global_context), Object);
658}
659
660
v8.team.kasperl727e9952008-09-02 14:56:44 +0000661Handle<DebugInfo> Factory::NewDebugInfo(Handle<SharedFunctionInfo> shared) {
662 // Get the original code of the function.
663 Handle<Code> code(shared->code());
664
665 // Create a copy of the code before allocating the debug info object to avoid
666 // allocation while setting up the debug info object.
667 Handle<Code> original_code(*Factory::CopyCode(code));
668
669 // Allocate initial fixed array for active break points before allocating the
670 // debug info object to avoid allocation while setting up the debug info
671 // object.
672 Handle<FixedArray> break_points(
673 Factory::NewFixedArray(Debug::kEstimatedNofBreakPointsInFunction));
674
675 // Create and set up the debug info object. Debug info contains function, a
676 // copy of the original code, the executing code and initial fixed array for
677 // active break points.
678 Handle<DebugInfo> debug_info =
679 Handle<DebugInfo>::cast(Factory::NewStruct(DEBUG_INFO_TYPE));
680 debug_info->set_shared(*shared);
681 debug_info->set_original_code(*original_code);
682 debug_info->set_code(*code);
683 debug_info->set_break_points(*break_points);
684
685 // Link debug info to function.
686 shared->set_debug_info(*debug_info);
687
688 return debug_info;
689}
690
691
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000692Handle<JSObject> Factory::NewArgumentsObject(Handle<Object> callee,
693 int length) {
694 CALL_HEAP_FUNCTION(Heap::AllocateArgumentsObject(*callee, length), JSObject);
695}
696
697
698Handle<JSFunction> Factory::CreateApiFunction(
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000699 Handle<FunctionTemplateInfo> obj, ApiInstanceType instance_type) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000700 Handle<Code> code = Handle<Code>(Builtins::builtin(Builtins::HandleApiCall));
701
kasper.lund212ac232008-07-16 07:07:30 +0000702 int internal_field_count = 0;
703 if (!obj->instance_template()->IsUndefined()) {
704 Handle<ObjectTemplateInfo> instance_template =
705 Handle<ObjectTemplateInfo>(
706 ObjectTemplateInfo::cast(obj->instance_template()));
707 internal_field_count =
708 Smi::cast(instance_template->internal_field_count())->value();
709 }
710
711 int instance_size = kPointerSize * internal_field_count;
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000712 InstanceType type = INVALID_TYPE;
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000713 switch (instance_type) {
714 case JavaScriptObject:
715 type = JS_OBJECT_TYPE;
716 instance_size += JSObject::kHeaderSize;
717 break;
718 case InnerGlobalObject:
719 type = JS_GLOBAL_OBJECT_TYPE;
720 instance_size += JSGlobalObject::kSize;
721 break;
722 case OuterGlobalObject:
723 type = JS_GLOBAL_PROXY_TYPE;
724 instance_size += JSGlobalProxy::kSize;
725 break;
726 default:
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000727 break;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000728 }
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000729 ASSERT(type != INVALID_TYPE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000730
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000731 Handle<JSFunction> result =
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000732 Factory::NewFunction(Factory::empty_symbol(),
733 type,
734 instance_size,
735 code,
736 true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000737 // Set class name.
738 Handle<Object> class_name = Handle<Object>(obj->class_name());
739 if (class_name->IsString()) {
740 result->shared()->set_instance_class_name(*class_name);
741 result->shared()->set_name(*class_name);
742 }
743
744 Handle<Map> map = Handle<Map>(result->initial_map());
745
746 // Mark as undetectable if needed.
747 if (obj->undetectable()) {
748 map->set_is_undetectable();
749 }
750
751 // Mark as hidden for the __proto__ accessor if needed.
752 if (obj->hidden_prototype()) {
753 map->set_is_hidden_prototype();
754 }
755
756 // Mark as needs_access_check if needed.
757 if (obj->needs_access_check()) {
ager@chromium.org870a0b62008-11-04 11:43:05 +0000758 map->set_is_access_check_needed(true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000759 }
760
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000761 // Set interceptor information in the map.
762 if (!obj->named_property_handler()->IsUndefined()) {
763 map->set_has_named_interceptor();
764 }
765 if (!obj->indexed_property_handler()->IsUndefined()) {
766 map->set_has_indexed_interceptor();
767 }
768
769 // Set instance call-as-function information in the map.
770 if (!obj->instance_call_handler()->IsUndefined()) {
771 map->set_has_instance_call_handler();
772 }
773
774 result->shared()->set_function_data(*obj);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000775 result->shared()->DontAdaptArguments();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000776
777 // Recursively copy parent templates' accessors, 'data' may be modified.
778 Handle<DescriptorArray> array =
779 Handle<DescriptorArray>(map->instance_descriptors());
780 while (true) {
781 Handle<Object> props = Handle<Object>(obj->property_accessors());
782 if (!props->IsUndefined()) {
783 array = Factory::CopyAppendCallbackDescriptors(array, props);
784 }
785 Handle<Object> parent = Handle<Object>(obj->parent_template());
786 if (parent->IsUndefined()) break;
787 obj = Handle<FunctionTemplateInfo>::cast(parent);
788 }
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000789 if (!array->IsEmpty()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000790 map->set_instance_descriptors(*array);
791 }
792
793 return result;
794}
795
796
ager@chromium.org236ad962008-09-25 09:45:57 +0000797Handle<MapCache> Factory::NewMapCache(int at_least_space_for) {
798 CALL_HEAP_FUNCTION(MapCache::Allocate(at_least_space_for), MapCache);
799}
800
801
802static Object* UpdateMapCacheWith(Context* context,
803 FixedArray* keys,
804 Map* map) {
805 Object* result = MapCache::cast(context->map_cache())->Put(keys, map);
806 if (!result->IsFailure()) context->set_map_cache(MapCache::cast(result));
807 return result;
808}
809
810
811Handle<MapCache> Factory::AddToMapCache(Handle<Context> context,
812 Handle<FixedArray> keys,
813 Handle<Map> map) {
814 CALL_HEAP_FUNCTION(UpdateMapCacheWith(*context, *keys, *map), MapCache);
815}
816
817
818Handle<Map> Factory::ObjectLiteralMapFromCache(Handle<Context> context,
819 Handle<FixedArray> keys) {
820 if (context->map_cache()->IsUndefined()) {
821 // Allocate the new map cache for the global context.
822 Handle<MapCache> new_cache = NewMapCache(24);
823 context->set_map_cache(*new_cache);
824 }
ager@chromium.org32912102009-01-16 10:38:43 +0000825 // Check to see whether there is a matching element in the cache.
ager@chromium.org236ad962008-09-25 09:45:57 +0000826 Handle<MapCache> cache =
827 Handle<MapCache>(MapCache::cast(context->map_cache()));
828 Handle<Object> result = Handle<Object>(cache->Lookup(*keys));
829 if (result->IsMap()) return Handle<Map>::cast(result);
830 // Create a new map and add it to the cache.
831 Handle<Map> map =
ager@chromium.org32912102009-01-16 10:38:43 +0000832 CopyMap(Handle<Map>(context->object_function()->initial_map()),
833 keys->length());
ager@chromium.org236ad962008-09-25 09:45:57 +0000834 AddToMapCache(context, keys, map);
835 return Handle<Map>(map);
836}
837
838
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000839void Factory::SetRegExpData(Handle<JSRegExp> regexp,
840 JSRegExp::Type type,
841 Handle<String> source,
842 JSRegExp::Flags flags,
843 Handle<Object> data) {
844 Handle<FixedArray> store = NewFixedArray(JSRegExp::kDataSize);
845 store->set(JSRegExp::kTagIndex, Smi::FromInt(type));
846 store->set(JSRegExp::kSourceIndex, *source);
847 store->set(JSRegExp::kFlagsIndex, Smi::FromInt(flags.value()));
848 store->set(JSRegExp::kAtomPatternIndex, *data);
849 regexp->set_data(*store);
850}
851
852
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000853void Factory::ConfigureInstance(Handle<FunctionTemplateInfo> desc,
854 Handle<JSObject> instance,
855 bool* pending_exception) {
856 // Configure the instance by adding the properties specified by the
857 // instance template.
858 Handle<Object> instance_template = Handle<Object>(desc->instance_template());
859 if (!instance_template->IsUndefined()) {
860 Execution::ConfigureInstance(instance,
861 instance_template,
862 pending_exception);
863 } else {
864 *pending_exception = false;
865 }
866}
867
868
869} } // namespace v8::internal