-
Notifications
You must be signed in to change notification settings - Fork 3k
/
native.h
340 lines (304 loc) · 11.6 KB
/
native.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
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
/*
+----------------------------------------------------------------------+
| HipHop for PHP |
+----------------------------------------------------------------------+
| Copyright (c) 2010-2014 Facebook, Inc. (http://www.facebook.com) |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
*/
#ifndef _incl_HPHP_RUNTIME_VM_NATIVE_H
#define _incl_HPHP_RUNTIME_VM_NATIVE_H
#include "hphp/runtime/base/type-string.h"
#include "hphp/runtime/base/typed-value.h"
#include "hphp/runtime/vm/func.h"
#include <type_traits>
namespace HPHP {
struct ActRec;
struct Class;
class Object;
};
/* Macros related to declaring/registering internal implementations
* of <<__Native>> global functions.
*
* Declare a function in ext_foo.h using:
* ReturnType HHVM_FUNCTION(functionName, parameterList...)
* For example:
* int64_t HHVM_FUNCTION(sum, int64_t a, int64_t b) {
* return a + b;
* }
*
* Then register it from your Extension's moduleLoad() hook:
* virtual moduleLoad(const IniSetting::Map& ini, Hdf config) {
* HHVM_FE(sum);
* }
*
* To finish exposing it to PHP, add an entry to Systemlib
* using matching hack typehints:
* <?php
* <<__Native>>
* function sum(int $a, int $b): int;
*
****************************************************************************
*
* For functions with variadic args or other "special" handling,
* declare the prototype as a BuiltinFuncPtr:
* TypedValue* HHVM_FN(sum)(ActRec* ar) { ... }
* And declare it in Systemlib with the "ActRec" subattribute
* <?php
* <<__Native("ActRec")>>
* function sum(int $a, int $b): int;
* Registering the function in moduleLoad() remains the same.
*
****************************************************************************
*
* If, for whatever reason, the standard declaration doesn't work,
* you may declare the function directly as:
* ReturnType localSymbolName(parameterList...)
* For example:
* int64_t my_sum_function(int64_t a, int64_t b) {
* return a + b;
* }
*
* In which case you will need to use a different macro in moduleLoad()
* virtual moduleLoad(const IniSetting::Map& ini, Hdf config) {
* HHVM_NAME_FE(sum, my_sum_function)
* }
* Or an explicit call to registerBuildinFunction()
* static const StaticString s_sum("sum");
* virtual moduleLoad(const IniSetting::Map& ini, Hdf config) {
* Native::registerBuiltinFunction(s_sum.get(), (void*)my_sum_function);
* }
*/
#define HHVM_FN(fn) f_ ## fn
#define HHVM_FUNCTION(fn, ...) \
HHVM_FN(fn)(__VA_ARGS__)
#define HHVM_NAMED_FE(fn, fimpl) Native::registerBuiltinFunction(\
makeStaticString(#fn), fimpl)
#define HHVM_FE(fn) HHVM_NAMED_FE(fn, HHVM_FN(fn))
#define HHVM_FALIAS(fn, falias) HHVM_NAMED_FE(fn, HHVM_FN(falias))
/* Macros related to declaring/registering internal implementations
* of <<__Native>> class instance methods.
*
* See the definition of function macros above for general explanation.
* These macros only differ in the following ways:
* - They accept a classname in addition to the function name
* - The registered name of the function is "ClassName->FunctionName"
* - Prototypes include a prepended const Object& parameter (named this_)
*/
#define HHVM_MN(cn,fn) c_ ## cn ## _ni_ ## fn
#define HHVM_METHOD(cn, fn, ...) \
HHVM_MN(cn,fn)(const Object& this_, ##__VA_ARGS__)
#define HHVM_NAMED_ME(cn,fn,mimpl) Native::registerBuiltinFunction(\
makeStaticString(#cn "->" #fn), \
mimpl)
#define HHVM_ME(cn,fn) HHVM_NAMED_ME(cn,fn, HHVM_MN(cn,fn))
/* Macros related to declaring/registering internal implementations
* of <<__Native>> class static methods.
*
* See the definition of function macros above for general explanation.
* These macros only differ in the following ways:
* - They accept a classname in addition to the function name
* - The registered name of the function is "ClassName::FunctionName"
* - Prototypes include a prepended const Class* parameter (named self_)
*/
#define HHVM_STATIC_MN(cn,fn) c_ ## cn ## _ns_ ## fn
#define HHVM_STATIC_METHOD(cn, fn, ...) \
HHVM_STATIC_MN(cn,fn)(const Class *self_, ##__VA_ARGS__)
#define HHVM_NAMED_STATIC_ME(cn,fn,mimpl) Native::registerBuiltinFunction(\
makeStaticString(#cn "::" #fn), \
mimpl)
#define HHVM_STATIC_ME(cn,fn) HHVM_NAMED_STATIC_ME(cn,fn,HHVM_STATIC_MN(cn,fn))
namespace HPHP { namespace Native {
//////////////////////////////////////////////////////////////////////////////
// Maximum number of args for a native function call
// Beyond this number, a function/method will have to
// take a raw ActRec (using <<__Native("ActRec")>>) and
// deal with the args using getArg<KindOf*>(ar, argNum)
//
// To paraphrase you-know-who, "32 args should be enough for anybody"
//
// Note: If changing this number, update native-func-caller.h
// using make_native-func-caller.php
const int kMaxBuiltinArgs = 32;
// t#3982283 - Our ARM code gen doesn't support stack args yet.
// In fact, it only supports six of the eight register args.
// Put a hard limit of five to account for the return register for now.
constexpr int kMaxFCallBuiltinArgsARM = 5;
inline int maxFCallBuiltinArgs() {
#ifdef __AARCH64EL__
return kMaxFCallBuiltinArgsARM;
#else
if (UNLIKELY(RuntimeOption::EvalSimulateARM)) {
return kMaxFCallBuiltinArgsARM;
}
return kMaxBuiltinArgs;
#endif
}
// t#3982283 - Our ARM code gen doesn't support FP args/returns yet.
inline bool allowFCallBuiltinDoubles() {
#ifdef __AARCH64EL__
return false;
#else
if (UNLIKELY(RuntimeOption::EvalSimulateARM)) {
return false;
}
return true;
#endif
}
enum Attr {
AttrNone = 0,
AttrActRec = 1 << 0,
AttrZendCompat = 1 << 1,
};
/**
* Prepare function call arguments to match their expected
* types on a native function/method call.
*
* Uses typehints in Func and tvCast*InPlace
*/
bool coerceFCallArgs(TypedValue* args,
int32_t numArgs, int32_t numNonDefault,
const Func* func);
/**
* Dispatches a call to the native function bound to <func>
* If <ctx> is not nullptr, it is prepended to <args> when
* calling.
*/
void callFunc(const Func* func, TypedValue *ctx,
TypedValue* args, int32_t numArgs,
TypedValue &ret);
/**
* ActRec to native function call wrapper
*
* Unpacks args and coerces types according to Func typehints
* Calls C++ function in the form ret f_foo(type arg1, type arg2, ...)
* Marshalls return into TypedValue
*/
TypedValue* functionWrapper(ActRec* ar);
/**
* Method version of nativeFunctionWrapper() above.
*
* Also prepends a calling context:
* const Object& for instance methods
* Class* for static methods
*/
TypedValue* methodWrapper(ActRec* ar);
/**
* Fallback method bound to declared methods with no matching
* internal implementation.
*
* Throws a NotImplementedException
*/
TypedValue* unimplementedWrapper(ActRec* ar);
/**
* Case insensitive map of "name" to function pointer
*
* Extensions should generally add items to this map using
* the HHVM_FE/ME macros above
*/
typedef hphp_hash_map<const StringData*, BuiltinFunction,
string_data_hash, string_data_isame> BuiltinFunctionMap;
extern BuiltinFunctionMap s_builtinFunctions;
template <class Fun>
inline void registerBuiltinFunction(const StringData* fname, Fun func) {
static_assert(
std::is_pointer<Fun>::value &&
std::is_function<typename std::remove_pointer<Fun>::type>::value,
"You can only register pointers to function.");
s_builtinFunctions[fname] = (BuiltinFunction)func;
}
inline BuiltinFunction GetBuiltinFunction(const StringData* fname,
const StringData* cname = nullptr,
bool isStatic = false) {
auto it = s_builtinFunctions.find((cname == nullptr) ? fname :
(String(const_cast<StringData*>(cname)) +
(isStatic ? "::" : "->") +
String(const_cast<StringData*>(fname))).get());
return (it == s_builtinFunctions.end()) ? nullptr : it->second;
}
inline BuiltinFunction GetBuiltinFunction(const char* fname,
const char* cname = nullptr,
bool isStatic = false) {
return GetBuiltinFunction(makeStaticString(fname),
cname ? makeStaticString(cname) : nullptr,
isStatic);
}
//////////////////////////////////////////////////////////////////////////////
// Global constants
typedef std::map<const StringData*,TypedValue> ConstantMap;
extern ConstantMap s_constant_map;
inline
bool registerConstant(const StringData* cnsName, Cell cns) {
assert(cellIsPlausible(cns));
s_constant_map[cnsName] = cns;
return Unit::defCns(cnsName, &cns, true);
}
template<DataType DType>
typename std::enable_if<
!std::is_same<typename DataTypeCPPType<DType>::type,void>::value,
bool>::type
registerConstant(const StringData* cnsName,
typename DataTypeCPPType<DType>::type val) {
return registerConstant(cnsName, make_tv<DType>(val));
}
template<DataType DType>
typename std::enable_if<
std::is_same<typename DataTypeCPPType<DType>::type,void>::value,
bool>::type
registerConstant(const StringData* cnsName) {
return registerConstant(cnsName, make_tv<DType>());
}
inline
const ConstantMap& getConstants() {
return s_constant_map;
}
//////////////////////////////////////////////////////////////////////////////
// Class Constants
typedef hphp_hash_map<const StringData*, ConstantMap,
string_data_hash, string_data_isame> ClassConstantMapMap;
extern ClassConstantMapMap s_class_constant_map;
inline
bool registerClassConstant(const StringData *clsName,
const StringData *cnsName,
Cell cns) {
assert(cellIsPlausible(cns));
auto &cls = s_class_constant_map[clsName];
assert(cls.find(cnsName) == cls.end());
cls[cnsName] = cns;
return true;
}
template<DataType DType>
typename std::enable_if<
!std::is_same<typename DataTypeCPPType<DType>::type,void>::value,
bool>::type
registerClassConstant(const StringData* clsName,
const StringData* cnsName,
typename DataTypeCPPType<DType>::type val) {
return registerClassConstant(clsName, cnsName, make_tv<DType>(val));
}
template<DataType DType>
typename std::enable_if<
std::is_same<typename DataTypeCPPType<DType>::type,void>::value,
bool>::type
registerClassConstant(const StringData* clsName,
const StringData* cnsName) {
return registerClassConstant(clsName, cnsName, make_tv<DType>());
}
inline
const ConstantMap* getClassConstants(const StringData* clsName) {
auto clsit = s_class_constant_map.find(const_cast<StringData*>(clsName));
if (clsit == s_class_constant_map.end()) {
return nullptr;
}
return &clsit->second;
}
//////////////////////////////////////////////////////////////////////////////
}} // namespace HPHP::Native
#endif // _incl_HPHP_RUNTIME_VM_NATIVE_H