GCC Code Coverage Report
Directory: ../ Exec Total Coverage
File: /home/iojs/build/workspace/node-test-commit-linux-coverage/nodes/benchmark/out/../src/uv.cc Lines: 42 43 97.7 %
Date: 2019-01-07 12:15:22 Branches: 14 20 70.0 %

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 "node.h"
24
#include "node_internals.h"
25
#include "env-inl.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
80
void ErrName(const FunctionCallbackInfo<Value>& args) {
62
80
  Environment* env = Environment::GetCurrent(args);
63


80
  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
2
        "DEP0119").IsNothing())
70
    return;
71
  }
72
  int err;
73
320
  if (!args[0]->Int32Value(env->context()).To(&err)) return;
74
80
  CHECK_LT(err, 0);
75
80
  const char* name = uv_err_name(err);
76
240
  args.GetReturnValue().Set(OneByteString(env->isolate(), name));
77
}
78
79
3644
void GetErrMap(const FunctionCallbackInfo<Value>& args) {
80
3644
  Environment* env = Environment::GetCurrent(args);
81
3644
  Isolate* isolate = env->isolate();
82
3644
  Local<Context> context = env->context();
83
84
3644
  Local<Map> err_map = Map::New(isolate);
85
86
3644
  size_t errors_len = arraysize(per_process::uv_errors_map);
87
291520
  for (size_t i = 0; i < errors_len; ++i) {
88
287876
    const auto& error = per_process::uv_errors_map[i];
89
    Local<Value> arr[] = {OneByteString(isolate, error.name),
90
863628
                          OneByteString(isolate, error.message)};
91
575752
    if (err_map
92
            ->Set(context,
93
                  Integer::New(isolate, error.value),
94
1151504
                  Array::New(isolate, arr, arraysize(arr)))
95
575752
            .IsEmpty()) {
96
3644
      return;
97
    }
98
  }
99
100
7288
  args.GetReturnValue().Set(err_map);
101
}
102
103
3654
void Initialize(Local<Object> target,
104
                Local<Value> unused,
105
                Local<Context> context,
106
                void* priv) {
107
3654
  Environment* env = Environment::GetCurrent(context);
108
3654
  Isolate* isolate = env->isolate();
109
  target->Set(env->context(),
110
              FIXED_ONE_BYTE_STRING(isolate, "errname"),
111
3654
              env->NewFunctionTemplate(ErrName)
112
14616
                  ->GetFunction(env->context())
113
18270
                  .ToLocalChecked()).FromJust();
114
115
  // TODO(joyeecheung): This should be deprecated in user land in favor of
116
  // `util.getSystemErrorName(err)`.
117
  PropertyAttribute attributes =
118
3654
      static_cast<PropertyAttribute>(ReadOnly | DontDelete);
119
3654
  size_t errors_len = arraysize(per_process::uv_errors_map);
120
3654
  const std::string prefix = "UV_";
121
292320
  for (size_t i = 0; i < errors_len; ++i) {
122
288666
    const auto& error = per_process::uv_errors_map[i];
123
288666
    const std::string prefixed_name = prefix + error.name;
124
288666
    Local<String> name = OneByteString(isolate, prefixed_name.c_str());
125
288666
    Local<Integer> value = Integer::New(isolate, error.value);
126
577332
    target->DefineOwnProperty(context, name, value, attributes).FromJust();
127
288666
  }
128
129
3654
  env->SetMethod(target, "getErrorMap", GetErrMap);
130
3654
}
131
132
}  // anonymous namespace
133
}  // namespace node
134
135
3596
NODE_MODULE_CONTEXT_AWARE_INTERNAL(uv, node::Initialize)