forked from ClearFoundry/ClearScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathV8WeakContextBinding.h
More file actions
74 lines (60 loc) · 2.11 KB
/
V8WeakContextBinding.h
File metadata and controls
74 lines (60 loc) · 2.11 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
#pragma once
//-----------------------------------------------------------------------------
// V8WeakContextBinding
//-----------------------------------------------------------------------------
class V8WeakContextBinding final: public SharedPtrTarget
{
public:
V8WeakContextBinding(const SharedPtr<V8IsolateImpl>& spIsolateImpl, V8ContextImpl* pContextImpl):
m_wrIsolate(spIsolateImpl->CreateWeakRef()),
m_IsolateName(spIsolateImpl->GetName()),
m_wrContext(pContextImpl->CreateWeakRef()),
m_ContextName(pContextImpl->GetName())
{
}
SharedPtr<V8IsolateImpl> GetIsolateImpl() const
{
SharedPtr<V8IsolateImpl> spIsolateImpl;
if (TryGetIsolateImpl(spIsolateImpl))
{
return spIsolateImpl;
}
throw V8Exception(V8Exception::Type::General, m_IsolateName, StdString(SL("The V8 runtime has been destroyed")), false /*executionStarted*/);
}
bool TryGetIsolateImpl(SharedPtr<V8IsolateImpl>& spIsolateImpl) const
{
auto spIsolate = m_wrIsolate.GetTarget();
if (!spIsolate.IsEmpty())
{
spIsolateImpl = spIsolate.CastTo<V8IsolateImpl>();
return true;
}
return false;
}
SharedPtr<V8ContextImpl> GetContextImpl() const
{
SharedPtr<V8ContextImpl> spContextImpl;
if (TryGetContextImpl(spContextImpl))
{
return spContextImpl;
}
throw V8Exception(V8Exception::Type::General, m_ContextName, StdString(SL("The V8 script engine has been destroyed")), false /*executionStarted*/);
}
bool TryGetContextImpl(SharedPtr<V8ContextImpl>& spContextImpl) const
{
auto spContext = m_wrContext.GetTarget();
if (!spContext.IsEmpty())
{
spContextImpl = spContext.CastTo<V8ContextImpl>();
return true;
}
return false;
}
private:
WeakRef<V8Isolate> m_wrIsolate;
StdString m_IsolateName;
WeakRef<V8Context> m_wrContext;
StdString m_ContextName;
};