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/js_proxy.h"
17
18 #include "ecmascript/ecma_macros.h"
19 #include "ecmascript/global_env.h"
20 #include "ecmascript/interpreter/interpreter.h"
21 #include "ecmascript/js_array.h"
22 #include "ecmascript/js_handle.h"
23 #include "ecmascript/object_factory.h"
24
25 namespace panda::ecmascript {
26 // ES6 9.5.15 ProxyCreate(target, handler)
ProxyCreate(JSThread * thread,const JSHandle<JSTaggedValue> & target,const JSHandle<JSTaggedValue> & handler)27 JSHandle<JSProxy> JSProxy::ProxyCreate(JSThread *thread, const JSHandle<JSTaggedValue> &target,
28 const JSHandle<JSTaggedValue> &handler)
29 {
30 // 1. If Type(target) is not Object, throw a TypeError exception.
31 if (!target->IsECMAObject()) {
32 THROW_TYPE_ERROR_AND_RETURN(thread, "ProxyCreate: target is not Object",
33 JSHandle<JSProxy>(thread, JSTaggedValue::Exception()));
34 }
35
36 // 2. If Type(handler) is not Object, throw a TypeError exception.
37 if (!handler->IsECMAObject()) {
38 THROW_TYPE_ERROR_AND_RETURN(thread, "ProxyCreate: handler is not Object",
39 JSHandle<JSProxy>(thread, JSTaggedValue::Exception()));
40 }
41 // 3. Let P be ! MakeBasicObject(« [[ProxyHandler]], [[ProxyTarget]] »).
42 // 6. If IsCallable(target) is true, then P.[[Call]] as specified in 9.5.12.
43
44 // 8. Set the [[ProxyTarget]] internal slot of P to target.
45 // 9. Set the [[ProxyHandler]] internal slot of P to handler.
46 return thread->GetEcmaVM()->GetFactory()->NewJSProxy(target, handler);
47 }
48
49 // ES6 9.5.1 [[GetPrototypeOf]] ( )
GetPrototype(JSThread * thread,const JSHandle<JSProxy> & proxy)50 JSTaggedValue JSProxy::GetPrototype(JSThread *thread, const JSHandle<JSProxy> &proxy)
51 {
52 const GlobalEnvConstants *globalConst = thread->GlobalConstants();
53 // 1. Let handler be the value of the [[ProxyHandler]] internal slot of O.
54 JSHandle<JSTaggedValue> handler(thread, proxy->GetHandler());
55 // 2. If handler is null, throw a TypeError exception.
56 if (handler->IsNull()) {
57 THROW_TYPE_ERROR_AND_RETURN(thread, "JSProxy::GetPrototype: handler is null", JSTaggedValue::Exception());
58 }
59 // 3. Assert: Type(handler) is Object.
60 ASSERT(handler->IsECMAObject());
61 // 4. Let target be the value of the [[ProxyTarget]] internal slot of O.
62 JSHandle<JSTaggedValue> targetHandle(thread, proxy->GetTarget());
63 // 5. Let trap be GetMethod(handler, "getPrototypeOf").
64 JSHandle<JSTaggedValue> name(globalConst->GetHandledGetPrototypeOfString());
65 JSHandle<JSTaggedValue> trap = JSObject::GetMethod(thread, handler, name);
66 // 6. ReturnIfAbrupt(trap).
67 RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
68
69 // 7. If trap is undefined, then Return target.[[GetPrototypeOf]]().
70 if (trap->IsUndefined()) {
71 return JSTaggedValue::GetPrototype(thread, targetHandle);
72 }
73 // 8. Let handlerProto be Call(trap, handler, «target»).
74 JSHandle<JSTaggedValue> undefined = globalConst->GetHandledUndefined();
75 EcmaRuntimeCallInfo *info = EcmaInterpreter::NewRuntimeCallInfo(thread, trap, handler, undefined, 1);
76 RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
77 info->SetCallArg(targetHandle.GetTaggedValue());
78 JSTaggedValue handlerProto = JSFunction::Call(info);
79
80 // 9. ReturnIfAbrupt(handlerProto).
81 RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
82 // 10. If Type(handlerProto) is neither Object nor Null, throw a TypeError exception.
83 if (!handlerProto.IsECMAObject() && !handlerProto.IsNull()) {
84 THROW_TYPE_ERROR_AND_RETURN(thread, "JSProxy::GetPrototype: Type(handlerProto) is neither Object nor Null",
85 JSTaggedValue::Exception());
86 }
87 // 11. Let extensibleTarget be IsExtensible(target).
88 // 12. ReturnIfAbrupt(extensibleTarget).
89 // 13. If extensibleTarget is true, return handlerProto.
90 if (targetHandle->IsExtensible(thread)) {
91 return handlerProto;
92 }
93 RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
94
95 // 14. Let targetProto be target.[[GetPrototypeOf]]().
96 JSTaggedValue targetProto = JSTaggedValue::GetPrototype(thread, targetHandle);
97 // 15. ReturnIfAbrupt(targetProto).
98 RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
99 // 16. If SameValue(handlerProto, targetProto) is false, throw a TypeError exception.
100 if (!JSTaggedValue::SameValue(handlerProto, targetProto)) {
101 THROW_TYPE_ERROR_AND_RETURN(thread, "JSProxy::GetPrototype: SameValue(handlerProto, targetProto) is false",
102 JSTaggedValue::Exception());
103 }
104 // 17. Return handlerProto.
105 return handlerProto;
106 }
107
108 // ES6 9.5.2 [[SetPrototypeOf]] (V)
SetPrototype(JSThread * thread,const JSHandle<JSProxy> & proxy,const JSHandle<JSTaggedValue> & proto)109 bool JSProxy::SetPrototype(JSThread *thread, const JSHandle<JSProxy> &proxy, const JSHandle<JSTaggedValue> &proto)
110 {
111 const GlobalEnvConstants *globalConst = thread->GlobalConstants();
112 // 1. Assert: Either Type(V) is Object or Type(V) is Null.
113 ASSERT(proto->IsECMAObject() || proto->IsNull());
114 // 2. Let handler be the value of the [[ProxyHandler]] internal slot of O.
115 JSTaggedValue handler = proxy->GetHandler();
116 // 3. If handler is null, throw a TypeError exception.
117 if (handler.IsNull()) {
118 THROW_TYPE_ERROR_AND_RETURN(thread, "JSProxy::SetPrototype: handler is null", false);
119 }
120 // 4. Assert: Type(handler) is Object.
121 ASSERT(handler.IsECMAObject());
122 // 5. Let target be the value of the [[ProxyTarget]] internal slot of O.
123 JSHandle<JSTaggedValue> targetHandle(thread, proxy->GetTarget());
124 // 6. Let trap be GetMethod(handler, "setPrototypeOf").
125 JSHandle<JSTaggedValue> name = globalConst->GetHandledSetPrototypeOfString();
126 JSHandle<JSTaggedValue> trap(JSObject::GetMethod(thread, JSHandle<JSTaggedValue>(thread, handler), name));
127 // 7. ReturnIfAbrupt(trap).
128 RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, false);
129
130 // 7. If trap is undefined, then Return target.[[SetPrototypeOf]](V).
131 if (trap->IsUndefined()) {
132 return JSTaggedValue::SetPrototype(thread, targetHandle, proto);
133 }
134 JSHandle<JSTaggedValue> handlerTag(thread, proxy->GetHandler());
135 const int32_t argsLength = 2; // 2: target and proto
136 JSHandle<JSTaggedValue> undefined = globalConst->GetHandledUndefined();
137 EcmaRuntimeCallInfo *info = EcmaInterpreter::NewRuntimeCallInfo(thread, trap, handlerTag, undefined, argsLength);
138 RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, false);
139 info->SetCallArg(targetHandle.GetTaggedValue(), proto.GetTaggedValue());
140 JSTaggedValue trapResult = JSFunction::Call(info);
141
142 // 9. Let booleanTrapResult be ToBoolean(Call(trap, handler, «target, V»)).
143 // If booleanTrapResult is false, return false
144 bool booleanTrapResult = trapResult.ToBoolean();
145 if (!booleanTrapResult) {
146 return false;
147 }
148 // 10. ReturnIfAbrupt(booleanTrapResult).
149 // 11. Let extensibleTarget be IsExtensible(target).
150 // 12. ReturnIfAbrupt(extensibleTarget).
151 // 13. If extensibleTarget is true, return booleanTrapResult
152 if (targetHandle->IsExtensible(thread)) {
153 return booleanTrapResult;
154 }
155 RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, false);
156
157 // 14. Let targetProto be target.[[GetPrototypeOf]]().
158 JSTaggedValue targetProto = JSTaggedValue::GetPrototype(thread, targetHandle);
159 // 15. ReturnIfAbrupt(targetProto).
160 RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, false);
161
162 // 16. If booleanTrapResult is true and SameValue(V, targetProto) is false, throw a TypeError exception.
163 if (booleanTrapResult && !JSTaggedValue::SameValue(proto.GetTaggedValue(), targetProto)) {
164 THROW_TYPE_ERROR_AND_RETURN(thread, "JSProxy::SetPrototype: TypeError of targetProto and Result", false);
165 }
166 // 17. Return handlerProto.
167 return booleanTrapResult;
168 }
169
170 // ES6 9.5.3 [[IsExtensible]] ( )
IsExtensible(JSThread * thread,const JSHandle<JSProxy> & proxy)171 bool JSProxy::IsExtensible(JSThread *thread, const JSHandle<JSProxy> &proxy)
172 {
173 const GlobalEnvConstants *globalConst = thread->GlobalConstants();
174 // 1. Let handler be the value of the [[ProxyHandler]] internal slot of O.
175 JSTaggedValue handler = proxy->GetHandler();
176 // 2. If handler is null, throw a TypeError exception.
177 if (handler.IsNull()) {
178 THROW_TYPE_ERROR_AND_RETURN(thread, "JSProxy::IsExtensible: handler is null", false);
179 }
180 // 3. Assert: Type(handler) is Object.
181 ASSERT(handler.IsECMAObject());
182 // 4. Let target be the value of the [[ProxyTarget]] internal slot of O.
183 JSHandle<JSTaggedValue> targetHandle(thread, proxy->GetTarget());
184 // 5. Let trap be GetMethod(handler, "isExtensible").
185 JSHandle<JSTaggedValue> name = globalConst->GetHandledIsExtensibleString();
186 JSHandle<JSTaggedValue> trap(JSObject::GetMethod(thread, JSHandle<JSTaggedValue>(thread, handler), name));
187 // 6. ReturnIfAbrupt(trap).
188 RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, false);
189
190 // 7. If trap is undefined, then Return target.[[IsExtensible]]().
191 if (trap->IsUndefined()) {
192 return targetHandle->IsExtensible(thread);
193 }
194 // 8. Let booleanTrapResult be ToBoolean(Call(trap, handler, «target»)).
195 JSHandle<JSTaggedValue> newTgt(thread, JSTaggedValue::Undefined());
196 JSHandle<JSTaggedValue> handlerTag(thread, proxy->GetHandler());
197 JSHandle<JSTaggedValue> undefined = globalConst->GetHandledUndefined();
198 EcmaRuntimeCallInfo *info = EcmaInterpreter::NewRuntimeCallInfo(thread, trap, handlerTag, undefined, 1);
199 RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, false);
200 info->SetCallArg(targetHandle.GetTaggedValue());
201 JSTaggedValue trapResult = JSFunction::Call(info);
202
203 bool booleanTrapResult = trapResult.ToBoolean();
204 // 9. ReturnIfAbrupt(booleanTrapResult).
205 RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, false);
206
207 // 10. Let targetResult be target.[[IsExtensible]]().
208 // 11. ReturnIfAbrupt(targetResult).
209 // 12. If SameValue(booleanTrapResult, targetResult) is false, throw a TypeError exception.
210 // 13. Return booleanTrapResult.
211 if (targetHandle->IsExtensible(thread) != booleanTrapResult) {
212 THROW_TYPE_ERROR_AND_RETURN(thread, "JSProxy::IsExtensible: TypeError of targetResult", false);
213 }
214
215 return booleanTrapResult;
216 }
217
218 // ES6 9.5.4 [[PreventExtensions]] ( )
PreventExtensions(JSThread * thread,const JSHandle<JSProxy> & proxy)219 bool JSProxy::PreventExtensions(JSThread *thread, const JSHandle<JSProxy> &proxy)
220 {
221 const GlobalEnvConstants *globalConst = thread->GlobalConstants();
222 // 1. Let handler be the value of the [[ProxyHandler]] internal slot of O.
223 // 2. If handler is null, throw a TypeError exception.
224 // 3. Assert: Type(handler) is Object.
225 // 4. Let target be the value of the [[ProxyTarget]] internal slot of O.
226 // 5. Let trap be GetMethod(handler, "preventExtensions").
227 // 6. ReturnIfAbrupt(trap).
228 // 7. If trap is undefined, then
229 // a. Return target.[[PreventExtensions]]().
230 // 8. Let booleanTrapResult be ToBoolean(Call(trap, handler, «target»)).
231 // 9. ReturnIfAbrupt(booleanTrapResult).
232 JSTaggedValue handler = proxy->GetHandler();
233 if (handler.IsNull()) {
234 THROW_TYPE_ERROR_AND_RETURN(thread, "JSProxy::PreventExtensions: handler is null", false);
235 }
236 ASSERT(handler.IsECMAObject());
237 JSHandle<JSTaggedValue> targetHandle(thread, proxy->GetTarget());
238 JSHandle<JSTaggedValue> name = globalConst->GetHandledPreventExtensionsString();
239 JSHandle<JSTaggedValue> trap(JSObject::GetMethod(thread, JSHandle<JSTaggedValue>(thread, handler), name));
240 // 6. ReturnIfAbrupt(trap).
241 RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, false);
242
243 if (trap->IsUndefined()) {
244 return JSTaggedValue::PreventExtensions(thread, targetHandle);
245 }
246 JSHandle<JSTaggedValue> handlerTag(thread, proxy->GetHandler());
247 JSHandle<JSTaggedValue> undefined = globalConst->GetHandledUndefined();
248 EcmaRuntimeCallInfo *info = EcmaInterpreter::NewRuntimeCallInfo(thread, trap, handlerTag, undefined, 1);
249 RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, false);
250 info->SetCallArg(targetHandle.GetTaggedValue());
251 JSTaggedValue trapResult = JSFunction::Call(info);
252
253 bool booleanTrapResult = trapResult.ToBoolean();
254 // 9. ReturnIfAbrupt(booleanTrapResult).
255 RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, false);
256 // 10. If booleanTrapResult is true, then
257 // a. Let targetIsExtensible be target.[[IsExtensible]]().
258 // b. ReturnIfAbrupt(targetIsExtensible).
259 // c. If targetIsExtensible is true, throw a TypeError exception.
260 if (booleanTrapResult && targetHandle->IsExtensible(thread)) {
261 THROW_TYPE_ERROR_AND_RETURN(thread, "JSProxy::PreventExtensions: targetIsExtensible is true", false);
262 }
263 // 11. Return booleanTrapResult.
264 return booleanTrapResult;
265 }
266
267 // ES6 9.5.5 [[GetOwnProperty]] (P)
GetOwnProperty(JSThread * thread,const JSHandle<JSProxy> & proxy,const JSHandle<JSTaggedValue> & key,PropertyDescriptor & desc)268 bool JSProxy::GetOwnProperty(JSThread *thread, const JSHandle<JSProxy> &proxy, const JSHandle<JSTaggedValue> &key,
269 PropertyDescriptor &desc)
270 {
271 const GlobalEnvConstants *globalConst = thread->GlobalConstants();
272 // 1. Assert: IsPropertyKey(P) is true.
273 ASSERT(JSTaggedValue::IsPropertyKey(key));
274 // 2. Let handler be the value of the [[ProxyHandler]] internal slot of O.
275 // 3. If handler is null, throw a TypeError exception.
276 // 4. Assert: Type(handler) is Object.
277 // 5. Let target be the value of the [[ProxyTarget]] internal slot of O.
278 // 6. Let trap be GetMethod(handler, "getOwnPropertyDescriptor").
279 // 7. ReturnIfAbrupt(trap).
280 // 8. If trap is undefined, then
281 // a. Return target.[[GetOwnProperty]](P).
282 // 9. Let trapResultObj be Call(trap, handler, «target, P»).
283 // 10. ReturnIfAbrupt(trapResultObj).
284 // 11. If Type(trapResultObj) is neither Object nor Undefined, throw a TypeError exception
285 JSTaggedValue handler = proxy->GetHandler();
286 if (handler.IsNull()) {
287 THROW_TYPE_ERROR_AND_RETURN(thread, "JSProxy::GetOwnProperty: handler is null", false);
288 }
289 ASSERT(handler.IsECMAObject());
290 JSHandle<JSTaggedValue> targetHandle(thread, proxy->GetTarget());
291 JSHandle<JSTaggedValue> name = globalConst->GetHandledGetOwnPropertyDescriptorString();
292 JSHandle<JSTaggedValue> trap(JSObject::GetMethod(thread, JSHandle<JSTaggedValue>(thread, handler), name));
293 // 7. ReturnIfAbrupt(trap).
294 RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, false);
295
296 if (trap->IsUndefined()) {
297 return JSTaggedValue::GetOwnProperty(thread, targetHandle, key, desc);
298 }
299 JSHandle<JSTaggedValue> handlerTag(thread, proxy->GetHandler());
300 const int32_t argsLength = 2; // 2: target and key
301 JSHandle<JSTaggedValue> undefined = globalConst->GetHandledUndefined();
302 EcmaRuntimeCallInfo *info = EcmaInterpreter::NewRuntimeCallInfo(thread, trap, handlerTag, undefined, argsLength);
303 RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, false);
304 info->SetCallArg(targetHandle.GetTaggedValue(), key.GetTaggedValue());
305 JSTaggedValue trapResultObj = JSFunction::Call(info);
306
307 JSHandle<JSTaggedValue> resultHandle(thread, trapResultObj);
308
309 // 11. If Type(trapResultObj) is neither Object nor Undefined, throw a TypeError exception.
310 if (!trapResultObj.IsECMAObject() && !trapResultObj.IsUndefined()) {
311 THROW_TYPE_ERROR_AND_RETURN(thread, "JSProxy::GetOwnProperty: TypeError of trapResultObj", false);
312 }
313 // 12. Let targetDesc be target.[[GetOwnProperty]](P).
314 PropertyDescriptor targetDesc(thread);
315 bool found = JSTaggedValue::GetOwnProperty(thread, targetHandle, key, targetDesc);
316 // 13. ReturnIfAbrupt(targetDesc).
317 RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, false);
318
319 // 14. If trapResultObj is undefined, then
320 if (resultHandle->IsUndefined()) {
321 // a. If targetDesc is undefined, return undefined.
322 if (!found) {
323 return false;
324 }
325 // b. If targetDesc.[[Configurable]] is false, throw a TypeError exception.
326 if (!targetDesc.IsConfigurable()) {
327 THROW_TYPE_ERROR_AND_RETURN(thread, "JSProxy::GetOwnProperty: targetDesc.[[Configurable]] is false", false);
328 }
329 // c. Let extensibleTarget be IsExtensible(target).
330 // d. ReturnIfAbrupt(extensibleTarget).
331 // e. Assert: Type(extensibleTarget) is Boolean.
332 // f. If extensibleTarget is false, throw a TypeError exception.
333 if (!targetHandle->IsExtensible(thread)) {
334 THROW_TYPE_ERROR_AND_RETURN(thread, "JSProxy::GetOwnProperty: extensibleTarget is false", false);
335 }
336 // g. Return undefined.
337 return false;
338 }
339 // 15. Let extensibleTarget be IsExtensible(target).
340 // 16. ReturnIfAbrupt(extensibleTarget).
341 // 17. Let resultDesc be ToPropertyDescriptor(trapResultObj).
342 PropertyDescriptor &resultDesc = desc;
343 JSObject::ToPropertyDescriptor(thread, resultHandle, resultDesc);
344 // 18. ReturnIfAbrupt(resultDesc)
345 RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, false);
346 // 19. Call CompletePropertyDescriptor(resultDesc).
347 PropertyDescriptor::CompletePropertyDescriptor(thread, resultDesc);
348 // 20. Let valid be IsCompatiblePropertyDescriptor (extensibleTarget, resultDesc, targetDesc).
349 bool valid = JSObject::IsCompatiblePropertyDescriptor(targetHandle->IsExtensible(thread), resultDesc, targetDesc);
350 if (!valid) {
351 THROW_TYPE_ERROR_AND_RETURN(thread, "JSProxy::GetOwnProperty: TypeError of valid", false);
352 }
353 // 22. If resultDesc.[[Configurable]] is false, then
354 if (!resultDesc.IsConfigurable()) {
355 // a. If targetDesc is undefined or targetDesc.[[Configurable]] is true, then
356 if (!found || targetDesc.IsConfigurable()) {
357 THROW_TYPE_ERROR_AND_RETURN(thread, "JSProxy::GetOwnProperty: TypeError of targetDesc configurable", false);
358 }
359 // b. If resultDesc has a [[Writable]] field and resultDesc.[[Writable]] is false, then
360 // If targetDesc.[[Writable]] is true, throw a TypeError exception.
361 if (resultDesc.HasWritable() && !resultDesc.IsWritable() && targetDesc.IsWritable()) {
362 THROW_TYPE_ERROR_AND_RETURN(thread, "JSProxy::GetOwnProperty: TypeError of targetDesc writable", false);
363 }
364 // b. If resultDesc has a [[Writable]] field and resultDesc.[[Writable]] is false, then
365 // If targetDesc.[[Writable]] is true, throw a TypeError exception.
366 if (resultDesc.HasWritable() && !resultDesc.IsWritable() && targetDesc.IsWritable()) {
367 THROW_TYPE_ERROR_AND_RETURN(thread, "", false);
368 }
369 }
370 // 23. Return resultDesc.
371 return true;
372 }
373
374 // ES6 9.5.6 [[DefineOwnProperty]] (P, Desc)
DefineOwnProperty(JSThread * thread,const JSHandle<JSProxy> & proxy,const JSHandle<JSTaggedValue> & key,const PropertyDescriptor & desc)375 bool JSProxy::DefineOwnProperty(JSThread *thread, const JSHandle<JSProxy> &proxy, const JSHandle<JSTaggedValue> &key,
376 const PropertyDescriptor &desc)
377 {
378 const GlobalEnvConstants *globalConst = thread->GlobalConstants();
379 // step 1 ~ 10 are almost same as GetOwnProperty
380 ASSERT(JSTaggedValue::IsPropertyKey(key));
381 JSTaggedValue handler = proxy->GetHandler();
382 if (handler.IsNull()) {
383 THROW_TYPE_ERROR_AND_RETURN(thread, "JSProxy::DefineOwnProperty: handler is Null", false);
384 }
385 ASSERT(handler.IsECMAObject());
386 JSHandle<JSTaggedValue> targetHandle(thread, proxy->GetTarget());
387 JSHandle<JSTaggedValue> name = globalConst->GetHandledDefinePropertyString();
388 JSHandle<JSTaggedValue> trap(JSObject::GetMethod(thread, JSHandle<JSTaggedValue>(thread, handler), name));
389 // 7. ReturnIfAbrupt(trap).
390 RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, false);
391 if (trap->IsUndefined()) {
392 return JSTaggedValue::DefineOwnProperty(thread, targetHandle, key, desc);
393 }
394
395 // 9. Let descObj be FromPropertyDescriptor(Desc).
396 JSHandle<JSTaggedValue> descObj = JSObject::FromPropertyDescriptor(thread, desc);
397 JSHandle<JSTaggedValue> handlerTag(thread, proxy->GetHandler());
398 const int32_t argsLength = 3; // 3: target, key and desc
399 JSHandle<JSTaggedValue> undefined = globalConst->GetHandledUndefined();
400 EcmaRuntimeCallInfo *info = EcmaInterpreter::NewRuntimeCallInfo(thread, trap, handlerTag, undefined, argsLength);
401 RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, false);
402 info->SetCallArg(targetHandle.GetTaggedValue(), key.GetTaggedValue(), descObj.GetTaggedValue());
403 JSTaggedValue trapResult = JSFunction::Call(info);
404
405 bool booleanTrapResult = trapResult.ToBoolean();
406 // 11. ReturnIfAbrupt(booleanTrapResult).
407 RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, false);
408 if (!booleanTrapResult) {
409 return false;
410 }
411 // 13. Let targetDesc be target.[[GetOwnProperty]](P).
412 PropertyDescriptor targetDesc(thread);
413 bool found = JSTaggedValue::GetOwnProperty(thread, targetHandle, key, targetDesc);
414 // 14. ReturnIfAbrupt(targetDesc).
415 RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, false);
416
417 // 15. Let extensibleTarget be IsExtensible(target).
418 // 16. ReturnIfAbrupt(extensibleTarget).
419 // 17. If Desc has a [[Configurable]] field and if Desc.[[Configurable]] is false, then Let settingConfigFalse be
420 // true.
421 // 18. Else let settingConfigFalse be false.
422 bool settingConfigFalse = false;
423 if (desc.HasConfigurable() && !desc.IsConfigurable()) {
424 settingConfigFalse = true;
425 }
426 // 19. If targetDesc is undefined, then
427 if (!found) {
428 // a. If extensibleTarget is false, throw a TypeError exception.
429 if (!targetHandle->IsExtensible(thread)) {
430 THROW_TYPE_ERROR_AND_RETURN(thread, "JSProxy::DefineOwnProperty: extensibleTarget is false", false);
431 }
432 // b. If settingConfigFalse is true, throw a TypeError exception.
433 if (settingConfigFalse) {
434 THROW_TYPE_ERROR_AND_RETURN(thread, "JSProxy::DefineOwnProperty: settingConfigFalse is true", false);
435 }
436 } else {
437 // a. If IsCompatiblePropertyDescriptor(extensibleTarget, Desc , targetDesc) is false, throw a TypeError
438 // exception.
439 if (!JSObject::IsCompatiblePropertyDescriptor(targetHandle->IsExtensible(thread), desc, targetDesc)) {
440 THROW_TYPE_ERROR_AND_RETURN(thread, "JSProxy::DefineOwnProperty: CompatiblePropertyDescriptor err", false);
441 }
442 // b. If settingConfigFalse is true and targetDesc.[[Configurable]] is true, throw a TypeError exception.
443 if (settingConfigFalse && targetDesc.IsConfigurable()) {
444 THROW_TYPE_ERROR_AND_RETURN(thread, "JSProxy::DefineOwnProperty: TypeError of settingConfigFalse", false);
445 }
446 // c. If IsDataDescriptor(targetDesc) is true, targetDesc.[[Configurable]] is false, and targetDesc.[[Writable]]
447 // is true, then If Desc has a [[Writable]] field and Desc.[[Writable]] is false, throw a TypeError exception.
448 if (targetDesc.IsDataDescriptor() && !targetDesc.IsConfigurable() && targetDesc.IsWritable() &&
449 desc.HasWritable() && !desc.IsWritable()) {
450 THROW_TYPE_ERROR_AND_RETURN(thread, "JSProxy::DefineOwnProperty: TypeError of DataDescriptor", false);
451 }
452 // c. If IsDataDescriptor(targetDesc) is true, targetDesc.[[Configurable]] is false, and targetDesc.[[Writable]]
453 // is true, then If Desc has a [[Writable]] field and Desc.[[Writable]] is false, throw a TypeError exception.
454 if (targetDesc.IsDataDescriptor() && !targetDesc.IsConfigurable() && targetDesc.IsWritable() &&
455 desc.HasWritable() && !desc.IsWritable()) {
456 THROW_TYPE_ERROR_AND_RETURN(thread, "", false);
457 }
458 }
459 // 21. Return true.
460 return true;
461 }
462
463 // ES6 9.5.7 [[HasProperty]] (P)
HasProperty(JSThread * thread,const JSHandle<JSProxy> & proxy,const JSHandle<JSTaggedValue> & key)464 bool JSProxy::HasProperty(JSThread *thread, const JSHandle<JSProxy> &proxy, const JSHandle<JSTaggedValue> &key)
465 {
466 const GlobalEnvConstants *globalConst = thread->GlobalConstants();
467 // step 1 ~ 10 are almost same as GetOwnProperty
468 ASSERT(JSTaggedValue::IsPropertyKey(key));
469 JSTaggedValue handler = proxy->GetHandler();
470 if (handler.IsNull()) {
471 THROW_TYPE_ERROR_AND_RETURN(thread, "JSProxy::HasProperty: handler is Null", false);
472 }
473 ASSERT(handler.IsECMAObject());
474 JSHandle<JSTaggedValue> targetHandle(thread, proxy->GetTarget());
475 JSHandle<JSTaggedValue> name = globalConst->GetHandledHasString();
476 JSHandle<JSTaggedValue> trap(JSObject::GetMethod(thread, JSHandle<JSTaggedValue>(thread, handler), name));
477 // 7. ReturnIfAbrupt(trap).
478 RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, false);
479 if (trap->IsUndefined()) {
480 return JSTaggedValue::HasProperty(thread, targetHandle, key);
481 }
482
483 // 9. Let booleanTrapResult be ToBoolean(Call(trap, handler, «target, P»)).
484 JSHandle<JSTaggedValue> handlerTag(thread, proxy->GetHandler());
485
486 const int32_t argsLength = 2; // 2: target and key
487 JSHandle<JSTaggedValue> undefined = globalConst->GetHandledUndefined();
488 EcmaRuntimeCallInfo *info = EcmaInterpreter::NewRuntimeCallInfo(thread, trap, handlerTag, undefined, argsLength);
489 RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, false);
490 info->SetCallArg(targetHandle.GetTaggedValue(), key.GetTaggedValue());
491 JSTaggedValue trapResult = JSFunction::Call(info);
492
493 bool booleanTrapResult = trapResult.ToBoolean();
494 // 10. ReturnIfAbrupt(booleanTrapResult).
495 RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, false);
496
497 // 11. If booleanTrapResult is false, then
498 if (!booleanTrapResult) {
499 // a. Let targetDesc be target.[[GetOwnProperty]](P).
500 PropertyDescriptor targetDesc(thread);
501 bool found = JSTaggedValue::GetOwnProperty(thread, targetHandle, key, targetDesc);
502 // b. ReturnIfAbrupt(targetDesc).
503 RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, false);
504 // c. If targetDesc is not undefined, then
505 if (found) {
506 // i. If targetDesc.[[Configurable]] is false, throw a TypeError exception.
507 if (!targetDesc.IsConfigurable()) {
508 THROW_TYPE_ERROR_AND_RETURN(thread, "JSProxy::HasProperty: TypeError of targetDesc", false);
509 }
510 // ii. Let extensibleTarget be IsExtensible(target).
511 // iii. ReturnIfAbrupt(extensibleTarget).
512 // iv. If extensibleTarget is false, throw a TypeError exception.
513 if (!targetHandle->IsExtensible(thread)) {
514 THROW_TYPE_ERROR_AND_RETURN(thread, "JSProxy::HasProperty: extensibleTarget is false", false);
515 }
516 }
517 }
518 return booleanTrapResult;
519 }
520
521 // ES6 9.5.8 [[Get]] (P, Receiver)
GetProperty(JSThread * thread,const JSHandle<JSProxy> & proxy,const JSHandle<JSTaggedValue> & key,const JSHandle<JSTaggedValue> & receiver)522 OperationResult JSProxy::GetProperty(JSThread *thread, const JSHandle<JSProxy> &proxy,
523 const JSHandle<JSTaggedValue> &key, const JSHandle<JSTaggedValue> &receiver)
524 {
525 const GlobalEnvConstants *globalConst = thread->GlobalConstants();
526 // step 1 ~ 10 are almost same as GetOwnProperty
527 ASSERT(JSTaggedValue::IsPropertyKey(key));
528 JSTaggedValue handler = proxy->GetHandler();
529 JSHandle<JSTaggedValue> exceptionHandle(thread, JSTaggedValue::Exception());
530 if (handler.IsNull()) {
531 THROW_TYPE_ERROR_AND_RETURN(thread, "JSProxy::GetProperty: handler is Null",
532 OperationResult(thread, exceptionHandle.GetTaggedValue(), PropertyMetaData(false)));
533 }
534 ASSERT(handler.IsECMAObject());
535 JSHandle<JSTaggedValue> targetHandle(thread, proxy->GetTarget());
536 JSHandle<JSTaggedValue> name = globalConst->GetHandledGetString();
537 JSHandle<JSTaggedValue> trap(JSObject::GetMethod(thread, JSHandle<JSTaggedValue>(thread, handler), name));
538 // 7. ReturnIfAbrupt(trap).
539 RETURN_VALUE_IF_ABRUPT_COMPLETION(
540 thread, OperationResult(thread, exceptionHandle.GetTaggedValue(), PropertyMetaData(false)));
541
542 if (trap->IsUndefined()) {
543 return JSTaggedValue::GetProperty(thread, targetHandle, key, receiver);
544 }
545 // 9. Let trapResult be Call(trap, handler, «target, P, Receiver»).
546 JSHandle<JSTaggedValue> handlerTag(thread, proxy->GetHandler());
547 const int32_t argsLength = 3; // 3: «target, P, Receiver»
548 JSHandle<JSTaggedValue> undefined = globalConst->GetHandledUndefined();
549 EcmaRuntimeCallInfo *info = EcmaInterpreter::NewRuntimeCallInfo(thread, trap, handlerTag, undefined, argsLength);
550 RETURN_VALUE_IF_ABRUPT_COMPLETION(
551 thread, OperationResult(thread, exceptionHandle.GetTaggedValue(), PropertyMetaData(false)));
552 info->SetCallArg(targetHandle.GetTaggedValue(), key.GetTaggedValue(), receiver.GetTaggedValue());
553 JSTaggedValue trapResult = JSFunction::Call(info);
554 JSHandle<JSTaggedValue> resultHandle(thread, trapResult);
555
556 // 10. ReturnIfAbrupt(trapResult).
557 RETURN_VALUE_IF_ABRUPT_COMPLETION(
558 thread, OperationResult(thread, exceptionHandle.GetTaggedValue(), PropertyMetaData(false)));
559
560 // 11. Let targetDesc be target.[[GetOwnProperty]](P).
561 PropertyDescriptor targetDesc(thread);
562 bool found = JSTaggedValue::GetOwnProperty(thread, targetHandle, key, targetDesc);
563 // 12. ReturnIfAbrupt(targetDesc).
564 RETURN_VALUE_IF_ABRUPT_COMPLETION(
565 thread, OperationResult(thread, exceptionHandle.GetTaggedValue(), PropertyMetaData(false)));
566
567 // 13. If targetDesc is not undefined, then
568 if (found) {
569 // a. If IsDataDescriptor(targetDesc) and targetDesc.[[Configurable]] is false and targetDesc.[[Writable]] is
570 // false, then
571 if (targetDesc.IsDataDescriptor() && !targetDesc.IsConfigurable() && !targetDesc.IsWritable()) {
572 // i. If SameValue(trapResult, targetDesc.[[Value]]) is false, throw a TypeError exception.
573 if (!JSTaggedValue::SameValue(resultHandle.GetTaggedValue(), targetDesc.GetValue().GetTaggedValue())) {
574 THROW_TYPE_ERROR_AND_RETURN(
575 thread, "JSProxy::GetProperty: TypeError of trapResult",
576 OperationResult(thread, exceptionHandle.GetTaggedValue(), PropertyMetaData(false)));
577 }
578 }
579 // b. If IsAccessorDescriptor(targetDesc) and targetDesc.[[Configurable]] is false and targetDesc.[[Get]] is
580 // undefined, then
581 if (targetDesc.IsAccessorDescriptor() && !targetDesc.IsConfigurable() &&
582 targetDesc.GetGetter()->IsUndefined()) {
583 // i. If trapResult is not undefined, throw a TypeError exception.
584 if (!resultHandle.GetTaggedValue().IsUndefined()) {
585 THROW_TYPE_ERROR_AND_RETURN(
586 thread, "JSProxy::GetProperty: trapResult is not undefined",
587 OperationResult(thread, exceptionHandle.GetTaggedValue(), PropertyMetaData(false)));
588 }
589 }
590 }
591 // 14. Return trapResult.
592 return OperationResult(thread, resultHandle.GetTaggedValue(), PropertyMetaData(true));
593 }
594
595 // ES6 9.5.9 [[Set]] ( P, V, Receiver)
SetProperty(JSThread * thread,const JSHandle<JSProxy> & proxy,const JSHandle<JSTaggedValue> & key,const JSHandle<JSTaggedValue> & value,const JSHandle<JSTaggedValue> & receiver,bool mayThrow)596 bool JSProxy::SetProperty(JSThread *thread, const JSHandle<JSProxy> &proxy, const JSHandle<JSTaggedValue> &key,
597 const JSHandle<JSTaggedValue> &value, const JSHandle<JSTaggedValue> &receiver, bool mayThrow)
598 {
599 const GlobalEnvConstants *globalConst = thread->GlobalConstants();
600 // step 1 ~ 10 are almost same as GetOwnProperty
601 ASSERT(JSTaggedValue::IsPropertyKey(key));
602 JSTaggedValue handler = proxy->GetHandler();
603 if (handler.IsNull()) {
604 THROW_TYPE_ERROR_AND_RETURN(thread, "JSProxy::SetProperty: handler is Null", false);
605 }
606 ASSERT(handler.IsECMAObject());
607 JSHandle<JSTaggedValue> targetHandle(thread, proxy->GetTarget());
608 JSHandle<JSTaggedValue> name = globalConst->GetHandledSetString();
609 JSHandle<JSTaggedValue> trap(JSObject::GetMethod(thread, JSHandle<JSTaggedValue>(thread, handler), name));
610 // 7. ReturnIfAbrupt(trap).
611 RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, false);
612 if (trap->IsUndefined()) {
613 return JSTaggedValue::SetProperty(thread, targetHandle, key, value, receiver, mayThrow);
614 }
615
616 // 9. Let booleanTrapResult be ToBoolean(Call(trap, handler, «target, P, V, Receiver»))
617 JSHandle<JSTaggedValue> handlerTag(thread, proxy->GetHandler());
618 const int32_t argsLength = 4; // 4: «target, P, V, Receiver»
619 JSHandle<JSTaggedValue> undefined = globalConst->GetHandledUndefined();
620 EcmaRuntimeCallInfo *info = EcmaInterpreter::NewRuntimeCallInfo(thread, trap, handlerTag, undefined, argsLength);
621 RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, false);
622 info->SetCallArg(
623 targetHandle.GetTaggedValue(), key.GetTaggedValue(), value.GetTaggedValue(), receiver.GetTaggedValue());
624 JSTaggedValue trapResult = JSFunction::Call(info);
625
626 bool booleanTrapResult = trapResult.ToBoolean();
627 // 11. ReturnIfAbrupt(booleanTrapResult).
628 RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, false);
629 if (!booleanTrapResult) {
630 return false;
631 }
632 // 13. Let targetDesc be target.[[GetOwnProperty]](P).
633 PropertyDescriptor targetDesc(thread);
634 bool found = JSTaggedValue::GetOwnProperty(thread, targetHandle, key, targetDesc);
635 // 14. If targetDesc is not undefined, then
636 if (found) {
637 // a. If IsDataDescriptor(targetDesc) and targetDesc.[[Configurable]] is false and targetDesc.[[Writable]] is
638 // false, then
639 if (targetDesc.IsDataDescriptor() && !targetDesc.IsConfigurable() && !targetDesc.IsWritable()) {
640 // i. If SameValue(trapResult, targetDesc.[[Value]]) is false, throw a TypeError exception.
641 if (!JSTaggedValue::SameValue(value, targetDesc.GetValue())) {
642 THROW_TYPE_ERROR_AND_RETURN(thread, "JSProxy::SetProperty: TypeError of trapResult", false);
643 }
644 }
645 // b. If IsAccessorDescriptor(targetDesc) and targetDesc.[[Configurable]] is false, then
646 // i. If targetDesc.[[Set]] is undefined, throw a TypeError exception.
647 if (targetDesc.IsAccessorDescriptor() && !targetDesc.IsConfigurable() &&
648 targetDesc.GetSetter()->IsUndefined()) {
649 THROW_TYPE_ERROR_AND_RETURN(thread, "JSProxy::SetProperty: TypeError of AccessorDescriptor", false);
650 }
651 }
652 return true;
653 }
654
655 // ES6 9.5.10 [[Delete]] (P)
DeleteProperty(JSThread * thread,const JSHandle<JSProxy> & proxy,const JSHandle<JSTaggedValue> & key)656 bool JSProxy::DeleteProperty(JSThread *thread, const JSHandle<JSProxy> &proxy, const JSHandle<JSTaggedValue> &key)
657 {
658 const GlobalEnvConstants *globalConst = thread->GlobalConstants();
659 // step 1 ~ 13 are almost same as GetOwnProperty
660 ASSERT(JSTaggedValue::IsPropertyKey(key));
661 JSTaggedValue handler = proxy->GetHandler();
662 if (handler.IsNull()) {
663 THROW_TYPE_ERROR_AND_RETURN(thread, "JSProxy::DeleteProperty: handler is Null", false);
664 }
665 ASSERT(handler.IsECMAObject());
666 JSHandle<JSTaggedValue> targetHandle(thread, proxy->GetTarget());
667 JSHandle<JSTaggedValue> name = globalConst->GetHandledDeletePropertyString();
668 JSHandle<JSTaggedValue> trap(JSObject::GetMethod(thread, JSHandle<JSTaggedValue>(thread, handler), name));
669 // 7. ReturnIfAbrupt(trap).
670 RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, false);
671 if (trap->IsUndefined()) {
672 return JSTaggedValue::DeleteProperty(thread, targetHandle, key);
673 }
674
675 // 9. Let booleanTrapResult be ToBoolean(Call(trap, handler, «target, P»)).
676 JSHandle<JSTaggedValue> newTgt(thread, JSTaggedValue::Undefined());
677 JSHandle<JSTaggedValue> handlerTag(thread, proxy->GetHandler());
678 const int32_t argsLength = 2; // 2: target and key
679 JSHandle<JSTaggedValue> undefined = globalConst->GetHandledUndefined();
680 EcmaRuntimeCallInfo *info = EcmaInterpreter::NewRuntimeCallInfo(thread, trap, handlerTag, undefined, argsLength);
681 RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, false);
682 info->SetCallArg(targetHandle.GetTaggedValue(), key.GetTaggedValue());
683 JSTaggedValue trapResult = JSFunction::Call(info);
684
685 bool booleanTrapResult = trapResult.ToBoolean();
686 // 11. ReturnIfAbrupt(booleanTrapResult).
687 RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, false);
688 if (!booleanTrapResult) {
689 return false;
690 }
691 // 13. Let targetDesc be target.[[GetOwnProperty]](P).
692 PropertyDescriptor targetDesc(thread);
693 bool found = JSTaggedValue::GetOwnProperty(thread, targetHandle, key, targetDesc);
694 // 14. If targetDesc is undefined, return true.
695 if (!found) {
696 return true;
697 }
698 // 15. If targetDesc.[[Configurable]] is false, throw a TypeError exception.
699 if (!targetDesc.IsConfigurable()) {
700 THROW_TYPE_ERROR_AND_RETURN(thread, "JSProxy::DeleteProperty: targetDesc is not Configurable", false);
701 }
702 if (!targetHandle->IsExtensible(thread)) {
703 THROW_TYPE_ERROR_AND_RETURN(thread, "JSProxy::DeleteProperty: targetHandle is not Extensible", false);
704 }
705 if (!targetHandle->IsExtensible(thread)) {
706 THROW_TYPE_ERROR_AND_RETURN(thread, "", false);
707 }
708 // 16. Return true.
709 return true;
710 }
711
712 // ES6 9.5.12 [[OwnPropertyKeys]] ()
OwnPropertyKeys(JSThread * thread,const JSHandle<JSProxy> & proxy)713 JSHandle<TaggedArray> JSProxy::OwnPropertyKeys(JSThread *thread, const JSHandle<JSProxy> &proxy)
714 {
715 const GlobalEnvConstants *globalConst = thread->GlobalConstants();
716 // step 1 ~ 4 get ProxyHandler and ProxyTarget
717 JSTaggedValue handler = proxy->GetHandler();
718 if (handler.IsNull()) {
719 THROW_TYPE_ERROR_AND_RETURN(thread, "OwnPropertyKeys: handler is null",
720 JSHandle<TaggedArray>(thread, JSTaggedValue::Exception()));
721 }
722
723 ASSERT(handler.IsECMAObject());
724 JSHandle<JSTaggedValue> targetHandle(thread, proxy->GetTarget());
725
726 // 5.Let trap be GetMethod(handler, "ownKeys").
727 JSHandle<JSTaggedValue> key = globalConst->GetHandledOwnKeysString();
728 JSHandle<JSTaggedValue> handlerHandle(thread, handler);
729 JSHandle<JSTaggedValue> trap(JSObject::GetMethod(thread, handlerHandle, key));
730
731 // 6.ReturnIfAbrupt(trap).
732 RETURN_HANDLE_IF_ABRUPT_COMPLETION(TaggedArray, thread);
733
734 // 7.If trap is undefined, then
735 // a.Return target.[[OwnPropertyKeys]]().
736 if (trap->IsUndefined()) {
737 return JSTaggedValue::GetOwnPropertyKeys(thread, targetHandle);
738 }
739
740 // 8.Let trapResultArray be Call(trap, handler, «target»).
741 JSHandle<JSFunction> tagFunc(targetHandle);
742 JSHandle<JSTaggedValue> undefined = globalConst->GetHandledUndefined();
743 EcmaRuntimeCallInfo *info = EcmaInterpreter::NewRuntimeCallInfo(thread, trap, handlerHandle, undefined, 1);
744 RETURN_HANDLE_IF_ABRUPT_COMPLETION(TaggedArray, thread);
745 info->SetCallArg(targetHandle.GetTaggedValue());
746 JSTaggedValue res = JSFunction::Call(info);
747 RETURN_HANDLE_IF_ABRUPT_COMPLETION(TaggedArray, thread);
748 JSHandle<JSTaggedValue> trap_res_arr(thread, res);
749
750 // 9.Let trapResult be CreateListFromArrayLike(trapResultArray, «String, Symbol»).
751 // 10.ReturnIfAbrupt(trapResult)
752 // If trapResult contains any duplicate entries, throw a TypeError exception.
753 JSHandle<TaggedArray> trapRes(
754 JSObject::CreateListFromArrayLike<ElementTypes::STRING_AND_SYMBOL>(thread, trap_res_arr));
755 RETURN_HANDLE_IF_ABRUPT_COMPLETION(TaggedArray, thread);
756
757 if (trapRes->HasDuplicateEntry()) {
758 THROW_TYPE_ERROR_AND_RETURN(thread, "OwnPropertyKeys: contains duplicate entries",
759 JSHandle<TaggedArray>(thread, JSTaggedValue::Exception()));
760 }
761
762 // 11.Let extensibleTarget be IsExtensible(target).
763 bool extensibleTarget = targetHandle->IsExtensible(thread);
764
765 // 12.ReturnIfAbrupt(extensibleTarget).
766 RETURN_HANDLE_IF_ABRUPT_COMPLETION(TaggedArray, thread);
767
768 // 13.Let targetKeys be target.[[OwnPropertyKeys]]().
769 JSHandle<TaggedArray> targetKeys = JSTaggedValue::GetOwnPropertyKeys(thread, targetHandle);
770
771 // 14.ReturnIfAbrupt(targetKeys).
772 RETURN_HANDLE_IF_ABRUPT_COMPLETION(TaggedArray, thread);
773
774 // 15.Assert: targetKeys is a List containing only String and Symbol values.
775 // 16.Let targetConfigurableKeys be an empty List.
776 // 17.Let targetNonconfigurableKeys be an empty List.
777 // 18.Repeat, for each element key of targetKeys,
778 // a.Let desc be target.[[GetOwnProperty]](key).
779 // b.ReturnIfAbrupt(desc).
780 // c.If desc is not undefined and desc.[[Configurable]] is false, then
781 // i.Append key as an element of targetNonconfigurableKeys.
782 // d.Else,
783 // i.Append key as an element of targetConfigurableKeys.
784 uint32_t length = targetKeys->GetLength();
785 JSHandle<TaggedArray> tgtCfigKeys = thread->GetEcmaVM()->GetFactory()->NewTaggedArray(length);
786 JSHandle<TaggedArray> tgtNoCfigKeys = thread->GetEcmaVM()->GetFactory()->NewTaggedArray(length);
787
788 uint32_t cfigLength = 0;
789 uint32_t noCfigLength = 0;
790 for (uint32_t i = 0; i < length; i++) {
791 JSHandle<JSTaggedValue> targetKey(thread, targetKeys->Get(i));
792 ASSERT(targetKey->IsStringOrSymbol());
793
794 PropertyDescriptor desc(thread);
795 JSTaggedValue::GetOwnProperty(thread, targetHandle, targetKey, desc);
796 RETURN_HANDLE_IF_ABRUPT_COMPLETION(TaggedArray, thread);
797
798 if (!desc.IsEmpty() && !desc.IsConfigurable()) {
799 tgtNoCfigKeys->Set(thread, noCfigLength, targetKey);
800 noCfigLength++;
801 } else {
802 tgtCfigKeys->Set(thread, cfigLength, targetKey);
803 cfigLength++;
804 }
805 }
806
807 // 19.If extensibleTarget is true and targetNonconfigurableKeys is empty, then
808 // a.Return trapResult.
809 if (extensibleTarget && (cfigLength == 0)) {
810 return trapRes;
811 }
812
813 // 20.Let uncheckedResultKeys be a new List which is a copy of trapResult.
814 JSHandle<TaggedArray> uncheckFesKeys =
815 thread->GetEcmaVM()->GetFactory()->CopyArray(trapRes, trapRes->GetLength(), trapRes->GetLength());
816 uint32_t uncheckLength = uncheckFesKeys->GetLength();
817
818 // 21.Repeat, for each key that is an element of targetNonconfigurableKeys,
819 // a.If key is not an element of uncheckedResultKeys, throw a TypeError exception.
820 // b.Remove key from uncheckedResultKeys
821 for (uint32_t i = 0; i < noCfigLength; i++) {
822 uint32_t idx = uncheckFesKeys->GetIdx(tgtNoCfigKeys->Get(i));
823 if (idx == TaggedArray::MAX_ARRAY_INDEX) {
824 THROW_TYPE_ERROR_AND_RETURN(thread, "OwnPropertyKeys: key is not an element of uncheckedResultKeys",
825 JSHandle<TaggedArray>(thread, JSTaggedValue::Exception()));
826 }
827 uncheckFesKeys->Set(thread, idx, JSTaggedValue::Hole());
828 uncheckLength--;
829 }
830
831 // 22.If extensibleTarget is true, return trapResult.
832 if (extensibleTarget) {
833 return trapRes;
834 }
835
836 // 23.Repeat, for each key that is an element of targetConfigurableKeys,
837 // a.If key is not an element of uncheckedResultKeys, throw a TypeError exception.
838 // b.Remove key from uncheckedResultKeys
839 for (uint32_t i = 0; i < cfigLength; i++) {
840 uint32_t idx = uncheckFesKeys->GetIdx(tgtCfigKeys->Get(i));
841 if (idx == TaggedArray::MAX_ARRAY_INDEX) {
842 THROW_TYPE_ERROR_AND_RETURN(thread, "OwnPropertyKeys: key is not an element of uncheckedResultKeys",
843 JSHandle<TaggedArray>(thread, JSTaggedValue::Exception()));
844 }
845 uncheckFesKeys->Set(thread, idx, JSTaggedValue::Hole());
846 uncheckLength--;
847 }
848
849 // 24.If uncheckedResultKeys is not empty, throw a TypeError exception.
850 if (uncheckLength != 0) {
851 THROW_TYPE_ERROR_AND_RETURN(thread, "OwnPropertyKeys: uncheckedResultKeys is not empty",
852 JSHandle<TaggedArray>(thread, JSTaggedValue::Exception()));
853 }
854
855 // 25.Return trapResult.
856 return trapRes;
857 }
858
859 // ES6 9.5.13 [[Call]] (thisArgument, argumentsList)
CallInternal(EcmaRuntimeCallInfo * info)860 JSTaggedValue JSProxy::CallInternal(EcmaRuntimeCallInfo *info)
861 {
862 if (info == nullptr) {
863 return JSTaggedValue::Exception();
864 }
865
866 JSThread *thread = info->GetThread();
867 const GlobalEnvConstants *globalConst = thread->GlobalConstants();
868 JSHandle<JSProxy> proxy(info->GetFunction());
869 // step 1 ~ 4 get ProxyHandler and ProxyTarget
870 JSHandle<JSTaggedValue> handler(thread, proxy->GetHandler());
871 if (handler->IsNull()) {
872 THROW_TYPE_ERROR_AND_RETURN(thread, "Call: handler is null", JSTaggedValue::Exception());
873 }
874 ASSERT(handler->IsECMAObject());
875 JSHandle<JSTaggedValue> target(thread, proxy->GetTarget());
876
877 // 5.Let trap be GetMethod(handler, "apply").
878 JSHandle<JSTaggedValue> key(globalConst->GetHandledApplyString());
879 JSHandle<JSTaggedValue> method = JSObject::GetMethod(thread, handler, key);
880
881 // 6.ReturnIfAbrupt(trap).
882 RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
883 uint32_t argc = info->GetArgsNumber();
884 JSHandle<JSTaggedValue> thisArg = info->GetThis();
885 JSHandle<JSTaggedValue> undefined = globalConst->GetHandledUndefined();
886 // 7.If trap is undefined, then
887 // a.Return Call(target, thisArgument, argumentsList).
888 if (method->IsUndefined()) {
889 EcmaRuntimeCallInfo *runtimeInfo =
890 EcmaInterpreter::NewRuntimeCallInfo(thread, target, thisArg, undefined, argc);
891 RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
892 runtimeInfo->SetCallArg(argc, 0, info, 0);
893 return JSFunction::Call(runtimeInfo);
894 }
895 // 8.Let argArray be CreateArrayFromList(argumentsList).
896 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
897 JSHandle<TaggedArray> taggedArray = factory->NewTaggedArray(argc);
898 for (uint32_t index = 0; index < argc; ++index) {
899 taggedArray->Set(thread, index, info->GetCallArg(index));
900 }
901 JSHandle<JSArray> arrHandle = JSArray::CreateArrayFromList(thread, taggedArray);
902
903 // 9.Return Call(trap, handler, «target, thisArgument, argArray»).
904 const uint32_t argsLength = 3; // 3: «target, thisArgument, argArray»
905 EcmaRuntimeCallInfo *runtimeInfo =
906 EcmaInterpreter::NewRuntimeCallInfo(thread, method, handler, undefined, argsLength);
907 RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
908 runtimeInfo->SetCallArg(target.GetTaggedValue(), thisArg.GetTaggedValue(), arrHandle.GetTaggedValue());
909 return JSFunction::Call(runtimeInfo);
910 }
911
912 // ES6 9.5.14 [[Construct]] ( argumentsList, newTarget)
ConstructInternal(EcmaRuntimeCallInfo * info)913 JSTaggedValue JSProxy::ConstructInternal(EcmaRuntimeCallInfo *info)
914 {
915 if (info == nullptr) {
916 return JSTaggedValue::Exception();
917 }
918
919 JSThread *thread = info->GetThread();
920 const GlobalEnvConstants *globalConst = thread->GlobalConstants();
921 // step 1 ~ 4 get ProxyHandler and ProxyTarget
922 JSHandle<JSProxy> proxy(info->GetFunction());
923 JSHandle<JSTaggedValue> handler(thread, proxy->GetHandler());
924 if (handler->IsNull()) {
925 THROW_TYPE_ERROR_AND_RETURN(thread, "Constructor: handler is null", JSTaggedValue::Exception());
926 }
927 ASSERT(handler->IsECMAObject());
928 JSHandle<JSTaggedValue> target(thread, proxy->GetTarget());
929
930 // 5.Let trap be GetMethod(handler, "construct").
931 JSHandle<JSTaggedValue> key(globalConst->GetHandledProxyConstructString());
932 JSHandle<JSTaggedValue> method = JSObject::GetMethod(thread, handler, key);
933
934 // 6.ReturnIfAbrupt(trap).
935 RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
936 // 7.If trap is undefined, then
937 // a.Assert: target has a [[Construct]] internal method.
938 // b.Return Construct(target, argumentsList, newTarget).
939 if (method->IsUndefined()) {
940 ASSERT(target->IsConstructor());
941 info->SetFunction(target.GetTaggedValue());
942 return JSFunction::Construct(info);
943 }
944
945 // 8.Let argArray be CreateArrayFromList(argumentsList).
946 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
947 uint32_t argc = info->GetArgsNumber();
948 JSHandle<TaggedArray> taggedArray = factory->NewTaggedArray(argc);
949 for (uint32_t index = 0; index < argc; ++index) {
950 taggedArray->Set(thread, index, info->GetCallArg(index));
951 }
952 JSHandle<JSArray> arrHandle = JSArray::CreateArrayFromList(thread, taggedArray);
953
954 // step 8 ~ 9 Call(trap, handler, «target, argArray, newTarget »).
955 JSHandle<JSTaggedValue> newTarget(info->GetNewTarget());
956 const int32_t argsLength = 3; // 3: «target, argArray, newTarget »
957 JSHandle<JSTaggedValue> undefined = globalConst->GetHandledUndefined();
958 EcmaRuntimeCallInfo *runtimeInfo =
959 EcmaInterpreter::NewRuntimeCallInfo(thread, method, handler, undefined, argsLength);
960 RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
961 runtimeInfo->SetCallArg(target.GetTaggedValue(), arrHandle.GetTaggedValue(), newTarget.GetTaggedValue());
962 JSTaggedValue newObj = JSFunction::Call(runtimeInfo);
963
964 // 10.ReturnIfAbrupt(newObj).
965 RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
966 // 11.If Type(newObj) is not Object, throw a TypeError exception.
967 if (!newObj.IsECMAObject()) {
968 THROW_TYPE_ERROR_AND_RETURN(thread, "new object is not object", JSTaggedValue::Exception());
969 }
970 // 12.Return newObj.
971 return newObj;
972 }
973
IsArray(JSThread * thread) const974 bool JSProxy::IsArray(JSThread *thread) const
975 {
976 if (GetHandler().IsNull()) {
977 THROW_TYPE_ERROR_AND_RETURN(thread, "", false);
978 }
979 return GetTarget().IsArray(thread);
980 }
981
GetSourceTarget(JSThread * thread) const982 JSHandle<JSTaggedValue> JSProxy::GetSourceTarget(JSThread *thread) const
983 {
984 JSMutableHandle<JSProxy> proxy(thread, JSTaggedValue(this));
985 JSMutableHandle<JSTaggedValue> target(thread, proxy->GetTarget());
986 while (target->IsJSProxy()) {
987 proxy.Update(target.GetTaggedValue());
988 target.Update(proxy->GetTarget());
989 }
990 return target;
991 }
992 } // namespace panda::ecmascript
993