@@ -29,17 +29,21 @@ using v8::Value; |
29 | 29 | class RealEnvStore final : public KVStore { |
30 | 30 | public: |
31 | 31 | MaybeLocal<String> Get(Isolate* isolate, Local<String> key) const override; |
| 32 | + Maybe<std::string> Get(const char* key) const override; |
32 | 33 | void Set(Isolate* isolate, Local<String> key, Local<String> value) override; |
33 | 34 | int32_t Query(Isolate* isolate, Local<String> key) const override; |
| 35 | + int32_t Query(const char* key) const override; |
34 | 36 | void Delete(Isolate* isolate, Local<String> key) override; |
35 | 37 | Local<Array> Enumerate(Isolate* isolate) const override; |
36 | 38 | }; |
37 | 39 |
|
38 | 40 | class MapKVStore final : public KVStore { |
39 | 41 | public: |
40 | 42 | MaybeLocal<String> Get(Isolate* isolate, Local<String> key) const override; |
| 43 | + Maybe<std::string> Get(const char* key) const override; |
41 | 44 | void Set(Isolate* isolate, Local<String> key, Local<String> value) override; |
42 | 45 | int32_t Query(Isolate* isolate, Local<String> key) const override; |
| 46 | + int32_t Query(const char* key) const override; |
43 | 47 | void Delete(Isolate* isolate, Local<String> key) override; |
44 | 48 | Local<Array> Enumerate(Isolate* isolate) const override; |
45 | 49 |
|
@@ -58,26 +62,36 @@ Mutex env_var_mutex; |
58 | 62 | std::shared_ptr<KVStore> system_environment = std::make_shared<RealEnvStore>(); |
59 | 63 | } // namespace per_process |
60 | 64 |
|
61 | | -MaybeLocal<String> RealEnvStore::Get(Isolate* isolate, |
62 | | - Local<String> property) const { |
| 65 | +Maybe<std::string> RealEnvStore::Get(const char* key) const { |
63 | 66 | Mutex::ScopedLock lock(per_process::env_var_mutex); |
64 | 67 |
|
65 | | - node::Utf8Value key(isolate, property); |
66 | 68 | size_t init_sz = 256; |
67 | 69 | MaybeStackBuffer<char, 256> val; |
68 | | - int ret = uv_os_getenv(*key, *val, &init_sz); |
| 70 | + int ret = uv_os_getenv(key, *val, &init_sz); |
69 | 71 |
|
70 | 72 | if (ret == UV_ENOBUFS) { |
71 | 73 | // Buffer is not large enough, reallocate to the updated init_sz |
72 | 74 | // and fetch env value again. |
73 | 75 | val.AllocateSufficientStorage(init_sz); |
74 | | - ret = uv_os_getenv(*key, *val, &init_sz); |
| 76 | + ret = uv_os_getenv(key, *val, &init_sz); |
75 | 77 | } |
76 | 78 |
|
77 | 79 | if (ret >= 0) { // Env key value fetch success. |
78 | | - MaybeLocal<String> value_string = |
79 | | - String::NewFromUtf8(isolate, *val, NewStringType::kNormal, init_sz); |
80 | | - return value_string; |
| 80 | + return v8::Just(std::string(*val, init_sz)); |
| 81 | + } |
| 82 | + |
| 83 | + return v8::Nothing<std::string>(); |
| 84 | +} |
| 85 | + |
| 86 | +MaybeLocal<String> RealEnvStore::Get(Isolate* isolate, |
| 87 | + Local<String> property) const { |
| 88 | + node::Utf8Value key(isolate, property); |
| 89 | + Maybe<std::string> value = Get(*key); |
| 90 | + |
| 91 | + if (value.IsJust()) { |
| 92 | + std::string val = value.FromJust(); |
| 93 | + return String::NewFromUtf8( |
| 94 | + isolate, val.data(), NewStringType::kNormal, val.size()); |
81 | 95 | } |
82 | 96 |
|
83 | 97 | return MaybeLocal<String>(); |
@@ -97,14 +111,12 @@ void RealEnvStore::Set(Isolate* isolate, |
97 | 111 | uv_os_setenv(*key, *val); |
98 | 112 | } |
99 | 113 |
|
100 | | -int32_t RealEnvStore::Query(Isolate* isolate, Local<String> property) const { |
| 114 | +int32_t RealEnvStore::Query(const char* key) const { |
101 | 115 | Mutex::ScopedLock lock(per_process::env_var_mutex); |
102 | 116 |
|
103 | | - node::Utf8Value key(isolate, property); |
104 | | - |
105 | 117 | char val[2]; |
106 | 118 | size_t init_sz = sizeof(val); |
107 | | - int ret = uv_os_getenv(*key, val, &init_sz); |
| 119 | + int ret = uv_os_getenv(key, val, &init_sz); |
108 | 120 |
|
109 | 121 | if (ret == UV_ENOENT) { |
110 | 122 | return -1; |
@@ -121,6 +133,11 @@ int32_t RealEnvStore::Query(Isolate* isolate, Local<String> property) const { |
121 | 133 | return 0; |
122 | 134 | } |
123 | 135 |
|
| 136 | +int32_t RealEnvStore::Query(Isolate* isolate, Local<String> property) const { |
| 137 | + node::Utf8Value key(isolate, property); |
| 138 | + return Query(*key); |
| 139 | +} |
| 140 | + |
124 | 141 | void RealEnvStore::Delete(Isolate* isolate, Local<String> property) { |
125 | 142 | Mutex::ScopedLock lock(per_process::env_var_mutex); |
126 | 143 |
|
@@ -174,13 +191,19 @@ std::shared_ptr<KVStore> KVStore::Clone(v8::Isolate* isolate) const { |
174 | 191 | return copy; |
175 | 192 | } |
176 | 193 |
|
177 | | -MaybeLocal<String> MapKVStore::Get(Isolate* isolate, Local<String> key) const { |
| 194 | +Maybe<std::string> MapKVStore::Get(const char* key) const { |
178 | 195 | Mutex::ScopedLock lock(mutex_); |
| 196 | + auto it = map_.find(key); |
| 197 | + return it == map_.end() ? v8::Nothing<std::string>() : v8::Just(it->second); |
| 198 | +} |
| 199 | + |
| 200 | +MaybeLocal<String> MapKVStore::Get(Isolate* isolate, Local<String> key) const { |
179 | 201 | Utf8Value str(isolate, key); |
180 | | - auto it = map_.find(std::string(*str, str.length())); |
181 | | - if (it == map_.end()) return Local<String>(); |
182 | | - return String::NewFromUtf8(isolate, it->second.data(), |
183 | | - NewStringType::kNormal, it->second.size()); |
| 202 | + Maybe<std::string> value = Get(*str); |
| 203 | + if (value.IsNothing()) return Local<String>(); |
| 204 | + std::string val = value.FromJust(); |
| 205 | + return String::NewFromUtf8( |
| 206 | + isolate, val.data(), NewStringType::kNormal, val.size()); |
184 | 207 | } |
185 | 208 |
|
186 | 209 | void MapKVStore::Set(Isolate* isolate, Local<String> key, Local<String> value) { |
@@ -193,11 +216,14 @@ void MapKVStore::Set(Isolate* isolate, Local<String> key, Local<String> value) { |
193 | 216 | } |
194 | 217 | } |
195 | 218 |
|
196 | | -int32_t MapKVStore::Query(Isolate* isolate, Local<String> key) const { |
| 219 | +int32_t MapKVStore::Query(const char* key) const { |
197 | 220 | Mutex::ScopedLock lock(mutex_); |
| 221 | + return map_.find(key) == map_.end() ? -1 : 0; |
| 222 | +} |
| 223 | + |
| 224 | +int32_t MapKVStore::Query(Isolate* isolate, Local<String> key) const { |
198 | 225 | Utf8Value str(isolate, key); |
199 | | - auto it = map_.find(std::string(*str, str.length())); |
200 | | - return it == map_.end() ? -1 : 0; |
| 226 | + return Query(*str); |
201 | 227 | } |
202 | 228 |
|
203 | 229 | void MapKVStore::Delete(Isolate* isolate, Local<String> key) { |
|