@@ -20,18 +20,16 @@ JavascriptFunction::JavascriptFunction(v8::Local<v8::Object> iFunction, Javascri
20
20
throw gcnew System::ArgumentException (" Must provide a JavascriptContext" );
21
21
22
22
mFuncHandle = new Persistent<Function>(context->GetCurrentIsolate (), Local<Function>::Cast (iFunction));
23
- mContext = context;
24
-
25
- mContext ->RegisterFunction (this );
23
+ mContextHandle = gcnew System::WeakReference (context);
26
24
}
27
25
28
26
JavascriptFunction::~JavascriptFunction ()
29
27
{
30
28
if (mFuncHandle )
31
29
{
32
- if (mContext )
30
+ if (IsAlive () )
33
31
{
34
- JavascriptScope scope (mContext );
32
+ JavascriptScope scope (GetContext () );
35
33
mFuncHandle ->Reset ();
36
34
}
37
35
delete mFuncHandle ;
@@ -46,16 +44,17 @@ JavascriptFunction::!JavascriptFunction()
46
44
47
45
System::Object^ JavascriptFunction::Call(... cli::array<System::Object^>^ args)
48
46
{
49
- if (mFuncHandle == nullptr )
47
+ if (! IsAlive () )
50
48
throw gcnew JavascriptException (L" This function's owning JavascriptContext has been disposed" );
51
49
if (!args)
52
50
throw gcnew System::ArgumentNullException (" args" );
53
51
54
- JavascriptScope scope (mContext );
55
- v8::Isolate* isolate = mContext ->GetCurrentIsolate ();
52
+ auto context = GetContext ();
53
+ JavascriptScope scope (context);
54
+ v8::Isolate* isolate = context->GetCurrentIsolate ();
56
55
HandleScope handleScope (isolate);
57
56
58
- Local<v8::Object> global = mContext ->GetGlobal ();
57
+ Local<v8::Object> global = context ->GetGlobal ();
59
58
60
59
int argc = args->Length ;
61
60
Local<v8::Value> *argv = new Local<v8::Value>[argc];
@@ -81,13 +80,13 @@ bool JavascriptFunction::operator==(JavascriptFunction^ func1, JavascriptFunctio
81
80
return false ;
82
81
if (func1_null && func2_null)
83
82
return true ;
84
- if (func1->mFuncHandle == nullptr )
83
+ if (! func1->IsAlive () )
85
84
throw gcnew JavascriptException (L" 'func1's owning JavascriptContext has been disposed" );
86
- if (func2->mFuncHandle == nullptr )
85
+ if (! func2->IsAlive () )
87
86
throw gcnew JavascriptException (L" 'func2's owning JavascriptContext has been disposed" );
88
87
89
- Local<Function> jsFuncPtr1 = func1->mFuncHandle ->Get (func1->mContext ->GetCurrentIsolate ());
90
- Local<Function> jsFuncPtr2 = func2->mFuncHandle ->Get (func2->mContext ->GetCurrentIsolate ());
88
+ Local<Function> jsFuncPtr1 = func1->mFuncHandle ->Get (func1->GetContext () ->GetCurrentIsolate ());
89
+ Local<Function> jsFuncPtr2 = func2->mFuncHandle ->Get (func2->GetContext () ->GetCurrentIsolate ());
91
90
92
91
return jsFuncPtr1->Equals (JavascriptContext::GetCurrentIsolate ()->GetCurrentContext (), jsFuncPtr2).ToChecked ();
93
92
}
@@ -99,22 +98,23 @@ bool JavascriptFunction::Equals(JavascriptFunction^ other)
99
98
100
99
bool JavascriptFunction::Equals (Object^ other)
101
100
{
102
- if (mFuncHandle == nullptr )
101
+ if (! IsAlive () )
103
102
throw gcnew JavascriptException (L" This function's owning JavascriptContext has been disposed" );
104
103
JavascriptFunction^ otherFunc = dynamic_cast <JavascriptFunction^>(other);
105
- if (otherFunc != nullptr && otherFunc->mFuncHandle == nullptr )
106
- throw gcnew JavascriptException (L" This function's owning JavascriptContext has been disposed" );
104
+ if (otherFunc != nullptr && ! otherFunc->IsAlive () )
105
+ throw gcnew JavascriptException (L" The other function's owning JavascriptContext has been disposed" );
107
106
108
107
return (otherFunc && this ->Equals (otherFunc));
109
108
}
110
109
111
110
System::String^ JavascriptFunction::ToString()
112
111
{
113
- if (mFuncHandle == nullptr )
112
+ if (! IsAlive () )
114
113
throw gcnew JavascriptException (L" This function's owning JavascriptContext has been disposed" );
115
114
116
- JavascriptScope scope (mContext );
117
- auto isolate = mContext ->GetCurrentIsolate ();
115
+ auto context = GetContext ();
116
+ JavascriptScope scope (context);
117
+ auto isolate = context->GetCurrentIsolate ();
118
118
HandleScope handleScope (isolate);
119
119
auto asString = mFuncHandle ->Get (isolate)->ToString (isolate->GetCurrentContext ());
120
120
return safe_cast<System::String^>(JavascriptInterop::ConvertFromV8 (asString.ToLocalChecked ()));
0 commit comments