Browse Source

Fix #3521 Use an object as the process.env proto

For some reason, though, it looks like EnvGetter is not called for the
key `__proto__`, so I can't make the info->Data() accessible.  However,
putting the Object.prototype keys there, in such a way that they are not
OwnProperties, and are supersceded by environs, makes process.env much
less weird.
v0.9.1-release
isaacs 13 years ago
parent
commit
e3074689f5
  1. 6
      src/node.cc
  2. 10
      test/simple/test-process-env.js

6
src/node.cc

@ -1959,8 +1959,8 @@ static Handle<Value> EnvGetter(Local<String> property,
return scope.Close(String::New(reinterpret_cast<uint16_t*>(buffer), result)); return scope.Close(String::New(reinterpret_cast<uint16_t*>(buffer), result));
} }
#endif #endif
// Not found // Not found. Fetch from prototype.
return Undefined(); return info.Data().As<Object>()->Get(property);
} }
@ -2210,7 +2210,7 @@ Handle<Object> SetupProcessObject(int argc, char *argv[]) {
EnvQuery, EnvQuery,
EnvDeleter, EnvDeleter,
EnvEnumerator, EnvEnumerator,
Undefined()); Object::New());
Local<Object> env = envTemplate->NewInstance(); Local<Object> env = envTemplate->NewInstance();
process->Set(String::NewSymbol("env"), env); process->Set(String::NewSymbol("env"), env);

10
test/simple/test-process-env.js

@ -47,8 +47,18 @@ if (process.argv[2] == 'you-are-the-child') {
// failed assertion results in process exiting with status code 1 // failed assertion results in process exiting with status code 1
assert.equal(false, 'NODE_PROCESS_ENV_DELETED' in process.env); assert.equal(false, 'NODE_PROCESS_ENV_DELETED' in process.env);
assert.equal(42, process.env.NODE_PROCESS_ENV); assert.equal(42, process.env.NODE_PROCESS_ENV);
assert.equal('asdf', process.env.hasOwnProperty);
var hasOwnProperty = Object.prototype.hasOwnProperty;
var has = hasOwnProperty.call(process.env, 'hasOwnProperty');
assert.equal(true, has);
process.exit(0); process.exit(0);
} else { } else {
assert.equal(Object.prototype.hasOwnProperty, process.env.hasOwnProperty);
var has = process.env.hasOwnProperty('hasOwnProperty');
assert.equal(false, has);
process.env.hasOwnProperty = 'asdf';
process.env.NODE_PROCESS_ENV = 42; process.env.NODE_PROCESS_ENV = 42;
assert.equal(42, process.env.NODE_PROCESS_ENV); assert.equal(42, process.env.NODE_PROCESS_ENV);

Loading…
Cancel
Save