Skip to content

Commit d77cf92

Browse files
joyeecheungBridgeAR
authored andcommitted
lib: move setupAllowedFlags() into per_thread.js
Because most of its code (the getter) has nothing to do with the actual bootstrapping and it is run per-thread. PR-URL: #24704 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Christopher Hiller <boneskull@boneskull.com>
1 parent 0894899 commit d77cf92

2 files changed

Lines changed: 126 additions & 124 deletions

File tree

‎lib/internal/bootstrap/node.js‎

Lines changed: 1 addition & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@
223223

224224
perf.markMilestone(NODE_PERFORMANCE_MILESTONE_BOOTSTRAP_COMPLETE);
225225

226-
setupAllowedFlags();
226+
perThreadSetup.setupAllowedFlags();
227227

228228
startExecution();
229229
}
@@ -739,128 +739,5 @@
739739
newvm.Script(source,{displayErrors: true, filename });
740740
}
741741

742-
functionsetupAllowedFlags(){
743-
// This builds process.allowedNodeEnvironmentFlags
744-
// from data in the config binding
745-
746-
constreplaceUnderscoresRegex=/_/g;
747-
constleadingDashesRegex=/^--?/;
748-
consttrailingValuesRegex=/=.*$/;
749-
750-
// Save references so user code does not interfere
751-
constreplace=Function.call.bind(String.prototype.replace);
752-
consthas=Function.call.bind(Set.prototype.has);
753-
consttest=Function.call.bind(RegExp.prototype.test);
754-
755-
constget=()=>{
756-
const{
757-
envSettings: { kAllowedInEnvironment }
758-
}=internalBinding('options');
759-
const{ options, aliases }=NativeModule.require('internal/options');
760-
761-
constallowedNodeEnvironmentFlags=[];
762-
for(const[name,info]ofoptions){
763-
if(info.envVarSettings===kAllowedInEnvironment){
764-
allowedNodeEnvironmentFlags.push(name);
765-
}
766-
}
767-
768-
for(const[from,expansion]ofaliases){
769-
letisAccepted=true;
770-
for(consttoofexpansion){
771-
if(!to.startsWith('-')||to==='--')continue;
772-
constrecursiveExpansion=aliases.get(to);
773-
if(recursiveExpansion){
774-
if(recursiveExpansion[0]===to)
775-
recursiveExpansion.splice(0,1);
776-
expansion.push(...recursiveExpansion);
777-
continue;
778-
}
779-
isAccepted=options.get(to).envVarSettings===kAllowedInEnvironment;
780-
if(!isAccepted)break;
781-
}
782-
if(isAccepted){
783-
letcanonical=from;
784-
if(canonical.endsWith('='))
785-
canonical=canonical.substr(0,canonical.length-1);
786-
if(canonical.endsWith(' <arg>'))
787-
canonical=canonical.substr(0,canonical.length-4);
788-
allowedNodeEnvironmentFlags.push(canonical);
789-
}
790-
}
791-
792-
consttrimLeadingDashes=(flag)=>replace(flag,leadingDashesRegex,'');
793-
794-
// Save these for comparison against flags provided to
795-
// process.allowedNodeEnvironmentFlags.has() which lack leading dashes.
796-
// Avoid interference w/ user code by flattening `Set.prototype` into
797-
// each object.
798-
constnodeFlags=Object.defineProperties(
799-
newSet(allowedNodeEnvironmentFlags.map(trimLeadingDashes)),
800-
Object.getOwnPropertyDescriptors(Set.prototype)
801-
);
802-
803-
classNodeEnvironmentFlagsSetextendsSet{
804-
constructor(...args){
805-
super(...args);
806-
807-
// the super constructor consumes `add`, but
808-
// disallow any future adds.
809-
this.add=()=>this;
810-
}
811-
812-
delete(){
813-
// noop, `Set` API compatible
814-
returnfalse;
815-
}
816-
817-
clear(){
818-
// noop
819-
}
820-
821-
has(key){
822-
// This will return `true` based on various possible
823-
// permutations of a flag, including present/missing leading
824-
// dash(es) and/or underscores-for-dashes.
825-
// Strips any values after `=`, inclusive.
826-
// TODO(addaleax): It might be more flexible to run the option parser
827-
// on a dummy option set and see whether it rejects the argument or
828-
// not.
829-
if(typeofkey==='string'){
830-
key=replace(key,replaceUnderscoresRegex,'-');
831-
if(test(leadingDashesRegex,key)){
832-
key=replace(key,trailingValuesRegex,'');
833-
returnhas(this,key);
834-
}
835-
returnhas(nodeFlags,key);
836-
}
837-
returnfalse;
838-
}
839-
}
840-
841-
Object.freeze(NodeEnvironmentFlagsSet.prototype.constructor);
842-
Object.freeze(NodeEnvironmentFlagsSet.prototype);
843-
844-
returnprocess.allowedNodeEnvironmentFlags=Object.freeze(
845-
newNodeEnvironmentFlagsSet(
846-
allowedNodeEnvironmentFlags
847-
));
848-
};
849-
850-
Object.defineProperty(process,'allowedNodeEnvironmentFlags',{
851-
get,
852-
set(value){
853-
Object.defineProperty(this,'allowedNodeEnvironmentFlags',{
854-
value,
855-
configurable: true,
856-
enumerable: true,
857-
writable: true
858-
});
859-
},
860-
enumerable: true,
861-
configurable: true
862-
});
863-
}
864-
865742
startup();
866743
});

‎lib/internal/process/per_thread.js‎

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,132 @@ function setupUncaughtExceptionCapture(exceptionHandlerState,
234234
};
235235
}
236236

237+
constreplaceUnderscoresRegex=/_/g;
238+
constleadingDashesRegex=/^--?/;
239+
consttrailingValuesRegex=/=.*$/;
240+
241+
// Save references so user code does not interfere
242+
constreplace=Function.call.bind(String.prototype.replace);
243+
consthas=Function.call.bind(Set.prototype.has);
244+
consttest=Function.call.bind(RegExp.prototype.test);
245+
246+
// This builds the initial process.allowedNodeEnvironmentFlags
247+
// from data in the config binding.
248+
functionbuildAllowedFlags(){
249+
const{
250+
envSettings: { kAllowedInEnvironment }
251+
}=internalBinding('options');
252+
const{ options, aliases }=require('internal/options');
253+
254+
constallowedNodeEnvironmentFlags=[];
255+
for(const[name,info]ofoptions){
256+
if(info.envVarSettings===kAllowedInEnvironment){
257+
allowedNodeEnvironmentFlags.push(name);
258+
}
259+
}
260+
261+
for(const[from,expansion]ofaliases){
262+
letisAccepted=true;
263+
for(consttoofexpansion){
264+
if(!to.startsWith('-')||to==='--')continue;
265+
constrecursiveExpansion=aliases.get(to);
266+
if(recursiveExpansion){
267+
if(recursiveExpansion[0]===to)
268+
recursiveExpansion.splice(0,1);
269+
expansion.push(...recursiveExpansion);
270+
continue;
271+
}
272+
isAccepted=options.get(to).envVarSettings===kAllowedInEnvironment;
273+
if(!isAccepted)break;
274+
}
275+
if(isAccepted){
276+
letcanonical=from;
277+
if(canonical.endsWith('='))
278+
canonical=canonical.substr(0,canonical.length-1);
279+
if(canonical.endsWith(' <arg>'))
280+
canonical=canonical.substr(0,canonical.length-4);
281+
allowedNodeEnvironmentFlags.push(canonical);
282+
}
283+
}
284+
285+
consttrimLeadingDashes=(flag)=>replace(flag,leadingDashesRegex,'');
286+
287+
// Save these for comparison against flags provided to
288+
// process.allowedNodeEnvironmentFlags.has() which lack leading dashes.
289+
// Avoid interference w/ user code by flattening `Set.prototype` into
290+
// each object.
291+
constnodeFlags=Object.defineProperties(
292+
newSet(allowedNodeEnvironmentFlags.map(trimLeadingDashes)),
293+
Object.getOwnPropertyDescriptors(Set.prototype)
294+
);
295+
296+
classNodeEnvironmentFlagsSetextendsSet{
297+
constructor(...args){
298+
super(...args);
299+
300+
// the super constructor consumes `add`, but
301+
// disallow any future adds.
302+
this.add=()=>this;
303+
}
304+
305+
delete(){
306+
// noop, `Set` API compatible
307+
returnfalse;
308+
}
309+
310+
clear(){
311+
// noop
312+
}
313+
314+
has(key){
315+
// This will return `true` based on various possible
316+
// permutations of a flag, including present/missing leading
317+
// dash(es) and/or underscores-for-dashes.
318+
// Strips any values after `=`, inclusive.
319+
// TODO(addaleax): It might be more flexible to run the option parser
320+
// on a dummy option set and see whether it rejects the argument or
321+
// not.
322+
if(typeofkey==='string'){
323+
key=replace(key,replaceUnderscoresRegex,'-');
324+
if(test(leadingDashesRegex,key)){
325+
key=replace(key,trailingValuesRegex,'');
326+
returnhas(this,key);
327+
}
328+
returnhas(nodeFlags,key);
329+
}
330+
returnfalse;
331+
}
332+
}
333+
334+
Object.freeze(NodeEnvironmentFlagsSet.prototype.constructor);
335+
Object.freeze(NodeEnvironmentFlagsSet.prototype);
336+
337+
returnprocess.allowedNodeEnvironmentFlags=Object.freeze(
338+
newNodeEnvironmentFlagsSet(
339+
allowedNodeEnvironmentFlags
340+
));
341+
}
342+
343+
functionsetupAllowedFlags(){
344+
Object.defineProperty(process,'allowedNodeEnvironmentFlags',{
345+
get: buildAllowedFlags,
346+
set(value){
347+
// If the user tries to set this to another value, override
348+
// this completely to that value.
349+
Object.defineProperty(this,'allowedNodeEnvironmentFlags',{
350+
value,
351+
configurable: true,
352+
enumerable: true,
353+
writable: true
354+
});
355+
},
356+
enumerable: true,
357+
configurable: true
358+
});
359+
}
360+
237361
module.exports={
362+
setupAllowedFlags,
238363
setupAssert,
239364
setupCpuUsage,
240365
setupHrtime,

0 commit comments

Comments
 (0)