forked from facebook/hermes
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move WriteUtf8String functionality to stream_base.cpp
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
1 parent
f0edb80
commit 831fab2
Showing
6 changed files
with
69 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ | |
|
||
#include "InternalBindings.h" | ||
#include "hermes/hermes.h" | ||
#include "stream_base.h" | ||
|
||
#include "uv.h" | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ | |
|
||
#include "InternalBindings.h" | ||
#include "hermes/hermes.h" | ||
#include "stream_base.h" | ||
|
||
#include "uv.h" | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters