diff --git a/tools/node-hermes/CMakeLists.txt b/tools/node-hermes/CMakeLists.txt index 19c87659fb5..42c5446573e 100644 --- a/tools/node-hermes/CMakeLists.txt +++ b/tools/node-hermes/CMakeLists.txt @@ -19,7 +19,7 @@ if(HERMES_BUILD_NODE_HERMES) node-hermes.cpp RuntimeState.cpp InternalBindings/buffer.cpp InternalBindings/constants.cpp InternalBindings/file.cpp InternalBindings/util.cpp InternalBindings/tty_wrap.cpp - InternalBindings/pipe_wrap.cpp + InternalBindings/pipe_wrap.cpp InternalBindings/stream_base.cpp ${ALL_HEADER_FILES} LINK_LIBS hermesapi hermesNodeBytecode uv_a ) diff --git a/tools/node-hermes/InternalBindings/pipe_wrap.cpp b/tools/node-hermes/InternalBindings/pipe_wrap.cpp index 472d995622b..5f40b328671 100644 --- a/tools/node-hermes/InternalBindings/pipe_wrap.cpp +++ b/tools/node-hermes/InternalBindings/pipe_wrap.cpp @@ -7,6 +7,7 @@ #include "InternalBindings.h" #include "hermes/hermes.h" +#include "stream_base.h" #include "uv.h" diff --git a/tools/node-hermes/InternalBindings/stream_base.cpp b/tools/node-hermes/InternalBindings/stream_base.cpp new file mode 100644 index 00000000000..67776c8b00c --- /dev/null +++ b/tools/node-hermes/InternalBindings/stream_base.cpp @@ -0,0 +1,46 @@ +/* + * 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 "stream_base.h" +#include "../RuntimeState.h" +#include "hermes/hermes.h" + +#include "uv.h" + +using namespace facebook; + +class WriteContext { + public: + std::string data_; + WriteContext(std::string data) { + data_ = data; + } +}; + +/// Callback for cleaning up the allocations made in streamBaseWriteUtf8String. +/// Necessary because uv_write can be async. +static void afterWrite(uv_write_t *req, int status) { + WriteContext *context = reinterpret_cast(req->data); + delete context; + free(req); +} + +int facebook::streamBaseWriteUtf8String( + RuntimeState &rs, + const jsi::Value &arg, + uv_stream_t *stream) { + jsi::Runtime &rt = rs.getRuntime(); + + WriteContext *writeContext = new WriteContext(arg.toString(rt).utf8(rt)); + uv_write_t *writeReq = (uv_write_t *)malloc(sizeof(uv_write_t)); + writeReq->data = writeContext; + uv_buf_t buf = + uv_buf_init(&writeContext->data_[0], writeContext->data_.size()); + + int err = uv_write(writeReq, stream, &buf, 1, afterWrite); + return err; +} diff --git a/tools/node-hermes/InternalBindings/stream_base.h b/tools/node-hermes/InternalBindings/stream_base.h new file mode 100644 index 00000000000..1dbf50cf7a5 --- /dev/null +++ b/tools/node-hermes/InternalBindings/stream_base.h @@ -0,0 +1,19 @@ +/* + * 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 "../RuntimeState.h" +#include "hermes/hermes.h" + +namespace facebook { +/// Given the data argument, initializes a uv_buf and +/// writes to the stream associated with the object this +/// function is defined on. +int streamBaseWriteUtf8String( + RuntimeState &rs, + const jsi::Value &arg, + uv_stream_t *stream); +} // namespace facebook diff --git a/tools/node-hermes/InternalBindings/tty_wrap.cpp b/tools/node-hermes/InternalBindings/tty_wrap.cpp index 51d7a257436..2b6269a0478 100644 --- a/tools/node-hermes/InternalBindings/tty_wrap.cpp +++ b/tools/node-hermes/InternalBindings/tty_wrap.cpp @@ -7,6 +7,7 @@ #include "InternalBindings.h" #include "hermes/hermes.h" +#include "stream_base.h" #include "uv.h" diff --git a/tools/node-hermes/node-hermes.cpp b/tools/node-hermes/node-hermes.cpp index 534466637cd..fba102323e6 100644 --- a/tools/node-hermes/node-hermes.cpp +++ b/tools/node-hermes/node-hermes.cpp @@ -318,6 +318,7 @@ int main(int argc, char **argv) { try { initialize(rs, builtinModules, intBinding); auto result = rt.evaluateJavaScript(jsiBuffer, srcPath); + uv_run(rs.getLoop(), UV_RUN_DEFAULT); } catch (const jsi::JSIException &e) { llvh::errs() << "JavaScript terminated via uncaught exception: " << e.what()