blob: fad3e9c281c05fb40bab83c1e0af0ba1c90c47ef [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());
ager@chromium.orge2902be2009-06-08 12:21:35 +0000180 script->set_type(Smi::FromInt(Script::TYPE_NORMAL));
181 script->set_compilation_type(Smi::FromInt(Script::COMPILATION_TYPE_HOST));
ager@chromium.org9085a012009-05-11 19:22:57 +0000182 script->set_wrapper(*wrapper);
iposva@chromium.org245aa852009-02-10 00:49:54 +0000183 script->set_line_ends(Heap::undefined_value());
ager@chromium.orge2902be2009-06-08 12:21:35 +0000184 script->set_eval_from_function(Heap::undefined_value());
185 script->set_eval_from_instructions_offset(Smi::FromInt(0));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000186
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000187 return script;
188}
189
190
191Handle<Proxy> Factory::NewProxy(Address addr, PretenureFlag pretenure) {
192 CALL_HEAP_FUNCTION(Heap::AllocateProxy(addr, pretenure), Proxy);
193}
194
195
196Handle<Proxy> Factory::NewProxy(const AccessorDescriptor* desc) {
197 return NewProxy((Address) desc, TENURED);
198}
199
200
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000201Handle<ByteArray> Factory::NewByteArray(int length, PretenureFlag pretenure) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000202 ASSERT(0 <= length);
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000203 CALL_HEAP_FUNCTION(Heap::AllocateByteArray(length, pretenure), ByteArray);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000204}
205
206
207Handle<Map> Factory::NewMap(InstanceType type, int instance_size) {
208 CALL_HEAP_FUNCTION(Heap::AllocateMap(type, instance_size), Map);
209}
210
211
212Handle<JSObject> Factory::NewFunctionPrototype(Handle<JSFunction> function) {
213 CALL_HEAP_FUNCTION(Heap::AllocateFunctionPrototype(*function), JSObject);
214}
215
216
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000217Handle<Map> Factory::CopyMapDropDescriptors(Handle<Map> src) {
218 CALL_HEAP_FUNCTION(src->CopyDropDescriptors(), Map);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000219}
220
221
ager@chromium.org32912102009-01-16 10:38:43 +0000222Handle<Map> Factory::CopyMap(Handle<Map> src,
223 int extra_inobject_properties) {
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000224 Handle<Map> copy = CopyMapDropDescriptors(src);
ager@chromium.org32912102009-01-16 10:38:43 +0000225 // Check that we do not overflow the instance size when adding the
226 // extra inobject properties.
227 int instance_size_delta = extra_inobject_properties * kPointerSize;
228 int max_instance_size_delta =
229 JSObject::kMaxInstanceSize - copy->instance_size();
230 if (instance_size_delta > max_instance_size_delta) {
231 // If the instance size overflows, we allocate as many properties
232 // as we can as inobject properties.
233 instance_size_delta = max_instance_size_delta;
234 extra_inobject_properties = max_instance_size_delta >> kPointerSizeLog2;
235 }
236 // Adjust the map with the extra inobject properties.
237 int inobject_properties =
238 copy->inobject_properties() + extra_inobject_properties;
239 copy->set_inobject_properties(inobject_properties);
240 copy->set_unused_property_fields(inobject_properties);
241 copy->set_instance_size(copy->instance_size() + instance_size_delta);
242 return copy;
243}
244
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000245Handle<Map> Factory::CopyMapDropTransitions(Handle<Map> src) {
246 CALL_HEAP_FUNCTION(src->CopyDropTransitions(), Map);
247}
248
249
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000250Handle<FixedArray> Factory::CopyFixedArray(Handle<FixedArray> array) {
251 CALL_HEAP_FUNCTION(array->Copy(), FixedArray);
252}
253
254
255Handle<JSFunction> Factory::BaseNewFunctionFromBoilerplate(
256 Handle<JSFunction> boilerplate,
257 Handle<Map> function_map) {
258 ASSERT(boilerplate->IsBoilerplate());
259 ASSERT(!boilerplate->has_initial_map());
260 ASSERT(!boilerplate->has_prototype());
261 ASSERT(boilerplate->properties() == Heap::empty_fixed_array());
262 ASSERT(boilerplate->elements() == Heap::empty_fixed_array());
263 CALL_HEAP_FUNCTION(Heap::AllocateFunction(*function_map,
264 boilerplate->shared(),
265 Heap::the_hole_value()),
266 JSFunction);
267}
268
269
270Handle<JSFunction> Factory::NewFunctionFromBoilerplate(
271 Handle<JSFunction> boilerplate,
272 Handle<Context> context) {
273 Handle<JSFunction> result =
274 BaseNewFunctionFromBoilerplate(boilerplate, Top::function_map());
275 result->set_context(*context);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000276 int number_of_literals = boilerplate->NumberOfLiterals();
277 Handle<FixedArray> literals =
278 Factory::NewFixedArray(number_of_literals, TENURED);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000279 if (number_of_literals > 0) {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000280 // Store the object, regexp and array functions in the literals
281 // array prefix. These functions will be used when creating
282 // object, regexp and array literals in this function.
ager@chromium.org236ad962008-09-25 09:45:57 +0000283 literals->set(JSFunction::kLiteralGlobalContextIndex,
284 context->global_context());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000285 }
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000286 result->set_literals(*literals);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000287 ASSERT(!result->IsBoilerplate());
288 return result;
289}
290
291
292Handle<Object> Factory::NewNumber(double value,
293 PretenureFlag pretenure) {
294 CALL_HEAP_FUNCTION(Heap::NumberFromDouble(value, pretenure), Object);
295}
296
297
298Handle<Object> Factory::NewNumberFromInt(int value) {
299 CALL_HEAP_FUNCTION(Heap::NumberFromInt32(value), Object);
300}
301
302
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000303Handle<Object> Factory::NewNumberFromUint(uint32_t value) {
304 CALL_HEAP_FUNCTION(Heap::NumberFromUint32(value), Object);
305}
306
307
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000308Handle<JSObject> Factory::NewNeanderObject() {
309 CALL_HEAP_FUNCTION(Heap::AllocateJSObjectFromMap(Heap::neander_map()),
310 JSObject);
311}
312
313
314Handle<Object> Factory::NewTypeError(const char* type,
315 Vector< Handle<Object> > args) {
316 return NewError("MakeTypeError", type, args);
317}
318
319
320Handle<Object> Factory::NewTypeError(Handle<String> message) {
321 return NewError("$TypeError", message);
322}
323
324
325Handle<Object> Factory::NewRangeError(const char* type,
326 Vector< Handle<Object> > args) {
327 return NewError("MakeRangeError", type, args);
328}
329
330
331Handle<Object> Factory::NewRangeError(Handle<String> message) {
332 return NewError("$RangeError", message);
333}
334
335
336Handle<Object> Factory::NewSyntaxError(const char* type, Handle<JSArray> args) {
337 return NewError("MakeSyntaxError", type, args);
338}
339
340
341Handle<Object> Factory::NewSyntaxError(Handle<String> message) {
342 return NewError("$SyntaxError", message);
343}
344
345
346Handle<Object> Factory::NewReferenceError(const char* type,
347 Vector< Handle<Object> > args) {
348 return NewError("MakeReferenceError", type, args);
349}
350
351
352Handle<Object> Factory::NewReferenceError(Handle<String> message) {
353 return NewError("$ReferenceError", message);
354}
355
356
357Handle<Object> Factory::NewError(const char* maker, const char* type,
358 Vector< Handle<Object> > args) {
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000359 v8::HandleScope scope; // Instantiate a closeable HandleScope for EscapeFrom.
ager@chromium.org7c537e22008-10-16 08:43:32 +0000360 Handle<FixedArray> array = Factory::NewFixedArray(args.length());
361 for (int i = 0; i < args.length(); i++) {
362 array->set(i, *args[i]);
363 }
364 Handle<JSArray> object = Factory::NewJSArrayWithElements(array);
365 Handle<Object> result = NewError(maker, type, object);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000366 return result.EscapeFrom(&scope);
367}
368
369
370Handle<Object> Factory::NewEvalError(const char* type,
371 Vector< Handle<Object> > args) {
372 return NewError("MakeEvalError", type, args);
373}
374
375
376Handle<Object> Factory::NewError(const char* type,
377 Vector< Handle<Object> > args) {
378 return NewError("MakeError", type, args);
379}
380
381
382Handle<Object> Factory::NewError(const char* maker,
383 const char* type,
384 Handle<JSArray> args) {
385 Handle<String> make_str = Factory::LookupAsciiSymbol(maker);
386 Handle<JSFunction> fun =
387 Handle<JSFunction>(
388 JSFunction::cast(
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000389 Top::builtins()->GetProperty(*make_str)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000390 Handle<Object> type_obj = Factory::LookupAsciiSymbol(type);
391 Object** argv[2] = { type_obj.location(),
392 Handle<Object>::cast(args).location() };
393
394 // Invoke the JavaScript factory method. If an exception is thrown while
395 // running the factory method, use the exception as the result.
396 bool caught_exception;
397 Handle<Object> result = Execution::TryCall(fun,
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000398 Top::builtins(),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000399 2,
400 argv,
401 &caught_exception);
402 return result;
403}
404
405
406Handle<Object> Factory::NewError(Handle<String> message) {
407 return NewError("$Error", message);
408}
409
410
411Handle<Object> Factory::NewError(const char* constructor,
412 Handle<String> message) {
413 Handle<String> constr = Factory::LookupAsciiSymbol(constructor);
414 Handle<JSFunction> fun =
415 Handle<JSFunction>(
416 JSFunction::cast(
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000417 Top::builtins()->GetProperty(*constr)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000418 Object** argv[1] = { Handle<Object>::cast(message).location() };
419
420 // Invoke the JavaScript factory method. If an exception is thrown while
421 // running the factory method, use the exception as the result.
422 bool caught_exception;
423 Handle<Object> result = Execution::TryCall(fun,
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000424 Top::builtins(),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000425 1,
426 argv,
427 &caught_exception);
428 return result;
429}
430
431
432Handle<JSFunction> Factory::NewFunction(Handle<String> name,
433 InstanceType type,
434 int instance_size,
435 Handle<Code> code,
436 bool force_initial_map) {
437 // Allocate the function
438 Handle<JSFunction> function = NewFunction(name, the_hole_value());
439 function->set_code(*code);
440
441 if (force_initial_map ||
442 type != JS_OBJECT_TYPE ||
443 instance_size != JSObject::kHeaderSize) {
444 Handle<Map> initial_map = NewMap(type, instance_size);
445 Handle<JSObject> prototype = NewFunctionPrototype(function);
446 initial_map->set_prototype(*prototype);
447 function->set_initial_map(*initial_map);
448 initial_map->set_constructor(*function);
449 } else {
450 ASSERT(!function->has_initial_map());
451 ASSERT(!function->has_prototype());
452 }
453
454 return function;
455}
456
457
458Handle<JSFunction> Factory::NewFunctionBoilerplate(Handle<String> name,
459 int number_of_literals,
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000460 bool contains_array_literal,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000461 Handle<Code> code) {
462 Handle<JSFunction> function = NewFunctionBoilerplate(name);
463 function->set_code(*code);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000464 int literals_array_size = number_of_literals;
465 // If the function contains object, regexp or array literals,
466 // allocate extra space for a literals array prefix containing the
467 // object, regexp and array constructor functions.
468 if (number_of_literals > 0 || contains_array_literal) {
469 literals_array_size += JSFunction::kLiteralsPrefixSize;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000470 }
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000471 Handle<FixedArray> literals =
472 Factory::NewFixedArray(literals_array_size, TENURED);
473 function->set_literals(*literals);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000474 ASSERT(!function->has_initial_map());
475 ASSERT(!function->has_prototype());
476 return function;
477}
478
479
480Handle<JSFunction> Factory::NewFunctionBoilerplate(Handle<String> name) {
481 Handle<SharedFunctionInfo> shared = NewSharedFunctionInfo(name);
482 CALL_HEAP_FUNCTION(Heap::AllocateFunction(Heap::boilerplate_function_map(),
483 *shared,
484 Heap::the_hole_value()),
485 JSFunction);
486}
487
488
489Handle<JSFunction> Factory::NewFunctionWithPrototype(Handle<String> name,
490 InstanceType type,
491 int instance_size,
492 Handle<JSObject> prototype,
493 Handle<Code> code,
494 bool force_initial_map) {
495 // Allocate the function
496 Handle<JSFunction> function = NewFunction(name, prototype);
497
498 function->set_code(*code);
499
500 if (force_initial_map ||
501 type != JS_OBJECT_TYPE ||
502 instance_size != JSObject::kHeaderSize) {
503 Handle<Map> initial_map = NewMap(type, instance_size);
504 function->set_initial_map(*initial_map);
505 initial_map->set_constructor(*function);
506 }
507
508 // Set function.prototype and give the prototype a constructor
509 // property that refers to the function.
510 SetPrototypeProperty(function, prototype);
511 SetProperty(prototype, Factory::constructor_symbol(), function, DONT_ENUM);
512 return function;
513}
514
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000515
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000516Handle<Code> Factory::NewCode(const CodeDesc& desc,
517 ZoneScopeInfo* sinfo,
518 Code::Flags flags,
519 Handle<Object> self_ref) {
kasperl@chromium.org061ef742009-02-27 12:16:20 +0000520 CALL_HEAP_FUNCTION(Heap::CreateCode(desc, sinfo, flags, self_ref), Code);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000521}
522
523
524Handle<Code> Factory::CopyCode(Handle<Code> code) {
525 CALL_HEAP_FUNCTION(Heap::CopyCode(*code), Code);
526}
527
528
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000529static inline Object* DoCopyInsert(DescriptorArray* array,
530 String* key,
531 Object* value,
532 PropertyAttributes attributes) {
533 CallbacksDescriptor desc(key, value, attributes);
534 Object* obj = array->CopyInsert(&desc, REMOVE_TRANSITIONS);
535 return obj;
536}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000537
538
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000539// Allocate the new array.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000540Handle<DescriptorArray> Factory::CopyAppendProxyDescriptor(
541 Handle<DescriptorArray> array,
542 Handle<String> key,
543 Handle<Object> value,
544 PropertyAttributes attributes) {
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000545 CALL_HEAP_FUNCTION(DoCopyInsert(*array, *key, *value, attributes),
546 DescriptorArray);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000547}
548
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000549
550Handle<String> Factory::SymbolFromString(Handle<String> value) {
551 CALL_HEAP_FUNCTION(Heap::LookupSymbol(*value), String);
552}
553
554
555Handle<DescriptorArray> Factory::CopyAppendCallbackDescriptors(
556 Handle<DescriptorArray> array,
557 Handle<Object> descriptors) {
558 v8::NeanderArray callbacks(descriptors);
559 int nof_callbacks = callbacks.length();
560 Handle<DescriptorArray> result =
561 NewDescriptorArray(array->number_of_descriptors() + nof_callbacks);
562
563 // Number of descriptors added to the result so far.
564 int descriptor_count = 0;
565
566 // Copy the descriptors from the array.
567 DescriptorWriter w(*result);
568 for (DescriptorReader r(*array); !r.eos(); r.advance()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000569 if (!r.IsNullDescriptor()) {
570 w.WriteFrom(&r);
571 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000572 descriptor_count++;
573 }
574
575 // Number of duplicates detected.
576 int duplicates = 0;
577
578 // Fill in new callback descriptors. Process the callbacks from
579 // back to front so that the last callback with a given name takes
580 // precedence over previously added callbacks with that name.
581 for (int i = nof_callbacks - 1; i >= 0; i--) {
582 Handle<AccessorInfo> entry =
583 Handle<AccessorInfo>(AccessorInfo::cast(callbacks.get(i)));
584 // Ensure the key is a symbol before writing into the instance descriptor.
585 Handle<String> key =
586 SymbolFromString(Handle<String>(String::cast(entry->name())));
587 // Check if a descriptor with this name already exists before writing.
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000588 if (result->LinearSearch(*key, descriptor_count) ==
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000589 DescriptorArray::kNotFound) {
590 CallbacksDescriptor desc(*key, *entry, entry->property_attributes());
591 w.Write(&desc);
592 descriptor_count++;
593 } else {
594 duplicates++;
595 }
596 }
597
598 // If duplicates were detected, allocate a result of the right size
599 // and transfer the elements.
600 if (duplicates > 0) {
601 Handle<DescriptorArray> new_result =
602 NewDescriptorArray(result->number_of_descriptors() - duplicates);
603 DescriptorWriter w(*new_result);
604 DescriptorReader r(*result);
605 while (!w.eos()) {
606 w.WriteFrom(&r);
607 r.advance();
608 }
609 result = new_result;
610 }
611
612 // Sort the result before returning.
613 result->Sort();
614 return result;
615}
616
617
618Handle<JSObject> Factory::NewJSObject(Handle<JSFunction> constructor,
619 PretenureFlag pretenure) {
620 CALL_HEAP_FUNCTION(Heap::AllocateJSObject(*constructor, pretenure), JSObject);
621}
622
623
ager@chromium.org236ad962008-09-25 09:45:57 +0000624Handle<JSObject> Factory::NewJSObjectFromMap(Handle<Map> map) {
625 CALL_HEAP_FUNCTION(Heap::AllocateJSObjectFromMap(*map, NOT_TENURED),
626 JSObject);
627}
628
629
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000630Handle<JSArray> Factory::NewJSArray(int length,
631 PretenureFlag pretenure) {
632 Handle<JSObject> obj = NewJSObject(Top::array_function(), pretenure);
633 CALL_HEAP_FUNCTION(Handle<JSArray>::cast(obj)->Initialize(length), JSArray);
634}
635
636
637Handle<JSArray> Factory::NewJSArrayWithElements(Handle<FixedArray> elements,
638 PretenureFlag pretenure) {
639 Handle<JSArray> result =
640 Handle<JSArray>::cast(NewJSObject(Top::array_function(), pretenure));
641 result->SetContent(*elements);
642 return result;
643}
644
645
646Handle<SharedFunctionInfo> Factory::NewSharedFunctionInfo(Handle<String> name) {
647 CALL_HEAP_FUNCTION(Heap::AllocateSharedFunctionInfo(*name),
648 SharedFunctionInfo);
649}
650
651
652Handle<Dictionary> Factory::DictionaryAtNumberPut(Handle<Dictionary> dictionary,
653 uint32_t key,
654 Handle<Object> value) {
655 CALL_HEAP_FUNCTION(dictionary->AtNumberPut(key, *value), Dictionary);
656}
657
658
659Handle<JSFunction> Factory::NewFunctionHelper(Handle<String> name,
660 Handle<Object> prototype) {
661 Handle<SharedFunctionInfo> function_share = NewSharedFunctionInfo(name);
662 CALL_HEAP_FUNCTION(Heap::AllocateFunction(*Top::function_map(),
663 *function_share,
664 *prototype),
665 JSFunction);
666}
667
668
669Handle<JSFunction> Factory::NewFunction(Handle<String> name,
670 Handle<Object> prototype) {
671 Handle<JSFunction> fun = NewFunctionHelper(name, prototype);
672 fun->set_context(Top::context()->global_context());
673 return fun;
674}
675
676
677Handle<Object> Factory::ToObject(Handle<Object> object,
678 Handle<Context> global_context) {
679 CALL_HEAP_FUNCTION(object->ToObject(*global_context), Object);
680}
681
682
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000683#ifdef ENABLE_DEBUGGER_SUPPORT
v8.team.kasperl727e9952008-09-02 14:56:44 +0000684Handle<DebugInfo> Factory::NewDebugInfo(Handle<SharedFunctionInfo> shared) {
685 // Get the original code of the function.
686 Handle<Code> code(shared->code());
687
688 // Create a copy of the code before allocating the debug info object to avoid
689 // allocation while setting up the debug info object.
690 Handle<Code> original_code(*Factory::CopyCode(code));
691
692 // Allocate initial fixed array for active break points before allocating the
693 // debug info object to avoid allocation while setting up the debug info
694 // object.
695 Handle<FixedArray> break_points(
696 Factory::NewFixedArray(Debug::kEstimatedNofBreakPointsInFunction));
697
698 // Create and set up the debug info object. Debug info contains function, a
699 // copy of the original code, the executing code and initial fixed array for
700 // active break points.
701 Handle<DebugInfo> debug_info =
702 Handle<DebugInfo>::cast(Factory::NewStruct(DEBUG_INFO_TYPE));
703 debug_info->set_shared(*shared);
704 debug_info->set_original_code(*original_code);
705 debug_info->set_code(*code);
706 debug_info->set_break_points(*break_points);
707
708 // Link debug info to function.
709 shared->set_debug_info(*debug_info);
710
711 return debug_info;
712}
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000713#endif
v8.team.kasperl727e9952008-09-02 14:56:44 +0000714
715
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000716Handle<JSObject> Factory::NewArgumentsObject(Handle<Object> callee,
717 int length) {
718 CALL_HEAP_FUNCTION(Heap::AllocateArgumentsObject(*callee, length), JSObject);
719}
720
721
722Handle<JSFunction> Factory::CreateApiFunction(
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000723 Handle<FunctionTemplateInfo> obj, ApiInstanceType instance_type) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000724 Handle<Code> code = Handle<Code>(Builtins::builtin(Builtins::HandleApiCall));
725
kasper.lund212ac232008-07-16 07:07:30 +0000726 int internal_field_count = 0;
727 if (!obj->instance_template()->IsUndefined()) {
728 Handle<ObjectTemplateInfo> instance_template =
729 Handle<ObjectTemplateInfo>(
730 ObjectTemplateInfo::cast(obj->instance_template()));
731 internal_field_count =
732 Smi::cast(instance_template->internal_field_count())->value();
733 }
734
735 int instance_size = kPointerSize * internal_field_count;
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000736 InstanceType type = INVALID_TYPE;
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000737 switch (instance_type) {
738 case JavaScriptObject:
739 type = JS_OBJECT_TYPE;
740 instance_size += JSObject::kHeaderSize;
741 break;
742 case InnerGlobalObject:
743 type = JS_GLOBAL_OBJECT_TYPE;
744 instance_size += JSGlobalObject::kSize;
745 break;
746 case OuterGlobalObject:
747 type = JS_GLOBAL_PROXY_TYPE;
748 instance_size += JSGlobalProxy::kSize;
749 break;
750 default:
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000751 break;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000752 }
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000753 ASSERT(type != INVALID_TYPE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000754
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000755 Handle<JSFunction> result =
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000756 Factory::NewFunction(Factory::empty_symbol(),
757 type,
758 instance_size,
759 code,
760 true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000761 // Set class name.
762 Handle<Object> class_name = Handle<Object>(obj->class_name());
763 if (class_name->IsString()) {
764 result->shared()->set_instance_class_name(*class_name);
765 result->shared()->set_name(*class_name);
766 }
767
768 Handle<Map> map = Handle<Map>(result->initial_map());
769
770 // Mark as undetectable if needed.
771 if (obj->undetectable()) {
772 map->set_is_undetectable();
773 }
774
775 // Mark as hidden for the __proto__ accessor if needed.
776 if (obj->hidden_prototype()) {
777 map->set_is_hidden_prototype();
778 }
779
780 // Mark as needs_access_check if needed.
781 if (obj->needs_access_check()) {
ager@chromium.org870a0b62008-11-04 11:43:05 +0000782 map->set_is_access_check_needed(true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000783 }
784
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000785 // Set interceptor information in the map.
786 if (!obj->named_property_handler()->IsUndefined()) {
787 map->set_has_named_interceptor();
788 }
789 if (!obj->indexed_property_handler()->IsUndefined()) {
790 map->set_has_indexed_interceptor();
791 }
792
793 // Set instance call-as-function information in the map.
794 if (!obj->instance_call_handler()->IsUndefined()) {
795 map->set_has_instance_call_handler();
796 }
797
798 result->shared()->set_function_data(*obj);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000799 result->shared()->DontAdaptArguments();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000800
801 // Recursively copy parent templates' accessors, 'data' may be modified.
802 Handle<DescriptorArray> array =
803 Handle<DescriptorArray>(map->instance_descriptors());
804 while (true) {
805 Handle<Object> props = Handle<Object>(obj->property_accessors());
806 if (!props->IsUndefined()) {
807 array = Factory::CopyAppendCallbackDescriptors(array, props);
808 }
809 Handle<Object> parent = Handle<Object>(obj->parent_template());
810 if (parent->IsUndefined()) break;
811 obj = Handle<FunctionTemplateInfo>::cast(parent);
812 }
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000813 if (!array->IsEmpty()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000814 map->set_instance_descriptors(*array);
815 }
816
817 return result;
818}
819
820
ager@chromium.org236ad962008-09-25 09:45:57 +0000821Handle<MapCache> Factory::NewMapCache(int at_least_space_for) {
822 CALL_HEAP_FUNCTION(MapCache::Allocate(at_least_space_for), MapCache);
823}
824
825
826static Object* UpdateMapCacheWith(Context* context,
827 FixedArray* keys,
828 Map* map) {
829 Object* result = MapCache::cast(context->map_cache())->Put(keys, map);
830 if (!result->IsFailure()) context->set_map_cache(MapCache::cast(result));
831 return result;
832}
833
834
835Handle<MapCache> Factory::AddToMapCache(Handle<Context> context,
836 Handle<FixedArray> keys,
837 Handle<Map> map) {
838 CALL_HEAP_FUNCTION(UpdateMapCacheWith(*context, *keys, *map), MapCache);
839}
840
841
842Handle<Map> Factory::ObjectLiteralMapFromCache(Handle<Context> context,
843 Handle<FixedArray> keys) {
844 if (context->map_cache()->IsUndefined()) {
845 // Allocate the new map cache for the global context.
846 Handle<MapCache> new_cache = NewMapCache(24);
847 context->set_map_cache(*new_cache);
848 }
ager@chromium.org32912102009-01-16 10:38:43 +0000849 // Check to see whether there is a matching element in the cache.
ager@chromium.org236ad962008-09-25 09:45:57 +0000850 Handle<MapCache> cache =
851 Handle<MapCache>(MapCache::cast(context->map_cache()));
852 Handle<Object> result = Handle<Object>(cache->Lookup(*keys));
853 if (result->IsMap()) return Handle<Map>::cast(result);
854 // Create a new map and add it to the cache.
855 Handle<Map> map =
ager@chromium.org32912102009-01-16 10:38:43 +0000856 CopyMap(Handle<Map>(context->object_function()->initial_map()),
857 keys->length());
ager@chromium.org236ad962008-09-25 09:45:57 +0000858 AddToMapCache(context, keys, map);
859 return Handle<Map>(map);
860}
861
862
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000863void Factory::SetRegExpAtomData(Handle<JSRegExp> regexp,
864 JSRegExp::Type type,
865 Handle<String> source,
866 JSRegExp::Flags flags,
867 Handle<Object> data) {
868 Handle<FixedArray> store = NewFixedArray(JSRegExp::kAtomDataSize);
869
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000870 store->set(JSRegExp::kTagIndex, Smi::FromInt(type));
871 store->set(JSRegExp::kSourceIndex, *source);
872 store->set(JSRegExp::kFlagsIndex, Smi::FromInt(flags.value()));
873 store->set(JSRegExp::kAtomPatternIndex, *data);
874 regexp->set_data(*store);
875}
876
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000877void Factory::SetRegExpIrregexpData(Handle<JSRegExp> regexp,
878 JSRegExp::Type type,
879 Handle<String> source,
880 JSRegExp::Flags flags,
881 int capture_count) {
882 Handle<FixedArray> store = NewFixedArray(JSRegExp::kIrregexpDataSize);
883
884 store->set(JSRegExp::kTagIndex, Smi::FromInt(type));
885 store->set(JSRegExp::kSourceIndex, *source);
886 store->set(JSRegExp::kFlagsIndex, Smi::FromInt(flags.value()));
887 store->set(JSRegExp::kIrregexpASCIICodeIndex, Heap::the_hole_value());
888 store->set(JSRegExp::kIrregexpUC16CodeIndex, Heap::the_hole_value());
889 store->set(JSRegExp::kIrregexpMaxRegisterCountIndex, Smi::FromInt(0));
890 store->set(JSRegExp::kIrregexpCaptureCountIndex,
891 Smi::FromInt(capture_count));
892 regexp->set_data(*store);
893}
894
895
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000896
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000897void Factory::ConfigureInstance(Handle<FunctionTemplateInfo> desc,
898 Handle<JSObject> instance,
899 bool* pending_exception) {
900 // Configure the instance by adding the properties specified by the
901 // instance template.
902 Handle<Object> instance_template = Handle<Object>(desc->instance_template());
903 if (!instance_template->IsUndefined()) {
904 Execution::ConfigureInstance(instance,
905 instance_template,
906 pending_exception);
907 } else {
908 *pending_exception = false;
909 }
910}
911
912
913} } // namespace v8::internal