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