• 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 //   +--------------------------+
NewRuntimeCallInfo(JSThread * thread,JSHandle<JSTaggedValue> func,JSHandle<JSTaggedValue> thisObj,JSHandle<JSTaggedValue> newTarget,uint32_t numArgs,bool needCheckStack)39 EcmaRuntimeCallInfo* EcmaInterpreter::NewRuntimeCallInfo(
40     JSThread *thread, JSHandle<JSTaggedValue> func, JSHandle<JSTaggedValue> thisObj, JSHandle<JSTaggedValue> 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.GetTaggedType();
53     *(--newSp) = newTarget.GetTaggedType();
54     *(--newSp) = func.GetTaggedType();
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 
ReBuildRuntimeCallInfo(JSThread * thread,EcmaRuntimeCallInfo * info,uint32_t numArgs,bool needCheckStack)69 EcmaRuntimeCallInfo* EcmaInterpreter::ReBuildRuntimeCallInfo(JSThread *thread, EcmaRuntimeCallInfo* info,
70     uint32_t numArgs, bool needCheckStack)
71 {
72     JSTaggedValue func = info->GetFunctionValue();
73     JSTaggedValue newTarget = info->GetNewTargetValue();
74     JSTaggedValue thisObj = info->GetThisValue();
75     JSTaggedType *currentSp = reinterpret_cast<JSTaggedType *>(info);
76 
77     InterpretedEntryFrame *currentEntryState = InterpretedEntryFrame::GetFrameFromSp(currentSp);
78     JSTaggedType *prevSp = currentEntryState->base.prev;
79 
80     uint32_t actualArgc = info->GetArgsNumber();
81     std::vector<JSTaggedType> args(actualArgc);
82     for (uint32_t i = 0; i < actualArgc; i++) {
83         args[i] = info->GetCallArgValue(actualArgc - i - 1).GetRawData();
84     }
85     currentSp += (info->GetArgsNumber() + NUM_MANDATORY_JSFUNC_ARGS + 2); // 2: include thread_ and numArgs_
86     if (needCheckStack && UNLIKELY(thread->DoStackOverflowCheck(currentSp - numArgs - NUM_MANDATORY_JSFUNC_ARGS))) {
87         return nullptr;
88     }
89     ASSERT(numArgs > actualArgc);
90     for (uint32_t i = 0; i < (numArgs - actualArgc); i++) {
91         *(--currentSp) = JSTaggedValue::VALUE_UNDEFINED;
92     }
93     for (uint32_t i = 0; i < actualArgc; i++) {
94         *(--currentSp) = args[i];
95     }
96     *(--currentSp) = thisObj.GetRawData();
97     *(--currentSp) = newTarget.GetRawData();
98     *(--currentSp) = func.GetRawData();
99     *(--currentSp) = numArgs + NUM_MANDATORY_JSFUNC_ARGS;
100     *(--currentSp) = ToUintPtr(thread);
101     EcmaRuntimeCallInfo *ecmaRuntimeCallInfo = reinterpret_cast<EcmaRuntimeCallInfo *>(currentSp);
102 
103     InterpretedEntryFrame *entryState = InterpretedEntryFrame::GetFrameFromSp(currentSp);
104     entryState->base.type = FrameType::INTERPRETER_ENTRY_FRAME;
105     entryState->base.prev = prevSp;
106     entryState->pc = nullptr;
107 
108     thread->SetCurrentSPFrame(currentSp);
109     return ecmaRuntimeCallInfo;
110 }
111 }  // namespace panda::ecmascript
112