• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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 #ifndef ECMASCRIPT_TOOLING_BACKEND_DEBUGGER_API_H
17 #define ECMASCRIPT_TOOLING_BACKEND_DEBUGGER_API_H
18 
19 #include <functional>
20 
21 #include "ecmascript/common.h"
22 #include "ecmascript/debugger/js_debugger_interface.h"
23 #include "ecmascript/debugger/js_pt_method.h"
24 #include "ecmascript/jspandafile/scope_info_extractor.h"
25 #include "ecmascript/napi/include/jsnapi.h"
26 #include "ecmascript/lexical_env.h"
27 
28 namespace panda {
29 namespace ecmascript {
30 class EcmaVM;
31 class FrameHandler;
32 class JSThread;
33 class Method;
34 class ModuleManager;
35 class NameDictionary;
36 class SourceTextModule;
37 class DebugInfoExtractor;
38 namespace tooling {
39 class JSDebugger;
40 }
41 }  // ecmascript
42 }  // panda
43 
44 namespace panda::ecmascript::tooling {
45 enum StackState {
46     CONTINUE,
47     FAILED,
48     SUCCESS,
49 };
50 
51 class PUBLIC_API DebuggerApi {
52 public:
53     // FrameHandler
54     static uint32_t GetStackDepth(const EcmaVM *ecmaVm);
55     static std::shared_ptr<FrameHandler> NewFrameHandler(const EcmaVM *ecmaVm);
56     static bool StackWalker(const EcmaVM *ecmaVm, std::function<StackState(const FrameHandler *)> func);
57 
58     static uint32_t GetBytecodeOffset(const EcmaVM *ecmaVm);
59     static uint32_t GetBytecodeOffset(const FrameHandler *frameHandler);
60     static std::unique_ptr<PtMethod> GetMethod(const EcmaVM *ecmaVm);
61     static Method *GetMethod(const FrameHandler *frameHandler);
62     static bool IsNativeMethod(const EcmaVM *ecmaVm);
63     static bool IsNativeMethod(const FrameHandler *frameHandler);
64     static JSPandaFile *GetJSPandaFile(const EcmaVM *ecmaVm);
65 
66     static JSTaggedValue GetEnv(const FrameHandler *frameHandler);
67     static JSTaggedType *GetSp(const FrameHandler *frameHandler);
68     static int32_t GetVregIndex(const FrameHandler *frameHandler, std::string_view name);
69     static Local<JSValueRef> GetVRegValue(const EcmaVM *ecmaVm,
70                                           const FrameHandler *frameHandler, size_t index);
71     static void SetVRegValue(FrameHandler *frameHandler, size_t index, Local<JSValueRef> value);
72 
73     static Local<JSValueRef> GetProperties(const EcmaVM *ecmaVm, const FrameHandler *frameHandler,
74                                            int32_t level, uint32_t slot);
75     static void SetProperties(const EcmaVM *vm, const FrameHandler *frameHandler, int32_t level,
76                               uint32_t slot, Local<JSValueRef> value);
77     static std::pair<int32_t, uint32_t> GetLevelSlot(const FrameHandler *frameHandler, std::string_view name);
78     static Local<JSValueRef> GetGlobalValue(const EcmaVM *vm, Local<StringRef> name);
79     static bool SetGlobalValue(const EcmaVM *vm, Local<StringRef> name, Local<JSValueRef> value);
80 
81     // JSThread
82     static Local<JSValueRef> GetAndClearException(const EcmaVM *ecmaVm);
83         static JSTaggedValue GetCurrentModule(const EcmaVM *ecmaVm);
84     static JSHandle<JSTaggedValue> GetImportModule(const EcmaVM *ecmaVm, const JSHandle<JSTaggedValue> &currentModule,
85                                                    std::string &name);
86     static int32_t GetModuleVariableIndex(const EcmaVM *ecmaVm, const JSHandle<JSTaggedValue> &currentModule,
87                                           std::string &name);
88     static int32_t GetRequestModuleIndex(const EcmaVM *ecmaVm, const JSTaggedValue moduleRequest,
89                                          const JSHandle<JSTaggedValue> &currentModule);
90     static Local<JSValueRef> GetModuleValue(const EcmaVM *ecmaVm, const JSHandle<JSTaggedValue> &currentModule,
91                                             std::string &name);
92     static bool SetModuleValue(const EcmaVM *ecmaVm, const JSHandle<JSTaggedValue> &currentModule,
93                                std::string &name, Local<JSValueRef> value);
94     static void InitializeExportVariables(const EcmaVM *ecmaVm, Local<ObjectRef> &moduleObj,
95                                           const JSHandle<JSTaggedValue> &currentModule);
96     static void GetLocalExportVariables(const EcmaVM *ecmaVm, Local<ObjectRef> &moduleObj,
97                                         const JSHandle<JSTaggedValue> &currentModule, bool isImportStar);
98     static void GetIndirectExportVariables(const EcmaVM *ecmaVm, Local<ObjectRef> &moduleObj,
99                                            const JSHandle<JSTaggedValue> &currentModule);
100     static void GetImportVariables(const EcmaVM *ecmaVm, Local<ObjectRef> &moduleObj,
101                                    const JSHandle<JSTaggedValue> &currentModule);
102     static void SetException(const EcmaVM *ecmaVm, Local<JSValueRef> exception);
103     static void ClearException(const EcmaVM *ecmaVm);
104     static bool IsExceptionCaught(const EcmaVM *ecmaVm);
105 
106     // NumberHelper
107     static double StringToDouble(const uint8_t *start, const uint8_t *end, uint8_t radix);
108 
109     // JSDebugger
110     static JSDebugger *CreateJSDebugger(const EcmaVM *ecmaVm);
111     static void DestroyJSDebugger(JSDebugger *debugger);
112     static void RegisterHooks(JSDebugger *debugger, PtHooks *hooks);
113     static bool SetBreakpoint(JSDebugger *debugger, const JSPtLocation &location,
114         Local<FunctionRef> condFuncRef);
115     static bool RemoveBreakpoint(JSDebugger *debugger, const JSPtLocation &location);
116     static void RemoveAllBreakpoints(JSDebugger *debugger);
117     static void HandleUncaughtException(const EcmaVM *ecmaVm, std::string &message);
118     static Local<JSValueRef> EvaluateViaFuncCall(EcmaVM *ecmaVm, Local<FunctionRef> funcRef,
119         std::shared_ptr<FrameHandler> &frameHandler);
120     static Local<FunctionRef> GenerateFuncFromBuffer(const EcmaVM *ecmaVm, const void *buffer, size_t size,
121         std::string_view entryPoint);
122 
123     // HotReload
124     static DebugInfoExtractor *GetPatchExtractor(const EcmaVM *ecmaVm, const std::string &url);
125     static const JSPandaFile *GetBaseJSPandaFile(const EcmaVM *ecmaVm, const JSPandaFile *jsPandaFile);
126 };
127 }  // namespace panda::ecmascript::tooling
128 
129 #endif  // ECMASCRIPT_TOOLING_BACKEND_DEBUGGER_API_H
130