mirror of
https://github.com/facebook/react-native.git
synced 2026-11-18 20:19:07 +00:00
Define console.createTask (#54338)
Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/54338 # Changelog: [Internal] Adds a basic implementation of the `console.createTask()` method. When this method is called, the `ConsoleTaskContext` is allocated. When `task.run()` is called, the corresponding `ConsoleTask` that has a pointer to `ConsoleTaskContext` will push it onto the stack in `ConsoleTaskOrchestrator`. Also adds types to corresponding Flow declarations. Reviewed By: sbuggay Differential Revision: D85481865 fbshipit-source-id: 31c96dba9eb4e6cb76f6e5a13661fe40e4d49807
This commit is contained in:
1 parent
98be2d2f92
commit
33ec06154a
4 files changed
+237
No files matched your search
@@ -571,6 +571,11 @@ function consoleAssertPolyfill(expression, label) {
|
||||
|
||||
function stub() {}
|
||||
|
||||
// https://developer.chrome.com/docs/devtools/console/api#createtask
|
||||
function consoleCreateTaskStub() {
|
||||
return {run: cb => cb()};
|
||||
}
|
||||
|
||||
if (global.nativeLoggingHook) {
|
||||
const originalConsole = global.console;
|
||||
// Preserve the original `console` as `originalConsole`
|
||||
@@ -587,6 +592,7 @@ if (global.nativeLoggingHook) {
|
||||
timeStamp: stub,
|
||||
count: stub,
|
||||
countReset: stub,
|
||||
createTask: consoleCreateTaskStub,
|
||||
...(originalConsole ?? {}),
|
||||
error: getNativeLogFunction(LOG_LEVELS.error),
|
||||
info: getNativeLogFunction(LOG_LEVELS.info),
|
||||
@@ -705,6 +711,7 @@ if (global.nativeLoggingHook) {
|
||||
time: stub,
|
||||
timeEnd: stub,
|
||||
timeStamp: stub,
|
||||
createTask: consoleCreateTaskStub,
|
||||
};
|
||||
|
||||
Object.defineProperty(console, '_isPolyfilled', {
|
||||
|
||||
@@ -5,6 +5,9 @@
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
#include "ConsoleTask.h"
|
||||
#include "ConsoleTaskContext.h"
|
||||
|
||||
#include <jsinspector-modern/RuntimeTarget.h>
|
||||
#include <jsinspector-modern/tracing/PerformanceTracer.h>
|
||||
|
||||
@@ -521,6 +524,68 @@ void installConsoleTimeStamp(
|
||||
})));
|
||||
}
|
||||
|
||||
/**
|
||||
* run method of the task object returned from console.createTask().
|
||||
*/
|
||||
jsi::Value consoleTaskRun(
|
||||
jsi::Runtime& runtime,
|
||||
const jsi::Value* args,
|
||||
size_t count,
|
||||
std::shared_ptr<ConsoleTaskContext> taskContext) {
|
||||
if (count < 1 || !args[0].isObject()) {
|
||||
throw JSError(runtime, "First argument must be a function");
|
||||
}
|
||||
auto fnObj = args[0].getObject(runtime);
|
||||
if (!fnObj.isFunction(runtime)) {
|
||||
throw JSError(runtime, "First argument must be a function");
|
||||
}
|
||||
|
||||
ConsoleTask consoleTask{taskContext};
|
||||
|
||||
auto fn = fnObj.getFunction(runtime);
|
||||
return fn.call(runtime);
|
||||
}
|
||||
|
||||
/**
|
||||
* console.createTask. Non-standardized.
|
||||
* https://developer.chrome.com/docs/devtools/console/api#createtask
|
||||
*/
|
||||
jsi::Value consoleCreateTask(
|
||||
jsi::Runtime& runtime,
|
||||
const jsi::Value* args,
|
||||
size_t count,
|
||||
RuntimeTargetDelegate& runtimeTargetDelegate,
|
||||
bool /* enabled */) {
|
||||
if (count < 1 || !args[0].isString()) {
|
||||
throw JSError(runtime, "First argument must be a non-empty string");
|
||||
}
|
||||
auto name = args[0].asString(runtime).utf8(runtime);
|
||||
if (name.empty()) {
|
||||
throw JSError(runtime, "First argument must be a non-empty string");
|
||||
}
|
||||
|
||||
jsi::Object task{runtime};
|
||||
auto taskContext = std::make_shared<ConsoleTaskContext>(
|
||||
runtime, runtimeTargetDelegate, name);
|
||||
taskContext->schedule();
|
||||
|
||||
task.setProperty(
|
||||
runtime,
|
||||
"run",
|
||||
jsi::Function::createFromHostFunction(
|
||||
runtime,
|
||||
jsi::PropNameID::forAscii(runtime, "run"),
|
||||
0,
|
||||
[taskContext](
|
||||
jsi::Runtime& runtime,
|
||||
const jsi::Value& /*thisVal*/,
|
||||
const jsi::Value* args,
|
||||
size_t count) {
|
||||
return consoleTaskRun(runtime, args, count, taskContext);
|
||||
}));
|
||||
return task;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
void RuntimeTarget::installConsoleHandler() {
|
||||
@@ -624,6 +689,33 @@ void RuntimeTarget::installConsoleHandler() {
|
||||
*/
|
||||
installConsoleTimeStamp(runtime, originalConsole, console);
|
||||
|
||||
/**
|
||||
* console.createTask
|
||||
*/
|
||||
console.setProperty(
|
||||
runtime,
|
||||
"createTask",
|
||||
jsi::Function::createFromHostFunction(
|
||||
runtime,
|
||||
jsi::PropNameID::forAscii(runtime, "createTask"),
|
||||
0,
|
||||
[state, selfWeak](
|
||||
jsi::Runtime& runtime,
|
||||
const jsi::Value& /*thisVal*/,
|
||||
const jsi::Value* args,
|
||||
size_t count) {
|
||||
jsi::Value task;
|
||||
tryExecuteSync(selfWeak, [&](auto& self) {
|
||||
task = consoleCreateTask(
|
||||
runtime,
|
||||
args,
|
||||
count,
|
||||
self.delegate_,
|
||||
self.isConsoleCreateTaskEnabled());
|
||||
});
|
||||
return task;
|
||||
}));
|
||||
|
||||
// Install forwarding console methods.
|
||||
#define FORWARDING_CONSOLE_METHOD(name, type) \
|
||||
installConsoleMethod(#name, console_##name);
|
||||
|
||||
@@ -0,0 +1,131 @@
|
||||
/*
|
||||
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
#include <folly/executors/QueuedImmediateExecutor.h>
|
||||
|
||||
#include "JsiIntegrationTest.h"
|
||||
#include "engines/JsiIntegrationTestHermesEngineAdapter.h"
|
||||
|
||||
#include <jsinspector-modern/ConsoleTaskOrchestrator.h>
|
||||
|
||||
using namespace ::testing;
|
||||
|
||||
namespace facebook::react::jsinspector_modern {
|
||||
|
||||
/**
|
||||
* A test fixture for the console.createTask API.
|
||||
*/
|
||||
class ConsoleCreateTaskTest : public JsiIntegrationPortableTestBase<
|
||||
JsiIntegrationTestHermesEngineAdapter,
|
||||
folly::QueuedImmediateExecutor> {};
|
||||
|
||||
TEST_F(ConsoleCreateTaskTest, Installed) {
|
||||
auto result = eval("typeof console.createTask");
|
||||
auto& runtime = engineAdapter_->getRuntime();
|
||||
EXPECT_EQ(result.asString(runtime).utf8(runtime), "function");
|
||||
}
|
||||
|
||||
TEST_F(ConsoleCreateTaskTest, ReturnsTaskObject) {
|
||||
auto result = eval("typeof console.createTask('test-task')");
|
||||
auto& runtime = engineAdapter_->getRuntime();
|
||||
EXPECT_EQ(result.asString(runtime).utf8(runtime), "object");
|
||||
}
|
||||
|
||||
TEST_F(ConsoleCreateTaskTest, TaskObjectHasRunMethod) {
|
||||
auto result = eval("typeof console.createTask('test-task').run");
|
||||
auto& runtime = engineAdapter_->getRuntime();
|
||||
EXPECT_EQ(result.asString(runtime).utf8(runtime), "function");
|
||||
}
|
||||
|
||||
TEST_F(ConsoleCreateTaskTest, RunMethodExecutesFunction) {
|
||||
auto result = eval(R"(
|
||||
let executed = false;
|
||||
const task = console.createTask('test-task');
|
||||
task.run(() => { executed = true; });
|
||||
executed;
|
||||
)");
|
||||
EXPECT_TRUE(result.getBool());
|
||||
}
|
||||
|
||||
TEST_F(ConsoleCreateTaskTest, RunMethodReturnsValue) {
|
||||
auto result = eval(R"(
|
||||
const task = console.createTask('test-task');
|
||||
task.run(() => 42);
|
||||
)");
|
||||
EXPECT_EQ(result.getNumber(), 42);
|
||||
}
|
||||
|
||||
TEST_F(ConsoleCreateTaskTest, ThrowsOnNoArguments) {
|
||||
EXPECT_THROW(eval("console.createTask()"), facebook::jsi::JSError);
|
||||
}
|
||||
|
||||
TEST_F(ConsoleCreateTaskTest, ThrowsOnEmptyString) {
|
||||
EXPECT_THROW(eval("console.createTask('')"), facebook::jsi::JSError);
|
||||
}
|
||||
|
||||
TEST_F(ConsoleCreateTaskTest, ThrowsOnNonStringArgument) {
|
||||
EXPECT_THROW(eval("console.createTask(123)"), facebook::jsi::JSError);
|
||||
EXPECT_THROW(eval("console.createTask(null)"), facebook::jsi::JSError);
|
||||
EXPECT_THROW(eval("console.createTask(undefined)"), facebook::jsi::JSError);
|
||||
EXPECT_THROW(eval("console.createTask({})"), facebook::jsi::JSError);
|
||||
}
|
||||
|
||||
TEST_F(ConsoleCreateTaskTest, RunMethodThrowsOnNoArguments) {
|
||||
EXPECT_THROW(
|
||||
eval(R"(
|
||||
const task = console.createTask('test-task');
|
||||
task.run();
|
||||
)"),
|
||||
facebook::jsi::JSError);
|
||||
}
|
||||
|
||||
TEST_F(ConsoleCreateTaskTest, RunMethodThrowsOnNonFunction) {
|
||||
EXPECT_THROW(
|
||||
eval(R"(
|
||||
const task = console.createTask('test-task');
|
||||
task.run(123);
|
||||
)"),
|
||||
facebook::jsi::JSError);
|
||||
EXPECT_THROW(
|
||||
eval(R"(
|
||||
const task = console.createTask('test-task');
|
||||
task.run('not a function');
|
||||
)"),
|
||||
facebook::jsi::JSError);
|
||||
EXPECT_THROW(
|
||||
eval(R"(
|
||||
const task = console.createTask('test-task');
|
||||
task.run({});
|
||||
)"),
|
||||
facebook::jsi::JSError);
|
||||
}
|
||||
|
||||
TEST_F(ConsoleCreateTaskTest, MultipleTasksCanBeCreated) {
|
||||
auto result = eval(R"(
|
||||
const task1 = console.createTask('task-1');
|
||||
const task2 = console.createTask('task-2');
|
||||
let count = 0;
|
||||
task1.run(() => { count++; });
|
||||
task2.run(() => { count++; });
|
||||
count;
|
||||
)");
|
||||
EXPECT_EQ(result.getNumber(), 2);
|
||||
}
|
||||
|
||||
TEST_F(ConsoleCreateTaskTest, TaskCanBeRunMultipleTimes) {
|
||||
auto result = eval(R"(
|
||||
const task = console.createTask('test-task');
|
||||
let count = 0;
|
||||
task.run(() => { count++; });
|
||||
task.run(() => { count++; });
|
||||
task.run(() => { count++; });
|
||||
count;
|
||||
)");
|
||||
EXPECT_EQ(result.getNumber(), 3);
|
||||
}
|
||||
|
||||
} // namespace facebook::react::jsinspector_modern
|
||||
@@ -25,6 +25,10 @@ type DevToolsColor =
|
||||
| 'warning'
|
||||
| 'error';
|
||||
|
||||
declare interface ConsoleTask {
|
||||
run<T>(f: () => T): T;
|
||||
}
|
||||
|
||||
// $FlowExpectedError[libdef-override] Flow core definitions are incomplete.
|
||||
declare var console: {
|
||||
// Logging
|
||||
@@ -75,6 +79,9 @@ declare var console: {
|
||||
detail?: {[string]: mixed},
|
||||
): void,
|
||||
|
||||
// Stack tagging
|
||||
createTask(label: string): ConsoleTask,
|
||||
|
||||
...
|
||||
};
|
||||
|
||||
|
||||
Reference in new issue
Block a user