ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 1 | // Copyright 2006-2008 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" |
| 35 | |
| 36 | namespace v8 { namespace internal { |
| 37 | |
| 38 | |
| 39 | Handle<FixedArray> Factory::NewFixedArray(int size, PretenureFlag pretenure) { |
| 40 | ASSERT(0 <= size); |
| 41 | CALL_HEAP_FUNCTION(Heap::AllocateFixedArray(size, pretenure), FixedArray); |
| 42 | } |
| 43 | |
| 44 | |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 45 | Handle<FixedArray> Factory::NewFixedArrayWithHoles(int size) { |
| 46 | ASSERT(0 <= size); |
| 47 | CALL_HEAP_FUNCTION(Heap::AllocateFixedArrayWithHoles(size), FixedArray); |
| 48 | } |
| 49 | |
| 50 | |
| 51 | Handle<Dictionary> Factory::NewDictionary(int at_least_space_for) { |
| 52 | ASSERT(0 <= at_least_space_for); |
| 53 | CALL_HEAP_FUNCTION(Dictionary::Allocate(at_least_space_for), Dictionary); |
| 54 | } |
| 55 | |
| 56 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 57 | Handle<DescriptorArray> Factory::NewDescriptorArray(int number_of_descriptors) { |
| 58 | ASSERT(0 <= number_of_descriptors); |
| 59 | CALL_HEAP_FUNCTION(DescriptorArray::Allocate(number_of_descriptors), |
| 60 | DescriptorArray); |
| 61 | } |
| 62 | |
| 63 | |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 64 | // Symbols are created in the old generation (data space). |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 65 | Handle<String> Factory::LookupSymbol(Vector<const char> string) { |
| 66 | CALL_HEAP_FUNCTION(Heap::LookupSymbol(string), String); |
| 67 | } |
| 68 | |
| 69 | |
| 70 | Handle<String> Factory::NewStringFromAscii(Vector<const char> string, |
| 71 | PretenureFlag pretenure) { |
| 72 | CALL_HEAP_FUNCTION(Heap::AllocateStringFromAscii(string, pretenure), String); |
| 73 | } |
| 74 | |
| 75 | Handle<String> Factory::NewStringFromUtf8(Vector<const char> string, |
| 76 | PretenureFlag pretenure) { |
| 77 | CALL_HEAP_FUNCTION(Heap::AllocateStringFromUtf8(string, pretenure), String); |
| 78 | } |
| 79 | |
| 80 | |
| 81 | Handle<String> Factory::NewStringFromTwoByte(Vector<const uc16> string) { |
| 82 | CALL_HEAP_FUNCTION(Heap::AllocateStringFromTwoByte(string), String); |
| 83 | } |
| 84 | |
| 85 | |
| 86 | Handle<String> Factory::NewRawTwoByteString(int length, |
| 87 | PretenureFlag pretenure) { |
| 88 | CALL_HEAP_FUNCTION(Heap::AllocateRawTwoByteString(length, pretenure), String); |
| 89 | } |
| 90 | |
| 91 | |
| 92 | Handle<String> Factory::NewConsString(Handle<String> first, |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 93 | StringShape first_shape, |
| 94 | Handle<String> second, |
| 95 | StringShape second_shape) { |
| 96 | if (first->length(first_shape) == 0) return second; |
| 97 | if (second->length(second_shape) == 0) return first; |
ager@chromium.org | c3e50d8 | 2008-11-05 11:53:10 +0000 | [diff] [blame] | 98 | CALL_HEAP_FUNCTION(Heap::AllocateConsString(*first, *second), String); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 102 | Handle<String> Factory::NewStringSlice(Handle<String> str, |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 103 | int begin, |
| 104 | int end) { |
ager@chromium.org | c3e50d8 | 2008-11-05 11:53:10 +0000 | [diff] [blame] | 105 | CALL_HEAP_FUNCTION(str->Slice(begin, end), String); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | |
| 109 | Handle<String> Factory::NewExternalStringFromAscii( |
| 110 | ExternalAsciiString::Resource* resource) { |
| 111 | CALL_HEAP_FUNCTION(Heap::AllocateExternalStringFromAscii(resource), String); |
| 112 | } |
| 113 | |
| 114 | |
| 115 | Handle<String> Factory::NewExternalStringFromTwoByte( |
| 116 | ExternalTwoByteString::Resource* resource) { |
| 117 | CALL_HEAP_FUNCTION(Heap::AllocateExternalStringFromTwoByte(resource), String); |
| 118 | } |
| 119 | |
| 120 | |
| 121 | Handle<Context> Factory::NewGlobalContext() { |
| 122 | CALL_HEAP_FUNCTION(Heap::AllocateGlobalContext(), Context); |
| 123 | } |
| 124 | |
| 125 | |
| 126 | Handle<Context> Factory::NewFunctionContext(int length, |
| 127 | Handle<JSFunction> closure) { |
| 128 | CALL_HEAP_FUNCTION(Heap::AllocateFunctionContext(length, *closure), Context); |
| 129 | } |
| 130 | |
| 131 | |
| 132 | Handle<Context> Factory::NewWithContext(Handle<Context> previous, |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 133 | Handle<JSObject> extension, |
| 134 | bool is_catch_context) { |
| 135 | CALL_HEAP_FUNCTION(Heap::AllocateWithContext(*previous, |
| 136 | *extension, |
| 137 | is_catch_context), |
| 138 | Context); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | |
| 142 | Handle<Struct> Factory::NewStruct(InstanceType type) { |
| 143 | CALL_HEAP_FUNCTION(Heap::AllocateStruct(type), Struct); |
| 144 | } |
| 145 | |
| 146 | |
| 147 | Handle<AccessorInfo> Factory::NewAccessorInfo() { |
| 148 | Handle<AccessorInfo> info = |
| 149 | Handle<AccessorInfo>::cast(NewStruct(ACCESSOR_INFO_TYPE)); |
| 150 | info->set_flag(0); // Must clear the flag, it was initialized as undefined. |
| 151 | return info; |
| 152 | } |
| 153 | |
| 154 | |
| 155 | Handle<Script> Factory::NewScript(Handle<String> source) { |
| 156 | Handle<Script> script = Handle<Script>::cast(NewStruct(SCRIPT_TYPE)); |
| 157 | script->set_source(*source); |
| 158 | script->set_name(Heap::undefined_value()); |
| 159 | script->set_line_offset(Smi::FromInt(0)); |
| 160 | script->set_column_offset(Smi::FromInt(0)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 161 | script->set_type(Smi::FromInt(SCRIPT_TYPE_NORMAL)); |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 162 | script->set_wrapper(*Factory::NewProxy(0, TENURED)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 163 | return script; |
| 164 | } |
| 165 | |
| 166 | |
| 167 | Handle<Proxy> Factory::NewProxy(Address addr, PretenureFlag pretenure) { |
| 168 | CALL_HEAP_FUNCTION(Heap::AllocateProxy(addr, pretenure), Proxy); |
| 169 | } |
| 170 | |
| 171 | |
| 172 | Handle<Proxy> Factory::NewProxy(const AccessorDescriptor* desc) { |
| 173 | return NewProxy((Address) desc, TENURED); |
| 174 | } |
| 175 | |
| 176 | |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 177 | Handle<ByteArray> Factory::NewByteArray(int length, PretenureFlag pretenure) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 178 | ASSERT(0 <= length); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 179 | CALL_HEAP_FUNCTION(Heap::AllocateByteArray(length, pretenure), ByteArray); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | |
| 183 | Handle<Map> Factory::NewMap(InstanceType type, int instance_size) { |
| 184 | CALL_HEAP_FUNCTION(Heap::AllocateMap(type, instance_size), Map); |
| 185 | } |
| 186 | |
| 187 | |
| 188 | Handle<JSObject> Factory::NewFunctionPrototype(Handle<JSFunction> function) { |
| 189 | CALL_HEAP_FUNCTION(Heap::AllocateFunctionPrototype(*function), JSObject); |
| 190 | } |
| 191 | |
| 192 | |
| 193 | Handle<Map> Factory::CopyMap(Handle<Map> src) { |
| 194 | CALL_HEAP_FUNCTION(src->Copy(), Map); |
| 195 | } |
| 196 | |
| 197 | |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame^] | 198 | Handle<Map> Factory::CopyMap(Handle<Map> src, |
| 199 | int extra_inobject_properties) { |
| 200 | Handle<Map> copy = CopyMap(src); |
| 201 | // Check that we do not overflow the instance size when adding the |
| 202 | // extra inobject properties. |
| 203 | int instance_size_delta = extra_inobject_properties * kPointerSize; |
| 204 | int max_instance_size_delta = |
| 205 | JSObject::kMaxInstanceSize - copy->instance_size(); |
| 206 | if (instance_size_delta > max_instance_size_delta) { |
| 207 | // If the instance size overflows, we allocate as many properties |
| 208 | // as we can as inobject properties. |
| 209 | instance_size_delta = max_instance_size_delta; |
| 210 | extra_inobject_properties = max_instance_size_delta >> kPointerSizeLog2; |
| 211 | } |
| 212 | // Adjust the map with the extra inobject properties. |
| 213 | int inobject_properties = |
| 214 | copy->inobject_properties() + extra_inobject_properties; |
| 215 | copy->set_inobject_properties(inobject_properties); |
| 216 | copy->set_unused_property_fields(inobject_properties); |
| 217 | copy->set_instance_size(copy->instance_size() + instance_size_delta); |
| 218 | return copy; |
| 219 | } |
| 220 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 221 | Handle<Map> Factory::CopyMapDropTransitions(Handle<Map> src) { |
| 222 | CALL_HEAP_FUNCTION(src->CopyDropTransitions(), Map); |
| 223 | } |
| 224 | |
| 225 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 226 | Handle<FixedArray> Factory::CopyFixedArray(Handle<FixedArray> array) { |
| 227 | CALL_HEAP_FUNCTION(array->Copy(), FixedArray); |
| 228 | } |
| 229 | |
| 230 | |
| 231 | Handle<JSFunction> Factory::BaseNewFunctionFromBoilerplate( |
| 232 | Handle<JSFunction> boilerplate, |
| 233 | Handle<Map> function_map) { |
| 234 | ASSERT(boilerplate->IsBoilerplate()); |
| 235 | ASSERT(!boilerplate->has_initial_map()); |
| 236 | ASSERT(!boilerplate->has_prototype()); |
| 237 | ASSERT(boilerplate->properties() == Heap::empty_fixed_array()); |
| 238 | ASSERT(boilerplate->elements() == Heap::empty_fixed_array()); |
| 239 | CALL_HEAP_FUNCTION(Heap::AllocateFunction(*function_map, |
| 240 | boilerplate->shared(), |
| 241 | Heap::the_hole_value()), |
| 242 | JSFunction); |
| 243 | } |
| 244 | |
| 245 | |
| 246 | Handle<JSFunction> Factory::NewFunctionFromBoilerplate( |
| 247 | Handle<JSFunction> boilerplate, |
| 248 | Handle<Context> context) { |
| 249 | Handle<JSFunction> result = |
| 250 | BaseNewFunctionFromBoilerplate(boilerplate, Top::function_map()); |
| 251 | result->set_context(*context); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 252 | int number_of_literals = boilerplate->NumberOfLiterals(); |
| 253 | Handle<FixedArray> literals = |
| 254 | Factory::NewFixedArray(number_of_literals, TENURED); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 255 | if (number_of_literals > 0) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 256 | // Store the object, regexp and array functions in the literals |
| 257 | // array prefix. These functions will be used when creating |
| 258 | // object, regexp and array literals in this function. |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 259 | literals->set(JSFunction::kLiteralGlobalContextIndex, |
| 260 | context->global_context()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 261 | } |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 262 | result->set_literals(*literals); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 263 | ASSERT(!result->IsBoilerplate()); |
| 264 | return result; |
| 265 | } |
| 266 | |
| 267 | |
| 268 | Handle<Object> Factory::NewNumber(double value, |
| 269 | PretenureFlag pretenure) { |
| 270 | CALL_HEAP_FUNCTION(Heap::NumberFromDouble(value, pretenure), Object); |
| 271 | } |
| 272 | |
| 273 | |
| 274 | Handle<Object> Factory::NewNumberFromInt(int value) { |
| 275 | CALL_HEAP_FUNCTION(Heap::NumberFromInt32(value), Object); |
| 276 | } |
| 277 | |
| 278 | |
| 279 | Handle<JSObject> Factory::NewNeanderObject() { |
| 280 | CALL_HEAP_FUNCTION(Heap::AllocateJSObjectFromMap(Heap::neander_map()), |
| 281 | JSObject); |
| 282 | } |
| 283 | |
| 284 | |
| 285 | Handle<Object> Factory::NewTypeError(const char* type, |
| 286 | Vector< Handle<Object> > args) { |
| 287 | return NewError("MakeTypeError", type, args); |
| 288 | } |
| 289 | |
| 290 | |
| 291 | Handle<Object> Factory::NewTypeError(Handle<String> message) { |
| 292 | return NewError("$TypeError", message); |
| 293 | } |
| 294 | |
| 295 | |
| 296 | Handle<Object> Factory::NewRangeError(const char* type, |
| 297 | Vector< Handle<Object> > args) { |
| 298 | return NewError("MakeRangeError", type, args); |
| 299 | } |
| 300 | |
| 301 | |
| 302 | Handle<Object> Factory::NewRangeError(Handle<String> message) { |
| 303 | return NewError("$RangeError", message); |
| 304 | } |
| 305 | |
| 306 | |
| 307 | Handle<Object> Factory::NewSyntaxError(const char* type, Handle<JSArray> args) { |
| 308 | return NewError("MakeSyntaxError", type, args); |
| 309 | } |
| 310 | |
| 311 | |
| 312 | Handle<Object> Factory::NewSyntaxError(Handle<String> message) { |
| 313 | return NewError("$SyntaxError", message); |
| 314 | } |
| 315 | |
| 316 | |
| 317 | Handle<Object> Factory::NewReferenceError(const char* type, |
| 318 | Vector< Handle<Object> > args) { |
| 319 | return NewError("MakeReferenceError", type, args); |
| 320 | } |
| 321 | |
| 322 | |
| 323 | Handle<Object> Factory::NewReferenceError(Handle<String> message) { |
| 324 | return NewError("$ReferenceError", message); |
| 325 | } |
| 326 | |
| 327 | |
| 328 | Handle<Object> Factory::NewError(const char* maker, const char* type, |
| 329 | Vector< Handle<Object> > args) { |
| 330 | HandleScope scope; |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 331 | Handle<FixedArray> array = Factory::NewFixedArray(args.length()); |
| 332 | for (int i = 0; i < args.length(); i++) { |
| 333 | array->set(i, *args[i]); |
| 334 | } |
| 335 | Handle<JSArray> object = Factory::NewJSArrayWithElements(array); |
| 336 | Handle<Object> result = NewError(maker, type, object); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 337 | return result.EscapeFrom(&scope); |
| 338 | } |
| 339 | |
| 340 | |
| 341 | Handle<Object> Factory::NewEvalError(const char* type, |
| 342 | Vector< Handle<Object> > args) { |
| 343 | return NewError("MakeEvalError", type, args); |
| 344 | } |
| 345 | |
| 346 | |
| 347 | Handle<Object> Factory::NewError(const char* type, |
| 348 | Vector< Handle<Object> > args) { |
| 349 | return NewError("MakeError", type, args); |
| 350 | } |
| 351 | |
| 352 | |
| 353 | Handle<Object> Factory::NewError(const char* maker, |
| 354 | const char* type, |
| 355 | Handle<JSArray> args) { |
| 356 | Handle<String> make_str = Factory::LookupAsciiSymbol(maker); |
| 357 | Handle<JSFunction> fun = |
| 358 | Handle<JSFunction>( |
| 359 | JSFunction::cast( |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 360 | Top::builtins()->GetProperty(*make_str))); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 361 | Handle<Object> type_obj = Factory::LookupAsciiSymbol(type); |
| 362 | Object** argv[2] = { type_obj.location(), |
| 363 | Handle<Object>::cast(args).location() }; |
| 364 | |
| 365 | // Invoke the JavaScript factory method. If an exception is thrown while |
| 366 | // running the factory method, use the exception as the result. |
| 367 | bool caught_exception; |
| 368 | Handle<Object> result = Execution::TryCall(fun, |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 369 | Top::builtins(), |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 370 | 2, |
| 371 | argv, |
| 372 | &caught_exception); |
| 373 | return result; |
| 374 | } |
| 375 | |
| 376 | |
| 377 | Handle<Object> Factory::NewError(Handle<String> message) { |
| 378 | return NewError("$Error", message); |
| 379 | } |
| 380 | |
| 381 | |
| 382 | Handle<Object> Factory::NewError(const char* constructor, |
| 383 | Handle<String> message) { |
| 384 | Handle<String> constr = Factory::LookupAsciiSymbol(constructor); |
| 385 | Handle<JSFunction> fun = |
| 386 | Handle<JSFunction>( |
| 387 | JSFunction::cast( |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 388 | Top::builtins()->GetProperty(*constr))); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 389 | Object** argv[1] = { Handle<Object>::cast(message).location() }; |
| 390 | |
| 391 | // Invoke the JavaScript factory method. If an exception is thrown while |
| 392 | // running the factory method, use the exception as the result. |
| 393 | bool caught_exception; |
| 394 | Handle<Object> result = Execution::TryCall(fun, |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 395 | Top::builtins(), |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 396 | 1, |
| 397 | argv, |
| 398 | &caught_exception); |
| 399 | return result; |
| 400 | } |
| 401 | |
| 402 | |
| 403 | Handle<JSFunction> Factory::NewFunction(Handle<String> name, |
| 404 | InstanceType type, |
| 405 | int instance_size, |
| 406 | Handle<Code> code, |
| 407 | bool force_initial_map) { |
| 408 | // Allocate the function |
| 409 | Handle<JSFunction> function = NewFunction(name, the_hole_value()); |
| 410 | function->set_code(*code); |
| 411 | |
| 412 | if (force_initial_map || |
| 413 | type != JS_OBJECT_TYPE || |
| 414 | instance_size != JSObject::kHeaderSize) { |
| 415 | Handle<Map> initial_map = NewMap(type, instance_size); |
| 416 | Handle<JSObject> prototype = NewFunctionPrototype(function); |
| 417 | initial_map->set_prototype(*prototype); |
| 418 | function->set_initial_map(*initial_map); |
| 419 | initial_map->set_constructor(*function); |
| 420 | } else { |
| 421 | ASSERT(!function->has_initial_map()); |
| 422 | ASSERT(!function->has_prototype()); |
| 423 | } |
| 424 | |
| 425 | return function; |
| 426 | } |
| 427 | |
| 428 | |
| 429 | Handle<JSFunction> Factory::NewFunctionBoilerplate(Handle<String> name, |
| 430 | int number_of_literals, |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 431 | bool contains_array_literal, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 432 | Handle<Code> code) { |
| 433 | Handle<JSFunction> function = NewFunctionBoilerplate(name); |
| 434 | function->set_code(*code); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 435 | int literals_array_size = number_of_literals; |
| 436 | // If the function contains object, regexp or array literals, |
| 437 | // allocate extra space for a literals array prefix containing the |
| 438 | // object, regexp and array constructor functions. |
| 439 | if (number_of_literals > 0 || contains_array_literal) { |
| 440 | literals_array_size += JSFunction::kLiteralsPrefixSize; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 441 | } |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 442 | Handle<FixedArray> literals = |
| 443 | Factory::NewFixedArray(literals_array_size, TENURED); |
| 444 | function->set_literals(*literals); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 445 | ASSERT(!function->has_initial_map()); |
| 446 | ASSERT(!function->has_prototype()); |
| 447 | return function; |
| 448 | } |
| 449 | |
| 450 | |
| 451 | Handle<JSFunction> Factory::NewFunctionBoilerplate(Handle<String> name) { |
| 452 | Handle<SharedFunctionInfo> shared = NewSharedFunctionInfo(name); |
| 453 | CALL_HEAP_FUNCTION(Heap::AllocateFunction(Heap::boilerplate_function_map(), |
| 454 | *shared, |
| 455 | Heap::the_hole_value()), |
| 456 | JSFunction); |
| 457 | } |
| 458 | |
| 459 | |
| 460 | Handle<JSFunction> Factory::NewFunctionWithPrototype(Handle<String> name, |
| 461 | InstanceType type, |
| 462 | int instance_size, |
| 463 | Handle<JSObject> prototype, |
| 464 | Handle<Code> code, |
| 465 | bool force_initial_map) { |
| 466 | // Allocate the function |
| 467 | Handle<JSFunction> function = NewFunction(name, prototype); |
| 468 | |
| 469 | function->set_code(*code); |
| 470 | |
| 471 | if (force_initial_map || |
| 472 | type != JS_OBJECT_TYPE || |
| 473 | instance_size != JSObject::kHeaderSize) { |
| 474 | Handle<Map> initial_map = NewMap(type, instance_size); |
| 475 | function->set_initial_map(*initial_map); |
| 476 | initial_map->set_constructor(*function); |
| 477 | } |
| 478 | |
| 479 | // Set function.prototype and give the prototype a constructor |
| 480 | // property that refers to the function. |
| 481 | SetPrototypeProperty(function, prototype); |
| 482 | SetProperty(prototype, Factory::constructor_symbol(), function, DONT_ENUM); |
| 483 | return function; |
| 484 | } |
| 485 | |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 486 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 487 | Handle<Code> Factory::NewCode(const CodeDesc& desc, ScopeInfo<>* sinfo, |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 488 | Code::Flags flags, Handle<Object> self_ref) { |
| 489 | CALL_HEAP_FUNCTION(Heap::CreateCode( |
| 490 | desc, sinfo, flags, reinterpret_cast<Code**>(self_ref.location())), Code); |
| 491 | } |
| 492 | |
| 493 | Handle<Code> Factory::NewCode(const CodeDesc& desc, ScopeInfo<>* sinfo, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 494 | Code::Flags flags) { |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 495 | CALL_HEAP_FUNCTION(Heap::CreateCode(desc, sinfo, flags, NULL), Code); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 496 | } |
| 497 | |
| 498 | |
| 499 | Handle<Code> Factory::CopyCode(Handle<Code> code) { |
| 500 | CALL_HEAP_FUNCTION(Heap::CopyCode(*code), Code); |
| 501 | } |
| 502 | |
| 503 | |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 504 | static inline Object* DoCopyInsert(DescriptorArray* array, |
| 505 | String* key, |
| 506 | Object* value, |
| 507 | PropertyAttributes attributes) { |
| 508 | CallbacksDescriptor desc(key, value, attributes); |
| 509 | Object* obj = array->CopyInsert(&desc, REMOVE_TRANSITIONS); |
| 510 | return obj; |
| 511 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 512 | |
| 513 | |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 514 | // Allocate the new array. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 515 | Handle<DescriptorArray> Factory::CopyAppendProxyDescriptor( |
| 516 | Handle<DescriptorArray> array, |
| 517 | Handle<String> key, |
| 518 | Handle<Object> value, |
| 519 | PropertyAttributes attributes) { |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 520 | CALL_HEAP_FUNCTION(DoCopyInsert(*array, *key, *value, attributes), |
| 521 | DescriptorArray); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 522 | } |
| 523 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 524 | |
| 525 | Handle<String> Factory::SymbolFromString(Handle<String> value) { |
| 526 | CALL_HEAP_FUNCTION(Heap::LookupSymbol(*value), String); |
| 527 | } |
| 528 | |
| 529 | |
| 530 | Handle<DescriptorArray> Factory::CopyAppendCallbackDescriptors( |
| 531 | Handle<DescriptorArray> array, |
| 532 | Handle<Object> descriptors) { |
| 533 | v8::NeanderArray callbacks(descriptors); |
| 534 | int nof_callbacks = callbacks.length(); |
| 535 | Handle<DescriptorArray> result = |
| 536 | NewDescriptorArray(array->number_of_descriptors() + nof_callbacks); |
| 537 | |
| 538 | // Number of descriptors added to the result so far. |
| 539 | int descriptor_count = 0; |
| 540 | |
| 541 | // Copy the descriptors from the array. |
| 542 | DescriptorWriter w(*result); |
| 543 | for (DescriptorReader r(*array); !r.eos(); r.advance()) { |
| 544 | w.WriteFrom(&r); |
| 545 | descriptor_count++; |
| 546 | } |
| 547 | |
| 548 | // Number of duplicates detected. |
| 549 | int duplicates = 0; |
| 550 | |
| 551 | // Fill in new callback descriptors. Process the callbacks from |
| 552 | // back to front so that the last callback with a given name takes |
| 553 | // precedence over previously added callbacks with that name. |
| 554 | for (int i = nof_callbacks - 1; i >= 0; i--) { |
| 555 | Handle<AccessorInfo> entry = |
| 556 | Handle<AccessorInfo>(AccessorInfo::cast(callbacks.get(i))); |
| 557 | // Ensure the key is a symbol before writing into the instance descriptor. |
| 558 | Handle<String> key = |
| 559 | SymbolFromString(Handle<String>(String::cast(entry->name()))); |
| 560 | // Check if a descriptor with this name already exists before writing. |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 561 | if (result->LinearSearch(*key, descriptor_count) == |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 562 | DescriptorArray::kNotFound) { |
| 563 | CallbacksDescriptor desc(*key, *entry, entry->property_attributes()); |
| 564 | w.Write(&desc); |
| 565 | descriptor_count++; |
| 566 | } else { |
| 567 | duplicates++; |
| 568 | } |
| 569 | } |
| 570 | |
| 571 | // If duplicates were detected, allocate a result of the right size |
| 572 | // and transfer the elements. |
| 573 | if (duplicates > 0) { |
| 574 | Handle<DescriptorArray> new_result = |
| 575 | NewDescriptorArray(result->number_of_descriptors() - duplicates); |
| 576 | DescriptorWriter w(*new_result); |
| 577 | DescriptorReader r(*result); |
| 578 | while (!w.eos()) { |
| 579 | w.WriteFrom(&r); |
| 580 | r.advance(); |
| 581 | } |
| 582 | result = new_result; |
| 583 | } |
| 584 | |
| 585 | // Sort the result before returning. |
| 586 | result->Sort(); |
| 587 | return result; |
| 588 | } |
| 589 | |
| 590 | |
| 591 | Handle<JSObject> Factory::NewJSObject(Handle<JSFunction> constructor, |
| 592 | PretenureFlag pretenure) { |
| 593 | CALL_HEAP_FUNCTION(Heap::AllocateJSObject(*constructor, pretenure), JSObject); |
| 594 | } |
| 595 | |
| 596 | |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 597 | Handle<JSObject> Factory::NewJSObjectFromMap(Handle<Map> map) { |
| 598 | CALL_HEAP_FUNCTION(Heap::AllocateJSObjectFromMap(*map, NOT_TENURED), |
| 599 | JSObject); |
| 600 | } |
| 601 | |
| 602 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 603 | Handle<JSArray> Factory::NewArrayLiteral(int length) { |
| 604 | return NewJSArrayWithElements(NewFixedArray(length), TENURED); |
| 605 | } |
| 606 | |
| 607 | |
| 608 | Handle<JSArray> Factory::NewJSArray(int length, |
| 609 | PretenureFlag pretenure) { |
| 610 | Handle<JSObject> obj = NewJSObject(Top::array_function(), pretenure); |
| 611 | CALL_HEAP_FUNCTION(Handle<JSArray>::cast(obj)->Initialize(length), JSArray); |
| 612 | } |
| 613 | |
| 614 | |
| 615 | Handle<JSArray> Factory::NewJSArrayWithElements(Handle<FixedArray> elements, |
| 616 | PretenureFlag pretenure) { |
| 617 | Handle<JSArray> result = |
| 618 | Handle<JSArray>::cast(NewJSObject(Top::array_function(), pretenure)); |
| 619 | result->SetContent(*elements); |
| 620 | return result; |
| 621 | } |
| 622 | |
| 623 | |
| 624 | Handle<SharedFunctionInfo> Factory::NewSharedFunctionInfo(Handle<String> name) { |
| 625 | CALL_HEAP_FUNCTION(Heap::AllocateSharedFunctionInfo(*name), |
| 626 | SharedFunctionInfo); |
| 627 | } |
| 628 | |
| 629 | |
| 630 | Handle<Dictionary> Factory::DictionaryAtNumberPut(Handle<Dictionary> dictionary, |
| 631 | uint32_t key, |
| 632 | Handle<Object> value) { |
| 633 | CALL_HEAP_FUNCTION(dictionary->AtNumberPut(key, *value), Dictionary); |
| 634 | } |
| 635 | |
| 636 | |
| 637 | Handle<JSFunction> Factory::NewFunctionHelper(Handle<String> name, |
| 638 | Handle<Object> prototype) { |
| 639 | Handle<SharedFunctionInfo> function_share = NewSharedFunctionInfo(name); |
| 640 | CALL_HEAP_FUNCTION(Heap::AllocateFunction(*Top::function_map(), |
| 641 | *function_share, |
| 642 | *prototype), |
| 643 | JSFunction); |
| 644 | } |
| 645 | |
| 646 | |
| 647 | Handle<JSFunction> Factory::NewFunction(Handle<String> name, |
| 648 | Handle<Object> prototype) { |
| 649 | Handle<JSFunction> fun = NewFunctionHelper(name, prototype); |
| 650 | fun->set_context(Top::context()->global_context()); |
| 651 | return fun; |
| 652 | } |
| 653 | |
| 654 | |
| 655 | Handle<Object> Factory::ToObject(Handle<Object> object, |
| 656 | Handle<Context> global_context) { |
| 657 | CALL_HEAP_FUNCTION(object->ToObject(*global_context), Object); |
| 658 | } |
| 659 | |
| 660 | |
v8.team.kasperl | 727e995 | 2008-09-02 14:56:44 +0000 | [diff] [blame] | 661 | Handle<DebugInfo> Factory::NewDebugInfo(Handle<SharedFunctionInfo> shared) { |
| 662 | // Get the original code of the function. |
| 663 | Handle<Code> code(shared->code()); |
| 664 | |
| 665 | // Create a copy of the code before allocating the debug info object to avoid |
| 666 | // allocation while setting up the debug info object. |
| 667 | Handle<Code> original_code(*Factory::CopyCode(code)); |
| 668 | |
| 669 | // Allocate initial fixed array for active break points before allocating the |
| 670 | // debug info object to avoid allocation while setting up the debug info |
| 671 | // object. |
| 672 | Handle<FixedArray> break_points( |
| 673 | Factory::NewFixedArray(Debug::kEstimatedNofBreakPointsInFunction)); |
| 674 | |
| 675 | // Create and set up the debug info object. Debug info contains function, a |
| 676 | // copy of the original code, the executing code and initial fixed array for |
| 677 | // active break points. |
| 678 | Handle<DebugInfo> debug_info = |
| 679 | Handle<DebugInfo>::cast(Factory::NewStruct(DEBUG_INFO_TYPE)); |
| 680 | debug_info->set_shared(*shared); |
| 681 | debug_info->set_original_code(*original_code); |
| 682 | debug_info->set_code(*code); |
| 683 | debug_info->set_break_points(*break_points); |
| 684 | |
| 685 | // Link debug info to function. |
| 686 | shared->set_debug_info(*debug_info); |
| 687 | |
| 688 | return debug_info; |
| 689 | } |
| 690 | |
| 691 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 692 | Handle<JSObject> Factory::NewArgumentsObject(Handle<Object> callee, |
| 693 | int length) { |
| 694 | CALL_HEAP_FUNCTION(Heap::AllocateArgumentsObject(*callee, length), JSObject); |
| 695 | } |
| 696 | |
| 697 | |
| 698 | Handle<JSFunction> Factory::CreateApiFunction( |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 699 | Handle<FunctionTemplateInfo> obj, ApiInstanceType instance_type) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 700 | Handle<Code> code = Handle<Code>(Builtins::builtin(Builtins::HandleApiCall)); |
| 701 | |
kasper.lund | 212ac23 | 2008-07-16 07:07:30 +0000 | [diff] [blame] | 702 | int internal_field_count = 0; |
| 703 | if (!obj->instance_template()->IsUndefined()) { |
| 704 | Handle<ObjectTemplateInfo> instance_template = |
| 705 | Handle<ObjectTemplateInfo>( |
| 706 | ObjectTemplateInfo::cast(obj->instance_template())); |
| 707 | internal_field_count = |
| 708 | Smi::cast(instance_template->internal_field_count())->value(); |
| 709 | } |
| 710 | |
| 711 | int instance_size = kPointerSize * internal_field_count; |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 712 | InstanceType type = INVALID_TYPE; |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 713 | switch (instance_type) { |
| 714 | case JavaScriptObject: |
| 715 | type = JS_OBJECT_TYPE; |
| 716 | instance_size += JSObject::kHeaderSize; |
| 717 | break; |
| 718 | case InnerGlobalObject: |
| 719 | type = JS_GLOBAL_OBJECT_TYPE; |
| 720 | instance_size += JSGlobalObject::kSize; |
| 721 | break; |
| 722 | case OuterGlobalObject: |
| 723 | type = JS_GLOBAL_PROXY_TYPE; |
| 724 | instance_size += JSGlobalProxy::kSize; |
| 725 | break; |
| 726 | default: |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 727 | break; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 728 | } |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 729 | ASSERT(type != INVALID_TYPE); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 730 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 731 | Handle<JSFunction> result = |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 732 | Factory::NewFunction(Factory::empty_symbol(), |
| 733 | type, |
| 734 | instance_size, |
| 735 | code, |
| 736 | true); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 737 | // Set class name. |
| 738 | Handle<Object> class_name = Handle<Object>(obj->class_name()); |
| 739 | if (class_name->IsString()) { |
| 740 | result->shared()->set_instance_class_name(*class_name); |
| 741 | result->shared()->set_name(*class_name); |
| 742 | } |
| 743 | |
| 744 | Handle<Map> map = Handle<Map>(result->initial_map()); |
| 745 | |
| 746 | // Mark as undetectable if needed. |
| 747 | if (obj->undetectable()) { |
| 748 | map->set_is_undetectable(); |
| 749 | } |
| 750 | |
| 751 | // Mark as hidden for the __proto__ accessor if needed. |
| 752 | if (obj->hidden_prototype()) { |
| 753 | map->set_is_hidden_prototype(); |
| 754 | } |
| 755 | |
| 756 | // Mark as needs_access_check if needed. |
| 757 | if (obj->needs_access_check()) { |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 758 | map->set_is_access_check_needed(true); |
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 | // Set interceptor information in the map. |
| 762 | if (!obj->named_property_handler()->IsUndefined()) { |
| 763 | map->set_has_named_interceptor(); |
| 764 | } |
| 765 | if (!obj->indexed_property_handler()->IsUndefined()) { |
| 766 | map->set_has_indexed_interceptor(); |
| 767 | } |
| 768 | |
| 769 | // Set instance call-as-function information in the map. |
| 770 | if (!obj->instance_call_handler()->IsUndefined()) { |
| 771 | map->set_has_instance_call_handler(); |
| 772 | } |
| 773 | |
| 774 | result->shared()->set_function_data(*obj); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 775 | result->shared()->DontAdaptArguments(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 776 | |
| 777 | // Recursively copy parent templates' accessors, 'data' may be modified. |
| 778 | Handle<DescriptorArray> array = |
| 779 | Handle<DescriptorArray>(map->instance_descriptors()); |
| 780 | while (true) { |
| 781 | Handle<Object> props = Handle<Object>(obj->property_accessors()); |
| 782 | if (!props->IsUndefined()) { |
| 783 | array = Factory::CopyAppendCallbackDescriptors(array, props); |
| 784 | } |
| 785 | Handle<Object> parent = Handle<Object>(obj->parent_template()); |
| 786 | if (parent->IsUndefined()) break; |
| 787 | obj = Handle<FunctionTemplateInfo>::cast(parent); |
| 788 | } |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 789 | if (!array->IsEmpty()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 790 | map->set_instance_descriptors(*array); |
| 791 | } |
| 792 | |
| 793 | return result; |
| 794 | } |
| 795 | |
| 796 | |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 797 | Handle<MapCache> Factory::NewMapCache(int at_least_space_for) { |
| 798 | CALL_HEAP_FUNCTION(MapCache::Allocate(at_least_space_for), MapCache); |
| 799 | } |
| 800 | |
| 801 | |
| 802 | static Object* UpdateMapCacheWith(Context* context, |
| 803 | FixedArray* keys, |
| 804 | Map* map) { |
| 805 | Object* result = MapCache::cast(context->map_cache())->Put(keys, map); |
| 806 | if (!result->IsFailure()) context->set_map_cache(MapCache::cast(result)); |
| 807 | return result; |
| 808 | } |
| 809 | |
| 810 | |
| 811 | Handle<MapCache> Factory::AddToMapCache(Handle<Context> context, |
| 812 | Handle<FixedArray> keys, |
| 813 | Handle<Map> map) { |
| 814 | CALL_HEAP_FUNCTION(UpdateMapCacheWith(*context, *keys, *map), MapCache); |
| 815 | } |
| 816 | |
| 817 | |
| 818 | Handle<Map> Factory::ObjectLiteralMapFromCache(Handle<Context> context, |
| 819 | Handle<FixedArray> keys) { |
| 820 | if (context->map_cache()->IsUndefined()) { |
| 821 | // Allocate the new map cache for the global context. |
| 822 | Handle<MapCache> new_cache = NewMapCache(24); |
| 823 | context->set_map_cache(*new_cache); |
| 824 | } |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame^] | 825 | // 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] | 826 | Handle<MapCache> cache = |
| 827 | Handle<MapCache>(MapCache::cast(context->map_cache())); |
| 828 | Handle<Object> result = Handle<Object>(cache->Lookup(*keys)); |
| 829 | if (result->IsMap()) return Handle<Map>::cast(result); |
| 830 | // Create a new map and add it to the cache. |
| 831 | Handle<Map> map = |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame^] | 832 | CopyMap(Handle<Map>(context->object_function()->initial_map()), |
| 833 | keys->length()); |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 834 | AddToMapCache(context, keys, map); |
| 835 | return Handle<Map>(map); |
| 836 | } |
| 837 | |
| 838 | |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 839 | void Factory::SetRegExpData(Handle<JSRegExp> regexp, |
| 840 | JSRegExp::Type type, |
| 841 | Handle<String> source, |
| 842 | JSRegExp::Flags flags, |
| 843 | Handle<Object> data) { |
| 844 | Handle<FixedArray> store = NewFixedArray(JSRegExp::kDataSize); |
| 845 | store->set(JSRegExp::kTagIndex, Smi::FromInt(type)); |
| 846 | store->set(JSRegExp::kSourceIndex, *source); |
| 847 | store->set(JSRegExp::kFlagsIndex, Smi::FromInt(flags.value())); |
| 848 | store->set(JSRegExp::kAtomPatternIndex, *data); |
| 849 | regexp->set_data(*store); |
| 850 | } |
| 851 | |
| 852 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 853 | void Factory::ConfigureInstance(Handle<FunctionTemplateInfo> desc, |
| 854 | Handle<JSObject> instance, |
| 855 | bool* pending_exception) { |
| 856 | // Configure the instance by adding the properties specified by the |
| 857 | // instance template. |
| 858 | Handle<Object> instance_template = Handle<Object>(desc->instance_template()); |
| 859 | if (!instance_template->IsUndefined()) { |
| 860 | Execution::ConfigureInstance(instance, |
| 861 | instance_template, |
| 862 | pending_exception); |
| 863 | } else { |
| 864 | *pending_exception = false; |
| 865 | } |
| 866 | } |
| 867 | |
| 868 | |
| 869 | } } // namespace v8::internal |