-
Notifications
You must be signed in to change notification settings - Fork 33
/
compat.h
143 lines (119 loc) · 4.6 KB
/
compat.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
// Copyright (c) 2014, StrongLoop Inc.
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#ifndef COMPAT_H_ // NOLINT(build/header_guard)
#define COMPAT_H_ // NOLINT(build/header_guard)
#include "node_version.h"
#include "v8.h"
#include "v8-profiler.h"
#define COMPAT_IOJS_1_x 42
#if NODE_MAJOR_VERSION == 0 && NODE_MINOR_VERSION >= 11
#define COMPAT_NODE_VERSION 12 // v0.12
#elif NODE_MAJOR_VERSION == 0 && NODE_MINOR_VERSION == 10
#define COMPAT_NODE_VERSION 10 // v0.10
#elif NODE_MODULE_VERSION >= COMPAT_IOJS_1_x // io.js semver 1.0.x
#define COMPAT_NODE_VERSION COMPAT_IOJS_1_x
#else
#error "Unsupported node.js version."
#endif
namespace compat {
#if COMPAT_NODE_VERSION == 10
typedef v8::Arguments ArgumentType;
typedef v8::Handle<v8::Value> ReturnType;
typedef v8::InvocationCallback FunctionCallback;
#elif COMPAT_NODE_VERSION == 12 || COMPAT_NODE_VERSION == COMPAT_IOJS_1_x
typedef v8::FunctionCallbackInfo<v8::Value> ArgumentType;
typedef void ReturnType;
typedef v8::FunctionCallback FunctionCallback;
#endif
inline v8::Local<v8::Boolean> True(v8::Isolate* isolate);
inline v8::Local<v8::Boolean> False(v8::Isolate* isolate);
inline v8::Local<v8::Primitive> Null(v8::Isolate* isolate);
inline v8::Local<v8::Primitive> Undefined(v8::Isolate* isolate);
class AllStatic {
private:
AllStatic();
};
struct Array : public AllStatic {
inline static v8::Local<v8::Array> New(v8::Isolate* isolate, int length = 0);
};
struct Boolean : public AllStatic {
inline static v8::Local<v8::Boolean> New(v8::Isolate* isolate, bool value);
};
struct FunctionTemplate : public AllStatic {
inline static v8::Local<v8::FunctionTemplate> New(v8::Isolate* isolate,
FunctionCallback callback =
0);
};
struct HeapProfiler : public AllStatic {
inline static const v8::HeapSnapshot* TakeHeapSnapshot(
v8::Isolate* isolate,
v8::Local<v8::String> title = v8::Local<v8::String>());
inline static void DeleteAllHeapSnapshots(v8::Isolate* isolate);
};
struct Integer : public AllStatic {
inline static v8::Local<v8::Integer> New(v8::Isolate* isolate, int32_t value);
inline static v8::Local<v8::Integer> NewFromUnsigned(v8::Isolate* isolate,
uint32_t value);
};
struct Number : public AllStatic {
inline static v8::Local<v8::Number> New(v8::Isolate* isolate, double value);
};
struct Object : public AllStatic {
inline static v8::Local<v8::Object> New(v8::Isolate* isolate);
};
struct String : public AllStatic {
enum NewStringType {
kNormalString,
kInternalizedString,
kUndetectableString
};
inline static v8::Local<v8::String> NewFromUtf8(v8::Isolate* isolate,
const char* data,
NewStringType type =
kNormalString,
int length = -1);
};
class HandleScope {
public:
inline explicit HandleScope(v8::Isolate* isolate);
private:
v8::HandleScope handle_scope_;
};
template <typename T>
class Persistent {
public:
inline ~Persistent();
inline v8::Local<T> ToLocal(v8::Isolate* isolate) const;
inline void Reset();
inline void Reset(v8::Isolate* isolate, v8::Local<T> value);
inline bool IsEmpty() const;
private:
v8::Persistent<T> handle_;
};
class ReturnableHandleScope {
public:
inline explicit ReturnableHandleScope(const ArgumentType& args);
inline ReturnType Return();
inline ReturnType Return(bool value);
inline ReturnType Return(int value);
inline ReturnType Return(double value);
inline ReturnType Return(const char* value);
inline ReturnType Return(v8::Local<v8::Value> value);
private:
inline v8::Isolate* isolate() const;
const ArgumentType& args_;
v8::HandleScope handle_scope_;
};
} // namespace compat
#endif // COMPAT_H_ // NOLINT(build/header_guard)