GCC Code Coverage Report
Directory: ../ Exec Total Coverage
File: /home/iojs/build/workspace/node-test-commit-linux-coverage-daily/nodes/benchmark/out/../src/js_native_api_v8.h Lines: 55 56 98.2 %
Date: 2019-05-05 22:32:45 Branches: 23 34 67.6 %

Line Branch Exec Source
1
#ifndef SRC_JS_NATIVE_API_V8_H_
2
#define SRC_JS_NATIVE_API_V8_H_
3
4
// This file needs to be compatible with C compilers.
5
#include <string.h>  // NOLINT(modernize-deprecated-headers)
6
#include "js_native_api_types.h"
7
#include "js_native_api_v8_internals.h"
8
9
struct napi_env__ {
10
67
  explicit napi_env__(v8::Local<v8::Context> context)
11
67
      : isolate(context->GetIsolate()),
12
268
        context_persistent(isolate, context) {
13
134
    CHECK_EQ(isolate, context->GetIsolate());
14
67
  }
15
198
  virtual ~napi_env__() = default;
16
  v8::Isolate* const isolate;  // Shortcut for context()->GetIsolate()
17
  v8impl::Persistent<v8::Context> context_persistent;
18
19
10823
  inline v8::Local<v8::Context> context() const {
20
10823
    return v8impl::PersistentToLocal::Strong(context_persistent);
21
  }
22
23
14
  inline void Ref() { refs++; }
24

80
  inline void Unref() { if ( --refs == 0) delete this; }
25
26
  virtual bool can_call_into_js() const { return true; }
27
28
  v8impl::Persistent<v8::Value> last_exception;
29
  napi_extended_error_info last_error;
30
  int open_handle_scopes = 0;
31
  int open_callback_scopes = 0;
32
  int refs = 1;
33
};
34
35
27741
static inline napi_status napi_clear_last_error(napi_env env) {
36
27741
  env->last_error.error_code = napi_ok;
37
38
  // TODO(boingoing): Should this be a callback?
39
27741
  env->last_error.engine_error_code = 0;
40
27741
  env->last_error.engine_reserved = nullptr;
41
27741
  return napi_ok;
42
}
43
44
static inline
45
1239
napi_status napi_set_last_error(napi_env env, napi_status error_code,
46
                                uint32_t engine_error_code = 0,
47
                                void* engine_reserved = nullptr) {
48
1239
  env->last_error.error_code = error_code;
49
1239
  env->last_error.engine_error_code = engine_error_code;
50
1239
  env->last_error.engine_reserved = engine_reserved;
51
1239
  return error_code;
52
}
53
54
#define RETURN_STATUS_IF_FALSE(env, condition, status)                  \
55
  do {                                                                  \
56
    if (!(condition)) {                                                 \
57
      return napi_set_last_error((env), (status));                      \
58
    }                                                                   \
59
  } while (0)
60
61
#define CHECK_ENV(env)          \
62
  do {                          \
63
    if ((env) == nullptr) {     \
64
      return napi_invalid_arg;  \
65
    }                           \
66
  } while (0)
67
68
#define CHECK_ARG(env, arg) \
69
  RETURN_STATUS_IF_FALSE((env), ((arg) != nullptr), napi_invalid_arg)
70
71
#define CHECK_MAYBE_EMPTY(env, maybe, status) \
72
  RETURN_STATUS_IF_FALSE((env), !((maybe).IsEmpty()), (status))
73
74
// NAPI_PREAMBLE is not wrapped in do..while: try_catch must have function scope
75
#define NAPI_PREAMBLE(env)                                          \
76
  CHECK_ENV((env));                                                 \
77
  RETURN_STATUS_IF_FALSE((env),                                     \
78
      (env)->last_exception.IsEmpty() && (env)->can_call_into_js(), \
79
      napi_pending_exception);                                      \
80
  napi_clear_last_error((env));                                     \
81
  v8impl::TryCatch try_catch((env))
82
83
#define CHECK_TO_TYPE(env, type, context, result, src, status)                \
84
  do {                                                                        \
85
    CHECK_ARG((env), (src));                                                  \
86
    auto maybe = v8impl::V8LocalValueFromJsValue((src))->To##type((context)); \
87
    CHECK_MAYBE_EMPTY((env), maybe, (status));                                \
88
    (result) = maybe.ToLocalChecked();                                        \
89
  } while (0)
90
91
#define CHECK_TO_FUNCTION(env, result, src)                                 \
92
  do {                                                                      \
93
    CHECK_ARG((env), (src));                                                \
94
    v8::Local<v8::Value> v8value = v8impl::V8LocalValueFromJsValue((src));  \
95
    RETURN_STATUS_IF_FALSE((env), v8value->IsFunction(), napi_invalid_arg); \
96
    (result) = v8value.As<v8::Function>();                                  \
97
  } while (0)
98
99
#define CHECK_TO_OBJECT(env, context, result, src) \
100
  CHECK_TO_TYPE((env), Object, (context), (result), (src), napi_object_expected)
101
102
#define CHECK_TO_STRING(env, context, result, src) \
103
  CHECK_TO_TYPE((env), String, (context), (result), (src), napi_string_expected)
104
105
#define GET_RETURN_STATUS(env)      \
106
  (!try_catch.HasCaught() ? napi_ok \
107
                         : napi_set_last_error((env), napi_pending_exception))
108
109
#define THROW_RANGE_ERROR_IF_FALSE(env, condition, error, message) \
110
  do {                                                             \
111
    if (!(condition)) {                                            \
112
      napi_throw_range_error((env), (error), (message));           \
113
      return napi_set_last_error((env), napi_generic_failure);     \
114
    }                                                              \
115
  } while (0)
116
117
template <typename T, typename U>
118
6111
void NapiCallIntoModule(napi_env env, T&& call, U&& handle_exception) {
119
6111
  int open_handle_scopes = env->open_handle_scopes;
120
6111
  int open_callback_scopes = env->open_callback_scopes;
121
6111
  napi_clear_last_error(env);
122
6111
  call();
123


6111
  CHECK_EQ(env->open_handle_scopes, open_handle_scopes);
124


6111
  CHECK_EQ(env->open_callback_scopes, open_callback_scopes);
125


12222
  if (!env->last_exception.IsEmpty()) {
126
2468
    handle_exception(env->last_exception.Get(env->isolate));
127
1233
    env->last_exception.Reset();
128
  }
129
6110
}
130
131
template <typename T>
132
5601
void NapiCallIntoModuleThrow(napi_env env, T&& call) {
133
6833
  NapiCallIntoModule(env, call, [&](v8::Local<v8::Value> value) {
134
1232
    env->isolate->ThrowException(value);
135
12434
  });
136
5601
}
137
138
namespace v8impl {
139
140
//=== Conversion between V8 Handles and napi_value ========================
141
142
// This asserts v8::Local<> will always be implemented with a single
143
// pointer field so that we can pass it around as a void*.
144
static_assert(sizeof(v8::Local<v8::Value>) == sizeof(napi_value),
145
  "Cannot convert between v8::Local<v8::Value> and napi_value");
146
147
16378
inline napi_value JsValueFromV8LocalValue(v8::Local<v8::Value> local) {
148
16378
  return reinterpret_cast<napi_value>(*local);
149
}
150
151
13355
inline v8::Local<v8::Value> V8LocalValueFromJsValue(napi_value v) {
152
  v8::Local<v8::Value> local;
153
13355
  memcpy(&local, &v, sizeof(v));
154
13355
  return local;
155
}
156
157
// Adapter for napi_finalize callbacks.
158
class Finalizer {
159
 protected:
160
2560
  Finalizer(napi_env env,
161
            napi_finalize finalize_callback,
162
            void* finalize_data,
163
            void* finalize_hint)
164
    : _env(env),
165
      _finalize_callback(finalize_callback),
166
      _finalize_data(finalize_data),
167
2560
      _finalize_hint(finalize_hint) {
168
2560
  }
169
170
  ~Finalizer() = default;
171
172
 public:
173
3
  static Finalizer* New(napi_env env,
174
                        napi_finalize finalize_callback = nullptr,
175
                        void* finalize_data = nullptr,
176
                        void* finalize_hint = nullptr) {
177
    return new Finalizer(
178
3
      env, finalize_callback, finalize_data, finalize_hint);
179
  }
180
181
3
  static void Delete(Finalizer* finalizer) {
182
3
    delete finalizer;
183
3
  }
184
185
 protected:
186
  napi_env _env;
187
  napi_finalize _finalize_callback;
188
  void* _finalize_data;
189
  void* _finalize_hint;
190
  bool _finalize_ran = false;
191
};
192
193
class TryCatch : public v8::TryCatch {
194
 public:
195
4635
  explicit TryCatch(napi_env env)
196
4635
      : v8::TryCatch(env->isolate), _env(env) {}
197
198
9270
  ~TryCatch() {
199
4635
    if (HasCaught()) {
200
2470
      _env->last_exception.Reset(_env->isolate, Exception());
201
    }
202
4635
  }
203
204
 private:
205
  napi_env _env;
206
};
207
208
}  // end of namespace v8impl
209
210
#endif  // SRC_JS_NATIVE_API_V8_H_