Skip to content

Commit

Permalink
src: use As() instead of Cast() for conversions
Browse files Browse the repository at this point in the history
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 <darshan.sen@postman.com>

PR-URL: #40287
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
Reviewed-By: Juan José Arboleda <soyjuanarbol@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Gerhard Stöbich <deb2001-github@yahoo.de>
  • Loading branch information
RaisinTen authored and danielleadams committed Oct 5, 2021
1 parent abfcbcd commit b694b0c
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 21 deletions.
5 changes: 2 additions & 3 deletions src/cares_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1701,7 +1701,7 @@ void SetServers(const FunctionCallbackInfo<Value>& args) {

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

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

uint32_t len = arr->Length();

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

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

CHECK(elm->Get(env->context(),
0).ToLocalChecked()->Int32Value(env->context()).FromJust());
Expand Down
7 changes: 3 additions & 4 deletions src/js_native_api_v8.cc
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ inline static napi_status ConcludeDeferred(napi_env env,
v8::Local<v8::Value> v8_deferred =
v8::Local<v8::Value>::New(env->isolate, *deferred_ref);

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

v8::Maybe<bool> success = is_resolved ?
v8_resolver->Resolve(context, v8impl::V8LocalValueFromJsValue(result)) :
Expand Down Expand Up @@ -293,7 +293,7 @@ class CallbackWrapperBase : public CallbackWrapper {
nullptr),
_cbinfo(cbinfo) {
_bundle = reinterpret_cast<CallbackBundle*>(
v8::Local<v8::External>::Cast(cbinfo.Data())->Value());
cbinfo.Data().As<v8::External>()->Value());
_data = _bundle->cb_data;
}

Expand Down Expand Up @@ -3131,8 +3131,7 @@ napi_status napi_run_script(napi_env env,

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

auto maybe_script = v8::Script::Compile(context,
v8::Local<v8::String>::Cast(v8_script));
auto maybe_script = v8::Script::Compile(context, v8_script.As<v8::String>());
CHECK_MAYBE_EMPTY(env, maybe_script, napi_generic_failure);

auto script_result =
Expand Down
4 changes: 2 additions & 2 deletions src/module_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -292,8 +292,8 @@ void ModuleWrap::Link(const FunctionCallbackInfo<Value>& args) {
for (int i = 0; i < raw_assertions->Length(); i += 3) {
assertions
->Set(env->context(),
Local<String>::Cast(raw_assertions->Get(env->context(), i)),
Local<Value>::Cast(raw_assertions->Get(env->context(), i + 1)))
raw_assertions->Get(env->context(), i).As<String>(),
raw_assertions->Get(env->context(), i + 1).As<Value>())
.ToChecked();
}

Expand Down
4 changes: 2 additions & 2 deletions src/node_contextify.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ class ContextifyContext {
}

inline v8::Local<v8::Object> sandbox() const {
return v8::Local<v8::Object>::Cast(
context()->GetEmbedderData(ContextEmbedderIndex::kSandboxObject));
return context()->GetEmbedderData(ContextEmbedderIndex::kSandboxObject)
.As<v8::Object>();
}

inline std::shared_ptr<v8::MicrotaskQueue> microtask_queue() const {
Expand Down
16 changes: 8 additions & 8 deletions src/node_dtrace.cc
Original file line number Diff line number Diff line change
Expand Up @@ -87,16 +87,16 @@ using v8::Value;
return node::THROW_ERR_INVALID_ARG_TYPE(env, \
"expected object for " #obj " to contain object member " #member); \
} \
*valp = Local<Object>::Cast(obj->Get(env->context(), \
OneByteString(env->isolate(), #member)).ToLocalChecked());
*valp = obj->Get(env->context(), \
OneByteString(env->isolate(), #member)).ToLocalChecked().As<Object>();

#define SLURP_CONNECTION(arg, conn) \
if (!(arg)->IsObject()) { \
return node::THROW_ERR_INVALID_ARG_TYPE(env, \
"expected argument " #arg " to be a connection object"); \
} \
node_dtrace_connection_t conn; \
Local<Object> _##conn = Local<Object>::Cast(arg); \
Local<Object> _##conn = arg.As<Object>(); \
Local<Value> _handle = \
(_##conn)->Get(env->context(), \
FIXED_ONE_BYTE_STRING(env->isolate(), "_handle")) \
Expand All @@ -116,7 +116,7 @@ using v8::Value;
"expected argument " #arg " to be a connection object"); \
} \
node_dtrace_connection_t conn; \
Local<Object> _##conn = Local<Object>::Cast(arg); \
Local<Object> _##conn = arg.As<Object>(); \
SLURP_INT(_##conn, fd, &conn.fd); \
SLURP_STRING(_##conn, host, &conn.remote); \
SLURP_INT(_##conn, port, &conn.port); \
Expand All @@ -132,10 +132,10 @@ using v8::Value;
"expected argument " #arg1 " to be a connection object"); \
} \
node_dtrace_connection_t conn; \
Local<Object> _##conn = Local<Object>::Cast(arg0); \
Local<Object> _##conn = arg0.As<Object>(); \
SLURP_INT(_##conn, fd, &conn.fd); \
SLURP_INT(_##conn, bufferSize, &conn.buffered); \
_##conn = Local<Object>::Cast(arg1); \
_##conn = arg1.As<Object>(); \
SLURP_STRING(_##conn, host, &conn.remote); \
SLURP_INT(_##conn, port, &conn.port);

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

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

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

req.method = header;
Expand Down
4 changes: 2 additions & 2 deletions src/process_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ class ProcessWrap : public HandleWrap {
Local<Value> argv_v =
js_options->Get(context, env->args_string()).ToLocalChecked();
if (!argv_v.IsEmpty() && argv_v->IsArray()) {
Local<Array> js_argv = Local<Array>::Cast(argv_v);
Local<Array> js_argv = argv_v.As<Array>();
int argc = js_argv->Length();
CHECK_GT(argc + 1, 0); // Check for overflow.

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

0 comments on commit b694b0c

Please sign in to comment.