Skip to content

Commit

Permalink
src: remove throws in set/getHiddenValue
Browse files Browse the repository at this point in the history
These are internal only utility functions, CHECK instead of throw
  • Loading branch information
jasnell committed Oct 30, 2017
1 parent 64168eb commit 7925adc
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 55 deletions.
19 changes: 12 additions & 7 deletions lib/internal/util.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
'use strict';

const errors = require('internal/errors');
const binding = process.binding('util');
const { signals } = process.binding('constants').os;

const { createPromise, promiseResolve, promiseReject } = binding;
const {
createPromise,
getHiddenValue,
promiseResolve,
promiseReject,
setHiddenValue,
arrow_message_private_symbol: kArrowMessagePrivateSymbolIndex,
decorated_private_symbol: kDecoratedPrivateSymbolIndex
} = process.binding('util');

const kArrowMessagePrivateSymbolIndex = binding['arrow_message_private_symbol'];
const kDecoratedPrivateSymbolIndex = binding['decorated_private_symbol'];
const noCrypto = !process.versions.openssl;

function isError(e) {
Expand Down Expand Up @@ -66,14 +71,14 @@ function deprecate(fn, msg, code) {

function decorateErrorStack(err) {
if (!(isError(err) && err.stack) ||
binding.getHiddenValue(err, kDecoratedPrivateSymbolIndex) === true)
getHiddenValue(err, kDecoratedPrivateSymbolIndex) === true)
return;

const arrow = binding.getHiddenValue(err, kArrowMessagePrivateSymbolIndex);
const arrow = getHiddenValue(err, kArrowMessagePrivateSymbolIndex);

if (arrow) {
err.stack = arrow + err.stack;
binding.setHiddenValue(err, kDecoratedPrivateSymbolIndex, true);
setHiddenValue(err, kDecoratedPrivateSymbolIndex, true);
}
}

Expand Down
3 changes: 2 additions & 1 deletion lib/repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,8 @@ function REPLServer(prompt,
const top = replMap.get(self);
const pstrace = Error.prepareStackTrace;
Error.prepareStackTrace = prepareStackTrace(pstrace);
internalUtil.decorateErrorStack(e);
if (typeof e === 'object')
internalUtil.decorateErrorStack(e);
Error.prepareStackTrace = pstrace;
const isError = internalUtil.isError(e);
if (e instanceof SyntaxError && e.stack) {
Expand Down
14 changes: 4 additions & 10 deletions src/node_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,8 @@ inline Local<Private> IndexToPrivateSymbol(Environment* env, uint32_t index) {
static void GetHiddenValue(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);

if (!args[0]->IsObject())
return env->ThrowTypeError("obj must be an object");

if (!args[1]->IsUint32())
return env->ThrowTypeError("index must be an uint32");
CHECK(args[0]->IsObject());
CHECK(args[1]->IsUint32());

Local<Object> obj = args[0].As<Object>();
auto index = args[1]->Uint32Value(env->context()).FromJust();
Expand All @@ -118,11 +115,8 @@ static void GetHiddenValue(const FunctionCallbackInfo<Value>& args) {
static void SetHiddenValue(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);

if (!args[0]->IsObject())
return env->ThrowTypeError("obj must be an object");

if (!args[1]->IsUint32())
return env->ThrowTypeError("index must be an uint32");
CHECK(args[0]->IsObject());
CHECK(args[1]->IsUint32());

Local<Object> obj = args[0].As<Object>();
auto index = args[1]->Uint32Value(env->context()).FromJust();
Expand Down
46 changes: 9 additions & 37 deletions test/parallel/test-util-internal.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,50 +5,22 @@ require('../common');
const assert = require('assert');
const fixtures = require('../common/fixtures');

const binding = process.binding('util');
const kArrowMessagePrivateSymbolIndex = binding['arrow_message_private_symbol'];
const {
getHiddenValue,
setHiddenValue,
arrow_message_private_symbol: kArrowMessagePrivateSymbolIndex
} = process.binding('util');

function getHiddenValue(obj, index) {
return function() {
binding.getHiddenValue(obj, index);
};
}

function setHiddenValue(obj, index, val) {
return function() {
binding.setHiddenValue(obj, index, val);
};
}

const errMessageObj = /obj must be an object/;
const errMessageIndex = /index must be an uint32/;

assert.throws(getHiddenValue(), errMessageObj);
assert.throws(getHiddenValue(null, 'foo'), errMessageObj);
assert.throws(getHiddenValue(undefined, 'foo'), errMessageObj);
assert.throws(getHiddenValue('bar', 'foo'), errMessageObj);
assert.throws(getHiddenValue(85, 'foo'), errMessageObj);
assert.throws(getHiddenValue({}), errMessageIndex);
assert.throws(getHiddenValue({}, null), errMessageIndex);
assert.throws(getHiddenValue({}, []), errMessageIndex);
assert.deepStrictEqual(
binding.getHiddenValue({}, kArrowMessagePrivateSymbolIndex),
getHiddenValue({}, kArrowMessagePrivateSymbolIndex),
undefined);

assert.throws(setHiddenValue(), errMessageObj);
assert.throws(setHiddenValue(null, 'foo'), errMessageObj);
assert.throws(setHiddenValue(undefined, 'foo'), errMessageObj);
assert.throws(setHiddenValue('bar', 'foo'), errMessageObj);
assert.throws(setHiddenValue(85, 'foo'), errMessageObj);
assert.throws(setHiddenValue({}), errMessageIndex);
assert.throws(setHiddenValue({}, null), errMessageIndex);
assert.throws(setHiddenValue({}, []), errMessageIndex);
const obj = {};
assert.strictEqual(
binding.setHiddenValue(obj, kArrowMessagePrivateSymbolIndex, 'bar'),
setHiddenValue(obj, kArrowMessagePrivateSymbolIndex, 'bar'),
true);
assert.strictEqual(
binding.getHiddenValue(obj, kArrowMessagePrivateSymbolIndex),
getHiddenValue(obj, kArrowMessagePrivateSymbolIndex),
'bar');

let arrowMessage;
Expand All @@ -57,7 +29,7 @@ try {
require(fixtures.path('syntax', 'bad_syntax'));
} catch (err) {
arrowMessage =
binding.getHiddenValue(err, kArrowMessagePrivateSymbolIndex);
getHiddenValue(err, kArrowMessagePrivateSymbolIndex);
}

assert(/bad_syntax\.js:1/.test(arrowMessage));

0 comments on commit 7925adc

Please sign in to comment.