@@ -9,225 +9,29 @@ using namespace v8;
99using namespace tns ;
1010using namespace std ;
1111
12- WeakRef::WeakRef ()
13- : m_objectManager(nullptr ), m_poClearFunc(nullptr ), m_poGetterFunc(nullptr ) {
12+ WeakRef::WeakRef () {
1413}
1514
16- void WeakRef::Init (v8::Isolate* isolate, Local<ObjectTemplate>& globalObjectTemplate, ObjectManager* objectManager) {
17- m_objectManager = objectManager;
18- auto extData = External::New (isolate, this );
19- globalObjectTemplate->Set (ArgConverter::ConvertToV8String (isolate, " WeakRef" ), FunctionTemplate::New (isolate, ConstructorCallback, extData));
20- }
21-
22- void WeakRef::Init (v8::Isolate* isolate, Local<v8::Context> context, Local<Object> globalObject, ObjectManager* objectManager) {
23- m_objectManager = objectManager;
24- auto extData = External::New (isolate, this );
25- auto propName = ArgConverter::ConvertToV8String (isolate, " WeakRef" );
26- auto propValue = FunctionTemplate::New (isolate, ConstructorCallback, extData)->GetFunction (context).ToLocalChecked ();
27- globalObject->Set (context, propName, propValue);
28- }
29-
30- void WeakRef::ConstructorCallback (const FunctionCallbackInfo<Value>& args) {
31- try {
32- auto extData = args.Data ().As <External>();
33- auto thiz = reinterpret_cast <WeakRef*>(extData->Value ());
34- thiz->ConstructorCallbackImpl (args);
35- } catch (NativeScriptException& e) {
36- e.ReThrowToV8 ();
37- } catch (std::exception e) {
38- stringstream ss;
39- ss << " Error: c++ exception: " << e.what () << endl;
40- NativeScriptException nsEx (ss.str ());
41- nsEx.ReThrowToV8 ();
42- } catch (...) {
43- NativeScriptException nsEx (std::string (" Error: c++ exception!" ));
44- nsEx.ReThrowToV8 ();
45- }
46- }
47-
48- void WeakRef::ConstructorCallbackImpl (const FunctionCallbackInfo<Value>& args) {
49- auto isolate = args.GetIsolate ();
50-
51- if (args.IsConstructCall ()) {
52- if (args.Length () == 1 ) {
53- auto target = args[0 ];
54-
55- if (target->IsObject ()) {
56- auto targetObj = target.As <Object>();
57-
58- auto weakRef = m_objectManager->GetEmptyObject (isolate);
59-
60- auto poTarget = new Persistent<Object>(isolate, targetObj);
61- auto poHolder = new Persistent<Object>(isolate, weakRef);
62- auto callbackState = new CallbackState (poTarget, poHolder);
63-
64- poTarget->SetWeak (callbackState, WeakTargetCallback, WeakCallbackType::kFinalizer );
65- poHolder->SetWeak (callbackState, WeakHolderCallback, WeakCallbackType::kFinalizer );
66-
67- auto context = isolate->GetCurrentContext ();
68- weakRef->Set (context, ArgConverter::ConvertToV8String (isolate, " get" ), GetGetterFunction (isolate));
69- weakRef->Set (context, ArgConverter::ConvertToV8String (isolate, " clear" ), GetClearFunction (isolate));
70- V8SetPrivateValue (isolate, weakRef, V8StringConstants::GetTarget (isolate), External::New (isolate, poTarget));
71-
72- args.GetReturnValue ().Set (weakRef);
73- } else {
74- throw NativeScriptException (string (" The WeakRef constructor expects an object argument." ));
75- }
76- } else {
77- throw NativeScriptException (string (" The WeakRef constructor expects single parameter." ));
78- }
79- } else {
80- throw NativeScriptException (string (" WeakRef must be used as a construct call." ));
81- }
82- }
83-
84- void WeakRef::WeakTargetCallback (const WeakCallbackInfo<CallbackState>& data) {
85- auto callbackState = data.GetParameter ();
86- auto poTarget = callbackState->target ;
87- poTarget->Reset ();
88- delete poTarget;
89- callbackState->target = nullptr ;
90-
91- auto isolate = data.GetIsolate ();
92- auto poHolder = callbackState->holder ;
93- if (poHolder != nullptr ) {
94- auto holder = Local<Object>::New (isolate, *poHolder);
95- V8SetPrivateValue (isolate, holder, V8StringConstants::GetTarget (isolate), External::New (isolate, nullptr ));
96- }
97-
98- if (callbackState->holder == nullptr ) {
99- delete callbackState;
100- }
101- }
102-
103- void WeakRef::WeakHolderCallback (const WeakCallbackInfo<CallbackState>& data) {
104- try {
105- auto callbackState = data.GetParameter ();
106- auto poHolder = callbackState->holder ;
107- auto isolate = data.GetIsolate ();
108- auto holder = Local<Object>::New (isolate, *poHolder);
109-
110- Local<Value> hiddenVal;
111- V8GetPrivateValue (isolate, holder, V8StringConstants::GetTarget (isolate), hiddenVal);
112- auto poTarget = reinterpret_cast <Persistent<Object>*>(hiddenVal.As <External>()->Value ());
113-
114- if (poTarget != nullptr ) {
115- poHolder->SetWeak (callbackState, WeakHolderCallback, WeakCallbackType::kFinalizer );
116- } else {
117- poHolder->Reset ();
118- delete poHolder;
119- callbackState->holder = nullptr ;
120- if (callbackState->target == nullptr ) {
121- delete callbackState;
15+ void WeakRef::Init (v8::Isolate* isolate, Local<v8::Context> context) {
16+ std::string source = R"(
17+ // WeakRef polyfills
18+ global.WeakRef.prototype.get = global.WeakRef.prototype.deref;
19+ global.WeakRef.prototype.__hasWarnedAboutClear = false;
20+ global.WeakRef.prototype.clear = () => {
21+ if(global.WeakRef.prototype.__hasWarnedAboutClear) {
22+ return;
12223 }
24+ global.WeakRef.prototype.__hasWarnedAboutClear = true;
25+ console.warn('WeakRef.clear() is non-standard and has been deprecated. It does nothing and the call can be safely removed.');
12326 }
124- } catch (NativeScriptException& e) {
125- e.ReThrowToV8 ();
126- } catch (std::exception e) {
127- stringstream ss;
128- ss << " Error: c++ exception: " << e.what () << endl;
129- NativeScriptException nsEx (ss.str ());
130- nsEx.ReThrowToV8 ();
131- } catch (...) {
132- NativeScriptException nsEx (std::string (" Error: c++ exception!" ));
133- nsEx.ReThrowToV8 ();
134- }
135- }
27+ )" ;
13628
137- void WeakRef::ClearCallback (const FunctionCallbackInfo<Value>& args) {
138- try {
139- auto holder = args.This ();
140- auto isolate = args.GetIsolate ();
141-
142- V8SetPrivateValue (isolate, holder, V8StringConstants::GetTarget (isolate), External::New (isolate, nullptr ));
143- } catch (NativeScriptException& e) {
144- e.ReThrowToV8 ();
145- } catch (std::exception e) {
146- stringstream ss;
147- ss << " Error: c++ exception: " << e.what () << endl;
148- NativeScriptException nsEx (ss.str ());
149- nsEx.ReThrowToV8 ();
150- } catch (...) {
151- NativeScriptException nsEx (std::string (" Error: c++ exception!" ));
152- nsEx.ReThrowToV8 ();
153- }
154- }
29+ Local<Script> script;
30+ bool success = Script::Compile (context, ArgConverter::ConvertToV8String (isolate, source)).ToLocal (&script);
31+ assert (success && !script.IsEmpty ());
15532
156- void WeakRef::GettertCallback (const FunctionCallbackInfo<Value>& args) {
157- try {
158- auto holder = args.This ();
159- Local<Value> hiddenVal;
160- auto isolate = args.GetIsolate ();
161- V8GetPrivateValue (isolate, holder, V8StringConstants::GetTarget (isolate), hiddenVal);
162- auto poTarget = reinterpret_cast <Persistent<Object>*>(hiddenVal.As <External>()->Value ());
33+ Local<Value> result;
34+ success = script->Run (context).ToLocal (&result);
35+ assert (success);
16336
164- if (poTarget != nullptr ) {
165- auto target = Local<Object>::New (isolate, *poTarget);
166- args.GetReturnValue ().Set (target);
167- } else {
168- args.GetReturnValue ().SetNull ();
169- }
170- } catch (NativeScriptException& e) {
171- e.ReThrowToV8 ();
172- } catch (std::exception e) {
173- stringstream ss;
174- ss << " Error: c++ exception: " << e.what () << endl;
175- NativeScriptException nsEx (ss.str ());
176- nsEx.ReThrowToV8 ();
177- } catch (...) {
178- NativeScriptException nsEx (std::string (" Error: c++ exception!" ));
179- nsEx.ReThrowToV8 ();
180- }
181- }
182-
183- Local<Function> WeakRef::GetGetterFunction (Isolate* isolate) {
184- try {
185- if (m_poGetterFunc != nullptr ) {
186- return Local<Function>::New (isolate, *m_poGetterFunc);
187- } else {
188- auto extData = External::New (isolate, this );
189- Local<Context> context = isolate->GetCurrentContext ();
190- auto getterFunc = FunctionTemplate::New (isolate, GettertCallback, extData)->GetFunction (context).ToLocalChecked ();
191- m_poGetterFunc = new Persistent<Function>(isolate, getterFunc);
192- return getterFunc;
193- }
194- } catch (NativeScriptException& e) {
195- e.ReThrowToV8 ();
196- } catch (std::exception e) {
197- stringstream ss;
198- ss << " Error: c++ exception: " << e.what () << endl;
199- NativeScriptException nsEx (ss.str ());
200- nsEx.ReThrowToV8 ();
201- } catch (...) {
202- NativeScriptException nsEx (std::string (" Error: c++ exception!" ));
203- nsEx.ReThrowToV8 ();
204- }
205- // this is only to avoid warnings, we should never come here
206- return Local<Function>();
207- }
208-
209- Local<Function> WeakRef::GetClearFunction (Isolate* isolate) {
210- try {
211- if (m_poClearFunc != nullptr ) {
212- return Local<Function>::New (isolate, *m_poClearFunc);
213- } else {
214- auto extData = External::New (isolate, this );
215- Local<Context> context = isolate->GetCurrentContext ();
216- auto clearFunc = FunctionTemplate::New (isolate, ClearCallback, extData)->GetFunction (context).ToLocalChecked ();
217- m_poClearFunc = new Persistent<Function>(isolate, clearFunc);
218- return clearFunc;
219- }
220- } catch (NativeScriptException& e) {
221- e.ReThrowToV8 ();
222- } catch (std::exception e) {
223- stringstream ss;
224- ss << " Error: c++ exception: " << e.what () << endl;
225- NativeScriptException nsEx (ss.str ());
226- nsEx.ReThrowToV8 ();
227- } catch (...) {
228- NativeScriptException nsEx (std::string (" Error: c++ exception!" ));
229- nsEx.ReThrowToV8 ();
230- }
231- // this is only to avoid warnings, we should never come here
232- return Local<Function>();
233- }
37+ }
0 commit comments