blob: c2976a577a1a035749f25dd95c97bdf8fbba9af7 [file] [log] [blame]
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001// Copyright 2011 the V8 project authors. All rights reserved.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28#include "v8.h"
29
30#include "api.h"
v8.team.kasperl727e9952008-09-02 14:56:44 +000031#include "debug.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000032#include "execution.h"
33#include "factory.h"
34#include "macro-assembler.h"
kasperl@chromium.orga5551262010-12-07 12:49:48 +000035#include "objects.h"
ager@chromium.orgea4f62e2010-08-16 16:28:43 +000036#include "objects-visiting.h"
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +000037#include "scopeinfo.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000038
kasperl@chromium.org71affb52009-05-26 05:44:31 +000039namespace v8 {
40namespace internal {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000041
42
43Handle<FixedArray> Factory::NewFixedArray(int size, PretenureFlag pretenure) {
44 ASSERT(0 <= size);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000045 CALL_HEAP_FUNCTION(
46 isolate(),
47 isolate()->heap()->AllocateFixedArray(size, pretenure),
48 FixedArray);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000049}
50
51
ricow@chromium.orgc9c80822010-04-21 08:22:37 +000052Handle<FixedArray> Factory::NewFixedArrayWithHoles(int size,
53 PretenureFlag pretenure) {
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +000054 ASSERT(0 <= size);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000055 CALL_HEAP_FUNCTION(
56 isolate(),
57 isolate()->heap()->AllocateFixedArrayWithHoles(size, pretenure),
58 FixedArray);
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +000059}
60
61
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +000062Handle<FixedDoubleArray> Factory::NewFixedDoubleArray(int size,
63 PretenureFlag pretenure) {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +000064 ASSERT(0 <= size);
65 CALL_HEAP_FUNCTION(
66 isolate(),
67 isolate()->heap()->AllocateUninitializedFixedDoubleArray(size, pretenure),
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +000068 FixedDoubleArray);
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +000069}
70
71
kasperl@chromium.org86f77b72009-07-06 08:21:57 +000072Handle<StringDictionary> Factory::NewStringDictionary(int at_least_space_for) {
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +000073 ASSERT(0 <= at_least_space_for);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000074 CALL_HEAP_FUNCTION(isolate(),
75 StringDictionary::Allocate(at_least_space_for),
kasperl@chromium.org86f77b72009-07-06 08:21:57 +000076 StringDictionary);
77}
78
79
80Handle<NumberDictionary> Factory::NewNumberDictionary(int at_least_space_for) {
81 ASSERT(0 <= at_least_space_for);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000082 CALL_HEAP_FUNCTION(isolate(),
83 NumberDictionary::Allocate(at_least_space_for),
kasperl@chromium.org86f77b72009-07-06 08:21:57 +000084 NumberDictionary);
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +000085}
86
87
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +000088Handle<ObjectHashSet> Factory::NewObjectHashSet(int at_least_space_for) {
89 ASSERT(0 <= at_least_space_for);
90 CALL_HEAP_FUNCTION(isolate(),
91 ObjectHashSet::Allocate(at_least_space_for),
92 ObjectHashSet);
93}
94
95
vegorov@chromium.org7943d462011-08-01 11:41:52 +000096Handle<ObjectHashTable> Factory::NewObjectHashTable(int at_least_space_for) {
97 ASSERT(0 <= at_least_space_for);
98 CALL_HEAP_FUNCTION(isolate(),
99 ObjectHashTable::Allocate(at_least_space_for),
100 ObjectHashTable);
101}
102
103
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000104Handle<DescriptorArray> Factory::NewDescriptorArray(int number_of_descriptors) {
105 ASSERT(0 <= number_of_descriptors);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000106 CALL_HEAP_FUNCTION(isolate(),
107 DescriptorArray::Allocate(number_of_descriptors),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000108 DescriptorArray);
109}
110
111
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000112Handle<DeoptimizationInputData> Factory::NewDeoptimizationInputData(
113 int deopt_entry_count,
114 PretenureFlag pretenure) {
115 ASSERT(deopt_entry_count > 0);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000116 CALL_HEAP_FUNCTION(isolate(),
117 DeoptimizationInputData::Allocate(deopt_entry_count,
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000118 pretenure),
119 DeoptimizationInputData);
120}
121
122
123Handle<DeoptimizationOutputData> Factory::NewDeoptimizationOutputData(
124 int deopt_entry_count,
125 PretenureFlag pretenure) {
126 ASSERT(deopt_entry_count > 0);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000127 CALL_HEAP_FUNCTION(isolate(),
128 DeoptimizationOutputData::Allocate(deopt_entry_count,
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000129 pretenure),
130 DeoptimizationOutputData);
131}
132
133
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000134// Symbols are created in the old generation (data space).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000135Handle<String> Factory::LookupSymbol(Vector<const char> string) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000136 CALL_HEAP_FUNCTION(isolate(),
137 isolate()->heap()->LookupSymbol(string),
138 String);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000139}
140
danno@chromium.org40cb8782011-05-25 07:58:50 +0000141// Symbols are created in the old generation (data space).
142Handle<String> Factory::LookupSymbol(Handle<String> string) {
143 CALL_HEAP_FUNCTION(isolate(),
144 isolate()->heap()->LookupSymbol(*string),
145 String);
146}
147
fschneider@chromium.org9e3e0b62011-01-03 10:16:46 +0000148Handle<String> Factory::LookupAsciiSymbol(Vector<const char> string) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000149 CALL_HEAP_FUNCTION(isolate(),
150 isolate()->heap()->LookupAsciiSymbol(string),
151 String);
fschneider@chromium.org9e3e0b62011-01-03 10:16:46 +0000152}
153
danno@chromium.org40cb8782011-05-25 07:58:50 +0000154
155Handle<String> Factory::LookupAsciiSymbol(Handle<SeqAsciiString> string,
156 int from,
157 int length) {
158 CALL_HEAP_FUNCTION(isolate(),
159 isolate()->heap()->LookupAsciiSymbol(string,
160 from,
161 length),
162 String);
163}
164
165
fschneider@chromium.org9e3e0b62011-01-03 10:16:46 +0000166Handle<String> Factory::LookupTwoByteSymbol(Vector<const uc16> string) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000167 CALL_HEAP_FUNCTION(isolate(),
168 isolate()->heap()->LookupTwoByteSymbol(string),
169 String);
fschneider@chromium.org9e3e0b62011-01-03 10:16:46 +0000170}
171
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000172
173Handle<String> Factory::NewStringFromAscii(Vector<const char> string,
174 PretenureFlag pretenure) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000175 CALL_HEAP_FUNCTION(
176 isolate(),
177 isolate()->heap()->AllocateStringFromAscii(string, pretenure),
178 String);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000179}
180
181Handle<String> Factory::NewStringFromUtf8(Vector<const char> string,
182 PretenureFlag pretenure) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000183 CALL_HEAP_FUNCTION(
184 isolate(),
185 isolate()->heap()->AllocateStringFromUtf8(string, pretenure),
186 String);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000187}
188
189
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000190Handle<String> Factory::NewStringFromTwoByte(Vector<const uc16> string,
191 PretenureFlag pretenure) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000192 CALL_HEAP_FUNCTION(
193 isolate(),
194 isolate()->heap()->AllocateStringFromTwoByte(string, pretenure),
195 String);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000196}
197
198
ager@chromium.org04921a82011-06-27 13:21:41 +0000199Handle<SeqAsciiString> Factory::NewRawAsciiString(int length,
200 PretenureFlag pretenure) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000201 CALL_HEAP_FUNCTION(
202 isolate(),
203 isolate()->heap()->AllocateRawAsciiString(length, pretenure),
ager@chromium.org04921a82011-06-27 13:21:41 +0000204 SeqAsciiString);
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +0000205}
206
207
ager@chromium.org04921a82011-06-27 13:21:41 +0000208Handle<SeqTwoByteString> Factory::NewRawTwoByteString(int length,
209 PretenureFlag pretenure) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000210 CALL_HEAP_FUNCTION(
211 isolate(),
212 isolate()->heap()->AllocateRawTwoByteString(length, pretenure),
ager@chromium.org04921a82011-06-27 13:21:41 +0000213 SeqTwoByteString);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000214}
215
216
217Handle<String> Factory::NewConsString(Handle<String> first,
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000218 Handle<String> second) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000219 CALL_HEAP_FUNCTION(isolate(),
220 isolate()->heap()->AllocateConsString(*first, *second),
221 String);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000222}
223
224
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000225Handle<String> Factory::NewSubString(Handle<String> str,
226 int begin,
227 int end) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000228 CALL_HEAP_FUNCTION(isolate(),
229 str->SubString(begin, end),
230 String);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000231}
232
233
ager@chromium.org04921a82011-06-27 13:21:41 +0000234Handle<String> Factory::NewProperSubString(Handle<String> str,
235 int begin,
236 int end) {
237 ASSERT(begin > 0 || end < str->length());
238 CALL_HEAP_FUNCTION(isolate(),
239 isolate()->heap()->AllocateSubString(*str, begin, end),
240 String);
241}
242
243
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000244Handle<String> Factory::NewExternalStringFromAscii(
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000245 const ExternalAsciiString::Resource* resource) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000246 CALL_HEAP_FUNCTION(
247 isolate(),
248 isolate()->heap()->AllocateExternalStringFromAscii(resource),
249 String);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000250}
251
252
253Handle<String> Factory::NewExternalStringFromTwoByte(
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000254 const ExternalTwoByteString::Resource* resource) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000255 CALL_HEAP_FUNCTION(
256 isolate(),
257 isolate()->heap()->AllocateExternalStringFromTwoByte(resource),
258 String);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000259}
260
261
262Handle<Context> Factory::NewGlobalContext() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000263 CALL_HEAP_FUNCTION(
264 isolate(),
265 isolate()->heap()->AllocateGlobalContext(),
266 Context);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000267}
268
269
270Handle<Context> Factory::NewFunctionContext(int length,
vegorov@chromium.org3cf47312011-06-29 13:20:01 +0000271 Handle<JSFunction> function) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000272 CALL_HEAP_FUNCTION(
273 isolate(),
vegorov@chromium.org3cf47312011-06-29 13:20:01 +0000274 isolate()->heap()->AllocateFunctionContext(length, *function),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000275 Context);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000276}
277
278
vegorov@chromium.org3cf47312011-06-29 13:20:01 +0000279Handle<Context> Factory::NewCatchContext(Handle<JSFunction> function,
280 Handle<Context> previous,
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000281 Handle<String> name,
282 Handle<Object> thrown_object) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000283 CALL_HEAP_FUNCTION(
284 isolate(),
vegorov@chromium.org3cf47312011-06-29 13:20:01 +0000285 isolate()->heap()->AllocateCatchContext(*function,
286 *previous,
287 *name,
288 *thrown_object),
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000289 Context);
290}
291
292
vegorov@chromium.org3cf47312011-06-29 13:20:01 +0000293Handle<Context> Factory::NewWithContext(Handle<JSFunction> function,
294 Handle<Context> previous,
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000295 Handle<JSObject> extension) {
296 CALL_HEAP_FUNCTION(
297 isolate(),
vegorov@chromium.org3cf47312011-06-29 13:20:01 +0000298 isolate()->heap()->AllocateWithContext(*function, *previous, *extension),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000299 Context);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000300}
301
302
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000303Handle<Context> Factory::NewBlockContext(
304 Handle<JSFunction> function,
305 Handle<Context> previous,
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000306 Handle<ScopeInfo> scope_info) {
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000307 CALL_HEAP_FUNCTION(
308 isolate(),
309 isolate()->heap()->AllocateBlockContext(*function,
310 *previous,
311 *scope_info),
312 Context);
313}
314
315
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000316Handle<Struct> Factory::NewStruct(InstanceType type) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000317 CALL_HEAP_FUNCTION(
318 isolate(),
319 isolate()->heap()->AllocateStruct(type),
320 Struct);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000321}
322
323
324Handle<AccessorInfo> Factory::NewAccessorInfo() {
325 Handle<AccessorInfo> info =
326 Handle<AccessorInfo>::cast(NewStruct(ACCESSOR_INFO_TYPE));
327 info->set_flag(0); // Must clear the flag, it was initialized as undefined.
328 return info;
329}
330
331
332Handle<Script> Factory::NewScript(Handle<String> source) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000333 // Generate id for this script.
334 int id;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000335 Heap* heap = isolate()->heap();
336 if (heap->last_script_id()->IsUndefined()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000337 // Script ids start from one.
338 id = 1;
339 } else {
340 // Increment id, wrap when positive smi is exhausted.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000341 id = Smi::cast(heap->last_script_id())->value();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000342 id++;
343 if (!Smi::IsValid(id)) {
344 id = 0;
345 }
346 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000347 heap->SetLastScriptId(Smi::FromInt(id));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000348
349 // Create and initialize script object.
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000350 Handle<Foreign> wrapper = NewForeign(0, TENURED);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000351 Handle<Script> script = Handle<Script>::cast(NewStruct(SCRIPT_TYPE));
352 script->set_source(*source);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000353 script->set_name(heap->undefined_value());
354 script->set_id(heap->last_script_id());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000355 script->set_line_offset(Smi::FromInt(0));
356 script->set_column_offset(Smi::FromInt(0));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000357 script->set_data(heap->undefined_value());
358 script->set_context_data(heap->undefined_value());
ager@chromium.orge2902be2009-06-08 12:21:35 +0000359 script->set_type(Smi::FromInt(Script::TYPE_NORMAL));
360 script->set_compilation_type(Smi::FromInt(Script::COMPILATION_TYPE_HOST));
ager@chromium.org9085a012009-05-11 19:22:57 +0000361 script->set_wrapper(*wrapper);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000362 script->set_line_ends(heap->undefined_value());
363 script->set_eval_from_shared(heap->undefined_value());
ager@chromium.orge2902be2009-06-08 12:21:35 +0000364 script->set_eval_from_instructions_offset(Smi::FromInt(0));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000365
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000366 return script;
367}
368
369
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000370Handle<Foreign> Factory::NewForeign(Address addr, PretenureFlag pretenure) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000371 CALL_HEAP_FUNCTION(isolate(),
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000372 isolate()->heap()->AllocateForeign(addr, pretenure),
373 Foreign);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000374}
375
376
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000377Handle<Foreign> Factory::NewForeign(const AccessorDescriptor* desc) {
378 return NewForeign((Address) desc, TENURED);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000379}
380
381
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000382Handle<ByteArray> Factory::NewByteArray(int length, PretenureFlag pretenure) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000383 ASSERT(0 <= length);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000384 CALL_HEAP_FUNCTION(
385 isolate(),
386 isolate()->heap()->AllocateByteArray(length, pretenure),
387 ByteArray);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000388}
389
390
ager@chromium.org3811b432009-10-28 14:53:37 +0000391Handle<ExternalArray> Factory::NewExternalArray(int length,
392 ExternalArrayType array_type,
393 void* external_pointer,
394 PretenureFlag pretenure) {
395 ASSERT(0 <= length);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000396 CALL_HEAP_FUNCTION(
397 isolate(),
398 isolate()->heap()->AllocateExternalArray(length,
399 array_type,
400 external_pointer,
401 pretenure),
402 ExternalArray);
ager@chromium.org3811b432009-10-28 14:53:37 +0000403}
404
405
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000406Handle<JSGlobalPropertyCell> Factory::NewJSGlobalPropertyCell(
407 Handle<Object> value) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000408 CALL_HEAP_FUNCTION(
409 isolate(),
410 isolate()->heap()->AllocateJSGlobalPropertyCell(*value),
411 JSGlobalPropertyCell);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000412}
413
414
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000415Handle<Map> Factory::NewMap(InstanceType type,
416 int instance_size,
417 ElementsKind elements_kind) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000418 CALL_HEAP_FUNCTION(
419 isolate(),
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000420 isolate()->heap()->AllocateMap(type, instance_size, elements_kind),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000421 Map);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000422}
423
424
425Handle<JSObject> Factory::NewFunctionPrototype(Handle<JSFunction> function) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000426 CALL_HEAP_FUNCTION(
427 isolate(),
428 isolate()->heap()->AllocateFunctionPrototype(*function),
429 JSObject);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000430}
431
432
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000433Handle<Map> Factory::CopyMapDropDescriptors(Handle<Map> src) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000434 CALL_HEAP_FUNCTION(isolate(), src->CopyDropDescriptors(), Map);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000435}
436
437
ager@chromium.org32912102009-01-16 10:38:43 +0000438Handle<Map> Factory::CopyMap(Handle<Map> src,
439 int extra_inobject_properties) {
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000440 Handle<Map> copy = CopyMapDropDescriptors(src);
ager@chromium.org32912102009-01-16 10:38:43 +0000441 // Check that we do not overflow the instance size when adding the
442 // extra inobject properties.
443 int instance_size_delta = extra_inobject_properties * kPointerSize;
444 int max_instance_size_delta =
445 JSObject::kMaxInstanceSize - copy->instance_size();
446 if (instance_size_delta > max_instance_size_delta) {
447 // If the instance size overflows, we allocate as many properties
448 // as we can as inobject properties.
449 instance_size_delta = max_instance_size_delta;
450 extra_inobject_properties = max_instance_size_delta >> kPointerSizeLog2;
451 }
452 // Adjust the map with the extra inobject properties.
453 int inobject_properties =
454 copy->inobject_properties() + extra_inobject_properties;
455 copy->set_inobject_properties(inobject_properties);
456 copy->set_unused_property_fields(inobject_properties);
457 copy->set_instance_size(copy->instance_size() + instance_size_delta);
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000458 copy->set_visitor_id(StaticVisitorBase::GetVisitorId(*copy));
ager@chromium.org32912102009-01-16 10:38:43 +0000459 return copy;
460}
461
fschneider@chromium.org40b9da32010-06-28 11:29:21 +0000462
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000463Handle<Map> Factory::CopyMapDropTransitions(Handle<Map> src) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000464 CALL_HEAP_FUNCTION(isolate(), src->CopyDropTransitions(), Map);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000465}
466
467
kmillikin@chromium.org83e16822011-09-13 08:21:47 +0000468Handle<Map> Factory::GetElementsTransitionMap(
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000469 Handle<JSObject> src,
470 ElementsKind elements_kind) {
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000471 CALL_HEAP_FUNCTION(isolate(),
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000472 src->GetElementsTransitionMap(elements_kind),
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000473 Map);
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +0000474}
475
476
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000477Handle<FixedArray> Factory::CopyFixedArray(Handle<FixedArray> array) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000478 CALL_HEAP_FUNCTION(isolate(), array->Copy(), FixedArray);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000479}
480
481
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000482Handle<FixedDoubleArray> Factory::CopyFixedDoubleArray(
483 Handle<FixedDoubleArray> array) {
484 CALL_HEAP_FUNCTION(isolate(), array->Copy(), FixedDoubleArray);
485}
486
487
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000488Handle<JSFunction> Factory::BaseNewFunctionFromSharedFunctionInfo(
489 Handle<SharedFunctionInfo> function_info,
sgjesse@chromium.org846fb742009-12-18 08:56:33 +0000490 Handle<Map> function_map,
491 PretenureFlag pretenure) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000492 CALL_HEAP_FUNCTION(
493 isolate(),
494 isolate()->heap()->AllocateFunction(*function_map,
495 *function_info,
496 isolate()->heap()->the_hole_value(),
497 pretenure),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000498 JSFunction);
499}
500
501
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000502Handle<JSFunction> Factory::NewFunctionFromSharedFunctionInfo(
503 Handle<SharedFunctionInfo> function_info,
sgjesse@chromium.org846fb742009-12-18 08:56:33 +0000504 Handle<Context> context,
505 PretenureFlag pretenure) {
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000506 Handle<JSFunction> result = BaseNewFunctionFromSharedFunctionInfo(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000507 function_info,
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000508 function_info->is_classic_mode()
509 ? isolate()->function_map()
510 : isolate()->strict_mode_function_map(),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000511 pretenure);
512
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000513 result->set_context(*context);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000514 if (!function_info->bound()) {
515 int number_of_literals = function_info->num_literals();
516 Handle<FixedArray> literals = NewFixedArray(number_of_literals, pretenure);
517 if (number_of_literals > 0) {
518 // Store the object, regexp and array functions in the literals
519 // array prefix. These functions will be used when creating
520 // object, regexp and array literals in this function.
521 literals->set(JSFunction::kLiteralGlobalContextIndex,
522 context->global_context());
523 }
524 result->set_literals(*literals);
525 } else {
526 result->set_function_bindings(isolate()->heap()->empty_fixed_array());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000527 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000528 result->set_next_function_link(isolate()->heap()->undefined_value());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000529
530 if (V8::UseCrankshaft() &&
531 FLAG_always_opt &&
532 result->is_compiled() &&
533 !function_info->is_toplevel() &&
534 function_info->allows_lazy_compilation()) {
535 result->MarkForLazyRecompilation();
536 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000537 return result;
538}
539
540
541Handle<Object> Factory::NewNumber(double value,
542 PretenureFlag pretenure) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000543 CALL_HEAP_FUNCTION(
544 isolate(),
545 isolate()->heap()->NumberFromDouble(value, pretenure), Object);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000546}
547
548
erikcorry0ad885c2011-11-21 13:51:57 +0000549Handle<Object> Factory::NewNumberFromInt(int32_t value,
550 PretenureFlag pretenure) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000551 CALL_HEAP_FUNCTION(
552 isolate(),
erikcorry0ad885c2011-11-21 13:51:57 +0000553 isolate()->heap()->NumberFromInt32(value, pretenure), Object);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000554}
555
556
erikcorry0ad885c2011-11-21 13:51:57 +0000557Handle<Object> Factory::NewNumberFromUint(uint32_t value,
558 PretenureFlag pretenure) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000559 CALL_HEAP_FUNCTION(
560 isolate(),
erikcorry0ad885c2011-11-21 13:51:57 +0000561 isolate()->heap()->NumberFromUint32(value, pretenure), Object);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000562}
563
564
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000565Handle<JSObject> Factory::NewNeanderObject() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000566 CALL_HEAP_FUNCTION(
567 isolate(),
568 isolate()->heap()->AllocateJSObjectFromMap(
569 isolate()->heap()->neander_map()),
570 JSObject);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000571}
572
573
574Handle<Object> Factory::NewTypeError(const char* type,
575 Vector< Handle<Object> > args) {
576 return NewError("MakeTypeError", type, args);
577}
578
579
580Handle<Object> Factory::NewTypeError(Handle<String> message) {
581 return NewError("$TypeError", message);
582}
583
584
585Handle<Object> Factory::NewRangeError(const char* type,
586 Vector< Handle<Object> > args) {
587 return NewError("MakeRangeError", type, args);
588}
589
590
591Handle<Object> Factory::NewRangeError(Handle<String> message) {
592 return NewError("$RangeError", message);
593}
594
595
596Handle<Object> Factory::NewSyntaxError(const char* type, Handle<JSArray> args) {
597 return NewError("MakeSyntaxError", type, args);
598}
599
600
601Handle<Object> Factory::NewSyntaxError(Handle<String> message) {
602 return NewError("$SyntaxError", message);
603}
604
605
606Handle<Object> Factory::NewReferenceError(const char* type,
607 Vector< Handle<Object> > args) {
608 return NewError("MakeReferenceError", type, args);
609}
610
611
612Handle<Object> Factory::NewReferenceError(Handle<String> message) {
613 return NewError("$ReferenceError", message);
614}
615
616
617Handle<Object> Factory::NewError(const char* maker, const char* type,
618 Vector< Handle<Object> > args) {
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000619 v8::HandleScope scope; // Instantiate a closeable HandleScope for EscapeFrom.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000620 Handle<FixedArray> array = NewFixedArray(args.length());
ager@chromium.org7c537e22008-10-16 08:43:32 +0000621 for (int i = 0; i < args.length(); i++) {
622 array->set(i, *args[i]);
623 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000624 Handle<JSArray> object = NewJSArrayWithElements(array);
ager@chromium.org7c537e22008-10-16 08:43:32 +0000625 Handle<Object> result = NewError(maker, type, object);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000626 return result.EscapeFrom(&scope);
627}
628
629
630Handle<Object> Factory::NewEvalError(const char* type,
631 Vector< Handle<Object> > args) {
632 return NewError("MakeEvalError", type, args);
633}
634
635
636Handle<Object> Factory::NewError(const char* type,
637 Vector< Handle<Object> > args) {
638 return NewError("MakeError", type, args);
639}
640
641
642Handle<Object> Factory::NewError(const char* maker,
643 const char* type,
644 Handle<JSArray> args) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000645 Handle<String> make_str = LookupAsciiSymbol(maker);
646 Handle<Object> fun_obj(
647 isolate()->js_builtins_object()->GetPropertyNoExceptionThrown(*make_str));
ager@chromium.org4af710e2009-09-15 12:20:11 +0000648 // If the builtins haven't been properly configured yet this error
649 // constructor may not have been defined. Bail out.
650 if (!fun_obj->IsJSFunction())
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000651 return undefined_value();
ager@chromium.org4af710e2009-09-15 12:20:11 +0000652 Handle<JSFunction> fun = Handle<JSFunction>::cast(fun_obj);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000653 Handle<Object> type_obj = LookupAsciiSymbol(type);
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +0000654 Handle<Object> argv[] = { type_obj, args };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000655
656 // Invoke the JavaScript factory method. If an exception is thrown while
657 // running the factory method, use the exception as the result.
658 bool caught_exception;
659 Handle<Object> result = Execution::TryCall(fun,
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +0000660 isolate()->js_builtins_object(),
661 ARRAY_SIZE(argv),
662 argv,
663 &caught_exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000664 return result;
665}
666
667
668Handle<Object> Factory::NewError(Handle<String> message) {
669 return NewError("$Error", message);
670}
671
672
673Handle<Object> Factory::NewError(const char* constructor,
674 Handle<String> message) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000675 Handle<String> constr = LookupAsciiSymbol(constructor);
676 Handle<JSFunction> fun = Handle<JSFunction>(
677 JSFunction::cast(isolate()->js_builtins_object()->
678 GetPropertyNoExceptionThrown(*constr)));
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +0000679 Handle<Object> argv[] = { message };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000680
681 // Invoke the JavaScript factory method. If an exception is thrown while
682 // running the factory method, use the exception as the result.
683 bool caught_exception;
684 Handle<Object> result = Execution::TryCall(fun,
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +0000685 isolate()->js_builtins_object(),
686 ARRAY_SIZE(argv),
687 argv,
688 &caught_exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000689 return result;
690}
691
692
693Handle<JSFunction> Factory::NewFunction(Handle<String> name,
694 InstanceType type,
695 int instance_size,
696 Handle<Code> code,
697 bool force_initial_map) {
698 // Allocate the function
699 Handle<JSFunction> function = NewFunction(name, the_hole_value());
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000700
701 // Setup the code pointer in both the shared function info and in
702 // the function itself.
703 function->shared()->set_code(*code);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000704 function->set_code(*code);
705
706 if (force_initial_map ||
707 type != JS_OBJECT_TYPE ||
708 instance_size != JSObject::kHeaderSize) {
709 Handle<Map> initial_map = NewMap(type, instance_size);
710 Handle<JSObject> prototype = NewFunctionPrototype(function);
711 initial_map->set_prototype(*prototype);
712 function->set_initial_map(*initial_map);
713 initial_map->set_constructor(*function);
714 } else {
715 ASSERT(!function->has_initial_map());
716 ASSERT(!function->has_prototype());
717 }
718
719 return function;
720}
721
722
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000723Handle<JSFunction> Factory::NewFunctionWithPrototype(Handle<String> name,
724 InstanceType type,
725 int instance_size,
726 Handle<JSObject> prototype,
727 Handle<Code> code,
728 bool force_initial_map) {
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000729 // Allocate the function.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000730 Handle<JSFunction> function = NewFunction(name, prototype);
731
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000732 // Setup the code pointer in both the shared function info and in
733 // the function itself.
734 function->shared()->set_code(*code);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000735 function->set_code(*code);
736
737 if (force_initial_map ||
738 type != JS_OBJECT_TYPE ||
739 instance_size != JSObject::kHeaderSize) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000740 ElementsKind default_elements_kind = FLAG_smi_only_arrays
741 ? FAST_SMI_ONLY_ELEMENTS
742 : FAST_ELEMENTS;
743 Handle<Map> initial_map = NewMap(type,
744 instance_size,
745 default_elements_kind);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000746 function->set_initial_map(*initial_map);
747 initial_map->set_constructor(*function);
748 }
749
750 // Set function.prototype and give the prototype a constructor
751 // property that refers to the function.
752 SetPrototypeProperty(function, prototype);
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +0000753 // Currently safe because it is only invoked from Genesis.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000754 SetLocalPropertyNoThrow(prototype, constructor_symbol(), function, DONT_ENUM);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000755 return function;
756}
757
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000758
kmillikin@chromium.org4111b802010-05-03 10:34:42 +0000759Handle<JSFunction> Factory::NewFunctionWithoutPrototype(Handle<String> name,
760 Handle<Code> code) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000761 Handle<JSFunction> function = NewFunctionWithoutPrototype(name,
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000762 CLASSIC_MODE);
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000763 function->shared()->set_code(*code);
kmillikin@chromium.org4111b802010-05-03 10:34:42 +0000764 function->set_code(*code);
765 ASSERT(!function->has_initial_map());
766 ASSERT(!function->has_prototype());
767 return function;
768}
769
770
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000771Handle<ScopeInfo> Factory::NewScopeInfo(int length) {
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000772 CALL_HEAP_FUNCTION(
773 isolate(),
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000774 isolate()->heap()->AllocateScopeInfo(length),
775 ScopeInfo);
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000776}
777
778
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000779Handle<Code> Factory::NewCode(const CodeDesc& desc,
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000780 Code::Flags flags,
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +0000781 Handle<Object> self_ref,
782 bool immovable) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000783 CALL_HEAP_FUNCTION(isolate(),
784 isolate()->heap()->CreateCode(
785 desc, flags, self_ref, immovable),
786 Code);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000787}
788
789
790Handle<Code> Factory::CopyCode(Handle<Code> code) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000791 CALL_HEAP_FUNCTION(isolate(),
792 isolate()->heap()->CopyCode(*code),
793 Code);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000794}
795
796
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000797Handle<Code> Factory::CopyCode(Handle<Code> code, Vector<byte> reloc_info) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000798 CALL_HEAP_FUNCTION(isolate(),
799 isolate()->heap()->CopyCode(*code, reloc_info),
800 Code);
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000801}
802
803
lrn@chromium.org303ada72010-10-27 09:33:13 +0000804MUST_USE_RESULT static inline MaybeObject* DoCopyInsert(
805 DescriptorArray* array,
806 String* key,
807 Object* value,
808 PropertyAttributes attributes) {
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000809 CallbacksDescriptor desc(key, value, attributes);
lrn@chromium.org303ada72010-10-27 09:33:13 +0000810 MaybeObject* obj = array->CopyInsert(&desc, REMOVE_TRANSITIONS);
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000811 return obj;
812}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000813
814
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000815// Allocate the new array.
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000816Handle<DescriptorArray> Factory::CopyAppendForeignDescriptor(
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000817 Handle<DescriptorArray> array,
818 Handle<String> key,
819 Handle<Object> value,
820 PropertyAttributes attributes) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000821 CALL_HEAP_FUNCTION(isolate(),
822 DoCopyInsert(*array, *key, *value, attributes),
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000823 DescriptorArray);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000824}
825
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000826
827Handle<String> Factory::SymbolFromString(Handle<String> value) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000828 CALL_HEAP_FUNCTION(isolate(),
829 isolate()->heap()->LookupSymbol(*value), String);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000830}
831
832
833Handle<DescriptorArray> Factory::CopyAppendCallbackDescriptors(
834 Handle<DescriptorArray> array,
835 Handle<Object> descriptors) {
836 v8::NeanderArray callbacks(descriptors);
837 int nof_callbacks = callbacks.length();
838 Handle<DescriptorArray> result =
839 NewDescriptorArray(array->number_of_descriptors() + nof_callbacks);
840
841 // Number of descriptors added to the result so far.
842 int descriptor_count = 0;
843
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000844 // Ensure that marking will not progress and change color of objects.
845 DescriptorArray::WhitenessWitness witness(*result);
846
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000847 // Copy the descriptors from the array.
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000848 for (int i = 0; i < array->number_of_descriptors(); i++) {
danno@chromium.orgc612e022011-11-10 11:38:15 +0000849 if (!array->IsNullDescriptor(i)) {
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000850 result->CopyFrom(descriptor_count++, *array, i, witness);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000851 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000852 }
853
854 // Number of duplicates detected.
855 int duplicates = 0;
856
857 // Fill in new callback descriptors. Process the callbacks from
858 // back to front so that the last callback with a given name takes
859 // precedence over previously added callbacks with that name.
860 for (int i = nof_callbacks - 1; i >= 0; i--) {
861 Handle<AccessorInfo> entry =
862 Handle<AccessorInfo>(AccessorInfo::cast(callbacks.get(i)));
863 // Ensure the key is a symbol before writing into the instance descriptor.
864 Handle<String> key =
865 SymbolFromString(Handle<String>(String::cast(entry->name())));
866 // Check if a descriptor with this name already exists before writing.
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000867 if (result->LinearSearch(*key, descriptor_count) ==
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000868 DescriptorArray::kNotFound) {
869 CallbacksDescriptor desc(*key, *entry, entry->property_attributes());
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000870 result->Set(descriptor_count, &desc, witness);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000871 descriptor_count++;
872 } else {
873 duplicates++;
874 }
875 }
876
877 // If duplicates were detected, allocate a result of the right size
878 // and transfer the elements.
879 if (duplicates > 0) {
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000880 int number_of_descriptors = result->number_of_descriptors() - duplicates;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000881 Handle<DescriptorArray> new_result =
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000882 NewDescriptorArray(number_of_descriptors);
883 for (int i = 0; i < number_of_descriptors; i++) {
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000884 new_result->CopyFrom(i, *result, i, witness);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000885 }
886 result = new_result;
887 }
888
889 // Sort the result before returning.
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000890 result->Sort(witness);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000891 return result;
892}
893
894
895Handle<JSObject> Factory::NewJSObject(Handle<JSFunction> constructor,
896 PretenureFlag pretenure) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000897 CALL_HEAP_FUNCTION(
898 isolate(),
899 isolate()->heap()->AllocateJSObject(*constructor, pretenure), JSObject);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000900}
901
902
kasperl@chromium.org2abc4502009-07-02 07:00:29 +0000903Handle<GlobalObject> Factory::NewGlobalObject(
904 Handle<JSFunction> constructor) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000905 CALL_HEAP_FUNCTION(isolate(),
906 isolate()->heap()->AllocateGlobalObject(*constructor),
kasperl@chromium.org2abc4502009-07-02 07:00:29 +0000907 GlobalObject);
908}
909
910
911
ager@chromium.org236ad962008-09-25 09:45:57 +0000912Handle<JSObject> Factory::NewJSObjectFromMap(Handle<Map> map) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000913 CALL_HEAP_FUNCTION(
914 isolate(),
915 isolate()->heap()->AllocateJSObjectFromMap(*map, NOT_TENURED),
916 JSObject);
ager@chromium.org236ad962008-09-25 09:45:57 +0000917}
918
919
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +0000920Handle<JSArray> Factory::NewJSArray(int capacity,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000921 PretenureFlag pretenure) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000922 Handle<JSObject> obj = NewJSObject(isolate()->array_function(), pretenure);
923 CALL_HEAP_FUNCTION(isolate(),
924 Handle<JSArray>::cast(obj)->Initialize(capacity),
925 JSArray);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000926}
927
928
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +0000929Handle<JSArray> Factory::NewJSArrayWithElements(Handle<FixedArrayBase> elements,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000930 PretenureFlag pretenure) {
931 Handle<JSArray> result =
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000932 Handle<JSArray>::cast(NewJSObject(isolate()->array_function(),
933 pretenure));
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +0000934 result->set_length(Smi::FromInt(0));
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000935 SetContent(result, elements);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000936 return result;
937}
938
939
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +0000940void Factory::SetElementsCapacityAndLength(Handle<JSArray> array,
941 int capacity,
942 int length) {
943 ElementsAccessor* accessor = array->GetElementsAccessor();
944 CALL_HEAP_FUNCTION_VOID(
945 isolate(),
946 accessor->SetCapacityAndLength(*array, capacity, length));
947}
948
949
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000950void Factory::SetContent(Handle<JSArray> array,
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +0000951 Handle<FixedArrayBase> elements) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000952 CALL_HEAP_FUNCTION_VOID(
953 isolate(),
954 array->SetContent(*elements));
955}
956
957
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +0000958void Factory::EnsureCanContainHeapObjectElements(Handle<JSArray> array) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000959 CALL_HEAP_FUNCTION_VOID(
960 isolate(),
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +0000961 array->EnsureCanContainHeapObjectElements());
962}
963
964
965void Factory::EnsureCanContainElements(Handle<JSArray> array,
966 Handle<FixedArrayBase> elements,
967 EnsureElementsMode mode) {
968 CALL_HEAP_FUNCTION_VOID(
969 isolate(),
970 array->EnsureCanContainElements(*elements, mode));
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000971}
972
973
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000974Handle<JSProxy> Factory::NewJSProxy(Handle<Object> handler,
975 Handle<Object> prototype) {
976 CALL_HEAP_FUNCTION(
977 isolate(),
978 isolate()->heap()->AllocateJSProxy(*handler, *prototype),
979 JSProxy);
980}
981
982
lrn@chromium.org34e60782011-09-15 07:25:40 +0000983void Factory::BecomeJSObject(Handle<JSReceiver> object) {
rossberg@chromium.org717967f2011-07-20 13:44:42 +0000984 CALL_HEAP_FUNCTION_VOID(
985 isolate(),
lrn@chromium.org34e60782011-09-15 07:25:40 +0000986 isolate()->heap()->ReinitializeJSReceiver(
987 *object, JS_OBJECT_TYPE, JSObject::kHeaderSize));
988}
989
990
991void Factory::BecomeJSFunction(Handle<JSReceiver> object) {
992 CALL_HEAP_FUNCTION_VOID(
993 isolate(),
994 isolate()->heap()->ReinitializeJSReceiver(
995 *object, JS_FUNCTION_TYPE, JSFunction::kSize));
rossberg@chromium.org717967f2011-07-20 13:44:42 +0000996}
997
998
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000999void Factory::SetIdentityHash(Handle<JSObject> object, Object* hash) {
1000 CALL_HEAP_FUNCTION_VOID(
1001 isolate(),
1002 object->SetIdentityHash(hash, ALLOW_CREATION));
1003}
1004
1005
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001006Handle<SharedFunctionInfo> Factory::NewSharedFunctionInfo(
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00001007 Handle<String> name,
1008 int number_of_literals,
1009 Handle<Code> code,
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001010 Handle<ScopeInfo> scope_info) {
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001011 Handle<SharedFunctionInfo> shared = NewSharedFunctionInfo(name);
1012 shared->set_code(*code);
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00001013 shared->set_scope_info(*scope_info);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001014 int literals_array_size = number_of_literals;
1015 // If the function contains object, regexp or array literals,
1016 // allocate extra space for a literals array prefix containing the
1017 // context.
1018 if (number_of_literals > 0) {
1019 literals_array_size += JSFunction::kLiteralsPrefixSize;
1020 }
1021 shared->set_num_literals(literals_array_size);
1022 return shared;
1023}
1024
1025
kmillikin@chromium.org31b12772011-02-02 16:08:26 +00001026Handle<JSMessageObject> Factory::NewJSMessageObject(
1027 Handle<String> type,
1028 Handle<JSArray> arguments,
1029 int start_position,
1030 int end_position,
1031 Handle<Object> script,
1032 Handle<Object> stack_trace,
1033 Handle<Object> stack_frames) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001034 CALL_HEAP_FUNCTION(isolate(),
1035 isolate()->heap()->AllocateJSMessageObject(*type,
1036 *arguments,
1037 start_position,
1038 end_position,
1039 *script,
1040 *stack_trace,
1041 *stack_frames),
kmillikin@chromium.org31b12772011-02-02 16:08:26 +00001042 JSMessageObject);
1043}
1044
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001045Handle<SharedFunctionInfo> Factory::NewSharedFunctionInfo(Handle<String> name) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001046 CALL_HEAP_FUNCTION(isolate(),
1047 isolate()->heap()->AllocateSharedFunctionInfo(*name),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001048 SharedFunctionInfo);
1049}
1050
1051
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00001052Handle<String> Factory::NumberToString(Handle<Object> number) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001053 CALL_HEAP_FUNCTION(isolate(),
1054 isolate()->heap()->NumberToString(*number), String);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00001055}
1056
1057
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001058Handle<String> Factory::Uint32ToString(uint32_t value) {
1059 CALL_HEAP_FUNCTION(isolate(),
1060 isolate()->heap()->Uint32ToString(value), String);
1061}
1062
1063
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00001064Handle<NumberDictionary> Factory::DictionaryAtNumberPut(
1065 Handle<NumberDictionary> dictionary,
1066 uint32_t key,
1067 Handle<Object> value) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001068 CALL_HEAP_FUNCTION(isolate(),
1069 dictionary->AtNumberPut(key, *value),
1070 NumberDictionary);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001071}
1072
1073
1074Handle<JSFunction> Factory::NewFunctionHelper(Handle<String> name,
1075 Handle<Object> prototype) {
1076 Handle<SharedFunctionInfo> function_share = NewSharedFunctionInfo(name);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001077 CALL_HEAP_FUNCTION(
1078 isolate(),
1079 isolate()->heap()->AllocateFunction(*isolate()->function_map(),
1080 *function_share,
1081 *prototype),
1082 JSFunction);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001083}
1084
1085
1086Handle<JSFunction> Factory::NewFunction(Handle<String> name,
1087 Handle<Object> prototype) {
1088 Handle<JSFunction> fun = NewFunctionHelper(name, prototype);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001089 fun->set_context(isolate()->context()->global_context());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001090 return fun;
1091}
1092
1093
kmillikin@chromium.org4111b802010-05-03 10:34:42 +00001094Handle<JSFunction> Factory::NewFunctionWithoutPrototypeHelper(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001095 Handle<String> name,
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00001096 LanguageMode language_mode) {
kmillikin@chromium.org4111b802010-05-03 10:34:42 +00001097 Handle<SharedFunctionInfo> function_share = NewSharedFunctionInfo(name);
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00001098 Handle<Map> map = (language_mode == CLASSIC_MODE)
1099 ? isolate()->function_without_prototype_map()
1100 : isolate()->strict_mode_function_without_prototype_map();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001101 CALL_HEAP_FUNCTION(isolate(),
1102 isolate()->heap()->AllocateFunction(
1103 *map,
kmillikin@chromium.org4111b802010-05-03 10:34:42 +00001104 *function_share,
1105 *the_hole_value()),
1106 JSFunction);
1107}
1108
1109
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001110Handle<JSFunction> Factory::NewFunctionWithoutPrototype(
1111 Handle<String> name,
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00001112 LanguageMode language_mode) {
1113 Handle<JSFunction> fun =
1114 NewFunctionWithoutPrototypeHelper(name, language_mode);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001115 fun->set_context(isolate()->context()->global_context());
kmillikin@chromium.org4111b802010-05-03 10:34:42 +00001116 return fun;
1117}
1118
1119
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001120Handle<Object> Factory::ToObject(Handle<Object> object) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001121 CALL_HEAP_FUNCTION(isolate(), object->ToObject(), Object);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001122}
1123
1124
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001125Handle<Object> Factory::ToObject(Handle<Object> object,
1126 Handle<Context> global_context) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001127 CALL_HEAP_FUNCTION(isolate(), object->ToObject(*global_context), Object);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001128}
1129
1130
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001131#ifdef ENABLE_DEBUGGER_SUPPORT
v8.team.kasperl727e9952008-09-02 14:56:44 +00001132Handle<DebugInfo> Factory::NewDebugInfo(Handle<SharedFunctionInfo> shared) {
1133 // Get the original code of the function.
1134 Handle<Code> code(shared->code());
1135
1136 // Create a copy of the code before allocating the debug info object to avoid
1137 // allocation while setting up the debug info object.
1138 Handle<Code> original_code(*Factory::CopyCode(code));
1139
1140 // Allocate initial fixed array for active break points before allocating the
1141 // debug info object to avoid allocation while setting up the debug info
1142 // object.
1143 Handle<FixedArray> break_points(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001144 NewFixedArray(Debug::kEstimatedNofBreakPointsInFunction));
v8.team.kasperl727e9952008-09-02 14:56:44 +00001145
1146 // Create and set up the debug info object. Debug info contains function, a
1147 // copy of the original code, the executing code and initial fixed array for
1148 // active break points.
1149 Handle<DebugInfo> debug_info =
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001150 Handle<DebugInfo>::cast(NewStruct(DEBUG_INFO_TYPE));
v8.team.kasperl727e9952008-09-02 14:56:44 +00001151 debug_info->set_shared(*shared);
1152 debug_info->set_original_code(*original_code);
1153 debug_info->set_code(*code);
1154 debug_info->set_break_points(*break_points);
1155
1156 // Link debug info to function.
1157 shared->set_debug_info(*debug_info);
1158
1159 return debug_info;
1160}
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001161#endif
v8.team.kasperl727e9952008-09-02 14:56:44 +00001162
1163
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001164Handle<JSObject> Factory::NewArgumentsObject(Handle<Object> callee,
1165 int length) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001166 CALL_HEAP_FUNCTION(
1167 isolate(),
1168 isolate()->heap()->AllocateArgumentsObject(*callee, length), JSObject);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001169}
1170
1171
1172Handle<JSFunction> Factory::CreateApiFunction(
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001173 Handle<FunctionTemplateInfo> obj, ApiInstanceType instance_type) {
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00001174 Handle<Code> code = isolate()->builtins()->HandleApiCall();
1175 Handle<Code> construct_stub = isolate()->builtins()->JSConstructStubApi();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001176
kasper.lund212ac232008-07-16 07:07:30 +00001177 int internal_field_count = 0;
1178 if (!obj->instance_template()->IsUndefined()) {
1179 Handle<ObjectTemplateInfo> instance_template =
1180 Handle<ObjectTemplateInfo>(
1181 ObjectTemplateInfo::cast(obj->instance_template()));
1182 internal_field_count =
1183 Smi::cast(instance_template->internal_field_count())->value();
1184 }
1185
1186 int instance_size = kPointerSize * internal_field_count;
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001187 InstanceType type = INVALID_TYPE;
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001188 switch (instance_type) {
1189 case JavaScriptObject:
1190 type = JS_OBJECT_TYPE;
1191 instance_size += JSObject::kHeaderSize;
1192 break;
1193 case InnerGlobalObject:
1194 type = JS_GLOBAL_OBJECT_TYPE;
1195 instance_size += JSGlobalObject::kSize;
1196 break;
1197 case OuterGlobalObject:
1198 type = JS_GLOBAL_PROXY_TYPE;
1199 instance_size += JSGlobalProxy::kSize;
1200 break;
1201 default:
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001202 break;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001203 }
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001204 ASSERT(type != INVALID_TYPE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001205
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001206 Handle<JSFunction> result =
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001207 NewFunction(Factory::empty_symbol(),
1208 type,
1209 instance_size,
1210 code,
1211 true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001212 // Set class name.
1213 Handle<Object> class_name = Handle<Object>(obj->class_name());
1214 if (class_name->IsString()) {
1215 result->shared()->set_instance_class_name(*class_name);
1216 result->shared()->set_name(*class_name);
1217 }
1218
1219 Handle<Map> map = Handle<Map>(result->initial_map());
1220
1221 // Mark as undetectable if needed.
1222 if (obj->undetectable()) {
1223 map->set_is_undetectable();
1224 }
1225
1226 // Mark as hidden for the __proto__ accessor if needed.
1227 if (obj->hidden_prototype()) {
1228 map->set_is_hidden_prototype();
1229 }
1230
1231 // Mark as needs_access_check if needed.
1232 if (obj->needs_access_check()) {
ager@chromium.org870a0b62008-11-04 11:43:05 +00001233 map->set_is_access_check_needed(true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001234 }
1235
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001236 // Set interceptor information in the map.
1237 if (!obj->named_property_handler()->IsUndefined()) {
1238 map->set_has_named_interceptor();
1239 }
1240 if (!obj->indexed_property_handler()->IsUndefined()) {
1241 map->set_has_indexed_interceptor();
1242 }
1243
1244 // Set instance call-as-function information in the map.
1245 if (!obj->instance_call_handler()->IsUndefined()) {
1246 map->set_has_instance_call_handler();
1247 }
1248
1249 result->shared()->set_function_data(*obj);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001250 result->shared()->set_construct_stub(*construct_stub);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001251 result->shared()->DontAdaptArguments();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001252
1253 // Recursively copy parent templates' accessors, 'data' may be modified.
1254 Handle<DescriptorArray> array =
1255 Handle<DescriptorArray>(map->instance_descriptors());
1256 while (true) {
1257 Handle<Object> props = Handle<Object>(obj->property_accessors());
1258 if (!props->IsUndefined()) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001259 array = CopyAppendCallbackDescriptors(array, props);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001260 }
1261 Handle<Object> parent = Handle<Object>(obj->parent_template());
1262 if (parent->IsUndefined()) break;
1263 obj = Handle<FunctionTemplateInfo>::cast(parent);
1264 }
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001265 if (!array->IsEmpty()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001266 map->set_instance_descriptors(*array);
1267 }
1268
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001269 ASSERT(result->shared()->IsApiFunction());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001270 return result;
1271}
1272
1273
ager@chromium.org236ad962008-09-25 09:45:57 +00001274Handle<MapCache> Factory::NewMapCache(int at_least_space_for) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001275 CALL_HEAP_FUNCTION(isolate(),
1276 MapCache::Allocate(at_least_space_for), MapCache);
ager@chromium.org236ad962008-09-25 09:45:57 +00001277}
1278
1279
lrn@chromium.org303ada72010-10-27 09:33:13 +00001280MUST_USE_RESULT static MaybeObject* UpdateMapCacheWith(Context* context,
1281 FixedArray* keys,
1282 Map* map) {
1283 Object* result;
1284 { MaybeObject* maybe_result =
1285 MapCache::cast(context->map_cache())->Put(keys, map);
1286 if (!maybe_result->ToObject(&result)) return maybe_result;
1287 }
1288 context->set_map_cache(MapCache::cast(result));
ager@chromium.org236ad962008-09-25 09:45:57 +00001289 return result;
1290}
1291
1292
1293Handle<MapCache> Factory::AddToMapCache(Handle<Context> context,
1294 Handle<FixedArray> keys,
1295 Handle<Map> map) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001296 CALL_HEAP_FUNCTION(isolate(),
1297 UpdateMapCacheWith(*context, *keys, *map), MapCache);
ager@chromium.org236ad962008-09-25 09:45:57 +00001298}
1299
1300
1301Handle<Map> Factory::ObjectLiteralMapFromCache(Handle<Context> context,
1302 Handle<FixedArray> keys) {
1303 if (context->map_cache()->IsUndefined()) {
1304 // Allocate the new map cache for the global context.
1305 Handle<MapCache> new_cache = NewMapCache(24);
1306 context->set_map_cache(*new_cache);
1307 }
ager@chromium.org32912102009-01-16 10:38:43 +00001308 // Check to see whether there is a matching element in the cache.
ager@chromium.org236ad962008-09-25 09:45:57 +00001309 Handle<MapCache> cache =
1310 Handle<MapCache>(MapCache::cast(context->map_cache()));
1311 Handle<Object> result = Handle<Object>(cache->Lookup(*keys));
1312 if (result->IsMap()) return Handle<Map>::cast(result);
1313 // Create a new map and add it to the cache.
1314 Handle<Map> map =
ager@chromium.org32912102009-01-16 10:38:43 +00001315 CopyMap(Handle<Map>(context->object_function()->initial_map()),
1316 keys->length());
ager@chromium.org236ad962008-09-25 09:45:57 +00001317 AddToMapCache(context, keys, map);
1318 return Handle<Map>(map);
1319}
1320
1321
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001322void Factory::SetRegExpAtomData(Handle<JSRegExp> regexp,
1323 JSRegExp::Type type,
1324 Handle<String> source,
1325 JSRegExp::Flags flags,
1326 Handle<Object> data) {
1327 Handle<FixedArray> store = NewFixedArray(JSRegExp::kAtomDataSize);
1328
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001329 store->set(JSRegExp::kTagIndex, Smi::FromInt(type));
1330 store->set(JSRegExp::kSourceIndex, *source);
1331 store->set(JSRegExp::kFlagsIndex, Smi::FromInt(flags.value()));
1332 store->set(JSRegExp::kAtomPatternIndex, *data);
1333 regexp->set_data(*store);
1334}
1335
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001336void Factory::SetRegExpIrregexpData(Handle<JSRegExp> regexp,
1337 JSRegExp::Type type,
1338 Handle<String> source,
1339 JSRegExp::Flags flags,
1340 int capture_count) {
1341 Handle<FixedArray> store = NewFixedArray(JSRegExp::kIrregexpDataSize);
jkummerow@chromium.orgddda9e82011-07-06 11:27:02 +00001342 Smi* uninitialized = Smi::FromInt(JSRegExp::kUninitializedValue);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001343 store->set(JSRegExp::kTagIndex, Smi::FromInt(type));
1344 store->set(JSRegExp::kSourceIndex, *source);
1345 store->set(JSRegExp::kFlagsIndex, Smi::FromInt(flags.value()));
jkummerow@chromium.orgddda9e82011-07-06 11:27:02 +00001346 store->set(JSRegExp::kIrregexpASCIICodeIndex, uninitialized);
1347 store->set(JSRegExp::kIrregexpUC16CodeIndex, uninitialized);
1348 store->set(JSRegExp::kIrregexpASCIICodeSavedIndex, uninitialized);
1349 store->set(JSRegExp::kIrregexpUC16CodeSavedIndex, uninitialized);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001350 store->set(JSRegExp::kIrregexpMaxRegisterCountIndex, Smi::FromInt(0));
1351 store->set(JSRegExp::kIrregexpCaptureCountIndex,
1352 Smi::FromInt(capture_count));
1353 regexp->set_data(*store);
1354}
1355
1356
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001357
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001358void Factory::ConfigureInstance(Handle<FunctionTemplateInfo> desc,
1359 Handle<JSObject> instance,
1360 bool* pending_exception) {
1361 // Configure the instance by adding the properties specified by the
1362 // instance template.
1363 Handle<Object> instance_template = Handle<Object>(desc->instance_template());
1364 if (!instance_template->IsUndefined()) {
1365 Execution::ConfigureInstance(instance,
1366 instance_template,
1367 pending_exception);
1368 } else {
1369 *pending_exception = false;
1370 }
1371}
1372
1373
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001374Handle<Object> Factory::GlobalConstantFor(Handle<String> name) {
1375 Heap* h = isolate()->heap();
1376 if (name->Equals(h->undefined_symbol())) return undefined_value();
1377 if (name->Equals(h->nan_symbol())) return nan_value();
1378 if (name->Equals(h->infinity_symbol())) return infinity_value();
1379 return Handle<Object>::null();
1380}
1381
1382
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00001383Handle<Object> Factory::ToBoolean(bool value) {
1384 return Handle<Object>(value
1385 ? isolate()->heap()->true_value()
1386 : isolate()->heap()->false_value());
1387}
1388
1389
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001390} } // namespace v8::internal