-
Notifications
You must be signed in to change notification settings - Fork 29.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Why does the global process object have its prototype replaced? #14699
Comments
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. |
Even simpler than that end result: Unless there is some underlying reason, then this would seem to be more intuitive. Do note that just because |
@MSLaguana I dug a little and he're the story: if 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. |
The global `process` object had its prototype replaced with a fresh object that had `EventEmitter.prototype` as its prototype. With this change, the original `process.constructor.prototype` is modified to have `EventEmitter.prototype` as its prototype, reflecting that `process` objects are also `EventEmitter`s. Fixes: nodejs#14699
The global `process` object had its prototype replaced with a fresh object that had `EventEmitter.prototype` as its prototype. With this change, the original `process.constructor.prototype` is modified to have `EventEmitter.prototype` as its prototype, reflecting that `process` objects are also `EventEmitter`s. Fixes: #14699 PR-URL: #14715 Reviewed-By: Anna Henningsen <anna@addaleax.net>
I have been investigating some behaviors of node-chakracore, and I found some confusing code around the
process
object. In https://github.com/nodejs/node/blob/master/lib/internal/bootstrap_node.js#L17 the globalprocess
object instance has its__proto__
replaced with a fresh object that in turn has__proto__
ofEventEmitter.prototype
. That is,process.__proto__ !== process.__proto__.constructor.prototype
andprocess.__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 ofEventEmitter
andprocess
yet)?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 globalprocess
instance because it works by adding aSymbol.toStringTag
to the object prototype, which is explicitly overridden forprocess
.The text was updated successfully, but these errors were encountered: