1 // Copyright 2016 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "src/builtins/builtins-utils-inl.h"
6 #include "src/builtins/builtins.h"
7 #include "src/logging/counters.h"
8 #include "src/objects/keys.h"
9 #include "src/objects/lookup.h"
10 #include "src/objects/objects-inl.h"
11 #include "src/objects/property-descriptor.h"
12
13 namespace v8 {
14 namespace internal {
15
16 // -----------------------------------------------------------------------------
17 // ES6 section 26.1 The Reflect Object
18
19 // ES6 section 26.1.3 Reflect.defineProperty
BUILTIN(ReflectDefineProperty)20 BUILTIN(ReflectDefineProperty) {
21 HandleScope scope(isolate);
22 DCHECK_LE(4, args.length());
23 Handle<Object> target = args.at(1);
24 Handle<Object> key = args.at(2);
25 Handle<Object> attributes = args.at(3);
26
27 if (!target->IsJSReceiver()) {
28 THROW_NEW_ERROR_RETURN_FAILURE(
29 isolate, NewTypeError(MessageTemplate::kCalledOnNonObject,
30 isolate->factory()->NewStringFromAsciiChecked(
31 "Reflect.defineProperty")));
32 }
33
34 Handle<Name> name;
35 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, name,
36 Object::ToName(isolate, key));
37
38 PropertyDescriptor desc;
39 if (!PropertyDescriptor::ToPropertyDescriptor(isolate, attributes, &desc)) {
40 return ReadOnlyRoots(isolate).exception();
41 }
42
43 Maybe<bool> result = JSReceiver::DefineOwnProperty(
44 isolate, Handle<JSReceiver>::cast(target), name, &desc, Just(kDontThrow));
45 MAYBE_RETURN(result, ReadOnlyRoots(isolate).exception());
46 return *isolate->factory()->ToBoolean(result.FromJust());
47 }
48
49 // ES6 section 26.1.7 Reflect.getOwnPropertyDescriptor
BUILTIN(ReflectGetOwnPropertyDescriptor)50 BUILTIN(ReflectGetOwnPropertyDescriptor) {
51 HandleScope scope(isolate);
52 DCHECK_LE(3, args.length());
53 Handle<Object> target = args.at(1);
54 Handle<Object> key = args.at(2);
55
56 if (!target->IsJSReceiver()) {
57 THROW_NEW_ERROR_RETURN_FAILURE(
58 isolate, NewTypeError(MessageTemplate::kCalledOnNonObject,
59 isolate->factory()->NewStringFromAsciiChecked(
60 "Reflect.getOwnPropertyDescriptor")));
61 }
62
63 Handle<Name> name;
64 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, name,
65 Object::ToName(isolate, key));
66
67 PropertyDescriptor desc;
68 Maybe<bool> found = JSReceiver::GetOwnPropertyDescriptor(
69 isolate, Handle<JSReceiver>::cast(target), name, &desc);
70 MAYBE_RETURN(found, ReadOnlyRoots(isolate).exception());
71 if (!found.FromJust()) return ReadOnlyRoots(isolate).undefined_value();
72 return *desc.ToObject(isolate);
73 }
74
75 // ES6 section 26.1.11 Reflect.ownKeys
BUILTIN(ReflectOwnKeys)76 BUILTIN(ReflectOwnKeys) {
77 HandleScope scope(isolate);
78 DCHECK_LE(2, args.length());
79 Handle<Object> target = args.at(1);
80
81 if (!target->IsJSReceiver()) {
82 THROW_NEW_ERROR_RETURN_FAILURE(
83 isolate, NewTypeError(MessageTemplate::kCalledOnNonObject,
84 isolate->factory()->NewStringFromAsciiChecked(
85 "Reflect.ownKeys")));
86 }
87
88 Handle<FixedArray> keys;
89 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
90 isolate, keys,
91 KeyAccumulator::GetKeys(Handle<JSReceiver>::cast(target),
92 KeyCollectionMode::kOwnOnly, ALL_PROPERTIES,
93 GetKeysConversion::kConvertToString));
94 return *isolate->factory()->NewJSArrayWithElements(keys);
95 }
96
97 // ES6 section 26.1.13 Reflect.set
BUILTIN(ReflectSet)98 BUILTIN(ReflectSet) {
99 HandleScope scope(isolate);
100 Handle<Object> target = args.atOrUndefined(isolate, 1);
101 Handle<Object> key = args.atOrUndefined(isolate, 2);
102 Handle<Object> value = args.atOrUndefined(isolate, 3);
103 Handle<Object> receiver = args.length() > 4 ? args.at(4) : target;
104
105 if (!target->IsJSReceiver()) {
106 THROW_NEW_ERROR_RETURN_FAILURE(
107 isolate, NewTypeError(MessageTemplate::kCalledOnNonObject,
108 isolate->factory()->NewStringFromAsciiChecked(
109 "Reflect.set")));
110 }
111
112 Handle<Name> name;
113 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, name,
114 Object::ToName(isolate, key));
115
116 PropertyKey lookup_key(isolate, name);
117 LookupIterator it(isolate, receiver, lookup_key,
118 Handle<JSReceiver>::cast(target));
119 Maybe<bool> result = Object::SetSuperProperty(
120 &it, value, StoreOrigin::kMaybeKeyed, Just(ShouldThrow::kDontThrow));
121 MAYBE_RETURN(result, ReadOnlyRoots(isolate).exception());
122 return *isolate->factory()->ToBoolean(result.FromJust());
123 }
124
125 } // namespace internal
126 } // namespace v8
127