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