Skip to content

Commit

Permalink
Add a special case for assert in installConsoleFunction
Browse files Browse the repository at this point in the history
Summary:
The Hermes inspector always logged the message for `console.assert`.
It instead should check the first argument, and only if that argument
is false should it log.

Also remove the polyfill code that tried to avoid this situation.

Changelog: [Internal]

Reviewed By: mhorowitz

Differential Revision: D22299186

fbshipit-source-id: cdf4f8957d4db3d171d6673a82c7fc32b7152af3
  • Loading branch information
Riley Dulin authored and facebook-github-bot committed Jul 1, 2020
1 parent c2d827f commit 3c3f8ca
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 14 deletions.
10 changes: 1 addition & 9 deletions Libraries/polyfills/console.js
Original file line number Diff line number Diff line change
Expand Up @@ -580,15 +580,7 @@ if (global.nativeLoggingHook) {
const reactNativeMethod = console[methodName];
if (originalConsole[methodName]) {
console[methodName] = function() {
// TODO(T43930203): remove this special case once originalConsole.assert properly checks
// the condition
if (methodName === 'assert') {
if (!arguments[0]) {
originalConsole.assert(...arguments);
}
} else {
originalConsole[methodName](...arguments);
}
originalConsole[methodName](...arguments);
reactNativeMethod.apply(console, arguments);
};
}
Expand Down
52 changes: 47 additions & 5 deletions ReactCommon/hermes/inspector/Inspector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,29 @@ Inspector::~Inspector() {
debugger_.setEventObserver(nullptr);
}

static bool toBoolean(jsi::Runtime &runtime, const jsi::Value &val) {
// Based on Operations.cpp:toBoolean in the Hermes VM.
if (val.isUndefined() || val.isNull()) {
return false;
}
if (val.isBool()) {
return val.getBool();
}
if (val.isNumber()) {
double m = val.getNumber();
return m != 0 && !std::isnan(m);
}
if (val.isSymbol() || val.isObject()) {
return true;
}
if (val.isString()) {
std::string s = val.getString(runtime).utf8(runtime);
return !s.empty();
}
assert(false && "All cases should be covered");
return false;
}

void Inspector::installConsoleFunction(
jsi::Object &console,
std::shared_ptr<jsi::Object> &originalConsole,
Expand Down Expand Up @@ -169,11 +192,30 @@ void Inspector::installConsoleFunction(
}

if (auto inspector = weakInspector.lock()) {
jsi::Array argsArray(runtime, count);
for (size_t index = 0; index < count; ++index)
argsArray.setValueAtIndex(runtime, index, args[index]);
inspector->logMessage(
ConsoleMessageInfo{chromeType, std::move(argsArray)});
if (name != "assert") {
// All cases other than assert just log a simple message.
jsi::Array argsArray(runtime, count);
for (size_t index = 0; index < count; ++index)
argsArray.setValueAtIndex(runtime, index, args[index]);
inspector->logMessage(
ConsoleMessageInfo{chromeType, std::move(argsArray)});
return jsi::Value::undefined();
}
// console.assert needs to check the first parameter before
// logging.
if (count == 0) {
// No parameters, throw a blank assertion failed message.
inspector->logMessage(
ConsoleMessageInfo{chromeType, jsi::Array(runtime, 0)});
} else if (!toBoolean(runtime, args[0])) {
// Shift the message array down by one to not include the
// condition.
jsi::Array argsArray(runtime, count - 1);
for (size_t index = 1; index < count; ++index)
argsArray.setValueAtIndex(runtime, index, args[index]);
inspector->logMessage(
ConsoleMessageInfo{chromeType, std::move(argsArray)});
}
}

return jsi::Value::undefined();
Expand Down

0 comments on commit 3c3f8ca

Please sign in to comment.