blob: 462af590d2f02ab592062ffe5f943e2cf31fc812 [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"
verwaest@chromium.org37141392012-05-31 13:27:02 +000037#include "platform.h"
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +000038#include "scopeinfo.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000039
kasperl@chromium.org71affb52009-05-26 05:44:31 +000040namespace v8 {
41namespace internal {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000042
43
44Handle<FixedArray> Factory::NewFixedArray(int size, PretenureFlag pretenure) {
45 ASSERT(0 <= size);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000046 CALL_HEAP_FUNCTION(
47 isolate(),
48 isolate()->heap()->AllocateFixedArray(size, pretenure),
49 FixedArray);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000050}
51
52
ricow@chromium.orgc9c80822010-04-21 08:22:37 +000053Handle<FixedArray> Factory::NewFixedArrayWithHoles(int size,
54 PretenureFlag pretenure) {
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +000055 ASSERT(0 <= size);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000056 CALL_HEAP_FUNCTION(
57 isolate(),
58 isolate()->heap()->AllocateFixedArrayWithHoles(size, pretenure),
59 FixedArray);
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +000060}
61
62
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +000063Handle<FixedDoubleArray> Factory::NewFixedDoubleArray(int size,
64 PretenureFlag pretenure) {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +000065 ASSERT(0 <= size);
66 CALL_HEAP_FUNCTION(
67 isolate(),
68 isolate()->heap()->AllocateUninitializedFixedDoubleArray(size, pretenure),
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +000069 FixedDoubleArray);
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +000070}
71
72
kasperl@chromium.org86f77b72009-07-06 08:21:57 +000073Handle<StringDictionary> Factory::NewStringDictionary(int at_least_space_for) {
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +000074 ASSERT(0 <= at_least_space_for);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000075 CALL_HEAP_FUNCTION(isolate(),
76 StringDictionary::Allocate(at_least_space_for),
kasperl@chromium.org86f77b72009-07-06 08:21:57 +000077 StringDictionary);
78}
79
80
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +000081Handle<SeededNumberDictionary> Factory::NewSeededNumberDictionary(
82 int at_least_space_for) {
kasperl@chromium.org86f77b72009-07-06 08:21:57 +000083 ASSERT(0 <= at_least_space_for);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000084 CALL_HEAP_FUNCTION(isolate(),
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +000085 SeededNumberDictionary::Allocate(at_least_space_for),
86 SeededNumberDictionary);
87}
88
89
90Handle<UnseededNumberDictionary> Factory::NewUnseededNumberDictionary(
91 int at_least_space_for) {
92 ASSERT(0 <= at_least_space_for);
93 CALL_HEAP_FUNCTION(isolate(),
94 UnseededNumberDictionary::Allocate(at_least_space_for),
95 UnseededNumberDictionary);
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +000096}
97
98
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +000099Handle<ObjectHashSet> Factory::NewObjectHashSet(int at_least_space_for) {
100 ASSERT(0 <= at_least_space_for);
101 CALL_HEAP_FUNCTION(isolate(),
102 ObjectHashSet::Allocate(at_least_space_for),
103 ObjectHashSet);
104}
105
106
vegorov@chromium.org7943d462011-08-01 11:41:52 +0000107Handle<ObjectHashTable> Factory::NewObjectHashTable(int at_least_space_for) {
108 ASSERT(0 <= at_least_space_for);
109 CALL_HEAP_FUNCTION(isolate(),
110 ObjectHashTable::Allocate(at_least_space_for),
111 ObjectHashTable);
112}
113
114
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000115Handle<DescriptorArray> Factory::NewDescriptorArray(int number_of_descriptors) {
116 ASSERT(0 <= number_of_descriptors);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000117 CALL_HEAP_FUNCTION(isolate(),
verwaest@chromium.orgde64f722012-08-16 15:44:54 +0000118 DescriptorArray::Allocate(number_of_descriptors),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000119 DescriptorArray);
120}
121
122
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000123Handle<DeoptimizationInputData> Factory::NewDeoptimizationInputData(
124 int deopt_entry_count,
125 PretenureFlag pretenure) {
126 ASSERT(deopt_entry_count > 0);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000127 CALL_HEAP_FUNCTION(isolate(),
128 DeoptimizationInputData::Allocate(deopt_entry_count,
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000129 pretenure),
130 DeoptimizationInputData);
131}
132
133
134Handle<DeoptimizationOutputData> Factory::NewDeoptimizationOutputData(
135 int deopt_entry_count,
136 PretenureFlag pretenure) {
137 ASSERT(deopt_entry_count > 0);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000138 CALL_HEAP_FUNCTION(isolate(),
139 DeoptimizationOutputData::Allocate(deopt_entry_count,
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000140 pretenure),
141 DeoptimizationOutputData);
142}
143
144
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000145Handle<AccessorPair> Factory::NewAccessorPair() {
146 CALL_HEAP_FUNCTION(isolate(),
147 isolate()->heap()->AllocateAccessorPair(),
148 AccessorPair);
149}
150
151
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000152Handle<TypeFeedbackInfo> Factory::NewTypeFeedbackInfo() {
153 CALL_HEAP_FUNCTION(isolate(),
154 isolate()->heap()->AllocateTypeFeedbackInfo(),
155 TypeFeedbackInfo);
156}
157
158
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000159// Symbols are created in the old generation (data space).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000160Handle<String> Factory::LookupSymbol(Vector<const char> string) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000161 CALL_HEAP_FUNCTION(isolate(),
162 isolate()->heap()->LookupSymbol(string),
163 String);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000164}
165
danno@chromium.org40cb8782011-05-25 07:58:50 +0000166// Symbols are created in the old generation (data space).
167Handle<String> Factory::LookupSymbol(Handle<String> string) {
168 CALL_HEAP_FUNCTION(isolate(),
169 isolate()->heap()->LookupSymbol(*string),
170 String);
171}
172
fschneider@chromium.org9e3e0b62011-01-03 10:16:46 +0000173Handle<String> Factory::LookupAsciiSymbol(Vector<const char> string) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000174 CALL_HEAP_FUNCTION(isolate(),
175 isolate()->heap()->LookupAsciiSymbol(string),
176 String);
fschneider@chromium.org9e3e0b62011-01-03 10:16:46 +0000177}
178
danno@chromium.org40cb8782011-05-25 07:58:50 +0000179
180Handle<String> Factory::LookupAsciiSymbol(Handle<SeqAsciiString> string,
181 int from,
182 int length) {
183 CALL_HEAP_FUNCTION(isolate(),
184 isolate()->heap()->LookupAsciiSymbol(string,
185 from,
186 length),
187 String);
188}
189
190
fschneider@chromium.org9e3e0b62011-01-03 10:16:46 +0000191Handle<String> Factory::LookupTwoByteSymbol(Vector<const uc16> string) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000192 CALL_HEAP_FUNCTION(isolate(),
193 isolate()->heap()->LookupTwoByteSymbol(string),
194 String);
fschneider@chromium.org9e3e0b62011-01-03 10:16:46 +0000195}
196
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000197
198Handle<String> Factory::NewStringFromAscii(Vector<const char> string,
199 PretenureFlag pretenure) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000200 CALL_HEAP_FUNCTION(
201 isolate(),
202 isolate()->heap()->AllocateStringFromAscii(string, pretenure),
203 String);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000204}
205
206Handle<String> Factory::NewStringFromUtf8(Vector<const char> string,
207 PretenureFlag pretenure) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000208 CALL_HEAP_FUNCTION(
209 isolate(),
210 isolate()->heap()->AllocateStringFromUtf8(string, pretenure),
211 String);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000212}
213
214
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000215Handle<String> Factory::NewStringFromTwoByte(Vector<const uc16> string,
216 PretenureFlag pretenure) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000217 CALL_HEAP_FUNCTION(
218 isolate(),
219 isolate()->heap()->AllocateStringFromTwoByte(string, pretenure),
220 String);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000221}
222
223
ager@chromium.org04921a82011-06-27 13:21:41 +0000224Handle<SeqAsciiString> Factory::NewRawAsciiString(int length,
225 PretenureFlag pretenure) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000226 CALL_HEAP_FUNCTION(
227 isolate(),
228 isolate()->heap()->AllocateRawAsciiString(length, pretenure),
ager@chromium.org04921a82011-06-27 13:21:41 +0000229 SeqAsciiString);
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +0000230}
231
232
ager@chromium.org04921a82011-06-27 13:21:41 +0000233Handle<SeqTwoByteString> Factory::NewRawTwoByteString(int length,
234 PretenureFlag pretenure) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000235 CALL_HEAP_FUNCTION(
236 isolate(),
237 isolate()->heap()->AllocateRawTwoByteString(length, pretenure),
ager@chromium.org04921a82011-06-27 13:21:41 +0000238 SeqTwoByteString);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000239}
240
241
242Handle<String> Factory::NewConsString(Handle<String> first,
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000243 Handle<String> second) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000244 CALL_HEAP_FUNCTION(isolate(),
245 isolate()->heap()->AllocateConsString(*first, *second),
246 String);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000247}
248
249
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000250Handle<String> Factory::NewSubString(Handle<String> str,
251 int begin,
252 int end) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000253 CALL_HEAP_FUNCTION(isolate(),
254 str->SubString(begin, end),
255 String);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000256}
257
258
ager@chromium.org04921a82011-06-27 13:21:41 +0000259Handle<String> Factory::NewProperSubString(Handle<String> str,
260 int begin,
261 int end) {
262 ASSERT(begin > 0 || end < str->length());
263 CALL_HEAP_FUNCTION(isolate(),
264 isolate()->heap()->AllocateSubString(*str, begin, end),
265 String);
266}
267
268
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000269Handle<String> Factory::NewExternalStringFromAscii(
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000270 const ExternalAsciiString::Resource* resource) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000271 CALL_HEAP_FUNCTION(
272 isolate(),
273 isolate()->heap()->AllocateExternalStringFromAscii(resource),
274 String);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000275}
276
277
278Handle<String> Factory::NewExternalStringFromTwoByte(
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000279 const ExternalTwoByteString::Resource* resource) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000280 CALL_HEAP_FUNCTION(
281 isolate(),
282 isolate()->heap()->AllocateExternalStringFromTwoByte(resource),
283 String);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000284}
285
286
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000287Handle<Context> Factory::NewNativeContext() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000288 CALL_HEAP_FUNCTION(
289 isolate(),
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000290 isolate()->heap()->AllocateNativeContext(),
291 Context);
292}
293
294
295Handle<Context> Factory::NewGlobalContext(Handle<JSFunction> function,
296 Handle<ScopeInfo> scope_info) {
297 CALL_HEAP_FUNCTION(
298 isolate(),
299 isolate()->heap()->AllocateGlobalContext(*function, *scope_info),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000300 Context);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000301}
302
303
danno@chromium.org81cac2b2012-07-10 11:28:27 +0000304Handle<Context> Factory::NewModuleContext(Handle<ScopeInfo> scope_info) {
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000305 CALL_HEAP_FUNCTION(
306 isolate(),
danno@chromium.org81cac2b2012-07-10 11:28:27 +0000307 isolate()->heap()->AllocateModuleContext(*scope_info),
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000308 Context);
309}
310
311
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000312Handle<Context> Factory::NewFunctionContext(int length,
vegorov@chromium.org3cf47312011-06-29 13:20:01 +0000313 Handle<JSFunction> function) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000314 CALL_HEAP_FUNCTION(
315 isolate(),
vegorov@chromium.org3cf47312011-06-29 13:20:01 +0000316 isolate()->heap()->AllocateFunctionContext(length, *function),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000317 Context);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000318}
319
320
vegorov@chromium.org3cf47312011-06-29 13:20:01 +0000321Handle<Context> Factory::NewCatchContext(Handle<JSFunction> function,
322 Handle<Context> previous,
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000323 Handle<String> name,
324 Handle<Object> thrown_object) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000325 CALL_HEAP_FUNCTION(
326 isolate(),
vegorov@chromium.org3cf47312011-06-29 13:20:01 +0000327 isolate()->heap()->AllocateCatchContext(*function,
328 *previous,
329 *name,
330 *thrown_object),
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000331 Context);
332}
333
334
vegorov@chromium.org3cf47312011-06-29 13:20:01 +0000335Handle<Context> Factory::NewWithContext(Handle<JSFunction> function,
336 Handle<Context> previous,
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000337 Handle<JSObject> extension) {
338 CALL_HEAP_FUNCTION(
339 isolate(),
vegorov@chromium.org3cf47312011-06-29 13:20:01 +0000340 isolate()->heap()->AllocateWithContext(*function, *previous, *extension),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000341 Context);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000342}
343
344
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000345Handle<Context> Factory::NewBlockContext(Handle<JSFunction> function,
346 Handle<Context> previous,
347 Handle<ScopeInfo> scope_info) {
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000348 CALL_HEAP_FUNCTION(
349 isolate(),
350 isolate()->heap()->AllocateBlockContext(*function,
351 *previous,
352 *scope_info),
353 Context);
354}
355
356
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000357Handle<Struct> Factory::NewStruct(InstanceType type) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000358 CALL_HEAP_FUNCTION(
359 isolate(),
360 isolate()->heap()->AllocateStruct(type),
361 Struct);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000362}
363
364
365Handle<AccessorInfo> Factory::NewAccessorInfo() {
366 Handle<AccessorInfo> info =
367 Handle<AccessorInfo>::cast(NewStruct(ACCESSOR_INFO_TYPE));
368 info->set_flag(0); // Must clear the flag, it was initialized as undefined.
369 return info;
370}
371
372
373Handle<Script> Factory::NewScript(Handle<String> source) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000374 // Generate id for this script.
375 int id;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000376 Heap* heap = isolate()->heap();
377 if (heap->last_script_id()->IsUndefined()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000378 // Script ids start from one.
379 id = 1;
380 } else {
381 // Increment id, wrap when positive smi is exhausted.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000382 id = Smi::cast(heap->last_script_id())->value();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000383 id++;
384 if (!Smi::IsValid(id)) {
385 id = 0;
386 }
387 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000388 heap->SetLastScriptId(Smi::FromInt(id));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000389
390 // Create and initialize script object.
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000391 Handle<Foreign> wrapper = NewForeign(0, TENURED);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000392 Handle<Script> script = Handle<Script>::cast(NewStruct(SCRIPT_TYPE));
393 script->set_source(*source);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000394 script->set_name(heap->undefined_value());
395 script->set_id(heap->last_script_id());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000396 script->set_line_offset(Smi::FromInt(0));
397 script->set_column_offset(Smi::FromInt(0));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000398 script->set_data(heap->undefined_value());
399 script->set_context_data(heap->undefined_value());
ager@chromium.orge2902be2009-06-08 12:21:35 +0000400 script->set_type(Smi::FromInt(Script::TYPE_NORMAL));
401 script->set_compilation_type(Smi::FromInt(Script::COMPILATION_TYPE_HOST));
rossberg@chromium.org2c067b12012-03-19 11:01:52 +0000402 script->set_compilation_state(
403 Smi::FromInt(Script::COMPILATION_STATE_INITIAL));
ager@chromium.org9085a012009-05-11 19:22:57 +0000404 script->set_wrapper(*wrapper);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000405 script->set_line_ends(heap->undefined_value());
406 script->set_eval_from_shared(heap->undefined_value());
ager@chromium.orge2902be2009-06-08 12:21:35 +0000407 script->set_eval_from_instructions_offset(Smi::FromInt(0));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000408
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000409 return script;
410}
411
412
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000413Handle<Foreign> Factory::NewForeign(Address addr, PretenureFlag pretenure) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000414 CALL_HEAP_FUNCTION(isolate(),
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000415 isolate()->heap()->AllocateForeign(addr, pretenure),
416 Foreign);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000417}
418
419
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000420Handle<Foreign> Factory::NewForeign(const AccessorDescriptor* desc) {
421 return NewForeign((Address) desc, TENURED);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000422}
423
424
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000425Handle<ByteArray> Factory::NewByteArray(int length, PretenureFlag pretenure) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000426 ASSERT(0 <= length);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000427 CALL_HEAP_FUNCTION(
428 isolate(),
429 isolate()->heap()->AllocateByteArray(length, pretenure),
430 ByteArray);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000431}
432
433
ager@chromium.org3811b432009-10-28 14:53:37 +0000434Handle<ExternalArray> Factory::NewExternalArray(int length,
435 ExternalArrayType array_type,
436 void* external_pointer,
437 PretenureFlag pretenure) {
438 ASSERT(0 <= length);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000439 CALL_HEAP_FUNCTION(
440 isolate(),
441 isolate()->heap()->AllocateExternalArray(length,
442 array_type,
443 external_pointer,
444 pretenure),
445 ExternalArray);
ager@chromium.org3811b432009-10-28 14:53:37 +0000446}
447
448
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000449Handle<JSGlobalPropertyCell> Factory::NewJSGlobalPropertyCell(
450 Handle<Object> value) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000451 CALL_HEAP_FUNCTION(
452 isolate(),
453 isolate()->heap()->AllocateJSGlobalPropertyCell(*value),
454 JSGlobalPropertyCell);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000455}
456
457
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000458Handle<Map> Factory::NewMap(InstanceType type,
459 int instance_size,
460 ElementsKind elements_kind) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000461 CALL_HEAP_FUNCTION(
462 isolate(),
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000463 isolate()->heap()->AllocateMap(type, instance_size, elements_kind),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000464 Map);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000465}
466
467
468Handle<JSObject> Factory::NewFunctionPrototype(Handle<JSFunction> function) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000469 CALL_HEAP_FUNCTION(
470 isolate(),
471 isolate()->heap()->AllocateFunctionPrototype(*function),
472 JSObject);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000473}
474
475
rossberg@chromium.org657d53b2012-07-12 11:06:03 +0000476Handle<Map> Factory::CopyWithPreallocatedFieldDescriptors(Handle<Map> src) {
477 CALL_HEAP_FUNCTION(
478 isolate(), src->CopyWithPreallocatedFieldDescriptors(), Map);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000479}
480
481
ager@chromium.org32912102009-01-16 10:38:43 +0000482Handle<Map> Factory::CopyMap(Handle<Map> src,
483 int extra_inobject_properties) {
rossberg@chromium.org657d53b2012-07-12 11:06:03 +0000484 Handle<Map> copy = CopyWithPreallocatedFieldDescriptors(src);
ager@chromium.org32912102009-01-16 10:38:43 +0000485 // Check that we do not overflow the instance size when adding the
486 // extra inobject properties.
487 int instance_size_delta = extra_inobject_properties * kPointerSize;
488 int max_instance_size_delta =
489 JSObject::kMaxInstanceSize - copy->instance_size();
490 if (instance_size_delta > max_instance_size_delta) {
491 // If the instance size overflows, we allocate as many properties
492 // as we can as inobject properties.
493 instance_size_delta = max_instance_size_delta;
494 extra_inobject_properties = max_instance_size_delta >> kPointerSizeLog2;
495 }
496 // Adjust the map with the extra inobject properties.
497 int inobject_properties =
498 copy->inobject_properties() + extra_inobject_properties;
499 copy->set_inobject_properties(inobject_properties);
500 copy->set_unused_property_fields(inobject_properties);
501 copy->set_instance_size(copy->instance_size() + instance_size_delta);
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000502 copy->set_visitor_id(StaticVisitorBase::GetVisitorId(*copy));
ager@chromium.org32912102009-01-16 10:38:43 +0000503 return copy;
504}
505
fschneider@chromium.org40b9da32010-06-28 11:29:21 +0000506
verwaest@chromium.org753aee42012-07-17 16:15:42 +0000507Handle<Map> Factory::CopyMap(Handle<Map> src) {
verwaest@chromium.orgde64f722012-08-16 15:44:54 +0000508 CALL_HEAP_FUNCTION(isolate(), src->Copy(), Map);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000509}
510
511
kmillikin@chromium.org83e16822011-09-13 08:21:47 +0000512Handle<Map> Factory::GetElementsTransitionMap(
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000513 Handle<JSObject> src,
514 ElementsKind elements_kind) {
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000515 Isolate* i = isolate();
516 CALL_HEAP_FUNCTION(i,
517 src->GetElementsTransitionMap(i, elements_kind),
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000518 Map);
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +0000519}
520
521
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000522Handle<FixedArray> Factory::CopyFixedArray(Handle<FixedArray> array) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000523 CALL_HEAP_FUNCTION(isolate(), array->Copy(), FixedArray);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000524}
525
526
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000527Handle<FixedDoubleArray> Factory::CopyFixedDoubleArray(
528 Handle<FixedDoubleArray> array) {
529 CALL_HEAP_FUNCTION(isolate(), array->Copy(), FixedDoubleArray);
530}
531
532
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000533Handle<JSFunction> Factory::BaseNewFunctionFromSharedFunctionInfo(
534 Handle<SharedFunctionInfo> function_info,
sgjesse@chromium.org846fb742009-12-18 08:56:33 +0000535 Handle<Map> function_map,
536 PretenureFlag pretenure) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000537 CALL_HEAP_FUNCTION(
538 isolate(),
539 isolate()->heap()->AllocateFunction(*function_map,
540 *function_info,
541 isolate()->heap()->the_hole_value(),
542 pretenure),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000543 JSFunction);
544}
545
546
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000547Handle<JSFunction> Factory::NewFunctionFromSharedFunctionInfo(
548 Handle<SharedFunctionInfo> function_info,
sgjesse@chromium.org846fb742009-12-18 08:56:33 +0000549 Handle<Context> context,
550 PretenureFlag pretenure) {
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000551 Handle<JSFunction> result = BaseNewFunctionFromSharedFunctionInfo(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000552 function_info,
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000553 function_info->is_classic_mode()
554 ? isolate()->function_map()
555 : isolate()->strict_mode_function_map(),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000556 pretenure);
557
jkummerow@chromium.org1456e702012-03-30 08:38:13 +0000558 if (function_info->ic_age() != isolate()->heap()->global_ic_age()) {
559 function_info->ResetForNewContext(isolate()->heap()->global_ic_age());
560 }
561
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000562 result->set_context(*context);
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000563
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000564 int index = function_info->SearchOptimizedCodeMap(context->native_context());
ulan@chromium.orgd9e468a2012-06-25 09:47:40 +0000565 if (!function_info->bound() && index < 0) {
566 int number_of_literals = function_info->num_literals();
567 Handle<FixedArray> literals = NewFixedArray(number_of_literals, pretenure);
568 if (number_of_literals > 0) {
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000569 // Store the native context in the literals array prefix. This
ulan@chromium.orgd9e468a2012-06-25 09:47:40 +0000570 // context will be used when creating object, regexp and array
571 // literals in this function.
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000572 literals->set(JSFunction::kLiteralNativeContextIndex,
573 context->native_context());
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000574 }
ulan@chromium.orgd9e468a2012-06-25 09:47:40 +0000575 result->set_literals(*literals);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000576 }
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000577
578 if (index > 0) {
579 // Caching of optimized code enabled and optimized code found.
ulan@chromium.orgd9e468a2012-06-25 09:47:40 +0000580 function_info->InstallFromOptimizedCodeMap(*result, index);
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000581 return result;
582 }
583
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000584 if (V8::UseCrankshaft() &&
585 FLAG_always_opt &&
586 result->is_compiled() &&
587 !function_info->is_toplevel() &&
rossberg@chromium.org2c067b12012-03-19 11:01:52 +0000588 function_info->allows_lazy_compilation() &&
589 !function_info->optimization_disabled()) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000590 result->MarkForLazyRecompilation();
591 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000592 return result;
593}
594
595
596Handle<Object> Factory::NewNumber(double value,
597 PretenureFlag pretenure) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000598 CALL_HEAP_FUNCTION(
599 isolate(),
600 isolate()->heap()->NumberFromDouble(value, pretenure), Object);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000601}
602
603
erikcorry0ad885c2011-11-21 13:51:57 +0000604Handle<Object> Factory::NewNumberFromInt(int32_t value,
605 PretenureFlag pretenure) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000606 CALL_HEAP_FUNCTION(
607 isolate(),
erikcorry0ad885c2011-11-21 13:51:57 +0000608 isolate()->heap()->NumberFromInt32(value, pretenure), Object);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000609}
610
611
erikcorry0ad885c2011-11-21 13:51:57 +0000612Handle<Object> Factory::NewNumberFromUint(uint32_t value,
613 PretenureFlag pretenure) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000614 CALL_HEAP_FUNCTION(
615 isolate(),
erikcorry0ad885c2011-11-21 13:51:57 +0000616 isolate()->heap()->NumberFromUint32(value, pretenure), Object);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000617}
618
619
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000620Handle<JSObject> Factory::NewNeanderObject() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000621 CALL_HEAP_FUNCTION(
622 isolate(),
623 isolate()->heap()->AllocateJSObjectFromMap(
624 isolate()->heap()->neander_map()),
625 JSObject);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000626}
627
628
629Handle<Object> Factory::NewTypeError(const char* type,
630 Vector< Handle<Object> > args) {
631 return NewError("MakeTypeError", type, args);
632}
633
634
635Handle<Object> Factory::NewTypeError(Handle<String> message) {
636 return NewError("$TypeError", message);
637}
638
639
640Handle<Object> Factory::NewRangeError(const char* type,
641 Vector< Handle<Object> > args) {
642 return NewError("MakeRangeError", type, args);
643}
644
645
646Handle<Object> Factory::NewRangeError(Handle<String> message) {
647 return NewError("$RangeError", message);
648}
649
650
651Handle<Object> Factory::NewSyntaxError(const char* type, Handle<JSArray> args) {
652 return NewError("MakeSyntaxError", type, args);
653}
654
655
656Handle<Object> Factory::NewSyntaxError(Handle<String> message) {
657 return NewError("$SyntaxError", message);
658}
659
660
661Handle<Object> Factory::NewReferenceError(const char* type,
662 Vector< Handle<Object> > args) {
663 return NewError("MakeReferenceError", type, args);
664}
665
666
667Handle<Object> Factory::NewReferenceError(Handle<String> message) {
668 return NewError("$ReferenceError", message);
669}
670
671
672Handle<Object> Factory::NewError(const char* maker, const char* type,
673 Vector< Handle<Object> > args) {
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000674 v8::HandleScope scope; // Instantiate a closeable HandleScope for EscapeFrom.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000675 Handle<FixedArray> array = NewFixedArray(args.length());
ager@chromium.org7c537e22008-10-16 08:43:32 +0000676 for (int i = 0; i < args.length(); i++) {
677 array->set(i, *args[i]);
678 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000679 Handle<JSArray> object = NewJSArrayWithElements(array);
ager@chromium.org7c537e22008-10-16 08:43:32 +0000680 Handle<Object> result = NewError(maker, type, object);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000681 return result.EscapeFrom(&scope);
682}
683
684
685Handle<Object> Factory::NewEvalError(const char* type,
686 Vector< Handle<Object> > args) {
687 return NewError("MakeEvalError", type, args);
688}
689
690
691Handle<Object> Factory::NewError(const char* type,
692 Vector< Handle<Object> > args) {
693 return NewError("MakeError", type, args);
694}
695
696
verwaest@chromium.org37141392012-05-31 13:27:02 +0000697Handle<String> Factory::EmergencyNewError(const char* type,
698 Handle<JSArray> args) {
699 const int kBufferSize = 1000;
700 char buffer[kBufferSize];
701 size_t space = kBufferSize;
702 char* p = &buffer[0];
703
704 Vector<char> v(buffer, kBufferSize);
705 OS::StrNCpy(v, type, space);
706 space -= Min(space, strlen(type));
707 p = &buffer[kBufferSize] - space;
708
709 for (unsigned i = 0; i < ARRAY_SIZE(args); i++) {
710 if (space > 0) {
711 *p++ = ' ';
712 space--;
713 if (space > 0) {
714 MaybeObject* maybe_arg = args->GetElement(i);
715 Handle<String> arg_str(reinterpret_cast<String*>(maybe_arg));
716 const char* arg = *arg_str->ToCString();
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000717 Vector<char> v2(p, static_cast<int>(space));
verwaest@chromium.org37141392012-05-31 13:27:02 +0000718 OS::StrNCpy(v2, arg, space);
719 space -= Min(space, strlen(arg));
720 p = &buffer[kBufferSize] - space;
721 }
722 }
723 }
724 if (space > 0) {
725 *p = '\0';
726 } else {
727 buffer[kBufferSize - 1] = '\0';
728 }
729 Handle<String> error_string = NewStringFromUtf8(CStrVector(buffer), TENURED);
730 return error_string;
731}
732
733
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000734Handle<Object> Factory::NewError(const char* maker,
735 const char* type,
736 Handle<JSArray> args) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000737 Handle<String> make_str = LookupAsciiSymbol(maker);
738 Handle<Object> fun_obj(
739 isolate()->js_builtins_object()->GetPropertyNoExceptionThrown(*make_str));
ager@chromium.org4af710e2009-09-15 12:20:11 +0000740 // If the builtins haven't been properly configured yet this error
741 // constructor may not have been defined. Bail out.
verwaest@chromium.org37141392012-05-31 13:27:02 +0000742 if (!fun_obj->IsJSFunction()) {
743 return EmergencyNewError(type, args);
744 }
ager@chromium.org4af710e2009-09-15 12:20:11 +0000745 Handle<JSFunction> fun = Handle<JSFunction>::cast(fun_obj);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000746 Handle<Object> type_obj = LookupAsciiSymbol(type);
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +0000747 Handle<Object> argv[] = { type_obj, args };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000748
749 // Invoke the JavaScript factory method. If an exception is thrown while
750 // running the factory method, use the exception as the result.
751 bool caught_exception;
752 Handle<Object> result = Execution::TryCall(fun,
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +0000753 isolate()->js_builtins_object(),
754 ARRAY_SIZE(argv),
755 argv,
756 &caught_exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000757 return result;
758}
759
760
761Handle<Object> Factory::NewError(Handle<String> message) {
762 return NewError("$Error", message);
763}
764
765
766Handle<Object> Factory::NewError(const char* constructor,
767 Handle<String> message) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000768 Handle<String> constr = LookupAsciiSymbol(constructor);
769 Handle<JSFunction> fun = Handle<JSFunction>(
770 JSFunction::cast(isolate()->js_builtins_object()->
771 GetPropertyNoExceptionThrown(*constr)));
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +0000772 Handle<Object> argv[] = { message };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000773
774 // Invoke the JavaScript factory method. If an exception is thrown while
775 // running the factory method, use the exception as the result.
776 bool caught_exception;
777 Handle<Object> result = Execution::TryCall(fun,
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +0000778 isolate()->js_builtins_object(),
779 ARRAY_SIZE(argv),
780 argv,
781 &caught_exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000782 return result;
783}
784
785
786Handle<JSFunction> Factory::NewFunction(Handle<String> name,
787 InstanceType type,
788 int instance_size,
789 Handle<Code> code,
790 bool force_initial_map) {
791 // Allocate the function
792 Handle<JSFunction> function = NewFunction(name, the_hole_value());
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000793
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000794 // Set up the code pointer in both the shared function info and in
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000795 // the function itself.
796 function->shared()->set_code(*code);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000797 function->set_code(*code);
798
799 if (force_initial_map ||
800 type != JS_OBJECT_TYPE ||
801 instance_size != JSObject::kHeaderSize) {
802 Handle<Map> initial_map = NewMap(type, instance_size);
803 Handle<JSObject> prototype = NewFunctionPrototype(function);
804 initial_map->set_prototype(*prototype);
805 function->set_initial_map(*initial_map);
806 initial_map->set_constructor(*function);
807 } else {
808 ASSERT(!function->has_initial_map());
809 ASSERT(!function->has_prototype());
810 }
811
812 return function;
813}
814
815
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000816Handle<JSFunction> Factory::NewFunctionWithPrototype(Handle<String> name,
817 InstanceType type,
818 int instance_size,
819 Handle<JSObject> prototype,
820 Handle<Code> code,
821 bool force_initial_map) {
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000822 // Allocate the function.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000823 Handle<JSFunction> function = NewFunction(name, prototype);
824
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000825 // Set up the code pointer in both the shared function info and in
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000826 // the function itself.
827 function->shared()->set_code(*code);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000828 function->set_code(*code);
829
830 if (force_initial_map ||
831 type != JS_OBJECT_TYPE ||
832 instance_size != JSObject::kHeaderSize) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000833 Handle<Map> initial_map = NewMap(type,
834 instance_size,
svenpanne@chromium.org830d30c2012-05-29 13:20:14 +0000835 GetInitialFastElementsKind());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000836 function->set_initial_map(*initial_map);
837 initial_map->set_constructor(*function);
838 }
839
840 // Set function.prototype and give the prototype a constructor
841 // property that refers to the function.
842 SetPrototypeProperty(function, prototype);
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +0000843 // Currently safe because it is only invoked from Genesis.
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000844 CHECK_NOT_EMPTY_HANDLE(isolate(),
845 JSObject::SetLocalPropertyIgnoreAttributes(
846 prototype, constructor_symbol(),
847 function, DONT_ENUM));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000848 return function;
849}
850
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000851
kmillikin@chromium.org4111b802010-05-03 10:34:42 +0000852Handle<JSFunction> Factory::NewFunctionWithoutPrototype(Handle<String> name,
853 Handle<Code> code) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000854 Handle<JSFunction> function = NewFunctionWithoutPrototype(name,
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000855 CLASSIC_MODE);
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000856 function->shared()->set_code(*code);
kmillikin@chromium.org4111b802010-05-03 10:34:42 +0000857 function->set_code(*code);
858 ASSERT(!function->has_initial_map());
859 ASSERT(!function->has_prototype());
860 return function;
861}
862
863
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000864Handle<ScopeInfo> Factory::NewScopeInfo(int length) {
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000865 CALL_HEAP_FUNCTION(
866 isolate(),
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000867 isolate()->heap()->AllocateScopeInfo(length),
868 ScopeInfo);
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000869}
870
871
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000872Handle<Code> Factory::NewCode(const CodeDesc& desc,
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000873 Code::Flags flags,
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +0000874 Handle<Object> self_ref,
875 bool immovable) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000876 CALL_HEAP_FUNCTION(isolate(),
877 isolate()->heap()->CreateCode(
878 desc, flags, self_ref, immovable),
879 Code);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000880}
881
882
883Handle<Code> Factory::CopyCode(Handle<Code> code) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000884 CALL_HEAP_FUNCTION(isolate(),
885 isolate()->heap()->CopyCode(*code),
886 Code);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000887}
888
889
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000890Handle<Code> Factory::CopyCode(Handle<Code> code, Vector<byte> reloc_info) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000891 CALL_HEAP_FUNCTION(isolate(),
892 isolate()->heap()->CopyCode(*code, reloc_info),
893 Code);
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000894}
895
896
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000897Handle<String> Factory::SymbolFromString(Handle<String> value) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000898 CALL_HEAP_FUNCTION(isolate(),
899 isolate()->heap()->LookupSymbol(*value), String);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000900}
901
902
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000903Handle<JSObject> Factory::NewJSObject(Handle<JSFunction> constructor,
904 PretenureFlag pretenure) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000905 CALL_HEAP_FUNCTION(
906 isolate(),
907 isolate()->heap()->AllocateJSObject(*constructor, pretenure), JSObject);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000908}
909
910
danno@chromium.org81cac2b2012-07-10 11:28:27 +0000911Handle<JSModule> Factory::NewJSModule(Handle<Context> context,
912 Handle<ScopeInfo> scope_info) {
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000913 CALL_HEAP_FUNCTION(
914 isolate(),
danno@chromium.org81cac2b2012-07-10 11:28:27 +0000915 isolate()->heap()->AllocateJSModule(*context, *scope_info), JSModule);
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000916}
917
918
kasperl@chromium.org2abc4502009-07-02 07:00:29 +0000919Handle<GlobalObject> Factory::NewGlobalObject(
920 Handle<JSFunction> constructor) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000921 CALL_HEAP_FUNCTION(isolate(),
922 isolate()->heap()->AllocateGlobalObject(*constructor),
kasperl@chromium.org2abc4502009-07-02 07:00:29 +0000923 GlobalObject);
924}
925
926
927
ager@chromium.org236ad962008-09-25 09:45:57 +0000928Handle<JSObject> Factory::NewJSObjectFromMap(Handle<Map> map) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000929 CALL_HEAP_FUNCTION(
930 isolate(),
931 isolate()->heap()->AllocateJSObjectFromMap(*map, NOT_TENURED),
932 JSObject);
ager@chromium.org236ad962008-09-25 09:45:57 +0000933}
934
935
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +0000936Handle<JSArray> Factory::NewJSArray(int capacity,
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000937 ElementsKind elements_kind,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000938 PretenureFlag pretenure) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000939 CALL_HEAP_FUNCTION(isolate(),
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000940 isolate()->heap()->AllocateJSArrayAndStorage(
941 elements_kind,
942 0,
943 capacity,
944 INITIALIZE_ARRAY_ELEMENTS_WITH_HOLE,
945 pretenure),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000946 JSArray);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000947}
948
949
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +0000950Handle<JSArray> Factory::NewJSArrayWithElements(Handle<FixedArrayBase> elements,
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000951 ElementsKind elements_kind,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000952 PretenureFlag pretenure) {
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000953 CALL_HEAP_FUNCTION(
954 isolate(),
955 isolate()->heap()->AllocateJSArrayWithElements(*elements,
956 elements_kind,
957 pretenure),
958 JSArray);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000959}
960
961
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +0000962void Factory::SetElementsCapacityAndLength(Handle<JSArray> array,
963 int capacity,
964 int length) {
965 ElementsAccessor* accessor = array->GetElementsAccessor();
966 CALL_HEAP_FUNCTION_VOID(
967 isolate(),
968 accessor->SetCapacityAndLength(*array, capacity, length));
969}
970
971
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000972void Factory::SetContent(Handle<JSArray> array,
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +0000973 Handle<FixedArrayBase> elements) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000974 CALL_HEAP_FUNCTION_VOID(
975 isolate(),
976 array->SetContent(*elements));
977}
978
979
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +0000980void Factory::EnsureCanContainHeapObjectElements(Handle<JSArray> array) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000981 CALL_HEAP_FUNCTION_VOID(
982 isolate(),
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +0000983 array->EnsureCanContainHeapObjectElements());
984}
985
986
987void Factory::EnsureCanContainElements(Handle<JSArray> array,
988 Handle<FixedArrayBase> elements,
svenpanne@chromium.org830d30c2012-05-29 13:20:14 +0000989 uint32_t length,
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +0000990 EnsureElementsMode mode) {
991 CALL_HEAP_FUNCTION_VOID(
992 isolate(),
svenpanne@chromium.org830d30c2012-05-29 13:20:14 +0000993 array->EnsureCanContainElements(*elements, length, mode));
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000994}
995
996
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000997Handle<JSProxy> Factory::NewJSProxy(Handle<Object> handler,
998 Handle<Object> prototype) {
999 CALL_HEAP_FUNCTION(
1000 isolate(),
1001 isolate()->heap()->AllocateJSProxy(*handler, *prototype),
1002 JSProxy);
1003}
1004
1005
lrn@chromium.org34e60782011-09-15 07:25:40 +00001006void Factory::BecomeJSObject(Handle<JSReceiver> object) {
rossberg@chromium.org717967f2011-07-20 13:44:42 +00001007 CALL_HEAP_FUNCTION_VOID(
1008 isolate(),
lrn@chromium.org34e60782011-09-15 07:25:40 +00001009 isolate()->heap()->ReinitializeJSReceiver(
1010 *object, JS_OBJECT_TYPE, JSObject::kHeaderSize));
1011}
1012
1013
1014void Factory::BecomeJSFunction(Handle<JSReceiver> object) {
1015 CALL_HEAP_FUNCTION_VOID(
1016 isolate(),
1017 isolate()->heap()->ReinitializeJSReceiver(
1018 *object, JS_FUNCTION_TYPE, JSFunction::kSize));
rossberg@chromium.org717967f2011-07-20 13:44:42 +00001019}
1020
1021
jkummerow@chromium.org000f7fb2012-08-01 11:14:42 +00001022void Factory::SetIdentityHash(Handle<JSObject> object, Smi* hash) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001023 CALL_HEAP_FUNCTION_VOID(
1024 isolate(),
1025 object->SetIdentityHash(hash, ALLOW_CREATION));
1026}
1027
1028
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001029Handle<SharedFunctionInfo> Factory::NewSharedFunctionInfo(
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00001030 Handle<String> name,
1031 int number_of_literals,
1032 Handle<Code> code,
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001033 Handle<ScopeInfo> scope_info) {
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001034 Handle<SharedFunctionInfo> shared = NewSharedFunctionInfo(name);
1035 shared->set_code(*code);
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00001036 shared->set_scope_info(*scope_info);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001037 int literals_array_size = number_of_literals;
1038 // If the function contains object, regexp or array literals,
1039 // allocate extra space for a literals array prefix containing the
1040 // context.
1041 if (number_of_literals > 0) {
1042 literals_array_size += JSFunction::kLiteralsPrefixSize;
1043 }
1044 shared->set_num_literals(literals_array_size);
1045 return shared;
1046}
1047
1048
kmillikin@chromium.org31b12772011-02-02 16:08:26 +00001049Handle<JSMessageObject> Factory::NewJSMessageObject(
1050 Handle<String> type,
1051 Handle<JSArray> arguments,
1052 int start_position,
1053 int end_position,
1054 Handle<Object> script,
1055 Handle<Object> stack_trace,
1056 Handle<Object> stack_frames) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001057 CALL_HEAP_FUNCTION(isolate(),
1058 isolate()->heap()->AllocateJSMessageObject(*type,
1059 *arguments,
1060 start_position,
1061 end_position,
1062 *script,
1063 *stack_trace,
1064 *stack_frames),
kmillikin@chromium.org31b12772011-02-02 16:08:26 +00001065 JSMessageObject);
1066}
1067
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001068Handle<SharedFunctionInfo> Factory::NewSharedFunctionInfo(Handle<String> name) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001069 CALL_HEAP_FUNCTION(isolate(),
1070 isolate()->heap()->AllocateSharedFunctionInfo(*name),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001071 SharedFunctionInfo);
1072}
1073
1074
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00001075Handle<String> Factory::NumberToString(Handle<Object> number) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001076 CALL_HEAP_FUNCTION(isolate(),
1077 isolate()->heap()->NumberToString(*number), String);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00001078}
1079
1080
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001081Handle<String> Factory::Uint32ToString(uint32_t value) {
1082 CALL_HEAP_FUNCTION(isolate(),
1083 isolate()->heap()->Uint32ToString(value), String);
1084}
1085
1086
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001087Handle<SeededNumberDictionary> Factory::DictionaryAtNumberPut(
1088 Handle<SeededNumberDictionary> dictionary,
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00001089 uint32_t key,
1090 Handle<Object> value) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001091 CALL_HEAP_FUNCTION(isolate(),
1092 dictionary->AtNumberPut(key, *value),
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001093 SeededNumberDictionary);
1094}
1095
1096
1097Handle<UnseededNumberDictionary> Factory::DictionaryAtNumberPut(
1098 Handle<UnseededNumberDictionary> dictionary,
1099 uint32_t key,
1100 Handle<Object> value) {
1101 CALL_HEAP_FUNCTION(isolate(),
1102 dictionary->AtNumberPut(key, *value),
1103 UnseededNumberDictionary);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001104}
1105
1106
1107Handle<JSFunction> Factory::NewFunctionHelper(Handle<String> name,
1108 Handle<Object> prototype) {
1109 Handle<SharedFunctionInfo> function_share = NewSharedFunctionInfo(name);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001110 CALL_HEAP_FUNCTION(
1111 isolate(),
1112 isolate()->heap()->AllocateFunction(*isolate()->function_map(),
1113 *function_share,
1114 *prototype),
1115 JSFunction);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001116}
1117
1118
1119Handle<JSFunction> Factory::NewFunction(Handle<String> name,
1120 Handle<Object> prototype) {
1121 Handle<JSFunction> fun = NewFunctionHelper(name, prototype);
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001122 fun->set_context(isolate()->context()->native_context());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001123 return fun;
1124}
1125
1126
kmillikin@chromium.org4111b802010-05-03 10:34:42 +00001127Handle<JSFunction> Factory::NewFunctionWithoutPrototypeHelper(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001128 Handle<String> name,
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00001129 LanguageMode language_mode) {
kmillikin@chromium.org4111b802010-05-03 10:34:42 +00001130 Handle<SharedFunctionInfo> function_share = NewSharedFunctionInfo(name);
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00001131 Handle<Map> map = (language_mode == CLASSIC_MODE)
1132 ? isolate()->function_without_prototype_map()
1133 : isolate()->strict_mode_function_without_prototype_map();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001134 CALL_HEAP_FUNCTION(isolate(),
1135 isolate()->heap()->AllocateFunction(
1136 *map,
kmillikin@chromium.org4111b802010-05-03 10:34:42 +00001137 *function_share,
1138 *the_hole_value()),
1139 JSFunction);
1140}
1141
1142
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001143Handle<JSFunction> Factory::NewFunctionWithoutPrototype(
1144 Handle<String> name,
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00001145 LanguageMode language_mode) {
1146 Handle<JSFunction> fun =
1147 NewFunctionWithoutPrototypeHelper(name, language_mode);
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001148 fun->set_context(isolate()->context()->native_context());
kmillikin@chromium.org4111b802010-05-03 10:34:42 +00001149 return fun;
1150}
1151
1152
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001153Handle<Object> Factory::ToObject(Handle<Object> object) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001154 CALL_HEAP_FUNCTION(isolate(), object->ToObject(), Object);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001155}
1156
1157
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001158Handle<Object> Factory::ToObject(Handle<Object> object,
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001159 Handle<Context> native_context) {
1160 CALL_HEAP_FUNCTION(isolate(), object->ToObject(*native_context), Object);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001161}
1162
1163
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001164#ifdef ENABLE_DEBUGGER_SUPPORT
v8.team.kasperl727e9952008-09-02 14:56:44 +00001165Handle<DebugInfo> Factory::NewDebugInfo(Handle<SharedFunctionInfo> shared) {
1166 // Get the original code of the function.
1167 Handle<Code> code(shared->code());
1168
1169 // Create a copy of the code before allocating the debug info object to avoid
1170 // allocation while setting up the debug info object.
1171 Handle<Code> original_code(*Factory::CopyCode(code));
1172
1173 // Allocate initial fixed array for active break points before allocating the
1174 // debug info object to avoid allocation while setting up the debug info
1175 // object.
1176 Handle<FixedArray> break_points(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001177 NewFixedArray(Debug::kEstimatedNofBreakPointsInFunction));
v8.team.kasperl727e9952008-09-02 14:56:44 +00001178
1179 // Create and set up the debug info object. Debug info contains function, a
1180 // copy of the original code, the executing code and initial fixed array for
1181 // active break points.
1182 Handle<DebugInfo> debug_info =
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001183 Handle<DebugInfo>::cast(NewStruct(DEBUG_INFO_TYPE));
v8.team.kasperl727e9952008-09-02 14:56:44 +00001184 debug_info->set_shared(*shared);
1185 debug_info->set_original_code(*original_code);
1186 debug_info->set_code(*code);
1187 debug_info->set_break_points(*break_points);
1188
1189 // Link debug info to function.
1190 shared->set_debug_info(*debug_info);
1191
1192 return debug_info;
1193}
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001194#endif
v8.team.kasperl727e9952008-09-02 14:56:44 +00001195
1196
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001197Handle<JSObject> Factory::NewArgumentsObject(Handle<Object> callee,
1198 int length) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001199 CALL_HEAP_FUNCTION(
1200 isolate(),
1201 isolate()->heap()->AllocateArgumentsObject(*callee, length), JSObject);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001202}
1203
1204
1205Handle<JSFunction> Factory::CreateApiFunction(
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001206 Handle<FunctionTemplateInfo> obj, ApiInstanceType instance_type) {
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00001207 Handle<Code> code = isolate()->builtins()->HandleApiCall();
1208 Handle<Code> construct_stub = isolate()->builtins()->JSConstructStubApi();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001209
kasper.lund212ac232008-07-16 07:07:30 +00001210 int internal_field_count = 0;
1211 if (!obj->instance_template()->IsUndefined()) {
1212 Handle<ObjectTemplateInfo> instance_template =
1213 Handle<ObjectTemplateInfo>(
1214 ObjectTemplateInfo::cast(obj->instance_template()));
1215 internal_field_count =
1216 Smi::cast(instance_template->internal_field_count())->value();
1217 }
1218
1219 int instance_size = kPointerSize * internal_field_count;
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001220 InstanceType type = INVALID_TYPE;
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001221 switch (instance_type) {
1222 case JavaScriptObject:
1223 type = JS_OBJECT_TYPE;
1224 instance_size += JSObject::kHeaderSize;
1225 break;
1226 case InnerGlobalObject:
1227 type = JS_GLOBAL_OBJECT_TYPE;
1228 instance_size += JSGlobalObject::kSize;
1229 break;
1230 case OuterGlobalObject:
1231 type = JS_GLOBAL_PROXY_TYPE;
1232 instance_size += JSGlobalProxy::kSize;
1233 break;
1234 default:
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001235 break;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001236 }
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001237 ASSERT(type != INVALID_TYPE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001238
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001239 Handle<JSFunction> result =
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001240 NewFunction(Factory::empty_symbol(),
1241 type,
1242 instance_size,
1243 code,
1244 true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001245 // Set class name.
1246 Handle<Object> class_name = Handle<Object>(obj->class_name());
1247 if (class_name->IsString()) {
1248 result->shared()->set_instance_class_name(*class_name);
1249 result->shared()->set_name(*class_name);
1250 }
1251
1252 Handle<Map> map = Handle<Map>(result->initial_map());
1253
1254 // Mark as undetectable if needed.
1255 if (obj->undetectable()) {
1256 map->set_is_undetectable();
1257 }
1258
1259 // Mark as hidden for the __proto__ accessor if needed.
1260 if (obj->hidden_prototype()) {
1261 map->set_is_hidden_prototype();
1262 }
1263
1264 // Mark as needs_access_check if needed.
1265 if (obj->needs_access_check()) {
ager@chromium.org870a0b62008-11-04 11:43:05 +00001266 map->set_is_access_check_needed(true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001267 }
1268
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001269 // Set interceptor information in the map.
1270 if (!obj->named_property_handler()->IsUndefined()) {
1271 map->set_has_named_interceptor();
1272 }
1273 if (!obj->indexed_property_handler()->IsUndefined()) {
1274 map->set_has_indexed_interceptor();
1275 }
1276
1277 // Set instance call-as-function information in the map.
1278 if (!obj->instance_call_handler()->IsUndefined()) {
1279 map->set_has_instance_call_handler();
1280 }
1281
1282 result->shared()->set_function_data(*obj);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001283 result->shared()->set_construct_stub(*construct_stub);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001284 result->shared()->DontAdaptArguments();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001285
1286 // Recursively copy parent templates' accessors, 'data' may be modified.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001287 while (true) {
1288 Handle<Object> props = Handle<Object>(obj->property_accessors());
1289 if (!props->IsUndefined()) {
danno@chromium.org129d3982012-07-25 15:01:47 +00001290 Map::CopyAppendCallbackDescriptors(map, props);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001291 }
1292 Handle<Object> parent = Handle<Object>(obj->parent_template());
1293 if (parent->IsUndefined()) break;
1294 obj = Handle<FunctionTemplateInfo>::cast(parent);
1295 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001296
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001297 ASSERT(result->shared()->IsApiFunction());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001298 return result;
1299}
1300
1301
ager@chromium.org236ad962008-09-25 09:45:57 +00001302Handle<MapCache> Factory::NewMapCache(int at_least_space_for) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001303 CALL_HEAP_FUNCTION(isolate(),
1304 MapCache::Allocate(at_least_space_for), MapCache);
ager@chromium.org236ad962008-09-25 09:45:57 +00001305}
1306
1307
lrn@chromium.org303ada72010-10-27 09:33:13 +00001308MUST_USE_RESULT static MaybeObject* UpdateMapCacheWith(Context* context,
1309 FixedArray* keys,
1310 Map* map) {
1311 Object* result;
1312 { MaybeObject* maybe_result =
1313 MapCache::cast(context->map_cache())->Put(keys, map);
1314 if (!maybe_result->ToObject(&result)) return maybe_result;
1315 }
1316 context->set_map_cache(MapCache::cast(result));
ager@chromium.org236ad962008-09-25 09:45:57 +00001317 return result;
1318}
1319
1320
1321Handle<MapCache> Factory::AddToMapCache(Handle<Context> context,
1322 Handle<FixedArray> keys,
1323 Handle<Map> map) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001324 CALL_HEAP_FUNCTION(isolate(),
1325 UpdateMapCacheWith(*context, *keys, *map), MapCache);
ager@chromium.org236ad962008-09-25 09:45:57 +00001326}
1327
1328
1329Handle<Map> Factory::ObjectLiteralMapFromCache(Handle<Context> context,
1330 Handle<FixedArray> keys) {
1331 if (context->map_cache()->IsUndefined()) {
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001332 // Allocate the new map cache for the native context.
ager@chromium.org236ad962008-09-25 09:45:57 +00001333 Handle<MapCache> new_cache = NewMapCache(24);
1334 context->set_map_cache(*new_cache);
1335 }
ager@chromium.org32912102009-01-16 10:38:43 +00001336 // Check to see whether there is a matching element in the cache.
ager@chromium.org236ad962008-09-25 09:45:57 +00001337 Handle<MapCache> cache =
1338 Handle<MapCache>(MapCache::cast(context->map_cache()));
1339 Handle<Object> result = Handle<Object>(cache->Lookup(*keys));
1340 if (result->IsMap()) return Handle<Map>::cast(result);
1341 // Create a new map and add it to the cache.
1342 Handle<Map> map =
ager@chromium.org32912102009-01-16 10:38:43 +00001343 CopyMap(Handle<Map>(context->object_function()->initial_map()),
1344 keys->length());
ager@chromium.org236ad962008-09-25 09:45:57 +00001345 AddToMapCache(context, keys, map);
1346 return Handle<Map>(map);
1347}
1348
1349
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001350void Factory::SetRegExpAtomData(Handle<JSRegExp> regexp,
1351 JSRegExp::Type type,
1352 Handle<String> source,
1353 JSRegExp::Flags flags,
1354 Handle<Object> data) {
1355 Handle<FixedArray> store = NewFixedArray(JSRegExp::kAtomDataSize);
1356
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001357 store->set(JSRegExp::kTagIndex, Smi::FromInt(type));
1358 store->set(JSRegExp::kSourceIndex, *source);
1359 store->set(JSRegExp::kFlagsIndex, Smi::FromInt(flags.value()));
1360 store->set(JSRegExp::kAtomPatternIndex, *data);
1361 regexp->set_data(*store);
1362}
1363
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001364void Factory::SetRegExpIrregexpData(Handle<JSRegExp> regexp,
1365 JSRegExp::Type type,
1366 Handle<String> source,
1367 JSRegExp::Flags flags,
1368 int capture_count) {
1369 Handle<FixedArray> store = NewFixedArray(JSRegExp::kIrregexpDataSize);
jkummerow@chromium.orgddda9e82011-07-06 11:27:02 +00001370 Smi* uninitialized = Smi::FromInt(JSRegExp::kUninitializedValue);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001371 store->set(JSRegExp::kTagIndex, Smi::FromInt(type));
1372 store->set(JSRegExp::kSourceIndex, *source);
1373 store->set(JSRegExp::kFlagsIndex, Smi::FromInt(flags.value()));
jkummerow@chromium.orgddda9e82011-07-06 11:27:02 +00001374 store->set(JSRegExp::kIrregexpASCIICodeIndex, uninitialized);
1375 store->set(JSRegExp::kIrregexpUC16CodeIndex, uninitialized);
1376 store->set(JSRegExp::kIrregexpASCIICodeSavedIndex, uninitialized);
1377 store->set(JSRegExp::kIrregexpUC16CodeSavedIndex, uninitialized);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001378 store->set(JSRegExp::kIrregexpMaxRegisterCountIndex, Smi::FromInt(0));
1379 store->set(JSRegExp::kIrregexpCaptureCountIndex,
1380 Smi::FromInt(capture_count));
1381 regexp->set_data(*store);
1382}
1383
1384
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001385
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001386void Factory::ConfigureInstance(Handle<FunctionTemplateInfo> desc,
1387 Handle<JSObject> instance,
1388 bool* pending_exception) {
1389 // Configure the instance by adding the properties specified by the
1390 // instance template.
1391 Handle<Object> instance_template = Handle<Object>(desc->instance_template());
1392 if (!instance_template->IsUndefined()) {
1393 Execution::ConfigureInstance(instance,
1394 instance_template,
1395 pending_exception);
1396 } else {
1397 *pending_exception = false;
1398 }
1399}
1400
1401
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001402Handle<Object> Factory::GlobalConstantFor(Handle<String> name) {
1403 Heap* h = isolate()->heap();
1404 if (name->Equals(h->undefined_symbol())) return undefined_value();
1405 if (name->Equals(h->nan_symbol())) return nan_value();
1406 if (name->Equals(h->infinity_symbol())) return infinity_value();
1407 return Handle<Object>::null();
1408}
1409
1410
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00001411Handle<Object> Factory::ToBoolean(bool value) {
1412 return Handle<Object>(value
1413 ? isolate()->heap()->true_value()
1414 : isolate()->heap()->false_value());
1415}
1416
1417
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001418} } // namespace v8::internal