@@ -30,17 +30,21 @@ using v8::Value;
3030class RealEnvStore final : public KVStore{
3131public:
3232 MaybeLocal<String> Get (Isolate* isolate, Local<String> key) const override ;
33+ Maybe<std::string> Get (const char * key) const override ;
3334void Set (Isolate* isolate, Local<String> key, Local<String> value) override ;
3435int32_t Query (Isolate* isolate, Local<String> key) const override ;
36+ int32_t Query (const char * key) const override ;
3537void Delete (Isolate* isolate, Local<String> key) override ;
3638 Local<Array> Enumerate (Isolate* isolate) const override ;
3739};
3840
3941class MapKVStore final : public KVStore{
4042public:
4143 MaybeLocal<String> Get (Isolate* isolate, Local<String> key) const override ;
44+ Maybe<std::string> Get (const char * key) const override ;
4245void Set (Isolate* isolate, Local<String> key, Local<String> value) override ;
4346int32_t Query (Isolate* isolate, Local<String> key) const override ;
47+ int32_t Query (const char * key) const override ;
4448void Delete (Isolate* isolate, Local<String> key) override ;
4549 Local<Array> Enumerate (Isolate* isolate) const override ;
4650
@@ -72,26 +76,36 @@ void DateTimeConfigurationChangeNotification(Isolate* isolate, const T& key){
7276 }
7377}
7478
75- MaybeLocal<String> RealEnvStore::Get (Isolate* isolate,
76- Local<String> property) const {
79+ Maybe<std::string> RealEnvStore::Get (const char * key) const {
7780 Mutex::ScopedLock lock (per_process::env_var_mutex);
7881
79- node::Utf8Value key (isolate, property);
8082size_t init_sz = 256 ;
8183 MaybeStackBuffer<char , 256 > val;
82- int ret = uv_os_getenv (* key, *val, &init_sz);
84+ int ret = uv_os_getenv (key, *val, &init_sz);
8385
8486if (ret == UV_ENOBUFS){
8587// Buffer is not large enough, reallocate to the updated init_sz
8688// and fetch env value again.
8789 val.AllocateSufficientStorage (init_sz);
88- ret = uv_os_getenv (* key, *val, &init_sz);
90+ ret = uv_os_getenv (key, *val, &init_sz);
8991 }
9092
9193if (ret >= 0 ){// Env key value fetch success.
92- MaybeLocal<String> value_string =
93- String::NewFromUtf8 (isolate, *val, NewStringType::kNormal , init_sz);
94- return value_string;
94+ return v8::Just (std::string (*val, init_sz));
95+ }
96+
97+ return v8::Nothing<std::string>();
98+ }
99+
100+ MaybeLocal<String> RealEnvStore::Get (Isolate* isolate,
101+ Local<String> property) const {
102+ node::Utf8Value key (isolate, property);
103+ Maybe<std::string> value = Get (*key);
104+
105+ if (value.IsJust ()){
106+ std::string val = value.FromJust ();
107+ return String::NewFromUtf8 (
108+ isolate, val.data (), NewStringType::kNormal , val.size ());
95109 }
96110
97111return MaybeLocal<String>();
@@ -112,14 +126,12 @@ void RealEnvStore::Set(Isolate* isolate,
112126DateTimeConfigurationChangeNotification (isolate, key);
113127}
114128
115- int32_t RealEnvStore::Query (Isolate* isolate, Local<String> property ) const {
129+ int32_t RealEnvStore::Query (const char * key ) const {
116130 Mutex::ScopedLock lock (per_process::env_var_mutex);
117131
118- node::Utf8Value key (isolate, property);
119-
120132char val[2 ];
121133size_t init_sz = sizeof (val);
122- int ret = uv_os_getenv (* key, val, &init_sz);
134+ int ret = uv_os_getenv (key, val, &init_sz);
123135
124136if (ret == UV_ENOENT){
125137return -1 ;
@@ -136,6 +148,11 @@ int32_t RealEnvStore::Query(Isolate* isolate, Local<String> property) const{
136148return 0 ;
137149}
138150
151+ int32_t RealEnvStore::Query (Isolate* isolate, Local<String> property) const {
152+ node::Utf8Value key (isolate, property);
153+ return Query (*key);
154+ }
155+
139156void RealEnvStore::Delete (Isolate* isolate, Local<String> property){
140157 Mutex::ScopedLock lock (per_process::env_var_mutex);
141158
@@ -190,13 +207,19 @@ std::shared_ptr<KVStore> KVStore::Clone(v8::Isolate* isolate) const{
190207return copy;
191208}
192209
193- MaybeLocal<String > MapKVStore::Get (Isolate* isolate, Local<String> key) const {
210+ Maybe<std::string > MapKVStore::Get (const char * key) const {
194211 Mutex::ScopedLock lock (mutex_);
212+ auto it = map_.find (key);
213+ return it == map_.end () ? v8::Nothing<std::string>() : v8::Just (it->second );
214+ }
215+
216+ MaybeLocal<String> MapKVStore::Get (Isolate* isolate, Local<String> key) const {
195217 Utf8Value str (isolate, key);
196- auto it = map_.find (std::string (*str, str.length ()));
197- if (it == map_.end ()) return Local<String>();
198- return String::NewFromUtf8 (isolate, it->second .data (),
199- NewStringType::kNormal , it->second .size ());
218+ Maybe<std::string> value = Get (*str);
219+ if (value.IsNothing ()) return Local<String>();
220+ std::string val = value.FromJust ();
221+ return String::NewFromUtf8 (
222+ isolate, val.data (), NewStringType::kNormal , val.size ());
200223}
201224
202225void MapKVStore::Set (Isolate* isolate, Local<String> key, Local<String> value){
@@ -209,11 +232,14 @@ void MapKVStore::Set(Isolate* isolate, Local<String> key, Local<String> value){
209232 }
210233}
211234
212- int32_t MapKVStore::Query (Isolate* isolate, Local<String> key) const {
235+ int32_t MapKVStore::Query (const char * key) const {
213236 Mutex::ScopedLock lock (mutex_);
237+ return map_.find (key) == map_.end () ? -1 : 0 ;
238+ }
239+
240+ int32_t MapKVStore::Query (Isolate* isolate, Local<String> key) const {
214241 Utf8Value str (isolate, key);
215- auto it = map_.find (std::string (*str, str.length ()));
216- return it == map_.end () ? -1 : 0 ;
242+ return Query (*str);
217243}
218244
219245void MapKVStore::Delete (Isolate* isolate, Local<String> key){
0 commit comments