9
9
10
10
void KeyDBExecuteCallback (const v8::FunctionCallbackInfo<v8::Value>& args);
11
11
12
- thread_local v8::Isolate *isolate = nullptr ;
13
- thread_local v8::Persistent<v8::ObjectTemplate, v8::CopyablePersistentTraits<v8::ObjectTemplate>> tls_global;
14
- thread_local v8::Persistent<v8::Context, v8::CopyablePersistentTraits<v8::Context>> tls_context;
15
-
16
12
void javascript_initialize ()
17
13
{
18
14
v8::V8::InitializeICUDefaultLocation (" keydb-server" );
@@ -31,30 +27,34 @@ void javascript_shutdown()
31
27
class HotScript
32
28
{
33
29
v8::Persistent<v8::Script> m_script;
30
+ size_t m_cch;
34
31
BYTE m_hash[SHA256_BLOCK_SIZE];
35
32
36
33
public:
37
- HotScript (const char *rgch, size_t cch, v8::Local<v8::Script> script)
38
- : m_script(isolate, script)
34
+ HotScript (v8::Isolate *isolate, const char *rgch, size_t cch, v8::Local<v8::Script> script)
35
+ : m_script(isolate, script), m_cch(cch)
39
36
{
40
37
SHA256_CTX ctx;
41
38
sha256_init (&ctx);
42
39
sha256_update (&ctx, (BYTE*)rgch, cch);
43
40
sha256_final (&ctx, m_hash);
44
41
}
45
42
46
- bool FGetScript (const char *rgch, size_t cch, v8::Local<v8::Script> *pscriptOut)
43
+ bool FGetScript (v8::Isolate *isolate, const char *rgch, size_t cch, v8::Local<v8::Script> *pscriptOut)
47
44
{
45
+ if (cch != m_cch)
46
+ return false ;
47
+
48
48
BYTE hashT[SHA256_BLOCK_SIZE];
49
49
SHA256_CTX ctx;
50
50
sha256_init (&ctx);
51
51
sha256_update (&ctx, (BYTE*)rgch, cch);
52
52
sha256_final (&ctx, hashT);
53
53
54
- return FGetScript (hashT, pscriptOut);
54
+ return FGetScript (isolate, hashT, pscriptOut);
55
55
}
56
56
57
- bool FGetScript (const BYTE *hash, v8::Local<v8::Script> *pscriptOut)
57
+ bool FGetScript (v8::Isolate *isolate, const BYTE *hash, v8::Local<v8::Script> *pscriptOut)
58
58
{
59
59
if (memcmp (m_hash, hash, SHA256_BLOCK_SIZE))
60
60
return false ;
@@ -93,6 +93,9 @@ class StackPopper
93
93
}
94
94
};
95
95
96
+ JSContext::JSContext ()
97
+ {}
98
+
96
99
std::experimental::filesystem::path find_module (std::experimental::filesystem::path name)
97
100
{
98
101
std::experimental::filesystem::path path;
@@ -119,11 +122,15 @@ std::experimental::filesystem::path find_module(std::experimental::filesystem::p
119
122
} while (!wdir.empty ());
120
123
}
121
124
122
- static void RequireCallback (const v8::FunctionCallbackInfo<v8::Value>& args)
125
+ /* static*/ void JSContext:: RequireCallback (const v8::FunctionCallbackInfo<v8::Value>& args)
123
126
{
124
127
static thread_local std::stack<std::experimental::filesystem::path> stackpath;
128
+ v8::Isolate *isolate = args.GetIsolate ();
125
129
126
130
if (args.Length () != 1 ) return ;
131
+
132
+ JSContext *jscontext = (JSContext*)isolate->GetData (0 );
133
+
127
134
v8::HandleScope scope (isolate);
128
135
v8::Local<v8::Value> arg = args[0 ];
129
136
v8::String::Utf8Value utf8Path (isolate, arg);
@@ -171,7 +178,7 @@ static void RequireCallback(const v8::FunctionCallbackInfo<v8::Value>& args)
171
178
return ; // Failed to read file
172
179
}
173
180
174
- v8::Local<v8::ObjectTemplate> global = v8::Local<v8::ObjectTemplate>::New (isolate, tls_global );
181
+ v8::Local<v8::ObjectTemplate> global = v8::Local<v8::ObjectTemplate>::New (isolate, jscontext-> m_global );
175
182
v8::Local<v8::Context> context = v8::Context::New (isolate, nullptr , global);
176
183
177
184
v8::ScriptOrigin origin (arg, // specifier
@@ -226,7 +233,7 @@ static void RequireCallback(const v8::FunctionCallbackInfo<v8::Value>& args)
226
233
}
227
234
228
235
229
- void javascript_hooks_initialize (v8::Local<v8::ObjectTemplate> &keydb_obj)
236
+ void JSContext:: javascript_hooks_initialize (v8::Local<v8::ObjectTemplate> &keydb_obj)
230
237
{
231
238
keydb_obj->Set (v8::String::NewFromUtf8 (isolate, " log" , v8::NewStringType::kNormal )
232
239
.ToLocalChecked (),
@@ -237,7 +244,7 @@ void javascript_hooks_initialize(v8::Local<v8::ObjectTemplate> &keydb_obj)
237
244
v8::FunctionTemplate::New (isolate, KeyDBExecuteCallback));
238
245
}
239
246
240
- void javascript_thread_initialize ()
247
+ void JSContext::initialize ()
241
248
{
242
249
v8::Isolate::CreateParams create_params;
243
250
create_params.array_buffer_allocator = v8::ArrayBuffer::Allocator::NewDefaultAllocator ();
@@ -270,11 +277,13 @@ void javascript_thread_initialize()
270
277
.ToLocalChecked (),
271
278
module );
272
279
273
- tls_global = v8::Persistent<v8::ObjectTemplate, v8::CopyablePersistentTraits<v8::ObjectTemplate>>(isolate, global);
274
- tls_context = v8::Persistent<v8::Context, v8::CopyablePersistentTraits<v8::Context>>(isolate, v8::Context::New (isolate, nullptr , global));
280
+
281
+ isolate->SetData (0 , this );
282
+ m_global = v8::Persistent<v8::ObjectTemplate, v8::CopyablePersistentTraits<v8::ObjectTemplate>>(isolate, global);
283
+ m_context = v8::Persistent<v8::Context, v8::CopyablePersistentTraits<v8::Context>>(isolate, v8::Context::New (isolate, nullptr , global));
275
284
}
276
285
277
- std::string prettyPrintException (v8::TryCatch &trycatch)
286
+ std::string JSContext:: prettyPrintException (v8::TryCatch &trycatch)
278
287
{
279
288
auto e = trycatch.Exception ();
280
289
v8::Local<v8::Context> context = v8::Local<v8::Context>::New (isolate, isolate->GetCurrentContext ());
@@ -292,7 +301,7 @@ std::string prettyPrintException(v8::TryCatch &trycatch)
292
301
return str;
293
302
}
294
303
295
- v8::Local<v8::Value> javascript_run (v8::Local<v8::Context> &context, v8::Local<v8::Script> &script)
304
+ v8::Local<v8::Value> JSContext::run (v8::Local<v8::Context> &context, v8::Local<v8::Script> &script)
296
305
{
297
306
// Run the script to get the result.
298
307
v8::Context::Scope context_scope (context);
@@ -314,16 +323,16 @@ v8::Local<v8::Value> javascript_run(v8::Local<v8::Context> &context, v8::Local<v
314
323
return result;
315
324
}
316
325
317
- v8::Local<v8::Value> javascript_run (v8::Local<v8::Context> &context, const char *rgch, size_t cch)
326
+ v8::Local<v8::Value> JSContext::run ( const char *rgch, size_t cch)
318
327
{
319
- static thread_local HotScript *hotscript = nullptr ;
320
328
v8::TryCatch trycatch (isolate);
321
329
322
330
// Enter the context for compiling and running the hello world script.
331
+ v8::Local<v8::Context> context = v8::Local<v8::Context>::New (isolate, m_context);
323
332
v8::Context::Scope context_scope (context);
324
333
325
334
v8::Local<v8::Script> script;
326
- if (hotscript == nullptr || !hotscript ->FGetScript (rgch, cch, &script))
335
+ if (m_sphotscript == nullptr || !m_sphotscript ->FGetScript (isolate, rgch, cch, &script))
327
336
{
328
337
// Create a string containing the JavaScript source code.
329
338
v8::Local<v8::String> sourceText =
@@ -345,13 +354,12 @@ v8::Local<v8::Value> javascript_run(v8::Local<v8::Context> &context, const char
345
354
}
346
355
throw std::nullptr_t ();
347
356
}
348
- delete hotscript;
349
- hotscript = new HotScript (rgch, cch, script);
357
+ m_sphotscript = std::make_unique<HotScript>(isolate, rgch, cch, script);
350
358
}
351
- return javascript_run (context, script);
359
+ return run (context, script);
352
360
}
353
361
354
- void javascript_thread_shutdown ()
362
+ JSContext::~JSContext ()
355
363
{
356
364
isolate->Dispose ();
357
365
}
0 commit comments