Skip to content

Commit

Permalink
guessHandleType in util.cpp
Browse files Browse the repository at this point in the history
Summary:
guessHandleType functionality added in
util.cpp.

Reviewed By: neildhar

Differential Revision: D29746448

fbshipit-source-id: e731ebb762b459e6b4993464e3d4c948dd4ea8d3
  • Loading branch information
adithiraofb authored and facebook-github-bot committed Jul 26, 2021
1 parent 3d31993 commit 5fe85ef
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
42 changes: 42 additions & 0 deletions tools/node-hermes/InternalBindings/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,53 @@

using namespace facebook;

/// Returns the const char* equivalent of the uv_handle_type.
static const char *handleType(uv_handle_type t) {
switch (t) {
case UV_TCP:
return "TCP";
case UV_TTY:
return "TTY";
case UV_UDP:
return "UDP";
case UV_FILE:
return "FILE";
case UV_NAMED_PIPE:
return "PIPE";
case UV_UNKNOWN_HANDLE:
return "UNKNOWN";
default:
llvm_unreachable("No other uv type exists.");
}
}

/// Given a file descriptor, this function will return the type it is. This
/// function is just a wrapper for uv_guess_handle.
static jsi::Value guessHandleType(
RuntimeState &rs,
const jsi::Value &,
const jsi::Value *args,
size_t count) {
jsi::Runtime &rt = rs.getRuntime();
if (count < 1) {
throw jsi::JSError(
rt,
"Not enough arguments being passed into synchronous guessHandleType call.");
}
int fd = args[0].asNumber();
uv_handle_type t = uv_guess_handle(fd);
const char *type = handleType(t);

return jsi::String::createFromAscii(rt, type);
}

/// Adds the 'util' object as a property of internalBinding.
jsi::Value facebook::utilBinding(RuntimeState &rs) {
jsi::Runtime &rt = rs.getRuntime();
jsi::Object util{rt};

rs.defineJSFunction(guessHandleType, "guessHandleType", 1, util);

rs.setInternalBindingProp("util", std::move(util));
return rs.getInternalBindingProp("util");
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ var _require = require('internal/process/signal'),
// process.on('removeListener', stopListeningIfSignal); // ---- keep the attachment of the wrappers above so that it's easier to ----
// ---- compare the setups side-by-side -----

// var _internalBinding = internalBinding('util'),
// guessHandleType = _internalBinding.guessHandleType;
var _internalBinding = internalBinding('util'),
guessHandleType = _internalBinding.guessHandleType;

function createWritableStdioStream(fd) {
var stream; // Note stream._type is used for test-module-load-list.js
Expand Down

0 comments on commit 5fe85ef

Please sign in to comment.