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: 41 41 100.0 %
Date: 2019-02-01 22:03:38 Branches: 7 10 70.0 %

Line Branch Exec Source
1
#ifndef SRC_JS_NATIVE_API_V8_H_
2
#define SRC_JS_NATIVE_API_V8_H_
3
4
#include <string.h>
5
#include "js_native_api_types.h"
6
#include "js_native_api_v8_internals.h"
7
8
165
struct napi_env__ {
9
56
  explicit napi_env__(v8::Local<v8::Context> context)
10
56
      : isolate(context->GetIsolate()),
11
168
        context_persistent(isolate, context) {
12
112
    CHECK_EQ(isolate, context->GetIsolate());
13
56
  }
14
  v8::Isolate* const isolate;  // Shortcut for context()->GetIsolate()
15
  v8impl::Persistent<v8::Context> context_persistent;
16
17
6053
  inline v8::Local<v8::Context> context() const {
18
6053
    return v8impl::PersistentToLocal::Strong(context_persistent);
19
  }
20
21
14
  inline void Ref() { refs++; }
22

69
  inline void Unref() { if ( --refs == 0) delete this; }
23
24
  v8impl::Persistent<v8::Value> last_exception;
25
  napi_extended_error_info last_error;
26
  int open_handle_scopes = 0;
27
  int open_callback_scopes = 0;
28
  int refs = 1;
29
};
30
31
26481
static inline napi_status napi_clear_last_error(napi_env env) {
32
26481
  env->last_error.error_code = napi_ok;
33
34
  // TODO(boingoing): Should this be a callback?
35
26481
  env->last_error.engine_error_code = 0;
36
26481
  env->last_error.engine_reserved = nullptr;
37
26481
  return napi_ok;
38
}
39
40
static inline
41
1228
napi_status napi_set_last_error(napi_env env, napi_status error_code,
42
                                uint32_t engine_error_code = 0,
43
                                void* engine_reserved = nullptr) {
44
1228
  env->last_error.error_code = error_code;
45
1228
  env->last_error.engine_error_code = engine_error_code;
46
1228
  env->last_error.engine_reserved = engine_reserved;
47
1228
  return error_code;
48
}
49
50
#define RETURN_STATUS_IF_FALSE(env, condition, status)                  \
51
  do {                                                                  \
52
    if (!(condition)) {                                                 \
53
      return napi_set_last_error((env), (status));                      \
54
    }                                                                   \
55
  } while (0)
56
57
#define CHECK_ENV(env)          \
58
  do {                          \
59
    if ((env) == nullptr) {     \
60
      return napi_invalid_arg;  \
61
    }                           \
62
  } while (0)
63
64
#define CHECK_ARG(env, arg) \
65
  RETURN_STATUS_IF_FALSE((env), ((arg) != nullptr), napi_invalid_arg)
66
67
#define CHECK_MAYBE_EMPTY(env, maybe, status) \
68
  RETURN_STATUS_IF_FALSE((env), !((maybe).IsEmpty()), (status))
69
70
// NAPI_PREAMBLE is not wrapped in do..while: try_catch must have function scope
71
#define NAPI_PREAMBLE(env)                                       \
72
  CHECK_ENV((env));                                              \
73
  RETURN_STATUS_IF_FALSE((env), (env)->last_exception.IsEmpty(), \
74
                         napi_pending_exception);                \
75
  napi_clear_last_error((env));                                  \
76
  v8impl::TryCatch try_catch((env))
77
78
#define CHECK_TO_TYPE(env, type, context, result, src, status)                \
79
  do {                                                                        \
80
    CHECK_ARG((env), (src));                                                  \
81
    auto maybe = v8impl::V8LocalValueFromJsValue((src))->To##type((context)); \
82
    CHECK_MAYBE_EMPTY((env), maybe, (status));                                \
83
    (result) = maybe.ToLocalChecked();                                        \
84
  } while (0)
85
86
#define CHECK_TO_FUNCTION(env, result, src)                                 \
87
  do {                                                                      \
88
    CHECK_ARG((env), (src));                                                \
89
    v8::Local<v8::Value> v8value = v8impl::V8LocalValueFromJsValue((src));  \
90
    RETURN_STATUS_IF_FALSE((env), v8value->IsFunction(), napi_invalid_arg); \
91
    (result) = v8value.As<v8::Function>();                                  \
92
  } while (0)
93
94
#define CHECK_TO_OBJECT(env, context, result, src) \
95
  CHECK_TO_TYPE((env), Object, (context), (result), (src), napi_object_expected)
96
97
#define CHECK_TO_STRING(env, context, result, src) \
98
  CHECK_TO_TYPE((env), String, (context), (result), (src), napi_string_expected)
99
100
#define GET_RETURN_STATUS(env)      \
101
  (!try_catch.HasCaught() ? napi_ok \
102
                         : napi_set_last_error((env), napi_pending_exception))
103
104
#define THROW_RANGE_ERROR_IF_FALSE(env, condition, error, message) \
105
  do {                                                             \
106
    if (!(condition)) {                                            \
107
      napi_throw_range_error((env), (error), (message));           \
108
      return napi_set_last_error((env), napi_generic_failure);     \
109
    }                                                              \
110
  } while (0)
111
112
#define NAPI_CALL_INTO_MODULE(env, call, handle_exception)                   \
113
  do {                                                                       \
114
    int open_handle_scopes = (env)->open_handle_scopes;                      \
115
    int open_callback_scopes = (env)->open_callback_scopes;                  \
116
    napi_clear_last_error((env));                                            \
117
    call;                                                                    \
118
    CHECK_EQ((env)->open_handle_scopes, open_handle_scopes);                 \
119
    CHECK_EQ((env)->open_callback_scopes, open_callback_scopes);             \
120
    if (!(env)->last_exception.IsEmpty()) {                                  \
121
      handle_exception(                                                      \
122
          v8::Local<v8::Value>::New((env)->isolate, (env)->last_exception)); \
123
      (env)->last_exception.Reset();                                         \
124
    }                                                                        \
125
  } while (0)
126
127
#define NAPI_CALL_INTO_MODULE_THROW(env, call) \
128
  NAPI_CALL_INTO_MODULE((env), call, (env)->isolate->ThrowException)
129
130
namespace v8impl {
131
132
//=== Conversion between V8 Handles and napi_value ========================
133
134
// This asserts v8::Local<> will always be implemented with a single
135
// pointer field so that we can pass it around as a void*.
136
static_assert(sizeof(v8::Local<v8::Value>) == sizeof(napi_value),
137
  "Cannot convert between v8::Local<v8::Value> and napi_value");
138
139
15213
inline napi_value JsValueFromV8LocalValue(v8::Local<v8::Value> local) {
140
15213
  return reinterpret_cast<napi_value>(*local);
141
}
142
143
13191
inline v8::Local<v8::Value> V8LocalValueFromJsValue(napi_value v) {
144
  v8::Local<v8::Value> local;
145
13191
  memcpy(&local, &v, sizeof(v));
146
13191
  return local;
147
}
148
149
// Adapter for napi_finalize callbacks.
150
class Finalizer {
151
 protected:
152
2550
  Finalizer(napi_env env,
153
            napi_finalize finalize_callback,
154
            void* finalize_data,
155
            void* finalize_hint)
156
    : _env(env),
157
      _finalize_callback(finalize_callback),
158
      _finalize_data(finalize_data),
159
2550
      _finalize_hint(finalize_hint) {
160
2550
  }
161
162
2019
  ~Finalizer() {
163
2019
  }
164
165
 public:
166
3
  static Finalizer* New(napi_env env,
167
                        napi_finalize finalize_callback = nullptr,
168
                        void* finalize_data = nullptr,
169
                        void* finalize_hint = nullptr) {
170
    return new Finalizer(
171
3
      env, finalize_callback, finalize_data, finalize_hint);
172
  }
173
174
3
  static void Delete(Finalizer* finalizer) {
175
3
    delete finalizer;
176
3
  }
177
178
 protected:
179
  napi_env _env;
180
  napi_finalize _finalize_callback;
181
  void* _finalize_data;
182
  void* _finalize_hint;
183
  bool _finalize_ran = false;
184
};
185
186
class TryCatch : public v8::TryCatch {
187
 public:
188
4532
  explicit TryCatch(napi_env env)
189
4532
      : v8::TryCatch(env->isolate), _env(env) {}
190
191
9064
  ~TryCatch() {
192
4532
    if (HasCaught()) {
193
2466
      _env->last_exception.Reset(_env->isolate, Exception());
194
    }
195
4532
  }
196
197
 private:
198
  napi_env _env;
199
};
200
201
}  // end of namespace v8impl
202
203
#endif  // SRC_JS_NATIVE_API_V8_H_