Skip to content

Commit 8968aa6

Browse files
committed
src: create parser error macro
1 parent e6e48aa commit 8968aa6

File tree

1 file changed

+19
-21
lines changed

1 file changed

+19
-21
lines changed

src/node_json_parser.cc

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
11
#include "node_json_parser.h"
2+
#include "env-inl.h"
23
#include "node_errors.h"
34
#include "node_external_reference.h"
45
#include "simdjson.h"
5-
#include "env-inl.h"
6+
#include "simdutf.h"
7+
8+
#define THROW_AND_RETURN_EMPTY_IF_SIMDJSON_ERROR(isolate, error) \
9+
do { \
10+
if ((error)) { \
11+
THROW_ERR_MODULE_NOT_INSTANTIATED((isolate)); \
12+
return MaybeLocal<Value>(); \
13+
} \
14+
} while (0)
615

716
namespace node {
817
namespace json_parser {
@@ -30,15 +39,14 @@ inline MaybeLocal<Value> ToV8Number(Isolate* isolate,
3039
T value;
3140
simdjson::error_code error = std::move(element.get<T>()).get(value);
3241

33-
if (error) {
34-
THROW_ERR_MODULE_NOT_INSTANTIATED(isolate);
35-
return MaybeLocal<Value>();
36-
}
42+
THROW_AND_RETURN_EMPTY_IF_SIMDJSON_ERROR(isolate, error);
3743

3844
return MaybeLocal<Value>(Number::New(isolate, static_cast<T>(value)));
3945
}
4046

4147
string ObjectToString(Isolate* isolate, Local<Value> value) {
48+
// TODO(araujogui): investigate if V8 WriteUtf8V2 is faster than
49+
// convert_utf16_to_utf8
4250
Utf8Value utf8_value(isolate, value);
4351
return string(*utf8_value);
4452
}
@@ -61,10 +69,7 @@ MaybeLocal<Value> ConvertSimdjsonElement(Isolate* isolate,
6169
bool value;
6270
simdjson::error_code error = element.get_bool().get(value);
6371

64-
if (error) {
65-
THROW_ERR_MODULE_NOT_INSTANTIATED(isolate);
66-
return MaybeLocal<Value>();
67-
}
72+
THROW_AND_RETURN_EMPTY_IF_SIMDJSON_ERROR(isolate, error);
6873

6974
return MaybeLocal<Value>(Boolean::New(isolate, value));
7075
}
@@ -77,21 +82,15 @@ MaybeLocal<Value> ConvertSimdjsonElement(Isolate* isolate,
7782
string value;
7883
simdjson::error_code error = element.get_string().get(value);
7984

80-
if (error) {
81-
THROW_ERR_MODULE_NOT_INSTANTIATED(isolate);
82-
return MaybeLocal<Value>();
83-
}
85+
THROW_AND_RETURN_EMPTY_IF_SIMDJSON_ERROR(isolate, error);
8486

8587
return String::NewFromUtf8(isolate, value.c_str());
8688
}
8789
case simdjson::dom::element_type::ARRAY: {
8890
simdjson::dom::array array;
8991
simdjson::error_code error = element.get_array().get(array);
9092

91-
if (error) {
92-
THROW_ERR_MODULE_NOT_INSTANTIATED(isolate);
93-
return MaybeLocal<Value>();
94-
}
93+
THROW_AND_RETURN_EMPTY_IF_SIMDJSON_ERROR(isolate, error);
9594

9695
Local<Array> v8_array =
9796
v8::Array::New(isolate, array.size());
@@ -116,10 +115,7 @@ MaybeLocal<Value> ConvertSimdjsonElement(Isolate* isolate,
116115
simdjson::dom::object object;
117116
simdjson::error_code error = element.get_object().get(object);
118117

119-
if (error) {
120-
THROW_ERR_MODULE_NOT_INSTANTIATED(isolate);
121-
return MaybeLocal<Value>();
122-
}
118+
THROW_AND_RETURN_EMPTY_IF_SIMDJSON_ERROR(isolate, error);
123119

124120
Local<Object> v8_object = Object::New(isolate);
125121
Local<Context> context = isolate->GetCurrentContext();
@@ -198,6 +194,8 @@ void Initialize(Local<Object> target,
198194
} // namespace json_parser
199195
} // namespace node
200196

197+
#undef THROW_AND_RETURN_EMPTY_IF_SIMDJSON_ERROR
198+
201199
NODE_BINDING_CONTEXT_AWARE_INTERNAL(json_parser, node::json_parser::Initialize)
202200
NODE_BINDING_EXTERNAL_REFERENCE(json_parser,
203201
node::json_parser::RegisterExternalReferences)

0 commit comments

Comments
 (0)