Skip to content
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

Global getter deprecate #1

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
63 changes: 33 additions & 30 deletions lib/internal/bootstrap/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -426,50 +426,53 @@ function setupProcessObject() {
}

function setupGlobalVariables() {
const util = NativeModule.require('util');

function getDeprecatedDescriptor(name, replacement, value, code) {
const msg = `'${name}' is deprecated, use '${replacement}'`;
return {
configurable: true,
enumerable: false,
get: util.deprecate(function() {
return value;
}, msg, code),
set: util.deprecate(function(_value) {
value = _value;
}, msg, code)
};
}

Object.defineProperty(global, Symbol.toStringTag, {
value: 'global',
writable: false,
enumerable: false,
configurable: true
});
global.process = process;
const util = NativeModule.require('util');

function makeGetter(name) {
return util.deprecate(function() {
return this;
}, `'${name}' is deprecated, use 'global'`, 'DEP0016');
}

function makeSetter(name) {
return util.deprecate(function(value) {
Object.defineProperty(this, name, {
configurable: true,
writable: true,
enumerable: true,
value: value
});
}, `'${name}' is deprecated, use 'global'`, 'DEP0016');
}
Object.defineProperty(global, 'process', getDeprecatedDescriptor(
'global.process', 'process', process, 'DEPXXXY'
));

Object.defineProperties(global, {
GLOBAL: {
configurable: true,
get: makeGetter('GLOBAL'),
set: makeSetter('GLOBAL')
},
root: {
configurable: true,
get: makeGetter('root'),
set: makeSetter('root')
}
GLOBAL: getDeprecatedDescriptor(
'GLOBAL', 'global', this, 'DEP0016'
),
root: getDeprecatedDescriptor(
'root', 'global', this, 'DEP00016'
)
});

Object.defineProperty(Atomics, 'wake', getDeprecatedDescriptor(
'Atomics.wake', 'Atomics.notify', Atomics.wake, 'DEPXXXZ'
));

// This, as side effect, removes `setupBufferJS` from the buffer binding,
// and exposes it on `internal/buffer`.
NativeModule.require('internal/buffer');

global.Buffer = NativeModule.require('buffer').Buffer;
const Buffer = NativeModule.require('buffer').Buffer;
Object.defineProperty(global, 'Buffer', getDeprecatedDescriptor(
'global.Buffer', 'Buffer', Buffer, 'DEPXXXX'
));
process.domain = null;
process._exiting = false;
}
Expand Down
13 changes: 7 additions & 6 deletions lib/internal/modules/cjs/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,11 @@ function stripShebang(content) {
}

const builtinLibs = [
'assert', 'async_hooks', 'buffer', 'child_process', 'cluster', 'crypto',
'dgram', 'dns', 'domain', 'events', 'fs', 'http', 'http2', 'https', 'net',
'os', 'path', 'perf_hooks', 'punycode', 'querystring', 'readline', 'repl',
'stream', 'string_decoder', 'tls', 'trace_events', 'tty', 'url', 'util',
'v8', 'vm', 'zlib'
'assert', 'async_hooks', 'Buffer', 'buffer', 'child_process', 'cluster',
'crypto', 'dgram', 'dns', 'domain', 'events', 'fs', 'http', 'http2', 'https',
'net', 'os', 'path', 'perf_hooks', 'process', 'punycode', 'querystring',
'readline', 'repl', 'stream', 'string_decoder', 'tls', 'trace_events', 'tty',
'url', 'util', 'v8', 'vm', 'zlib'
];

if (getOptionValue('--experimental-worker')) {
Expand Down Expand Up @@ -135,7 +135,8 @@ function addBuiltinLibsToObject(object) {

Object.defineProperty(object, name, {
get: () => {
const lib = require(name);
const lib =
name === 'Buffer' ? require('buffer').Buffer : require(name);

// Disable the current getter/setter and set up a new
// non-enumerable property.
Expand Down
73 changes: 59 additions & 14 deletions lib/internal/modules/cjs/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const fs = require('fs');
const internalFS = require('internal/fs/utils');
const path = require('path');
const { URL } = require('url');
const { Buffer } = require('buffer');
const {
internalModuleReadJSON,
internalModuleStat
Expand Down Expand Up @@ -124,15 +125,52 @@ Module._extensions = Object.create(null);
var modulePaths = [];
Module.globalPaths = [];

Module.wrap = function(script) {
let patched = false;

// eslint-disable-next-line func-style
let wrap = function(script) {
return Module.wrapper[0] + script + Module.wrapper[1];
};

Module.wrapper = [
const wrapper = [
'(function (exports, require, module, __filename, __dirname) { ',
'\n});'
];

let wrapperProxy = new Proxy(wrapper, {
set(target, property, value, receiver) {
patched = true;
return Reflect.set(target, property, value, receiver);
},

defineProperty(target, property, descriptor) {
patched = true;
return Object.defineProperty(target, property, descriptor);
}
});

Object.defineProperty(Module, 'wrap', {
get() {
return wrap;
},

set(value) {
patched = true;
wrap = value;
}
});

Object.defineProperty(Module, 'wrapper', {
get() {
return wrapperProxy;
},

set(value) {
patched = true;
wrapperProxy = value;
}
});

const debug = util.debuglog('module');

Module._debug = util.deprecate(debug, 'Module._debug is deprecated.',
Expand Down Expand Up @@ -676,24 +714,31 @@ function normalizeReferrerURL(referrer) {
// the correct helper variables (require, module, exports) to
// the file.
// Returns exception, if any.
const processBufferContext = { process, Buffer };
Module.prototype._compile = function(content, filename) {

content = stripShebang(content);

// create wrapper function
var wrapper = Module.wrap(content);

var compiledWrapper = vm.runInThisContext(wrapper, {
filename: filename,
lineOffset: 0,
displayErrors: true,
importModuleDynamically: experimentalModules ? async (specifier) => {
if (asyncESM === undefined) lazyLoadESM();
const loader = await asyncESM.loaderPromise;
return loader.import(specifier, normalizeReferrerURL(filename));
} : undefined,
let compiledWrapper;
if (patched) {
console.log(patched);
content = 'return ' + Module._wrap(content);
}

compiledWrapper = vm.compileFunction(content, [
'exports',
'require',
'module',
'__filename',
'__dirname',
], {
filename,
contextExtensions: [processBufferContext]
});

if (patched)
compiledWrapper = compiledWrapper();

var inspectorWrapper = null;
if (process._breakFirstLine && process._eval == null) {
if (!resolvedArgv) {
Expand Down
24 changes: 3 additions & 21 deletions lib/internal/per_context.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,14 @@
if (global.Intl) delete global.Intl.v8BreakIterator;

// https://github.com/nodejs/node/issues/21219
// Adds Atomics.notify and warns on first usage of Atomics.wake

// https://github.com/v8/v8/commit/c79206b363 adds Atomics.notify so
// now we alias Atomics.wake to notify so that we can remove it
// semver major without worrying about V8.

const AtomicsNotify = global.Atomics.notify;
const ReflectApply = global.Reflect.apply;

const warning = 'Atomics.wake will be removed in a future version, ' +
'use Atomics.notify instead.';

let wakeWarned = false;
// The warning for Atomics.wake access is added in the main bootstrap itself
function wake(typedArray, index, count) {
if (!wakeWarned) {
wakeWarned = true;

if (global.process !== undefined) {
global.process.emitWarning(warning, 'Atomics');
} else {
global.console.error(`Atomics: ${warning}`);
}
}

return ReflectApply(AtomicsNotify, this, arguments);
return global.Reflect.apply(global.Atomics.notify, this, arguments);
}

global.Object.defineProperties(global.Atomics, {
wake: {
value: wake,
Expand Down
3 changes: 0 additions & 3 deletions test/common/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

/* eslint-disable node-core/required-modules, node-core/crypto-check */
'use strict';
const process = global.process; // Some tests tamper with the process global.
const path = require('path');
const fs = require('fs');
const assert = require('assert');
Expand Down Expand Up @@ -217,12 +216,10 @@ function platformTimeout(ms) {
}

let knownGlobals = [
Buffer,
clearImmediate,
clearInterval,
clearTimeout,
global,
process,
setImmediate,
setInterval,
setTimeout
Expand Down
6 changes: 3 additions & 3 deletions test/parallel/test-atomics-notify.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ assert.strictEqual(runInNewContext('typeof Atomics.wake'), 'function');
assert.strictEqual(runInNewContext('typeof Atomics.notify'), 'function');

expectWarning(
'Atomics',
'Atomics.wake will be removed in a future version, ' +
'use Atomics.notify instead.', noWarnCode);
'DeprecationWarning',
`'Atomics.wake' is deprecated, use 'Atomics.notify'`,
'DEPXXXZ');

Atomics.wake(new Int32Array(new SharedArrayBuffer(4)), 0, 0);
2 changes: 1 addition & 1 deletion test/parallel/test-module-cjs-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ const { builtinLibs } = require('internal/modules/cjs/helpers');

const hasInspector = process.config.variables.v8_enable_inspector === 1;

const expectedLibs = hasInspector ? 34 : 33;
const expectedLibs = hasInspector ? 36 : 31;
assert.strictEqual(builtinLibs.length, expectedLibs);