Skip to content
This repository has been archived by the owner on Oct 15, 2020. It is now read-only.

Commit

Permalink
meta: merge node/master into node-chakracore/master
Browse files Browse the repository at this point in the history
Merge e1c29f2 as of 2018-01-20
This commit was automatically generated. For any problems, please contact jackhorton

Reviewed-By: chakrabot <[email protected]>
  • Loading branch information
chakrabot committed Feb 5, 2018
2 parents 2d7d72f + e1c29f2 commit 59f2d51
Show file tree
Hide file tree
Showing 10 changed files with 118 additions and 67 deletions.
20 changes: 20 additions & 0 deletions doc/api/util.md
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,25 @@ intended as a debugging tool. Some input values can have a significant
performance overhead that can block the event loop. Use this function
with care and never in a hot code path.

## util.getSystemErrorName(err)
<!-- YAML
added: REPLACEME
-->

* `err` {number}
* Returns: {string}

Returns the string name for a numeric error code that comes from a Node.js API.
The mapping between error codes and error names is platform-dependent.
See [Common System Errors][] for the names of common errors.

```js
fs.access('file/that/does/not/exist', (err) => {
const name = util.getSystemErrorName(err.errno);
console.error(name); // ENOENT
});
```

## util.inherits(constructor, superConstructor)
<!-- YAML
added: v0.3.0
Expand Down Expand Up @@ -1362,6 +1381,7 @@ Deprecated predecessor of `console.log`.
[Customizing `util.inspect` colors]: #util_customizing_util_inspect_colors
[Internationalization]: intl.html
[WHATWG Encoding Standard]: https://encoding.spec.whatwg.org/
[Common System Errors]: errors.html#errors_common_system_errors
[constructor]: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Object/constructor
[list of deprecated APIS]: deprecations.html#deprecations_list_of_deprecated_apis
[semantically incompatible]: https://github.com/nodejs/node/issues/4179
7 changes: 4 additions & 3 deletions lib/child_process.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,16 @@
'use strict';

const util = require('util');
const { deprecate, convertToValidSignal } = require('internal/util');
const {
deprecate, convertToValidSignal, getSystemErrorName
} = require('internal/util');
const { isUint8Array } = require('internal/util/types');
const { createPromise,
promiseResolve, promiseReject } = process.binding('util');
const debug = util.debuglog('child_process');
const { Buffer } = require('buffer');
const { Pipe, constants: PipeConstants } = process.binding('pipe_wrap');
const errors = require('internal/errors');
const { errname } = process.binding('uv');
const child_process = require('internal/child_process');
const {
_validateStdio,
Expand Down Expand Up @@ -275,7 +276,7 @@ exports.execFile = function(file /*, args, options, callback*/) {
if (!ex) {
ex = new Error('Command failed: ' + cmd + '\n' + stderr);
ex.killed = child.killed || killed;
ex.code = code < 0 ? errname(code) : code;
ex.code = code < 0 ? getSystemErrorName(code) : code;
ex.signal = signal;
}

Expand Down
15 changes: 15 additions & 0 deletions lib/internal/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const {
arrow_message_private_symbol: kArrowMessagePrivateSymbolIndex,
decorated_private_symbol: kDecoratedPrivateSymbolIndex
} = process.binding('util');
const { errmap } = process.binding('uv');

const noCrypto = !process.versions.openssl;

Expand Down Expand Up @@ -213,6 +214,19 @@ function getConstructorOf(obj) {
return null;
}

function getSystemErrorName(err) {
if (typeof err !== 'number') {
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'err', 'number', err);
}
if (err >= 0 || !Number.isSafeInteger(err)) {
throw new errors.RangeError('ERR_OUT_OF_RANGE', 'err',
'a negative integer', err);
}

const entry = errmap.get(err);
return entry ? entry[0] : `Unknown system error ${err}`;
}

// getConstructorOf is wrapped into this to save iterations
function getIdentificationOf(obj) {
const original = obj;
Expand Down Expand Up @@ -340,6 +354,7 @@ module.exports = {
emitExperimentalWarning,
filterDuplicateStrings,
getConstructorOf,
getSystemErrorName,
getIdentificationOf,
isError,
join,
Expand Down
12 changes: 3 additions & 9 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ const errors = require('internal/errors');
const { TextDecoder, TextEncoder } = require('internal/encoding');
const { isBuffer } = require('buffer').Buffer;

const { errname } = process.binding('uv');
const { previewMapIterator, previewSetIterator } = require('internal/v8');

const {
Expand Down Expand Up @@ -56,6 +55,7 @@ const {
const {
customInspectSymbol,
deprecate,
getSystemErrorName,
getIdentificationOf,
isError,
promisify,
Expand Down Expand Up @@ -1055,14 +1055,7 @@ function error(...args) {
}

function _errnoException(err, syscall, original) {
if (typeof err !== 'number') {
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'err', 'number', err);
}
if (err >= 0 || !Number.isSafeInteger(err)) {
throw new errors.RangeError('ERR_OUT_OF_RANGE', 'err',
'a negative integer', err);
}
const name = errname(err);
const name = getSystemErrorName(err);
var message = `${syscall} ${name}`;
if (original)
message += ` ${original}`;
Expand Down Expand Up @@ -1151,6 +1144,7 @@ module.exports = exports = {
debuglog,
deprecate,
format,
getSystemErrorName,
inherits,
inspect,
isArray: Array.isArray,
Expand Down
55 changes: 31 additions & 24 deletions src/node_file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ using v8::Function;
using v8::FunctionCallbackInfo;
using v8::FunctionTemplate;
using v8::HandleScope;
using v8::Int32;
using v8::Integer;
using v8::Isolate;
using v8::Local;
Expand Down Expand Up @@ -375,14 +376,15 @@ inline FSReqWrap* AsyncCall(Environment* env,
// Template counterpart of SYNC_CALL, except that it only puts
// the error number and the syscall in the context instead of
// creating an error in the C++ land.
// ctx must be checked using value->IsObject() before being passed.
template <typename Func, typename... Args>
inline int SyncCall(Environment* env, Local<Value> ctx, fs_req_wrap* req_wrap,
const char* syscall, Func fn, Args... args) {
env->PrintSyncTrace();
int err = fn(env->event_loop(), &(req_wrap->req), args..., nullptr);
if (err < 0) {
Local<Context> context = env->context();
Local<Object> ctx_obj = ctx->ToObject(context).ToLocalChecked();
Local<Object> ctx_obj = ctx.As<Object>();
Isolate *isolate = env->isolate();
ctx_obj->Set(context,
env->errno_string(),
Expand Down Expand Up @@ -415,19 +417,22 @@ inline int SyncCall(Environment* env, Local<Value> ctx, fs_req_wrap* req_wrap,
void Access(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args.GetIsolate());
HandleScope scope(env->isolate());
Local<Context> context = env->context();
CHECK_GE(args.Length(), 2);

const int argc = args.Length();
CHECK_GE(argc, 2);

CHECK(args[1]->IsInt32());
int mode = args[1].As<Int32>()->Value();

BufferValue path(env->isolate(), args[0]);
int mode = static_cast<int>(args[1]->Int32Value(context).FromJust());
CHECK_NE(*path, nullptr);

if (args[2]->IsObject()) { // access(path, mode, req)
CHECK_EQ(args.Length(), 3);
CHECK_EQ(argc, 3);
AsyncCall(env, args, "access", UTF8, AfterNoArgs,
uv_fs_access, *path, mode);
} else { // access(path, mode, undefined, ctx)
CHECK_EQ(args.Length(), 4);
CHECK_EQ(argc, 4);
fs_req_wrap req_wrap;
SyncCall(env, args[3], &req_wrap, "access", uv_fs_access, *path, mode);
}
Expand All @@ -436,20 +441,19 @@ void Access(const FunctionCallbackInfo<Value>& args) {

void Close(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);
Local<Context> context = env->context();

int length = args.Length();
CHECK_GE(length, 2);
CHECK(args[0]->IsInt32());
const int argc = args.Length();
CHECK_GE(argc, 2);

int fd = static_cast<int>(args[0]->Int32Value(context).FromJust());
CHECK(args[0]->IsInt32());
int fd = args[0].As<Int32>()->Value();

if (args[1]->IsObject()) { // close(fd, req)
CHECK_EQ(args.Length(), 2);
CHECK_EQ(argc, 2);
AsyncCall(env, args, "close", UTF8, AfterNoArgs,
uv_fs_close, fd);
} else { // close(fd, undefined, ctx)
CHECK_EQ(args.Length(), 3);
CHECK_EQ(argc, 3);
fs_req_wrap req_wrap;
SyncCall(env, args[2], &req_wrap, "close", uv_fs_close, fd);
}
Expand Down Expand Up @@ -543,17 +547,18 @@ static void InternalModuleStat(const FunctionCallbackInfo<Value>& args) {
static void Stat(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);

CHECK_GE(args.Length(), 1);
const int argc = args.Length();
CHECK_GE(argc, 1);

BufferValue path(env->isolate(), args[0]);
CHECK_NE(*path, nullptr);

if (args[1]->IsObject()) { // stat(path, req)
CHECK_EQ(args.Length(), 2);
CHECK_EQ(argc, 2);
AsyncCall(env, args, "stat", UTF8, AfterStat,
uv_fs_stat, *path);
} else { // stat(path, undefined, ctx)
CHECK_EQ(args.Length(), 3);
CHECK_EQ(argc, 3);
fs_req_wrap req_wrap;
int err = SyncCall(env, args[2], &req_wrap, "stat", uv_fs_stat, *path);
if (err == 0) {
Expand All @@ -566,17 +571,18 @@ static void Stat(const FunctionCallbackInfo<Value>& args) {
static void LStat(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);

CHECK_GE(args.Length(), 1);
const int argc = args.Length();
CHECK_GE(argc, 1);

BufferValue path(env->isolate(), args[0]);
CHECK_NE(*path, nullptr);

if (args[1]->IsObject()) { // lstat(path, req)
CHECK_EQ(args.Length(), 2);
CHECK_EQ(argc, 2);
AsyncCall(env, args, "lstat", UTF8, AfterStat,
uv_fs_lstat, *path);
} else { // lstat(path, undefined, ctx)
CHECK_EQ(args.Length(), 3);
CHECK_EQ(argc, 3);
fs_req_wrap req_wrap;
int err = SyncCall(env, args[2], &req_wrap, "lstat", uv_fs_lstat, *path);
if (err == 0) {
Expand All @@ -588,18 +594,19 @@ static void LStat(const FunctionCallbackInfo<Value>& args) {

static void FStat(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);
Local<Context> context = env->context();

CHECK(args[0]->IsInt32());
const int argc = args.Length();
CHECK_GE(argc, 1);

int fd = static_cast<int>(args[0]->Int32Value(context).FromJust());
CHECK(args[0]->IsInt32());
int fd = args[0].As<Int32>()->Value();

if (args[1]->IsObject()) { // fstat(fd, req)
CHECK_EQ(args.Length(), 2);
CHECK_EQ(argc, 2);
AsyncCall(env, args, "fstat", UTF8, AfterStat,
uv_fs_fstat, fd);
} else { // fstat(fd, undefined, ctx)
CHECK_EQ(args.Length(), 3);
CHECK_EQ(argc, 3);
fs_req_wrap req_wrap;
int err = SyncCall(env, args[2], &req_wrap, "fstat", uv_fs_fstat, fd);
if (err == 0) {
Expand Down
2 changes: 2 additions & 0 deletions src/uv.cc
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ using v8::String;
using v8::Value;


// TODO(joyeecheung): deprecate this function in favor of
// lib/util.getSystemErrorName()
void ErrName(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);
int err = args[0]->Int32Value();
Expand Down
5 changes: 3 additions & 2 deletions test/parallel/test-child-process-execfile.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
'use strict';

const common = require('../common');
const assert = require('assert');
const execFile = require('child_process').execFile;
const uv = process.binding('uv');
const { getSystemErrorName } = require('util');
const fixtures = require('../common/fixtures');

const fixture = fixtures.path('exit.js');
Expand All @@ -26,7 +27,7 @@ const fixture = fixtures.path('exit.js');
const code = -1;
const callback = common.mustCall((err, stdout, stderr) => {
assert.strictEqual(err.toString().trim(), errorString);
assert.strictEqual(err.code, uv.errname(code));
assert.strictEqual(err.code, getSystemErrorName(code));
assert.strictEqual(err.killed, true);
assert.strictEqual(err.signal, null);
assert.strictEqual(err.cmd, process.execPath);
Expand Down
7 changes: 4 additions & 3 deletions test/parallel/test-net-server-listen-handle.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const common = require('../common');
const assert = require('assert');
const net = require('net');
const fs = require('fs');
const uv = process.binding('uv');
const { getSystemErrorName } = require('util');
const { TCP, constants: TCPConstants } = process.binding('tcp_wrap');
const { Pipe, constants: PipeConstants } = process.binding('pipe_wrap');

Expand Down Expand Up @@ -46,9 +46,10 @@ function randomHandle(type) {
handleName = `pipe ${path}`;
}

if (errno < 0) { // uv.errname requires err < 0
assert(errno >= 0, `unable to bind ${handleName}: ${uv.errname(errno)}`);
if (errno < 0) {
assert.fail(`unable to bind ${handleName}: ${getSystemErrorName(errno)}`);
}

if (!common.isWindows) { // fd doesn't work on windows
// err >= 0 but fd = -1, should not happen
assert.notStrictEqual(handle.fd, -1,
Expand Down
Loading

0 comments on commit 59f2d51

Please sign in to comment.