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