blob: c849ab71e94bc1c6e36a1c4b82f739e91a7bca55 [file] [log] [blame]
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001// Copyright 2006-2008 the V8 project authors. All rights reserved.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28#include "v8.h"
29
30#include "api.h"
v8.team.kasperl727e9952008-09-02 14:56:44 +000031#include "debug.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000032#include "execution.h"
33#include "factory.h"
34#include "macro-assembler.h"
35
36namespace v8 { namespace internal {
37
38
39Handle<FixedArray> Factory::NewFixedArray(int size, PretenureFlag pretenure) {
40 ASSERT(0 <= size);
41 CALL_HEAP_FUNCTION(Heap::AllocateFixedArray(size, pretenure), FixedArray);
42}
43
44
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +000045Handle<FixedArray> Factory::NewFixedArrayWithHoles(int size) {
46 ASSERT(0 <= size);
47 CALL_HEAP_FUNCTION(Heap::AllocateFixedArrayWithHoles(size), FixedArray);
48}
49
50
51Handle<Dictionary> Factory::NewDictionary(int at_least_space_for) {
52 ASSERT(0 <= at_least_space_for);
53 CALL_HEAP_FUNCTION(Dictionary::Allocate(at_least_space_for), Dictionary);
54}
55
56
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000057Handle<DescriptorArray> Factory::NewDescriptorArray(int number_of_descriptors) {
58 ASSERT(0 <= number_of_descriptors);
59 CALL_HEAP_FUNCTION(DescriptorArray::Allocate(number_of_descriptors),
60 DescriptorArray);
61}
62
63
ager@chromium.org9258b6b2008-09-11 09:11:10 +000064// Symbols are created in the old generation (data space).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000065Handle<String> Factory::LookupSymbol(Vector<const char> string) {
66 CALL_HEAP_FUNCTION(Heap::LookupSymbol(string), String);
67}
68
69
70Handle<String> Factory::NewStringFromAscii(Vector<const char> string,
71 PretenureFlag pretenure) {
72 CALL_HEAP_FUNCTION(Heap::AllocateStringFromAscii(string, pretenure), String);
73}
74
75Handle<String> Factory::NewStringFromUtf8(Vector<const char> string,
76 PretenureFlag pretenure) {
77 CALL_HEAP_FUNCTION(Heap::AllocateStringFromUtf8(string, pretenure), String);
78}
79
80
81Handle<String> Factory::NewStringFromTwoByte(Vector<const uc16> string) {
82 CALL_HEAP_FUNCTION(Heap::AllocateStringFromTwoByte(string), String);
83}
84
85
86Handle<String> Factory::NewRawTwoByteString(int length,
87 PretenureFlag pretenure) {
88 CALL_HEAP_FUNCTION(Heap::AllocateRawTwoByteString(length, pretenure), String);
89}
90
91
92Handle<String> Factory::NewConsString(Handle<String> first,
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000093 Handle<String> second) {
94 if (first->length() == 0) return second;
95 if (second->length() == 0) return first;
ager@chromium.orgc3e50d82008-11-05 11:53:10 +000096 CALL_HEAP_FUNCTION(Heap::AllocateConsString(*first, *second), String);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000097}
98
99
ager@chromium.org870a0b62008-11-04 11:43:05 +0000100Handle<String> Factory::NewStringSlice(Handle<String> str,
ager@chromium.org870a0b62008-11-04 11:43:05 +0000101 int begin,
102 int end) {
ager@chromium.orgc3e50d82008-11-05 11:53:10 +0000103 CALL_HEAP_FUNCTION(str->Slice(begin, end), String);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000104}
105
106
107Handle<String> Factory::NewExternalStringFromAscii(
108 ExternalAsciiString::Resource* resource) {
109 CALL_HEAP_FUNCTION(Heap::AllocateExternalStringFromAscii(resource), String);
110}
111
112
113Handle<String> Factory::NewExternalStringFromTwoByte(
114 ExternalTwoByteString::Resource* resource) {
115 CALL_HEAP_FUNCTION(Heap::AllocateExternalStringFromTwoByte(resource), String);
116}
117
118
119Handle<Context> Factory::NewGlobalContext() {
120 CALL_HEAP_FUNCTION(Heap::AllocateGlobalContext(), Context);
121}
122
123
124Handle<Context> Factory::NewFunctionContext(int length,
125 Handle<JSFunction> closure) {
126 CALL_HEAP_FUNCTION(Heap::AllocateFunctionContext(length, *closure), Context);
127}
128
129
130Handle<Context> Factory::NewWithContext(Handle<Context> previous,
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000131 Handle<JSObject> extension,
132 bool is_catch_context) {
133 CALL_HEAP_FUNCTION(Heap::AllocateWithContext(*previous,
134 *extension,
135 is_catch_context),
136 Context);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000137}
138
139
140Handle<Struct> Factory::NewStruct(InstanceType type) {
141 CALL_HEAP_FUNCTION(Heap::AllocateStruct(type), Struct);
142}
143
144
145Handle<AccessorInfo> Factory::NewAccessorInfo() {
146 Handle<AccessorInfo> info =
147 Handle<AccessorInfo>::cast(NewStruct(ACCESSOR_INFO_TYPE));
148 info->set_flag(0); // Must clear the flag, it was initialized as undefined.
149 return info;
150}
151
152
153Handle<Script> Factory::NewScript(Handle<String> source) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000154 // Generate id for this script.
155 int id;
156 if (Heap::last_script_id()->IsUndefined()) {
157 // Script ids start from one.
158 id = 1;
159 } else {
160 // Increment id, wrap when positive smi is exhausted.
161 id = Smi::cast(Heap::last_script_id())->value();
162 id++;
163 if (!Smi::IsValid(id)) {
164 id = 0;
165 }
166 }
167 Heap::SetLastScriptId(Smi::FromInt(id));
168
169 // Create and initialize script object.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000170 Handle<Script> script = Handle<Script>::cast(NewStruct(SCRIPT_TYPE));
171 script->set_source(*source);
172 script->set_name(Heap::undefined_value());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000173 script->set_id(Heap::last_script_id());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000174 script->set_line_offset(Smi::FromInt(0));
175 script->set_column_offset(Smi::FromInt(0));
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000176 script->set_data(Heap::undefined_value());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000177 script->set_type(Smi::FromInt(SCRIPT_TYPE_NORMAL));
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000178 script->set_wrapper(*Factory::NewProxy(0, TENURED));
iposva@chromium.org245aa852009-02-10 00:49:54 +0000179 script->set_line_ends(Heap::undefined_value());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000180
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000181 return script;
182}
183
184
185Handle<Proxy> Factory::NewProxy(Address addr, PretenureFlag pretenure) {
186 CALL_HEAP_FUNCTION(Heap::AllocateProxy(addr, pretenure), Proxy);
187}
188
189
190Handle<Proxy> Factory::NewProxy(const AccessorDescriptor* desc) {
191 return NewProxy((Address) desc, TENURED);
192}
193
194
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000195Handle<ByteArray> Factory::NewByteArray(int length, PretenureFlag pretenure) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000196 ASSERT(0 <= length);
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000197 CALL_HEAP_FUNCTION(Heap::AllocateByteArray(length, pretenure), ByteArray);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000198}
199
200
201Handle<Map> Factory::NewMap(InstanceType type, int instance_size) {
202 CALL_HEAP_FUNCTION(Heap::AllocateMap(type, instance_size), Map);
203}
204
205
206Handle<JSObject> Factory::NewFunctionPrototype(Handle<JSFunction> function) {
207 CALL_HEAP_FUNCTION(Heap::AllocateFunctionPrototype(*function), JSObject);
208}
209
210
211Handle<Map> Factory::CopyMap(Handle<Map> src) {
212 CALL_HEAP_FUNCTION(src->Copy(), Map);
213}
214
215
ager@chromium.org32912102009-01-16 10:38:43 +0000216Handle<Map> Factory::CopyMap(Handle<Map> src,
217 int extra_inobject_properties) {
218 Handle<Map> copy = CopyMap(src);
219 // Check that we do not overflow the instance size when adding the
220 // extra inobject properties.
221 int instance_size_delta = extra_inobject_properties * kPointerSize;
222 int max_instance_size_delta =
223 JSObject::kMaxInstanceSize - copy->instance_size();
224 if (instance_size_delta > max_instance_size_delta) {
225 // If the instance size overflows, we allocate as many properties
226 // as we can as inobject properties.
227 instance_size_delta = max_instance_size_delta;
228 extra_inobject_properties = max_instance_size_delta >> kPointerSizeLog2;
229 }
230 // Adjust the map with the extra inobject properties.
231 int inobject_properties =
232 copy->inobject_properties() + extra_inobject_properties;
233 copy->set_inobject_properties(inobject_properties);
234 copy->set_unused_property_fields(inobject_properties);
235 copy->set_instance_size(copy->instance_size() + instance_size_delta);
236 return copy;
237}
238
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000239Handle<Map> Factory::CopyMapDropTransitions(Handle<Map> src) {
240 CALL_HEAP_FUNCTION(src->CopyDropTransitions(), Map);
241}
242
243
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000244Handle<FixedArray> Factory::CopyFixedArray(Handle<FixedArray> array) {
245 CALL_HEAP_FUNCTION(array->Copy(), FixedArray);
246}
247
248
249Handle<JSFunction> Factory::BaseNewFunctionFromBoilerplate(
250 Handle<JSFunction> boilerplate,
251 Handle<Map> function_map) {
252 ASSERT(boilerplate->IsBoilerplate());
253 ASSERT(!boilerplate->has_initial_map());
254 ASSERT(!boilerplate->has_prototype());
255 ASSERT(boilerplate->properties() == Heap::empty_fixed_array());
256 ASSERT(boilerplate->elements() == Heap::empty_fixed_array());
257 CALL_HEAP_FUNCTION(Heap::AllocateFunction(*function_map,
258 boilerplate->shared(),
259 Heap::the_hole_value()),
260 JSFunction);
261}
262
263
264Handle<JSFunction> Factory::NewFunctionFromBoilerplate(
265 Handle<JSFunction> boilerplate,
266 Handle<Context> context) {
267 Handle<JSFunction> result =
268 BaseNewFunctionFromBoilerplate(boilerplate, Top::function_map());
269 result->set_context(*context);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000270 int number_of_literals = boilerplate->NumberOfLiterals();
271 Handle<FixedArray> literals =
272 Factory::NewFixedArray(number_of_literals, TENURED);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000273 if (number_of_literals > 0) {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000274 // Store the object, regexp and array functions in the literals
275 // array prefix. These functions will be used when creating
276 // object, regexp and array literals in this function.
ager@chromium.org236ad962008-09-25 09:45:57 +0000277 literals->set(JSFunction::kLiteralGlobalContextIndex,
278 context->global_context());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000279 }
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000280 result->set_literals(*literals);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000281 ASSERT(!result->IsBoilerplate());
282 return result;
283}
284
285
286Handle<Object> Factory::NewNumber(double value,
287 PretenureFlag pretenure) {
288 CALL_HEAP_FUNCTION(Heap::NumberFromDouble(value, pretenure), Object);
289}
290
291
292Handle<Object> Factory::NewNumberFromInt(int value) {
293 CALL_HEAP_FUNCTION(Heap::NumberFromInt32(value), Object);
294}
295
296
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000297Handle<Object> Factory::NewNumberFromUint(uint32_t value) {
298 CALL_HEAP_FUNCTION(Heap::NumberFromUint32(value), Object);
299}
300
301
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000302Handle<JSObject> Factory::NewNeanderObject() {
303 CALL_HEAP_FUNCTION(Heap::AllocateJSObjectFromMap(Heap::neander_map()),
304 JSObject);
305}
306
307
308Handle<Object> Factory::NewTypeError(const char* type,
309 Vector< Handle<Object> > args) {
310 return NewError("MakeTypeError", type, args);
311}
312
313
314Handle<Object> Factory::NewTypeError(Handle<String> message) {
315 return NewError("$TypeError", message);
316}
317
318
319Handle<Object> Factory::NewRangeError(const char* type,
320 Vector< Handle<Object> > args) {
321 return NewError("MakeRangeError", type, args);
322}
323
324
325Handle<Object> Factory::NewRangeError(Handle<String> message) {
326 return NewError("$RangeError", message);
327}
328
329
330Handle<Object> Factory::NewSyntaxError(const char* type, Handle<JSArray> args) {
331 return NewError("MakeSyntaxError", type, args);
332}
333
334
335Handle<Object> Factory::NewSyntaxError(Handle<String> message) {
336 return NewError("$SyntaxError", message);
337}
338
339
340Handle<Object> Factory::NewReferenceError(const char* type,
341 Vector< Handle<Object> > args) {
342 return NewError("MakeReferenceError", type, args);
343}
344
345
346Handle<Object> Factory::NewReferenceError(Handle<String> message) {
347 return NewError("$ReferenceError", message);
348}
349
350
351Handle<Object> Factory::NewError(const char* maker, const char* type,
352 Vector< Handle<Object> > args) {
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000353 v8::HandleScope scope; // Instantiate a closeable HandleScope for EscapeFrom.
ager@chromium.org7c537e22008-10-16 08:43:32 +0000354 Handle<FixedArray> array = Factory::NewFixedArray(args.length());
355 for (int i = 0; i < args.length(); i++) {
356 array->set(i, *args[i]);
357 }
358 Handle<JSArray> object = Factory::NewJSArrayWithElements(array);
359 Handle<Object> result = NewError(maker, type, object);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000360 return result.EscapeFrom(&scope);
361}
362
363
364Handle<Object> Factory::NewEvalError(const char* type,
365 Vector< Handle<Object> > args) {
366 return NewError("MakeEvalError", type, args);
367}
368
369
370Handle<Object> Factory::NewError(const char* type,
371 Vector< Handle<Object> > args) {
372 return NewError("MakeError", type, args);
373}
374
375
376Handle<Object> Factory::NewError(const char* maker,
377 const char* type,
378 Handle<JSArray> args) {
379 Handle<String> make_str = Factory::LookupAsciiSymbol(maker);
380 Handle<JSFunction> fun =
381 Handle<JSFunction>(
382 JSFunction::cast(
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000383 Top::builtins()->GetProperty(*make_str)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000384 Handle<Object> type_obj = Factory::LookupAsciiSymbol(type);
385 Object** argv[2] = { type_obj.location(),
386 Handle<Object>::cast(args).location() };
387
388 // Invoke the JavaScript factory method. If an exception is thrown while
389 // running the factory method, use the exception as the result.
390 bool caught_exception;
391 Handle<Object> result = Execution::TryCall(fun,
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000392 Top::builtins(),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000393 2,
394 argv,
395 &caught_exception);
396 return result;
397}
398
399
400Handle<Object> Factory::NewError(Handle<String> message) {
401 return NewError("$Error", message);
402}
403
404
405Handle<Object> Factory::NewError(const char* constructor,
406 Handle<String> message) {
407 Handle<String> constr = Factory::LookupAsciiSymbol(constructor);
408 Handle<JSFunction> fun =
409 Handle<JSFunction>(
410 JSFunction::cast(
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000411 Top::builtins()->GetProperty(*constr)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000412 Object** argv[1] = { Handle<Object>::cast(message).location() };
413
414 // Invoke the JavaScript factory method. If an exception is thrown while
415 // running the factory method, use the exception as the result.
416 bool caught_exception;
417 Handle<Object> result = Execution::TryCall(fun,
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000418 Top::builtins(),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000419 1,
420 argv,
421 &caught_exception);
422 return result;
423}
424
425
426Handle<JSFunction> Factory::NewFunction(Handle<String> name,
427 InstanceType type,
428 int instance_size,
429 Handle<Code> code,
430 bool force_initial_map) {
431 // Allocate the function
432 Handle<JSFunction> function = NewFunction(name, the_hole_value());
433 function->set_code(*code);
434
435 if (force_initial_map ||
436 type != JS_OBJECT_TYPE ||
437 instance_size != JSObject::kHeaderSize) {
438 Handle<Map> initial_map = NewMap(type, instance_size);
439 Handle<JSObject> prototype = NewFunctionPrototype(function);
440 initial_map->set_prototype(*prototype);
441 function->set_initial_map(*initial_map);
442 initial_map->set_constructor(*function);
443 } else {
444 ASSERT(!function->has_initial_map());
445 ASSERT(!function->has_prototype());
446 }
447
448 return function;
449}
450
451
452Handle<JSFunction> Factory::NewFunctionBoilerplate(Handle<String> name,
453 int number_of_literals,
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000454 bool contains_array_literal,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000455 Handle<Code> code) {
456 Handle<JSFunction> function = NewFunctionBoilerplate(name);
457 function->set_code(*code);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000458 int literals_array_size = number_of_literals;
459 // If the function contains object, regexp or array literals,
460 // allocate extra space for a literals array prefix containing the
461 // object, regexp and array constructor functions.
462 if (number_of_literals > 0 || contains_array_literal) {
463 literals_array_size += JSFunction::kLiteralsPrefixSize;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000464 }
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000465 Handle<FixedArray> literals =
466 Factory::NewFixedArray(literals_array_size, TENURED);
467 function->set_literals(*literals);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000468 ASSERT(!function->has_initial_map());
469 ASSERT(!function->has_prototype());
470 return function;
471}
472
473
474Handle<JSFunction> Factory::NewFunctionBoilerplate(Handle<String> name) {
475 Handle<SharedFunctionInfo> shared = NewSharedFunctionInfo(name);
476 CALL_HEAP_FUNCTION(Heap::AllocateFunction(Heap::boilerplate_function_map(),
477 *shared,
478 Heap::the_hole_value()),
479 JSFunction);
480}
481
482
483Handle<JSFunction> Factory::NewFunctionWithPrototype(Handle<String> name,
484 InstanceType type,
485 int instance_size,
486 Handle<JSObject> prototype,
487 Handle<Code> code,
488 bool force_initial_map) {
489 // Allocate the function
490 Handle<JSFunction> function = NewFunction(name, prototype);
491
492 function->set_code(*code);
493
494 if (force_initial_map ||
495 type != JS_OBJECT_TYPE ||
496 instance_size != JSObject::kHeaderSize) {
497 Handle<Map> initial_map = NewMap(type, instance_size);
498 function->set_initial_map(*initial_map);
499 initial_map->set_constructor(*function);
500 }
501
502 // Set function.prototype and give the prototype a constructor
503 // property that refers to the function.
504 SetPrototypeProperty(function, prototype);
505 SetProperty(prototype, Factory::constructor_symbol(), function, DONT_ENUM);
506 return function;
507}
508
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000509
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000510Handle<Code> Factory::NewCode(const CodeDesc& desc, ScopeInfo<>* sinfo,
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000511 Code::Flags flags, Handle<Object> self_ref) {
kasperl@chromium.org061ef742009-02-27 12:16:20 +0000512 CALL_HEAP_FUNCTION(Heap::CreateCode(desc, sinfo, flags, self_ref), Code);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000513}
514
515
516Handle<Code> Factory::CopyCode(Handle<Code> code) {
517 CALL_HEAP_FUNCTION(Heap::CopyCode(*code), Code);
518}
519
520
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000521static inline Object* DoCopyInsert(DescriptorArray* array,
522 String* key,
523 Object* value,
524 PropertyAttributes attributes) {
525 CallbacksDescriptor desc(key, value, attributes);
526 Object* obj = array->CopyInsert(&desc, REMOVE_TRANSITIONS);
527 return obj;
528}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000529
530
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000531// Allocate the new array.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000532Handle<DescriptorArray> Factory::CopyAppendProxyDescriptor(
533 Handle<DescriptorArray> array,
534 Handle<String> key,
535 Handle<Object> value,
536 PropertyAttributes attributes) {
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000537 CALL_HEAP_FUNCTION(DoCopyInsert(*array, *key, *value, attributes),
538 DescriptorArray);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000539}
540
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000541
542Handle<String> Factory::SymbolFromString(Handle<String> value) {
543 CALL_HEAP_FUNCTION(Heap::LookupSymbol(*value), String);
544}
545
546
547Handle<DescriptorArray> Factory::CopyAppendCallbackDescriptors(
548 Handle<DescriptorArray> array,
549 Handle<Object> descriptors) {
550 v8::NeanderArray callbacks(descriptors);
551 int nof_callbacks = callbacks.length();
552 Handle<DescriptorArray> result =
553 NewDescriptorArray(array->number_of_descriptors() + nof_callbacks);
554
555 // Number of descriptors added to the result so far.
556 int descriptor_count = 0;
557
558 // Copy the descriptors from the array.
559 DescriptorWriter w(*result);
560 for (DescriptorReader r(*array); !r.eos(); r.advance()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000561 if (!r.IsNullDescriptor()) {
562 w.WriteFrom(&r);
563 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000564 descriptor_count++;
565 }
566
567 // Number of duplicates detected.
568 int duplicates = 0;
569
570 // Fill in new callback descriptors. Process the callbacks from
571 // back to front so that the last callback with a given name takes
572 // precedence over previously added callbacks with that name.
573 for (int i = nof_callbacks - 1; i >= 0; i--) {
574 Handle<AccessorInfo> entry =
575 Handle<AccessorInfo>(AccessorInfo::cast(callbacks.get(i)));
576 // Ensure the key is a symbol before writing into the instance descriptor.
577 Handle<String> key =
578 SymbolFromString(Handle<String>(String::cast(entry->name())));
579 // Check if a descriptor with this name already exists before writing.
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000580 if (result->LinearSearch(*key, descriptor_count) ==
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000581 DescriptorArray::kNotFound) {
582 CallbacksDescriptor desc(*key, *entry, entry->property_attributes());
583 w.Write(&desc);
584 descriptor_count++;
585 } else {
586 duplicates++;
587 }
588 }
589
590 // If duplicates were detected, allocate a result of the right size
591 // and transfer the elements.
592 if (duplicates > 0) {
593 Handle<DescriptorArray> new_result =
594 NewDescriptorArray(result->number_of_descriptors() - duplicates);
595 DescriptorWriter w(*new_result);
596 DescriptorReader r(*result);
597 while (!w.eos()) {
598 w.WriteFrom(&r);
599 r.advance();
600 }
601 result = new_result;
602 }
603
604 // Sort the result before returning.
605 result->Sort();
606 return result;
607}
608
609
610Handle<JSObject> Factory::NewJSObject(Handle<JSFunction> constructor,
611 PretenureFlag pretenure) {
612 CALL_HEAP_FUNCTION(Heap::AllocateJSObject(*constructor, pretenure), JSObject);
613}
614
615
ager@chromium.org236ad962008-09-25 09:45:57 +0000616Handle<JSObject> Factory::NewJSObjectFromMap(Handle<Map> map) {
617 CALL_HEAP_FUNCTION(Heap::AllocateJSObjectFromMap(*map, NOT_TENURED),
618 JSObject);
619}
620
621
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000622Handle<JSArray> Factory::NewJSArray(int length,
623 PretenureFlag pretenure) {
624 Handle<JSObject> obj = NewJSObject(Top::array_function(), pretenure);
625 CALL_HEAP_FUNCTION(Handle<JSArray>::cast(obj)->Initialize(length), JSArray);
626}
627
628
629Handle<JSArray> Factory::NewJSArrayWithElements(Handle<FixedArray> elements,
630 PretenureFlag pretenure) {
631 Handle<JSArray> result =
632 Handle<JSArray>::cast(NewJSObject(Top::array_function(), pretenure));
633 result->SetContent(*elements);
634 return result;
635}
636
637
638Handle<SharedFunctionInfo> Factory::NewSharedFunctionInfo(Handle<String> name) {
639 CALL_HEAP_FUNCTION(Heap::AllocateSharedFunctionInfo(*name),
640 SharedFunctionInfo);
641}
642
643
644Handle<Dictionary> Factory::DictionaryAtNumberPut(Handle<Dictionary> dictionary,
645 uint32_t key,
646 Handle<Object> value) {
647 CALL_HEAP_FUNCTION(dictionary->AtNumberPut(key, *value), Dictionary);
648}
649
650
651Handle<JSFunction> Factory::NewFunctionHelper(Handle<String> name,
652 Handle<Object> prototype) {
653 Handle<SharedFunctionInfo> function_share = NewSharedFunctionInfo(name);
654 CALL_HEAP_FUNCTION(Heap::AllocateFunction(*Top::function_map(),
655 *function_share,
656 *prototype),
657 JSFunction);
658}
659
660
661Handle<JSFunction> Factory::NewFunction(Handle<String> name,
662 Handle<Object> prototype) {
663 Handle<JSFunction> fun = NewFunctionHelper(name, prototype);
664 fun->set_context(Top::context()->global_context());
665 return fun;
666}
667
668
669Handle<Object> Factory::ToObject(Handle<Object> object,
670 Handle<Context> global_context) {
671 CALL_HEAP_FUNCTION(object->ToObject(*global_context), Object);
672}
673
674
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000675#ifdef ENABLE_DEBUGGER_SUPPORT
v8.team.kasperl727e9952008-09-02 14:56:44 +0000676Handle<DebugInfo> Factory::NewDebugInfo(Handle<SharedFunctionInfo> shared) {
677 // Get the original code of the function.
678 Handle<Code> code(shared->code());
679
680 // Create a copy of the code before allocating the debug info object to avoid
681 // allocation while setting up the debug info object.
682 Handle<Code> original_code(*Factory::CopyCode(code));
683
684 // Allocate initial fixed array for active break points before allocating the
685 // debug info object to avoid allocation while setting up the debug info
686 // object.
687 Handle<FixedArray> break_points(
688 Factory::NewFixedArray(Debug::kEstimatedNofBreakPointsInFunction));
689
690 // Create and set up the debug info object. Debug info contains function, a
691 // copy of the original code, the executing code and initial fixed array for
692 // active break points.
693 Handle<DebugInfo> debug_info =
694 Handle<DebugInfo>::cast(Factory::NewStruct(DEBUG_INFO_TYPE));
695 debug_info->set_shared(*shared);
696 debug_info->set_original_code(*original_code);
697 debug_info->set_code(*code);
698 debug_info->set_break_points(*break_points);
699
700 // Link debug info to function.
701 shared->set_debug_info(*debug_info);
702
703 return debug_info;
704}
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000705#endif
v8.team.kasperl727e9952008-09-02 14:56:44 +0000706
707
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000708Handle<JSObject> Factory::NewArgumentsObject(Handle<Object> callee,
709 int length) {
710 CALL_HEAP_FUNCTION(Heap::AllocateArgumentsObject(*callee, length), JSObject);
711}
712
713
714Handle<JSFunction> Factory::CreateApiFunction(
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000715 Handle<FunctionTemplateInfo> obj, ApiInstanceType instance_type) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000716 Handle<Code> code = Handle<Code>(Builtins::builtin(Builtins::HandleApiCall));
717
kasper.lund212ac232008-07-16 07:07:30 +0000718 int internal_field_count = 0;
719 if (!obj->instance_template()->IsUndefined()) {
720 Handle<ObjectTemplateInfo> instance_template =
721 Handle<ObjectTemplateInfo>(
722 ObjectTemplateInfo::cast(obj->instance_template()));
723 internal_field_count =
724 Smi::cast(instance_template->internal_field_count())->value();
725 }
726
727 int instance_size = kPointerSize * internal_field_count;
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000728 InstanceType type = INVALID_TYPE;
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000729 switch (instance_type) {
730 case JavaScriptObject:
731 type = JS_OBJECT_TYPE;
732 instance_size += JSObject::kHeaderSize;
733 break;
734 case InnerGlobalObject:
735 type = JS_GLOBAL_OBJECT_TYPE;
736 instance_size += JSGlobalObject::kSize;
737 break;
738 case OuterGlobalObject:
739 type = JS_GLOBAL_PROXY_TYPE;
740 instance_size += JSGlobalProxy::kSize;
741 break;
742 default:
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000743 break;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000744 }
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000745 ASSERT(type != INVALID_TYPE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000746
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000747 Handle<JSFunction> result =
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000748 Factory::NewFunction(Factory::empty_symbol(),
749 type,
750 instance_size,
751 code,
752 true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000753 // Set class name.
754 Handle<Object> class_name = Handle<Object>(obj->class_name());
755 if (class_name->IsString()) {
756 result->shared()->set_instance_class_name(*class_name);
757 result->shared()->set_name(*class_name);
758 }
759
760 Handle<Map> map = Handle<Map>(result->initial_map());
761
762 // Mark as undetectable if needed.
763 if (obj->undetectable()) {
764 map->set_is_undetectable();
765 }
766
767 // Mark as hidden for the __proto__ accessor if needed.
768 if (obj->hidden_prototype()) {
769 map->set_is_hidden_prototype();
770 }
771
772 // Mark as needs_access_check if needed.
773 if (obj->needs_access_check()) {
ager@chromium.org870a0b62008-11-04 11:43:05 +0000774 map->set_is_access_check_needed(true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000775 }
776
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000777 // Set interceptor information in the map.
778 if (!obj->named_property_handler()->IsUndefined()) {
779 map->set_has_named_interceptor();
780 }
781 if (!obj->indexed_property_handler()->IsUndefined()) {
782 map->set_has_indexed_interceptor();
783 }
784
785 // Set instance call-as-function information in the map.
786 if (!obj->instance_call_handler()->IsUndefined()) {
787 map->set_has_instance_call_handler();
788 }
789
790 result->shared()->set_function_data(*obj);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000791 result->shared()->DontAdaptArguments();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000792
793 // Recursively copy parent templates' accessors, 'data' may be modified.
794 Handle<DescriptorArray> array =
795 Handle<DescriptorArray>(map->instance_descriptors());
796 while (true) {
797 Handle<Object> props = Handle<Object>(obj->property_accessors());
798 if (!props->IsUndefined()) {
799 array = Factory::CopyAppendCallbackDescriptors(array, props);
800 }
801 Handle<Object> parent = Handle<Object>(obj->parent_template());
802 if (parent->IsUndefined()) break;
803 obj = Handle<FunctionTemplateInfo>::cast(parent);
804 }
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000805 if (!array->IsEmpty()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000806 map->set_instance_descriptors(*array);
807 }
808
809 return result;
810}
811
812
ager@chromium.org236ad962008-09-25 09:45:57 +0000813Handle<MapCache> Factory::NewMapCache(int at_least_space_for) {
814 CALL_HEAP_FUNCTION(MapCache::Allocate(at_least_space_for), MapCache);
815}
816
817
818static Object* UpdateMapCacheWith(Context* context,
819 FixedArray* keys,
820 Map* map) {
821 Object* result = MapCache::cast(context->map_cache())->Put(keys, map);
822 if (!result->IsFailure()) context->set_map_cache(MapCache::cast(result));
823 return result;
824}
825
826
827Handle<MapCache> Factory::AddToMapCache(Handle<Context> context,
828 Handle<FixedArray> keys,
829 Handle<Map> map) {
830 CALL_HEAP_FUNCTION(UpdateMapCacheWith(*context, *keys, *map), MapCache);
831}
832
833
834Handle<Map> Factory::ObjectLiteralMapFromCache(Handle<Context> context,
835 Handle<FixedArray> keys) {
836 if (context->map_cache()->IsUndefined()) {
837 // Allocate the new map cache for the global context.
838 Handle<MapCache> new_cache = NewMapCache(24);
839 context->set_map_cache(*new_cache);
840 }
ager@chromium.org32912102009-01-16 10:38:43 +0000841 // Check to see whether there is a matching element in the cache.
ager@chromium.org236ad962008-09-25 09:45:57 +0000842 Handle<MapCache> cache =
843 Handle<MapCache>(MapCache::cast(context->map_cache()));
844 Handle<Object> result = Handle<Object>(cache->Lookup(*keys));
845 if (result->IsMap()) return Handle<Map>::cast(result);
846 // Create a new map and add it to the cache.
847 Handle<Map> map =
ager@chromium.org32912102009-01-16 10:38:43 +0000848 CopyMap(Handle<Map>(context->object_function()->initial_map()),
849 keys->length());
ager@chromium.org236ad962008-09-25 09:45:57 +0000850 AddToMapCache(context, keys, map);
851 return Handle<Map>(map);
852}
853
854
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000855void Factory::SetRegExpAtomData(Handle<JSRegExp> regexp,
856 JSRegExp::Type type,
857 Handle<String> source,
858 JSRegExp::Flags flags,
859 Handle<Object> data) {
860 Handle<FixedArray> store = NewFixedArray(JSRegExp::kAtomDataSize);
861
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000862 store->set(JSRegExp::kTagIndex, Smi::FromInt(type));
863 store->set(JSRegExp::kSourceIndex, *source);
864 store->set(JSRegExp::kFlagsIndex, Smi::FromInt(flags.value()));
865 store->set(JSRegExp::kAtomPatternIndex, *data);
866 regexp->set_data(*store);
867}
868
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000869void Factory::SetRegExpIrregexpData(Handle<JSRegExp> regexp,
870 JSRegExp::Type type,
871 Handle<String> source,
872 JSRegExp::Flags flags,
873 int capture_count) {
874 Handle<FixedArray> store = NewFixedArray(JSRegExp::kIrregexpDataSize);
875
876 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::kIrregexpASCIICodeIndex, Heap::the_hole_value());
880 store->set(JSRegExp::kIrregexpUC16CodeIndex, Heap::the_hole_value());
881 store->set(JSRegExp::kIrregexpMaxRegisterCountIndex, Smi::FromInt(0));
882 store->set(JSRegExp::kIrregexpCaptureCountIndex,
883 Smi::FromInt(capture_count));
884 regexp->set_data(*store);
885}
886
887
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000888
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000889void Factory::ConfigureInstance(Handle<FunctionTemplateInfo> desc,
890 Handle<JSObject> instance,
891 bool* pending_exception) {
892 // Configure the instance by adding the properties specified by the
893 // instance template.
894 Handle<Object> instance_template = Handle<Object>(desc->instance_template());
895 if (!instance_template->IsUndefined()) {
896 Execution::ConfigureInstance(instance,
897 instance_template,
898 pending_exception);
899 } else {
900 *pending_exception = false;
901 }
902}
903
904
905} } // namespace v8::internal