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 "accessors.h" |
| 31 | #include "execution.h" |
| 32 | #include "factory.h" |
| 33 | #include "scopeinfo.h" |
| 34 | #include "top.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 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 | template <class C> |
| 41 | static C* FindInPrototypeChain(Object* obj, bool* found_it) { |
| 42 | ASSERT(!*found_it); |
| 43 | while (!Is<C>(obj)) { |
| 44 | if (obj == Heap::null_value()) return NULL; |
| 45 | obj = obj->GetPrototype(); |
| 46 | } |
| 47 | *found_it = true; |
| 48 | return C::cast(obj); |
| 49 | } |
| 50 | |
| 51 | |
| 52 | // Entry point that never should be called. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 53 | MaybeObject* Accessors::IllegalSetter(JSObject*, Object*, void*) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 54 | UNREACHABLE(); |
| 55 | return NULL; |
| 56 | } |
| 57 | |
| 58 | |
| 59 | Object* Accessors::IllegalGetAccessor(Object* object, void*) { |
| 60 | UNREACHABLE(); |
| 61 | return object; |
| 62 | } |
| 63 | |
| 64 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 65 | MaybeObject* Accessors::ReadOnlySetAccessor(JSObject*, Object* value, void*) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 66 | // According to ECMA-262, section 8.6.2.2, page 28, setting |
| 67 | // read-only properties must be silently ignored. |
| 68 | return value; |
| 69 | } |
| 70 | |
| 71 | |
| 72 | // |
| 73 | // Accessors::ArrayLength |
| 74 | // |
| 75 | |
| 76 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 77 | MaybeObject* Accessors::ArrayGetLength(Object* object, void*) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 78 | // Traverse the prototype chain until we reach an array. |
| 79 | bool found_it = false; |
| 80 | JSArray* holder = FindInPrototypeChain<JSArray>(object, &found_it); |
| 81 | if (!found_it) return Smi::FromInt(0); |
| 82 | return holder->length(); |
| 83 | } |
| 84 | |
| 85 | |
| 86 | // The helper function will 'flatten' Number objects. |
| 87 | Object* Accessors::FlattenNumber(Object* value) { |
| 88 | if (value->IsNumber() || !value->IsJSValue()) return value; |
| 89 | JSValue* wrapper = JSValue::cast(value); |
| 90 | ASSERT( |
| 91 | Top::context()->global_context()->number_function()->has_initial_map()); |
| 92 | Map* number_map = |
| 93 | Top::context()->global_context()->number_function()->initial_map(); |
| 94 | if (wrapper->map() == number_map) return wrapper->value(); |
| 95 | return value; |
| 96 | } |
| 97 | |
| 98 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 99 | MaybeObject* Accessors::ArraySetLength(JSObject* object, Object* value, void*) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 100 | value = FlattenNumber(value); |
| 101 | |
| 102 | // Need to call methods that may trigger GC. |
| 103 | HandleScope scope; |
| 104 | |
| 105 | // Protect raw pointers. |
| 106 | Handle<JSObject> object_handle(object); |
| 107 | Handle<Object> value_handle(value); |
| 108 | |
| 109 | bool has_exception; |
| 110 | Handle<Object> uint32_v = Execution::ToUint32(value_handle, &has_exception); |
| 111 | if (has_exception) return Failure::Exception(); |
| 112 | Handle<Object> number_v = Execution::ToNumber(value_handle, &has_exception); |
| 113 | if (has_exception) return Failure::Exception(); |
| 114 | |
| 115 | // Restore raw pointers, |
| 116 | object = *object_handle; |
| 117 | value = *value_handle; |
| 118 | |
| 119 | if (uint32_v->Number() == number_v->Number()) { |
| 120 | if (object->IsJSArray()) { |
| 121 | return JSArray::cast(object)->SetElementsLength(*uint32_v); |
| 122 | } else { |
| 123 | // This means one of the object's prototypes is a JSArray and |
| 124 | // the object does not have a 'length' property. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 125 | // Calling SetProperty causes an infinite loop. |
| 126 | return object->IgnoreAttributesAndSetLocalProperty(Heap::length_symbol(), |
| 127 | value, NONE); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 128 | } |
| 129 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 130 | return Top::Throw(*Factory::NewRangeError("invalid_array_length", |
| 131 | HandleVector<Object>(NULL, 0))); |
| 132 | } |
| 133 | |
| 134 | |
| 135 | const AccessorDescriptor Accessors::ArrayLength = { |
| 136 | ArrayGetLength, |
| 137 | ArraySetLength, |
| 138 | 0 |
| 139 | }; |
| 140 | |
| 141 | |
| 142 | // |
| 143 | // Accessors::StringLength |
| 144 | // |
| 145 | |
| 146 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 147 | MaybeObject* Accessors::StringGetLength(Object* object, void*) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 148 | Object* value = object; |
| 149 | if (object->IsJSValue()) value = JSValue::cast(object)->value(); |
| 150 | if (value->IsString()) return Smi::FromInt(String::cast(value)->length()); |
| 151 | // If object is not a string we return 0 to be compatible with WebKit. |
| 152 | // Note: Firefox returns the length of ToString(object). |
| 153 | return Smi::FromInt(0); |
| 154 | } |
| 155 | |
| 156 | |
| 157 | const AccessorDescriptor Accessors::StringLength = { |
| 158 | StringGetLength, |
| 159 | IllegalSetter, |
| 160 | 0 |
| 161 | }; |
| 162 | |
| 163 | |
| 164 | // |
| 165 | // Accessors::ScriptSource |
| 166 | // |
| 167 | |
| 168 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 169 | MaybeObject* Accessors::ScriptGetSource(Object* object, void*) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 170 | Object* script = JSValue::cast(object)->value(); |
| 171 | return Script::cast(script)->source(); |
| 172 | } |
| 173 | |
| 174 | |
| 175 | const AccessorDescriptor Accessors::ScriptSource = { |
| 176 | ScriptGetSource, |
| 177 | IllegalSetter, |
| 178 | 0 |
| 179 | }; |
| 180 | |
| 181 | |
| 182 | // |
| 183 | // Accessors::ScriptName |
| 184 | // |
| 185 | |
| 186 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 187 | MaybeObject* Accessors::ScriptGetName(Object* object, void*) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 188 | Object* script = JSValue::cast(object)->value(); |
| 189 | return Script::cast(script)->name(); |
| 190 | } |
| 191 | |
| 192 | |
| 193 | const AccessorDescriptor Accessors::ScriptName = { |
| 194 | ScriptGetName, |
| 195 | IllegalSetter, |
| 196 | 0 |
| 197 | }; |
| 198 | |
| 199 | |
| 200 | // |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 201 | // Accessors::ScriptId |
| 202 | // |
| 203 | |
| 204 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 205 | MaybeObject* Accessors::ScriptGetId(Object* object, void*) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 206 | Object* script = JSValue::cast(object)->value(); |
| 207 | return Script::cast(script)->id(); |
| 208 | } |
| 209 | |
| 210 | |
| 211 | const AccessorDescriptor Accessors::ScriptId = { |
| 212 | ScriptGetId, |
| 213 | IllegalSetter, |
| 214 | 0 |
| 215 | }; |
| 216 | |
| 217 | |
| 218 | // |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 219 | // Accessors::ScriptLineOffset |
| 220 | // |
| 221 | |
| 222 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 223 | MaybeObject* Accessors::ScriptGetLineOffset(Object* object, void*) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 224 | Object* script = JSValue::cast(object)->value(); |
| 225 | return Script::cast(script)->line_offset(); |
| 226 | } |
| 227 | |
| 228 | |
| 229 | const AccessorDescriptor Accessors::ScriptLineOffset = { |
| 230 | ScriptGetLineOffset, |
| 231 | IllegalSetter, |
| 232 | 0 |
| 233 | }; |
| 234 | |
| 235 | |
| 236 | // |
| 237 | // Accessors::ScriptColumnOffset |
| 238 | // |
| 239 | |
| 240 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 241 | MaybeObject* Accessors::ScriptGetColumnOffset(Object* object, void*) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 242 | Object* script = JSValue::cast(object)->value(); |
| 243 | return Script::cast(script)->column_offset(); |
| 244 | } |
| 245 | |
| 246 | |
| 247 | const AccessorDescriptor Accessors::ScriptColumnOffset = { |
| 248 | ScriptGetColumnOffset, |
| 249 | IllegalSetter, |
| 250 | 0 |
| 251 | }; |
| 252 | |
| 253 | |
| 254 | // |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 255 | // Accessors::ScriptData |
| 256 | // |
| 257 | |
| 258 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 259 | MaybeObject* Accessors::ScriptGetData(Object* object, void*) { |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 260 | Object* script = JSValue::cast(object)->value(); |
| 261 | return Script::cast(script)->data(); |
| 262 | } |
| 263 | |
| 264 | |
| 265 | const AccessorDescriptor Accessors::ScriptData = { |
| 266 | ScriptGetData, |
| 267 | IllegalSetter, |
| 268 | 0 |
| 269 | }; |
| 270 | |
| 271 | |
| 272 | // |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 273 | // Accessors::ScriptType |
| 274 | // |
| 275 | |
| 276 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 277 | MaybeObject* Accessors::ScriptGetType(Object* object, void*) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 278 | Object* script = JSValue::cast(object)->value(); |
| 279 | return Script::cast(script)->type(); |
| 280 | } |
| 281 | |
| 282 | |
| 283 | const AccessorDescriptor Accessors::ScriptType = { |
| 284 | ScriptGetType, |
| 285 | IllegalSetter, |
| 286 | 0 |
| 287 | }; |
| 288 | |
| 289 | |
| 290 | // |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 291 | // Accessors::ScriptCompilationType |
| 292 | // |
| 293 | |
| 294 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 295 | MaybeObject* Accessors::ScriptGetCompilationType(Object* object, void*) { |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 296 | Object* script = JSValue::cast(object)->value(); |
| 297 | return Script::cast(script)->compilation_type(); |
| 298 | } |
| 299 | |
| 300 | |
| 301 | const AccessorDescriptor Accessors::ScriptCompilationType = { |
| 302 | ScriptGetCompilationType, |
| 303 | IllegalSetter, |
| 304 | 0 |
| 305 | }; |
| 306 | |
| 307 | |
| 308 | // |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 309 | // Accessors::ScriptGetLineEnds |
| 310 | // |
| 311 | |
| 312 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 313 | MaybeObject* Accessors::ScriptGetLineEnds(Object* object, void*) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 314 | HandleScope scope; |
| 315 | Handle<Script> script(Script::cast(JSValue::cast(object)->value())); |
| 316 | InitScriptLineEnds(script); |
sgjesse@chromium.org | 499aaa5 | 2009-11-30 08:07:20 +0000 | [diff] [blame] | 317 | ASSERT(script->line_ends()->IsFixedArray()); |
| 318 | Handle<FixedArray> line_ends(FixedArray::cast(script->line_ends())); |
| 319 | Handle<FixedArray> copy = Factory::CopyFixedArray(line_ends); |
| 320 | Handle<JSArray> js_array = Factory::NewJSArrayWithElements(copy); |
| 321 | return *js_array; |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | |
| 325 | const AccessorDescriptor Accessors::ScriptLineEnds = { |
| 326 | ScriptGetLineEnds, |
| 327 | IllegalSetter, |
| 328 | 0 |
| 329 | }; |
| 330 | |
| 331 | |
| 332 | // |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 333 | // Accessors::ScriptGetContextData |
| 334 | // |
| 335 | |
| 336 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 337 | MaybeObject* Accessors::ScriptGetContextData(Object* object, void*) { |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 338 | Object* script = JSValue::cast(object)->value(); |
| 339 | return Script::cast(script)->context_data(); |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | |
| 343 | const AccessorDescriptor Accessors::ScriptContextData = { |
| 344 | ScriptGetContextData, |
| 345 | IllegalSetter, |
| 346 | 0 |
| 347 | }; |
| 348 | |
| 349 | |
| 350 | // |
sgjesse@chromium.org | 9818059 | 2009-12-02 08:17:28 +0000 | [diff] [blame] | 351 | // Accessors::ScriptGetEvalFromScript |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 352 | // |
| 353 | |
| 354 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 355 | MaybeObject* Accessors::ScriptGetEvalFromScript(Object* object, void*) { |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 356 | Object* script = JSValue::cast(object)->value(); |
sgjesse@chromium.org | 9818059 | 2009-12-02 08:17:28 +0000 | [diff] [blame] | 357 | if (!Script::cast(script)->eval_from_shared()->IsUndefined()) { |
| 358 | Handle<SharedFunctionInfo> eval_from_shared( |
| 359 | SharedFunctionInfo::cast(Script::cast(script)->eval_from_shared())); |
| 360 | |
| 361 | if (eval_from_shared->script()->IsScript()) { |
| 362 | Handle<Script> eval_from_script(Script::cast(eval_from_shared->script())); |
| 363 | return *GetScriptWrapper(eval_from_script); |
| 364 | } |
| 365 | } |
| 366 | return Heap::undefined_value(); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 367 | } |
| 368 | |
| 369 | |
sgjesse@chromium.org | 9818059 | 2009-12-02 08:17:28 +0000 | [diff] [blame] | 370 | const AccessorDescriptor Accessors::ScriptEvalFromScript = { |
| 371 | ScriptGetEvalFromScript, |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 372 | IllegalSetter, |
| 373 | 0 |
| 374 | }; |
| 375 | |
| 376 | |
| 377 | // |
sgjesse@chromium.org | 9818059 | 2009-12-02 08:17:28 +0000 | [diff] [blame] | 378 | // Accessors::ScriptGetEvalFromScriptPosition |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 379 | // |
| 380 | |
| 381 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 382 | MaybeObject* Accessors::ScriptGetEvalFromScriptPosition(Object* object, void*) { |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 383 | HandleScope scope; |
| 384 | Handle<Script> script(Script::cast(JSValue::cast(object)->value())); |
| 385 | |
| 386 | // If this is not a script compiled through eval there is no eval position. |
| 387 | int compilation_type = Smi::cast(script->compilation_type())->value(); |
| 388 | if (compilation_type != Script::COMPILATION_TYPE_EVAL) { |
| 389 | return Heap::undefined_value(); |
| 390 | } |
| 391 | |
| 392 | // Get the function from where eval was called and find the source position |
| 393 | // from the instruction offset. |
sgjesse@chromium.org | 9818059 | 2009-12-02 08:17:28 +0000 | [diff] [blame] | 394 | Handle<Code> code(SharedFunctionInfo::cast( |
| 395 | script->eval_from_shared())->code()); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 396 | return Smi::FromInt(code->SourcePosition(code->instruction_start() + |
| 397 | script->eval_from_instructions_offset()->value())); |
| 398 | } |
| 399 | |
| 400 | |
sgjesse@chromium.org | 9818059 | 2009-12-02 08:17:28 +0000 | [diff] [blame] | 401 | const AccessorDescriptor Accessors::ScriptEvalFromScriptPosition = { |
| 402 | ScriptGetEvalFromScriptPosition, |
| 403 | IllegalSetter, |
| 404 | 0 |
| 405 | }; |
| 406 | |
| 407 | |
| 408 | // |
| 409 | // Accessors::ScriptGetEvalFromFunctionName |
| 410 | // |
| 411 | |
| 412 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 413 | MaybeObject* Accessors::ScriptGetEvalFromFunctionName(Object* object, void*) { |
sgjesse@chromium.org | 9818059 | 2009-12-02 08:17:28 +0000 | [diff] [blame] | 414 | Object* script = JSValue::cast(object)->value(); |
| 415 | Handle<SharedFunctionInfo> shared(SharedFunctionInfo::cast( |
| 416 | Script::cast(script)->eval_from_shared())); |
| 417 | |
| 418 | |
| 419 | // Find the name of the function calling eval. |
| 420 | if (!shared->name()->IsUndefined()) { |
| 421 | return shared->name(); |
| 422 | } else { |
| 423 | return shared->inferred_name(); |
| 424 | } |
| 425 | } |
| 426 | |
| 427 | |
| 428 | const AccessorDescriptor Accessors::ScriptEvalFromFunctionName = { |
| 429 | ScriptGetEvalFromFunctionName, |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 430 | IllegalSetter, |
| 431 | 0 |
| 432 | }; |
| 433 | |
| 434 | |
| 435 | // |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 436 | // Accessors::FunctionPrototype |
| 437 | // |
| 438 | |
| 439 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 440 | MaybeObject* Accessors::FunctionGetPrototype(Object* object, void*) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 441 | bool found_it = false; |
| 442 | JSFunction* function = FindInPrototypeChain<JSFunction>(object, &found_it); |
| 443 | if (!found_it) return Heap::undefined_value(); |
| 444 | if (!function->has_prototype()) { |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 445 | Object* prototype; |
| 446 | { MaybeObject* maybe_prototype = Heap::AllocateFunctionPrototype(function); |
| 447 | if (!maybe_prototype->ToObject(&prototype)) return maybe_prototype; |
| 448 | } |
| 449 | Object* result; |
| 450 | { MaybeObject* maybe_result = function->SetPrototype(prototype); |
| 451 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 452 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 453 | } |
| 454 | return function->prototype(); |
| 455 | } |
| 456 | |
| 457 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 458 | MaybeObject* Accessors::FunctionSetPrototype(JSObject* object, |
| 459 | Object* value, |
| 460 | void*) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 461 | bool found_it = false; |
| 462 | JSFunction* function = FindInPrototypeChain<JSFunction>(object, &found_it); |
| 463 | if (!found_it) return Heap::undefined_value(); |
| 464 | if (function->has_initial_map()) { |
| 465 | // If the function has allocated the initial map |
| 466 | // replace it with a copy containing the new prototype. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 467 | Object* new_map; |
| 468 | { MaybeObject* maybe_new_map = |
| 469 | function->initial_map()->CopyDropTransitions(); |
| 470 | if (!maybe_new_map->ToObject(&new_map)) return maybe_new_map; |
| 471 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 472 | function->set_initial_map(Map::cast(new_map)); |
| 473 | } |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 474 | Object* prototype; |
| 475 | { MaybeObject* maybe_prototype = function->SetPrototype(value); |
| 476 | if (!maybe_prototype->ToObject(&prototype)) return maybe_prototype; |
| 477 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 478 | ASSERT(function->prototype() == value); |
| 479 | return function; |
| 480 | } |
| 481 | |
| 482 | |
| 483 | const AccessorDescriptor Accessors::FunctionPrototype = { |
| 484 | FunctionGetPrototype, |
| 485 | FunctionSetPrototype, |
| 486 | 0 |
| 487 | }; |
| 488 | |
| 489 | |
| 490 | // |
| 491 | // Accessors::FunctionLength |
| 492 | // |
| 493 | |
| 494 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 495 | MaybeObject* Accessors::FunctionGetLength(Object* object, void*) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 496 | bool found_it = false; |
| 497 | JSFunction* function = FindInPrototypeChain<JSFunction>(object, &found_it); |
| 498 | if (!found_it) return Smi::FromInt(0); |
| 499 | // Check if already compiled. |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 500 | if (!function->shared()->is_compiled()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 501 | // If the function isn't compiled yet, the length is not computed |
| 502 | // correctly yet. Compile it now and return the right length. |
| 503 | HandleScope scope; |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 504 | Handle<SharedFunctionInfo> shared(function->shared()); |
| 505 | if (!CompileLazyShared(shared, KEEP_EXCEPTION)) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 506 | return Failure::Exception(); |
| 507 | } |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 508 | return Smi::FromInt(shared->length()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 509 | } else { |
| 510 | return Smi::FromInt(function->shared()->length()); |
| 511 | } |
| 512 | } |
| 513 | |
| 514 | |
| 515 | const AccessorDescriptor Accessors::FunctionLength = { |
| 516 | FunctionGetLength, |
| 517 | ReadOnlySetAccessor, |
| 518 | 0 |
| 519 | }; |
| 520 | |
| 521 | |
| 522 | // |
| 523 | // Accessors::FunctionName |
| 524 | // |
| 525 | |
| 526 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 527 | MaybeObject* Accessors::FunctionGetName(Object* object, void*) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 528 | bool found_it = false; |
| 529 | JSFunction* holder = FindInPrototypeChain<JSFunction>(object, &found_it); |
| 530 | if (!found_it) return Heap::undefined_value(); |
| 531 | return holder->shared()->name(); |
| 532 | } |
| 533 | |
| 534 | |
| 535 | const AccessorDescriptor Accessors::FunctionName = { |
| 536 | FunctionGetName, |
| 537 | ReadOnlySetAccessor, |
| 538 | 0 |
| 539 | }; |
| 540 | |
| 541 | |
| 542 | // |
| 543 | // Accessors::FunctionArguments |
| 544 | // |
| 545 | |
| 546 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 547 | MaybeObject* Accessors::FunctionGetArguments(Object* object, void*) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 548 | HandleScope scope; |
| 549 | bool found_it = false; |
| 550 | JSFunction* holder = FindInPrototypeChain<JSFunction>(object, &found_it); |
| 551 | if (!found_it) return Heap::undefined_value(); |
| 552 | Handle<JSFunction> function(holder); |
| 553 | |
| 554 | // Find the top invocation of the function by traversing frames. |
| 555 | for (JavaScriptFrameIterator it; !it.done(); it.Advance()) { |
| 556 | // Skip all frames that aren't invocations of the given function. |
| 557 | JavaScriptFrame* frame = it.frame(); |
| 558 | if (frame->function() != *function) continue; |
| 559 | |
| 560 | // If there is an arguments variable in the stack, we return that. |
ager@chromium.org | b573749 | 2010-07-15 09:29:43 +0000 | [diff] [blame] | 561 | int index = function->shared()->scope_info()-> |
| 562 | StackSlotIndex(Heap::arguments_symbol()); |
ager@chromium.org | 3e87580 | 2009-06-29 08:26:34 +0000 | [diff] [blame] | 563 | if (index >= 0) { |
| 564 | Handle<Object> arguments = Handle<Object>(frame->GetExpression(index)); |
| 565 | if (!arguments->IsTheHole()) return *arguments; |
| 566 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 567 | |
| 568 | // If there isn't an arguments variable in the stack, we need to |
| 569 | // find the frame that holds the actual arguments passed to the |
| 570 | // function on the stack. |
| 571 | it.AdvanceToArgumentsFrame(); |
| 572 | frame = it.frame(); |
| 573 | |
| 574 | // Get the number of arguments and construct an arguments object |
| 575 | // mirror for the right frame. |
| 576 | const int length = frame->GetProvidedParametersCount(); |
| 577 | Handle<JSObject> arguments = Factory::NewArgumentsObject(function, length); |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 578 | Handle<FixedArray> array = Factory::NewFixedArray(length); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 579 | |
| 580 | // Copy the parameters to the arguments object. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 581 | ASSERT(array->length() == length); |
| 582 | for (int i = 0; i < length; i++) array->set(i, frame->GetParameter(i)); |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 583 | arguments->set_elements(*array); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 584 | |
| 585 | // Return the freshly allocated arguments object. |
| 586 | return *arguments; |
| 587 | } |
| 588 | |
| 589 | // No frame corresponding to the given function found. Return null. |
| 590 | return Heap::null_value(); |
| 591 | } |
| 592 | |
| 593 | |
| 594 | const AccessorDescriptor Accessors::FunctionArguments = { |
| 595 | FunctionGetArguments, |
| 596 | ReadOnlySetAccessor, |
| 597 | 0 |
| 598 | }; |
| 599 | |
| 600 | |
| 601 | // |
| 602 | // Accessors::FunctionCaller |
| 603 | // |
| 604 | |
| 605 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 606 | MaybeObject* Accessors::FunctionGetCaller(Object* object, void*) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 607 | HandleScope scope; |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 608 | AssertNoAllocation no_alloc; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 609 | bool found_it = false; |
| 610 | JSFunction* holder = FindInPrototypeChain<JSFunction>(object, &found_it); |
| 611 | if (!found_it) return Heap::undefined_value(); |
| 612 | Handle<JSFunction> function(holder); |
| 613 | |
| 614 | // Find the top invocation of the function by traversing frames. |
| 615 | for (JavaScriptFrameIterator it; !it.done(); it.Advance()) { |
| 616 | // Skip all frames that aren't invocations of the given function. |
| 617 | if (it.frame()->function() != *function) continue; |
| 618 | // Once we have found the frame, we need to go to the caller |
| 619 | // frame. This may require skipping through a number of top-level |
| 620 | // frames, e.g. frames for scripts not functions. |
| 621 | while (true) { |
| 622 | it.Advance(); |
| 623 | if (it.done()) return Heap::null_value(); |
| 624 | JSFunction* caller = JSFunction::cast(it.frame()->function()); |
| 625 | if (!caller->shared()->is_toplevel()) return caller; |
| 626 | } |
| 627 | } |
| 628 | |
| 629 | // No frame corresponding to the given function found. Return null. |
| 630 | return Heap::null_value(); |
| 631 | } |
| 632 | |
| 633 | |
| 634 | const AccessorDescriptor Accessors::FunctionCaller = { |
| 635 | FunctionGetCaller, |
| 636 | ReadOnlySetAccessor, |
| 637 | 0 |
| 638 | }; |
| 639 | |
| 640 | |
| 641 | // |
| 642 | // Accessors::ObjectPrototype |
| 643 | // |
| 644 | |
| 645 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 646 | MaybeObject* Accessors::ObjectGetPrototype(Object* receiver, void*) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 647 | Object* current = receiver->GetPrototype(); |
| 648 | while (current->IsJSObject() && |
| 649 | JSObject::cast(current)->map()->is_hidden_prototype()) { |
| 650 | current = current->GetPrototype(); |
| 651 | } |
| 652 | return current; |
| 653 | } |
| 654 | |
| 655 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 656 | MaybeObject* Accessors::ObjectSetPrototype(JSObject* receiver, |
| 657 | Object* value, |
| 658 | void*) { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 659 | const bool skip_hidden_prototypes = true; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 660 | // To be consistent with other Set functions, return the value. |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 661 | return receiver->SetPrototype(value, skip_hidden_prototypes); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 662 | } |
| 663 | |
| 664 | |
| 665 | const AccessorDescriptor Accessors::ObjectPrototype = { |
| 666 | ObjectGetPrototype, |
| 667 | ObjectSetPrototype, |
| 668 | 0 |
| 669 | }; |
| 670 | |
| 671 | } } // namespace v8::internal |