Skip to content

Commit b694b0c

Browse files
RaisinTendanielleadams
authored andcommitted
src: use As() instead of Cast() for conversions
We mostly use `As()` instead of `Cast()` in our code, so this change replaces the remaining calls to `Cast()` with calls to `As()` to maintain the consistency. Refs: https://github.com/nodejs/node/pull/39921/files#r702293529 Signed-off-by: Darshan Sen <[email protected]> PR-URL: #40287 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Tobias Nießen <[email protected]> Reviewed-By: Juan José Arboleda <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Gerhard Stöbich <[email protected]>
1 parent abfcbcd commit b694b0c

6 files changed

+19
-21
lines changed

src/cares_wrap.cc

+2-3
Original file line numberDiff line numberDiff line change
@@ -1701,7 +1701,7 @@ void SetServers(const FunctionCallbackInfo<Value>& args) {
17011701

17021702
CHECK(args[0]->IsArray());
17031703

1704-
Local<Array> arr = Local<Array>::Cast(args[0]);
1704+
Local<Array> arr = args[0].As<Array>();
17051705

17061706
uint32_t len = arr->Length();
17071707

@@ -1718,8 +1718,7 @@ void SetServers(const FunctionCallbackInfo<Value>& args) {
17181718
for (uint32_t i = 0; i < len; i++) {
17191719
CHECK(arr->Get(env->context(), i).ToLocalChecked()->IsArray());
17201720

1721-
Local<Array> elm =
1722-
Local<Array>::Cast(arr->Get(env->context(), i).ToLocalChecked());
1721+
Local<Array> elm = arr->Get(env->context(), i).ToLocalChecked().As<Array>();
17231722

17241723
CHECK(elm->Get(env->context(),
17251724
0).ToLocalChecked()->Int32Value(env->context()).FromJust());

src/js_native_api_v8.cc

+3-4
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ inline static napi_status ConcludeDeferred(napi_env env,
175175
v8::Local<v8::Value> v8_deferred =
176176
v8::Local<v8::Value>::New(env->isolate, *deferred_ref);
177177

178-
auto v8_resolver = v8::Local<v8::Promise::Resolver>::Cast(v8_deferred);
178+
auto v8_resolver = v8_deferred.As<v8::Promise::Resolver>();
179179

180180
v8::Maybe<bool> success = is_resolved ?
181181
v8_resolver->Resolve(context, v8impl::V8LocalValueFromJsValue(result)) :
@@ -293,7 +293,7 @@ class CallbackWrapperBase : public CallbackWrapper {
293293
nullptr),
294294
_cbinfo(cbinfo) {
295295
_bundle = reinterpret_cast<CallbackBundle*>(
296-
v8::Local<v8::External>::Cast(cbinfo.Data())->Value());
296+
cbinfo.Data().As<v8::External>()->Value());
297297
_data = _bundle->cb_data;
298298
}
299299

@@ -3131,8 +3131,7 @@ napi_status napi_run_script(napi_env env,
31313131

31323132
v8::Local<v8::Context> context = env->context();
31333133

3134-
auto maybe_script = v8::Script::Compile(context,
3135-
v8::Local<v8::String>::Cast(v8_script));
3134+
auto maybe_script = v8::Script::Compile(context, v8_script.As<v8::String>());
31363135
CHECK_MAYBE_EMPTY(env, maybe_script, napi_generic_failure);
31373136

31383137
auto script_result =

src/module_wrap.cc

+2-2
Original file line numberDiff line numberDiff line change
@@ -292,8 +292,8 @@ void ModuleWrap::Link(const FunctionCallbackInfo<Value>& args) {
292292
for (int i = 0; i < raw_assertions->Length(); i += 3) {
293293
assertions
294294
->Set(env->context(),
295-
Local<String>::Cast(raw_assertions->Get(env->context(), i)),
296-
Local<Value>::Cast(raw_assertions->Get(env->context(), i + 1)))
295+
raw_assertions->Get(env->context(), i).As<String>(),
296+
raw_assertions->Get(env->context(), i + 1).As<Value>())
297297
.ToChecked();
298298
}
299299

src/node_contextify.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,8 @@ class ContextifyContext {
7474
}
7575

7676
inline v8::Local<v8::Object> sandbox() const {
77-
return v8::Local<v8::Object>::Cast(
78-
context()->GetEmbedderData(ContextEmbedderIndex::kSandboxObject));
77+
return context()->GetEmbedderData(ContextEmbedderIndex::kSandboxObject)
78+
.As<v8::Object>();
7979
}
8080

8181
inline std::shared_ptr<v8::MicrotaskQueue> microtask_queue() const {

src/node_dtrace.cc

+8-8
Original file line numberDiff line numberDiff line change
@@ -87,16 +87,16 @@ using v8::Value;
8787
return node::THROW_ERR_INVALID_ARG_TYPE(env, \
8888
"expected object for " #obj " to contain object member " #member); \
8989
} \
90-
*valp = Local<Object>::Cast(obj->Get(env->context(), \
91-
OneByteString(env->isolate(), #member)).ToLocalChecked());
90+
*valp = obj->Get(env->context(), \
91+
OneByteString(env->isolate(), #member)).ToLocalChecked().As<Object>();
9292

9393
#define SLURP_CONNECTION(arg, conn) \
9494
if (!(arg)->IsObject()) { \
9595
return node::THROW_ERR_INVALID_ARG_TYPE(env, \
9696
"expected argument " #arg " to be a connection object"); \
9797
} \
9898
node_dtrace_connection_t conn; \
99-
Local<Object> _##conn = Local<Object>::Cast(arg); \
99+
Local<Object> _##conn = arg.As<Object>(); \
100100
Local<Value> _handle = \
101101
(_##conn)->Get(env->context(), \
102102
FIXED_ONE_BYTE_STRING(env->isolate(), "_handle")) \
@@ -116,7 +116,7 @@ using v8::Value;
116116
"expected argument " #arg " to be a connection object"); \
117117
} \
118118
node_dtrace_connection_t conn; \
119-
Local<Object> _##conn = Local<Object>::Cast(arg); \
119+
Local<Object> _##conn = arg.As<Object>(); \
120120
SLURP_INT(_##conn, fd, &conn.fd); \
121121
SLURP_STRING(_##conn, host, &conn.remote); \
122122
SLURP_INT(_##conn, port, &conn.port); \
@@ -132,10 +132,10 @@ using v8::Value;
132132
"expected argument " #arg1 " to be a connection object"); \
133133
} \
134134
node_dtrace_connection_t conn; \
135-
Local<Object> _##conn = Local<Object>::Cast(arg0); \
135+
Local<Object> _##conn = arg0.As<Object>(); \
136136
SLURP_INT(_##conn, fd, &conn.fd); \
137137
SLURP_INT(_##conn, bufferSize, &conn.buffered); \
138-
_##conn = Local<Object>::Cast(arg1); \
138+
_##conn = arg1.As<Object>(); \
139139
SLURP_STRING(_##conn, host, &conn.remote); \
140140
SLURP_INT(_##conn, port, &conn.port);
141141

@@ -165,7 +165,7 @@ void DTRACE_HTTP_SERVER_REQUEST(const FunctionCallbackInfo<Value>& args) {
165165

166166
Environment* env = Environment::GetCurrent(args);
167167
HandleScope scope(env->isolate());
168-
Local<Object> arg0 = Local<Object>::Cast(args[0]);
168+
Local<Object> arg0 = args[0].As<Object>();
169169
Local<Object> headers;
170170

171171
memset(&req, 0, sizeof(req));
@@ -217,7 +217,7 @@ void DTRACE_HTTP_CLIENT_REQUEST(const FunctionCallbackInfo<Value>& args) {
217217
* caller here to retain their method and URL until the time at which
218218
* DTRACE_HTTP_CLIENT_REQUEST can be called.
219219
*/
220-
Local<Object> arg0 = Local<Object>::Cast(args[0]);
220+
Local<Object> arg0 = args[0].As<Object>();
221221
SLURP_STRING(arg0, _header, &header);
222222

223223
req.method = header;

src/process_wrap.cc

+2-2
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ class ProcessWrap : public HandleWrap {
186186
Local<Value> argv_v =
187187
js_options->Get(context, env->args_string()).ToLocalChecked();
188188
if (!argv_v.IsEmpty() && argv_v->IsArray()) {
189-
Local<Array> js_argv = Local<Array>::Cast(argv_v);
189+
Local<Array> js_argv = argv_v.As<Array>();
190190
int argc = js_argv->Length();
191191
CHECK_GT(argc + 1, 0); // Check for overflow.
192192

@@ -214,7 +214,7 @@ class ProcessWrap : public HandleWrap {
214214
Local<Value> env_v =
215215
js_options->Get(context, env->env_pairs_string()).ToLocalChecked();
216216
if (!env_v.IsEmpty() && env_v->IsArray()) {
217-
Local<Array> env_opt = Local<Array>::Cast(env_v);
217+
Local<Array> env_opt = env_v.As<Array>();
218218
int envc = env_opt->Length();
219219
CHECK_GT(envc + 1, 0); // Check for overflow.
220220
options.env = new char*[envc + 1]; // Heap allocated to detect errors.

0 commit comments

Comments
 (0)