yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 1 | // Copyright 2012 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" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 29 | #include "accessors.h" |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 30 | |
| 31 | #include "contexts.h" |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 32 | #include "deoptimizer.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 33 | #include "execution.h" |
| 34 | #include "factory.h" |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 35 | #include "frames-inl.h" |
| 36 | #include "isolate.h" |
fschneider@chromium.org | fb144a0 | 2011-05-04 12:43:48 +0000 | [diff] [blame] | 37 | #include "list-inl.h" |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 38 | #include "property-details.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 39 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 40 | namespace v8 { |
| 41 | namespace internal { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 42 | |
| 43 | |
| 44 | template <class C> |
hpayer@chromium.org | 8432c91 | 2013-02-28 15:55:26 +0000 | [diff] [blame] | 45 | static C* FindInstanceOf(Isolate* isolate, Object* obj) { |
| 46 | for (Object* cur = obj; !cur->IsNull(); cur = cur->GetPrototype(isolate)) { |
verwaest@chromium.org | 33e09c8 | 2012-10-10 17:07:22 +0000 | [diff] [blame] | 47 | if (Is<C>(cur)) return C::cast(cur); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 48 | } |
verwaest@chromium.org | 33e09c8 | 2012-10-10 17:07:22 +0000 | [diff] [blame] | 49 | return NULL; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | |
| 53 | // Entry point that never should be called. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 54 | MaybeObject* Accessors::IllegalSetter(JSObject*, Object*, void*) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 55 | UNREACHABLE(); |
| 56 | return NULL; |
| 57 | } |
| 58 | |
| 59 | |
| 60 | Object* Accessors::IllegalGetAccessor(Object* object, void*) { |
| 61 | UNREACHABLE(); |
| 62 | return object; |
| 63 | } |
| 64 | |
| 65 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 66 | MaybeObject* Accessors::ReadOnlySetAccessor(JSObject*, Object* value, void*) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 67 | // According to ECMA-262, section 8.6.2.2, page 28, setting |
| 68 | // read-only properties must be silently ignored. |
| 69 | return value; |
| 70 | } |
| 71 | |
| 72 | |
| 73 | // |
| 74 | // Accessors::ArrayLength |
| 75 | // |
| 76 | |
| 77 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 78 | MaybeObject* Accessors::ArrayGetLength(Object* object, void*) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 79 | // Traverse the prototype chain until we reach an array. |
hpayer@chromium.org | 8432c91 | 2013-02-28 15:55:26 +0000 | [diff] [blame] | 80 | JSArray* holder = FindInstanceOf<JSArray>(Isolate::Current(), object); |
verwaest@chromium.org | 33e09c8 | 2012-10-10 17:07:22 +0000 | [diff] [blame] | 81 | return holder == NULL ? Smi::FromInt(0) : holder->length(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | |
| 85 | // The helper function will 'flatten' Number objects. |
| 86 | Object* Accessors::FlattenNumber(Object* value) { |
| 87 | if (value->IsNumber() || !value->IsJSValue()) return value; |
| 88 | JSValue* wrapper = JSValue::cast(value); |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 89 | ASSERT(Isolate::Current()->context()->native_context()->number_function()-> |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 90 | has_initial_map()); |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 91 | Map* number_map = Isolate::Current()->context()->native_context()-> |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 92 | number_function()->initial_map(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 93 | if (wrapper->map() == number_map) return wrapper->value(); |
| 94 | return value; |
| 95 | } |
| 96 | |
| 97 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 98 | MaybeObject* Accessors::ArraySetLength(JSObject* object, Object* value, void*) { |
lrn@chromium.org | 7516f05 | 2011-03-30 08:52:27 +0000 | [diff] [blame] | 99 | Isolate* isolate = object->GetIsolate(); |
lrn@chromium.org | ac2828d | 2011-06-23 06:29:21 +0000 | [diff] [blame] | 100 | |
| 101 | // This means one of the object's prototypes is a JSArray and the |
| 102 | // object does not have a 'length' property. Calling SetProperty |
| 103 | // causes an infinite loop. |
| 104 | if (!object->IsJSArray()) { |
| 105 | return object->SetLocalPropertyIgnoreAttributes( |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 106 | isolate->heap()->length_string(), value, NONE); |
lrn@chromium.org | ac2828d | 2011-06-23 06:29:21 +0000 | [diff] [blame] | 107 | } |
| 108 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 109 | value = FlattenNumber(value); |
| 110 | |
| 111 | // Need to call methods that may trigger GC. |
lrn@chromium.org | 7516f05 | 2011-03-30 08:52:27 +0000 | [diff] [blame] | 112 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 113 | |
| 114 | // Protect raw pointers. |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 115 | Handle<JSArray> array_handle(JSArray::cast(object), isolate); |
lrn@chromium.org | 7516f05 | 2011-03-30 08:52:27 +0000 | [diff] [blame] | 116 | Handle<Object> value_handle(value, isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 117 | |
| 118 | bool has_exception; |
| 119 | Handle<Object> uint32_v = Execution::ToUint32(value_handle, &has_exception); |
| 120 | if (has_exception) return Failure::Exception(); |
| 121 | Handle<Object> number_v = Execution::ToNumber(value_handle, &has_exception); |
| 122 | if (has_exception) return Failure::Exception(); |
| 123 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 124 | if (uint32_v->Number() == number_v->Number()) { |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 125 | return array_handle->SetElementsLength(*uint32_v); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 126 | } |
lrn@chromium.org | 7516f05 | 2011-03-30 08:52:27 +0000 | [diff] [blame] | 127 | return isolate->Throw( |
| 128 | *isolate->factory()->NewRangeError("invalid_array_length", |
| 129 | HandleVector<Object>(NULL, 0))); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | |
| 133 | const AccessorDescriptor Accessors::ArrayLength = { |
| 134 | ArrayGetLength, |
| 135 | ArraySetLength, |
| 136 | 0 |
| 137 | }; |
| 138 | |
| 139 | |
| 140 | // |
| 141 | // Accessors::StringLength |
| 142 | // |
| 143 | |
| 144 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 145 | MaybeObject* Accessors::StringGetLength(Object* object, void*) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 146 | Object* value = object; |
| 147 | if (object->IsJSValue()) value = JSValue::cast(object)->value(); |
| 148 | if (value->IsString()) return Smi::FromInt(String::cast(value)->length()); |
| 149 | // If object is not a string we return 0 to be compatible with WebKit. |
| 150 | // Note: Firefox returns the length of ToString(object). |
| 151 | return Smi::FromInt(0); |
| 152 | } |
| 153 | |
| 154 | |
| 155 | const AccessorDescriptor Accessors::StringLength = { |
| 156 | StringGetLength, |
| 157 | IllegalSetter, |
| 158 | 0 |
| 159 | }; |
| 160 | |
| 161 | |
| 162 | // |
| 163 | // Accessors::ScriptSource |
| 164 | // |
| 165 | |
| 166 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 167 | MaybeObject* Accessors::ScriptGetSource(Object* object, void*) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 168 | Object* script = JSValue::cast(object)->value(); |
| 169 | return Script::cast(script)->source(); |
| 170 | } |
| 171 | |
| 172 | |
| 173 | const AccessorDescriptor Accessors::ScriptSource = { |
| 174 | ScriptGetSource, |
| 175 | IllegalSetter, |
| 176 | 0 |
| 177 | }; |
| 178 | |
| 179 | |
| 180 | // |
| 181 | // Accessors::ScriptName |
| 182 | // |
| 183 | |
| 184 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 185 | MaybeObject* Accessors::ScriptGetName(Object* object, void*) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 186 | Object* script = JSValue::cast(object)->value(); |
| 187 | return Script::cast(script)->name(); |
| 188 | } |
| 189 | |
| 190 | |
| 191 | const AccessorDescriptor Accessors::ScriptName = { |
| 192 | ScriptGetName, |
| 193 | IllegalSetter, |
| 194 | 0 |
| 195 | }; |
| 196 | |
| 197 | |
| 198 | // |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 199 | // Accessors::ScriptId |
| 200 | // |
| 201 | |
| 202 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 203 | MaybeObject* Accessors::ScriptGetId(Object* object, void*) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 204 | Object* script = JSValue::cast(object)->value(); |
| 205 | return Script::cast(script)->id(); |
| 206 | } |
| 207 | |
| 208 | |
| 209 | const AccessorDescriptor Accessors::ScriptId = { |
| 210 | ScriptGetId, |
| 211 | IllegalSetter, |
| 212 | 0 |
| 213 | }; |
| 214 | |
| 215 | |
| 216 | // |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 217 | // Accessors::ScriptLineOffset |
| 218 | // |
| 219 | |
| 220 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 221 | MaybeObject* Accessors::ScriptGetLineOffset(Object* object, void*) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 222 | Object* script = JSValue::cast(object)->value(); |
| 223 | return Script::cast(script)->line_offset(); |
| 224 | } |
| 225 | |
| 226 | |
| 227 | const AccessorDescriptor Accessors::ScriptLineOffset = { |
| 228 | ScriptGetLineOffset, |
| 229 | IllegalSetter, |
| 230 | 0 |
| 231 | }; |
| 232 | |
| 233 | |
| 234 | // |
| 235 | // Accessors::ScriptColumnOffset |
| 236 | // |
| 237 | |
| 238 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 239 | MaybeObject* Accessors::ScriptGetColumnOffset(Object* object, void*) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 240 | Object* script = JSValue::cast(object)->value(); |
| 241 | return Script::cast(script)->column_offset(); |
| 242 | } |
| 243 | |
| 244 | |
| 245 | const AccessorDescriptor Accessors::ScriptColumnOffset = { |
| 246 | ScriptGetColumnOffset, |
| 247 | IllegalSetter, |
| 248 | 0 |
| 249 | }; |
| 250 | |
| 251 | |
| 252 | // |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 253 | // Accessors::ScriptData |
| 254 | // |
| 255 | |
| 256 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 257 | MaybeObject* Accessors::ScriptGetData(Object* object, void*) { |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 258 | Object* script = JSValue::cast(object)->value(); |
| 259 | return Script::cast(script)->data(); |
| 260 | } |
| 261 | |
| 262 | |
| 263 | const AccessorDescriptor Accessors::ScriptData = { |
| 264 | ScriptGetData, |
| 265 | IllegalSetter, |
| 266 | 0 |
| 267 | }; |
| 268 | |
| 269 | |
| 270 | // |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 271 | // Accessors::ScriptType |
| 272 | // |
| 273 | |
| 274 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 275 | MaybeObject* Accessors::ScriptGetType(Object* object, void*) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 276 | Object* script = JSValue::cast(object)->value(); |
| 277 | return Script::cast(script)->type(); |
| 278 | } |
| 279 | |
| 280 | |
| 281 | const AccessorDescriptor Accessors::ScriptType = { |
| 282 | ScriptGetType, |
| 283 | IllegalSetter, |
| 284 | 0 |
| 285 | }; |
| 286 | |
| 287 | |
| 288 | // |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 289 | // Accessors::ScriptCompilationType |
| 290 | // |
| 291 | |
| 292 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 293 | MaybeObject* Accessors::ScriptGetCompilationType(Object* object, void*) { |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 294 | Object* script = JSValue::cast(object)->value(); |
danno@chromium.org | d3c4210 | 2013-08-01 16:58:23 +0000 | [diff] [blame^] | 295 | return Smi::FromInt(Script::cast(script)->compilation_type()); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | |
| 299 | const AccessorDescriptor Accessors::ScriptCompilationType = { |
| 300 | ScriptGetCompilationType, |
| 301 | IllegalSetter, |
| 302 | 0 |
| 303 | }; |
| 304 | |
| 305 | |
| 306 | // |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 307 | // Accessors::ScriptGetLineEnds |
| 308 | // |
| 309 | |
| 310 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 311 | MaybeObject* Accessors::ScriptGetLineEnds(Object* object, void*) { |
lrn@chromium.org | 7516f05 | 2011-03-30 08:52:27 +0000 | [diff] [blame] | 312 | JSValue* wrapper = JSValue::cast(object); |
| 313 | Isolate* isolate = wrapper->GetIsolate(); |
| 314 | HandleScope scope(isolate); |
| 315 | Handle<Script> script(Script::cast(wrapper->value()), isolate); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 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())); |
erik.corry@gmail.com | 4a6c327 | 2010-11-18 12:04:40 +0000 | [diff] [blame] | 319 | // We do not want anyone to modify this array from JS. |
lrn@chromium.org | 7516f05 | 2011-03-30 08:52:27 +0000 | [diff] [blame] | 320 | ASSERT(*line_ends == isolate->heap()->empty_fixed_array() || |
| 321 | line_ends->map() == isolate->heap()->fixed_cow_array_map()); |
| 322 | Handle<JSArray> js_array = |
| 323 | isolate->factory()->NewJSArrayWithElements(line_ends); |
sgjesse@chromium.org | 499aaa5 | 2009-11-30 08:07:20 +0000 | [diff] [blame] | 324 | return *js_array; |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | |
| 328 | const AccessorDescriptor Accessors::ScriptLineEnds = { |
| 329 | ScriptGetLineEnds, |
| 330 | IllegalSetter, |
| 331 | 0 |
| 332 | }; |
| 333 | |
| 334 | |
| 335 | // |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 336 | // Accessors::ScriptGetContextData |
| 337 | // |
| 338 | |
| 339 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 340 | MaybeObject* Accessors::ScriptGetContextData(Object* object, void*) { |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 341 | Object* script = JSValue::cast(object)->value(); |
| 342 | return Script::cast(script)->context_data(); |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 343 | } |
| 344 | |
| 345 | |
| 346 | const AccessorDescriptor Accessors::ScriptContextData = { |
| 347 | ScriptGetContextData, |
| 348 | IllegalSetter, |
| 349 | 0 |
| 350 | }; |
| 351 | |
| 352 | |
| 353 | // |
sgjesse@chromium.org | 9818059 | 2009-12-02 08:17:28 +0000 | [diff] [blame] | 354 | // Accessors::ScriptGetEvalFromScript |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 355 | // |
| 356 | |
| 357 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 358 | MaybeObject* Accessors::ScriptGetEvalFromScript(Object* object, void*) { |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 359 | Object* script = JSValue::cast(object)->value(); |
sgjesse@chromium.org | 9818059 | 2009-12-02 08:17:28 +0000 | [diff] [blame] | 360 | if (!Script::cast(script)->eval_from_shared()->IsUndefined()) { |
| 361 | Handle<SharedFunctionInfo> eval_from_shared( |
| 362 | SharedFunctionInfo::cast(Script::cast(script)->eval_from_shared())); |
| 363 | |
| 364 | if (eval_from_shared->script()->IsScript()) { |
| 365 | Handle<Script> eval_from_script(Script::cast(eval_from_shared->script())); |
| 366 | return *GetScriptWrapper(eval_from_script); |
| 367 | } |
| 368 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 369 | return HEAP->undefined_value(); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | |
sgjesse@chromium.org | 9818059 | 2009-12-02 08:17:28 +0000 | [diff] [blame] | 373 | const AccessorDescriptor Accessors::ScriptEvalFromScript = { |
| 374 | ScriptGetEvalFromScript, |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 375 | IllegalSetter, |
| 376 | 0 |
| 377 | }; |
| 378 | |
| 379 | |
| 380 | // |
sgjesse@chromium.org | 9818059 | 2009-12-02 08:17:28 +0000 | [diff] [blame] | 381 | // Accessors::ScriptGetEvalFromScriptPosition |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 382 | // |
| 383 | |
| 384 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 385 | MaybeObject* Accessors::ScriptGetEvalFromScriptPosition(Object* object, void*) { |
yangguo@chromium.org | c03a192 | 2013-02-19 13:55:47 +0000 | [diff] [blame] | 386 | Script* raw_script = Script::cast(JSValue::cast(object)->value()); |
| 387 | HandleScope scope(raw_script->GetIsolate()); |
| 388 | Handle<Script> script(raw_script); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 389 | |
| 390 | // If this is not a script compiled through eval there is no eval position. |
danno@chromium.org | d3c4210 | 2013-08-01 16:58:23 +0000 | [diff] [blame^] | 391 | if (script->compilation_type() != Script::COMPILATION_TYPE_EVAL) { |
yangguo@chromium.org | c03a192 | 2013-02-19 13:55:47 +0000 | [diff] [blame] | 392 | return script->GetHeap()->undefined_value(); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 393 | } |
| 394 | |
| 395 | // Get the function from where eval was called and find the source position |
| 396 | // from the instruction offset. |
sgjesse@chromium.org | 9818059 | 2009-12-02 08:17:28 +0000 | [diff] [blame] | 397 | Handle<Code> code(SharedFunctionInfo::cast( |
| 398 | script->eval_from_shared())->code()); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 399 | return Smi::FromInt(code->SourcePosition(code->instruction_start() + |
| 400 | script->eval_from_instructions_offset()->value())); |
| 401 | } |
| 402 | |
| 403 | |
sgjesse@chromium.org | 9818059 | 2009-12-02 08:17:28 +0000 | [diff] [blame] | 404 | const AccessorDescriptor Accessors::ScriptEvalFromScriptPosition = { |
| 405 | ScriptGetEvalFromScriptPosition, |
| 406 | IllegalSetter, |
| 407 | 0 |
| 408 | }; |
| 409 | |
| 410 | |
| 411 | // |
| 412 | // Accessors::ScriptGetEvalFromFunctionName |
| 413 | // |
| 414 | |
| 415 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 416 | MaybeObject* Accessors::ScriptGetEvalFromFunctionName(Object* object, void*) { |
sgjesse@chromium.org | 9818059 | 2009-12-02 08:17:28 +0000 | [diff] [blame] | 417 | Object* script = JSValue::cast(object)->value(); |
| 418 | Handle<SharedFunctionInfo> shared(SharedFunctionInfo::cast( |
| 419 | Script::cast(script)->eval_from_shared())); |
| 420 | |
| 421 | |
| 422 | // Find the name of the function calling eval. |
| 423 | if (!shared->name()->IsUndefined()) { |
| 424 | return shared->name(); |
| 425 | } else { |
| 426 | return shared->inferred_name(); |
| 427 | } |
| 428 | } |
| 429 | |
| 430 | |
| 431 | const AccessorDescriptor Accessors::ScriptEvalFromFunctionName = { |
| 432 | ScriptGetEvalFromFunctionName, |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 433 | IllegalSetter, |
| 434 | 0 |
| 435 | }; |
| 436 | |
| 437 | |
| 438 | // |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 439 | // Accessors::FunctionPrototype |
| 440 | // |
| 441 | |
| 442 | |
ulan@chromium.org | 77ca49a | 2013-04-22 09:43:56 +0000 | [diff] [blame] | 443 | Handle<Object> Accessors::FunctionGetPrototype(Handle<Object> object) { |
| 444 | Isolate* isolate = Isolate::Current(); |
| 445 | CALL_HEAP_FUNCTION( |
| 446 | isolate, Accessors::FunctionGetPrototype(*object, 0), Object); |
| 447 | } |
| 448 | |
| 449 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 450 | MaybeObject* Accessors::FunctionGetPrototype(Object* object, void*) { |
hpayer@chromium.org | 8432c91 | 2013-02-28 15:55:26 +0000 | [diff] [blame] | 451 | Isolate* isolate = Isolate::Current(); |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 452 | JSFunction* function_raw = FindInstanceOf<JSFunction>(isolate, object); |
| 453 | if (function_raw == NULL) return isolate->heap()->undefined_value(); |
| 454 | while (!function_raw->should_have_prototype()) { |
| 455 | function_raw = FindInstanceOf<JSFunction>(isolate, |
| 456 | function_raw->GetPrototype()); |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 457 | // There has to be one because we hit the getter. |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 458 | ASSERT(function_raw != NULL); |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 459 | } |
| 460 | |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 461 | if (!function_raw->has_prototype()) { |
| 462 | HandleScope scope(isolate); |
| 463 | Handle<JSFunction> function(function_raw); |
| 464 | Handle<Object> proto = isolate->factory()->NewFunctionPrototype(function); |
| 465 | JSFunction::SetPrototype(function, proto); |
| 466 | function_raw = *function; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 467 | } |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 468 | return function_raw->prototype(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 469 | } |
| 470 | |
| 471 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 472 | MaybeObject* Accessors::FunctionSetPrototype(JSObject* object, |
jkummerow@chromium.org | 5323a9c | 2012-12-10 19:00:50 +0000 | [diff] [blame] | 473 | Object* value_raw, |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 474 | void*) { |
jkummerow@chromium.org | 5323a9c | 2012-12-10 19:00:50 +0000 | [diff] [blame] | 475 | Isolate* isolate = object->GetIsolate(); |
| 476 | Heap* heap = isolate->heap(); |
hpayer@chromium.org | 8432c91 | 2013-02-28 15:55:26 +0000 | [diff] [blame] | 477 | JSFunction* function_raw = FindInstanceOf<JSFunction>(isolate, object); |
jkummerow@chromium.org | 5323a9c | 2012-12-10 19:00:50 +0000 | [diff] [blame] | 478 | if (function_raw == NULL) return heap->undefined_value(); |
| 479 | if (!function_raw->should_have_prototype()) { |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 480 | // Since we hit this accessor, object will have no prototype property. |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 481 | return object->SetLocalPropertyIgnoreAttributes(heap->prototype_string(), |
jkummerow@chromium.org | 5323a9c | 2012-12-10 19:00:50 +0000 | [diff] [blame] | 482 | value_raw, |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 483 | NONE); |
| 484 | } |
| 485 | |
jkummerow@chromium.org | 5323a9c | 2012-12-10 19:00:50 +0000 | [diff] [blame] | 486 | HandleScope scope(isolate); |
| 487 | Handle<JSFunction> function(function_raw, isolate); |
| 488 | Handle<Object> value(value_raw, isolate); |
| 489 | |
| 490 | Handle<Object> old_value; |
| 491 | bool is_observed = |
| 492 | FLAG_harmony_observation && |
| 493 | *function == object && |
| 494 | function->map()->is_observed(); |
| 495 | if (is_observed) { |
| 496 | if (function->has_prototype()) |
| 497 | old_value = handle(function->prototype(), isolate); |
| 498 | else |
| 499 | old_value = isolate->factory()->NewFunctionPrototype(function); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 500 | } |
jkummerow@chromium.org | 5323a9c | 2012-12-10 19:00:50 +0000 | [diff] [blame] | 501 | |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 502 | JSFunction::SetPrototype(function, value); |
jkummerow@chromium.org | 5323a9c | 2012-12-10 19:00:50 +0000 | [diff] [blame] | 503 | ASSERT(function->prototype() == *value); |
| 504 | |
| 505 | if (is_observed && !old_value->SameValue(*value)) { |
| 506 | JSObject::EnqueueChangeRecord( |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 507 | function, "updated", isolate->factory()->prototype_string(), old_value); |
jkummerow@chromium.org | 5323a9c | 2012-12-10 19:00:50 +0000 | [diff] [blame] | 508 | } |
| 509 | |
| 510 | return *function; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 511 | } |
| 512 | |
| 513 | |
| 514 | const AccessorDescriptor Accessors::FunctionPrototype = { |
| 515 | FunctionGetPrototype, |
| 516 | FunctionSetPrototype, |
| 517 | 0 |
| 518 | }; |
| 519 | |
| 520 | |
| 521 | // |
| 522 | // Accessors::FunctionLength |
| 523 | // |
| 524 | |
| 525 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 526 | MaybeObject* Accessors::FunctionGetLength(Object* object, void*) { |
hpayer@chromium.org | 8432c91 | 2013-02-28 15:55:26 +0000 | [diff] [blame] | 527 | Isolate* isolate = Isolate::Current(); |
| 528 | JSFunction* function = FindInstanceOf<JSFunction>(isolate, object); |
verwaest@chromium.org | 33e09c8 | 2012-10-10 17:07:22 +0000 | [diff] [blame] | 529 | if (function == NULL) return Smi::FromInt(0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 530 | // Check if already compiled. |
verwaest@chromium.org | 33e09c8 | 2012-10-10 17:07:22 +0000 | [diff] [blame] | 531 | if (function->shared()->is_compiled()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 532 | return Smi::FromInt(function->shared()->length()); |
| 533 | } |
verwaest@chromium.org | 33e09c8 | 2012-10-10 17:07:22 +0000 | [diff] [blame] | 534 | // If the function isn't compiled yet, the length is not computed correctly |
| 535 | // yet. Compile it now and return the right length. |
hpayer@chromium.org | 8432c91 | 2013-02-28 15:55:26 +0000 | [diff] [blame] | 536 | HandleScope scope(isolate); |
verwaest@chromium.org | 33e09c8 | 2012-10-10 17:07:22 +0000 | [diff] [blame] | 537 | Handle<JSFunction> handle(function); |
| 538 | if (JSFunction::CompileLazy(handle, KEEP_EXCEPTION)) { |
| 539 | return Smi::FromInt(handle->shared()->length()); |
| 540 | } |
| 541 | return Failure::Exception(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 542 | } |
| 543 | |
| 544 | |
| 545 | const AccessorDescriptor Accessors::FunctionLength = { |
| 546 | FunctionGetLength, |
| 547 | ReadOnlySetAccessor, |
| 548 | 0 |
| 549 | }; |
| 550 | |
| 551 | |
| 552 | // |
| 553 | // Accessors::FunctionName |
| 554 | // |
| 555 | |
| 556 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 557 | MaybeObject* Accessors::FunctionGetName(Object* object, void*) { |
hpayer@chromium.org | 8432c91 | 2013-02-28 15:55:26 +0000 | [diff] [blame] | 558 | Isolate* isolate = Isolate::Current(); |
| 559 | JSFunction* holder = FindInstanceOf<JSFunction>(isolate, object); |
| 560 | return holder == NULL |
| 561 | ? isolate->heap()->undefined_value() |
| 562 | : holder->shared()->name(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 563 | } |
| 564 | |
| 565 | |
| 566 | const AccessorDescriptor Accessors::FunctionName = { |
| 567 | FunctionGetName, |
| 568 | ReadOnlySetAccessor, |
| 569 | 0 |
| 570 | }; |
| 571 | |
| 572 | |
| 573 | // |
| 574 | // Accessors::FunctionArguments |
| 575 | // |
| 576 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 577 | |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 578 | Handle<Object> Accessors::FunctionGetArguments(Handle<Object> object) { |
| 579 | Isolate* isolate = Isolate::Current(); |
| 580 | CALL_HEAP_FUNCTION( |
| 581 | isolate, Accessors::FunctionGetArguments(*object, 0), Object); |
| 582 | } |
| 583 | |
| 584 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 585 | static MaybeObject* ConstructArgumentsObjectForInlinedFunction( |
| 586 | JavaScriptFrame* frame, |
| 587 | Handle<JSFunction> inlined_function, |
| 588 | int inlined_frame_index) { |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 589 | Isolate* isolate = inlined_function->GetIsolate(); |
| 590 | Factory* factory = isolate->factory(); |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 591 | Vector<SlotRef> args_slots = |
| 592 | SlotRef::ComputeSlotMappingForArguments( |
| 593 | frame, |
| 594 | inlined_frame_index, |
| 595 | inlined_function->shared()->formal_parameter_count()); |
| 596 | int args_count = args_slots.length(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 597 | Handle<JSObject> arguments = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 598 | factory->NewArgumentsObject(inlined_function, args_count); |
| 599 | Handle<FixedArray> array = factory->NewFixedArray(args_count); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 600 | for (int i = 0; i < args_count; ++i) { |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 601 | Handle<Object> value = args_slots[i].GetValue(isolate); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 602 | array->set(i, *value); |
| 603 | } |
| 604 | arguments->set_elements(*array); |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 605 | args_slots.Dispose(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 606 | |
| 607 | // Return the freshly allocated arguments object. |
| 608 | return *arguments; |
| 609 | } |
| 610 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 611 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 612 | MaybeObject* Accessors::FunctionGetArguments(Object* object, void*) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 613 | Isolate* isolate = Isolate::Current(); |
| 614 | HandleScope scope(isolate); |
hpayer@chromium.org | 8432c91 | 2013-02-28 15:55:26 +0000 | [diff] [blame] | 615 | JSFunction* holder = FindInstanceOf<JSFunction>(isolate, object); |
verwaest@chromium.org | 33e09c8 | 2012-10-10 17:07:22 +0000 | [diff] [blame] | 616 | if (holder == NULL) return isolate->heap()->undefined_value(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 617 | Handle<JSFunction> function(holder, isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 618 | |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame] | 619 | if (function->shared()->native()) return isolate->heap()->null_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 620 | // Find the top invocation of the function by traversing frames. |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 621 | List<JSFunction*> functions(2); |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 622 | for (JavaScriptFrameIterator it(isolate); !it.done(); it.Advance()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 623 | JavaScriptFrame* frame = it.frame(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 624 | frame->GetFunctions(&functions); |
| 625 | for (int i = functions.length() - 1; i >= 0; i--) { |
| 626 | // Skip all frames that aren't invocations of the given function. |
| 627 | if (functions[i] != *function) continue; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 628 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 629 | if (i > 0) { |
kmillikin@chromium.org | 31b1277 | 2011-02-02 16:08:26 +0000 | [diff] [blame] | 630 | // The function in question was inlined. Inlined functions have the |
| 631 | // correct number of arguments and no allocated arguments object, so |
| 632 | // we can construct a fresh one by interpreting the function's |
| 633 | // deoptimization input data. |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 634 | return ConstructArgumentsObjectForInlinedFunction(frame, function, i); |
kmillikin@chromium.org | 31b1277 | 2011-02-02 16:08:26 +0000 | [diff] [blame] | 635 | } |
| 636 | |
| 637 | if (!frame->is_optimized()) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 638 | // If there is an arguments variable in the stack, we return that. |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 639 | Handle<ScopeInfo> scope_info(function->shared()->scope_info()); |
| 640 | int index = scope_info->StackSlotIndex( |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 641 | isolate->heap()->arguments_string()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 642 | if (index >= 0) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 643 | Handle<Object> arguments(frame->GetExpression(index), isolate); |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 644 | if (!arguments->IsArgumentsMarker()) return *arguments; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 645 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 646 | } |
kmillikin@chromium.org | 31b1277 | 2011-02-02 16:08:26 +0000 | [diff] [blame] | 647 | |
| 648 | // If there is no arguments variable in the stack or we have an |
| 649 | // optimized frame, we find the frame that holds the actual arguments |
| 650 | // passed to the function. |
| 651 | it.AdvanceToArgumentsFrame(); |
| 652 | frame = it.frame(); |
| 653 | |
| 654 | // Get the number of arguments and construct an arguments object |
| 655 | // mirror for the right frame. |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 656 | const int length = frame->ComputeParametersCount(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 657 | Handle<JSObject> arguments = isolate->factory()->NewArgumentsObject( |
| 658 | function, length); |
| 659 | Handle<FixedArray> array = isolate->factory()->NewFixedArray(length); |
kmillikin@chromium.org | 31b1277 | 2011-02-02 16:08:26 +0000 | [diff] [blame] | 660 | |
| 661 | // Copy the parameters to the arguments object. |
| 662 | ASSERT(array->length() == length); |
| 663 | for (int i = 0; i < length; i++) array->set(i, frame->GetParameter(i)); |
| 664 | arguments->set_elements(*array); |
| 665 | |
| 666 | // Return the freshly allocated arguments object. |
| 667 | return *arguments; |
ager@chromium.org | 3e87580 | 2009-06-29 08:26:34 +0000 | [diff] [blame] | 668 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 669 | functions.Rewind(0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 670 | } |
| 671 | |
| 672 | // No frame corresponding to the given function found. Return null. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 673 | return isolate->heap()->null_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 674 | } |
| 675 | |
| 676 | |
| 677 | const AccessorDescriptor Accessors::FunctionArguments = { |
| 678 | FunctionGetArguments, |
| 679 | ReadOnlySetAccessor, |
| 680 | 0 |
| 681 | }; |
| 682 | |
| 683 | |
| 684 | // |
| 685 | // Accessors::FunctionCaller |
| 686 | // |
| 687 | |
| 688 | |
ricow@chromium.org | 4668a2c | 2011-08-29 10:41:00 +0000 | [diff] [blame] | 689 | class FrameFunctionIterator { |
| 690 | public: |
rossberg@chromium.org | 79e7902 | 2013-06-03 15:43:46 +0000 | [diff] [blame] | 691 | FrameFunctionIterator(Isolate* isolate, const DisallowHeapAllocation& promise) |
ricow@chromium.org | 4668a2c | 2011-08-29 10:41:00 +0000 | [diff] [blame] | 692 | : frame_iterator_(isolate), |
| 693 | functions_(2), |
| 694 | index_(0) { |
| 695 | GetFunctions(); |
| 696 | } |
| 697 | JSFunction* next() { |
| 698 | if (functions_.length() == 0) return NULL; |
| 699 | JSFunction* next_function = functions_[index_]; |
| 700 | index_--; |
| 701 | if (index_ < 0) { |
| 702 | GetFunctions(); |
| 703 | } |
| 704 | return next_function; |
| 705 | } |
| 706 | |
| 707 | // Iterate through functions until the first occurence of 'function'. |
| 708 | // Returns true if 'function' is found, and false if the iterator ends |
| 709 | // without finding it. |
| 710 | bool Find(JSFunction* function) { |
| 711 | JSFunction* next_function; |
| 712 | do { |
| 713 | next_function = next(); |
| 714 | if (next_function == function) return true; |
| 715 | } while (next_function != NULL); |
| 716 | return false; |
| 717 | } |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 718 | |
ricow@chromium.org | 4668a2c | 2011-08-29 10:41:00 +0000 | [diff] [blame] | 719 | private: |
| 720 | void GetFunctions() { |
| 721 | functions_.Rewind(0); |
| 722 | if (frame_iterator_.done()) return; |
| 723 | JavaScriptFrame* frame = frame_iterator_.frame(); |
| 724 | frame->GetFunctions(&functions_); |
| 725 | ASSERT(functions_.length() > 0); |
| 726 | frame_iterator_.Advance(); |
| 727 | index_ = functions_.length() - 1; |
| 728 | } |
| 729 | JavaScriptFrameIterator frame_iterator_; |
| 730 | List<JSFunction*> functions_; |
| 731 | int index_; |
| 732 | }; |
| 733 | |
| 734 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 735 | MaybeObject* Accessors::FunctionGetCaller(Object* object, void*) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 736 | Isolate* isolate = Isolate::Current(); |
| 737 | HandleScope scope(isolate); |
rossberg@chromium.org | 79e7902 | 2013-06-03 15:43:46 +0000 | [diff] [blame] | 738 | DisallowHeapAllocation no_allocation; |
hpayer@chromium.org | 8432c91 | 2013-02-28 15:55:26 +0000 | [diff] [blame] | 739 | JSFunction* holder = FindInstanceOf<JSFunction>(isolate, object); |
verwaest@chromium.org | 33e09c8 | 2012-10-10 17:07:22 +0000 | [diff] [blame] | 740 | if (holder == NULL) return isolate->heap()->undefined_value(); |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame] | 741 | if (holder->shared()->native()) return isolate->heap()->null_value(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 742 | Handle<JSFunction> function(holder, isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 743 | |
rossberg@chromium.org | 79e7902 | 2013-06-03 15:43:46 +0000 | [diff] [blame] | 744 | FrameFunctionIterator it(isolate, no_allocation); |
ricow@chromium.org | 4668a2c | 2011-08-29 10:41:00 +0000 | [diff] [blame] | 745 | |
| 746 | // Find the function from the frames. |
| 747 | if (!it.Find(*function)) { |
| 748 | // No frame corresponding to the given function found. Return null. |
| 749 | return isolate->heap()->null_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 750 | } |
| 751 | |
ricow@chromium.org | 4668a2c | 2011-08-29 10:41:00 +0000 | [diff] [blame] | 752 | // Find previously called non-toplevel function. |
| 753 | JSFunction* caller; |
| 754 | do { |
| 755 | caller = it.next(); |
| 756 | if (caller == NULL) return isolate->heap()->null_value(); |
| 757 | } while (caller->shared()->is_toplevel()); |
| 758 | |
| 759 | // If caller is a built-in function and caller's caller is also built-in, |
| 760 | // use that instead. |
| 761 | JSFunction* potential_caller = caller; |
| 762 | while (potential_caller != NULL && potential_caller->IsBuiltin()) { |
| 763 | caller = potential_caller; |
| 764 | potential_caller = it.next(); |
| 765 | } |
jkummerow@chromium.org | 78502a9 | 2012-09-06 13:50:42 +0000 | [diff] [blame] | 766 | if (!caller->shared()->native() && potential_caller != NULL) { |
| 767 | caller = potential_caller; |
| 768 | } |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 769 | // If caller is bound, return null. This is compatible with JSC, and |
| 770 | // allows us to make bound functions use the strict function map |
| 771 | // and its associated throwing caller and arguments. |
| 772 | if (caller->shared()->bound()) { |
| 773 | return isolate->heap()->null_value(); |
| 774 | } |
danno@chromium.org | 1f34ad3 | 2012-11-26 14:53:56 +0000 | [diff] [blame] | 775 | // Censor if the caller is not a classic mode function. |
| 776 | // Change from ES5, which used to throw, see: |
| 777 | // https://bugs.ecmascript.org/show_bug.cgi?id=310 |
| 778 | if (!caller->shared()->is_classic_mode()) { |
| 779 | return isolate->heap()->null_value(); |
| 780 | } |
| 781 | |
| 782 | return caller; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 783 | } |
| 784 | |
| 785 | |
| 786 | const AccessorDescriptor Accessors::FunctionCaller = { |
| 787 | FunctionGetCaller, |
| 788 | ReadOnlySetAccessor, |
| 789 | 0 |
| 790 | }; |
| 791 | |
| 792 | |
| 793 | // |
danno@chromium.org | 81cac2b | 2012-07-10 11:28:27 +0000 | [diff] [blame] | 794 | // Accessors::MakeModuleExport |
| 795 | // |
| 796 | |
verwaest@chromium.org | d4be0f0 | 2013-06-05 13:39:03 +0000 | [diff] [blame] | 797 | static void ModuleGetExport( |
danno@chromium.org | 81cac2b | 2012-07-10 11:28:27 +0000 | [diff] [blame] | 798 | v8::Local<v8::String> property, |
verwaest@chromium.org | d4be0f0 | 2013-06-05 13:39:03 +0000 | [diff] [blame] | 799 | const v8::PropertyCallbackInfo<v8::Value>& info) { |
danno@chromium.org | 81cac2b | 2012-07-10 11:28:27 +0000 | [diff] [blame] | 800 | JSModule* instance = JSModule::cast(*v8::Utils::OpenHandle(*info.Holder())); |
| 801 | Context* context = Context::cast(instance->context()); |
| 802 | ASSERT(context->IsModuleContext()); |
| 803 | int slot = info.Data()->Int32Value(); |
| 804 | Object* value = context->get(slot); |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 805 | Isolate* isolate = instance->GetIsolate(); |
danno@chromium.org | 81cac2b | 2012-07-10 11:28:27 +0000 | [diff] [blame] | 806 | if (value->IsTheHole()) { |
| 807 | Handle<String> name = v8::Utils::OpenHandle(*property); |
danno@chromium.org | 81cac2b | 2012-07-10 11:28:27 +0000 | [diff] [blame] | 808 | isolate->ScheduleThrow( |
| 809 | *isolate->factory()->NewReferenceError("not_defined", |
| 810 | HandleVector(&name, 1))); |
verwaest@chromium.org | d4be0f0 | 2013-06-05 13:39:03 +0000 | [diff] [blame] | 811 | return; |
danno@chromium.org | 81cac2b | 2012-07-10 11:28:27 +0000 | [diff] [blame] | 812 | } |
verwaest@chromium.org | d4be0f0 | 2013-06-05 13:39:03 +0000 | [diff] [blame] | 813 | info.GetReturnValue().Set(v8::Utils::ToLocal(Handle<Object>(value, isolate))); |
danno@chromium.org | 81cac2b | 2012-07-10 11:28:27 +0000 | [diff] [blame] | 814 | } |
| 815 | |
| 816 | |
| 817 | static void ModuleSetExport( |
| 818 | v8::Local<v8::String> property, |
| 819 | v8::Local<v8::Value> value, |
verwaest@chromium.org | d4be0f0 | 2013-06-05 13:39:03 +0000 | [diff] [blame] | 820 | const v8::PropertyCallbackInfo<v8::Value>& info) { |
danno@chromium.org | 81cac2b | 2012-07-10 11:28:27 +0000 | [diff] [blame] | 821 | JSModule* instance = JSModule::cast(*v8::Utils::OpenHandle(*info.Holder())); |
| 822 | Context* context = Context::cast(instance->context()); |
| 823 | ASSERT(context->IsModuleContext()); |
| 824 | int slot = info.Data()->Int32Value(); |
| 825 | Object* old_value = context->get(slot); |
| 826 | if (old_value->IsTheHole()) { |
| 827 | Handle<String> name = v8::Utils::OpenHandle(*property); |
| 828 | Isolate* isolate = instance->GetIsolate(); |
| 829 | isolate->ScheduleThrow( |
| 830 | *isolate->factory()->NewReferenceError("not_defined", |
| 831 | HandleVector(&name, 1))); |
| 832 | return; |
| 833 | } |
| 834 | context->set(slot, *v8::Utils::OpenHandle(*value)); |
| 835 | } |
| 836 | |
| 837 | |
| 838 | Handle<AccessorInfo> Accessors::MakeModuleExport( |
| 839 | Handle<String> name, |
| 840 | int index, |
| 841 | PropertyAttributes attributes) { |
| 842 | Factory* factory = name->GetIsolate()->factory(); |
hpayer@chromium.org | 7c3372b | 2013-02-13 17:26:04 +0000 | [diff] [blame] | 843 | Handle<ExecutableAccessorInfo> info = factory->NewExecutableAccessorInfo(); |
danno@chromium.org | 81cac2b | 2012-07-10 11:28:27 +0000 | [diff] [blame] | 844 | info->set_property_attributes(attributes); |
| 845 | info->set_all_can_read(true); |
| 846 | info->set_all_can_write(true); |
| 847 | info->set_name(*name); |
| 848 | info->set_data(Smi::FromInt(index)); |
| 849 | Handle<Object> getter = v8::FromCData(&ModuleGetExport); |
| 850 | Handle<Object> setter = v8::FromCData(&ModuleSetExport); |
| 851 | info->set_getter(*getter); |
| 852 | if (!(attributes & ReadOnly)) info->set_setter(*setter); |
| 853 | return info; |
| 854 | } |
| 855 | |
| 856 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 857 | } } // namespace v8::internal |