Skip to content

Commit

Permalink
[Fix] cache Object.prototype[__proto__] getter
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Nov 22, 2024
1 parent 23cf537 commit 68acea1
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

"rules": {
"id-length": 0,
"max-statements": [2, 20],
"max-statements": [2, 25],
"new-cap": [2, {
"capIsNewExceptions": [
"GetIntrinsic",
Expand Down
31 changes: 31 additions & 0 deletions .github/workflows/node-twenties.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,34 @@ jobs:
range: '>= 20'
type: minors
command: npm run tests-only

no-proto:
name: 'node, --disable-proto=delete'
runs-on: ubuntu-latest
steps:
- name: Harden Runner
uses: step-security/harden-runner@v2
with:
disable-sudo: true
egress-policy: audit
allowed-endpoints: >
github.com:443
raw.githubusercontent.com:443
nodejs.org:443
iojs.org:443
registry.npmjs.org:443
actions-results-receiver-production.githubapp.com:443
- uses: actions/checkout@v4
with:
show-progress: false
- uses: ljharb/actions/node/install@main
- run: NODE_OPTIONS=--disable-proto=delete npm run tests-only
- uses: codecov/[email protected]

finisher:
needs: [tests, no-proto]
name: 'node >= 20'
runs-on: ubuntu-latest
steps:
- run: true
10 changes: 7 additions & 3 deletions implementation.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,17 @@ var IsCallable = require('es-abstract/2024/IsCallable');
var Type = require('es-abstract/2024/Type');
var whichBuiltinType = require('which-builtin-type');
var $TypeError = require('es-errors/type');
var callBind = require('call-bind');
var gOPD = require('gopd');

var $gPO = GetIntrinsic('%Object.getPrototypeOf%', true);
var $ObjectPrototype = GetIntrinsic('%Object.prototype%');

var hasProto = [].__proto__ === Array.prototype; // eslint-disable-line no-proto

var dunderGetter = !$gPO && hasProto && gOPD && gOPD(Object.prototype, '__proto__');
var getDunder = dunderGetter && dunderGetter.get && callBind(dunderGetter.get);

module.exports = function getPrototypeOf(O) {
if (Type(O) !== 'Object') {
throw new $TypeError('Reflect.getPrototypeOf called on non-object');
Expand All @@ -20,9 +25,8 @@ module.exports = function getPrototypeOf(O) {
return $gPO(O);
}

if (hasProto) {
// eslint-disable-next-line no-proto
var proto = O.__proto__;
if (getDunder) {
var proto = getDunder(O);
if (proto || proto === null) {
return proto;
}
Expand Down
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
"es-abstract": "^1.23.5",
"es-errors": "^1.3.0",
"get-intrinsic": "^1.2.4",
"gopd": "^1.0.1",
"which-builtin-type": "^1.1.4"
},
"devDependencies": {
Expand Down Expand Up @@ -89,5 +90,8 @@
"ignore": [
".github/workflows"
]
},
"testling": {
"files": "test/implementation.js"
}
}
9 changes: 8 additions & 1 deletion polyfill.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,22 @@ var Type = require('es-abstract/2024/Type');

var $TypeError = require('es-errors/type');

var callBind = require('call-bind');
var gOPD = require('gopd');

var implementation = require('./implementation');

var hasProto = [].__proto__ === Array.prototype; // eslint-disable-line no-proto

var dunderGetter = hasProto && gOPD && gOPD(Object.prototype, '__proto__');
var getDunder = dunderGetter && dunderGetter.get && callBind(dunderGetter.get);

var getProto = function getPrototypeOf(value) {
if (Type(value) !== 'Object') {
throw new $TypeError('Reflect.getPrototypeOf called on non-object');
}
return value.__proto__; // eslint-disable-line no-proto
// eslint-disable-next-line no-proto
return getDunder ? getDunder(value) : value.__proto__;
};

module.exports = function getPolyfill() {
Expand Down
8 changes: 8 additions & 0 deletions test/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,12 @@ module.exports = function (getPrototypeOf, t) {
t.equal(getPrototypeOf(function () {}), Function.prototype);
t.equal(getPrototypeOf([]), Array.prototype);
t.equal(getPrototypeOf({}), Object.prototype);

var obj = { __proto__: null };
if ('toString' in obj) {
t.comment('no null objects in this engine');
t.equal(getPrototypeOf(obj), Object.prototype, '"null" object has Object.prototype as [[Prototype]]');
} else {
t.equal(getPrototypeOf(obj), null, 'null object has null [[Prototype]]');
}
};

0 comments on commit 68acea1

Please sign in to comment.