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