SilkJS icon indicating copy to clipboard operation
SilkJS copied to clipboard

Return object from process.env()

Open markc opened this issue 14 years ago • 2 comments

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 [email protected] 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);
}

markc avatar Dec 27 '11 01:12 markc

I like this change. No pull request?

On Dec 26, 2011, at 5:17 PM, Mark Constable wrote:

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 [email protected] 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); }


Reply to this email directly or view it on GitHub: https://github.com/mschwartz/SilkJS/issues/10

mschwartz avatar Dec 27 '11 04:12 mschwartz

There we go, there's one there now.

I just made a fresh clone and whereas previously it took about a minute, maybe 2, this time I had to go off make a cup of coffee because of the extra 90Mb of v8 code.

markc avatar Dec 27 '11 04:12 markc