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 | |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 253 | Handle<PixelArray> Factory::NewPixelArray(int length, |
| 254 | uint8_t* external_pointer, |
| 255 | PretenureFlag pretenure) { |
| 256 | ASSERT(0 <= length); |
| 257 | CALL_HEAP_FUNCTION(Heap::AllocatePixelArray(length, |
| 258 | external_pointer, |
| 259 | pretenure), PixelArray); |
| 260 | } |
| 261 | |
| 262 | |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 263 | Handle<ExternalArray> Factory::NewExternalArray(int length, |
| 264 | ExternalArrayType array_type, |
| 265 | void* external_pointer, |
| 266 | PretenureFlag pretenure) { |
| 267 | ASSERT(0 <= length); |
| 268 | CALL_HEAP_FUNCTION(Heap::AllocateExternalArray(length, |
| 269 | array_type, |
| 270 | external_pointer, |
| 271 | pretenure), ExternalArray); |
| 272 | } |
| 273 | |
| 274 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 275 | Handle<JSGlobalPropertyCell> Factory::NewJSGlobalPropertyCell( |
| 276 | Handle<Object> value) { |
| 277 | CALL_HEAP_FUNCTION(Heap::AllocateJSGlobalPropertyCell(*value), |
| 278 | JSGlobalPropertyCell); |
| 279 | } |
| 280 | |
| 281 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 282 | Handle<Map> Factory::NewMap(InstanceType type, int instance_size) { |
| 283 | CALL_HEAP_FUNCTION(Heap::AllocateMap(type, instance_size), Map); |
| 284 | } |
| 285 | |
| 286 | |
| 287 | Handle<JSObject> Factory::NewFunctionPrototype(Handle<JSFunction> function) { |
| 288 | CALL_HEAP_FUNCTION(Heap::AllocateFunctionPrototype(*function), JSObject); |
| 289 | } |
| 290 | |
| 291 | |
ager@chromium.org | 3a37e9b | 2009-04-27 09:26:21 +0000 | [diff] [blame] | 292 | Handle<Map> Factory::CopyMapDropDescriptors(Handle<Map> src) { |
| 293 | CALL_HEAP_FUNCTION(src->CopyDropDescriptors(), Map); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 297 | Handle<Map> Factory::CopyMap(Handle<Map> src, |
| 298 | int extra_inobject_properties) { |
ager@chromium.org | 3a37e9b | 2009-04-27 09:26:21 +0000 | [diff] [blame] | 299 | Handle<Map> copy = CopyMapDropDescriptors(src); |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 300 | // Check that we do not overflow the instance size when adding the |
| 301 | // extra inobject properties. |
| 302 | int instance_size_delta = extra_inobject_properties * kPointerSize; |
| 303 | int max_instance_size_delta = |
| 304 | JSObject::kMaxInstanceSize - copy->instance_size(); |
| 305 | if (instance_size_delta > max_instance_size_delta) { |
| 306 | // If the instance size overflows, we allocate as many properties |
| 307 | // as we can as inobject properties. |
| 308 | instance_size_delta = max_instance_size_delta; |
| 309 | extra_inobject_properties = max_instance_size_delta >> kPointerSizeLog2; |
| 310 | } |
| 311 | // Adjust the map with the extra inobject properties. |
| 312 | int inobject_properties = |
| 313 | copy->inobject_properties() + extra_inobject_properties; |
| 314 | copy->set_inobject_properties(inobject_properties); |
| 315 | copy->set_unused_property_fields(inobject_properties); |
| 316 | copy->set_instance_size(copy->instance_size() + instance_size_delta); |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 317 | copy->set_visitor_id(StaticVisitorBase::GetVisitorId(*copy)); |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 318 | return copy; |
| 319 | } |
| 320 | |
fschneider@chromium.org | 40b9da3 | 2010-06-28 11:29:21 +0000 | [diff] [blame] | 321 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 322 | Handle<Map> Factory::CopyMapDropTransitions(Handle<Map> src) { |
| 323 | CALL_HEAP_FUNCTION(src->CopyDropTransitions(), Map); |
| 324 | } |
| 325 | |
| 326 | |
fschneider@chromium.org | 40b9da3 | 2010-06-28 11:29:21 +0000 | [diff] [blame] | 327 | Handle<Map> Factory::GetFastElementsMap(Handle<Map> src) { |
| 328 | CALL_HEAP_FUNCTION(src->GetFastElementsMap(), Map); |
| 329 | } |
| 330 | |
| 331 | |
| 332 | Handle<Map> Factory::GetSlowElementsMap(Handle<Map> src) { |
| 333 | CALL_HEAP_FUNCTION(src->GetSlowElementsMap(), Map); |
| 334 | } |
| 335 | |
| 336 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 337 | Handle<FixedArray> Factory::CopyFixedArray(Handle<FixedArray> array) { |
| 338 | CALL_HEAP_FUNCTION(array->Copy(), FixedArray); |
| 339 | } |
| 340 | |
| 341 | |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 342 | Handle<JSFunction> Factory::BaseNewFunctionFromSharedFunctionInfo( |
| 343 | Handle<SharedFunctionInfo> function_info, |
sgjesse@chromium.org | 846fb74 | 2009-12-18 08:56:33 +0000 | [diff] [blame] | 344 | Handle<Map> function_map, |
| 345 | PretenureFlag pretenure) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 346 | CALL_HEAP_FUNCTION(Heap::AllocateFunction(*function_map, |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 347 | *function_info, |
sgjesse@chromium.org | 846fb74 | 2009-12-18 08:56:33 +0000 | [diff] [blame] | 348 | Heap::the_hole_value(), |
| 349 | pretenure), |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 350 | JSFunction); |
| 351 | } |
| 352 | |
| 353 | |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 354 | Handle<JSFunction> Factory::NewFunctionFromSharedFunctionInfo( |
| 355 | Handle<SharedFunctionInfo> function_info, |
sgjesse@chromium.org | 846fb74 | 2009-12-18 08:56:33 +0000 | [diff] [blame] | 356 | Handle<Context> context, |
| 357 | PretenureFlag pretenure) { |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 358 | Handle<JSFunction> result = BaseNewFunctionFromSharedFunctionInfo( |
| 359 | function_info, Top::function_map(), pretenure); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 360 | result->set_context(*context); |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 361 | int number_of_literals = function_info->num_literals(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 362 | Handle<FixedArray> literals = |
sgjesse@chromium.org | 846fb74 | 2009-12-18 08:56:33 +0000 | [diff] [blame] | 363 | Factory::NewFixedArray(number_of_literals, pretenure); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 364 | if (number_of_literals > 0) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 365 | // Store the object, regexp and array functions in the literals |
| 366 | // array prefix. These functions will be used when creating |
| 367 | // object, regexp and array literals in this function. |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 368 | literals->set(JSFunction::kLiteralGlobalContextIndex, |
| 369 | context->global_context()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 370 | } |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 371 | result->set_literals(*literals); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 372 | result->set_next_function_link(Heap::undefined_value()); |
| 373 | |
| 374 | if (V8::UseCrankshaft() && |
| 375 | FLAG_always_opt && |
| 376 | result->is_compiled() && |
| 377 | !function_info->is_toplevel() && |
| 378 | function_info->allows_lazy_compilation()) { |
| 379 | result->MarkForLazyRecompilation(); |
| 380 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 381 | return result; |
| 382 | } |
| 383 | |
| 384 | |
| 385 | Handle<Object> Factory::NewNumber(double value, |
| 386 | PretenureFlag pretenure) { |
| 387 | CALL_HEAP_FUNCTION(Heap::NumberFromDouble(value, pretenure), Object); |
| 388 | } |
| 389 | |
| 390 | |
| 391 | Handle<Object> Factory::NewNumberFromInt(int value) { |
| 392 | CALL_HEAP_FUNCTION(Heap::NumberFromInt32(value), Object); |
| 393 | } |
| 394 | |
| 395 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 396 | Handle<Object> Factory::NewNumberFromUint(uint32_t value) { |
| 397 | CALL_HEAP_FUNCTION(Heap::NumberFromUint32(value), Object); |
| 398 | } |
| 399 | |
| 400 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 401 | Handle<JSObject> Factory::NewNeanderObject() { |
| 402 | CALL_HEAP_FUNCTION(Heap::AllocateJSObjectFromMap(Heap::neander_map()), |
| 403 | JSObject); |
| 404 | } |
| 405 | |
| 406 | |
| 407 | Handle<Object> Factory::NewTypeError(const char* type, |
| 408 | Vector< Handle<Object> > args) { |
| 409 | return NewError("MakeTypeError", type, args); |
| 410 | } |
| 411 | |
| 412 | |
| 413 | Handle<Object> Factory::NewTypeError(Handle<String> message) { |
| 414 | return NewError("$TypeError", message); |
| 415 | } |
| 416 | |
| 417 | |
| 418 | Handle<Object> Factory::NewRangeError(const char* type, |
| 419 | Vector< Handle<Object> > args) { |
| 420 | return NewError("MakeRangeError", type, args); |
| 421 | } |
| 422 | |
| 423 | |
| 424 | Handle<Object> Factory::NewRangeError(Handle<String> message) { |
| 425 | return NewError("$RangeError", message); |
| 426 | } |
| 427 | |
| 428 | |
| 429 | Handle<Object> Factory::NewSyntaxError(const char* type, Handle<JSArray> args) { |
| 430 | return NewError("MakeSyntaxError", type, args); |
| 431 | } |
| 432 | |
| 433 | |
| 434 | Handle<Object> Factory::NewSyntaxError(Handle<String> message) { |
| 435 | return NewError("$SyntaxError", message); |
| 436 | } |
| 437 | |
| 438 | |
| 439 | Handle<Object> Factory::NewReferenceError(const char* type, |
| 440 | Vector< Handle<Object> > args) { |
| 441 | return NewError("MakeReferenceError", type, args); |
| 442 | } |
| 443 | |
| 444 | |
| 445 | Handle<Object> Factory::NewReferenceError(Handle<String> message) { |
| 446 | return NewError("$ReferenceError", message); |
| 447 | } |
| 448 | |
| 449 | |
| 450 | Handle<Object> Factory::NewError(const char* maker, const char* type, |
| 451 | Vector< Handle<Object> > args) { |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 452 | v8::HandleScope scope; // Instantiate a closeable HandleScope for EscapeFrom. |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 453 | Handle<FixedArray> array = Factory::NewFixedArray(args.length()); |
| 454 | for (int i = 0; i < args.length(); i++) { |
| 455 | array->set(i, *args[i]); |
| 456 | } |
| 457 | Handle<JSArray> object = Factory::NewJSArrayWithElements(array); |
| 458 | Handle<Object> result = NewError(maker, type, object); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 459 | return result.EscapeFrom(&scope); |
| 460 | } |
| 461 | |
| 462 | |
| 463 | Handle<Object> Factory::NewEvalError(const char* type, |
| 464 | Vector< Handle<Object> > args) { |
| 465 | return NewError("MakeEvalError", type, args); |
| 466 | } |
| 467 | |
| 468 | |
| 469 | Handle<Object> Factory::NewError(const char* type, |
| 470 | Vector< Handle<Object> > args) { |
| 471 | return NewError("MakeError", type, args); |
| 472 | } |
| 473 | |
| 474 | |
| 475 | Handle<Object> Factory::NewError(const char* maker, |
| 476 | const char* type, |
| 477 | Handle<JSArray> args) { |
| 478 | Handle<String> make_str = Factory::LookupAsciiSymbol(maker); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 479 | Handle<Object> fun_obj(Top::builtins()->GetPropertyNoExceptionThrown( |
| 480 | *make_str)); |
ager@chromium.org | 4af710e | 2009-09-15 12:20:11 +0000 | [diff] [blame] | 481 | // If the builtins haven't been properly configured yet this error |
| 482 | // constructor may not have been defined. Bail out. |
| 483 | if (!fun_obj->IsJSFunction()) |
| 484 | return Factory::undefined_value(); |
| 485 | Handle<JSFunction> fun = Handle<JSFunction>::cast(fun_obj); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 486 | Handle<Object> type_obj = Factory::LookupAsciiSymbol(type); |
| 487 | Object** argv[2] = { type_obj.location(), |
| 488 | Handle<Object>::cast(args).location() }; |
| 489 | |
| 490 | // Invoke the JavaScript factory method. If an exception is thrown while |
| 491 | // running the factory method, use the exception as the result. |
| 492 | bool caught_exception; |
| 493 | Handle<Object> result = Execution::TryCall(fun, |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 494 | Top::builtins(), |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 495 | 2, |
| 496 | argv, |
| 497 | &caught_exception); |
| 498 | return result; |
| 499 | } |
| 500 | |
| 501 | |
| 502 | Handle<Object> Factory::NewError(Handle<String> message) { |
| 503 | return NewError("$Error", message); |
| 504 | } |
| 505 | |
| 506 | |
| 507 | Handle<Object> Factory::NewError(const char* constructor, |
| 508 | Handle<String> message) { |
| 509 | Handle<String> constr = Factory::LookupAsciiSymbol(constructor); |
| 510 | Handle<JSFunction> fun = |
| 511 | Handle<JSFunction>( |
| 512 | JSFunction::cast( |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 513 | Top::builtins()->GetPropertyNoExceptionThrown(*constr))); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 514 | Object** argv[1] = { Handle<Object>::cast(message).location() }; |
| 515 | |
| 516 | // Invoke the JavaScript factory method. If an exception is thrown while |
| 517 | // running the factory method, use the exception as the result. |
| 518 | bool caught_exception; |
| 519 | Handle<Object> result = Execution::TryCall(fun, |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 520 | Top::builtins(), |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 521 | 1, |
| 522 | argv, |
| 523 | &caught_exception); |
| 524 | return result; |
| 525 | } |
| 526 | |
| 527 | |
| 528 | Handle<JSFunction> Factory::NewFunction(Handle<String> name, |
| 529 | InstanceType type, |
| 530 | int instance_size, |
| 531 | Handle<Code> code, |
| 532 | bool force_initial_map) { |
| 533 | // Allocate the function |
| 534 | Handle<JSFunction> function = NewFunction(name, the_hole_value()); |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 535 | |
| 536 | // Setup the code pointer in both the shared function info and in |
| 537 | // the function itself. |
| 538 | function->shared()->set_code(*code); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 539 | function->set_code(*code); |
| 540 | |
| 541 | if (force_initial_map || |
| 542 | type != JS_OBJECT_TYPE || |
| 543 | instance_size != JSObject::kHeaderSize) { |
| 544 | Handle<Map> initial_map = NewMap(type, instance_size); |
| 545 | Handle<JSObject> prototype = NewFunctionPrototype(function); |
| 546 | initial_map->set_prototype(*prototype); |
| 547 | function->set_initial_map(*initial_map); |
| 548 | initial_map->set_constructor(*function); |
| 549 | } else { |
| 550 | ASSERT(!function->has_initial_map()); |
| 551 | ASSERT(!function->has_prototype()); |
| 552 | } |
| 553 | |
| 554 | return function; |
| 555 | } |
| 556 | |
| 557 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 558 | Handle<JSFunction> Factory::NewFunctionWithPrototype(Handle<String> name, |
| 559 | InstanceType type, |
| 560 | int instance_size, |
| 561 | Handle<JSObject> prototype, |
| 562 | Handle<Code> code, |
| 563 | bool force_initial_map) { |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 564 | // Allocate the function. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 565 | Handle<JSFunction> function = NewFunction(name, prototype); |
| 566 | |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 567 | // Setup the code pointer in both the shared function info and in |
| 568 | // the function itself. |
| 569 | function->shared()->set_code(*code); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 570 | function->set_code(*code); |
| 571 | |
| 572 | if (force_initial_map || |
| 573 | type != JS_OBJECT_TYPE || |
| 574 | instance_size != JSObject::kHeaderSize) { |
| 575 | Handle<Map> initial_map = NewMap(type, instance_size); |
| 576 | function->set_initial_map(*initial_map); |
| 577 | initial_map->set_constructor(*function); |
| 578 | } |
| 579 | |
| 580 | // Set function.prototype and give the prototype a constructor |
| 581 | // property that refers to the function. |
| 582 | SetPrototypeProperty(function, prototype); |
| 583 | SetProperty(prototype, Factory::constructor_symbol(), function, DONT_ENUM); |
| 584 | return function; |
| 585 | } |
| 586 | |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 587 | |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 588 | Handle<JSFunction> Factory::NewFunctionWithoutPrototype(Handle<String> name, |
| 589 | Handle<Code> code) { |
| 590 | Handle<JSFunction> function = NewFunctionWithoutPrototype(name); |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 591 | function->shared()->set_code(*code); |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 592 | function->set_code(*code); |
| 593 | ASSERT(!function->has_initial_map()); |
| 594 | ASSERT(!function->has_prototype()); |
| 595 | return function; |
| 596 | } |
| 597 | |
| 598 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 599 | Handle<Code> Factory::NewCode(const CodeDesc& desc, |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 600 | Code::Flags flags, |
| 601 | Handle<Object> self_ref) { |
ager@chromium.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 602 | CALL_HEAP_FUNCTION(Heap::CreateCode(desc, flags, self_ref), Code); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 603 | } |
| 604 | |
| 605 | |
| 606 | Handle<Code> Factory::CopyCode(Handle<Code> code) { |
| 607 | CALL_HEAP_FUNCTION(Heap::CopyCode(*code), Code); |
| 608 | } |
| 609 | |
| 610 | |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 611 | Handle<Code> Factory::CopyCode(Handle<Code> code, Vector<byte> reloc_info) { |
| 612 | CALL_HEAP_FUNCTION(Heap::CopyCode(*code, reloc_info), Code); |
| 613 | } |
| 614 | |
| 615 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 616 | MUST_USE_RESULT static inline MaybeObject* DoCopyInsert( |
| 617 | DescriptorArray* array, |
| 618 | String* key, |
| 619 | Object* value, |
| 620 | PropertyAttributes attributes) { |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 621 | CallbacksDescriptor desc(key, value, attributes); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 622 | MaybeObject* obj = array->CopyInsert(&desc, REMOVE_TRANSITIONS); |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 623 | return obj; |
| 624 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 625 | |
| 626 | |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 627 | // Allocate the new array. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 628 | Handle<DescriptorArray> Factory::CopyAppendProxyDescriptor( |
| 629 | Handle<DescriptorArray> array, |
| 630 | Handle<String> key, |
| 631 | Handle<Object> value, |
| 632 | PropertyAttributes attributes) { |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 633 | CALL_HEAP_FUNCTION(DoCopyInsert(*array, *key, *value, attributes), |
| 634 | DescriptorArray); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 635 | } |
| 636 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 637 | |
| 638 | Handle<String> Factory::SymbolFromString(Handle<String> value) { |
| 639 | CALL_HEAP_FUNCTION(Heap::LookupSymbol(*value), String); |
| 640 | } |
| 641 | |
| 642 | |
| 643 | Handle<DescriptorArray> Factory::CopyAppendCallbackDescriptors( |
| 644 | Handle<DescriptorArray> array, |
| 645 | Handle<Object> descriptors) { |
| 646 | v8::NeanderArray callbacks(descriptors); |
| 647 | int nof_callbacks = callbacks.length(); |
| 648 | Handle<DescriptorArray> result = |
| 649 | NewDescriptorArray(array->number_of_descriptors() + nof_callbacks); |
| 650 | |
| 651 | // Number of descriptors added to the result so far. |
| 652 | int descriptor_count = 0; |
| 653 | |
| 654 | // Copy the descriptors from the array. |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 655 | for (int i = 0; i < array->number_of_descriptors(); i++) { |
| 656 | if (array->GetType(i) != NULL_DESCRIPTOR) { |
| 657 | result->CopyFrom(descriptor_count++, *array, i); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 658 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 659 | } |
| 660 | |
| 661 | // Number of duplicates detected. |
| 662 | int duplicates = 0; |
| 663 | |
| 664 | // Fill in new callback descriptors. Process the callbacks from |
| 665 | // back to front so that the last callback with a given name takes |
| 666 | // precedence over previously added callbacks with that name. |
| 667 | for (int i = nof_callbacks - 1; i >= 0; i--) { |
| 668 | Handle<AccessorInfo> entry = |
| 669 | Handle<AccessorInfo>(AccessorInfo::cast(callbacks.get(i))); |
| 670 | // Ensure the key is a symbol before writing into the instance descriptor. |
| 671 | Handle<String> key = |
| 672 | SymbolFromString(Handle<String>(String::cast(entry->name()))); |
| 673 | // Check if a descriptor with this name already exists before writing. |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 674 | if (result->LinearSearch(*key, descriptor_count) == |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 675 | DescriptorArray::kNotFound) { |
| 676 | CallbacksDescriptor desc(*key, *entry, entry->property_attributes()); |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 677 | result->Set(descriptor_count, &desc); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 678 | descriptor_count++; |
| 679 | } else { |
| 680 | duplicates++; |
| 681 | } |
| 682 | } |
| 683 | |
| 684 | // If duplicates were detected, allocate a result of the right size |
| 685 | // and transfer the elements. |
| 686 | if (duplicates > 0) { |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 687 | int number_of_descriptors = result->number_of_descriptors() - duplicates; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 688 | Handle<DescriptorArray> new_result = |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 689 | NewDescriptorArray(number_of_descriptors); |
| 690 | for (int i = 0; i < number_of_descriptors; i++) { |
| 691 | new_result->CopyFrom(i, *result, i); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 692 | } |
| 693 | result = new_result; |
| 694 | } |
| 695 | |
| 696 | // Sort the result before returning. |
| 697 | result->Sort(); |
| 698 | return result; |
| 699 | } |
| 700 | |
| 701 | |
| 702 | Handle<JSObject> Factory::NewJSObject(Handle<JSFunction> constructor, |
| 703 | PretenureFlag pretenure) { |
| 704 | CALL_HEAP_FUNCTION(Heap::AllocateJSObject(*constructor, pretenure), JSObject); |
| 705 | } |
| 706 | |
| 707 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 708 | Handle<GlobalObject> Factory::NewGlobalObject( |
| 709 | Handle<JSFunction> constructor) { |
| 710 | CALL_HEAP_FUNCTION(Heap::AllocateGlobalObject(*constructor), |
| 711 | GlobalObject); |
| 712 | } |
| 713 | |
| 714 | |
| 715 | |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 716 | Handle<JSObject> Factory::NewJSObjectFromMap(Handle<Map> map) { |
| 717 | CALL_HEAP_FUNCTION(Heap::AllocateJSObjectFromMap(*map, NOT_TENURED), |
| 718 | JSObject); |
| 719 | } |
| 720 | |
| 721 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 722 | Handle<JSArray> Factory::NewJSArray(int length, |
| 723 | PretenureFlag pretenure) { |
| 724 | Handle<JSObject> obj = NewJSObject(Top::array_function(), pretenure); |
| 725 | CALL_HEAP_FUNCTION(Handle<JSArray>::cast(obj)->Initialize(length), JSArray); |
| 726 | } |
| 727 | |
| 728 | |
| 729 | Handle<JSArray> Factory::NewJSArrayWithElements(Handle<FixedArray> elements, |
| 730 | PretenureFlag pretenure) { |
| 731 | Handle<JSArray> result = |
| 732 | Handle<JSArray>::cast(NewJSObject(Top::array_function(), pretenure)); |
| 733 | result->SetContent(*elements); |
| 734 | return result; |
| 735 | } |
| 736 | |
| 737 | |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 738 | Handle<SharedFunctionInfo> Factory::NewSharedFunctionInfo( |
ager@chromium.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 739 | Handle<String> name, |
| 740 | int number_of_literals, |
| 741 | Handle<Code> code, |
ager@chromium.org | b573749 | 2010-07-15 09:29:43 +0000 | [diff] [blame] | 742 | Handle<SerializedScopeInfo> scope_info) { |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 743 | Handle<SharedFunctionInfo> shared = NewSharedFunctionInfo(name); |
| 744 | shared->set_code(*code); |
ager@chromium.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 745 | shared->set_scope_info(*scope_info); |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 746 | int literals_array_size = number_of_literals; |
| 747 | // If the function contains object, regexp or array literals, |
| 748 | // allocate extra space for a literals array prefix containing the |
| 749 | // context. |
| 750 | if (number_of_literals > 0) { |
| 751 | literals_array_size += JSFunction::kLiteralsPrefixSize; |
| 752 | } |
| 753 | shared->set_num_literals(literals_array_size); |
| 754 | return shared; |
| 755 | } |
| 756 | |
| 757 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 758 | Handle<SharedFunctionInfo> Factory::NewSharedFunctionInfo(Handle<String> name) { |
| 759 | CALL_HEAP_FUNCTION(Heap::AllocateSharedFunctionInfo(*name), |
| 760 | SharedFunctionInfo); |
| 761 | } |
| 762 | |
| 763 | |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 764 | Handle<String> Factory::NumberToString(Handle<Object> number) { |
| 765 | CALL_HEAP_FUNCTION(Heap::NumberToString(*number), String); |
| 766 | } |
| 767 | |
| 768 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 769 | Handle<NumberDictionary> Factory::DictionaryAtNumberPut( |
| 770 | Handle<NumberDictionary> dictionary, |
| 771 | uint32_t key, |
| 772 | Handle<Object> value) { |
| 773 | CALL_HEAP_FUNCTION(dictionary->AtNumberPut(key, *value), NumberDictionary); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 774 | } |
| 775 | |
| 776 | |
| 777 | Handle<JSFunction> Factory::NewFunctionHelper(Handle<String> name, |
| 778 | Handle<Object> prototype) { |
| 779 | Handle<SharedFunctionInfo> function_share = NewSharedFunctionInfo(name); |
| 780 | CALL_HEAP_FUNCTION(Heap::AllocateFunction(*Top::function_map(), |
| 781 | *function_share, |
| 782 | *prototype), |
| 783 | JSFunction); |
| 784 | } |
| 785 | |
| 786 | |
| 787 | Handle<JSFunction> Factory::NewFunction(Handle<String> name, |
| 788 | Handle<Object> prototype) { |
| 789 | Handle<JSFunction> fun = NewFunctionHelper(name, prototype); |
| 790 | fun->set_context(Top::context()->global_context()); |
| 791 | return fun; |
| 792 | } |
| 793 | |
| 794 | |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 795 | Handle<JSFunction> Factory::NewFunctionWithoutPrototypeHelper( |
| 796 | Handle<String> name) { |
| 797 | Handle<SharedFunctionInfo> function_share = NewSharedFunctionInfo(name); |
| 798 | CALL_HEAP_FUNCTION(Heap::AllocateFunction( |
| 799 | *Top::function_without_prototype_map(), |
| 800 | *function_share, |
| 801 | *the_hole_value()), |
| 802 | JSFunction); |
| 803 | } |
| 804 | |
| 805 | |
| 806 | Handle<JSFunction> Factory::NewFunctionWithoutPrototype(Handle<String> name) { |
| 807 | Handle<JSFunction> fun = NewFunctionWithoutPrototypeHelper(name); |
| 808 | fun->set_context(Top::context()->global_context()); |
| 809 | return fun; |
| 810 | } |
| 811 | |
| 812 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 813 | Handle<Object> Factory::ToObject(Handle<Object> object) { |
| 814 | CALL_HEAP_FUNCTION(object->ToObject(), Object); |
| 815 | } |
| 816 | |
| 817 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 818 | Handle<Object> Factory::ToObject(Handle<Object> object, |
| 819 | Handle<Context> global_context) { |
| 820 | CALL_HEAP_FUNCTION(object->ToObject(*global_context), Object); |
| 821 | } |
| 822 | |
| 823 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 824 | #ifdef ENABLE_DEBUGGER_SUPPORT |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 825 | Handle<DebugInfo> Factory::NewDebugInfo(Handle<SharedFunctionInfo> shared) { |
| 826 | // Get the original code of the function. |
| 827 | Handle<Code> code(shared->code()); |
| 828 | |
| 829 | // Create a copy of the code before allocating the debug info object to avoid |
| 830 | // allocation while setting up the debug info object. |
| 831 | Handle<Code> original_code(*Factory::CopyCode(code)); |
| 832 | |
| 833 | // Allocate initial fixed array for active break points before allocating the |
| 834 | // debug info object to avoid allocation while setting up the debug info |
| 835 | // object. |
| 836 | Handle<FixedArray> break_points( |
| 837 | Factory::NewFixedArray(Debug::kEstimatedNofBreakPointsInFunction)); |
| 838 | |
| 839 | // Create and set up the debug info object. Debug info contains function, a |
| 840 | // copy of the original code, the executing code and initial fixed array for |
| 841 | // active break points. |
| 842 | Handle<DebugInfo> debug_info = |
| 843 | Handle<DebugInfo>::cast(Factory::NewStruct(DEBUG_INFO_TYPE)); |
| 844 | debug_info->set_shared(*shared); |
| 845 | debug_info->set_original_code(*original_code); |
| 846 | debug_info->set_code(*code); |
| 847 | debug_info->set_break_points(*break_points); |
| 848 | |
| 849 | // Link debug info to function. |
| 850 | shared->set_debug_info(*debug_info); |
| 851 | |
| 852 | return debug_info; |
| 853 | } |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 854 | #endif |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 855 | |
| 856 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 857 | Handle<JSObject> Factory::NewArgumentsObject(Handle<Object> callee, |
| 858 | int length) { |
| 859 | CALL_HEAP_FUNCTION(Heap::AllocateArgumentsObject(*callee, length), JSObject); |
| 860 | } |
| 861 | |
| 862 | |
| 863 | Handle<JSFunction> Factory::CreateApiFunction( |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 864 | Handle<FunctionTemplateInfo> obj, ApiInstanceType instance_type) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 865 | Handle<Code> code = Handle<Code>(Builtins::builtin(Builtins::HandleApiCall)); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 866 | Handle<Code> construct_stub = |
| 867 | Handle<Code>(Builtins::builtin(Builtins::JSConstructStubApi)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 868 | |
kasper.lund | 212ac23 | 2008-07-16 07:07:30 +0000 | [diff] [blame] | 869 | int internal_field_count = 0; |
| 870 | if (!obj->instance_template()->IsUndefined()) { |
| 871 | Handle<ObjectTemplateInfo> instance_template = |
| 872 | Handle<ObjectTemplateInfo>( |
| 873 | ObjectTemplateInfo::cast(obj->instance_template())); |
| 874 | internal_field_count = |
| 875 | Smi::cast(instance_template->internal_field_count())->value(); |
| 876 | } |
| 877 | |
| 878 | int instance_size = kPointerSize * internal_field_count; |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 879 | InstanceType type = INVALID_TYPE; |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 880 | switch (instance_type) { |
| 881 | case JavaScriptObject: |
| 882 | type = JS_OBJECT_TYPE; |
| 883 | instance_size += JSObject::kHeaderSize; |
| 884 | break; |
| 885 | case InnerGlobalObject: |
| 886 | type = JS_GLOBAL_OBJECT_TYPE; |
| 887 | instance_size += JSGlobalObject::kSize; |
| 888 | break; |
| 889 | case OuterGlobalObject: |
| 890 | type = JS_GLOBAL_PROXY_TYPE; |
| 891 | instance_size += JSGlobalProxy::kSize; |
| 892 | break; |
| 893 | default: |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 894 | break; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 895 | } |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 896 | ASSERT(type != INVALID_TYPE); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 897 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 898 | Handle<JSFunction> result = |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 899 | Factory::NewFunction(Factory::empty_symbol(), |
| 900 | type, |
| 901 | instance_size, |
| 902 | code, |
| 903 | true); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 904 | // Set class name. |
| 905 | Handle<Object> class_name = Handle<Object>(obj->class_name()); |
| 906 | if (class_name->IsString()) { |
| 907 | result->shared()->set_instance_class_name(*class_name); |
| 908 | result->shared()->set_name(*class_name); |
| 909 | } |
| 910 | |
| 911 | Handle<Map> map = Handle<Map>(result->initial_map()); |
| 912 | |
| 913 | // Mark as undetectable if needed. |
| 914 | if (obj->undetectable()) { |
| 915 | map->set_is_undetectable(); |
| 916 | } |
| 917 | |
| 918 | // Mark as hidden for the __proto__ accessor if needed. |
| 919 | if (obj->hidden_prototype()) { |
| 920 | map->set_is_hidden_prototype(); |
| 921 | } |
| 922 | |
| 923 | // Mark as needs_access_check if needed. |
| 924 | if (obj->needs_access_check()) { |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 925 | map->set_is_access_check_needed(true); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 926 | } |
| 927 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 928 | // Set interceptor information in the map. |
| 929 | if (!obj->named_property_handler()->IsUndefined()) { |
| 930 | map->set_has_named_interceptor(); |
| 931 | } |
| 932 | if (!obj->indexed_property_handler()->IsUndefined()) { |
| 933 | map->set_has_indexed_interceptor(); |
| 934 | } |
| 935 | |
| 936 | // Set instance call-as-function information in the map. |
| 937 | if (!obj->instance_call_handler()->IsUndefined()) { |
| 938 | map->set_has_instance_call_handler(); |
| 939 | } |
| 940 | |
| 941 | result->shared()->set_function_data(*obj); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 942 | result->shared()->set_construct_stub(*construct_stub); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 943 | result->shared()->DontAdaptArguments(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 944 | |
| 945 | // Recursively copy parent templates' accessors, 'data' may be modified. |
| 946 | Handle<DescriptorArray> array = |
| 947 | Handle<DescriptorArray>(map->instance_descriptors()); |
| 948 | while (true) { |
| 949 | Handle<Object> props = Handle<Object>(obj->property_accessors()); |
| 950 | if (!props->IsUndefined()) { |
| 951 | array = Factory::CopyAppendCallbackDescriptors(array, props); |
| 952 | } |
| 953 | Handle<Object> parent = Handle<Object>(obj->parent_template()); |
| 954 | if (parent->IsUndefined()) break; |
| 955 | obj = Handle<FunctionTemplateInfo>::cast(parent); |
| 956 | } |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 957 | if (!array->IsEmpty()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 958 | map->set_instance_descriptors(*array); |
| 959 | } |
| 960 | |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 961 | ASSERT(result->shared()->IsApiFunction()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 962 | return result; |
| 963 | } |
| 964 | |
| 965 | |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 966 | Handle<MapCache> Factory::NewMapCache(int at_least_space_for) { |
| 967 | CALL_HEAP_FUNCTION(MapCache::Allocate(at_least_space_for), MapCache); |
| 968 | } |
| 969 | |
| 970 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 971 | MUST_USE_RESULT static MaybeObject* UpdateMapCacheWith(Context* context, |
| 972 | FixedArray* keys, |
| 973 | Map* map) { |
| 974 | Object* result; |
| 975 | { MaybeObject* maybe_result = |
| 976 | MapCache::cast(context->map_cache())->Put(keys, map); |
| 977 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 978 | } |
| 979 | context->set_map_cache(MapCache::cast(result)); |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 980 | return result; |
| 981 | } |
| 982 | |
| 983 | |
| 984 | Handle<MapCache> Factory::AddToMapCache(Handle<Context> context, |
| 985 | Handle<FixedArray> keys, |
| 986 | Handle<Map> map) { |
| 987 | CALL_HEAP_FUNCTION(UpdateMapCacheWith(*context, *keys, *map), MapCache); |
| 988 | } |
| 989 | |
| 990 | |
| 991 | Handle<Map> Factory::ObjectLiteralMapFromCache(Handle<Context> context, |
| 992 | Handle<FixedArray> keys) { |
| 993 | if (context->map_cache()->IsUndefined()) { |
| 994 | // Allocate the new map cache for the global context. |
| 995 | Handle<MapCache> new_cache = NewMapCache(24); |
| 996 | context->set_map_cache(*new_cache); |
| 997 | } |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 998 | // 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] | 999 | Handle<MapCache> cache = |
| 1000 | Handle<MapCache>(MapCache::cast(context->map_cache())); |
| 1001 | Handle<Object> result = Handle<Object>(cache->Lookup(*keys)); |
| 1002 | if (result->IsMap()) return Handle<Map>::cast(result); |
| 1003 | // Create a new map and add it to the cache. |
| 1004 | Handle<Map> map = |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 1005 | CopyMap(Handle<Map>(context->object_function()->initial_map()), |
| 1006 | keys->length()); |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 1007 | AddToMapCache(context, keys, map); |
| 1008 | return Handle<Map>(map); |
| 1009 | } |
| 1010 | |
| 1011 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1012 | void Factory::SetRegExpAtomData(Handle<JSRegExp> regexp, |
| 1013 | JSRegExp::Type type, |
| 1014 | Handle<String> source, |
| 1015 | JSRegExp::Flags flags, |
| 1016 | Handle<Object> data) { |
| 1017 | Handle<FixedArray> store = NewFixedArray(JSRegExp::kAtomDataSize); |
| 1018 | |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 1019 | store->set(JSRegExp::kTagIndex, Smi::FromInt(type)); |
| 1020 | store->set(JSRegExp::kSourceIndex, *source); |
| 1021 | store->set(JSRegExp::kFlagsIndex, Smi::FromInt(flags.value())); |
| 1022 | store->set(JSRegExp::kAtomPatternIndex, *data); |
| 1023 | regexp->set_data(*store); |
| 1024 | } |
| 1025 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1026 | void Factory::SetRegExpIrregexpData(Handle<JSRegExp> regexp, |
| 1027 | JSRegExp::Type type, |
| 1028 | Handle<String> source, |
| 1029 | JSRegExp::Flags flags, |
| 1030 | int capture_count) { |
| 1031 | Handle<FixedArray> store = NewFixedArray(JSRegExp::kIrregexpDataSize); |
| 1032 | |
| 1033 | store->set(JSRegExp::kTagIndex, Smi::FromInt(type)); |
| 1034 | store->set(JSRegExp::kSourceIndex, *source); |
| 1035 | store->set(JSRegExp::kFlagsIndex, Smi::FromInt(flags.value())); |
| 1036 | store->set(JSRegExp::kIrregexpASCIICodeIndex, Heap::the_hole_value()); |
| 1037 | store->set(JSRegExp::kIrregexpUC16CodeIndex, Heap::the_hole_value()); |
| 1038 | store->set(JSRegExp::kIrregexpMaxRegisterCountIndex, Smi::FromInt(0)); |
| 1039 | store->set(JSRegExp::kIrregexpCaptureCountIndex, |
| 1040 | Smi::FromInt(capture_count)); |
| 1041 | regexp->set_data(*store); |
| 1042 | } |
| 1043 | |
| 1044 | |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 1045 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1046 | void Factory::ConfigureInstance(Handle<FunctionTemplateInfo> desc, |
| 1047 | Handle<JSObject> instance, |
| 1048 | bool* pending_exception) { |
| 1049 | // Configure the instance by adding the properties specified by the |
| 1050 | // instance template. |
| 1051 | Handle<Object> instance_template = Handle<Object>(desc->instance_template()); |
| 1052 | if (!instance_template->IsUndefined()) { |
| 1053 | Execution::ConfigureInstance(instance, |
| 1054 | instance_template, |
| 1055 | pending_exception); |
| 1056 | } else { |
| 1057 | *pending_exception = false; |
| 1058 | } |
| 1059 | } |
| 1060 | |
| 1061 | |
| 1062 | } } // namespace v8::internal |