From 4de4c54069274e34cfbb48e971db14701a6d6454 Mon Sep 17 00:00:00 2001 From: Joyee Cheung Date: Mon, 25 Dec 2017 21:36:15 +0800 Subject: [PATCH] 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. Backport-PR-URL: https://github.com/nodejs/node/pull/18916 PR-URL: https://github.com/nodejs/node/pull/17338 Reviewed-By: James M Snell Reviewed-By: Ruben Bridgewater --- src/uv.cc | 25 +++++++++++++++++++++-- test/parallel/test-uv-binding-constant.js | 12 +++++------ test/parallel/test-uv-errno.js | 2 +- 3 files changed, 30 insertions(+), 9 deletions(-) diff --git a/src/uv.cc b/src/uv.cc index f70da1baae5deb..3c115cf05b5a3a 100644 --- a/src/uv.cc +++ b/src/uv.cc @@ -27,10 +27,15 @@ namespace node { namespace { +using v8::Array; using v8::Context; using v8::FunctionCallbackInfo; +using v8::Integer; +using v8::Isolate; using v8::Local; +using v8::Map; using v8::Object; +using v8::String; using v8::Value; @@ -47,14 +52,30 @@ void InitializeUV(Local target, Local unused, Local 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()); #define V(name, _) NODE_DEFINE_CONSTANT(target, UV_##name); UV_ERRNO_MAP(V) #undef V -} + Local err_map = Map::New(isolate); + +#define V(name, msg) do { \ + Local 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 } // namespace node diff --git a/test/parallel/test-uv-binding-constant.js b/test/parallel/test-uv-binding-constant.js index cad08575ca92eb..beae7672db12f5 100644 --- a/test/parallel/test-uv-binding-constant.js +++ b/test/parallel/test-uv-binding-constant.js @@ -9,10 +9,10 @@ const uv = process.binding('uv'); const keys = Object.keys(uv); keys.forEach((key) => { - if (key === 'errname') - return; // skip this - const val = uv[key]; - assert.throws(() => uv[key] = 1, - /^TypeError: Cannot assign to read only property/); - assert.strictEqual(uv[key], val); + if (key.startsWith('UV_')) { + const val = uv[key]; + assert.throws(() => uv[key] = 1, + /^TypeError: Cannot assign to read only property/); + assert.strictEqual(uv[key], val); + } }); diff --git a/test/parallel/test-uv-errno.js b/test/parallel/test-uv-errno.js index d5ae7548a040d7..223be9058050d6 100644 --- a/test/parallel/test-uv-errno.js +++ b/test/parallel/test-uv-errno.js @@ -8,7 +8,7 @@ const uv = process.binding('uv'); const keys = Object.keys(uv); keys.forEach((key) => { - if (key === 'errname') + if (!key.startsWith('UV_')) return; assert.doesNotThrow(() => {