OLD | NEW |
1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 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/builtins.h" | 5 #include "src/builtins.h" |
6 | 6 |
7 #include "src/api-arguments.h" | 7 #include "src/api-arguments.h" |
8 #include "src/api-natives.h" | 8 #include "src/api-natives.h" |
9 #include "src/api.h" | 9 #include "src/api.h" |
10 #include "src/base/once.h" | 10 #include "src/base/once.h" |
(...skipping 1797 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1808 BUILTIN(ObjectDefineProperty) { | 1808 BUILTIN(ObjectDefineProperty) { |
1809 HandleScope scope(isolate); | 1809 HandleScope scope(isolate); |
1810 DCHECK_EQ(4, args.length()); | 1810 DCHECK_EQ(4, args.length()); |
1811 Handle<Object> target = args.at<Object>(1); | 1811 Handle<Object> target = args.at<Object>(1); |
1812 Handle<Object> key = args.at<Object>(2); | 1812 Handle<Object> key = args.at<Object>(2); |
1813 Handle<Object> attributes = args.at<Object>(3); | 1813 Handle<Object> attributes = args.at<Object>(3); |
1814 | 1814 |
1815 return JSReceiver::DefineProperty(isolate, target, key, attributes); | 1815 return JSReceiver::DefineProperty(isolate, target, key, attributes); |
1816 } | 1816 } |
1817 | 1817 |
| 1818 namespace { |
| 1819 |
| 1820 template <AccessorComponent which_accessor> |
| 1821 Object* ObjectDefineAccessor(Isolate* isolate, Handle<Object> object, |
| 1822 Handle<Object> name, Handle<Object> accessor) { |
| 1823 // 1. Let O be ? ToObject(this value). |
| 1824 Handle<JSReceiver> receiver; |
| 1825 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, receiver, |
| 1826 Object::ConvertReceiver(isolate, object)); |
| 1827 // 2. If IsCallable(getter) is false, throw a TypeError exception. |
| 1828 if (!accessor->IsCallable()) { |
| 1829 MessageTemplate::Template message = |
| 1830 which_accessor == ACCESSOR_GETTER |
| 1831 ? MessageTemplate::kObjectGetterExpectingFunction |
| 1832 : MessageTemplate::kObjectSetterExpectingFunction; |
| 1833 THROW_NEW_ERROR_RETURN_FAILURE(isolate, NewTypeError(message)); |
| 1834 } |
| 1835 // 3. Let desc be PropertyDescriptor{[[Get]]: getter, [[Enumerable]]: true, |
| 1836 // [[Configurable]]: true}. |
| 1837 PropertyDescriptor desc; |
| 1838 if (which_accessor == ACCESSOR_GETTER) { |
| 1839 desc.set_get(accessor); |
| 1840 } else { |
| 1841 DCHECK(which_accessor == ACCESSOR_SETTER); |
| 1842 desc.set_set(accessor); |
| 1843 } |
| 1844 desc.set_enumerable(true); |
| 1845 desc.set_configurable(true); |
| 1846 // 4. Let key be ? ToPropertyKey(P). |
| 1847 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, name, |
| 1848 Object::ToPropertyKey(isolate, name)); |
| 1849 // 5. Perform ? DefinePropertyOrThrow(O, key, desc). |
| 1850 Maybe<bool> success = JSReceiver::DefineOwnProperty( |
| 1851 isolate, receiver, name, &desc, Object::THROW_ON_ERROR); |
| 1852 MAYBE_RETURN(success, isolate->heap()->exception()); |
| 1853 CHECK(success.FromJust()); |
| 1854 // 6. Return undefined. |
| 1855 return isolate->heap()->undefined_value(); |
| 1856 } |
| 1857 |
| 1858 Object* ObjectLookupAccessor(Isolate* isolate, Handle<Object> object, |
| 1859 Handle<Object> key, AccessorComponent component) { |
| 1860 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, object, |
| 1861 Object::ConvertReceiver(isolate, object)); |
| 1862 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, key, |
| 1863 Object::ToPropertyKey(isolate, key)); |
| 1864 bool success = false; |
| 1865 LookupIterator it = LookupIterator::PropertyOrElement( |
| 1866 isolate, object, key, &success, |
| 1867 LookupIterator::PROTOTYPE_CHAIN_SKIP_INTERCEPTOR); |
| 1868 DCHECK(success); |
| 1869 |
| 1870 for (; it.IsFound(); it.Next()) { |
| 1871 switch (it.state()) { |
| 1872 case LookupIterator::INTERCEPTOR: |
| 1873 case LookupIterator::NOT_FOUND: |
| 1874 case LookupIterator::TRANSITION: |
| 1875 UNREACHABLE(); |
| 1876 |
| 1877 case LookupIterator::ACCESS_CHECK: |
| 1878 if (it.HasAccess()) continue; |
| 1879 isolate->ReportFailedAccessCheck(it.GetHolder<JSObject>()); |
| 1880 RETURN_FAILURE_IF_SCHEDULED_EXCEPTION(isolate); |
| 1881 return isolate->heap()->undefined_value(); |
| 1882 |
| 1883 case LookupIterator::JSPROXY: |
| 1884 return isolate->heap()->undefined_value(); |
| 1885 |
| 1886 case LookupIterator::INTEGER_INDEXED_EXOTIC: |
| 1887 return isolate->heap()->undefined_value(); |
| 1888 case LookupIterator::DATA: |
| 1889 continue; |
| 1890 case LookupIterator::ACCESSOR: { |
| 1891 Handle<Object> maybe_pair = it.GetAccessors(); |
| 1892 if (maybe_pair->IsAccessorPair()) { |
| 1893 return *AccessorPair::GetComponent( |
| 1894 Handle<AccessorPair>::cast(maybe_pair), component); |
| 1895 } |
| 1896 } |
| 1897 } |
| 1898 } |
| 1899 |
| 1900 return isolate->heap()->undefined_value(); |
| 1901 } |
| 1902 |
| 1903 } // namespace |
| 1904 |
| 1905 // ES6 B.2.2.2 a.k.a. |
| 1906 // https://tc39.github.io/ecma262/#sec-object.prototype.__defineGetter__ |
| 1907 BUILTIN(ObjectDefineGetter) { |
| 1908 HandleScope scope(isolate); |
| 1909 Handle<Object> object = args.at<Object>(0); // Receiver. |
| 1910 Handle<Object> name = args.at<Object>(1); |
| 1911 Handle<Object> getter = args.at<Object>(2); |
| 1912 return ObjectDefineAccessor<ACCESSOR_GETTER>(isolate, object, name, getter); |
| 1913 } |
| 1914 |
| 1915 // ES6 B.2.2.3 a.k.a. |
| 1916 // https://tc39.github.io/ecma262/#sec-object.prototype.__defineSetter__ |
| 1917 BUILTIN(ObjectDefineSetter) { |
| 1918 HandleScope scope(isolate); |
| 1919 Handle<Object> object = args.at<Object>(0); // Receiver. |
| 1920 Handle<Object> name = args.at<Object>(1); |
| 1921 Handle<Object> setter = args.at<Object>(2); |
| 1922 return ObjectDefineAccessor<ACCESSOR_SETTER>(isolate, object, name, setter); |
| 1923 } |
| 1924 |
| 1925 // ES6 B.2.2.4 a.k.a. |
| 1926 // https://tc39.github.io/ecma262/#sec-object.prototype.__lookupGetter__ |
| 1927 BUILTIN(ObjectLookupGetter) { |
| 1928 HandleScope scope(isolate); |
| 1929 Handle<Object> object = args.at<Object>(0); |
| 1930 Handle<Object> name = args.at<Object>(1); |
| 1931 return ObjectLookupAccessor(isolate, object, name, ACCESSOR_GETTER); |
| 1932 } |
| 1933 |
| 1934 // ES6 B.2.2.5 a.k.a. |
| 1935 // https://tc39.github.io/ecma262/#sec-object.prototype.__lookupSetter__ |
| 1936 BUILTIN(ObjectLookupSetter) { |
| 1937 HandleScope scope(isolate); |
| 1938 Handle<Object> object = args.at<Object>(0); |
| 1939 Handle<Object> name = args.at<Object>(1); |
| 1940 return ObjectLookupAccessor(isolate, object, name, ACCESSOR_SETTER); |
| 1941 } |
| 1942 |
1818 // ES6 section 19.1.2.5 Object.freeze ( O ) | 1943 // ES6 section 19.1.2.5 Object.freeze ( O ) |
1819 BUILTIN(ObjectFreeze) { | 1944 BUILTIN(ObjectFreeze) { |
1820 HandleScope scope(isolate); | 1945 HandleScope scope(isolate); |
1821 Handle<Object> object = args.atOrUndefined(isolate, 1); | 1946 Handle<Object> object = args.atOrUndefined(isolate, 1); |
1822 if (object->IsJSReceiver()) { | 1947 if (object->IsJSReceiver()) { |
1823 MAYBE_RETURN(JSReceiver::SetIntegrityLevel(Handle<JSReceiver>::cast(object), | 1948 MAYBE_RETURN(JSReceiver::SetIntegrityLevel(Handle<JSReceiver>::cast(object), |
1824 FROZEN, Object::THROW_ON_ERROR), | 1949 FROZEN, Object::THROW_ON_ERROR), |
1825 isolate->heap()->exception()); | 1950 isolate->heap()->exception()); |
1826 } | 1951 } |
1827 return *object; | 1952 return *object; |
(...skipping 2892 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
4720 MaybeHandle<Object> Builtins::InvokeApiFunction(Handle<HeapObject> function, | 4845 MaybeHandle<Object> Builtins::InvokeApiFunction(Handle<HeapObject> function, |
4721 Handle<Object> receiver, | 4846 Handle<Object> receiver, |
4722 int argc, | 4847 int argc, |
4723 Handle<Object> args[]) { | 4848 Handle<Object> args[]) { |
4724 Isolate* isolate = function->GetIsolate(); | 4849 Isolate* isolate = function->GetIsolate(); |
4725 // Do proper receiver conversion for non-strict mode api functions. | 4850 // Do proper receiver conversion for non-strict mode api functions. |
4726 if (!receiver->IsJSReceiver()) { | 4851 if (!receiver->IsJSReceiver()) { |
4727 DCHECK(function->IsFunctionTemplateInfo() || function->IsJSFunction()); | 4852 DCHECK(function->IsFunctionTemplateInfo() || function->IsJSFunction()); |
4728 if (function->IsFunctionTemplateInfo() || | 4853 if (function->IsFunctionTemplateInfo() || |
4729 is_sloppy(JSFunction::cast(*function)->shared()->language_mode())) { | 4854 is_sloppy(JSFunction::cast(*function)->shared()->language_mode())) { |
4730 if (receiver->IsUndefined() || receiver->IsNull()) { | 4855 ASSIGN_RETURN_ON_EXCEPTION(isolate, receiver, |
4731 receiver = handle(isolate->global_proxy(), isolate); | 4856 Object::ConvertReceiver(isolate, receiver), |
4732 } else { | 4857 Object); |
4733 ASSIGN_RETURN_ON_EXCEPTION(isolate, receiver, | |
4734 Object::ToObject(isolate, receiver), Object); | |
4735 } | |
4736 } | 4858 } |
4737 } | 4859 } |
4738 // Construct BuiltinArguments object: | 4860 // Construct BuiltinArguments object: |
4739 // new target, function, arguments reversed, receiver. | 4861 // new target, function, arguments reversed, receiver. |
4740 const int kBufferSize = 32; | 4862 const int kBufferSize = 32; |
4741 Object* small_argv[kBufferSize]; | 4863 Object* small_argv[kBufferSize]; |
4742 Object** argv; | 4864 Object** argv; |
4743 if (argc + 3 <= kBufferSize) { | 4865 if (argc + 3 <= kBufferSize) { |
4744 argv = small_argv; | 4866 argv = small_argv; |
4745 } else { | 4867 } else { |
(...skipping 648 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
5394 BUILTIN_LIST_T(DEFINE_BUILTIN_ACCESSOR_T) | 5516 BUILTIN_LIST_T(DEFINE_BUILTIN_ACCESSOR_T) |
5395 BUILTIN_LIST_H(DEFINE_BUILTIN_ACCESSOR_H) | 5517 BUILTIN_LIST_H(DEFINE_BUILTIN_ACCESSOR_H) |
5396 BUILTIN_LIST_DEBUG_A(DEFINE_BUILTIN_ACCESSOR_A) | 5518 BUILTIN_LIST_DEBUG_A(DEFINE_BUILTIN_ACCESSOR_A) |
5397 #undef DEFINE_BUILTIN_ACCESSOR_C | 5519 #undef DEFINE_BUILTIN_ACCESSOR_C |
5398 #undef DEFINE_BUILTIN_ACCESSOR_A | 5520 #undef DEFINE_BUILTIN_ACCESSOR_A |
5399 #undef DEFINE_BUILTIN_ACCESSOR_T | 5521 #undef DEFINE_BUILTIN_ACCESSOR_T |
5400 #undef DEFINE_BUILTIN_ACCESSOR_H | 5522 #undef DEFINE_BUILTIN_ACCESSOR_H |
5401 | 5523 |
5402 } // namespace internal | 5524 } // namespace internal |
5403 } // namespace v8 | 5525 } // namespace v8 |
OLD | NEW |