blob: 216a07e19cc9f766620a46ab340228bb111100c6 [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) {
ager@chromium.orgc3e50d82008-11-05 11:53:10 +000095 CALL_HEAP_FUNCTION(Heap::AllocateConsString(*first, *second), String);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000096}
97
98
ager@chromium.org870a0b62008-11-04 11:43:05 +000099Handle<String> Factory::NewStringSlice(Handle<String> str,
ager@chromium.org870a0b62008-11-04 11:43:05 +0000100 int begin,
101 int end) {
ager@chromium.orgc3e50d82008-11-05 11:53:10 +0000102 CALL_HEAP_FUNCTION(str->Slice(begin, end), String);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000103}
104
105
106Handle<String> Factory::NewExternalStringFromAscii(
107 ExternalAsciiString::Resource* resource) {
108 CALL_HEAP_FUNCTION(Heap::AllocateExternalStringFromAscii(resource), String);
109}
110
111
112Handle<String> Factory::NewExternalStringFromTwoByte(
113 ExternalTwoByteString::Resource* resource) {
114 CALL_HEAP_FUNCTION(Heap::AllocateExternalStringFromTwoByte(resource), String);
115}
116
117
118Handle<Context> Factory::NewGlobalContext() {
119 CALL_HEAP_FUNCTION(Heap::AllocateGlobalContext(), Context);
120}
121
122
123Handle<Context> Factory::NewFunctionContext(int length,
124 Handle<JSFunction> closure) {
125 CALL_HEAP_FUNCTION(Heap::AllocateFunctionContext(length, *closure), Context);
126}
127
128
129Handle<Context> Factory::NewWithContext(Handle<Context> previous,
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000130 Handle<JSObject> extension,
131 bool is_catch_context) {
132 CALL_HEAP_FUNCTION(Heap::AllocateWithContext(*previous,
133 *extension,
134 is_catch_context),
135 Context);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000136}
137
138
139Handle<Struct> Factory::NewStruct(InstanceType type) {
140 CALL_HEAP_FUNCTION(Heap::AllocateStruct(type), Struct);
141}
142
143
144Handle<AccessorInfo> Factory::NewAccessorInfo() {
145 Handle<AccessorInfo> info =
146 Handle<AccessorInfo>::cast(NewStruct(ACCESSOR_INFO_TYPE));
147 info->set_flag(0); // Must clear the flag, it was initialized as undefined.
148 return info;
149}
150
151
152Handle<Script> Factory::NewScript(Handle<String> source) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000153 // Generate id for this script.
154 int id;
155 if (Heap::last_script_id()->IsUndefined()) {
156 // Script ids start from one.
157 id = 1;
158 } else {
159 // Increment id, wrap when positive smi is exhausted.
160 id = Smi::cast(Heap::last_script_id())->value();
161 id++;
162 if (!Smi::IsValid(id)) {
163 id = 0;
164 }
165 }
166 Heap::SetLastScriptId(Smi::FromInt(id));
167
168 // Create and initialize script object.
ager@chromium.org9085a012009-05-11 19:22:57 +0000169 Handle<Proxy> wrapper = Factory::NewProxy(0, TENURED);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000170 Handle<Script> script = Handle<Script>::cast(NewStruct(SCRIPT_TYPE));
171 script->set_source(*source);
172 script->set_name(Heap::undefined_value());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000173 script->set_id(Heap::last_script_id());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000174 script->set_line_offset(Smi::FromInt(0));
175 script->set_column_offset(Smi::FromInt(0));
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000176 script->set_data(Heap::undefined_value());
ager@chromium.org9085a012009-05-11 19:22:57 +0000177 script->set_context_data(Heap::undefined_value());
ager@chromium.orge2902be2009-06-08 12:21:35 +0000178 script->set_type(Smi::FromInt(Script::TYPE_NORMAL));
179 script->set_compilation_type(Smi::FromInt(Script::COMPILATION_TYPE_HOST));
ager@chromium.org9085a012009-05-11 19:22:57 +0000180 script->set_wrapper(*wrapper);
iposva@chromium.org245aa852009-02-10 00:49:54 +0000181 script->set_line_ends(Heap::undefined_value());
ager@chromium.orge2902be2009-06-08 12:21:35 +0000182 script->set_eval_from_function(Heap::undefined_value());
183 script->set_eval_from_instructions_offset(Smi::FromInt(0));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000184
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000185 return script;
186}
187
188
189Handle<Proxy> Factory::NewProxy(Address addr, PretenureFlag pretenure) {
190 CALL_HEAP_FUNCTION(Heap::AllocateProxy(addr, pretenure), Proxy);
191}
192
193
194Handle<Proxy> Factory::NewProxy(const AccessorDescriptor* desc) {
195 return NewProxy((Address) desc, TENURED);
196}
197
198
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000199Handle<ByteArray> Factory::NewByteArray(int length, PretenureFlag pretenure) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000200 ASSERT(0 <= length);
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000201 CALL_HEAP_FUNCTION(Heap::AllocateByteArray(length, pretenure), ByteArray);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000202}
203
204
205Handle<Map> Factory::NewMap(InstanceType type, int instance_size) {
206 CALL_HEAP_FUNCTION(Heap::AllocateMap(type, instance_size), Map);
207}
208
209
210Handle<JSObject> Factory::NewFunctionPrototype(Handle<JSFunction> function) {
211 CALL_HEAP_FUNCTION(Heap::AllocateFunctionPrototype(*function), JSObject);
212}
213
214
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000215Handle<Map> Factory::CopyMapDropDescriptors(Handle<Map> src) {
216 CALL_HEAP_FUNCTION(src->CopyDropDescriptors(), Map);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000217}
218
219
ager@chromium.org32912102009-01-16 10:38:43 +0000220Handle<Map> Factory::CopyMap(Handle<Map> src,
221 int extra_inobject_properties) {
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000222 Handle<Map> copy = CopyMapDropDescriptors(src);
ager@chromium.org32912102009-01-16 10:38:43 +0000223 // Check that we do not overflow the instance size when adding the
224 // extra inobject properties.
225 int instance_size_delta = extra_inobject_properties * kPointerSize;
226 int max_instance_size_delta =
227 JSObject::kMaxInstanceSize - copy->instance_size();
228 if (instance_size_delta > max_instance_size_delta) {
229 // If the instance size overflows, we allocate as many properties
230 // as we can as inobject properties.
231 instance_size_delta = max_instance_size_delta;
232 extra_inobject_properties = max_instance_size_delta >> kPointerSizeLog2;
233 }
234 // Adjust the map with the extra inobject properties.
235 int inobject_properties =
236 copy->inobject_properties() + extra_inobject_properties;
237 copy->set_inobject_properties(inobject_properties);
238 copy->set_unused_property_fields(inobject_properties);
239 copy->set_instance_size(copy->instance_size() + instance_size_delta);
240 return copy;
241}
242
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000243Handle<Map> Factory::CopyMapDropTransitions(Handle<Map> src) {
244 CALL_HEAP_FUNCTION(src->CopyDropTransitions(), Map);
245}
246
247
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000248Handle<FixedArray> Factory::CopyFixedArray(Handle<FixedArray> array) {
249 CALL_HEAP_FUNCTION(array->Copy(), FixedArray);
250}
251
252
253Handle<JSFunction> Factory::BaseNewFunctionFromBoilerplate(
254 Handle<JSFunction> boilerplate,
255 Handle<Map> function_map) {
256 ASSERT(boilerplate->IsBoilerplate());
257 ASSERT(!boilerplate->has_initial_map());
258 ASSERT(!boilerplate->has_prototype());
259 ASSERT(boilerplate->properties() == Heap::empty_fixed_array());
260 ASSERT(boilerplate->elements() == Heap::empty_fixed_array());
261 CALL_HEAP_FUNCTION(Heap::AllocateFunction(*function_map,
262 boilerplate->shared(),
263 Heap::the_hole_value()),
264 JSFunction);
265}
266
267
268Handle<JSFunction> Factory::NewFunctionFromBoilerplate(
269 Handle<JSFunction> boilerplate,
270 Handle<Context> context) {
271 Handle<JSFunction> result =
272 BaseNewFunctionFromBoilerplate(boilerplate, Top::function_map());
273 result->set_context(*context);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000274 int number_of_literals = boilerplate->NumberOfLiterals();
275 Handle<FixedArray> literals =
276 Factory::NewFixedArray(number_of_literals, TENURED);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000277 if (number_of_literals > 0) {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000278 // Store the object, regexp and array functions in the literals
279 // array prefix. These functions will be used when creating
280 // object, regexp and array literals in this function.
ager@chromium.org236ad962008-09-25 09:45:57 +0000281 literals->set(JSFunction::kLiteralGlobalContextIndex,
282 context->global_context());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000283 }
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000284 result->set_literals(*literals);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000285 ASSERT(!result->IsBoilerplate());
286 return result;
287}
288
289
290Handle<Object> Factory::NewNumber(double value,
291 PretenureFlag pretenure) {
292 CALL_HEAP_FUNCTION(Heap::NumberFromDouble(value, pretenure), Object);
293}
294
295
296Handle<Object> Factory::NewNumberFromInt(int value) {
297 CALL_HEAP_FUNCTION(Heap::NumberFromInt32(value), Object);
298}
299
300
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000301Handle<Object> Factory::NewNumberFromUint(uint32_t value) {
302 CALL_HEAP_FUNCTION(Heap::NumberFromUint32(value), Object);
303}
304
305
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000306Handle<JSObject> Factory::NewNeanderObject() {
307 CALL_HEAP_FUNCTION(Heap::AllocateJSObjectFromMap(Heap::neander_map()),
308 JSObject);
309}
310
311
312Handle<Object> Factory::NewTypeError(const char* type,
313 Vector< Handle<Object> > args) {
314 return NewError("MakeTypeError", type, args);
315}
316
317
318Handle<Object> Factory::NewTypeError(Handle<String> message) {
319 return NewError("$TypeError", message);
320}
321
322
323Handle<Object> Factory::NewRangeError(const char* type,
324 Vector< Handle<Object> > args) {
325 return NewError("MakeRangeError", type, args);
326}
327
328
329Handle<Object> Factory::NewRangeError(Handle<String> message) {
330 return NewError("$RangeError", message);
331}
332
333
334Handle<Object> Factory::NewSyntaxError(const char* type, Handle<JSArray> args) {
335 return NewError("MakeSyntaxError", type, args);
336}
337
338
339Handle<Object> Factory::NewSyntaxError(Handle<String> message) {
340 return NewError("$SyntaxError", message);
341}
342
343
344Handle<Object> Factory::NewReferenceError(const char* type,
345 Vector< Handle<Object> > args) {
346 return NewError("MakeReferenceError", type, args);
347}
348
349
350Handle<Object> Factory::NewReferenceError(Handle<String> message) {
351 return NewError("$ReferenceError", message);
352}
353
354
355Handle<Object> Factory::NewError(const char* maker, const char* type,
356 Vector< Handle<Object> > args) {
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000357 v8::HandleScope scope; // Instantiate a closeable HandleScope for EscapeFrom.
ager@chromium.org7c537e22008-10-16 08:43:32 +0000358 Handle<FixedArray> array = Factory::NewFixedArray(args.length());
359 for (int i = 0; i < args.length(); i++) {
360 array->set(i, *args[i]);
361 }
362 Handle<JSArray> object = Factory::NewJSArrayWithElements(array);
363 Handle<Object> result = NewError(maker, type, object);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000364 return result.EscapeFrom(&scope);
365}
366
367
368Handle<Object> Factory::NewEvalError(const char* type,
369 Vector< Handle<Object> > args) {
370 return NewError("MakeEvalError", type, args);
371}
372
373
374Handle<Object> Factory::NewError(const char* type,
375 Vector< Handle<Object> > args) {
376 return NewError("MakeError", type, args);
377}
378
379
380Handle<Object> Factory::NewError(const char* maker,
381 const char* type,
382 Handle<JSArray> args) {
383 Handle<String> make_str = Factory::LookupAsciiSymbol(maker);
384 Handle<JSFunction> fun =
385 Handle<JSFunction>(
386 JSFunction::cast(
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000387 Top::builtins()->GetProperty(*make_str)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000388 Handle<Object> type_obj = Factory::LookupAsciiSymbol(type);
389 Object** argv[2] = { type_obj.location(),
390 Handle<Object>::cast(args).location() };
391
392 // Invoke the JavaScript factory method. If an exception is thrown while
393 // running the factory method, use the exception as the result.
394 bool caught_exception;
395 Handle<Object> result = Execution::TryCall(fun,
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000396 Top::builtins(),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000397 2,
398 argv,
399 &caught_exception);
400 return result;
401}
402
403
404Handle<Object> Factory::NewError(Handle<String> message) {
405 return NewError("$Error", message);
406}
407
408
409Handle<Object> Factory::NewError(const char* constructor,
410 Handle<String> message) {
411 Handle<String> constr = Factory::LookupAsciiSymbol(constructor);
412 Handle<JSFunction> fun =
413 Handle<JSFunction>(
414 JSFunction::cast(
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000415 Top::builtins()->GetProperty(*constr)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000416 Object** argv[1] = { Handle<Object>::cast(message).location() };
417
418 // Invoke the JavaScript factory method. If an exception is thrown while
419 // running the factory method, use the exception as the result.
420 bool caught_exception;
421 Handle<Object> result = Execution::TryCall(fun,
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000422 Top::builtins(),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000423 1,
424 argv,
425 &caught_exception);
426 return result;
427}
428
429
430Handle<JSFunction> Factory::NewFunction(Handle<String> name,
431 InstanceType type,
432 int instance_size,
433 Handle<Code> code,
434 bool force_initial_map) {
435 // Allocate the function
436 Handle<JSFunction> function = NewFunction(name, the_hole_value());
437 function->set_code(*code);
438
439 if (force_initial_map ||
440 type != JS_OBJECT_TYPE ||
441 instance_size != JSObject::kHeaderSize) {
442 Handle<Map> initial_map = NewMap(type, instance_size);
443 Handle<JSObject> prototype = NewFunctionPrototype(function);
444 initial_map->set_prototype(*prototype);
445 function->set_initial_map(*initial_map);
446 initial_map->set_constructor(*function);
447 } else {
448 ASSERT(!function->has_initial_map());
449 ASSERT(!function->has_prototype());
450 }
451
452 return function;
453}
454
455
456Handle<JSFunction> Factory::NewFunctionBoilerplate(Handle<String> name,
457 int number_of_literals,
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000458 bool contains_array_literal,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000459 Handle<Code> code) {
460 Handle<JSFunction> function = NewFunctionBoilerplate(name);
461 function->set_code(*code);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000462 int literals_array_size = number_of_literals;
463 // If the function contains object, regexp or array literals,
464 // allocate extra space for a literals array prefix containing the
465 // object, regexp and array constructor functions.
466 if (number_of_literals > 0 || contains_array_literal) {
467 literals_array_size += JSFunction::kLiteralsPrefixSize;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000468 }
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000469 Handle<FixedArray> literals =
470 Factory::NewFixedArray(literals_array_size, TENURED);
471 function->set_literals(*literals);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000472 ASSERT(!function->has_initial_map());
473 ASSERT(!function->has_prototype());
474 return function;
475}
476
477
478Handle<JSFunction> Factory::NewFunctionBoilerplate(Handle<String> name) {
479 Handle<SharedFunctionInfo> shared = NewSharedFunctionInfo(name);
480 CALL_HEAP_FUNCTION(Heap::AllocateFunction(Heap::boilerplate_function_map(),
481 *shared,
482 Heap::the_hole_value()),
483 JSFunction);
484}
485
486
487Handle<JSFunction> Factory::NewFunctionWithPrototype(Handle<String> name,
488 InstanceType type,
489 int instance_size,
490 Handle<JSObject> prototype,
491 Handle<Code> code,
492 bool force_initial_map) {
493 // Allocate the function
494 Handle<JSFunction> function = NewFunction(name, prototype);
495
496 function->set_code(*code);
497
498 if (force_initial_map ||
499 type != JS_OBJECT_TYPE ||
500 instance_size != JSObject::kHeaderSize) {
501 Handle<Map> initial_map = NewMap(type, instance_size);
502 function->set_initial_map(*initial_map);
503 initial_map->set_constructor(*function);
504 }
505
506 // Set function.prototype and give the prototype a constructor
507 // property that refers to the function.
508 SetPrototypeProperty(function, prototype);
509 SetProperty(prototype, Factory::constructor_symbol(), function, DONT_ENUM);
510 return function;
511}
512
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000513
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000514Handle<Code> Factory::NewCode(const CodeDesc& desc,
515 ZoneScopeInfo* sinfo,
516 Code::Flags flags,
517 Handle<Object> self_ref) {
kasperl@chromium.org061ef742009-02-27 12:16:20 +0000518 CALL_HEAP_FUNCTION(Heap::CreateCode(desc, sinfo, flags, self_ref), Code);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000519}
520
521
522Handle<Code> Factory::CopyCode(Handle<Code> code) {
523 CALL_HEAP_FUNCTION(Heap::CopyCode(*code), Code);
524}
525
526
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000527static inline Object* DoCopyInsert(DescriptorArray* array,
528 String* key,
529 Object* value,
530 PropertyAttributes attributes) {
531 CallbacksDescriptor desc(key, value, attributes);
532 Object* obj = array->CopyInsert(&desc, REMOVE_TRANSITIONS);
533 return obj;
534}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000535
536
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000537// Allocate the new array.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000538Handle<DescriptorArray> Factory::CopyAppendProxyDescriptor(
539 Handle<DescriptorArray> array,
540 Handle<String> key,
541 Handle<Object> value,
542 PropertyAttributes attributes) {
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000543 CALL_HEAP_FUNCTION(DoCopyInsert(*array, *key, *value, attributes),
544 DescriptorArray);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000545}
546
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000547
548Handle<String> Factory::SymbolFromString(Handle<String> value) {
549 CALL_HEAP_FUNCTION(Heap::LookupSymbol(*value), String);
550}
551
552
553Handle<DescriptorArray> Factory::CopyAppendCallbackDescriptors(
554 Handle<DescriptorArray> array,
555 Handle<Object> descriptors) {
556 v8::NeanderArray callbacks(descriptors);
557 int nof_callbacks = callbacks.length();
558 Handle<DescriptorArray> result =
559 NewDescriptorArray(array->number_of_descriptors() + nof_callbacks);
560
561 // Number of descriptors added to the result so far.
562 int descriptor_count = 0;
563
564 // Copy the descriptors from the array.
565 DescriptorWriter w(*result);
566 for (DescriptorReader r(*array); !r.eos(); r.advance()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000567 if (!r.IsNullDescriptor()) {
568 w.WriteFrom(&r);
569 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000570 descriptor_count++;
571 }
572
573 // Number of duplicates detected.
574 int duplicates = 0;
575
576 // Fill in new callback descriptors. Process the callbacks from
577 // back to front so that the last callback with a given name takes
578 // precedence over previously added callbacks with that name.
579 for (int i = nof_callbacks - 1; i >= 0; i--) {
580 Handle<AccessorInfo> entry =
581 Handle<AccessorInfo>(AccessorInfo::cast(callbacks.get(i)));
582 // Ensure the key is a symbol before writing into the instance descriptor.
583 Handle<String> key =
584 SymbolFromString(Handle<String>(String::cast(entry->name())));
585 // Check if a descriptor with this name already exists before writing.
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000586 if (result->LinearSearch(*key, descriptor_count) ==
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000587 DescriptorArray::kNotFound) {
588 CallbacksDescriptor desc(*key, *entry, entry->property_attributes());
589 w.Write(&desc);
590 descriptor_count++;
591 } else {
592 duplicates++;
593 }
594 }
595
596 // If duplicates were detected, allocate a result of the right size
597 // and transfer the elements.
598 if (duplicates > 0) {
599 Handle<DescriptorArray> new_result =
600 NewDescriptorArray(result->number_of_descriptors() - duplicates);
601 DescriptorWriter w(*new_result);
602 DescriptorReader r(*result);
603 while (!w.eos()) {
604 w.WriteFrom(&r);
605 r.advance();
606 }
607 result = new_result;
608 }
609
610 // Sort the result before returning.
611 result->Sort();
612 return result;
613}
614
615
616Handle<JSObject> Factory::NewJSObject(Handle<JSFunction> constructor,
617 PretenureFlag pretenure) {
618 CALL_HEAP_FUNCTION(Heap::AllocateJSObject(*constructor, pretenure), JSObject);
619}
620
621
kasperl@chromium.org2abc4502009-07-02 07:00:29 +0000622Handle<GlobalObject> Factory::NewGlobalObject(
623 Handle<JSFunction> constructor) {
624 CALL_HEAP_FUNCTION(Heap::AllocateGlobalObject(*constructor),
625 GlobalObject);
626}
627
628
629
ager@chromium.org236ad962008-09-25 09:45:57 +0000630Handle<JSObject> Factory::NewJSObjectFromMap(Handle<Map> map) {
631 CALL_HEAP_FUNCTION(Heap::AllocateJSObjectFromMap(*map, NOT_TENURED),
632 JSObject);
633}
634
635
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000636Handle<JSArray> Factory::NewJSArray(int length,
637 PretenureFlag pretenure) {
638 Handle<JSObject> obj = NewJSObject(Top::array_function(), pretenure);
639 CALL_HEAP_FUNCTION(Handle<JSArray>::cast(obj)->Initialize(length), JSArray);
640}
641
642
643Handle<JSArray> Factory::NewJSArrayWithElements(Handle<FixedArray> elements,
644 PretenureFlag pretenure) {
645 Handle<JSArray> result =
646 Handle<JSArray>::cast(NewJSObject(Top::array_function(), pretenure));
647 result->SetContent(*elements);
648 return result;
649}
650
651
652Handle<SharedFunctionInfo> Factory::NewSharedFunctionInfo(Handle<String> name) {
653 CALL_HEAP_FUNCTION(Heap::AllocateSharedFunctionInfo(*name),
654 SharedFunctionInfo);
655}
656
657
658Handle<Dictionary> Factory::DictionaryAtNumberPut(Handle<Dictionary> dictionary,
659 uint32_t key,
660 Handle<Object> value) {
661 CALL_HEAP_FUNCTION(dictionary->AtNumberPut(key, *value), Dictionary);
662}
663
664
665Handle<JSFunction> Factory::NewFunctionHelper(Handle<String> name,
666 Handle<Object> prototype) {
667 Handle<SharedFunctionInfo> function_share = NewSharedFunctionInfo(name);
668 CALL_HEAP_FUNCTION(Heap::AllocateFunction(*Top::function_map(),
669 *function_share,
670 *prototype),
671 JSFunction);
672}
673
674
675Handle<JSFunction> Factory::NewFunction(Handle<String> name,
676 Handle<Object> prototype) {
677 Handle<JSFunction> fun = NewFunctionHelper(name, prototype);
678 fun->set_context(Top::context()->global_context());
679 return fun;
680}
681
682
683Handle<Object> Factory::ToObject(Handle<Object> object,
684 Handle<Context> global_context) {
685 CALL_HEAP_FUNCTION(object->ToObject(*global_context), Object);
686}
687
688
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000689#ifdef ENABLE_DEBUGGER_SUPPORT
v8.team.kasperl727e9952008-09-02 14:56:44 +0000690Handle<DebugInfo> Factory::NewDebugInfo(Handle<SharedFunctionInfo> shared) {
691 // Get the original code of the function.
692 Handle<Code> code(shared->code());
693
694 // Create a copy of the code before allocating the debug info object to avoid
695 // allocation while setting up the debug info object.
696 Handle<Code> original_code(*Factory::CopyCode(code));
697
698 // Allocate initial fixed array for active break points before allocating the
699 // debug info object to avoid allocation while setting up the debug info
700 // object.
701 Handle<FixedArray> break_points(
702 Factory::NewFixedArray(Debug::kEstimatedNofBreakPointsInFunction));
703
704 // Create and set up the debug info object. Debug info contains function, a
705 // copy of the original code, the executing code and initial fixed array for
706 // active break points.
707 Handle<DebugInfo> debug_info =
708 Handle<DebugInfo>::cast(Factory::NewStruct(DEBUG_INFO_TYPE));
709 debug_info->set_shared(*shared);
710 debug_info->set_original_code(*original_code);
711 debug_info->set_code(*code);
712 debug_info->set_break_points(*break_points);
713
714 // Link debug info to function.
715 shared->set_debug_info(*debug_info);
716
717 return debug_info;
718}
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000719#endif
v8.team.kasperl727e9952008-09-02 14:56:44 +0000720
721
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000722Handle<JSObject> Factory::NewArgumentsObject(Handle<Object> callee,
723 int length) {
724 CALL_HEAP_FUNCTION(Heap::AllocateArgumentsObject(*callee, length), JSObject);
725}
726
727
728Handle<JSFunction> Factory::CreateApiFunction(
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000729 Handle<FunctionTemplateInfo> obj, ApiInstanceType instance_type) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000730 Handle<Code> code = Handle<Code>(Builtins::builtin(Builtins::HandleApiCall));
731
kasper.lund212ac232008-07-16 07:07:30 +0000732 int internal_field_count = 0;
733 if (!obj->instance_template()->IsUndefined()) {
734 Handle<ObjectTemplateInfo> instance_template =
735 Handle<ObjectTemplateInfo>(
736 ObjectTemplateInfo::cast(obj->instance_template()));
737 internal_field_count =
738 Smi::cast(instance_template->internal_field_count())->value();
739 }
740
741 int instance_size = kPointerSize * internal_field_count;
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000742 InstanceType type = INVALID_TYPE;
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000743 switch (instance_type) {
744 case JavaScriptObject:
745 type = JS_OBJECT_TYPE;
746 instance_size += JSObject::kHeaderSize;
747 break;
748 case InnerGlobalObject:
749 type = JS_GLOBAL_OBJECT_TYPE;
750 instance_size += JSGlobalObject::kSize;
751 break;
752 case OuterGlobalObject:
753 type = JS_GLOBAL_PROXY_TYPE;
754 instance_size += JSGlobalProxy::kSize;
755 break;
756 default:
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000757 break;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000758 }
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000759 ASSERT(type != INVALID_TYPE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000760
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000761 Handle<JSFunction> result =
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000762 Factory::NewFunction(Factory::empty_symbol(),
763 type,
764 instance_size,
765 code,
766 true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000767 // Set class name.
768 Handle<Object> class_name = Handle<Object>(obj->class_name());
769 if (class_name->IsString()) {
770 result->shared()->set_instance_class_name(*class_name);
771 result->shared()->set_name(*class_name);
772 }
773
774 Handle<Map> map = Handle<Map>(result->initial_map());
775
776 // Mark as undetectable if needed.
777 if (obj->undetectable()) {
778 map->set_is_undetectable();
779 }
780
781 // Mark as hidden for the __proto__ accessor if needed.
782 if (obj->hidden_prototype()) {
783 map->set_is_hidden_prototype();
784 }
785
786 // Mark as needs_access_check if needed.
787 if (obj->needs_access_check()) {
ager@chromium.org870a0b62008-11-04 11:43:05 +0000788 map->set_is_access_check_needed(true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000789 }
790
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000791 // Set interceptor information in the map.
792 if (!obj->named_property_handler()->IsUndefined()) {
793 map->set_has_named_interceptor();
794 }
795 if (!obj->indexed_property_handler()->IsUndefined()) {
796 map->set_has_indexed_interceptor();
797 }
798
799 // Set instance call-as-function information in the map.
800 if (!obj->instance_call_handler()->IsUndefined()) {
801 map->set_has_instance_call_handler();
802 }
803
804 result->shared()->set_function_data(*obj);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000805 result->shared()->DontAdaptArguments();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000806
807 // Recursively copy parent templates' accessors, 'data' may be modified.
808 Handle<DescriptorArray> array =
809 Handle<DescriptorArray>(map->instance_descriptors());
810 while (true) {
811 Handle<Object> props = Handle<Object>(obj->property_accessors());
812 if (!props->IsUndefined()) {
813 array = Factory::CopyAppendCallbackDescriptors(array, props);
814 }
815 Handle<Object> parent = Handle<Object>(obj->parent_template());
816 if (parent->IsUndefined()) break;
817 obj = Handle<FunctionTemplateInfo>::cast(parent);
818 }
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000819 if (!array->IsEmpty()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000820 map->set_instance_descriptors(*array);
821 }
822
823 return result;
824}
825
826
ager@chromium.org236ad962008-09-25 09:45:57 +0000827Handle<MapCache> Factory::NewMapCache(int at_least_space_for) {
828 CALL_HEAP_FUNCTION(MapCache::Allocate(at_least_space_for), MapCache);
829}
830
831
832static Object* UpdateMapCacheWith(Context* context,
833 FixedArray* keys,
834 Map* map) {
835 Object* result = MapCache::cast(context->map_cache())->Put(keys, map);
836 if (!result->IsFailure()) context->set_map_cache(MapCache::cast(result));
837 return result;
838}
839
840
841Handle<MapCache> Factory::AddToMapCache(Handle<Context> context,
842 Handle<FixedArray> keys,
843 Handle<Map> map) {
844 CALL_HEAP_FUNCTION(UpdateMapCacheWith(*context, *keys, *map), MapCache);
845}
846
847
848Handle<Map> Factory::ObjectLiteralMapFromCache(Handle<Context> context,
849 Handle<FixedArray> keys) {
850 if (context->map_cache()->IsUndefined()) {
851 // Allocate the new map cache for the global context.
852 Handle<MapCache> new_cache = NewMapCache(24);
853 context->set_map_cache(*new_cache);
854 }
ager@chromium.org32912102009-01-16 10:38:43 +0000855 // Check to see whether there is a matching element in the cache.
ager@chromium.org236ad962008-09-25 09:45:57 +0000856 Handle<MapCache> cache =
857 Handle<MapCache>(MapCache::cast(context->map_cache()));
858 Handle<Object> result = Handle<Object>(cache->Lookup(*keys));
859 if (result->IsMap()) return Handle<Map>::cast(result);
860 // Create a new map and add it to the cache.
861 Handle<Map> map =
ager@chromium.org32912102009-01-16 10:38:43 +0000862 CopyMap(Handle<Map>(context->object_function()->initial_map()),
863 keys->length());
ager@chromium.org236ad962008-09-25 09:45:57 +0000864 AddToMapCache(context, keys, map);
865 return Handle<Map>(map);
866}
867
868
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000869void Factory::SetRegExpAtomData(Handle<JSRegExp> regexp,
870 JSRegExp::Type type,
871 Handle<String> source,
872 JSRegExp::Flags flags,
873 Handle<Object> data) {
874 Handle<FixedArray> store = NewFixedArray(JSRegExp::kAtomDataSize);
875
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000876 store->set(JSRegExp::kTagIndex, Smi::FromInt(type));
877 store->set(JSRegExp::kSourceIndex, *source);
878 store->set(JSRegExp::kFlagsIndex, Smi::FromInt(flags.value()));
879 store->set(JSRegExp::kAtomPatternIndex, *data);
880 regexp->set_data(*store);
881}
882
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000883void Factory::SetRegExpIrregexpData(Handle<JSRegExp> regexp,
884 JSRegExp::Type type,
885 Handle<String> source,
886 JSRegExp::Flags flags,
887 int capture_count) {
888 Handle<FixedArray> store = NewFixedArray(JSRegExp::kIrregexpDataSize);
889
890 store->set(JSRegExp::kTagIndex, Smi::FromInt(type));
891 store->set(JSRegExp::kSourceIndex, *source);
892 store->set(JSRegExp::kFlagsIndex, Smi::FromInt(flags.value()));
893 store->set(JSRegExp::kIrregexpASCIICodeIndex, Heap::the_hole_value());
894 store->set(JSRegExp::kIrregexpUC16CodeIndex, Heap::the_hole_value());
895 store->set(JSRegExp::kIrregexpMaxRegisterCountIndex, Smi::FromInt(0));
896 store->set(JSRegExp::kIrregexpCaptureCountIndex,
897 Smi::FromInt(capture_count));
898 regexp->set_data(*store);
899}
900
901
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000902
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000903void Factory::ConfigureInstance(Handle<FunctionTemplateInfo> desc,
904 Handle<JSObject> instance,
905 bool* pending_exception) {
906 // Configure the instance by adding the properties specified by the
907 // instance template.
908 Handle<Object> instance_template = Handle<Object>(desc->instance_template());
909 if (!instance_template->IsUndefined()) {
910 Execution::ConfigureInstance(instance,
911 instance_template,
912 pending_exception);
913 } else {
914 *pending_exception = false;
915 }
916}
917
918
919} } // namespace v8::internal