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 "jsvm_env.h"
17 #include "jsvm_reference-inl.h"
18
19 #include "libplatform/libplatform.h"
20
RunAndClearInterrupts()21 void JSVM_Env__::RunAndClearInterrupts()
22 {
23 while (messageQueue.size() > 0) {
24 std::vector<Callback> messageQueueTmp {};
25 {
26 const std::lock_guard<std::mutex> lock(messageQueueMutex);
27 messageQueueTmp.swap(messageQueue);
28 }
29 jsvm::DebugSealHandleScope sealHandleScope(isolate);
30
31 for (auto& cb : messageQueueTmp) {
32 cb(this);
33 }
34 }
35 }
36
JSVM_Env__(v8::Isolate * isolate,int32_t apiVersion)37 JSVM_Env__::JSVM_Env__(v8::Isolate* isolate, int32_t apiVersion) : isolate(isolate), apiVersion(apiVersion)
38 {
39 inspectorAgent = jsvm::InspectorAgent::New(this);
40 ClearLastError(this);
41 }
42
DeleteMe()43 void JSVM_Env__::DeleteMe()
44 {
45 v8impl::RefTracker::FinalizeAll(&finalizerList);
46 v8impl::RefTracker::FinalizeAll(&userReferenceList);
47
48 {
49 v8::Context::Scope context_scope(context());
50 if (inspectorAgent->IsActive()) {
51 inspectorAgent->WaitForDisconnect();
52 }
53 delete inspectorAgent;
54 inspectorAgent = nullptr;
55 }
56
57 // release lock
58 if (locker) {
59 delete locker;
60 locker = nullptr;
61 }
62
63 if (scopeTracker) {
64 delete scopeTracker;
65 scopeTracker = nullptr;
66 }
67
68 delete this;
69 }
70
71 #ifndef ENABLE_INSPECTOR
72 #include "jsvm_log.h"
73 namespace {
74 /*
75 * If inspector is not enabled, using fake jsvm inspect agent.
76 * All Interface in fake agent log error.
77 */
78 class FakeAgent final : public jsvm::InspectorAgent {
79 public:
FakeAgent(JSVM_Env env)80 explicit FakeAgent(JSVM_Env env)
81 {
82 LogError();
83 }
84 ~FakeAgent() override = default;
85
86 public:
Start(const std::string & path,const std::string & hostName,int port,int pid=-1)87 bool Start(const std::string& path, const std::string& hostName, int port, int pid = -1) override
88 {
89 LogError();
90 return false;
91 }
92
Start(const std::string & path,int pid)93 bool Start(const std::string& path, int pid) override
94 {
95 LogError();
96 return false;
97 }
98
Stop()99 void Stop() override
100 {
101 LogError();
102 }
103
IsActive()104 bool IsActive() override
105 {
106 LogError();
107 return false;
108 }
109
WaitForConnect()110 void WaitForConnect() override
111 {
112 LogError();
113 }
114
WaitForDisconnect()115 void WaitForDisconnect() override
116 {
117 LogError();
118 }
119
PauseOnNextJavascriptStatement(const std::string & reason)120 void PauseOnNextJavascriptStatement(const std::string& reason) override
121 {
122 LogError();
123 }
124
125 private:
LogError()126 void LogError()
127 {
128 LOG(Error) << "JSVM Inspector is not enabled";
129 }
130 };
131
132 } // namespace
133
New(JSVM_Env env)134 jsvm::InspectorAgent* jsvm::InspectorAgent::New(JSVM_Env env)
135 {
136 return new FakeAgent(env);
137 }
138 #endif