Repository navigation
Expand file tree
/
Copy pathMetadataBuilder.h
More file actions
237 lines (221 loc) · 11.2 KB
/
Copy pathMetadataBuilder.h
File metadata and controls
237 lines (221 loc) · 11.2 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#ifndef MetadataBuilder_h
#define MetadataBuilder_h
#include <objc/runtime.h>
#include "ClassBuilder.h"
#include "Common.h"
#include "DataWrapper.h"
#include "Metadata.h"
#include "libffi.h"
#include "robin_hood.h"
namespace tns {
struct NativeSetter;
struct NativeSetters;
class MetadataBuilder {
public:
static void RegisterConstantsOnGlobalObject(
v8::Isolate* isolate, v8::Local<v8::ObjectTemplate> globalTemplate,
bool isWorkerThread);
static v8::Local<v8::FunctionTemplate> GetOrCreateConstructorFunctionTemplate(
v8::Local<v8::Context> context, const BaseClassMeta* meta,
KnownUnknownClassPair pair,
const std::vector<std::string>& additionalProtocols =
std::vector<std::string>());
static v8::Local<v8::Function> GetOrCreateStructCtorFunction(
v8::Local<v8::Context> context, const StructInfo& structInfo);
// See Caches::StructInstanceFactory.
static v8::Local<v8::Object> NewStructInstance(v8::Local<v8::Context> context,
const StructInfo& structInfo,
void* data,
StructStorage storage,
v8::Local<v8::Object> parent);
// Canary mode: the setter of `owner`'s native property `property`, when
// `owner` is a native class's prototype still holding the accessor pair it
// was built with for `property`, which has a setter; null otherwise. The
// caller establishes that `owner` is where a receiver's prototype chain
// finds `property`. The result stays valid for the isolate's lifetime.
static NativeSetter* FindNativeSetter(v8::Isolate* isolate,
v8::Local<v8::Context> context,
v8::Local<v8::Object> owner,
v8::Local<v8::Name> property);
// Runs `setter` on `receiver`, as the property's accessor would, and
// returns true; false, having run nothing, once JS has assigned or defined
// the property on its prototype since FindNativeSetter.
static bool RunNativeSetter(v8::Isolate* isolate, NativeSetter* setter,
v8::Local<v8::Object> receiver,
v8::Local<v8::Value> value);
// Where the native member guard is on (GuardsNativeMembers): turns each
// of `names`, and Symbol.iterator with `iterator`, an own method of
// `prototype`, a prototype level of a JS-extended class, that native code
// calls through the function the class captured, into an accessor pair
// whose getter returns that same function, so reads and `super` loads
// resolve as before. Assigning the name on a native object throws.
static void GuardNativeCalledMethods(v8::Local<v8::Context> context,
v8::Local<v8::Object> prototype,
const std::vector<std::string>& names,
bool iterator);
// The method behind an accessor pair GuardNativeCalledMethods installed,
// given its getter; empty for any other value.
static v8::Local<v8::Value> GuardedNativeCalledMethod(
v8::Local<v8::Context> context, v8::Local<v8::Value> getter);
private:
// The binding of an object whose static type adds protocols to `meta`: a
// template inheriting from the class's own binding with the protocols'
// extra members, cached per class and protocol set. Never published as the
// class's constructor.
static v8::Local<v8::FunctionTemplate> GetOrCreateProtocolBindingTemplate(
v8::Local<v8::Context> context, const BaseClassMeta* meta,
KnownUnknownClassPair pair,
const std::vector<std::string>& additionalProtocols);
static v8::Local<v8::FunctionTemplate>
GetOrCreateConstructorFunctionTemplateInternal(
v8::Local<v8::Context> context, const BaseClassMeta* meta,
KnownUnknownClassPair pair,
robin_hood::unordered_map<std::string, uint8_t>& instanceMembers,
robin_hood::unordered_map<std::string, uint8_t>& staticMembers,
const std::vector<std::string>& additionalProtocols =
std::vector<std::string>());
static v8::Intercepted GlobalPropertyGetter(
v8::Local<v8::Name> property,
const v8::PropertyCallbackInfo<v8::Value>& info);
static void ClassConstructorCallback(
const v8::FunctionCallbackInfo<v8::Value>& info);
static void AllocCallback(const v8::FunctionCallbackInfo<v8::Value>& info);
static void MethodCallback(const v8::FunctionCallbackInfo<v8::Value>& info);
static void InstanceMethodCallback(
const v8::FunctionCallbackInfo<v8::Value>& info);
static void CFunctionCallback(
const v8::FunctionCallbackInfo<v8::Value>& info);
static void PropertyGetterCallback(
const v8::FunctionCallbackInfo<v8::Value>& info);
static void PropertySetterCallback(
const v8::FunctionCallbackInfo<v8::Value>& info);
static void PropertyNameGetterCallback(
const v8::FunctionCallbackInfo<v8::Value>& info);
static void PropertyNameSetterCallback(
const v8::FunctionCallbackInfo<v8::Value>& info);
static void StructConstructorCallback(
const v8::FunctionCallbackInfo<v8::Value>& info);
static void StructEqualsCallback(
const v8::FunctionCallbackInfo<v8::Value>& info);
static void StructFieldGetterCallback(
v8::Local<v8::Name> property,
const v8::PropertyCallbackInfo<v8::Value>& info);
static void StructFieldSetterCallback(
v8::Local<v8::Name> property, v8::Local<v8::Value> value,
const v8::PropertyCallbackInfo<v8::Boolean>& info);
static StructWrapper* AttachStructStorage(v8::Isolate* isolate,
v8::Local<v8::Object> instance,
const StructInfo& structInfo,
void* data, StructStorage storage);
static v8::Local<v8::FunctionTemplate> CreateStructCtorTemplate(
v8::Isolate* isolate, StructTypeWrapper* typeWrapper);
static v8::Local<v8::FunctionTemplate> GetOrCreateStructCtorTemplate(
v8::Local<v8::Context> context, const StructInfo& structInfo);
static void ToStringFunctionCallback(
const v8::FunctionCallbackInfo<v8::Value>& info);
static std::pair<ffi_type*, void*> GetStructData(
v8::Local<v8::Context> context, v8::Local<v8::Object> initializer,
const StructInfo& structInfo);
static v8::Local<v8::Value> InvokeMethod(v8::Local<v8::Context> context,
const MethodMeta* meta,
v8::Local<v8::Object> receiver,
V8Args& args, Class klass,
bool isMethodCallback);
static void RegisterAllocMethod(
v8::Isolate* isolate, v8::Local<v8::FunctionTemplate> ctorFuncTemplate,
const InterfaceMeta* interfaceMeta);
// `guarded`, when given, records the JS name of each method installed.
static void RegisterInstanceMethods(
v8::Local<v8::Context> context,
v8::Local<v8::FunctionTemplate> ctorFuncTemplate,
const BaseClassMeta* meta, KnownUnknownClassPair pair,
robin_hood::unordered_map<std::string, uint8_t>& names,
std::vector<std::string>* guarded = nullptr);
// `setters`, when given, records each accessor pair installed.
static void RegisterInstanceProperties(
v8::Local<v8::Context> context,
v8::Local<v8::FunctionTemplate> ctorFuncTemplate,
const BaseClassMeta* meta, std::string className,
KnownUnknownClassPair pair,
robin_hood::unordered_map<std::string, uint8_t>& names,
NativeSetters* setters = nullptr);
static void RegisterInstanceProtocols(
v8::Local<v8::Context> context,
v8::Local<v8::FunctionTemplate> ctorFuncTemplate,
const BaseClassMeta* meta, std::string className,
KnownUnknownClassPair pair,
robin_hood::unordered_map<std::string, uint8_t>& names,
NativeSetters* setters = nullptr,
std::vector<std::string>* guarded = nullptr);
static void RegisterAdditionalProtocols(
v8::Local<v8::Context> context,
v8::Local<v8::FunctionTemplate> ctorFuncTemplate,
KnownUnknownClassPair pair,
const std::vector<std::string>& additionalProtocols,
robin_hood::unordered_map<std::string, uint8_t>& names,
std::vector<std::string>* guarded = nullptr);
static void RegisterStaticMethods(
v8::Local<v8::Context> context, v8::Local<v8::Function> ctorFunc,
const BaseClassMeta* meta, KnownUnknownClassPair pair,
robin_hood::unordered_map<std::string, uint8_t>& names);
static void RegisterStaticProperties(
v8::Local<v8::Context> context, v8::Local<v8::Function> ctorFunc,
const BaseClassMeta* meta, const std::string className,
KnownUnknownClassPair pair,
robin_hood::unordered_map<std::string, uint8_t>& names);
static void RegisterStaticProtocols(
v8::Local<v8::Context> context, v8::Local<v8::Function> ctorFunc,
const BaseClassMeta* meta, const std::string className,
KnownUnknownClassPair pair,
robin_hood::unordered_map<std::string, uint8_t>& names);
static void DefineFunctionLengthProperty(
v8::Local<v8::Context> context,
const TypeEncodingsList<ArrayCount>* encodings,
v8::Local<v8::Function> func);
static v8::Intercepted SwizzledInstanceMethodCallback(
v8::Local<v8::Name> property, v8::Local<v8::Value> value,
const v8::PropertyCallbackInfo<v8::Boolean>& info);
static v8::Intercepted SwizzledPropertyCallback(
v8::Local<v8::Name> property, const v8::PropertyDescriptor& desc,
const v8::PropertyCallbackInfo<v8::Boolean>& info);
struct GlobalHandlerContext {
GlobalHandlerContext(bool isWorkerThread)
: isWorkerThread_(isWorkerThread) {}
bool isWorkerThread_;
// Name currently being defined as a real own property on the global from
// inside GlobalPropertyGetter. CreateDataProperty performs a lookup that
// re-enters the (kNonMasking) interceptor before the property exists, so
// the getter must decline for this name to avoid infinite recursion.
std::string definingProperty_;
};
template <class T>
struct CacheItem {
CacheItem(const T* meta, const std::string className,
void* userData = nullptr)
: meta_(meta), className_(className), userData_(userData) {
static_assert(std::is_base_of<Meta, T>::value,
"Derived not derived from Meta");
}
const T* meta_;
const std::string className_;
void* userData_;
// Metadata can describe classes that are not loaded yet, so a nil
// lookup is retried on the next call instead of being cached.
Class ResolveClass() {
if (this->klass_ == nil && !this->className_.empty()) {
this->klass_ = objc_getClass(this->className_.c_str());
}
return this->klass_;
}
private:
Class klass_ = nil;
};
friend struct NativeSetter;
friend struct NativeSetters;
static void SetNativeProperty(v8::Isolate* isolate,
CacheItem<PropertyMeta>* item,
v8::Local<v8::Object> receiver,
v8::Local<v8::Value> value);
};
} // namespace tns
#endif /* MetadataBuilder_h */