diff options
author | Joyee Cheung <joyeec9h3@gmail.com> | 2019-04-18 10:46:39 +0800 |
---|---|---|
committer | Joyee Cheung <joyeec9h3@gmail.com> | 2019-04-20 13:25:41 +0800 |
commit | 8d901bb44e52077c4778261764892c959a94a6f2 (patch) | |
tree | dc3e97af8f314716f3ffb6f848ccfd2536d2de64 /src/node_util.cc | |
parent | b581d596558b8ebf2906fbf0d5a9ece2b07fa8d7 (diff) | |
download | android-node-v8-8d901bb44e52077c4778261764892c959a94a6f2.tar.gz android-node-v8-8d901bb44e52077c4778261764892c959a94a6f2.tar.bz2 android-node-v8-8d901bb44e52077c4778261764892c959a94a6f2.zip |
src: move guessHandleType in the util binding
It does not make too much sense to have modules unrelated to TTY
load the TTY binding just to use this method. Put this in the
util binding instead.
PR-URL: https://github.com/nodejs/node/pull/27289
Reviewed-By: Richard Lau <riclau@uk.ibm.com>
Reviewed-By: Daniel Bevenius <daniel.bevenius@gmail.com>
Reviewed-By: Yongsheng Zhang <zyszys98@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Diffstat (limited to 'src/node_util.cc')
-rw-r--r-- | src/node_util.cc | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/src/node_util.cc b/src/node_util.cc index 960e194f76..d13624a436 100644 --- a/src/node_util.cc +++ b/src/node_util.cc @@ -211,6 +211,41 @@ class WeakReference : public BaseObject { Persistent<Object> target_; }; +static void GuessHandleType(const FunctionCallbackInfo<Value>& args) { + Environment* env = Environment::GetCurrent(args); + int fd; + if (!args[0]->Int32Value(env->context()).To(&fd)) return; + CHECK_GE(fd, 0); + + uv_handle_type t = uv_guess_handle(fd); + const char* type = nullptr; + + switch (t) { + case UV_TCP: + type = "TCP"; + break; + case UV_TTY: + type = "TTY"; + break; + case UV_UDP: + type = "UDP"; + break; + case UV_FILE: + type = "FILE"; + break; + case UV_NAMED_PIPE: + type = "PIPE"; + break; + case UV_UNKNOWN_HANDLE: + type = "UNKNOWN"; + break; + default: + ABORT(); + } + + args.GetReturnValue().Set(OneByteString(env->isolate(), type)); +} + void Initialize(Local<Object> target, Local<Value> unused, Local<Context> context, @@ -280,6 +315,8 @@ void Initialize(Local<Object> target, env->SetProtoMethod(weak_ref, "get", WeakReference::Get); target->Set(context, weak_ref_string, weak_ref->GetFunction(context).ToLocalChecked()).Check(); + + env->SetMethod(target, "guessHandleType", GuessHandleType); } } // namespace util |