MCPcopy Create free account
hub / github.com/buke/quickjs-go / build_envp

Function build_envp

deps/quickjs/quickjs-libc.c:3442–3499  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

3440}
3441
3442static char **build_envp(JSContext *ctx, JSValue obj)
3443{
3444 uint32_t len, i;
3445 JSPropertyEnum *tab;
3446 char **envp, *pair;
3447 const char *key, *str;
3448 JSValue val;
3449 size_t key_len, str_len;
3450
3451 if (JS_GetOwnPropertyNames(ctx, &tab, &len, obj,
3452 JS_GPN_STRING_MASK | JS_GPN_ENUM_ONLY) < 0)
3453 return NULL;
3454 envp = js_mallocz(ctx, sizeof(envp[0]) * ((size_t)len + 1));
3455 if (!envp)
3456 goto fail;
3457 for(i = 0; i < len; i++) {
3458 val = JS_GetProperty(ctx, obj, tab[i].atom);
3459 if (JS_IsException(val))
3460 goto fail;
3461 str = JS_ToCString(ctx, val);
3462 JS_FreeValue(ctx, val);
3463 if (!str)
3464 goto fail;
3465 key = JS_AtomToCString(ctx, tab[i].atom);
3466 if (!key) {
3467 JS_FreeCString(ctx, str);
3468 goto fail;
3469 }
3470 key_len = strlen(key);
3471 str_len = strlen(str);
3472 pair = js_malloc(ctx, key_len + str_len + 2);
3473 if (!pair) {
3474 JS_FreeCString(ctx, key);
3475 JS_FreeCString(ctx, str);
3476 goto fail;
3477 }
3478 memcpy(pair, key, key_len);
3479 pair[key_len] = '=';
3480 memcpy(pair + key_len + 1, str, str_len);
3481 pair[key_len + 1 + str_len] = '\0';
3482 envp[i] = pair;
3483 JS_FreeCString(ctx, key);
3484 JS_FreeCString(ctx, str);
3485 }
3486 done:
3487 for(i = 0; i < len; i++)
3488 JS_FreeAtom(ctx, tab[i].atom);
3489 js_free(ctx, tab);
3490 return envp;
3491 fail:
3492 if (envp) {
3493 for(i = 0; i < len; i++)
3494 js_free(ctx, envp[i]);
3495 js_free(ctx, envp);
3496 envp = NULL;
3497 }
3498 goto done;
3499}

Callers 1

js_os_execFunction · 0.85

Calls 3

JS_IsExceptionFunction · 0.85
JS_ToCStringFunction · 0.85
JS_AtomToCStringFunction · 0.85

Tested by

no test coverage detected