blob: 4d2c6b4ceaa8bcddcf796e033e98e507319da528 [file] [log] [blame]
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001// Copyright 2010 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"
kasperl@chromium.orga5551262010-12-07 12:49:48 +000035#include "objects.h"
ager@chromium.orgea4f62e2010-08-16 16:28:43 +000036#include "objects-visiting.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000037
kasperl@chromium.org71affb52009-05-26 05:44:31 +000038namespace v8 {
39namespace internal {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000040
41
42Handle<FixedArray> Factory::NewFixedArray(int size, PretenureFlag pretenure) {
43 ASSERT(0 <= size);
44 CALL_HEAP_FUNCTION(Heap::AllocateFixedArray(size, pretenure), FixedArray);
45}
46
47
ricow@chromium.orgc9c80822010-04-21 08:22:37 +000048Handle<FixedArray> Factory::NewFixedArrayWithHoles(int size,
49 PretenureFlag pretenure) {
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +000050 ASSERT(0 <= size);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +000051 CALL_HEAP_FUNCTION(Heap::AllocateFixedArrayWithHoles(size, pretenure),
52 FixedArray);
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +000053}
54
55
kasperl@chromium.org86f77b72009-07-06 08:21:57 +000056Handle<StringDictionary> Factory::NewStringDictionary(int at_least_space_for) {
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +000057 ASSERT(0 <= at_least_space_for);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +000058 CALL_HEAP_FUNCTION(StringDictionary::Allocate(at_least_space_for),
59 StringDictionary);
60}
61
62
63Handle<NumberDictionary> Factory::NewNumberDictionary(int at_least_space_for) {
64 ASSERT(0 <= at_least_space_for);
65 CALL_HEAP_FUNCTION(NumberDictionary::Allocate(at_least_space_for),
66 NumberDictionary);
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +000067}
68
69
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000070Handle<DescriptorArray> Factory::NewDescriptorArray(int number_of_descriptors) {
71 ASSERT(0 <= number_of_descriptors);
72 CALL_HEAP_FUNCTION(DescriptorArray::Allocate(number_of_descriptors),
73 DescriptorArray);
74}
75
76
kasperl@chromium.orga5551262010-12-07 12:49:48 +000077Handle<DeoptimizationInputData> Factory::NewDeoptimizationInputData(
78 int deopt_entry_count,
79 PretenureFlag pretenure) {
80 ASSERT(deopt_entry_count > 0);
81 CALL_HEAP_FUNCTION(DeoptimizationInputData::Allocate(deopt_entry_count,
82 pretenure),
83 DeoptimizationInputData);
84}
85
86
87Handle<DeoptimizationOutputData> Factory::NewDeoptimizationOutputData(
88 int deopt_entry_count,
89 PretenureFlag pretenure) {
90 ASSERT(deopt_entry_count > 0);
91 CALL_HEAP_FUNCTION(DeoptimizationOutputData::Allocate(deopt_entry_count,
92 pretenure),
93 DeoptimizationOutputData);
94}
95
96
ager@chromium.org9258b6b2008-09-11 09:11:10 +000097// Symbols are created in the old generation (data space).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000098Handle<String> Factory::LookupSymbol(Vector<const char> string) {
99 CALL_HEAP_FUNCTION(Heap::LookupSymbol(string), String);
100}
101
fschneider@chromium.org9e3e0b62011-01-03 10:16:46 +0000102Handle<String> Factory::LookupAsciiSymbol(Vector<const char> string) {
103 CALL_HEAP_FUNCTION(Heap::LookupAsciiSymbol(string), String);
104}
105
106Handle<String> Factory::LookupTwoByteSymbol(Vector<const uc16> string) {
107 CALL_HEAP_FUNCTION(Heap::LookupTwoByteSymbol(string), String);
108}
109
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000110
111Handle<String> Factory::NewStringFromAscii(Vector<const char> string,
112 PretenureFlag pretenure) {
113 CALL_HEAP_FUNCTION(Heap::AllocateStringFromAscii(string, pretenure), String);
114}
115
116Handle<String> Factory::NewStringFromUtf8(Vector<const char> string,
117 PretenureFlag pretenure) {
118 CALL_HEAP_FUNCTION(Heap::AllocateStringFromUtf8(string, pretenure), String);
119}
120
121
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000122Handle<String> Factory::NewStringFromTwoByte(Vector<const uc16> string,
123 PretenureFlag pretenure) {
124 CALL_HEAP_FUNCTION(Heap::AllocateStringFromTwoByte(string, pretenure),
125 String);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000126}
127
128
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +0000129Handle<String> Factory::NewRawAsciiString(int length,
130 PretenureFlag pretenure) {
131 CALL_HEAP_FUNCTION(Heap::AllocateRawAsciiString(length, pretenure), String);
132}
133
134
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000135Handle<String> Factory::NewRawTwoByteString(int length,
136 PretenureFlag pretenure) {
137 CALL_HEAP_FUNCTION(Heap::AllocateRawTwoByteString(length, pretenure), String);
138}
139
140
141Handle<String> Factory::NewConsString(Handle<String> first,
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000142 Handle<String> second) {
ager@chromium.orgc3e50d82008-11-05 11:53:10 +0000143 CALL_HEAP_FUNCTION(Heap::AllocateConsString(*first, *second), String);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000144}
145
146
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000147Handle<String> Factory::NewSubString(Handle<String> str,
148 int begin,
149 int end) {
150 CALL_HEAP_FUNCTION(str->SubString(begin, end), String);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000151}
152
153
154Handle<String> Factory::NewExternalStringFromAscii(
155 ExternalAsciiString::Resource* resource) {
156 CALL_HEAP_FUNCTION(Heap::AllocateExternalStringFromAscii(resource), String);
157}
158
159
160Handle<String> Factory::NewExternalStringFromTwoByte(
161 ExternalTwoByteString::Resource* resource) {
162 CALL_HEAP_FUNCTION(Heap::AllocateExternalStringFromTwoByte(resource), String);
163}
164
165
166Handle<Context> Factory::NewGlobalContext() {
167 CALL_HEAP_FUNCTION(Heap::AllocateGlobalContext(), Context);
168}
169
170
171Handle<Context> Factory::NewFunctionContext(int length,
172 Handle<JSFunction> closure) {
173 CALL_HEAP_FUNCTION(Heap::AllocateFunctionContext(length, *closure), Context);
174}
175
176
177Handle<Context> Factory::NewWithContext(Handle<Context> previous,
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000178 Handle<JSObject> extension,
179 bool is_catch_context) {
180 CALL_HEAP_FUNCTION(Heap::AllocateWithContext(*previous,
181 *extension,
182 is_catch_context),
183 Context);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000184}
185
186
187Handle<Struct> Factory::NewStruct(InstanceType type) {
188 CALL_HEAP_FUNCTION(Heap::AllocateStruct(type), Struct);
189}
190
191
192Handle<AccessorInfo> Factory::NewAccessorInfo() {
193 Handle<AccessorInfo> info =
194 Handle<AccessorInfo>::cast(NewStruct(ACCESSOR_INFO_TYPE));
195 info->set_flag(0); // Must clear the flag, it was initialized as undefined.
196 return info;
197}
198
199
200Handle<Script> Factory::NewScript(Handle<String> source) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000201 // Generate id for this script.
202 int id;
203 if (Heap::last_script_id()->IsUndefined()) {
204 // Script ids start from one.
205 id = 1;
206 } else {
207 // Increment id, wrap when positive smi is exhausted.
208 id = Smi::cast(Heap::last_script_id())->value();
209 id++;
210 if (!Smi::IsValid(id)) {
211 id = 0;
212 }
213 }
214 Heap::SetLastScriptId(Smi::FromInt(id));
215
216 // Create and initialize script object.
ager@chromium.org9085a012009-05-11 19:22:57 +0000217 Handle<Proxy> wrapper = Factory::NewProxy(0, TENURED);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000218 Handle<Script> script = Handle<Script>::cast(NewStruct(SCRIPT_TYPE));
219 script->set_source(*source);
220 script->set_name(Heap::undefined_value());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000221 script->set_id(Heap::last_script_id());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000222 script->set_line_offset(Smi::FromInt(0));
223 script->set_column_offset(Smi::FromInt(0));
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000224 script->set_data(Heap::undefined_value());
ager@chromium.org9085a012009-05-11 19:22:57 +0000225 script->set_context_data(Heap::undefined_value());
ager@chromium.orge2902be2009-06-08 12:21:35 +0000226 script->set_type(Smi::FromInt(Script::TYPE_NORMAL));
227 script->set_compilation_type(Smi::FromInt(Script::COMPILATION_TYPE_HOST));
ager@chromium.org9085a012009-05-11 19:22:57 +0000228 script->set_wrapper(*wrapper);
sgjesse@chromium.org499aaa52009-11-30 08:07:20 +0000229 script->set_line_ends(Heap::undefined_value());
sgjesse@chromium.org98180592009-12-02 08:17:28 +0000230 script->set_eval_from_shared(Heap::undefined_value());
ager@chromium.orge2902be2009-06-08 12:21:35 +0000231 script->set_eval_from_instructions_offset(Smi::FromInt(0));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000232
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000233 return script;
234}
235
236
237Handle<Proxy> Factory::NewProxy(Address addr, PretenureFlag pretenure) {
238 CALL_HEAP_FUNCTION(Heap::AllocateProxy(addr, pretenure), Proxy);
239}
240
241
242Handle<Proxy> Factory::NewProxy(const AccessorDescriptor* desc) {
243 return NewProxy((Address) desc, TENURED);
244}
245
246
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000247Handle<ByteArray> Factory::NewByteArray(int length, PretenureFlag pretenure) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000248 ASSERT(0 <= length);
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000249 CALL_HEAP_FUNCTION(Heap::AllocateByteArray(length, pretenure), ByteArray);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000250}
251
252
ager@chromium.org3811b432009-10-28 14:53:37 +0000253Handle<ExternalArray> Factory::NewExternalArray(int length,
254 ExternalArrayType array_type,
255 void* external_pointer,
256 PretenureFlag pretenure) {
257 ASSERT(0 <= length);
258 CALL_HEAP_FUNCTION(Heap::AllocateExternalArray(length,
259 array_type,
260 external_pointer,
261 pretenure), ExternalArray);
262}
263
264
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000265Handle<JSGlobalPropertyCell> Factory::NewJSGlobalPropertyCell(
266 Handle<Object> value) {
267 CALL_HEAP_FUNCTION(Heap::AllocateJSGlobalPropertyCell(*value),
268 JSGlobalPropertyCell);
269}
270
271
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000272Handle<Map> Factory::NewMap(InstanceType type, int instance_size) {
273 CALL_HEAP_FUNCTION(Heap::AllocateMap(type, instance_size), Map);
274}
275
276
277Handle<JSObject> Factory::NewFunctionPrototype(Handle<JSFunction> function) {
278 CALL_HEAP_FUNCTION(Heap::AllocateFunctionPrototype(*function), JSObject);
279}
280
281
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000282Handle<Map> Factory::CopyMapDropDescriptors(Handle<Map> src) {
283 CALL_HEAP_FUNCTION(src->CopyDropDescriptors(), Map);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000284}
285
286
ager@chromium.org32912102009-01-16 10:38:43 +0000287Handle<Map> Factory::CopyMap(Handle<Map> src,
288 int extra_inobject_properties) {
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000289 Handle<Map> copy = CopyMapDropDescriptors(src);
ager@chromium.org32912102009-01-16 10:38:43 +0000290 // Check that we do not overflow the instance size when adding the
291 // extra inobject properties.
292 int instance_size_delta = extra_inobject_properties * kPointerSize;
293 int max_instance_size_delta =
294 JSObject::kMaxInstanceSize - copy->instance_size();
295 if (instance_size_delta > max_instance_size_delta) {
296 // If the instance size overflows, we allocate as many properties
297 // as we can as inobject properties.
298 instance_size_delta = max_instance_size_delta;
299 extra_inobject_properties = max_instance_size_delta >> kPointerSizeLog2;
300 }
301 // Adjust the map with the extra inobject properties.
302 int inobject_properties =
303 copy->inobject_properties() + extra_inobject_properties;
304 copy->set_inobject_properties(inobject_properties);
305 copy->set_unused_property_fields(inobject_properties);
306 copy->set_instance_size(copy->instance_size() + instance_size_delta);
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000307 copy->set_visitor_id(StaticVisitorBase::GetVisitorId(*copy));
ager@chromium.org32912102009-01-16 10:38:43 +0000308 return copy;
309}
310
fschneider@chromium.org40b9da32010-06-28 11:29:21 +0000311
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000312Handle<Map> Factory::CopyMapDropTransitions(Handle<Map> src) {
313 CALL_HEAP_FUNCTION(src->CopyDropTransitions(), Map);
314}
315
316
fschneider@chromium.org40b9da32010-06-28 11:29:21 +0000317Handle<Map> Factory::GetFastElementsMap(Handle<Map> src) {
318 CALL_HEAP_FUNCTION(src->GetFastElementsMap(), Map);
319}
320
321
322Handle<Map> Factory::GetSlowElementsMap(Handle<Map> src) {
323 CALL_HEAP_FUNCTION(src->GetSlowElementsMap(), Map);
324}
325
326
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +0000327Handle<Map> Factory::NewExternalArrayElementsMap(Handle<Map> src) {
328 CALL_HEAP_FUNCTION(src->NewExternalArrayElementsMap(), Map);
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +0000329}
330
331
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000332Handle<FixedArray> Factory::CopyFixedArray(Handle<FixedArray> array) {
333 CALL_HEAP_FUNCTION(array->Copy(), FixedArray);
334}
335
336
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000337Handle<JSFunction> Factory::BaseNewFunctionFromSharedFunctionInfo(
338 Handle<SharedFunctionInfo> function_info,
sgjesse@chromium.org846fb742009-12-18 08:56:33 +0000339 Handle<Map> function_map,
340 PretenureFlag pretenure) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000341 CALL_HEAP_FUNCTION(Heap::AllocateFunction(*function_map,
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000342 *function_info,
sgjesse@chromium.org846fb742009-12-18 08:56:33 +0000343 Heap::the_hole_value(),
344 pretenure),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000345 JSFunction);
346}
347
348
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000349Handle<JSFunction> Factory::NewFunctionFromSharedFunctionInfo(
350 Handle<SharedFunctionInfo> function_info,
sgjesse@chromium.org846fb742009-12-18 08:56:33 +0000351 Handle<Context> context,
352 PretenureFlag pretenure) {
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000353 Handle<JSFunction> result = BaseNewFunctionFromSharedFunctionInfo(
354 function_info, Top::function_map(), pretenure);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000355 result->set_context(*context);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000356 int number_of_literals = function_info->num_literals();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000357 Handle<FixedArray> literals =
sgjesse@chromium.org846fb742009-12-18 08:56:33 +0000358 Factory::NewFixedArray(number_of_literals, pretenure);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000359 if (number_of_literals > 0) {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000360 // Store the object, regexp and array functions in the literals
361 // array prefix. These functions will be used when creating
362 // object, regexp and array literals in this function.
ager@chromium.org236ad962008-09-25 09:45:57 +0000363 literals->set(JSFunction::kLiteralGlobalContextIndex,
364 context->global_context());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000365 }
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000366 result->set_literals(*literals);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000367 result->set_next_function_link(Heap::undefined_value());
368
369 if (V8::UseCrankshaft() &&
370 FLAG_always_opt &&
371 result->is_compiled() &&
372 !function_info->is_toplevel() &&
373 function_info->allows_lazy_compilation()) {
374 result->MarkForLazyRecompilation();
375 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000376 return result;
377}
378
379
380Handle<Object> Factory::NewNumber(double value,
381 PretenureFlag pretenure) {
382 CALL_HEAP_FUNCTION(Heap::NumberFromDouble(value, pretenure), Object);
383}
384
385
386Handle<Object> Factory::NewNumberFromInt(int value) {
387 CALL_HEAP_FUNCTION(Heap::NumberFromInt32(value), Object);
388}
389
390
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000391Handle<Object> Factory::NewNumberFromUint(uint32_t value) {
392 CALL_HEAP_FUNCTION(Heap::NumberFromUint32(value), Object);
393}
394
395
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000396Handle<JSObject> Factory::NewNeanderObject() {
397 CALL_HEAP_FUNCTION(Heap::AllocateJSObjectFromMap(Heap::neander_map()),
398 JSObject);
399}
400
401
402Handle<Object> Factory::NewTypeError(const char* type,
403 Vector< Handle<Object> > args) {
404 return NewError("MakeTypeError", type, args);
405}
406
407
408Handle<Object> Factory::NewTypeError(Handle<String> message) {
409 return NewError("$TypeError", message);
410}
411
412
413Handle<Object> Factory::NewRangeError(const char* type,
414 Vector< Handle<Object> > args) {
415 return NewError("MakeRangeError", type, args);
416}
417
418
419Handle<Object> Factory::NewRangeError(Handle<String> message) {
420 return NewError("$RangeError", message);
421}
422
423
424Handle<Object> Factory::NewSyntaxError(const char* type, Handle<JSArray> args) {
425 return NewError("MakeSyntaxError", type, args);
426}
427
428
429Handle<Object> Factory::NewSyntaxError(Handle<String> message) {
430 return NewError("$SyntaxError", message);
431}
432
433
434Handle<Object> Factory::NewReferenceError(const char* type,
435 Vector< Handle<Object> > args) {
436 return NewError("MakeReferenceError", type, args);
437}
438
439
440Handle<Object> Factory::NewReferenceError(Handle<String> message) {
441 return NewError("$ReferenceError", message);
442}
443
444
445Handle<Object> Factory::NewError(const char* maker, const char* type,
446 Vector< Handle<Object> > args) {
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000447 v8::HandleScope scope; // Instantiate a closeable HandleScope for EscapeFrom.
ager@chromium.org7c537e22008-10-16 08:43:32 +0000448 Handle<FixedArray> array = Factory::NewFixedArray(args.length());
449 for (int i = 0; i < args.length(); i++) {
450 array->set(i, *args[i]);
451 }
452 Handle<JSArray> object = Factory::NewJSArrayWithElements(array);
453 Handle<Object> result = NewError(maker, type, object);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000454 return result.EscapeFrom(&scope);
455}
456
457
458Handle<Object> Factory::NewEvalError(const char* type,
459 Vector< Handle<Object> > args) {
460 return NewError("MakeEvalError", type, args);
461}
462
463
464Handle<Object> Factory::NewError(const char* type,
465 Vector< Handle<Object> > args) {
466 return NewError("MakeError", type, args);
467}
468
469
470Handle<Object> Factory::NewError(const char* maker,
471 const char* type,
472 Handle<JSArray> args) {
473 Handle<String> make_str = Factory::LookupAsciiSymbol(maker);
lrn@chromium.org303ada72010-10-27 09:33:13 +0000474 Handle<Object> fun_obj(Top::builtins()->GetPropertyNoExceptionThrown(
475 *make_str));
ager@chromium.org4af710e2009-09-15 12:20:11 +0000476 // If the builtins haven't been properly configured yet this error
477 // constructor may not have been defined. Bail out.
478 if (!fun_obj->IsJSFunction())
479 return Factory::undefined_value();
480 Handle<JSFunction> fun = Handle<JSFunction>::cast(fun_obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000481 Handle<Object> type_obj = Factory::LookupAsciiSymbol(type);
482 Object** argv[2] = { type_obj.location(),
483 Handle<Object>::cast(args).location() };
484
485 // Invoke the JavaScript factory method. If an exception is thrown while
486 // running the factory method, use the exception as the result.
487 bool caught_exception;
488 Handle<Object> result = Execution::TryCall(fun,
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000489 Top::builtins(),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000490 2,
491 argv,
492 &caught_exception);
493 return result;
494}
495
496
497Handle<Object> Factory::NewError(Handle<String> message) {
498 return NewError("$Error", message);
499}
500
501
502Handle<Object> Factory::NewError(const char* constructor,
503 Handle<String> message) {
504 Handle<String> constr = Factory::LookupAsciiSymbol(constructor);
505 Handle<JSFunction> fun =
506 Handle<JSFunction>(
507 JSFunction::cast(
lrn@chromium.org303ada72010-10-27 09:33:13 +0000508 Top::builtins()->GetPropertyNoExceptionThrown(*constr)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000509 Object** argv[1] = { Handle<Object>::cast(message).location() };
510
511 // Invoke the JavaScript factory method. If an exception is thrown while
512 // running the factory method, use the exception as the result.
513 bool caught_exception;
514 Handle<Object> result = Execution::TryCall(fun,
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000515 Top::builtins(),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000516 1,
517 argv,
518 &caught_exception);
519 return result;
520}
521
522
523Handle<JSFunction> Factory::NewFunction(Handle<String> name,
524 InstanceType type,
525 int instance_size,
526 Handle<Code> code,
527 bool force_initial_map) {
528 // Allocate the function
529 Handle<JSFunction> function = NewFunction(name, the_hole_value());
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000530
531 // Setup the code pointer in both the shared function info and in
532 // the function itself.
533 function->shared()->set_code(*code);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000534 function->set_code(*code);
535
536 if (force_initial_map ||
537 type != JS_OBJECT_TYPE ||
538 instance_size != JSObject::kHeaderSize) {
539 Handle<Map> initial_map = NewMap(type, instance_size);
540 Handle<JSObject> prototype = NewFunctionPrototype(function);
541 initial_map->set_prototype(*prototype);
542 function->set_initial_map(*initial_map);
543 initial_map->set_constructor(*function);
544 } else {
545 ASSERT(!function->has_initial_map());
546 ASSERT(!function->has_prototype());
547 }
548
549 return function;
550}
551
552
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000553Handle<JSFunction> Factory::NewFunctionWithPrototype(Handle<String> name,
554 InstanceType type,
555 int instance_size,
556 Handle<JSObject> prototype,
557 Handle<Code> code,
558 bool force_initial_map) {
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000559 // Allocate the function.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000560 Handle<JSFunction> function = NewFunction(name, prototype);
561
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000562 // Setup the code pointer in both the shared function info and in
563 // the function itself.
564 function->shared()->set_code(*code);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000565 function->set_code(*code);
566
567 if (force_initial_map ||
568 type != JS_OBJECT_TYPE ||
569 instance_size != JSObject::kHeaderSize) {
570 Handle<Map> initial_map = NewMap(type, instance_size);
571 function->set_initial_map(*initial_map);
572 initial_map->set_constructor(*function);
573 }
574
575 // Set function.prototype and give the prototype a constructor
576 // property that refers to the function.
577 SetPrototypeProperty(function, prototype);
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +0000578 // Currently safe because it is only invoked from Genesis.
579 SetLocalPropertyNoThrow(
580 prototype, Factory::constructor_symbol(), function, DONT_ENUM);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000581 return function;
582}
583
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000584
kmillikin@chromium.org4111b802010-05-03 10:34:42 +0000585Handle<JSFunction> Factory::NewFunctionWithoutPrototype(Handle<String> name,
586 Handle<Code> code) {
587 Handle<JSFunction> function = NewFunctionWithoutPrototype(name);
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000588 function->shared()->set_code(*code);
kmillikin@chromium.org4111b802010-05-03 10:34:42 +0000589 function->set_code(*code);
590 ASSERT(!function->has_initial_map());
591 ASSERT(!function->has_prototype());
592 return function;
593}
594
595
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000596Handle<Code> Factory::NewCode(const CodeDesc& desc,
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000597 Code::Flags flags,
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +0000598 Handle<Object> self_ref,
599 bool immovable) {
600 CALL_HEAP_FUNCTION(Heap::CreateCode(desc, flags, self_ref, immovable), Code);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000601}
602
603
604Handle<Code> Factory::CopyCode(Handle<Code> code) {
605 CALL_HEAP_FUNCTION(Heap::CopyCode(*code), Code);
606}
607
608
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000609Handle<Code> Factory::CopyCode(Handle<Code> code, Vector<byte> reloc_info) {
610 CALL_HEAP_FUNCTION(Heap::CopyCode(*code, reloc_info), Code);
611}
612
613
lrn@chromium.org303ada72010-10-27 09:33:13 +0000614MUST_USE_RESULT static inline MaybeObject* DoCopyInsert(
615 DescriptorArray* array,
616 String* key,
617 Object* value,
618 PropertyAttributes attributes) {
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000619 CallbacksDescriptor desc(key, value, attributes);
lrn@chromium.org303ada72010-10-27 09:33:13 +0000620 MaybeObject* obj = array->CopyInsert(&desc, REMOVE_TRANSITIONS);
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000621 return obj;
622}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000623
624
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000625// Allocate the new array.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000626Handle<DescriptorArray> Factory::CopyAppendProxyDescriptor(
627 Handle<DescriptorArray> array,
628 Handle<String> key,
629 Handle<Object> value,
630 PropertyAttributes attributes) {
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000631 CALL_HEAP_FUNCTION(DoCopyInsert(*array, *key, *value, attributes),
632 DescriptorArray);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000633}
634
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000635
636Handle<String> Factory::SymbolFromString(Handle<String> value) {
637 CALL_HEAP_FUNCTION(Heap::LookupSymbol(*value), String);
638}
639
640
641Handle<DescriptorArray> Factory::CopyAppendCallbackDescriptors(
642 Handle<DescriptorArray> array,
643 Handle<Object> descriptors) {
644 v8::NeanderArray callbacks(descriptors);
645 int nof_callbacks = callbacks.length();
646 Handle<DescriptorArray> result =
647 NewDescriptorArray(array->number_of_descriptors() + nof_callbacks);
648
649 // Number of descriptors added to the result so far.
650 int descriptor_count = 0;
651
652 // Copy the descriptors from the array.
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000653 for (int i = 0; i < array->number_of_descriptors(); i++) {
654 if (array->GetType(i) != NULL_DESCRIPTOR) {
655 result->CopyFrom(descriptor_count++, *array, i);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000656 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000657 }
658
659 // Number of duplicates detected.
660 int duplicates = 0;
661
662 // Fill in new callback descriptors. Process the callbacks from
663 // back to front so that the last callback with a given name takes
664 // precedence over previously added callbacks with that name.
665 for (int i = nof_callbacks - 1; i >= 0; i--) {
666 Handle<AccessorInfo> entry =
667 Handle<AccessorInfo>(AccessorInfo::cast(callbacks.get(i)));
668 // Ensure the key is a symbol before writing into the instance descriptor.
669 Handle<String> key =
670 SymbolFromString(Handle<String>(String::cast(entry->name())));
671 // Check if a descriptor with this name already exists before writing.
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000672 if (result->LinearSearch(*key, descriptor_count) ==
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000673 DescriptorArray::kNotFound) {
674 CallbacksDescriptor desc(*key, *entry, entry->property_attributes());
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000675 result->Set(descriptor_count, &desc);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000676 descriptor_count++;
677 } else {
678 duplicates++;
679 }
680 }
681
682 // If duplicates were detected, allocate a result of the right size
683 // and transfer the elements.
684 if (duplicates > 0) {
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000685 int number_of_descriptors = result->number_of_descriptors() - duplicates;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000686 Handle<DescriptorArray> new_result =
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000687 NewDescriptorArray(number_of_descriptors);
688 for (int i = 0; i < number_of_descriptors; i++) {
689 new_result->CopyFrom(i, *result, i);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000690 }
691 result = new_result;
692 }
693
694 // Sort the result before returning.
695 result->Sort();
696 return result;
697}
698
699
700Handle<JSObject> Factory::NewJSObject(Handle<JSFunction> constructor,
701 PretenureFlag pretenure) {
702 CALL_HEAP_FUNCTION(Heap::AllocateJSObject(*constructor, pretenure), JSObject);
703}
704
705
kasperl@chromium.org2abc4502009-07-02 07:00:29 +0000706Handle<GlobalObject> Factory::NewGlobalObject(
707 Handle<JSFunction> constructor) {
708 CALL_HEAP_FUNCTION(Heap::AllocateGlobalObject(*constructor),
709 GlobalObject);
710}
711
712
713
ager@chromium.org236ad962008-09-25 09:45:57 +0000714Handle<JSObject> Factory::NewJSObjectFromMap(Handle<Map> map) {
715 CALL_HEAP_FUNCTION(Heap::AllocateJSObjectFromMap(*map, NOT_TENURED),
716 JSObject);
717}
718
719
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000720Handle<JSArray> Factory::NewJSArray(int length,
721 PretenureFlag pretenure) {
722 Handle<JSObject> obj = NewJSObject(Top::array_function(), pretenure);
723 CALL_HEAP_FUNCTION(Handle<JSArray>::cast(obj)->Initialize(length), JSArray);
724}
725
726
727Handle<JSArray> Factory::NewJSArrayWithElements(Handle<FixedArray> elements,
728 PretenureFlag pretenure) {
729 Handle<JSArray> result =
730 Handle<JSArray>::cast(NewJSObject(Top::array_function(), pretenure));
731 result->SetContent(*elements);
732 return result;
733}
734
735
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000736Handle<SharedFunctionInfo> Factory::NewSharedFunctionInfo(
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +0000737 Handle<String> name,
738 int number_of_literals,
739 Handle<Code> code,
ager@chromium.orgb5737492010-07-15 09:29:43 +0000740 Handle<SerializedScopeInfo> scope_info) {
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000741 Handle<SharedFunctionInfo> shared = NewSharedFunctionInfo(name);
742 shared->set_code(*code);
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +0000743 shared->set_scope_info(*scope_info);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000744 int literals_array_size = number_of_literals;
745 // If the function contains object, regexp or array literals,
746 // allocate extra space for a literals array prefix containing the
747 // context.
748 if (number_of_literals > 0) {
749 literals_array_size += JSFunction::kLiteralsPrefixSize;
750 }
751 shared->set_num_literals(literals_array_size);
752 return shared;
753}
754
755
kmillikin@chromium.org31b12772011-02-02 16:08:26 +0000756Handle<JSMessageObject> Factory::NewJSMessageObject(
757 Handle<String> type,
758 Handle<JSArray> arguments,
759 int start_position,
760 int end_position,
761 Handle<Object> script,
762 Handle<Object> stack_trace,
763 Handle<Object> stack_frames) {
764 CALL_HEAP_FUNCTION(Heap::AllocateJSMessageObject(*type,
765 *arguments,
766 start_position,
767 end_position,
768 *script,
769 *stack_trace,
770 *stack_frames),
771 JSMessageObject);
772}
773
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000774Handle<SharedFunctionInfo> Factory::NewSharedFunctionInfo(Handle<String> name) {
775 CALL_HEAP_FUNCTION(Heap::AllocateSharedFunctionInfo(*name),
776 SharedFunctionInfo);
777}
778
779
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000780Handle<String> Factory::NumberToString(Handle<Object> number) {
781 CALL_HEAP_FUNCTION(Heap::NumberToString(*number), String);
782}
783
784
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000785Handle<NumberDictionary> Factory::DictionaryAtNumberPut(
786 Handle<NumberDictionary> dictionary,
787 uint32_t key,
788 Handle<Object> value) {
789 CALL_HEAP_FUNCTION(dictionary->AtNumberPut(key, *value), NumberDictionary);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000790}
791
792
793Handle<JSFunction> Factory::NewFunctionHelper(Handle<String> name,
794 Handle<Object> prototype) {
795 Handle<SharedFunctionInfo> function_share = NewSharedFunctionInfo(name);
796 CALL_HEAP_FUNCTION(Heap::AllocateFunction(*Top::function_map(),
797 *function_share,
798 *prototype),
799 JSFunction);
800}
801
802
803Handle<JSFunction> Factory::NewFunction(Handle<String> name,
804 Handle<Object> prototype) {
805 Handle<JSFunction> fun = NewFunctionHelper(name, prototype);
806 fun->set_context(Top::context()->global_context());
807 return fun;
808}
809
810
kmillikin@chromium.org4111b802010-05-03 10:34:42 +0000811Handle<JSFunction> Factory::NewFunctionWithoutPrototypeHelper(
812 Handle<String> name) {
813 Handle<SharedFunctionInfo> function_share = NewSharedFunctionInfo(name);
814 CALL_HEAP_FUNCTION(Heap::AllocateFunction(
815 *Top::function_without_prototype_map(),
816 *function_share,
817 *the_hole_value()),
818 JSFunction);
819}
820
821
822Handle<JSFunction> Factory::NewFunctionWithoutPrototype(Handle<String> name) {
823 Handle<JSFunction> fun = NewFunctionWithoutPrototypeHelper(name);
824 fun->set_context(Top::context()->global_context());
825 return fun;
826}
827
828
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000829Handle<Object> Factory::ToObject(Handle<Object> object) {
830 CALL_HEAP_FUNCTION(object->ToObject(), Object);
831}
832
833
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000834Handle<Object> Factory::ToObject(Handle<Object> object,
835 Handle<Context> global_context) {
836 CALL_HEAP_FUNCTION(object->ToObject(*global_context), Object);
837}
838
839
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000840#ifdef ENABLE_DEBUGGER_SUPPORT
v8.team.kasperl727e9952008-09-02 14:56:44 +0000841Handle<DebugInfo> Factory::NewDebugInfo(Handle<SharedFunctionInfo> shared) {
842 // Get the original code of the function.
843 Handle<Code> code(shared->code());
844
845 // Create a copy of the code before allocating the debug info object to avoid
846 // allocation while setting up the debug info object.
847 Handle<Code> original_code(*Factory::CopyCode(code));
848
849 // Allocate initial fixed array for active break points before allocating the
850 // debug info object to avoid allocation while setting up the debug info
851 // object.
852 Handle<FixedArray> break_points(
853 Factory::NewFixedArray(Debug::kEstimatedNofBreakPointsInFunction));
854
855 // Create and set up the debug info object. Debug info contains function, a
856 // copy of the original code, the executing code and initial fixed array for
857 // active break points.
858 Handle<DebugInfo> debug_info =
859 Handle<DebugInfo>::cast(Factory::NewStruct(DEBUG_INFO_TYPE));
860 debug_info->set_shared(*shared);
861 debug_info->set_original_code(*original_code);
862 debug_info->set_code(*code);
863 debug_info->set_break_points(*break_points);
864
865 // Link debug info to function.
866 shared->set_debug_info(*debug_info);
867
868 return debug_info;
869}
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000870#endif
v8.team.kasperl727e9952008-09-02 14:56:44 +0000871
872
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000873Handle<JSObject> Factory::NewArgumentsObject(Handle<Object> callee,
874 int length) {
875 CALL_HEAP_FUNCTION(Heap::AllocateArgumentsObject(*callee, length), JSObject);
876}
877
878
879Handle<JSFunction> Factory::CreateApiFunction(
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000880 Handle<FunctionTemplateInfo> obj, ApiInstanceType instance_type) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000881 Handle<Code> code = Handle<Code>(Builtins::builtin(Builtins::HandleApiCall));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000882 Handle<Code> construct_stub =
883 Handle<Code>(Builtins::builtin(Builtins::JSConstructStubApi));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000884
kasper.lund212ac232008-07-16 07:07:30 +0000885 int internal_field_count = 0;
886 if (!obj->instance_template()->IsUndefined()) {
887 Handle<ObjectTemplateInfo> instance_template =
888 Handle<ObjectTemplateInfo>(
889 ObjectTemplateInfo::cast(obj->instance_template()));
890 internal_field_count =
891 Smi::cast(instance_template->internal_field_count())->value();
892 }
893
894 int instance_size = kPointerSize * internal_field_count;
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000895 InstanceType type = INVALID_TYPE;
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000896 switch (instance_type) {
897 case JavaScriptObject:
898 type = JS_OBJECT_TYPE;
899 instance_size += JSObject::kHeaderSize;
900 break;
901 case InnerGlobalObject:
902 type = JS_GLOBAL_OBJECT_TYPE;
903 instance_size += JSGlobalObject::kSize;
904 break;
905 case OuterGlobalObject:
906 type = JS_GLOBAL_PROXY_TYPE;
907 instance_size += JSGlobalProxy::kSize;
908 break;
909 default:
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000910 break;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000911 }
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000912 ASSERT(type != INVALID_TYPE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000913
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000914 Handle<JSFunction> result =
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000915 Factory::NewFunction(Factory::empty_symbol(),
916 type,
917 instance_size,
918 code,
919 true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000920 // Set class name.
921 Handle<Object> class_name = Handle<Object>(obj->class_name());
922 if (class_name->IsString()) {
923 result->shared()->set_instance_class_name(*class_name);
924 result->shared()->set_name(*class_name);
925 }
926
927 Handle<Map> map = Handle<Map>(result->initial_map());
928
929 // Mark as undetectable if needed.
930 if (obj->undetectable()) {
931 map->set_is_undetectable();
932 }
933
934 // Mark as hidden for the __proto__ accessor if needed.
935 if (obj->hidden_prototype()) {
936 map->set_is_hidden_prototype();
937 }
938
939 // Mark as needs_access_check if needed.
940 if (obj->needs_access_check()) {
ager@chromium.org870a0b62008-11-04 11:43:05 +0000941 map->set_is_access_check_needed(true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000942 }
943
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000944 // Set interceptor information in the map.
945 if (!obj->named_property_handler()->IsUndefined()) {
946 map->set_has_named_interceptor();
947 }
948 if (!obj->indexed_property_handler()->IsUndefined()) {
949 map->set_has_indexed_interceptor();
950 }
951
952 // Set instance call-as-function information in the map.
953 if (!obj->instance_call_handler()->IsUndefined()) {
954 map->set_has_instance_call_handler();
955 }
956
957 result->shared()->set_function_data(*obj);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000958 result->shared()->set_construct_stub(*construct_stub);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000959 result->shared()->DontAdaptArguments();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000960
961 // Recursively copy parent templates' accessors, 'data' may be modified.
962 Handle<DescriptorArray> array =
963 Handle<DescriptorArray>(map->instance_descriptors());
964 while (true) {
965 Handle<Object> props = Handle<Object>(obj->property_accessors());
966 if (!props->IsUndefined()) {
967 array = Factory::CopyAppendCallbackDescriptors(array, props);
968 }
969 Handle<Object> parent = Handle<Object>(obj->parent_template());
970 if (parent->IsUndefined()) break;
971 obj = Handle<FunctionTemplateInfo>::cast(parent);
972 }
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000973 if (!array->IsEmpty()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000974 map->set_instance_descriptors(*array);
975 }
976
vegorov@chromium.orgf8372902010-03-15 10:26:20 +0000977 ASSERT(result->shared()->IsApiFunction());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000978 return result;
979}
980
981
ager@chromium.org236ad962008-09-25 09:45:57 +0000982Handle<MapCache> Factory::NewMapCache(int at_least_space_for) {
983 CALL_HEAP_FUNCTION(MapCache::Allocate(at_least_space_for), MapCache);
984}
985
986
lrn@chromium.org303ada72010-10-27 09:33:13 +0000987MUST_USE_RESULT static MaybeObject* UpdateMapCacheWith(Context* context,
988 FixedArray* keys,
989 Map* map) {
990 Object* result;
991 { MaybeObject* maybe_result =
992 MapCache::cast(context->map_cache())->Put(keys, map);
993 if (!maybe_result->ToObject(&result)) return maybe_result;
994 }
995 context->set_map_cache(MapCache::cast(result));
ager@chromium.org236ad962008-09-25 09:45:57 +0000996 return result;
997}
998
999
1000Handle<MapCache> Factory::AddToMapCache(Handle<Context> context,
1001 Handle<FixedArray> keys,
1002 Handle<Map> map) {
1003 CALL_HEAP_FUNCTION(UpdateMapCacheWith(*context, *keys, *map), MapCache);
1004}
1005
1006
1007Handle<Map> Factory::ObjectLiteralMapFromCache(Handle<Context> context,
1008 Handle<FixedArray> keys) {
1009 if (context->map_cache()->IsUndefined()) {
1010 // Allocate the new map cache for the global context.
1011 Handle<MapCache> new_cache = NewMapCache(24);
1012 context->set_map_cache(*new_cache);
1013 }
ager@chromium.org32912102009-01-16 10:38:43 +00001014 // Check to see whether there is a matching element in the cache.
ager@chromium.org236ad962008-09-25 09:45:57 +00001015 Handle<MapCache> cache =
1016 Handle<MapCache>(MapCache::cast(context->map_cache()));
1017 Handle<Object> result = Handle<Object>(cache->Lookup(*keys));
1018 if (result->IsMap()) return Handle<Map>::cast(result);
1019 // Create a new map and add it to the cache.
1020 Handle<Map> map =
ager@chromium.org32912102009-01-16 10:38:43 +00001021 CopyMap(Handle<Map>(context->object_function()->initial_map()),
1022 keys->length());
ager@chromium.org236ad962008-09-25 09:45:57 +00001023 AddToMapCache(context, keys, map);
1024 return Handle<Map>(map);
1025}
1026
1027
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001028void Factory::SetRegExpAtomData(Handle<JSRegExp> regexp,
1029 JSRegExp::Type type,
1030 Handle<String> source,
1031 JSRegExp::Flags flags,
1032 Handle<Object> data) {
1033 Handle<FixedArray> store = NewFixedArray(JSRegExp::kAtomDataSize);
1034
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001035 store->set(JSRegExp::kTagIndex, Smi::FromInt(type));
1036 store->set(JSRegExp::kSourceIndex, *source);
1037 store->set(JSRegExp::kFlagsIndex, Smi::FromInt(flags.value()));
1038 store->set(JSRegExp::kAtomPatternIndex, *data);
1039 regexp->set_data(*store);
1040}
1041
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001042void Factory::SetRegExpIrregexpData(Handle<JSRegExp> regexp,
1043 JSRegExp::Type type,
1044 Handle<String> source,
1045 JSRegExp::Flags flags,
1046 int capture_count) {
1047 Handle<FixedArray> store = NewFixedArray(JSRegExp::kIrregexpDataSize);
1048
1049 store->set(JSRegExp::kTagIndex, Smi::FromInt(type));
1050 store->set(JSRegExp::kSourceIndex, *source);
1051 store->set(JSRegExp::kFlagsIndex, Smi::FromInt(flags.value()));
1052 store->set(JSRegExp::kIrregexpASCIICodeIndex, Heap::the_hole_value());
1053 store->set(JSRegExp::kIrregexpUC16CodeIndex, Heap::the_hole_value());
1054 store->set(JSRegExp::kIrregexpMaxRegisterCountIndex, Smi::FromInt(0));
1055 store->set(JSRegExp::kIrregexpCaptureCountIndex,
1056 Smi::FromInt(capture_count));
1057 regexp->set_data(*store);
1058}
1059
1060
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001061
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001062void Factory::ConfigureInstance(Handle<FunctionTemplateInfo> desc,
1063 Handle<JSObject> instance,
1064 bool* pending_exception) {
1065 // Configure the instance by adding the properties specified by the
1066 // instance template.
1067 Handle<Object> instance_template = Handle<Object>(desc->instance_template());
1068 if (!instance_template->IsUndefined()) {
1069 Execution::ConfigureInstance(instance,
1070 instance_template,
1071 pending_exception);
1072 } else {
1073 *pending_exception = false;
1074 }
1075}
1076
1077
1078} } // namespace v8::internal