Skip to content

Commit 49b2969

Browse files
dustinnewmantargos
authored andcommitted
vm: migrate isContext to internal/errors
PR-URL: #19268 Refs: #18106 Reviewed-By: Tiancheng "Timothy" Gu <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Gus Caplan <[email protected]> Reviewed-By: Jon Moss <[email protected]> Reviewed-By: Joyee Cheung <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Yuta Hiroto <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]>
1 parent 4e1f090 commit 49b2969

File tree

5 files changed

+50
-17
lines changed

5 files changed

+50
-17
lines changed

lib/vm.js

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,13 @@ const {
2626
kParsingContext,
2727

2828
makeContext,
29-
isContext,
29+
isContext: _isContext,
3030
} = process.binding('contextify');
3131

32-
const { ERR_INVALID_ARG_TYPE } = require('internal/errors').codes;
32+
const {
33+
ERR_INVALID_ARG_TYPE,
34+
ERR_MISSING_ARGS
35+
} = require('internal/errors').codes;
3336

3437
// The binding provides a few useful primitives:
3538
// - Script(code, { filename = "evalmachine.anonymous",
@@ -119,6 +122,19 @@ function getContextOptions(options) {
119122
return {};
120123
}
121124

125+
function isContext(sandbox) {
126+
if (arguments.length < 1) {
127+
throw new ERR_MISSING_ARGS('sandbox');
128+
}
129+
130+
if (typeof sandbox !== 'object' && typeof sandbox !== 'function' ||
131+
sandbox === null) {
132+
throw new ERR_INVALID_ARG_TYPE('sandbox', 'object', sandbox);
133+
}
134+
135+
return _isContext(sandbox);
136+
}
137+
122138
let defaultContextNameIndex = 1;
123139
function createContext(sandbox, options) {
124140
if (sandbox === undefined) {

src/node_contextify.cc

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -276,10 +276,8 @@ void ContextifyContext::MakeContext(const FunctionCallbackInfo<Value>& args) {
276276
void ContextifyContext::IsContext(const FunctionCallbackInfo<Value>& args) {
277277
Environment* env = Environment::GetCurrent(args);
278278

279-
if (!args[0]->IsObject()) {
280-
env->ThrowTypeError("sandbox must be an object");
281-
return;
282-
}
279+
CHECK(args[0]->IsObject());
280+
283281
Local<Object> sandbox = args[0].As<Object>();
284282

285283
Maybe<bool> result =

test/parallel/test-vm-context.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
// USE OR OTHER DEALINGS IN THE SOFTWARE.
2121

2222
'use strict';
23-
require('../common');
23+
const common = require('../common');
2424
const assert = require('assert');
2525

2626
const vm = require('vm');
@@ -44,9 +44,12 @@ assert.strictEqual(3, context.foo);
4444
assert.strictEqual('lala', context.thing);
4545

4646
// Issue GH-227:
47-
assert.throws(() => {
47+
common.expectsError(() => {
4848
vm.runInNewContext('', null, 'some.js');
49-
}, /^TypeError: sandbox must be an object$/);
49+
}, {
50+
code: 'ERR_INVALID_ARG_TYPE',
51+
type: TypeError
52+
});
5053

5154
// Issue GH-1140:
5255
// Test runInContext signature

test/parallel/test-vm-create-context-arg.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,15 @@
2020
// USE OR OTHER DEALINGS IN THE SOFTWARE.
2121

2222
'use strict';
23-
require('../common');
24-
const assert = require('assert');
23+
const common = require('../common');
2524
const vm = require('vm');
2625

27-
assert.throws(function() {
26+
common.expectsError(() => {
2827
vm.createContext('string is not supported');
29-
}, /^TypeError: sandbox must be an object$/);
28+
}, {
29+
code: 'ERR_INVALID_ARG_TYPE',
30+
type: TypeError
31+
});
3032

3133
// Should not throw.
3234
vm.createContext({ a: 1 });

test/parallel/test-vm-is-context.js

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,27 @@
2020
// USE OR OTHER DEALINGS IN THE SOFTWARE.
2121

2222
'use strict';
23-
require('../common');
23+
const common = require('../common');
2424
const assert = require('assert');
2525
const vm = require('vm');
2626

27-
assert.throws(function() {
28-
vm.isContext('string is not supported');
29-
}, /^TypeError: sandbox must be an object$/);
27+
for (const valToTest of [
28+
'string', null, undefined, 8.9, Symbol('sym'), true
29+
]) {
30+
common.expectsError(() => {
31+
vm.isContext(valToTest);
32+
}, {
33+
code: 'ERR_INVALID_ARG_TYPE',
34+
type: TypeError
35+
});
36+
}
37+
38+
common.expectsError(() => {
39+
vm.isContext();
40+
}, {
41+
code: 'ERR_MISSING_ARGS',
42+
type: TypeError
43+
});
3044

3145
assert.strictEqual(vm.isContext({}), false);
3246
assert.strictEqual(vm.isContext([]), false);

0 commit comments

Comments
 (0)