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

vm: migrate isContext to internal/errors #19268

Closed
wants to merge 1 commit 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
20 changes: 18 additions & 2 deletions lib/vm.js
Original file line number Diff line number Diff line change
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 @@ -97,6 +100,19 @@ function getContextOptions(options) {
return {};
}

function isContext(sandbox) {
if (arguments.length < 1) {
Copy link
Member

Choose a reason for hiding this comment

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

Do we need this one though? If sandbox is undefined, it will show up as a ERR_INVALID_ARG_TYPE as well, it seems rare that someone would just do “isContext()”

throw new ERR_MISSING_ARGS('sandbox');
}

if (typeof sandbox !== 'object' && typeof sandbox !== 'function' ||
Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure we want to accept functions?

Copy link
Member

Choose a reason for hiding this comment

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

I believe that’s just for compatibility: the original ->IsObject() test accepts functions.

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
Original file line number Diff line number Diff line change
Expand Up @@ -252,10 +252,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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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