src: expose uv.errmap to binding

Add a errno -> [error code, uv error message] map to the uv binding
so the error message can be assembled in the JS layer.

PR-URL: https://github.com/nodejs/node/pull/17338
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
This commit is contained in:
Joyee Cheung 2017-12-25 21:36:15 +08:00
parent 24c71fb47c
commit c56972779b
No known key found for this signature in database
GPG Key ID: F586868AAD831D0C
3 changed files with 30 additions and 9 deletions

View File

@ -27,10 +27,15 @@
namespace node { namespace node {
namespace { namespace {
using v8::Array;
using v8::Context; using v8::Context;
using v8::FunctionCallbackInfo; using v8::FunctionCallbackInfo;
using v8::Integer;
using v8::Isolate;
using v8::Local; using v8::Local;
using v8::Map;
using v8::Object; using v8::Object;
using v8::String;
using v8::Value; using v8::Value;
@ -47,14 +52,30 @@ void InitializeUV(Local<Object> target,
Local<Value> unused, Local<Value> unused,
Local<Context> context) { Local<Context> context) {
Environment* env = Environment::GetCurrent(context); Environment* env = Environment::GetCurrent(context);
target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "errname"), Isolate* isolate = env->isolate();
target->Set(FIXED_ONE_BYTE_STRING(isolate, "errname"),
env->NewFunctionTemplate(ErrName)->GetFunction()); env->NewFunctionTemplate(ErrName)->GetFunction());
#define V(name, _) NODE_DEFINE_CONSTANT(target, UV_##name); #define V(name, _) NODE_DEFINE_CONSTANT(target, UV_##name);
UV_ERRNO_MAP(V) UV_ERRNO_MAP(V)
#undef V #undef V
}
Local<Map> err_map = Map::New(isolate);
#define V(name, msg) do { \
Local<Array> arr = Array::New(isolate, 2); \
arr->Set(0, OneByteString(isolate, #name)); \
arr->Set(1, OneByteString(isolate, msg)); \
err_map->Set(context, \
Integer::New(isolate, UV_##name), \
arr).ToLocalChecked(); \
} while (0);
UV_ERRNO_MAP(V)
#undef V
target->Set(context, FIXED_ONE_BYTE_STRING(isolate, "errmap"),
err_map).FromJust();
}
} // anonymous namespace } // anonymous namespace
} // namespace node } // namespace node

View File

@ -9,10 +9,10 @@ const uv = process.binding('uv');
const keys = Object.keys(uv); const keys = Object.keys(uv);
keys.forEach((key) => { keys.forEach((key) => {
if (key === 'errname') if (key.startsWith('UV_')) {
return; // skip this
const val = uv[key]; const val = uv[key];
assert.throws(() => uv[key] = 1, assert.throws(() => uv[key] = 1,
/^TypeError: Cannot assign to read only property/); /^TypeError: Cannot assign to read only property/);
assert.strictEqual(uv[key], val); assert.strictEqual(uv[key], val);
}
}); });

View File

@ -8,7 +8,7 @@ const uv = process.binding('uv');
const keys = Object.keys(uv); const keys = Object.keys(uv);
keys.forEach((key) => { keys.forEach((key) => {
if (key === 'errname') if (!key.startsWith('UV_'))
return; return;
assert.doesNotThrow(() => { assert.doesNotThrow(() => {