blob: 25f197b82e6c6385bbd0703d7605dbcd5319b534 [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
verwaest@chromium.org33e09c82012-10-10 17:07:22 +0000115Handle<DescriptorArray> Factory::NewDescriptorArray(int number_of_descriptors,
116 int slack) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000117 ASSERT(0 <= number_of_descriptors);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000118 CALL_HEAP_FUNCTION(isolate(),
verwaest@chromium.org33e09c82012-10-10 17:07:22 +0000119 DescriptorArray::Allocate(number_of_descriptors, slack),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000120 DescriptorArray);
121}
122
123
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000124Handle<DeoptimizationInputData> Factory::NewDeoptimizationInputData(
125 int deopt_entry_count,
126 PretenureFlag pretenure) {
127 ASSERT(deopt_entry_count > 0);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000128 CALL_HEAP_FUNCTION(isolate(),
129 DeoptimizationInputData::Allocate(deopt_entry_count,
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000130 pretenure),
131 DeoptimizationInputData);
132}
133
134
135Handle<DeoptimizationOutputData> Factory::NewDeoptimizationOutputData(
136 int deopt_entry_count,
137 PretenureFlag pretenure) {
138 ASSERT(deopt_entry_count > 0);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000139 CALL_HEAP_FUNCTION(isolate(),
140 DeoptimizationOutputData::Allocate(deopt_entry_count,
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000141 pretenure),
142 DeoptimizationOutputData);
143}
144
145
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000146Handle<AccessorPair> Factory::NewAccessorPair() {
147 CALL_HEAP_FUNCTION(isolate(),
148 isolate()->heap()->AllocateAccessorPair(),
149 AccessorPair);
150}
151
152
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000153Handle<TypeFeedbackInfo> Factory::NewTypeFeedbackInfo() {
154 CALL_HEAP_FUNCTION(isolate(),
155 isolate()->heap()->AllocateTypeFeedbackInfo(),
156 TypeFeedbackInfo);
157}
158
159
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000160// Symbols are created in the old generation (data space).
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +0000161Handle<String> Factory::LookupUtf8Symbol(Vector<const char> string) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000162 CALL_HEAP_FUNCTION(isolate(),
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +0000163 isolate()->heap()->LookupUtf8Symbol(string),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000164 String);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000165}
166
danno@chromium.org40cb8782011-05-25 07:58:50 +0000167// Symbols are created in the old generation (data space).
168Handle<String> Factory::LookupSymbol(Handle<String> string) {
169 CALL_HEAP_FUNCTION(isolate(),
170 isolate()->heap()->LookupSymbol(*string),
171 String);
172}
173
jkummerow@chromium.org59297c72013-01-09 16:32:23 +0000174Handle<String> Factory::LookupOneByteSymbol(Vector<const uint8_t> string) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000175 CALL_HEAP_FUNCTION(isolate(),
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +0000176 isolate()->heap()->LookupOneByteSymbol(string),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000177 String);
fschneider@chromium.org9e3e0b62011-01-03 10:16:46 +0000178}
179
danno@chromium.org40cb8782011-05-25 07:58:50 +0000180
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +0000181Handle<String> Factory::LookupOneByteSymbol(Handle<SeqOneByteString> string,
danno@chromium.org40cb8782011-05-25 07:58:50 +0000182 int from,
183 int length) {
184 CALL_HEAP_FUNCTION(isolate(),
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +0000185 isolate()->heap()->LookupOneByteSymbol(string,
danno@chromium.org40cb8782011-05-25 07:58:50 +0000186 from,
187 length),
188 String);
189}
190
191
fschneider@chromium.org9e3e0b62011-01-03 10:16:46 +0000192Handle<String> Factory::LookupTwoByteSymbol(Vector<const uc16> string) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000193 CALL_HEAP_FUNCTION(isolate(),
194 isolate()->heap()->LookupTwoByteSymbol(string),
195 String);
fschneider@chromium.org9e3e0b62011-01-03 10:16:46 +0000196}
197
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000198
jkummerow@chromium.org59297c72013-01-09 16:32:23 +0000199Handle<String> Factory::NewStringFromOneByte(Vector<const uint8_t> string,
200 PretenureFlag pretenure) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000201 CALL_HEAP_FUNCTION(
202 isolate(),
ulan@chromium.org8e8d8822012-11-23 14:36:46 +0000203 isolate()->heap()->AllocateStringFromOneByte(string, pretenure),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000204 String);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000205}
206
207Handle<String> Factory::NewStringFromUtf8(Vector<const char> string,
208 PretenureFlag pretenure) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000209 CALL_HEAP_FUNCTION(
210 isolate(),
211 isolate()->heap()->AllocateStringFromUtf8(string, pretenure),
212 String);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000213}
214
215
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000216Handle<String> Factory::NewStringFromTwoByte(Vector<const uc16> string,
217 PretenureFlag pretenure) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000218 CALL_HEAP_FUNCTION(
219 isolate(),
220 isolate()->heap()->AllocateStringFromTwoByte(string, pretenure),
221 String);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000222}
223
224
yangguo@chromium.orgfb377212012-11-16 14:43:43 +0000225Handle<SeqOneByteString> Factory::NewRawOneByteString(int length,
ager@chromium.org04921a82011-06-27 13:21:41 +0000226 PretenureFlag pretenure) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000227 CALL_HEAP_FUNCTION(
228 isolate(),
ulan@chromium.org8e8d8822012-11-23 14:36:46 +0000229 isolate()->heap()->AllocateRawOneByteString(length, pretenure),
yangguo@chromium.orgfb377212012-11-16 14:43:43 +0000230 SeqOneByteString);
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +0000231}
232
233
ager@chromium.org04921a82011-06-27 13:21:41 +0000234Handle<SeqTwoByteString> Factory::NewRawTwoByteString(int length,
235 PretenureFlag pretenure) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000236 CALL_HEAP_FUNCTION(
237 isolate(),
238 isolate()->heap()->AllocateRawTwoByteString(length, pretenure),
ager@chromium.org04921a82011-06-27 13:21:41 +0000239 SeqTwoByteString);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000240}
241
242
243Handle<String> Factory::NewConsString(Handle<String> first,
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000244 Handle<String> second) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000245 CALL_HEAP_FUNCTION(isolate(),
246 isolate()->heap()->AllocateConsString(*first, *second),
247 String);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000248}
249
250
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000251Handle<String> Factory::NewSubString(Handle<String> str,
252 int begin,
253 int end) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000254 CALL_HEAP_FUNCTION(isolate(),
255 str->SubString(begin, end),
256 String);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000257}
258
259
ager@chromium.org04921a82011-06-27 13:21:41 +0000260Handle<String> Factory::NewProperSubString(Handle<String> str,
261 int begin,
262 int end) {
263 ASSERT(begin > 0 || end < str->length());
264 CALL_HEAP_FUNCTION(isolate(),
265 isolate()->heap()->AllocateSubString(*str, begin, end),
266 String);
267}
268
269
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000270Handle<String> Factory::NewExternalStringFromAscii(
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000271 const ExternalAsciiString::Resource* resource) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000272 CALL_HEAP_FUNCTION(
273 isolate(),
274 isolate()->heap()->AllocateExternalStringFromAscii(resource),
275 String);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000276}
277
278
279Handle<String> Factory::NewExternalStringFromTwoByte(
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000280 const ExternalTwoByteString::Resource* resource) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000281 CALL_HEAP_FUNCTION(
282 isolate(),
283 isolate()->heap()->AllocateExternalStringFromTwoByte(resource),
284 String);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000285}
286
287
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000288Handle<Context> Factory::NewNativeContext() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000289 CALL_HEAP_FUNCTION(
290 isolate(),
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000291 isolate()->heap()->AllocateNativeContext(),
292 Context);
293}
294
295
296Handle<Context> Factory::NewGlobalContext(Handle<JSFunction> function,
297 Handle<ScopeInfo> scope_info) {
298 CALL_HEAP_FUNCTION(
299 isolate(),
300 isolate()->heap()->AllocateGlobalContext(*function, *scope_info),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000301 Context);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000302}
303
304
danno@chromium.org81cac2b2012-07-10 11:28:27 +0000305Handle<Context> Factory::NewModuleContext(Handle<ScopeInfo> scope_info) {
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000306 CALL_HEAP_FUNCTION(
307 isolate(),
danno@chromium.org81cac2b2012-07-10 11:28:27 +0000308 isolate()->heap()->AllocateModuleContext(*scope_info),
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000309 Context);
310}
311
312
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000313Handle<Context> Factory::NewFunctionContext(int length,
vegorov@chromium.org3cf47312011-06-29 13:20:01 +0000314 Handle<JSFunction> function) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000315 CALL_HEAP_FUNCTION(
316 isolate(),
vegorov@chromium.org3cf47312011-06-29 13:20:01 +0000317 isolate()->heap()->AllocateFunctionContext(length, *function),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000318 Context);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000319}
320
321
vegorov@chromium.org3cf47312011-06-29 13:20:01 +0000322Handle<Context> Factory::NewCatchContext(Handle<JSFunction> function,
323 Handle<Context> previous,
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000324 Handle<String> name,
325 Handle<Object> thrown_object) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000326 CALL_HEAP_FUNCTION(
327 isolate(),
vegorov@chromium.org3cf47312011-06-29 13:20:01 +0000328 isolate()->heap()->AllocateCatchContext(*function,
329 *previous,
330 *name,
331 *thrown_object),
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000332 Context);
333}
334
335
vegorov@chromium.org3cf47312011-06-29 13:20:01 +0000336Handle<Context> Factory::NewWithContext(Handle<JSFunction> function,
337 Handle<Context> previous,
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000338 Handle<JSObject> extension) {
339 CALL_HEAP_FUNCTION(
340 isolate(),
vegorov@chromium.org3cf47312011-06-29 13:20:01 +0000341 isolate()->heap()->AllocateWithContext(*function, *previous, *extension),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000342 Context);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000343}
344
345
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000346Handle<Context> Factory::NewBlockContext(Handle<JSFunction> function,
347 Handle<Context> previous,
348 Handle<ScopeInfo> scope_info) {
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000349 CALL_HEAP_FUNCTION(
350 isolate(),
351 isolate()->heap()->AllocateBlockContext(*function,
352 *previous,
353 *scope_info),
354 Context);
355}
356
357
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000358Handle<Struct> Factory::NewStruct(InstanceType type) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000359 CALL_HEAP_FUNCTION(
360 isolate(),
361 isolate()->heap()->AllocateStruct(type),
362 Struct);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000363}
364
365
366Handle<AccessorInfo> Factory::NewAccessorInfo() {
367 Handle<AccessorInfo> info =
368 Handle<AccessorInfo>::cast(NewStruct(ACCESSOR_INFO_TYPE));
369 info->set_flag(0); // Must clear the flag, it was initialized as undefined.
370 return info;
371}
372
373
374Handle<Script> Factory::NewScript(Handle<String> source) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000375 // Generate id for this script.
376 int id;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000377 Heap* heap = isolate()->heap();
378 if (heap->last_script_id()->IsUndefined()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000379 // Script ids start from one.
380 id = 1;
381 } else {
382 // Increment id, wrap when positive smi is exhausted.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000383 id = Smi::cast(heap->last_script_id())->value();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000384 id++;
385 if (!Smi::IsValid(id)) {
386 id = 0;
387 }
388 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000389 heap->SetLastScriptId(Smi::FromInt(id));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000390
391 // Create and initialize script object.
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000392 Handle<Foreign> wrapper = NewForeign(0, TENURED);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000393 Handle<Script> script = Handle<Script>::cast(NewStruct(SCRIPT_TYPE));
394 script->set_source(*source);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000395 script->set_name(heap->undefined_value());
396 script->set_id(heap->last_script_id());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000397 script->set_line_offset(Smi::FromInt(0));
398 script->set_column_offset(Smi::FromInt(0));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000399 script->set_data(heap->undefined_value());
400 script->set_context_data(heap->undefined_value());
ager@chromium.orge2902be2009-06-08 12:21:35 +0000401 script->set_type(Smi::FromInt(Script::TYPE_NORMAL));
402 script->set_compilation_type(Smi::FromInt(Script::COMPILATION_TYPE_HOST));
rossberg@chromium.org2c067b12012-03-19 11:01:52 +0000403 script->set_compilation_state(
404 Smi::FromInt(Script::COMPILATION_STATE_INITIAL));
ager@chromium.org9085a012009-05-11 19:22:57 +0000405 script->set_wrapper(*wrapper);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000406 script->set_line_ends(heap->undefined_value());
407 script->set_eval_from_shared(heap->undefined_value());
ager@chromium.orge2902be2009-06-08 12:21:35 +0000408 script->set_eval_from_instructions_offset(Smi::FromInt(0));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000409
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000410 return script;
411}
412
413
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000414Handle<Foreign> Factory::NewForeign(Address addr, PretenureFlag pretenure) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000415 CALL_HEAP_FUNCTION(isolate(),
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000416 isolate()->heap()->AllocateForeign(addr, pretenure),
417 Foreign);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000418}
419
420
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000421Handle<Foreign> Factory::NewForeign(const AccessorDescriptor* desc) {
422 return NewForeign((Address) desc, TENURED);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000423}
424
425
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000426Handle<ByteArray> Factory::NewByteArray(int length, PretenureFlag pretenure) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000427 ASSERT(0 <= length);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000428 CALL_HEAP_FUNCTION(
429 isolate(),
430 isolate()->heap()->AllocateByteArray(length, pretenure),
431 ByteArray);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000432}
433
434
ager@chromium.org3811b432009-10-28 14:53:37 +0000435Handle<ExternalArray> Factory::NewExternalArray(int length,
436 ExternalArrayType array_type,
437 void* external_pointer,
438 PretenureFlag pretenure) {
439 ASSERT(0 <= length);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000440 CALL_HEAP_FUNCTION(
441 isolate(),
442 isolate()->heap()->AllocateExternalArray(length,
443 array_type,
444 external_pointer,
445 pretenure),
446 ExternalArray);
ager@chromium.org3811b432009-10-28 14:53:37 +0000447}
448
449
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000450Handle<JSGlobalPropertyCell> Factory::NewJSGlobalPropertyCell(
451 Handle<Object> value) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000452 CALL_HEAP_FUNCTION(
453 isolate(),
454 isolate()->heap()->AllocateJSGlobalPropertyCell(*value),
455 JSGlobalPropertyCell);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000456}
457
458
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000459Handle<Map> Factory::NewMap(InstanceType type,
460 int instance_size,
461 ElementsKind elements_kind) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000462 CALL_HEAP_FUNCTION(
463 isolate(),
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000464 isolate()->heap()->AllocateMap(type, instance_size, elements_kind),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000465 Map);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000466}
467
468
469Handle<JSObject> Factory::NewFunctionPrototype(Handle<JSFunction> function) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000470 CALL_HEAP_FUNCTION(
471 isolate(),
472 isolate()->heap()->AllocateFunctionPrototype(*function),
473 JSObject);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000474}
475
476
rossberg@chromium.org657d53b2012-07-12 11:06:03 +0000477Handle<Map> Factory::CopyWithPreallocatedFieldDescriptors(Handle<Map> src) {
478 CALL_HEAP_FUNCTION(
479 isolate(), src->CopyWithPreallocatedFieldDescriptors(), Map);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000480}
481
482
ager@chromium.org32912102009-01-16 10:38:43 +0000483Handle<Map> Factory::CopyMap(Handle<Map> src,
484 int extra_inobject_properties) {
rossberg@chromium.org657d53b2012-07-12 11:06:03 +0000485 Handle<Map> copy = CopyWithPreallocatedFieldDescriptors(src);
ager@chromium.org32912102009-01-16 10:38:43 +0000486 // Check that we do not overflow the instance size when adding the
487 // extra inobject properties.
488 int instance_size_delta = extra_inobject_properties * kPointerSize;
489 int max_instance_size_delta =
490 JSObject::kMaxInstanceSize - copy->instance_size();
491 if (instance_size_delta > max_instance_size_delta) {
492 // If the instance size overflows, we allocate as many properties
493 // as we can as inobject properties.
494 instance_size_delta = max_instance_size_delta;
495 extra_inobject_properties = max_instance_size_delta >> kPointerSizeLog2;
496 }
497 // Adjust the map with the extra inobject properties.
498 int inobject_properties =
499 copy->inobject_properties() + extra_inobject_properties;
500 copy->set_inobject_properties(inobject_properties);
501 copy->set_unused_property_fields(inobject_properties);
502 copy->set_instance_size(copy->instance_size() + instance_size_delta);
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000503 copy->set_visitor_id(StaticVisitorBase::GetVisitorId(*copy));
ager@chromium.org32912102009-01-16 10:38:43 +0000504 return copy;
505}
506
fschneider@chromium.org40b9da32010-06-28 11:29:21 +0000507
verwaest@chromium.org753aee42012-07-17 16:15:42 +0000508Handle<Map> Factory::CopyMap(Handle<Map> src) {
verwaest@chromium.orgde64f722012-08-16 15:44:54 +0000509 CALL_HEAP_FUNCTION(isolate(), src->Copy(), Map);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000510}
511
512
kmillikin@chromium.org83e16822011-09-13 08:21:47 +0000513Handle<Map> Factory::GetElementsTransitionMap(
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000514 Handle<JSObject> src,
515 ElementsKind elements_kind) {
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000516 Isolate* i = isolate();
517 CALL_HEAP_FUNCTION(i,
518 src->GetElementsTransitionMap(i, elements_kind),
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000519 Map);
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +0000520}
521
522
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000523Handle<FixedArray> Factory::CopyFixedArray(Handle<FixedArray> array) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000524 CALL_HEAP_FUNCTION(isolate(), array->Copy(), FixedArray);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000525}
526
527
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +0000528Handle<FixedArray> Factory::CopySizeFixedArray(Handle<FixedArray> array,
529 int new_length) {
530 CALL_HEAP_FUNCTION(isolate(), array->CopySize(new_length), FixedArray);
531}
532
533
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000534Handle<FixedDoubleArray> Factory::CopyFixedDoubleArray(
535 Handle<FixedDoubleArray> array) {
536 CALL_HEAP_FUNCTION(isolate(), array->Copy(), FixedDoubleArray);
537}
538
539
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000540Handle<JSFunction> Factory::BaseNewFunctionFromSharedFunctionInfo(
541 Handle<SharedFunctionInfo> function_info,
sgjesse@chromium.org846fb742009-12-18 08:56:33 +0000542 Handle<Map> function_map,
543 PretenureFlag pretenure) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000544 CALL_HEAP_FUNCTION(
545 isolate(),
546 isolate()->heap()->AllocateFunction(*function_map,
547 *function_info,
548 isolate()->heap()->the_hole_value(),
549 pretenure),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000550 JSFunction);
551}
552
553
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000554Handle<JSFunction> Factory::NewFunctionFromSharedFunctionInfo(
555 Handle<SharedFunctionInfo> function_info,
sgjesse@chromium.org846fb742009-12-18 08:56:33 +0000556 Handle<Context> context,
557 PretenureFlag pretenure) {
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000558 Handle<JSFunction> result = BaseNewFunctionFromSharedFunctionInfo(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000559 function_info,
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000560 function_info->is_classic_mode()
561 ? isolate()->function_map()
562 : isolate()->strict_mode_function_map(),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000563 pretenure);
564
jkummerow@chromium.org1456e702012-03-30 08:38:13 +0000565 if (function_info->ic_age() != isolate()->heap()->global_ic_age()) {
566 function_info->ResetForNewContext(isolate()->heap()->global_ic_age());
567 }
568
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000569 result->set_context(*context);
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000570
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000571 int index = function_info->SearchOptimizedCodeMap(context->native_context());
ulan@chromium.orgd9e468a2012-06-25 09:47:40 +0000572 if (!function_info->bound() && index < 0) {
573 int number_of_literals = function_info->num_literals();
574 Handle<FixedArray> literals = NewFixedArray(number_of_literals, pretenure);
575 if (number_of_literals > 0) {
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000576 // Store the native context in the literals array prefix. This
ulan@chromium.orgd9e468a2012-06-25 09:47:40 +0000577 // context will be used when creating object, regexp and array
578 // literals in this function.
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000579 literals->set(JSFunction::kLiteralNativeContextIndex,
580 context->native_context());
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000581 }
ulan@chromium.orgd9e468a2012-06-25 09:47:40 +0000582 result->set_literals(*literals);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000583 }
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000584
585 if (index > 0) {
586 // Caching of optimized code enabled and optimized code found.
ulan@chromium.orgd9e468a2012-06-25 09:47:40 +0000587 function_info->InstallFromOptimizedCodeMap(*result, index);
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000588 return result;
589 }
590
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000591 if (V8::UseCrankshaft() &&
592 FLAG_always_opt &&
593 result->is_compiled() &&
594 !function_info->is_toplevel() &&
rossberg@chromium.org2c067b12012-03-19 11:01:52 +0000595 function_info->allows_lazy_compilation() &&
596 !function_info->optimization_disabled()) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000597 result->MarkForLazyRecompilation();
598 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000599 return result;
600}
601
602
603Handle<Object> Factory::NewNumber(double value,
604 PretenureFlag pretenure) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000605 CALL_HEAP_FUNCTION(
606 isolate(),
607 isolate()->heap()->NumberFromDouble(value, pretenure), Object);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000608}
609
610
erikcorry0ad885c2011-11-21 13:51:57 +0000611Handle<Object> Factory::NewNumberFromInt(int32_t value,
612 PretenureFlag pretenure) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000613 CALL_HEAP_FUNCTION(
614 isolate(),
erikcorry0ad885c2011-11-21 13:51:57 +0000615 isolate()->heap()->NumberFromInt32(value, pretenure), Object);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000616}
617
618
erikcorry0ad885c2011-11-21 13:51:57 +0000619Handle<Object> Factory::NewNumberFromUint(uint32_t value,
620 PretenureFlag pretenure) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000621 CALL_HEAP_FUNCTION(
622 isolate(),
erikcorry0ad885c2011-11-21 13:51:57 +0000623 isolate()->heap()->NumberFromUint32(value, pretenure), Object);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000624}
625
626
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000627Handle<JSObject> Factory::NewNeanderObject() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000628 CALL_HEAP_FUNCTION(
629 isolate(),
630 isolate()->heap()->AllocateJSObjectFromMap(
631 isolate()->heap()->neander_map()),
632 JSObject);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000633}
634
635
636Handle<Object> Factory::NewTypeError(const char* type,
637 Vector< Handle<Object> > args) {
638 return NewError("MakeTypeError", type, args);
639}
640
641
642Handle<Object> Factory::NewTypeError(Handle<String> message) {
643 return NewError("$TypeError", message);
644}
645
646
647Handle<Object> Factory::NewRangeError(const char* type,
648 Vector< Handle<Object> > args) {
649 return NewError("MakeRangeError", type, args);
650}
651
652
653Handle<Object> Factory::NewRangeError(Handle<String> message) {
654 return NewError("$RangeError", message);
655}
656
657
658Handle<Object> Factory::NewSyntaxError(const char* type, Handle<JSArray> args) {
659 return NewError("MakeSyntaxError", type, args);
660}
661
662
663Handle<Object> Factory::NewSyntaxError(Handle<String> message) {
664 return NewError("$SyntaxError", message);
665}
666
667
668Handle<Object> Factory::NewReferenceError(const char* type,
669 Vector< Handle<Object> > args) {
670 return NewError("MakeReferenceError", type, args);
671}
672
673
674Handle<Object> Factory::NewReferenceError(Handle<String> message) {
675 return NewError("$ReferenceError", message);
676}
677
678
679Handle<Object> Factory::NewError(const char* maker, const char* type,
680 Vector< Handle<Object> > args) {
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000681 v8::HandleScope scope; // Instantiate a closeable HandleScope for EscapeFrom.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000682 Handle<FixedArray> array = NewFixedArray(args.length());
ager@chromium.org7c537e22008-10-16 08:43:32 +0000683 for (int i = 0; i < args.length(); i++) {
684 array->set(i, *args[i]);
685 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000686 Handle<JSArray> object = NewJSArrayWithElements(array);
ager@chromium.org7c537e22008-10-16 08:43:32 +0000687 Handle<Object> result = NewError(maker, type, object);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000688 return result.EscapeFrom(&scope);
689}
690
691
692Handle<Object> Factory::NewEvalError(const char* type,
693 Vector< Handle<Object> > args) {
694 return NewError("MakeEvalError", type, args);
695}
696
697
698Handle<Object> Factory::NewError(const char* type,
699 Vector< Handle<Object> > args) {
700 return NewError("MakeError", type, args);
701}
702
703
verwaest@chromium.org37141392012-05-31 13:27:02 +0000704Handle<String> Factory::EmergencyNewError(const char* type,
705 Handle<JSArray> args) {
706 const int kBufferSize = 1000;
707 char buffer[kBufferSize];
708 size_t space = kBufferSize;
709 char* p = &buffer[0];
710
711 Vector<char> v(buffer, kBufferSize);
712 OS::StrNCpy(v, type, space);
713 space -= Min(space, strlen(type));
714 p = &buffer[kBufferSize] - space;
715
716 for (unsigned i = 0; i < ARRAY_SIZE(args); i++) {
717 if (space > 0) {
718 *p++ = ' ';
719 space--;
720 if (space > 0) {
721 MaybeObject* maybe_arg = args->GetElement(i);
722 Handle<String> arg_str(reinterpret_cast<String*>(maybe_arg));
723 const char* arg = *arg_str->ToCString();
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000724 Vector<char> v2(p, static_cast<int>(space));
verwaest@chromium.org37141392012-05-31 13:27:02 +0000725 OS::StrNCpy(v2, arg, space);
726 space -= Min(space, strlen(arg));
727 p = &buffer[kBufferSize] - space;
728 }
729 }
730 }
731 if (space > 0) {
732 *p = '\0';
733 } else {
734 buffer[kBufferSize - 1] = '\0';
735 }
736 Handle<String> error_string = NewStringFromUtf8(CStrVector(buffer), TENURED);
737 return error_string;
738}
739
740
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000741Handle<Object> Factory::NewError(const char* maker,
742 const char* type,
743 Handle<JSArray> args) {
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +0000744 Handle<String> make_str = LookupUtf8Symbol(maker);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000745 Handle<Object> fun_obj(
746 isolate()->js_builtins_object()->GetPropertyNoExceptionThrown(*make_str));
ager@chromium.org4af710e2009-09-15 12:20:11 +0000747 // If the builtins haven't been properly configured yet this error
748 // constructor may not have been defined. Bail out.
verwaest@chromium.org37141392012-05-31 13:27:02 +0000749 if (!fun_obj->IsJSFunction()) {
750 return EmergencyNewError(type, args);
751 }
ager@chromium.org4af710e2009-09-15 12:20:11 +0000752 Handle<JSFunction> fun = Handle<JSFunction>::cast(fun_obj);
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +0000753 Handle<Object> type_obj = LookupUtf8Symbol(type);
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +0000754 Handle<Object> argv[] = { type_obj, args };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000755
756 // Invoke the JavaScript factory method. If an exception is thrown while
757 // running the factory method, use the exception as the result.
758 bool caught_exception;
759 Handle<Object> result = Execution::TryCall(fun,
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +0000760 isolate()->js_builtins_object(),
761 ARRAY_SIZE(argv),
762 argv,
763 &caught_exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000764 return result;
765}
766
767
768Handle<Object> Factory::NewError(Handle<String> message) {
769 return NewError("$Error", message);
770}
771
772
773Handle<Object> Factory::NewError(const char* constructor,
774 Handle<String> message) {
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +0000775 Handle<String> constr = LookupUtf8Symbol(constructor);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000776 Handle<JSFunction> fun = Handle<JSFunction>(
777 JSFunction::cast(isolate()->js_builtins_object()->
778 GetPropertyNoExceptionThrown(*constr)));
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +0000779 Handle<Object> argv[] = { message };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000780
781 // Invoke the JavaScript factory method. If an exception is thrown while
782 // running the factory method, use the exception as the result.
783 bool caught_exception;
784 Handle<Object> result = Execution::TryCall(fun,
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +0000785 isolate()->js_builtins_object(),
786 ARRAY_SIZE(argv),
787 argv,
788 &caught_exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000789 return result;
790}
791
792
793Handle<JSFunction> Factory::NewFunction(Handle<String> name,
794 InstanceType type,
795 int instance_size,
796 Handle<Code> code,
797 bool force_initial_map) {
798 // Allocate the function
799 Handle<JSFunction> function = NewFunction(name, the_hole_value());
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000800
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000801 // Set up the code pointer in both the shared function info and in
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000802 // the function itself.
803 function->shared()->set_code(*code);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000804 function->set_code(*code);
805
806 if (force_initial_map ||
807 type != JS_OBJECT_TYPE ||
808 instance_size != JSObject::kHeaderSize) {
809 Handle<Map> initial_map = NewMap(type, instance_size);
810 Handle<JSObject> prototype = NewFunctionPrototype(function);
811 initial_map->set_prototype(*prototype);
812 function->set_initial_map(*initial_map);
813 initial_map->set_constructor(*function);
814 } else {
815 ASSERT(!function->has_initial_map());
816 ASSERT(!function->has_prototype());
817 }
818
819 return function;
820}
821
822
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000823Handle<JSFunction> Factory::NewFunctionWithPrototype(Handle<String> name,
824 InstanceType type,
825 int instance_size,
826 Handle<JSObject> prototype,
827 Handle<Code> code,
828 bool force_initial_map) {
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000829 // Allocate the function.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000830 Handle<JSFunction> function = NewFunction(name, prototype);
831
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000832 // Set up the code pointer in both the shared function info and in
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000833 // the function itself.
834 function->shared()->set_code(*code);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000835 function->set_code(*code);
836
837 if (force_initial_map ||
838 type != JS_OBJECT_TYPE ||
839 instance_size != JSObject::kHeaderSize) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000840 Handle<Map> initial_map = NewMap(type,
841 instance_size,
svenpanne@chromium.org830d30c2012-05-29 13:20:14 +0000842 GetInitialFastElementsKind());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000843 function->set_initial_map(*initial_map);
844 initial_map->set_constructor(*function);
845 }
846
847 // Set function.prototype and give the prototype a constructor
848 // property that refers to the function.
849 SetPrototypeProperty(function, prototype);
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +0000850 // Currently safe because it is only invoked from Genesis.
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000851 CHECK_NOT_EMPTY_HANDLE(isolate(),
852 JSObject::SetLocalPropertyIgnoreAttributes(
853 prototype, constructor_symbol(),
854 function, DONT_ENUM));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000855 return function;
856}
857
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000858
kmillikin@chromium.org4111b802010-05-03 10:34:42 +0000859Handle<JSFunction> Factory::NewFunctionWithoutPrototype(Handle<String> name,
860 Handle<Code> code) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000861 Handle<JSFunction> function = NewFunctionWithoutPrototype(name,
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000862 CLASSIC_MODE);
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000863 function->shared()->set_code(*code);
kmillikin@chromium.org4111b802010-05-03 10:34:42 +0000864 function->set_code(*code);
865 ASSERT(!function->has_initial_map());
866 ASSERT(!function->has_prototype());
867 return function;
868}
869
870
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000871Handle<ScopeInfo> Factory::NewScopeInfo(int length) {
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000872 CALL_HEAP_FUNCTION(
873 isolate(),
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000874 isolate()->heap()->AllocateScopeInfo(length),
875 ScopeInfo);
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000876}
877
878
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +0000879Handle<JSObject> Factory::NewExternal(void* value) {
880 CALL_HEAP_FUNCTION(isolate(),
881 isolate()->heap()->AllocateExternal(value),
882 JSObject);
883}
884
885
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000886Handle<Code> Factory::NewCode(const CodeDesc& desc,
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000887 Code::Flags flags,
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +0000888 Handle<Object> self_ref,
889 bool immovable) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000890 CALL_HEAP_FUNCTION(isolate(),
891 isolate()->heap()->CreateCode(
892 desc, flags, self_ref, immovable),
893 Code);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000894}
895
896
897Handle<Code> Factory::CopyCode(Handle<Code> code) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000898 CALL_HEAP_FUNCTION(isolate(),
899 isolate()->heap()->CopyCode(*code),
900 Code);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000901}
902
903
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000904Handle<Code> Factory::CopyCode(Handle<Code> code, Vector<byte> reloc_info) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000905 CALL_HEAP_FUNCTION(isolate(),
906 isolate()->heap()->CopyCode(*code, reloc_info),
907 Code);
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000908}
909
910
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000911Handle<String> Factory::SymbolFromString(Handle<String> value) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000912 CALL_HEAP_FUNCTION(isolate(),
913 isolate()->heap()->LookupSymbol(*value), String);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000914}
915
916
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000917Handle<JSObject> Factory::NewJSObject(Handle<JSFunction> constructor,
918 PretenureFlag pretenure) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000919 CALL_HEAP_FUNCTION(
920 isolate(),
921 isolate()->heap()->AllocateJSObject(*constructor, pretenure), JSObject);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000922}
923
924
danno@chromium.org81cac2b2012-07-10 11:28:27 +0000925Handle<JSModule> Factory::NewJSModule(Handle<Context> context,
926 Handle<ScopeInfo> scope_info) {
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000927 CALL_HEAP_FUNCTION(
928 isolate(),
danno@chromium.org81cac2b2012-07-10 11:28:27 +0000929 isolate()->heap()->AllocateJSModule(*context, *scope_info), JSModule);
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000930}
931
932
kasperl@chromium.org2abc4502009-07-02 07:00:29 +0000933Handle<GlobalObject> Factory::NewGlobalObject(
934 Handle<JSFunction> constructor) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000935 CALL_HEAP_FUNCTION(isolate(),
936 isolate()->heap()->AllocateGlobalObject(*constructor),
kasperl@chromium.org2abc4502009-07-02 07:00:29 +0000937 GlobalObject);
938}
939
940
941
ager@chromium.org236ad962008-09-25 09:45:57 +0000942Handle<JSObject> Factory::NewJSObjectFromMap(Handle<Map> map) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000943 CALL_HEAP_FUNCTION(
944 isolate(),
945 isolate()->heap()->AllocateJSObjectFromMap(*map, NOT_TENURED),
946 JSObject);
ager@chromium.org236ad962008-09-25 09:45:57 +0000947}
948
949
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +0000950Handle<JSArray> Factory::NewJSArray(int capacity,
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000951 ElementsKind elements_kind,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000952 PretenureFlag pretenure) {
ulan@chromium.org8e8d8822012-11-23 14:36:46 +0000953 if (capacity != 0) {
954 elements_kind = GetHoleyElementsKind(elements_kind);
955 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000956 CALL_HEAP_FUNCTION(isolate(),
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000957 isolate()->heap()->AllocateJSArrayAndStorage(
958 elements_kind,
959 0,
960 capacity,
961 INITIALIZE_ARRAY_ELEMENTS_WITH_HOLE,
962 pretenure),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000963 JSArray);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000964}
965
966
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +0000967Handle<JSArray> Factory::NewJSArrayWithElements(Handle<FixedArrayBase> elements,
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000968 ElementsKind elements_kind,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000969 PretenureFlag pretenure) {
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000970 CALL_HEAP_FUNCTION(
971 isolate(),
972 isolate()->heap()->AllocateJSArrayWithElements(*elements,
973 elements_kind,
yangguo@chromium.orgfb377212012-11-16 14:43:43 +0000974 elements->length(),
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000975 pretenure),
976 JSArray);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000977}
978
979
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +0000980void Factory::SetElementsCapacityAndLength(Handle<JSArray> array,
981 int capacity,
982 int length) {
983 ElementsAccessor* accessor = array->GetElementsAccessor();
984 CALL_HEAP_FUNCTION_VOID(
985 isolate(),
986 accessor->SetCapacityAndLength(*array, capacity, length));
987}
988
989
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000990void Factory::SetContent(Handle<JSArray> array,
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +0000991 Handle<FixedArrayBase> elements) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000992 CALL_HEAP_FUNCTION_VOID(
993 isolate(),
994 array->SetContent(*elements));
995}
996
997
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +0000998void Factory::EnsureCanContainHeapObjectElements(Handle<JSArray> array) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000999 CALL_HEAP_FUNCTION_VOID(
1000 isolate(),
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00001001 array->EnsureCanContainHeapObjectElements());
1002}
1003
1004
1005void Factory::EnsureCanContainElements(Handle<JSArray> array,
1006 Handle<FixedArrayBase> elements,
svenpanne@chromium.org830d30c2012-05-29 13:20:14 +00001007 uint32_t length,
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00001008 EnsureElementsMode mode) {
1009 CALL_HEAP_FUNCTION_VOID(
1010 isolate(),
svenpanne@chromium.org830d30c2012-05-29 13:20:14 +00001011 array->EnsureCanContainElements(*elements, length, mode));
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001012}
1013
1014
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001015Handle<JSProxy> Factory::NewJSProxy(Handle<Object> handler,
1016 Handle<Object> prototype) {
1017 CALL_HEAP_FUNCTION(
1018 isolate(),
1019 isolate()->heap()->AllocateJSProxy(*handler, *prototype),
1020 JSProxy);
1021}
1022
1023
lrn@chromium.org34e60782011-09-15 07:25:40 +00001024void Factory::BecomeJSObject(Handle<JSReceiver> object) {
rossberg@chromium.org717967f2011-07-20 13:44:42 +00001025 CALL_HEAP_FUNCTION_VOID(
1026 isolate(),
lrn@chromium.org34e60782011-09-15 07:25:40 +00001027 isolate()->heap()->ReinitializeJSReceiver(
1028 *object, JS_OBJECT_TYPE, JSObject::kHeaderSize));
1029}
1030
1031
1032void Factory::BecomeJSFunction(Handle<JSReceiver> object) {
1033 CALL_HEAP_FUNCTION_VOID(
1034 isolate(),
1035 isolate()->heap()->ReinitializeJSReceiver(
1036 *object, JS_FUNCTION_TYPE, JSFunction::kSize));
rossberg@chromium.org717967f2011-07-20 13:44:42 +00001037}
1038
1039
jkummerow@chromium.org000f7fb2012-08-01 11:14:42 +00001040void Factory::SetIdentityHash(Handle<JSObject> object, Smi* hash) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001041 CALL_HEAP_FUNCTION_VOID(
1042 isolate(),
1043 object->SetIdentityHash(hash, ALLOW_CREATION));
1044}
1045
1046
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001047Handle<SharedFunctionInfo> Factory::NewSharedFunctionInfo(
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00001048 Handle<String> name,
1049 int number_of_literals,
1050 Handle<Code> code,
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001051 Handle<ScopeInfo> scope_info) {
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001052 Handle<SharedFunctionInfo> shared = NewSharedFunctionInfo(name);
1053 shared->set_code(*code);
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00001054 shared->set_scope_info(*scope_info);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001055 int literals_array_size = number_of_literals;
1056 // If the function contains object, regexp or array literals,
1057 // allocate extra space for a literals array prefix containing the
1058 // context.
1059 if (number_of_literals > 0) {
1060 literals_array_size += JSFunction::kLiteralsPrefixSize;
1061 }
1062 shared->set_num_literals(literals_array_size);
1063 return shared;
1064}
1065
1066
kmillikin@chromium.org31b12772011-02-02 16:08:26 +00001067Handle<JSMessageObject> Factory::NewJSMessageObject(
1068 Handle<String> type,
1069 Handle<JSArray> arguments,
1070 int start_position,
1071 int end_position,
1072 Handle<Object> script,
1073 Handle<Object> stack_trace,
1074 Handle<Object> stack_frames) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001075 CALL_HEAP_FUNCTION(isolate(),
1076 isolate()->heap()->AllocateJSMessageObject(*type,
1077 *arguments,
1078 start_position,
1079 end_position,
1080 *script,
1081 *stack_trace,
1082 *stack_frames),
kmillikin@chromium.org31b12772011-02-02 16:08:26 +00001083 JSMessageObject);
1084}
1085
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001086Handle<SharedFunctionInfo> Factory::NewSharedFunctionInfo(Handle<String> name) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001087 CALL_HEAP_FUNCTION(isolate(),
1088 isolate()->heap()->AllocateSharedFunctionInfo(*name),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001089 SharedFunctionInfo);
1090}
1091
1092
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00001093Handle<String> Factory::NumberToString(Handle<Object> number) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001094 CALL_HEAP_FUNCTION(isolate(),
1095 isolate()->heap()->NumberToString(*number), String);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00001096}
1097
1098
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001099Handle<String> Factory::Uint32ToString(uint32_t value) {
1100 CALL_HEAP_FUNCTION(isolate(),
1101 isolate()->heap()->Uint32ToString(value), String);
1102}
1103
1104
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001105Handle<SeededNumberDictionary> Factory::DictionaryAtNumberPut(
1106 Handle<SeededNumberDictionary> dictionary,
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00001107 uint32_t key,
1108 Handle<Object> value) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001109 CALL_HEAP_FUNCTION(isolate(),
1110 dictionary->AtNumberPut(key, *value),
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001111 SeededNumberDictionary);
1112}
1113
1114
1115Handle<UnseededNumberDictionary> Factory::DictionaryAtNumberPut(
1116 Handle<UnseededNumberDictionary> dictionary,
1117 uint32_t key,
1118 Handle<Object> value) {
1119 CALL_HEAP_FUNCTION(isolate(),
1120 dictionary->AtNumberPut(key, *value),
1121 UnseededNumberDictionary);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001122}
1123
1124
1125Handle<JSFunction> Factory::NewFunctionHelper(Handle<String> name,
1126 Handle<Object> prototype) {
1127 Handle<SharedFunctionInfo> function_share = NewSharedFunctionInfo(name);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001128 CALL_HEAP_FUNCTION(
1129 isolate(),
1130 isolate()->heap()->AllocateFunction(*isolate()->function_map(),
1131 *function_share,
1132 *prototype),
1133 JSFunction);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001134}
1135
1136
1137Handle<JSFunction> Factory::NewFunction(Handle<String> name,
1138 Handle<Object> prototype) {
1139 Handle<JSFunction> fun = NewFunctionHelper(name, prototype);
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001140 fun->set_context(isolate()->context()->native_context());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001141 return fun;
1142}
1143
1144
kmillikin@chromium.org4111b802010-05-03 10:34:42 +00001145Handle<JSFunction> Factory::NewFunctionWithoutPrototypeHelper(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001146 Handle<String> name,
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00001147 LanguageMode language_mode) {
kmillikin@chromium.org4111b802010-05-03 10:34:42 +00001148 Handle<SharedFunctionInfo> function_share = NewSharedFunctionInfo(name);
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00001149 Handle<Map> map = (language_mode == CLASSIC_MODE)
1150 ? isolate()->function_without_prototype_map()
1151 : isolate()->strict_mode_function_without_prototype_map();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001152 CALL_HEAP_FUNCTION(isolate(),
1153 isolate()->heap()->AllocateFunction(
1154 *map,
kmillikin@chromium.org4111b802010-05-03 10:34:42 +00001155 *function_share,
1156 *the_hole_value()),
1157 JSFunction);
1158}
1159
1160
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001161Handle<JSFunction> Factory::NewFunctionWithoutPrototype(
1162 Handle<String> name,
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00001163 LanguageMode language_mode) {
1164 Handle<JSFunction> fun =
1165 NewFunctionWithoutPrototypeHelper(name, language_mode);
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001166 fun->set_context(isolate()->context()->native_context());
kmillikin@chromium.org4111b802010-05-03 10:34:42 +00001167 return fun;
1168}
1169
1170
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001171Handle<Object> Factory::ToObject(Handle<Object> object) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001172 CALL_HEAP_FUNCTION(isolate(), object->ToObject(), Object);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001173}
1174
1175
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001176Handle<Object> Factory::ToObject(Handle<Object> object,
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001177 Handle<Context> native_context) {
1178 CALL_HEAP_FUNCTION(isolate(), object->ToObject(*native_context), Object);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001179}
1180
1181
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001182#ifdef ENABLE_DEBUGGER_SUPPORT
v8.team.kasperl727e9952008-09-02 14:56:44 +00001183Handle<DebugInfo> Factory::NewDebugInfo(Handle<SharedFunctionInfo> shared) {
1184 // Get the original code of the function.
1185 Handle<Code> code(shared->code());
1186
1187 // Create a copy of the code before allocating the debug info object to avoid
1188 // allocation while setting up the debug info object.
1189 Handle<Code> original_code(*Factory::CopyCode(code));
1190
1191 // Allocate initial fixed array for active break points before allocating the
1192 // debug info object to avoid allocation while setting up the debug info
1193 // object.
1194 Handle<FixedArray> break_points(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001195 NewFixedArray(Debug::kEstimatedNofBreakPointsInFunction));
v8.team.kasperl727e9952008-09-02 14:56:44 +00001196
1197 // Create and set up the debug info object. Debug info contains function, a
1198 // copy of the original code, the executing code and initial fixed array for
1199 // active break points.
1200 Handle<DebugInfo> debug_info =
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001201 Handle<DebugInfo>::cast(NewStruct(DEBUG_INFO_TYPE));
v8.team.kasperl727e9952008-09-02 14:56:44 +00001202 debug_info->set_shared(*shared);
1203 debug_info->set_original_code(*original_code);
1204 debug_info->set_code(*code);
1205 debug_info->set_break_points(*break_points);
1206
1207 // Link debug info to function.
1208 shared->set_debug_info(*debug_info);
1209
1210 return debug_info;
1211}
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001212#endif
v8.team.kasperl727e9952008-09-02 14:56:44 +00001213
1214
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001215Handle<JSObject> Factory::NewArgumentsObject(Handle<Object> callee,
1216 int length) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001217 CALL_HEAP_FUNCTION(
1218 isolate(),
1219 isolate()->heap()->AllocateArgumentsObject(*callee, length), JSObject);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001220}
1221
1222
1223Handle<JSFunction> Factory::CreateApiFunction(
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001224 Handle<FunctionTemplateInfo> obj, ApiInstanceType instance_type) {
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00001225 Handle<Code> code = isolate()->builtins()->HandleApiCall();
1226 Handle<Code> construct_stub = isolate()->builtins()->JSConstructStubApi();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001227
kasper.lund212ac232008-07-16 07:07:30 +00001228 int internal_field_count = 0;
1229 if (!obj->instance_template()->IsUndefined()) {
1230 Handle<ObjectTemplateInfo> instance_template =
1231 Handle<ObjectTemplateInfo>(
1232 ObjectTemplateInfo::cast(obj->instance_template()));
1233 internal_field_count =
1234 Smi::cast(instance_template->internal_field_count())->value();
1235 }
1236
1237 int instance_size = kPointerSize * internal_field_count;
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001238 InstanceType type = INVALID_TYPE;
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001239 switch (instance_type) {
1240 case JavaScriptObject:
1241 type = JS_OBJECT_TYPE;
1242 instance_size += JSObject::kHeaderSize;
1243 break;
1244 case InnerGlobalObject:
1245 type = JS_GLOBAL_OBJECT_TYPE;
1246 instance_size += JSGlobalObject::kSize;
1247 break;
1248 case OuterGlobalObject:
1249 type = JS_GLOBAL_PROXY_TYPE;
1250 instance_size += JSGlobalProxy::kSize;
1251 break;
1252 default:
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001253 break;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001254 }
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001255 ASSERT(type != INVALID_TYPE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001256
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001257 Handle<JSFunction> result =
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001258 NewFunction(Factory::empty_symbol(),
1259 type,
1260 instance_size,
1261 code,
1262 true);
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00001263
1264 // Set length.
1265 result->shared()->set_length(obj->length());
1266
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001267 // Set class name.
1268 Handle<Object> class_name = Handle<Object>(obj->class_name());
1269 if (class_name->IsString()) {
1270 result->shared()->set_instance_class_name(*class_name);
1271 result->shared()->set_name(*class_name);
1272 }
1273
1274 Handle<Map> map = Handle<Map>(result->initial_map());
1275
1276 // Mark as undetectable if needed.
1277 if (obj->undetectable()) {
1278 map->set_is_undetectable();
1279 }
1280
1281 // Mark as hidden for the __proto__ accessor if needed.
1282 if (obj->hidden_prototype()) {
1283 map->set_is_hidden_prototype();
1284 }
1285
1286 // Mark as needs_access_check if needed.
1287 if (obj->needs_access_check()) {
ager@chromium.org870a0b62008-11-04 11:43:05 +00001288 map->set_is_access_check_needed(true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001289 }
1290
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001291 // Set interceptor information in the map.
1292 if (!obj->named_property_handler()->IsUndefined()) {
1293 map->set_has_named_interceptor();
1294 }
1295 if (!obj->indexed_property_handler()->IsUndefined()) {
1296 map->set_has_indexed_interceptor();
1297 }
1298
1299 // Set instance call-as-function information in the map.
1300 if (!obj->instance_call_handler()->IsUndefined()) {
1301 map->set_has_instance_call_handler();
1302 }
1303
1304 result->shared()->set_function_data(*obj);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001305 result->shared()->set_construct_stub(*construct_stub);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001306 result->shared()->DontAdaptArguments();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001307
1308 // Recursively copy parent templates' accessors, 'data' may be modified.
verwaest@chromium.org33e09c82012-10-10 17:07:22 +00001309 int max_number_of_additional_properties = 0;
1310 FunctionTemplateInfo* info = *obj;
1311 while (true) {
1312 Object* props = info->property_accessors();
1313 if (!props->IsUndefined()) {
1314 Handle<Object> props_handle(props);
1315 NeanderArray props_array(props_handle);
1316 max_number_of_additional_properties += props_array.length();
1317 }
1318 Object* parent = info->parent_template();
1319 if (parent->IsUndefined()) break;
1320 info = FunctionTemplateInfo::cast(parent);
1321 }
1322
1323 Map::EnsureDescriptorSlack(map, max_number_of_additional_properties);
1324
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001325 while (true) {
1326 Handle<Object> props = Handle<Object>(obj->property_accessors());
1327 if (!props->IsUndefined()) {
verwaest@chromium.org33e09c82012-10-10 17:07:22 +00001328 Map::AppendCallbackDescriptors(map, props);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001329 }
1330 Handle<Object> parent = Handle<Object>(obj->parent_template());
1331 if (parent->IsUndefined()) break;
1332 obj = Handle<FunctionTemplateInfo>::cast(parent);
1333 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001334
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001335 ASSERT(result->shared()->IsApiFunction());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001336 return result;
1337}
1338
1339
ager@chromium.org236ad962008-09-25 09:45:57 +00001340Handle<MapCache> Factory::NewMapCache(int at_least_space_for) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001341 CALL_HEAP_FUNCTION(isolate(),
1342 MapCache::Allocate(at_least_space_for), MapCache);
ager@chromium.org236ad962008-09-25 09:45:57 +00001343}
1344
1345
lrn@chromium.org303ada72010-10-27 09:33:13 +00001346MUST_USE_RESULT static MaybeObject* UpdateMapCacheWith(Context* context,
1347 FixedArray* keys,
1348 Map* map) {
1349 Object* result;
1350 { MaybeObject* maybe_result =
1351 MapCache::cast(context->map_cache())->Put(keys, map);
1352 if (!maybe_result->ToObject(&result)) return maybe_result;
1353 }
1354 context->set_map_cache(MapCache::cast(result));
ager@chromium.org236ad962008-09-25 09:45:57 +00001355 return result;
1356}
1357
1358
1359Handle<MapCache> Factory::AddToMapCache(Handle<Context> context,
1360 Handle<FixedArray> keys,
1361 Handle<Map> map) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001362 CALL_HEAP_FUNCTION(isolate(),
1363 UpdateMapCacheWith(*context, *keys, *map), MapCache);
ager@chromium.org236ad962008-09-25 09:45:57 +00001364}
1365
1366
1367Handle<Map> Factory::ObjectLiteralMapFromCache(Handle<Context> context,
1368 Handle<FixedArray> keys) {
1369 if (context->map_cache()->IsUndefined()) {
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001370 // Allocate the new map cache for the native context.
ager@chromium.org236ad962008-09-25 09:45:57 +00001371 Handle<MapCache> new_cache = NewMapCache(24);
1372 context->set_map_cache(*new_cache);
1373 }
ager@chromium.org32912102009-01-16 10:38:43 +00001374 // Check to see whether there is a matching element in the cache.
ager@chromium.org236ad962008-09-25 09:45:57 +00001375 Handle<MapCache> cache =
1376 Handle<MapCache>(MapCache::cast(context->map_cache()));
yangguo@chromium.orgfb377212012-11-16 14:43:43 +00001377 Handle<Object> result = Handle<Object>(cache->Lookup(*keys), isolate());
ager@chromium.org236ad962008-09-25 09:45:57 +00001378 if (result->IsMap()) return Handle<Map>::cast(result);
1379 // Create a new map and add it to the cache.
1380 Handle<Map> map =
ager@chromium.org32912102009-01-16 10:38:43 +00001381 CopyMap(Handle<Map>(context->object_function()->initial_map()),
1382 keys->length());
ager@chromium.org236ad962008-09-25 09:45:57 +00001383 AddToMapCache(context, keys, map);
1384 return Handle<Map>(map);
1385}
1386
1387
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001388void Factory::SetRegExpAtomData(Handle<JSRegExp> regexp,
1389 JSRegExp::Type type,
1390 Handle<String> source,
1391 JSRegExp::Flags flags,
1392 Handle<Object> data) {
1393 Handle<FixedArray> store = NewFixedArray(JSRegExp::kAtomDataSize);
1394
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001395 store->set(JSRegExp::kTagIndex, Smi::FromInt(type));
1396 store->set(JSRegExp::kSourceIndex, *source);
1397 store->set(JSRegExp::kFlagsIndex, Smi::FromInt(flags.value()));
1398 store->set(JSRegExp::kAtomPatternIndex, *data);
1399 regexp->set_data(*store);
1400}
1401
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001402void Factory::SetRegExpIrregexpData(Handle<JSRegExp> regexp,
1403 JSRegExp::Type type,
1404 Handle<String> source,
1405 JSRegExp::Flags flags,
1406 int capture_count) {
1407 Handle<FixedArray> store = NewFixedArray(JSRegExp::kIrregexpDataSize);
jkummerow@chromium.orgddda9e82011-07-06 11:27:02 +00001408 Smi* uninitialized = Smi::FromInt(JSRegExp::kUninitializedValue);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001409 store->set(JSRegExp::kTagIndex, Smi::FromInt(type));
1410 store->set(JSRegExp::kSourceIndex, *source);
1411 store->set(JSRegExp::kFlagsIndex, Smi::FromInt(flags.value()));
jkummerow@chromium.orgddda9e82011-07-06 11:27:02 +00001412 store->set(JSRegExp::kIrregexpASCIICodeIndex, uninitialized);
1413 store->set(JSRegExp::kIrregexpUC16CodeIndex, uninitialized);
1414 store->set(JSRegExp::kIrregexpASCIICodeSavedIndex, uninitialized);
1415 store->set(JSRegExp::kIrregexpUC16CodeSavedIndex, uninitialized);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001416 store->set(JSRegExp::kIrregexpMaxRegisterCountIndex, Smi::FromInt(0));
1417 store->set(JSRegExp::kIrregexpCaptureCountIndex,
1418 Smi::FromInt(capture_count));
1419 regexp->set_data(*store);
1420}
1421
1422
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001423
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001424void Factory::ConfigureInstance(Handle<FunctionTemplateInfo> desc,
1425 Handle<JSObject> instance,
1426 bool* pending_exception) {
1427 // Configure the instance by adding the properties specified by the
1428 // instance template.
yangguo@chromium.orgfb377212012-11-16 14:43:43 +00001429 Handle<Object> instance_template(desc->instance_template(), isolate());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001430 if (!instance_template->IsUndefined()) {
1431 Execution::ConfigureInstance(instance,
1432 instance_template,
1433 pending_exception);
1434 } else {
1435 *pending_exception = false;
1436 }
1437}
1438
1439
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001440Handle<Object> Factory::GlobalConstantFor(Handle<String> name) {
1441 Heap* h = isolate()->heap();
1442 if (name->Equals(h->undefined_symbol())) return undefined_value();
1443 if (name->Equals(h->nan_symbol())) return nan_value();
1444 if (name->Equals(h->infinity_symbol())) return infinity_value();
1445 return Handle<Object>::null();
1446}
1447
1448
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00001449Handle<Object> Factory::ToBoolean(bool value) {
1450 return Handle<Object>(value
1451 ? isolate()->heap()->true_value()
1452 : isolate()->heap()->false_value());
1453}
1454
1455
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001456} } // namespace v8::internal