Skip to content

Commit

Permalink
Access runtime on correct thread for RuntimeConsoleLog
Browse files Browse the repository at this point in the history
Summary:
The test code for RuntimeConsoleLog was accessing the runtime not on
the runtime thread, which could result in a race between the domain
agent's initialization code and the code that's accessing the runtime.

Reviewed By: mattbfb

Differential Revision: D57009804

fbshipit-source-id: fcbafcd713d2ea40bad4098202291de30e2746f0
  • Loading branch information
dannysu authored and facebook-github-bot committed May 6, 2024
1 parent e2216d4 commit 4ab9f17
Showing 1 changed file with 41 additions and 30 deletions.
71 changes: 41 additions & 30 deletions unittests/API/CDPAgentTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2838,37 +2838,48 @@ TEST_F(CDPAgentTest, RuntimeConsoleLog) {
constexpr double kTimestamp = 123.0;
const std::string kStringValue = "string value";

runtime_->global().setProperty(
*runtime_,
"consoleLog",
jsi::Function::createFromHostFunction(
waitFor<bool>([this, timestamp = kTimestamp, kStringValue](auto promise) {
runtimeThread_->add([this, timestamp, kStringValue, promise]() {
runtime_->global().setProperty(
*runtime_,
jsi::PropNameID::forAscii(*runtime_, "consoleLog"),
0,
[this, timestamp = kTimestamp, kStringValue](
jsi::Runtime &, const jsi::Value &, const jsi::Value *, size_t) {
jsi::String arg0 =
jsi::String::createFromAscii(*runtime_, kStringValue);

jsi::Object arg1 = jsi::Object(*runtime_);
arg1.setProperty(*runtime_, "number1", 1);
arg1.setProperty(*runtime_, "bool1", false);

jsi::Object arg2 = jsi::Object(*runtime_);
arg2.setProperty(*runtime_, "number2", 2);
arg2.setProperty(*runtime_, "bool2", true);

ConsoleMessage message(
timestamp, ConsoleAPIType::kWarning, std::vector<jsi::Value>());
message.args.reserve(3);
message.args.push_back(std::move(arg0));
message.args.push_back(std::move(arg1));
message.args.push_back(std::move(arg2));
message.stackTrace = runtime_->getDebugger().captureStackTrace();
cdpDebugAPI_->addConsoleMessage(std::move(message));

return jsi::Value::undefined();
}));
"consoleLog",
jsi::Function::createFromHostFunction(
*runtime_,
jsi::PropNameID::forAscii(*runtime_, "consoleLog"),
0,
[this, timestamp, kStringValue](
jsi::Runtime &,
const jsi::Value &,
const jsi::Value *,
size_t) {
jsi::String arg0 =
jsi::String::createFromAscii(*runtime_, kStringValue);

jsi::Object arg1 = jsi::Object(*runtime_);
arg1.setProperty(*runtime_, "number1", 1);
arg1.setProperty(*runtime_, "bool1", false);

jsi::Object arg2 = jsi::Object(*runtime_);
arg2.setProperty(*runtime_, "number2", 2);
arg2.setProperty(*runtime_, "bool2", true);

ConsoleMessage message(
timestamp,
ConsoleAPIType::kWarning,
std::vector<jsi::Value>());
message.args.reserve(3);
message.args.push_back(std::move(arg0));
message.args.push_back(std::move(arg1));
message.args.push_back(std::move(arg2));
message.stackTrace =
runtime_->getDebugger().captureStackTrace();
cdpDebugAPI_->addConsoleMessage(std::move(message));

return jsi::Value::undefined();
}));
promise->set_value(true);
});
});

// Startup
sendAndCheckResponse("Runtime.enable", msgId++);
Expand Down

0 comments on commit 4ab9f17

Please sign in to comment.