forked from chakra-core/ChakraCore
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJsrtHelper.cpp
More file actions
182 lines (151 loc) · 5.34 KB
/
JsrtHelper.cpp
File metadata and controls
182 lines (151 loc) · 5.34 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
//-------------------------------------------------------------------------------------------------------
// 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"
#if !defined(_WIN32) && !defined(__ANDROID__)
#include <pthread.h>
#endif
#include "jsrtHelper.h"
#include "Base/ThreadContextTlsEntry.h"
#ifdef DYNAMIC_PROFILE_STORAGE
#include "Language/DynamicProfileStorage.h"
#endif
#if !defined(_WIN32) || defined(CHAKRA_STATIC_LIBRARY)
#include "Core/ConfigParser.h"
#include "Base/ThreadBoundThreadContextManager.h"
#ifdef CHAKRA_STATIC_LIBRARY
bool ConfigParserAPI::FillConsoleTitle(__ecount(cchBufferSize) LPWSTR buffer, size_t cchBufferSize, __in LPWSTR moduleName)
{
return false;
}
void ConfigParserAPI::DisplayInitialOutput(__in LPWSTR moduleName)
{
}
LPCWSTR JsUtil::ExternalApi::GetFeatureKeyName()
{
return _u("");
}
#endif // CHAKRA_STATIC_LIBRARY
#endif
JsrtCallbackState::JsrtCallbackState(ThreadContext* currentThreadContext)
{
if (currentThreadContext == nullptr)
{
originalThreadContext = ThreadContext::GetContextForCurrentThread();
}
else
{
originalThreadContext = currentThreadContext;
}
originalJsrtContext = JsrtContext::GetCurrent();
Assert(originalJsrtContext == nullptr || originalThreadContext == originalJsrtContext->GetScriptContext()->GetThreadContext());
}
JsrtCallbackState::~JsrtCallbackState()
{
if (originalJsrtContext != nullptr)
{
if (originalJsrtContext != JsrtContext::GetCurrent())
{
// This shouldn't fail as the context was previously set on the current thread.
bool isSet = JsrtContext::TrySetCurrent(originalJsrtContext);
if (!isSet)
{
Js::Throw::FatalInternalError();
}
}
}
else
{
bool isSet = ThreadContextTLSEntry::TrySetThreadContext(originalThreadContext);
if (!isSet)
{
Js::Throw::FatalInternalError();
}
}
}
void JsrtCallbackState::ObjectBeforeCallectCallbackWrapper(JsObjectBeforeCollectCallback callback, void* object, void* callbackState, void* threadContext)
{
JsrtCallbackState scope(reinterpret_cast<ThreadContext*>(threadContext));
callback(object, callbackState);
}
#if !defined(_WIN32) || defined(CHAKRA_STATIC_LIBRARY)
void ChakraBinaryAutoSystemInfoInit(AutoSystemInfo * autoSystemInfo)
{
autoSystemInfo->buildDateHash = JsUtil::CharacterBuffer<char>::StaticGetHashCode(__DATE__, _countof(__DATE__));
autoSystemInfo->buildTimeHash = JsUtil::CharacterBuffer<char>::StaticGetHashCode(__TIME__, _countof(__TIME__));
}
#ifndef _WIN32
static pthread_key_t s_threadLocalDummy;
#endif
static THREAD_LOCAL bool s_threadWasEntered = false;
_NOINLINE void DISPOSE_CHAKRA_CORE_THREAD(void *_)
{
free(_);
ThreadBoundThreadContextManager::DestroyContextAndEntryForCurrentThread();
}
_NOINLINE bool InitializeProcess()
{
#if !defined(_WIN32)
pthread_key_create(&s_threadLocalDummy, DISPOSE_CHAKRA_CORE_THREAD);
#endif
// setup the cleanup
// we do not track the main thread. When it exits do the cleanup below
#ifdef CHAKRA_STATIC_LIBRARY
atexit([]() {
ThreadBoundThreadContextManager::DestroyContextAndEntryForCurrentThread();
JsrtRuntime::Uninitialize();
// thread-bound entrypoint should be able to get cleanup correctly, however tlsentry
// for current thread might be left behind if this thread was initialized.
ThreadContextTLSEntry::CleanupThread();
ThreadContextTLSEntry::CleanupProcess();
});
#endif
#ifndef _WIN32
PAL_InitializeChakraCore(0, NULL);
#endif
HMODULE mod = GetModuleHandleW(NULL);
AutoSystemInfo::SaveModuleFileName(mod);
#if defined(_M_IX86) && !defined(__clang__)
// Enable SSE2 math functions in CRT if SSE2 is available
#pragma prefast(suppress:6031, "We don't require SSE2, but will use it if available")
_set_SSE2_enable(TRUE);
#endif
{
CmdLineArgsParser parser;
ConfigParser::ParseOnModuleLoad(parser, mod);
}
#ifdef ENABLE_JS_ETW
EtwTrace::Register();
#endif
ValueType::Initialize();
ThreadContext::GlobalInitialize();
#ifdef ENABLE_BASIC_TELEMETRY
g_TraceLoggingClient = NoCheckHeapNewStruct(TraceLoggingClient);
#endif
#ifdef DYNAMIC_PROFILE_STORAGE
DynamicProfileStorage::Initialize();
#endif
return true;
}
_NOINLINE void VALIDATE_ENTER_CURRENT_THREAD()
{
// We do also initialize the process part here
// This is thread safe by the standard
// Let's hope compiler doesn't fail
static bool _has_init = InitializeProcess();
if (!_has_init) // do not assert this.
{
abort();
}
if (s_threadWasEntered) return;
s_threadWasEntered = true;
#ifdef HEAP_TRACK_ALLOC
HeapAllocator::InitializeThread();
#endif
#ifndef _WIN32
// put something into key to make sure destructor is going to be called
pthread_setspecific(s_threadLocalDummy, malloc(1));
#endif
}
#endif