Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Return object from process.env() #10

Open
markc opened this Issue · 2 comments

2 participants

@markc

Here's a modification to process.env() to return an object instead of an array. It makes it slightly more usable in that var home = process.env()["HOME"] becomes possible without having to string parse the results of the previous version. It's kind of a cheap process.getenv(). I'm not 100% sure of these 2 lines as to whether they are correct but the function works with my testing.

    if (val[0] == '=') val = val + 1;
    const int vlen = val ? strlen(val) : 0;

And here is the function itself. Unfortunately the tabs I added are removed in this paste...

// 20111227 markc@renta.net MIT license
static JSVAL process_env(JSARGS args) {
        HandleScope scope;
        int size = 0;
        while (environ[size]) size++;
        Handle<Object>env = Object::New();
        for (int i = 0; i < size; ++i) {
                const char* key = environ[i];
                const char* val = strchr(key, '=');
                const int klen = val ? val - key : strlen(key);
                if (val[0] == '=') val = val + 1;
                const int vlen = val ? strlen(val) : 0;
                env->Set(String::New(key, klen), String::New(val, vlen));
        }
        return scope.Close(env);
}
@mschwartz
Owner
@markc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.