Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(39)

Side by Side Diff: src/runtime/runtime-object.cc

Issue 1368753003: Add C++ implementation of Object.defineProperties (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: preview -- needs refactoring Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/runtime/runtime.h ('k') | src/v8natives.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 the V8 project authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "src/runtime/runtime-utils.h" 5 #include "src/runtime/runtime-utils.h"
6 6
7 #include "src/arguments.h" 7 #include "src/arguments.h"
8 #include "src/bootstrapper.h" 8 #include "src/bootstrapper.h"
9 #include "src/debug/debug.h" 9 #include "src/debug/debug.h"
10 #include "src/isolate-inl.h" 10 #include "src/isolate-inl.h"
(...skipping 1596 matching lines...) Expand 10 before | Expand all | Expand 10 after
1607 1607
1608 1608
1609 RUNTIME_FUNCTION(Runtime_IsAccessCheckNeeded) { 1609 RUNTIME_FUNCTION(Runtime_IsAccessCheckNeeded) {
1610 SealHandleScope shs(isolate); 1610 SealHandleScope shs(isolate);
1611 DCHECK_EQ(1, args.length()); 1611 DCHECK_EQ(1, args.length());
1612 CONVERT_ARG_CHECKED(Object, object, 0); 1612 CONVERT_ARG_CHECKED(Object, object, 0);
1613 return isolate->heap()->ToBoolean(object->IsAccessCheckNeeded()); 1613 return isolate->heap()->ToBoolean(object->IsAccessCheckNeeded());
1614 } 1614 }
1615 1615
1616 1616
1617 class PropertyDescriptor : public ZoneObject {
1618 public:
1619 PropertyDescriptor()
1620 : enumerable_(false),
1621 has_enumerable_(false),
1622 configurable_(false),
1623 has_configurable_(false),
1624 writable_(false),
1625 has_writable_(false) {}
1626
1627 // ES6 6.2.4.1
1628 static bool IsAccessorDescriptor(PropertyDescriptor* desc) {
1629 if (desc == NULL) return false;
1630 return desc->has_get() || desc->has_set();
1631 }
1632
1633 // ES6 6.2.4.2
1634 static bool IsDataDescriptor(PropertyDescriptor* desc) {
1635 if (desc == NULL) return false;
1636 return desc->has_value() || desc->has_writable();
1637 }
1638
1639 // ES6 6.2.4.3
1640 static bool IsGenericDescriptor(PropertyDescriptor* desc) {
1641 if (desc == NULL) return false;
1642 return !IsAccessorDescriptor(desc) && !IsDataDescriptor(desc);
1643 }
1644
1645 bool enumerable() const { return enumerable_; }
1646 void set_enumerable(bool enumerable) {
1647 enumerable_ = enumerable;
1648 has_enumerable_ = true;
1649 }
1650 bool has_enumerable() const { return has_enumerable_; }
1651
1652 bool configurable() const { return configurable_; }
1653 void set_configurable(bool configurable) {
1654 configurable_ = configurable;
1655 has_configurable_ = true;
1656 }
1657 bool has_configurable() const { return has_configurable_; }
1658
1659 Handle<Object> value() const { return value_; }
1660 void set_value(Handle<Object> value) { value_ = value; }
1661 bool has_value() const { return !value_.is_null(); }
1662
1663 bool writable() const { return writable_; }
1664 void set_writable(bool writable) {
1665 writable_ = writable;
1666 has_writable_ = true;
1667 }
1668 bool has_writable() const { return has_writable_; }
1669
1670 Handle<Object> get() const { return get_; }
1671 void set_get(Handle<Object> get) { get_ = get; }
1672 bool has_get() const { return !get_.is_null(); }
1673
1674 Handle<Object> set() const { return set_; }
1675 void set_set(Handle<Object> set) { set_ = set; }
1676 bool has_set() const { return !set_.is_null(); }
1677
1678 Handle<Object> name() const { return name_; }
1679 void set_name(Handle<Object> name) { name_ = name; }
1680
1681 PropertyAttributes ToAttributes() {
1682 return static_cast<PropertyAttributes>(
1683 (has_enumerable() && !enumerable() ? DONT_ENUM : NONE) |
1684 (has_configurable() && !configurable() ? DONT_DELETE : NONE) |
1685 (has_writable() && !writable() ? READ_ONLY : NONE));
1686 }
1687
1688 private:
1689 bool enumerable_ : 1;
1690 bool has_enumerable_ : 1;
1691 bool configurable_ : 1;
1692 bool has_configurable_ : 1;
1693 bool writable_ : 1;
1694 bool has_writable_ : 1;
1695 Handle<Object> value_;
1696 Handle<Object> get_;
1697 Handle<Object> set_;
1698 Handle<Object> name_;
1699 };
1700
1701
1702 // ES6 9.1.5.1
1703 // TODO(jkummerow): Unify with "MaybeHandle<Object> GetOwnProperty()" above.
1704 PropertyDescriptor* GetOwnPropertyDescriptor(Isolate* isolate, Zone* zone,
1705 Handle<JSObject> obj,
1706 Handle<Object> name) {
1707 // 2. If O does not have an own property with key P, return undefined.
1708 LookupIterator it = LookupIterator::PropertyOrElement(isolate, obj, name,
1709 LookupIterator::HIDDEN);
1710 Maybe<PropertyAttributes> maybe = JSObject::GetPropertyAttributes(&it);
1711
1712 if (!maybe.IsJust()) return NULL;
1713 PropertyAttributes attrs = maybe.FromJust();
1714 if (attrs == ABSENT) return NULL;
1715 DCHECK(!isolate->has_pending_exception());
1716
1717 // 3. Let D be a newly created Property Descriptor with no fields.
1718 PropertyDescriptor* d = new (zone) PropertyDescriptor();
1719 // 4. Let X be O's own property whose key is P.
1720 // 5. If X is a data property, then
1721 bool is_accessor_pair = it.state() == LookupIterator::ACCESSOR &&
1722 it.GetAccessors()->IsAccessorPair();
1723 if (!is_accessor_pair) {
1724 // 5a. Set D.[[Value]] to the value of X's [[Value]] attribute.
1725 Handle<Object> value = JSObject::GetProperty(&it).ToHandleChecked();
1726 d->set_value(value);
1727 // 5b. Set D.[[Writable]] to the value of X's [[Writable]] attribute
1728 d->set_writable((attrs & READ_ONLY) == 0);
1729 } else {
1730 // 6. Else X is an accessor property, so
1731 Handle<AccessorPair> accessors =
1732 Handle<AccessorPair>::cast(it.GetAccessors());
1733 // 6a. Set D.[[Get]] to the value of X's [[Get]] attribute.
1734 d->set_get(handle(accessors->GetComponent(ACCESSOR_GETTER), isolate));
1735 // 6b. Set D.[[Set]] to the value of X's [[Set]] attribute.
1736 d->set_set(handle(accessors->GetComponent(ACCESSOR_SETTER), isolate));
1737 }
1738
1739 // 7. Set D.[[Enumerable]] to the value of X's [[Enumerable]] attribute.
1740 d->set_enumerable((attrs & DONT_ENUM) == 0);
1741 // 8. Set D.[[Configurable]] to the value of X's [[Configurable]] attribute.
1742 d->set_configurable((attrs & DONT_DELETE) == 0);
1743 // 9. Return D.
1744 return d;
1745 }
1746
1747
1748 // ES6 9.1.6.1
1749 bool OrdinaryDefineOwnProperty(Isolate* isolate, Zone* zone, Handle<JSObject> o,
1750 Handle<Object> name, PropertyDescriptor* desc,
1751 bool should_throw) {
1752 // == OrdinaryDefineOwnProperty (O, P, Desc) ==
1753 // 1. Let current be O.[[GetOwnProperty]](P).
1754 PropertyDescriptor* current =
1755 GetOwnPropertyDescriptor(isolate, zone, o, name);
1756 // 2. ReturnIfAbrupt(current).
1757 if (current == NULL && isolate->has_pending_exception()) return false;
1758 // 3. Let extensible be the value of the [[Extensible]] internal slot of O.
1759 bool extensible = o->IsExtensible();
1760
1761 bool desc_is_data_descriptor = PropertyDescriptor::IsDataDescriptor(desc);
1762 bool desc_is_accessor_descriptor =
1763 PropertyDescriptor::IsAccessorDescriptor(desc);
1764 bool desc_is_generic_descriptor =
1765 PropertyDescriptor::IsGenericDescriptor(desc);
1766
1767 // == ValidateAndApplyPropertyDescriptor (O, P, extensible, Desc, current) ==
1768 // 2. If current is undefined, then
1769 if (current == NULL) {
1770 // 2a. If extensible is false, return false.
1771 if (!extensible) {
1772 if (should_throw) {
1773 isolate->Throw(*isolate->factory()->NewTypeError(
1774 MessageTemplate::kDefineDisallowed, name));
1775 }
1776 return false;
1777 }
1778 // 2c. If IsGenericDescriptor(Desc) or IsDataDescriptor(Desc) is true, then:
1779 // (This is equivalent to !IsAccessorDescriptor(desc).)
1780 if (!desc_is_accessor_descriptor) {
1781 DCHECK(desc_is_generic_descriptor || desc_is_data_descriptor);
1782 // 2c i. If O is not undefined, create an own data property named P of
1783 // object O whose [[Value]], [[Writable]], [[Enumerable]] and
1784 // [[Configurable]] attribute values are described by Desc. If the value
1785 // of an attribute field of Desc is absent, the attribute of the newly
1786 // created property is set to its default value.
1787 if (!o->IsUndefined()) {
1788 if (!desc->has_writable()) desc->set_writable(false);
1789 if (!desc->has_enumerable()) desc->set_enumerable(false);
1790 if (!desc->has_configurable()) desc->set_configurable(false);
1791 LookupIterator it = LookupIterator::PropertyOrElement(
1792 isolate, o, name, LookupIterator::HIDDEN);
1793 Handle<Object> value(
1794 desc->has_value()
1795 ? desc->value()
1796 : Handle<Object>::cast(isolate->factory()->undefined_value()));
1797 MaybeHandle<Object> result =
1798 JSObject::DefineOwnPropertyIgnoreAttributes(&it, value,
1799 desc->ToAttributes());
1800 if (result.is_null()) return false;
1801 }
1802 } else {
1803 // 2d. Else Desc must be an accessor Property Descriptor,
1804 DCHECK(desc_is_accessor_descriptor);
1805 // 2d i. If O is not undefined, create an own accessor property named P
1806 // of object O whose [[Get]], [[Set]], [[Enumerable]] and
1807 // [[Configurable]] attribute values are described by Desc. If the value
1808 // of an attribute field of Desc is absent, the attribute of the newly
1809 // created property is set to its default value.
1810 if (!o->IsUndefined()) {
1811 if (!desc->has_enumerable()) desc->set_enumerable(false);
1812 if (!desc->has_configurable()) desc->set_configurable(false);
1813 Handle<Object> getter(
1814 desc->has_get()
1815 ? desc->get()
1816 : Handle<Object>::cast(isolate->factory()->undefined_value()));
1817 Handle<Object> setter(
1818 desc->has_set()
1819 ? desc->set()
1820 : Handle<Object>::cast(isolate->factory()->undefined_value()));
1821 MaybeHandle<Object> result = JSObject::DefineAccessor(
1822 o, name, getter, setter, desc->ToAttributes());
1823 if (result.is_null()) return false;
1824 }
1825 }
1826 // 2e. Return true.
1827 return true;
1828 }
1829 // 3. Return true, if every field in Desc is absent.
1830 // 4. Return true, if every field in Desc also occurs in current and the
1831 // value of every field in Desc is the same value as the corresponding field
1832 // in current when compared using the SameValue algorithm.
1833 if ((!desc->has_enumerable() ||
1834 desc->enumerable() == current->enumerable()) &&
1835 (!desc->has_configurable() ||
1836 desc->configurable() == current->configurable()) &&
1837 (!desc->has_value() ||
1838 (current->has_value() && current->value()->SameValue(*desc->value()))) &&
1839 (!desc->has_writable() ||
1840 (current->has_writable() && current->writable() == desc->writable())) &&
1841 (!desc->has_get() ||
1842 (current->has_get() && current->get()->SameValue(*desc->get()))) &&
1843 (!desc->has_set() ||
1844 (current->has_set() && current->set()->SameValue(*desc->set())))) {
1845 return true;
1846 }
1847 // 5. If the [[Configurable]] field of current is false, then
1848 if (!current->configurable()) {
1849 // 5a. Return false, if the [[Configurable]] field of Desc is true.
1850 if (desc->has_configurable() && desc->configurable()) {
1851 if (should_throw) {
1852 isolate->Throw(*isolate->factory()->NewTypeError(
1853 MessageTemplate::kRedefineDisallowed, name));
1854 }
1855 return false;
1856 }
1857 // 5b. Return false, if the [[Enumerable]] field of Desc is present and the
1858 // [[Enumerable]] fields of current and Desc are the Boolean negation of
1859 // each other.
1860 if (desc->has_enumerable() && desc->enumerable() != current->enumerable()) {
1861 if (should_throw) {
1862 isolate->Throw(*isolate->factory()->NewTypeError(
1863 MessageTemplate::kRedefineDisallowed, name));
1864 }
1865 return false;
1866 }
1867 }
1868
1869 bool current_is_data_descriptor =
1870 PropertyDescriptor::IsDataDescriptor(current);
1871 // 6. If IsGenericDescriptor(Desc) is true, no further validation is required.
1872 if (desc_is_generic_descriptor) {
1873 // Nothing to see here.
1874
1875 // 7. Else if IsDataDescriptor(current) and IsDataDescriptor(Desc) have
1876 // different results, then:
1877 } else if (current_is_data_descriptor != desc_is_data_descriptor) {
1878 // 7a. Return false, if the [[Configurable]] field of current is false.
1879 if (!current->configurable()) {
1880 if (should_throw) {
1881 isolate->Throw(*isolate->factory()->NewTypeError(
1882 MessageTemplate::kRedefineDisallowed, name));
1883 }
1884 return false;
1885 }
1886 // 7b. If IsDataDescriptor(current) is true, then:
1887 if (current_is_data_descriptor) {
1888 // 7b i. If O is not undefined, convert the property named P of object O
1889 // from a data property to an accessor property. Preserve the existing
1890 // values of the converted property's [[Configurable]] and [[Enumerable]]
1891 // attributes and set the rest of the property's attributes to their
1892 // default values.
1893 // --> Folded into step 10.
1894 } else {
1895 // 7c i. If O is not undefined, convert the property named P of object O
1896 // from an accessor property to a data property. Preserve the existing
1897 // values of the converted property’s [[Configurable]] and [[Enumerable]]
1898 // attributes and set the rest of the property’s attributes to their
1899 // default values.
1900 // --> Folded into step 10.
1901 }
1902
1903 // 8. Else if IsDataDescriptor(current) and IsDataDescriptor(Desc) are both
1904 // true, then:
1905 } else if (current_is_data_descriptor && desc_is_data_descriptor) {
1906 // 8a. If the [[Configurable]] field of current is false, then:
1907 if (!current->configurable()) {
1908 // [Strong mode] Disallow changing writable -> readonly for
1909 // non-configurable properties.
1910 if (current->writable() && desc->has_writable() && !desc->writable() &&
1911 o->map()->is_strong()) {
1912 if (should_throw) {
1913 isolate->Throw(*isolate->factory()->NewTypeError(
1914 MessageTemplate::kStrongRedefineDisallowed, o, name));
1915 }
1916 return false;
1917 }
1918 // 8a i. Return false, if the [[Writable]] field of current is false and
1919 // the [[Writable]] field of Desc is true.
1920 if (!current->writable() && desc->has_writable() && desc->writable()) {
1921 if (should_throw) {
1922 isolate->Throw(*isolate->factory()->NewTypeError(
1923 MessageTemplate::kRedefineDisallowed, name));
1924 }
1925 return false;
1926 }
1927 // 8a ii. If the [[Writable]] field of current is false, then:
1928 if (!current->writable()) {
1929 // 8a ii 1. Return false, if the [[Value]] field of Desc is present and
1930 // SameValue(Desc.[[Value]], current.[[Value]]) is false.
1931 if (desc->has_value() && !desc->value()->SameValue(*current->value())) {
1932 if (should_throw) {
1933 isolate->Throw(*isolate->factory()->NewTypeError(
1934 MessageTemplate::kRedefineDisallowed, name));
1935 }
1936 return false;
1937 }
1938 }
1939 }
1940 } else {
1941 // 9. Else IsAccessorDescriptor(current) and IsAccessorDescriptor(Desc)
1942 // are both true,
1943 DCHECK(PropertyDescriptor::IsAccessorDescriptor(current) &&
1944 desc_is_accessor_descriptor);
1945 // 9a. If the [[Configurable]] field of current is false, then:
1946 if (!current->configurable()) {
1947 // 9a i. Return false, if the [[Set]] field of Desc is present and
1948 // SameValue(Desc.[[Set]], current.[[Set]]) is false.
1949 if (desc->has_set() && !desc->set()->SameValue(*current->set())) {
1950 if (should_throw) {
1951 isolate->Throw(*isolate->factory()->NewTypeError(
1952 MessageTemplate::kRedefineDisallowed, name));
1953 }
1954 return false;
1955 }
1956 // 9a ii. Return false, if the [[Get]] field of Desc is present and
1957 // SameValue(Desc.[[Get]], current.[[Get]]) is false.
1958 if (desc->has_get() && !desc->get()->SameValue(*current->get())) {
1959 if (should_throw) {
1960 isolate->Throw(*isolate->factory()->NewTypeError(
1961 MessageTemplate::kRedefineDisallowed, name));
1962 }
1963 return false;
1964 }
1965 }
1966 }
1967
1968 // 10. If O is not undefined, then:
1969 if (!o->IsUndefined()) {
1970 // 10a. For each field of Desc that is present, set the corresponding
1971 // attribute of the property named P of object O to the value of the field.
1972 PropertyAttributes attrs = NONE;
1973
1974 if (desc->has_enumerable()) {
1975 attrs = static_cast<PropertyAttributes>(
1976 attrs | (desc->enumerable() ? NONE : DONT_ENUM));
1977 } else {
1978 attrs = static_cast<PropertyAttributes>(
1979 attrs | (current->enumerable() ? NONE : DONT_ENUM));
1980 }
1981 if (desc->has_configurable()) {
1982 attrs = static_cast<PropertyAttributes>(
1983 attrs | (desc->configurable() ? NONE : DONT_DELETE));
1984 } else {
1985 attrs = static_cast<PropertyAttributes>(
1986 attrs | (current->configurable() ? NONE : DONT_DELETE));
1987 }
1988 if (desc_is_data_descriptor ||
1989 (desc_is_generic_descriptor && current_is_data_descriptor)) {
1990 if (desc->has_writable()) {
1991 attrs = static_cast<PropertyAttributes>(
1992 attrs | (desc->writable() ? NONE : READ_ONLY));
1993 } else {
1994 attrs = static_cast<PropertyAttributes>(
1995 attrs | (current->writable() ? NONE : READ_ONLY));
1996 }
1997 LookupIterator it = LookupIterator::PropertyOrElement(
1998 isolate, o, name, LookupIterator::HIDDEN);
1999 Handle<Object> value(
2000 desc->has_value() ? desc->value()
2001 : current->has_value()
2002 ? current->value()
2003 : Handle<Object>::cast(
2004 isolate->factory()->undefined_value()));
2005 MaybeHandle<Object> result =
2006 JSObject::DefineOwnPropertyIgnoreAttributes(&it, value, attrs);
2007 if (result.is_null()) return false;
2008 } else {
2009 DCHECK(desc_is_accessor_descriptor ||
2010 (desc_is_generic_descriptor &&
2011 PropertyDescriptor::IsAccessorDescriptor(current)));
2012 Handle<Object> getter(
2013 desc->has_get() ? desc->get()
2014 : current->has_get()
2015 ? current->get()
2016 : Handle<Object>::cast(
2017 isolate->factory()->undefined_value()));
2018 Handle<Object> setter(
2019 desc->has_set() ? desc->set()
2020 : current->has_set()
2021 ? current->set()
2022 : Handle<Object>::cast(
2023 isolate->factory()->undefined_value()));
2024 MaybeHandle<Object> result =
2025 JSObject::DefineAccessor(o, name, getter, setter, attrs);
2026 if (result.is_null()) return false;
2027 }
2028 }
2029
2030 // 11. Return true.
2031 return true;
2032 }
2033
2034
2035 // TODO(jkummerow): This is copied from FastAsArrayLength() in accessors.cc,
2036 // consider unification.
2037 bool PropertyKeyToArrayLength(Handle<Object> value, uint32_t* length) {
2038 DCHECK(value->IsNumber() || value->IsName());
2039 if (value->ToArrayLength(length)) return true;
2040 // We don't support AsArrayLength, so use AsArrayIndex for now. This just
2041 // misses out on kMaxUInt32.
2042 if (value->IsString()) return String::cast(*value)->AsArrayIndex(length);
2043 return false;
2044 }
2045
2046
2047 // TODO(jkummerow): Unify with ArrayLengthSetter in accessors.cc.
2048 bool AnythingToArrayLength(Isolate* isolate, Handle<Object> length_obj,
2049 uint32_t* output) {
2050 // Fast path: check numbers and strings that can be converted directly
2051 // and unobservably.
2052 if (length_obj->ToUint32(output)) return true;
2053 if (length_obj->IsString() &&
2054 Handle<String>::cast(length_obj)->AsArrayIndex(output)) {
2055 return true;
2056 }
2057 // Slow path: follow steps in ES6 9.4.2.4 "ArraySetLength".
2058 // 3. Let newLen be ToUint32(Desc.[[Value]]).
2059 Handle<Object> uint32_v;
2060 if (!Execution::ToUint32(isolate, length_obj).ToHandle(&uint32_v)) {
2061 // 4. ReturnIfAbrupt(newLen).
2062 return false;
2063 }
2064 // 5. Let numberLen be ToNumber(Desc.[[Value]]).
2065 Handle<Object> number_v;
2066 if (!Object::ToNumber(length_obj).ToHandle(&number_v)) {
2067 // 6. ReturnIfAbrupt(newLen).
2068 return false;
2069 }
2070 // 7. If newLen != numberLen, throw a RangeError exception.
2071 if (uint32_v->Number() != number_v->Number()) {
2072 Handle<Object> exception =
2073 isolate->factory()->NewRangeError(MessageTemplate::kInvalidArrayLength);
2074 isolate->Throw(*exception);
2075 return false;
2076 }
2077 return uint32_v->ToArrayLength(output);
2078 }
2079
2080
2081 bool PropertyKeyToArrayIndex(Handle<Object> index_obj, uint32_t* output) {
2082 return PropertyKeyToArrayLength(index_obj, output) && *output != kMaxUInt32;
2083 }
2084
2085
2086 // ES6 9.4.2.4
2087 bool ArraySetLength(Isolate* isolate, Zone* zone, Handle<JSArray> a,
2088 PropertyDescriptor* desc, bool should_throw) {
2089 // 1. If the [[Value]] field of Desc is absent, then
2090 if (!desc->has_value()) {
2091 // 1a. Return OrdinaryDefineOwnProperty(A, "length", Desc).
2092 return OrdinaryDefineOwnProperty(isolate, zone, a,
2093 isolate->factory()->length_string(), desc,
2094 should_throw);
2095 }
2096 // 2. Let newLenDesc be a copy of Desc.
2097 // (Actual copying is not needed.)
2098 PropertyDescriptor* new_len_desc = desc;
2099 // 3. - 7. Convert Desc.[[Value]] to newLen.
2100 uint32_t new_len = 0;
2101 if (!AnythingToArrayLength(isolate, desc->value(), &new_len)) {
2102 if (should_throw && !isolate->has_pending_exception()) {
2103 isolate->Throw(*isolate->factory()->NewTypeError(
2104 MessageTemplate::kCannotConvertToPrimitive));
2105 }
2106 return false;
2107 }
2108 // 8. Set newLenDesc.[[Value]] to newLen.
2109 // (Not needed.)
2110 // 9. Let oldLenDesc be OrdinaryGetOwnProperty(A, "length").
2111 PropertyDescriptor* old_len_desc = GetOwnPropertyDescriptor(
2112 isolate, zone, a, isolate->factory()->length_string());
2113 // 10. (Assert)
2114 // 11. Let oldLen be oldLenDesc.[[Value]].
2115 uint32_t old_len = 0;
2116 CHECK(old_len_desc->value()->ToArrayLength(&old_len));
2117 // 12. If newLen >= oldLen, then
2118 if (new_len >= old_len) {
2119 // 12a. Return OrdinaryDefineOwnProperty(A, "length", newLenDesc).
2120 new_len_desc->set_value(isolate->factory()->NewNumberFromUint(new_len));
2121 return OrdinaryDefineOwnProperty(isolate, zone, a,
2122 isolate->factory()->length_string(),
2123 new_len_desc, should_throw);
2124 }
2125 // 13. If oldLenDesc.[[Writable]] is false, return false.
2126 if (!old_len_desc->writable()) {
2127 if (should_throw)
2128 isolate->Throw(*isolate->factory()->NewTypeError(
2129 MessageTemplate::kRedefineDisallowed,
2130 isolate->factory()->length_string()));
2131 return false;
2132 }
2133 // 14. If newLenDesc.[[Writable]] is absent or has the value true,
2134 // let newWritable be true.
2135 bool new_writable = false;
2136 if (!new_len_desc->has_writable() || new_len_desc->writable()) {
2137 new_writable = true;
2138 } else {
2139 // 15. Else,
2140 // 15a. Need to defer setting the [[Writable]] attribute to false in case
2141 // any elements cannot be deleted.
2142 // 15b. Let newWritable be false. (It's initialized as "false" anyway.)
2143 // 15c. Set newLenDesc.[[Writable]] to true.
2144 // (Not needed.)
2145 }
2146 // Most of steps 16 through 19 is implemented by JSArray::SetLength.
2147 if (JSArray::ObservableSetLength(a, new_len).is_null()) {
2148 if (should_throw) isolate->OptionalRescheduleException(false);
2149 return false;
2150 }
2151 // Steps 19d-ii, 20.
2152 if (!new_writable) {
2153 PropertyDescriptor* readonly = new (zone) PropertyDescriptor();
2154 readonly->set_writable(false);
2155 OrdinaryDefineOwnProperty(isolate, zone, a,
2156 isolate->factory()->length_string(), readonly,
2157 should_throw);
2158 }
2159 uint32_t actual_new_len = 0;
2160 CHECK(a->length()->ToArrayLength(&actual_new_len));
2161 // Steps 19d-v, 21. Return false if there were non-deletable elements.
2162 return actual_new_len == new_len;
2163 }
2164
2165
2166 // ES6 9.4.2.1
2167 bool DefineOwnProperty_Array(Isolate* isolate, Zone* zone, Handle<JSArray> o,
2168 Handle<Object> name, PropertyDescriptor* desc,
2169 bool should_throw) {
2170 // 1. Assert: IsPropertyKey(P) is true. ("P" is |name|.)
2171 // 2. If P is "length", then:
2172 // TODO(jkummerow): Check if we need slow string comparison.
2173 if (*name == isolate->heap()->length_string()) {
2174 // 2a. Return ArraySetLength(A, Desc).
2175 return ArraySetLength(isolate, zone, o, desc, should_throw);
2176 }
2177 // 3. Else if P is an array index, then:
2178 uint32_t index = 0;
2179 if (PropertyKeyToArrayIndex(name, &index)) {
2180 // 3a. Let oldLenDesc be OrdinaryGetOwnProperty(A, "length").
2181 PropertyDescriptor* old_len_desc = GetOwnPropertyDescriptor(
2182 isolate, zone, o, isolate->factory()->length_string());
2183 // 3b. (Assert)
2184 // 3c. Let oldLen be oldLenDesc.[[Value]].
2185 uint32_t old_len = 0;
2186 CHECK(old_len_desc->value()->ToArrayLength(&old_len));
2187 // 3d. Let index be ToUint32(P).
2188 // (Already done above.)
2189 // 3e. (Assert)
2190 // 3f. If index >= oldLen and oldLenDesc.[[Writable]] is false,
2191 // return false.
2192 if (index >= old_len && old_len_desc->has_writable() &&
2193 !old_len_desc->writable()) {
2194 if (should_throw) {
2195 isolate->Throw(*isolate->factory()->NewTypeError(
2196 MessageTemplate::kDefineDisallowed, name));
2197 }
2198 return false;
2199 }
2200 // 3g. Let succeeded be OrdinaryDefineOwnProperty(A, P, Desc).
2201 bool succeeded =
2202 OrdinaryDefineOwnProperty(isolate, zone, o, name, desc, should_throw);
2203 // 3h. (Assert)
2204 // 3i. If succeeded is false, return false.
2205 if (!succeeded) return false;
2206 // 3j. If index >= oldLen, then:
2207 if (index >= old_len) {
2208 // 3j i. Set oldLenDesc.[[Value]] to index + 1.
2209 old_len_desc->set_value(isolate->factory()->NewNumberFromUint(index + 1));
2210 // 3j ii. Let succeeded be
2211 // OrdinaryDefineOwnProperty(A, "length", oldLenDesc).
2212 OrdinaryDefineOwnProperty(isolate, zone, o,
2213 isolate->factory()->name_string(), old_len_desc,
2214 should_throw);
2215 // 3j iii. (Assert)
2216 }
2217 // 3k. Return true.
2218 return true;
2219 }
2220
2221 // 4. Return OrdinaryDefineOwnProperty(A, P, Desc).
2222 return OrdinaryDefineOwnProperty(isolate, zone, o, name, desc, should_throw);
2223 }
2224
2225
2226 // TODO(jkummerow): DefineOwnProperty_Arguments (9.4.4.2)
2227 // TODO(jkummerow): DefineOwnProperty_IntegerIndexedExotic (9.4.5.3)
2228 // TODO(jkummerow): DefineOwnProperty_Module (9.4.6.6)
2229 // TODO(jkummerow): DefineOwnProperty_Proxy (9.5.6)
2230
2231
2232 bool DefineOwnProperty(Isolate* isolate, Zone* zone, Handle<JSObject> obj,
2233 Handle<Object> name, PropertyDescriptor* desc,
2234 bool should_throw) {
2235 if (obj->IsJSArray()) {
2236 return DefineOwnProperty_Array(isolate, zone, Handle<JSArray>::cast(obj),
2237 name, desc, should_throw);
2238 }
2239 return OrdinaryDefineOwnProperty(isolate, zone, obj, name, desc,
2240 should_throw);
2241 }
2242
2243
2244 // Helper function for ToPropertyDescriptor. Handles the case of "simple"
2245 // objects: nothing on the prototype chain, just own fast data properties.
2246 // Must not have observable side effects, because the slow path will restart
2247 // the entire conversion!
2248 bool ToPropertyDescriptorFastPath(Isolate* isolate, Zone* zone,
2249 Handle<JSObject> obj,
2250 PropertyDescriptor* desc) {
2251 Map* map = obj->map();
2252 if (map->instance_type() != JS_OBJECT_TYPE) return false;
2253 if (map->is_access_check_needed()) return false;
2254 if (map->prototype() != *isolate->initial_object_prototype()) return false;
2255 // TODO(jkummerow): support dictionary properties?
2256 if (map->is_dictionary_map()) return false;
2257 Handle<DescriptorArray> descs =
2258 Handle<DescriptorArray>(map->instance_descriptors());
2259 for (int i = 0; i < map->NumberOfOwnDescriptors(); i++) {
2260 PropertyDetails details = descs->GetDetails(i);
2261 Name* key = descs->GetKey(i);
2262 Handle<Object> value;
2263 switch (details.type()) {
2264 case DATA:
2265 value = JSObject::FastPropertyAt(obj, details.representation(),
2266 FieldIndex::ForDescriptor(map, i));
2267 break;
2268 case DATA_CONSTANT:
2269 value = handle(descs->GetConstant(i), isolate);
2270 break;
2271 case ACCESSOR:
2272 case ACCESSOR_CONSTANT:
2273 // Bail out to slow path.
2274 return false;
2275 }
2276 Heap* heap = isolate->heap();
2277 if (key == heap->enumerable_string()) {
2278 desc->set_enumerable(value->BooleanValue());
2279 } else if (key == heap->configurable_string()) {
2280 desc->set_configurable(value->BooleanValue());
2281 } else if (key == heap->value_string()) {
2282 desc->set_value(value);
2283 } else if (key == heap->writable_string()) {
2284 desc->set_writable(value->BooleanValue());
2285 } else if (key == heap->get_string()) {
2286 // Bail out to slow path to throw an exception if necessary.
2287 if (!value->IsCallable()) return false;
2288 desc->set_get(value);
2289 } else if (key == heap->set_string()) {
2290 // Bail out to slow path to throw an exception if necessary.
2291 if (!value->IsCallable()) return false;
2292 desc->set_set(value);
2293 }
2294 }
2295 if ((desc->has_get() || desc->has_set()) &&
2296 (desc->has_value() || desc->has_writable())) {
2297 // Bail out to slow path to throw an exception.
2298 return false;
2299 }
2300 return true;
2301 }
2302
2303
2304 // Helper function for ToPropertyDescriptor. Comments describe steps for
2305 // "enumerable", other properties are handled the same way.
2306 // Returns false if an exception was thrown.
2307 bool GetPropertyIfPresent(Isolate* isolate, Handle<Object> obj,
2308 Handle<String> name, Handle<Object>* value) {
2309 LookupIterator it(obj, name);
2310 // 4. Let hasEnumerable be HasProperty(Obj, "enumerable").
2311 Maybe<PropertyAttributes> maybe_attr = JSReceiver::GetPropertyAttributes(&it);
2312 // 5. ReturnIfAbrupt(hasEnumerable).
2313 if (!maybe_attr.IsJust()) return false;
2314 // 6. If hasEnumerable is true, then
2315 if (maybe_attr.FromJust() != ABSENT) {
2316 // 6a. Let enum be ToBoolean(Get(Obj, "enumerable")).
2317 // 6b. ReturnIfAbrupt(enum).
2318 if (!JSObject::GetProperty(&it).ToHandle(value)) return false;
2319 }
2320 return true;
2321 }
2322
2323
2324 // ES6 6.2.4.5
2325 // Returns NULL in case of exception.
2326 PropertyDescriptor* ToPropertyDescriptor(Isolate* isolate, Zone* zone,
2327 Handle<Object> obj) {
2328 // 1. ReturnIfAbrupt(Obj).
2329 // 2. If Type(Obj) is not Object, throw a TypeError exception.
2330 if (!obj->IsSpecObject()) {
2331 isolate->Throw(*isolate->factory()->NewTypeError(
2332 MessageTemplate::kPropertyDescObject, obj));
2333 return NULL;
2334 }
2335 // 3. Let desc be a new Property Descriptor that initially has no fields.
2336 PropertyDescriptor* desc = new (zone) PropertyDescriptor();
2337
2338 if (obj->IsJSObject() &&
2339 ToPropertyDescriptorFastPath(isolate, zone, Handle<JSObject>::cast(obj),
2340 desc)) {
2341 return desc;
2342 }
2343
2344 // TODO(jkummerow): Implement JSProxy support.
2345 if (!obj->IsJSProxy()) {
2346 { // enumerable?
2347 Handle<Object> enumerable;
2348 // 4 through 6b.
2349 if (!GetPropertyIfPresent(isolate, obj,
2350 isolate->factory()->enumerable_string(),
2351 &enumerable)) {
2352 return NULL;
2353 }
2354 // 6c. Set the [[Enumerable]] field of desc to enum.
2355 if (!enumerable.is_null()) {
2356 desc->set_enumerable(enumerable->BooleanValue());
2357 }
2358 }
2359 { // configurable?
2360 Handle<Object> configurable;
2361 // 7 through 9b.
2362 if (!GetPropertyIfPresent(isolate, obj,
2363 isolate->factory()->configurable_string(),
2364 &configurable)) {
2365 return NULL;
2366 }
2367 // 9c. Set the [[Configurable]] field of desc to conf.
2368 if (!configurable.is_null()) {
2369 desc->set_configurable(configurable->BooleanValue());
2370 }
2371 }
2372 { // value?
2373 Handle<Object> value;
2374 // 10 through 12b.
2375 if (!GetPropertyIfPresent(isolate, obj,
2376 isolate->factory()->value_string(), &value))
2377 return NULL;
2378 // 12c. Set the [[Value]] field of desc to value.
2379 if (!value.is_null()) desc->set_value(value);
2380 }
2381 { // writable?
2382 Handle<Object> writable;
2383 // 13 through 15b.
2384 if (!GetPropertyIfPresent(
2385 isolate, obj, isolate->factory()->writable_string(), &writable)) {
2386 return NULL;
2387 }
2388 // 15c. Set the [[Writable]] field of desc to writable.
2389 if (!writable.is_null()) desc->set_writable(writable->BooleanValue());
2390 }
2391 { // getter?
2392 Handle<Object> getter;
2393 // 16 through 18b.
2394 if (!GetPropertyIfPresent(isolate, obj, isolate->factory()->get_string(),
2395 &getter))
2396 return NULL;
2397 if (!getter.is_null()) {
2398 // 18c. If IsCallable(getter) is false and getter is not undefined,
2399 // throw a TypeError exception.
2400 if (!getter->IsCallable() && !getter->IsUndefined()) {
2401 isolate->Throw(*isolate->factory()->NewTypeError(
2402 MessageTemplate::kObjectGetterCallable, getter));
2403 return NULL;
2404 }
2405 // 18d. Set the [[Get]] field of desc to getter.
2406 desc->set_get(getter);
2407 }
2408 { // setter?
2409 Handle<Object> setter;
2410 // 19 through 21b.
2411 if (!GetPropertyIfPresent(isolate, obj,
2412 isolate->factory()->set_string(), &setter))
2413 return NULL;
2414 if (!setter.is_null()) {
2415 // 21c. If IsCallable(setter) is false and setter is not undefined,
2416 // throw a TypeError exception.
2417 if (!setter->IsCallable() && !setter->IsUndefined()) {
2418 isolate->Throw(*isolate->factory()->NewTypeError(
2419 MessageTemplate::kObjectSetterCallable, setter));
2420 return NULL;
2421 }
2422 // 21d. Set the [[Set]] field of desc to setter.
2423 desc->set_set(setter);
2424 }
2425 }
2426 // 22. If either desc.[[Get]] or desc.[[Set]] is present, then
2427 // 22a. If either desc.[[Value]] or desc.[[Writable]] is present,
2428 // throw a TypeError exception.
2429 if ((desc->has_get() || desc->has_set()) &&
2430 (desc->has_value() || desc->has_writable())) {
2431 isolate->Throw(*isolate->factory()->NewTypeError(
2432 MessageTemplate::kValueAndAccessor, obj));
2433 return NULL;
2434 }
2435 }
2436 } else {
2437 DCHECK(obj->IsJSProxy());
2438 UNIMPLEMENTED();
2439 }
2440 // 23. Return desc.
2441 return desc;
2442 }
2443
2444
2445 // ES6 19.1.2.3.1
2446 RUNTIME_FUNCTION(Runtime_ObjectDefineProperties) {
2447 HandleScope scope(isolate);
2448 DCHECK(args.length() == 2);
2449 CONVERT_ARG_HANDLE_CHECKED(Object, o, 0);
2450 CONVERT_ARG_HANDLE_CHECKED(Object, properties, 1);
2451
2452 // 1. If Type(O) is not Object, throw a TypeError exception.
2453 // TODO(jkummerow): Implement Proxy support, change to "IsSpecObject".
2454 if (!o->IsJSObject()) {
2455 Handle<String> fun_name =
2456 isolate->factory()->InternalizeUtf8String("Object.defineProperties");
2457 THROW_NEW_ERROR_RETURN_FAILURE(
2458 isolate, NewTypeError(MessageTemplate::kCalledOnNonObject, fun_name));
2459 }
2460 // 2. Let props be ToObject(Properties).
2461 // 3. ReturnIfAbrupt(props).
2462 Handle<JSReceiver> props;
2463 if (!Object::ToObject(isolate, properties).ToHandle(&props)) {
2464 THROW_NEW_ERROR_RETURN_FAILURE(
2465 isolate, NewTypeError(MessageTemplate::kUndefinedOrNullToObject));
2466 }
2467 // 4. Let keys be props.[[OwnPropertyKeys]]().
2468 // 5. ReturnIfAbrupt(keys).
2469 Handle<FixedArray> keys;
2470 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
2471 isolate, keys, JSReceiver::GetKeys(props, JSReceiver::OWN_ONLY,
2472 JSReceiver::INCLUDE_SYMBOLS));
2473 // 6. Let descriptors be an empty List.
2474 ZoneScope zone_scope(isolate->runtime_zone());
2475 int capacity = keys->length();
2476 ZoneList<PropertyDescriptor*> descriptors(capacity, zone_scope.zone());
2477 // 7. Repeat for each element nextKey of keys in List order,
2478 for (int i = 0; i < keys->length(); ++i) {
2479 Handle<Object> next_key(keys->get(i), isolate);
2480 // 7a. Let propDesc be props.[[GetOwnProperty]](nextKey).
2481 // 7b. ReturnIfAbrupt(propDesc).
2482 LookupIterator it = LookupIterator::PropertyOrElement(
2483 isolate, props, next_key, LookupIterator::HIDDEN);
2484 Maybe<PropertyAttributes> maybe = JSObject::GetPropertyAttributes(&it);
2485 if (!maybe.IsJust()) return isolate->heap()->exception();
2486 PropertyAttributes attrs = maybe.FromJust();
2487 // 7c. If propDesc is not undefined and propDesc.[[Enumerable]] is true:
2488 if (attrs == ABSENT) continue;
2489 if ((attrs & DONT_ENUM) == 0) {
2490 // 7c i. Let descObj be Get(props, nextKey).
2491 // 7c ii. ReturnIfAbrupt(descObj).
2492 Handle<Object> desc_obj;
2493 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, desc_obj,
2494 JSObject::GetProperty(&it));
2495 // 7c iii. Let desc be ToPropertyDescriptor(descObj).
2496 PropertyDescriptor* desc =
2497 ToPropertyDescriptor(isolate, zone_scope.zone(), desc_obj);
2498 // 7c iv. ReturnIfAbrupt(desc).
2499 if (desc == NULL) return isolate->heap()->exception();
2500 // 7c v. Append the pair (a two element List) consisting of nextKey and
2501 // desc to the end of descriptors.
2502 desc->set_name(next_key);
2503 descriptors.Add(desc, zone_scope.zone());
2504 }
2505 }
2506 // 8. For each pair from descriptors in list order,
2507 for (int i = 0; i < descriptors.length(); ++i) {
2508 PropertyDescriptor* desc = descriptors[i];
2509 // 8a. Let P be the first element of pair.
2510 // 8b. Let desc be the second element of pair.
2511 // 8c. Let status be DefinePropertyOrThrow(O, P, desc).
2512 bool status =
2513 DefineOwnProperty(isolate, zone_scope.zone(), Handle<JSObject>::cast(o),
2514 desc->name(), desc, true /* should_throw */);
2515 // 8d. ReturnIfAbrupt(status).
2516 if (isolate->has_pending_exception()) return isolate->heap()->exception();
2517 RUNTIME_ASSERT(status == true);
2518 }
2519 // 9. Return o.
2520 return *o;
2521 }
1617 } // namespace internal 2522 } // namespace internal
1618 } // namespace v8 2523 } // namespace v8
OLDNEW
« no previous file with comments | « src/runtime/runtime.h ('k') | src/v8natives.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698