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
132 lines (120 loc) · 4.08 KB
/
jsrtRuntime.cpp
File metadata and controls
132 lines (120 loc) · 4.08 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
//-------------------------------------------------------------------------------------------------------
// 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 "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->callbackContext = NULL;
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
this->debugObject = nullptr;
}
JsrtRuntime::~JsrtRuntime()
{
HeapDelete(allocationPolicyManager);
if (this->debugObject)
{
HeapDelete(this->debugObject);
}
this->debugObject = nullptr;
}
// 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();
currentRuntime->CloseContexts();
RentalThreadContextManager::DestroyThreadContext(tmpThreadContext);
HeapDelete(currentRuntime);
}
}
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 != NULL)
{
if (this->collectCallback == NULL)
{
this->collectCallback = this->threadContext->AddRecyclerCollectCallBack(RecyclerCollectCallbackStatic, this);
}
this->beforeCollectCallback = beforeCollectCallback;
this->callbackContext = callbackContext;
}
else
{
if (this->collectCallback != NULL)
{
this->threadContext->RemoveRecyclerCollectCallBack(this->collectCallback);
this->collectCallback = NULL;
}
this->beforeCollectCallback = NULL;
this->callbackContext = NULL;
}
}
void JsrtRuntime::RecyclerCollectCallbackStatic(void * context, RecyclerCollectCallBackFlags flags)
{
if (flags & Collect_Begin)
{
JsrtRuntime * _this = reinterpret_cast<JsrtRuntime *>(context);
try
{
_this->beforeCollectCallback(_this->callbackContext);
}
catch (...)
{
AssertMsg(false, "Unexpected non-engine exception.");
}
}
}
unsigned int JsrtRuntime::Idle()
{
return this->threadService.Idle();
}
void JsrtRuntime::EnsureDebugObject()
{
if (this->debugObject == nullptr)
{
this->debugObject = HeapNew(JsrtDebug, this->threadContext);
}
Assert(this->debugObject != nullptr);
}
JsrtDebug * JsrtRuntime::GetDebugObject()
{
return this->debugObject;
}