Permalink
Browse files

lib,src: make constants not inherit from Object

Make sure `constants` object and all the nested objects don't inherit
from `Object.prototype` but from `null`.

PR-URL: #10458
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Brian White <mscdex@mscdex.net>
  • Loading branch information...
thefourtheye authored and jasnell committed Dec 26, 2016
1 parent 221b03a commit caf9ae748b1324c34284b324f2951b91368ca840
Showing with 54 additions and 4 deletions.
  1. +2 −2 lib/fs.js
  2. +1 −2 lib/internal/process.js
  3. +2 −0 src/node.cc
  4. +19 −0 src/node_constants.cc
  5. +30 −0 test/parallel/test-binding-constants.js
View
@@ -1058,7 +1058,7 @@ fs.fchmodSync = function(fd, mode) {
return binding.fchmod(fd, modeNum(mode));
};
if (constants.hasOwnProperty('O_SYMLINK')) {
if (constants.O_SYMLINK !== undefined) {
fs.lchmod = function(path, mode, callback) {
callback = maybeCallback(callback);
fs.open(path, constants.O_WRONLY | constants.O_SYMLINK, function(err, fd) {
@@ -1116,7 +1116,7 @@ fs.chmodSync = function(path, mode) {
return binding.chmod(pathModule._makeLong(path), modeNum(mode));
};
if (constants.hasOwnProperty('O_SYMLINK')) {
if (constants.O_SYMLINK !== undefined) {
fs.lchown = function(path, uid, gid, callback) {
callback = maybeCallback(callback);
fs.open(path, constants.O_WRONLY | constants.O_SYMLINK, function(err, fd) {
View
@@ -201,8 +201,7 @@ function setupSignalHandlers() {
const signalWraps = {};
function isSignal(event) {
return typeof event === 'string' &&
lazyConstants().hasOwnProperty(event);
return typeof event === 'string' && lazyConstants()[event] !== undefined;
}
// Detect presence of a listener for the special signal types
View
@@ -2673,6 +2673,8 @@ static void Binding(const FunctionCallbackInfo<Value>& args) {
cache->Set(module, exports);
} else if (!strcmp(*module_v, "constants")) {
exports = Object::New(env->isolate());
CHECK(exports->SetPrototype(env->context(),
Null(env->isolate())).FromJust());
DefineConstants(env->isolate(), exports);
cache->Set(module, exports);
} else if (!strcmp(*module_v, "natives")) {
View
@@ -1245,12 +1245,31 @@ void DefineZlibConstants(Local<Object> target) {
}
void DefineConstants(v8::Isolate* isolate, Local<Object> target) {
Environment* env = Environment::GetCurrent(isolate);
Local<Object> os_constants = Object::New(isolate);
CHECK(os_constants->SetPrototype(env->context(),
Null(env->isolate())).FromJust());
Local<Object> err_constants = Object::New(isolate);
CHECK(err_constants->SetPrototype(env->context(),
Null(env->isolate())).FromJust());
Local<Object> sig_constants = Object::New(isolate);
CHECK(sig_constants->SetPrototype(env->context(),
Null(env->isolate())).FromJust());
Local<Object> fs_constants = Object::New(isolate);
CHECK(fs_constants->SetPrototype(env->context(),
Null(env->isolate())).FromJust());
Local<Object> crypto_constants = Object::New(isolate);
CHECK(crypto_constants->SetPrototype(env->context(),
Null(env->isolate())).FromJust());
Local<Object> zlib_constants = Object::New(isolate);
CHECK(zlib_constants->SetPrototype(env->context(),
Null(env->isolate())).FromJust());
DefineErrnoConstants(err_constants);
DefineWindowsErrorConstants(err_constants);
@@ -0,0 +1,30 @@
'use strict';
require('../common');
const constants = process.binding('constants');
const assert = require('assert');
assert.deepStrictEqual(
Object.keys(constants).sort(), ['crypto', 'fs', 'os', 'zlib']
);
assert.deepStrictEqual(
Object.keys(constants.os).sort(), ['UV_UDP_REUSEADDR', 'errno', 'signals']
);
// Make sure all the constants objects don't inherit from Object.prototype
const inheritedProperties = Object.getOwnPropertyNames(Object.prototype);
function test(obj) {
assert(obj);
assert.strictEqual(Object.prototype.toString.call(obj), '[object Object]');
assert.strictEqual(Object.getPrototypeOf(obj), null);
inheritedProperties.forEach((property) => {
assert.strictEqual(property in obj, false);
});
}
[
constants, constants.crypto, constants.fs, constants.os, constants.zlib,
constants.os.errno, constants.os.signals
].forEach(test);

0 comments on commit caf9ae7

Please sign in to comment.