6
6
#include " node_process-inl.h"
7
7
8
8
#include < time.h> // tzset(), _tzset()
9
+ #include < optional>
9
10
10
11
namespace node {
11
12
using v8::Array;
@@ -19,6 +20,7 @@ using v8::Integer;
19
20
using v8::Intercepted;
20
21
using v8::Isolate;
21
22
using v8::Just;
23
+ using v8::JustVoid;
22
24
using v8::Local;
23
25
using v8::Maybe;
24
26
using v8::MaybeLocal;
@@ -38,7 +40,7 @@ using v8::Value;
38
40
class RealEnvStore final : public KVStore {
39
41
public:
40
42
MaybeLocal<String> Get (Isolate* isolate, Local<String> key) const override ;
41
- Maybe <std::string> Get (const char * key) const override ;
43
+ std::optional <std::string> Get (const char * key) const override ;
42
44
void Set (Isolate* isolate, Local<String> key, Local<String> value) override ;
43
45
int32_t Query (Isolate* isolate, Local<String> key) const override ;
44
46
int32_t Query (const char * key) const override ;
@@ -49,7 +51,7 @@ class RealEnvStore final : public KVStore {
49
51
class MapKVStore final : public KVStore {
50
52
public:
51
53
MaybeLocal<String> Get (Isolate* isolate, Local<String> key) const override ;
52
- Maybe <std::string> Get (const char * key) const override ;
54
+ std::optional <std::string> Get (const char * key) const override ;
53
55
void Set (Isolate* isolate, Local<String> key, Local<String> value) override ;
54
56
int32_t Query (Isolate* isolate, Local<String> key) const override ;
55
57
int32_t Query (const char * key) const override ;
@@ -101,7 +103,7 @@ void DateTimeConfigurationChangeNotification(
101
103
}
102
104
}
103
105
104
- Maybe <std::string> RealEnvStore::Get (const char * key) const {
106
+ std::optional <std::string> RealEnvStore::Get (const char * key) const {
105
107
Mutex::ScopedLock lock (per_process::env_var_mutex);
106
108
107
109
size_t init_sz = 256 ;
@@ -116,19 +118,19 @@ Maybe<std::string> RealEnvStore::Get(const char* key) const {
116
118
}
117
119
118
120
if (ret >= 0 ) { // Env key value fetch success.
119
- return Just ( std::string (*val, init_sz) );
121
+ return std::string (*val, init_sz);
120
122
}
121
123
122
- return Nothing< std::string>() ;
124
+ return std::nullopt ;
123
125
}
124
126
125
127
MaybeLocal<String> RealEnvStore::Get (Isolate* isolate,
126
128
Local<String> property) const {
127
129
node::Utf8Value key (isolate, property);
128
- Maybe <std::string> value = Get (*key);
130
+ std::optional <std::string> value = Get (*key);
129
131
130
- if (value.IsJust ()) {
131
- std::string val = value.FromJust ();
132
+ if (value.has_value ()) {
133
+ std::string val = value.value ();
132
134
return String::NewFromUtf8 (
133
135
isolate, val.data (), NewStringType::kNormal , val.size ());
134
136
}
@@ -229,17 +231,17 @@ std::shared_ptr<KVStore> KVStore::Clone(Isolate* isolate) const {
229
231
return copy;
230
232
}
231
233
232
- Maybe <std::string> MapKVStore::Get (const char * key) const {
234
+ std::optional <std::string> MapKVStore::Get (const char * key) const {
233
235
Mutex::ScopedLock lock (mutex_);
234
236
auto it = map_.find (key);
235
- return it == map_.end () ? Nothing< std::string>() : Just (it->second );
237
+ return it == map_.end () ? std::nullopt : std::make_optional (it->second );
236
238
}
237
239
238
240
MaybeLocal<String> MapKVStore::Get (Isolate* isolate, Local<String> key) const {
239
241
Utf8Value str (isolate, key);
240
- Maybe <std::string> value = Get (*str);
241
- if (value.IsNothing ()) return Local <String>();
242
- std::string val = value.FromJust ();
242
+ std::optional <std::string> value = Get (*str);
243
+ if (! value.has_value ()) return MaybeLocal <String>();
244
+ std::string val = value.value ();
243
245
return String::NewFromUtf8 (
244
246
isolate, val.data (), NewStringType::kNormal , val.size ());
245
247
}
@@ -291,30 +293,29 @@ std::shared_ptr<KVStore> KVStore::CreateMapKVStore() {
291
293
return std::make_shared<MapKVStore>();
292
294
}
293
295
294
- Maybe<bool > KVStore::AssignFromObject (Local<Context> context,
296
+ Maybe<void > KVStore::AssignFromObject (Local<Context> context,
295
297
Local<Object> entries) {
296
298
Isolate* isolate = context->GetIsolate ();
297
299
HandleScope handle_scope (isolate);
298
300
Local<Array> keys;
299
301
if (!entries->GetOwnPropertyNames (context).ToLocal (&keys))
300
- return Nothing<bool >();
302
+ return Nothing<void >();
301
303
uint32_t keys_length = keys->Length ();
302
304
for (uint32_t i = 0 ; i < keys_length; i++) {
303
305
Local<Value> key;
304
- if (!keys->Get (context, i).ToLocal (&key))
305
- return Nothing<bool >();
306
+ if (!keys->Get (context, i).ToLocal (&key)) return Nothing<void >();
306
307
if (!key->IsString ()) continue ;
307
308
308
309
Local<Value> value;
309
310
Local<String> value_string;
310
311
if (!entries->Get (context, key).ToLocal (&value) ||
311
312
!value->ToString (context).ToLocal (&value_string)) {
312
- return Nothing<bool >();
313
+ return Nothing<void >();
313
314
}
314
315
315
316
Set (isolate, key.As <String>(), value_string);
316
317
}
317
- return Just ( true );
318
+ return JustVoid ( );
318
319
}
319
320
320
321
// TODO(bnoordhuis) Not super efficient but called infrequently. Not worth
0 commit comments