@@ -29,17 +29,21 @@ using v8::Value;
2929class RealEnvStore final : public KVStore {
3030 public:
3131 MaybeLocal<String> Get (Isolate* isolate, Local<String> key) const override ;
32+ Maybe<std::string> Get (const char * key) const override ;
3233 void Set (Isolate* isolate, Local<String> key, Local<String> value) override ;
3334 int32_t Query (Isolate* isolate, Local<String> key) const override ;
35+ int32_t Query (const char * key) const override ;
3436 void Delete (Isolate* isolate, Local<String> key) override ;
3537 Local<Array> Enumerate (Isolate* isolate) const override ;
3638};
3739
3840class MapKVStore final : public KVStore {
3941 public:
4042 MaybeLocal<String> Get (Isolate* isolate, Local<String> key) const override ;
43+ Maybe<std::string> Get (const char * key) const override ;
4144 void Set (Isolate* isolate, Local<String> key, Local<String> value) override ;
4245 int32_t Query (Isolate* isolate, Local<String> key) const override ;
46+ int32_t Query (const char * key) const override ;
4347 void Delete (Isolate* isolate, Local<String> key) override ;
4448 Local<Array> Enumerate (Isolate* isolate) const override ;
4549
@@ -58,26 +62,36 @@ Mutex env_var_mutex;
5862std::shared_ptr<KVStore> system_environment = std::make_shared<RealEnvStore>();
5963} // namespace per_process
6064
61- MaybeLocal<String> RealEnvStore::Get (Isolate* isolate,
62- Local<String> property) const {
65+ Maybe<std::string> RealEnvStore::Get (const char * key) const {
6366 Mutex::ScopedLock lock (per_process::env_var_mutex);
6467
65- node::Utf8Value key (isolate, property);
6668 size_t init_sz = 256 ;
6769 MaybeStackBuffer<char , 256 > val;
68- int ret = uv_os_getenv (* key, *val, &init_sz);
70+ int ret = uv_os_getenv (key, *val, &init_sz);
6971
7072 if (ret == UV_ENOBUFS) {
7173 // Buffer is not large enough, reallocate to the updated init_sz
7274 // and fetch env value again.
7375 val.AllocateSufficientStorage (init_sz);
74- ret = uv_os_getenv (* key, *val, &init_sz);
76+ ret = uv_os_getenv (key, *val, &init_sz);
7577 }
7678
7779 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 ());
8195 }
8296
8397 return MaybeLocal<String>();
@@ -97,14 +111,12 @@ void RealEnvStore::Set(Isolate* isolate,
97111 uv_os_setenv (*key, *val);
98112}
99113
100- int32_t RealEnvStore::Query (Isolate* isolate, Local<String> property ) const {
114+ int32_t RealEnvStore::Query (const char * key ) const {
101115 Mutex::ScopedLock lock (per_process::env_var_mutex);
102116
103- node::Utf8Value key (isolate, property);
104-
105117 char val[2 ];
106118 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);
108120
109121 if (ret == UV_ENOENT) {
110122 return -1 ;
@@ -121,6 +133,11 @@ int32_t RealEnvStore::Query(Isolate* isolate, Local<String> property) const {
121133 return 0 ;
122134}
123135
136+ int32_t RealEnvStore::Query (Isolate* isolate, Local<String> property) const {
137+ node::Utf8Value key (isolate, property);
138+ return Query (*key);
139+ }
140+
124141void RealEnvStore::Delete (Isolate* isolate, Local<String> property) {
125142 Mutex::ScopedLock lock (per_process::env_var_mutex);
126143
@@ -174,13 +191,19 @@ std::shared_ptr<KVStore> KVStore::Clone(v8::Isolate* isolate) const {
174191 return copy;
175192}
176193
177- MaybeLocal<String > MapKVStore::Get (Isolate* isolate, Local<String> key) const {
194+ Maybe<std::string > MapKVStore::Get (const char * key) const {
178195 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 {
179201 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 ());
184207}
185208
186209void 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) {
193216 }
194217}
195218
196- int32_t MapKVStore::Query (Isolate* isolate, Local<String> key) const {
219+ int32_t MapKVStore::Query (const char * key) const {
197220 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 {
198225 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);
201227}
202228
203229void MapKVStore::Delete (Isolate* isolate, Local<String> key) {
0 commit comments