GCC Code Coverage Report
Directory: ../ Exec Total Coverage
File: /home/iojs/build/workspace/node-test-commit-linux-coverage-daily/nodes/benchmark/out/../src/uv.cc Lines: 45 47 95.7 %
Date: 2021-06-06 04:11:53 Branches: 16 24 66.7 %

Line Branch Exec Source
1
// Copyright Joyent, Inc. and other Node contributors.
2
//
3
// Permission is hereby granted, free of charge, to any person obtaining a
4
// copy of this software and associated documentation files (the
5
// "Software"), to deal in the Software without restriction, including
6
// without limitation the rights to use, copy, modify, merge, publish,
7
// distribute, sublicense, and/or sell copies of the Software, and to permit
8
// persons to whom the Software is furnished to do so, subject to the
9
// following conditions:
10
//
11
// The above copyright notice and this permission notice shall be included
12
// in all copies or substantial portions of the Software.
13
//
14
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20
// USE OR OTHER DEALINGS IN THE SOFTWARE.
21
22
#include "uv.h"
23
#include "env-inl.h"
24
#include "node.h"
25
#include "node_external_reference.h"
26
#include "node_process-inl.h"
27
28
namespace node {
29
30
namespace per_process {
31
struct UVError {
32
  int value;
33
  const char* name;
34
  const char* message;
35
};
36
37
// We only expand the macro once here to reduce the amount of code
38
// generated.
39
static const struct UVError uv_errors_map[] = {
40
#define V(name, message) {UV_##name, #name, message},
41
    UV_ERRNO_MAP(V)
42
#undef V
43
};
44
}  // namespace per_process
45
46
namespace uv {
47
48
using v8::Array;
49
using v8::Context;
50
using v8::DontDelete;
51
using v8::FunctionCallbackInfo;
52
using v8::Integer;
53
using v8::Isolate;
54
using v8::Local;
55
using v8::Map;
56
using v8::Object;
57
using v8::PropertyAttribute;
58
using v8::ReadOnly;
59
using v8::String;
60
using v8::Value;
61
62
161
void ErrName(const FunctionCallbackInfo<Value>& args) {
63
161
  Environment* env = Environment::GetCurrent(args);
64


161
  if (env->options()->pending_deprecation && env->EmitErrNameWarning()) {
65
2
    if (ProcessEmitDeprecationWarning(
66
        env,
67
        "Directly calling process.binding('uv').errname(<val>) is being"
68
        " deprecated. "
69
        "Please make sure to use util.getSystemErrorName() instead.",
70
        "DEP0119").IsNothing())
71
    return;
72
  }
73
  int err;
74
644
  if (!args[0]->Int32Value(env->context()).To(&err)) return;
75
161
  CHECK_LT(err, 0);
76
161
  const char* name = uv_err_name(err);
77
483
  args.GetReturnValue().Set(OneByteString(env->isolate(), name));
78
}
79
80
524
void GetErrMap(const FunctionCallbackInfo<Value>& args) {
81
524
  Environment* env = Environment::GetCurrent(args);
82
524
  Isolate* isolate = env->isolate();
83
524
  Local<Context> context = env->context();
84
85
  // This can't return a SafeMap, because the uv binding can be referenced
86
  // by user code by using `process.binding('uv').getErrorMap()`:
87
524
  Local<Map> err_map = Map::New(isolate);
88
89
524
  size_t errors_len = arraysize(per_process::uv_errors_map);
90
42444
  for (size_t i = 0; i < errors_len; ++i) {
91
41920
    const auto& error = per_process::uv_errors_map[i];
92
41920
    Local<Value> arr[] = {OneByteString(isolate, error.name),
93
167680
                          OneByteString(isolate, error.message)};
94
83840
    if (err_map
95
83840
            ->Set(context,
96
41920
                  Integer::New(isolate, error.value),
97
167680
                  Array::New(isolate, arr, arraysize(arr)))
98
            .IsEmpty()) {
99
      return;
100
    }
101
  }
102
103
1048
  args.GetReturnValue().Set(err_map);
104
}
105
106
467
void Initialize(Local<Object> target,
107
                Local<Value> unused,
108
                Local<Context> context,
109
                void* priv) {
110
467
  Environment* env = Environment::GetCurrent(context);
111
467
  Isolate* isolate = env->isolate();
112
934
  env->SetConstructorFunction(
113
      target,
114
      "errname",
115
467
      env->NewFunctionTemplate(ErrName));
116
117
  // TODO(joyeecheung): This should be deprecated in user land in favor of
118
  // `util.getSystemErrorName(err)`.
119
  PropertyAttribute attributes =
120
467
      static_cast<PropertyAttribute>(ReadOnly | DontDelete);
121
467
  size_t errors_len = arraysize(per_process::uv_errors_map);
122
934
  const std::string prefix = "UV_";
123
37825
  for (size_t i = 0; i < errors_len; ++i) {
124
37358
    const auto& error = per_process::uv_errors_map[i];
125
74715
    const std::string prefixed_name = prefix + error.name;
126
37356
    Local<String> name = OneByteString(isolate, prefixed_name.c_str());
127
37358
    Local<Integer> value = Integer::New(isolate, error.value);
128
74715
    target->DefineOwnProperty(context, name, value, attributes).Check();
129
  }
130
131
467
  env->SetMethod(target, "getErrorMap", GetErrMap);
132
467
}
133
134
4779
void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
135
4779
  registry->Register(ErrName);
136
4779
  registry->Register(GetErrMap);
137
4779
}
138
}  // namespace uv
139
}  // namespace node
140
141
4837
NODE_MODULE_CONTEXT_AWARE_INTERNAL(uv, node::uv::Initialize)
142

19314
NODE_MODULE_EXTERNAL_REFERENCE(uv, node::uv::RegisterExternalReferences)