Skip to content

Commit

Permalink
Make writes to process.env update the real environment. Tests included.
Browse files Browse the repository at this point in the history
  • Loading branch information
bnoordhuis authored and ry committed Nov 16, 2010
1 parent 4e0c7dd commit 96e0615
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 13 deletions.
80 changes: 67 additions & 13 deletions src/node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1550,6 +1550,69 @@ static void ProcessTitleSetter(Local<String> property,
}


static Handle<Value> EnvGetter(Local<String> property,
const AccessorInfo& info) {
String::Utf8Value key(property);
const char* val = getenv(*key);
if (val) {
HandleScope scope;
return scope.Close(String::New(val));
}
return Undefined();
}


static Handle<Value> EnvSetter(Local<String> property,
Local<Value> value,
const AccessorInfo& info) {
String::Utf8Value key(property);
String::Utf8Value val(value);
setenv(*key, *val, 1);
return value;
}


static Handle<Integer> EnvQuery(Local<String> property,
const AccessorInfo& info) {
String::Utf8Value key(property);
if (getenv(*key)) {
HandleScope scope;
return scope.Close(Integer::New(None));
}
return Handle<Integer>();
}


static Handle<Boolean> EnvDeleter(Local<String> property,
const AccessorInfo& info) {
String::Utf8Value key(property);
if (getenv(*key)) {
unsetenv(*key); // prototyped as `void unsetenv(const char*)` on some platforms
return True();
}
return False();
}


static Handle<Array> EnvEnumerator(const AccessorInfo& info) {
HandleScope scope;

int size = 0;
while (environ[size]) size++;

Local<Array> env = Array::New(size);

for (int i = 0; i < size; ++i) {
const char* var = environ[i];
const char* s = strchr(var, '=');
const int length = s ? s - var : strlen(var);
env->Set(i, String::New(var, length));
}

return scope.Close(env);
}


static void Load(int argc, char *argv[]) {
HandleScope scope;

Expand Down Expand Up @@ -1602,20 +1665,11 @@ static void Load(int argc, char *argv[]) {
process->Set(String::NewSymbol("argv"), arguments);

// create process.env
Local<Object> env = Object::New();
for (i = 0; environ[i]; i++) {
// skip entries without a '=' character
for (j = 0; environ[i][j] && environ[i][j] != '='; j++) { ; }
// create the v8 objects
Local<String> field = String::New(environ[i], j);
Local<String> value = Local<String>();
if (environ[i][j] == '=') {
value = String::New(environ[i]+j+1);
}
// assign them
env->Set(field, value);
}
Local<ObjectTemplate> envTemplate = ObjectTemplate::New();
envTemplate->SetNamedPropertyHandler(EnvGetter, EnvSetter, EnvQuery, EnvDeleter, EnvEnumerator, Undefined());

// assign process.ENV
Local<Object> env = envTemplate->NewInstance();
process->Set(String::NewSymbol("ENV"), env);
process->Set(String::NewSymbol("env"), env);

Expand Down
36 changes: 36 additions & 0 deletions test/simple/test-process-env.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// first things first, set the timezone; see tzset(3)
process.env.TZ = 'Europe/Amsterdam';

assert = require('assert');
spawn = require('child_process').spawn;

// time difference between Greenwich and Amsterdam is +2 hours in the summer
date = new Date('Fri, 10 Sep 1982 03:15:00 GMT');
assert.equal(3, date.getUTCHours());
assert.equal(5, date.getHours());

// changes in environment should be visible to child processes
if (process.argv[2] == 'you-are-the-child') {
// failed assertion results in process exiting with status code 1
assert.equal(false, 'NODE_PROCESS_ENV_DELETED' in process.env);
assert.equal(42, process.env.NODE_PROCESS_ENV);
process.exit(0);
} else {
process.env.NODE_PROCESS_ENV = 42;
assert.equal(42, process.env.NODE_PROCESS_ENV);

process.env.NODE_PROCESS_ENV_DELETED = 42;
assert.equal(true, 'NODE_PROCESS_ENV_DELETED' in process.env);

delete process.env.NODE_PROCESS_ENV_DELETED;
assert.equal(false, 'NODE_PROCESS_ENV_DELETED' in process.env);

child = spawn(process.argv[0], [process.argv[1], 'you-are-the-child']);
child.stdout.on('data', function(data) { console.log(data.toString()); });
child.stderr.on('data', function(data) { console.log(data.toString()); });
child.on('exit', function(statusCode) {
if (statusCode != 0) {
process.exit(statusCode); // failed assertion in child process
}
});
}

0 comments on commit 96e0615

Please sign in to comment.