Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 1 | // Copyright 2014 the V8 project authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5 | #include "src/runtime/runtime-utils.h" |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 6 | |
| 7 | #include "src/arguments.h" |
| 8 | #include "src/bootstrapper.h" |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 9 | #include "src/debug/debug.h" |
| 10 | #include "src/isolate-inl.h" |
| 11 | #include "src/messages.h" |
| 12 | #include "src/property-descriptor.h" |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 13 | #include "src/runtime/runtime.h" |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 14 | |
| 15 | namespace v8 { |
| 16 | namespace internal { |
| 17 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 18 | MaybeHandle<Object> Runtime::GetObjectProperty(Isolate* isolate, |
| 19 | Handle<Object> object, |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 20 | Handle<Object> key) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 21 | if (object->IsUndefined() || object->IsNull()) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 22 | THROW_NEW_ERROR( |
| 23 | isolate, |
| 24 | NewTypeError(MessageTemplate::kNonObjectPropertyLoad, key, object), |
| 25 | Object); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 26 | } |
| 27 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 28 | bool success = false; |
| 29 | LookupIterator it = |
| 30 | LookupIterator::PropertyOrElement(isolate, object, key, &success); |
| 31 | if (!success) return MaybeHandle<Object>(); |
| 32 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 33 | return Object::GetProperty(&it); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 34 | } |
| 35 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 36 | static MaybeHandle<Object> KeyedGetObjectProperty(Isolate* isolate, |
| 37 | Handle<Object> receiver_obj, |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 38 | Handle<Object> key_obj) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 39 | // Fast cases for getting named properties of the receiver JSObject |
| 40 | // itself. |
| 41 | // |
| 42 | // The global proxy objects has to be excluded since LookupOwn on |
| 43 | // the global proxy object can return a valid result even though the |
| 44 | // global proxy object never has properties. This is the case |
| 45 | // because the global proxy object forwards everything to its hidden |
| 46 | // prototype including own lookups. |
| 47 | // |
| 48 | // Additionally, we need to make sure that we do not cache results |
| 49 | // for objects that require access checks. |
| 50 | if (receiver_obj->IsJSObject()) { |
| 51 | if (!receiver_obj->IsJSGlobalProxy() && |
| 52 | !receiver_obj->IsAccessCheckNeeded() && key_obj->IsName()) { |
| 53 | DisallowHeapAllocation no_allocation; |
| 54 | Handle<JSObject> receiver = Handle<JSObject>::cast(receiver_obj); |
| 55 | Handle<Name> key = Handle<Name>::cast(key_obj); |
| 56 | if (receiver->IsJSGlobalObject()) { |
| 57 | // Attempt dictionary lookup. |
| 58 | GlobalDictionary* dictionary = receiver->global_dictionary(); |
| 59 | int entry = dictionary->FindEntry(key); |
| 60 | if (entry != GlobalDictionary::kNotFound) { |
| 61 | DCHECK(dictionary->ValueAt(entry)->IsPropertyCell()); |
| 62 | PropertyCell* cell = PropertyCell::cast(dictionary->ValueAt(entry)); |
| 63 | if (cell->property_details().type() == DATA) { |
| 64 | Object* value = cell->value(); |
| 65 | if (!value->IsTheHole()) return Handle<Object>(value, isolate); |
| 66 | // If value is the hole (meaning, absent) do the general lookup. |
| 67 | } |
| 68 | } |
| 69 | } else if (!receiver->HasFastProperties()) { |
| 70 | // Attempt dictionary lookup. |
| 71 | NameDictionary* dictionary = receiver->property_dictionary(); |
| 72 | int entry = dictionary->FindEntry(key); |
| 73 | if ((entry != NameDictionary::kNotFound) && |
| 74 | (dictionary->DetailsAt(entry).type() == DATA)) { |
| 75 | Object* value = dictionary->ValueAt(entry); |
| 76 | return Handle<Object>(value, isolate); |
| 77 | } |
| 78 | } |
| 79 | } else if (key_obj->IsSmi()) { |
| 80 | // JSObject without a name key. If the key is a Smi, check for a |
| 81 | // definite out-of-bounds access to elements, which is a strong indicator |
| 82 | // that subsequent accesses will also call the runtime. Proactively |
| 83 | // transition elements to FAST_*_ELEMENTS to avoid excessive boxing of |
| 84 | // doubles for those future calls in the case that the elements would |
| 85 | // become FAST_DOUBLE_ELEMENTS. |
| 86 | Handle<JSObject> js_object = Handle<JSObject>::cast(receiver_obj); |
| 87 | ElementsKind elements_kind = js_object->GetElementsKind(); |
| 88 | if (IsFastDoubleElementsKind(elements_kind)) { |
| 89 | if (Smi::cast(*key_obj)->value() >= js_object->elements()->length()) { |
| 90 | elements_kind = IsFastHoleyElementsKind(elements_kind) |
| 91 | ? FAST_HOLEY_ELEMENTS |
| 92 | : FAST_ELEMENTS; |
| 93 | JSObject::TransitionElementsKind(js_object, elements_kind); |
| 94 | } |
| 95 | } else { |
| 96 | DCHECK(IsFastSmiOrObjectElementsKind(elements_kind) || |
| 97 | !IsFastElementsKind(elements_kind)); |
| 98 | } |
| 99 | } |
| 100 | } else if (receiver_obj->IsString() && key_obj->IsSmi()) { |
| 101 | // Fast case for string indexing using [] with a smi index. |
| 102 | Handle<String> str = Handle<String>::cast(receiver_obj); |
| 103 | int index = Handle<Smi>::cast(key_obj)->value(); |
| 104 | if (index >= 0 && index < str->length()) { |
| 105 | Factory* factory = isolate->factory(); |
| 106 | return factory->LookupSingleCharacterStringFromCode( |
| 107 | String::Flatten(str)->Get(index)); |
| 108 | } |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 109 | } |
| 110 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 111 | // Fall back to GetObjectProperty. |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 112 | return Runtime::GetObjectProperty(isolate, receiver_obj, key_obj); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 113 | } |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 114 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 115 | |
| 116 | Maybe<bool> Runtime::DeleteObjectProperty(Isolate* isolate, |
| 117 | Handle<JSReceiver> receiver, |
| 118 | Handle<Object> key, |
| 119 | LanguageMode language_mode) { |
| 120 | bool success = false; |
| 121 | LookupIterator it = LookupIterator::PropertyOrElement( |
| 122 | isolate, receiver, key, &success, LookupIterator::HIDDEN); |
| 123 | if (!success) return Nothing<bool>(); |
| 124 | |
| 125 | return JSReceiver::DeleteProperty(&it, language_mode); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 126 | } |
| 127 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 128 | // ES6 19.1.3.2 |
| 129 | RUNTIME_FUNCTION(Runtime_ObjectHasOwnProperty) { |
| 130 | HandleScope scope(isolate); |
| 131 | Handle<Object> property = args.at<Object>(1); |
| 132 | |
| 133 | Handle<Name> key; |
| 134 | uint32_t index; |
| 135 | bool key_is_array_index = property->ToArrayIndex(&index); |
| 136 | |
| 137 | if (!key_is_array_index) { |
| 138 | ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, key, |
| 139 | Object::ToName(isolate, property)); |
| 140 | key_is_array_index = key->AsArrayIndex(&index); |
| 141 | } |
| 142 | |
| 143 | Handle<Object> object = args.at<Object>(0); |
| 144 | |
| 145 | if (object->IsJSObject()) { |
| 146 | Handle<JSObject> js_obj = Handle<JSObject>::cast(object); |
| 147 | // Fast case: either the key is a real named property or it is not |
| 148 | // an array index and there are no interceptors or hidden |
| 149 | // prototypes. |
| 150 | // TODO(jkummerow): Make JSReceiver::HasOwnProperty fast enough to |
| 151 | // handle all cases directly (without this custom fast path). |
| 152 | { |
| 153 | LookupIterator::Configuration c = LookupIterator::OWN_SKIP_INTERCEPTOR; |
| 154 | LookupIterator it = |
| 155 | key_is_array_index ? LookupIterator(isolate, js_obj, index, js_obj, c) |
| 156 | : LookupIterator(js_obj, key, js_obj, c); |
| 157 | Maybe<bool> maybe = JSReceiver::HasProperty(&it); |
| 158 | if (maybe.IsNothing()) return isolate->heap()->exception(); |
| 159 | DCHECK(!isolate->has_pending_exception()); |
| 160 | if (maybe.FromJust()) return isolate->heap()->true_value(); |
| 161 | } |
| 162 | |
| 163 | Map* map = js_obj->map(); |
| 164 | if (!map->has_hidden_prototype() && |
| 165 | (key_is_array_index ? !map->has_indexed_interceptor() |
| 166 | : !map->has_named_interceptor())) { |
| 167 | return isolate->heap()->false_value(); |
| 168 | } |
| 169 | |
| 170 | // Slow case. |
| 171 | LookupIterator::Configuration c = LookupIterator::HIDDEN; |
| 172 | LookupIterator it = key_is_array_index |
| 173 | ? LookupIterator(isolate, js_obj, index, js_obj, c) |
| 174 | : LookupIterator(js_obj, key, js_obj, c); |
| 175 | |
| 176 | Maybe<bool> maybe = JSReceiver::HasProperty(&it); |
| 177 | if (maybe.IsNothing()) return isolate->heap()->exception(); |
| 178 | DCHECK(!isolate->has_pending_exception()); |
| 179 | return isolate->heap()->ToBoolean(maybe.FromJust()); |
| 180 | |
| 181 | } else if (object->IsJSProxy()) { |
| 182 | if (key.is_null()) { |
| 183 | DCHECK(key_is_array_index); |
| 184 | key = isolate->factory()->Uint32ToString(index); |
| 185 | } |
| 186 | |
| 187 | Maybe<bool> result = |
| 188 | JSReceiver::HasOwnProperty(Handle<JSProxy>::cast(object), key); |
| 189 | if (!result.IsJust()) return isolate->heap()->exception(); |
| 190 | return isolate->heap()->ToBoolean(result.FromJust()); |
| 191 | |
| 192 | } else if (object->IsString()) { |
| 193 | return isolate->heap()->ToBoolean( |
| 194 | key_is_array_index |
| 195 | ? index < static_cast<uint32_t>(String::cast(*object)->length()) |
| 196 | : key->Equals(isolate->heap()->length_string())); |
| 197 | } else if (object->IsNull() || object->IsUndefined()) { |
| 198 | THROW_NEW_ERROR_RETURN_FAILURE( |
| 199 | isolate, NewTypeError(MessageTemplate::kUndefinedOrNullToObject)); |
| 200 | } |
| 201 | |
| 202 | return isolate->heap()->false_value(); |
| 203 | } |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 204 | |
| 205 | MaybeHandle<Object> Runtime::SetObjectProperty(Isolate* isolate, |
| 206 | Handle<Object> object, |
| 207 | Handle<Object> key, |
| 208 | Handle<Object> value, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 209 | LanguageMode language_mode) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 210 | if (object->IsUndefined() || object->IsNull()) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 211 | THROW_NEW_ERROR( |
| 212 | isolate, |
| 213 | NewTypeError(MessageTemplate::kNonObjectPropertyStore, key, object), |
| 214 | Object); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | // Check if the given key is an array index. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 218 | bool success = false; |
| 219 | LookupIterator it = |
| 220 | LookupIterator::PropertyOrElement(isolate, object, key, &success); |
| 221 | if (!success) return MaybeHandle<Object>(); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 222 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 223 | MAYBE_RETURN_NULL(Object::SetProperty(&it, value, language_mode, |
| 224 | Object::MAY_BE_STORE_FROM_KEYED)); |
| 225 | return value; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | |
| 229 | RUNTIME_FUNCTION(Runtime_GetPrototype) { |
| 230 | HandleScope scope(isolate); |
| 231 | DCHECK(args.length() == 1); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 232 | CONVERT_ARG_HANDLE_CHECKED(JSReceiver, obj, 0); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 233 | Handle<Object> prototype; |
| 234 | ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, prototype, |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 235 | JSReceiver::GetPrototype(isolate, obj)); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 236 | return *prototype; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | |
| 240 | RUNTIME_FUNCTION(Runtime_InternalSetPrototype) { |
| 241 | HandleScope scope(isolate); |
| 242 | DCHECK(args.length() == 2); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 243 | CONVERT_ARG_HANDLE_CHECKED(JSReceiver, obj, 0); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 244 | CONVERT_ARG_HANDLE_CHECKED(Object, prototype, 1); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 245 | MAYBE_RETURN( |
| 246 | JSReceiver::SetPrototype(obj, prototype, false, Object::THROW_ON_ERROR), |
| 247 | isolate->heap()->exception()); |
| 248 | return *obj; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | |
| 252 | RUNTIME_FUNCTION(Runtime_SetPrototype) { |
| 253 | HandleScope scope(isolate); |
| 254 | DCHECK(args.length() == 2); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 255 | CONVERT_ARG_HANDLE_CHECKED(JSReceiver, obj, 0); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 256 | CONVERT_ARG_HANDLE_CHECKED(Object, prototype, 1); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 257 | MAYBE_RETURN( |
| 258 | JSReceiver::SetPrototype(obj, prototype, true, Object::THROW_ON_ERROR), |
| 259 | isolate->heap()->exception()); |
| 260 | return *obj; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | |
| 264 | // Enumerator used as indices into the array returned from GetOwnProperty |
| 265 | enum PropertyDescriptorIndices { |
| 266 | IS_ACCESSOR_INDEX, |
| 267 | VALUE_INDEX, |
| 268 | GETTER_INDEX, |
| 269 | SETTER_INDEX, |
| 270 | WRITABLE_INDEX, |
| 271 | ENUMERABLE_INDEX, |
| 272 | CONFIGURABLE_INDEX, |
| 273 | DESCRIPTOR_SIZE |
| 274 | }; |
| 275 | |
| 276 | |
| 277 | MUST_USE_RESULT static MaybeHandle<Object> GetOwnProperty(Isolate* isolate, |
| 278 | Handle<JSObject> obj, |
| 279 | Handle<Name> name) { |
| 280 | Heap* heap = isolate->heap(); |
| 281 | Factory* factory = isolate->factory(); |
| 282 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 283 | // Get attributes. |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 284 | LookupIterator it = LookupIterator::PropertyOrElement(isolate, obj, name, obj, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 285 | LookupIterator::HIDDEN); |
| 286 | Maybe<PropertyAttributes> maybe = JSObject::GetPropertyAttributes(&it); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 287 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 288 | if (!maybe.IsJust()) return MaybeHandle<Object>(); |
| 289 | PropertyAttributes attrs = maybe.FromJust(); |
| 290 | if (attrs == ABSENT) return factory->undefined_value(); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 291 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 292 | DCHECK(!isolate->has_pending_exception()); |
| 293 | Handle<FixedArray> elms = factory->NewFixedArray(DESCRIPTOR_SIZE); |
| 294 | elms->set(ENUMERABLE_INDEX, heap->ToBoolean((attrs & DONT_ENUM) == 0)); |
| 295 | elms->set(CONFIGURABLE_INDEX, heap->ToBoolean((attrs & DONT_DELETE) == 0)); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 296 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 297 | bool is_accessor_pair = it.state() == LookupIterator::ACCESSOR && |
| 298 | it.GetAccessors()->IsAccessorPair(); |
| 299 | elms->set(IS_ACCESSOR_INDEX, heap->ToBoolean(is_accessor_pair)); |
| 300 | |
| 301 | if (is_accessor_pair) { |
| 302 | Handle<AccessorPair> accessors = |
| 303 | Handle<AccessorPair>::cast(it.GetAccessors()); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 304 | Handle<Object> getter = |
| 305 | AccessorPair::GetComponent(accessors, ACCESSOR_GETTER); |
| 306 | Handle<Object> setter = |
| 307 | AccessorPair::GetComponent(accessors, ACCESSOR_SETTER); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 308 | elms->set(GETTER_INDEX, *getter); |
| 309 | elms->set(SETTER_INDEX, *setter); |
| 310 | } else { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 311 | Handle<Object> value; |
| 312 | ASSIGN_RETURN_ON_EXCEPTION(isolate, value, Object::GetProperty(&it), |
| 313 | Object); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 314 | elms->set(WRITABLE_INDEX, heap->ToBoolean((attrs & READ_ONLY) == 0)); |
| 315 | elms->set(VALUE_INDEX, *value); |
| 316 | } |
| 317 | |
| 318 | return factory->NewJSArrayWithElements(elms); |
| 319 | } |
| 320 | |
| 321 | |
| 322 | // Returns an array with the property description: |
| 323 | // if args[1] is not a property on args[0] |
| 324 | // returns undefined |
| 325 | // if args[1] is a data property on args[0] |
| 326 | // [false, value, Writeable, Enumerable, Configurable] |
| 327 | // if args[1] is an accessor on args[0] |
| 328 | // [true, GetFunction, SetFunction, Enumerable, Configurable] |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 329 | // TODO(jkummerow): Deprecated. Remove all callers and delete. |
| 330 | RUNTIME_FUNCTION(Runtime_GetOwnProperty_Legacy) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 331 | HandleScope scope(isolate); |
| 332 | DCHECK(args.length() == 2); |
| 333 | CONVERT_ARG_HANDLE_CHECKED(JSObject, obj, 0); |
| 334 | CONVERT_ARG_HANDLE_CHECKED(Name, name, 1); |
| 335 | Handle<Object> result; |
| 336 | ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, result, |
| 337 | GetOwnProperty(isolate, obj, name)); |
| 338 | return *result; |
| 339 | } |
| 340 | |
| 341 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 342 | RUNTIME_FUNCTION(Runtime_OptimizeObjectForAddingMultipleProperties) { |
| 343 | HandleScope scope(isolate); |
| 344 | DCHECK(args.length() == 2); |
| 345 | CONVERT_ARG_HANDLE_CHECKED(JSObject, object, 0); |
| 346 | CONVERT_SMI_ARG_CHECKED(properties, 1); |
| 347 | // Conservative upper limit to prevent fuzz tests from going OOM. |
| 348 | RUNTIME_ASSERT(properties <= 100000); |
| 349 | if (object->HasFastProperties() && !object->IsJSGlobalProxy()) { |
| 350 | JSObject::NormalizeProperties(object, KEEP_INOBJECT_PROPERTIES, properties, |
| 351 | "OptimizeForAdding"); |
| 352 | } |
| 353 | return *object; |
| 354 | } |
| 355 | |
| 356 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 357 | RUNTIME_FUNCTION(Runtime_LoadGlobalViaContext) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 358 | HandleScope scope(isolate); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 359 | DCHECK_EQ(1, args.length()); |
| 360 | CONVERT_SMI_ARG_CHECKED(slot, 0); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 361 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 362 | // Go up context chain to the script context. |
| 363 | Handle<Context> script_context(isolate->context()->script_context(), isolate); |
| 364 | DCHECK(script_context->IsScriptContext()); |
| 365 | DCHECK(script_context->get(slot)->IsPropertyCell()); |
| 366 | |
| 367 | // Lookup the named property on the global object. |
| 368 | Handle<ScopeInfo> scope_info(script_context->scope_info(), isolate); |
| 369 | Handle<Name> name(scope_info->ContextSlotName(slot), isolate); |
| 370 | Handle<JSGlobalObject> global_object(script_context->global_object(), |
| 371 | isolate); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 372 | LookupIterator it(global_object, name, global_object, LookupIterator::HIDDEN); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 373 | |
| 374 | // Switch to fast mode only if there is a data property and it's not on |
| 375 | // a hidden prototype. |
| 376 | if (it.state() == LookupIterator::DATA && |
| 377 | it.GetHolder<Object>().is_identical_to(global_object)) { |
| 378 | // Now update the cell in the script context. |
| 379 | Handle<PropertyCell> cell = it.GetPropertyCell(); |
| 380 | script_context->set(slot, *cell); |
| 381 | } else { |
| 382 | // This is not a fast case, so keep this access in a slow mode. |
| 383 | // Store empty_property_cell here to release the outdated property cell. |
| 384 | script_context->set(slot, isolate->heap()->empty_property_cell()); |
| 385 | } |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 386 | |
| 387 | Handle<Object> result; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 388 | ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, result, Object::GetProperty(&it)); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 389 | return *result; |
| 390 | } |
| 391 | |
| 392 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 393 | namespace { |
| 394 | |
| 395 | Object* StoreGlobalViaContext(Isolate* isolate, int slot, Handle<Object> value, |
| 396 | LanguageMode language_mode) { |
| 397 | // Go up context chain to the script context. |
| 398 | Handle<Context> script_context(isolate->context()->script_context(), isolate); |
| 399 | DCHECK(script_context->IsScriptContext()); |
| 400 | DCHECK(script_context->get(slot)->IsPropertyCell()); |
| 401 | |
| 402 | // Lookup the named property on the global object. |
| 403 | Handle<ScopeInfo> scope_info(script_context->scope_info(), isolate); |
| 404 | Handle<Name> name(scope_info->ContextSlotName(slot), isolate); |
| 405 | Handle<JSGlobalObject> global_object(script_context->global_object(), |
| 406 | isolate); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 407 | LookupIterator it(global_object, name, global_object, LookupIterator::HIDDEN); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 408 | |
| 409 | // Switch to fast mode only if there is a data property and it's not on |
| 410 | // a hidden prototype. |
| 411 | if (it.state() == LookupIterator::DATA && |
| 412 | it.GetHolder<Object>().is_identical_to(global_object)) { |
| 413 | // Now update cell in the script context. |
| 414 | Handle<PropertyCell> cell = it.GetPropertyCell(); |
| 415 | script_context->set(slot, *cell); |
| 416 | } else { |
| 417 | // This is not a fast case, so keep this access in a slow mode. |
| 418 | // Store empty_property_cell here to release the outdated property cell. |
| 419 | script_context->set(slot, isolate->heap()->empty_property_cell()); |
| 420 | } |
| 421 | |
| 422 | MAYBE_RETURN(Object::SetProperty(&it, value, language_mode, |
| 423 | Object::CERTAINLY_NOT_STORE_FROM_KEYED), |
| 424 | isolate->heap()->exception()); |
| 425 | return *value; |
| 426 | } |
| 427 | |
| 428 | } // namespace |
| 429 | |
| 430 | |
| 431 | RUNTIME_FUNCTION(Runtime_StoreGlobalViaContext_Sloppy) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 432 | HandleScope scope(isolate); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 433 | DCHECK_EQ(2, args.length()); |
| 434 | CONVERT_SMI_ARG_CHECKED(slot, 0); |
| 435 | CONVERT_ARG_HANDLE_CHECKED(Object, value, 1); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 436 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 437 | return StoreGlobalViaContext(isolate, slot, value, SLOPPY); |
| 438 | } |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 439 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 440 | |
| 441 | RUNTIME_FUNCTION(Runtime_StoreGlobalViaContext_Strict) { |
| 442 | HandleScope scope(isolate); |
| 443 | DCHECK_EQ(2, args.length()); |
| 444 | CONVERT_SMI_ARG_CHECKED(slot, 0); |
| 445 | CONVERT_ARG_HANDLE_CHECKED(Object, value, 1); |
| 446 | |
| 447 | return StoreGlobalViaContext(isolate, slot, value, STRICT); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 448 | } |
| 449 | |
| 450 | |
| 451 | RUNTIME_FUNCTION(Runtime_GetProperty) { |
| 452 | HandleScope scope(isolate); |
| 453 | DCHECK(args.length() == 2); |
| 454 | |
| 455 | CONVERT_ARG_HANDLE_CHECKED(Object, object, 0); |
| 456 | CONVERT_ARG_HANDLE_CHECKED(Object, key, 1); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 457 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 458 | Handle<Object> result; |
| 459 | ASSIGN_RETURN_FAILURE_ON_EXCEPTION( |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 460 | isolate, result, Runtime::GetObjectProperty(isolate, object, key)); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 461 | return *result; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 462 | } |
| 463 | |
| 464 | |
| 465 | // KeyedGetProperty is called from KeyedLoadIC::GenerateGeneric. |
| 466 | RUNTIME_FUNCTION(Runtime_KeyedGetProperty) { |
| 467 | HandleScope scope(isolate); |
| 468 | DCHECK(args.length() == 2); |
| 469 | |
| 470 | CONVERT_ARG_HANDLE_CHECKED(Object, receiver_obj, 0); |
| 471 | CONVERT_ARG_HANDLE_CHECKED(Object, key_obj, 1); |
| 472 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 473 | Handle<Object> result; |
| 474 | ASSIGN_RETURN_FAILURE_ON_EXCEPTION( |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 475 | isolate, result, KeyedGetObjectProperty(isolate, receiver_obj, key_obj)); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 476 | return *result; |
| 477 | } |
| 478 | |
| 479 | |
| 480 | RUNTIME_FUNCTION(Runtime_AddNamedProperty) { |
| 481 | HandleScope scope(isolate); |
| 482 | RUNTIME_ASSERT(args.length() == 4); |
| 483 | |
| 484 | CONVERT_ARG_HANDLE_CHECKED(JSObject, object, 0); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 485 | CONVERT_ARG_HANDLE_CHECKED(Name, name, 1); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 486 | CONVERT_ARG_HANDLE_CHECKED(Object, value, 2); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 487 | CONVERT_PROPERTY_ATTRIBUTES_CHECKED(attrs, 3); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 488 | |
| 489 | #ifdef DEBUG |
| 490 | uint32_t index = 0; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 491 | DCHECK(!name->ToArrayIndex(&index)); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 492 | LookupIterator it(object, name, object, LookupIterator::OWN_SKIP_INTERCEPTOR); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 493 | Maybe<PropertyAttributes> maybe = JSReceiver::GetPropertyAttributes(&it); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 494 | if (!maybe.IsJust()) return isolate->heap()->exception(); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 495 | RUNTIME_ASSERT(!it.IsFound()); |
| 496 | #endif |
| 497 | |
| 498 | Handle<Object> result; |
| 499 | ASSIGN_RETURN_FAILURE_ON_EXCEPTION( |
| 500 | isolate, result, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 501 | JSObject::SetOwnPropertyIgnoreAttributes(object, name, value, attrs)); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 502 | return *result; |
| 503 | } |
| 504 | |
| 505 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 506 | // Adds an element to an array. |
| 507 | // This is used to create an indexed data property into an array. |
| 508 | RUNTIME_FUNCTION(Runtime_AddElement) { |
| 509 | HandleScope scope(isolate); |
| 510 | RUNTIME_ASSERT(args.length() == 3); |
| 511 | |
| 512 | CONVERT_ARG_HANDLE_CHECKED(JSObject, object, 0); |
| 513 | CONVERT_ARG_HANDLE_CHECKED(Object, key, 1); |
| 514 | CONVERT_ARG_HANDLE_CHECKED(Object, value, 2); |
| 515 | |
| 516 | uint32_t index = 0; |
| 517 | CHECK(key->ToArrayIndex(&index)); |
| 518 | |
| 519 | #ifdef DEBUG |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 520 | LookupIterator it(isolate, object, index, object, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 521 | LookupIterator::OWN_SKIP_INTERCEPTOR); |
| 522 | Maybe<PropertyAttributes> maybe = JSReceiver::GetPropertyAttributes(&it); |
| 523 | if (!maybe.IsJust()) return isolate->heap()->exception(); |
| 524 | RUNTIME_ASSERT(!it.IsFound()); |
| 525 | |
| 526 | if (object->IsJSArray()) { |
| 527 | Handle<JSArray> array = Handle<JSArray>::cast(object); |
| 528 | RUNTIME_ASSERT(!JSArray::WouldChangeReadOnlyLength(array, index)); |
| 529 | } |
| 530 | #endif |
| 531 | |
| 532 | Handle<Object> result; |
| 533 | ASSIGN_RETURN_FAILURE_ON_EXCEPTION( |
| 534 | isolate, result, |
| 535 | JSObject::SetOwnElementIgnoreAttributes(object, index, value, NONE)); |
| 536 | return *result; |
| 537 | } |
| 538 | |
| 539 | |
| 540 | RUNTIME_FUNCTION(Runtime_AppendElement) { |
| 541 | HandleScope scope(isolate); |
| 542 | RUNTIME_ASSERT(args.length() == 2); |
| 543 | |
| 544 | CONVERT_ARG_HANDLE_CHECKED(JSArray, array, 0); |
| 545 | CONVERT_ARG_HANDLE_CHECKED(Object, value, 1); |
| 546 | |
| 547 | uint32_t index; |
| 548 | CHECK(array->length()->ToArrayIndex(&index)); |
| 549 | |
| 550 | Handle<Object> result; |
| 551 | ASSIGN_RETURN_FAILURE_ON_EXCEPTION( |
| 552 | isolate, result, JSObject::AddDataElement(array, index, value, NONE)); |
| 553 | JSObject::ValidateElements(array); |
| 554 | return *array; |
| 555 | } |
| 556 | |
| 557 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 558 | RUNTIME_FUNCTION(Runtime_SetProperty) { |
| 559 | HandleScope scope(isolate); |
| 560 | RUNTIME_ASSERT(args.length() == 4); |
| 561 | |
| 562 | CONVERT_ARG_HANDLE_CHECKED(Object, object, 0); |
| 563 | CONVERT_ARG_HANDLE_CHECKED(Object, key, 1); |
| 564 | CONVERT_ARG_HANDLE_CHECKED(Object, value, 2); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 565 | CONVERT_LANGUAGE_MODE_ARG_CHECKED(language_mode_arg, 3); |
| 566 | LanguageMode language_mode = language_mode_arg; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 567 | |
| 568 | Handle<Object> result; |
| 569 | ASSIGN_RETURN_FAILURE_ON_EXCEPTION( |
| 570 | isolate, result, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 571 | Runtime::SetObjectProperty(isolate, object, key, value, language_mode)); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 572 | return *result; |
| 573 | } |
| 574 | |
| 575 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 576 | namespace { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 577 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 578 | // ES6 section 12.5.4. |
| 579 | Object* DeleteProperty(Isolate* isolate, Handle<Object> object, |
| 580 | Handle<Object> key, LanguageMode language_mode) { |
| 581 | Handle<JSReceiver> receiver; |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 582 | ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, receiver, |
| 583 | Object::ToObject(isolate, object)); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 584 | Maybe<bool> result = |
| 585 | Runtime::DeleteObjectProperty(isolate, receiver, key, language_mode); |
| 586 | MAYBE_RETURN(result, isolate->heap()->exception()); |
| 587 | return isolate->heap()->ToBoolean(result.FromJust()); |
| 588 | } |
| 589 | |
| 590 | } // namespace |
| 591 | |
| 592 | |
| 593 | RUNTIME_FUNCTION(Runtime_DeleteProperty_Sloppy) { |
| 594 | HandleScope scope(isolate); |
| 595 | DCHECK_EQ(2, args.length()); |
| 596 | CONVERT_ARG_HANDLE_CHECKED(Object, object, 0); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 597 | CONVERT_ARG_HANDLE_CHECKED(Object, key, 1); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 598 | return DeleteProperty(isolate, object, key, SLOPPY); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 599 | } |
| 600 | |
| 601 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 602 | RUNTIME_FUNCTION(Runtime_DeleteProperty_Strict) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 603 | HandleScope scope(isolate); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 604 | DCHECK_EQ(2, args.length()); |
| 605 | CONVERT_ARG_HANDLE_CHECKED(Object, object, 0); |
| 606 | CONVERT_ARG_HANDLE_CHECKED(Object, key, 1); |
| 607 | return DeleteProperty(isolate, object, key, STRICT); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 608 | } |
| 609 | |
| 610 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 611 | // ES6 section 12.9.3, operator in. |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 612 | RUNTIME_FUNCTION(Runtime_HasProperty) { |
| 613 | HandleScope scope(isolate); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 614 | DCHECK_EQ(2, args.length()); |
| 615 | CONVERT_ARG_HANDLE_CHECKED(Object, key, 0); |
| 616 | CONVERT_ARG_HANDLE_CHECKED(Object, object, 1); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 617 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 618 | // Check that {object} is actually a receiver. |
| 619 | if (!object->IsJSReceiver()) { |
| 620 | THROW_NEW_ERROR_RETURN_FAILURE( |
| 621 | isolate, |
| 622 | NewTypeError(MessageTemplate::kInvalidInOperatorUse, key, object)); |
| 623 | } |
| 624 | Handle<JSReceiver> receiver = Handle<JSReceiver>::cast(object); |
| 625 | |
| 626 | // Convert the {key} to a name. |
| 627 | Handle<Name> name; |
| 628 | ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, name, |
| 629 | Object::ToName(isolate, key)); |
| 630 | |
| 631 | // Lookup the {name} on {receiver}. |
| 632 | Maybe<bool> maybe = JSReceiver::HasProperty(receiver, name); |
| 633 | if (!maybe.IsJust()) return isolate->heap()->exception(); |
| 634 | return isolate->heap()->ToBoolean(maybe.FromJust()); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 635 | } |
| 636 | |
| 637 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 638 | RUNTIME_FUNCTION(Runtime_PropertyIsEnumerable) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 639 | HandleScope scope(isolate); |
| 640 | DCHECK(args.length() == 2); |
| 641 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 642 | CONVERT_ARG_HANDLE_CHECKED(JSReceiver, object, 0); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 643 | CONVERT_ARG_HANDLE_CHECKED(Name, key, 1); |
| 644 | |
| 645 | Maybe<PropertyAttributes> maybe = |
| 646 | JSReceiver::GetOwnPropertyAttributes(object, key); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 647 | if (!maybe.IsJust()) return isolate->heap()->exception(); |
| 648 | if (maybe.FromJust() == ABSENT) return isolate->heap()->false_value(); |
| 649 | return isolate->heap()->ToBoolean((maybe.FromJust() & DONT_ENUM) == 0); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 650 | } |
| 651 | |
| 652 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 653 | RUNTIME_FUNCTION(Runtime_GetOwnPropertyKeys) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 654 | HandleScope scope(isolate); |
| 655 | DCHECK(args.length() == 2); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 656 | CONVERT_ARG_HANDLE_CHECKED(JSReceiver, object, 0); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 657 | CONVERT_SMI_ARG_CHECKED(filter_value, 1); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 658 | PropertyFilter filter = static_cast<PropertyFilter>(filter_value); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 659 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 660 | Handle<FixedArray> keys; |
| 661 | ASSIGN_RETURN_FAILURE_ON_EXCEPTION( |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 662 | isolate, keys, |
| 663 | JSReceiver::GetKeys(object, OWN_ONLY, filter, CONVERT_TO_STRING)); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 664 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 665 | return *isolate->factory()->NewJSArrayWithElements(keys); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 666 | } |
| 667 | |
| 668 | |
| 669 | // Return information on whether an object has a named or indexed interceptor. |
| 670 | // args[0]: object |
| 671 | RUNTIME_FUNCTION(Runtime_GetInterceptorInfo) { |
| 672 | HandleScope scope(isolate); |
| 673 | DCHECK(args.length() == 1); |
| 674 | if (!args[0]->IsJSObject()) { |
| 675 | return Smi::FromInt(0); |
| 676 | } |
| 677 | CONVERT_ARG_HANDLE_CHECKED(JSObject, obj, 0); |
| 678 | |
| 679 | int result = 0; |
| 680 | if (obj->HasNamedInterceptor()) result |= 2; |
| 681 | if (obj->HasIndexedInterceptor()) result |= 1; |
| 682 | |
| 683 | return Smi::FromInt(result); |
| 684 | } |
| 685 | |
| 686 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 687 | RUNTIME_FUNCTION(Runtime_ToFastProperties) { |
| 688 | HandleScope scope(isolate); |
| 689 | DCHECK(args.length() == 1); |
| 690 | CONVERT_ARG_HANDLE_CHECKED(Object, object, 0); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 691 | if (object->IsJSObject() && !object->IsJSGlobalObject()) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 692 | JSObject::MigrateSlowToFast(Handle<JSObject>::cast(object), 0, |
| 693 | "RuntimeToFastProperties"); |
| 694 | } |
| 695 | return *object; |
| 696 | } |
| 697 | |
| 698 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 699 | RUNTIME_FUNCTION(Runtime_AllocateHeapNumber) { |
| 700 | HandleScope scope(isolate); |
| 701 | DCHECK(args.length() == 0); |
| 702 | return *isolate->factory()->NewHeapNumber(0); |
| 703 | } |
| 704 | |
| 705 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 706 | RUNTIME_FUNCTION(Runtime_NewObject) { |
| 707 | HandleScope scope(isolate); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 708 | DCHECK_EQ(2, args.length()); |
| 709 | CONVERT_ARG_HANDLE_CHECKED(JSFunction, target, 0); |
| 710 | CONVERT_ARG_HANDLE_CHECKED(JSReceiver, new_target, 1); |
| 711 | Handle<JSObject> result; |
| 712 | ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, result, |
| 713 | JSObject::New(target, new_target)); |
| 714 | return *result; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 715 | } |
| 716 | |
| 717 | |
| 718 | RUNTIME_FUNCTION(Runtime_FinalizeInstanceSize) { |
| 719 | HandleScope scope(isolate); |
| 720 | DCHECK(args.length() == 1); |
| 721 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 722 | CONVERT_ARG_HANDLE_CHECKED(Map, initial_map, 0); |
| 723 | initial_map->CompleteInobjectSlackTracking(); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 724 | |
| 725 | return isolate->heap()->undefined_value(); |
| 726 | } |
| 727 | |
| 728 | |
| 729 | RUNTIME_FUNCTION(Runtime_GlobalProxy) { |
| 730 | SealHandleScope shs(isolate); |
| 731 | DCHECK(args.length() == 1); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 732 | CONVERT_ARG_CHECKED(JSFunction, function, 0); |
| 733 | return function->context()->global_proxy(); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 734 | } |
| 735 | |
| 736 | |
| 737 | RUNTIME_FUNCTION(Runtime_LookupAccessor) { |
| 738 | HandleScope scope(isolate); |
| 739 | DCHECK(args.length() == 3); |
| 740 | CONVERT_ARG_HANDLE_CHECKED(JSReceiver, receiver, 0); |
| 741 | CONVERT_ARG_HANDLE_CHECKED(Name, name, 1); |
| 742 | CONVERT_SMI_ARG_CHECKED(flag, 2); |
| 743 | AccessorComponent component = flag == 0 ? ACCESSOR_GETTER : ACCESSOR_SETTER; |
| 744 | if (!receiver->IsJSObject()) return isolate->heap()->undefined_value(); |
| 745 | Handle<Object> result; |
| 746 | ASSIGN_RETURN_FAILURE_ON_EXCEPTION( |
| 747 | isolate, result, |
| 748 | JSObject::GetAccessor(Handle<JSObject>::cast(receiver), name, component)); |
| 749 | return *result; |
| 750 | } |
| 751 | |
| 752 | |
| 753 | RUNTIME_FUNCTION(Runtime_LoadMutableDouble) { |
| 754 | HandleScope scope(isolate); |
| 755 | DCHECK(args.length() == 2); |
| 756 | CONVERT_ARG_HANDLE_CHECKED(JSObject, object, 0); |
| 757 | CONVERT_ARG_HANDLE_CHECKED(Smi, index, 1); |
| 758 | RUNTIME_ASSERT((index->value() & 1) == 1); |
| 759 | FieldIndex field_index = |
| 760 | FieldIndex::ForLoadByFieldIndex(object->map(), index->value()); |
| 761 | if (field_index.is_inobject()) { |
| 762 | RUNTIME_ASSERT(field_index.property_index() < |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 763 | object->map()->GetInObjectProperties()); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 764 | } else { |
| 765 | RUNTIME_ASSERT(field_index.outobject_array_index() < |
| 766 | object->properties()->length()); |
| 767 | } |
| 768 | return *JSObject::FastPropertyAt(object, Representation::Double(), |
| 769 | field_index); |
| 770 | } |
| 771 | |
| 772 | |
| 773 | RUNTIME_FUNCTION(Runtime_TryMigrateInstance) { |
| 774 | HandleScope scope(isolate); |
| 775 | DCHECK(args.length() == 1); |
| 776 | CONVERT_ARG_HANDLE_CHECKED(Object, object, 0); |
| 777 | if (!object->IsJSObject()) return Smi::FromInt(0); |
| 778 | Handle<JSObject> js_object = Handle<JSObject>::cast(object); |
| 779 | if (!js_object->map()->is_deprecated()) return Smi::FromInt(0); |
| 780 | // This call must not cause lazy deopts, because it's called from deferred |
| 781 | // code where we can't handle lazy deopts for lack of a suitable bailout |
| 782 | // ID. So we just try migration and signal failure if necessary, |
| 783 | // which will also trigger a deopt. |
| 784 | if (!JSObject::TryMigrateInstance(js_object)) return Smi::FromInt(0); |
| 785 | return *object; |
| 786 | } |
| 787 | |
| 788 | |
| 789 | RUNTIME_FUNCTION(Runtime_IsJSGlobalProxy) { |
| 790 | SealHandleScope shs(isolate); |
| 791 | DCHECK(args.length() == 1); |
| 792 | CONVERT_ARG_CHECKED(Object, obj, 0); |
| 793 | return isolate->heap()->ToBoolean(obj->IsJSGlobalProxy()); |
| 794 | } |
| 795 | |
| 796 | |
| 797 | static bool IsValidAccessor(Handle<Object> obj) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 798 | return obj->IsUndefined() || obj->IsCallable() || obj->IsNull(); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 799 | } |
| 800 | |
| 801 | |
| 802 | // Implements part of 8.12.9 DefineOwnProperty. |
| 803 | // There are 3 cases that lead here: |
| 804 | // Step 4b - define a new accessor property. |
| 805 | // Steps 9c & 12 - replace an existing data property with an accessor property. |
| 806 | // Step 12 - update an existing accessor property with an accessor or generic |
| 807 | // descriptor. |
| 808 | RUNTIME_FUNCTION(Runtime_DefineAccessorPropertyUnchecked) { |
| 809 | HandleScope scope(isolate); |
| 810 | DCHECK(args.length() == 5); |
| 811 | CONVERT_ARG_HANDLE_CHECKED(JSObject, obj, 0); |
| 812 | RUNTIME_ASSERT(!obj->IsNull()); |
| 813 | CONVERT_ARG_HANDLE_CHECKED(Name, name, 1); |
| 814 | CONVERT_ARG_HANDLE_CHECKED(Object, getter, 2); |
| 815 | RUNTIME_ASSERT(IsValidAccessor(getter)); |
| 816 | CONVERT_ARG_HANDLE_CHECKED(Object, setter, 3); |
| 817 | RUNTIME_ASSERT(IsValidAccessor(setter)); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 818 | CONVERT_PROPERTY_ATTRIBUTES_CHECKED(attrs, 4); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 819 | |
| 820 | RETURN_FAILURE_ON_EXCEPTION( |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 821 | isolate, JSObject::DefineAccessor(obj, name, getter, setter, attrs)); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 822 | return isolate->heap()->undefined_value(); |
| 823 | } |
| 824 | |
| 825 | |
| 826 | // Implements part of 8.12.9 DefineOwnProperty. |
| 827 | // There are 3 cases that lead here: |
| 828 | // Step 4a - define a new data property. |
| 829 | // Steps 9b & 12 - replace an existing accessor property with a data property. |
| 830 | // Step 12 - update an existing data property with a data or generic |
| 831 | // descriptor. |
| 832 | RUNTIME_FUNCTION(Runtime_DefineDataPropertyUnchecked) { |
| 833 | HandleScope scope(isolate); |
| 834 | DCHECK(args.length() == 4); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 835 | CONVERT_ARG_HANDLE_CHECKED(JSObject, object, 0); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 836 | CONVERT_ARG_HANDLE_CHECKED(Name, name, 1); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 837 | CONVERT_ARG_HANDLE_CHECKED(Object, value, 2); |
| 838 | CONVERT_PROPERTY_ATTRIBUTES_CHECKED(attrs, 3); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 839 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 840 | LookupIterator it = LookupIterator::PropertyOrElement( |
| 841 | isolate, object, name, object, LookupIterator::OWN); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 842 | if (it.state() == LookupIterator::ACCESS_CHECK && !it.HasAccess()) { |
| 843 | return isolate->heap()->undefined_value(); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 844 | } |
| 845 | |
| 846 | Handle<Object> result; |
| 847 | ASSIGN_RETURN_FAILURE_ON_EXCEPTION( |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 848 | isolate, result, JSObject::DefineOwnPropertyIgnoreAttributes( |
| 849 | &it, value, attrs, JSObject::DONT_FORCE_FIELD)); |
| 850 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 851 | return *result; |
| 852 | } |
| 853 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 854 | RUNTIME_FUNCTION(Runtime_DefineDataPropertyInLiteral) { |
| 855 | HandleScope scope(isolate); |
| 856 | DCHECK(args.length() == 5); |
| 857 | CONVERT_ARG_HANDLE_CHECKED(JSObject, object, 0); |
| 858 | CONVERT_ARG_HANDLE_CHECKED(Name, name, 1); |
| 859 | CONVERT_ARG_HANDLE_CHECKED(Object, value, 2); |
| 860 | CONVERT_PROPERTY_ATTRIBUTES_CHECKED(attrs, 3); |
| 861 | CONVERT_SMI_ARG_CHECKED(set_function_name, 4); |
| 862 | |
| 863 | if (FLAG_harmony_function_name && set_function_name) { |
| 864 | DCHECK(value->IsJSFunction()); |
| 865 | JSFunction::SetName(Handle<JSFunction>::cast(value), name, |
| 866 | isolate->factory()->empty_string()); |
| 867 | } |
| 868 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 869 | LookupIterator it = LookupIterator::PropertyOrElement( |
| 870 | isolate, object, name, object, LookupIterator::OWN); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 871 | // Cannot fail since this should only be called when |
| 872 | // creating an object literal. |
| 873 | CHECK(JSObject::DefineOwnPropertyIgnoreAttributes(&it, value, attrs, |
| 874 | Object::DONT_THROW) |
| 875 | .IsJust()); |
| 876 | return *object; |
| 877 | } |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 878 | |
| 879 | // Return property without being observable by accessors or interceptors. |
| 880 | RUNTIME_FUNCTION(Runtime_GetDataProperty) { |
| 881 | HandleScope scope(isolate); |
| 882 | DCHECK(args.length() == 2); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 883 | CONVERT_ARG_HANDLE_CHECKED(JSReceiver, object, 0); |
| 884 | CONVERT_ARG_HANDLE_CHECKED(Name, name, 1); |
| 885 | return *JSReceiver::GetDataProperty(object, name); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 886 | } |
| 887 | |
| 888 | |
| 889 | RUNTIME_FUNCTION(Runtime_HasFastPackedElements) { |
| 890 | SealHandleScope shs(isolate); |
| 891 | DCHECK(args.length() == 1); |
| 892 | CONVERT_ARG_CHECKED(HeapObject, obj, 0); |
| 893 | return isolate->heap()->ToBoolean( |
| 894 | IsFastPackedElementsKind(obj->map()->elements_kind())); |
| 895 | } |
| 896 | |
| 897 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 898 | RUNTIME_FUNCTION(Runtime_ValueOf) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 899 | SealHandleScope shs(isolate); |
| 900 | DCHECK(args.length() == 1); |
| 901 | CONVERT_ARG_CHECKED(Object, obj, 0); |
| 902 | if (!obj->IsJSValue()) return obj; |
| 903 | return JSValue::cast(obj)->value(); |
| 904 | } |
| 905 | |
| 906 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 907 | RUNTIME_FUNCTION(Runtime_IsJSReceiver) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 908 | SealHandleScope shs(isolate); |
| 909 | DCHECK(args.length() == 1); |
| 910 | CONVERT_ARG_CHECKED(Object, obj, 0); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 911 | return isolate->heap()->ToBoolean(obj->IsJSReceiver()); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 912 | } |
| 913 | |
| 914 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 915 | RUNTIME_FUNCTION(Runtime_ClassOf) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 916 | SealHandleScope shs(isolate); |
| 917 | DCHECK(args.length() == 1); |
| 918 | CONVERT_ARG_CHECKED(Object, obj, 0); |
| 919 | if (!obj->IsJSReceiver()) return isolate->heap()->null_value(); |
| 920 | return JSReceiver::cast(obj)->class_name(); |
| 921 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 922 | |
| 923 | |
| 924 | RUNTIME_FUNCTION(Runtime_DefineGetterPropertyUnchecked) { |
| 925 | HandleScope scope(isolate); |
| 926 | DCHECK(args.length() == 4); |
| 927 | CONVERT_ARG_HANDLE_CHECKED(JSObject, object, 0); |
| 928 | CONVERT_ARG_HANDLE_CHECKED(Name, name, 1); |
| 929 | CONVERT_ARG_HANDLE_CHECKED(JSFunction, getter, 2); |
| 930 | CONVERT_PROPERTY_ATTRIBUTES_CHECKED(attrs, 3); |
| 931 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 932 | if (FLAG_harmony_function_name && |
| 933 | String::cast(getter->shared()->name())->length() == 0) { |
| 934 | JSFunction::SetName(getter, name, isolate->factory()->get_string()); |
| 935 | } |
| 936 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 937 | RETURN_FAILURE_ON_EXCEPTION( |
| 938 | isolate, |
| 939 | JSObject::DefineAccessor(object, name, getter, |
| 940 | isolate->factory()->null_value(), attrs)); |
| 941 | return isolate->heap()->undefined_value(); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 942 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 943 | |
| 944 | |
| 945 | RUNTIME_FUNCTION(Runtime_DefineSetterPropertyUnchecked) { |
| 946 | HandleScope scope(isolate); |
| 947 | DCHECK(args.length() == 4); |
| 948 | CONVERT_ARG_HANDLE_CHECKED(JSObject, object, 0); |
| 949 | CONVERT_ARG_HANDLE_CHECKED(Name, name, 1); |
| 950 | CONVERT_ARG_HANDLE_CHECKED(JSFunction, setter, 2); |
| 951 | CONVERT_PROPERTY_ATTRIBUTES_CHECKED(attrs, 3); |
| 952 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 953 | if (FLAG_harmony_function_name && |
| 954 | String::cast(setter->shared()->name())->length() == 0) { |
| 955 | JSFunction::SetName(setter, name, isolate->factory()->set_string()); |
| 956 | } |
| 957 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 958 | RETURN_FAILURE_ON_EXCEPTION( |
| 959 | isolate, |
| 960 | JSObject::DefineAccessor(object, name, isolate->factory()->null_value(), |
| 961 | setter, attrs)); |
| 962 | return isolate->heap()->undefined_value(); |
| 963 | } |
| 964 | |
| 965 | |
| 966 | RUNTIME_FUNCTION(Runtime_ToObject) { |
| 967 | HandleScope scope(isolate); |
| 968 | DCHECK_EQ(1, args.length()); |
| 969 | CONVERT_ARG_HANDLE_CHECKED(Object, object, 0); |
| 970 | Handle<JSReceiver> receiver; |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 971 | ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, receiver, |
| 972 | Object::ToObject(isolate, object)); |
| 973 | return *receiver; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 974 | } |
| 975 | |
| 976 | |
| 977 | RUNTIME_FUNCTION(Runtime_ToPrimitive) { |
| 978 | HandleScope scope(isolate); |
| 979 | DCHECK_EQ(1, args.length()); |
| 980 | CONVERT_ARG_HANDLE_CHECKED(Object, input, 0); |
| 981 | Handle<Object> result; |
| 982 | ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, result, |
| 983 | Object::ToPrimitive(input)); |
| 984 | return *result; |
| 985 | } |
| 986 | |
| 987 | |
| 988 | RUNTIME_FUNCTION(Runtime_ToPrimitive_Number) { |
| 989 | HandleScope scope(isolate); |
| 990 | DCHECK_EQ(1, args.length()); |
| 991 | CONVERT_ARG_HANDLE_CHECKED(Object, input, 0); |
| 992 | Handle<Object> result; |
| 993 | ASSIGN_RETURN_FAILURE_ON_EXCEPTION( |
| 994 | isolate, result, Object::ToPrimitive(input, ToPrimitiveHint::kNumber)); |
| 995 | return *result; |
| 996 | } |
| 997 | |
| 998 | |
| 999 | RUNTIME_FUNCTION(Runtime_ToPrimitive_String) { |
| 1000 | HandleScope scope(isolate); |
| 1001 | DCHECK_EQ(1, args.length()); |
| 1002 | CONVERT_ARG_HANDLE_CHECKED(Object, input, 0); |
| 1003 | Handle<Object> result; |
| 1004 | ASSIGN_RETURN_FAILURE_ON_EXCEPTION( |
| 1005 | isolate, result, Object::ToPrimitive(input, ToPrimitiveHint::kString)); |
| 1006 | return *result; |
| 1007 | } |
| 1008 | |
| 1009 | |
| 1010 | RUNTIME_FUNCTION(Runtime_ToNumber) { |
| 1011 | HandleScope scope(isolate); |
| 1012 | DCHECK_EQ(1, args.length()); |
| 1013 | CONVERT_ARG_HANDLE_CHECKED(Object, input, 0); |
| 1014 | Handle<Object> result; |
| 1015 | ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, result, Object::ToNumber(input)); |
| 1016 | return *result; |
| 1017 | } |
| 1018 | |
| 1019 | |
| 1020 | RUNTIME_FUNCTION(Runtime_ToInteger) { |
| 1021 | HandleScope scope(isolate); |
| 1022 | DCHECK_EQ(1, args.length()); |
| 1023 | CONVERT_ARG_HANDLE_CHECKED(Object, input, 0); |
| 1024 | Handle<Object> result; |
| 1025 | ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, result, |
| 1026 | Object::ToInteger(isolate, input)); |
| 1027 | return *result; |
| 1028 | } |
| 1029 | |
| 1030 | |
| 1031 | RUNTIME_FUNCTION(Runtime_ToLength) { |
| 1032 | HandleScope scope(isolate); |
| 1033 | DCHECK_EQ(1, args.length()); |
| 1034 | CONVERT_ARG_HANDLE_CHECKED(Object, input, 0); |
| 1035 | Handle<Object> result; |
| 1036 | ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, result, |
| 1037 | Object::ToLength(isolate, input)); |
| 1038 | return *result; |
| 1039 | } |
| 1040 | |
| 1041 | |
| 1042 | RUNTIME_FUNCTION(Runtime_ToString) { |
| 1043 | HandleScope scope(isolate); |
| 1044 | DCHECK_EQ(1, args.length()); |
| 1045 | CONVERT_ARG_HANDLE_CHECKED(Object, input, 0); |
| 1046 | Handle<Object> result; |
| 1047 | ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, result, |
| 1048 | Object::ToString(isolate, input)); |
| 1049 | return *result; |
| 1050 | } |
| 1051 | |
| 1052 | |
| 1053 | RUNTIME_FUNCTION(Runtime_ToName) { |
| 1054 | HandleScope scope(isolate); |
| 1055 | DCHECK_EQ(1, args.length()); |
| 1056 | CONVERT_ARG_HANDLE_CHECKED(Object, input, 0); |
| 1057 | Handle<Object> result; |
| 1058 | ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, result, |
| 1059 | Object::ToName(isolate, input)); |
| 1060 | return *result; |
| 1061 | } |
| 1062 | |
| 1063 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 1064 | RUNTIME_FUNCTION(Runtime_SameValue) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1065 | SealHandleScope scope(isolate); |
| 1066 | DCHECK_EQ(2, args.length()); |
| 1067 | CONVERT_ARG_CHECKED(Object, x, 0); |
| 1068 | CONVERT_ARG_CHECKED(Object, y, 1); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 1069 | return isolate->heap()->ToBoolean(x->SameValue(y)); |
| 1070 | } |
| 1071 | |
| 1072 | |
| 1073 | RUNTIME_FUNCTION(Runtime_SameValueZero) { |
| 1074 | SealHandleScope scope(isolate); |
| 1075 | DCHECK_EQ(2, args.length()); |
| 1076 | CONVERT_ARG_CHECKED(Object, x, 0); |
| 1077 | CONVERT_ARG_CHECKED(Object, y, 1); |
| 1078 | return isolate->heap()->ToBoolean(x->SameValueZero(y)); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1079 | } |
| 1080 | |
| 1081 | |
| 1082 | // TODO(bmeurer): Kill this special wrapper and use TF compatible LessThan, |
| 1083 | // GreaterThan, etc. which return true or false. |
| 1084 | RUNTIME_FUNCTION(Runtime_Compare) { |
| 1085 | HandleScope scope(isolate); |
| 1086 | DCHECK_EQ(3, args.length()); |
| 1087 | CONVERT_ARG_HANDLE_CHECKED(Object, x, 0); |
| 1088 | CONVERT_ARG_HANDLE_CHECKED(Object, y, 1); |
| 1089 | CONVERT_ARG_HANDLE_CHECKED(Object, ncr, 2); |
| 1090 | Maybe<ComparisonResult> result = Object::Compare(x, y); |
| 1091 | if (result.IsJust()) { |
| 1092 | switch (result.FromJust()) { |
| 1093 | case ComparisonResult::kLessThan: |
| 1094 | return Smi::FromInt(LESS); |
| 1095 | case ComparisonResult::kEqual: |
| 1096 | return Smi::FromInt(EQUAL); |
| 1097 | case ComparisonResult::kGreaterThan: |
| 1098 | return Smi::FromInt(GREATER); |
| 1099 | case ComparisonResult::kUndefined: |
| 1100 | return *ncr; |
| 1101 | } |
| 1102 | UNREACHABLE(); |
| 1103 | } |
| 1104 | return isolate->heap()->exception(); |
| 1105 | } |
| 1106 | |
| 1107 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1108 | RUNTIME_FUNCTION(Runtime_InstanceOf) { |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 1109 | // TODO(4447): Remove this function when ES6 instanceof ships for good. |
| 1110 | DCHECK(!FLAG_harmony_instanceof); |
| 1111 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1112 | // ECMA-262, section 11.8.6, page 54. |
| 1113 | HandleScope shs(isolate); |
| 1114 | DCHECK_EQ(2, args.length()); |
| 1115 | DCHECK(args.length() == 2); |
| 1116 | CONVERT_ARG_HANDLE_CHECKED(Object, object, 0); |
| 1117 | CONVERT_ARG_HANDLE_CHECKED(Object, callable, 1); |
| 1118 | // {callable} must have a [[Call]] internal method. |
| 1119 | if (!callable->IsCallable()) { |
| 1120 | THROW_NEW_ERROR_RETURN_FAILURE( |
| 1121 | isolate, |
| 1122 | NewTypeError(MessageTemplate::kInstanceofFunctionExpected, callable)); |
| 1123 | } |
| 1124 | // If {object} is not a receiver, return false. |
| 1125 | if (!object->IsJSReceiver()) { |
| 1126 | return isolate->heap()->false_value(); |
| 1127 | } |
| 1128 | // Check if {callable} is bound, if so, get [[BoundTargetFunction]] from it |
| 1129 | // and use that instead of {callable}. |
| 1130 | while (callable->IsJSBoundFunction()) { |
| 1131 | callable = |
| 1132 | handle(Handle<JSBoundFunction>::cast(callable)->bound_target_function(), |
| 1133 | isolate); |
| 1134 | } |
| 1135 | DCHECK(callable->IsCallable()); |
| 1136 | // Get the "prototype" of {callable}; raise an error if it's not a receiver. |
| 1137 | Handle<Object> prototype; |
| 1138 | ASSIGN_RETURN_FAILURE_ON_EXCEPTION( |
| 1139 | isolate, prototype, |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 1140 | JSReceiver::GetProperty(Handle<JSReceiver>::cast(callable), |
| 1141 | isolate->factory()->prototype_string())); |
| 1142 | if (!prototype->IsJSReceiver()) { |
| 1143 | THROW_NEW_ERROR_RETURN_FAILURE( |
| 1144 | isolate, |
| 1145 | NewTypeError(MessageTemplate::kInstanceofNonobjectProto, prototype)); |
| 1146 | } |
| 1147 | // Return whether or not {prototype} is in the prototype chain of {object}. |
| 1148 | Handle<JSReceiver> receiver = Handle<JSReceiver>::cast(object); |
| 1149 | Maybe<bool> result = |
| 1150 | JSReceiver::HasInPrototypeChain(isolate, receiver, prototype); |
| 1151 | MAYBE_RETURN(result, isolate->heap()->exception()); |
| 1152 | return isolate->heap()->ToBoolean(result.FromJust()); |
| 1153 | } |
| 1154 | |
| 1155 | RUNTIME_FUNCTION(Runtime_OrdinaryHasInstance) { |
| 1156 | // ES6 section 19.2.3.6 Function.prototype[@@hasInstance](V) |
| 1157 | HandleScope shs(isolate); |
| 1158 | DCHECK_EQ(2, args.length()); |
| 1159 | DCHECK(args.length() == 2); |
| 1160 | CONVERT_ARG_HANDLE_CHECKED(Object, object, 0); |
| 1161 | CONVERT_ARG_HANDLE_CHECKED(Object, callable, 1); |
| 1162 | // {callable} must have a [[Call]] internal method. |
| 1163 | if (!callable->IsCallable()) { |
| 1164 | return isolate->heap()->false_value(); |
| 1165 | } |
| 1166 | // If {object} is not a receiver, return false. |
| 1167 | if (!object->IsJSReceiver()) { |
| 1168 | return isolate->heap()->false_value(); |
| 1169 | } |
| 1170 | // Check if {callable} is bound, if so, get [[BoundTargetFunction]] from it |
| 1171 | // and use that instead of {callable}. |
| 1172 | while (callable->IsJSBoundFunction()) { |
| 1173 | callable = |
| 1174 | handle(Handle<JSBoundFunction>::cast(callable)->bound_target_function(), |
| 1175 | isolate); |
| 1176 | } |
| 1177 | DCHECK(callable->IsCallable()); |
| 1178 | // Get the "prototype" of {callable}; raise an error if it's not a receiver. |
| 1179 | Handle<Object> prototype; |
| 1180 | ASSIGN_RETURN_FAILURE_ON_EXCEPTION( |
| 1181 | isolate, prototype, |
| 1182 | JSReceiver::GetProperty(Handle<JSReceiver>::cast(callable), |
| 1183 | isolate->factory()->prototype_string())); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1184 | if (!prototype->IsJSReceiver()) { |
| 1185 | THROW_NEW_ERROR_RETURN_FAILURE( |
| 1186 | isolate, |
| 1187 | NewTypeError(MessageTemplate::kInstanceofNonobjectProto, prototype)); |
| 1188 | } |
| 1189 | // Return whether or not {prototype} is in the prototype chain of {object}. |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 1190 | Handle<JSReceiver> receiver = Handle<JSReceiver>::cast(object); |
| 1191 | Maybe<bool> result = |
| 1192 | JSReceiver::HasInPrototypeChain(isolate, receiver, prototype); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1193 | MAYBE_RETURN(result, isolate->heap()->exception()); |
| 1194 | return isolate->heap()->ToBoolean(result.FromJust()); |
| 1195 | } |
| 1196 | |
| 1197 | |
| 1198 | RUNTIME_FUNCTION(Runtime_HasInPrototypeChain) { |
| 1199 | HandleScope scope(isolate); |
| 1200 | DCHECK_EQ(2, args.length()); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 1201 | CONVERT_ARG_HANDLE_CHECKED(JSReceiver, object, 0); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1202 | CONVERT_ARG_HANDLE_CHECKED(Object, prototype, 1); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 1203 | Maybe<bool> result = |
| 1204 | JSReceiver::HasInPrototypeChain(isolate, object, prototype); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1205 | MAYBE_RETURN(result, isolate->heap()->exception()); |
| 1206 | return isolate->heap()->ToBoolean(result.FromJust()); |
| 1207 | } |
| 1208 | |
| 1209 | |
| 1210 | // ES6 section 7.4.7 CreateIterResultObject ( value, done ) |
| 1211 | RUNTIME_FUNCTION(Runtime_CreateIterResultObject) { |
| 1212 | HandleScope scope(isolate); |
| 1213 | DCHECK_EQ(2, args.length()); |
| 1214 | CONVERT_ARG_HANDLE_CHECKED(Object, value, 0); |
| 1215 | CONVERT_ARG_HANDLE_CHECKED(Object, done, 1); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 1216 | Handle<JSObject> result = |
| 1217 | isolate->factory()->NewJSObjectFromMap(isolate->iterator_result_map()); |
| 1218 | result->InObjectPropertyAtPut(JSIteratorResult::kValueIndex, *value); |
| 1219 | result->InObjectPropertyAtPut(JSIteratorResult::kDoneIndex, *done); |
| 1220 | return *result; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1221 | } |
| 1222 | |
| 1223 | |
| 1224 | RUNTIME_FUNCTION(Runtime_IsAccessCheckNeeded) { |
| 1225 | SealHandleScope shs(isolate); |
| 1226 | DCHECK_EQ(1, args.length()); |
| 1227 | CONVERT_ARG_CHECKED(Object, object, 0); |
| 1228 | return isolate->heap()->ToBoolean(object->IsAccessCheckNeeded()); |
| 1229 | } |
| 1230 | |
| 1231 | |
| 1232 | RUNTIME_FUNCTION(Runtime_ObjectDefineProperty) { |
| 1233 | HandleScope scope(isolate); |
| 1234 | DCHECK(args.length() == 3); |
| 1235 | CONVERT_ARG_HANDLE_CHECKED(Object, o, 0); |
| 1236 | CONVERT_ARG_HANDLE_CHECKED(Object, name, 1); |
| 1237 | CONVERT_ARG_HANDLE_CHECKED(Object, attributes, 2); |
| 1238 | return JSReceiver::DefineProperty(isolate, o, name, attributes); |
| 1239 | } |
| 1240 | |
| 1241 | |
| 1242 | RUNTIME_FUNCTION(Runtime_ObjectDefineProperties) { |
| 1243 | HandleScope scope(isolate); |
| 1244 | DCHECK(args.length() == 2); |
| 1245 | CONVERT_ARG_HANDLE_CHECKED(Object, o, 0); |
| 1246 | CONVERT_ARG_HANDLE_CHECKED(Object, properties, 1); |
| 1247 | ASSIGN_RETURN_FAILURE_ON_EXCEPTION( |
| 1248 | isolate, o, JSReceiver::DefineProperties(isolate, o, properties)); |
| 1249 | return *o; |
| 1250 | } |
| 1251 | |
| 1252 | } // namespace internal |
| 1253 | } // namespace v8 |