Skip to content

Commit

Permalink
Revert "src: do proper Maybe usage"
Browse files Browse the repository at this point in the history
This reverts commit 4bd6caf.

Refs: #47588 (comment)
Signed-off-by: Darshan Sen <raisinten@gmail.com>
  • Loading branch information
RaisinTen committed Apr 29, 2023
1 parent 4c5f8f2 commit 4d19a45
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 21 deletions.
24 changes: 11 additions & 13 deletions src/json_parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@ namespace node {
using v8::ArrayBuffer;
using v8::Context;
using v8::Isolate;
using v8::Just;
using v8::Local;
using v8::Maybe;
using v8::Nothing;
using v8::Object;
using v8::String;
using v8::Value;
Expand Down Expand Up @@ -61,7 +58,8 @@ bool JSONParser::Parse(const std::string& content) {
return true;
}

Maybe<std::string> JSONParser::GetTopLevelStringField(std::string_view field) {
std::optional<std::string> JSONParser::GetTopLevelStringField(
std::string_view field) {
Isolate* isolate = isolate_.get();
Local<Context> context = context_.Get(isolate);
Local<Object> content_object = content_.Get(isolate);
Expand All @@ -71,17 +69,17 @@ Maybe<std::string> JSONParser::GetTopLevelStringField(std::string_view field) {
isolate, errors::PrinterTryCatch::kDontPrintSourceLine);
Local<Value> field_local;
if (!ToV8Value(context, field, isolate).ToLocal(&field_local)) {
return Nothing<std::string>();
return {};
}
if (!content_object->Get(context, field_local).ToLocal(&value) ||
!value->IsString()) {
return Nothing<std::string>();
return {};
}
Utf8Value utf8_value(isolate, value);
return Just(utf8_value.ToString());
return utf8_value.ToString();
}

Maybe<bool> JSONParser::GetTopLevelBoolField(std::string_view field) {
std::optional<bool> JSONParser::GetTopLevelBoolField(std::string_view field) {
Isolate* isolate = isolate_.get();
Local<Context> context = context_.Get(isolate);
Local<Object> content_object = content_.Get(isolate);
Expand All @@ -92,19 +90,19 @@ Maybe<bool> JSONParser::GetTopLevelBoolField(std::string_view field) {
isolate, errors::PrinterTryCatch::kDontPrintSourceLine);
Local<Value> field_local;
if (!ToV8Value(context, field, isolate).ToLocal(&field_local)) {
return Nothing<bool>();
return {};
}
if (!content_object->Has(context, field_local).To(&has_field)) {
return Nothing<bool>();
return {};
}
if (!has_field) {
return Just(false);
return false;
}
if (!content_object->Get(context, field_local).ToLocal(&value) ||
!value->IsBoolean()) {
return Nothing<bool>();
return {};
}
return Just(value->BooleanValue(isolate));
return value->BooleanValue(isolate);
}

} // namespace node
5 changes: 3 additions & 2 deletions src/json_parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS

#include <memory>
#include <optional>
#include <string>
#include "util.h"
#include "v8.h"
Expand All @@ -17,8 +18,8 @@ class JSONParser {
JSONParser();
~JSONParser() {}
bool Parse(const std::string& content);
v8::Maybe<std::string> GetTopLevelStringField(std::string_view field);
v8::Maybe<bool> GetTopLevelBoolField(std::string_view field);
std::optional<std::string> GetTopLevelStringField(std::string_view field);
std::optional<bool> GetTopLevelBoolField(std::string_view field);

private:
// We might want a lighter-weight JSON parser for this use case. But for now
Expand Down
12 changes: 6 additions & 6 deletions src/node_sea.cc
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ std::optional<SeaConfig> ParseSingleExecutableConfig(
}

result.main_path =
parser.GetTopLevelStringField("main").FromMaybe(std::string());
parser.GetTopLevelStringField("main").value_or(std::string());
if (result.main_path.empty()) {
FPrintF(stderr,
"\"main\" field of %s is not a non-empty string\n",
Expand All @@ -170,23 +170,23 @@ std::optional<SeaConfig> ParseSingleExecutableConfig(
}

result.output_path =
parser.GetTopLevelStringField("output").FromMaybe(std::string());
parser.GetTopLevelStringField("output").value_or(std::string());
if (result.output_path.empty()) {
FPrintF(stderr,
"\"output\" field of %s is not a non-empty string\n",
config_path);
return std::nullopt;
}

bool disable_experimental_sea_warning;
if (!parser.GetTopLevelBoolField("disableExperimentalSEAWarning")
.To(&disable_experimental_sea_warning)) {
std::optional<bool> disable_experimental_sea_warning =
parser.GetTopLevelBoolField("disableExperimentalSEAWarning");
if (!disable_experimental_sea_warning.has_value()) {
FPrintF(stderr,
"\"disableExperimentalSEAWarning\" field of %s is not a Boolean\n",
config_path);
return std::nullopt;
}
if (disable_experimental_sea_warning) {
if (disable_experimental_sea_warning.value()) {
result.flags |= SeaFlags::kDisableExperimentalSeaWarning;
}

Expand Down

0 comments on commit 4d19a45

Please sign in to comment.