-
Notifications
You must be signed in to change notification settings - Fork 34
/
V8FastFunction.h
56 lines (43 loc) · 1.65 KB
/
V8FastFunction.h
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
#pragma once
#ifdef ALT_CLIENT
#include "v8.h"
#include "v8-fast-api-calls.h"
#include <unordered_map>
#include <functional>
class V8FastFunction
{
std::unordered_map<v8::Isolate*, v8::Persistent<v8::FunctionTemplate, v8::CopyablePersistentTraits<v8::FunctionTemplate>>> tplMap;
v8::FunctionCallback slowCallback;
v8::CFunction fastCallback;
static auto& All()
{
static std::unordered_map<std::string, V8FastFunction*> _All;
return _All;
}
static V8FastFunction* Get(const std::string& name, const std::string& className);
static inline std::string GetIdentifier(const std::string& name, const std::string& className)
{
return name + ":" + className;
}
V8FastFunction() = default;
public:
V8FastFunction(const V8FastFunction&) = delete;
V8FastFunction(V8FastFunction&&) = delete;
V8FastFunction& operator=(V8FastFunction&) = delete;
v8::Local<v8::FunctionTemplate> GetTemplate(v8::Isolate* isolate);
template<typename Func>
static V8FastFunction* GetOrCreate(const std::string& name, const std::string& className, v8::FunctionCallback slowFunc, Func&& fastFunc)
{
// First check if we have this fast function already cached
V8FastFunction* cached = Get(name, className);
if(cached != nullptr) return cached;
// Not cached, create a new instance
V8FastFunction* f = new V8FastFunction();
f->slowCallback = slowFunc;
f->fastCallback = v8::CFunction::Make(fastFunc);
All().insert({ GetIdentifier(name, className), f });
return f;
}
static void UnloadAll(v8::Isolate* isolate);
};
#endif