blob: 4d7a957f0b3463520ebe5e8e9050cc3daf92150c [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
kasperl@chromium.org86f77b72009-07-06 08:21:57 +000052Handle<StringDictionary> Factory::NewStringDictionary(int at_least_space_for) {
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +000053 ASSERT(0 <= at_least_space_for);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +000054 CALL_HEAP_FUNCTION(StringDictionary::Allocate(at_least_space_for),
55 StringDictionary);
56}
57
58
59Handle<NumberDictionary> Factory::NewNumberDictionary(int at_least_space_for) {
60 ASSERT(0 <= at_least_space_for);
61 CALL_HEAP_FUNCTION(NumberDictionary::Allocate(at_least_space_for),
62 NumberDictionary);
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +000063}
64
65
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000066Handle<DescriptorArray> Factory::NewDescriptorArray(int number_of_descriptors) {
67 ASSERT(0 <= number_of_descriptors);
68 CALL_HEAP_FUNCTION(DescriptorArray::Allocate(number_of_descriptors),
69 DescriptorArray);
70}
71
72
ager@chromium.org9258b6b2008-09-11 09:11:10 +000073// Symbols are created in the old generation (data space).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000074Handle<String> Factory::LookupSymbol(Vector<const char> string) {
75 CALL_HEAP_FUNCTION(Heap::LookupSymbol(string), String);
76}
77
78
79Handle<String> Factory::NewStringFromAscii(Vector<const char> string,
80 PretenureFlag pretenure) {
81 CALL_HEAP_FUNCTION(Heap::AllocateStringFromAscii(string, pretenure), String);
82}
83
84Handle<String> Factory::NewStringFromUtf8(Vector<const char> string,
85 PretenureFlag pretenure) {
86 CALL_HEAP_FUNCTION(Heap::AllocateStringFromUtf8(string, pretenure), String);
87}
88
89
90Handle<String> Factory::NewStringFromTwoByte(Vector<const uc16> string) {
91 CALL_HEAP_FUNCTION(Heap::AllocateStringFromTwoByte(string), String);
92}
93
94
95Handle<String> Factory::NewRawTwoByteString(int length,
96 PretenureFlag pretenure) {
97 CALL_HEAP_FUNCTION(Heap::AllocateRawTwoByteString(length, pretenure), String);
98}
99
100
101Handle<String> Factory::NewConsString(Handle<String> first,
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000102 Handle<String> second) {
ager@chromium.orgc3e50d82008-11-05 11:53:10 +0000103 CALL_HEAP_FUNCTION(Heap::AllocateConsString(*first, *second), String);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000104}
105
106
ager@chromium.org870a0b62008-11-04 11:43:05 +0000107Handle<String> Factory::NewStringSlice(Handle<String> str,
ager@chromium.org870a0b62008-11-04 11:43:05 +0000108 int begin,
109 int end) {
ager@chromium.orgc3e50d82008-11-05 11:53:10 +0000110 CALL_HEAP_FUNCTION(str->Slice(begin, end), String);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000111}
112
113
114Handle<String> Factory::NewExternalStringFromAscii(
115 ExternalAsciiString::Resource* resource) {
116 CALL_HEAP_FUNCTION(Heap::AllocateExternalStringFromAscii(resource), String);
117}
118
119
120Handle<String> Factory::NewExternalStringFromTwoByte(
121 ExternalTwoByteString::Resource* resource) {
122 CALL_HEAP_FUNCTION(Heap::AllocateExternalStringFromTwoByte(resource), String);
123}
124
125
126Handle<Context> Factory::NewGlobalContext() {
127 CALL_HEAP_FUNCTION(Heap::AllocateGlobalContext(), Context);
128}
129
130
131Handle<Context> Factory::NewFunctionContext(int length,
132 Handle<JSFunction> closure) {
133 CALL_HEAP_FUNCTION(Heap::AllocateFunctionContext(length, *closure), Context);
134}
135
136
137Handle<Context> Factory::NewWithContext(Handle<Context> previous,
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000138 Handle<JSObject> extension,
139 bool is_catch_context) {
140 CALL_HEAP_FUNCTION(Heap::AllocateWithContext(*previous,
141 *extension,
142 is_catch_context),
143 Context);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000144}
145
146
147Handle<Struct> Factory::NewStruct(InstanceType type) {
148 CALL_HEAP_FUNCTION(Heap::AllocateStruct(type), Struct);
149}
150
151
152Handle<AccessorInfo> Factory::NewAccessorInfo() {
153 Handle<AccessorInfo> info =
154 Handle<AccessorInfo>::cast(NewStruct(ACCESSOR_INFO_TYPE));
155 info->set_flag(0); // Must clear the flag, it was initialized as undefined.
156 return info;
157}
158
159
160Handle<Script> Factory::NewScript(Handle<String> source) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000161 // Generate id for this script.
162 int id;
163 if (Heap::last_script_id()->IsUndefined()) {
164 // Script ids start from one.
165 id = 1;
166 } else {
167 // Increment id, wrap when positive smi is exhausted.
168 id = Smi::cast(Heap::last_script_id())->value();
169 id++;
170 if (!Smi::IsValid(id)) {
171 id = 0;
172 }
173 }
174 Heap::SetLastScriptId(Smi::FromInt(id));
175
176 // Create and initialize script object.
ager@chromium.org9085a012009-05-11 19:22:57 +0000177 Handle<Proxy> wrapper = Factory::NewProxy(0, TENURED);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000178 Handle<Script> script = Handle<Script>::cast(NewStruct(SCRIPT_TYPE));
179 script->set_source(*source);
180 script->set_name(Heap::undefined_value());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000181 script->set_id(Heap::last_script_id());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000182 script->set_line_offset(Smi::FromInt(0));
183 script->set_column_offset(Smi::FromInt(0));
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000184 script->set_data(Heap::undefined_value());
ager@chromium.org9085a012009-05-11 19:22:57 +0000185 script->set_context_data(Heap::undefined_value());
ager@chromium.orge2902be2009-06-08 12:21:35 +0000186 script->set_type(Smi::FromInt(Script::TYPE_NORMAL));
187 script->set_compilation_type(Smi::FromInt(Script::COMPILATION_TYPE_HOST));
ager@chromium.org9085a012009-05-11 19:22:57 +0000188 script->set_wrapper(*wrapper);
iposva@chromium.org245aa852009-02-10 00:49:54 +0000189 script->set_line_ends(Heap::undefined_value());
ager@chromium.orge2902be2009-06-08 12:21:35 +0000190 script->set_eval_from_function(Heap::undefined_value());
191 script->set_eval_from_instructions_offset(Smi::FromInt(0));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000192
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000193 return script;
194}
195
196
197Handle<Proxy> Factory::NewProxy(Address addr, PretenureFlag pretenure) {
198 CALL_HEAP_FUNCTION(Heap::AllocateProxy(addr, pretenure), Proxy);
199}
200
201
202Handle<Proxy> Factory::NewProxy(const AccessorDescriptor* desc) {
203 return NewProxy((Address) desc, TENURED);
204}
205
206
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000207Handle<ByteArray> Factory::NewByteArray(int length, PretenureFlag pretenure) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000208 ASSERT(0 <= length);
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000209 CALL_HEAP_FUNCTION(Heap::AllocateByteArray(length, pretenure), ByteArray);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000210}
211
212
213Handle<Map> Factory::NewMap(InstanceType type, int instance_size) {
214 CALL_HEAP_FUNCTION(Heap::AllocateMap(type, instance_size), Map);
215}
216
217
218Handle<JSObject> Factory::NewFunctionPrototype(Handle<JSFunction> function) {
219 CALL_HEAP_FUNCTION(Heap::AllocateFunctionPrototype(*function), JSObject);
220}
221
222
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000223Handle<Map> Factory::CopyMapDropDescriptors(Handle<Map> src) {
224 CALL_HEAP_FUNCTION(src->CopyDropDescriptors(), Map);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000225}
226
227
ager@chromium.org32912102009-01-16 10:38:43 +0000228Handle<Map> Factory::CopyMap(Handle<Map> src,
229 int extra_inobject_properties) {
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000230 Handle<Map> copy = CopyMapDropDescriptors(src);
ager@chromium.org32912102009-01-16 10:38:43 +0000231 // Check that we do not overflow the instance size when adding the
232 // extra inobject properties.
233 int instance_size_delta = extra_inobject_properties * kPointerSize;
234 int max_instance_size_delta =
235 JSObject::kMaxInstanceSize - copy->instance_size();
236 if (instance_size_delta > max_instance_size_delta) {
237 // If the instance size overflows, we allocate as many properties
238 // as we can as inobject properties.
239 instance_size_delta = max_instance_size_delta;
240 extra_inobject_properties = max_instance_size_delta >> kPointerSizeLog2;
241 }
242 // Adjust the map with the extra inobject properties.
243 int inobject_properties =
244 copy->inobject_properties() + extra_inobject_properties;
245 copy->set_inobject_properties(inobject_properties);
246 copy->set_unused_property_fields(inobject_properties);
247 copy->set_instance_size(copy->instance_size() + instance_size_delta);
248 return copy;
249}
250
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000251Handle<Map> Factory::CopyMapDropTransitions(Handle<Map> src) {
252 CALL_HEAP_FUNCTION(src->CopyDropTransitions(), Map);
253}
254
255
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000256Handle<FixedArray> Factory::CopyFixedArray(Handle<FixedArray> array) {
257 CALL_HEAP_FUNCTION(array->Copy(), FixedArray);
258}
259
260
261Handle<JSFunction> Factory::BaseNewFunctionFromBoilerplate(
262 Handle<JSFunction> boilerplate,
263 Handle<Map> function_map) {
264 ASSERT(boilerplate->IsBoilerplate());
265 ASSERT(!boilerplate->has_initial_map());
266 ASSERT(!boilerplate->has_prototype());
267 ASSERT(boilerplate->properties() == Heap::empty_fixed_array());
268 ASSERT(boilerplate->elements() == Heap::empty_fixed_array());
269 CALL_HEAP_FUNCTION(Heap::AllocateFunction(*function_map,
270 boilerplate->shared(),
271 Heap::the_hole_value()),
272 JSFunction);
273}
274
275
276Handle<JSFunction> Factory::NewFunctionFromBoilerplate(
277 Handle<JSFunction> boilerplate,
278 Handle<Context> context) {
279 Handle<JSFunction> result =
280 BaseNewFunctionFromBoilerplate(boilerplate, Top::function_map());
281 result->set_context(*context);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000282 int number_of_literals = boilerplate->NumberOfLiterals();
283 Handle<FixedArray> literals =
284 Factory::NewFixedArray(number_of_literals, TENURED);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000285 if (number_of_literals > 0) {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000286 // Store the object, regexp and array functions in the literals
287 // array prefix. These functions will be used when creating
288 // object, regexp and array literals in this function.
ager@chromium.org236ad962008-09-25 09:45:57 +0000289 literals->set(JSFunction::kLiteralGlobalContextIndex,
290 context->global_context());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000291 }
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000292 result->set_literals(*literals);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000293 ASSERT(!result->IsBoilerplate());
294 return result;
295}
296
297
298Handle<Object> Factory::NewNumber(double value,
299 PretenureFlag pretenure) {
300 CALL_HEAP_FUNCTION(Heap::NumberFromDouble(value, pretenure), Object);
301}
302
303
304Handle<Object> Factory::NewNumberFromInt(int value) {
305 CALL_HEAP_FUNCTION(Heap::NumberFromInt32(value), Object);
306}
307
308
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000309Handle<Object> Factory::NewNumberFromUint(uint32_t value) {
310 CALL_HEAP_FUNCTION(Heap::NumberFromUint32(value), Object);
311}
312
313
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000314Handle<JSObject> Factory::NewNeanderObject() {
315 CALL_HEAP_FUNCTION(Heap::AllocateJSObjectFromMap(Heap::neander_map()),
316 JSObject);
317}
318
319
320Handle<Object> Factory::NewTypeError(const char* type,
321 Vector< Handle<Object> > args) {
322 return NewError("MakeTypeError", type, args);
323}
324
325
326Handle<Object> Factory::NewTypeError(Handle<String> message) {
327 return NewError("$TypeError", message);
328}
329
330
331Handle<Object> Factory::NewRangeError(const char* type,
332 Vector< Handle<Object> > args) {
333 return NewError("MakeRangeError", type, args);
334}
335
336
337Handle<Object> Factory::NewRangeError(Handle<String> message) {
338 return NewError("$RangeError", message);
339}
340
341
342Handle<Object> Factory::NewSyntaxError(const char* type, Handle<JSArray> args) {
343 return NewError("MakeSyntaxError", type, args);
344}
345
346
347Handle<Object> Factory::NewSyntaxError(Handle<String> message) {
348 return NewError("$SyntaxError", message);
349}
350
351
352Handle<Object> Factory::NewReferenceError(const char* type,
353 Vector< Handle<Object> > args) {
354 return NewError("MakeReferenceError", type, args);
355}
356
357
358Handle<Object> Factory::NewReferenceError(Handle<String> message) {
359 return NewError("$ReferenceError", message);
360}
361
362
363Handle<Object> Factory::NewError(const char* maker, const char* type,
364 Vector< Handle<Object> > args) {
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000365 v8::HandleScope scope; // Instantiate a closeable HandleScope for EscapeFrom.
ager@chromium.org7c537e22008-10-16 08:43:32 +0000366 Handle<FixedArray> array = Factory::NewFixedArray(args.length());
367 for (int i = 0; i < args.length(); i++) {
368 array->set(i, *args[i]);
369 }
370 Handle<JSArray> object = Factory::NewJSArrayWithElements(array);
371 Handle<Object> result = NewError(maker, type, object);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000372 return result.EscapeFrom(&scope);
373}
374
375
376Handle<Object> Factory::NewEvalError(const char* type,
377 Vector< Handle<Object> > args) {
378 return NewError("MakeEvalError", type, args);
379}
380
381
382Handle<Object> Factory::NewError(const char* type,
383 Vector< Handle<Object> > args) {
384 return NewError("MakeError", type, args);
385}
386
387
388Handle<Object> Factory::NewError(const char* maker,
389 const char* type,
390 Handle<JSArray> args) {
391 Handle<String> make_str = Factory::LookupAsciiSymbol(maker);
392 Handle<JSFunction> fun =
393 Handle<JSFunction>(
394 JSFunction::cast(
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000395 Top::builtins()->GetProperty(*make_str)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000396 Handle<Object> type_obj = Factory::LookupAsciiSymbol(type);
397 Object** argv[2] = { type_obj.location(),
398 Handle<Object>::cast(args).location() };
399
400 // Invoke the JavaScript factory method. If an exception is thrown while
401 // running the factory method, use the exception as the result.
402 bool caught_exception;
403 Handle<Object> result = Execution::TryCall(fun,
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000404 Top::builtins(),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000405 2,
406 argv,
407 &caught_exception);
408 return result;
409}
410
411
412Handle<Object> Factory::NewError(Handle<String> message) {
413 return NewError("$Error", message);
414}
415
416
417Handle<Object> Factory::NewError(const char* constructor,
418 Handle<String> message) {
419 Handle<String> constr = Factory::LookupAsciiSymbol(constructor);
420 Handle<JSFunction> fun =
421 Handle<JSFunction>(
422 JSFunction::cast(
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000423 Top::builtins()->GetProperty(*constr)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000424 Object** argv[1] = { Handle<Object>::cast(message).location() };
425
426 // Invoke the JavaScript factory method. If an exception is thrown while
427 // running the factory method, use the exception as the result.
428 bool caught_exception;
429 Handle<Object> result = Execution::TryCall(fun,
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000430 Top::builtins(),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000431 1,
432 argv,
433 &caught_exception);
434 return result;
435}
436
437
438Handle<JSFunction> Factory::NewFunction(Handle<String> name,
439 InstanceType type,
440 int instance_size,
441 Handle<Code> code,
442 bool force_initial_map) {
443 // Allocate the function
444 Handle<JSFunction> function = NewFunction(name, the_hole_value());
445 function->set_code(*code);
446
447 if (force_initial_map ||
448 type != JS_OBJECT_TYPE ||
449 instance_size != JSObject::kHeaderSize) {
450 Handle<Map> initial_map = NewMap(type, instance_size);
451 Handle<JSObject> prototype = NewFunctionPrototype(function);
452 initial_map->set_prototype(*prototype);
453 function->set_initial_map(*initial_map);
454 initial_map->set_constructor(*function);
455 } else {
456 ASSERT(!function->has_initial_map());
457 ASSERT(!function->has_prototype());
458 }
459
460 return function;
461}
462
463
464Handle<JSFunction> Factory::NewFunctionBoilerplate(Handle<String> name,
465 int number_of_literals,
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000466 bool contains_array_literal,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000467 Handle<Code> code) {
468 Handle<JSFunction> function = NewFunctionBoilerplate(name);
469 function->set_code(*code);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000470 int literals_array_size = number_of_literals;
471 // If the function contains object, regexp or array literals,
472 // allocate extra space for a literals array prefix containing the
473 // object, regexp and array constructor functions.
474 if (number_of_literals > 0 || contains_array_literal) {
475 literals_array_size += JSFunction::kLiteralsPrefixSize;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000476 }
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000477 Handle<FixedArray> literals =
478 Factory::NewFixedArray(literals_array_size, TENURED);
479 function->set_literals(*literals);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000480 ASSERT(!function->has_initial_map());
481 ASSERT(!function->has_prototype());
482 return function;
483}
484
485
486Handle<JSFunction> Factory::NewFunctionBoilerplate(Handle<String> name) {
487 Handle<SharedFunctionInfo> shared = NewSharedFunctionInfo(name);
488 CALL_HEAP_FUNCTION(Heap::AllocateFunction(Heap::boilerplate_function_map(),
489 *shared,
490 Heap::the_hole_value()),
491 JSFunction);
492}
493
494
495Handle<JSFunction> Factory::NewFunctionWithPrototype(Handle<String> name,
496 InstanceType type,
497 int instance_size,
498 Handle<JSObject> prototype,
499 Handle<Code> code,
500 bool force_initial_map) {
501 // Allocate the function
502 Handle<JSFunction> function = NewFunction(name, prototype);
503
504 function->set_code(*code);
505
506 if (force_initial_map ||
507 type != JS_OBJECT_TYPE ||
508 instance_size != JSObject::kHeaderSize) {
509 Handle<Map> initial_map = NewMap(type, instance_size);
510 function->set_initial_map(*initial_map);
511 initial_map->set_constructor(*function);
512 }
513
514 // Set function.prototype and give the prototype a constructor
515 // property that refers to the function.
516 SetPrototypeProperty(function, prototype);
517 SetProperty(prototype, Factory::constructor_symbol(), function, DONT_ENUM);
518 return function;
519}
520
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000521
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000522Handle<Code> Factory::NewCode(const CodeDesc& desc,
523 ZoneScopeInfo* sinfo,
524 Code::Flags flags,
525 Handle<Object> self_ref) {
kasperl@chromium.org061ef742009-02-27 12:16:20 +0000526 CALL_HEAP_FUNCTION(Heap::CreateCode(desc, sinfo, flags, self_ref), Code);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000527}
528
529
530Handle<Code> Factory::CopyCode(Handle<Code> code) {
531 CALL_HEAP_FUNCTION(Heap::CopyCode(*code), Code);
532}
533
534
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000535static inline Object* DoCopyInsert(DescriptorArray* array,
536 String* key,
537 Object* value,
538 PropertyAttributes attributes) {
539 CallbacksDescriptor desc(key, value, attributes);
540 Object* obj = array->CopyInsert(&desc, REMOVE_TRANSITIONS);
541 return obj;
542}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000543
544
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000545// Allocate the new array.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000546Handle<DescriptorArray> Factory::CopyAppendProxyDescriptor(
547 Handle<DescriptorArray> array,
548 Handle<String> key,
549 Handle<Object> value,
550 PropertyAttributes attributes) {
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000551 CALL_HEAP_FUNCTION(DoCopyInsert(*array, *key, *value, attributes),
552 DescriptorArray);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000553}
554
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000555
556Handle<String> Factory::SymbolFromString(Handle<String> value) {
557 CALL_HEAP_FUNCTION(Heap::LookupSymbol(*value), String);
558}
559
560
561Handle<DescriptorArray> Factory::CopyAppendCallbackDescriptors(
562 Handle<DescriptorArray> array,
563 Handle<Object> descriptors) {
564 v8::NeanderArray callbacks(descriptors);
565 int nof_callbacks = callbacks.length();
566 Handle<DescriptorArray> result =
567 NewDescriptorArray(array->number_of_descriptors() + nof_callbacks);
568
569 // Number of descriptors added to the result so far.
570 int descriptor_count = 0;
571
572 // Copy the descriptors from the array.
573 DescriptorWriter w(*result);
574 for (DescriptorReader r(*array); !r.eos(); r.advance()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000575 if (!r.IsNullDescriptor()) {
576 w.WriteFrom(&r);
577 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000578 descriptor_count++;
579 }
580
581 // Number of duplicates detected.
582 int duplicates = 0;
583
584 // Fill in new callback descriptors. Process the callbacks from
585 // back to front so that the last callback with a given name takes
586 // precedence over previously added callbacks with that name.
587 for (int i = nof_callbacks - 1; i >= 0; i--) {
588 Handle<AccessorInfo> entry =
589 Handle<AccessorInfo>(AccessorInfo::cast(callbacks.get(i)));
590 // Ensure the key is a symbol before writing into the instance descriptor.
591 Handle<String> key =
592 SymbolFromString(Handle<String>(String::cast(entry->name())));
593 // Check if a descriptor with this name already exists before writing.
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000594 if (result->LinearSearch(*key, descriptor_count) ==
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000595 DescriptorArray::kNotFound) {
596 CallbacksDescriptor desc(*key, *entry, entry->property_attributes());
597 w.Write(&desc);
598 descriptor_count++;
599 } else {
600 duplicates++;
601 }
602 }
603
604 // If duplicates were detected, allocate a result of the right size
605 // and transfer the elements.
606 if (duplicates > 0) {
607 Handle<DescriptorArray> new_result =
608 NewDescriptorArray(result->number_of_descriptors() - duplicates);
609 DescriptorWriter w(*new_result);
610 DescriptorReader r(*result);
611 while (!w.eos()) {
612 w.WriteFrom(&r);
613 r.advance();
614 }
615 result = new_result;
616 }
617
618 // Sort the result before returning.
619 result->Sort();
620 return result;
621}
622
623
624Handle<JSObject> Factory::NewJSObject(Handle<JSFunction> constructor,
625 PretenureFlag pretenure) {
626 CALL_HEAP_FUNCTION(Heap::AllocateJSObject(*constructor, pretenure), JSObject);
627}
628
629
kasperl@chromium.org2abc4502009-07-02 07:00:29 +0000630Handle<GlobalObject> Factory::NewGlobalObject(
631 Handle<JSFunction> constructor) {
632 CALL_HEAP_FUNCTION(Heap::AllocateGlobalObject(*constructor),
633 GlobalObject);
634}
635
636
637
ager@chromium.org236ad962008-09-25 09:45:57 +0000638Handle<JSObject> Factory::NewJSObjectFromMap(Handle<Map> map) {
639 CALL_HEAP_FUNCTION(Heap::AllocateJSObjectFromMap(*map, NOT_TENURED),
640 JSObject);
641}
642
643
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000644Handle<JSArray> Factory::NewJSArray(int length,
645 PretenureFlag pretenure) {
646 Handle<JSObject> obj = NewJSObject(Top::array_function(), pretenure);
647 CALL_HEAP_FUNCTION(Handle<JSArray>::cast(obj)->Initialize(length), JSArray);
648}
649
650
651Handle<JSArray> Factory::NewJSArrayWithElements(Handle<FixedArray> elements,
652 PretenureFlag pretenure) {
653 Handle<JSArray> result =
654 Handle<JSArray>::cast(NewJSObject(Top::array_function(), pretenure));
655 result->SetContent(*elements);
656 return result;
657}
658
659
660Handle<SharedFunctionInfo> Factory::NewSharedFunctionInfo(Handle<String> name) {
661 CALL_HEAP_FUNCTION(Heap::AllocateSharedFunctionInfo(*name),
662 SharedFunctionInfo);
663}
664
665
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000666Handle<NumberDictionary> Factory::DictionaryAtNumberPut(
667 Handle<NumberDictionary> dictionary,
668 uint32_t key,
669 Handle<Object> value) {
670 CALL_HEAP_FUNCTION(dictionary->AtNumberPut(key, *value), NumberDictionary);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000671}
672
673
674Handle<JSFunction> Factory::NewFunctionHelper(Handle<String> name,
675 Handle<Object> prototype) {
676 Handle<SharedFunctionInfo> function_share = NewSharedFunctionInfo(name);
677 CALL_HEAP_FUNCTION(Heap::AllocateFunction(*Top::function_map(),
678 *function_share,
679 *prototype),
680 JSFunction);
681}
682
683
684Handle<JSFunction> Factory::NewFunction(Handle<String> name,
685 Handle<Object> prototype) {
686 Handle<JSFunction> fun = NewFunctionHelper(name, prototype);
687 fun->set_context(Top::context()->global_context());
688 return fun;
689}
690
691
692Handle<Object> Factory::ToObject(Handle<Object> object,
693 Handle<Context> global_context) {
694 CALL_HEAP_FUNCTION(object->ToObject(*global_context), Object);
695}
696
697
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000698#ifdef ENABLE_DEBUGGER_SUPPORT
v8.team.kasperl727e9952008-09-02 14:56:44 +0000699Handle<DebugInfo> Factory::NewDebugInfo(Handle<SharedFunctionInfo> shared) {
700 // Get the original code of the function.
701 Handle<Code> code(shared->code());
702
703 // Create a copy of the code before allocating the debug info object to avoid
704 // allocation while setting up the debug info object.
705 Handle<Code> original_code(*Factory::CopyCode(code));
706
707 // Allocate initial fixed array for active break points before allocating the
708 // debug info object to avoid allocation while setting up the debug info
709 // object.
710 Handle<FixedArray> break_points(
711 Factory::NewFixedArray(Debug::kEstimatedNofBreakPointsInFunction));
712
713 // Create and set up the debug info object. Debug info contains function, a
714 // copy of the original code, the executing code and initial fixed array for
715 // active break points.
716 Handle<DebugInfo> debug_info =
717 Handle<DebugInfo>::cast(Factory::NewStruct(DEBUG_INFO_TYPE));
718 debug_info->set_shared(*shared);
719 debug_info->set_original_code(*original_code);
720 debug_info->set_code(*code);
721 debug_info->set_break_points(*break_points);
722
723 // Link debug info to function.
724 shared->set_debug_info(*debug_info);
725
726 return debug_info;
727}
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000728#endif
v8.team.kasperl727e9952008-09-02 14:56:44 +0000729
730
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000731Handle<JSObject> Factory::NewArgumentsObject(Handle<Object> callee,
732 int length) {
733 CALL_HEAP_FUNCTION(Heap::AllocateArgumentsObject(*callee, length), JSObject);
734}
735
736
737Handle<JSFunction> Factory::CreateApiFunction(
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000738 Handle<FunctionTemplateInfo> obj, ApiInstanceType instance_type) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000739 Handle<Code> code = Handle<Code>(Builtins::builtin(Builtins::HandleApiCall));
740
kasper.lund212ac232008-07-16 07:07:30 +0000741 int internal_field_count = 0;
742 if (!obj->instance_template()->IsUndefined()) {
743 Handle<ObjectTemplateInfo> instance_template =
744 Handle<ObjectTemplateInfo>(
745 ObjectTemplateInfo::cast(obj->instance_template()));
746 internal_field_count =
747 Smi::cast(instance_template->internal_field_count())->value();
748 }
749
750 int instance_size = kPointerSize * internal_field_count;
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000751 InstanceType type = INVALID_TYPE;
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000752 switch (instance_type) {
753 case JavaScriptObject:
754 type = JS_OBJECT_TYPE;
755 instance_size += JSObject::kHeaderSize;
756 break;
757 case InnerGlobalObject:
758 type = JS_GLOBAL_OBJECT_TYPE;
759 instance_size += JSGlobalObject::kSize;
760 break;
761 case OuterGlobalObject:
762 type = JS_GLOBAL_PROXY_TYPE;
763 instance_size += JSGlobalProxy::kSize;
764 break;
765 default:
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000766 break;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000767 }
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000768 ASSERT(type != INVALID_TYPE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000769
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000770 Handle<JSFunction> result =
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000771 Factory::NewFunction(Factory::empty_symbol(),
772 type,
773 instance_size,
774 code,
775 true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000776 // Set class name.
777 Handle<Object> class_name = Handle<Object>(obj->class_name());
778 if (class_name->IsString()) {
779 result->shared()->set_instance_class_name(*class_name);
780 result->shared()->set_name(*class_name);
781 }
782
783 Handle<Map> map = Handle<Map>(result->initial_map());
784
785 // Mark as undetectable if needed.
786 if (obj->undetectable()) {
787 map->set_is_undetectable();
788 }
789
790 // Mark as hidden for the __proto__ accessor if needed.
791 if (obj->hidden_prototype()) {
792 map->set_is_hidden_prototype();
793 }
794
795 // Mark as needs_access_check if needed.
796 if (obj->needs_access_check()) {
ager@chromium.org870a0b62008-11-04 11:43:05 +0000797 map->set_is_access_check_needed(true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000798 }
799
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000800 // Set interceptor information in the map.
801 if (!obj->named_property_handler()->IsUndefined()) {
802 map->set_has_named_interceptor();
803 }
804 if (!obj->indexed_property_handler()->IsUndefined()) {
805 map->set_has_indexed_interceptor();
806 }
807
808 // Set instance call-as-function information in the map.
809 if (!obj->instance_call_handler()->IsUndefined()) {
810 map->set_has_instance_call_handler();
811 }
812
813 result->shared()->set_function_data(*obj);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000814 result->shared()->DontAdaptArguments();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000815
816 // Recursively copy parent templates' accessors, 'data' may be modified.
817 Handle<DescriptorArray> array =
818 Handle<DescriptorArray>(map->instance_descriptors());
819 while (true) {
820 Handle<Object> props = Handle<Object>(obj->property_accessors());
821 if (!props->IsUndefined()) {
822 array = Factory::CopyAppendCallbackDescriptors(array, props);
823 }
824 Handle<Object> parent = Handle<Object>(obj->parent_template());
825 if (parent->IsUndefined()) break;
826 obj = Handle<FunctionTemplateInfo>::cast(parent);
827 }
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000828 if (!array->IsEmpty()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000829 map->set_instance_descriptors(*array);
830 }
831
832 return result;
833}
834
835
ager@chromium.org236ad962008-09-25 09:45:57 +0000836Handle<MapCache> Factory::NewMapCache(int at_least_space_for) {
837 CALL_HEAP_FUNCTION(MapCache::Allocate(at_least_space_for), MapCache);
838}
839
840
841static Object* UpdateMapCacheWith(Context* context,
842 FixedArray* keys,
843 Map* map) {
844 Object* result = MapCache::cast(context->map_cache())->Put(keys, map);
845 if (!result->IsFailure()) context->set_map_cache(MapCache::cast(result));
846 return result;
847}
848
849
850Handle<MapCache> Factory::AddToMapCache(Handle<Context> context,
851 Handle<FixedArray> keys,
852 Handle<Map> map) {
853 CALL_HEAP_FUNCTION(UpdateMapCacheWith(*context, *keys, *map), MapCache);
854}
855
856
857Handle<Map> Factory::ObjectLiteralMapFromCache(Handle<Context> context,
858 Handle<FixedArray> keys) {
859 if (context->map_cache()->IsUndefined()) {
860 // Allocate the new map cache for the global context.
861 Handle<MapCache> new_cache = NewMapCache(24);
862 context->set_map_cache(*new_cache);
863 }
ager@chromium.org32912102009-01-16 10:38:43 +0000864 // Check to see whether there is a matching element in the cache.
ager@chromium.org236ad962008-09-25 09:45:57 +0000865 Handle<MapCache> cache =
866 Handle<MapCache>(MapCache::cast(context->map_cache()));
867 Handle<Object> result = Handle<Object>(cache->Lookup(*keys));
868 if (result->IsMap()) return Handle<Map>::cast(result);
869 // Create a new map and add it to the cache.
870 Handle<Map> map =
ager@chromium.org32912102009-01-16 10:38:43 +0000871 CopyMap(Handle<Map>(context->object_function()->initial_map()),
872 keys->length());
ager@chromium.org236ad962008-09-25 09:45:57 +0000873 AddToMapCache(context, keys, map);
874 return Handle<Map>(map);
875}
876
877
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000878void Factory::SetRegExpAtomData(Handle<JSRegExp> regexp,
879 JSRegExp::Type type,
880 Handle<String> source,
881 JSRegExp::Flags flags,
882 Handle<Object> data) {
883 Handle<FixedArray> store = NewFixedArray(JSRegExp::kAtomDataSize);
884
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000885 store->set(JSRegExp::kTagIndex, Smi::FromInt(type));
886 store->set(JSRegExp::kSourceIndex, *source);
887 store->set(JSRegExp::kFlagsIndex, Smi::FromInt(flags.value()));
888 store->set(JSRegExp::kAtomPatternIndex, *data);
889 regexp->set_data(*store);
890}
891
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000892void Factory::SetRegExpIrregexpData(Handle<JSRegExp> regexp,
893 JSRegExp::Type type,
894 Handle<String> source,
895 JSRegExp::Flags flags,
896 int capture_count) {
897 Handle<FixedArray> store = NewFixedArray(JSRegExp::kIrregexpDataSize);
898
899 store->set(JSRegExp::kTagIndex, Smi::FromInt(type));
900 store->set(JSRegExp::kSourceIndex, *source);
901 store->set(JSRegExp::kFlagsIndex, Smi::FromInt(flags.value()));
902 store->set(JSRegExp::kIrregexpASCIICodeIndex, Heap::the_hole_value());
903 store->set(JSRegExp::kIrregexpUC16CodeIndex, Heap::the_hole_value());
904 store->set(JSRegExp::kIrregexpMaxRegisterCountIndex, Smi::FromInt(0));
905 store->set(JSRegExp::kIrregexpCaptureCountIndex,
906 Smi::FromInt(capture_count));
907 regexp->set_data(*store);
908}
909
910
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000911
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000912void Factory::ConfigureInstance(Handle<FunctionTemplateInfo> desc,
913 Handle<JSObject> instance,
914 bool* pending_exception) {
915 // Configure the instance by adding the properties specified by the
916 // instance template.
917 Handle<Object> instance_template = Handle<Object>(desc->instance_template());
918 if (!instance_template->IsUndefined()) {
919 Execution::ConfigureInstance(instance,
920 instance_template,
921 pending_exception);
922 } else {
923 *pending_exception = false;
924 }
925}
926
927
928} } // namespace v8::internal