forked from chakra-core/ChakraCore
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJsrtRuntime.cpp
More file actions
238 lines (216 loc) · 7.53 KB
/
JsrtRuntime.cpp
File metadata and controls
238 lines (216 loc) · 7.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
//-------------------------------------------------------------------------------------------------------
// Copyright (C) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
//-------------------------------------------------------------------------------------------------------
#include <JsrtPch.h>
#include "JsrtRuntime.h"
#include "jsrtHelper.h"
#include "Base/ThreadContextTlsEntry.h"
#include "Base/ThreadBoundThreadContextManager.h"
JsrtRuntime::JsrtRuntime(ThreadContext * threadContext, bool useIdle, bool dispatchExceptions)
{
Assert(threadContext != NULL);
this->threadContext = threadContext;
this->contextList = NULL;
this->collectCallback = NULL;
this->beforeCollectCallback = NULL;
this->beforeCollectCallbackContext = NULL;
#ifdef _CHAKRACOREBUILD
this->beforeSweepCallback = NULL;
this->beforeSweepCallbackContext = NULL;
#endif
this->allocationPolicyManager = threadContext->GetAllocationPolicyManager();
this->useIdle = useIdle;
this->dispatchExceptions = dispatchExceptions;
if (useIdle)
{
this->threadService.Initialize(threadContext);
}
threadContext->SetJSRTRuntime(this);
#ifdef ENABLE_DEBUG_CONFIG_OPTIONS
serializeByteCodeForLibrary = false;
#endif
#ifdef ENABLE_SCRIPT_DEBUGGING
this->jsrtDebugManager = nullptr;
#endif
}
JsrtRuntime::~JsrtRuntime()
{
HeapDelete(allocationPolicyManager);
#ifdef ENABLE_SCRIPT_DEBUGGING
if (this->jsrtDebugManager != nullptr)
{
HeapDelete(this->jsrtDebugManager);
this->jsrtDebugManager = nullptr;
}
#endif
}
// This is called at process detach.
// threadcontext created from runtime should not be destroyed in ThreadBoundThreadContext
// we should clean them up at process detach only as runtime can be used in other threads
// even after the current physical thread was destroyed.
// This is called after ThreadBoundThreadContext are cleaned up, so the remaining items
// in the globalthreadContext linklist should be for jsrt only.
void JsrtRuntime::Uninitialize()
{
ThreadContext* currentThreadContext = ThreadContext::GetThreadContextList();
ThreadContext* tmpThreadContext;
while (currentThreadContext)
{
Assert(!currentThreadContext->IsScriptActive());
JsrtRuntime* currentRuntime = static_cast<JsrtRuntime*>(currentThreadContext->GetJSRTRuntime());
tmpThreadContext = currentThreadContext;
currentThreadContext = currentThreadContext->Next();
#ifdef CHAKRA_STATIC_LIBRARY
// xplat-todo: Cleanup staticlib shutdown. This only shuts down threads.
// Other closing contexts / finalizers having trouble with current
// runtime/context.
RentalThreadContextManager::DestroyThreadContext(tmpThreadContext);
#else
currentRuntime->CloseContexts();
RentalThreadContextManager::DestroyThreadContext(tmpThreadContext);
HeapDelete(currentRuntime);
#endif
}
}
void JsrtRuntime::CloseContexts()
{
while (this->contextList != NULL)
{
this->contextList->Dispose(false);
// This will remove it from the list
}
}
void JsrtRuntime::SetBeforeCollectCallback(JsBeforeCollectCallback beforeCollectCallback, void * callbackContext)
{
if (beforeCollectCallback != nullptr)
{
if (this->collectCallback == nullptr)
{
this->collectCallback = this->threadContext->AddRecyclerCollectCallBack(RecyclerCollectCallbackStatic, this);
}
this->beforeCollectCallback = beforeCollectCallback;
this->beforeCollectCallbackContext = callbackContext;
}
else
{
if (this->collectCallback != nullptr
#ifdef _CHAKRACOREBUILD
&& this->beforeSweepCallback == nullptr
#endif
)
{
this->threadContext->RemoveRecyclerCollectCallBack(this->collectCallback);
this->collectCallback = nullptr;
}
this->beforeCollectCallback = nullptr;
this->beforeCollectCallbackContext = nullptr;
}
}
#ifdef _CHAKRACOREBUILD
void JsrtRuntime::SetBeforeSweepCallback(JsBeforeSweepCallback afterCollectCallback, void * callbackContext)
{
if (afterCollectCallback != nullptr)
{
if (this->collectCallback == nullptr)
{
this->collectCallback = this->threadContext->AddRecyclerCollectCallBack(RecyclerCollectCallbackStatic, this);
}
this->beforeSweepCallback = afterCollectCallback;
this->beforeSweepCallbackContext = callbackContext;
}
else
{
if (this->collectCallback != nullptr && this->beforeCollectCallback == nullptr)
{
this->threadContext->RemoveRecyclerCollectCallBack(this->collectCallback);
this->collectCallback = nullptr;
}
this->beforeSweepCallback = nullptr;
this->beforeSweepCallbackContext = nullptr;
}
}
#endif
void JsrtRuntime::RecyclerCollectCallbackStatic(void * context, RecyclerCollectCallBackFlags flags)
{
if (flags & Collect_Begin)
{
JsrtRuntime * _this = reinterpret_cast<JsrtRuntime *>(context);
if (_this->beforeCollectCallback == nullptr)
{
return;
}
try
{
JsrtCallbackState scope(reinterpret_cast<ThreadContext*>(_this->GetThreadContext()));
_this->beforeCollectCallback(_this->beforeCollectCallbackContext);
}
catch (...)
{
AssertMsg(false, "Unexpected non-engine exception.");
}
}
#ifdef _CHAKRACOREBUILD
else if (flags & Collect_Begin_Sweep)
{
JsrtRuntime * _this = reinterpret_cast<JsrtRuntime *>(context);
if (_this->beforeSweepCallback == nullptr)
{
return;
}
try
{
JsrtCallbackState scope(reinterpret_cast<ThreadContext*>(_this->GetThreadContext()));
_this->beforeSweepCallback(_this->beforeSweepCallbackContext);
}
catch (...)
{
AssertMsg(false, "Unexpected non-engine exception.");
}
}
#endif
}
unsigned int JsrtRuntime::Idle()
{
return this->threadService.Idle();
}
#ifdef ENABLE_SCRIPT_DEBUGGING
void JsrtRuntime::EnsureJsrtDebugManager()
{
if (this->jsrtDebugManager == nullptr)
{
this->jsrtDebugManager = HeapNew(JsrtDebugManager, this->threadContext);
}
Assert(this->jsrtDebugManager != nullptr);
}
void JsrtRuntime::DeleteJsrtDebugManager()
{
if (this->jsrtDebugManager != nullptr)
{
HeapDelete(this->jsrtDebugManager);
this->jsrtDebugManager = nullptr;
}
}
JsrtDebugManager * JsrtRuntime::GetJsrtDebugManager()
{
return this->jsrtDebugManager;
}
#if ENABLE_TTD
uint32 JsrtRuntime::BPRegister_TTD(int64 bpID, Js::ScriptContext* scriptContext, Js::Utf8SourceInfo* utf8SourceInfo, uint32 line, uint32 column, BOOL* isNewBP)
{
TTDAssert(this->jsrtDebugManager != nullptr, "This needs to be setup before registering any breakpoints.");
Js::BreakpointProbe* probe = this->jsrtDebugManager->SetBreakpointHelper_TTD(bpID, scriptContext, utf8SourceInfo, line, column, isNewBP);
return probe->GetId();
}
void JsrtRuntime::BPDelete_TTD(uint32 bpID)
{
TTDAssert(this->jsrtDebugManager != nullptr, "This needs to be setup before deleting any breakpoints.");
this->jsrtDebugManager->GetDebugDocumentManager()->RemoveBreakpoint(bpID);
}
void JsrtRuntime::BPClearDocument_TTD()
{
TTDAssert(this->jsrtDebugManager != nullptr, "This needs to be setup before deleting any breakpoints.");
this->jsrtDebugManager->ClearBreakpointDebugDocumentDictionary();
}
#endif
#endif