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