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