blob: 5915f487de3432956a0edb39809d813e1e713ff6 [file] [log] [blame]
danno@chromium.orgfa458e42012-02-01 10:48:36 +00001// Copyright 2012 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"
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +000037#include "scopeinfo.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000038
kasperl@chromium.org71affb52009-05-26 05:44:31 +000039namespace v8 {
40namespace internal {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000041
42
43Handle<FixedArray> Factory::NewFixedArray(int size, PretenureFlag pretenure) {
44 ASSERT(0 <= size);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000045 CALL_HEAP_FUNCTION(
46 isolate(),
47 isolate()->heap()->AllocateFixedArray(size, pretenure),
48 FixedArray);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000049}
50
51
ricow@chromium.orgc9c80822010-04-21 08:22:37 +000052Handle<FixedArray> Factory::NewFixedArrayWithHoles(int size,
53 PretenureFlag pretenure) {
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +000054 ASSERT(0 <= size);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000055 CALL_HEAP_FUNCTION(
56 isolate(),
57 isolate()->heap()->AllocateFixedArrayWithHoles(size, pretenure),
58 FixedArray);
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +000059}
60
61
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +000062Handle<FixedDoubleArray> Factory::NewFixedDoubleArray(int size,
63 PretenureFlag pretenure) {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +000064 ASSERT(0 <= size);
65 CALL_HEAP_FUNCTION(
66 isolate(),
67 isolate()->heap()->AllocateUninitializedFixedDoubleArray(size, pretenure),
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +000068 FixedDoubleArray);
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +000069}
70
71
kasperl@chromium.org86f77b72009-07-06 08:21:57 +000072Handle<StringDictionary> Factory::NewStringDictionary(int at_least_space_for) {
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +000073 ASSERT(0 <= at_least_space_for);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000074 CALL_HEAP_FUNCTION(isolate(),
75 StringDictionary::Allocate(at_least_space_for),
kasperl@chromium.org86f77b72009-07-06 08:21:57 +000076 StringDictionary);
77}
78
79
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +000080Handle<SeededNumberDictionary> Factory::NewSeededNumberDictionary(
81 int at_least_space_for) {
kasperl@chromium.org86f77b72009-07-06 08:21:57 +000082 ASSERT(0 <= at_least_space_for);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000083 CALL_HEAP_FUNCTION(isolate(),
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +000084 SeededNumberDictionary::Allocate(at_least_space_for),
85 SeededNumberDictionary);
86}
87
88
89Handle<UnseededNumberDictionary> Factory::NewUnseededNumberDictionary(
90 int at_least_space_for) {
91 ASSERT(0 <= at_least_space_for);
92 CALL_HEAP_FUNCTION(isolate(),
93 UnseededNumberDictionary::Allocate(at_least_space_for),
94 UnseededNumberDictionary);
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +000095}
96
97
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +000098Handle<ObjectHashSet> Factory::NewObjectHashSet(int at_least_space_for) {
99 ASSERT(0 <= at_least_space_for);
100 CALL_HEAP_FUNCTION(isolate(),
101 ObjectHashSet::Allocate(at_least_space_for),
102 ObjectHashSet);
103}
104
105
vegorov@chromium.org7943d462011-08-01 11:41:52 +0000106Handle<ObjectHashTable> Factory::NewObjectHashTable(int at_least_space_for) {
107 ASSERT(0 <= at_least_space_for);
108 CALL_HEAP_FUNCTION(isolate(),
109 ObjectHashTable::Allocate(at_least_space_for),
110 ObjectHashTable);
111}
112
113
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000114Handle<DescriptorArray> Factory::NewDescriptorArray(int number_of_descriptors) {
115 ASSERT(0 <= number_of_descriptors);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000116 CALL_HEAP_FUNCTION(isolate(),
117 DescriptorArray::Allocate(number_of_descriptors),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000118 DescriptorArray);
119}
120
121
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000122Handle<DeoptimizationInputData> Factory::NewDeoptimizationInputData(
123 int deopt_entry_count,
124 PretenureFlag pretenure) {
125 ASSERT(deopt_entry_count > 0);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000126 CALL_HEAP_FUNCTION(isolate(),
127 DeoptimizationInputData::Allocate(deopt_entry_count,
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000128 pretenure),
129 DeoptimizationInputData);
130}
131
132
133Handle<DeoptimizationOutputData> Factory::NewDeoptimizationOutputData(
134 int deopt_entry_count,
135 PretenureFlag pretenure) {
136 ASSERT(deopt_entry_count > 0);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000137 CALL_HEAP_FUNCTION(isolate(),
138 DeoptimizationOutputData::Allocate(deopt_entry_count,
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000139 pretenure),
140 DeoptimizationOutputData);
141}
142
143
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000144Handle<AccessorPair> Factory::NewAccessorPair() {
145 CALL_HEAP_FUNCTION(isolate(),
146 isolate()->heap()->AllocateAccessorPair(),
147 AccessorPair);
148}
149
150
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000151// Symbols are created in the old generation (data space).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000152Handle<String> Factory::LookupSymbol(Vector<const char> string) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000153 CALL_HEAP_FUNCTION(isolate(),
154 isolate()->heap()->LookupSymbol(string),
155 String);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000156}
157
danno@chromium.org40cb8782011-05-25 07:58:50 +0000158// Symbols are created in the old generation (data space).
159Handle<String> Factory::LookupSymbol(Handle<String> string) {
160 CALL_HEAP_FUNCTION(isolate(),
161 isolate()->heap()->LookupSymbol(*string),
162 String);
163}
164
fschneider@chromium.org9e3e0b62011-01-03 10:16:46 +0000165Handle<String> Factory::LookupAsciiSymbol(Vector<const char> string) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000166 CALL_HEAP_FUNCTION(isolate(),
167 isolate()->heap()->LookupAsciiSymbol(string),
168 String);
fschneider@chromium.org9e3e0b62011-01-03 10:16:46 +0000169}
170
danno@chromium.org40cb8782011-05-25 07:58:50 +0000171
172Handle<String> Factory::LookupAsciiSymbol(Handle<SeqAsciiString> string,
173 int from,
174 int length) {
175 CALL_HEAP_FUNCTION(isolate(),
176 isolate()->heap()->LookupAsciiSymbol(string,
177 from,
178 length),
179 String);
180}
181
182
fschneider@chromium.org9e3e0b62011-01-03 10:16:46 +0000183Handle<String> Factory::LookupTwoByteSymbol(Vector<const uc16> string) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000184 CALL_HEAP_FUNCTION(isolate(),
185 isolate()->heap()->LookupTwoByteSymbol(string),
186 String);
fschneider@chromium.org9e3e0b62011-01-03 10:16:46 +0000187}
188
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000189
190Handle<String> Factory::NewStringFromAscii(Vector<const char> string,
191 PretenureFlag pretenure) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000192 CALL_HEAP_FUNCTION(
193 isolate(),
194 isolate()->heap()->AllocateStringFromAscii(string, pretenure),
195 String);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000196}
197
198Handle<String> Factory::NewStringFromUtf8(Vector<const char> string,
199 PretenureFlag pretenure) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000200 CALL_HEAP_FUNCTION(
201 isolate(),
202 isolate()->heap()->AllocateStringFromUtf8(string, pretenure),
203 String);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000204}
205
206
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000207Handle<String> Factory::NewStringFromTwoByte(Vector<const uc16> string,
208 PretenureFlag pretenure) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000209 CALL_HEAP_FUNCTION(
210 isolate(),
211 isolate()->heap()->AllocateStringFromTwoByte(string, pretenure),
212 String);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000213}
214
215
ager@chromium.org04921a82011-06-27 13:21:41 +0000216Handle<SeqAsciiString> Factory::NewRawAsciiString(int length,
217 PretenureFlag pretenure) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000218 CALL_HEAP_FUNCTION(
219 isolate(),
220 isolate()->heap()->AllocateRawAsciiString(length, pretenure),
ager@chromium.org04921a82011-06-27 13:21:41 +0000221 SeqAsciiString);
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +0000222}
223
224
ager@chromium.org04921a82011-06-27 13:21:41 +0000225Handle<SeqTwoByteString> Factory::NewRawTwoByteString(int length,
226 PretenureFlag pretenure) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000227 CALL_HEAP_FUNCTION(
228 isolate(),
229 isolate()->heap()->AllocateRawTwoByteString(length, pretenure),
ager@chromium.org04921a82011-06-27 13:21:41 +0000230 SeqTwoByteString);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000231}
232
233
234Handle<String> Factory::NewConsString(Handle<String> first,
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000235 Handle<String> second) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000236 CALL_HEAP_FUNCTION(isolate(),
237 isolate()->heap()->AllocateConsString(*first, *second),
238 String);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000239}
240
241
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000242Handle<String> Factory::NewSubString(Handle<String> str,
243 int begin,
244 int end) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000245 CALL_HEAP_FUNCTION(isolate(),
246 str->SubString(begin, end),
247 String);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000248}
249
250
ager@chromium.org04921a82011-06-27 13:21:41 +0000251Handle<String> Factory::NewProperSubString(Handle<String> str,
252 int begin,
253 int end) {
254 ASSERT(begin > 0 || end < str->length());
255 CALL_HEAP_FUNCTION(isolate(),
256 isolate()->heap()->AllocateSubString(*str, begin, end),
257 String);
258}
259
260
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000261Handle<String> Factory::NewExternalStringFromAscii(
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000262 const ExternalAsciiString::Resource* resource) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000263 CALL_HEAP_FUNCTION(
264 isolate(),
265 isolate()->heap()->AllocateExternalStringFromAscii(resource),
266 String);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000267}
268
269
270Handle<String> Factory::NewExternalStringFromTwoByte(
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000271 const ExternalTwoByteString::Resource* resource) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000272 CALL_HEAP_FUNCTION(
273 isolate(),
274 isolate()->heap()->AllocateExternalStringFromTwoByte(resource),
275 String);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000276}
277
278
279Handle<Context> Factory::NewGlobalContext() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000280 CALL_HEAP_FUNCTION(
281 isolate(),
282 isolate()->heap()->AllocateGlobalContext(),
283 Context);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000284}
285
286
287Handle<Context> Factory::NewFunctionContext(int length,
vegorov@chromium.org3cf47312011-06-29 13:20:01 +0000288 Handle<JSFunction> function) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000289 CALL_HEAP_FUNCTION(
290 isolate(),
vegorov@chromium.org3cf47312011-06-29 13:20:01 +0000291 isolate()->heap()->AllocateFunctionContext(length, *function),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000292 Context);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000293}
294
295
vegorov@chromium.org3cf47312011-06-29 13:20:01 +0000296Handle<Context> Factory::NewCatchContext(Handle<JSFunction> function,
297 Handle<Context> previous,
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000298 Handle<String> name,
299 Handle<Object> thrown_object) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000300 CALL_HEAP_FUNCTION(
301 isolate(),
vegorov@chromium.org3cf47312011-06-29 13:20:01 +0000302 isolate()->heap()->AllocateCatchContext(*function,
303 *previous,
304 *name,
305 *thrown_object),
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000306 Context);
307}
308
309
vegorov@chromium.org3cf47312011-06-29 13:20:01 +0000310Handle<Context> Factory::NewWithContext(Handle<JSFunction> function,
311 Handle<Context> previous,
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000312 Handle<JSObject> extension) {
313 CALL_HEAP_FUNCTION(
314 isolate(),
vegorov@chromium.org3cf47312011-06-29 13:20:01 +0000315 isolate()->heap()->AllocateWithContext(*function, *previous, *extension),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000316 Context);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000317}
318
319
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000320Handle<Context> Factory::NewBlockContext(
321 Handle<JSFunction> function,
322 Handle<Context> previous,
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000323 Handle<ScopeInfo> scope_info) {
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000324 CALL_HEAP_FUNCTION(
325 isolate(),
326 isolate()->heap()->AllocateBlockContext(*function,
327 *previous,
328 *scope_info),
329 Context);
330}
331
332
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000333Handle<Struct> Factory::NewStruct(InstanceType type) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000334 CALL_HEAP_FUNCTION(
335 isolate(),
336 isolate()->heap()->AllocateStruct(type),
337 Struct);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000338}
339
340
341Handle<AccessorInfo> Factory::NewAccessorInfo() {
342 Handle<AccessorInfo> info =
343 Handle<AccessorInfo>::cast(NewStruct(ACCESSOR_INFO_TYPE));
344 info->set_flag(0); // Must clear the flag, it was initialized as undefined.
345 return info;
346}
347
348
349Handle<Script> Factory::NewScript(Handle<String> source) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000350 // Generate id for this script.
351 int id;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000352 Heap* heap = isolate()->heap();
353 if (heap->last_script_id()->IsUndefined()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000354 // Script ids start from one.
355 id = 1;
356 } else {
357 // Increment id, wrap when positive smi is exhausted.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000358 id = Smi::cast(heap->last_script_id())->value();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000359 id++;
360 if (!Smi::IsValid(id)) {
361 id = 0;
362 }
363 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000364 heap->SetLastScriptId(Smi::FromInt(id));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000365
366 // Create and initialize script object.
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000367 Handle<Foreign> wrapper = NewForeign(0, TENURED);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000368 Handle<Script> script = Handle<Script>::cast(NewStruct(SCRIPT_TYPE));
369 script->set_source(*source);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000370 script->set_name(heap->undefined_value());
371 script->set_id(heap->last_script_id());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000372 script->set_line_offset(Smi::FromInt(0));
373 script->set_column_offset(Smi::FromInt(0));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000374 script->set_data(heap->undefined_value());
375 script->set_context_data(heap->undefined_value());
ager@chromium.orge2902be2009-06-08 12:21:35 +0000376 script->set_type(Smi::FromInt(Script::TYPE_NORMAL));
377 script->set_compilation_type(Smi::FromInt(Script::COMPILATION_TYPE_HOST));
ager@chromium.org9085a012009-05-11 19:22:57 +0000378 script->set_wrapper(*wrapper);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000379 script->set_line_ends(heap->undefined_value());
380 script->set_eval_from_shared(heap->undefined_value());
ager@chromium.orge2902be2009-06-08 12:21:35 +0000381 script->set_eval_from_instructions_offset(Smi::FromInt(0));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000382
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000383 return script;
384}
385
386
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000387Handle<Foreign> Factory::NewForeign(Address addr, PretenureFlag pretenure) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000388 CALL_HEAP_FUNCTION(isolate(),
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000389 isolate()->heap()->AllocateForeign(addr, pretenure),
390 Foreign);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000391}
392
393
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000394Handle<Foreign> Factory::NewForeign(const AccessorDescriptor* desc) {
395 return NewForeign((Address) desc, TENURED);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000396}
397
398
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000399Handle<ByteArray> Factory::NewByteArray(int length, PretenureFlag pretenure) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000400 ASSERT(0 <= length);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000401 CALL_HEAP_FUNCTION(
402 isolate(),
403 isolate()->heap()->AllocateByteArray(length, pretenure),
404 ByteArray);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000405}
406
407
ager@chromium.org3811b432009-10-28 14:53:37 +0000408Handle<ExternalArray> Factory::NewExternalArray(int length,
409 ExternalArrayType array_type,
410 void* external_pointer,
411 PretenureFlag pretenure) {
412 ASSERT(0 <= length);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000413 CALL_HEAP_FUNCTION(
414 isolate(),
415 isolate()->heap()->AllocateExternalArray(length,
416 array_type,
417 external_pointer,
418 pretenure),
419 ExternalArray);
ager@chromium.org3811b432009-10-28 14:53:37 +0000420}
421
422
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000423Handle<JSGlobalPropertyCell> Factory::NewJSGlobalPropertyCell(
424 Handle<Object> value) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000425 CALL_HEAP_FUNCTION(
426 isolate(),
427 isolate()->heap()->AllocateJSGlobalPropertyCell(*value),
428 JSGlobalPropertyCell);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000429}
430
431
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000432Handle<Map> Factory::NewMap(InstanceType type,
433 int instance_size,
434 ElementsKind elements_kind) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000435 CALL_HEAP_FUNCTION(
436 isolate(),
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000437 isolate()->heap()->AllocateMap(type, instance_size, elements_kind),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000438 Map);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000439}
440
441
442Handle<JSObject> Factory::NewFunctionPrototype(Handle<JSFunction> function) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000443 CALL_HEAP_FUNCTION(
444 isolate(),
445 isolate()->heap()->AllocateFunctionPrototype(*function),
446 JSObject);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000447}
448
449
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000450Handle<Map> Factory::CopyMapDropDescriptors(Handle<Map> src) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000451 CALL_HEAP_FUNCTION(isolate(), src->CopyDropDescriptors(), Map);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000452}
453
454
ager@chromium.org32912102009-01-16 10:38:43 +0000455Handle<Map> Factory::CopyMap(Handle<Map> src,
456 int extra_inobject_properties) {
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000457 Handle<Map> copy = CopyMapDropDescriptors(src);
ager@chromium.org32912102009-01-16 10:38:43 +0000458 // Check that we do not overflow the instance size when adding the
459 // extra inobject properties.
460 int instance_size_delta = extra_inobject_properties * kPointerSize;
461 int max_instance_size_delta =
462 JSObject::kMaxInstanceSize - copy->instance_size();
463 if (instance_size_delta > max_instance_size_delta) {
464 // If the instance size overflows, we allocate as many properties
465 // as we can as inobject properties.
466 instance_size_delta = max_instance_size_delta;
467 extra_inobject_properties = max_instance_size_delta >> kPointerSizeLog2;
468 }
469 // Adjust the map with the extra inobject properties.
470 int inobject_properties =
471 copy->inobject_properties() + extra_inobject_properties;
472 copy->set_inobject_properties(inobject_properties);
473 copy->set_unused_property_fields(inobject_properties);
474 copy->set_instance_size(copy->instance_size() + instance_size_delta);
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000475 copy->set_visitor_id(StaticVisitorBase::GetVisitorId(*copy));
ager@chromium.org32912102009-01-16 10:38:43 +0000476 return copy;
477}
478
fschneider@chromium.org40b9da32010-06-28 11:29:21 +0000479
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000480Handle<Map> Factory::CopyMapDropTransitions(Handle<Map> src) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000481 CALL_HEAP_FUNCTION(isolate(), src->CopyDropTransitions(), Map);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000482}
483
484
kmillikin@chromium.org83e16822011-09-13 08:21:47 +0000485Handle<Map> Factory::GetElementsTransitionMap(
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000486 Handle<JSObject> src,
487 ElementsKind elements_kind) {
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000488 Isolate* i = isolate();
489 CALL_HEAP_FUNCTION(i,
490 src->GetElementsTransitionMap(i, elements_kind),
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000491 Map);
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +0000492}
493
494
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000495Handle<FixedArray> Factory::CopyFixedArray(Handle<FixedArray> array) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000496 CALL_HEAP_FUNCTION(isolate(), array->Copy(), FixedArray);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000497}
498
499
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000500Handle<FixedDoubleArray> Factory::CopyFixedDoubleArray(
501 Handle<FixedDoubleArray> array) {
502 CALL_HEAP_FUNCTION(isolate(), array->Copy(), FixedDoubleArray);
503}
504
505
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000506Handle<JSFunction> Factory::BaseNewFunctionFromSharedFunctionInfo(
507 Handle<SharedFunctionInfo> function_info,
sgjesse@chromium.org846fb742009-12-18 08:56:33 +0000508 Handle<Map> function_map,
509 PretenureFlag pretenure) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000510 CALL_HEAP_FUNCTION(
511 isolate(),
512 isolate()->heap()->AllocateFunction(*function_map,
513 *function_info,
514 isolate()->heap()->the_hole_value(),
515 pretenure),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000516 JSFunction);
517}
518
519
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000520Handle<JSFunction> Factory::NewFunctionFromSharedFunctionInfo(
521 Handle<SharedFunctionInfo> function_info,
sgjesse@chromium.org846fb742009-12-18 08:56:33 +0000522 Handle<Context> context,
523 PretenureFlag pretenure) {
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000524 Handle<JSFunction> result = BaseNewFunctionFromSharedFunctionInfo(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000525 function_info,
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000526 function_info->is_classic_mode()
527 ? isolate()->function_map()
528 : isolate()->strict_mode_function_map(),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000529 pretenure);
530
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000531 result->set_context(*context);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000532 if (!function_info->bound()) {
533 int number_of_literals = function_info->num_literals();
534 Handle<FixedArray> literals = NewFixedArray(number_of_literals, pretenure);
535 if (number_of_literals > 0) {
536 // Store the object, regexp and array functions in the literals
537 // array prefix. These functions will be used when creating
538 // object, regexp and array literals in this function.
539 literals->set(JSFunction::kLiteralGlobalContextIndex,
540 context->global_context());
541 }
542 result->set_literals(*literals);
543 } else {
544 result->set_function_bindings(isolate()->heap()->empty_fixed_array());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000545 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000546 result->set_next_function_link(isolate()->heap()->undefined_value());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000547
548 if (V8::UseCrankshaft() &&
549 FLAG_always_opt &&
550 result->is_compiled() &&
551 !function_info->is_toplevel() &&
552 function_info->allows_lazy_compilation()) {
553 result->MarkForLazyRecompilation();
554 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000555 return result;
556}
557
558
559Handle<Object> Factory::NewNumber(double value,
560 PretenureFlag pretenure) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000561 CALL_HEAP_FUNCTION(
562 isolate(),
563 isolate()->heap()->NumberFromDouble(value, pretenure), Object);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000564}
565
566
erikcorry0ad885c2011-11-21 13:51:57 +0000567Handle<Object> Factory::NewNumberFromInt(int32_t value,
568 PretenureFlag pretenure) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000569 CALL_HEAP_FUNCTION(
570 isolate(),
erikcorry0ad885c2011-11-21 13:51:57 +0000571 isolate()->heap()->NumberFromInt32(value, pretenure), Object);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000572}
573
574
erikcorry0ad885c2011-11-21 13:51:57 +0000575Handle<Object> Factory::NewNumberFromUint(uint32_t value,
576 PretenureFlag pretenure) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000577 CALL_HEAP_FUNCTION(
578 isolate(),
erikcorry0ad885c2011-11-21 13:51:57 +0000579 isolate()->heap()->NumberFromUint32(value, pretenure), Object);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000580}
581
582
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000583Handle<JSObject> Factory::NewNeanderObject() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000584 CALL_HEAP_FUNCTION(
585 isolate(),
586 isolate()->heap()->AllocateJSObjectFromMap(
587 isolate()->heap()->neander_map()),
588 JSObject);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000589}
590
591
592Handle<Object> Factory::NewTypeError(const char* type,
593 Vector< Handle<Object> > args) {
594 return NewError("MakeTypeError", type, args);
595}
596
597
598Handle<Object> Factory::NewTypeError(Handle<String> message) {
599 return NewError("$TypeError", message);
600}
601
602
603Handle<Object> Factory::NewRangeError(const char* type,
604 Vector< Handle<Object> > args) {
605 return NewError("MakeRangeError", type, args);
606}
607
608
609Handle<Object> Factory::NewRangeError(Handle<String> message) {
610 return NewError("$RangeError", message);
611}
612
613
614Handle<Object> Factory::NewSyntaxError(const char* type, Handle<JSArray> args) {
615 return NewError("MakeSyntaxError", type, args);
616}
617
618
619Handle<Object> Factory::NewSyntaxError(Handle<String> message) {
620 return NewError("$SyntaxError", message);
621}
622
623
624Handle<Object> Factory::NewReferenceError(const char* type,
625 Vector< Handle<Object> > args) {
626 return NewError("MakeReferenceError", type, args);
627}
628
629
630Handle<Object> Factory::NewReferenceError(Handle<String> message) {
631 return NewError("$ReferenceError", message);
632}
633
634
635Handle<Object> Factory::NewError(const char* maker, const char* type,
636 Vector< Handle<Object> > args) {
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000637 v8::HandleScope scope; // Instantiate a closeable HandleScope for EscapeFrom.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000638 Handle<FixedArray> array = NewFixedArray(args.length());
ager@chromium.org7c537e22008-10-16 08:43:32 +0000639 for (int i = 0; i < args.length(); i++) {
640 array->set(i, *args[i]);
641 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000642 Handle<JSArray> object = NewJSArrayWithElements(array);
ager@chromium.org7c537e22008-10-16 08:43:32 +0000643 Handle<Object> result = NewError(maker, type, object);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000644 return result.EscapeFrom(&scope);
645}
646
647
648Handle<Object> Factory::NewEvalError(const char* type,
649 Vector< Handle<Object> > args) {
650 return NewError("MakeEvalError", type, args);
651}
652
653
654Handle<Object> Factory::NewError(const char* type,
655 Vector< Handle<Object> > args) {
656 return NewError("MakeError", type, args);
657}
658
659
660Handle<Object> Factory::NewError(const char* maker,
661 const char* type,
662 Handle<JSArray> args) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000663 Handle<String> make_str = LookupAsciiSymbol(maker);
664 Handle<Object> fun_obj(
665 isolate()->js_builtins_object()->GetPropertyNoExceptionThrown(*make_str));
ager@chromium.org4af710e2009-09-15 12:20:11 +0000666 // If the builtins haven't been properly configured yet this error
667 // constructor may not have been defined. Bail out.
668 if (!fun_obj->IsJSFunction())
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000669 return undefined_value();
ager@chromium.org4af710e2009-09-15 12:20:11 +0000670 Handle<JSFunction> fun = Handle<JSFunction>::cast(fun_obj);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000671 Handle<Object> type_obj = LookupAsciiSymbol(type);
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +0000672 Handle<Object> argv[] = { type_obj, args };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000673
674 // Invoke the JavaScript factory method. If an exception is thrown while
675 // running the factory method, use the exception as the result.
676 bool caught_exception;
677 Handle<Object> result = Execution::TryCall(fun,
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +0000678 isolate()->js_builtins_object(),
679 ARRAY_SIZE(argv),
680 argv,
681 &caught_exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000682 return result;
683}
684
685
686Handle<Object> Factory::NewError(Handle<String> message) {
687 return NewError("$Error", message);
688}
689
690
691Handle<Object> Factory::NewError(const char* constructor,
692 Handle<String> message) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000693 Handle<String> constr = LookupAsciiSymbol(constructor);
694 Handle<JSFunction> fun = Handle<JSFunction>(
695 JSFunction::cast(isolate()->js_builtins_object()->
696 GetPropertyNoExceptionThrown(*constr)));
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +0000697 Handle<Object> argv[] = { message };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000698
699 // Invoke the JavaScript factory method. If an exception is thrown while
700 // running the factory method, use the exception as the result.
701 bool caught_exception;
702 Handle<Object> result = Execution::TryCall(fun,
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +0000703 isolate()->js_builtins_object(),
704 ARRAY_SIZE(argv),
705 argv,
706 &caught_exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000707 return result;
708}
709
710
711Handle<JSFunction> Factory::NewFunction(Handle<String> name,
712 InstanceType type,
713 int instance_size,
714 Handle<Code> code,
715 bool force_initial_map) {
716 // Allocate the function
717 Handle<JSFunction> function = NewFunction(name, the_hole_value());
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000718
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000719 // Set up the code pointer in both the shared function info and in
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000720 // the function itself.
721 function->shared()->set_code(*code);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000722 function->set_code(*code);
723
724 if (force_initial_map ||
725 type != JS_OBJECT_TYPE ||
726 instance_size != JSObject::kHeaderSize) {
727 Handle<Map> initial_map = NewMap(type, instance_size);
728 Handle<JSObject> prototype = NewFunctionPrototype(function);
729 initial_map->set_prototype(*prototype);
730 function->set_initial_map(*initial_map);
731 initial_map->set_constructor(*function);
732 } else {
733 ASSERT(!function->has_initial_map());
734 ASSERT(!function->has_prototype());
735 }
736
737 return function;
738}
739
740
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000741Handle<JSFunction> Factory::NewFunctionWithPrototype(Handle<String> name,
742 InstanceType type,
743 int instance_size,
744 Handle<JSObject> prototype,
745 Handle<Code> code,
746 bool force_initial_map) {
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000747 // Allocate the function.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000748 Handle<JSFunction> function = NewFunction(name, prototype);
749
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000750 // Set up the code pointer in both the shared function info and in
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000751 // the function itself.
752 function->shared()->set_code(*code);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000753 function->set_code(*code);
754
755 if (force_initial_map ||
756 type != JS_OBJECT_TYPE ||
757 instance_size != JSObject::kHeaderSize) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000758 Handle<Map> initial_map = NewMap(type,
759 instance_size,
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000760 FAST_SMI_ONLY_ELEMENTS);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000761 function->set_initial_map(*initial_map);
762 initial_map->set_constructor(*function);
763 }
764
765 // Set function.prototype and give the prototype a constructor
766 // property that refers to the function.
767 SetPrototypeProperty(function, prototype);
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +0000768 // Currently safe because it is only invoked from Genesis.
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000769 CHECK_NOT_EMPTY_HANDLE(isolate(),
770 JSObject::SetLocalPropertyIgnoreAttributes(
771 prototype, constructor_symbol(),
772 function, DONT_ENUM));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000773 return function;
774}
775
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000776
kmillikin@chromium.org4111b802010-05-03 10:34:42 +0000777Handle<JSFunction> Factory::NewFunctionWithoutPrototype(Handle<String> name,
778 Handle<Code> code) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000779 Handle<JSFunction> function = NewFunctionWithoutPrototype(name,
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000780 CLASSIC_MODE);
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000781 function->shared()->set_code(*code);
kmillikin@chromium.org4111b802010-05-03 10:34:42 +0000782 function->set_code(*code);
783 ASSERT(!function->has_initial_map());
784 ASSERT(!function->has_prototype());
785 return function;
786}
787
788
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000789Handle<ScopeInfo> Factory::NewScopeInfo(int length) {
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000790 CALL_HEAP_FUNCTION(
791 isolate(),
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000792 isolate()->heap()->AllocateScopeInfo(length),
793 ScopeInfo);
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000794}
795
796
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000797Handle<Code> Factory::NewCode(const CodeDesc& desc,
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000798 Code::Flags flags,
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +0000799 Handle<Object> self_ref,
800 bool immovable) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000801 CALL_HEAP_FUNCTION(isolate(),
802 isolate()->heap()->CreateCode(
803 desc, flags, self_ref, immovable),
804 Code);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000805}
806
807
808Handle<Code> Factory::CopyCode(Handle<Code> code) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000809 CALL_HEAP_FUNCTION(isolate(),
810 isolate()->heap()->CopyCode(*code),
811 Code);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000812}
813
814
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000815Handle<Code> Factory::CopyCode(Handle<Code> code, Vector<byte> reloc_info) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000816 CALL_HEAP_FUNCTION(isolate(),
817 isolate()->heap()->CopyCode(*code, reloc_info),
818 Code);
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000819}
820
821
lrn@chromium.org303ada72010-10-27 09:33:13 +0000822MUST_USE_RESULT static inline MaybeObject* DoCopyInsert(
823 DescriptorArray* array,
824 String* key,
825 Object* value,
826 PropertyAttributes attributes) {
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000827 CallbacksDescriptor desc(key, value, attributes);
lrn@chromium.org303ada72010-10-27 09:33:13 +0000828 MaybeObject* obj = array->CopyInsert(&desc, REMOVE_TRANSITIONS);
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000829 return obj;
830}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000831
832
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000833// Allocate the new array.
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000834Handle<DescriptorArray> Factory::CopyAppendForeignDescriptor(
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000835 Handle<DescriptorArray> array,
836 Handle<String> key,
837 Handle<Object> value,
838 PropertyAttributes attributes) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000839 CALL_HEAP_FUNCTION(isolate(),
840 DoCopyInsert(*array, *key, *value, attributes),
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000841 DescriptorArray);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000842}
843
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000844
845Handle<String> Factory::SymbolFromString(Handle<String> value) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000846 CALL_HEAP_FUNCTION(isolate(),
847 isolate()->heap()->LookupSymbol(*value), String);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000848}
849
850
851Handle<DescriptorArray> Factory::CopyAppendCallbackDescriptors(
852 Handle<DescriptorArray> array,
853 Handle<Object> descriptors) {
854 v8::NeanderArray callbacks(descriptors);
855 int nof_callbacks = callbacks.length();
856 Handle<DescriptorArray> result =
857 NewDescriptorArray(array->number_of_descriptors() + nof_callbacks);
858
859 // Number of descriptors added to the result so far.
860 int descriptor_count = 0;
861
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000862 // Ensure that marking will not progress and change color of objects.
863 DescriptorArray::WhitenessWitness witness(*result);
864
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000865 // Copy the descriptors from the array.
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000866 for (int i = 0; i < array->number_of_descriptors(); i++) {
danno@chromium.orgc612e022011-11-10 11:38:15 +0000867 if (!array->IsNullDescriptor(i)) {
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000868 result->CopyFrom(descriptor_count++, *array, i, witness);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000869 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000870 }
871
872 // Number of duplicates detected.
873 int duplicates = 0;
874
875 // Fill in new callback descriptors. Process the callbacks from
876 // back to front so that the last callback with a given name takes
877 // precedence over previously added callbacks with that name.
878 for (int i = nof_callbacks - 1; i >= 0; i--) {
879 Handle<AccessorInfo> entry =
880 Handle<AccessorInfo>(AccessorInfo::cast(callbacks.get(i)));
881 // Ensure the key is a symbol before writing into the instance descriptor.
882 Handle<String> key =
883 SymbolFromString(Handle<String>(String::cast(entry->name())));
884 // Check if a descriptor with this name already exists before writing.
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000885 if (result->LinearSearch(*key, descriptor_count) ==
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000886 DescriptorArray::kNotFound) {
887 CallbacksDescriptor desc(*key, *entry, entry->property_attributes());
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000888 result->Set(descriptor_count, &desc, witness);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000889 descriptor_count++;
890 } else {
891 duplicates++;
892 }
893 }
894
895 // If duplicates were detected, allocate a result of the right size
896 // and transfer the elements.
897 if (duplicates > 0) {
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000898 int number_of_descriptors = result->number_of_descriptors() - duplicates;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000899 Handle<DescriptorArray> new_result =
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000900 NewDescriptorArray(number_of_descriptors);
901 for (int i = 0; i < number_of_descriptors; i++) {
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000902 new_result->CopyFrom(i, *result, i, witness);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000903 }
904 result = new_result;
905 }
906
907 // Sort the result before returning.
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000908 result->Sort(witness);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000909 return result;
910}
911
912
913Handle<JSObject> Factory::NewJSObject(Handle<JSFunction> constructor,
914 PretenureFlag pretenure) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000915 CALL_HEAP_FUNCTION(
916 isolate(),
917 isolate()->heap()->AllocateJSObject(*constructor, pretenure), JSObject);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000918}
919
920
kasperl@chromium.org2abc4502009-07-02 07:00:29 +0000921Handle<GlobalObject> Factory::NewGlobalObject(
922 Handle<JSFunction> constructor) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000923 CALL_HEAP_FUNCTION(isolate(),
924 isolate()->heap()->AllocateGlobalObject(*constructor),
kasperl@chromium.org2abc4502009-07-02 07:00:29 +0000925 GlobalObject);
926}
927
928
929
ager@chromium.org236ad962008-09-25 09:45:57 +0000930Handle<JSObject> Factory::NewJSObjectFromMap(Handle<Map> map) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000931 CALL_HEAP_FUNCTION(
932 isolate(),
933 isolate()->heap()->AllocateJSObjectFromMap(*map, NOT_TENURED),
934 JSObject);
ager@chromium.org236ad962008-09-25 09:45:57 +0000935}
936
937
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +0000938Handle<JSArray> Factory::NewJSArray(int capacity,
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000939 ElementsKind elements_kind,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000940 PretenureFlag pretenure) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000941 CALL_HEAP_FUNCTION(isolate(),
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000942 isolate()->heap()->AllocateJSArrayAndStorage(
943 elements_kind,
944 0,
945 capacity,
946 INITIALIZE_ARRAY_ELEMENTS_WITH_HOLE,
947 pretenure),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000948 JSArray);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000949}
950
951
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +0000952Handle<JSArray> Factory::NewJSArrayWithElements(Handle<FixedArrayBase> elements,
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000953 ElementsKind elements_kind,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000954 PretenureFlag pretenure) {
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000955 CALL_HEAP_FUNCTION(
956 isolate(),
957 isolate()->heap()->AllocateJSArrayWithElements(*elements,
958 elements_kind,
959 pretenure),
960 JSArray);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000961}
962
963
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +0000964void Factory::SetElementsCapacityAndLength(Handle<JSArray> array,
965 int capacity,
966 int length) {
967 ElementsAccessor* accessor = array->GetElementsAccessor();
968 CALL_HEAP_FUNCTION_VOID(
969 isolate(),
970 accessor->SetCapacityAndLength(*array, capacity, length));
971}
972
973
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000974void Factory::SetContent(Handle<JSArray> array,
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +0000975 Handle<FixedArrayBase> elements) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000976 CALL_HEAP_FUNCTION_VOID(
977 isolate(),
978 array->SetContent(*elements));
979}
980
981
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +0000982void Factory::EnsureCanContainHeapObjectElements(Handle<JSArray> array) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000983 CALL_HEAP_FUNCTION_VOID(
984 isolate(),
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +0000985 array->EnsureCanContainHeapObjectElements());
986}
987
988
989void Factory::EnsureCanContainElements(Handle<JSArray> array,
990 Handle<FixedArrayBase> elements,
991 EnsureElementsMode mode) {
992 CALL_HEAP_FUNCTION_VOID(
993 isolate(),
994 array->EnsureCanContainElements(*elements, mode));
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000995}
996
997
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000998Handle<JSProxy> Factory::NewJSProxy(Handle<Object> handler,
999 Handle<Object> prototype) {
1000 CALL_HEAP_FUNCTION(
1001 isolate(),
1002 isolate()->heap()->AllocateJSProxy(*handler, *prototype),
1003 JSProxy);
1004}
1005
1006
lrn@chromium.org34e60782011-09-15 07:25:40 +00001007void Factory::BecomeJSObject(Handle<JSReceiver> object) {
rossberg@chromium.org717967f2011-07-20 13:44:42 +00001008 CALL_HEAP_FUNCTION_VOID(
1009 isolate(),
lrn@chromium.org34e60782011-09-15 07:25:40 +00001010 isolate()->heap()->ReinitializeJSReceiver(
1011 *object, JS_OBJECT_TYPE, JSObject::kHeaderSize));
1012}
1013
1014
1015void Factory::BecomeJSFunction(Handle<JSReceiver> object) {
1016 CALL_HEAP_FUNCTION_VOID(
1017 isolate(),
1018 isolate()->heap()->ReinitializeJSReceiver(
1019 *object, JS_FUNCTION_TYPE, JSFunction::kSize));
rossberg@chromium.org717967f2011-07-20 13:44:42 +00001020}
1021
1022
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001023void Factory::SetIdentityHash(Handle<JSObject> object, Object* hash) {
1024 CALL_HEAP_FUNCTION_VOID(
1025 isolate(),
1026 object->SetIdentityHash(hash, ALLOW_CREATION));
1027}
1028
1029
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001030Handle<SharedFunctionInfo> Factory::NewSharedFunctionInfo(
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00001031 Handle<String> name,
1032 int number_of_literals,
1033 Handle<Code> code,
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001034 Handle<ScopeInfo> scope_info) {
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001035 Handle<SharedFunctionInfo> shared = NewSharedFunctionInfo(name);
1036 shared->set_code(*code);
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00001037 shared->set_scope_info(*scope_info);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001038 int literals_array_size = number_of_literals;
1039 // If the function contains object, regexp or array literals,
1040 // allocate extra space for a literals array prefix containing the
1041 // context.
1042 if (number_of_literals > 0) {
1043 literals_array_size += JSFunction::kLiteralsPrefixSize;
1044 }
1045 shared->set_num_literals(literals_array_size);
1046 return shared;
1047}
1048
1049
kmillikin@chromium.org31b12772011-02-02 16:08:26 +00001050Handle<JSMessageObject> Factory::NewJSMessageObject(
1051 Handle<String> type,
1052 Handle<JSArray> arguments,
1053 int start_position,
1054 int end_position,
1055 Handle<Object> script,
1056 Handle<Object> stack_trace,
1057 Handle<Object> stack_frames) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001058 CALL_HEAP_FUNCTION(isolate(),
1059 isolate()->heap()->AllocateJSMessageObject(*type,
1060 *arguments,
1061 start_position,
1062 end_position,
1063 *script,
1064 *stack_trace,
1065 *stack_frames),
kmillikin@chromium.org31b12772011-02-02 16:08:26 +00001066 JSMessageObject);
1067}
1068
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001069Handle<SharedFunctionInfo> Factory::NewSharedFunctionInfo(Handle<String> name) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001070 CALL_HEAP_FUNCTION(isolate(),
1071 isolate()->heap()->AllocateSharedFunctionInfo(*name),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001072 SharedFunctionInfo);
1073}
1074
1075
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00001076Handle<String> Factory::NumberToString(Handle<Object> number) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001077 CALL_HEAP_FUNCTION(isolate(),
1078 isolate()->heap()->NumberToString(*number), String);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00001079}
1080
1081
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001082Handle<String> Factory::Uint32ToString(uint32_t value) {
1083 CALL_HEAP_FUNCTION(isolate(),
1084 isolate()->heap()->Uint32ToString(value), String);
1085}
1086
1087
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001088Handle<SeededNumberDictionary> Factory::DictionaryAtNumberPut(
1089 Handle<SeededNumberDictionary> dictionary,
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00001090 uint32_t key,
1091 Handle<Object> value) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001092 CALL_HEAP_FUNCTION(isolate(),
1093 dictionary->AtNumberPut(key, *value),
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001094 SeededNumberDictionary);
1095}
1096
1097
1098Handle<UnseededNumberDictionary> Factory::DictionaryAtNumberPut(
1099 Handle<UnseededNumberDictionary> dictionary,
1100 uint32_t key,
1101 Handle<Object> value) {
1102 CALL_HEAP_FUNCTION(isolate(),
1103 dictionary->AtNumberPut(key, *value),
1104 UnseededNumberDictionary);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001105}
1106
1107
1108Handle<JSFunction> Factory::NewFunctionHelper(Handle<String> name,
1109 Handle<Object> prototype) {
1110 Handle<SharedFunctionInfo> function_share = NewSharedFunctionInfo(name);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001111 CALL_HEAP_FUNCTION(
1112 isolate(),
1113 isolate()->heap()->AllocateFunction(*isolate()->function_map(),
1114 *function_share,
1115 *prototype),
1116 JSFunction);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001117}
1118
1119
1120Handle<JSFunction> Factory::NewFunction(Handle<String> name,
1121 Handle<Object> prototype) {
1122 Handle<JSFunction> fun = NewFunctionHelper(name, prototype);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001123 fun->set_context(isolate()->context()->global_context());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001124 return fun;
1125}
1126
1127
kmillikin@chromium.org4111b802010-05-03 10:34:42 +00001128Handle<JSFunction> Factory::NewFunctionWithoutPrototypeHelper(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001129 Handle<String> name,
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00001130 LanguageMode language_mode) {
kmillikin@chromium.org4111b802010-05-03 10:34:42 +00001131 Handle<SharedFunctionInfo> function_share = NewSharedFunctionInfo(name);
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00001132 Handle<Map> map = (language_mode == CLASSIC_MODE)
1133 ? isolate()->function_without_prototype_map()
1134 : isolate()->strict_mode_function_without_prototype_map();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001135 CALL_HEAP_FUNCTION(isolate(),
1136 isolate()->heap()->AllocateFunction(
1137 *map,
kmillikin@chromium.org4111b802010-05-03 10:34:42 +00001138 *function_share,
1139 *the_hole_value()),
1140 JSFunction);
1141}
1142
1143
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001144Handle<JSFunction> Factory::NewFunctionWithoutPrototype(
1145 Handle<String> name,
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00001146 LanguageMode language_mode) {
1147 Handle<JSFunction> fun =
1148 NewFunctionWithoutPrototypeHelper(name, language_mode);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001149 fun->set_context(isolate()->context()->global_context());
kmillikin@chromium.org4111b802010-05-03 10:34:42 +00001150 return fun;
1151}
1152
1153
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001154Handle<Object> Factory::ToObject(Handle<Object> object) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001155 CALL_HEAP_FUNCTION(isolate(), object->ToObject(), Object);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001156}
1157
1158
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001159Handle<Object> Factory::ToObject(Handle<Object> object,
1160 Handle<Context> global_context) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001161 CALL_HEAP_FUNCTION(isolate(), object->ToObject(*global_context), Object);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001162}
1163
1164
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001165#ifdef ENABLE_DEBUGGER_SUPPORT
v8.team.kasperl727e9952008-09-02 14:56:44 +00001166Handle<DebugInfo> Factory::NewDebugInfo(Handle<SharedFunctionInfo> shared) {
1167 // Get the original code of the function.
1168 Handle<Code> code(shared->code());
1169
1170 // Create a copy of the code before allocating the debug info object to avoid
1171 // allocation while setting up the debug info object.
1172 Handle<Code> original_code(*Factory::CopyCode(code));
1173
1174 // Allocate initial fixed array for active break points before allocating the
1175 // debug info object to avoid allocation while setting up the debug info
1176 // object.
1177 Handle<FixedArray> break_points(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001178 NewFixedArray(Debug::kEstimatedNofBreakPointsInFunction));
v8.team.kasperl727e9952008-09-02 14:56:44 +00001179
1180 // Create and set up the debug info object. Debug info contains function, a
1181 // copy of the original code, the executing code and initial fixed array for
1182 // active break points.
1183 Handle<DebugInfo> debug_info =
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001184 Handle<DebugInfo>::cast(NewStruct(DEBUG_INFO_TYPE));
v8.team.kasperl727e9952008-09-02 14:56:44 +00001185 debug_info->set_shared(*shared);
1186 debug_info->set_original_code(*original_code);
1187 debug_info->set_code(*code);
1188 debug_info->set_break_points(*break_points);
1189
1190 // Link debug info to function.
1191 shared->set_debug_info(*debug_info);
1192
1193 return debug_info;
1194}
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001195#endif
v8.team.kasperl727e9952008-09-02 14:56:44 +00001196
1197
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001198Handle<JSObject> Factory::NewArgumentsObject(Handle<Object> callee,
1199 int length) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001200 CALL_HEAP_FUNCTION(
1201 isolate(),
1202 isolate()->heap()->AllocateArgumentsObject(*callee, length), JSObject);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001203}
1204
1205
1206Handle<JSFunction> Factory::CreateApiFunction(
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001207 Handle<FunctionTemplateInfo> obj, ApiInstanceType instance_type) {
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00001208 Handle<Code> code = isolate()->builtins()->HandleApiCall();
1209 Handle<Code> construct_stub = isolate()->builtins()->JSConstructStubApi();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001210
kasper.lund212ac232008-07-16 07:07:30 +00001211 int internal_field_count = 0;
1212 if (!obj->instance_template()->IsUndefined()) {
1213 Handle<ObjectTemplateInfo> instance_template =
1214 Handle<ObjectTemplateInfo>(
1215 ObjectTemplateInfo::cast(obj->instance_template()));
1216 internal_field_count =
1217 Smi::cast(instance_template->internal_field_count())->value();
1218 }
1219
1220 int instance_size = kPointerSize * internal_field_count;
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001221 InstanceType type = INVALID_TYPE;
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001222 switch (instance_type) {
1223 case JavaScriptObject:
1224 type = JS_OBJECT_TYPE;
1225 instance_size += JSObject::kHeaderSize;
1226 break;
1227 case InnerGlobalObject:
1228 type = JS_GLOBAL_OBJECT_TYPE;
1229 instance_size += JSGlobalObject::kSize;
1230 break;
1231 case OuterGlobalObject:
1232 type = JS_GLOBAL_PROXY_TYPE;
1233 instance_size += JSGlobalProxy::kSize;
1234 break;
1235 default:
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001236 break;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001237 }
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001238 ASSERT(type != INVALID_TYPE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001239
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001240 Handle<JSFunction> result =
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001241 NewFunction(Factory::empty_symbol(),
1242 type,
1243 instance_size,
1244 code,
1245 true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001246 // Set class name.
1247 Handle<Object> class_name = Handle<Object>(obj->class_name());
1248 if (class_name->IsString()) {
1249 result->shared()->set_instance_class_name(*class_name);
1250 result->shared()->set_name(*class_name);
1251 }
1252
1253 Handle<Map> map = Handle<Map>(result->initial_map());
1254
1255 // Mark as undetectable if needed.
1256 if (obj->undetectable()) {
1257 map->set_is_undetectable();
1258 }
1259
1260 // Mark as hidden for the __proto__ accessor if needed.
1261 if (obj->hidden_prototype()) {
1262 map->set_is_hidden_prototype();
1263 }
1264
1265 // Mark as needs_access_check if needed.
1266 if (obj->needs_access_check()) {
ager@chromium.org870a0b62008-11-04 11:43:05 +00001267 map->set_is_access_check_needed(true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001268 }
1269
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001270 // Set interceptor information in the map.
1271 if (!obj->named_property_handler()->IsUndefined()) {
1272 map->set_has_named_interceptor();
1273 }
1274 if (!obj->indexed_property_handler()->IsUndefined()) {
1275 map->set_has_indexed_interceptor();
1276 }
1277
1278 // Set instance call-as-function information in the map.
1279 if (!obj->instance_call_handler()->IsUndefined()) {
1280 map->set_has_instance_call_handler();
1281 }
1282
1283 result->shared()->set_function_data(*obj);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001284 result->shared()->set_construct_stub(*construct_stub);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001285 result->shared()->DontAdaptArguments();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001286
1287 // Recursively copy parent templates' accessors, 'data' may be modified.
1288 Handle<DescriptorArray> array =
1289 Handle<DescriptorArray>(map->instance_descriptors());
1290 while (true) {
1291 Handle<Object> props = Handle<Object>(obj->property_accessors());
1292 if (!props->IsUndefined()) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001293 array = CopyAppendCallbackDescriptors(array, props);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001294 }
1295 Handle<Object> parent = Handle<Object>(obj->parent_template());
1296 if (parent->IsUndefined()) break;
1297 obj = Handle<FunctionTemplateInfo>::cast(parent);
1298 }
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001299 if (!array->IsEmpty()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001300 map->set_instance_descriptors(*array);
1301 }
1302
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001303 ASSERT(result->shared()->IsApiFunction());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001304 return result;
1305}
1306
1307
ager@chromium.org236ad962008-09-25 09:45:57 +00001308Handle<MapCache> Factory::NewMapCache(int at_least_space_for) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001309 CALL_HEAP_FUNCTION(isolate(),
1310 MapCache::Allocate(at_least_space_for), MapCache);
ager@chromium.org236ad962008-09-25 09:45:57 +00001311}
1312
1313
lrn@chromium.org303ada72010-10-27 09:33:13 +00001314MUST_USE_RESULT static MaybeObject* UpdateMapCacheWith(Context* context,
1315 FixedArray* keys,
1316 Map* map) {
1317 Object* result;
1318 { MaybeObject* maybe_result =
1319 MapCache::cast(context->map_cache())->Put(keys, map);
1320 if (!maybe_result->ToObject(&result)) return maybe_result;
1321 }
1322 context->set_map_cache(MapCache::cast(result));
ager@chromium.org236ad962008-09-25 09:45:57 +00001323 return result;
1324}
1325
1326
1327Handle<MapCache> Factory::AddToMapCache(Handle<Context> context,
1328 Handle<FixedArray> keys,
1329 Handle<Map> map) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001330 CALL_HEAP_FUNCTION(isolate(),
1331 UpdateMapCacheWith(*context, *keys, *map), MapCache);
ager@chromium.org236ad962008-09-25 09:45:57 +00001332}
1333
1334
1335Handle<Map> Factory::ObjectLiteralMapFromCache(Handle<Context> context,
1336 Handle<FixedArray> keys) {
1337 if (context->map_cache()->IsUndefined()) {
1338 // Allocate the new map cache for the global context.
1339 Handle<MapCache> new_cache = NewMapCache(24);
1340 context->set_map_cache(*new_cache);
1341 }
ager@chromium.org32912102009-01-16 10:38:43 +00001342 // Check to see whether there is a matching element in the cache.
ager@chromium.org236ad962008-09-25 09:45:57 +00001343 Handle<MapCache> cache =
1344 Handle<MapCache>(MapCache::cast(context->map_cache()));
1345 Handle<Object> result = Handle<Object>(cache->Lookup(*keys));
1346 if (result->IsMap()) return Handle<Map>::cast(result);
1347 // Create a new map and add it to the cache.
1348 Handle<Map> map =
ager@chromium.org32912102009-01-16 10:38:43 +00001349 CopyMap(Handle<Map>(context->object_function()->initial_map()),
1350 keys->length());
ager@chromium.org236ad962008-09-25 09:45:57 +00001351 AddToMapCache(context, keys, map);
1352 return Handle<Map>(map);
1353}
1354
1355
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001356void Factory::SetRegExpAtomData(Handle<JSRegExp> regexp,
1357 JSRegExp::Type type,
1358 Handle<String> source,
1359 JSRegExp::Flags flags,
1360 Handle<Object> data) {
1361 Handle<FixedArray> store = NewFixedArray(JSRegExp::kAtomDataSize);
1362
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001363 store->set(JSRegExp::kTagIndex, Smi::FromInt(type));
1364 store->set(JSRegExp::kSourceIndex, *source);
1365 store->set(JSRegExp::kFlagsIndex, Smi::FromInt(flags.value()));
1366 store->set(JSRegExp::kAtomPatternIndex, *data);
1367 regexp->set_data(*store);
1368}
1369
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001370void Factory::SetRegExpIrregexpData(Handle<JSRegExp> regexp,
1371 JSRegExp::Type type,
1372 Handle<String> source,
1373 JSRegExp::Flags flags,
1374 int capture_count) {
1375 Handle<FixedArray> store = NewFixedArray(JSRegExp::kIrregexpDataSize);
jkummerow@chromium.orgddda9e82011-07-06 11:27:02 +00001376 Smi* uninitialized = Smi::FromInt(JSRegExp::kUninitializedValue);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001377 store->set(JSRegExp::kTagIndex, Smi::FromInt(type));
1378 store->set(JSRegExp::kSourceIndex, *source);
1379 store->set(JSRegExp::kFlagsIndex, Smi::FromInt(flags.value()));
jkummerow@chromium.orgddda9e82011-07-06 11:27:02 +00001380 store->set(JSRegExp::kIrregexpASCIICodeIndex, uninitialized);
1381 store->set(JSRegExp::kIrregexpUC16CodeIndex, uninitialized);
1382 store->set(JSRegExp::kIrregexpASCIICodeSavedIndex, uninitialized);
1383 store->set(JSRegExp::kIrregexpUC16CodeSavedIndex, uninitialized);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001384 store->set(JSRegExp::kIrregexpMaxRegisterCountIndex, Smi::FromInt(0));
1385 store->set(JSRegExp::kIrregexpCaptureCountIndex,
1386 Smi::FromInt(capture_count));
1387 regexp->set_data(*store);
1388}
1389
1390
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001391
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001392void Factory::ConfigureInstance(Handle<FunctionTemplateInfo> desc,
1393 Handle<JSObject> instance,
1394 bool* pending_exception) {
1395 // Configure the instance by adding the properties specified by the
1396 // instance template.
1397 Handle<Object> instance_template = Handle<Object>(desc->instance_template());
1398 if (!instance_template->IsUndefined()) {
1399 Execution::ConfigureInstance(instance,
1400 instance_template,
1401 pending_exception);
1402 } else {
1403 *pending_exception = false;
1404 }
1405}
1406
1407
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001408Handle<Object> Factory::GlobalConstantFor(Handle<String> name) {
1409 Heap* h = isolate()->heap();
1410 if (name->Equals(h->undefined_symbol())) return undefined_value();
1411 if (name->Equals(h->nan_symbol())) return nan_value();
1412 if (name->Equals(h->infinity_symbol())) return infinity_value();
1413 return Handle<Object>::null();
1414}
1415
1416
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00001417Handle<Object> Factory::ToBoolean(bool value) {
1418 return Handle<Object>(value
1419 ? isolate()->heap()->true_value()
1420 : isolate()->heap()->false_value());
1421}
1422
1423
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001424} } // namespace v8::internal