blob: 8340b4691e5c9bae11edeb1db0e7e4f0e4632f63 [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(),
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000118 DescriptorArray::Allocate(number_of_descriptors,
119 DescriptorArray::MAY_BE_SHARED),
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).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000161Handle<String> Factory::LookupSymbol(Vector<const char> string) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000162 CALL_HEAP_FUNCTION(isolate(),
163 isolate()->heap()->LookupSymbol(string),
164 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
fschneider@chromium.org9e3e0b62011-01-03 10:16:46 +0000174Handle<String> Factory::LookupAsciiSymbol(Vector<const char> string) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000175 CALL_HEAP_FUNCTION(isolate(),
176 isolate()->heap()->LookupAsciiSymbol(string),
177 String);
fschneider@chromium.org9e3e0b62011-01-03 10:16:46 +0000178}
179
danno@chromium.org40cb8782011-05-25 07:58:50 +0000180
181Handle<String> Factory::LookupAsciiSymbol(Handle<SeqAsciiString> string,
182 int from,
183 int length) {
184 CALL_HEAP_FUNCTION(isolate(),
185 isolate()->heap()->LookupAsciiSymbol(string,
186 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
199Handle<String> Factory::NewStringFromAscii(Vector<const char> string,
200 PretenureFlag pretenure) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000201 CALL_HEAP_FUNCTION(
202 isolate(),
203 isolate()->heap()->AllocateStringFromAscii(string, pretenure),
204 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
ager@chromium.org04921a82011-06-27 13:21:41 +0000225Handle<SeqAsciiString> Factory::NewRawAsciiString(int length,
226 PretenureFlag pretenure) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000227 CALL_HEAP_FUNCTION(
228 isolate(),
229 isolate()->heap()->AllocateRawAsciiString(length, pretenure),
ager@chromium.org04921a82011-06-27 13:21:41 +0000230 SeqAsciiString);
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
288Handle<Context> Factory::NewGlobalContext() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000289 CALL_HEAP_FUNCTION(
290 isolate(),
291 isolate()->heap()->AllocateGlobalContext(),
292 Context);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000293}
294
295
danno@chromium.org81cac2b2012-07-10 11:28:27 +0000296Handle<Context> Factory::NewModuleContext(Handle<ScopeInfo> scope_info) {
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000297 CALL_HEAP_FUNCTION(
298 isolate(),
danno@chromium.org81cac2b2012-07-10 11:28:27 +0000299 isolate()->heap()->AllocateModuleContext(*scope_info),
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000300 Context);
301}
302
303
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000304Handle<Context> Factory::NewFunctionContext(int length,
vegorov@chromium.org3cf47312011-06-29 13:20:01 +0000305 Handle<JSFunction> function) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000306 CALL_HEAP_FUNCTION(
307 isolate(),
vegorov@chromium.org3cf47312011-06-29 13:20:01 +0000308 isolate()->heap()->AllocateFunctionContext(length, *function),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000309 Context);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000310}
311
312
vegorov@chromium.org3cf47312011-06-29 13:20:01 +0000313Handle<Context> Factory::NewCatchContext(Handle<JSFunction> function,
314 Handle<Context> previous,
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000315 Handle<String> name,
316 Handle<Object> thrown_object) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000317 CALL_HEAP_FUNCTION(
318 isolate(),
vegorov@chromium.org3cf47312011-06-29 13:20:01 +0000319 isolate()->heap()->AllocateCatchContext(*function,
320 *previous,
321 *name,
322 *thrown_object),
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000323 Context);
324}
325
326
vegorov@chromium.org3cf47312011-06-29 13:20:01 +0000327Handle<Context> Factory::NewWithContext(Handle<JSFunction> function,
328 Handle<Context> previous,
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000329 Handle<JSObject> extension) {
330 CALL_HEAP_FUNCTION(
331 isolate(),
vegorov@chromium.org3cf47312011-06-29 13:20:01 +0000332 isolate()->heap()->AllocateWithContext(*function, *previous, *extension),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000333 Context);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000334}
335
336
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000337Handle<Context> Factory::NewBlockContext(Handle<JSFunction> function,
338 Handle<Context> previous,
339 Handle<ScopeInfo> scope_info) {
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000340 CALL_HEAP_FUNCTION(
341 isolate(),
342 isolate()->heap()->AllocateBlockContext(*function,
343 *previous,
344 *scope_info),
345 Context);
346}
347
348
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000349Handle<Struct> Factory::NewStruct(InstanceType type) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000350 CALL_HEAP_FUNCTION(
351 isolate(),
352 isolate()->heap()->AllocateStruct(type),
353 Struct);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000354}
355
356
357Handle<AccessorInfo> Factory::NewAccessorInfo() {
358 Handle<AccessorInfo> info =
359 Handle<AccessorInfo>::cast(NewStruct(ACCESSOR_INFO_TYPE));
360 info->set_flag(0); // Must clear the flag, it was initialized as undefined.
361 return info;
362}
363
364
365Handle<Script> Factory::NewScript(Handle<String> source) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000366 // Generate id for this script.
367 int id;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000368 Heap* heap = isolate()->heap();
369 if (heap->last_script_id()->IsUndefined()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000370 // Script ids start from one.
371 id = 1;
372 } else {
373 // Increment id, wrap when positive smi is exhausted.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000374 id = Smi::cast(heap->last_script_id())->value();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000375 id++;
376 if (!Smi::IsValid(id)) {
377 id = 0;
378 }
379 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000380 heap->SetLastScriptId(Smi::FromInt(id));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000381
382 // Create and initialize script object.
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000383 Handle<Foreign> wrapper = NewForeign(0, TENURED);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000384 Handle<Script> script = Handle<Script>::cast(NewStruct(SCRIPT_TYPE));
385 script->set_source(*source);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000386 script->set_name(heap->undefined_value());
387 script->set_id(heap->last_script_id());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000388 script->set_line_offset(Smi::FromInt(0));
389 script->set_column_offset(Smi::FromInt(0));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000390 script->set_data(heap->undefined_value());
391 script->set_context_data(heap->undefined_value());
ager@chromium.orge2902be2009-06-08 12:21:35 +0000392 script->set_type(Smi::FromInt(Script::TYPE_NORMAL));
393 script->set_compilation_type(Smi::FromInt(Script::COMPILATION_TYPE_HOST));
rossberg@chromium.org2c067b12012-03-19 11:01:52 +0000394 script->set_compilation_state(
395 Smi::FromInt(Script::COMPILATION_STATE_INITIAL));
ager@chromium.org9085a012009-05-11 19:22:57 +0000396 script->set_wrapper(*wrapper);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000397 script->set_line_ends(heap->undefined_value());
398 script->set_eval_from_shared(heap->undefined_value());
ager@chromium.orge2902be2009-06-08 12:21:35 +0000399 script->set_eval_from_instructions_offset(Smi::FromInt(0));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000400
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000401 return script;
402}
403
404
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000405Handle<Foreign> Factory::NewForeign(Address addr, PretenureFlag pretenure) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000406 CALL_HEAP_FUNCTION(isolate(),
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000407 isolate()->heap()->AllocateForeign(addr, pretenure),
408 Foreign);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000409}
410
411
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000412Handle<Foreign> Factory::NewForeign(const AccessorDescriptor* desc) {
413 return NewForeign((Address) desc, TENURED);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000414}
415
416
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000417Handle<ByteArray> Factory::NewByteArray(int length, PretenureFlag pretenure) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000418 ASSERT(0 <= length);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000419 CALL_HEAP_FUNCTION(
420 isolate(),
421 isolate()->heap()->AllocateByteArray(length, pretenure),
422 ByteArray);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000423}
424
425
ager@chromium.org3811b432009-10-28 14:53:37 +0000426Handle<ExternalArray> Factory::NewExternalArray(int length,
427 ExternalArrayType array_type,
428 void* external_pointer,
429 PretenureFlag pretenure) {
430 ASSERT(0 <= length);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000431 CALL_HEAP_FUNCTION(
432 isolate(),
433 isolate()->heap()->AllocateExternalArray(length,
434 array_type,
435 external_pointer,
436 pretenure),
437 ExternalArray);
ager@chromium.org3811b432009-10-28 14:53:37 +0000438}
439
440
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000441Handle<JSGlobalPropertyCell> Factory::NewJSGlobalPropertyCell(
442 Handle<Object> value) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000443 CALL_HEAP_FUNCTION(
444 isolate(),
445 isolate()->heap()->AllocateJSGlobalPropertyCell(*value),
446 JSGlobalPropertyCell);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000447}
448
449
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000450Handle<Map> Factory::NewMap(InstanceType type,
451 int instance_size,
452 ElementsKind elements_kind) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000453 CALL_HEAP_FUNCTION(
454 isolate(),
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000455 isolate()->heap()->AllocateMap(type, instance_size, elements_kind),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000456 Map);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000457}
458
459
460Handle<JSObject> Factory::NewFunctionPrototype(Handle<JSFunction> function) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000461 CALL_HEAP_FUNCTION(
462 isolate(),
463 isolate()->heap()->AllocateFunctionPrototype(*function),
464 JSObject);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000465}
466
467
rossberg@chromium.org657d53b2012-07-12 11:06:03 +0000468Handle<Map> Factory::CopyWithPreallocatedFieldDescriptors(Handle<Map> src) {
469 CALL_HEAP_FUNCTION(
470 isolate(), src->CopyWithPreallocatedFieldDescriptors(), Map);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000471}
472
473
ager@chromium.org32912102009-01-16 10:38:43 +0000474Handle<Map> Factory::CopyMap(Handle<Map> src,
475 int extra_inobject_properties) {
rossberg@chromium.org657d53b2012-07-12 11:06:03 +0000476 Handle<Map> copy = CopyWithPreallocatedFieldDescriptors(src);
ager@chromium.org32912102009-01-16 10:38:43 +0000477 // Check that we do not overflow the instance size when adding the
478 // extra inobject properties.
479 int instance_size_delta = extra_inobject_properties * kPointerSize;
480 int max_instance_size_delta =
481 JSObject::kMaxInstanceSize - copy->instance_size();
482 if (instance_size_delta > max_instance_size_delta) {
483 // If the instance size overflows, we allocate as many properties
484 // as we can as inobject properties.
485 instance_size_delta = max_instance_size_delta;
486 extra_inobject_properties = max_instance_size_delta >> kPointerSizeLog2;
487 }
488 // Adjust the map with the extra inobject properties.
489 int inobject_properties =
490 copy->inobject_properties() + extra_inobject_properties;
491 copy->set_inobject_properties(inobject_properties);
492 copy->set_unused_property_fields(inobject_properties);
493 copy->set_instance_size(copy->instance_size() + instance_size_delta);
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000494 copy->set_visitor_id(StaticVisitorBase::GetVisitorId(*copy));
ager@chromium.org32912102009-01-16 10:38:43 +0000495 return copy;
496}
497
fschneider@chromium.org40b9da32010-06-28 11:29:21 +0000498
verwaest@chromium.org753aee42012-07-17 16:15:42 +0000499Handle<Map> Factory::CopyMap(Handle<Map> src) {
500 CALL_HEAP_FUNCTION(isolate(), src->Copy(DescriptorArray::MAY_BE_SHARED), Map);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000501}
502
503
kmillikin@chromium.org83e16822011-09-13 08:21:47 +0000504Handle<Map> Factory::GetElementsTransitionMap(
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000505 Handle<JSObject> src,
506 ElementsKind elements_kind) {
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000507 Isolate* i = isolate();
508 CALL_HEAP_FUNCTION(i,
509 src->GetElementsTransitionMap(i, elements_kind),
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000510 Map);
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +0000511}
512
513
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000514Handle<FixedArray> Factory::CopyFixedArray(Handle<FixedArray> array) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000515 CALL_HEAP_FUNCTION(isolate(), array->Copy(), FixedArray);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000516}
517
518
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000519Handle<FixedDoubleArray> Factory::CopyFixedDoubleArray(
520 Handle<FixedDoubleArray> array) {
521 CALL_HEAP_FUNCTION(isolate(), array->Copy(), FixedDoubleArray);
522}
523
524
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000525Handle<JSFunction> Factory::BaseNewFunctionFromSharedFunctionInfo(
526 Handle<SharedFunctionInfo> function_info,
sgjesse@chromium.org846fb742009-12-18 08:56:33 +0000527 Handle<Map> function_map,
528 PretenureFlag pretenure) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000529 CALL_HEAP_FUNCTION(
530 isolate(),
531 isolate()->heap()->AllocateFunction(*function_map,
532 *function_info,
533 isolate()->heap()->the_hole_value(),
534 pretenure),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000535 JSFunction);
536}
537
538
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000539Handle<JSFunction> Factory::NewFunctionFromSharedFunctionInfo(
540 Handle<SharedFunctionInfo> function_info,
sgjesse@chromium.org846fb742009-12-18 08:56:33 +0000541 Handle<Context> context,
542 PretenureFlag pretenure) {
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000543 Handle<JSFunction> result = BaseNewFunctionFromSharedFunctionInfo(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000544 function_info,
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000545 function_info->is_classic_mode()
546 ? isolate()->function_map()
547 : isolate()->strict_mode_function_map(),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000548 pretenure);
549
jkummerow@chromium.org1456e702012-03-30 08:38:13 +0000550 if (function_info->ic_age() != isolate()->heap()->global_ic_age()) {
551 function_info->ResetForNewContext(isolate()->heap()->global_ic_age());
552 }
553
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000554 result->set_context(*context);
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000555
ulan@chromium.orgd9e468a2012-06-25 09:47:40 +0000556 int index = function_info->SearchOptimizedCodeMap(context->global_context());
557 if (!function_info->bound() && index < 0) {
558 int number_of_literals = function_info->num_literals();
559 Handle<FixedArray> literals = NewFixedArray(number_of_literals, pretenure);
560 if (number_of_literals > 0) {
561 // Store the global context in the literals array prefix. This
562 // context will be used when creating object, regexp and array
563 // literals in this function.
564 literals->set(JSFunction::kLiteralGlobalContextIndex,
565 context->global_context());
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000566 }
ulan@chromium.orgd9e468a2012-06-25 09:47:40 +0000567 result->set_literals(*literals);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000568 }
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000569
570 if (index > 0) {
571 // Caching of optimized code enabled and optimized code found.
ulan@chromium.orgd9e468a2012-06-25 09:47:40 +0000572 function_info->InstallFromOptimizedCodeMap(*result, index);
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000573 return result;
574 }
575
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000576 if (V8::UseCrankshaft() &&
577 FLAG_always_opt &&
578 result->is_compiled() &&
579 !function_info->is_toplevel() &&
rossberg@chromium.org2c067b12012-03-19 11:01:52 +0000580 function_info->allows_lazy_compilation() &&
581 !function_info->optimization_disabled()) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000582 result->MarkForLazyRecompilation();
583 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000584 return result;
585}
586
587
588Handle<Object> Factory::NewNumber(double value,
589 PretenureFlag pretenure) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000590 CALL_HEAP_FUNCTION(
591 isolate(),
592 isolate()->heap()->NumberFromDouble(value, pretenure), Object);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000593}
594
595
erikcorry0ad885c2011-11-21 13:51:57 +0000596Handle<Object> Factory::NewNumberFromInt(int32_t value,
597 PretenureFlag pretenure) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000598 CALL_HEAP_FUNCTION(
599 isolate(),
erikcorry0ad885c2011-11-21 13:51:57 +0000600 isolate()->heap()->NumberFromInt32(value, pretenure), Object);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000601}
602
603
erikcorry0ad885c2011-11-21 13:51:57 +0000604Handle<Object> Factory::NewNumberFromUint(uint32_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()->NumberFromUint32(value, pretenure), Object);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000609}
610
611
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000612Handle<JSObject> Factory::NewNeanderObject() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000613 CALL_HEAP_FUNCTION(
614 isolate(),
615 isolate()->heap()->AllocateJSObjectFromMap(
616 isolate()->heap()->neander_map()),
617 JSObject);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000618}
619
620
621Handle<Object> Factory::NewTypeError(const char* type,
622 Vector< Handle<Object> > args) {
623 return NewError("MakeTypeError", type, args);
624}
625
626
627Handle<Object> Factory::NewTypeError(Handle<String> message) {
628 return NewError("$TypeError", message);
629}
630
631
632Handle<Object> Factory::NewRangeError(const char* type,
633 Vector< Handle<Object> > args) {
634 return NewError("MakeRangeError", type, args);
635}
636
637
638Handle<Object> Factory::NewRangeError(Handle<String> message) {
639 return NewError("$RangeError", message);
640}
641
642
643Handle<Object> Factory::NewSyntaxError(const char* type, Handle<JSArray> args) {
644 return NewError("MakeSyntaxError", type, args);
645}
646
647
648Handle<Object> Factory::NewSyntaxError(Handle<String> message) {
649 return NewError("$SyntaxError", message);
650}
651
652
653Handle<Object> Factory::NewReferenceError(const char* type,
654 Vector< Handle<Object> > args) {
655 return NewError("MakeReferenceError", type, args);
656}
657
658
659Handle<Object> Factory::NewReferenceError(Handle<String> message) {
660 return NewError("$ReferenceError", message);
661}
662
663
664Handle<Object> Factory::NewError(const char* maker, const char* type,
665 Vector< Handle<Object> > args) {
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000666 v8::HandleScope scope; // Instantiate a closeable HandleScope for EscapeFrom.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000667 Handle<FixedArray> array = NewFixedArray(args.length());
ager@chromium.org7c537e22008-10-16 08:43:32 +0000668 for (int i = 0; i < args.length(); i++) {
669 array->set(i, *args[i]);
670 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000671 Handle<JSArray> object = NewJSArrayWithElements(array);
ager@chromium.org7c537e22008-10-16 08:43:32 +0000672 Handle<Object> result = NewError(maker, type, object);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000673 return result.EscapeFrom(&scope);
674}
675
676
677Handle<Object> Factory::NewEvalError(const char* type,
678 Vector< Handle<Object> > args) {
679 return NewError("MakeEvalError", type, args);
680}
681
682
683Handle<Object> Factory::NewError(const char* type,
684 Vector< Handle<Object> > args) {
685 return NewError("MakeError", type, args);
686}
687
688
verwaest@chromium.org37141392012-05-31 13:27:02 +0000689Handle<String> Factory::EmergencyNewError(const char* type,
690 Handle<JSArray> args) {
691 const int kBufferSize = 1000;
692 char buffer[kBufferSize];
693 size_t space = kBufferSize;
694 char* p = &buffer[0];
695
696 Vector<char> v(buffer, kBufferSize);
697 OS::StrNCpy(v, type, space);
698 space -= Min(space, strlen(type));
699 p = &buffer[kBufferSize] - space;
700
701 for (unsigned i = 0; i < ARRAY_SIZE(args); i++) {
702 if (space > 0) {
703 *p++ = ' ';
704 space--;
705 if (space > 0) {
706 MaybeObject* maybe_arg = args->GetElement(i);
707 Handle<String> arg_str(reinterpret_cast<String*>(maybe_arg));
708 const char* arg = *arg_str->ToCString();
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000709 Vector<char> v2(p, static_cast<int>(space));
verwaest@chromium.org37141392012-05-31 13:27:02 +0000710 OS::StrNCpy(v2, arg, space);
711 space -= Min(space, strlen(arg));
712 p = &buffer[kBufferSize] - space;
713 }
714 }
715 }
716 if (space > 0) {
717 *p = '\0';
718 } else {
719 buffer[kBufferSize - 1] = '\0';
720 }
721 Handle<String> error_string = NewStringFromUtf8(CStrVector(buffer), TENURED);
722 return error_string;
723}
724
725
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000726Handle<Object> Factory::NewError(const char* maker,
727 const char* type,
728 Handle<JSArray> args) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000729 Handle<String> make_str = LookupAsciiSymbol(maker);
730 Handle<Object> fun_obj(
731 isolate()->js_builtins_object()->GetPropertyNoExceptionThrown(*make_str));
ager@chromium.org4af710e2009-09-15 12:20:11 +0000732 // If the builtins haven't been properly configured yet this error
733 // constructor may not have been defined. Bail out.
verwaest@chromium.org37141392012-05-31 13:27:02 +0000734 if (!fun_obj->IsJSFunction()) {
735 return EmergencyNewError(type, args);
736 }
ager@chromium.org4af710e2009-09-15 12:20:11 +0000737 Handle<JSFunction> fun = Handle<JSFunction>::cast(fun_obj);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000738 Handle<Object> type_obj = LookupAsciiSymbol(type);
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +0000739 Handle<Object> argv[] = { type_obj, args };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000740
741 // Invoke the JavaScript factory method. If an exception is thrown while
742 // running the factory method, use the exception as the result.
743 bool caught_exception;
744 Handle<Object> result = Execution::TryCall(fun,
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +0000745 isolate()->js_builtins_object(),
746 ARRAY_SIZE(argv),
747 argv,
748 &caught_exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000749 return result;
750}
751
752
753Handle<Object> Factory::NewError(Handle<String> message) {
754 return NewError("$Error", message);
755}
756
757
758Handle<Object> Factory::NewError(const char* constructor,
759 Handle<String> message) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000760 Handle<String> constr = LookupAsciiSymbol(constructor);
761 Handle<JSFunction> fun = Handle<JSFunction>(
762 JSFunction::cast(isolate()->js_builtins_object()->
763 GetPropertyNoExceptionThrown(*constr)));
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +0000764 Handle<Object> argv[] = { message };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000765
766 // Invoke the JavaScript factory method. If an exception is thrown while
767 // running the factory method, use the exception as the result.
768 bool caught_exception;
769 Handle<Object> result = Execution::TryCall(fun,
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +0000770 isolate()->js_builtins_object(),
771 ARRAY_SIZE(argv),
772 argv,
773 &caught_exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000774 return result;
775}
776
777
778Handle<JSFunction> Factory::NewFunction(Handle<String> name,
779 InstanceType type,
780 int instance_size,
781 Handle<Code> code,
782 bool force_initial_map) {
783 // Allocate the function
784 Handle<JSFunction> function = NewFunction(name, the_hole_value());
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000785
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000786 // Set up the code pointer in both the shared function info and in
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000787 // the function itself.
788 function->shared()->set_code(*code);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000789 function->set_code(*code);
790
791 if (force_initial_map ||
792 type != JS_OBJECT_TYPE ||
793 instance_size != JSObject::kHeaderSize) {
794 Handle<Map> initial_map = NewMap(type, instance_size);
795 Handle<JSObject> prototype = NewFunctionPrototype(function);
796 initial_map->set_prototype(*prototype);
797 function->set_initial_map(*initial_map);
798 initial_map->set_constructor(*function);
799 } else {
800 ASSERT(!function->has_initial_map());
801 ASSERT(!function->has_prototype());
802 }
803
804 return function;
805}
806
807
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000808Handle<JSFunction> Factory::NewFunctionWithPrototype(Handle<String> name,
809 InstanceType type,
810 int instance_size,
811 Handle<JSObject> prototype,
812 Handle<Code> code,
813 bool force_initial_map) {
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000814 // Allocate the function.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000815 Handle<JSFunction> function = NewFunction(name, prototype);
816
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000817 // Set up the code pointer in both the shared function info and in
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000818 // the function itself.
819 function->shared()->set_code(*code);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000820 function->set_code(*code);
821
822 if (force_initial_map ||
823 type != JS_OBJECT_TYPE ||
824 instance_size != JSObject::kHeaderSize) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000825 Handle<Map> initial_map = NewMap(type,
826 instance_size,
svenpanne@chromium.org830d30c2012-05-29 13:20:14 +0000827 GetInitialFastElementsKind());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000828 function->set_initial_map(*initial_map);
829 initial_map->set_constructor(*function);
830 }
831
832 // Set function.prototype and give the prototype a constructor
833 // property that refers to the function.
834 SetPrototypeProperty(function, prototype);
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +0000835 // Currently safe because it is only invoked from Genesis.
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000836 CHECK_NOT_EMPTY_HANDLE(isolate(),
837 JSObject::SetLocalPropertyIgnoreAttributes(
838 prototype, constructor_symbol(),
839 function, DONT_ENUM));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000840 return function;
841}
842
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000843
kmillikin@chromium.org4111b802010-05-03 10:34:42 +0000844Handle<JSFunction> Factory::NewFunctionWithoutPrototype(Handle<String> name,
845 Handle<Code> code) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000846 Handle<JSFunction> function = NewFunctionWithoutPrototype(name,
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000847 CLASSIC_MODE);
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000848 function->shared()->set_code(*code);
kmillikin@chromium.org4111b802010-05-03 10:34:42 +0000849 function->set_code(*code);
850 ASSERT(!function->has_initial_map());
851 ASSERT(!function->has_prototype());
852 return function;
853}
854
855
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000856Handle<ScopeInfo> Factory::NewScopeInfo(int length) {
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000857 CALL_HEAP_FUNCTION(
858 isolate(),
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000859 isolate()->heap()->AllocateScopeInfo(length),
860 ScopeInfo);
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000861}
862
863
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000864Handle<Code> Factory::NewCode(const CodeDesc& desc,
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000865 Code::Flags flags,
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +0000866 Handle<Object> self_ref,
867 bool immovable) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000868 CALL_HEAP_FUNCTION(isolate(),
869 isolate()->heap()->CreateCode(
870 desc, flags, self_ref, immovable),
871 Code);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000872}
873
874
875Handle<Code> Factory::CopyCode(Handle<Code> code) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000876 CALL_HEAP_FUNCTION(isolate(),
877 isolate()->heap()->CopyCode(*code),
878 Code);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000879}
880
881
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000882Handle<Code> Factory::CopyCode(Handle<Code> code, Vector<byte> reloc_info) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000883 CALL_HEAP_FUNCTION(isolate(),
884 isolate()->heap()->CopyCode(*code, reloc_info),
885 Code);
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000886}
887
888
jkummerow@chromium.org28583c92012-07-16 11:31:55 +0000889MUST_USE_RESULT static inline MaybeObject* DoCopyAdd(
lrn@chromium.org303ada72010-10-27 09:33:13 +0000890 DescriptorArray* array,
891 String* key,
892 Object* value,
893 PropertyAttributes attributes) {
jkummerow@chromium.org28583c92012-07-16 11:31:55 +0000894 CallbacksDescriptor desc(key, value, attributes);
895 MaybeObject* obj = array->CopyAdd(&desc);
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000896 return obj;
897}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000898
899
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000900// Allocate the new array.
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000901Handle<DescriptorArray> Factory::CopyAppendForeignDescriptor(
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000902 Handle<DescriptorArray> array,
903 Handle<String> key,
904 Handle<Object> value,
905 PropertyAttributes attributes) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000906 CALL_HEAP_FUNCTION(isolate(),
jkummerow@chromium.org28583c92012-07-16 11:31:55 +0000907 DoCopyAdd(*array, *key, *value, attributes),
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000908 DescriptorArray);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000909}
910
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000911
912Handle<String> Factory::SymbolFromString(Handle<String> value) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000913 CALL_HEAP_FUNCTION(isolate(),
914 isolate()->heap()->LookupSymbol(*value), String);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000915}
916
917
918Handle<DescriptorArray> Factory::CopyAppendCallbackDescriptors(
919 Handle<DescriptorArray> array,
920 Handle<Object> descriptors) {
921 v8::NeanderArray callbacks(descriptors);
922 int nof_callbacks = callbacks.length();
jkummerow@chromium.org7a6fc812012-06-27 11:12:38 +0000923 int descriptor_count = array->number_of_descriptors();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000924 Handle<DescriptorArray> result =
jkummerow@chromium.org7a6fc812012-06-27 11:12:38 +0000925 NewDescriptorArray(descriptor_count + nof_callbacks);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000926
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000927 // Ensure that marking will not progress and change color of objects.
928 DescriptorArray::WhitenessWitness witness(*result);
929
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000930 // Copy the descriptors from the array.
jkummerow@chromium.org28583c92012-07-16 11:31:55 +0000931 if (0 < descriptor_count) {
932 result->SetLastAdded(array->LastAdded());
933 for (int i = 0; i < descriptor_count; i++) {
verwaest@chromium.org753aee42012-07-17 16:15:42 +0000934 result->CopyFrom(i, *array, i, witness);
jkummerow@chromium.org28583c92012-07-16 11:31:55 +0000935 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000936 }
937
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000938 // Fill in new callback descriptors. Process the callbacks from
939 // back to front so that the last callback with a given name takes
940 // precedence over previously added callbacks with that name.
941 for (int i = nof_callbacks - 1; i >= 0; i--) {
942 Handle<AccessorInfo> entry =
943 Handle<AccessorInfo>(AccessorInfo::cast(callbacks.get(i)));
944 // Ensure the key is a symbol before writing into the instance descriptor.
945 Handle<String> key =
946 SymbolFromString(Handle<String>(String::cast(entry->name())));
947 // Check if a descriptor with this name already exists before writing.
jkummerow@chromium.org28583c92012-07-16 11:31:55 +0000948 if (LinearSearch(*result,
949 EXPECT_UNSORTED,
950 *key,
951 result->NumberOfSetDescriptors()) ==
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000952 DescriptorArray::kNotFound) {
jkummerow@chromium.org28583c92012-07-16 11:31:55 +0000953 CallbacksDescriptor desc(*key, *entry, entry->property_attributes());
954 result->Append(&desc, witness);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000955 }
956 }
957
jkummerow@chromium.org28583c92012-07-16 11:31:55 +0000958 int new_number_of_descriptors = result->NumberOfSetDescriptors();
rossberg@chromium.org657d53b2012-07-12 11:06:03 +0000959 // Return the old descriptor array if there were no new elements.
jkummerow@chromium.org28583c92012-07-16 11:31:55 +0000960 if (new_number_of_descriptors == descriptor_count) return array;
rossberg@chromium.org657d53b2012-07-12 11:06:03 +0000961
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000962 // If duplicates were detected, allocate a result of the right size
963 // and transfer the elements.
jkummerow@chromium.org28583c92012-07-16 11:31:55 +0000964 if (new_number_of_descriptors < result->length()) {
rossberg@chromium.org657d53b2012-07-12 11:06:03 +0000965 Handle<DescriptorArray> new_result =
jkummerow@chromium.org28583c92012-07-16 11:31:55 +0000966 NewDescriptorArray(new_number_of_descriptors);
967 for (int i = 0; i < new_number_of_descriptors; i++) {
verwaest@chromium.org753aee42012-07-17 16:15:42 +0000968 new_result->CopyFrom(i, *result, i, witness);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000969 }
970 result = new_result;
971 }
972
973 // Sort the result before returning.
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000974 result->Sort(witness);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000975 return result;
976}
977
978
979Handle<JSObject> Factory::NewJSObject(Handle<JSFunction> constructor,
980 PretenureFlag pretenure) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000981 CALL_HEAP_FUNCTION(
982 isolate(),
983 isolate()->heap()->AllocateJSObject(*constructor, pretenure), JSObject);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000984}
985
986
danno@chromium.org81cac2b2012-07-10 11:28:27 +0000987Handle<JSModule> Factory::NewJSModule(Handle<Context> context,
988 Handle<ScopeInfo> scope_info) {
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000989 CALL_HEAP_FUNCTION(
990 isolate(),
danno@chromium.org81cac2b2012-07-10 11:28:27 +0000991 isolate()->heap()->AllocateJSModule(*context, *scope_info), JSModule);
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000992}
993
994
kasperl@chromium.org2abc4502009-07-02 07:00:29 +0000995Handle<GlobalObject> Factory::NewGlobalObject(
996 Handle<JSFunction> constructor) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000997 CALL_HEAP_FUNCTION(isolate(),
998 isolate()->heap()->AllocateGlobalObject(*constructor),
kasperl@chromium.org2abc4502009-07-02 07:00:29 +0000999 GlobalObject);
1000}
1001
1002
1003
ager@chromium.org236ad962008-09-25 09:45:57 +00001004Handle<JSObject> Factory::NewJSObjectFromMap(Handle<Map> map) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001005 CALL_HEAP_FUNCTION(
1006 isolate(),
1007 isolate()->heap()->AllocateJSObjectFromMap(*map, NOT_TENURED),
1008 JSObject);
ager@chromium.org236ad962008-09-25 09:45:57 +00001009}
1010
1011
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +00001012Handle<JSArray> Factory::NewJSArray(int capacity,
danno@chromium.orgfa458e42012-02-01 10:48:36 +00001013 ElementsKind elements_kind,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001014 PretenureFlag pretenure) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001015 CALL_HEAP_FUNCTION(isolate(),
danno@chromium.orgfa458e42012-02-01 10:48:36 +00001016 isolate()->heap()->AllocateJSArrayAndStorage(
1017 elements_kind,
1018 0,
1019 capacity,
1020 INITIALIZE_ARRAY_ELEMENTS_WITH_HOLE,
1021 pretenure),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001022 JSArray);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001023}
1024
1025
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00001026Handle<JSArray> Factory::NewJSArrayWithElements(Handle<FixedArrayBase> elements,
danno@chromium.orgfa458e42012-02-01 10:48:36 +00001027 ElementsKind elements_kind,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001028 PretenureFlag pretenure) {
danno@chromium.orgfa458e42012-02-01 10:48:36 +00001029 CALL_HEAP_FUNCTION(
1030 isolate(),
1031 isolate()->heap()->AllocateJSArrayWithElements(*elements,
1032 elements_kind,
1033 pretenure),
1034 JSArray);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001035}
1036
1037
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00001038void Factory::SetElementsCapacityAndLength(Handle<JSArray> array,
1039 int capacity,
1040 int length) {
1041 ElementsAccessor* accessor = array->GetElementsAccessor();
1042 CALL_HEAP_FUNCTION_VOID(
1043 isolate(),
1044 accessor->SetCapacityAndLength(*array, capacity, length));
1045}
1046
1047
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001048void Factory::SetContent(Handle<JSArray> array,
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00001049 Handle<FixedArrayBase> elements) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001050 CALL_HEAP_FUNCTION_VOID(
1051 isolate(),
1052 array->SetContent(*elements));
1053}
1054
1055
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00001056void Factory::EnsureCanContainHeapObjectElements(Handle<JSArray> array) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001057 CALL_HEAP_FUNCTION_VOID(
1058 isolate(),
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00001059 array->EnsureCanContainHeapObjectElements());
1060}
1061
1062
1063void Factory::EnsureCanContainElements(Handle<JSArray> array,
1064 Handle<FixedArrayBase> elements,
svenpanne@chromium.org830d30c2012-05-29 13:20:14 +00001065 uint32_t length,
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00001066 EnsureElementsMode mode) {
1067 CALL_HEAP_FUNCTION_VOID(
1068 isolate(),
svenpanne@chromium.org830d30c2012-05-29 13:20:14 +00001069 array->EnsureCanContainElements(*elements, length, mode));
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001070}
1071
1072
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001073Handle<JSProxy> Factory::NewJSProxy(Handle<Object> handler,
1074 Handle<Object> prototype) {
1075 CALL_HEAP_FUNCTION(
1076 isolate(),
1077 isolate()->heap()->AllocateJSProxy(*handler, *prototype),
1078 JSProxy);
1079}
1080
1081
lrn@chromium.org34e60782011-09-15 07:25:40 +00001082void Factory::BecomeJSObject(Handle<JSReceiver> object) {
rossberg@chromium.org717967f2011-07-20 13:44:42 +00001083 CALL_HEAP_FUNCTION_VOID(
1084 isolate(),
lrn@chromium.org34e60782011-09-15 07:25:40 +00001085 isolate()->heap()->ReinitializeJSReceiver(
1086 *object, JS_OBJECT_TYPE, JSObject::kHeaderSize));
1087}
1088
1089
1090void Factory::BecomeJSFunction(Handle<JSReceiver> object) {
1091 CALL_HEAP_FUNCTION_VOID(
1092 isolate(),
1093 isolate()->heap()->ReinitializeJSReceiver(
1094 *object, JS_FUNCTION_TYPE, JSFunction::kSize));
rossberg@chromium.org717967f2011-07-20 13:44:42 +00001095}
1096
1097
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001098void Factory::SetIdentityHash(Handle<JSObject> object, Object* hash) {
1099 CALL_HEAP_FUNCTION_VOID(
1100 isolate(),
1101 object->SetIdentityHash(hash, ALLOW_CREATION));
1102}
1103
1104
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001105Handle<SharedFunctionInfo> Factory::NewSharedFunctionInfo(
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00001106 Handle<String> name,
1107 int number_of_literals,
1108 Handle<Code> code,
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001109 Handle<ScopeInfo> scope_info) {
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001110 Handle<SharedFunctionInfo> shared = NewSharedFunctionInfo(name);
1111 shared->set_code(*code);
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00001112 shared->set_scope_info(*scope_info);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001113 int literals_array_size = number_of_literals;
1114 // If the function contains object, regexp or array literals,
1115 // allocate extra space for a literals array prefix containing the
1116 // context.
1117 if (number_of_literals > 0) {
1118 literals_array_size += JSFunction::kLiteralsPrefixSize;
1119 }
1120 shared->set_num_literals(literals_array_size);
1121 return shared;
1122}
1123
1124
kmillikin@chromium.org31b12772011-02-02 16:08:26 +00001125Handle<JSMessageObject> Factory::NewJSMessageObject(
1126 Handle<String> type,
1127 Handle<JSArray> arguments,
1128 int start_position,
1129 int end_position,
1130 Handle<Object> script,
1131 Handle<Object> stack_trace,
1132 Handle<Object> stack_frames) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001133 CALL_HEAP_FUNCTION(isolate(),
1134 isolate()->heap()->AllocateJSMessageObject(*type,
1135 *arguments,
1136 start_position,
1137 end_position,
1138 *script,
1139 *stack_trace,
1140 *stack_frames),
kmillikin@chromium.org31b12772011-02-02 16:08:26 +00001141 JSMessageObject);
1142}
1143
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001144Handle<SharedFunctionInfo> Factory::NewSharedFunctionInfo(Handle<String> name) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001145 CALL_HEAP_FUNCTION(isolate(),
1146 isolate()->heap()->AllocateSharedFunctionInfo(*name),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001147 SharedFunctionInfo);
1148}
1149
1150
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00001151Handle<String> Factory::NumberToString(Handle<Object> number) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001152 CALL_HEAP_FUNCTION(isolate(),
1153 isolate()->heap()->NumberToString(*number), String);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00001154}
1155
1156
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001157Handle<String> Factory::Uint32ToString(uint32_t value) {
1158 CALL_HEAP_FUNCTION(isolate(),
1159 isolate()->heap()->Uint32ToString(value), String);
1160}
1161
1162
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001163Handle<SeededNumberDictionary> Factory::DictionaryAtNumberPut(
1164 Handle<SeededNumberDictionary> dictionary,
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00001165 uint32_t key,
1166 Handle<Object> value) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001167 CALL_HEAP_FUNCTION(isolate(),
1168 dictionary->AtNumberPut(key, *value),
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001169 SeededNumberDictionary);
1170}
1171
1172
1173Handle<UnseededNumberDictionary> Factory::DictionaryAtNumberPut(
1174 Handle<UnseededNumberDictionary> dictionary,
1175 uint32_t key,
1176 Handle<Object> value) {
1177 CALL_HEAP_FUNCTION(isolate(),
1178 dictionary->AtNumberPut(key, *value),
1179 UnseededNumberDictionary);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001180}
1181
1182
1183Handle<JSFunction> Factory::NewFunctionHelper(Handle<String> name,
1184 Handle<Object> prototype) {
1185 Handle<SharedFunctionInfo> function_share = NewSharedFunctionInfo(name);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001186 CALL_HEAP_FUNCTION(
1187 isolate(),
1188 isolate()->heap()->AllocateFunction(*isolate()->function_map(),
1189 *function_share,
1190 *prototype),
1191 JSFunction);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001192}
1193
1194
1195Handle<JSFunction> Factory::NewFunction(Handle<String> name,
1196 Handle<Object> prototype) {
1197 Handle<JSFunction> fun = NewFunctionHelper(name, prototype);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001198 fun->set_context(isolate()->context()->global_context());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001199 return fun;
1200}
1201
1202
kmillikin@chromium.org4111b802010-05-03 10:34:42 +00001203Handle<JSFunction> Factory::NewFunctionWithoutPrototypeHelper(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001204 Handle<String> name,
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00001205 LanguageMode language_mode) {
kmillikin@chromium.org4111b802010-05-03 10:34:42 +00001206 Handle<SharedFunctionInfo> function_share = NewSharedFunctionInfo(name);
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00001207 Handle<Map> map = (language_mode == CLASSIC_MODE)
1208 ? isolate()->function_without_prototype_map()
1209 : isolate()->strict_mode_function_without_prototype_map();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001210 CALL_HEAP_FUNCTION(isolate(),
1211 isolate()->heap()->AllocateFunction(
1212 *map,
kmillikin@chromium.org4111b802010-05-03 10:34:42 +00001213 *function_share,
1214 *the_hole_value()),
1215 JSFunction);
1216}
1217
1218
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001219Handle<JSFunction> Factory::NewFunctionWithoutPrototype(
1220 Handle<String> name,
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00001221 LanguageMode language_mode) {
1222 Handle<JSFunction> fun =
1223 NewFunctionWithoutPrototypeHelper(name, language_mode);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001224 fun->set_context(isolate()->context()->global_context());
kmillikin@chromium.org4111b802010-05-03 10:34:42 +00001225 return fun;
1226}
1227
1228
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001229Handle<Object> Factory::ToObject(Handle<Object> object) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001230 CALL_HEAP_FUNCTION(isolate(), object->ToObject(), Object);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001231}
1232
1233
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001234Handle<Object> Factory::ToObject(Handle<Object> object,
1235 Handle<Context> global_context) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001236 CALL_HEAP_FUNCTION(isolate(), object->ToObject(*global_context), Object);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001237}
1238
1239
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001240#ifdef ENABLE_DEBUGGER_SUPPORT
v8.team.kasperl727e9952008-09-02 14:56:44 +00001241Handle<DebugInfo> Factory::NewDebugInfo(Handle<SharedFunctionInfo> shared) {
1242 // Get the original code of the function.
1243 Handle<Code> code(shared->code());
1244
1245 // Create a copy of the code before allocating the debug info object to avoid
1246 // allocation while setting up the debug info object.
1247 Handle<Code> original_code(*Factory::CopyCode(code));
1248
1249 // Allocate initial fixed array for active break points before allocating the
1250 // debug info object to avoid allocation while setting up the debug info
1251 // object.
1252 Handle<FixedArray> break_points(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001253 NewFixedArray(Debug::kEstimatedNofBreakPointsInFunction));
v8.team.kasperl727e9952008-09-02 14:56:44 +00001254
1255 // Create and set up the debug info object. Debug info contains function, a
1256 // copy of the original code, the executing code and initial fixed array for
1257 // active break points.
1258 Handle<DebugInfo> debug_info =
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001259 Handle<DebugInfo>::cast(NewStruct(DEBUG_INFO_TYPE));
v8.team.kasperl727e9952008-09-02 14:56:44 +00001260 debug_info->set_shared(*shared);
1261 debug_info->set_original_code(*original_code);
1262 debug_info->set_code(*code);
1263 debug_info->set_break_points(*break_points);
1264
1265 // Link debug info to function.
1266 shared->set_debug_info(*debug_info);
1267
1268 return debug_info;
1269}
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001270#endif
v8.team.kasperl727e9952008-09-02 14:56:44 +00001271
1272
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001273Handle<JSObject> Factory::NewArgumentsObject(Handle<Object> callee,
1274 int length) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001275 CALL_HEAP_FUNCTION(
1276 isolate(),
1277 isolate()->heap()->AllocateArgumentsObject(*callee, length), JSObject);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001278}
1279
1280
1281Handle<JSFunction> Factory::CreateApiFunction(
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001282 Handle<FunctionTemplateInfo> obj, ApiInstanceType instance_type) {
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00001283 Handle<Code> code = isolate()->builtins()->HandleApiCall();
1284 Handle<Code> construct_stub = isolate()->builtins()->JSConstructStubApi();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001285
kasper.lund212ac232008-07-16 07:07:30 +00001286 int internal_field_count = 0;
1287 if (!obj->instance_template()->IsUndefined()) {
1288 Handle<ObjectTemplateInfo> instance_template =
1289 Handle<ObjectTemplateInfo>(
1290 ObjectTemplateInfo::cast(obj->instance_template()));
1291 internal_field_count =
1292 Smi::cast(instance_template->internal_field_count())->value();
1293 }
1294
1295 int instance_size = kPointerSize * internal_field_count;
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001296 InstanceType type = INVALID_TYPE;
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001297 switch (instance_type) {
1298 case JavaScriptObject:
1299 type = JS_OBJECT_TYPE;
1300 instance_size += JSObject::kHeaderSize;
1301 break;
1302 case InnerGlobalObject:
1303 type = JS_GLOBAL_OBJECT_TYPE;
1304 instance_size += JSGlobalObject::kSize;
1305 break;
1306 case OuterGlobalObject:
1307 type = JS_GLOBAL_PROXY_TYPE;
1308 instance_size += JSGlobalProxy::kSize;
1309 break;
1310 default:
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001311 break;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001312 }
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001313 ASSERT(type != INVALID_TYPE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001314
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001315 Handle<JSFunction> result =
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001316 NewFunction(Factory::empty_symbol(),
1317 type,
1318 instance_size,
1319 code,
1320 true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001321 // Set class name.
1322 Handle<Object> class_name = Handle<Object>(obj->class_name());
1323 if (class_name->IsString()) {
1324 result->shared()->set_instance_class_name(*class_name);
1325 result->shared()->set_name(*class_name);
1326 }
1327
1328 Handle<Map> map = Handle<Map>(result->initial_map());
1329
1330 // Mark as undetectable if needed.
1331 if (obj->undetectable()) {
1332 map->set_is_undetectable();
1333 }
1334
1335 // Mark as hidden for the __proto__ accessor if needed.
1336 if (obj->hidden_prototype()) {
1337 map->set_is_hidden_prototype();
1338 }
1339
1340 // Mark as needs_access_check if needed.
1341 if (obj->needs_access_check()) {
ager@chromium.org870a0b62008-11-04 11:43:05 +00001342 map->set_is_access_check_needed(true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001343 }
1344
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001345 // Set interceptor information in the map.
1346 if (!obj->named_property_handler()->IsUndefined()) {
1347 map->set_has_named_interceptor();
1348 }
1349 if (!obj->indexed_property_handler()->IsUndefined()) {
1350 map->set_has_indexed_interceptor();
1351 }
1352
1353 // Set instance call-as-function information in the map.
1354 if (!obj->instance_call_handler()->IsUndefined()) {
1355 map->set_has_instance_call_handler();
1356 }
1357
1358 result->shared()->set_function_data(*obj);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001359 result->shared()->set_construct_stub(*construct_stub);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001360 result->shared()->DontAdaptArguments();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001361
1362 // Recursively copy parent templates' accessors, 'data' may be modified.
1363 Handle<DescriptorArray> array =
1364 Handle<DescriptorArray>(map->instance_descriptors());
1365 while (true) {
1366 Handle<Object> props = Handle<Object>(obj->property_accessors());
1367 if (!props->IsUndefined()) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001368 array = CopyAppendCallbackDescriptors(array, props);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001369 }
1370 Handle<Object> parent = Handle<Object>(obj->parent_template());
1371 if (parent->IsUndefined()) break;
1372 obj = Handle<FunctionTemplateInfo>::cast(parent);
1373 }
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001374 if (!array->IsEmpty()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001375 map->set_instance_descriptors(*array);
1376 }
1377
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001378 ASSERT(result->shared()->IsApiFunction());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001379 return result;
1380}
1381
1382
ager@chromium.org236ad962008-09-25 09:45:57 +00001383Handle<MapCache> Factory::NewMapCache(int at_least_space_for) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001384 CALL_HEAP_FUNCTION(isolate(),
1385 MapCache::Allocate(at_least_space_for), MapCache);
ager@chromium.org236ad962008-09-25 09:45:57 +00001386}
1387
1388
lrn@chromium.org303ada72010-10-27 09:33:13 +00001389MUST_USE_RESULT static MaybeObject* UpdateMapCacheWith(Context* context,
1390 FixedArray* keys,
1391 Map* map) {
1392 Object* result;
1393 { MaybeObject* maybe_result =
1394 MapCache::cast(context->map_cache())->Put(keys, map);
1395 if (!maybe_result->ToObject(&result)) return maybe_result;
1396 }
1397 context->set_map_cache(MapCache::cast(result));
ager@chromium.org236ad962008-09-25 09:45:57 +00001398 return result;
1399}
1400
1401
1402Handle<MapCache> Factory::AddToMapCache(Handle<Context> context,
1403 Handle<FixedArray> keys,
1404 Handle<Map> map) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001405 CALL_HEAP_FUNCTION(isolate(),
1406 UpdateMapCacheWith(*context, *keys, *map), MapCache);
ager@chromium.org236ad962008-09-25 09:45:57 +00001407}
1408
1409
1410Handle<Map> Factory::ObjectLiteralMapFromCache(Handle<Context> context,
1411 Handle<FixedArray> keys) {
1412 if (context->map_cache()->IsUndefined()) {
1413 // Allocate the new map cache for the global context.
1414 Handle<MapCache> new_cache = NewMapCache(24);
1415 context->set_map_cache(*new_cache);
1416 }
ager@chromium.org32912102009-01-16 10:38:43 +00001417 // Check to see whether there is a matching element in the cache.
ager@chromium.org236ad962008-09-25 09:45:57 +00001418 Handle<MapCache> cache =
1419 Handle<MapCache>(MapCache::cast(context->map_cache()));
1420 Handle<Object> result = Handle<Object>(cache->Lookup(*keys));
1421 if (result->IsMap()) return Handle<Map>::cast(result);
1422 // Create a new map and add it to the cache.
1423 Handle<Map> map =
ager@chromium.org32912102009-01-16 10:38:43 +00001424 CopyMap(Handle<Map>(context->object_function()->initial_map()),
1425 keys->length());
ager@chromium.org236ad962008-09-25 09:45:57 +00001426 AddToMapCache(context, keys, map);
1427 return Handle<Map>(map);
1428}
1429
1430
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001431void Factory::SetRegExpAtomData(Handle<JSRegExp> regexp,
1432 JSRegExp::Type type,
1433 Handle<String> source,
1434 JSRegExp::Flags flags,
1435 Handle<Object> data) {
1436 Handle<FixedArray> store = NewFixedArray(JSRegExp::kAtomDataSize);
1437
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001438 store->set(JSRegExp::kTagIndex, Smi::FromInt(type));
1439 store->set(JSRegExp::kSourceIndex, *source);
1440 store->set(JSRegExp::kFlagsIndex, Smi::FromInt(flags.value()));
1441 store->set(JSRegExp::kAtomPatternIndex, *data);
1442 regexp->set_data(*store);
1443}
1444
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001445void Factory::SetRegExpIrregexpData(Handle<JSRegExp> regexp,
1446 JSRegExp::Type type,
1447 Handle<String> source,
1448 JSRegExp::Flags flags,
1449 int capture_count) {
1450 Handle<FixedArray> store = NewFixedArray(JSRegExp::kIrregexpDataSize);
jkummerow@chromium.orgddda9e82011-07-06 11:27:02 +00001451 Smi* uninitialized = Smi::FromInt(JSRegExp::kUninitializedValue);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001452 store->set(JSRegExp::kTagIndex, Smi::FromInt(type));
1453 store->set(JSRegExp::kSourceIndex, *source);
1454 store->set(JSRegExp::kFlagsIndex, Smi::FromInt(flags.value()));
jkummerow@chromium.orgddda9e82011-07-06 11:27:02 +00001455 store->set(JSRegExp::kIrregexpASCIICodeIndex, uninitialized);
1456 store->set(JSRegExp::kIrregexpUC16CodeIndex, uninitialized);
1457 store->set(JSRegExp::kIrregexpASCIICodeSavedIndex, uninitialized);
1458 store->set(JSRegExp::kIrregexpUC16CodeSavedIndex, uninitialized);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001459 store->set(JSRegExp::kIrregexpMaxRegisterCountIndex, Smi::FromInt(0));
1460 store->set(JSRegExp::kIrregexpCaptureCountIndex,
1461 Smi::FromInt(capture_count));
1462 regexp->set_data(*store);
1463}
1464
1465
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001466
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001467void Factory::ConfigureInstance(Handle<FunctionTemplateInfo> desc,
1468 Handle<JSObject> instance,
1469 bool* pending_exception) {
1470 // Configure the instance by adding the properties specified by the
1471 // instance template.
1472 Handle<Object> instance_template = Handle<Object>(desc->instance_template());
1473 if (!instance_template->IsUndefined()) {
1474 Execution::ConfigureInstance(instance,
1475 instance_template,
1476 pending_exception);
1477 } else {
1478 *pending_exception = false;
1479 }
1480}
1481
1482
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001483Handle<Object> Factory::GlobalConstantFor(Handle<String> name) {
1484 Heap* h = isolate()->heap();
1485 if (name->Equals(h->undefined_symbol())) return undefined_value();
1486 if (name->Equals(h->nan_symbol())) return nan_value();
1487 if (name->Equals(h->infinity_symbol())) return infinity_value();
1488 return Handle<Object>::null();
1489}
1490
1491
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00001492Handle<Object> Factory::ToBoolean(bool value) {
1493 return Handle<Object>(value
1494 ? isolate()->heap()->true_value()
1495 : isolate()->heap()->false_value());
1496}
1497
1498
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001499} } // namespace v8::internal