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 |
|
478 |
void GetErrMap(const FunctionCallbackInfo<Value>& args) { |
80 |
|
478 |
Environment* env = Environment::GetCurrent(args); |
81 |
|
478 |
Isolate* isolate = env->isolate(); |
82 |
|
478 |
Local<Context> context = env->context(); |
83 |
|
|
|
84 |
|
478 |
Local<Map> err_map = Map::New(isolate); |
85 |
|
|
|
86 |
|
478 |
size_t errors_len = arraysize(per_process::uv_errors_map); |
87 |
✓✓ |
38718 |
for (size_t i = 0; i < errors_len; ++i) { |
88 |
|
38240 |
const auto& error = per_process::uv_errors_map[i]; |
89 |
|
38240 |
Local<Value> arr[] = {OneByteString(isolate, error.name), |
90 |
|
152960 |
OneByteString(isolate, error.message)}; |
91 |
✗✓ |
76480 |
if (err_map |
92 |
|
76480 |
->Set(context, |
93 |
|
38240 |
Integer::New(isolate, error.value), |
94 |
|
152960 |
Array::New(isolate, arr, arraysize(arr))) |
95 |
|
|
.IsEmpty()) { |
96 |
|
|
return; |
97 |
|
|
} |
98 |
|
|
} |
99 |
|
|
|
100 |
|
956 |
args.GetReturnValue().Set(err_map); |
101 |
|
|
} |
102 |
|
|
|
103 |
|
4521 |
void Initialize(Local<Object> target, |
104 |
|
|
Local<Value> unused, |
105 |
|
|
Local<Context> context, |
106 |
|
|
void* priv) { |
107 |
|
4521 |
Environment* env = Environment::GetCurrent(context); |
108 |
|
4521 |
Isolate* isolate = env->isolate(); |
109 |
|
9042 |
target->Set(env->context(), |
110 |
|
|
FIXED_ONE_BYTE_STRING(isolate, "errname"), |
111 |
|
9042 |
env->NewFunctionTemplate(ErrName) |
112 |
|
13563 |
->GetFunction(env->context()) |
113 |
|
18084 |
.ToLocalChecked()).Check(); |
114 |
|
|
|
115 |
|
|
// TODO(joyeecheung): This should be deprecated in user land in favor of |
116 |
|
|
// `util.getSystemErrorName(err)`. |
117 |
|
|
PropertyAttribute attributes = |
118 |
|
4521 |
static_cast<PropertyAttribute>(ReadOnly | DontDelete); |
119 |
|
4521 |
size_t errors_len = arraysize(per_process::uv_errors_map); |
120 |
|
9042 |
const std::string prefix = "UV_"; |
121 |
✓✓ |
366201 |
for (size_t i = 0; i < errors_len; ++i) { |
122 |
|
361680 |
const auto& error = per_process::uv_errors_map[i]; |
123 |
|
723360 |
const std::string prefixed_name = prefix + error.name; |
124 |
|
361680 |
Local<String> name = OneByteString(isolate, prefixed_name.c_str()); |
125 |
|
361680 |
Local<Integer> value = Integer::New(isolate, error.value); |
126 |
|
723360 |
target->DefineOwnProperty(context, name, value, attributes).Check(); |
127 |
|
|
} |
128 |
|
|
|
129 |
|
4521 |
env->SetMethod(target, "getErrorMap", GetErrMap); |
130 |
|
4521 |
} |
131 |
|
|
|
132 |
|
|
} // anonymous namespace |
133 |
|
|
} // namespace node |
134 |
|
|
|
135 |
✓✗✓✗
|
18756 |
NODE_MODULE_CONTEXT_AWARE_INTERNAL(uv, node::Initialize) |