Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

src: remove throws in set/getHiddenValue #16544

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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')
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to protect against the odd random non-object being emitted on the domain error event. If e is not an object, it will trigger the CHECK

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
48 changes: 10 additions & 38 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),
assert.strictEqual(
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));