@@ -30,17 +30,21 @@ using v8::Value;
30
30
class RealEnvStore final : public KVStore {
31
31
public:
32
32
MaybeLocal<String> Get (Isolate* isolate, Local<String> key) const override ;
33
+ Maybe<std::string> Get (const char * key) const override ;
33
34
void Set (Isolate* isolate, Local<String> key, Local<String> value) override ;
34
35
int32_t Query (Isolate* isolate, Local<String> key) const override ;
36
+ int32_t Query (const char * key) const override ;
35
37
void Delete (Isolate* isolate, Local<String> key) override ;
36
38
Local<Array> Enumerate (Isolate* isolate) const override ;
37
39
};
38
40
39
41
class MapKVStore final : public KVStore {
40
42
public:
41
43
MaybeLocal<String> Get (Isolate* isolate, Local<String> key) const override ;
44
+ Maybe<std::string> Get (const char * key) const override ;
42
45
void Set (Isolate* isolate, Local<String> key, Local<String> value) override ;
43
46
int32_t Query (Isolate* isolate, Local<String> key) const override ;
47
+ int32_t Query (const char * key) const override ;
44
48
void Delete (Isolate* isolate, Local<String> key) override ;
45
49
Local<Array> Enumerate (Isolate* isolate) const override ;
46
50
@@ -72,26 +76,36 @@ void DateTimeConfigurationChangeNotification(Isolate* isolate, const T& key) {
72
76
}
73
77
}
74
78
75
- MaybeLocal<String> RealEnvStore::Get (Isolate* isolate,
76
- Local<String> property) const {
79
+ Maybe<std::string> RealEnvStore::Get (const char * key) const {
77
80
Mutex::ScopedLock lock (per_process::env_var_mutex);
78
81
79
- node::Utf8Value key (isolate, property);
80
82
size_t init_sz = 256 ;
81
83
MaybeStackBuffer<char , 256 > val;
82
- int ret = uv_os_getenv (* key, *val, &init_sz);
84
+ int ret = uv_os_getenv (key, *val, &init_sz);
83
85
84
86
if (ret == UV_ENOBUFS) {
85
87
// Buffer is not large enough, reallocate to the updated init_sz
86
88
// and fetch env value again.
87
89
val.AllocateSufficientStorage (init_sz);
88
- ret = uv_os_getenv (* key, *val, &init_sz);
90
+ ret = uv_os_getenv (key, *val, &init_sz);
89
91
}
90
92
91
93
if (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 ());
95
109
}
96
110
97
111
return MaybeLocal<String>();
@@ -112,14 +126,12 @@ void RealEnvStore::Set(Isolate* isolate,
112
126
DateTimeConfigurationChangeNotification (isolate, key);
113
127
}
114
128
115
- int32_t RealEnvStore::Query (Isolate* isolate, Local<String> property ) const {
129
+ int32_t RealEnvStore::Query (const char * key ) const {
116
130
Mutex::ScopedLock lock (per_process::env_var_mutex);
117
131
118
- node::Utf8Value key (isolate, property);
119
-
120
132
char val[2 ];
121
133
size_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);
123
135
124
136
if (ret == UV_ENOENT) {
125
137
return -1 ;
@@ -136,6 +148,11 @@ int32_t RealEnvStore::Query(Isolate* isolate, Local<String> property) const {
136
148
return 0 ;
137
149
}
138
150
151
+ int32_t RealEnvStore::Query (Isolate* isolate, Local<String> property) const {
152
+ node::Utf8Value key (isolate, property);
153
+ return Query (*key);
154
+ }
155
+
139
156
void RealEnvStore::Delete (Isolate* isolate, Local<String> property) {
140
157
Mutex::ScopedLock lock (per_process::env_var_mutex);
141
158
@@ -190,13 +207,19 @@ std::shared_ptr<KVStore> KVStore::Clone(v8::Isolate* isolate) const {
190
207
return copy;
191
208
}
192
209
193
- MaybeLocal<String > MapKVStore::Get (Isolate* isolate, Local<String> key) const {
210
+ Maybe<std::string > MapKVStore::Get (const char * key) const {
194
211
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 {
195
217
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 ());
200
223
}
201
224
202
225
void 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) {
209
232
}
210
233
}
211
234
212
- int32_t MapKVStore::Query (Isolate* isolate, Local<String> key) const {
235
+ int32_t MapKVStore::Query (const char * key) const {
213
236
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 {
214
241
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);
217
243
}
218
244
219
245
void MapKVStore::Delete (Isolate* isolate, Local<String> key) {
0 commit comments