• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/interpreter/interpreter-inl.h"
17 
18 #include "ecmascript/frames.h"
19 #include "ecmascript/interpreter/frame_handler.h"
20 namespace panda::ecmascript {
21 // make EcmaRuntimeCallInfo in stack pointer as fallows:
22 //   +----------------------+   —
23 //   |        args...       |   ^
24 //   |----------------------|   |
25 //   |        numArgs       |   |
26 //   |----------------------|   |
27 //   |        this          |   |
28 //   |----------------------| EcmaRuntimeCallInfo
29 //   |       newTarget      |   |
30 //   |----------------------|   |
31 //   |        func          |   v
32 //   +----------------------+   —
33 //   |      base.type       |   ^
34 //   |----------------------|   |
35 //   |      base.prev       | InterpretedEntryFrame
36 //   |----------------------|   |
37 //   |          pc          |   v
38 //   +--------------------------+
NewRuntimeCallInfoBase(JSThread * thread,JSTaggedType func,JSTaggedType thisObj,JSTaggedType newTarget,uint32_t numArgs,bool needCheckStack)39 EcmaRuntimeCallInfo* EcmaInterpreter::NewRuntimeCallInfoBase(
40     JSThread *thread, JSTaggedType func, JSTaggedType thisObj, JSTaggedType newTarget,
41     uint32_t numArgs, bool needCheckStack)
42 {
43     JSTaggedType *prevSp = const_cast<JSTaggedType *>(thread->GetCurrentSPFrame());
44     JSTaggedType *newSp = GetInterpreterFrameEnd(thread, prevSp);
45     if (needCheckStack && UNLIKELY(thread->DoStackOverflowCheck(newSp - numArgs - NUM_MANDATORY_JSFUNC_ARGS))) {
46         return nullptr;
47     }
48 
49     for (uint32_t i = 0; i < numArgs; i++) {
50         *(--newSp) = JSTaggedValue::VALUE_UNDEFINED;
51     }
52     *(--newSp) = thisObj;
53     *(--newSp) = newTarget;
54     *(--newSp) = func;
55     *(--newSp) = numArgs + NUM_MANDATORY_JSFUNC_ARGS;
56     *(--newSp) = ToUintPtr(thread);
57     EcmaRuntimeCallInfo *ecmaRuntimeCallInfo = reinterpret_cast<EcmaRuntimeCallInfo *>(newSp);
58 
59     // create entry frame.
60     InterpretedEntryFrame *entryState = InterpretedEntryFrame::GetFrameFromSp(newSp);
61     entryState->base.type = FrameType::INTERPRETER_ENTRY_FRAME;
62     entryState->base.prev = prevSp;
63     entryState->pc = nullptr;
64 
65     thread->SetCurrentSPFrame(newSp);
66     return ecmaRuntimeCallInfo;
67 }
68 
NewRuntimeCallInfo(JSThread * thread,JSTaggedValue func,JSTaggedValue thisObj,JSTaggedValue newTarget,uint32_t numArgs,bool needCheckStack)69 EcmaRuntimeCallInfo* EcmaInterpreter::NewRuntimeCallInfo(
70     JSThread *thread, JSTaggedValue func, JSTaggedValue thisObj, JSTaggedValue newTarget,
71     uint32_t numArgs, bool needCheckStack)
72 {
73     return NewRuntimeCallInfoBase(thread, func.GetRawData(), thisObj.GetRawData(), newTarget.GetRawData(),
74         numArgs, needCheckStack);
75 }
76 
NewRuntimeCallInfo(JSThread * thread,JSHandle<JSTaggedValue> func,JSHandle<JSTaggedValue> thisObj,JSHandle<JSTaggedValue> newTarget,uint32_t numArgs,bool needCheckStack)77 EcmaRuntimeCallInfo* EcmaInterpreter::NewRuntimeCallInfo(
78     JSThread *thread, JSHandle<JSTaggedValue> func, JSHandle<JSTaggedValue> thisObj,
79     JSHandle<JSTaggedValue> newTarget, uint32_t numArgs, bool needCheckStack)
80 {
81     return NewRuntimeCallInfoBase(thread, func.GetTaggedType(), thisObj.GetTaggedType(), newTarget.GetTaggedType(),
82         numArgs, needCheckStack);
83 }
84 
ReBuildRuntimeCallInfo(JSThread * thread,EcmaRuntimeCallInfo * info,uint32_t numArgs,bool needCheckStack)85 EcmaRuntimeCallInfo* EcmaInterpreter::ReBuildRuntimeCallInfo(JSThread *thread, EcmaRuntimeCallInfo* info,
86     uint32_t numArgs, bool needCheckStack)
87 {
88     JSTaggedValue func = info->GetFunctionValue();
89     JSTaggedValue newTarget = info->GetNewTargetValue();
90     JSTaggedValue thisObj = info->GetThisValue();
91     JSTaggedType *currentSp = reinterpret_cast<JSTaggedType *>(info);
92 
93     InterpretedEntryFrame *currentEntryState = InterpretedEntryFrame::GetFrameFromSp(currentSp);
94     JSTaggedType *prevSp = currentEntryState->base.prev;
95 
96     uint32_t actualArgc = info->GetArgsNumber();
97     std::vector<JSTaggedType> args(actualArgc);
98     for (uint32_t i = 0; i < actualArgc; i++) {
99         args[i] = info->GetCallArgValue(actualArgc - i - 1).GetRawData();
100     }
101     currentSp += (info->GetArgsNumber() + NUM_MANDATORY_JSFUNC_ARGS + 2); // 2: include thread_ and numArgs_
102     if (needCheckStack && UNLIKELY(thread->DoStackOverflowCheck(currentSp - numArgs - NUM_MANDATORY_JSFUNC_ARGS))) {
103         return nullptr;
104     }
105     ASSERT(numArgs > actualArgc);
106     for (uint32_t i = 0; i < (numArgs - actualArgc); i++) {
107         *(--currentSp) = JSTaggedValue::VALUE_UNDEFINED;
108     }
109     for (uint32_t i = 0; i < actualArgc; i++) {
110         *(--currentSp) = args[i];
111     }
112     *(--currentSp) = thisObj.GetRawData();
113     *(--currentSp) = newTarget.GetRawData();
114     *(--currentSp) = func.GetRawData();
115     *(--currentSp) = numArgs + NUM_MANDATORY_JSFUNC_ARGS;
116     *(--currentSp) = ToUintPtr(thread);
117     EcmaRuntimeCallInfo *ecmaRuntimeCallInfo = reinterpret_cast<EcmaRuntimeCallInfo *>(currentSp);
118 
119     InterpretedEntryFrame *entryState = InterpretedEntryFrame::GetFrameFromSp(currentSp);
120     entryState->base.type = FrameType::INTERPRETER_ENTRY_FRAME;
121     entryState->base.prev = prevSp;
122     entryState->pc = nullptr;
123 
124     thread->SetCurrentSPFrame(currentSp);
125     return ecmaRuntimeCallInfo;
126 }
127 }  // namespace panda::ecmascript
128