Skip to content

Commit

Permalink
Move WriteUtf8String functionality to stream_base.cpp
Browse files Browse the repository at this point in the history
Summary:
Moved WriteUtf8String functionality to stream_base.cpp
because the same write functionality is used for both
tty and pipe. This move was primarily made to mirror
the file structure that node uses.

Additionally, since uv_write is async, the data string
and the uv_write_t object need to survive past the
end of the function. Thus we created a callback where
these allocated objects are freed, and added the uv_run
line to ensure that the callback is called.

Reviewed By: avp

Differential Revision: D29909965

fbshipit-source-id: e6b0b469eca26671d3051d201e85587c454ba767
  • Loading branch information
adithiraofb authored and facebook-github-bot committed Jul 30, 2021
1 parent f0edb80 commit 831fab2
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 1 deletion.
2 changes: 1 addition & 1 deletion tools/node-hermes/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down
1 change: 1 addition & 0 deletions tools/node-hermes/InternalBindings/pipe_wrap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include "InternalBindings.h"
#include "hermes/hermes.h"
#include "stream_base.h"

#include "uv.h"

Expand Down
46 changes: 46 additions & 0 deletions tools/node-hermes/InternalBindings/stream_base.cpp
Original file line number Diff line number Diff line change
@@ -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<WriteContext *>(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;
}
19 changes: 19 additions & 0 deletions tools/node-hermes/InternalBindings/stream_base.h
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions tools/node-hermes/InternalBindings/tty_wrap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include "InternalBindings.h"
#include "hermes/hermes.h"
#include "stream_base.h"

#include "uv.h"

Expand Down
1 change: 1 addition & 0 deletions tools/node-hermes/node-hermes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 831fab2

Please sign in to comment.