Repository navigation
Why does the global process object have its prototype replaced? #14699
Description
Activity
- addedprocessIssues and PRs related to the process subsystem.Issues and PRs related to the process subsystem.questionIssues asking questions about Node.js.Issues asking questions about Node.js.
on Aug 8, 2017 Just so we are on the same page, you are essentially asking for this change?
diff --git a/lib/internal/bootstrap_node.js b/lib/internal/bootstrap_node.js index cf517cdcf2..efdb63b5e9 100644 --- a/lib/internal/bootstrap_node.js +++ b/lib/internal/bootstrap_node.js @@ -13,10 +13,8 @@ const EventEmitter = NativeModule.require('events'); process._eventsCount = 0; - const origProcProto = Object.getPrototypeOf(process); - Object.setPrototypeOf(process, Object.create(EventEmitter.prototype, { - constructor: Object.getOwnPropertyDescriptor(origProcProto, 'constructor') - })); + Object.setPrototypeOf(Object.getPrototypeOf(process).constructor.prototype, + EventEmitter.prototype); EventEmitter.call(process);
To be honest, I don't know why we do what we do.
process.constructor.name === 'process'andObject.prototype.toString.call(process) === '[object process]'either way.Even simpler than that end result:
Object.setPrototypeOf(Object.getPrototypeOf(process), EventEmitter.prototype)Unless there is some underlying reason, then this would seem to be more intuitive.
Do note that just because
process.constructor.name === 'process'that has no relation toprocess.toString() === '[object process]'; if you left off theconstructormethod on the copy that is currently created it would still hold true because of how theprocessis configured in native code. The way that node-chakracore is shimming the v8 approach is actually what I am trying to improve here.@MSLaguana I dug a little and he're the story:
It was made to inherit EE but be considered of classprocess
6d70a4a
Then is was decided to just copy thectorproperty instead of just the value
e0bc5a7if you can make the tests pass, you can PR a change, and IMHO it'll get the proper discussion.
Thanks @refack, I'll give it a go. Just wanted to make sure there wasn't some subtle reason that I was missing.
- added a commit that references this issue
on Sep 10, 2017 - added a commit that references this issue
on Jul 27, 2026
I have been investigating some behaviors of node-chakracore, and I found some confusing code around the
processobject. In https://github.com/nodejs/node/blob/master/lib/internal/bootstrap_node.js#L17 the globalprocessobject instance has its__proto__replaced with a fresh object that in turn has__proto__ofEventEmitter.prototype. That is,process.__proto__ !== process.__proto__.constructor.prototypeandprocess.__proto__.__proto__ === EventEmitter.prototype.What is the reason behind this, and why not simply set
process.__proto__.constructor.prototype.__proto__to beEventEmitter.prototype, preferably in the native code whereprocess's constructor function is defined (although I haven't checked the order of instantiation ofEventEmitterandprocessyet)?The reason that I care about this is it is currently blocking an improvement in node-chakracore regarding how some objects
toString()to something like[object process]rather than[object Object]. That improvement works for all cases except for the globalprocessinstance because it works by adding aSymbol.toStringTagto the object prototype, which is explicitly overridden forprocess.