Serialize params when making/queuing native call

Reviewed By: mhorowitz

Differential Revision: D3460507

fbshipit-source-id: a0600ffe3da89791af3eb64fc2973eb6aafa7d2b
This commit is contained in:
Chris Hopman 2016-06-23 14:47:56 -07:00 committed by Facebook Github Bot
parent e3f96acf26
commit df6d18358e
5 changed files with 24 additions and 7 deletions

View File

@ -12,8 +12,11 @@
const MessageQueue = require('MessageQueue'); const MessageQueue = require('MessageQueue');
const serializeNativeParams = typeof global.__fbBatchedBridgeSerializeNativeParams !== 'undefined';
const BatchedBridge = new MessageQueue( const BatchedBridge = new MessageQueue(
() => global.__fbBatchedBridgeConfig () => global.__fbBatchedBridgeConfig,
serializeNativeParams
); );
// TODO: Move these around to solve the cycle in a cleaner way. // TODO: Move these around to solve the cycle in a cleaner way.

View File

@ -49,7 +49,7 @@ type Config = {
}; };
class MessageQueue { class MessageQueue {
constructor(configProvider: () => Config) { constructor(configProvider: () => Config, serializeNativeParams: boolean) {
this._callableModules = {}; this._callableModules = {};
this._queue = [[], [], [], 0]; this._queue = [[], [], [], 0];
this._callbacks = []; this._callbacks = [];
@ -57,6 +57,7 @@ class MessageQueue {
this._callID = 0; this._callID = 0;
this._lastFlush = 0; this._lastFlush = 0;
this._eventLoopStartTime = new Date().getTime(); this._eventLoopStartTime = new Date().getTime();
this._serializeNativeParams = serializeNativeParams;
if (__DEV__) { if (__DEV__) {
this._debugInfo = {}; this._debugInfo = {};
@ -150,6 +151,7 @@ class MessageQueue {
onSucc && params.push(this._callbackID); onSucc && params.push(this._callbackID);
this._callbacks[this._callbackID++] = onSucc; this._callbacks[this._callbackID++] = onSucc;
} }
var preparedParams = this._serializeNativeParams ? JSON.stringify(params) : params;
if (__DEV__) { if (__DEV__) {
global.nativeTraceBeginAsyncFlow && global.nativeTraceBeginAsyncFlow &&
@ -159,7 +161,7 @@ class MessageQueue {
this._queue[MODULE_IDS].push(module); this._queue[MODULE_IDS].push(module);
this._queue[METHOD_IDS].push(method); this._queue[METHOD_IDS].push(method);
this._queue[PARAMS].push(params); this._queue[PARAMS].push(preparedParams);
const now = new Date().getTime(); const now = new Date().getTime();
if (global.nativeFlushQueueImmediate && if (global.nativeFlushQueueImmediate &&

View File

@ -58,6 +58,9 @@ ProxyExecutor::ProxyExecutor(jni::global_ref<jobject>&& executorInstance,
setGlobalVariable( setGlobalVariable(
"__fbBatchedBridgeConfig", "__fbBatchedBridgeConfig",
folly::make_unique<JSBigStdString>(detail::toStdString(folly::toJson(config)))); folly::make_unique<JSBigStdString>(detail::toStdString(folly::toJson(config))));
setGlobalVariable(
"__fbBatchedBridgeSerializeNativeParams",
folly::make_unique<JSBigStdString>("1"));
} }
ProxyExecutor::~ProxyExecutor() { ProxyExecutor::~ProxyExecutor() {

View File

@ -119,12 +119,17 @@ JSCExecutor::JSCExecutor(std::shared_ptr<ExecutorDelegate> delegate,
} }
folly::dynamic config = folly::dynamic config =
folly::dynamic::object("remoteModuleConfig", std::move(nativeModuleConfig)); folly::dynamic::object
("remoteModuleConfig", std::move(nativeModuleConfig));
SystraceSection t("setGlobalVariable"); SystraceSection t("setGlobalVariable");
setGlobalVariable( setGlobalVariable(
"__fbBatchedBridgeConfig", "__fbBatchedBridgeConfig",
folly::make_unique<JSBigStdString>(detail::toStdString(folly::toJson(config)))); folly::make_unique<JSBigStdString>(detail::toStdString(folly::toJson(config))));
setGlobalVariable(
"__fbBatchedBridgeSerializeNativeParams",
folly::make_unique<JSBigStdString>(""));
} }
JSCExecutor::JSCExecutor( JSCExecutor::JSCExecutor(

View File

@ -51,16 +51,20 @@ std::vector<MethodCall> parseMethodCalls(const std::string& json) throw(std::inv
std::vector<MethodCall> methodCalls; std::vector<MethodCall> methodCalls;
for (size_t i = 0; i < moduleIds.size(); i++) { for (size_t i = 0; i < moduleIds.size(); i++) {
auto paramsValue = params[i]; if (!params[i].isString()) {
throw std::invalid_argument(
folly::to<std::string>("Call argument isn't a string"));
}
auto paramsValue = folly::parseJson(params[i].asString());
if (!paramsValue.isArray()) { if (!paramsValue.isArray()) {
throw std::invalid_argument( throw std::invalid_argument(
folly::to<std::string>("Call argument isn't an array")); folly::to<std::string>("Parsed params isn't an array"));
} }
methodCalls.emplace_back( methodCalls.emplace_back(
moduleIds[i].getInt(), moduleIds[i].getInt(),
methodIds[i].getInt(), methodIds[i].getInt(),
std::move(params[i]), std::move(paramsValue),
callId); callId);
// only incremement callid if contains valid callid as callid is optional // only incremement callid if contains valid callid as callid is optional