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: 40 42 95.2 %
Date: 2021-02-11 04:11:15 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_process.h"
26
27
namespace node {
28
29
namespace per_process {
30
struct UVError {
31
  int value;
32
  const char* name;
33
  const char* message;
34
};
35
36
// We only expand the macro once here to reduce the amount of code
37
// generated.
38
static const struct UVError uv_errors_map[] = {
39
#define V(name, message) {UV_##name, #name, message},
40
    UV_ERRNO_MAP(V)
41
#undef V
42
};
43
}  // namespace per_process
44
45
namespace {
46
47
using v8::Array;
48
using v8::Context;
49
using v8::DontDelete;
50
using v8::FunctionCallbackInfo;
51
using v8::Integer;
52
using v8::Isolate;
53
using v8::Local;
54
using v8::Map;
55
using v8::Object;
56
using v8::PropertyAttribute;
57
using v8::ReadOnly;
58
using v8::String;
59
using v8::Value;
60
61
81
void ErrName(const FunctionCallbackInfo<Value>& args) {
62
81
  Environment* env = Environment::GetCurrent(args);
63


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

18788
NODE_MODULE_CONTEXT_AWARE_INTERNAL(uv, node::Initialize)