Skip to content

Commit 97d7465

Browse files
committed
feat: remove old WeakRef polyfill
1 parent 54d8fe8 commit 97d7465

File tree

3 files changed

+21
-252
lines changed

3 files changed

+21
-252
lines changed

test-app/runtime/src/main/cpp/Runtime.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -704,8 +704,6 @@ Isolate* Runtime::PrepareV8Runtime(const string& filesPath, const string& native
704704
}
705705
#endif
706706

707-
// m_weakRef.Init(isolate, globalTemplate, m_objectManager);
708-
709707
SimpleProfiler::Init(isolate, globalTemplate);
710708

711709
CallbackHandlers::CreateGlobalCastFunctions(isolate, globalTemplate);
@@ -716,8 +714,6 @@ Isolate* Runtime::PrepareV8Runtime(const string& filesPath, const string& native
716714

717715
auto global = context->Global();
718716

719-
m_weakRef.Init(isolate,context, global, m_objectManager);
720-
721717
context->Enter();
722718

723719
m_objectManager->Init(isolate);
@@ -732,6 +728,7 @@ Isolate* Runtime::PrepareV8Runtime(const string& filesPath, const string& native
732728

733729
global->DefineOwnProperty(context, ArgConverter::ConvertToV8String(isolate, "global"), global, readOnlyFlags);
734730
global->DefineOwnProperty(context, ArgConverter::ConvertToV8String(isolate, "__global"), global, readOnlyFlags);
731+
m_weakRef.Init(isolate, context);
735732

736733
// Do not set 'self' accessor to main thread JavaScript
737734
if (s_mainThreadInitialized) {

test-app/runtime/src/main/cpp/WeakRef.cpp

Lines changed: 19 additions & 215 deletions
Original file line numberDiff line numberDiff line change
@@ -9,225 +9,29 @@ using namespace v8;
99
using namespace tns;
1010
using 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+
}

test-app/runtime/src/main/cpp/WeakRef.h

Lines changed: 1 addition & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -10,41 +10,9 @@ class WeakRef {
1010
public:
1111
WeakRef();
1212

13-
void Init(v8::Isolate* isolate, v8::Local<v8::ObjectTemplate>& globalObjectTemplate, ObjectManager* objectManager);
14-
15-
void Init(v8::Isolate* isolate, v8::Local<v8::Context> context, v8::Local<v8::Object> globalObject, ObjectManager* objectManager);
13+
void Init(v8::Isolate* isolate, v8::Local<v8::Context> context);
1614

1715
private:
18-
struct CallbackState {
19-
CallbackState(v8::Persistent<v8::Object>* _target, v8::Persistent<v8::Object>* _holder)
20-
:
21-
target(_target), holder(_holder) {
22-
}
23-
v8::Persistent<v8::Object>* target;
24-
v8::Persistent<v8::Object>* holder;
25-
};
26-
27-
static void ConstructorCallback(const v8::FunctionCallbackInfo<v8::Value>& args);
28-
29-
void ConstructorCallbackImpl(const v8::FunctionCallbackInfo<v8::Value>& args);
30-
31-
static void ClearCallback(const v8::FunctionCallbackInfo<v8::Value>& args);
32-
33-
static void GettertCallback(const v8::FunctionCallbackInfo<v8::Value>& args);
34-
35-
v8::Local<v8::Function> GetClearFunction(v8::Isolate* isolate);
36-
37-
v8::Local<v8::Function> GetGetterFunction(v8::Isolate* isolate);
38-
39-
static void WeakTargetCallback(const v8::WeakCallbackInfo<CallbackState>& data);
40-
41-
static void WeakHolderCallback(const v8::WeakCallbackInfo<CallbackState>& data);
42-
43-
ObjectManager* m_objectManager;
44-
45-
v8::Persistent<v8::Function>* m_poClearFunc;
46-
47-
v8::Persistent<v8::Function>* m_poGetterFunc;
4816
};
4917
}
5018

0 commit comments

Comments
 (0)