Skip to content

Commit 3d20190

Browse files
committed
src: remove throws in set/getHiddenValue
These are internal only utility functions, CHECK instead of throw PR-URL: #16544 Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Anatoli Papirovski <apapirovski@mac.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
1 parent 841e305 commit 3d20190

File tree

4 files changed

+28
-56
lines changed

4 files changed

+28
-56
lines changed

lib/internal/util.js

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
'use strict';
22

33
const errors = require('internal/errors');
4-
const binding = process.binding('util');
54
const { signals } = process.binding('constants').os;
65

7-
const { createPromise, promiseResolve, promiseReject } = binding;
6+
const {
7+
createPromise,
8+
getHiddenValue,
9+
promiseResolve,
10+
promiseReject,
11+
setHiddenValue,
12+
arrow_message_private_symbol: kArrowMessagePrivateSymbolIndex,
13+
decorated_private_symbol: kDecoratedPrivateSymbolIndex
14+
} = process.binding('util');
815

9-
const kArrowMessagePrivateSymbolIndex = binding['arrow_message_private_symbol'];
10-
const kDecoratedPrivateSymbolIndex = binding['decorated_private_symbol'];
1116
const noCrypto = !process.versions.openssl;
1217

1318
function isError(e) {
@@ -66,14 +71,14 @@ function deprecate(fn, msg, code) {
6671

6772
function decorateErrorStack(err) {
6873
if (!(isError(err) && err.stack) ||
69-
binding.getHiddenValue(err, kDecoratedPrivateSymbolIndex) === true)
74+
getHiddenValue(err, kDecoratedPrivateSymbolIndex) === true)
7075
return;
7176

72-
const arrow = binding.getHiddenValue(err, kArrowMessagePrivateSymbolIndex);
77+
const arrow = getHiddenValue(err, kArrowMessagePrivateSymbolIndex);
7378

7479
if (arrow) {
7580
err.stack = arrow + err.stack;
76-
binding.setHiddenValue(err, kDecoratedPrivateSymbolIndex, true);
81+
setHiddenValue(err, kDecoratedPrivateSymbolIndex, true);
7782
}
7883
}
7984

lib/repl.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,8 @@ function REPLServer(prompt,
289289
const top = replMap.get(self);
290290
const pstrace = Error.prepareStackTrace;
291291
Error.prepareStackTrace = prepareStackTrace(pstrace);
292-
internalUtil.decorateErrorStack(e);
292+
if (typeof e === 'object')
293+
internalUtil.decorateErrorStack(e);
293294
Error.prepareStackTrace = pstrace;
294295
const isError = internalUtil.isError(e);
295296
if (e instanceof SyntaxError && e.stack) {

src/node_util.cc

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -101,11 +101,8 @@ inline Local<Private> IndexToPrivateSymbol(Environment* env, uint32_t index) {
101101
static void GetHiddenValue(const FunctionCallbackInfo<Value>& args) {
102102
Environment* env = Environment::GetCurrent(args);
103103

104-
if (!args[0]->IsObject())
105-
return env->ThrowTypeError("obj must be an object");
106-
107-
if (!args[1]->IsUint32())
108-
return env->ThrowTypeError("index must be an uint32");
104+
CHECK(args[0]->IsObject());
105+
CHECK(args[1]->IsUint32());
109106

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

121-
if (!args[0]->IsObject())
122-
return env->ThrowTypeError("obj must be an object");
123-
124-
if (!args[1]->IsUint32())
125-
return env->ThrowTypeError("index must be an uint32");
118+
CHECK(args[0]->IsObject());
119+
CHECK(args[1]->IsUint32());
126120

127121
Local<Object> obj = args[0].As<Object>();
128122
auto index = args[1]->Uint32Value(env->context()).FromJust();

test/parallel/test-util-internal.js

Lines changed: 10 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -5,50 +5,22 @@ require('../common');
55
const assert = require('assert');
66
const fixtures = require('../common/fixtures');
77

8-
const binding = process.binding('util');
9-
const kArrowMessagePrivateSymbolIndex = binding['arrow_message_private_symbol'];
8+
const {
9+
getHiddenValue,
10+
setHiddenValue,
11+
arrow_message_private_symbol: kArrowMessagePrivateSymbolIndex
12+
} = process.binding('util');
1013

11-
function getHiddenValue(obj, index) {
12-
return function() {
13-
binding.getHiddenValue(obj, index);
14-
};
15-
}
16-
17-
function setHiddenValue(obj, index, val) {
18-
return function() {
19-
binding.setHiddenValue(obj, index, val);
20-
};
21-
}
22-
23-
const errMessageObj = /obj must be an object/;
24-
const errMessageIndex = /index must be an uint32/;
25-
26-
assert.throws(getHiddenValue(), errMessageObj);
27-
assert.throws(getHiddenValue(null, 'foo'), errMessageObj);
28-
assert.throws(getHiddenValue(undefined, 'foo'), errMessageObj);
29-
assert.throws(getHiddenValue('bar', 'foo'), errMessageObj);
30-
assert.throws(getHiddenValue(85, 'foo'), errMessageObj);
31-
assert.throws(getHiddenValue({}), errMessageIndex);
32-
assert.throws(getHiddenValue({}, null), errMessageIndex);
33-
assert.throws(getHiddenValue({}, []), errMessageIndex);
34-
assert.deepStrictEqual(
35-
binding.getHiddenValue({}, kArrowMessagePrivateSymbolIndex),
14+
assert.strictEqual(
15+
getHiddenValue({}, kArrowMessagePrivateSymbolIndex),
3616
undefined);
3717

38-
assert.throws(setHiddenValue(), errMessageObj);
39-
assert.throws(setHiddenValue(null, 'foo'), errMessageObj);
40-
assert.throws(setHiddenValue(undefined, 'foo'), errMessageObj);
41-
assert.throws(setHiddenValue('bar', 'foo'), errMessageObj);
42-
assert.throws(setHiddenValue(85, 'foo'), errMessageObj);
43-
assert.throws(setHiddenValue({}), errMessageIndex);
44-
assert.throws(setHiddenValue({}, null), errMessageIndex);
45-
assert.throws(setHiddenValue({}, []), errMessageIndex);
4618
const obj = {};
4719
assert.strictEqual(
48-
binding.setHiddenValue(obj, kArrowMessagePrivateSymbolIndex, 'bar'),
20+
setHiddenValue(obj, kArrowMessagePrivateSymbolIndex, 'bar'),
4921
true);
5022
assert.strictEqual(
51-
binding.getHiddenValue(obj, kArrowMessagePrivateSymbolIndex),
23+
getHiddenValue(obj, kArrowMessagePrivateSymbolIndex),
5224
'bar');
5325

5426
let arrowMessage;
@@ -57,7 +29,7 @@ try {
5729
require(fixtures.path('syntax', 'bad_syntax'));
5830
} catch (err) {
5931
arrowMessage =
60-
binding.getHiddenValue(err, kArrowMessagePrivateSymbolIndex);
32+
getHiddenValue(err, kArrowMessagePrivateSymbolIndex);
6133
}
6234

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

0 commit comments

Comments
 (0)