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.
7 #include "src/arguments.h"
8 #include "src/bootstrapper.h"
10 #include "src/runtime/runtime.h"
11 #include "src/runtime/runtime-utils.h"
16 // Returns a single character string where first character equals
17 // string->Get(index).
18 static Handle<Object> GetCharAt(Handle<String> string, uint32_t index) {
19 if (index < static_cast<uint32_t>(string->length())) {
20 Factory* factory = string->GetIsolate()->factory();
21 return factory->LookupSingleCharacterStringFromCode(
22 String::Flatten(string)->Get(index));
24 return Execution::CharAt(string, index);
28 MaybeHandle<Object> Runtime::GetElementOrCharAt(Isolate* isolate,
29 Handle<Object> object,
31 // Handle [] indexing on Strings
32 if (object->IsString()) {
33 Handle<Object> result = GetCharAt(Handle<String>::cast(object), index);
34 if (!result->IsUndefined()) return result;
37 // Handle [] indexing on String objects
38 if (object->IsStringObjectWithCharacterAt(index)) {
39 Handle<JSValue> js_value = Handle<JSValue>::cast(object);
40 Handle<Object> result =
41 GetCharAt(Handle<String>(String::cast(js_value->value())), index);
42 if (!result->IsUndefined()) return result;
45 Handle<Object> result;
46 if (object->IsString() || object->IsNumber() || object->IsBoolean()) {
47 PrototypeIterator iter(isolate, object);
48 return Object::GetElement(isolate, PrototypeIterator::GetCurrent(iter),
51 return Object::GetElement(isolate, object, index);
56 MaybeHandle<Name> Runtime::ToName(Isolate* isolate, Handle<Object> key) {
58 return Handle<Name>::cast(key);
60 Handle<Object> converted;
61 ASSIGN_RETURN_ON_EXCEPTION(isolate, converted,
62 Execution::ToString(isolate, key), Name);
63 return Handle<Name>::cast(converted);
68 MaybeHandle<Object> Runtime::GetObjectProperty(Isolate* isolate,
69 Handle<Object> object,
71 if (object->IsUndefined() || object->IsNull()) {
72 Handle<Object> args[2] = {key, object};
73 THROW_NEW_ERROR(isolate, NewTypeError("non_object_property_load",
74 HandleVector(args, 2)),
78 // Check if the given key is an array index.
80 if (key->ToArrayIndex(&index)) {
81 return GetElementOrCharAt(isolate, object, index);
84 // Convert the key to a name - possibly by calling back into JavaScript.
86 ASSIGN_RETURN_ON_EXCEPTION(isolate, name, ToName(isolate, key), Object);
88 // Check if the name is trivially convertible to an index and get
90 if (name->AsArrayIndex(&index)) {
91 return GetElementOrCharAt(isolate, object, index);
93 return Object::GetProperty(object, name);
98 MaybeHandle<Object> Runtime::SetObjectProperty(Isolate* isolate,
99 Handle<Object> object,
101 Handle<Object> value,
102 LanguageMode language_mode) {
103 if (object->IsUndefined() || object->IsNull()) {
104 Handle<Object> args[2] = {key, object};
105 THROW_NEW_ERROR(isolate, NewTypeError("non_object_property_store",
106 HandleVector(args, 2)),
110 if (object->IsJSProxy()) {
111 Handle<Object> name_object;
112 if (key->IsSymbol()) {
115 ASSIGN_RETURN_ON_EXCEPTION(isolate, name_object,
116 Execution::ToString(isolate, key), Object);
118 Handle<Name> name = Handle<Name>::cast(name_object);
119 return Object::SetProperty(Handle<JSProxy>::cast(object), name, value,
123 // Check if the given key is an array index.
125 if (key->ToArrayIndex(&index)) {
126 // TODO(verwaest): Support non-JSObject receivers.
127 if (!object->IsJSObject()) return value;
128 Handle<JSObject> js_object = Handle<JSObject>::cast(object);
130 // In Firefox/SpiderMonkey, Safari and Opera you can access the characters
131 // of a string using [] notation. We need to support this too in
133 // In the case of a String object we just need to redirect the assignment to
134 // the underlying string if the index is in range. Since the underlying
135 // string does nothing with the assignment then we can ignore such
137 if (js_object->IsStringObjectWithCharacterAt(index)) {
141 JSObject::ValidateElements(js_object);
142 if (js_object->HasExternalArrayElements() ||
143 js_object->HasFixedTypedArrayElements()) {
144 if (!value->IsNumber() && !value->IsUndefined()) {
145 ASSIGN_RETURN_ON_EXCEPTION(isolate, value,
146 Execution::ToNumber(isolate, value), Object);
150 MaybeHandle<Object> result = JSObject::SetElement(
151 js_object, index, value, NONE, language_mode, true, SET_PROPERTY);
152 JSObject::ValidateElements(js_object);
154 return result.is_null() ? result : value;
158 Handle<Name> name = Handle<Name>::cast(key);
159 if (name->AsArrayIndex(&index)) {
160 // TODO(verwaest): Support non-JSObject receivers.
161 if (!object->IsJSObject()) return value;
162 Handle<JSObject> js_object = Handle<JSObject>::cast(object);
163 if (js_object->HasExternalArrayElements()) {
164 if (!value->IsNumber() && !value->IsUndefined()) {
165 ASSIGN_RETURN_ON_EXCEPTION(
166 isolate, value, Execution::ToNumber(isolate, value), Object);
169 return JSObject::SetElement(js_object, index, value, NONE, language_mode,
172 if (name->IsString()) name = String::Flatten(Handle<String>::cast(name));
173 return Object::SetProperty(object, name, value, language_mode);
177 // Call-back into JavaScript to convert the key to a string.
178 Handle<Object> converted;
179 ASSIGN_RETURN_ON_EXCEPTION(isolate, converted,
180 Execution::ToString(isolate, key), Object);
181 Handle<String> name = Handle<String>::cast(converted);
183 if (name->AsArrayIndex(&index)) {
184 // TODO(verwaest): Support non-JSObject receivers.
185 if (!object->IsJSObject()) return value;
186 Handle<JSObject> js_object = Handle<JSObject>::cast(object);
187 return JSObject::SetElement(js_object, index, value, NONE, language_mode,
190 return Object::SetProperty(object, name, value, language_mode);
194 MaybeHandle<Object> Runtime::DefineObjectProperty(Handle<JSObject> js_object,
196 Handle<Object> value,
197 PropertyAttributes attrs) {
198 Isolate* isolate = js_object->GetIsolate();
199 // Check if the given key is an array index.
201 if (key->ToArrayIndex(&index)) {
202 // In Firefox/SpiderMonkey, Safari and Opera you can access the characters
203 // of a string using [] notation. We need to support this too in
205 // In the case of a String object we just need to redirect the assignment to
206 // the underlying string if the index is in range. Since the underlying
207 // string does nothing with the assignment then we can ignore such
209 if (js_object->IsStringObjectWithCharacterAt(index)) {
213 return JSObject::SetElement(js_object, index, value, attrs, SLOPPY, false,
218 Handle<Name> name = Handle<Name>::cast(key);
219 if (name->AsArrayIndex(&index)) {
220 return JSObject::SetElement(js_object, index, value, attrs, SLOPPY, false,
223 if (name->IsString()) name = String::Flatten(Handle<String>::cast(name));
224 return JSObject::SetOwnPropertyIgnoreAttributes(js_object, name, value,
229 // Call-back into JavaScript to convert the key to a string.
230 Handle<Object> converted;
231 ASSIGN_RETURN_ON_EXCEPTION(isolate, converted,
232 Execution::ToString(isolate, key), Object);
233 Handle<String> name = Handle<String>::cast(converted);
235 if (name->AsArrayIndex(&index)) {
236 return JSObject::SetElement(js_object, index, value, attrs, SLOPPY, false,
239 return JSObject::SetOwnPropertyIgnoreAttributes(js_object, name, value,
245 MaybeHandle<Object> Runtime::GetPrototype(Isolate* isolate,
246 Handle<Object> obj) {
247 // We don't expect access checks to be needed on JSProxy objects.
248 DCHECK(!obj->IsAccessCheckNeeded() || obj->IsJSObject());
249 PrototypeIterator iter(isolate, obj, PrototypeIterator::START_AT_RECEIVER);
251 if (PrototypeIterator::GetCurrent(iter)->IsAccessCheckNeeded() &&
253 Handle<JSObject>::cast(PrototypeIterator::GetCurrent(iter)))) {
254 isolate->ReportFailedAccessCheck(
255 Handle<JSObject>::cast(PrototypeIterator::GetCurrent(iter)));
256 RETURN_EXCEPTION_IF_SCHEDULED_EXCEPTION(isolate, Object);
257 return isolate->factory()->undefined_value();
259 iter.AdvanceIgnoringProxies();
260 if (PrototypeIterator::GetCurrent(iter)->IsJSProxy()) {
261 return PrototypeIterator::GetCurrent(iter);
263 } while (!iter.IsAtEnd(PrototypeIterator::END_AT_NON_HIDDEN));
264 return PrototypeIterator::GetCurrent(iter);
268 RUNTIME_FUNCTION(Runtime_GetPrototype) {
269 HandleScope scope(isolate);
270 DCHECK(args.length() == 1);
271 CONVERT_ARG_HANDLE_CHECKED(Object, obj, 0);
272 Handle<Object> result;
273 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, result,
274 Runtime::GetPrototype(isolate, obj));
279 RUNTIME_FUNCTION(Runtime_InternalSetPrototype) {
280 HandleScope scope(isolate);
281 DCHECK(args.length() == 2);
282 CONVERT_ARG_HANDLE_CHECKED(JSObject, obj, 0);
283 CONVERT_ARG_HANDLE_CHECKED(Object, prototype, 1);
284 DCHECK(!obj->IsAccessCheckNeeded());
285 DCHECK(!obj->map()->is_observed());
286 Handle<Object> result;
287 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
288 isolate, result, JSObject::SetPrototype(obj, prototype, false));
293 RUNTIME_FUNCTION(Runtime_SetPrototype) {
294 HandleScope scope(isolate);
295 DCHECK(args.length() == 2);
296 CONVERT_ARG_HANDLE_CHECKED(JSObject, obj, 0);
297 CONVERT_ARG_HANDLE_CHECKED(Object, prototype, 1);
298 if (obj->IsAccessCheckNeeded() && !isolate->MayAccess(obj)) {
299 isolate->ReportFailedAccessCheck(obj);
300 RETURN_FAILURE_IF_SCHEDULED_EXCEPTION(isolate);
301 return isolate->heap()->undefined_value();
303 if (obj->map()->is_observed()) {
304 Handle<Object> old_value =
305 Object::GetPrototypeSkipHiddenPrototypes(isolate, obj);
306 Handle<Object> result;
307 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
308 isolate, result, JSObject::SetPrototype(obj, prototype, true));
310 Handle<Object> new_value =
311 Object::GetPrototypeSkipHiddenPrototypes(isolate, obj);
312 if (!new_value->SameValue(*old_value)) {
313 RETURN_FAILURE_ON_EXCEPTION(
314 isolate, JSObject::EnqueueChangeRecord(
315 obj, "setPrototype", isolate->factory()->proto_string(),
320 Handle<Object> result;
321 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
322 isolate, result, JSObject::SetPrototype(obj, prototype, true));
327 RUNTIME_FUNCTION(Runtime_IsInPrototypeChain) {
328 HandleScope shs(isolate);
329 DCHECK(args.length() == 2);
330 // See ECMA-262, section 15.3.5.3, page 88 (steps 5 - 8).
331 CONVERT_ARG_HANDLE_CHECKED(Object, O, 0);
332 CONVERT_ARG_HANDLE_CHECKED(Object, V, 1);
333 PrototypeIterator iter(isolate, V, PrototypeIterator::START_AT_RECEIVER);
335 iter.AdvanceIgnoringProxies();
336 if (iter.IsAtEnd()) return isolate->heap()->false_value();
337 if (iter.IsAtEnd(O)) return isolate->heap()->true_value();
342 // Enumerator used as indices into the array returned from GetOwnProperty
343 enum PropertyDescriptorIndices {
355 MUST_USE_RESULT static MaybeHandle<Object> GetOwnProperty(Isolate* isolate,
356 Handle<JSObject> obj,
358 Heap* heap = isolate->heap();
359 Factory* factory = isolate->factory();
361 PropertyAttributes attrs;
363 Handle<Object> value;
364 MaybeHandle<AccessorPair> maybe_accessors;
365 // TODO(verwaest): Unify once indexed properties can be handled by the
367 if (name->AsArrayIndex(&index)) {
369 Maybe<PropertyAttributes> maybe =
370 JSReceiver::GetOwnElementAttribute(obj, index);
371 if (!maybe.IsJust()) return MaybeHandle<Object>();
372 attrs = maybe.FromJust();
373 if (attrs == ABSENT) return factory->undefined_value();
375 // Get AccessorPair if present.
376 maybe_accessors = JSObject::GetOwnElementAccessorPair(obj, index);
378 // Get value if not an AccessorPair.
379 if (maybe_accessors.is_null()) {
380 ASSIGN_RETURN_ON_EXCEPTION(
381 isolate, value, Runtime::GetElementOrCharAt(isolate, obj, index),
386 LookupIterator it(obj, name, LookupIterator::HIDDEN);
387 Maybe<PropertyAttributes> maybe = JSObject::GetPropertyAttributes(&it);
388 if (!maybe.IsJust()) return MaybeHandle<Object>();
389 attrs = maybe.FromJust();
390 if (attrs == ABSENT) return factory->undefined_value();
392 // Get AccessorPair if present.
393 if (it.state() == LookupIterator::ACCESSOR &&
394 it.GetAccessors()->IsAccessorPair()) {
395 maybe_accessors = Handle<AccessorPair>::cast(it.GetAccessors());
398 // Get value if not an AccessorPair.
399 if (maybe_accessors.is_null()) {
400 ASSIGN_RETURN_ON_EXCEPTION(isolate, value, Object::GetProperty(&it),
404 DCHECK(!isolate->has_pending_exception());
405 Handle<FixedArray> elms = factory->NewFixedArray(DESCRIPTOR_SIZE);
406 elms->set(ENUMERABLE_INDEX, heap->ToBoolean((attrs & DONT_ENUM) == 0));
407 elms->set(CONFIGURABLE_INDEX, heap->ToBoolean((attrs & DONT_DELETE) == 0));
408 elms->set(IS_ACCESSOR_INDEX, heap->ToBoolean(!maybe_accessors.is_null()));
410 Handle<AccessorPair> accessors;
411 if (maybe_accessors.ToHandle(&accessors)) {
412 Handle<Object> getter(accessors->GetComponent(ACCESSOR_GETTER), isolate);
413 Handle<Object> setter(accessors->GetComponent(ACCESSOR_SETTER), isolate);
414 elms->set(GETTER_INDEX, *getter);
415 elms->set(SETTER_INDEX, *setter);
417 elms->set(WRITABLE_INDEX, heap->ToBoolean((attrs & READ_ONLY) == 0));
418 elms->set(VALUE_INDEX, *value);
421 return factory->NewJSArrayWithElements(elms);
425 // Returns an array with the property description:
426 // if args[1] is not a property on args[0]
428 // if args[1] is a data property on args[0]
429 // [false, value, Writeable, Enumerable, Configurable]
430 // if args[1] is an accessor on args[0]
431 // [true, GetFunction, SetFunction, Enumerable, Configurable]
432 RUNTIME_FUNCTION(Runtime_GetOwnProperty) {
433 HandleScope scope(isolate);
434 DCHECK(args.length() == 2);
435 CONVERT_ARG_HANDLE_CHECKED(JSObject, obj, 0);
436 CONVERT_ARG_HANDLE_CHECKED(Name, name, 1);
437 Handle<Object> result;
438 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, result,
439 GetOwnProperty(isolate, obj, name));
444 RUNTIME_FUNCTION(Runtime_PreventExtensions) {
445 HandleScope scope(isolate);
446 DCHECK(args.length() == 1);
447 CONVERT_ARG_HANDLE_CHECKED(JSObject, obj, 0);
448 Handle<Object> result;
449 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, result,
450 JSObject::PreventExtensions(obj));
455 RUNTIME_FUNCTION(Runtime_IsExtensible) {
456 SealHandleScope shs(isolate);
457 DCHECK(args.length() == 1);
458 CONVERT_ARG_CHECKED(JSObject, obj, 0);
459 if (obj->IsJSGlobalProxy()) {
460 PrototypeIterator iter(isolate, obj);
461 if (iter.IsAtEnd()) return isolate->heap()->false_value();
462 DCHECK(iter.GetCurrent()->IsJSGlobalObject());
463 obj = JSObject::cast(iter.GetCurrent());
465 return isolate->heap()->ToBoolean(obj->map()->is_extensible());
469 RUNTIME_FUNCTION(Runtime_DisableAccessChecks) {
470 HandleScope scope(isolate);
471 DCHECK(args.length() == 1);
472 CONVERT_ARG_HANDLE_CHECKED(HeapObject, object, 0);
473 Handle<Map> old_map(object->map());
474 bool needs_access_checks = old_map->is_access_check_needed();
475 if (needs_access_checks) {
476 // Copy map so it won't interfere constructor's initial map.
477 Handle<Map> new_map = Map::Copy(old_map, "DisableAccessChecks");
478 new_map->set_is_access_check_needed(false);
479 JSObject::MigrateToMap(Handle<JSObject>::cast(object), new_map);
481 return isolate->heap()->ToBoolean(needs_access_checks);
485 RUNTIME_FUNCTION(Runtime_EnableAccessChecks) {
486 HandleScope scope(isolate);
487 DCHECK(args.length() == 1);
488 CONVERT_ARG_HANDLE_CHECKED(JSObject, object, 0);
489 Handle<Map> old_map(object->map());
490 RUNTIME_ASSERT(!old_map->is_access_check_needed());
491 // Copy map so it won't interfere constructor's initial map.
492 Handle<Map> new_map = Map::Copy(old_map, "EnableAccessChecks");
493 new_map->set_is_access_check_needed(true);
494 JSObject::MigrateToMap(object, new_map);
495 return isolate->heap()->undefined_value();
499 RUNTIME_FUNCTION(Runtime_OptimizeObjectForAddingMultipleProperties) {
500 HandleScope scope(isolate);
501 DCHECK(args.length() == 2);
502 CONVERT_ARG_HANDLE_CHECKED(JSObject, object, 0);
503 CONVERT_SMI_ARG_CHECKED(properties, 1);
504 // Conservative upper limit to prevent fuzz tests from going OOM.
505 RUNTIME_ASSERT(properties <= 100000);
506 if (object->HasFastProperties() && !object->IsJSGlobalProxy()) {
507 JSObject::NormalizeProperties(object, KEEP_INOBJECT_PROPERTIES, properties,
508 "OptimizeForAdding");
514 RUNTIME_FUNCTION(Runtime_ObjectFreeze) {
515 HandleScope scope(isolate);
516 DCHECK(args.length() == 1);
517 CONVERT_ARG_HANDLE_CHECKED(JSObject, object, 0);
519 // %ObjectFreeze is a fast path and these cases are handled elsewhere.
520 RUNTIME_ASSERT(!object->HasSloppyArgumentsElements() &&
521 !object->map()->is_observed() && !object->IsJSProxy());
523 Handle<Object> result;
524 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, result, JSObject::Freeze(object));
529 RUNTIME_FUNCTION(Runtime_ObjectSeal) {
530 HandleScope scope(isolate);
531 DCHECK(args.length() == 1);
532 CONVERT_ARG_HANDLE_CHECKED(JSObject, object, 0);
534 // %ObjectSeal is a fast path and these cases are handled elsewhere.
535 RUNTIME_ASSERT(!object->HasSloppyArgumentsElements() &&
536 !object->map()->is_observed() && !object->IsJSProxy());
538 Handle<Object> result;
539 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, result, JSObject::Seal(object));
544 RUNTIME_FUNCTION(Runtime_GetProperty) {
545 HandleScope scope(isolate);
546 DCHECK(args.length() == 2);
548 CONVERT_ARG_HANDLE_CHECKED(Object, object, 0);
549 CONVERT_ARG_HANDLE_CHECKED(Object, key, 1);
550 Handle<Object> result;
551 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
552 isolate, result, Runtime::GetObjectProperty(isolate, object, key));
557 MUST_USE_RESULT static MaybeHandle<Object> TransitionElements(
558 Handle<Object> object, ElementsKind to_kind, Isolate* isolate) {
559 HandleScope scope(isolate);
560 if (!object->IsJSObject()) {
561 isolate->ThrowIllegalOperation();
562 return MaybeHandle<Object>();
564 ElementsKind from_kind =
565 Handle<JSObject>::cast(object)->map()->elements_kind();
566 if (Map::IsValidElementsTransition(from_kind, to_kind)) {
567 JSObject::TransitionElementsKind(Handle<JSObject>::cast(object), to_kind);
570 isolate->ThrowIllegalOperation();
571 return MaybeHandle<Object>();
575 // KeyedGetProperty is called from KeyedLoadIC::GenerateGeneric.
576 RUNTIME_FUNCTION(Runtime_KeyedGetProperty) {
577 HandleScope scope(isolate);
578 DCHECK(args.length() == 2);
580 CONVERT_ARG_HANDLE_CHECKED(Object, receiver_obj, 0);
581 CONVERT_ARG_HANDLE_CHECKED(Object, key_obj, 1);
583 // Fast cases for getting named properties of the receiver JSObject
586 // The global proxy objects has to be excluded since LookupOwn on
587 // the global proxy object can return a valid result even though the
588 // global proxy object never has properties. This is the case
589 // because the global proxy object forwards everything to its hidden
590 // prototype including own lookups.
592 // Additionally, we need to make sure that we do not cache results
593 // for objects that require access checks.
594 if (receiver_obj->IsJSObject()) {
595 if (!receiver_obj->IsJSGlobalProxy() &&
596 !receiver_obj->IsAccessCheckNeeded() && key_obj->IsName()) {
597 DisallowHeapAllocation no_allocation;
598 Handle<JSObject> receiver = Handle<JSObject>::cast(receiver_obj);
599 Handle<Name> key = Handle<Name>::cast(key_obj);
600 if (!receiver->HasFastProperties()) {
601 // Attempt dictionary lookup.
602 NameDictionary* dictionary = receiver->property_dictionary();
603 int entry = dictionary->FindEntry(key);
604 if ((entry != NameDictionary::kNotFound) &&
605 (dictionary->DetailsAt(entry).type() == DATA)) {
606 Object* value = dictionary->ValueAt(entry);
607 if (!receiver->IsGlobalObject()) return value;
608 DCHECK(value->IsPropertyCell());
609 value = PropertyCell::cast(value)->value();
610 if (!value->IsTheHole()) return value;
611 // If value is the hole (meaning, absent) do the general lookup.
614 } else if (key_obj->IsSmi()) {
615 // JSObject without a name key. If the key is a Smi, check for a
616 // definite out-of-bounds access to elements, which is a strong indicator
617 // that subsequent accesses will also call the runtime. Proactively
618 // transition elements to FAST_*_ELEMENTS to avoid excessive boxing of
619 // doubles for those future calls in the case that the elements would
620 // become FAST_DOUBLE_ELEMENTS.
621 Handle<JSObject> js_object = Handle<JSObject>::cast(receiver_obj);
622 ElementsKind elements_kind = js_object->GetElementsKind();
623 if (IsFastDoubleElementsKind(elements_kind)) {
624 Handle<Smi> key = Handle<Smi>::cast(key_obj);
625 if (key->value() >= js_object->elements()->length()) {
626 if (IsFastHoleyElementsKind(elements_kind)) {
627 elements_kind = FAST_HOLEY_ELEMENTS;
629 elements_kind = FAST_ELEMENTS;
631 RETURN_FAILURE_ON_EXCEPTION(
632 isolate, TransitionElements(js_object, elements_kind, isolate));
635 DCHECK(IsFastSmiOrObjectElementsKind(elements_kind) ||
636 !IsFastElementsKind(elements_kind));
639 } else if (receiver_obj->IsString() && key_obj->IsSmi()) {
640 // Fast case for string indexing using [] with a smi index.
641 Handle<String> str = Handle<String>::cast(receiver_obj);
642 int index = args.smi_at(1);
643 if (index >= 0 && index < str->length()) {
644 return *GetCharAt(str, index);
648 // Fall back to GetObjectProperty.
649 Handle<Object> result;
650 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
652 Runtime::GetObjectProperty(isolate, receiver_obj, key_obj));
657 RUNTIME_FUNCTION(Runtime_AddNamedProperty) {
658 HandleScope scope(isolate);
659 RUNTIME_ASSERT(args.length() == 4);
661 CONVERT_ARG_HANDLE_CHECKED(JSObject, object, 0);
662 CONVERT_ARG_HANDLE_CHECKED(Name, key, 1);
663 CONVERT_ARG_HANDLE_CHECKED(Object, value, 2);
664 CONVERT_PROPERTY_ATTRIBUTES_CHECKED(attrs, 3);
668 DCHECK(!key->ToArrayIndex(&index));
669 LookupIterator it(object, key, LookupIterator::OWN_SKIP_INTERCEPTOR);
670 Maybe<PropertyAttributes> maybe = JSReceiver::GetPropertyAttributes(&it);
671 if (!maybe.IsJust()) return isolate->heap()->exception();
672 RUNTIME_ASSERT(!it.IsFound());
675 Handle<Object> result;
676 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
678 JSObject::SetOwnPropertyIgnoreAttributes(object, key, value, attrs));
683 RUNTIME_FUNCTION(Runtime_SetProperty) {
684 HandleScope scope(isolate);
685 RUNTIME_ASSERT(args.length() == 4);
687 CONVERT_ARG_HANDLE_CHECKED(Object, object, 0);
688 CONVERT_ARG_HANDLE_CHECKED(Object, key, 1);
689 CONVERT_ARG_HANDLE_CHECKED(Object, value, 2);
690 CONVERT_LANGUAGE_MODE_ARG_CHECKED(language_mode_arg, 3);
691 LanguageMode language_mode = language_mode_arg;
693 Handle<Object> result;
694 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
696 Runtime::SetObjectProperty(isolate, object, key, value, language_mode));
701 // Adds an element to an array.
702 // This is used to create an indexed data property into an array.
703 RUNTIME_FUNCTION(Runtime_AddElement) {
704 HandleScope scope(isolate);
705 RUNTIME_ASSERT(args.length() == 4);
707 CONVERT_ARG_HANDLE_CHECKED(JSObject, object, 0);
708 CONVERT_ARG_HANDLE_CHECKED(Object, key, 1);
709 CONVERT_ARG_HANDLE_CHECKED(Object, value, 2);
710 CONVERT_PROPERTY_ATTRIBUTES_CHECKED(attrs, 3);
713 key->ToArrayIndex(&index);
715 Handle<Object> result;
716 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
717 isolate, result, JSObject::SetElement(object, index, value, attrs, SLOPPY,
718 false, DEFINE_PROPERTY));
723 RUNTIME_FUNCTION(Runtime_DeleteProperty) {
724 HandleScope scope(isolate);
725 DCHECK(args.length() == 3);
726 CONVERT_ARG_HANDLE_CHECKED(JSReceiver, object, 0);
727 CONVERT_ARG_HANDLE_CHECKED(Name, key, 1);
728 CONVERT_LANGUAGE_MODE_ARG_CHECKED(language_mode, 2);
729 Handle<Object> result;
730 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
731 isolate, result, JSReceiver::DeleteProperty(object, key, language_mode));
736 static Object* HasOwnPropertyImplementation(Isolate* isolate,
737 Handle<JSObject> object,
739 Maybe<bool> maybe = JSReceiver::HasOwnProperty(object, key);
740 if (!maybe.IsJust()) return isolate->heap()->exception();
741 if (maybe.FromJust()) return isolate->heap()->true_value();
742 // Handle hidden prototypes. If there's a hidden prototype above this thing
743 // then we have to check it for properties, because they are supposed to
744 // look like they are on this object.
745 PrototypeIterator iter(isolate, object);
746 if (!iter.IsAtEnd() &&
747 Handle<JSObject>::cast(PrototypeIterator::GetCurrent(iter))
749 ->is_hidden_prototype()) {
750 // TODO(verwaest): The recursion is not necessary for keys that are array
751 // indices. Removing this.
752 return HasOwnPropertyImplementation(
753 isolate, Handle<JSObject>::cast(PrototypeIterator::GetCurrent(iter)),
756 RETURN_FAILURE_IF_SCHEDULED_EXCEPTION(isolate);
757 return isolate->heap()->false_value();
761 RUNTIME_FUNCTION(Runtime_HasOwnProperty) {
762 HandleScope scope(isolate);
763 DCHECK(args.length() == 2);
764 CONVERT_ARG_HANDLE_CHECKED(Object, object, 0)
765 CONVERT_ARG_HANDLE_CHECKED(Name, key, 1);
768 const bool key_is_array_index = key->AsArrayIndex(&index);
770 // Only JS objects can have properties.
771 if (object->IsJSObject()) {
772 Handle<JSObject> js_obj = Handle<JSObject>::cast(object);
773 // Fast case: either the key is a real named property or it is not
774 // an array index and there are no interceptors or hidden
776 Maybe<bool> maybe = Nothing<bool>();
777 if (key_is_array_index) {
778 maybe = JSObject::HasOwnElement(js_obj, index);
780 maybe = JSObject::HasRealNamedProperty(js_obj, key);
782 if (!maybe.IsJust()) return isolate->heap()->exception();
783 DCHECK(!isolate->has_pending_exception());
784 if (maybe.FromJust()) {
785 return isolate->heap()->true_value();
787 Map* map = js_obj->map();
788 if (!key_is_array_index && !map->has_named_interceptor() &&
789 !HeapObject::cast(map->prototype())->map()->is_hidden_prototype()) {
790 return isolate->heap()->false_value();
793 return HasOwnPropertyImplementation(isolate, Handle<JSObject>(js_obj),
795 } else if (object->IsString() && key_is_array_index) {
796 // Well, there is one exception: Handle [] on strings.
797 Handle<String> string = Handle<String>::cast(object);
798 if (index < static_cast<uint32_t>(string->length())) {
799 return isolate->heap()->true_value();
802 return isolate->heap()->false_value();
806 RUNTIME_FUNCTION(Runtime_HasProperty) {
807 HandleScope scope(isolate);
808 DCHECK(args.length() == 2);
809 CONVERT_ARG_HANDLE_CHECKED(JSReceiver, receiver, 0);
810 CONVERT_ARG_HANDLE_CHECKED(Name, key, 1);
812 Maybe<bool> maybe = JSReceiver::HasProperty(receiver, key);
813 if (!maybe.IsJust()) return isolate->heap()->exception();
814 return isolate->heap()->ToBoolean(maybe.FromJust());
818 RUNTIME_FUNCTION(Runtime_HasElement) {
819 HandleScope scope(isolate);
820 DCHECK(args.length() == 2);
821 CONVERT_ARG_HANDLE_CHECKED(JSReceiver, receiver, 0);
822 CONVERT_SMI_ARG_CHECKED(index, 1);
824 Maybe<bool> maybe = JSReceiver::HasElement(receiver, index);
825 if (!maybe.IsJust()) return isolate->heap()->exception();
826 return isolate->heap()->ToBoolean(maybe.FromJust());
830 RUNTIME_FUNCTION(Runtime_IsPropertyEnumerable) {
831 HandleScope scope(isolate);
832 DCHECK(args.length() == 2);
834 CONVERT_ARG_HANDLE_CHECKED(JSObject, object, 0);
835 CONVERT_ARG_HANDLE_CHECKED(Name, key, 1);
837 Maybe<PropertyAttributes> maybe =
838 JSReceiver::GetOwnPropertyAttributes(object, key);
839 if (!maybe.IsJust()) return isolate->heap()->exception();
840 if (maybe.FromJust() == ABSENT) maybe = Just(DONT_ENUM);
841 return isolate->heap()->ToBoolean((maybe.FromJust() & DONT_ENUM) == 0);
845 RUNTIME_FUNCTION(Runtime_GetPropertyNames) {
846 HandleScope scope(isolate);
847 DCHECK(args.length() == 1);
848 CONVERT_ARG_HANDLE_CHECKED(JSReceiver, object, 0);
849 Handle<JSArray> result;
851 isolate->counters()->for_in()->Increment();
852 Handle<FixedArray> elements;
853 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
855 JSReceiver::GetKeys(object, JSReceiver::INCLUDE_PROTOS));
856 return *isolate->factory()->NewJSArrayWithElements(elements);
860 // Returns either a FixedArray as Runtime_GetPropertyNames,
861 // or, if the given object has an enum cache that contains
862 // all enumerable properties of the object and its prototypes
863 // have none, the map of the object. This is used to speed up
864 // the check for deletions during a for-in.
865 RUNTIME_FUNCTION(Runtime_GetPropertyNamesFast) {
866 SealHandleScope shs(isolate);
867 DCHECK(args.length() == 1);
869 CONVERT_ARG_CHECKED(JSReceiver, raw_object, 0);
871 if (raw_object->IsSimpleEnum()) return raw_object->map();
873 HandleScope scope(isolate);
874 Handle<JSReceiver> object(raw_object);
875 Handle<FixedArray> content;
876 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
878 JSReceiver::GetKeys(object, JSReceiver::INCLUDE_PROTOS));
880 // Test again, since cache may have been built by preceding call.
881 if (object->IsSimpleEnum()) return object->map();
887 // Find the length of the prototype chain that is to be handled as one. If a
888 // prototype object is hidden it is to be viewed as part of the the object it
890 static int OwnPrototypeChainLength(JSObject* obj) {
892 for (PrototypeIterator iter(obj->GetIsolate(), obj);
893 !iter.IsAtEnd(PrototypeIterator::END_AT_NON_HIDDEN); iter.Advance()) {
900 // Return the names of the own named properties.
902 // args[1]: PropertyAttributes as int
903 RUNTIME_FUNCTION(Runtime_GetOwnPropertyNames) {
904 HandleScope scope(isolate);
905 DCHECK(args.length() == 2);
906 if (!args[0]->IsJSObject()) {
907 return isolate->heap()->undefined_value();
909 CONVERT_ARG_HANDLE_CHECKED(JSObject, obj, 0);
910 CONVERT_SMI_ARG_CHECKED(filter_value, 1);
911 PropertyAttributes filter = static_cast<PropertyAttributes>(filter_value);
913 // Skip the global proxy as it has no properties and always delegates to the
914 // real global object.
915 if (obj->IsJSGlobalProxy()) {
916 // Only collect names if access is permitted.
917 if (obj->IsAccessCheckNeeded() && !isolate->MayAccess(obj)) {
918 isolate->ReportFailedAccessCheck(obj);
919 RETURN_FAILURE_IF_SCHEDULED_EXCEPTION(isolate);
920 return *isolate->factory()->NewJSArray(0);
922 PrototypeIterator iter(isolate, obj);
923 obj = Handle<JSObject>::cast(PrototypeIterator::GetCurrent(iter));
926 // Find the number of objects making up this.
927 int length = OwnPrototypeChainLength(*obj);
929 // Find the number of own properties for each of the objects.
930 ScopedVector<int> own_property_count(length);
931 int total_property_count = 0;
933 PrototypeIterator iter(isolate, obj, PrototypeIterator::START_AT_RECEIVER);
934 for (int i = 0; i < length; i++) {
935 DCHECK(!iter.IsAtEnd());
936 Handle<JSObject> jsproto =
937 Handle<JSObject>::cast(PrototypeIterator::GetCurrent(iter));
938 // Only collect names if access is permitted.
939 if (jsproto->IsAccessCheckNeeded() && !isolate->MayAccess(jsproto)) {
940 isolate->ReportFailedAccessCheck(jsproto);
941 RETURN_FAILURE_IF_SCHEDULED_EXCEPTION(isolate);
942 return *isolate->factory()->NewJSArray(0);
945 n = jsproto->NumberOfOwnProperties(filter);
946 own_property_count[i] = n;
947 total_property_count += n;
952 // Allocate an array with storage for all the property names.
953 Handle<FixedArray> names =
954 isolate->factory()->NewFixedArray(total_property_count);
956 // Get the property names.
957 int next_copy_index = 0;
958 int hidden_strings = 0;
960 PrototypeIterator iter(isolate, obj, PrototypeIterator::START_AT_RECEIVER);
961 for (int i = 0; i < length; i++) {
962 DCHECK(!iter.IsAtEnd());
963 Handle<JSObject> jsproto =
964 Handle<JSObject>::cast(PrototypeIterator::GetCurrent(iter));
965 jsproto->GetOwnPropertyNames(*names, next_copy_index, filter);
966 // Names from hidden prototypes may already have been added
967 // for inherited function template instances. Count the duplicates
968 // and stub them out; the final copy pass at the end ignores holes.
969 for (int j = next_copy_index; j < next_copy_index + own_property_count[i];
971 Object* name_from_hidden_proto = names->get(j);
972 if (isolate->IsInternallyUsedPropertyName(name_from_hidden_proto)) {
975 for (int k = 0; k < next_copy_index; k++) {
976 Object* name = names->get(k);
977 if (name_from_hidden_proto == name) {
978 names->set(j, isolate->heap()->hidden_string());
985 next_copy_index += own_property_count[i];
991 // Filter out name of hidden properties object and
992 // hidden prototype duplicates.
993 if (hidden_strings > 0) {
994 Handle<FixedArray> old_names = names;
995 names = isolate->factory()->NewFixedArray(names->length() - hidden_strings);
997 for (int i = 0; i < total_property_count; i++) {
998 Object* name = old_names->get(i);
999 if (isolate->IsInternallyUsedPropertyName(name)) {
1003 names->set(dest_pos++, name);
1005 DCHECK_EQ(0, hidden_strings);
1008 return *isolate->factory()->NewJSArrayWithElements(names);
1012 // Return the names of the own indexed properties.
1014 RUNTIME_FUNCTION(Runtime_GetOwnElementNames) {
1015 HandleScope scope(isolate);
1016 DCHECK(args.length() == 1);
1017 if (!args[0]->IsJSObject()) {
1018 return isolate->heap()->undefined_value();
1020 CONVERT_ARG_HANDLE_CHECKED(JSObject, obj, 0);
1022 int n = obj->NumberOfOwnElements(NONE);
1023 Handle<FixedArray> names = isolate->factory()->NewFixedArray(n);
1024 obj->GetOwnElementKeys(*names, NONE);
1025 return *isolate->factory()->NewJSArrayWithElements(names);
1029 // Return information on whether an object has a named or indexed interceptor.
1031 RUNTIME_FUNCTION(Runtime_GetInterceptorInfo) {
1032 HandleScope scope(isolate);
1033 DCHECK(args.length() == 1);
1034 if (!args[0]->IsJSObject()) {
1035 return Smi::FromInt(0);
1037 CONVERT_ARG_HANDLE_CHECKED(JSObject, obj, 0);
1040 if (obj->HasNamedInterceptor()) result |= 2;
1041 if (obj->HasIndexedInterceptor()) result |= 1;
1043 return Smi::FromInt(result);
1047 // Return property names from named interceptor.
1049 RUNTIME_FUNCTION(Runtime_GetNamedInterceptorPropertyNames) {
1050 HandleScope scope(isolate);
1051 DCHECK(args.length() == 1);
1052 CONVERT_ARG_HANDLE_CHECKED(JSObject, obj, 0);
1054 if (obj->HasNamedInterceptor()) {
1055 Handle<JSObject> result;
1056 if (JSObject::GetKeysForNamedInterceptor(obj, obj).ToHandle(&result)) {
1060 return isolate->heap()->undefined_value();
1064 // Return element names from indexed interceptor.
1066 RUNTIME_FUNCTION(Runtime_GetIndexedInterceptorElementNames) {
1067 HandleScope scope(isolate);
1068 DCHECK(args.length() == 1);
1069 CONVERT_ARG_HANDLE_CHECKED(JSObject, obj, 0);
1071 if (obj->HasIndexedInterceptor()) {
1072 Handle<JSObject> result;
1073 if (JSObject::GetKeysForIndexedInterceptor(obj, obj).ToHandle(&result)) {
1077 return isolate->heap()->undefined_value();
1081 RUNTIME_FUNCTION(Runtime_OwnKeys) {
1082 HandleScope scope(isolate);
1083 DCHECK(args.length() == 1);
1084 CONVERT_ARG_CHECKED(JSObject, raw_object, 0);
1085 Handle<JSObject> object(raw_object);
1087 if (object->IsJSGlobalProxy()) {
1088 // Do access checks before going to the global object.
1089 if (object->IsAccessCheckNeeded() && !isolate->MayAccess(object)) {
1090 isolate->ReportFailedAccessCheck(object);
1091 RETURN_FAILURE_IF_SCHEDULED_EXCEPTION(isolate);
1092 return *isolate->factory()->NewJSArray(0);
1095 PrototypeIterator iter(isolate, object);
1096 // If proxy is detached we simply return an empty array.
1097 if (iter.IsAtEnd()) return *isolate->factory()->NewJSArray(0);
1098 object = Handle<JSObject>::cast(PrototypeIterator::GetCurrent(iter));
1101 Handle<FixedArray> contents;
1102 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
1103 isolate, contents, JSReceiver::GetKeys(object, JSReceiver::OWN_ONLY));
1105 // Some fast paths through GetKeysInFixedArrayFor reuse a cached
1106 // property array and since the result is mutable we have to create
1107 // a fresh clone on each invocation.
1108 int length = contents->length();
1109 Handle<FixedArray> copy = isolate->factory()->NewFixedArray(length);
1110 for (int i = 0; i < length; i++) {
1111 Object* entry = contents->get(i);
1112 if (entry->IsString()) {
1113 copy->set(i, entry);
1115 DCHECK(entry->IsNumber());
1116 HandleScope scope(isolate);
1117 Handle<Object> entry_handle(entry, isolate);
1118 Handle<Object> entry_str =
1119 isolate->factory()->NumberToString(entry_handle);
1120 copy->set(i, *entry_str);
1123 return *isolate->factory()->NewJSArrayWithElements(copy);
1127 RUNTIME_FUNCTION(Runtime_ToFastProperties) {
1128 HandleScope scope(isolate);
1129 DCHECK(args.length() == 1);
1130 CONVERT_ARG_HANDLE_CHECKED(Object, object, 0);
1131 if (object->IsJSObject() && !object->IsGlobalObject()) {
1132 JSObject::MigrateSlowToFast(Handle<JSObject>::cast(object), 0,
1133 "RuntimeToFastProperties");
1139 RUNTIME_FUNCTION(Runtime_ToBool) {
1140 SealHandleScope shs(isolate);
1141 DCHECK(args.length() == 1);
1142 CONVERT_ARG_CHECKED(Object, object, 0);
1144 return isolate->heap()->ToBoolean(object->BooleanValue());
1148 // Returns the type string of a value; see ECMA-262, 11.4.3 (p 47).
1149 // Possible optimizations: put the type string into the oddballs.
1150 RUNTIME_FUNCTION(Runtime_Typeof) {
1151 SealHandleScope shs(isolate);
1152 DCHECK(args.length() == 1);
1153 CONVERT_ARG_CHECKED(Object, obj, 0);
1154 if (obj->IsNumber()) return isolate->heap()->number_string();
1155 HeapObject* heap_obj = HeapObject::cast(obj);
1157 // typeof an undetectable object is 'undefined'
1158 if (heap_obj->map()->is_undetectable()) {
1159 return isolate->heap()->undefined_string();
1162 InstanceType instance_type = heap_obj->map()->instance_type();
1163 if (instance_type < FIRST_NONSTRING_TYPE) {
1164 return isolate->heap()->string_string();
1167 switch (instance_type) {
1169 if (heap_obj->IsTrue() || heap_obj->IsFalse()) {
1170 return isolate->heap()->boolean_string();
1172 if (heap_obj->IsNull()) {
1173 return isolate->heap()->object_string();
1175 DCHECK(heap_obj->IsUndefined());
1176 return isolate->heap()->undefined_string();
1178 return isolate->heap()->symbol_string();
1179 case JS_FUNCTION_TYPE:
1180 case JS_FUNCTION_PROXY_TYPE:
1181 return isolate->heap()->function_string();
1183 // For any kind of object not handled above, the spec rule for
1184 // host objects gives that it is okay to return "object"
1185 return isolate->heap()->object_string();
1190 RUNTIME_FUNCTION(Runtime_NewStringWrapper) {
1191 HandleScope scope(isolate);
1192 DCHECK(args.length() == 1);
1193 CONVERT_ARG_HANDLE_CHECKED(String, value, 0);
1194 return *Object::ToObject(isolate, value).ToHandleChecked();
1198 RUNTIME_FUNCTION(Runtime_AllocateHeapNumber) {
1199 HandleScope scope(isolate);
1200 DCHECK(args.length() == 0);
1201 return *isolate->factory()->NewHeapNumber(0);
1205 static Object* Runtime_NewObjectHelper(Isolate* isolate,
1206 Handle<Object> constructor,
1207 Handle<Object> original_constructor,
1208 Handle<AllocationSite> site) {
1209 // If the constructor isn't a proper function we throw a type error.
1210 if (!constructor->IsJSFunction()) {
1211 Vector<Handle<Object> > arguments = HandleVector(&constructor, 1);
1212 THROW_NEW_ERROR_RETURN_FAILURE(isolate,
1213 NewTypeError("not_constructor", arguments));
1216 Handle<JSFunction> function = Handle<JSFunction>::cast(constructor);
1218 CHECK(original_constructor->IsJSFunction());
1219 Handle<JSFunction> original_function =
1220 Handle<JSFunction>::cast(original_constructor);
1223 // If function should not have prototype, construction is not allowed. In this
1224 // case generated code bailouts here, since function has no initial_map.
1225 if (!function->should_have_prototype() && !function->shared()->bound()) {
1226 Vector<Handle<Object> > arguments = HandleVector(&constructor, 1);
1227 THROW_NEW_ERROR_RETURN_FAILURE(isolate,
1228 NewTypeError("not_constructor", arguments));
1231 Debug* debug = isolate->debug();
1232 // Handle stepping into constructors if step into is active.
1233 if (debug->StepInActive()) {
1234 debug->HandleStepIn(function, Handle<Object>::null(), 0, true);
1237 if (function->has_initial_map()) {
1238 if (function->initial_map()->instance_type() == JS_FUNCTION_TYPE) {
1239 // The 'Function' function ignores the receiver object when
1240 // called using 'new' and creates a new JSFunction object that
1241 // is returned. The receiver object is only used for error
1242 // reporting if an error occurs when constructing the new
1243 // JSFunction. Factory::NewJSObject() should not be used to
1244 // allocate JSFunctions since it does not properly initialize
1245 // the shared part of the function. Since the receiver is
1246 // ignored anyway, we use the global object as the receiver
1247 // instead of a new JSFunction object. This way, errors are
1248 // reported the same way whether or not 'Function' is called
1250 return isolate->global_proxy();
1254 // The function should be compiled for the optimization hints to be
1256 Compiler::EnsureCompiled(function, CLEAR_EXCEPTION);
1258 Handle<JSObject> result;
1259 if (site.is_null()) {
1260 result = isolate->factory()->NewJSObject(function);
1262 result = isolate->factory()->NewJSObjectWithMemento(function, site);
1265 // Set up the prototoype using original function.
1266 // TODO(dslomov): instead of setting the __proto__,
1267 // use and cache the correct map.
1268 if (*original_function != *function) {
1269 if (original_function->has_instance_prototype()) {
1270 Handle<Object> prototype =
1271 handle(original_function->instance_prototype(), isolate);
1272 RETURN_FAILURE_ON_EXCEPTION(
1273 isolate, JSObject::SetPrototype(result, prototype, false));
1277 isolate->counters()->constructed_objects()->Increment();
1278 isolate->counters()->constructed_objects_runtime()->Increment();
1284 RUNTIME_FUNCTION(Runtime_NewObject) {
1285 HandleScope scope(isolate);
1286 DCHECK(args.length() == 2);
1287 CONVERT_ARG_HANDLE_CHECKED(Object, constructor, 0);
1288 CONVERT_ARG_HANDLE_CHECKED(Object, original_constructor, 1);
1289 return Runtime_NewObjectHelper(isolate, constructor, original_constructor,
1290 Handle<AllocationSite>::null());
1294 RUNTIME_FUNCTION(Runtime_NewObjectWithAllocationSite) {
1295 HandleScope scope(isolate);
1296 DCHECK(args.length() == 3);
1297 CONVERT_ARG_HANDLE_CHECKED(Object, original_constructor, 2);
1298 CONVERT_ARG_HANDLE_CHECKED(Object, constructor, 1);
1299 CONVERT_ARG_HANDLE_CHECKED(Object, feedback, 0);
1300 Handle<AllocationSite> site;
1301 if (feedback->IsAllocationSite()) {
1302 // The feedback can be an AllocationSite or undefined.
1303 site = Handle<AllocationSite>::cast(feedback);
1305 return Runtime_NewObjectHelper(isolate, constructor, original_constructor,
1310 RUNTIME_FUNCTION(Runtime_FinalizeInstanceSize) {
1311 HandleScope scope(isolate);
1312 DCHECK(args.length() == 1);
1314 CONVERT_ARG_HANDLE_CHECKED(JSFunction, function, 0);
1315 function->CompleteInobjectSlackTracking();
1317 return isolate->heap()->undefined_value();
1321 RUNTIME_FUNCTION(Runtime_GlobalProxy) {
1322 SealHandleScope shs(isolate);
1323 DCHECK(args.length() == 1);
1324 CONVERT_ARG_CHECKED(Object, global, 0);
1325 if (!global->IsJSGlobalObject()) return isolate->heap()->null_value();
1326 return JSGlobalObject::cast(global)->global_proxy();
1330 RUNTIME_FUNCTION(Runtime_LookupAccessor) {
1331 HandleScope scope(isolate);
1332 DCHECK(args.length() == 3);
1333 CONVERT_ARG_HANDLE_CHECKED(JSReceiver, receiver, 0);
1334 CONVERT_ARG_HANDLE_CHECKED(Name, name, 1);
1335 CONVERT_SMI_ARG_CHECKED(flag, 2);
1336 AccessorComponent component = flag == 0 ? ACCESSOR_GETTER : ACCESSOR_SETTER;
1337 if (!receiver->IsJSObject()) return isolate->heap()->undefined_value();
1338 Handle<Object> result;
1339 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
1341 JSObject::GetAccessor(Handle<JSObject>::cast(receiver), name, component));
1346 RUNTIME_FUNCTION(Runtime_LoadMutableDouble) {
1347 HandleScope scope(isolate);
1348 DCHECK(args.length() == 2);
1349 CONVERT_ARG_HANDLE_CHECKED(JSObject, object, 0);
1350 CONVERT_ARG_HANDLE_CHECKED(Smi, index, 1);
1351 RUNTIME_ASSERT((index->value() & 1) == 1);
1352 FieldIndex field_index =
1353 FieldIndex::ForLoadByFieldIndex(object->map(), index->value());
1354 if (field_index.is_inobject()) {
1355 RUNTIME_ASSERT(field_index.property_index() <
1356 object->map()->inobject_properties());
1358 RUNTIME_ASSERT(field_index.outobject_array_index() <
1359 object->properties()->length());
1361 return *JSObject::FastPropertyAt(object, Representation::Double(),
1366 RUNTIME_FUNCTION(Runtime_TryMigrateInstance) {
1367 HandleScope scope(isolate);
1368 DCHECK(args.length() == 1);
1369 CONVERT_ARG_HANDLE_CHECKED(Object, object, 0);
1370 if (!object->IsJSObject()) return Smi::FromInt(0);
1371 Handle<JSObject> js_object = Handle<JSObject>::cast(object);
1372 if (!js_object->map()->is_deprecated()) return Smi::FromInt(0);
1373 // This call must not cause lazy deopts, because it's called from deferred
1374 // code where we can't handle lazy deopts for lack of a suitable bailout
1375 // ID. So we just try migration and signal failure if necessary,
1376 // which will also trigger a deopt.
1377 if (!JSObject::TryMigrateInstance(js_object)) return Smi::FromInt(0);
1382 RUNTIME_FUNCTION(Runtime_IsJSGlobalProxy) {
1383 SealHandleScope shs(isolate);
1384 DCHECK(args.length() == 1);
1385 CONVERT_ARG_CHECKED(Object, obj, 0);
1386 return isolate->heap()->ToBoolean(obj->IsJSGlobalProxy());
1390 static bool IsValidAccessor(Handle<Object> obj) {
1391 return obj->IsUndefined() || obj->IsSpecFunction() || obj->IsNull();
1395 // Implements part of 8.12.9 DefineOwnProperty.
1396 // There are 3 cases that lead here:
1397 // Step 4b - define a new accessor property.
1398 // Steps 9c & 12 - replace an existing data property with an accessor property.
1399 // Step 12 - update an existing accessor property with an accessor or generic
1401 RUNTIME_FUNCTION(Runtime_DefineAccessorPropertyUnchecked) {
1402 HandleScope scope(isolate);
1403 DCHECK(args.length() == 5);
1404 CONVERT_ARG_HANDLE_CHECKED(JSObject, obj, 0);
1405 RUNTIME_ASSERT(!obj->IsNull());
1406 CONVERT_ARG_HANDLE_CHECKED(Name, name, 1);
1407 CONVERT_ARG_HANDLE_CHECKED(Object, getter, 2);
1408 RUNTIME_ASSERT(IsValidAccessor(getter));
1409 CONVERT_ARG_HANDLE_CHECKED(Object, setter, 3);
1410 RUNTIME_ASSERT(IsValidAccessor(setter));
1411 CONVERT_PROPERTY_ATTRIBUTES_CHECKED(attrs, 4);
1413 RETURN_FAILURE_ON_EXCEPTION(
1414 isolate, JSObject::DefineAccessor(obj, name, getter, setter, attrs));
1415 return isolate->heap()->undefined_value();
1419 // Implements part of 8.12.9 DefineOwnProperty.
1420 // There are 3 cases that lead here:
1421 // Step 4a - define a new data property.
1422 // Steps 9b & 12 - replace an existing accessor property with a data property.
1423 // Step 12 - update an existing data property with a data or generic
1425 RUNTIME_FUNCTION(Runtime_DefineDataPropertyUnchecked) {
1426 HandleScope scope(isolate);
1427 DCHECK(args.length() == 4);
1428 CONVERT_ARG_HANDLE_CHECKED(JSObject, js_object, 0);
1429 CONVERT_ARG_HANDLE_CHECKED(Name, name, 1);
1430 CONVERT_ARG_HANDLE_CHECKED(Object, obj_value, 2);
1431 CONVERT_PROPERTY_ATTRIBUTES_CHECKED(attrs, 3);
1433 LookupIterator it(js_object, name, LookupIterator::OWN_SKIP_INTERCEPTOR);
1434 if (it.IsFound() && it.state() == LookupIterator::ACCESS_CHECK) {
1435 if (!isolate->MayAccess(js_object)) {
1436 return isolate->heap()->undefined_value();
1441 // Take special care when attributes are different and there is already
1443 if (it.state() == LookupIterator::ACCESSOR) {
1444 // Use IgnoreAttributes version since a readonly property may be
1445 // overridden and SetProperty does not allow this.
1446 Handle<Object> result;
1447 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
1449 JSObject::SetOwnPropertyIgnoreAttributes(
1450 js_object, name, obj_value, attrs, JSObject::DONT_FORCE_FIELD));
1454 Handle<Object> result;
1455 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
1457 Runtime::DefineObjectProperty(js_object, name, obj_value, attrs));
1462 // Return property without being observable by accessors or interceptors.
1463 RUNTIME_FUNCTION(Runtime_GetDataProperty) {
1464 HandleScope scope(isolate);
1465 DCHECK(args.length() == 2);
1466 CONVERT_ARG_HANDLE_CHECKED(JSObject, object, 0);
1467 CONVERT_ARG_HANDLE_CHECKED(Name, key, 1);
1468 return *JSObject::GetDataProperty(object, key);
1472 RUNTIME_FUNCTION(Runtime_HasFastPackedElements) {
1473 SealHandleScope shs(isolate);
1474 DCHECK(args.length() == 1);
1475 CONVERT_ARG_CHECKED(HeapObject, obj, 0);
1476 return isolate->heap()->ToBoolean(
1477 IsFastPackedElementsKind(obj->map()->elements_kind()));
1481 RUNTIME_FUNCTION(Runtime_ValueOf) {
1482 SealHandleScope shs(isolate);
1483 DCHECK(args.length() == 1);
1484 CONVERT_ARG_CHECKED(Object, obj, 0);
1485 if (!obj->IsJSValue()) return obj;
1486 return JSValue::cast(obj)->value();
1490 RUNTIME_FUNCTION(Runtime_SetValueOf) {
1491 SealHandleScope shs(isolate);
1492 DCHECK(args.length() == 2);
1493 CONVERT_ARG_CHECKED(Object, obj, 0);
1494 CONVERT_ARG_CHECKED(Object, value, 1);
1495 if (!obj->IsJSValue()) return value;
1496 JSValue::cast(obj)->set_value(value);
1501 RUNTIME_FUNCTION(Runtime_JSValueGetValue) {
1502 SealHandleScope shs(isolate);
1503 DCHECK(args.length() == 1);
1504 CONVERT_ARG_CHECKED(JSValue, obj, 0);
1505 return JSValue::cast(obj)->value();
1509 RUNTIME_FUNCTION(Runtime_HeapObjectGetMap) {
1510 SealHandleScope shs(isolate);
1511 DCHECK(args.length() == 1);
1512 CONVERT_ARG_CHECKED(HeapObject, obj, 0);
1517 RUNTIME_FUNCTION(Runtime_MapGetInstanceType) {
1518 SealHandleScope shs(isolate);
1519 DCHECK(args.length() == 1);
1520 CONVERT_ARG_CHECKED(Map, map, 0);
1521 return Smi::FromInt(map->instance_type());
1525 RUNTIME_FUNCTION(Runtime_ObjectEquals) {
1526 SealHandleScope shs(isolate);
1527 DCHECK(args.length() == 2);
1528 CONVERT_ARG_CHECKED(Object, obj1, 0);
1529 CONVERT_ARG_CHECKED(Object, obj2, 1);
1530 return isolate->heap()->ToBoolean(obj1 == obj2);
1534 RUNTIME_FUNCTION(Runtime_IsObject) {
1535 SealHandleScope shs(isolate);
1536 DCHECK(args.length() == 1);
1537 CONVERT_ARG_CHECKED(Object, obj, 0);
1538 if (!obj->IsHeapObject()) return isolate->heap()->false_value();
1539 if (obj->IsNull()) return isolate->heap()->true_value();
1540 if (obj->IsUndetectableObject()) return isolate->heap()->false_value();
1541 Map* map = HeapObject::cast(obj)->map();
1542 bool is_non_callable_spec_object =
1543 map->instance_type() >= FIRST_NONCALLABLE_SPEC_OBJECT_TYPE &&
1544 map->instance_type() <= LAST_NONCALLABLE_SPEC_OBJECT_TYPE;
1545 return isolate->heap()->ToBoolean(is_non_callable_spec_object);
1549 RUNTIME_FUNCTION(Runtime_IsUndetectableObject) {
1550 SealHandleScope shs(isolate);
1551 DCHECK(args.length() == 1);
1552 CONVERT_ARG_CHECKED(Object, obj, 0);
1553 return isolate->heap()->ToBoolean(obj->IsUndetectableObject());
1557 RUNTIME_FUNCTION(Runtime_IsSpecObject) {
1558 SealHandleScope shs(isolate);
1559 DCHECK(args.length() == 1);
1560 CONVERT_ARG_CHECKED(Object, obj, 0);
1561 return isolate->heap()->ToBoolean(obj->IsSpecObject());
1565 RUNTIME_FUNCTION(Runtime_ClassOf) {
1566 SealHandleScope shs(isolate);
1567 DCHECK(args.length() == 1);
1568 CONVERT_ARG_CHECKED(Object, obj, 0);
1569 if (!obj->IsJSReceiver()) return isolate->heap()->null_value();
1570 return JSReceiver::cast(obj)->class_name();
1574 RUNTIME_FUNCTION(Runtime_DefineGetterPropertyUnchecked) {
1575 HandleScope scope(isolate);
1576 DCHECK(args.length() == 4);
1577 CONVERT_ARG_HANDLE_CHECKED(JSObject, object, 0);
1578 CONVERT_ARG_HANDLE_CHECKED(Name, name, 1);
1579 CONVERT_ARG_HANDLE_CHECKED(JSFunction, getter, 2);
1580 CONVERT_PROPERTY_ATTRIBUTES_CHECKED(attrs, 3);
1582 RETURN_FAILURE_ON_EXCEPTION(
1584 JSObject::DefineAccessor(object, name, getter,
1585 isolate->factory()->null_value(), attrs));
1586 return isolate->heap()->undefined_value();
1590 RUNTIME_FUNCTION(Runtime_DefineSetterPropertyUnchecked) {
1591 HandleScope scope(isolate);
1592 DCHECK(args.length() == 4);
1593 CONVERT_ARG_HANDLE_CHECKED(JSObject, object, 0);
1594 CONVERT_ARG_HANDLE_CHECKED(Name, name, 1);
1595 CONVERT_ARG_HANDLE_CHECKED(JSFunction, setter, 2);
1596 CONVERT_PROPERTY_ATTRIBUTES_CHECKED(attrs, 3);
1598 RETURN_FAILURE_ON_EXCEPTION(
1600 JSObject::DefineAccessor(object, name, isolate->factory()->null_value(),
1602 return isolate->heap()->undefined_value();
1605 } // namespace v8::internal