@@ -80,22 +80,23 @@ Script.prototype.runInNewContext = function(sandbox, options) {
80
80
return this . runInContext ( context , options ) ;
81
81
} ;
82
82
83
+ function validateString ( prop , propName ) {
84
+ if ( prop !== undefined && typeof prop !== 'string' )
85
+ throw new errors . TypeError ( 'ERR_INVALID_ARG_TYPE' , propName ,
86
+ 'string' , prop ) ;
87
+ }
88
+
83
89
function getContextOptions ( options ) {
84
- const contextOptions = options ? {
85
- name : options . contextName ,
86
- origin : options . contextOrigin
87
- } : { } ;
88
- if ( contextOptions . name !== undefined &&
89
- typeof contextOptions . name !== 'string' ) {
90
- throw new errors . TypeError ( 'ERR_INVALID_ARG_TYPE' , 'options.contextName' ,
91
- 'string' , contextOptions . name ) ;
92
- }
93
- if ( contextOptions . origin !== undefined &&
94
- typeof contextOptions . origin !== 'string' ) {
95
- throw new errors . TypeError ( 'ERR_INVALID_ARG_TYPE' , 'options.contextOrigin' ,
96
- 'string' , contextOptions . origin ) ;
90
+ if ( options ) {
91
+ const contextOptions = {
92
+ name : options . contextName ,
93
+ origin : options . contextOrigin
94
+ } ;
95
+ validateString ( contextOptions . name , 'options.contextName' ) ;
96
+ validateString ( contextOptions . origin , 'options.contextOrigin' ) ;
97
+ return contextOptions ;
97
98
}
98
- return contextOptions ;
99
+ return { } ;
99
100
}
100
101
101
102
let defaultContextNameIndex = 1 ;
@@ -121,10 +122,7 @@ function createContext(sandbox, options) {
121
122
throw new errors . TypeError ( 'ERR_INVALID_ARG_TYPE' , 'options.name' ,
122
123
'string' , options . name ) ;
123
124
}
124
- if ( options . origin !== undefined && typeof options . origin !== 'string' ) {
125
- throw new errors . TypeError ( 'ERR_INVALID_ARG_TYPE' , 'options.origin' ,
126
- 'string' , options . origin ) ;
127
- }
125
+ validateString ( options . origin , 'options.origin' ) ;
128
126
} else {
129
127
options = {
130
128
name : `VM Context ${ defaultContextNameIndex ++ } `
0 commit comments