Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1 | // Copyright 2012 the V8 project authors. All rights reserved. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5 | #include "src/accessors.h" |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7 | #include "src/api.h" |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 8 | #include "src/contexts.h" |
| 9 | #include "src/deoptimizer.h" |
| 10 | #include "src/execution.h" |
| 11 | #include "src/factory.h" |
| 12 | #include "src/frames-inl.h" |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 13 | #include "src/isolate-inl.h" |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14 | #include "src/list-inl.h" |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 15 | #include "src/messages.h" |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 16 | #include "src/property-details.h" |
| 17 | #include "src/prototype.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 18 | |
| 19 | namespace v8 { |
| 20 | namespace internal { |
| 21 | |
| 22 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 23 | Handle<AccessorInfo> Accessors::MakeAccessor( |
| 24 | Isolate* isolate, |
| 25 | Handle<Name> name, |
| 26 | AccessorNameGetterCallback getter, |
| 27 | AccessorNameSetterCallback setter, |
| 28 | PropertyAttributes attributes) { |
| 29 | Factory* factory = isolate->factory(); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 30 | Handle<AccessorInfo> info = factory->NewAccessorInfo(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 31 | info->set_property_attributes(attributes); |
| 32 | info->set_all_can_read(false); |
| 33 | info->set_all_can_write(false); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 34 | info->set_is_special_data_property(true); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 35 | name = factory->InternalizeName(name); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 36 | info->set_name(*name); |
| 37 | Handle<Object> get = v8::FromCData(isolate, getter); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 38 | if (setter == nullptr) setter = &ReconfigureToDataProperty; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 39 | Handle<Object> set = v8::FromCData(isolate, setter); |
| 40 | info->set_getter(*get); |
| 41 | info->set_setter(*set); |
| 42 | return info; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 46 | static V8_INLINE bool CheckForName(Handle<Name> name, |
| 47 | Handle<String> property_name, |
| 48 | int offset, |
| 49 | int* object_offset) { |
| 50 | if (Name::Equals(name, property_name)) { |
| 51 | *object_offset = offset; |
| 52 | return true; |
| 53 | } |
| 54 | return false; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 58 | // Returns true for properties that are accessors to object fields. |
| 59 | // If true, *object_offset contains offset of object field. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 60 | bool Accessors::IsJSObjectFieldAccessor(Handle<Map> map, Handle<Name> name, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 61 | int* object_offset) { |
| 62 | Isolate* isolate = name->GetIsolate(); |
| 63 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 64 | switch (map->instance_type()) { |
| 65 | case JS_ARRAY_TYPE: |
| 66 | return |
| 67 | CheckForName(name, isolate->factory()->length_string(), |
| 68 | JSArray::kLengthOffset, object_offset); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 69 | case JS_ARRAY_BUFFER_TYPE: |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 70 | return CheckForName(name, isolate->factory()->byte_length_string(), |
| 71 | JSArrayBuffer::kByteLengthOffset, object_offset); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 72 | default: |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 73 | if (map->instance_type() < FIRST_NONSTRING_TYPE) { |
| 74 | return CheckForName(name, isolate->factory()->length_string(), |
| 75 | String::kLengthOffset, object_offset); |
| 76 | } |
| 77 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 78 | return false; |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 83 | bool Accessors::IsJSArrayBufferViewFieldAccessor(Handle<Map> map, |
| 84 | Handle<Name> name, |
| 85 | int* object_offset) { |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 86 | DCHECK(name->IsUniqueName()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 87 | Isolate* isolate = name->GetIsolate(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 88 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 89 | switch (map->instance_type()) { |
| 90 | case JS_TYPED_ARRAY_TYPE: { |
| 91 | if (!CheckForName(name, isolate->factory()->length_string(), |
| 92 | JSTypedArray::kLengthOffset, object_offset) && |
| 93 | !CheckForName(name, isolate->factory()->byte_length_string(), |
| 94 | JSTypedArray::kByteLengthOffset, object_offset) && |
| 95 | !CheckForName(name, isolate->factory()->byte_offset_string(), |
| 96 | JSTypedArray::kByteOffsetOffset, object_offset)) { |
| 97 | return false; |
| 98 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 99 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 100 | if (map->is_dictionary_map()) return false; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 101 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 102 | // Check if the property is overridden on the instance. |
| 103 | DescriptorArray* descriptors = map->instance_descriptors(); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 104 | int descriptor = descriptors->SearchWithCache(isolate, *name, *map); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 105 | if (descriptor != DescriptorArray::kNotFound) return false; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 106 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 107 | Handle<Object> proto = Handle<Object>(map->prototype(), isolate); |
| 108 | if (!proto->IsJSReceiver()) return false; |
| 109 | |
| 110 | // Check if the property is defined in the prototype chain. |
| 111 | LookupIterator it(proto, name); |
| 112 | if (!it.IsFound()) return false; |
| 113 | |
| 114 | Object* original_proto = |
| 115 | JSFunction::cast(map->GetConstructor())->prototype(); |
| 116 | |
| 117 | // Property is not configurable. It is enough to verify that |
| 118 | // the holder is the same. |
| 119 | return *it.GetHolder<Object>() == original_proto; |
| 120 | } |
| 121 | case JS_DATA_VIEW_TYPE: |
| 122 | return CheckForName(name, isolate->factory()->byte_length_string(), |
| 123 | JSDataView::kByteLengthOffset, object_offset) || |
| 124 | CheckForName(name, isolate->factory()->byte_offset_string(), |
| 125 | JSDataView::kByteOffsetOffset, object_offset); |
| 126 | default: |
| 127 | return false; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 128 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 129 | } |
| 130 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 131 | MUST_USE_RESULT static MaybeHandle<Object> ReplaceAccessorWithDataProperty( |
| 132 | Isolate* isolate, Handle<JSObject> receiver, Handle<JSObject> holder, |
| 133 | Handle<Name> name, Handle<Object> value, bool observe) { |
| 134 | LookupIterator it(receiver, name, holder, |
| 135 | LookupIterator::OWN_SKIP_INTERCEPTOR); |
| 136 | // Skip any access checks we might hit. This accessor should never hit in a |
| 137 | // situation where the caller does not have access. |
| 138 | if (it.state() == LookupIterator::ACCESS_CHECK) { |
| 139 | CHECK(it.HasAccess()); |
| 140 | it.Next(); |
| 141 | } |
| 142 | CHECK_EQ(LookupIterator::ACCESSOR, it.state()); |
| 143 | |
| 144 | Handle<Object> old_value; |
| 145 | bool is_observed = observe && receiver->map()->is_observed(); |
| 146 | if (is_observed) { |
| 147 | MaybeHandle<Object> maybe_old = Object::GetPropertyWithAccessor(&it); |
| 148 | if (!maybe_old.ToHandle(&old_value)) return maybe_old; |
| 149 | } |
| 150 | |
| 151 | it.ReconfigureDataProperty(value, it.property_attributes()); |
| 152 | |
| 153 | if (is_observed && !old_value->SameValue(*value)) { |
| 154 | return JSObject::EnqueueChangeRecord(receiver, "update", name, old_value); |
| 155 | } |
| 156 | |
| 157 | return value; |
| 158 | } |
| 159 | |
| 160 | void Accessors::ReconfigureToDataProperty( |
| 161 | v8::Local<v8::Name> key, v8::Local<v8::Value> val, |
| 162 | const v8::PropertyCallbackInfo<void>& info) { |
| 163 | i::Isolate* isolate = reinterpret_cast<i::Isolate*>(info.GetIsolate()); |
| 164 | HandleScope scope(isolate); |
| 165 | Handle<JSObject> receiver = |
| 166 | Handle<JSObject>::cast(Utils::OpenHandle(*info.This())); |
| 167 | Handle<JSObject> holder = |
| 168 | Handle<JSObject>::cast(Utils::OpenHandle(*info.Holder())); |
| 169 | Handle<Name> name = Utils::OpenHandle(*key); |
| 170 | Handle<Object> value = Utils::OpenHandle(*val); |
| 171 | MaybeHandle<Object> result = ReplaceAccessorWithDataProperty( |
| 172 | isolate, receiver, holder, name, value, false); |
| 173 | if (result.is_null()) isolate->OptionalRescheduleException(false); |
| 174 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 175 | |
| 176 | // |
| 177 | // Accessors::ArgumentsIterator |
| 178 | // |
| 179 | |
| 180 | |
| 181 | void Accessors::ArgumentsIteratorGetter( |
| 182 | v8::Local<v8::Name> name, const v8::PropertyCallbackInfo<v8::Value>& info) { |
| 183 | i::Isolate* isolate = reinterpret_cast<i::Isolate*>(info.GetIsolate()); |
| 184 | DisallowHeapAllocation no_allocation; |
| 185 | HandleScope scope(isolate); |
| 186 | Object* result = isolate->native_context()->array_values_iterator(); |
| 187 | info.GetReturnValue().Set(Utils::ToLocal(Handle<Object>(result, isolate))); |
| 188 | } |
| 189 | |
| 190 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 191 | Handle<AccessorInfo> Accessors::ArgumentsIteratorInfo( |
| 192 | Isolate* isolate, PropertyAttributes attributes) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 193 | Handle<Name> name = isolate->factory()->iterator_symbol(); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 194 | return MakeAccessor(isolate, name, &ArgumentsIteratorGetter, nullptr, |
| 195 | attributes); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | |
| 199 | // |
| 200 | // Accessors::ArrayLength |
| 201 | // |
| 202 | |
| 203 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 204 | void Accessors::ArrayLengthGetter( |
| 205 | v8::Local<v8::Name> name, |
| 206 | const v8::PropertyCallbackInfo<v8::Value>& info) { |
| 207 | i::Isolate* isolate = reinterpret_cast<i::Isolate*>(info.GetIsolate()); |
| 208 | DisallowHeapAllocation no_allocation; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 209 | HandleScope scope(isolate); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 210 | JSArray* holder = JSArray::cast(*Utils::OpenHandle(*info.Holder())); |
| 211 | Object* result = holder->length(); |
| 212 | info.GetReturnValue().Set(Utils::ToLocal(Handle<Object>(result, isolate))); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 216 | void Accessors::ArrayLengthSetter( |
| 217 | v8::Local<v8::Name> name, |
| 218 | v8::Local<v8::Value> val, |
| 219 | const v8::PropertyCallbackInfo<void>& info) { |
| 220 | i::Isolate* isolate = reinterpret_cast<i::Isolate*>(info.GetIsolate()); |
| 221 | HandleScope scope(isolate); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 222 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 223 | Handle<JSReceiver> object = Utils::OpenHandle(*info.This()); |
| 224 | Handle<JSArray> array = Handle<JSArray>::cast(object); |
| 225 | Handle<Object> length_obj = Utils::OpenHandle(*val); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 226 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 227 | uint32_t length = 0; |
| 228 | if (!JSArray::AnythingToArrayLength(isolate, length_obj, &length)) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 229 | isolate->OptionalRescheduleException(false); |
| 230 | return; |
| 231 | } |
| 232 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 233 | if (JSArray::ObservableSetLength(array, length).is_null()) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 234 | isolate->OptionalRescheduleException(false); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 235 | } |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 236 | |
| 237 | if (info.ShouldThrowOnError()) { |
| 238 | uint32_t actual_new_len = 0; |
| 239 | CHECK(array->length()->ToArrayLength(&actual_new_len)); |
| 240 | // Throw TypeError if there were non-deletable elements. |
| 241 | if (actual_new_len != length) { |
| 242 | Factory* factory = isolate->factory(); |
| 243 | isolate->Throw(*factory->NewTypeError( |
| 244 | MessageTemplate::kStrictDeleteProperty, |
| 245 | factory->NewNumberFromUint(actual_new_len - 1), array)); |
| 246 | isolate->OptionalRescheduleException(false); |
| 247 | } |
| 248 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | |
| 252 | Handle<AccessorInfo> Accessors::ArrayLengthInfo( |
| 253 | Isolate* isolate, PropertyAttributes attributes) { |
| 254 | return MakeAccessor(isolate, |
| 255 | isolate->factory()->length_string(), |
| 256 | &ArrayLengthGetter, |
| 257 | &ArrayLengthSetter, |
| 258 | attributes); |
| 259 | } |
| 260 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 261 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 262 | // |
| 263 | // Accessors::StringLength |
| 264 | // |
| 265 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 266 | void Accessors::StringLengthGetter( |
| 267 | v8::Local<v8::Name> name, |
| 268 | const v8::PropertyCallbackInfo<v8::Value>& info) { |
| 269 | i::Isolate* isolate = reinterpret_cast<i::Isolate*>(info.GetIsolate()); |
| 270 | DisallowHeapAllocation no_allocation; |
| 271 | HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 272 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 273 | // We have a slight impedance mismatch between the external API and the way we |
| 274 | // use callbacks internally: Externally, callbacks can only be used with |
| 275 | // v8::Object, but internally we have callbacks on entities which are higher |
| 276 | // in the hierarchy, in this case for String values. |
| 277 | |
| 278 | Object* value = *Utils::OpenHandle(*v8::Local<v8::Value>(info.This())); |
| 279 | if (!value->IsString()) { |
| 280 | // Not a string value. That means that we either got a String wrapper or |
| 281 | // a Value with a String wrapper in its prototype chain. |
| 282 | value = JSValue::cast(*Utils::OpenHandle(*info.Holder()))->value(); |
| 283 | } |
| 284 | Object* result = Smi::FromInt(String::cast(value)->length()); |
| 285 | info.GetReturnValue().Set(Utils::ToLocal(Handle<Object>(result, isolate))); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 289 | Handle<AccessorInfo> Accessors::StringLengthInfo( |
| 290 | Isolate* isolate, PropertyAttributes attributes) { |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 291 | return MakeAccessor(isolate, isolate->factory()->length_string(), |
| 292 | &StringLengthGetter, nullptr, attributes); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 296 | // |
| 297 | // Accessors::ScriptColumnOffset |
| 298 | // |
| 299 | |
| 300 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 301 | void Accessors::ScriptColumnOffsetGetter( |
| 302 | v8::Local<v8::Name> name, |
| 303 | const v8::PropertyCallbackInfo<v8::Value>& info) { |
| 304 | i::Isolate* isolate = reinterpret_cast<i::Isolate*>(info.GetIsolate()); |
| 305 | DisallowHeapAllocation no_allocation; |
| 306 | HandleScope scope(isolate); |
| 307 | Object* object = *Utils::OpenHandle(*info.This()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 308 | Object* res = Smi::FromInt( |
| 309 | Script::cast(JSValue::cast(object)->value())->column_offset()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 310 | info.GetReturnValue().Set(Utils::ToLocal(Handle<Object>(res, isolate))); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 314 | Handle<AccessorInfo> Accessors::ScriptColumnOffsetInfo( |
| 315 | Isolate* isolate, PropertyAttributes attributes) { |
| 316 | Handle<String> name(isolate->factory()->InternalizeOneByteString( |
| 317 | STATIC_CHAR_VECTOR("column_offset"))); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 318 | return MakeAccessor(isolate, name, &ScriptColumnOffsetGetter, nullptr, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 319 | attributes); |
| 320 | } |
| 321 | |
| 322 | |
| 323 | // |
| 324 | // Accessors::ScriptId |
| 325 | // |
| 326 | |
| 327 | |
| 328 | void Accessors::ScriptIdGetter( |
| 329 | v8::Local<v8::Name> name, |
| 330 | const v8::PropertyCallbackInfo<v8::Value>& info) { |
| 331 | i::Isolate* isolate = reinterpret_cast<i::Isolate*>(info.GetIsolate()); |
| 332 | DisallowHeapAllocation no_allocation; |
| 333 | HandleScope scope(isolate); |
| 334 | Object* object = *Utils::OpenHandle(*info.This()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 335 | Object* id = Smi::FromInt(Script::cast(JSValue::cast(object)->value())->id()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 336 | info.GetReturnValue().Set(Utils::ToLocal(Handle<Object>(id, isolate))); |
| 337 | } |
| 338 | |
| 339 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 340 | Handle<AccessorInfo> Accessors::ScriptIdInfo( |
| 341 | Isolate* isolate, PropertyAttributes attributes) { |
| 342 | Handle<String> name( |
| 343 | isolate->factory()->InternalizeOneByteString(STATIC_CHAR_VECTOR("id"))); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 344 | return MakeAccessor(isolate, name, &ScriptIdGetter, nullptr, attributes); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | |
| 348 | // |
| 349 | // Accessors::ScriptName |
| 350 | // |
| 351 | |
| 352 | |
| 353 | void Accessors::ScriptNameGetter( |
| 354 | v8::Local<v8::Name> name, |
| 355 | const v8::PropertyCallbackInfo<v8::Value>& info) { |
| 356 | i::Isolate* isolate = reinterpret_cast<i::Isolate*>(info.GetIsolate()); |
| 357 | DisallowHeapAllocation no_allocation; |
| 358 | HandleScope scope(isolate); |
| 359 | Object* object = *Utils::OpenHandle(*info.This()); |
| 360 | Object* source = Script::cast(JSValue::cast(object)->value())->name(); |
| 361 | info.GetReturnValue().Set(Utils::ToLocal(Handle<Object>(source, isolate))); |
| 362 | } |
| 363 | |
| 364 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 365 | Handle<AccessorInfo> Accessors::ScriptNameInfo( |
| 366 | Isolate* isolate, PropertyAttributes attributes) { |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 367 | return MakeAccessor(isolate, isolate->factory()->name_string(), |
| 368 | &ScriptNameGetter, nullptr, attributes); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | |
| 372 | // |
| 373 | // Accessors::ScriptSource |
| 374 | // |
| 375 | |
| 376 | |
| 377 | void Accessors::ScriptSourceGetter( |
| 378 | v8::Local<v8::Name> name, |
| 379 | const v8::PropertyCallbackInfo<v8::Value>& info) { |
| 380 | i::Isolate* isolate = reinterpret_cast<i::Isolate*>(info.GetIsolate()); |
| 381 | DisallowHeapAllocation no_allocation; |
| 382 | HandleScope scope(isolate); |
| 383 | Object* object = *Utils::OpenHandle(*info.This()); |
| 384 | Object* source = Script::cast(JSValue::cast(object)->value())->source(); |
| 385 | info.GetReturnValue().Set(Utils::ToLocal(Handle<Object>(source, isolate))); |
| 386 | } |
| 387 | |
| 388 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 389 | Handle<AccessorInfo> Accessors::ScriptSourceInfo( |
| 390 | Isolate* isolate, PropertyAttributes attributes) { |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 391 | return MakeAccessor(isolate, isolate->factory()->source_string(), |
| 392 | &ScriptSourceGetter, nullptr, attributes); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 393 | } |
| 394 | |
| 395 | |
| 396 | // |
| 397 | // Accessors::ScriptLineOffset |
| 398 | // |
| 399 | |
| 400 | |
| 401 | void Accessors::ScriptLineOffsetGetter( |
| 402 | v8::Local<v8::Name> name, |
| 403 | const v8::PropertyCallbackInfo<v8::Value>& info) { |
| 404 | i::Isolate* isolate = reinterpret_cast<i::Isolate*>(info.GetIsolate()); |
| 405 | DisallowHeapAllocation no_allocation; |
| 406 | HandleScope scope(isolate); |
| 407 | Object* object = *Utils::OpenHandle(*info.This()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 408 | Object* res = |
| 409 | Smi::FromInt(Script::cast(JSValue::cast(object)->value())->line_offset()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 410 | info.GetReturnValue().Set(Utils::ToLocal(Handle<Object>(res, isolate))); |
| 411 | } |
| 412 | |
| 413 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 414 | Handle<AccessorInfo> Accessors::ScriptLineOffsetInfo( |
| 415 | Isolate* isolate, PropertyAttributes attributes) { |
| 416 | Handle<String> name(isolate->factory()->InternalizeOneByteString( |
| 417 | STATIC_CHAR_VECTOR("line_offset"))); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 418 | return MakeAccessor(isolate, name, &ScriptLineOffsetGetter, nullptr, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 419 | attributes); |
| 420 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 421 | |
| 422 | |
| 423 | // |
| 424 | // Accessors::ScriptType |
| 425 | // |
| 426 | |
| 427 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 428 | void Accessors::ScriptTypeGetter( |
| 429 | v8::Local<v8::Name> name, |
| 430 | const v8::PropertyCallbackInfo<v8::Value>& info) { |
| 431 | i::Isolate* isolate = reinterpret_cast<i::Isolate*>(info.GetIsolate()); |
| 432 | DisallowHeapAllocation no_allocation; |
| 433 | HandleScope scope(isolate); |
| 434 | Object* object = *Utils::OpenHandle(*info.This()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 435 | Object* res = |
| 436 | Smi::FromInt(Script::cast(JSValue::cast(object)->value())->type()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 437 | info.GetReturnValue().Set(Utils::ToLocal(Handle<Object>(res, isolate))); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 438 | } |
| 439 | |
| 440 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 441 | Handle<AccessorInfo> Accessors::ScriptTypeInfo( |
| 442 | Isolate* isolate, PropertyAttributes attributes) { |
| 443 | Handle<String> name( |
| 444 | isolate->factory()->InternalizeOneByteString(STATIC_CHAR_VECTOR("type"))); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 445 | return MakeAccessor(isolate, name, &ScriptTypeGetter, nullptr, attributes); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 446 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 447 | |
| 448 | |
| 449 | // |
| 450 | // Accessors::ScriptCompilationType |
| 451 | // |
| 452 | |
| 453 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 454 | void Accessors::ScriptCompilationTypeGetter( |
| 455 | v8::Local<v8::Name> name, |
| 456 | const v8::PropertyCallbackInfo<v8::Value>& info) { |
| 457 | i::Isolate* isolate = reinterpret_cast<i::Isolate*>(info.GetIsolate()); |
| 458 | DisallowHeapAllocation no_allocation; |
| 459 | HandleScope scope(isolate); |
| 460 | Object* object = *Utils::OpenHandle(*info.This()); |
| 461 | Object* res = Smi::FromInt( |
| 462 | Script::cast(JSValue::cast(object)->value())->compilation_type()); |
| 463 | info.GetReturnValue().Set(Utils::ToLocal(Handle<Object>(res, isolate))); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 464 | } |
| 465 | |
| 466 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 467 | Handle<AccessorInfo> Accessors::ScriptCompilationTypeInfo( |
| 468 | Isolate* isolate, PropertyAttributes attributes) { |
| 469 | Handle<String> name(isolate->factory()->InternalizeOneByteString( |
| 470 | STATIC_CHAR_VECTOR("compilation_type"))); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 471 | return MakeAccessor(isolate, name, &ScriptCompilationTypeGetter, nullptr, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 472 | attributes); |
| 473 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 474 | |
| 475 | |
| 476 | // |
| 477 | // Accessors::ScriptGetLineEnds |
| 478 | // |
| 479 | |
| 480 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 481 | void Accessors::ScriptLineEndsGetter( |
| 482 | v8::Local<v8::Name> name, |
| 483 | const v8::PropertyCallbackInfo<v8::Value>& info) { |
| 484 | i::Isolate* isolate = reinterpret_cast<i::Isolate*>(info.GetIsolate()); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 485 | HandleScope scope(isolate); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 486 | Handle<Object> object = Utils::OpenHandle(*info.This()); |
| 487 | Handle<Script> script( |
| 488 | Script::cast(Handle<JSValue>::cast(object)->value()), isolate); |
| 489 | Script::InitLineEnds(script); |
| 490 | DCHECK(script->line_ends()->IsFixedArray()); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 491 | Handle<FixedArray> line_ends(FixedArray::cast(script->line_ends())); |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 492 | // We do not want anyone to modify this array from JS. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 493 | DCHECK(*line_ends == isolate->heap()->empty_fixed_array() || |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 494 | line_ends->map() == isolate->heap()->fixed_cow_array_map()); |
| 495 | Handle<JSArray> js_array = |
| 496 | isolate->factory()->NewJSArrayWithElements(line_ends); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 497 | info.GetReturnValue().Set(Utils::ToLocal(js_array)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 498 | } |
| 499 | |
| 500 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 501 | Handle<AccessorInfo> Accessors::ScriptLineEndsInfo( |
| 502 | Isolate* isolate, PropertyAttributes attributes) { |
| 503 | Handle<String> name(isolate->factory()->InternalizeOneByteString( |
| 504 | STATIC_CHAR_VECTOR("line_ends"))); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 505 | return MakeAccessor(isolate, name, &ScriptLineEndsGetter, nullptr, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 506 | attributes); |
| 507 | } |
| 508 | |
| 509 | |
| 510 | // |
| 511 | // Accessors::ScriptSourceUrl |
| 512 | // |
| 513 | |
| 514 | |
| 515 | void Accessors::ScriptSourceUrlGetter( |
| 516 | v8::Local<v8::Name> name, |
| 517 | const v8::PropertyCallbackInfo<v8::Value>& info) { |
| 518 | i::Isolate* isolate = reinterpret_cast<i::Isolate*>(info.GetIsolate()); |
| 519 | DisallowHeapAllocation no_allocation; |
| 520 | HandleScope scope(isolate); |
| 521 | Object* object = *Utils::OpenHandle(*info.This()); |
| 522 | Object* url = Script::cast(JSValue::cast(object)->value())->source_url(); |
| 523 | info.GetReturnValue().Set(Utils::ToLocal(Handle<Object>(url, isolate))); |
| 524 | } |
| 525 | |
| 526 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 527 | Handle<AccessorInfo> Accessors::ScriptSourceUrlInfo( |
| 528 | Isolate* isolate, PropertyAttributes attributes) { |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 529 | return MakeAccessor(isolate, isolate->factory()->source_url_string(), |
| 530 | &ScriptSourceUrlGetter, nullptr, attributes); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 531 | } |
| 532 | |
| 533 | |
| 534 | // |
| 535 | // Accessors::ScriptSourceMappingUrl |
| 536 | // |
| 537 | |
| 538 | |
| 539 | void Accessors::ScriptSourceMappingUrlGetter( |
| 540 | v8::Local<v8::Name> name, |
| 541 | const v8::PropertyCallbackInfo<v8::Value>& info) { |
| 542 | i::Isolate* isolate = reinterpret_cast<i::Isolate*>(info.GetIsolate()); |
| 543 | DisallowHeapAllocation no_allocation; |
| 544 | HandleScope scope(isolate); |
| 545 | Object* object = *Utils::OpenHandle(*info.This()); |
| 546 | Object* url = |
| 547 | Script::cast(JSValue::cast(object)->value())->source_mapping_url(); |
| 548 | info.GetReturnValue().Set(Utils::ToLocal(Handle<Object>(url, isolate))); |
| 549 | } |
| 550 | |
| 551 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 552 | Handle<AccessorInfo> Accessors::ScriptSourceMappingUrlInfo( |
| 553 | Isolate* isolate, PropertyAttributes attributes) { |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 554 | return MakeAccessor(isolate, isolate->factory()->source_mapping_url_string(), |
| 555 | &ScriptSourceMappingUrlGetter, nullptr, attributes); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 556 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 557 | |
| 558 | |
| 559 | // |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 560 | // Accessors::ScriptIsEmbedderDebugScript |
| 561 | // |
| 562 | |
| 563 | |
| 564 | void Accessors::ScriptIsEmbedderDebugScriptGetter( |
| 565 | v8::Local<v8::Name> name, const v8::PropertyCallbackInfo<v8::Value>& info) { |
| 566 | i::Isolate* isolate = reinterpret_cast<i::Isolate*>(info.GetIsolate()); |
| 567 | DisallowHeapAllocation no_allocation; |
| 568 | HandleScope scope(isolate); |
| 569 | Object* object = *Utils::OpenHandle(*info.This()); |
| 570 | bool is_embedder_debug_script = Script::cast(JSValue::cast(object)->value()) |
| 571 | ->origin_options() |
| 572 | .IsEmbedderDebugScript(); |
| 573 | Object* res = *isolate->factory()->ToBoolean(is_embedder_debug_script); |
| 574 | info.GetReturnValue().Set(Utils::ToLocal(Handle<Object>(res, isolate))); |
| 575 | } |
| 576 | |
| 577 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 578 | Handle<AccessorInfo> Accessors::ScriptIsEmbedderDebugScriptInfo( |
| 579 | Isolate* isolate, PropertyAttributes attributes) { |
| 580 | Handle<String> name(isolate->factory()->InternalizeOneByteString( |
| 581 | STATIC_CHAR_VECTOR("is_debugger_script"))); |
| 582 | return MakeAccessor(isolate, name, &ScriptIsEmbedderDebugScriptGetter, |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 583 | nullptr, attributes); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 584 | } |
| 585 | |
| 586 | |
| 587 | // |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 588 | // Accessors::ScriptGetContextData |
| 589 | // |
| 590 | |
| 591 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 592 | void Accessors::ScriptContextDataGetter( |
| 593 | v8::Local<v8::Name> name, |
| 594 | const v8::PropertyCallbackInfo<v8::Value>& info) { |
| 595 | i::Isolate* isolate = reinterpret_cast<i::Isolate*>(info.GetIsolate()); |
| 596 | DisallowHeapAllocation no_allocation; |
| 597 | HandleScope scope(isolate); |
| 598 | Object* object = *Utils::OpenHandle(*info.This()); |
| 599 | Object* res = Script::cast(JSValue::cast(object)->value())->context_data(); |
| 600 | info.GetReturnValue().Set(Utils::ToLocal(Handle<Object>(res, isolate))); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 601 | } |
| 602 | |
| 603 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 604 | Handle<AccessorInfo> Accessors::ScriptContextDataInfo( |
| 605 | Isolate* isolate, PropertyAttributes attributes) { |
| 606 | Handle<String> name(isolate->factory()->InternalizeOneByteString( |
| 607 | STATIC_CHAR_VECTOR("context_data"))); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 608 | return MakeAccessor(isolate, name, &ScriptContextDataGetter, nullptr, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 609 | attributes); |
| 610 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 611 | |
| 612 | |
| 613 | // |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 614 | // Accessors::ScriptGetEvalFromScript |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 615 | // |
| 616 | |
| 617 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 618 | void Accessors::ScriptEvalFromScriptGetter( |
| 619 | v8::Local<v8::Name> name, |
| 620 | const v8::PropertyCallbackInfo<v8::Value>& info) { |
| 621 | i::Isolate* isolate = reinterpret_cast<i::Isolate*>(info.GetIsolate()); |
| 622 | HandleScope scope(isolate); |
| 623 | Handle<Object> object = Utils::OpenHandle(*info.This()); |
| 624 | Handle<Script> script( |
| 625 | Script::cast(Handle<JSValue>::cast(object)->value()), isolate); |
| 626 | Handle<Object> result = isolate->factory()->undefined_value(); |
| 627 | if (!script->eval_from_shared()->IsUndefined()) { |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 628 | Handle<SharedFunctionInfo> eval_from_shared( |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 629 | SharedFunctionInfo::cast(script->eval_from_shared())); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 630 | if (eval_from_shared->script()->IsScript()) { |
| 631 | Handle<Script> eval_from_script(Script::cast(eval_from_shared->script())); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 632 | result = Script::GetWrapper(eval_from_script); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 633 | } |
| 634 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 635 | |
| 636 | info.GetReturnValue().Set(Utils::ToLocal(result)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 637 | } |
| 638 | |
| 639 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 640 | Handle<AccessorInfo> Accessors::ScriptEvalFromScriptInfo( |
| 641 | Isolate* isolate, PropertyAttributes attributes) { |
| 642 | Handle<String> name(isolate->factory()->InternalizeOneByteString( |
| 643 | STATIC_CHAR_VECTOR("eval_from_script"))); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 644 | return MakeAccessor(isolate, name, &ScriptEvalFromScriptGetter, nullptr, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 645 | attributes); |
| 646 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 647 | |
| 648 | |
| 649 | // |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 650 | // Accessors::ScriptGetEvalFromScriptPosition |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 651 | // |
| 652 | |
| 653 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 654 | void Accessors::ScriptEvalFromScriptPositionGetter( |
| 655 | v8::Local<v8::Name> name, |
| 656 | const v8::PropertyCallbackInfo<v8::Value>& info) { |
| 657 | i::Isolate* isolate = reinterpret_cast<i::Isolate*>(info.GetIsolate()); |
| 658 | HandleScope scope(isolate); |
| 659 | Handle<Object> object = Utils::OpenHandle(*info.This()); |
| 660 | Handle<Script> script( |
| 661 | Script::cast(Handle<JSValue>::cast(object)->value()), isolate); |
| 662 | Handle<Object> result = isolate->factory()->undefined_value(); |
| 663 | if (script->compilation_type() == Script::COMPILATION_TYPE_EVAL) { |
| 664 | Handle<Code> code(SharedFunctionInfo::cast( |
| 665 | script->eval_from_shared())->code()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 666 | result = Handle<Object>(Smi::FromInt(code->SourcePosition( |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 667 | script->eval_from_instructions_offset())), |
| 668 | isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 669 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 670 | info.GetReturnValue().Set(Utils::ToLocal(result)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 671 | } |
| 672 | |
| 673 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 674 | Handle<AccessorInfo> Accessors::ScriptEvalFromScriptPositionInfo( |
| 675 | Isolate* isolate, PropertyAttributes attributes) { |
| 676 | Handle<String> name(isolate->factory()->InternalizeOneByteString( |
| 677 | STATIC_CHAR_VECTOR("eval_from_script_position"))); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 678 | return MakeAccessor(isolate, name, &ScriptEvalFromScriptPositionGetter, |
| 679 | nullptr, attributes); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 680 | } |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 681 | |
| 682 | |
| 683 | // |
| 684 | // Accessors::ScriptGetEvalFromFunctionName |
| 685 | // |
| 686 | |
| 687 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 688 | void Accessors::ScriptEvalFromFunctionNameGetter( |
| 689 | v8::Local<v8::Name> name, |
| 690 | const v8::PropertyCallbackInfo<v8::Value>& info) { |
| 691 | i::Isolate* isolate = reinterpret_cast<i::Isolate*>(info.GetIsolate()); |
| 692 | HandleScope scope(isolate); |
| 693 | Handle<Object> object = Utils::OpenHandle(*info.This()); |
| 694 | Handle<Script> script( |
| 695 | Script::cast(Handle<JSValue>::cast(object)->value()), isolate); |
| 696 | Handle<Object> result; |
| 697 | Handle<SharedFunctionInfo> shared( |
| 698 | SharedFunctionInfo::cast(script->eval_from_shared())); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 699 | // Find the name of the function calling eval. |
| 700 | if (!shared->name()->IsUndefined()) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 701 | result = Handle<Object>(shared->name(), isolate); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 702 | } else { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 703 | result = Handle<Object>(shared->inferred_name(), isolate); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 704 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 705 | info.GetReturnValue().Set(Utils::ToLocal(result)); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 706 | } |
| 707 | |
| 708 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 709 | Handle<AccessorInfo> Accessors::ScriptEvalFromFunctionNameInfo( |
| 710 | Isolate* isolate, PropertyAttributes attributes) { |
| 711 | Handle<String> name(isolate->factory()->InternalizeOneByteString( |
| 712 | STATIC_CHAR_VECTOR("eval_from_function_name"))); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 713 | return MakeAccessor(isolate, name, &ScriptEvalFromFunctionNameGetter, nullptr, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 714 | attributes); |
| 715 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 716 | |
| 717 | |
| 718 | // |
| 719 | // Accessors::FunctionPrototype |
| 720 | // |
| 721 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 722 | static Handle<Object> GetFunctionPrototype(Isolate* isolate, |
| 723 | Handle<JSFunction> function) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 724 | if (!function->has_prototype()) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 725 | Handle<Object> proto = isolate->factory()->NewFunctionPrototype(function); |
| 726 | JSFunction::SetPrototype(function, proto); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 727 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 728 | return Handle<Object>(function->prototype(), isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 729 | } |
| 730 | |
| 731 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 732 | MUST_USE_RESULT static MaybeHandle<Object> SetFunctionPrototype( |
| 733 | Isolate* isolate, Handle<JSFunction> function, Handle<Object> value) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 734 | Handle<Object> old_value; |
| 735 | bool is_observed = function->map()->is_observed(); |
| 736 | if (is_observed) { |
| 737 | if (function->has_prototype()) |
| 738 | old_value = handle(function->prototype(), isolate); |
| 739 | else |
| 740 | old_value = isolate->factory()->NewFunctionPrototype(function); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 741 | } |
| 742 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 743 | JSFunction::SetPrototype(function, value); |
| 744 | DCHECK(function->prototype() == *value); |
| 745 | |
| 746 | if (is_observed && !old_value->SameValue(*value)) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 747 | MaybeHandle<Object> result = JSObject::EnqueueChangeRecord( |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 748 | function, "update", isolate->factory()->prototype_string(), old_value); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 749 | if (result.is_null()) return MaybeHandle<Object>(); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 750 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 751 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 752 | return function; |
| 753 | } |
| 754 | |
| 755 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 756 | MaybeHandle<Object> Accessors::FunctionSetPrototype(Handle<JSFunction> function, |
| 757 | Handle<Object> prototype) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 758 | DCHECK(function->IsConstructor()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 759 | Isolate* isolate = function->GetIsolate(); |
| 760 | return SetFunctionPrototype(isolate, function, prototype); |
| 761 | } |
| 762 | |
| 763 | |
| 764 | void Accessors::FunctionPrototypeGetter( |
| 765 | v8::Local<v8::Name> name, |
| 766 | const v8::PropertyCallbackInfo<v8::Value>& info) { |
| 767 | i::Isolate* isolate = reinterpret_cast<i::Isolate*>(info.GetIsolate()); |
| 768 | HandleScope scope(isolate); |
| 769 | Handle<JSFunction> function = |
| 770 | Handle<JSFunction>::cast(Utils::OpenHandle(*info.Holder())); |
| 771 | Handle<Object> result = GetFunctionPrototype(isolate, function); |
| 772 | info.GetReturnValue().Set(Utils::ToLocal(result)); |
| 773 | } |
| 774 | |
| 775 | |
| 776 | void Accessors::FunctionPrototypeSetter( |
| 777 | v8::Local<v8::Name> name, |
| 778 | v8::Local<v8::Value> val, |
| 779 | const v8::PropertyCallbackInfo<void>& info) { |
| 780 | i::Isolate* isolate = reinterpret_cast<i::Isolate*>(info.GetIsolate()); |
| 781 | HandleScope scope(isolate); |
| 782 | Handle<Object> value = Utils::OpenHandle(*val); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 783 | Handle<JSFunction> object = |
| 784 | Handle<JSFunction>::cast(Utils::OpenHandle(*info.Holder())); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 785 | if (SetFunctionPrototype(isolate, object, value).is_null()) { |
| 786 | isolate->OptionalRescheduleException(false); |
| 787 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 788 | } |
| 789 | |
| 790 | |
| 791 | Handle<AccessorInfo> Accessors::FunctionPrototypeInfo( |
| 792 | Isolate* isolate, PropertyAttributes attributes) { |
| 793 | return MakeAccessor(isolate, |
| 794 | isolate->factory()->prototype_string(), |
| 795 | &FunctionPrototypeGetter, |
| 796 | &FunctionPrototypeSetter, |
| 797 | attributes); |
| 798 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 799 | |
| 800 | |
| 801 | // |
| 802 | // Accessors::FunctionLength |
| 803 | // |
| 804 | |
| 805 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 806 | void Accessors::FunctionLengthGetter( |
| 807 | v8::Local<v8::Name> name, |
| 808 | const v8::PropertyCallbackInfo<v8::Value>& info) { |
| 809 | i::Isolate* isolate = reinterpret_cast<i::Isolate*>(info.GetIsolate()); |
| 810 | HandleScope scope(isolate); |
| 811 | Handle<JSFunction> function = |
| 812 | Handle<JSFunction>::cast(Utils::OpenHandle(*info.Holder())); |
| 813 | |
| 814 | int length = 0; |
| 815 | if (function->shared()->is_compiled()) { |
| 816 | length = function->shared()->length(); |
| 817 | } else { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 818 | // If the function isn't compiled yet, the length is not computed |
| 819 | // correctly yet. Compile it now and return the right length. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 820 | if (Compiler::Compile(function, KEEP_EXCEPTION)) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 821 | length = function->shared()->length(); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 822 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 823 | if (isolate->has_pending_exception()) { |
| 824 | isolate->OptionalRescheduleException(false); |
| 825 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 826 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 827 | Handle<Object> result(Smi::FromInt(length), isolate); |
| 828 | info.GetReturnValue().Set(Utils::ToLocal(result)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 829 | } |
| 830 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 831 | void Accessors::ObservedReconfigureToDataProperty( |
| 832 | v8::Local<v8::Name> key, v8::Local<v8::Value> val, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 833 | const v8::PropertyCallbackInfo<void>& info) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 834 | i::Isolate* isolate = reinterpret_cast<i::Isolate*>(info.GetIsolate()); |
| 835 | HandleScope scope(isolate); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 836 | Handle<JSObject> receiver = |
| 837 | Handle<JSObject>::cast(Utils::OpenHandle(*info.This())); |
| 838 | Handle<JSObject> holder = |
| 839 | Handle<JSObject>::cast(Utils::OpenHandle(*info.Holder())); |
| 840 | Handle<Name> name = Utils::OpenHandle(*key); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 841 | Handle<Object> value = Utils::OpenHandle(*val); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 842 | MaybeHandle<Object> result = ReplaceAccessorWithDataProperty( |
| 843 | isolate, receiver, holder, name, value, true); |
| 844 | if (result.is_null()) isolate->OptionalRescheduleException(false); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 845 | } |
| 846 | |
| 847 | |
| 848 | Handle<AccessorInfo> Accessors::FunctionLengthInfo( |
| 849 | Isolate* isolate, PropertyAttributes attributes) { |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 850 | return MakeAccessor(isolate, isolate->factory()->length_string(), |
| 851 | &FunctionLengthGetter, &ObservedReconfigureToDataProperty, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 852 | attributes); |
| 853 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 854 | |
| 855 | |
| 856 | // |
| 857 | // Accessors::FunctionName |
| 858 | // |
| 859 | |
| 860 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 861 | void Accessors::FunctionNameGetter( |
| 862 | v8::Local<v8::Name> name, |
| 863 | const v8::PropertyCallbackInfo<v8::Value>& info) { |
| 864 | i::Isolate* isolate = reinterpret_cast<i::Isolate*>(info.GetIsolate()); |
| 865 | HandleScope scope(isolate); |
| 866 | Handle<JSFunction> function = |
| 867 | Handle<JSFunction>::cast(Utils::OpenHandle(*info.Holder())); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 868 | Handle<Object> result; |
| 869 | if (function->shared()->name_should_print_as_anonymous()) { |
| 870 | result = isolate->factory()->anonymous_string(); |
| 871 | } else { |
| 872 | result = handle(function->shared()->name(), isolate); |
| 873 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 874 | info.GetReturnValue().Set(Utils::ToLocal(result)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 875 | } |
| 876 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 877 | Handle<AccessorInfo> Accessors::FunctionNameInfo( |
| 878 | Isolate* isolate, PropertyAttributes attributes) { |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 879 | return MakeAccessor(isolate, isolate->factory()->name_string(), |
| 880 | &FunctionNameGetter, &ObservedReconfigureToDataProperty, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 881 | attributes); |
| 882 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 883 | |
| 884 | |
| 885 | // |
| 886 | // Accessors::FunctionArguments |
| 887 | // |
| 888 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 889 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 890 | static Handle<Object> ArgumentsForInlinedFunction( |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 891 | JavaScriptFrame* frame, |
| 892 | Handle<JSFunction> inlined_function, |
| 893 | int inlined_frame_index) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 894 | Isolate* isolate = inlined_function->GetIsolate(); |
| 895 | Factory* factory = isolate->factory(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 896 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 897 | TranslatedState translated_values(frame); |
| 898 | translated_values.Prepare(false, frame->fp()); |
| 899 | |
| 900 | int argument_count = 0; |
| 901 | TranslatedFrame* translated_frame = |
| 902 | translated_values.GetArgumentsInfoFromJSFrameIndex(inlined_frame_index, |
| 903 | &argument_count); |
| 904 | TranslatedFrame::iterator iter = translated_frame->begin(); |
| 905 | |
| 906 | // Skip the function. |
| 907 | iter++; |
| 908 | |
| 909 | // Skip the receiver. |
| 910 | iter++; |
| 911 | argument_count--; |
| 912 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 913 | Handle<JSObject> arguments = |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 914 | factory->NewArgumentsObject(inlined_function, argument_count); |
| 915 | Handle<FixedArray> array = factory->NewFixedArray(argument_count); |
| 916 | bool should_deoptimize = false; |
| 917 | for (int i = 0; i < argument_count; ++i) { |
| 918 | // If we materialize any object, we should deopt because we might alias |
| 919 | // an object that was eliminated by escape analysis. |
| 920 | should_deoptimize = should_deoptimize || iter->IsMaterializedObject(); |
| 921 | Handle<Object> value = iter->GetValue(); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 922 | array->set(i, *value); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 923 | iter++; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 924 | } |
| 925 | arguments->set_elements(*array); |
| 926 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 927 | if (should_deoptimize) { |
| 928 | translated_values.StoreMaterializedValuesAndDeopt(); |
| 929 | } |
| 930 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 931 | // Return the freshly allocated arguments object. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 932 | return arguments; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 933 | } |
| 934 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 935 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 936 | static int FindFunctionInFrame(JavaScriptFrame* frame, |
| 937 | Handle<JSFunction> function) { |
| 938 | DisallowHeapAllocation no_allocation; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 939 | List<JSFunction*> functions(2); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 940 | frame->GetFunctions(&functions); |
| 941 | for (int i = functions.length() - 1; i >= 0; i--) { |
| 942 | if (functions[i] == *function) return i; |
| 943 | } |
| 944 | return -1; |
| 945 | } |
| 946 | |
| 947 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 948 | namespace { |
| 949 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 950 | Handle<Object> GetFunctionArguments(Isolate* isolate, |
| 951 | Handle<JSFunction> function) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 952 | // Find the top invocation of the function by traversing frames. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 953 | for (JavaScriptFrameIterator it(isolate); !it.done(); it.Advance()) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 954 | JavaScriptFrame* frame = it.frame(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 955 | int function_index = FindFunctionInFrame(frame, function); |
| 956 | if (function_index < 0) continue; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 957 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 958 | if (function_index > 0) { |
| 959 | // The function in question was inlined. Inlined functions have the |
| 960 | // correct number of arguments and no allocated arguments object, so |
| 961 | // we can construct a fresh one by interpreting the function's |
| 962 | // deoptimization input data. |
| 963 | return ArgumentsForInlinedFunction(frame, function, function_index); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 964 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 965 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 966 | // Find the frame that holds the actual arguments passed to the function. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 967 | it.AdvanceToArgumentsFrame(); |
| 968 | frame = it.frame(); |
| 969 | |
| 970 | // Get the number of arguments and construct an arguments object |
| 971 | // mirror for the right frame. |
| 972 | const int length = frame->ComputeParametersCount(); |
| 973 | Handle<JSObject> arguments = isolate->factory()->NewArgumentsObject( |
| 974 | function, length); |
| 975 | Handle<FixedArray> array = isolate->factory()->NewFixedArray(length); |
| 976 | |
| 977 | // Copy the parameters to the arguments object. |
| 978 | DCHECK(array->length() == length); |
| 979 | for (int i = 0; i < length; i++) array->set(i, frame->GetParameter(i)); |
| 980 | arguments->set_elements(*array); |
| 981 | |
| 982 | // Return the freshly allocated arguments object. |
| 983 | return arguments; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 984 | } |
| 985 | |
| 986 | // No frame corresponding to the given function found. Return null. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 987 | return isolate->factory()->null_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 988 | } |
| 989 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 990 | } // namespace |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 991 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 992 | |
| 993 | Handle<JSObject> Accessors::FunctionGetArguments(Handle<JSFunction> function) { |
| 994 | Handle<Object> arguments = |
| 995 | GetFunctionArguments(function->GetIsolate(), function); |
| 996 | CHECK(arguments->IsJSObject()); |
| 997 | return Handle<JSObject>::cast(arguments); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 998 | } |
| 999 | |
| 1000 | |
| 1001 | void Accessors::FunctionArgumentsGetter( |
| 1002 | v8::Local<v8::Name> name, |
| 1003 | const v8::PropertyCallbackInfo<v8::Value>& info) { |
| 1004 | i::Isolate* isolate = reinterpret_cast<i::Isolate*>(info.GetIsolate()); |
| 1005 | HandleScope scope(isolate); |
| 1006 | Handle<JSFunction> function = |
| 1007 | Handle<JSFunction>::cast(Utils::OpenHandle(*info.Holder())); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 1008 | Handle<Object> result = |
| 1009 | function->shared()->native() |
| 1010 | ? Handle<Object>::cast(isolate->factory()->null_value()) |
| 1011 | : GetFunctionArguments(isolate, function); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1012 | info.GetReturnValue().Set(Utils::ToLocal(result)); |
| 1013 | } |
| 1014 | |
| 1015 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1016 | Handle<AccessorInfo> Accessors::FunctionArgumentsInfo( |
| 1017 | Isolate* isolate, PropertyAttributes attributes) { |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 1018 | return MakeAccessor(isolate, isolate->factory()->arguments_string(), |
| 1019 | &FunctionArgumentsGetter, nullptr, attributes); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1020 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1021 | |
| 1022 | |
| 1023 | // |
| 1024 | // Accessors::FunctionCaller |
| 1025 | // |
| 1026 | |
| 1027 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1028 | static inline bool AllowAccessToFunction(Context* current_context, |
| 1029 | JSFunction* function) { |
| 1030 | return current_context->HasSameSecurityTokenAs(function->context()); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1031 | } |
| 1032 | |
| 1033 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1034 | class FrameFunctionIterator { |
| 1035 | public: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1036 | FrameFunctionIterator(Isolate* isolate, const DisallowHeapAllocation& promise) |
| 1037 | : isolate_(isolate), |
| 1038 | frame_iterator_(isolate), |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1039 | functions_(2), |
| 1040 | index_(0) { |
| 1041 | GetFunctions(); |
| 1042 | } |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1043 | JSFunction* next() { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1044 | while (true) { |
| 1045 | if (functions_.length() == 0) return NULL; |
| 1046 | JSFunction* next_function = functions_[index_]; |
| 1047 | index_--; |
| 1048 | if (index_ < 0) { |
| 1049 | GetFunctions(); |
| 1050 | } |
| 1051 | // Skip functions from other origins. |
| 1052 | if (!AllowAccessToFunction(isolate_->context(), next_function)) continue; |
| 1053 | return next_function; |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1054 | } |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1055 | } |
| 1056 | |
| 1057 | // Iterate through functions until the first occurence of 'function'. |
| 1058 | // Returns true if 'function' is found, and false if the iterator ends |
| 1059 | // without finding it. |
| 1060 | bool Find(JSFunction* function) { |
| 1061 | JSFunction* next_function; |
| 1062 | do { |
| 1063 | next_function = next(); |
| 1064 | if (next_function == function) return true; |
| 1065 | } while (next_function != NULL); |
| 1066 | return false; |
| 1067 | } |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 1068 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1069 | private: |
| 1070 | void GetFunctions() { |
| 1071 | functions_.Rewind(0); |
| 1072 | if (frame_iterator_.done()) return; |
| 1073 | JavaScriptFrame* frame = frame_iterator_.frame(); |
| 1074 | frame->GetFunctions(&functions_); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1075 | DCHECK(functions_.length() > 0); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1076 | frame_iterator_.Advance(); |
| 1077 | index_ = functions_.length() - 1; |
| 1078 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1079 | Isolate* isolate_; |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1080 | JavaScriptFrameIterator frame_iterator_; |
| 1081 | List<JSFunction*> functions_; |
| 1082 | int index_; |
| 1083 | }; |
| 1084 | |
| 1085 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1086 | MaybeHandle<JSFunction> FindCaller(Isolate* isolate, |
| 1087 | Handle<JSFunction> function) { |
| 1088 | DisallowHeapAllocation no_allocation; |
| 1089 | FrameFunctionIterator it(isolate, no_allocation); |
| 1090 | if (function->shared()->native()) { |
| 1091 | return MaybeHandle<JSFunction>(); |
| 1092 | } |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1093 | // Find the function from the frames. |
| 1094 | if (!it.Find(*function)) { |
| 1095 | // No frame corresponding to the given function found. Return null. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1096 | return MaybeHandle<JSFunction>(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1097 | } |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1098 | // Find previously called non-toplevel function. |
| 1099 | JSFunction* caller; |
| 1100 | do { |
| 1101 | caller = it.next(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1102 | if (caller == NULL) return MaybeHandle<JSFunction>(); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1103 | } while (caller->shared()->is_toplevel()); |
| 1104 | |
| 1105 | // If caller is a built-in function and caller's caller is also built-in, |
| 1106 | // use that instead. |
| 1107 | JSFunction* potential_caller = caller; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1108 | while (potential_caller != NULL && potential_caller->shared()->IsBuiltin()) { |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1109 | caller = potential_caller; |
| 1110 | potential_caller = it.next(); |
| 1111 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1112 | if (!caller->shared()->native() && potential_caller != NULL) { |
| 1113 | caller = potential_caller; |
| 1114 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1115 | // Censor if the caller is not a sloppy mode function. |
| 1116 | // Change from ES5, which used to throw, see: |
| 1117 | // https://bugs.ecmascript.org/show_bug.cgi?id=310 |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1118 | if (is_strict(caller->shared()->language_mode())) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1119 | return MaybeHandle<JSFunction>(); |
| 1120 | } |
| 1121 | // Don't return caller from another security context. |
| 1122 | if (!AllowAccessToFunction(isolate->context(), caller)) { |
| 1123 | return MaybeHandle<JSFunction>(); |
| 1124 | } |
| 1125 | return Handle<JSFunction>(caller); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1126 | } |
| 1127 | |
| 1128 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1129 | void Accessors::FunctionCallerGetter( |
| 1130 | v8::Local<v8::Name> name, |
| 1131 | const v8::PropertyCallbackInfo<v8::Value>& info) { |
| 1132 | i::Isolate* isolate = reinterpret_cast<i::Isolate*>(info.GetIsolate()); |
| 1133 | HandleScope scope(isolate); |
| 1134 | Handle<JSFunction> function = |
| 1135 | Handle<JSFunction>::cast(Utils::OpenHandle(*info.Holder())); |
| 1136 | Handle<Object> result; |
| 1137 | MaybeHandle<JSFunction> maybe_caller; |
| 1138 | maybe_caller = FindCaller(isolate, function); |
| 1139 | Handle<JSFunction> caller; |
| 1140 | if (maybe_caller.ToHandle(&caller)) { |
| 1141 | result = caller; |
| 1142 | } else { |
| 1143 | result = isolate->factory()->null_value(); |
| 1144 | } |
| 1145 | info.GetReturnValue().Set(Utils::ToLocal(result)); |
| 1146 | } |
| 1147 | |
| 1148 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1149 | Handle<AccessorInfo> Accessors::FunctionCallerInfo( |
| 1150 | Isolate* isolate, PropertyAttributes attributes) { |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 1151 | return MakeAccessor(isolate, isolate->factory()->caller_string(), |
| 1152 | &FunctionCallerGetter, nullptr, attributes); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1153 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1154 | |
| 1155 | |
| 1156 | // |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1157 | // Accessors::MakeModuleExport |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1158 | // |
| 1159 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 1160 | static void ModuleGetExport(v8::Local<v8::Name> property, |
| 1161 | const v8::PropertyCallbackInfo<v8::Value>& info) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1162 | JSModule* instance = JSModule::cast(*v8::Utils::OpenHandle(*info.Holder())); |
| 1163 | Context* context = Context::cast(instance->context()); |
| 1164 | DCHECK(context->IsModuleContext()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1165 | Isolate* isolate = instance->GetIsolate(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1166 | int slot = info.Data() |
| 1167 | ->Int32Value(info.GetIsolate()->GetCurrentContext()) |
| 1168 | .FromMaybe(-1); |
| 1169 | if (slot < 0 || slot >= context->length()) { |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 1170 | Handle<Name> name = v8::Utils::OpenHandle(*property); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1171 | |
| 1172 | Handle<Object> exception = isolate->factory()->NewReferenceError( |
| 1173 | MessageTemplate::kNotDefined, name); |
| 1174 | isolate->ScheduleThrow(*exception); |
| 1175 | return; |
| 1176 | } |
| 1177 | Object* value = context->get(slot); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1178 | if (value->IsTheHole()) { |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 1179 | Handle<Name> name = v8::Utils::OpenHandle(*property); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1180 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1181 | Handle<Object> exception = isolate->factory()->NewReferenceError( |
| 1182 | MessageTemplate::kNotDefined, name); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1183 | isolate->ScheduleThrow(*exception); |
| 1184 | return; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1185 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1186 | info.GetReturnValue().Set(v8::Utils::ToLocal(Handle<Object>(value, isolate))); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1187 | } |
| 1188 | |
| 1189 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 1190 | static void ModuleSetExport(v8::Local<v8::Name> property, |
| 1191 | v8::Local<v8::Value> value, |
| 1192 | const v8::PropertyCallbackInfo<void>& info) { |
| 1193 | if (!info.ShouldThrowOnError()) return; |
| 1194 | Handle<Name> name = v8::Utils::OpenHandle(*property); |
| 1195 | Isolate* isolate = name->GetIsolate(); |
| 1196 | Handle<Object> exception = |
| 1197 | isolate->factory()->NewTypeError(MessageTemplate::kNotDefined, name); |
| 1198 | isolate->ScheduleThrow(*exception); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1199 | } |
| 1200 | |
| 1201 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1202 | Handle<AccessorInfo> Accessors::MakeModuleExport( |
| 1203 | Handle<String> name, |
| 1204 | int index, |
| 1205 | PropertyAttributes attributes) { |
| 1206 | Isolate* isolate = name->GetIsolate(); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 1207 | Handle<AccessorInfo> info = MakeAccessor(isolate, name, &ModuleGetExport, |
| 1208 | &ModuleSetExport, attributes); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1209 | info->set_data(Smi::FromInt(index)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1210 | return info; |
| 1211 | } |
| 1212 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1213 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1214 | } // namespace internal |
| 1215 | } // namespace v8 |