Skip to content

Commit

Permalink
TTY constructor in tty_wrap.cpp
Browse files Browse the repository at this point in the history
Summary: TTY constructor added in tty_wrap.cpp.

Reviewed By: avp

Differential Revision: D29746446

fbshipit-source-id: 00d54ef006583baba11b2286f0dff3b78fa4cce1
  • Loading branch information
adithiraofb authored and facebook-github-bot committed Jul 26, 2021
1 parent 36b0ff7 commit 3c5c8ab
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 1 deletion.
2 changes: 2 additions & 0 deletions tools/node-hermes/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ if(HERMES_BUILD_NODE_HERMES)
set(CXX_STANDARD 14)
set(CXX_STANDARD_REQUIRED ON)

set_source_files_properties(InternalBindings/tty_wrap.cpp PROPERTIES COMPILE_FLAGS -frtti)

add_subdirectory(third-party/libuv)
add_subdirectory(nodelib)
add_hermes_tool(node-hermes
Expand Down
42 changes: 42 additions & 0 deletions tools/node-hermes/InternalBindings/tty_wrap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,53 @@

using namespace facebook;

//// A HostObject subclass to be used as a TTY wrapper object.
class TTYStreamWrap : public jsi::HostObject {
public:
uv_tty_t *getTTYStreamHandle() {
return &ttyStreamHandle_;
}

private:
// Handle used for tty libuv stream operations.
uv_tty_t ttyStreamHandle_;
};

/// Creates a tty stream instance. Sets the error property in the second
/// argument if an error occurred. Sets the jsi::HostObject that is a wrapper
/// for the uv_tty_t stream handle on the returned jsi::Value.
static jsi::Value TTY(
RuntimeState &rs,
const jsi::Value &thisValue,
const jsi::Value *args,
size_t count) {
jsi::Runtime &rt = rs.getRuntime();
if (count < 2) {
throw jsi::JSError(
rt, "Not enough arguments being passed into TTY initialization call.");
}
auto ttyStreamObject = std::make_shared<TTYStreamWrap>();
uv_file fd = args[0].asNumber();
int err = uv_tty_init(
rs.getLoop(), ttyStreamObject.get()->getTTYStreamHandle(), fd, 0);
if (err != 0)
args[1].asObject(rt).setProperty(rt, "code", err);
else
thisValue.asObject(rt).setProperty(
rt,
rs.getTTYStreamPropId(),
jsi::Object::createFromHostObject(rt, ttyStreamObject));

return jsi::Value(rt, thisValue);
}

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

rs.defineJSFunction(TTY, "TTY", 2, tty_wrap);

rs.setInternalBindingProp("tty_wrap", std::move(tty_wrap));
return rs.getInternalBindingProp("tty_wrap");
}
11 changes: 10 additions & 1 deletion tools/node-hermes/RuntimeState.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

#include "hermes/hermes.h"

#include "jsi/jsi.h"
#include "llvh/Support/CommandLine.h"
#include "llvh/Support/FileSystem.h"
#include "llvh/Support/InitLLVM.h"
Expand All @@ -32,7 +33,9 @@ class RuntimeState {
RuntimeState(llvh::SmallString<32> dirname, uv_loop_t *loop)
: rt_(hermes::makeHermesRuntime()),
dirname_(std::move(dirname)),
loop_(loop){};
loop_(loop),
ttyStreamPropId_(
jsi::PropNameID::forAscii(*rt_, "%ttyStreamObject%")){};

RuntimeState(const RuntimeState &) = delete;
RuntimeState &operator=(const RuntimeState &) = delete;
Expand Down Expand Up @@ -106,6 +109,10 @@ class RuntimeState {
return loop_;
}

jsi::PropNameID &getTTYStreamPropId() {
return ttyStreamPropId_;
}

private:
// Runtime used to access internal binding properties.
std::unique_ptr<jsi::Runtime> rt_;
Expand All @@ -117,6 +124,8 @@ class RuntimeState {
llvh::SmallString<32> dirname_;
// Event loop used for libuv.
uv_loop_t *loop_;
// Cached PropNameID corresponding to the tty_wrap stream object.
jsi::PropNameID ttyStreamPropId_;
};
} // namespace facebook
#endif

0 comments on commit 3c5c8ab

Please sign in to comment.