blob: 05dd5c96619ea25d61feb38d8f54a9834b61f7e7 [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"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000037
kasperl@chromium.org71affb52009-05-26 05:44:31 +000038namespace v8 {
39namespace internal {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000040
41
42Handle<FixedArray> Factory::NewFixedArray(int size, PretenureFlag pretenure) {
43 ASSERT(0 <= size);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000044 CALL_HEAP_FUNCTION(
45 isolate(),
46 isolate()->heap()->AllocateFixedArray(size, pretenure),
47 FixedArray);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000048}
49
50
ricow@chromium.orgc9c80822010-04-21 08:22:37 +000051Handle<FixedArray> Factory::NewFixedArrayWithHoles(int size,
52 PretenureFlag pretenure) {
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +000053 ASSERT(0 <= size);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000054 CALL_HEAP_FUNCTION(
55 isolate(),
56 isolate()->heap()->AllocateFixedArrayWithHoles(size, pretenure),
57 FixedArray);
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +000058}
59
60
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +000061Handle<FixedArray> Factory::NewFixedDoubleArray(int size,
62 PretenureFlag pretenure) {
63 ASSERT(0 <= size);
64 CALL_HEAP_FUNCTION(
65 isolate(),
66 isolate()->heap()->AllocateUninitializedFixedDoubleArray(size, pretenure),
67 FixedArray);
68}
69
70
kasperl@chromium.org86f77b72009-07-06 08:21:57 +000071Handle<StringDictionary> Factory::NewStringDictionary(int at_least_space_for) {
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +000072 ASSERT(0 <= at_least_space_for);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000073 CALL_HEAP_FUNCTION(isolate(),
74 StringDictionary::Allocate(at_least_space_for),
kasperl@chromium.org86f77b72009-07-06 08:21:57 +000075 StringDictionary);
76}
77
78
79Handle<NumberDictionary> Factory::NewNumberDictionary(int at_least_space_for) {
80 ASSERT(0 <= at_least_space_for);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000081 CALL_HEAP_FUNCTION(isolate(),
82 NumberDictionary::Allocate(at_least_space_for),
kasperl@chromium.org86f77b72009-07-06 08:21:57 +000083 NumberDictionary);
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +000084}
85
86
vegorov@chromium.org7943d462011-08-01 11:41:52 +000087Handle<ObjectHashTable> Factory::NewObjectHashTable(int at_least_space_for) {
88 ASSERT(0 <= at_least_space_for);
89 CALL_HEAP_FUNCTION(isolate(),
90 ObjectHashTable::Allocate(at_least_space_for),
91 ObjectHashTable);
92}
93
94
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000095Handle<DescriptorArray> Factory::NewDescriptorArray(int number_of_descriptors) {
96 ASSERT(0 <= number_of_descriptors);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000097 CALL_HEAP_FUNCTION(isolate(),
98 DescriptorArray::Allocate(number_of_descriptors),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000099 DescriptorArray);
100}
101
102
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000103Handle<DeoptimizationInputData> Factory::NewDeoptimizationInputData(
104 int deopt_entry_count,
105 PretenureFlag pretenure) {
106 ASSERT(deopt_entry_count > 0);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000107 CALL_HEAP_FUNCTION(isolate(),
108 DeoptimizationInputData::Allocate(deopt_entry_count,
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000109 pretenure),
110 DeoptimizationInputData);
111}
112
113
114Handle<DeoptimizationOutputData> Factory::NewDeoptimizationOutputData(
115 int deopt_entry_count,
116 PretenureFlag pretenure) {
117 ASSERT(deopt_entry_count > 0);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000118 CALL_HEAP_FUNCTION(isolate(),
119 DeoptimizationOutputData::Allocate(deopt_entry_count,
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000120 pretenure),
121 DeoptimizationOutputData);
122}
123
124
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000125// Symbols are created in the old generation (data space).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000126Handle<String> Factory::LookupSymbol(Vector<const char> string) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000127 CALL_HEAP_FUNCTION(isolate(),
128 isolate()->heap()->LookupSymbol(string),
129 String);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000130}
131
danno@chromium.org40cb8782011-05-25 07:58:50 +0000132// Symbols are created in the old generation (data space).
133Handle<String> Factory::LookupSymbol(Handle<String> string) {
134 CALL_HEAP_FUNCTION(isolate(),
135 isolate()->heap()->LookupSymbol(*string),
136 String);
137}
138
fschneider@chromium.org9e3e0b62011-01-03 10:16:46 +0000139Handle<String> Factory::LookupAsciiSymbol(Vector<const char> string) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000140 CALL_HEAP_FUNCTION(isolate(),
141 isolate()->heap()->LookupAsciiSymbol(string),
142 String);
fschneider@chromium.org9e3e0b62011-01-03 10:16:46 +0000143}
144
danno@chromium.org40cb8782011-05-25 07:58:50 +0000145
146Handle<String> Factory::LookupAsciiSymbol(Handle<SeqAsciiString> string,
147 int from,
148 int length) {
149 CALL_HEAP_FUNCTION(isolate(),
150 isolate()->heap()->LookupAsciiSymbol(string,
151 from,
152 length),
153 String);
154}
155
156
fschneider@chromium.org9e3e0b62011-01-03 10:16:46 +0000157Handle<String> Factory::LookupTwoByteSymbol(Vector<const uc16> string) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000158 CALL_HEAP_FUNCTION(isolate(),
159 isolate()->heap()->LookupTwoByteSymbol(string),
160 String);
fschneider@chromium.org9e3e0b62011-01-03 10:16:46 +0000161}
162
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000163
164Handle<String> Factory::NewStringFromAscii(Vector<const char> string,
165 PretenureFlag pretenure) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000166 CALL_HEAP_FUNCTION(
167 isolate(),
168 isolate()->heap()->AllocateStringFromAscii(string, pretenure),
169 String);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000170}
171
172Handle<String> Factory::NewStringFromUtf8(Vector<const char> string,
173 PretenureFlag pretenure) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000174 CALL_HEAP_FUNCTION(
175 isolate(),
176 isolate()->heap()->AllocateStringFromUtf8(string, pretenure),
177 String);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000178}
179
180
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000181Handle<String> Factory::NewStringFromTwoByte(Vector<const uc16> string,
182 PretenureFlag pretenure) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000183 CALL_HEAP_FUNCTION(
184 isolate(),
185 isolate()->heap()->AllocateStringFromTwoByte(string, pretenure),
186 String);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000187}
188
189
ager@chromium.org04921a82011-06-27 13:21:41 +0000190Handle<SeqAsciiString> Factory::NewRawAsciiString(int length,
191 PretenureFlag pretenure) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000192 CALL_HEAP_FUNCTION(
193 isolate(),
194 isolate()->heap()->AllocateRawAsciiString(length, pretenure),
ager@chromium.org04921a82011-06-27 13:21:41 +0000195 SeqAsciiString);
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +0000196}
197
198
ager@chromium.org04921a82011-06-27 13:21:41 +0000199Handle<SeqTwoByteString> Factory::NewRawTwoByteString(int length,
200 PretenureFlag pretenure) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000201 CALL_HEAP_FUNCTION(
202 isolate(),
203 isolate()->heap()->AllocateRawTwoByteString(length, pretenure),
ager@chromium.org04921a82011-06-27 13:21:41 +0000204 SeqTwoByteString);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000205}
206
207
208Handle<String> Factory::NewConsString(Handle<String> first,
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000209 Handle<String> second) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000210 CALL_HEAP_FUNCTION(isolate(),
211 isolate()->heap()->AllocateConsString(*first, *second),
212 String);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000213}
214
215
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000216Handle<String> Factory::NewSubString(Handle<String> str,
217 int begin,
218 int end) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000219 CALL_HEAP_FUNCTION(isolate(),
220 str->SubString(begin, end),
221 String);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000222}
223
224
ager@chromium.org04921a82011-06-27 13:21:41 +0000225Handle<String> Factory::NewProperSubString(Handle<String> str,
226 int begin,
227 int end) {
228 ASSERT(begin > 0 || end < str->length());
229 CALL_HEAP_FUNCTION(isolate(),
230 isolate()->heap()->AllocateSubString(*str, begin, end),
231 String);
232}
233
234
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000235Handle<String> Factory::NewExternalStringFromAscii(
236 ExternalAsciiString::Resource* resource) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000237 CALL_HEAP_FUNCTION(
238 isolate(),
239 isolate()->heap()->AllocateExternalStringFromAscii(resource),
240 String);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000241}
242
243
244Handle<String> Factory::NewExternalStringFromTwoByte(
245 ExternalTwoByteString::Resource* resource) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000246 CALL_HEAP_FUNCTION(
247 isolate(),
248 isolate()->heap()->AllocateExternalStringFromTwoByte(resource),
249 String);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000250}
251
252
253Handle<Context> Factory::NewGlobalContext() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000254 CALL_HEAP_FUNCTION(
255 isolate(),
256 isolate()->heap()->AllocateGlobalContext(),
257 Context);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000258}
259
260
261Handle<Context> Factory::NewFunctionContext(int length,
vegorov@chromium.org3cf47312011-06-29 13:20:01 +0000262 Handle<JSFunction> function) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000263 CALL_HEAP_FUNCTION(
264 isolate(),
vegorov@chromium.org3cf47312011-06-29 13:20:01 +0000265 isolate()->heap()->AllocateFunctionContext(length, *function),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000266 Context);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000267}
268
269
vegorov@chromium.org3cf47312011-06-29 13:20:01 +0000270Handle<Context> Factory::NewCatchContext(Handle<JSFunction> function,
271 Handle<Context> previous,
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000272 Handle<String> name,
273 Handle<Object> thrown_object) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000274 CALL_HEAP_FUNCTION(
275 isolate(),
vegorov@chromium.org3cf47312011-06-29 13:20:01 +0000276 isolate()->heap()->AllocateCatchContext(*function,
277 *previous,
278 *name,
279 *thrown_object),
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000280 Context);
281}
282
283
vegorov@chromium.org3cf47312011-06-29 13:20:01 +0000284Handle<Context> Factory::NewWithContext(Handle<JSFunction> function,
285 Handle<Context> previous,
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000286 Handle<JSObject> extension) {
287 CALL_HEAP_FUNCTION(
288 isolate(),
vegorov@chromium.org3cf47312011-06-29 13:20:01 +0000289 isolate()->heap()->AllocateWithContext(*function, *previous, *extension),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000290 Context);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000291}
292
293
294Handle<Struct> Factory::NewStruct(InstanceType type) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000295 CALL_HEAP_FUNCTION(
296 isolate(),
297 isolate()->heap()->AllocateStruct(type),
298 Struct);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000299}
300
301
302Handle<AccessorInfo> Factory::NewAccessorInfo() {
303 Handle<AccessorInfo> info =
304 Handle<AccessorInfo>::cast(NewStruct(ACCESSOR_INFO_TYPE));
305 info->set_flag(0); // Must clear the flag, it was initialized as undefined.
306 return info;
307}
308
309
310Handle<Script> Factory::NewScript(Handle<String> source) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000311 // Generate id for this script.
312 int id;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000313 Heap* heap = isolate()->heap();
314 if (heap->last_script_id()->IsUndefined()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000315 // Script ids start from one.
316 id = 1;
317 } else {
318 // Increment id, wrap when positive smi is exhausted.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000319 id = Smi::cast(heap->last_script_id())->value();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000320 id++;
321 if (!Smi::IsValid(id)) {
322 id = 0;
323 }
324 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000325 heap->SetLastScriptId(Smi::FromInt(id));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000326
327 // Create and initialize script object.
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000328 Handle<Foreign> wrapper = NewForeign(0, TENURED);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000329 Handle<Script> script = Handle<Script>::cast(NewStruct(SCRIPT_TYPE));
330 script->set_source(*source);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000331 script->set_name(heap->undefined_value());
332 script->set_id(heap->last_script_id());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000333 script->set_line_offset(Smi::FromInt(0));
334 script->set_column_offset(Smi::FromInt(0));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000335 script->set_data(heap->undefined_value());
336 script->set_context_data(heap->undefined_value());
ager@chromium.orge2902be2009-06-08 12:21:35 +0000337 script->set_type(Smi::FromInt(Script::TYPE_NORMAL));
338 script->set_compilation_type(Smi::FromInt(Script::COMPILATION_TYPE_HOST));
ager@chromium.org9085a012009-05-11 19:22:57 +0000339 script->set_wrapper(*wrapper);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000340 script->set_line_ends(heap->undefined_value());
341 script->set_eval_from_shared(heap->undefined_value());
ager@chromium.orge2902be2009-06-08 12:21:35 +0000342 script->set_eval_from_instructions_offset(Smi::FromInt(0));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000343
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000344 return script;
345}
346
347
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000348Handle<Foreign> Factory::NewForeign(Address addr, PretenureFlag pretenure) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000349 CALL_HEAP_FUNCTION(isolate(),
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000350 isolate()->heap()->AllocateForeign(addr, pretenure),
351 Foreign);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000352}
353
354
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000355Handle<Foreign> Factory::NewForeign(const AccessorDescriptor* desc) {
356 return NewForeign((Address) desc, TENURED);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000357}
358
359
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000360Handle<ByteArray> Factory::NewByteArray(int length, PretenureFlag pretenure) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000361 ASSERT(0 <= length);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000362 CALL_HEAP_FUNCTION(
363 isolate(),
364 isolate()->heap()->AllocateByteArray(length, pretenure),
365 ByteArray);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000366}
367
368
ager@chromium.org3811b432009-10-28 14:53:37 +0000369Handle<ExternalArray> Factory::NewExternalArray(int length,
370 ExternalArrayType array_type,
371 void* external_pointer,
372 PretenureFlag pretenure) {
373 ASSERT(0 <= length);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000374 CALL_HEAP_FUNCTION(
375 isolate(),
376 isolate()->heap()->AllocateExternalArray(length,
377 array_type,
378 external_pointer,
379 pretenure),
380 ExternalArray);
ager@chromium.org3811b432009-10-28 14:53:37 +0000381}
382
383
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000384Handle<JSGlobalPropertyCell> Factory::NewJSGlobalPropertyCell(
385 Handle<Object> value) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000386 CALL_HEAP_FUNCTION(
387 isolate(),
388 isolate()->heap()->AllocateJSGlobalPropertyCell(*value),
389 JSGlobalPropertyCell);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000390}
391
392
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000393Handle<Map> Factory::NewMap(InstanceType type, int instance_size) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000394 CALL_HEAP_FUNCTION(
395 isolate(),
396 isolate()->heap()->AllocateMap(type, instance_size),
397 Map);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000398}
399
400
401Handle<JSObject> Factory::NewFunctionPrototype(Handle<JSFunction> function) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000402 CALL_HEAP_FUNCTION(
403 isolate(),
404 isolate()->heap()->AllocateFunctionPrototype(*function),
405 JSObject);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000406}
407
408
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000409Handle<Map> Factory::CopyMapDropDescriptors(Handle<Map> src) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000410 CALL_HEAP_FUNCTION(isolate(), src->CopyDropDescriptors(), Map);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000411}
412
413
ager@chromium.org32912102009-01-16 10:38:43 +0000414Handle<Map> Factory::CopyMap(Handle<Map> src,
415 int extra_inobject_properties) {
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000416 Handle<Map> copy = CopyMapDropDescriptors(src);
ager@chromium.org32912102009-01-16 10:38:43 +0000417 // Check that we do not overflow the instance size when adding the
418 // extra inobject properties.
419 int instance_size_delta = extra_inobject_properties * kPointerSize;
420 int max_instance_size_delta =
421 JSObject::kMaxInstanceSize - copy->instance_size();
422 if (instance_size_delta > max_instance_size_delta) {
423 // If the instance size overflows, we allocate as many properties
424 // as we can as inobject properties.
425 instance_size_delta = max_instance_size_delta;
426 extra_inobject_properties = max_instance_size_delta >> kPointerSizeLog2;
427 }
428 // Adjust the map with the extra inobject properties.
429 int inobject_properties =
430 copy->inobject_properties() + extra_inobject_properties;
431 copy->set_inobject_properties(inobject_properties);
432 copy->set_unused_property_fields(inobject_properties);
433 copy->set_instance_size(copy->instance_size() + instance_size_delta);
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000434 copy->set_visitor_id(StaticVisitorBase::GetVisitorId(*copy));
ager@chromium.org32912102009-01-16 10:38:43 +0000435 return copy;
436}
437
fschneider@chromium.org40b9da32010-06-28 11:29:21 +0000438
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000439Handle<Map> Factory::CopyMapDropTransitions(Handle<Map> src) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000440 CALL_HEAP_FUNCTION(isolate(), src->CopyDropTransitions(), Map);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000441}
442
443
fschneider@chromium.org40b9da32010-06-28 11:29:21 +0000444Handle<Map> Factory::GetFastElementsMap(Handle<Map> src) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000445 CALL_HEAP_FUNCTION(isolate(), src->GetFastElementsMap(), Map);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +0000446}
447
448
449Handle<Map> Factory::GetSlowElementsMap(Handle<Map> src) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000450 CALL_HEAP_FUNCTION(isolate(), src->GetSlowElementsMap(), Map);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +0000451}
452
453
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000454Handle<Map> Factory::GetExternalArrayElementsMap(
455 Handle<Map> src,
456 ExternalArrayType array_type,
457 bool safe_to_add_transition) {
458 CALL_HEAP_FUNCTION(isolate(),
459 src->GetExternalArrayElementsMap(array_type,
460 safe_to_add_transition),
461 Map);
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +0000462}
463
464
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000465Handle<FixedArray> Factory::CopyFixedArray(Handle<FixedArray> array) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000466 CALL_HEAP_FUNCTION(isolate(), array->Copy(), FixedArray);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000467}
468
469
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000470Handle<JSFunction> Factory::BaseNewFunctionFromSharedFunctionInfo(
471 Handle<SharedFunctionInfo> function_info,
sgjesse@chromium.org846fb742009-12-18 08:56:33 +0000472 Handle<Map> function_map,
473 PretenureFlag pretenure) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000474 CALL_HEAP_FUNCTION(
475 isolate(),
476 isolate()->heap()->AllocateFunction(*function_map,
477 *function_info,
478 isolate()->heap()->the_hole_value(),
479 pretenure),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000480 JSFunction);
481}
482
483
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000484Handle<JSFunction> Factory::NewFunctionFromSharedFunctionInfo(
485 Handle<SharedFunctionInfo> function_info,
sgjesse@chromium.org846fb742009-12-18 08:56:33 +0000486 Handle<Context> context,
487 PretenureFlag pretenure) {
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000488 Handle<JSFunction> result = BaseNewFunctionFromSharedFunctionInfo(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000489 function_info,
490 function_info->strict_mode()
491 ? isolate()->strict_mode_function_map()
492 : isolate()->function_map(),
493 pretenure);
494
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000495 result->set_context(*context);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000496 int number_of_literals = function_info->num_literals();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000497 Handle<FixedArray> literals = NewFixedArray(number_of_literals, pretenure);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000498 if (number_of_literals > 0) {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000499 // Store the object, regexp and array functions in the literals
500 // array prefix. These functions will be used when creating
501 // object, regexp and array literals in this function.
ager@chromium.org236ad962008-09-25 09:45:57 +0000502 literals->set(JSFunction::kLiteralGlobalContextIndex,
503 context->global_context());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000504 }
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000505 result->set_literals(*literals);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000506 result->set_next_function_link(isolate()->heap()->undefined_value());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000507
508 if (V8::UseCrankshaft() &&
509 FLAG_always_opt &&
510 result->is_compiled() &&
511 !function_info->is_toplevel() &&
512 function_info->allows_lazy_compilation()) {
513 result->MarkForLazyRecompilation();
514 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000515 return result;
516}
517
518
519Handle<Object> Factory::NewNumber(double value,
520 PretenureFlag pretenure) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000521 CALL_HEAP_FUNCTION(
522 isolate(),
523 isolate()->heap()->NumberFromDouble(value, pretenure), Object);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000524}
525
526
527Handle<Object> Factory::NewNumberFromInt(int value) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000528 CALL_HEAP_FUNCTION(
529 isolate(),
530 isolate()->heap()->NumberFromInt32(value), Object);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000531}
532
533
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000534Handle<Object> Factory::NewNumberFromUint(uint32_t value) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000535 CALL_HEAP_FUNCTION(
536 isolate(),
537 isolate()->heap()->NumberFromUint32(value), Object);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000538}
539
540
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000541Handle<JSObject> Factory::NewNeanderObject() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000542 CALL_HEAP_FUNCTION(
543 isolate(),
544 isolate()->heap()->AllocateJSObjectFromMap(
545 isolate()->heap()->neander_map()),
546 JSObject);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000547}
548
549
550Handle<Object> Factory::NewTypeError(const char* type,
551 Vector< Handle<Object> > args) {
552 return NewError("MakeTypeError", type, args);
553}
554
555
556Handle<Object> Factory::NewTypeError(Handle<String> message) {
557 return NewError("$TypeError", message);
558}
559
560
561Handle<Object> Factory::NewRangeError(const char* type,
562 Vector< Handle<Object> > args) {
563 return NewError("MakeRangeError", type, args);
564}
565
566
567Handle<Object> Factory::NewRangeError(Handle<String> message) {
568 return NewError("$RangeError", message);
569}
570
571
572Handle<Object> Factory::NewSyntaxError(const char* type, Handle<JSArray> args) {
573 return NewError("MakeSyntaxError", type, args);
574}
575
576
577Handle<Object> Factory::NewSyntaxError(Handle<String> message) {
578 return NewError("$SyntaxError", message);
579}
580
581
582Handle<Object> Factory::NewReferenceError(const char* type,
583 Vector< Handle<Object> > args) {
584 return NewError("MakeReferenceError", type, args);
585}
586
587
588Handle<Object> Factory::NewReferenceError(Handle<String> message) {
589 return NewError("$ReferenceError", message);
590}
591
592
593Handle<Object> Factory::NewError(const char* maker, const char* type,
594 Vector< Handle<Object> > args) {
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000595 v8::HandleScope scope; // Instantiate a closeable HandleScope for EscapeFrom.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000596 Handle<FixedArray> array = NewFixedArray(args.length());
ager@chromium.org7c537e22008-10-16 08:43:32 +0000597 for (int i = 0; i < args.length(); i++) {
598 array->set(i, *args[i]);
599 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000600 Handle<JSArray> object = NewJSArrayWithElements(array);
ager@chromium.org7c537e22008-10-16 08:43:32 +0000601 Handle<Object> result = NewError(maker, type, object);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000602 return result.EscapeFrom(&scope);
603}
604
605
606Handle<Object> Factory::NewEvalError(const char* type,
607 Vector< Handle<Object> > args) {
608 return NewError("MakeEvalError", type, args);
609}
610
611
612Handle<Object> Factory::NewError(const char* type,
613 Vector< Handle<Object> > args) {
614 return NewError("MakeError", type, args);
615}
616
617
618Handle<Object> Factory::NewError(const char* maker,
619 const char* type,
620 Handle<JSArray> args) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000621 Handle<String> make_str = LookupAsciiSymbol(maker);
622 Handle<Object> fun_obj(
623 isolate()->js_builtins_object()->GetPropertyNoExceptionThrown(*make_str));
ager@chromium.org4af710e2009-09-15 12:20:11 +0000624 // If the builtins haven't been properly configured yet this error
625 // constructor may not have been defined. Bail out.
626 if (!fun_obj->IsJSFunction())
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000627 return undefined_value();
ager@chromium.org4af710e2009-09-15 12:20:11 +0000628 Handle<JSFunction> fun = Handle<JSFunction>::cast(fun_obj);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000629 Handle<Object> type_obj = LookupAsciiSymbol(type);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000630 Object** argv[2] = { type_obj.location(),
631 Handle<Object>::cast(args).location() };
632
633 // Invoke the JavaScript factory method. If an exception is thrown while
634 // running the factory method, use the exception as the result.
635 bool caught_exception;
636 Handle<Object> result = Execution::TryCall(fun,
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000637 isolate()->js_builtins_object(), 2, argv, &caught_exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000638 return result;
639}
640
641
642Handle<Object> Factory::NewError(Handle<String> message) {
643 return NewError("$Error", message);
644}
645
646
647Handle<Object> Factory::NewError(const char* constructor,
648 Handle<String> message) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000649 Handle<String> constr = LookupAsciiSymbol(constructor);
650 Handle<JSFunction> fun = Handle<JSFunction>(
651 JSFunction::cast(isolate()->js_builtins_object()->
652 GetPropertyNoExceptionThrown(*constr)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000653 Object** argv[1] = { Handle<Object>::cast(message).location() };
654
655 // Invoke the JavaScript factory method. If an exception is thrown while
656 // running the factory method, use the exception as the result.
657 bool caught_exception;
658 Handle<Object> result = Execution::TryCall(fun,
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000659 isolate()->js_builtins_object(), 1, argv, &caught_exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000660 return result;
661}
662
663
664Handle<JSFunction> Factory::NewFunction(Handle<String> name,
665 InstanceType type,
666 int instance_size,
667 Handle<Code> code,
668 bool force_initial_map) {
669 // Allocate the function
670 Handle<JSFunction> function = NewFunction(name, the_hole_value());
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000671
672 // Setup the code pointer in both the shared function info and in
673 // the function itself.
674 function->shared()->set_code(*code);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000675 function->set_code(*code);
676
677 if (force_initial_map ||
678 type != JS_OBJECT_TYPE ||
679 instance_size != JSObject::kHeaderSize) {
680 Handle<Map> initial_map = NewMap(type, instance_size);
681 Handle<JSObject> prototype = NewFunctionPrototype(function);
682 initial_map->set_prototype(*prototype);
683 function->set_initial_map(*initial_map);
684 initial_map->set_constructor(*function);
685 } else {
686 ASSERT(!function->has_initial_map());
687 ASSERT(!function->has_prototype());
688 }
689
690 return function;
691}
692
693
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000694Handle<JSFunction> Factory::NewFunctionWithPrototype(Handle<String> name,
695 InstanceType type,
696 int instance_size,
697 Handle<JSObject> prototype,
698 Handle<Code> code,
699 bool force_initial_map) {
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000700 // Allocate the function.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000701 Handle<JSFunction> function = NewFunction(name, prototype);
702
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000703 // Setup the code pointer in both the shared function info and in
704 // the function itself.
705 function->shared()->set_code(*code);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000706 function->set_code(*code);
707
708 if (force_initial_map ||
709 type != JS_OBJECT_TYPE ||
710 instance_size != JSObject::kHeaderSize) {
711 Handle<Map> initial_map = NewMap(type, instance_size);
712 function->set_initial_map(*initial_map);
713 initial_map->set_constructor(*function);
714 }
715
716 // Set function.prototype and give the prototype a constructor
717 // property that refers to the function.
718 SetPrototypeProperty(function, prototype);
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +0000719 // Currently safe because it is only invoked from Genesis.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000720 SetLocalPropertyNoThrow(prototype, constructor_symbol(), function, DONT_ENUM);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000721 return function;
722}
723
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000724
kmillikin@chromium.org4111b802010-05-03 10:34:42 +0000725Handle<JSFunction> Factory::NewFunctionWithoutPrototype(Handle<String> name,
726 Handle<Code> code) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000727 Handle<JSFunction> function = NewFunctionWithoutPrototype(name,
728 kNonStrictMode);
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000729 function->shared()->set_code(*code);
kmillikin@chromium.org4111b802010-05-03 10:34:42 +0000730 function->set_code(*code);
731 ASSERT(!function->has_initial_map());
732 ASSERT(!function->has_prototype());
733 return function;
734}
735
736
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000737Handle<Code> Factory::NewCode(const CodeDesc& desc,
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000738 Code::Flags flags,
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +0000739 Handle<Object> self_ref,
740 bool immovable) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000741 CALL_HEAP_FUNCTION(isolate(),
742 isolate()->heap()->CreateCode(
743 desc, flags, self_ref, immovable),
744 Code);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000745}
746
747
748Handle<Code> Factory::CopyCode(Handle<Code> code) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000749 CALL_HEAP_FUNCTION(isolate(),
750 isolate()->heap()->CopyCode(*code),
751 Code);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000752}
753
754
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000755Handle<Code> Factory::CopyCode(Handle<Code> code, Vector<byte> reloc_info) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000756 CALL_HEAP_FUNCTION(isolate(),
757 isolate()->heap()->CopyCode(*code, reloc_info),
758 Code);
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000759}
760
761
lrn@chromium.org303ada72010-10-27 09:33:13 +0000762MUST_USE_RESULT static inline MaybeObject* DoCopyInsert(
763 DescriptorArray* array,
764 String* key,
765 Object* value,
766 PropertyAttributes attributes) {
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000767 CallbacksDescriptor desc(key, value, attributes);
lrn@chromium.org303ada72010-10-27 09:33:13 +0000768 MaybeObject* obj = array->CopyInsert(&desc, REMOVE_TRANSITIONS);
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000769 return obj;
770}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000771
772
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000773// Allocate the new array.
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000774Handle<DescriptorArray> Factory::CopyAppendForeignDescriptor(
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000775 Handle<DescriptorArray> array,
776 Handle<String> key,
777 Handle<Object> value,
778 PropertyAttributes attributes) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000779 CALL_HEAP_FUNCTION(isolate(),
780 DoCopyInsert(*array, *key, *value, attributes),
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000781 DescriptorArray);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000782}
783
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000784
785Handle<String> Factory::SymbolFromString(Handle<String> value) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000786 CALL_HEAP_FUNCTION(isolate(),
787 isolate()->heap()->LookupSymbol(*value), String);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000788}
789
790
791Handle<DescriptorArray> Factory::CopyAppendCallbackDescriptors(
792 Handle<DescriptorArray> array,
793 Handle<Object> descriptors) {
794 v8::NeanderArray callbacks(descriptors);
795 int nof_callbacks = callbacks.length();
796 Handle<DescriptorArray> result =
797 NewDescriptorArray(array->number_of_descriptors() + nof_callbacks);
798
799 // Number of descriptors added to the result so far.
800 int descriptor_count = 0;
801
802 // Copy the descriptors from the array.
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000803 for (int i = 0; i < array->number_of_descriptors(); i++) {
804 if (array->GetType(i) != NULL_DESCRIPTOR) {
805 result->CopyFrom(descriptor_count++, *array, i);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000806 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000807 }
808
809 // Number of duplicates detected.
810 int duplicates = 0;
811
812 // Fill in new callback descriptors. Process the callbacks from
813 // back to front so that the last callback with a given name takes
814 // precedence over previously added callbacks with that name.
815 for (int i = nof_callbacks - 1; i >= 0; i--) {
816 Handle<AccessorInfo> entry =
817 Handle<AccessorInfo>(AccessorInfo::cast(callbacks.get(i)));
818 // Ensure the key is a symbol before writing into the instance descriptor.
819 Handle<String> key =
820 SymbolFromString(Handle<String>(String::cast(entry->name())));
821 // Check if a descriptor with this name already exists before writing.
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000822 if (result->LinearSearch(*key, descriptor_count) ==
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000823 DescriptorArray::kNotFound) {
824 CallbacksDescriptor desc(*key, *entry, entry->property_attributes());
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000825 result->Set(descriptor_count, &desc);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000826 descriptor_count++;
827 } else {
828 duplicates++;
829 }
830 }
831
832 // If duplicates were detected, allocate a result of the right size
833 // and transfer the elements.
834 if (duplicates > 0) {
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000835 int number_of_descriptors = result->number_of_descriptors() - duplicates;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000836 Handle<DescriptorArray> new_result =
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000837 NewDescriptorArray(number_of_descriptors);
838 for (int i = 0; i < number_of_descriptors; i++) {
839 new_result->CopyFrom(i, *result, i);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000840 }
841 result = new_result;
842 }
843
844 // Sort the result before returning.
845 result->Sort();
846 return result;
847}
848
849
850Handle<JSObject> Factory::NewJSObject(Handle<JSFunction> constructor,
851 PretenureFlag pretenure) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000852 CALL_HEAP_FUNCTION(
853 isolate(),
854 isolate()->heap()->AllocateJSObject(*constructor, pretenure), JSObject);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000855}
856
857
kasperl@chromium.org2abc4502009-07-02 07:00:29 +0000858Handle<GlobalObject> Factory::NewGlobalObject(
859 Handle<JSFunction> constructor) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000860 CALL_HEAP_FUNCTION(isolate(),
861 isolate()->heap()->AllocateGlobalObject(*constructor),
kasperl@chromium.org2abc4502009-07-02 07:00:29 +0000862 GlobalObject);
863}
864
865
866
ager@chromium.org236ad962008-09-25 09:45:57 +0000867Handle<JSObject> Factory::NewJSObjectFromMap(Handle<Map> map) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000868 CALL_HEAP_FUNCTION(
869 isolate(),
870 isolate()->heap()->AllocateJSObjectFromMap(*map, NOT_TENURED),
871 JSObject);
ager@chromium.org236ad962008-09-25 09:45:57 +0000872}
873
874
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +0000875Handle<JSArray> Factory::NewJSArray(int capacity,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000876 PretenureFlag pretenure) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000877 Handle<JSObject> obj = NewJSObject(isolate()->array_function(), pretenure);
878 CALL_HEAP_FUNCTION(isolate(),
879 Handle<JSArray>::cast(obj)->Initialize(capacity),
880 JSArray);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000881}
882
883
884Handle<JSArray> Factory::NewJSArrayWithElements(Handle<FixedArray> elements,
885 PretenureFlag pretenure) {
886 Handle<JSArray> result =
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000887 Handle<JSArray>::cast(NewJSObject(isolate()->array_function(),
888 pretenure));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000889 result->SetContent(*elements);
890 return result;
891}
892
893
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000894Handle<JSProxy> Factory::NewJSProxy(Handle<Object> handler,
895 Handle<Object> prototype) {
896 CALL_HEAP_FUNCTION(
897 isolate(),
898 isolate()->heap()->AllocateJSProxy(*handler, *prototype),
899 JSProxy);
900}
901
902
rossberg@chromium.org717967f2011-07-20 13:44:42 +0000903void Factory::BecomeJSObject(Handle<JSProxy> object) {
904 CALL_HEAP_FUNCTION_VOID(
905 isolate(),
906 isolate()->heap()->ReinitializeJSProxyAsJSObject(*object));
907}
908
909
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000910Handle<SharedFunctionInfo> Factory::NewSharedFunctionInfo(
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +0000911 Handle<String> name,
912 int number_of_literals,
913 Handle<Code> code,
ager@chromium.orgb5737492010-07-15 09:29:43 +0000914 Handle<SerializedScopeInfo> scope_info) {
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000915 Handle<SharedFunctionInfo> shared = NewSharedFunctionInfo(name);
916 shared->set_code(*code);
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +0000917 shared->set_scope_info(*scope_info);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000918 int literals_array_size = number_of_literals;
919 // If the function contains object, regexp or array literals,
920 // allocate extra space for a literals array prefix containing the
921 // context.
922 if (number_of_literals > 0) {
923 literals_array_size += JSFunction::kLiteralsPrefixSize;
924 }
925 shared->set_num_literals(literals_array_size);
926 return shared;
927}
928
929
kmillikin@chromium.org31b12772011-02-02 16:08:26 +0000930Handle<JSMessageObject> Factory::NewJSMessageObject(
931 Handle<String> type,
932 Handle<JSArray> arguments,
933 int start_position,
934 int end_position,
935 Handle<Object> script,
936 Handle<Object> stack_trace,
937 Handle<Object> stack_frames) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000938 CALL_HEAP_FUNCTION(isolate(),
939 isolate()->heap()->AllocateJSMessageObject(*type,
940 *arguments,
941 start_position,
942 end_position,
943 *script,
944 *stack_trace,
945 *stack_frames),
kmillikin@chromium.org31b12772011-02-02 16:08:26 +0000946 JSMessageObject);
947}
948
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000949Handle<SharedFunctionInfo> Factory::NewSharedFunctionInfo(Handle<String> name) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000950 CALL_HEAP_FUNCTION(isolate(),
951 isolate()->heap()->AllocateSharedFunctionInfo(*name),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000952 SharedFunctionInfo);
953}
954
955
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000956Handle<String> Factory::NumberToString(Handle<Object> number) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000957 CALL_HEAP_FUNCTION(isolate(),
958 isolate()->heap()->NumberToString(*number), String);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000959}
960
961
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000962Handle<NumberDictionary> Factory::DictionaryAtNumberPut(
963 Handle<NumberDictionary> dictionary,
964 uint32_t key,
965 Handle<Object> value) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000966 CALL_HEAP_FUNCTION(isolate(),
967 dictionary->AtNumberPut(key, *value),
968 NumberDictionary);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000969}
970
971
972Handle<JSFunction> Factory::NewFunctionHelper(Handle<String> name,
973 Handle<Object> prototype) {
974 Handle<SharedFunctionInfo> function_share = NewSharedFunctionInfo(name);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000975 CALL_HEAP_FUNCTION(
976 isolate(),
977 isolate()->heap()->AllocateFunction(*isolate()->function_map(),
978 *function_share,
979 *prototype),
980 JSFunction);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000981}
982
983
984Handle<JSFunction> Factory::NewFunction(Handle<String> name,
985 Handle<Object> prototype) {
986 Handle<JSFunction> fun = NewFunctionHelper(name, prototype);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000987 fun->set_context(isolate()->context()->global_context());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000988 return fun;
989}
990
991
kmillikin@chromium.org4111b802010-05-03 10:34:42 +0000992Handle<JSFunction> Factory::NewFunctionWithoutPrototypeHelper(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000993 Handle<String> name,
994 StrictModeFlag strict_mode) {
kmillikin@chromium.org4111b802010-05-03 10:34:42 +0000995 Handle<SharedFunctionInfo> function_share = NewSharedFunctionInfo(name);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000996 Handle<Map> map = strict_mode == kStrictMode
997 ? isolate()->strict_mode_function_without_prototype_map()
998 : isolate()->function_without_prototype_map();
999 CALL_HEAP_FUNCTION(isolate(),
1000 isolate()->heap()->AllocateFunction(
1001 *map,
kmillikin@chromium.org4111b802010-05-03 10:34:42 +00001002 *function_share,
1003 *the_hole_value()),
1004 JSFunction);
1005}
1006
1007
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001008Handle<JSFunction> Factory::NewFunctionWithoutPrototype(
1009 Handle<String> name,
1010 StrictModeFlag strict_mode) {
1011 Handle<JSFunction> fun = NewFunctionWithoutPrototypeHelper(name, strict_mode);
1012 fun->set_context(isolate()->context()->global_context());
kmillikin@chromium.org4111b802010-05-03 10:34:42 +00001013 return fun;
1014}
1015
1016
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001017Handle<Object> Factory::ToObject(Handle<Object> object) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001018 CALL_HEAP_FUNCTION(isolate(), object->ToObject(), Object);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001019}
1020
1021
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001022Handle<Object> Factory::ToObject(Handle<Object> object,
1023 Handle<Context> global_context) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001024 CALL_HEAP_FUNCTION(isolate(), object->ToObject(*global_context), Object);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001025}
1026
1027
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001028#ifdef ENABLE_DEBUGGER_SUPPORT
v8.team.kasperl727e9952008-09-02 14:56:44 +00001029Handle<DebugInfo> Factory::NewDebugInfo(Handle<SharedFunctionInfo> shared) {
1030 // Get the original code of the function.
1031 Handle<Code> code(shared->code());
1032
1033 // Create a copy of the code before allocating the debug info object to avoid
1034 // allocation while setting up the debug info object.
1035 Handle<Code> original_code(*Factory::CopyCode(code));
1036
1037 // Allocate initial fixed array for active break points before allocating the
1038 // debug info object to avoid allocation while setting up the debug info
1039 // object.
1040 Handle<FixedArray> break_points(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001041 NewFixedArray(Debug::kEstimatedNofBreakPointsInFunction));
v8.team.kasperl727e9952008-09-02 14:56:44 +00001042
1043 // Create and set up the debug info object. Debug info contains function, a
1044 // copy of the original code, the executing code and initial fixed array for
1045 // active break points.
1046 Handle<DebugInfo> debug_info =
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001047 Handle<DebugInfo>::cast(NewStruct(DEBUG_INFO_TYPE));
v8.team.kasperl727e9952008-09-02 14:56:44 +00001048 debug_info->set_shared(*shared);
1049 debug_info->set_original_code(*original_code);
1050 debug_info->set_code(*code);
1051 debug_info->set_break_points(*break_points);
1052
1053 // Link debug info to function.
1054 shared->set_debug_info(*debug_info);
1055
1056 return debug_info;
1057}
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001058#endif
v8.team.kasperl727e9952008-09-02 14:56:44 +00001059
1060
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001061Handle<JSObject> Factory::NewArgumentsObject(Handle<Object> callee,
1062 int length) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001063 CALL_HEAP_FUNCTION(
1064 isolate(),
1065 isolate()->heap()->AllocateArgumentsObject(*callee, length), JSObject);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001066}
1067
1068
1069Handle<JSFunction> Factory::CreateApiFunction(
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001070 Handle<FunctionTemplateInfo> obj, ApiInstanceType instance_type) {
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00001071 Handle<Code> code = isolate()->builtins()->HandleApiCall();
1072 Handle<Code> construct_stub = isolate()->builtins()->JSConstructStubApi();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001073
kasper.lund212ac232008-07-16 07:07:30 +00001074 int internal_field_count = 0;
1075 if (!obj->instance_template()->IsUndefined()) {
1076 Handle<ObjectTemplateInfo> instance_template =
1077 Handle<ObjectTemplateInfo>(
1078 ObjectTemplateInfo::cast(obj->instance_template()));
1079 internal_field_count =
1080 Smi::cast(instance_template->internal_field_count())->value();
1081 }
1082
1083 int instance_size = kPointerSize * internal_field_count;
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001084 InstanceType type = INVALID_TYPE;
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001085 switch (instance_type) {
1086 case JavaScriptObject:
1087 type = JS_OBJECT_TYPE;
1088 instance_size += JSObject::kHeaderSize;
1089 break;
1090 case InnerGlobalObject:
1091 type = JS_GLOBAL_OBJECT_TYPE;
1092 instance_size += JSGlobalObject::kSize;
1093 break;
1094 case OuterGlobalObject:
1095 type = JS_GLOBAL_PROXY_TYPE;
1096 instance_size += JSGlobalProxy::kSize;
1097 break;
1098 default:
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001099 break;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001100 }
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001101 ASSERT(type != INVALID_TYPE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001102
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001103 Handle<JSFunction> result =
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001104 NewFunction(Factory::empty_symbol(),
1105 type,
1106 instance_size,
1107 code,
1108 true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001109 // Set class name.
1110 Handle<Object> class_name = Handle<Object>(obj->class_name());
1111 if (class_name->IsString()) {
1112 result->shared()->set_instance_class_name(*class_name);
1113 result->shared()->set_name(*class_name);
1114 }
1115
1116 Handle<Map> map = Handle<Map>(result->initial_map());
1117
1118 // Mark as undetectable if needed.
1119 if (obj->undetectable()) {
1120 map->set_is_undetectable();
1121 }
1122
1123 // Mark as hidden for the __proto__ accessor if needed.
1124 if (obj->hidden_prototype()) {
1125 map->set_is_hidden_prototype();
1126 }
1127
1128 // Mark as needs_access_check if needed.
1129 if (obj->needs_access_check()) {
ager@chromium.org870a0b62008-11-04 11:43:05 +00001130 map->set_is_access_check_needed(true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001131 }
1132
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001133 // Set interceptor information in the map.
1134 if (!obj->named_property_handler()->IsUndefined()) {
1135 map->set_has_named_interceptor();
1136 }
1137 if (!obj->indexed_property_handler()->IsUndefined()) {
1138 map->set_has_indexed_interceptor();
1139 }
1140
1141 // Set instance call-as-function information in the map.
1142 if (!obj->instance_call_handler()->IsUndefined()) {
1143 map->set_has_instance_call_handler();
1144 }
1145
1146 result->shared()->set_function_data(*obj);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001147 result->shared()->set_construct_stub(*construct_stub);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001148 result->shared()->DontAdaptArguments();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001149
1150 // Recursively copy parent templates' accessors, 'data' may be modified.
1151 Handle<DescriptorArray> array =
1152 Handle<DescriptorArray>(map->instance_descriptors());
1153 while (true) {
1154 Handle<Object> props = Handle<Object>(obj->property_accessors());
1155 if (!props->IsUndefined()) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001156 array = CopyAppendCallbackDescriptors(array, props);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001157 }
1158 Handle<Object> parent = Handle<Object>(obj->parent_template());
1159 if (parent->IsUndefined()) break;
1160 obj = Handle<FunctionTemplateInfo>::cast(parent);
1161 }
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001162 if (!array->IsEmpty()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001163 map->set_instance_descriptors(*array);
1164 }
1165
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001166 ASSERT(result->shared()->IsApiFunction());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001167 return result;
1168}
1169
1170
ager@chromium.org236ad962008-09-25 09:45:57 +00001171Handle<MapCache> Factory::NewMapCache(int at_least_space_for) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001172 CALL_HEAP_FUNCTION(isolate(),
1173 MapCache::Allocate(at_least_space_for), MapCache);
ager@chromium.org236ad962008-09-25 09:45:57 +00001174}
1175
1176
lrn@chromium.org303ada72010-10-27 09:33:13 +00001177MUST_USE_RESULT static MaybeObject* UpdateMapCacheWith(Context* context,
1178 FixedArray* keys,
1179 Map* map) {
1180 Object* result;
1181 { MaybeObject* maybe_result =
1182 MapCache::cast(context->map_cache())->Put(keys, map);
1183 if (!maybe_result->ToObject(&result)) return maybe_result;
1184 }
1185 context->set_map_cache(MapCache::cast(result));
ager@chromium.org236ad962008-09-25 09:45:57 +00001186 return result;
1187}
1188
1189
1190Handle<MapCache> Factory::AddToMapCache(Handle<Context> context,
1191 Handle<FixedArray> keys,
1192 Handle<Map> map) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001193 CALL_HEAP_FUNCTION(isolate(),
1194 UpdateMapCacheWith(*context, *keys, *map), MapCache);
ager@chromium.org236ad962008-09-25 09:45:57 +00001195}
1196
1197
1198Handle<Map> Factory::ObjectLiteralMapFromCache(Handle<Context> context,
1199 Handle<FixedArray> keys) {
1200 if (context->map_cache()->IsUndefined()) {
1201 // Allocate the new map cache for the global context.
1202 Handle<MapCache> new_cache = NewMapCache(24);
1203 context->set_map_cache(*new_cache);
1204 }
ager@chromium.org32912102009-01-16 10:38:43 +00001205 // Check to see whether there is a matching element in the cache.
ager@chromium.org236ad962008-09-25 09:45:57 +00001206 Handle<MapCache> cache =
1207 Handle<MapCache>(MapCache::cast(context->map_cache()));
1208 Handle<Object> result = Handle<Object>(cache->Lookup(*keys));
1209 if (result->IsMap()) return Handle<Map>::cast(result);
1210 // Create a new map and add it to the cache.
1211 Handle<Map> map =
ager@chromium.org32912102009-01-16 10:38:43 +00001212 CopyMap(Handle<Map>(context->object_function()->initial_map()),
1213 keys->length());
ager@chromium.org236ad962008-09-25 09:45:57 +00001214 AddToMapCache(context, keys, map);
1215 return Handle<Map>(map);
1216}
1217
1218
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001219void Factory::SetRegExpAtomData(Handle<JSRegExp> regexp,
1220 JSRegExp::Type type,
1221 Handle<String> source,
1222 JSRegExp::Flags flags,
1223 Handle<Object> data) {
1224 Handle<FixedArray> store = NewFixedArray(JSRegExp::kAtomDataSize);
1225
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001226 store->set(JSRegExp::kTagIndex, Smi::FromInt(type));
1227 store->set(JSRegExp::kSourceIndex, *source);
1228 store->set(JSRegExp::kFlagsIndex, Smi::FromInt(flags.value()));
1229 store->set(JSRegExp::kAtomPatternIndex, *data);
1230 regexp->set_data(*store);
1231}
1232
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001233void Factory::SetRegExpIrregexpData(Handle<JSRegExp> regexp,
1234 JSRegExp::Type type,
1235 Handle<String> source,
1236 JSRegExp::Flags flags,
1237 int capture_count) {
1238 Handle<FixedArray> store = NewFixedArray(JSRegExp::kIrregexpDataSize);
jkummerow@chromium.orgddda9e82011-07-06 11:27:02 +00001239 Smi* uninitialized = Smi::FromInt(JSRegExp::kUninitializedValue);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001240 store->set(JSRegExp::kTagIndex, Smi::FromInt(type));
1241 store->set(JSRegExp::kSourceIndex, *source);
1242 store->set(JSRegExp::kFlagsIndex, Smi::FromInt(flags.value()));
jkummerow@chromium.orgddda9e82011-07-06 11:27:02 +00001243 store->set(JSRegExp::kIrregexpASCIICodeIndex, uninitialized);
1244 store->set(JSRegExp::kIrregexpUC16CodeIndex, uninitialized);
1245 store->set(JSRegExp::kIrregexpASCIICodeSavedIndex, uninitialized);
1246 store->set(JSRegExp::kIrregexpUC16CodeSavedIndex, uninitialized);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001247 store->set(JSRegExp::kIrregexpMaxRegisterCountIndex, Smi::FromInt(0));
1248 store->set(JSRegExp::kIrregexpCaptureCountIndex,
1249 Smi::FromInt(capture_count));
1250 regexp->set_data(*store);
1251}
1252
1253
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001254
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001255void Factory::ConfigureInstance(Handle<FunctionTemplateInfo> desc,
1256 Handle<JSObject> instance,
1257 bool* pending_exception) {
1258 // Configure the instance by adding the properties specified by the
1259 // instance template.
1260 Handle<Object> instance_template = Handle<Object>(desc->instance_template());
1261 if (!instance_template->IsUndefined()) {
1262 Execution::ConfigureInstance(instance,
1263 instance_template,
1264 pending_exception);
1265 } else {
1266 *pending_exception = false;
1267 }
1268}
1269
1270
1271} } // namespace v8::internal