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