Skip to content

Commit 099d81a

Browse files
hoxyqfacebook-github-bot
authored andcommitted
Move up forwardToOriginalConsole declaration (#52193)
Summary: # Changelog: [Internal] Will be re-used for `console.timeStamp` installation. Reviewed By: motiz88 Differential Revision: D77027561
1 parent 5aa985b commit 099d81a

File tree

1 file changed

+31
-27
lines changed

1 file changed

+31
-27
lines changed

packages/react-native/ReactCommon/jsinspector-modern/RuntimeTargetConsole.cpp

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,36 @@ void consoleAssert(
324324
#include "ForwardingConsoleMethods.def"
325325
#undef FORWARDING_CONSOLE_METHOD
326326

327+
/**
328+
* Call innerFn and forward any arguments to the original console method
329+
* named methodName, if possible.
330+
*/
331+
jsi::Value forwardToOriginalConsole(
332+
std::shared_ptr<jsi::Object> originalConsole,
333+
const char* methodName,
334+
CallableAsHostFunction auto innerFn) {
335+
return [originalConsole = std::move(originalConsole),
336+
innerFn = std::move(innerFn),
337+
methodName](
338+
jsi::Runtime& runtime,
339+
const jsi::Value& thisVal,
340+
const jsi::Value* args,
341+
size_t count) {
342+
jsi::Value retVal = innerFn(runtime, thisVal, args, count);
343+
if (originalConsole) {
344+
auto val = originalConsole->getProperty(runtime, methodName);
345+
if (val.isObject()) {
346+
auto obj = val.getObject(runtime);
347+
if (obj.isFunction(runtime)) {
348+
auto func = obj.getFunction(runtime);
349+
func.callWithThis(runtime, *originalConsole, args, count);
350+
}
351+
}
352+
}
353+
return std::move(retVal);
354+
};
355+
};
356+
327357
} // namespace
328358

329359
void RuntimeTarget::installConsoleHandler() {
@@ -368,33 +398,6 @@ void RuntimeTarget::installConsoleHandler() {
368398
}
369399
};
370400

371-
/**
372-
* Call innerFn and forward any arguments to the original console method
373-
* named methodName, if possible.
374-
*/
375-
auto forwardToOriginalConsole = [originalConsole](
376-
const char* methodName,
377-
CallableAsHostFunction auto innerFn) {
378-
return [originalConsole, innerFn = std::move(innerFn), methodName](
379-
jsi::Runtime& runtime,
380-
const jsi::Value& thisVal,
381-
const jsi::Value* args,
382-
size_t count) {
383-
jsi::Value retVal = innerFn(runtime, thisVal, args, count);
384-
if (originalConsole) {
385-
auto val = originalConsole->getProperty(runtime, methodName);
386-
if (val.isObject()) {
387-
auto obj = val.getObject(runtime);
388-
if (obj.isFunction(runtime)) {
389-
auto func = obj.getFunction(runtime);
390-
func.callWithThis(runtime, *originalConsole, args, count);
391-
}
392-
}
393-
}
394-
return retVal;
395-
};
396-
};
397-
398401
/**
399402
* Install a console method with the given name and body. The body receives
400403
* the usual JSI host function parameters plus a ConsoleState reference, a
@@ -413,6 +416,7 @@ void RuntimeTarget::installConsoleHandler() {
413416
jsi::PropNameID::forAscii(runtime, methodName),
414417
0,
415418
forwardToOriginalConsole(
419+
originalConsole,
416420
methodName,
417421
[body = std::move(body), state, delegateExecutorSync](
418422
jsi::Runtime& runtime,

0 commit comments

Comments
 (0)