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_arguments.h"
17
18 #include "ecmascript/ecma_vm.h"
19 #include "ecmascript/global_env.h"
20 #include "ecmascript/object_factory.h"
21 #include "ecmascript/tagged_array-inl.h"
22
23 namespace panda::ecmascript {
GetOwnProperty(JSThread * thread,const JSHandle<JSArguments> & args,const JSHandle<JSTaggedValue> & key,PropertyDescriptor & desc)24 bool JSArguments::GetOwnProperty(JSThread *thread, const JSHandle<JSArguments> &args,
25 const JSHandle<JSTaggedValue> &key, PropertyDescriptor &desc)
26 {
27 // 1 ~ 3 Let desc be OrdinaryGetOwnProperty(args, P).
28 JSObject::OrdinaryGetOwnProperty(thread, JSHandle<JSObject>(args), key, desc);
29 if (desc.IsEmpty()) {
30 return true;
31 }
32
33 // 8.If IsDataDescriptor(desc) is true and P is "caller" and desc.[[Value]] is a strict mode Function object,
34 // throw a TypeError exception.
35 JSHandle<EcmaString> caller = thread->GetEcmaVM()->GetFactory()->NewFromASCII("caller");
36 if (desc.IsDataDescriptor() && JSTaggedValue::SameValue(key.GetTaggedValue(), caller.GetTaggedValue()) &&
37 desc.GetValue()->IsJSFunction()) {
38 THROW_TYPE_ERROR_AND_RETURN(thread, "Arguments GetOwnProperty: type error", false);
39 }
40 // 9.Return desc.
41 return true;
42 }
43
DefineOwnProperty(JSThread * thread,const JSHandle<JSArguments> & args,const JSHandle<JSTaggedValue> & key,const PropertyDescriptor & desc)44 bool JSArguments::DefineOwnProperty(JSThread *thread, const JSHandle<JSArguments> &args,
45 const JSHandle<JSTaggedValue> &key, const PropertyDescriptor &desc)
46 {
47 // 4.Let allowed be OrdinaryDefineOwnProperty(args, P, Desc).
48 bool allowed = JSObject::OrdinaryDefineOwnProperty(thread, JSHandle<JSObject>(args), key, desc);
49
50 // 5.ReturnIfAbrupt(allowed).
51 RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, allowed);
52
53 // 8.Return true.
54 return true;
55 }
56
GetProperty(JSThread * thread,const JSHandle<JSArguments> & args,const JSHandle<JSTaggedValue> & key,const JSHandle<JSTaggedValue> & receiver)57 OperationResult JSArguments::GetProperty(JSThread *thread, const JSHandle<JSArguments> &args,
58 const JSHandle<JSTaggedValue> &key, const JSHandle<JSTaggedValue> &receiver)
59 {
60 // 5.If the value of isMapped is false, then
61 // a.Return the result of calling the default ordinary object [[Get]] internal method (9.1.8)
62 // on args passing P and Receiver as the arguments.
63 return JSTaggedValue::GetProperty(thread, JSHandle<JSTaggedValue>::Cast(args), key, receiver);
64 }
65
SetProperty(JSThread * thread,const JSHandle<JSArguments> & args,const JSHandle<JSTaggedValue> & key,const JSHandle<JSTaggedValue> & value,const JSHandle<JSTaggedValue> & receiver)66 bool JSArguments::SetProperty(JSThread *thread, const JSHandle<JSArguments> &args, const JSHandle<JSTaggedValue> &key,
67 const JSHandle<JSTaggedValue> &value, const JSHandle<JSTaggedValue> &receiver)
68 {
69 // 5.Return the result of calling the default ordinary object [[Set]] internal method (9.1.9)
70 // on args passing P, V and Receiver as the arguments.
71 return JSTaggedValue::SetProperty(thread, JSHandle<JSTaggedValue>::Cast(args), key, value, receiver);
72 }
73
DeleteProperty(JSThread * thread,const JSHandle<JSArguments> & args,const JSHandle<JSTaggedValue> & key)74 bool JSArguments::DeleteProperty(JSThread *thread, const JSHandle<JSArguments> &args,
75 const JSHandle<JSTaggedValue> &key)
76 {
77 // 4.Let result be the result of calling the default [[Delete]] internal method for ordinary objects (9.1.10)
78 // on the arguments object passing P as the argument.
79 bool result = JSTaggedValue::DeleteProperty(thread, JSHandle<JSTaggedValue>(args), key);
80
81 // 5.ReturnIfAbrupt(result).
82 RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, result);
83
84 // 7.Return result.
85 return result;
86 }
87 } // namespace panda::ecmascript
88