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:
parent
4e1f0907da
commit
49b2969ef4
20
lib/vm.js
20
lib/vm.js
@ -26,10 +26,13 @@ const {
|
|||||||
kParsingContext,
|
kParsingContext,
|
||||||
|
|
||||||
makeContext,
|
makeContext,
|
||||||
isContext,
|
isContext: _isContext,
|
||||||
} = process.binding('contextify');
|
} = 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:
|
// The binding provides a few useful primitives:
|
||||||
// - Script(code, { filename = "evalmachine.anonymous",
|
// - Script(code, { filename = "evalmachine.anonymous",
|
||||||
@ -119,6 +122,19 @@ function getContextOptions(options) {
|
|||||||
return {};
|
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;
|
let defaultContextNameIndex = 1;
|
||||||
function createContext(sandbox, options) {
|
function createContext(sandbox, options) {
|
||||||
if (sandbox === undefined) {
|
if (sandbox === undefined) {
|
||||||
|
@ -276,10 +276,8 @@ void ContextifyContext::MakeContext(const FunctionCallbackInfo<Value>& args) {
|
|||||||
void ContextifyContext::IsContext(const FunctionCallbackInfo<Value>& args) {
|
void ContextifyContext::IsContext(const FunctionCallbackInfo<Value>& args) {
|
||||||
Environment* env = Environment::GetCurrent(args);
|
Environment* env = Environment::GetCurrent(args);
|
||||||
|
|
||||||
if (!args[0]->IsObject()) {
|
CHECK(args[0]->IsObject());
|
||||||
env->ThrowTypeError("sandbox must be an object");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
Local<Object> sandbox = args[0].As<Object>();
|
Local<Object> sandbox = args[0].As<Object>();
|
||||||
|
|
||||||
Maybe<bool> result =
|
Maybe<bool> result =
|
||||||
|
@ -20,7 +20,7 @@
|
|||||||
// USE OR OTHER DEALINGS IN THE SOFTWARE.
|
// USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
|
|
||||||
'use strict';
|
'use strict';
|
||||||
require('../common');
|
const common = require('../common');
|
||||||
const assert = require('assert');
|
const assert = require('assert');
|
||||||
|
|
||||||
const vm = require('vm');
|
const vm = require('vm');
|
||||||
@ -44,9 +44,12 @@ assert.strictEqual(3, context.foo);
|
|||||||
assert.strictEqual('lala', context.thing);
|
assert.strictEqual('lala', context.thing);
|
||||||
|
|
||||||
// Issue GH-227:
|
// Issue GH-227:
|
||||||
assert.throws(() => {
|
common.expectsError(() => {
|
||||||
vm.runInNewContext('', null, 'some.js');
|
vm.runInNewContext('', null, 'some.js');
|
||||||
}, /^TypeError: sandbox must be an object$/);
|
}, {
|
||||||
|
code: 'ERR_INVALID_ARG_TYPE',
|
||||||
|
type: TypeError
|
||||||
|
});
|
||||||
|
|
||||||
// Issue GH-1140:
|
// Issue GH-1140:
|
||||||
// Test runInContext signature
|
// Test runInContext signature
|
||||||
|
@ -20,13 +20,15 @@
|
|||||||
// USE OR OTHER DEALINGS IN THE SOFTWARE.
|
// USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
|
|
||||||
'use strict';
|
'use strict';
|
||||||
require('../common');
|
const common = require('../common');
|
||||||
const assert = require('assert');
|
|
||||||
const vm = require('vm');
|
const vm = require('vm');
|
||||||
|
|
||||||
assert.throws(function() {
|
common.expectsError(() => {
|
||||||
vm.createContext('string is not supported');
|
vm.createContext('string is not supported');
|
||||||
}, /^TypeError: sandbox must be an object$/);
|
}, {
|
||||||
|
code: 'ERR_INVALID_ARG_TYPE',
|
||||||
|
type: TypeError
|
||||||
|
});
|
||||||
|
|
||||||
// Should not throw.
|
// Should not throw.
|
||||||
vm.createContext({ a: 1 });
|
vm.createContext({ a: 1 });
|
||||||
|
@ -20,13 +20,27 @@
|
|||||||
// USE OR OTHER DEALINGS IN THE SOFTWARE.
|
// USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
|
|
||||||
'use strict';
|
'use strict';
|
||||||
require('../common');
|
const common = require('../common');
|
||||||
const assert = require('assert');
|
const assert = require('assert');
|
||||||
const vm = require('vm');
|
const vm = require('vm');
|
||||||
|
|
||||||
assert.throws(function() {
|
for (const valToTest of [
|
||||||
vm.isContext('string is not supported');
|
'string', null, undefined, 8.9, Symbol('sym'), true
|
||||||
}, /^TypeError: sandbox must be an object$/);
|
]) {
|
||||||
|
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);
|
||||||
assert.strictEqual(vm.isContext([]), false);
|
assert.strictEqual(vm.isContext([]), false);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user