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