Skip to content

Commit 0226fe1

Browse files
committed
fixup! src: lazily load internalBinding('uv') and build the errmap lazily
1 parent c5d4f00 commit 0226fe1

File tree

1 file changed

+46
-18
lines changed

1 file changed

+46
-18
lines changed

src/uv.cc

Lines changed: 46 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,39 @@
2525
#include "env-inl.h"
2626

2727
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+
2845
namespace {
2946

3047
using v8::Array;
3148
using v8::Context;
49+
using v8::DontDelete;
3250
using v8::FunctionCallbackInfo;
3351
using v8::Integer;
3452
using v8::Isolate;
3553
using v8::Local;
3654
using v8::Map;
3755
using v8::Object;
56+
using v8::PropertyAttribute;
57+
using v8::ReadOnly;
3858
using v8::String;
3959
using v8::Value;
4060

41-
4261
void ErrName(const FunctionCallbackInfo<Value>& args) {
4362
Environment* env = Environment::GetCurrent(args);
4463
if (env->options()->pending_deprecation && env->EmitErrNameWarning()) {
@@ -57,27 +76,26 @@ void ErrName(const FunctionCallbackInfo<Value>& args) {
5776
args.GetReturnValue().Set(OneByteString(env->isolate(), name));
5877
}
5978

60-
6179
void GetErrMap(const FunctionCallbackInfo<Value>& args) {
6280
Environment* env = Environment::GetCurrent(args);
6381
Isolate* isolate = env->isolate();
6482
Local<Context> context = env->context();
6583

6684
Local<Map> err_map = Map::New(isolate);
6785

68-
#define V(name, msg) do { \
69-
Local<Value> arr[] = { \
70-
OneByteString(isolate, #name), \
71-
OneByteString(isolate, msg) \
72-
}; \
73-
if (err_map->Set(context, \
74-
Integer::New(isolate, UV_##name), \
75-
Array::New(isolate, arr, arraysize(arr))).IsEmpty()) { \
76-
return; \
77-
} \
78-
} while (0);
79-
UV_ERRNO_MAP(V)
80-
#undef V
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+
}
8199

82100
args.GetReturnValue().Set(err_map);
83101
}
@@ -94,9 +112,19 @@ void Initialize(Local<Object> target,
94112
->GetFunction(env->context())
95113
.ToLocalChecked()).FromJust();
96114

97-
#define V(name, _) NODE_DEFINE_CONSTANT(target, UV_##name);
98-
UV_ERRNO_MAP(V)
99-
#undef V
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).FromJust();
127+
}
100128

101129
env->SetMethod(target, "getErrorMap", GetErrMap);
102130
}

0 commit comments

Comments
 (0)