blob: 16f5fd0ff49a5f16b3e6bf022b39b0c1dfe0bd67 [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,
508 function_info->strict_mode()
509 ? isolate()->strict_mode_function_map()
510 : isolate()->function_map(),
511 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,
762 kNonStrictMode);
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
929Handle<JSArray> Factory::NewJSArrayWithElements(Handle<FixedArray> elements,
930 PretenureFlag pretenure) {
931 Handle<JSArray> result =
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000932 Handle<JSArray>::cast(NewJSObject(isolate()->array_function(),
933 pretenure));
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000934 SetContent(result, elements);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000935 return result;
936}
937
938
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000939void Factory::SetContent(Handle<JSArray> array,
940 Handle<FixedArray> elements) {
941 CALL_HEAP_FUNCTION_VOID(
942 isolate(),
943 array->SetContent(*elements));
944}
945
946
947void Factory::EnsureCanContainNonSmiElements(Handle<JSArray> array) {
948 CALL_HEAP_FUNCTION_VOID(
949 isolate(),
950 array->EnsureCanContainNonSmiElements());
951}
952
953
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000954Handle<JSProxy> Factory::NewJSProxy(Handle<Object> handler,
955 Handle<Object> prototype) {
956 CALL_HEAP_FUNCTION(
957 isolate(),
958 isolate()->heap()->AllocateJSProxy(*handler, *prototype),
959 JSProxy);
960}
961
962
lrn@chromium.org34e60782011-09-15 07:25:40 +0000963void Factory::BecomeJSObject(Handle<JSReceiver> object) {
rossberg@chromium.org717967f2011-07-20 13:44:42 +0000964 CALL_HEAP_FUNCTION_VOID(
965 isolate(),
lrn@chromium.org34e60782011-09-15 07:25:40 +0000966 isolate()->heap()->ReinitializeJSReceiver(
967 *object, JS_OBJECT_TYPE, JSObject::kHeaderSize));
968}
969
970
971void Factory::BecomeJSFunction(Handle<JSReceiver> object) {
972 CALL_HEAP_FUNCTION_VOID(
973 isolate(),
974 isolate()->heap()->ReinitializeJSReceiver(
975 *object, JS_FUNCTION_TYPE, JSFunction::kSize));
rossberg@chromium.org717967f2011-07-20 13:44:42 +0000976}
977
978
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000979void Factory::SetIdentityHash(Handle<JSObject> object, Object* hash) {
980 CALL_HEAP_FUNCTION_VOID(
981 isolate(),
982 object->SetIdentityHash(hash, ALLOW_CREATION));
983}
984
985
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000986Handle<SharedFunctionInfo> Factory::NewSharedFunctionInfo(
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +0000987 Handle<String> name,
988 int number_of_literals,
989 Handle<Code> code,
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000990 Handle<ScopeInfo> scope_info) {
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000991 Handle<SharedFunctionInfo> shared = NewSharedFunctionInfo(name);
992 shared->set_code(*code);
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +0000993 shared->set_scope_info(*scope_info);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000994 int literals_array_size = number_of_literals;
995 // If the function contains object, regexp or array literals,
996 // allocate extra space for a literals array prefix containing the
997 // context.
998 if (number_of_literals > 0) {
999 literals_array_size += JSFunction::kLiteralsPrefixSize;
1000 }
1001 shared->set_num_literals(literals_array_size);
1002 return shared;
1003}
1004
1005
kmillikin@chromium.org31b12772011-02-02 16:08:26 +00001006Handle<JSMessageObject> Factory::NewJSMessageObject(
1007 Handle<String> type,
1008 Handle<JSArray> arguments,
1009 int start_position,
1010 int end_position,
1011 Handle<Object> script,
1012 Handle<Object> stack_trace,
1013 Handle<Object> stack_frames) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001014 CALL_HEAP_FUNCTION(isolate(),
1015 isolate()->heap()->AllocateJSMessageObject(*type,
1016 *arguments,
1017 start_position,
1018 end_position,
1019 *script,
1020 *stack_trace,
1021 *stack_frames),
kmillikin@chromium.org31b12772011-02-02 16:08:26 +00001022 JSMessageObject);
1023}
1024
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001025Handle<SharedFunctionInfo> Factory::NewSharedFunctionInfo(Handle<String> name) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001026 CALL_HEAP_FUNCTION(isolate(),
1027 isolate()->heap()->AllocateSharedFunctionInfo(*name),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001028 SharedFunctionInfo);
1029}
1030
1031
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00001032Handle<String> Factory::NumberToString(Handle<Object> number) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001033 CALL_HEAP_FUNCTION(isolate(),
1034 isolate()->heap()->NumberToString(*number), String);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00001035}
1036
1037
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001038Handle<String> Factory::Uint32ToString(uint32_t value) {
1039 CALL_HEAP_FUNCTION(isolate(),
1040 isolate()->heap()->Uint32ToString(value), String);
1041}
1042
1043
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00001044Handle<NumberDictionary> Factory::DictionaryAtNumberPut(
1045 Handle<NumberDictionary> dictionary,
1046 uint32_t key,
1047 Handle<Object> value) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001048 CALL_HEAP_FUNCTION(isolate(),
1049 dictionary->AtNumberPut(key, *value),
1050 NumberDictionary);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001051}
1052
1053
1054Handle<JSFunction> Factory::NewFunctionHelper(Handle<String> name,
1055 Handle<Object> prototype) {
1056 Handle<SharedFunctionInfo> function_share = NewSharedFunctionInfo(name);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001057 CALL_HEAP_FUNCTION(
1058 isolate(),
1059 isolate()->heap()->AllocateFunction(*isolate()->function_map(),
1060 *function_share,
1061 *prototype),
1062 JSFunction);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001063}
1064
1065
1066Handle<JSFunction> Factory::NewFunction(Handle<String> name,
1067 Handle<Object> prototype) {
1068 Handle<JSFunction> fun = NewFunctionHelper(name, prototype);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001069 fun->set_context(isolate()->context()->global_context());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001070 return fun;
1071}
1072
1073
kmillikin@chromium.org4111b802010-05-03 10:34:42 +00001074Handle<JSFunction> Factory::NewFunctionWithoutPrototypeHelper(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001075 Handle<String> name,
1076 StrictModeFlag strict_mode) {
kmillikin@chromium.org4111b802010-05-03 10:34:42 +00001077 Handle<SharedFunctionInfo> function_share = NewSharedFunctionInfo(name);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001078 Handle<Map> map = strict_mode == kStrictMode
1079 ? isolate()->strict_mode_function_without_prototype_map()
1080 : isolate()->function_without_prototype_map();
1081 CALL_HEAP_FUNCTION(isolate(),
1082 isolate()->heap()->AllocateFunction(
1083 *map,
kmillikin@chromium.org4111b802010-05-03 10:34:42 +00001084 *function_share,
1085 *the_hole_value()),
1086 JSFunction);
1087}
1088
1089
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001090Handle<JSFunction> Factory::NewFunctionWithoutPrototype(
1091 Handle<String> name,
1092 StrictModeFlag strict_mode) {
1093 Handle<JSFunction> fun = NewFunctionWithoutPrototypeHelper(name, strict_mode);
1094 fun->set_context(isolate()->context()->global_context());
kmillikin@chromium.org4111b802010-05-03 10:34:42 +00001095 return fun;
1096}
1097
1098
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001099Handle<Object> Factory::ToObject(Handle<Object> object) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001100 CALL_HEAP_FUNCTION(isolate(), object->ToObject(), Object);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001101}
1102
1103
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001104Handle<Object> Factory::ToObject(Handle<Object> object,
1105 Handle<Context> global_context) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001106 CALL_HEAP_FUNCTION(isolate(), object->ToObject(*global_context), Object);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001107}
1108
1109
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001110#ifdef ENABLE_DEBUGGER_SUPPORT
v8.team.kasperl727e9952008-09-02 14:56:44 +00001111Handle<DebugInfo> Factory::NewDebugInfo(Handle<SharedFunctionInfo> shared) {
1112 // Get the original code of the function.
1113 Handle<Code> code(shared->code());
1114
1115 // Create a copy of the code before allocating the debug info object to avoid
1116 // allocation while setting up the debug info object.
1117 Handle<Code> original_code(*Factory::CopyCode(code));
1118
1119 // Allocate initial fixed array for active break points before allocating the
1120 // debug info object to avoid allocation while setting up the debug info
1121 // object.
1122 Handle<FixedArray> break_points(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001123 NewFixedArray(Debug::kEstimatedNofBreakPointsInFunction));
v8.team.kasperl727e9952008-09-02 14:56:44 +00001124
1125 // Create and set up the debug info object. Debug info contains function, a
1126 // copy of the original code, the executing code and initial fixed array for
1127 // active break points.
1128 Handle<DebugInfo> debug_info =
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001129 Handle<DebugInfo>::cast(NewStruct(DEBUG_INFO_TYPE));
v8.team.kasperl727e9952008-09-02 14:56:44 +00001130 debug_info->set_shared(*shared);
1131 debug_info->set_original_code(*original_code);
1132 debug_info->set_code(*code);
1133 debug_info->set_break_points(*break_points);
1134
1135 // Link debug info to function.
1136 shared->set_debug_info(*debug_info);
1137
1138 return debug_info;
1139}
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001140#endif
v8.team.kasperl727e9952008-09-02 14:56:44 +00001141
1142
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001143Handle<JSObject> Factory::NewArgumentsObject(Handle<Object> callee,
1144 int length) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001145 CALL_HEAP_FUNCTION(
1146 isolate(),
1147 isolate()->heap()->AllocateArgumentsObject(*callee, length), JSObject);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001148}
1149
1150
1151Handle<JSFunction> Factory::CreateApiFunction(
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001152 Handle<FunctionTemplateInfo> obj, ApiInstanceType instance_type) {
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00001153 Handle<Code> code = isolate()->builtins()->HandleApiCall();
1154 Handle<Code> construct_stub = isolate()->builtins()->JSConstructStubApi();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001155
kasper.lund212ac232008-07-16 07:07:30 +00001156 int internal_field_count = 0;
1157 if (!obj->instance_template()->IsUndefined()) {
1158 Handle<ObjectTemplateInfo> instance_template =
1159 Handle<ObjectTemplateInfo>(
1160 ObjectTemplateInfo::cast(obj->instance_template()));
1161 internal_field_count =
1162 Smi::cast(instance_template->internal_field_count())->value();
1163 }
1164
1165 int instance_size = kPointerSize * internal_field_count;
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001166 InstanceType type = INVALID_TYPE;
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001167 switch (instance_type) {
1168 case JavaScriptObject:
1169 type = JS_OBJECT_TYPE;
1170 instance_size += JSObject::kHeaderSize;
1171 break;
1172 case InnerGlobalObject:
1173 type = JS_GLOBAL_OBJECT_TYPE;
1174 instance_size += JSGlobalObject::kSize;
1175 break;
1176 case OuterGlobalObject:
1177 type = JS_GLOBAL_PROXY_TYPE;
1178 instance_size += JSGlobalProxy::kSize;
1179 break;
1180 default:
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001181 break;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001182 }
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001183 ASSERT(type != INVALID_TYPE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001184
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001185 Handle<JSFunction> result =
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001186 NewFunction(Factory::empty_symbol(),
1187 type,
1188 instance_size,
1189 code,
1190 true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001191 // Set class name.
1192 Handle<Object> class_name = Handle<Object>(obj->class_name());
1193 if (class_name->IsString()) {
1194 result->shared()->set_instance_class_name(*class_name);
1195 result->shared()->set_name(*class_name);
1196 }
1197
1198 Handle<Map> map = Handle<Map>(result->initial_map());
1199
1200 // Mark as undetectable if needed.
1201 if (obj->undetectable()) {
1202 map->set_is_undetectable();
1203 }
1204
1205 // Mark as hidden for the __proto__ accessor if needed.
1206 if (obj->hidden_prototype()) {
1207 map->set_is_hidden_prototype();
1208 }
1209
1210 // Mark as needs_access_check if needed.
1211 if (obj->needs_access_check()) {
ager@chromium.org870a0b62008-11-04 11:43:05 +00001212 map->set_is_access_check_needed(true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001213 }
1214
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001215 // Set interceptor information in the map.
1216 if (!obj->named_property_handler()->IsUndefined()) {
1217 map->set_has_named_interceptor();
1218 }
1219 if (!obj->indexed_property_handler()->IsUndefined()) {
1220 map->set_has_indexed_interceptor();
1221 }
1222
1223 // Set instance call-as-function information in the map.
1224 if (!obj->instance_call_handler()->IsUndefined()) {
1225 map->set_has_instance_call_handler();
1226 }
1227
1228 result->shared()->set_function_data(*obj);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001229 result->shared()->set_construct_stub(*construct_stub);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001230 result->shared()->DontAdaptArguments();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001231
1232 // Recursively copy parent templates' accessors, 'data' may be modified.
1233 Handle<DescriptorArray> array =
1234 Handle<DescriptorArray>(map->instance_descriptors());
1235 while (true) {
1236 Handle<Object> props = Handle<Object>(obj->property_accessors());
1237 if (!props->IsUndefined()) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001238 array = CopyAppendCallbackDescriptors(array, props);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001239 }
1240 Handle<Object> parent = Handle<Object>(obj->parent_template());
1241 if (parent->IsUndefined()) break;
1242 obj = Handle<FunctionTemplateInfo>::cast(parent);
1243 }
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001244 if (!array->IsEmpty()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001245 map->set_instance_descriptors(*array);
1246 }
1247
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001248 ASSERT(result->shared()->IsApiFunction());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001249 return result;
1250}
1251
1252
ager@chromium.org236ad962008-09-25 09:45:57 +00001253Handle<MapCache> Factory::NewMapCache(int at_least_space_for) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001254 CALL_HEAP_FUNCTION(isolate(),
1255 MapCache::Allocate(at_least_space_for), MapCache);
ager@chromium.org236ad962008-09-25 09:45:57 +00001256}
1257
1258
lrn@chromium.org303ada72010-10-27 09:33:13 +00001259MUST_USE_RESULT static MaybeObject* UpdateMapCacheWith(Context* context,
1260 FixedArray* keys,
1261 Map* map) {
1262 Object* result;
1263 { MaybeObject* maybe_result =
1264 MapCache::cast(context->map_cache())->Put(keys, map);
1265 if (!maybe_result->ToObject(&result)) return maybe_result;
1266 }
1267 context->set_map_cache(MapCache::cast(result));
ager@chromium.org236ad962008-09-25 09:45:57 +00001268 return result;
1269}
1270
1271
1272Handle<MapCache> Factory::AddToMapCache(Handle<Context> context,
1273 Handle<FixedArray> keys,
1274 Handle<Map> map) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001275 CALL_HEAP_FUNCTION(isolate(),
1276 UpdateMapCacheWith(*context, *keys, *map), MapCache);
ager@chromium.org236ad962008-09-25 09:45:57 +00001277}
1278
1279
1280Handle<Map> Factory::ObjectLiteralMapFromCache(Handle<Context> context,
1281 Handle<FixedArray> keys) {
1282 if (context->map_cache()->IsUndefined()) {
1283 // Allocate the new map cache for the global context.
1284 Handle<MapCache> new_cache = NewMapCache(24);
1285 context->set_map_cache(*new_cache);
1286 }
ager@chromium.org32912102009-01-16 10:38:43 +00001287 // Check to see whether there is a matching element in the cache.
ager@chromium.org236ad962008-09-25 09:45:57 +00001288 Handle<MapCache> cache =
1289 Handle<MapCache>(MapCache::cast(context->map_cache()));
1290 Handle<Object> result = Handle<Object>(cache->Lookup(*keys));
1291 if (result->IsMap()) return Handle<Map>::cast(result);
1292 // Create a new map and add it to the cache.
1293 Handle<Map> map =
ager@chromium.org32912102009-01-16 10:38:43 +00001294 CopyMap(Handle<Map>(context->object_function()->initial_map()),
1295 keys->length());
ager@chromium.org236ad962008-09-25 09:45:57 +00001296 AddToMapCache(context, keys, map);
1297 return Handle<Map>(map);
1298}
1299
1300
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001301void Factory::SetRegExpAtomData(Handle<JSRegExp> regexp,
1302 JSRegExp::Type type,
1303 Handle<String> source,
1304 JSRegExp::Flags flags,
1305 Handle<Object> data) {
1306 Handle<FixedArray> store = NewFixedArray(JSRegExp::kAtomDataSize);
1307
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001308 store->set(JSRegExp::kTagIndex, Smi::FromInt(type));
1309 store->set(JSRegExp::kSourceIndex, *source);
1310 store->set(JSRegExp::kFlagsIndex, Smi::FromInt(flags.value()));
1311 store->set(JSRegExp::kAtomPatternIndex, *data);
1312 regexp->set_data(*store);
1313}
1314
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001315void Factory::SetRegExpIrregexpData(Handle<JSRegExp> regexp,
1316 JSRegExp::Type type,
1317 Handle<String> source,
1318 JSRegExp::Flags flags,
1319 int capture_count) {
1320 Handle<FixedArray> store = NewFixedArray(JSRegExp::kIrregexpDataSize);
jkummerow@chromium.orgddda9e82011-07-06 11:27:02 +00001321 Smi* uninitialized = Smi::FromInt(JSRegExp::kUninitializedValue);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001322 store->set(JSRegExp::kTagIndex, Smi::FromInt(type));
1323 store->set(JSRegExp::kSourceIndex, *source);
1324 store->set(JSRegExp::kFlagsIndex, Smi::FromInt(flags.value()));
jkummerow@chromium.orgddda9e82011-07-06 11:27:02 +00001325 store->set(JSRegExp::kIrregexpASCIICodeIndex, uninitialized);
1326 store->set(JSRegExp::kIrregexpUC16CodeIndex, uninitialized);
1327 store->set(JSRegExp::kIrregexpASCIICodeSavedIndex, uninitialized);
1328 store->set(JSRegExp::kIrregexpUC16CodeSavedIndex, uninitialized);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001329 store->set(JSRegExp::kIrregexpMaxRegisterCountIndex, Smi::FromInt(0));
1330 store->set(JSRegExp::kIrregexpCaptureCountIndex,
1331 Smi::FromInt(capture_count));
1332 regexp->set_data(*store);
1333}
1334
1335
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001336
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001337void Factory::ConfigureInstance(Handle<FunctionTemplateInfo> desc,
1338 Handle<JSObject> instance,
1339 bool* pending_exception) {
1340 // Configure the instance by adding the properties specified by the
1341 // instance template.
1342 Handle<Object> instance_template = Handle<Object>(desc->instance_template());
1343 if (!instance_template->IsUndefined()) {
1344 Execution::ConfigureInstance(instance,
1345 instance_template,
1346 pending_exception);
1347 } else {
1348 *pending_exception = false;
1349 }
1350}
1351
1352
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001353Handle<Object> Factory::GlobalConstantFor(Handle<String> name) {
1354 Heap* h = isolate()->heap();
1355 if (name->Equals(h->undefined_symbol())) return undefined_value();
1356 if (name->Equals(h->nan_symbol())) return nan_value();
1357 if (name->Equals(h->infinity_symbol())) return infinity_value();
1358 return Handle<Object>::null();
1359}
1360
1361
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00001362Handle<Object> Factory::ToBoolean(bool value) {
1363 return Handle<Object>(value
1364 ? isolate()->heap()->true_value()
1365 : isolate()->heap()->false_value());
1366}
1367
1368
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001369} } // namespace v8::internal