-
-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathFunctionReference.cpp
More file actions
58 lines (44 loc) · 2.19 KB
/
FunctionReference.cpp
File metadata and controls
58 lines (44 loc) · 2.19 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
#include "FunctionReference.h"
#include "Caches.h"
#include "ObjectManager.h"
#include "Helpers.h"
#include "Constants.h"
using namespace v8;
namespace tns {
void FunctionReference::Register(Local<Context> context, Local<Object> interop) {
Isolate* isolate = context->GetIsolate();
Local<v8::Function> ctorFunc = FunctionReference::GetFunctionReferenceCtorFunc(context);
bool success = interop->Set(context, tns::ToV8String(isolate, "FunctionReference"), ctorFunc).FromMaybe(false);
tns::Assert(success, isolate);
}
Local<v8::Function> FunctionReference::GetFunctionReferenceCtorFunc(Local<Context> context) {
Isolate* isolate = context->GetIsolate();
auto cache = Caches::Get(isolate);
Persistent<v8::Function>* poFunctionReferenceCtor = cache->FunctionReferenceCtorFunc.get();
if (poFunctionReferenceCtor != nullptr) {
return poFunctionReferenceCtor->Get(isolate);
}
Local<FunctionTemplate> ctorFuncTemplate = FunctionTemplate::New(isolate, FunctionReferenceConstructorCallback);
ctorFuncTemplate->InstanceTemplate()->SetInternalFieldCount(1);
ctorFuncTemplate->SetClassName(tns::ToV8String(isolate, "FunctionReference"));
Local<v8::Function> ctorFunc;
if (!ctorFuncTemplate->GetFunction(context).ToLocal(&ctorFunc)) {
tns::Assert(false, isolate);
}
tns::SetValue(isolate, ctorFunc, new FunctionReferenceTypeWrapper());
cache->FunctionReferenceCtorFunc = std::make_unique<Persistent<v8::Function>>(isolate, ctorFunc);
cache->FunctionReferenceCtorFunc->SetWrapperClassId(Constants::ClassTypes::DataWrapper);
return ctorFunc;
}
void FunctionReference::FunctionReferenceConstructorCallback(const FunctionCallbackInfo<Value>& info) {
Isolate* isolate = info.GetIsolate();
Local<Context> context = isolate->GetCurrentContext();
tns::Assert(info.Length() == 1, isolate);
tns::Assert(info[0]->IsFunction(), isolate);
Local<v8::Function> arg = info[0].As<v8::Function>();
std::shared_ptr<Persistent<v8::Value>> poArg = ObjectManager::Register(context, arg);
FunctionReferenceWrapper* wrapper = new FunctionReferenceWrapper(poArg);
tns::SetValue(isolate, arg, wrapper);
info.GetReturnValue().Set(arg);
}
}