vm: migrate isContext to internal/errors

PR-URL: https://github.com/nodejs/node/pull/19268
Refs: https://github.com/nodejs/node/issues/18106
Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Gus Caplan <me@gus.host>
Reviewed-By: Jon Moss <me@jonathanmoss.me>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Yuta Hiroto <hello@hiroppy.me>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
This commit is contained in:
dustinnewman98 2018-03-01 22:11:55 -08:00 committed by Michaël Zasso
parent 4e1f0907da
commit 49b2969ef4
No known key found for this signature in database
GPG Key ID: 770F7A9A5AE15600
5 changed files with 50 additions and 17 deletions

View File

@ -26,10 +26,13 @@ const {
kParsingContext,
makeContext,
isContext,
isContext: _isContext,
} = process.binding('contextify');
const { ERR_INVALID_ARG_TYPE } = require('internal/errors').codes;
const {
ERR_INVALID_ARG_TYPE,
ERR_MISSING_ARGS
} = require('internal/errors').codes;
// The binding provides a few useful primitives:
// - Script(code, { filename = "evalmachine.anonymous",
@ -119,6 +122,19 @@ function getContextOptions(options) {
return {};
}
function isContext(sandbox) {
if (arguments.length < 1) {
throw new ERR_MISSING_ARGS('sandbox');
}
if (typeof sandbox !== 'object' && typeof sandbox !== 'function' ||
sandbox === null) {
throw new ERR_INVALID_ARG_TYPE('sandbox', 'object', sandbox);
}
return _isContext(sandbox);
}
let defaultContextNameIndex = 1;
function createContext(sandbox, options) {
if (sandbox === undefined) {

View File

@ -276,10 +276,8 @@ void ContextifyContext::MakeContext(const FunctionCallbackInfo<Value>& args) {
void ContextifyContext::IsContext(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);
if (!args[0]->IsObject()) {
env->ThrowTypeError("sandbox must be an object");
return;
}
CHECK(args[0]->IsObject());
Local<Object> sandbox = args[0].As<Object>();
Maybe<bool> result =

View File

@ -20,7 +20,7 @@
// USE OR OTHER DEALINGS IN THE SOFTWARE.
'use strict';
require('../common');
const common = require('../common');
const assert = require('assert');
const vm = require('vm');
@ -44,9 +44,12 @@ assert.strictEqual(3, context.foo);
assert.strictEqual('lala', context.thing);
// Issue GH-227:
assert.throws(() => {
common.expectsError(() => {
vm.runInNewContext('', null, 'some.js');
}, /^TypeError: sandbox must be an object$/);
}, {
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError
});
// Issue GH-1140:
// Test runInContext signature

View File

@ -20,13 +20,15 @@
// USE OR OTHER DEALINGS IN THE SOFTWARE.
'use strict';
require('../common');
const assert = require('assert');
const common = require('../common');
const vm = require('vm');
assert.throws(function() {
common.expectsError(() => {
vm.createContext('string is not supported');
}, /^TypeError: sandbox must be an object$/);
}, {
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError
});
// Should not throw.
vm.createContext({ a: 1 });

View File

@ -20,13 +20,27 @@
// USE OR OTHER DEALINGS IN THE SOFTWARE.
'use strict';
require('../common');
const common = require('../common');
const assert = require('assert');
const vm = require('vm');
assert.throws(function() {
vm.isContext('string is not supported');
}, /^TypeError: sandbox must be an object$/);
for (const valToTest of [
'string', null, undefined, 8.9, Symbol('sym'), true
]) {
common.expectsError(() => {
vm.isContext(valToTest);
}, {
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError
});
}
common.expectsError(() => {
vm.isContext();
}, {
code: 'ERR_MISSING_ARGS',
type: TypeError
});
assert.strictEqual(vm.isContext({}), false);
assert.strictEqual(vm.isContext([]), false);