Details
-
Enhancement
-
Resolution: Fixed
-
P2
-
8
-
None
-
b106
-
generic
-
generic
Description
Although having a mutable __proto__ property doesn't make for a consensus (https://bugzilla.mozilla.org/show_bug.cgi?id=642500) the analysis of node, npm and top 20 most depended upon modules (plus associated dependencies and testing frameworks) shows that the writable __proto__ property is used by JS developers.
The following 5 patterns are quite common and not supported by nashorn.
1) CRITICAL: Update prototype of an Object instance or a function:
- function updateProto(a) { a.__proto__ = aNewProto; }
2) MAJOR: Class inheritency :
- DaughterClass.prototype.__proto__ = MotherClass.prototype;
3) MAJOR: Provide a prototype to an Object instance at creation time:
- var a = { __proto__: aProto, myprop: 9, … };
- var a = new A(); a.__proto__ = aProto;
4) MINOR : Access to __proto__ reference:
- x.__proto__ === y.__proto__
- x.__proto__.constructor
5) MINOR: Provide a prototype to a Function at creation time:
- var f = function() {}; f.__proto__ = aNewProto;
Usage in most depended upon node modules
====================================
1 occurs in 4 modules: proto-list, express, qs, ejs modules
2 occurs in 15 modules: socket-io, commander, connect, connect-redis, engine-io, send, stylus, browser-builtins, supertest, superagent, emitter-component, mocha, jade, node-unit, cli-table modules
3 occurs in 4 modules: traverse, hashish, express, esprima module test dependency modules
4 occurs in 4 modules: traverse module test script, readable-stream module test script, jade, insert-module-globals modules
5 occurs in 1 module: engine.io
Workaround and side effects
======================
1 can't be workarounded. Script needs to be rewritten in order to propagate prototype updates.
2 can be workarounded with Object.create but DaughterClass prototype can't be updated once Object.create has been called. Furthermore, any instance created prior to the extension are not updated.
DaughterClass.prototype = Object.create(MotherClass.prototype);
DaughterClass.prototype.constructor = DaughterClass;
3 can be workarounded with Object.create
Object.create(aProto, { myprop : {enumerable: true, value:9, ...} }
4 can be workarounded with Object.getPrototypeOf
Object.getPrototypeOf(x) === Object.getPrototypeOf(y)
5 can be workarounded with:
for(k in aNewProto) {
f[k] = aNewProto[k];
}
The following 5 patterns are quite common and not supported by nashorn.
1) CRITICAL: Update prototype of an Object instance or a function:
- function updateProto(a) { a.__proto__ = aNewProto; }
2) MAJOR: Class inheritency :
- DaughterClass.prototype.__proto__ = MotherClass.prototype;
3) MAJOR: Provide a prototype to an Object instance at creation time:
- var a = { __proto__: aProto, myprop: 9, … };
- var a = new A(); a.__proto__ = aProto;
4) MINOR : Access to __proto__ reference:
- x.__proto__ === y.__proto__
- x.__proto__.constructor
5) MINOR: Provide a prototype to a Function at creation time:
- var f = function() {}; f.__proto__ = aNewProto;
Usage in most depended upon node modules
====================================
1 occurs in 4 modules: proto-list, express, qs, ejs modules
2 occurs in 15 modules: socket-io, commander, connect, connect-redis, engine-io, send, stylus, browser-builtins, supertest, superagent, emitter-component, mocha, jade, node-unit, cli-table modules
3 occurs in 4 modules: traverse, hashish, express, esprima module test dependency modules
4 occurs in 4 modules: traverse module test script, readable-stream module test script, jade, insert-module-globals modules
5 occurs in 1 module: engine.io
Workaround and side effects
======================
1 can't be workarounded. Script needs to be rewritten in order to propagate prototype updates.
2 can be workarounded with Object.create but DaughterClass prototype can't be updated once Object.create has been called. Furthermore, any instance created prior to the extension are not updated.
DaughterClass.prototype = Object.create(MotherClass.prototype);
DaughterClass.prototype.constructor = DaughterClass;
3 can be workarounded with Object.create
Object.create(aProto, { myprop : {enumerable: true, value:9, ...} }
4 can be workarounded with Object.getPrototypeOf
Object.getPrototypeOf(x) === Object.getPrototypeOf(y)
5 can be workarounded with:
for(k in aNewProto) {
f[k] = aNewProto[k];
}
Attachments
Issue Links
- duplicates
-
JDK-8016220 An option for writable prototype would greatly help making existing scripts to run on top of nashorn
- Resolved