kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1 | // Copyright 2010 the V8 project authors. All rights reserved. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2 | // 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.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 31 | #include "debug.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 32 | #include "execution.h" |
| 33 | #include "factory.h" |
| 34 | #include "macro-assembler.h" |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 35 | #include "objects.h" |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 36 | #include "objects-visiting.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 37 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 38 | namespace v8 { |
| 39 | namespace internal { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 40 | |
| 41 | |
| 42 | Handle<FixedArray> Factory::NewFixedArray(int size, PretenureFlag pretenure) { |
| 43 | ASSERT(0 <= size); |
| 44 | CALL_HEAP_FUNCTION(Heap::AllocateFixedArray(size, pretenure), FixedArray); |
| 45 | } |
| 46 | |
| 47 | |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 48 | Handle<FixedArray> Factory::NewFixedArrayWithHoles(int size, |
| 49 | PretenureFlag pretenure) { |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 50 | ASSERT(0 <= size); |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 51 | CALL_HEAP_FUNCTION(Heap::AllocateFixedArrayWithHoles(size, pretenure), |
| 52 | FixedArray); |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 56 | Handle<StringDictionary> Factory::NewStringDictionary(int at_least_space_for) { |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 57 | ASSERT(0 <= at_least_space_for); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 58 | CALL_HEAP_FUNCTION(StringDictionary::Allocate(at_least_space_for), |
| 59 | StringDictionary); |
| 60 | } |
| 61 | |
| 62 | |
| 63 | Handle<NumberDictionary> Factory::NewNumberDictionary(int at_least_space_for) { |
| 64 | ASSERT(0 <= at_least_space_for); |
| 65 | CALL_HEAP_FUNCTION(NumberDictionary::Allocate(at_least_space_for), |
| 66 | NumberDictionary); |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 70 | Handle<DescriptorArray> Factory::NewDescriptorArray(int number_of_descriptors) { |
| 71 | ASSERT(0 <= number_of_descriptors); |
| 72 | CALL_HEAP_FUNCTION(DescriptorArray::Allocate(number_of_descriptors), |
| 73 | DescriptorArray); |
| 74 | } |
| 75 | |
| 76 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 77 | Handle<DeoptimizationInputData> Factory::NewDeoptimizationInputData( |
| 78 | int deopt_entry_count, |
| 79 | PretenureFlag pretenure) { |
| 80 | ASSERT(deopt_entry_count > 0); |
| 81 | CALL_HEAP_FUNCTION(DeoptimizationInputData::Allocate(deopt_entry_count, |
| 82 | pretenure), |
| 83 | DeoptimizationInputData); |
| 84 | } |
| 85 | |
| 86 | |
| 87 | Handle<DeoptimizationOutputData> Factory::NewDeoptimizationOutputData( |
| 88 | int deopt_entry_count, |
| 89 | PretenureFlag pretenure) { |
| 90 | ASSERT(deopt_entry_count > 0); |
| 91 | CALL_HEAP_FUNCTION(DeoptimizationOutputData::Allocate(deopt_entry_count, |
| 92 | pretenure), |
| 93 | DeoptimizationOutputData); |
| 94 | } |
| 95 | |
| 96 | |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 97 | // Symbols are created in the old generation (data space). |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 98 | Handle<String> Factory::LookupSymbol(Vector<const char> string) { |
| 99 | CALL_HEAP_FUNCTION(Heap::LookupSymbol(string), String); |
| 100 | } |
| 101 | |
fschneider@chromium.org | 9e3e0b6 | 2011-01-03 10:16:46 +0000 | [diff] [blame] | 102 | Handle<String> Factory::LookupAsciiSymbol(Vector<const char> string) { |
| 103 | CALL_HEAP_FUNCTION(Heap::LookupAsciiSymbol(string), String); |
| 104 | } |
| 105 | |
| 106 | Handle<String> Factory::LookupTwoByteSymbol(Vector<const uc16> string) { |
| 107 | CALL_HEAP_FUNCTION(Heap::LookupTwoByteSymbol(string), String); |
| 108 | } |
| 109 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 110 | |
| 111 | Handle<String> Factory::NewStringFromAscii(Vector<const char> string, |
| 112 | PretenureFlag pretenure) { |
| 113 | CALL_HEAP_FUNCTION(Heap::AllocateStringFromAscii(string, pretenure), String); |
| 114 | } |
| 115 | |
| 116 | Handle<String> Factory::NewStringFromUtf8(Vector<const char> string, |
| 117 | PretenureFlag pretenure) { |
| 118 | CALL_HEAP_FUNCTION(Heap::AllocateStringFromUtf8(string, pretenure), String); |
| 119 | } |
| 120 | |
| 121 | |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 122 | Handle<String> Factory::NewStringFromTwoByte(Vector<const uc16> string, |
| 123 | PretenureFlag pretenure) { |
| 124 | CALL_HEAP_FUNCTION(Heap::AllocateStringFromTwoByte(string, pretenure), |
| 125 | String); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | |
erik.corry@gmail.com | 4a2e25e | 2010-07-07 12:22:46 +0000 | [diff] [blame] | 129 | Handle<String> Factory::NewRawAsciiString(int length, |
| 130 | PretenureFlag pretenure) { |
| 131 | CALL_HEAP_FUNCTION(Heap::AllocateRawAsciiString(length, pretenure), String); |
| 132 | } |
| 133 | |
| 134 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 135 | Handle<String> Factory::NewRawTwoByteString(int length, |
| 136 | PretenureFlag pretenure) { |
| 137 | CALL_HEAP_FUNCTION(Heap::AllocateRawTwoByteString(length, pretenure), String); |
| 138 | } |
| 139 | |
| 140 | |
| 141 | Handle<String> Factory::NewConsString(Handle<String> first, |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 142 | Handle<String> second) { |
ager@chromium.org | c3e50d8 | 2008-11-05 11:53:10 +0000 | [diff] [blame] | 143 | CALL_HEAP_FUNCTION(Heap::AllocateConsString(*first, *second), String); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 147 | Handle<String> Factory::NewSubString(Handle<String> str, |
| 148 | int begin, |
| 149 | int end) { |
| 150 | CALL_HEAP_FUNCTION(str->SubString(begin, end), String); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | |
| 154 | Handle<String> Factory::NewExternalStringFromAscii( |
| 155 | ExternalAsciiString::Resource* resource) { |
| 156 | CALL_HEAP_FUNCTION(Heap::AllocateExternalStringFromAscii(resource), String); |
| 157 | } |
| 158 | |
| 159 | |
| 160 | Handle<String> Factory::NewExternalStringFromTwoByte( |
| 161 | ExternalTwoByteString::Resource* resource) { |
| 162 | CALL_HEAP_FUNCTION(Heap::AllocateExternalStringFromTwoByte(resource), String); |
| 163 | } |
| 164 | |
| 165 | |
| 166 | Handle<Context> Factory::NewGlobalContext() { |
| 167 | CALL_HEAP_FUNCTION(Heap::AllocateGlobalContext(), Context); |
| 168 | } |
| 169 | |
| 170 | |
| 171 | Handle<Context> Factory::NewFunctionContext(int length, |
| 172 | Handle<JSFunction> closure) { |
| 173 | CALL_HEAP_FUNCTION(Heap::AllocateFunctionContext(length, *closure), Context); |
| 174 | } |
| 175 | |
| 176 | |
| 177 | Handle<Context> Factory::NewWithContext(Handle<Context> previous, |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 178 | Handle<JSObject> extension, |
| 179 | bool is_catch_context) { |
| 180 | CALL_HEAP_FUNCTION(Heap::AllocateWithContext(*previous, |
| 181 | *extension, |
| 182 | is_catch_context), |
| 183 | Context); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | |
| 187 | Handle<Struct> Factory::NewStruct(InstanceType type) { |
| 188 | CALL_HEAP_FUNCTION(Heap::AllocateStruct(type), Struct); |
| 189 | } |
| 190 | |
| 191 | |
| 192 | Handle<AccessorInfo> Factory::NewAccessorInfo() { |
| 193 | Handle<AccessorInfo> info = |
| 194 | Handle<AccessorInfo>::cast(NewStruct(ACCESSOR_INFO_TYPE)); |
| 195 | info->set_flag(0); // Must clear the flag, it was initialized as undefined. |
| 196 | return info; |
| 197 | } |
| 198 | |
| 199 | |
| 200 | Handle<Script> Factory::NewScript(Handle<String> source) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 201 | // Generate id for this script. |
| 202 | int id; |
| 203 | if (Heap::last_script_id()->IsUndefined()) { |
| 204 | // Script ids start from one. |
| 205 | id = 1; |
| 206 | } else { |
| 207 | // Increment id, wrap when positive smi is exhausted. |
| 208 | id = Smi::cast(Heap::last_script_id())->value(); |
| 209 | id++; |
| 210 | if (!Smi::IsValid(id)) { |
| 211 | id = 0; |
| 212 | } |
| 213 | } |
| 214 | Heap::SetLastScriptId(Smi::FromInt(id)); |
| 215 | |
| 216 | // Create and initialize script object. |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 217 | Handle<Proxy> wrapper = Factory::NewProxy(0, TENURED); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 218 | Handle<Script> script = Handle<Script>::cast(NewStruct(SCRIPT_TYPE)); |
| 219 | script->set_source(*source); |
| 220 | script->set_name(Heap::undefined_value()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 221 | script->set_id(Heap::last_script_id()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 222 | script->set_line_offset(Smi::FromInt(0)); |
| 223 | script->set_column_offset(Smi::FromInt(0)); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 224 | script->set_data(Heap::undefined_value()); |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 225 | script->set_context_data(Heap::undefined_value()); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 226 | script->set_type(Smi::FromInt(Script::TYPE_NORMAL)); |
| 227 | script->set_compilation_type(Smi::FromInt(Script::COMPILATION_TYPE_HOST)); |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 228 | script->set_wrapper(*wrapper); |
sgjesse@chromium.org | 499aaa5 | 2009-11-30 08:07:20 +0000 | [diff] [blame] | 229 | script->set_line_ends(Heap::undefined_value()); |
sgjesse@chromium.org | 9818059 | 2009-12-02 08:17:28 +0000 | [diff] [blame] | 230 | script->set_eval_from_shared(Heap::undefined_value()); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 231 | script->set_eval_from_instructions_offset(Smi::FromInt(0)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 232 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 233 | return script; |
| 234 | } |
| 235 | |
| 236 | |
| 237 | Handle<Proxy> Factory::NewProxy(Address addr, PretenureFlag pretenure) { |
| 238 | CALL_HEAP_FUNCTION(Heap::AllocateProxy(addr, pretenure), Proxy); |
| 239 | } |
| 240 | |
| 241 | |
| 242 | Handle<Proxy> Factory::NewProxy(const AccessorDescriptor* desc) { |
| 243 | return NewProxy((Address) desc, TENURED); |
| 244 | } |
| 245 | |
| 246 | |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 247 | Handle<ByteArray> Factory::NewByteArray(int length, PretenureFlag pretenure) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 248 | ASSERT(0 <= length); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 249 | CALL_HEAP_FUNCTION(Heap::AllocateByteArray(length, pretenure), ByteArray); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 253 | Handle<ExternalArray> Factory::NewExternalArray(int length, |
| 254 | ExternalArrayType array_type, |
| 255 | void* external_pointer, |
| 256 | PretenureFlag pretenure) { |
| 257 | ASSERT(0 <= length); |
| 258 | CALL_HEAP_FUNCTION(Heap::AllocateExternalArray(length, |
| 259 | array_type, |
| 260 | external_pointer, |
| 261 | pretenure), ExternalArray); |
| 262 | } |
| 263 | |
| 264 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 265 | Handle<JSGlobalPropertyCell> Factory::NewJSGlobalPropertyCell( |
| 266 | Handle<Object> value) { |
| 267 | CALL_HEAP_FUNCTION(Heap::AllocateJSGlobalPropertyCell(*value), |
| 268 | JSGlobalPropertyCell); |
| 269 | } |
| 270 | |
| 271 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 272 | Handle<Map> Factory::NewMap(InstanceType type, int instance_size) { |
| 273 | CALL_HEAP_FUNCTION(Heap::AllocateMap(type, instance_size), Map); |
| 274 | } |
| 275 | |
| 276 | |
| 277 | Handle<JSObject> Factory::NewFunctionPrototype(Handle<JSFunction> function) { |
| 278 | CALL_HEAP_FUNCTION(Heap::AllocateFunctionPrototype(*function), JSObject); |
| 279 | } |
| 280 | |
| 281 | |
ager@chromium.org | 3a37e9b | 2009-04-27 09:26:21 +0000 | [diff] [blame] | 282 | Handle<Map> Factory::CopyMapDropDescriptors(Handle<Map> src) { |
| 283 | CALL_HEAP_FUNCTION(src->CopyDropDescriptors(), Map); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 287 | Handle<Map> Factory::CopyMap(Handle<Map> src, |
| 288 | int extra_inobject_properties) { |
ager@chromium.org | 3a37e9b | 2009-04-27 09:26:21 +0000 | [diff] [blame] | 289 | Handle<Map> copy = CopyMapDropDescriptors(src); |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 290 | // Check that we do not overflow the instance size when adding the |
| 291 | // extra inobject properties. |
| 292 | int instance_size_delta = extra_inobject_properties * kPointerSize; |
| 293 | int max_instance_size_delta = |
| 294 | JSObject::kMaxInstanceSize - copy->instance_size(); |
| 295 | if (instance_size_delta > max_instance_size_delta) { |
| 296 | // If the instance size overflows, we allocate as many properties |
| 297 | // as we can as inobject properties. |
| 298 | instance_size_delta = max_instance_size_delta; |
| 299 | extra_inobject_properties = max_instance_size_delta >> kPointerSizeLog2; |
| 300 | } |
| 301 | // Adjust the map with the extra inobject properties. |
| 302 | int inobject_properties = |
| 303 | copy->inobject_properties() + extra_inobject_properties; |
| 304 | copy->set_inobject_properties(inobject_properties); |
| 305 | copy->set_unused_property_fields(inobject_properties); |
| 306 | copy->set_instance_size(copy->instance_size() + instance_size_delta); |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 307 | copy->set_visitor_id(StaticVisitorBase::GetVisitorId(*copy)); |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 308 | return copy; |
| 309 | } |
| 310 | |
fschneider@chromium.org | 40b9da3 | 2010-06-28 11:29:21 +0000 | [diff] [blame] | 311 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 312 | Handle<Map> Factory::CopyMapDropTransitions(Handle<Map> src) { |
| 313 | CALL_HEAP_FUNCTION(src->CopyDropTransitions(), Map); |
| 314 | } |
| 315 | |
| 316 | |
fschneider@chromium.org | 40b9da3 | 2010-06-28 11:29:21 +0000 | [diff] [blame] | 317 | Handle<Map> Factory::GetFastElementsMap(Handle<Map> src) { |
| 318 | CALL_HEAP_FUNCTION(src->GetFastElementsMap(), Map); |
| 319 | } |
| 320 | |
| 321 | |
| 322 | Handle<Map> Factory::GetSlowElementsMap(Handle<Map> src) { |
| 323 | CALL_HEAP_FUNCTION(src->GetSlowElementsMap(), Map); |
| 324 | } |
| 325 | |
| 326 | |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame^] | 327 | Handle<Map> Factory::NewExternalArrayElementsMap(Handle<Map> src) { |
| 328 | CALL_HEAP_FUNCTION(src->NewExternalArrayElementsMap(), Map); |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 332 | Handle<FixedArray> Factory::CopyFixedArray(Handle<FixedArray> array) { |
| 333 | CALL_HEAP_FUNCTION(array->Copy(), FixedArray); |
| 334 | } |
| 335 | |
| 336 | |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 337 | Handle<JSFunction> Factory::BaseNewFunctionFromSharedFunctionInfo( |
| 338 | Handle<SharedFunctionInfo> function_info, |
sgjesse@chromium.org | 846fb74 | 2009-12-18 08:56:33 +0000 | [diff] [blame] | 339 | Handle<Map> function_map, |
| 340 | PretenureFlag pretenure) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 341 | CALL_HEAP_FUNCTION(Heap::AllocateFunction(*function_map, |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 342 | *function_info, |
sgjesse@chromium.org | 846fb74 | 2009-12-18 08:56:33 +0000 | [diff] [blame] | 343 | Heap::the_hole_value(), |
| 344 | pretenure), |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 345 | JSFunction); |
| 346 | } |
| 347 | |
| 348 | |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 349 | Handle<JSFunction> Factory::NewFunctionFromSharedFunctionInfo( |
| 350 | Handle<SharedFunctionInfo> function_info, |
sgjesse@chromium.org | 846fb74 | 2009-12-18 08:56:33 +0000 | [diff] [blame] | 351 | Handle<Context> context, |
| 352 | PretenureFlag pretenure) { |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 353 | Handle<JSFunction> result = BaseNewFunctionFromSharedFunctionInfo( |
| 354 | function_info, Top::function_map(), pretenure); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 355 | result->set_context(*context); |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 356 | int number_of_literals = function_info->num_literals(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 357 | Handle<FixedArray> literals = |
sgjesse@chromium.org | 846fb74 | 2009-12-18 08:56:33 +0000 | [diff] [blame] | 358 | Factory::NewFixedArray(number_of_literals, pretenure); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 359 | if (number_of_literals > 0) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 360 | // Store the object, regexp and array functions in the literals |
| 361 | // array prefix. These functions will be used when creating |
| 362 | // object, regexp and array literals in this function. |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 363 | literals->set(JSFunction::kLiteralGlobalContextIndex, |
| 364 | context->global_context()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 365 | } |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 366 | result->set_literals(*literals); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 367 | result->set_next_function_link(Heap::undefined_value()); |
| 368 | |
| 369 | if (V8::UseCrankshaft() && |
| 370 | FLAG_always_opt && |
| 371 | result->is_compiled() && |
| 372 | !function_info->is_toplevel() && |
| 373 | function_info->allows_lazy_compilation()) { |
| 374 | result->MarkForLazyRecompilation(); |
| 375 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 376 | return result; |
| 377 | } |
| 378 | |
| 379 | |
| 380 | Handle<Object> Factory::NewNumber(double value, |
| 381 | PretenureFlag pretenure) { |
| 382 | CALL_HEAP_FUNCTION(Heap::NumberFromDouble(value, pretenure), Object); |
| 383 | } |
| 384 | |
| 385 | |
| 386 | Handle<Object> Factory::NewNumberFromInt(int value) { |
| 387 | CALL_HEAP_FUNCTION(Heap::NumberFromInt32(value), Object); |
| 388 | } |
| 389 | |
| 390 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 391 | Handle<Object> Factory::NewNumberFromUint(uint32_t value) { |
| 392 | CALL_HEAP_FUNCTION(Heap::NumberFromUint32(value), Object); |
| 393 | } |
| 394 | |
| 395 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 396 | Handle<JSObject> Factory::NewNeanderObject() { |
| 397 | CALL_HEAP_FUNCTION(Heap::AllocateJSObjectFromMap(Heap::neander_map()), |
| 398 | JSObject); |
| 399 | } |
| 400 | |
| 401 | |
| 402 | Handle<Object> Factory::NewTypeError(const char* type, |
| 403 | Vector< Handle<Object> > args) { |
| 404 | return NewError("MakeTypeError", type, args); |
| 405 | } |
| 406 | |
| 407 | |
| 408 | Handle<Object> Factory::NewTypeError(Handle<String> message) { |
| 409 | return NewError("$TypeError", message); |
| 410 | } |
| 411 | |
| 412 | |
| 413 | Handle<Object> Factory::NewRangeError(const char* type, |
| 414 | Vector< Handle<Object> > args) { |
| 415 | return NewError("MakeRangeError", type, args); |
| 416 | } |
| 417 | |
| 418 | |
| 419 | Handle<Object> Factory::NewRangeError(Handle<String> message) { |
| 420 | return NewError("$RangeError", message); |
| 421 | } |
| 422 | |
| 423 | |
| 424 | Handle<Object> Factory::NewSyntaxError(const char* type, Handle<JSArray> args) { |
| 425 | return NewError("MakeSyntaxError", type, args); |
| 426 | } |
| 427 | |
| 428 | |
| 429 | Handle<Object> Factory::NewSyntaxError(Handle<String> message) { |
| 430 | return NewError("$SyntaxError", message); |
| 431 | } |
| 432 | |
| 433 | |
| 434 | Handle<Object> Factory::NewReferenceError(const char* type, |
| 435 | Vector< Handle<Object> > args) { |
| 436 | return NewError("MakeReferenceError", type, args); |
| 437 | } |
| 438 | |
| 439 | |
| 440 | Handle<Object> Factory::NewReferenceError(Handle<String> message) { |
| 441 | return NewError("$ReferenceError", message); |
| 442 | } |
| 443 | |
| 444 | |
| 445 | Handle<Object> Factory::NewError(const char* maker, const char* type, |
| 446 | Vector< Handle<Object> > args) { |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 447 | v8::HandleScope scope; // Instantiate a closeable HandleScope for EscapeFrom. |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 448 | Handle<FixedArray> array = Factory::NewFixedArray(args.length()); |
| 449 | for (int i = 0; i < args.length(); i++) { |
| 450 | array->set(i, *args[i]); |
| 451 | } |
| 452 | Handle<JSArray> object = Factory::NewJSArrayWithElements(array); |
| 453 | Handle<Object> result = NewError(maker, type, object); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 454 | return result.EscapeFrom(&scope); |
| 455 | } |
| 456 | |
| 457 | |
| 458 | Handle<Object> Factory::NewEvalError(const char* type, |
| 459 | Vector< Handle<Object> > args) { |
| 460 | return NewError("MakeEvalError", type, args); |
| 461 | } |
| 462 | |
| 463 | |
| 464 | Handle<Object> Factory::NewError(const char* type, |
| 465 | Vector< Handle<Object> > args) { |
| 466 | return NewError("MakeError", type, args); |
| 467 | } |
| 468 | |
| 469 | |
| 470 | Handle<Object> Factory::NewError(const char* maker, |
| 471 | const char* type, |
| 472 | Handle<JSArray> args) { |
| 473 | Handle<String> make_str = Factory::LookupAsciiSymbol(maker); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 474 | Handle<Object> fun_obj(Top::builtins()->GetPropertyNoExceptionThrown( |
| 475 | *make_str)); |
ager@chromium.org | 4af710e | 2009-09-15 12:20:11 +0000 | [diff] [blame] | 476 | // If the builtins haven't been properly configured yet this error |
| 477 | // constructor may not have been defined. Bail out. |
| 478 | if (!fun_obj->IsJSFunction()) |
| 479 | return Factory::undefined_value(); |
| 480 | Handle<JSFunction> fun = Handle<JSFunction>::cast(fun_obj); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 481 | Handle<Object> type_obj = Factory::LookupAsciiSymbol(type); |
| 482 | Object** argv[2] = { type_obj.location(), |
| 483 | Handle<Object>::cast(args).location() }; |
| 484 | |
| 485 | // Invoke the JavaScript factory method. If an exception is thrown while |
| 486 | // running the factory method, use the exception as the result. |
| 487 | bool caught_exception; |
| 488 | Handle<Object> result = Execution::TryCall(fun, |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 489 | Top::builtins(), |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 490 | 2, |
| 491 | argv, |
| 492 | &caught_exception); |
| 493 | return result; |
| 494 | } |
| 495 | |
| 496 | |
| 497 | Handle<Object> Factory::NewError(Handle<String> message) { |
| 498 | return NewError("$Error", message); |
| 499 | } |
| 500 | |
| 501 | |
| 502 | Handle<Object> Factory::NewError(const char* constructor, |
| 503 | Handle<String> message) { |
| 504 | Handle<String> constr = Factory::LookupAsciiSymbol(constructor); |
| 505 | Handle<JSFunction> fun = |
| 506 | Handle<JSFunction>( |
| 507 | JSFunction::cast( |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 508 | Top::builtins()->GetPropertyNoExceptionThrown(*constr))); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 509 | Object** argv[1] = { Handle<Object>::cast(message).location() }; |
| 510 | |
| 511 | // Invoke the JavaScript factory method. If an exception is thrown while |
| 512 | // running the factory method, use the exception as the result. |
| 513 | bool caught_exception; |
| 514 | Handle<Object> result = Execution::TryCall(fun, |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 515 | Top::builtins(), |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 516 | 1, |
| 517 | argv, |
| 518 | &caught_exception); |
| 519 | return result; |
| 520 | } |
| 521 | |
| 522 | |
| 523 | Handle<JSFunction> Factory::NewFunction(Handle<String> name, |
| 524 | InstanceType type, |
| 525 | int instance_size, |
| 526 | Handle<Code> code, |
| 527 | bool force_initial_map) { |
| 528 | // Allocate the function |
| 529 | Handle<JSFunction> function = NewFunction(name, the_hole_value()); |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 530 | |
| 531 | // Setup the code pointer in both the shared function info and in |
| 532 | // the function itself. |
| 533 | function->shared()->set_code(*code); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 534 | function->set_code(*code); |
| 535 | |
| 536 | if (force_initial_map || |
| 537 | type != JS_OBJECT_TYPE || |
| 538 | instance_size != JSObject::kHeaderSize) { |
| 539 | Handle<Map> initial_map = NewMap(type, instance_size); |
| 540 | Handle<JSObject> prototype = NewFunctionPrototype(function); |
| 541 | initial_map->set_prototype(*prototype); |
| 542 | function->set_initial_map(*initial_map); |
| 543 | initial_map->set_constructor(*function); |
| 544 | } else { |
| 545 | ASSERT(!function->has_initial_map()); |
| 546 | ASSERT(!function->has_prototype()); |
| 547 | } |
| 548 | |
| 549 | return function; |
| 550 | } |
| 551 | |
| 552 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 553 | Handle<JSFunction> Factory::NewFunctionWithPrototype(Handle<String> name, |
| 554 | InstanceType type, |
| 555 | int instance_size, |
| 556 | Handle<JSObject> prototype, |
| 557 | Handle<Code> code, |
| 558 | bool force_initial_map) { |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 559 | // Allocate the function. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 560 | Handle<JSFunction> function = NewFunction(name, prototype); |
| 561 | |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 562 | // Setup the code pointer in both the shared function info and in |
| 563 | // the function itself. |
| 564 | function->shared()->set_code(*code); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 565 | function->set_code(*code); |
| 566 | |
| 567 | if (force_initial_map || |
| 568 | type != JS_OBJECT_TYPE || |
| 569 | instance_size != JSObject::kHeaderSize) { |
| 570 | Handle<Map> initial_map = NewMap(type, instance_size); |
| 571 | function->set_initial_map(*initial_map); |
| 572 | initial_map->set_constructor(*function); |
| 573 | } |
| 574 | |
| 575 | // Set function.prototype and give the prototype a constructor |
| 576 | // property that refers to the function. |
| 577 | SetPrototypeProperty(function, prototype); |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 578 | // Currently safe because it is only invoked from Genesis. |
| 579 | SetLocalPropertyNoThrow( |
| 580 | prototype, Factory::constructor_symbol(), function, DONT_ENUM); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 581 | return function; |
| 582 | } |
| 583 | |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 584 | |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 585 | Handle<JSFunction> Factory::NewFunctionWithoutPrototype(Handle<String> name, |
| 586 | Handle<Code> code) { |
| 587 | Handle<JSFunction> function = NewFunctionWithoutPrototype(name); |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 588 | function->shared()->set_code(*code); |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 589 | function->set_code(*code); |
| 590 | ASSERT(!function->has_initial_map()); |
| 591 | ASSERT(!function->has_prototype()); |
| 592 | return function; |
| 593 | } |
| 594 | |
| 595 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 596 | Handle<Code> Factory::NewCode(const CodeDesc& desc, |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 597 | Code::Flags flags, |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame^] | 598 | Handle<Object> self_ref, |
| 599 | bool immovable) { |
| 600 | CALL_HEAP_FUNCTION(Heap::CreateCode(desc, flags, self_ref, immovable), Code); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 601 | } |
| 602 | |
| 603 | |
| 604 | Handle<Code> Factory::CopyCode(Handle<Code> code) { |
| 605 | CALL_HEAP_FUNCTION(Heap::CopyCode(*code), Code); |
| 606 | } |
| 607 | |
| 608 | |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 609 | Handle<Code> Factory::CopyCode(Handle<Code> code, Vector<byte> reloc_info) { |
| 610 | CALL_HEAP_FUNCTION(Heap::CopyCode(*code, reloc_info), Code); |
| 611 | } |
| 612 | |
| 613 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 614 | MUST_USE_RESULT static inline MaybeObject* DoCopyInsert( |
| 615 | DescriptorArray* array, |
| 616 | String* key, |
| 617 | Object* value, |
| 618 | PropertyAttributes attributes) { |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 619 | CallbacksDescriptor desc(key, value, attributes); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 620 | MaybeObject* obj = array->CopyInsert(&desc, REMOVE_TRANSITIONS); |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 621 | return obj; |
| 622 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 623 | |
| 624 | |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 625 | // Allocate the new array. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 626 | Handle<DescriptorArray> Factory::CopyAppendProxyDescriptor( |
| 627 | Handle<DescriptorArray> array, |
| 628 | Handle<String> key, |
| 629 | Handle<Object> value, |
| 630 | PropertyAttributes attributes) { |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 631 | CALL_HEAP_FUNCTION(DoCopyInsert(*array, *key, *value, attributes), |
| 632 | DescriptorArray); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 633 | } |
| 634 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 635 | |
| 636 | Handle<String> Factory::SymbolFromString(Handle<String> value) { |
| 637 | CALL_HEAP_FUNCTION(Heap::LookupSymbol(*value), String); |
| 638 | } |
| 639 | |
| 640 | |
| 641 | Handle<DescriptorArray> Factory::CopyAppendCallbackDescriptors( |
| 642 | Handle<DescriptorArray> array, |
| 643 | Handle<Object> descriptors) { |
| 644 | v8::NeanderArray callbacks(descriptors); |
| 645 | int nof_callbacks = callbacks.length(); |
| 646 | Handle<DescriptorArray> result = |
| 647 | NewDescriptorArray(array->number_of_descriptors() + nof_callbacks); |
| 648 | |
| 649 | // Number of descriptors added to the result so far. |
| 650 | int descriptor_count = 0; |
| 651 | |
| 652 | // Copy the descriptors from the array. |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 653 | for (int i = 0; i < array->number_of_descriptors(); i++) { |
| 654 | if (array->GetType(i) != NULL_DESCRIPTOR) { |
| 655 | result->CopyFrom(descriptor_count++, *array, i); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 656 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 657 | } |
| 658 | |
| 659 | // Number of duplicates detected. |
| 660 | int duplicates = 0; |
| 661 | |
| 662 | // Fill in new callback descriptors. Process the callbacks from |
| 663 | // back to front so that the last callback with a given name takes |
| 664 | // precedence over previously added callbacks with that name. |
| 665 | for (int i = nof_callbacks - 1; i >= 0; i--) { |
| 666 | Handle<AccessorInfo> entry = |
| 667 | Handle<AccessorInfo>(AccessorInfo::cast(callbacks.get(i))); |
| 668 | // Ensure the key is a symbol before writing into the instance descriptor. |
| 669 | Handle<String> key = |
| 670 | SymbolFromString(Handle<String>(String::cast(entry->name()))); |
| 671 | // Check if a descriptor with this name already exists before writing. |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 672 | if (result->LinearSearch(*key, descriptor_count) == |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 673 | DescriptorArray::kNotFound) { |
| 674 | CallbacksDescriptor desc(*key, *entry, entry->property_attributes()); |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 675 | result->Set(descriptor_count, &desc); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 676 | descriptor_count++; |
| 677 | } else { |
| 678 | duplicates++; |
| 679 | } |
| 680 | } |
| 681 | |
| 682 | // If duplicates were detected, allocate a result of the right size |
| 683 | // and transfer the elements. |
| 684 | if (duplicates > 0) { |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 685 | int number_of_descriptors = result->number_of_descriptors() - duplicates; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 686 | Handle<DescriptorArray> new_result = |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 687 | NewDescriptorArray(number_of_descriptors); |
| 688 | for (int i = 0; i < number_of_descriptors; i++) { |
| 689 | new_result->CopyFrom(i, *result, i); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 690 | } |
| 691 | result = new_result; |
| 692 | } |
| 693 | |
| 694 | // Sort the result before returning. |
| 695 | result->Sort(); |
| 696 | return result; |
| 697 | } |
| 698 | |
| 699 | |
| 700 | Handle<JSObject> Factory::NewJSObject(Handle<JSFunction> constructor, |
| 701 | PretenureFlag pretenure) { |
| 702 | CALL_HEAP_FUNCTION(Heap::AllocateJSObject(*constructor, pretenure), JSObject); |
| 703 | } |
| 704 | |
| 705 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 706 | Handle<GlobalObject> Factory::NewGlobalObject( |
| 707 | Handle<JSFunction> constructor) { |
| 708 | CALL_HEAP_FUNCTION(Heap::AllocateGlobalObject(*constructor), |
| 709 | GlobalObject); |
| 710 | } |
| 711 | |
| 712 | |
| 713 | |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 714 | Handle<JSObject> Factory::NewJSObjectFromMap(Handle<Map> map) { |
| 715 | CALL_HEAP_FUNCTION(Heap::AllocateJSObjectFromMap(*map, NOT_TENURED), |
| 716 | JSObject); |
| 717 | } |
| 718 | |
| 719 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 720 | Handle<JSArray> Factory::NewJSArray(int length, |
| 721 | PretenureFlag pretenure) { |
| 722 | Handle<JSObject> obj = NewJSObject(Top::array_function(), pretenure); |
| 723 | CALL_HEAP_FUNCTION(Handle<JSArray>::cast(obj)->Initialize(length), JSArray); |
| 724 | } |
| 725 | |
| 726 | |
| 727 | Handle<JSArray> Factory::NewJSArrayWithElements(Handle<FixedArray> elements, |
| 728 | PretenureFlag pretenure) { |
| 729 | Handle<JSArray> result = |
| 730 | Handle<JSArray>::cast(NewJSObject(Top::array_function(), pretenure)); |
| 731 | result->SetContent(*elements); |
| 732 | return result; |
| 733 | } |
| 734 | |
| 735 | |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 736 | Handle<SharedFunctionInfo> Factory::NewSharedFunctionInfo( |
ager@chromium.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 737 | Handle<String> name, |
| 738 | int number_of_literals, |
| 739 | Handle<Code> code, |
ager@chromium.org | b573749 | 2010-07-15 09:29:43 +0000 | [diff] [blame] | 740 | Handle<SerializedScopeInfo> scope_info) { |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 741 | Handle<SharedFunctionInfo> shared = NewSharedFunctionInfo(name); |
| 742 | shared->set_code(*code); |
ager@chromium.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 743 | shared->set_scope_info(*scope_info); |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 744 | int literals_array_size = number_of_literals; |
| 745 | // If the function contains object, regexp or array literals, |
| 746 | // allocate extra space for a literals array prefix containing the |
| 747 | // context. |
| 748 | if (number_of_literals > 0) { |
| 749 | literals_array_size += JSFunction::kLiteralsPrefixSize; |
| 750 | } |
| 751 | shared->set_num_literals(literals_array_size); |
| 752 | return shared; |
| 753 | } |
| 754 | |
| 755 | |
kmillikin@chromium.org | 31b1277 | 2011-02-02 16:08:26 +0000 | [diff] [blame] | 756 | Handle<JSMessageObject> Factory::NewJSMessageObject( |
| 757 | Handle<String> type, |
| 758 | Handle<JSArray> arguments, |
| 759 | int start_position, |
| 760 | int end_position, |
| 761 | Handle<Object> script, |
| 762 | Handle<Object> stack_trace, |
| 763 | Handle<Object> stack_frames) { |
| 764 | CALL_HEAP_FUNCTION(Heap::AllocateJSMessageObject(*type, |
| 765 | *arguments, |
| 766 | start_position, |
| 767 | end_position, |
| 768 | *script, |
| 769 | *stack_trace, |
| 770 | *stack_frames), |
| 771 | JSMessageObject); |
| 772 | } |
| 773 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 774 | Handle<SharedFunctionInfo> Factory::NewSharedFunctionInfo(Handle<String> name) { |
| 775 | CALL_HEAP_FUNCTION(Heap::AllocateSharedFunctionInfo(*name), |
| 776 | SharedFunctionInfo); |
| 777 | } |
| 778 | |
| 779 | |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 780 | Handle<String> Factory::NumberToString(Handle<Object> number) { |
| 781 | CALL_HEAP_FUNCTION(Heap::NumberToString(*number), String); |
| 782 | } |
| 783 | |
| 784 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 785 | Handle<NumberDictionary> Factory::DictionaryAtNumberPut( |
| 786 | Handle<NumberDictionary> dictionary, |
| 787 | uint32_t key, |
| 788 | Handle<Object> value) { |
| 789 | CALL_HEAP_FUNCTION(dictionary->AtNumberPut(key, *value), NumberDictionary); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 790 | } |
| 791 | |
| 792 | |
| 793 | Handle<JSFunction> Factory::NewFunctionHelper(Handle<String> name, |
| 794 | Handle<Object> prototype) { |
| 795 | Handle<SharedFunctionInfo> function_share = NewSharedFunctionInfo(name); |
| 796 | CALL_HEAP_FUNCTION(Heap::AllocateFunction(*Top::function_map(), |
| 797 | *function_share, |
| 798 | *prototype), |
| 799 | JSFunction); |
| 800 | } |
| 801 | |
| 802 | |
| 803 | Handle<JSFunction> Factory::NewFunction(Handle<String> name, |
| 804 | Handle<Object> prototype) { |
| 805 | Handle<JSFunction> fun = NewFunctionHelper(name, prototype); |
| 806 | fun->set_context(Top::context()->global_context()); |
| 807 | return fun; |
| 808 | } |
| 809 | |
| 810 | |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 811 | Handle<JSFunction> Factory::NewFunctionWithoutPrototypeHelper( |
| 812 | Handle<String> name) { |
| 813 | Handle<SharedFunctionInfo> function_share = NewSharedFunctionInfo(name); |
| 814 | CALL_HEAP_FUNCTION(Heap::AllocateFunction( |
| 815 | *Top::function_without_prototype_map(), |
| 816 | *function_share, |
| 817 | *the_hole_value()), |
| 818 | JSFunction); |
| 819 | } |
| 820 | |
| 821 | |
| 822 | Handle<JSFunction> Factory::NewFunctionWithoutPrototype(Handle<String> name) { |
| 823 | Handle<JSFunction> fun = NewFunctionWithoutPrototypeHelper(name); |
| 824 | fun->set_context(Top::context()->global_context()); |
| 825 | return fun; |
| 826 | } |
| 827 | |
| 828 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 829 | Handle<Object> Factory::ToObject(Handle<Object> object) { |
| 830 | CALL_HEAP_FUNCTION(object->ToObject(), Object); |
| 831 | } |
| 832 | |
| 833 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 834 | Handle<Object> Factory::ToObject(Handle<Object> object, |
| 835 | Handle<Context> global_context) { |
| 836 | CALL_HEAP_FUNCTION(object->ToObject(*global_context), Object); |
| 837 | } |
| 838 | |
| 839 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 840 | #ifdef ENABLE_DEBUGGER_SUPPORT |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 841 | Handle<DebugInfo> Factory::NewDebugInfo(Handle<SharedFunctionInfo> shared) { |
| 842 | // Get the original code of the function. |
| 843 | Handle<Code> code(shared->code()); |
| 844 | |
| 845 | // Create a copy of the code before allocating the debug info object to avoid |
| 846 | // allocation while setting up the debug info object. |
| 847 | Handle<Code> original_code(*Factory::CopyCode(code)); |
| 848 | |
| 849 | // Allocate initial fixed array for active break points before allocating the |
| 850 | // debug info object to avoid allocation while setting up the debug info |
| 851 | // object. |
| 852 | Handle<FixedArray> break_points( |
| 853 | Factory::NewFixedArray(Debug::kEstimatedNofBreakPointsInFunction)); |
| 854 | |
| 855 | // Create and set up the debug info object. Debug info contains function, a |
| 856 | // copy of the original code, the executing code and initial fixed array for |
| 857 | // active break points. |
| 858 | Handle<DebugInfo> debug_info = |
| 859 | Handle<DebugInfo>::cast(Factory::NewStruct(DEBUG_INFO_TYPE)); |
| 860 | debug_info->set_shared(*shared); |
| 861 | debug_info->set_original_code(*original_code); |
| 862 | debug_info->set_code(*code); |
| 863 | debug_info->set_break_points(*break_points); |
| 864 | |
| 865 | // Link debug info to function. |
| 866 | shared->set_debug_info(*debug_info); |
| 867 | |
| 868 | return debug_info; |
| 869 | } |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 870 | #endif |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 871 | |
| 872 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 873 | Handle<JSObject> Factory::NewArgumentsObject(Handle<Object> callee, |
| 874 | int length) { |
| 875 | CALL_HEAP_FUNCTION(Heap::AllocateArgumentsObject(*callee, length), JSObject); |
| 876 | } |
| 877 | |
| 878 | |
| 879 | Handle<JSFunction> Factory::CreateApiFunction( |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 880 | Handle<FunctionTemplateInfo> obj, ApiInstanceType instance_type) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 881 | Handle<Code> code = Handle<Code>(Builtins::builtin(Builtins::HandleApiCall)); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 882 | Handle<Code> construct_stub = |
| 883 | Handle<Code>(Builtins::builtin(Builtins::JSConstructStubApi)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 884 | |
kasper.lund | 212ac23 | 2008-07-16 07:07:30 +0000 | [diff] [blame] | 885 | int internal_field_count = 0; |
| 886 | if (!obj->instance_template()->IsUndefined()) { |
| 887 | Handle<ObjectTemplateInfo> instance_template = |
| 888 | Handle<ObjectTemplateInfo>( |
| 889 | ObjectTemplateInfo::cast(obj->instance_template())); |
| 890 | internal_field_count = |
| 891 | Smi::cast(instance_template->internal_field_count())->value(); |
| 892 | } |
| 893 | |
| 894 | int instance_size = kPointerSize * internal_field_count; |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 895 | InstanceType type = INVALID_TYPE; |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 896 | switch (instance_type) { |
| 897 | case JavaScriptObject: |
| 898 | type = JS_OBJECT_TYPE; |
| 899 | instance_size += JSObject::kHeaderSize; |
| 900 | break; |
| 901 | case InnerGlobalObject: |
| 902 | type = JS_GLOBAL_OBJECT_TYPE; |
| 903 | instance_size += JSGlobalObject::kSize; |
| 904 | break; |
| 905 | case OuterGlobalObject: |
| 906 | type = JS_GLOBAL_PROXY_TYPE; |
| 907 | instance_size += JSGlobalProxy::kSize; |
| 908 | break; |
| 909 | default: |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 910 | break; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 911 | } |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 912 | ASSERT(type != INVALID_TYPE); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 913 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 914 | Handle<JSFunction> result = |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 915 | Factory::NewFunction(Factory::empty_symbol(), |
| 916 | type, |
| 917 | instance_size, |
| 918 | code, |
| 919 | true); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 920 | // Set class name. |
| 921 | Handle<Object> class_name = Handle<Object>(obj->class_name()); |
| 922 | if (class_name->IsString()) { |
| 923 | result->shared()->set_instance_class_name(*class_name); |
| 924 | result->shared()->set_name(*class_name); |
| 925 | } |
| 926 | |
| 927 | Handle<Map> map = Handle<Map>(result->initial_map()); |
| 928 | |
| 929 | // Mark as undetectable if needed. |
| 930 | if (obj->undetectable()) { |
| 931 | map->set_is_undetectable(); |
| 932 | } |
| 933 | |
| 934 | // Mark as hidden for the __proto__ accessor if needed. |
| 935 | if (obj->hidden_prototype()) { |
| 936 | map->set_is_hidden_prototype(); |
| 937 | } |
| 938 | |
| 939 | // Mark as needs_access_check if needed. |
| 940 | if (obj->needs_access_check()) { |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 941 | map->set_is_access_check_needed(true); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 942 | } |
| 943 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 944 | // Set interceptor information in the map. |
| 945 | if (!obj->named_property_handler()->IsUndefined()) { |
| 946 | map->set_has_named_interceptor(); |
| 947 | } |
| 948 | if (!obj->indexed_property_handler()->IsUndefined()) { |
| 949 | map->set_has_indexed_interceptor(); |
| 950 | } |
| 951 | |
| 952 | // Set instance call-as-function information in the map. |
| 953 | if (!obj->instance_call_handler()->IsUndefined()) { |
| 954 | map->set_has_instance_call_handler(); |
| 955 | } |
| 956 | |
| 957 | result->shared()->set_function_data(*obj); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 958 | result->shared()->set_construct_stub(*construct_stub); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 959 | result->shared()->DontAdaptArguments(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 960 | |
| 961 | // Recursively copy parent templates' accessors, 'data' may be modified. |
| 962 | Handle<DescriptorArray> array = |
| 963 | Handle<DescriptorArray>(map->instance_descriptors()); |
| 964 | while (true) { |
| 965 | Handle<Object> props = Handle<Object>(obj->property_accessors()); |
| 966 | if (!props->IsUndefined()) { |
| 967 | array = Factory::CopyAppendCallbackDescriptors(array, props); |
| 968 | } |
| 969 | Handle<Object> parent = Handle<Object>(obj->parent_template()); |
| 970 | if (parent->IsUndefined()) break; |
| 971 | obj = Handle<FunctionTemplateInfo>::cast(parent); |
| 972 | } |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 973 | if (!array->IsEmpty()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 974 | map->set_instance_descriptors(*array); |
| 975 | } |
| 976 | |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 977 | ASSERT(result->shared()->IsApiFunction()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 978 | return result; |
| 979 | } |
| 980 | |
| 981 | |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 982 | Handle<MapCache> Factory::NewMapCache(int at_least_space_for) { |
| 983 | CALL_HEAP_FUNCTION(MapCache::Allocate(at_least_space_for), MapCache); |
| 984 | } |
| 985 | |
| 986 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 987 | MUST_USE_RESULT static MaybeObject* UpdateMapCacheWith(Context* context, |
| 988 | FixedArray* keys, |
| 989 | Map* map) { |
| 990 | Object* result; |
| 991 | { MaybeObject* maybe_result = |
| 992 | MapCache::cast(context->map_cache())->Put(keys, map); |
| 993 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 994 | } |
| 995 | context->set_map_cache(MapCache::cast(result)); |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 996 | return result; |
| 997 | } |
| 998 | |
| 999 | |
| 1000 | Handle<MapCache> Factory::AddToMapCache(Handle<Context> context, |
| 1001 | Handle<FixedArray> keys, |
| 1002 | Handle<Map> map) { |
| 1003 | CALL_HEAP_FUNCTION(UpdateMapCacheWith(*context, *keys, *map), MapCache); |
| 1004 | } |
| 1005 | |
| 1006 | |
| 1007 | Handle<Map> Factory::ObjectLiteralMapFromCache(Handle<Context> context, |
| 1008 | Handle<FixedArray> keys) { |
| 1009 | if (context->map_cache()->IsUndefined()) { |
| 1010 | // Allocate the new map cache for the global context. |
| 1011 | Handle<MapCache> new_cache = NewMapCache(24); |
| 1012 | context->set_map_cache(*new_cache); |
| 1013 | } |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 1014 | // Check to see whether there is a matching element in the cache. |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 1015 | Handle<MapCache> cache = |
| 1016 | Handle<MapCache>(MapCache::cast(context->map_cache())); |
| 1017 | Handle<Object> result = Handle<Object>(cache->Lookup(*keys)); |
| 1018 | if (result->IsMap()) return Handle<Map>::cast(result); |
| 1019 | // Create a new map and add it to the cache. |
| 1020 | Handle<Map> map = |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 1021 | CopyMap(Handle<Map>(context->object_function()->initial_map()), |
| 1022 | keys->length()); |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 1023 | AddToMapCache(context, keys, map); |
| 1024 | return Handle<Map>(map); |
| 1025 | } |
| 1026 | |
| 1027 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1028 | void Factory::SetRegExpAtomData(Handle<JSRegExp> regexp, |
| 1029 | JSRegExp::Type type, |
| 1030 | Handle<String> source, |
| 1031 | JSRegExp::Flags flags, |
| 1032 | Handle<Object> data) { |
| 1033 | Handle<FixedArray> store = NewFixedArray(JSRegExp::kAtomDataSize); |
| 1034 | |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 1035 | store->set(JSRegExp::kTagIndex, Smi::FromInt(type)); |
| 1036 | store->set(JSRegExp::kSourceIndex, *source); |
| 1037 | store->set(JSRegExp::kFlagsIndex, Smi::FromInt(flags.value())); |
| 1038 | store->set(JSRegExp::kAtomPatternIndex, *data); |
| 1039 | regexp->set_data(*store); |
| 1040 | } |
| 1041 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1042 | void Factory::SetRegExpIrregexpData(Handle<JSRegExp> regexp, |
| 1043 | JSRegExp::Type type, |
| 1044 | Handle<String> source, |
| 1045 | JSRegExp::Flags flags, |
| 1046 | int capture_count) { |
| 1047 | Handle<FixedArray> store = NewFixedArray(JSRegExp::kIrregexpDataSize); |
| 1048 | |
| 1049 | store->set(JSRegExp::kTagIndex, Smi::FromInt(type)); |
| 1050 | store->set(JSRegExp::kSourceIndex, *source); |
| 1051 | store->set(JSRegExp::kFlagsIndex, Smi::FromInt(flags.value())); |
| 1052 | store->set(JSRegExp::kIrregexpASCIICodeIndex, Heap::the_hole_value()); |
| 1053 | store->set(JSRegExp::kIrregexpUC16CodeIndex, Heap::the_hole_value()); |
| 1054 | store->set(JSRegExp::kIrregexpMaxRegisterCountIndex, Smi::FromInt(0)); |
| 1055 | store->set(JSRegExp::kIrregexpCaptureCountIndex, |
| 1056 | Smi::FromInt(capture_count)); |
| 1057 | regexp->set_data(*store); |
| 1058 | } |
| 1059 | |
| 1060 | |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 1061 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1062 | void Factory::ConfigureInstance(Handle<FunctionTemplateInfo> desc, |
| 1063 | Handle<JSObject> instance, |
| 1064 | bool* pending_exception) { |
| 1065 | // Configure the instance by adding the properties specified by the |
| 1066 | // instance template. |
| 1067 | Handle<Object> instance_template = Handle<Object>(desc->instance_template()); |
| 1068 | if (!instance_template->IsUndefined()) { |
| 1069 | Execution::ConfigureInstance(instance, |
| 1070 | instance_template, |
| 1071 | pending_exception); |
| 1072 | } else { |
| 1073 | *pending_exception = false; |
| 1074 | } |
| 1075 | } |
| 1076 | |
| 1077 | |
| 1078 | } } // namespace v8::internal |