Skip to content

Commit ef68349

Browse files
joyeecheungBethGriggs
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 d4b1666 commit ef68349

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
@@ -209,7 +209,7 @@
209209

210210
perf.markMilestone(NODE_PERFORMANCE_MILESTONE_BOOTSTRAP_COMPLETE);
211211

212-
setupAllowedFlags();
212+
perThreadSetup.setupAllowedFlags();
213213

214214
// There are various modes that Node can run in. The most common two
215215
// are running from a script and running the REPL - but there are a few
@@ -617,128 +617,5 @@
617617
newvm.Script(source,{displayErrors: true, filename });
618618
}
619619

620-
functionsetupAllowedFlags(){
621-
// This builds process.allowedNodeEnvironmentFlags
622-
// from data in the config binding
623-
624-
constreplaceUnderscoresRegex=/_/g;
625-
constleadingDashesRegex=/^--?/;
626-
consttrailingValuesRegex=/=.*$/;
627-
628-
// Save references so user code does not interfere
629-
constreplace=Function.call.bind(String.prototype.replace);
630-
consthas=Function.call.bind(Set.prototype.has);
631-
consttest=Function.call.bind(RegExp.prototype.test);
632-
633-
constget=()=>{
634-
const{
635-
envSettings: { kAllowedInEnvironment }
636-
}=internalBinding('options');
637-
const{ options, aliases }=NativeModule.require('internal/options');
638-
639-
constallowedNodeEnvironmentFlags=[];
640-
for(const[name,info]ofoptions){
641-
if(info.envVarSettings===kAllowedInEnvironment){
642-
allowedNodeEnvironmentFlags.push(name);
643-
}
644-
}
645-
646-
for(const[from,expansion]ofaliases){
647-
letisAccepted=true;
648-
for(consttoofexpansion){
649-
if(!to.startsWith('-')||to==='--')continue;
650-
constrecursiveExpansion=aliases.get(to);
651-
if(recursiveExpansion){
652-
if(recursiveExpansion[0]===to)
653-
recursiveExpansion.splice(0,1);
654-
expansion.push(...recursiveExpansion);
655-
continue;
656-
}
657-
isAccepted=options.get(to).envVarSettings===kAllowedInEnvironment;
658-
if(!isAccepted)break;
659-
}
660-
if(isAccepted){
661-
letcanonical=from;
662-
if(canonical.endsWith('='))
663-
canonical=canonical.substr(0,canonical.length-1);
664-
if(canonical.endsWith(' <arg>'))
665-
canonical=canonical.substr(0,canonical.length-4);
666-
allowedNodeEnvironmentFlags.push(canonical);
667-
}
668-
}
669-
670-
consttrimLeadingDashes=(flag)=>replace(flag,leadingDashesRegex,'');
671-
672-
// Save these for comparison against flags provided to
673-
// process.allowedNodeEnvironmentFlags.has() which lack leading dashes.
674-
// Avoid interference w/ user code by flattening `Set.prototype` into
675-
// each object.
676-
constnodeFlags=Object.defineProperties(
677-
newSet(allowedNodeEnvironmentFlags.map(trimLeadingDashes)),
678-
Object.getOwnPropertyDescriptors(Set.prototype)
679-
);
680-
681-
classNodeEnvironmentFlagsSetextendsSet{
682-
constructor(...args){
683-
super(...args);
684-
685-
// the super constructor consumes `add`, but
686-
// disallow any future adds.
687-
this.add=()=>this;
688-
}
689-
690-
delete(){
691-
// noop, `Set` API compatible
692-
returnfalse;
693-
}
694-
695-
clear(){
696-
// noop
697-
}
698-
699-
has(key){
700-
// This will return `true` based on various possible
701-
// permutations of a flag, including present/missing leading
702-
// dash(es) and/or underscores-for-dashes.
703-
// Strips any values after `=`, inclusive.
704-
// TODO(addaleax): It might be more flexible to run the option parser
705-
// on a dummy option set and see whether it rejects the argument or
706-
// not.
707-
if(typeofkey==='string'){
708-
key=replace(key,replaceUnderscoresRegex,'-');
709-
if(test(leadingDashesRegex,key)){
710-
key=replace(key,trailingValuesRegex,'');
711-
returnhas(this,key);
712-
}
713-
returnhas(nodeFlags,key);
714-
}
715-
returnfalse;
716-
}
717-
}
718-
719-
Object.freeze(NodeEnvironmentFlagsSet.prototype.constructor);
720-
Object.freeze(NodeEnvironmentFlagsSet.prototype);
721-
722-
returnprocess.allowedNodeEnvironmentFlags=Object.freeze(
723-
newNodeEnvironmentFlagsSet(
724-
allowedNodeEnvironmentFlags
725-
));
726-
};
727-
728-
Object.defineProperty(process,'allowedNodeEnvironmentFlags',{
729-
get,
730-
set(value){
731-
Object.defineProperty(this,'allowedNodeEnvironmentFlags',{
732-
value,
733-
configurable: true,
734-
enumerable: true,
735-
writable: true
736-
});
737-
},
738-
enumerable: true,
739-
configurable: true
740-
});
741-
}
742-
743620
startup();
744621
});

‎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)