Improve the bridge params validator, and its invariant string

Summary:
NaN is not handled consistently by the bridge in all cases,
so detect it and complain.  In order to make the complaint more
obvious, use JSON.stringify on the value, and a replacer so that some
of the censoring which normally takes place doesn't get in the way of
clarity.

Reviewed By: mmmulani

Differential Revision: D9779799

fbshipit-source-id: 6c1a6bfe05ecaa3aeb558acc49dfd54461e1ba74
This commit is contained in:
Marc Horowitz 2018-09-12 18:56:27 -07:00 committed by Facebook Github Bot
parent 6ea2e3ff8c
commit 7f1fcb67e5
1 changed files with 19 additions and 2 deletions

View File

@ -213,11 +213,13 @@ class MessageQueue {
t === 'undefined' ||
t === 'null' ||
t === 'boolean' ||
t === 'number' ||
t === 'string'
) {
return true;
}
if (t === 'number') {
return isFinite(val);
}
if (t === 'function' || t !== 'object') {
return false;
}
@ -232,10 +234,25 @@ class MessageQueue {
return true;
};
// Replacement allows normally non-JSON-convertible values to be
// seen. There is ambiguity with string values, but in context,
// it should at least be a strong hint.
const replacer = (key, val) => {
const t = typeof val;
if (t === 'function') {
return '<<Function ' + val.name + '>>';
} else if (t === 'number' && !isFinite(val)) {
return '<<' + val.toString() + '>>';
} else {
return val;
}
};
// Note that JSON.stringify
invariant(
isValidArgument(params),
'%s is not usable as a native method argument',
params,
JSON.stringify(params, replacer),
);
// The params object should not be mutated after being queued