Skip to content

Commit 2767eba

Browse files
jasnelltargos
authored andcommitted
src: move more to node_process.cc from node.cc
PR-URL: #22422 Reviewed-By: Gus Caplan <[email protected]> Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Refael Ackermann <[email protected]> Reviewed-By: Colin Ihrig <[email protected]>
1 parent 8fd55ff commit 2767eba

File tree

3 files changed

+250
-225
lines changed

3 files changed

+250
-225
lines changed

src/node.cc

-225
Original file line numberDiff line numberDiff line change
@@ -114,13 +114,6 @@ typedef int mode_t;
114114
#include <dlfcn.h>
115115
#endif
116116

117-
#ifdef __APPLE__
118-
#include <crt_externs.h>
119-
#define environ (*_NSGetEnviron())
120-
#elif !defined(_MSC_VER)
121-
extern char **environ;
122-
#endif
123-
124117
// This is used to load built-in modules. Instead of using
125118
// __attribute__((constructor)), we call the _register_<modname>
126119
// function for each built-in modules explicitly in
@@ -168,9 +161,6 @@ using v8::Undefined;
168161
using v8::V8;
169162
using v8::Value;
170163

171-
static Mutex process_mutex;
172-
static Mutex environ_mutex;
173-
174164
static bool v8_is_profiling = false;
175165
static bool node_is_initialized = false;
176166
static uv_once_t init_modpending_once = UV_ONCE_INIT;
@@ -1694,221 +1684,6 @@ static void GetLinkedBinding(const FunctionCallbackInfo<Value>& args) {
16941684
args.GetReturnValue().Set(effective_exports);
16951685
}
16961686

1697-
static void ProcessTitleGetter(Local<Name> property,
1698-
const PropertyCallbackInfo<Value>& info) {
1699-
char buffer[512];
1700-
uv_get_process_title(buffer, sizeof(buffer));
1701-
info.GetReturnValue().Set(String::NewFromUtf8(info.GetIsolate(), buffer,
1702-
v8::NewStringType::kNormal).ToLocalChecked());
1703-
}
1704-
1705-
1706-
static void ProcessTitleSetter(Local<Name> property,
1707-
Local<Value> value,
1708-
const PropertyCallbackInfo<void>& info) {
1709-
node::Utf8Value title(info.GetIsolate(), value);
1710-
TRACE_EVENT_METADATA1("__metadata", "process_name", "name",
1711-
TRACE_STR_COPY(*title));
1712-
uv_set_process_title(*title);
1713-
}
1714-
1715-
1716-
static void EnvGetter(Local<Name> property,
1717-
const PropertyCallbackInfo<Value>& info) {
1718-
Isolate* isolate = info.GetIsolate();
1719-
if (property->IsSymbol()) {
1720-
return info.GetReturnValue().SetUndefined();
1721-
}
1722-
Mutex::ScopedLock lock(environ_mutex);
1723-
#ifdef __POSIX__
1724-
node::Utf8Value key(isolate, property);
1725-
const char* val = getenv(*key);
1726-
if (val) {
1727-
return info.GetReturnValue().Set(String::NewFromUtf8(isolate, val,
1728-
v8::NewStringType::kNormal).ToLocalChecked());
1729-
}
1730-
#else // _WIN32
1731-
node::TwoByteValue key(isolate, property);
1732-
WCHAR buffer[32767]; // The maximum size allowed for environment variables.
1733-
SetLastError(ERROR_SUCCESS);
1734-
DWORD result = GetEnvironmentVariableW(reinterpret_cast<WCHAR*>(*key),
1735-
buffer,
1736-
arraysize(buffer));
1737-
// If result >= sizeof buffer the buffer was too small. That should never
1738-
// happen. If result == 0 and result != ERROR_SUCCESS the variable was not
1739-
// found.
1740-
if ((result > 0 || GetLastError() == ERROR_SUCCESS) &&
1741-
result < arraysize(buffer)) {
1742-
const uint16_t* two_byte_buffer = reinterpret_cast<const uint16_t*>(buffer);
1743-
Local<String> rc = String::NewFromTwoByte(isolate, two_byte_buffer);
1744-
return info.GetReturnValue().Set(rc);
1745-
}
1746-
#endif
1747-
}
1748-
1749-
1750-
static void EnvSetter(Local<Name> property,
1751-
Local<Value> value,
1752-
const PropertyCallbackInfo<Value>& info) {
1753-
Environment* env = Environment::GetCurrent(info);
1754-
if (env->options()->pending_deprecation && env->EmitProcessEnvWarning() &&
1755-
!value->IsString() && !value->IsNumber() && !value->IsBoolean()) {
1756-
if (ProcessEmitDeprecationWarning(
1757-
env,
1758-
"Assigning any value other than a string, number, or boolean to a "
1759-
"process.env property is deprecated. Please make sure to convert the "
1760-
"value to a string before setting process.env with it.",
1761-
"DEP0104").IsNothing())
1762-
return;
1763-
}
1764-
1765-
Mutex::ScopedLock lock(environ_mutex);
1766-
#ifdef __POSIX__
1767-
node::Utf8Value key(info.GetIsolate(), property);
1768-
node::Utf8Value val(info.GetIsolate(), value);
1769-
setenv(*key, *val, 1);
1770-
#else // _WIN32
1771-
node::TwoByteValue key(info.GetIsolate(), property);
1772-
node::TwoByteValue val(info.GetIsolate(), value);
1773-
WCHAR* key_ptr = reinterpret_cast<WCHAR*>(*key);
1774-
// Environment variables that start with '=' are read-only.
1775-
if (key_ptr[0] != L'=') {
1776-
SetEnvironmentVariableW(key_ptr, reinterpret_cast<WCHAR*>(*val));
1777-
}
1778-
#endif
1779-
// Whether it worked or not, always return value.
1780-
info.GetReturnValue().Set(value);
1781-
}
1782-
1783-
1784-
static void EnvQuery(Local<Name> property,
1785-
const PropertyCallbackInfo<Integer>& info) {
1786-
Mutex::ScopedLock lock(environ_mutex);
1787-
int32_t rc = -1; // Not found unless proven otherwise.
1788-
if (property->IsString()) {
1789-
#ifdef __POSIX__
1790-
node::Utf8Value key(info.GetIsolate(), property);
1791-
if (getenv(*key))
1792-
rc = 0;
1793-
#else // _WIN32
1794-
node::TwoByteValue key(info.GetIsolate(), property);
1795-
WCHAR* key_ptr = reinterpret_cast<WCHAR*>(*key);
1796-
SetLastError(ERROR_SUCCESS);
1797-
if (GetEnvironmentVariableW(key_ptr, nullptr, 0) > 0 ||
1798-
GetLastError() == ERROR_SUCCESS) {
1799-
rc = 0;
1800-
if (key_ptr[0] == L'=') {
1801-
// Environment variables that start with '=' are hidden and read-only.
1802-
rc = static_cast<int32_t>(v8::ReadOnly) |
1803-
static_cast<int32_t>(v8::DontDelete) |
1804-
static_cast<int32_t>(v8::DontEnum);
1805-
}
1806-
}
1807-
#endif
1808-
}
1809-
if (rc != -1)
1810-
info.GetReturnValue().Set(rc);
1811-
}
1812-
1813-
1814-
static void EnvDeleter(Local<Name> property,
1815-
const PropertyCallbackInfo<Boolean>& info) {
1816-
Mutex::ScopedLock lock(environ_mutex);
1817-
if (property->IsString()) {
1818-
#ifdef __POSIX__
1819-
node::Utf8Value key(info.GetIsolate(), property);
1820-
unsetenv(*key);
1821-
#else
1822-
node::TwoByteValue key(info.GetIsolate(), property);
1823-
WCHAR* key_ptr = reinterpret_cast<WCHAR*>(*key);
1824-
SetEnvironmentVariableW(key_ptr, nullptr);
1825-
#endif
1826-
}
1827-
1828-
// process.env never has non-configurable properties, so always
1829-
// return true like the tc39 delete operator.
1830-
info.GetReturnValue().Set(true);
1831-
}
1832-
1833-
1834-
static void EnvEnumerator(const PropertyCallbackInfo<Array>& info) {
1835-
Environment* env = Environment::GetCurrent(info);
1836-
Isolate* isolate = env->isolate();
1837-
Local<Context> ctx = env->context();
1838-
Local<Function> fn = env->push_values_to_array_function();
1839-
Local<Value> argv[NODE_PUSH_VAL_TO_ARRAY_MAX];
1840-
size_t idx = 0;
1841-
1842-
Mutex::ScopedLock lock(environ_mutex);
1843-
#ifdef __POSIX__
1844-
int size = 0;
1845-
while (environ[size])
1846-
size++;
1847-
1848-
Local<Array> envarr = Array::New(isolate);
1849-
1850-
for (int i = 0; i < size; ++i) {
1851-
const char* var = environ[i];
1852-
const char* s = strchr(var, '=');
1853-
const int length = s ? s - var : strlen(var);
1854-
argv[idx] = String::NewFromUtf8(isolate,
1855-
var,
1856-
v8::NewStringType::kNormal,
1857-
length).ToLocalChecked();
1858-
if (++idx >= arraysize(argv)) {
1859-
fn->Call(ctx, envarr, idx, argv).ToLocalChecked();
1860-
idx = 0;
1861-
}
1862-
}
1863-
if (idx > 0) {
1864-
fn->Call(ctx, envarr, idx, argv).ToLocalChecked();
1865-
}
1866-
#else // _WIN32
1867-
WCHAR* environment = GetEnvironmentStringsW();
1868-
if (environment == nullptr)
1869-
return; // This should not happen.
1870-
Local<Array> envarr = Array::New(isolate);
1871-
WCHAR* p = environment;
1872-
while (*p) {
1873-
WCHAR* s;
1874-
if (*p == L'=') {
1875-
// If the key starts with '=' it is a hidden environment variable.
1876-
p += wcslen(p) + 1;
1877-
continue;
1878-
} else {
1879-
s = wcschr(p, L'=');
1880-
}
1881-
if (!s) {
1882-
s = p + wcslen(p);
1883-
}
1884-
const uint16_t* two_byte_buffer = reinterpret_cast<const uint16_t*>(p);
1885-
const size_t two_byte_buffer_len = s - p;
1886-
argv[idx] = String::NewFromTwoByte(isolate,
1887-
two_byte_buffer,
1888-
String::kNormalString,
1889-
two_byte_buffer_len);
1890-
if (++idx >= arraysize(argv)) {
1891-
fn->Call(ctx, envarr, idx, argv).ToLocalChecked();
1892-
idx = 0;
1893-
}
1894-
p = s + wcslen(s) + 1;
1895-
}
1896-
if (idx > 0) {
1897-
fn->Call(ctx, envarr, idx, argv).ToLocalChecked();
1898-
}
1899-
FreeEnvironmentStringsW(environment);
1900-
#endif
1901-
1902-
info.GetReturnValue().Set(envarr);
1903-
}
1904-
1905-
1906-
static void GetParentProcessId(Local<Name> property,
1907-
const PropertyCallbackInfo<Value>& info) {
1908-
info.GetReturnValue().Set(Integer::New(info.GetIsolate(), uv_os_getppid()));
1909-
}
1910-
1911-
19121687
static Local<Object> GetFeatures(Environment* env) {
19131688
EscapableHandleScope scope(env->isolate());
19141689

src/node_internals.h

+23
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,9 @@ struct sockaddr;
170170

171171
namespace node {
172172

173+
extern Mutex process_mutex;
174+
extern Mutex environ_mutex;
175+
173176
// Tells whether it is safe to call v8::Isolate::GetCurrent().
174177
extern bool v8_initialized;
175178

@@ -888,6 +891,26 @@ void StopProfilerIdleNotifier(const v8::FunctionCallbackInfo<v8::Value>& args);
888891
void Umask(const v8::FunctionCallbackInfo<v8::Value>& args);
889892
void Uptime(const v8::FunctionCallbackInfo<v8::Value>& args);
890893

894+
void EnvDeleter(v8::Local<v8::Name> property,
895+
const v8::PropertyCallbackInfo<v8::Boolean>& info);
896+
void EnvGetter(v8::Local<v8::Name> property,
897+
const v8::PropertyCallbackInfo<v8::Value>& info);
898+
void EnvSetter(v8::Local<v8::Name> property,
899+
v8::Local<v8::Value> value,
900+
const v8::PropertyCallbackInfo<v8::Value>& info);
901+
void EnvQuery(v8::Local<v8::Name> property,
902+
const v8::PropertyCallbackInfo<v8::Integer>& info);
903+
void EnvEnumerator(const v8::PropertyCallbackInfo<v8::Array>& info);
904+
905+
void GetParentProcessId(v8::Local<v8::Name> property,
906+
const v8::PropertyCallbackInfo<v8::Value>& info);
907+
908+
void ProcessTitleGetter(v8::Local<v8::Name> property,
909+
const v8::PropertyCallbackInfo<v8::Value>& info);
910+
void ProcessTitleSetter(v8::Local<v8::Name> property,
911+
v8::Local<v8::Value> value,
912+
const v8::PropertyCallbackInfo<void>& info);
913+
891914
#if defined(__POSIX__) && !defined(__ANDROID__) && !defined(__CloudABI__)
892915
void SetGid(const v8::FunctionCallbackInfo<v8::Value>& args);
893916
void SetEGid(const v8::FunctionCallbackInfo<v8::Value>& args);

0 commit comments

Comments
 (0)