1 /*
2 * Copyright (c) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "ecmascript/builtins/builtins_reflect.h"
17 #include "ecmascript/internal_call_params.h"
18 #include "ecmascript/js_tagged_value-inl.h"
19
20 namespace panda::ecmascript::builtins {
21 // ecma 26.1.1 Reflect.apply (target, thisArgument, argumentsList)
ReflectApply(EcmaRuntimeCallInfo * argv)22 JSTaggedValue BuiltinsReflect::ReflectApply(EcmaRuntimeCallInfo *argv)
23 {
24 ASSERT(argv);
25 BUILTINS_API_TRACE(argv->GetThread(), Reflect, Apply);
26 JSThread *thread = argv->GetThread();
27 [[maybe_unused]] EcmaHandleScope handleScope(thread);
28 // 1. If IsCallable(target) is false, throw a TypeError exception.
29 JSHandle<JSTaggedValue> target = GetCallArg(argv, 0);
30 if (!target->IsCallable()) {
31 THROW_TYPE_ERROR_AND_RETURN(thread, "Reflect.apply target is not callable", JSTaggedValue::Exception());
32 }
33 // 2. Let args be ? CreateListFromArrayLike(argumentsList).
34 JSHandle<JSTaggedValue> thisArgument = GetCallArg(argv, 1);
35 JSHandle<JSTaggedValue> argumentsList = GetCallArg(argv, BuiltinsBase::ArgsPosition::THIRD);
36 JSHandle<JSTaggedValue> argOrAbrupt = JSObject::CreateListFromArrayLike(thread, argumentsList);
37 RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
38 JSHandle<TaggedArray> args = JSHandle<TaggedArray>::Cast(argOrAbrupt);
39
40 // 3. Perform PrepareForTailCall().
41 // 4. Return ? Call(target, thisArgument, args).
42 ecmascript::InternalCallParams *arguments = thread->GetInternalCallParams();
43 arguments->MakeArgList(*args);
44 return JSFunction::Call(thread, target, thisArgument, args->GetLength(), arguments->GetArgv());
45 }
46
47 // ecma 26.1.2 Reflect.construct (target, argumentsList [ , newTarget])
ReflectConstruct(EcmaRuntimeCallInfo * argv)48 JSTaggedValue BuiltinsReflect::ReflectConstruct(EcmaRuntimeCallInfo *argv)
49 {
50 ASSERT(argv);
51 BUILTINS_API_TRACE(argv->GetThread(), Reflect, Constructor);
52 JSThread *thread = argv->GetThread();
53 [[maybe_unused]] EcmaHandleScope handleScope(thread);
54 // 1. If IsConstructor(target) is false, throw a TypeError exception.
55 JSHandle<JSTaggedValue> target = GetCallArg(argv, 0);
56 if (!target->IsConstructor()) {
57 THROW_TYPE_ERROR_AND_RETURN(thread, "Reflect.construct target is not constructor", JSTaggedValue::Exception());
58 }
59 // 2. If newTarget is not present, set newTarget to target.
60 JSHandle<JSTaggedValue> newTarget =
61 argv->GetArgsNumber() > 2 ? GetCallArg(argv, BuiltinsBase::ArgsPosition::THIRD) : target; // 2: num args
62 // 3. Else if IsConstructor(newTarget) is false, throw a TypeError exception.
63 if (!newTarget->IsConstructor()) {
64 THROW_TYPE_ERROR_AND_RETURN(thread, "Reflect.construct newTarget is present, but not constructor",
65 JSTaggedValue::Exception());
66 }
67 // 4. Let args be ? CreateListFromArrayLike(argumentsList).
68 JSHandle<JSTaggedValue> argumentsList = GetCallArg(argv, 1);
69 JSHandle<JSTaggedValue> argOrAbrupt = JSObject::CreateListFromArrayLike(thread, argumentsList);
70 RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
71 JSHandle<TaggedArray> args = JSHandle<TaggedArray>::Cast(argOrAbrupt);
72 // 5. Return ? Construct(target, args, newTarget).
73 InternalCallParams *arguments = thread->GetInternalCallParams();
74 arguments->MakeArgList(*args);
75 return JSFunction::Construct(thread, target, args->GetLength(), arguments->GetArgv(), newTarget);
76 }
77
78 // ecma 26.1.3 Reflect.defineProperty (target, propertyKey, attributes)
ReflectDefineProperty(EcmaRuntimeCallInfo * argv)79 JSTaggedValue BuiltinsReflect::ReflectDefineProperty(EcmaRuntimeCallInfo *argv)
80 {
81 ASSERT(argv);
82 BUILTINS_API_TRACE(argv->GetThread(), Reflect, DefineProperty);
83 JSThread *thread = argv->GetThread();
84 [[maybe_unused]] EcmaHandleScope handleScope(thread);
85 // 1. If Type(target) is not Object, throw a TypeError exception.
86 JSHandle<JSTaggedValue> target = GetCallArg(argv, 0);
87 if (!target->IsECMAObject()) {
88 THROW_TYPE_ERROR_AND_RETURN(thread, "Reflect.defineProperty target is not object", JSTaggedValue::Exception());
89 }
90 // 2. Let key be ? ToPropertyKey(propertyKey).
91 JSHandle<JSTaggedValue> key = JSTaggedValue::ToPropertyKey(thread, GetCallArg(argv, 1));
92 RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
93 // 3. Let desc be ? ToPropertyDescriptor(attributes).
94 JSHandle<JSTaggedValue> attributes = GetCallArg(argv, BuiltinsBase::ArgsPosition::THIRD);
95 PropertyDescriptor desc(thread);
96 JSObject::ToPropertyDescriptor(thread, attributes, desc);
97 RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
98 // 4. Return ? target.[[DefineOwnProperty]](key, desc).
99 return GetTaggedBoolean(JSTaggedValue::DefineOwnProperty(thread, target, key, desc));
100 }
101
102 // ecma 21.1.4 Reflect.deleteProperty (target, propertyKey)
ReflectDeleteProperty(EcmaRuntimeCallInfo * argv)103 JSTaggedValue BuiltinsReflect::ReflectDeleteProperty(EcmaRuntimeCallInfo *argv)
104 {
105 ASSERT(argv);
106 BUILTINS_API_TRACE(argv->GetThread(), Reflect, DeleteProperty);
107 JSThread *thread = argv->GetThread();
108 [[maybe_unused]] EcmaHandleScope handleScope(thread);
109 // 1. If Type(target) is not Object, throw a TypeError exception.
110 JSHandle<JSTaggedValue> target = GetCallArg(argv, 0);
111 if (!target->IsECMAObject()) {
112 THROW_TYPE_ERROR_AND_RETURN(thread, "Reflect.deleteProperty target is not object", JSTaggedValue::Exception());
113 }
114 // 2. Let key be ? ToPropertyKey(propertyKey).
115 JSHandle<JSTaggedValue> key = JSTaggedValue::ToPropertyKey(thread, GetCallArg(argv, 1));
116 RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
117 // 3. Return ? target.[[Delete]](key).
118 return GetTaggedBoolean(JSTaggedValue::DeleteProperty(thread, target, key));
119 }
120
121 // ecma 26.1.5 Reflect.get (target, propertyKey [ , receiver])
ReflectGet(EcmaRuntimeCallInfo * argv)122 JSTaggedValue BuiltinsReflect::ReflectGet(EcmaRuntimeCallInfo *argv)
123 {
124 ASSERT(argv);
125 BUILTINS_API_TRACE(argv->GetThread(), Reflect, Get);
126 JSThread *thread = argv->GetThread();
127 [[maybe_unused]] EcmaHandleScope handleScope(thread);
128 // 1. If Type(target) is not Object, throw a TypeError exception.
129 JSHandle<JSTaggedValue> val = GetCallArg(argv, 0);
130 if (!val->IsECMAObject()) {
131 THROW_TYPE_ERROR_AND_RETURN(thread, "Reflect.get target is not object", JSTaggedValue::Exception());
132 }
133 JSHandle<JSObject> target = JSHandle<JSObject>::Cast(val);
134 // 2. Let key be ? ToPropertyKey(propertyKey).
135 JSHandle<JSTaggedValue> key = JSTaggedValue::ToPropertyKey(thread, GetCallArg(argv, 1));
136 RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
137 // 3. If receiver is not present, then
138 // a. Set receiver to target.
139 // 4. Return ? target.[[Get]](key, receiver).
140 if (argv->GetArgsNumber() == 2) { // 2: 2 means that there are 2 args in total
141 return JSObject::GetProperty(thread, target, key).GetValue().GetTaggedValue();
142 }
143 JSHandle<JSTaggedValue> receiver = GetCallArg(argv, BuiltinsBase::ArgsPosition::THIRD);
144 return JSObject::GetProperty(thread, val, key, receiver).GetValue().GetTaggedValue();
145 }
146
147 // ecma 26.1.6 Reflect.getOwnPropertyDescriptor ( target, propertyKey )
ReflectGetOwnPropertyDescriptor(EcmaRuntimeCallInfo * argv)148 JSTaggedValue BuiltinsReflect::ReflectGetOwnPropertyDescriptor(EcmaRuntimeCallInfo *argv)
149 {
150 ASSERT(argv);
151 BUILTINS_API_TRACE(argv->GetThread(), Reflect, GetOwnPropertyDescriptor);
152 JSThread *thread = argv->GetThread();
153 [[maybe_unused]] EcmaHandleScope handleScope(thread);
154 // 1. If Type(target) is not Object, throw a TypeError exception.
155 JSHandle<JSTaggedValue> target = GetCallArg(argv, 0);
156 if (!target->IsECMAObject()) {
157 THROW_TYPE_ERROR_AND_RETURN(thread, "Reflect.getOwnPropertyDescriptor target is not object",
158 JSTaggedValue::Exception());
159 }
160 // 2. Let key be ? ToPropertyKey(propertyKey).
161 JSHandle<JSTaggedValue> key = JSTaggedValue::ToPropertyKey(thread, GetCallArg(argv, 1));
162 RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
163 // 3. Let desc be ? target.[[GetOwnProperty]](key).
164 PropertyDescriptor desc(thread);
165 if (!JSTaggedValue::GetOwnProperty(thread, target, key, desc)) {
166 RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
167 }
168 // 4. Return FromPropertyDescriptor(desc).
169 JSHandle<JSTaggedValue> res = JSObject::FromPropertyDescriptor(thread, desc);
170 return res.GetTaggedValue();
171 }
172
173 // ecma 21.1.7 Reflect.getPrototypeOf (target)
ReflectGetPrototypeOf(EcmaRuntimeCallInfo * argv)174 JSTaggedValue BuiltinsReflect::ReflectGetPrototypeOf(EcmaRuntimeCallInfo *argv)
175 {
176 ASSERT(argv);
177 BUILTINS_API_TRACE(argv->GetThread(), Reflect, GetPrototypeOf);
178 JSThread *thread = argv->GetThread();
179 [[maybe_unused]] EcmaHandleScope handleScope(thread);
180 // 1. If Type(target) is not Object, throw a TypeError exception.
181 JSHandle<JSTaggedValue> val = GetCallArg(argv, 0);
182 if (!val->IsECMAObject()) {
183 THROW_TYPE_ERROR_AND_RETURN(thread, "Reflect.getPrototypeOf target is not object", JSTaggedValue::Exception());
184 }
185 JSHandle<JSObject> target = JSHandle<JSObject>::Cast(val);
186 // 2. Return ? target.[[GetPrototypeOf]]().
187 return target->GetPrototype(thread);
188 }
189
190 // ecma 26.1.8 Reflect.has (target, propertyKey)
ReflectHas(EcmaRuntimeCallInfo * argv)191 JSTaggedValue BuiltinsReflect::ReflectHas(EcmaRuntimeCallInfo *argv)
192 {
193 ASSERT(argv);
194 BUILTINS_API_TRACE(argv->GetThread(), Reflect, Has);
195 JSThread *thread = argv->GetThread();
196 [[maybe_unused]] EcmaHandleScope handleScope(thread);
197 // 1. If Type(target) is not Object, throw a TypeError exception.
198 JSHandle<JSTaggedValue> target = GetCallArg(argv, 0);
199 if (!target->IsECMAObject()) {
200 THROW_TYPE_ERROR_AND_RETURN(thread, "Reflect.has target is not object", JSTaggedValue::Exception());
201 }
202 // 2. Let key be ? ToPropertyKey(propertyKey).
203 JSHandle<JSTaggedValue> key = JSTaggedValue::ToPropertyKey(thread, GetCallArg(argv, 1));
204 RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
205 // 3. Return ? target.[[HasProperty]](key).
206 return GetTaggedBoolean(JSTaggedValue::HasProperty(thread, target, key));
207 }
208
209 // ecma 26.1.9 Reflect.isExtensible (target)
ReflectIsExtensible(EcmaRuntimeCallInfo * argv)210 JSTaggedValue BuiltinsReflect::ReflectIsExtensible(EcmaRuntimeCallInfo *argv)
211 {
212 ASSERT(argv);
213 JSThread *thread = argv->GetThread();
214 [[maybe_unused]] EcmaHandleScope handleScope(thread);
215 // 1. If Type(target) is not Object, throw a TypeError exception.
216 JSHandle<JSTaggedValue> target = GetCallArg(argv, 0);
217 if (!target->IsECMAObject()) {
218 THROW_TYPE_ERROR_AND_RETURN(thread, "Reflect.isExtensible target is not object", JSTaggedValue::Exception());
219 }
220 // 2. Return ? target.[[IsExtensible]]().
221 return GetTaggedBoolean(target->IsExtensible(thread));
222 }
223
224 // ecma 26.1.10 Reflect.ownKeys (target)
ReflectOwnKeys(EcmaRuntimeCallInfo * argv)225 JSTaggedValue BuiltinsReflect::ReflectOwnKeys(EcmaRuntimeCallInfo *argv)
226 {
227 ASSERT(argv);
228 BUILTINS_API_TRACE(argv->GetThread(), Reflect, OwnKeys);
229 JSThread *thread = argv->GetThread();
230 [[maybe_unused]] EcmaHandleScope handleScope(thread);
231 // 1. If Type(target) is not Object, throw a TypeError exception.
232 JSHandle<JSTaggedValue> target = GetCallArg(argv, 0);
233 if (!target->IsECMAObject()) {
234 THROW_TYPE_ERROR_AND_RETURN(thread, "Reflect.ownKeys target is not object", JSTaggedValue::Exception());
235 }
236 // 2. Let keys be ? target.[[OwnPropertyKeys]]().
237 JSHandle<TaggedArray> keys = JSTaggedValue::GetOwnPropertyKeys(thread, target);
238 RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
239 // 3. Return CreateArrayFromList(keys).
240 JSHandle<JSArray> result = JSArray::CreateArrayFromList(thread, keys);
241 return result.GetTaggedValue();
242 }
243
244 // ecma 26.1.11 Reflect.preventExtensions (target)
ReflectPreventExtensions(EcmaRuntimeCallInfo * argv)245 JSTaggedValue BuiltinsReflect::ReflectPreventExtensions(EcmaRuntimeCallInfo *argv)
246 {
247 ASSERT(argv);
248 BUILTINS_API_TRACE(argv->GetThread(), Reflect, PreventExtensions);
249 JSThread *thread = argv->GetThread();
250 [[maybe_unused]] EcmaHandleScope handleScope(thread);
251 // 1. If Type(target) is not Object, throw a TypeError exception.
252 JSHandle<JSTaggedValue> target = GetCallArg(argv, 0);
253 if (!target->IsECMAObject()) {
254 THROW_TYPE_ERROR_AND_RETURN(thread, "Reflect.preventExtensions target is not object",
255 JSTaggedValue::Exception());
256 }
257 // 2. Return ? target.[[PreventExtensions]]().
258 return GetTaggedBoolean(JSTaggedValue::PreventExtensions(thread, target));
259 }
260
261 // ecma 26.1.12 Reflect.set (target, propertyKey, V [ , receiver])
ReflectSet(EcmaRuntimeCallInfo * argv)262 JSTaggedValue BuiltinsReflect::ReflectSet(EcmaRuntimeCallInfo *argv)
263 {
264 ASSERT(argv);
265 BUILTINS_API_TRACE(argv->GetThread(), Reflect, Set);
266 JSThread *thread = argv->GetThread();
267 [[maybe_unused]] EcmaHandleScope handleScope(thread);
268 // 1. If Type(target) is not Object, throw a TypeError exception.
269 JSHandle<JSTaggedValue> targetVal = GetCallArg(argv, 0);
270 if (!targetVal->IsECMAObject()) {
271 THROW_TYPE_ERROR_AND_RETURN(thread, "Reflect.get target is not object", JSTaggedValue::Exception());
272 }
273 // 2. Let key be ? ToPropertyKey(propertyKey).
274 JSHandle<JSTaggedValue> key = JSTaggedValue::ToPropertyKey(thread, GetCallArg(argv, 1));
275 RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
276 JSHandle<JSTaggedValue> value = GetCallArg(argv, BuiltinsBase::ArgsPosition::THIRD);
277 // 3. If receiver is not present, then
278 // a. Set receiver to target.
279 // 4. Return ? target.[[Set]](key, receiver).
280 if (argv->GetArgsNumber() == 3) { // 3: 3 means that there are three args in total
281 return GetTaggedBoolean(JSTaggedValue::SetProperty(thread, targetVal, key, value));
282 }
283 JSHandle<JSTaggedValue> receiver = GetCallArg(argv, 3); // 3: 3 means the third arg
284 return GetTaggedBoolean(JSTaggedValue::SetProperty(thread, targetVal, key, value, receiver));
285 }
286
287 // ecma 26.1.13 Reflect.setPrototypeOf (target, proto)
ReflectSetPrototypeOf(EcmaRuntimeCallInfo * argv)288 JSTaggedValue BuiltinsReflect::ReflectSetPrototypeOf(EcmaRuntimeCallInfo *argv)
289 {
290 ASSERT(argv);
291 BUILTINS_API_TRACE(argv->GetThread(), Reflect, SetPrototypeOf);
292 JSThread *thread = argv->GetThread();
293 [[maybe_unused]] EcmaHandleScope handleScope(thread);
294 // 1. If Type(target) is not Object, throw a TypeError exception.
295 JSHandle<JSTaggedValue> target = GetCallArg(argv, 0);
296 if (!target->IsECMAObject()) {
297 THROW_TYPE_ERROR_AND_RETURN(thread, "Reflect.setPrototypeOf target is not object", JSTaggedValue::Exception());
298 }
299 // 2. If Type(proto) is not Object and proto is not null, throw a TypeError exception.
300 JSHandle<JSTaggedValue> proto = GetCallArg(argv, 1);
301 if (!proto->IsECMAObject() && !proto->IsNull()) {
302 THROW_TYPE_ERROR_AND_RETURN(thread, "SetPrototypeOf: proto is neither Object nor Null",
303 JSTaggedValue::Exception());
304 }
305 // 3. Return ? target.[[SetPrototypeOf]](proto).
306 return GetTaggedBoolean(JSTaggedValue::SetPrototype(thread, target, proto));
307 }
308 } // namespace panda::ecmascript::builtins
309