blob: a3d55921b67e679ad0f14aa39aac4836198a5306 [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
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000253Handle<PixelArray> Factory::NewPixelArray(int length,
254 uint8_t* external_pointer,
255 PretenureFlag pretenure) {
256 ASSERT(0 <= length);
257 CALL_HEAP_FUNCTION(Heap::AllocatePixelArray(length,
258 external_pointer,
259 pretenure), PixelArray);
260}
261
262
ager@chromium.org3811b432009-10-28 14:53:37 +0000263Handle<ExternalArray> Factory::NewExternalArray(int length,
264 ExternalArrayType array_type,
265 void* external_pointer,
266 PretenureFlag pretenure) {
267 ASSERT(0 <= length);
268 CALL_HEAP_FUNCTION(Heap::AllocateExternalArray(length,
269 array_type,
270 external_pointer,
271 pretenure), ExternalArray);
272}
273
274
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000275Handle<JSGlobalPropertyCell> Factory::NewJSGlobalPropertyCell(
276 Handle<Object> value) {
277 CALL_HEAP_FUNCTION(Heap::AllocateJSGlobalPropertyCell(*value),
278 JSGlobalPropertyCell);
279}
280
281
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000282Handle<Map> Factory::NewMap(InstanceType type, int instance_size) {
283 CALL_HEAP_FUNCTION(Heap::AllocateMap(type, instance_size), Map);
284}
285
286
287Handle<JSObject> Factory::NewFunctionPrototype(Handle<JSFunction> function) {
288 CALL_HEAP_FUNCTION(Heap::AllocateFunctionPrototype(*function), JSObject);
289}
290
291
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000292Handle<Map> Factory::CopyMapDropDescriptors(Handle<Map> src) {
293 CALL_HEAP_FUNCTION(src->CopyDropDescriptors(), Map);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000294}
295
296
ager@chromium.org32912102009-01-16 10:38:43 +0000297Handle<Map> Factory::CopyMap(Handle<Map> src,
298 int extra_inobject_properties) {
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000299 Handle<Map> copy = CopyMapDropDescriptors(src);
ager@chromium.org32912102009-01-16 10:38:43 +0000300 // Check that we do not overflow the instance size when adding the
301 // extra inobject properties.
302 int instance_size_delta = extra_inobject_properties * kPointerSize;
303 int max_instance_size_delta =
304 JSObject::kMaxInstanceSize - copy->instance_size();
305 if (instance_size_delta > max_instance_size_delta) {
306 // If the instance size overflows, we allocate as many properties
307 // as we can as inobject properties.
308 instance_size_delta = max_instance_size_delta;
309 extra_inobject_properties = max_instance_size_delta >> kPointerSizeLog2;
310 }
311 // Adjust the map with the extra inobject properties.
312 int inobject_properties =
313 copy->inobject_properties() + extra_inobject_properties;
314 copy->set_inobject_properties(inobject_properties);
315 copy->set_unused_property_fields(inobject_properties);
316 copy->set_instance_size(copy->instance_size() + instance_size_delta);
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000317 copy->set_visitor_id(StaticVisitorBase::GetVisitorId(*copy));
ager@chromium.org32912102009-01-16 10:38:43 +0000318 return copy;
319}
320
fschneider@chromium.org40b9da32010-06-28 11:29:21 +0000321
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000322Handle<Map> Factory::CopyMapDropTransitions(Handle<Map> src) {
323 CALL_HEAP_FUNCTION(src->CopyDropTransitions(), Map);
324}
325
326
fschneider@chromium.org40b9da32010-06-28 11:29:21 +0000327Handle<Map> Factory::GetFastElementsMap(Handle<Map> src) {
328 CALL_HEAP_FUNCTION(src->GetFastElementsMap(), Map);
329}
330
331
332Handle<Map> Factory::GetSlowElementsMap(Handle<Map> src) {
333 CALL_HEAP_FUNCTION(src->GetSlowElementsMap(), Map);
334}
335
336
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000337Handle<FixedArray> Factory::CopyFixedArray(Handle<FixedArray> array) {
338 CALL_HEAP_FUNCTION(array->Copy(), FixedArray);
339}
340
341
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000342Handle<JSFunction> Factory::BaseNewFunctionFromSharedFunctionInfo(
343 Handle<SharedFunctionInfo> function_info,
sgjesse@chromium.org846fb742009-12-18 08:56:33 +0000344 Handle<Map> function_map,
345 PretenureFlag pretenure) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000346 CALL_HEAP_FUNCTION(Heap::AllocateFunction(*function_map,
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000347 *function_info,
sgjesse@chromium.org846fb742009-12-18 08:56:33 +0000348 Heap::the_hole_value(),
349 pretenure),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000350 JSFunction);
351}
352
353
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000354Handle<JSFunction> Factory::NewFunctionFromSharedFunctionInfo(
355 Handle<SharedFunctionInfo> function_info,
sgjesse@chromium.org846fb742009-12-18 08:56:33 +0000356 Handle<Context> context,
357 PretenureFlag pretenure) {
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000358 Handle<JSFunction> result = BaseNewFunctionFromSharedFunctionInfo(
359 function_info, Top::function_map(), pretenure);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000360 result->set_context(*context);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000361 int number_of_literals = function_info->num_literals();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000362 Handle<FixedArray> literals =
sgjesse@chromium.org846fb742009-12-18 08:56:33 +0000363 Factory::NewFixedArray(number_of_literals, pretenure);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000364 if (number_of_literals > 0) {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000365 // Store the object, regexp and array functions in the literals
366 // array prefix. These functions will be used when creating
367 // object, regexp and array literals in this function.
ager@chromium.org236ad962008-09-25 09:45:57 +0000368 literals->set(JSFunction::kLiteralGlobalContextIndex,
369 context->global_context());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000370 }
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000371 result->set_literals(*literals);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000372 result->set_next_function_link(Heap::undefined_value());
373
374 if (V8::UseCrankshaft() &&
375 FLAG_always_opt &&
376 result->is_compiled() &&
377 !function_info->is_toplevel() &&
378 function_info->allows_lazy_compilation()) {
379 result->MarkForLazyRecompilation();
380 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000381 return result;
382}
383
384
385Handle<Object> Factory::NewNumber(double value,
386 PretenureFlag pretenure) {
387 CALL_HEAP_FUNCTION(Heap::NumberFromDouble(value, pretenure), Object);
388}
389
390
391Handle<Object> Factory::NewNumberFromInt(int value) {
392 CALL_HEAP_FUNCTION(Heap::NumberFromInt32(value), Object);
393}
394
395
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000396Handle<Object> Factory::NewNumberFromUint(uint32_t value) {
397 CALL_HEAP_FUNCTION(Heap::NumberFromUint32(value), Object);
398}
399
400
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000401Handle<JSObject> Factory::NewNeanderObject() {
402 CALL_HEAP_FUNCTION(Heap::AllocateJSObjectFromMap(Heap::neander_map()),
403 JSObject);
404}
405
406
407Handle<Object> Factory::NewTypeError(const char* type,
408 Vector< Handle<Object> > args) {
409 return NewError("MakeTypeError", type, args);
410}
411
412
413Handle<Object> Factory::NewTypeError(Handle<String> message) {
414 return NewError("$TypeError", message);
415}
416
417
418Handle<Object> Factory::NewRangeError(const char* type,
419 Vector< Handle<Object> > args) {
420 return NewError("MakeRangeError", type, args);
421}
422
423
424Handle<Object> Factory::NewRangeError(Handle<String> message) {
425 return NewError("$RangeError", message);
426}
427
428
429Handle<Object> Factory::NewSyntaxError(const char* type, Handle<JSArray> args) {
430 return NewError("MakeSyntaxError", type, args);
431}
432
433
434Handle<Object> Factory::NewSyntaxError(Handle<String> message) {
435 return NewError("$SyntaxError", message);
436}
437
438
439Handle<Object> Factory::NewReferenceError(const char* type,
440 Vector< Handle<Object> > args) {
441 return NewError("MakeReferenceError", type, args);
442}
443
444
445Handle<Object> Factory::NewReferenceError(Handle<String> message) {
446 return NewError("$ReferenceError", message);
447}
448
449
450Handle<Object> Factory::NewError(const char* maker, const char* type,
451 Vector< Handle<Object> > args) {
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000452 v8::HandleScope scope; // Instantiate a closeable HandleScope for EscapeFrom.
ager@chromium.org7c537e22008-10-16 08:43:32 +0000453 Handle<FixedArray> array = Factory::NewFixedArray(args.length());
454 for (int i = 0; i < args.length(); i++) {
455 array->set(i, *args[i]);
456 }
457 Handle<JSArray> object = Factory::NewJSArrayWithElements(array);
458 Handle<Object> result = NewError(maker, type, object);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000459 return result.EscapeFrom(&scope);
460}
461
462
463Handle<Object> Factory::NewEvalError(const char* type,
464 Vector< Handle<Object> > args) {
465 return NewError("MakeEvalError", type, args);
466}
467
468
469Handle<Object> Factory::NewError(const char* type,
470 Vector< Handle<Object> > args) {
471 return NewError("MakeError", type, args);
472}
473
474
475Handle<Object> Factory::NewError(const char* maker,
476 const char* type,
477 Handle<JSArray> args) {
478 Handle<String> make_str = Factory::LookupAsciiSymbol(maker);
lrn@chromium.org303ada72010-10-27 09:33:13 +0000479 Handle<Object> fun_obj(Top::builtins()->GetPropertyNoExceptionThrown(
480 *make_str));
ager@chromium.org4af710e2009-09-15 12:20:11 +0000481 // If the builtins haven't been properly configured yet this error
482 // constructor may not have been defined. Bail out.
483 if (!fun_obj->IsJSFunction())
484 return Factory::undefined_value();
485 Handle<JSFunction> fun = Handle<JSFunction>::cast(fun_obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000486 Handle<Object> type_obj = Factory::LookupAsciiSymbol(type);
487 Object** argv[2] = { type_obj.location(),
488 Handle<Object>::cast(args).location() };
489
490 // Invoke the JavaScript factory method. If an exception is thrown while
491 // running the factory method, use the exception as the result.
492 bool caught_exception;
493 Handle<Object> result = Execution::TryCall(fun,
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000494 Top::builtins(),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000495 2,
496 argv,
497 &caught_exception);
498 return result;
499}
500
501
502Handle<Object> Factory::NewError(Handle<String> message) {
503 return NewError("$Error", message);
504}
505
506
507Handle<Object> Factory::NewError(const char* constructor,
508 Handle<String> message) {
509 Handle<String> constr = Factory::LookupAsciiSymbol(constructor);
510 Handle<JSFunction> fun =
511 Handle<JSFunction>(
512 JSFunction::cast(
lrn@chromium.org303ada72010-10-27 09:33:13 +0000513 Top::builtins()->GetPropertyNoExceptionThrown(*constr)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000514 Object** argv[1] = { Handle<Object>::cast(message).location() };
515
516 // Invoke the JavaScript factory method. If an exception is thrown while
517 // running the factory method, use the exception as the result.
518 bool caught_exception;
519 Handle<Object> result = Execution::TryCall(fun,
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000520 Top::builtins(),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000521 1,
522 argv,
523 &caught_exception);
524 return result;
525}
526
527
528Handle<JSFunction> Factory::NewFunction(Handle<String> name,
529 InstanceType type,
530 int instance_size,
531 Handle<Code> code,
532 bool force_initial_map) {
533 // Allocate the function
534 Handle<JSFunction> function = NewFunction(name, the_hole_value());
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000535
536 // Setup the code pointer in both the shared function info and in
537 // the function itself.
538 function->shared()->set_code(*code);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000539 function->set_code(*code);
540
541 if (force_initial_map ||
542 type != JS_OBJECT_TYPE ||
543 instance_size != JSObject::kHeaderSize) {
544 Handle<Map> initial_map = NewMap(type, instance_size);
545 Handle<JSObject> prototype = NewFunctionPrototype(function);
546 initial_map->set_prototype(*prototype);
547 function->set_initial_map(*initial_map);
548 initial_map->set_constructor(*function);
549 } else {
550 ASSERT(!function->has_initial_map());
551 ASSERT(!function->has_prototype());
552 }
553
554 return function;
555}
556
557
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000558Handle<JSFunction> Factory::NewFunctionWithPrototype(Handle<String> name,
559 InstanceType type,
560 int instance_size,
561 Handle<JSObject> prototype,
562 Handle<Code> code,
563 bool force_initial_map) {
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000564 // Allocate the function.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000565 Handle<JSFunction> function = NewFunction(name, prototype);
566
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000567 // Setup the code pointer in both the shared function info and in
568 // the function itself.
569 function->shared()->set_code(*code);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000570 function->set_code(*code);
571
572 if (force_initial_map ||
573 type != JS_OBJECT_TYPE ||
574 instance_size != JSObject::kHeaderSize) {
575 Handle<Map> initial_map = NewMap(type, instance_size);
576 function->set_initial_map(*initial_map);
577 initial_map->set_constructor(*function);
578 }
579
580 // Set function.prototype and give the prototype a constructor
581 // property that refers to the function.
582 SetPrototypeProperty(function, prototype);
583 SetProperty(prototype, Factory::constructor_symbol(), function, DONT_ENUM);
584 return function;
585}
586
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000587
kmillikin@chromium.org4111b802010-05-03 10:34:42 +0000588Handle<JSFunction> Factory::NewFunctionWithoutPrototype(Handle<String> name,
589 Handle<Code> code) {
590 Handle<JSFunction> function = NewFunctionWithoutPrototype(name);
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000591 function->shared()->set_code(*code);
kmillikin@chromium.org4111b802010-05-03 10:34:42 +0000592 function->set_code(*code);
593 ASSERT(!function->has_initial_map());
594 ASSERT(!function->has_prototype());
595 return function;
596}
597
598
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000599Handle<Code> Factory::NewCode(const CodeDesc& desc,
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000600 Code::Flags flags,
601 Handle<Object> self_ref) {
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +0000602 CALL_HEAP_FUNCTION(Heap::CreateCode(desc, flags, self_ref), Code);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000603}
604
605
606Handle<Code> Factory::CopyCode(Handle<Code> code) {
607 CALL_HEAP_FUNCTION(Heap::CopyCode(*code), Code);
608}
609
610
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000611Handle<Code> Factory::CopyCode(Handle<Code> code, Vector<byte> reloc_info) {
612 CALL_HEAP_FUNCTION(Heap::CopyCode(*code, reloc_info), Code);
613}
614
615
lrn@chromium.org303ada72010-10-27 09:33:13 +0000616MUST_USE_RESULT static inline MaybeObject* DoCopyInsert(
617 DescriptorArray* array,
618 String* key,
619 Object* value,
620 PropertyAttributes attributes) {
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000621 CallbacksDescriptor desc(key, value, attributes);
lrn@chromium.org303ada72010-10-27 09:33:13 +0000622 MaybeObject* obj = array->CopyInsert(&desc, REMOVE_TRANSITIONS);
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000623 return obj;
624}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000625
626
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000627// Allocate the new array.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000628Handle<DescriptorArray> Factory::CopyAppendProxyDescriptor(
629 Handle<DescriptorArray> array,
630 Handle<String> key,
631 Handle<Object> value,
632 PropertyAttributes attributes) {
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000633 CALL_HEAP_FUNCTION(DoCopyInsert(*array, *key, *value, attributes),
634 DescriptorArray);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000635}
636
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000637
638Handle<String> Factory::SymbolFromString(Handle<String> value) {
639 CALL_HEAP_FUNCTION(Heap::LookupSymbol(*value), String);
640}
641
642
643Handle<DescriptorArray> Factory::CopyAppendCallbackDescriptors(
644 Handle<DescriptorArray> array,
645 Handle<Object> descriptors) {
646 v8::NeanderArray callbacks(descriptors);
647 int nof_callbacks = callbacks.length();
648 Handle<DescriptorArray> result =
649 NewDescriptorArray(array->number_of_descriptors() + nof_callbacks);
650
651 // Number of descriptors added to the result so far.
652 int descriptor_count = 0;
653
654 // Copy the descriptors from the array.
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000655 for (int i = 0; i < array->number_of_descriptors(); i++) {
656 if (array->GetType(i) != NULL_DESCRIPTOR) {
657 result->CopyFrom(descriptor_count++, *array, i);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000658 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000659 }
660
661 // Number of duplicates detected.
662 int duplicates = 0;
663
664 // Fill in new callback descriptors. Process the callbacks from
665 // back to front so that the last callback with a given name takes
666 // precedence over previously added callbacks with that name.
667 for (int i = nof_callbacks - 1; i >= 0; i--) {
668 Handle<AccessorInfo> entry =
669 Handle<AccessorInfo>(AccessorInfo::cast(callbacks.get(i)));
670 // Ensure the key is a symbol before writing into the instance descriptor.
671 Handle<String> key =
672 SymbolFromString(Handle<String>(String::cast(entry->name())));
673 // Check if a descriptor with this name already exists before writing.
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000674 if (result->LinearSearch(*key, descriptor_count) ==
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000675 DescriptorArray::kNotFound) {
676 CallbacksDescriptor desc(*key, *entry, entry->property_attributes());
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000677 result->Set(descriptor_count, &desc);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000678 descriptor_count++;
679 } else {
680 duplicates++;
681 }
682 }
683
684 // If duplicates were detected, allocate a result of the right size
685 // and transfer the elements.
686 if (duplicates > 0) {
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000687 int number_of_descriptors = result->number_of_descriptors() - duplicates;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000688 Handle<DescriptorArray> new_result =
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000689 NewDescriptorArray(number_of_descriptors);
690 for (int i = 0; i < number_of_descriptors; i++) {
691 new_result->CopyFrom(i, *result, i);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000692 }
693 result = new_result;
694 }
695
696 // Sort the result before returning.
697 result->Sort();
698 return result;
699}
700
701
702Handle<JSObject> Factory::NewJSObject(Handle<JSFunction> constructor,
703 PretenureFlag pretenure) {
704 CALL_HEAP_FUNCTION(Heap::AllocateJSObject(*constructor, pretenure), JSObject);
705}
706
707
kasperl@chromium.org2abc4502009-07-02 07:00:29 +0000708Handle<GlobalObject> Factory::NewGlobalObject(
709 Handle<JSFunction> constructor) {
710 CALL_HEAP_FUNCTION(Heap::AllocateGlobalObject(*constructor),
711 GlobalObject);
712}
713
714
715
ager@chromium.org236ad962008-09-25 09:45:57 +0000716Handle<JSObject> Factory::NewJSObjectFromMap(Handle<Map> map) {
717 CALL_HEAP_FUNCTION(Heap::AllocateJSObjectFromMap(*map, NOT_TENURED),
718 JSObject);
719}
720
721
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000722Handle<JSArray> Factory::NewJSArray(int length,
723 PretenureFlag pretenure) {
724 Handle<JSObject> obj = NewJSObject(Top::array_function(), pretenure);
725 CALL_HEAP_FUNCTION(Handle<JSArray>::cast(obj)->Initialize(length), JSArray);
726}
727
728
729Handle<JSArray> Factory::NewJSArrayWithElements(Handle<FixedArray> elements,
730 PretenureFlag pretenure) {
731 Handle<JSArray> result =
732 Handle<JSArray>::cast(NewJSObject(Top::array_function(), pretenure));
733 result->SetContent(*elements);
734 return result;
735}
736
737
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000738Handle<SharedFunctionInfo> Factory::NewSharedFunctionInfo(
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +0000739 Handle<String> name,
740 int number_of_literals,
741 Handle<Code> code,
ager@chromium.orgb5737492010-07-15 09:29:43 +0000742 Handle<SerializedScopeInfo> scope_info) {
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000743 Handle<SharedFunctionInfo> shared = NewSharedFunctionInfo(name);
744 shared->set_code(*code);
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +0000745 shared->set_scope_info(*scope_info);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000746 int literals_array_size = number_of_literals;
747 // If the function contains object, regexp or array literals,
748 // allocate extra space for a literals array prefix containing the
749 // context.
750 if (number_of_literals > 0) {
751 literals_array_size += JSFunction::kLiteralsPrefixSize;
752 }
753 shared->set_num_literals(literals_array_size);
754 return shared;
755}
756
757
kmillikin@chromium.org31b12772011-02-02 16:08:26 +0000758Handle<JSMessageObject> Factory::NewJSMessageObject(
759 Handle<String> type,
760 Handle<JSArray> arguments,
761 int start_position,
762 int end_position,
763 Handle<Object> script,
764 Handle<Object> stack_trace,
765 Handle<Object> stack_frames) {
766 CALL_HEAP_FUNCTION(Heap::AllocateJSMessageObject(*type,
767 *arguments,
768 start_position,
769 end_position,
770 *script,
771 *stack_trace,
772 *stack_frames),
773 JSMessageObject);
774}
775
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000776Handle<SharedFunctionInfo> Factory::NewSharedFunctionInfo(Handle<String> name) {
777 CALL_HEAP_FUNCTION(Heap::AllocateSharedFunctionInfo(*name),
778 SharedFunctionInfo);
779}
780
781
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000782Handle<String> Factory::NumberToString(Handle<Object> number) {
783 CALL_HEAP_FUNCTION(Heap::NumberToString(*number), String);
784}
785
786
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000787Handle<NumberDictionary> Factory::DictionaryAtNumberPut(
788 Handle<NumberDictionary> dictionary,
789 uint32_t key,
790 Handle<Object> value) {
791 CALL_HEAP_FUNCTION(dictionary->AtNumberPut(key, *value), NumberDictionary);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000792}
793
794
795Handle<JSFunction> Factory::NewFunctionHelper(Handle<String> name,
796 Handle<Object> prototype) {
797 Handle<SharedFunctionInfo> function_share = NewSharedFunctionInfo(name);
798 CALL_HEAP_FUNCTION(Heap::AllocateFunction(*Top::function_map(),
799 *function_share,
800 *prototype),
801 JSFunction);
802}
803
804
805Handle<JSFunction> Factory::NewFunction(Handle<String> name,
806 Handle<Object> prototype) {
807 Handle<JSFunction> fun = NewFunctionHelper(name, prototype);
808 fun->set_context(Top::context()->global_context());
809 return fun;
810}
811
812
kmillikin@chromium.org4111b802010-05-03 10:34:42 +0000813Handle<JSFunction> Factory::NewFunctionWithoutPrototypeHelper(
814 Handle<String> name) {
815 Handle<SharedFunctionInfo> function_share = NewSharedFunctionInfo(name);
816 CALL_HEAP_FUNCTION(Heap::AllocateFunction(
817 *Top::function_without_prototype_map(),
818 *function_share,
819 *the_hole_value()),
820 JSFunction);
821}
822
823
824Handle<JSFunction> Factory::NewFunctionWithoutPrototype(Handle<String> name) {
825 Handle<JSFunction> fun = NewFunctionWithoutPrototypeHelper(name);
826 fun->set_context(Top::context()->global_context());
827 return fun;
828}
829
830
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000831Handle<Object> Factory::ToObject(Handle<Object> object) {
832 CALL_HEAP_FUNCTION(object->ToObject(), Object);
833}
834
835
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000836Handle<Object> Factory::ToObject(Handle<Object> object,
837 Handle<Context> global_context) {
838 CALL_HEAP_FUNCTION(object->ToObject(*global_context), Object);
839}
840
841
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000842#ifdef ENABLE_DEBUGGER_SUPPORT
v8.team.kasperl727e9952008-09-02 14:56:44 +0000843Handle<DebugInfo> Factory::NewDebugInfo(Handle<SharedFunctionInfo> shared) {
844 // Get the original code of the function.
845 Handle<Code> code(shared->code());
846
847 // Create a copy of the code before allocating the debug info object to avoid
848 // allocation while setting up the debug info object.
849 Handle<Code> original_code(*Factory::CopyCode(code));
850
851 // Allocate initial fixed array for active break points before allocating the
852 // debug info object to avoid allocation while setting up the debug info
853 // object.
854 Handle<FixedArray> break_points(
855 Factory::NewFixedArray(Debug::kEstimatedNofBreakPointsInFunction));
856
857 // Create and set up the debug info object. Debug info contains function, a
858 // copy of the original code, the executing code and initial fixed array for
859 // active break points.
860 Handle<DebugInfo> debug_info =
861 Handle<DebugInfo>::cast(Factory::NewStruct(DEBUG_INFO_TYPE));
862 debug_info->set_shared(*shared);
863 debug_info->set_original_code(*original_code);
864 debug_info->set_code(*code);
865 debug_info->set_break_points(*break_points);
866
867 // Link debug info to function.
868 shared->set_debug_info(*debug_info);
869
870 return debug_info;
871}
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000872#endif
v8.team.kasperl727e9952008-09-02 14:56:44 +0000873
874
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000875Handle<JSObject> Factory::NewArgumentsObject(Handle<Object> callee,
876 int length) {
877 CALL_HEAP_FUNCTION(Heap::AllocateArgumentsObject(*callee, length), JSObject);
878}
879
880
881Handle<JSFunction> Factory::CreateApiFunction(
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000882 Handle<FunctionTemplateInfo> obj, ApiInstanceType instance_type) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000883 Handle<Code> code = Handle<Code>(Builtins::builtin(Builtins::HandleApiCall));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000884 Handle<Code> construct_stub =
885 Handle<Code>(Builtins::builtin(Builtins::JSConstructStubApi));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000886
kasper.lund212ac232008-07-16 07:07:30 +0000887 int internal_field_count = 0;
888 if (!obj->instance_template()->IsUndefined()) {
889 Handle<ObjectTemplateInfo> instance_template =
890 Handle<ObjectTemplateInfo>(
891 ObjectTemplateInfo::cast(obj->instance_template()));
892 internal_field_count =
893 Smi::cast(instance_template->internal_field_count())->value();
894 }
895
896 int instance_size = kPointerSize * internal_field_count;
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000897 InstanceType type = INVALID_TYPE;
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000898 switch (instance_type) {
899 case JavaScriptObject:
900 type = JS_OBJECT_TYPE;
901 instance_size += JSObject::kHeaderSize;
902 break;
903 case InnerGlobalObject:
904 type = JS_GLOBAL_OBJECT_TYPE;
905 instance_size += JSGlobalObject::kSize;
906 break;
907 case OuterGlobalObject:
908 type = JS_GLOBAL_PROXY_TYPE;
909 instance_size += JSGlobalProxy::kSize;
910 break;
911 default:
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000912 break;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000913 }
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000914 ASSERT(type != INVALID_TYPE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000915
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000916 Handle<JSFunction> result =
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000917 Factory::NewFunction(Factory::empty_symbol(),
918 type,
919 instance_size,
920 code,
921 true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000922 // Set class name.
923 Handle<Object> class_name = Handle<Object>(obj->class_name());
924 if (class_name->IsString()) {
925 result->shared()->set_instance_class_name(*class_name);
926 result->shared()->set_name(*class_name);
927 }
928
929 Handle<Map> map = Handle<Map>(result->initial_map());
930
931 // Mark as undetectable if needed.
932 if (obj->undetectable()) {
933 map->set_is_undetectable();
934 }
935
936 // Mark as hidden for the __proto__ accessor if needed.
937 if (obj->hidden_prototype()) {
938 map->set_is_hidden_prototype();
939 }
940
941 // Mark as needs_access_check if needed.
942 if (obj->needs_access_check()) {
ager@chromium.org870a0b62008-11-04 11:43:05 +0000943 map->set_is_access_check_needed(true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000944 }
945
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000946 // Set interceptor information in the map.
947 if (!obj->named_property_handler()->IsUndefined()) {
948 map->set_has_named_interceptor();
949 }
950 if (!obj->indexed_property_handler()->IsUndefined()) {
951 map->set_has_indexed_interceptor();
952 }
953
954 // Set instance call-as-function information in the map.
955 if (!obj->instance_call_handler()->IsUndefined()) {
956 map->set_has_instance_call_handler();
957 }
958
959 result->shared()->set_function_data(*obj);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000960 result->shared()->set_construct_stub(*construct_stub);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000961 result->shared()->DontAdaptArguments();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000962
963 // Recursively copy parent templates' accessors, 'data' may be modified.
964 Handle<DescriptorArray> array =
965 Handle<DescriptorArray>(map->instance_descriptors());
966 while (true) {
967 Handle<Object> props = Handle<Object>(obj->property_accessors());
968 if (!props->IsUndefined()) {
969 array = Factory::CopyAppendCallbackDescriptors(array, props);
970 }
971 Handle<Object> parent = Handle<Object>(obj->parent_template());
972 if (parent->IsUndefined()) break;
973 obj = Handle<FunctionTemplateInfo>::cast(parent);
974 }
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000975 if (!array->IsEmpty()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000976 map->set_instance_descriptors(*array);
977 }
978
vegorov@chromium.orgf8372902010-03-15 10:26:20 +0000979 ASSERT(result->shared()->IsApiFunction());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000980 return result;
981}
982
983
ager@chromium.org236ad962008-09-25 09:45:57 +0000984Handle<MapCache> Factory::NewMapCache(int at_least_space_for) {
985 CALL_HEAP_FUNCTION(MapCache::Allocate(at_least_space_for), MapCache);
986}
987
988
lrn@chromium.org303ada72010-10-27 09:33:13 +0000989MUST_USE_RESULT static MaybeObject* UpdateMapCacheWith(Context* context,
990 FixedArray* keys,
991 Map* map) {
992 Object* result;
993 { MaybeObject* maybe_result =
994 MapCache::cast(context->map_cache())->Put(keys, map);
995 if (!maybe_result->ToObject(&result)) return maybe_result;
996 }
997 context->set_map_cache(MapCache::cast(result));
ager@chromium.org236ad962008-09-25 09:45:57 +0000998 return result;
999}
1000
1001
1002Handle<MapCache> Factory::AddToMapCache(Handle<Context> context,
1003 Handle<FixedArray> keys,
1004 Handle<Map> map) {
1005 CALL_HEAP_FUNCTION(UpdateMapCacheWith(*context, *keys, *map), MapCache);
1006}
1007
1008
1009Handle<Map> Factory::ObjectLiteralMapFromCache(Handle<Context> context,
1010 Handle<FixedArray> keys) {
1011 if (context->map_cache()->IsUndefined()) {
1012 // Allocate the new map cache for the global context.
1013 Handle<MapCache> new_cache = NewMapCache(24);
1014 context->set_map_cache(*new_cache);
1015 }
ager@chromium.org32912102009-01-16 10:38:43 +00001016 // Check to see whether there is a matching element in the cache.
ager@chromium.org236ad962008-09-25 09:45:57 +00001017 Handle<MapCache> cache =
1018 Handle<MapCache>(MapCache::cast(context->map_cache()));
1019 Handle<Object> result = Handle<Object>(cache->Lookup(*keys));
1020 if (result->IsMap()) return Handle<Map>::cast(result);
1021 // Create a new map and add it to the cache.
1022 Handle<Map> map =
ager@chromium.org32912102009-01-16 10:38:43 +00001023 CopyMap(Handle<Map>(context->object_function()->initial_map()),
1024 keys->length());
ager@chromium.org236ad962008-09-25 09:45:57 +00001025 AddToMapCache(context, keys, map);
1026 return Handle<Map>(map);
1027}
1028
1029
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001030void Factory::SetRegExpAtomData(Handle<JSRegExp> regexp,
1031 JSRegExp::Type type,
1032 Handle<String> source,
1033 JSRegExp::Flags flags,
1034 Handle<Object> data) {
1035 Handle<FixedArray> store = NewFixedArray(JSRegExp::kAtomDataSize);
1036
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001037 store->set(JSRegExp::kTagIndex, Smi::FromInt(type));
1038 store->set(JSRegExp::kSourceIndex, *source);
1039 store->set(JSRegExp::kFlagsIndex, Smi::FromInt(flags.value()));
1040 store->set(JSRegExp::kAtomPatternIndex, *data);
1041 regexp->set_data(*store);
1042}
1043
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001044void Factory::SetRegExpIrregexpData(Handle<JSRegExp> regexp,
1045 JSRegExp::Type type,
1046 Handle<String> source,
1047 JSRegExp::Flags flags,
1048 int capture_count) {
1049 Handle<FixedArray> store = NewFixedArray(JSRegExp::kIrregexpDataSize);
1050
1051 store->set(JSRegExp::kTagIndex, Smi::FromInt(type));
1052 store->set(JSRegExp::kSourceIndex, *source);
1053 store->set(JSRegExp::kFlagsIndex, Smi::FromInt(flags.value()));
1054 store->set(JSRegExp::kIrregexpASCIICodeIndex, Heap::the_hole_value());
1055 store->set(JSRegExp::kIrregexpUC16CodeIndex, Heap::the_hole_value());
1056 store->set(JSRegExp::kIrregexpMaxRegisterCountIndex, Smi::FromInt(0));
1057 store->set(JSRegExp::kIrregexpCaptureCountIndex,
1058 Smi::FromInt(capture_count));
1059 regexp->set_data(*store);
1060}
1061
1062
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001063
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001064void Factory::ConfigureInstance(Handle<FunctionTemplateInfo> desc,
1065 Handle<JSObject> instance,
1066 bool* pending_exception) {
1067 // Configure the instance by adding the properties specified by the
1068 // instance template.
1069 Handle<Object> instance_template = Handle<Object>(desc->instance_template());
1070 if (!instance_template->IsUndefined()) {
1071 Execution::ConfigureInstance(instance,
1072 instance_template,
1073 pending_exception);
1074 } else {
1075 *pending_exception = false;
1076 }
1077}
1078
1079
1080} } // namespace v8::internal