From 277d9da876816697ae80942e19a7b73a5047bdf0 Mon Sep 17 00:00:00 2001 From: Deokjin Kim Date: Sat, 14 Jan 2023 09:21:54 +0900 Subject: [PATCH] vm: refactor to use validate function Throwing error after checking type is repeated. So replace it with validate function. PR-URL: https://github.com/nodejs/node/pull/46176 Reviewed-By: Mohammed Keyvanzadeh Reviewed-By: Antoine du Hamel Reviewed-By: James M Snell --- lib/internal/vm/module.js | 23 +++++++---------------- 1 file changed, 7 insertions(+), 16 deletions(-) diff --git a/lib/internal/vm/module.js b/lib/internal/vm/module.js index 7e0131c7db2872..7c0890c967ebc6 100644 --- a/lib/internal/vm/module.js +++ b/lib/internal/vm/module.js @@ -21,7 +21,6 @@ const { const { isContext } = internalBinding('contextify'); const { isModuleNamespaceObject, - isArrayBufferView, } = require('internal/util/types'); const { customInspectSymbol, @@ -41,6 +40,7 @@ const { } = require('internal/errors').codes; const { validateBoolean, + validateBuffer, validateFunction, validateInt32, validateObject, @@ -276,25 +276,16 @@ class SourceTextModule extends Module { validateInt32(lineOffset, 'options.lineOffset'); validateInt32(columnOffset, 'options.columnOffset'); - if (initializeImportMeta !== undefined && - typeof initializeImportMeta !== 'function') { - throw new ERR_INVALID_ARG_TYPE( - 'options.initializeImportMeta', 'function', initializeImportMeta); + if (initializeImportMeta !== undefined) { + validateFunction(initializeImportMeta, 'options.initializeImportMeta'); } - if (importModuleDynamically !== undefined && - typeof importModuleDynamically !== 'function') { - throw new ERR_INVALID_ARG_TYPE( - 'options.importModuleDynamically', 'function', - importModuleDynamically); + if (importModuleDynamically !== undefined) { + validateFunction(importModuleDynamically, 'options.importModuleDynamically'); } - if (cachedData !== undefined && !isArrayBufferView(cachedData)) { - throw new ERR_INVALID_ARG_TYPE( - 'options.cachedData', - ['Buffer', 'TypedArray', 'DataView'], - cachedData - ); + if (cachedData !== undefined) { + validateBuffer(cachedData, 'options.cachedData'); } super({