forked from facebook/hermes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.cpp
More file actions
64 lines (55 loc) · 1.64 KB
/
util.cpp
File metadata and controls
64 lines (55 loc) · 1.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#include "InternalBindings.h"
#include "hermes/hermes.h"
#include "uv.h"
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");
}