• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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-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 
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   // This can't return a SafeMap, because the uv binding can be referenced
85   // by user code by using `process.binding('uv').getErrorMap()`:
86   Local<Map> err_map = Map::New(isolate);
87 
88   size_t errors_len = arraysize(per_process::uv_errors_map);
89   for (size_t i = 0; i < errors_len; ++i) {
90     const auto& error = per_process::uv_errors_map[i];
91     Local<Value> arr[] = {OneByteString(isolate, error.name),
92                           OneByteString(isolate, error.message)};
93     if (err_map
94             ->Set(context,
95                   Integer::New(isolate, error.value),
96                   Array::New(isolate, arr, arraysize(arr)))
97             .IsEmpty()) {
98       return;
99     }
100   }
101 
102   args.GetReturnValue().Set(err_map);
103 }
104 
Initialize(Local<Object> target,Local<Value> unused,Local<Context> context,void * priv)105 void Initialize(Local<Object> target,
106                 Local<Value> unused,
107                 Local<Context> context,
108                 void* priv) {
109   Environment* env = Environment::GetCurrent(context);
110   Isolate* isolate = env->isolate();
111   env->SetConstructorFunction(
112       target,
113       "errname",
114       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       static_cast<PropertyAttribute>(ReadOnly | DontDelete);
120   size_t errors_len = arraysize(per_process::uv_errors_map);
121   const std::string prefix = "UV_";
122   for (size_t i = 0; i < errors_len; ++i) {
123     const auto& error = per_process::uv_errors_map[i];
124     const std::string prefixed_name = prefix + error.name;
125     Local<String> name = OneByteString(isolate, prefixed_name.c_str());
126     Local<Integer> value = Integer::New(isolate, error.value);
127     target->DefineOwnProperty(context, name, value, attributes).Check();
128   }
129 
130   env->SetMethod(target, "getErrorMap", GetErrMap);
131 }
132 
133 }  // anonymous namespace
134 }  // namespace node
135 
136 NODE_MODULE_CONTEXT_AWARE_INTERNAL(uv, node::Initialize)
137