1 /*
2 * Copyright (c) 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/compiler/builtins/builtins_reflect_stub_builder.h"
17
18 #include "ecmascript/compiler/stub_builder-inl.h"
19
20 namespace panda::ecmascript::kungfu {
Get(Variable * result,Label * exit,Label * slowPath)21 void BuiltinsReflectStubBuilder::Get(Variable *result, Label *exit, Label *slowPath)
22 {
23 auto env = GetEnvironment();
24 GateRef target = GetCallArg0(numArgs_);
25 Label isEcmaObject(env);
26 BRANCH(IsEcmaObject(glue_, target), &isEcmaObject, slowPath);
27 Bind(&isEcmaObject);
28 {
29 GateRef propKey = GetCallArg1(numArgs_);
30 Label twoArgs(env);
31 Label threeArgs(env);
32 #if ENABLE_NEXT_OPTIMIZATION
33 BRANCH(Int64Equal(numArgs_, IntPtr(2)), &twoArgs, &threeArgs); // 2 : two args
34 Bind(&twoArgs);
35 {
36 *result = GetPropertyByValue(glue_, target, propKey);
37 BRANCH(TaggedIsHole(result->ReadVariable()), slowPath, exit);
38 }
39 Bind(&threeArgs);
40 {
41 GateRef receiver = GetCallArg2(numArgs_);
42 *result = GetPropertyByValue(glue_, target, propKey, receiver);
43 BRANCH(TaggedIsHole(result->ReadVariable()), slowPath, exit);
44 }
45 #else
46 BRANCH(Int64Equal(numArgs_, IntPtr(2)), &twoArgs, slowPath); // 2 : two args
47 Bind(&twoArgs);
48 {
49 *result = GetPropertyByValue(glue_, target, propKey);
50 BRANCH(TaggedIsHole(result->ReadVariable()), slowPath, exit);
51 }
52 #endif
53 }
54 }
55
56 #if ENABLE_NEXT_OPTIMIZATION
Has(Variable * result,Label * exit,Label * slowPath)57 void BuiltinsReflectStubBuilder::Has(Variable* result, Label* exit, Label* slowPath)
58 {
59 auto env = GetEnvironment();
60 GateRef obj = GetCallArg0(numArgs_);
61 GateRef key = GetCallArg1(numArgs_);
62 Label isEcmaObject(env);
63 Label isPropertyKey(env);
64 BRANCH(IsEcmaObject(glue_, obj), &isEcmaObject, slowPath);
65 Bind(&isEcmaObject);
66 {
67 GateRef propertyKey = ToPropertyKey(glue_, key);
68 BRANCH_UNLIKELY(HasPendingException(glue_), slowPath, &isPropertyKey);
69 Bind(&isPropertyKey);
70 {
71 *result = HasProperty(glue_, obj, propertyKey);
72 Jump(exit);
73 }
74 }
75 }
76
Set(Variable * result,Label * exit,Label * slowPath)77 void BuiltinsReflectStubBuilder::Set(Variable* result, Label* exit, Label* slowPath)
78 {
79 auto env = GetEnvironment();
80 GateRef obj = GetCallArg0(numArgs_);
81 Label isEcmaObject(env);
82 Label isPropertyKey(env);
83 Label trySuccess(env);
84 BRANCH(IsEcmaObject(glue_, obj), &isEcmaObject, slowPath);
85 Bind(&isEcmaObject);
86 {
87 GateRef key = GetCallArg1(numArgs_);
88 GateRef value = GetCallArg2(numArgs_);
89 Label threeArgs(env);
90 Label checkType(env);
91 Label returnTrue(env);
92 Label returnValue(env);
93 BRANCH(Int64Equal(numArgs_, IntPtr(3)), &threeArgs, slowPath); // 3 : three args
94 Bind(&threeArgs);
95 {
96 BRANCH(IsModuleNamespace(glue_, obj), slowPath, &checkType);
97 Bind(&checkType);
98 GateRef setResult = SetPropertyByValue(glue_, obj, key, value, false, ProfileOperation(), false, false);
99 BRANCH(TaggedIsHole(setResult), slowPath, &trySuccess);
100 Bind(&trySuccess);
101 {
102 BRANCH(TaggedIsUndefined(setResult), &returnTrue, &returnValue);
103 Bind(&returnTrue);
104 {
105 *result = TaggedTrue();
106 Jump(exit);
107 }
108 Bind(&returnValue);
109 *result = setResult;
110 Jump(exit);
111 }
112 }
113 }
114 }
115 #endif
116
117 } // namespace panda::ecmascript::kungfu
118