mirror of
https://github.com/nodejs/node.git
synced 2025-08-15 13:48:44 +02:00
lib: flag to conditionally modify proto on deprecate
Refs: https://github.com/nodejs/node/issues/58218 Signed-off-by: RafaelGSS <rafael.nunu@hotmail.com> PR-URL: https://github.com/nodejs/node/pull/58928 Reviewed-By: Juan José Arboleda <soyjuanarbol@gmail.com> Reviewed-By: Ulises Gascón <ulisesgascongonzalez@gmail.com>
This commit is contained in:
parent
1c4fe6d795
commit
eff504ff12
2 changed files with 53 additions and 13 deletions
36
benchmark/util/deprecate.js
Normal file
36
benchmark/util/deprecate.js
Normal file
|
@ -0,0 +1,36 @@
|
||||||
|
'use strict';
|
||||||
|
|
||||||
|
const common = require('../common');
|
||||||
|
const assert = require('assert');
|
||||||
|
|
||||||
|
const bench = common.createBenchmark(main, {
|
||||||
|
n: [1e5],
|
||||||
|
modifyPrototype: [1, 0],
|
||||||
|
emitWarningSync: [1, 0],
|
||||||
|
}, {
|
||||||
|
flags: ['--expose-internals'],
|
||||||
|
});
|
||||||
|
|
||||||
|
function simpleFunction(x) {
|
||||||
|
return x * 2 + (new Array(1000)).fill(0).map((_, i) => i).reduce((a, b) => a + b, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
function main({ n, modifyPrototype, emitWarningSync }) {
|
||||||
|
const { deprecate } = require('internal/util');
|
||||||
|
|
||||||
|
const fn = deprecate(
|
||||||
|
simpleFunction,
|
||||||
|
'This function is deprecated',
|
||||||
|
'DEP0000',
|
||||||
|
emitWarningSync,
|
||||||
|
!!modifyPrototype,
|
||||||
|
);
|
||||||
|
|
||||||
|
let sum = 0;
|
||||||
|
bench.start();
|
||||||
|
for (let i = 0; i < n; ++i) {
|
||||||
|
sum += fn(i);
|
||||||
|
}
|
||||||
|
bench.end(n);
|
||||||
|
assert.ok(sum);
|
||||||
|
}
|
|
@ -169,7 +169,7 @@ function pendingDeprecate(fn, msg, code) {
|
||||||
// Mark that a method should not be used.
|
// Mark that a method should not be used.
|
||||||
// Returns a modified function which warns once by default.
|
// Returns a modified function which warns once by default.
|
||||||
// If --no-deprecation is set, then it is a no-op.
|
// If --no-deprecation is set, then it is a no-op.
|
||||||
function deprecate(fn, msg, code, useEmitSync) {
|
function deprecate(fn, msg, code, useEmitSync, modifyPrototype = true) {
|
||||||
// Lazy-load to avoid a circular dependency.
|
// Lazy-load to avoid a circular dependency.
|
||||||
if (validateString === undefined)
|
if (validateString === undefined)
|
||||||
({ validateString } = require('internal/validators'));
|
({ validateString } = require('internal/validators'));
|
||||||
|
@ -192,19 +192,23 @@ function deprecate(fn, msg, code, useEmitSync) {
|
||||||
return ReflectApply(fn, this, args);
|
return ReflectApply(fn, this, args);
|
||||||
}
|
}
|
||||||
|
|
||||||
// The wrapper will keep the same prototype as fn to maintain prototype chain
|
if (modifyPrototype) {
|
||||||
ObjectSetPrototypeOf(deprecated, fn);
|
// The wrapper will keep the same prototype as fn to maintain prototype chain
|
||||||
if (fn.prototype) {
|
// Modifying the prototype does alter the object chains, and as observed in
|
||||||
// Setting this (rather than using Object.setPrototype, as above) ensures
|
// most cases, it slows the code.
|
||||||
// that calling the unwrapped constructor gives an instanceof the wrapped
|
ObjectSetPrototypeOf(deprecated, fn);
|
||||||
// constructor.
|
if (fn.prototype) {
|
||||||
deprecated.prototype = fn.prototype;
|
// Setting this (rather than using Object.setPrototype, as above) ensures
|
||||||
}
|
// that calling the unwrapped constructor gives an instanceof the wrapped
|
||||||
|
// constructor.
|
||||||
|
deprecated.prototype = fn.prototype;
|
||||||
|
}
|
||||||
|
|
||||||
ObjectDefineProperty(deprecated, 'length', {
|
ObjectDefineProperty(deprecated, 'length', {
|
||||||
__proto__: null,
|
__proto__: null,
|
||||||
...ObjectGetOwnPropertyDescriptor(fn, 'length'),
|
...ObjectGetOwnPropertyDescriptor(fn, 'length'),
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
|
||||||
return deprecated;
|
return deprecated;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue