Skip to content

Commit

Permalink
vm: migrate isContext to internal/errors
Browse files Browse the repository at this point in the history
PR-URL: #19268
Refs: #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>
  • Loading branch information
dustinnewman authored and targos committed Mar 22, 2018
1 parent 4e1f090 commit 49b2969
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 17 deletions.
20 changes: 18 additions & 2 deletions lib/vm.js
Expand Up @@ -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",
Expand Down Expand Up @@ -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) {
Expand Down
6 changes: 2 additions & 4 deletions src/node_contextify.cc
Expand Up @@ -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 =
Expand Down
9 changes: 6 additions & 3 deletions test/parallel/test-vm-context.js
Expand Up @@ -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');
Expand All @@ -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
Expand Down
10 changes: 6 additions & 4 deletions test/parallel/test-vm-create-context-arg.js
Expand Up @@ -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 });
Expand Down
22 changes: 18 additions & 4 deletions test/parallel/test-vm-is-context.js
Expand Up @@ -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);
Expand Down

0 comments on commit 49b2969

Please sign in to comment.