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