diff --git a/packages/polyfills/console.js b/packages/polyfills/console.js index 04439593c96..9777f930586 100644 --- a/packages/polyfills/console.js +++ b/packages/polyfills/console.js @@ -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', { diff --git a/packages/react-native/ReactCommon/jsinspector-modern/RuntimeTargetConsole.cpp b/packages/react-native/ReactCommon/jsinspector-modern/RuntimeTargetConsole.cpp index 839869e013f..85511d92053 100644 --- a/packages/react-native/ReactCommon/jsinspector-modern/RuntimeTargetConsole.cpp +++ b/packages/react-native/ReactCommon/jsinspector-modern/RuntimeTargetConsole.cpp @@ -5,6 +5,9 @@ * LICENSE file in the root directory of this source tree. */ +#include "ConsoleTask.h" +#include "ConsoleTaskContext.h" + #include #include @@ -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 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( + 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); diff --git a/packages/react-native/ReactCommon/jsinspector-modern/tests/ConsoleCreateTaskTest.cpp b/packages/react-native/ReactCommon/jsinspector-modern/tests/ConsoleCreateTaskTest.cpp new file mode 100644 index 00000000000..79fbf844a67 --- /dev/null +++ b/packages/react-native/ReactCommon/jsinspector-modern/tests/ConsoleCreateTaskTest.cpp @@ -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 + +#include "JsiIntegrationTest.h" +#include "engines/JsiIntegrationTestHermesEngineAdapter.h" + +#include + +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 diff --git a/packages/react-native/flow/bom.js.flow b/packages/react-native/flow/bom.js.flow index 896253932a4..fe78011fa4c 100644 --- a/packages/react-native/flow/bom.js.flow +++ b/packages/react-native/flow/bom.js.flow @@ -25,6 +25,10 @@ type DevToolsColor = | 'warning' | 'error'; +declare interface ConsoleTask { + run(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, + ... };