From e8361287030fbaa773761bb3798d45903bb160f6 Mon Sep 17 00:00:00 2001 From: Michaƫl Zasso Date: Sun, 15 Apr 2018 11:16:50 +0200 Subject: lib: introduce internal/validators Create a file to centralize argument validators that are used in multiple internal modules. Move validateInt32 and validateUint32 to this file. PR-URL: https://github.com/nodejs/node/pull/19973 Reviewed-By: Joyee Cheung Reviewed-By: Ruben Bridgewater Reviewed-By: James M Snell --- lib/vm.js | 24 ++++++------------------ 1 file changed, 6 insertions(+), 18 deletions(-) (limited to 'lib/vm.js') diff --git a/lib/vm.js b/lib/vm.js index 5a5130d7c9..3ab50c8158 100644 --- a/lib/vm.js +++ b/lib/vm.js @@ -28,11 +28,9 @@ const { isContext: _isContext, } = process.binding('contextify'); -const { - ERR_INVALID_ARG_TYPE, - ERR_OUT_OF_RANGE -} = require('internal/errors').codes; +const { ERR_INVALID_ARG_TYPE } = require('internal/errors').codes; const { isUint8Array } = require('internal/util/types'); +const { validateInt32, validateUint32 } = require('internal/validators'); class Script extends ContextifyScript { constructor(code, options = {}) { @@ -56,8 +54,8 @@ class Script extends ContextifyScript { if (typeof filename !== 'string') { throw new ERR_INVALID_ARG_TYPE('options.filename', 'string', filename); } - validateInteger(lineOffset, 'options.lineOffset'); - validateInteger(columnOffset, 'options.columnOffset'); + validateInt32(lineOffset, 'options.lineOffset'); + validateInt32(columnOffset, 'options.columnOffset'); if (cachedData !== undefined && !isUint8Array(cachedData)) { throw new ERR_INVALID_ARG_TYPE('options.cachedData', ['Buffer', 'Uint8Array'], cachedData); @@ -119,15 +117,6 @@ function validateContext(sandbox) { } } -function validateInteger(prop, propName) { - if (!Number.isInteger(prop)) { - throw new ERR_INVALID_ARG_TYPE(propName, 'integer', prop); - } - if ((prop >> 0) !== prop) { - throw new ERR_OUT_OF_RANGE(propName, '32-bit integer', prop); - } -} - function validateString(prop, propName) { if (prop !== undefined && typeof prop !== 'string') throw new ERR_INVALID_ARG_TYPE(propName, 'string', prop); @@ -151,9 +140,8 @@ function getRunInContextArgs(options = {}) { let timeout = options.timeout; if (timeout === undefined) { timeout = -1; - } else if (!Number.isInteger(timeout) || timeout <= 0) { - throw new ERR_INVALID_ARG_TYPE('options.timeout', 'a positive integer', - timeout); + } else { + validateUint32(timeout, 'options.timeout', true); } const { -- cgit v1.2.3