2015-01-30 01:10:49 +00:00
|
|
|
/**
|
2015-03-23 22:07:33 +00:00
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
|
|
*
|
2018-02-17 02:24:55 +00:00
|
|
|
* This source code is licensed under the MIT license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree.
|
2015-01-30 01:10:49 +00:00
|
|
|
*
|
2018-05-11 02:06:46 +00:00
|
|
|
* @format
|
2015-03-25 22:36:50 +00:00
|
|
|
* @flow
|
2015-01-30 01:10:49 +00:00
|
|
|
*/
|
2018-05-11 02:06:46 +00:00
|
|
|
|
2015-01-30 01:10:49 +00:00
|
|
|
'use strict';
|
|
|
|
|
2015-12-10 12:27:45 +00:00
|
|
|
const BatchedBridge = require('BatchedBridge');
|
2015-01-30 01:10:49 +00:00
|
|
|
|
2016-09-23 18:12:54 +00:00
|
|
|
const invariant = require('fbjs/lib/invariant');
|
|
|
|
|
2017-06-14 16:16:26 +00:00
|
|
|
import type {ExtendedError} from 'parseErrorStack';
|
|
|
|
|
2016-09-23 18:12:54 +00:00
|
|
|
type ModuleConfig = [
|
2018-05-11 02:06:46 +00:00
|
|
|
string /* name */,
|
|
|
|
?Object /* constants */,
|
|
|
|
Array<string> /* functions */,
|
|
|
|
Array<number> /* promise method IDs */,
|
|
|
|
Array<number> /* sync method IDs */,
|
2016-09-23 18:12:54 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
export type MethodType = 'async' | 'promise' | 'sync';
|
|
|
|
|
2018-05-11 02:06:46 +00:00
|
|
|
function genModule(
|
|
|
|
config: ?ModuleConfig,
|
|
|
|
moduleID: number,
|
|
|
|
): ?{name: string, module?: Object} {
|
2016-09-23 18:12:54 +00:00
|
|
|
if (!config) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
const [moduleName, constants, methods, promiseMethods, syncMethods] = config;
|
2018-05-11 02:06:46 +00:00
|
|
|
invariant(
|
|
|
|
!moduleName.startsWith('RCT') && !moduleName.startsWith('RK'),
|
|
|
|
"Module name prefixes should've been stripped by the native side " +
|
|
|
|
"but wasn't for " +
|
|
|
|
moduleName,
|
|
|
|
);
|
2016-09-23 18:12:54 +00:00
|
|
|
|
|
|
|
if (!constants && !methods) {
|
|
|
|
// Module contents will be filled in lazily later
|
2018-05-11 02:06:46 +00:00
|
|
|
return {name: moduleName};
|
2016-09-23 18:12:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const module = {};
|
2018-05-11 02:06:46 +00:00
|
|
|
methods &&
|
|
|
|
methods.forEach((methodName, methodID) => {
|
|
|
|
const isPromise =
|
|
|
|
promiseMethods && arrayContains(promiseMethods, methodID);
|
|
|
|
const isSync = syncMethods && arrayContains(syncMethods, methodID);
|
|
|
|
invariant(
|
|
|
|
!isPromise || !isSync,
|
|
|
|
'Cannot have a method that is both async and a sync hook',
|
|
|
|
);
|
|
|
|
const methodType = isPromise ? 'promise' : isSync ? 'sync' : 'async';
|
|
|
|
module[methodName] = genMethod(moduleID, methodID, methodType);
|
|
|
|
});
|
2016-09-23 18:12:54 +00:00
|
|
|
Object.assign(module, constants);
|
|
|
|
|
|
|
|
if (__DEV__) {
|
|
|
|
BatchedBridge.createDebugLookup(moduleID, moduleName, methods);
|
|
|
|
}
|
|
|
|
|
2018-05-11 02:06:46 +00:00
|
|
|
return {name: moduleName, module};
|
2016-09-23 18:12:54 +00:00
|
|
|
}
|
|
|
|
|
2016-10-11 14:19:31 +00:00
|
|
|
// export this method as a global so we can call it from native
|
|
|
|
global.__fbGenNativeModule = genModule;
|
|
|
|
|
2016-09-23 18:12:54 +00:00
|
|
|
function loadModule(name: string, moduleID: number): ?Object {
|
2018-05-11 02:06:46 +00:00
|
|
|
invariant(
|
|
|
|
global.nativeRequireModuleConfig,
|
|
|
|
"Can't lazily create module without nativeRequireModuleConfig",
|
|
|
|
);
|
2016-09-23 18:12:54 +00:00
|
|
|
const config = global.nativeRequireModuleConfig(name);
|
|
|
|
const info = genModule(config, moduleID);
|
|
|
|
return info && info.module;
|
|
|
|
}
|
|
|
|
|
|
|
|
function genMethod(moduleID: number, methodID: number, type: MethodType) {
|
|
|
|
let fn = null;
|
|
|
|
if (type === 'promise') {
|
|
|
|
fn = function(...args: Array<any>) {
|
|
|
|
return new Promise((resolve, reject) => {
|
2018-05-11 02:06:46 +00:00
|
|
|
BatchedBridge.enqueueNativeCall(
|
|
|
|
moduleID,
|
|
|
|
methodID,
|
|
|
|
args,
|
|
|
|
data => resolve(data),
|
|
|
|
errorData => reject(createErrorFromErrorData(errorData)),
|
|
|
|
);
|
2016-09-23 18:12:54 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
} else if (type === 'sync') {
|
|
|
|
fn = function(...args: Array<any>) {
|
2017-09-07 21:23:13 +00:00
|
|
|
if (__DEV__) {
|
2018-05-11 02:06:46 +00:00
|
|
|
invariant(
|
|
|
|
global.nativeCallSyncHook,
|
|
|
|
'Calling synchronous methods on native ' +
|
|
|
|
'modules is not supported in Chrome.\n\n Consider providing alternative ' +
|
|
|
|
'methods to expose this method in debug mode, e.g. by exposing constants ' +
|
|
|
|
'ahead-of-time.',
|
|
|
|
);
|
2017-09-07 21:23:13 +00:00
|
|
|
}
|
|
|
|
return global.nativeCallSyncHook(moduleID, methodID, args);
|
2016-09-23 18:12:54 +00:00
|
|
|
};
|
|
|
|
} else {
|
|
|
|
fn = function(...args: Array<any>) {
|
|
|
|
const lastArg = args.length > 0 ? args[args.length - 1] : null;
|
|
|
|
const secondLastArg = args.length > 1 ? args[args.length - 2] : null;
|
|
|
|
const hasSuccessCallback = typeof lastArg === 'function';
|
|
|
|
const hasErrorCallback = typeof secondLastArg === 'function';
|
2018-05-11 02:06:46 +00:00
|
|
|
hasErrorCallback &&
|
|
|
|
invariant(
|
|
|
|
hasSuccessCallback,
|
|
|
|
'Cannot have a non-function arg after a function arg.',
|
|
|
|
);
|
2016-09-23 18:12:54 +00:00
|
|
|
const onSuccess = hasSuccessCallback ? lastArg : null;
|
|
|
|
const onFail = hasErrorCallback ? secondLastArg : null;
|
|
|
|
const callbackCount = hasSuccessCallback + hasErrorCallback;
|
|
|
|
args = args.slice(0, args.length - callbackCount);
|
2018-05-11 02:06:46 +00:00
|
|
|
BatchedBridge.enqueueNativeCall(
|
|
|
|
moduleID,
|
|
|
|
methodID,
|
|
|
|
args,
|
|
|
|
onFail,
|
|
|
|
onSuccess,
|
|
|
|
);
|
2016-09-23 18:12:54 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
fn.type = type;
|
|
|
|
return fn;
|
|
|
|
}
|
|
|
|
|
|
|
|
function arrayContains<T>(array: Array<T>, value: T): boolean {
|
|
|
|
return array.indexOf(value) !== -1;
|
|
|
|
}
|
|
|
|
|
2017-06-14 16:16:26 +00:00
|
|
|
function createErrorFromErrorData(errorData: {message: string}): ExtendedError {
|
2018-05-11 02:06:46 +00:00
|
|
|
const {message, ...extraErrorInfo} = errorData || {};
|
|
|
|
const error: ExtendedError = new Error(message);
|
2017-06-14 16:16:26 +00:00
|
|
|
error.framesToPop = 1;
|
2016-09-23 18:12:54 +00:00
|
|
|
return Object.assign(error, extraErrorInfo);
|
|
|
|
}
|
|
|
|
|
2018-05-11 02:06:46 +00:00
|
|
|
let NativeModules: {[moduleName: string]: Object} = {};
|
2016-10-11 14:19:31 +00:00
|
|
|
if (global.nativeModuleProxy) {
|
|
|
|
NativeModules = global.nativeModuleProxy;
|
2018-05-11 17:54:22 +00:00
|
|
|
} else if (!global.nativeExtensions) {
|
2016-10-11 14:19:31 +00:00
|
|
|
const bridgeConfig = global.__fbBatchedBridgeConfig;
|
2018-05-11 02:06:46 +00:00
|
|
|
invariant(
|
|
|
|
bridgeConfig,
|
|
|
|
'__fbBatchedBridgeConfig is not set, cannot invoke native modules',
|
|
|
|
);
|
2016-10-11 14:19:31 +00:00
|
|
|
|
2017-03-24 16:22:32 +00:00
|
|
|
const defineLazyObjectProperty = require('defineLazyObjectProperty');
|
2018-05-11 02:06:46 +00:00
|
|
|
(bridgeConfig.remoteModuleConfig || []).forEach(
|
|
|
|
(config: ModuleConfig, moduleID: number) => {
|
|
|
|
// Initially this config will only contain the module name when running in JSC. The actual
|
|
|
|
// configuration of the module will be lazily loaded.
|
|
|
|
const info = genModule(config, moduleID);
|
|
|
|
if (!info) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (info.module) {
|
|
|
|
NativeModules[info.name] = info.module;
|
|
|
|
}
|
|
|
|
// If there's no module config, define a lazy getter
|
|
|
|
else {
|
|
|
|
defineLazyObjectProperty(NativeModules, info.name, {
|
|
|
|
get: () => loadModule(info.name, moduleID),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
2016-10-11 14:19:31 +00:00
|
|
|
}
|
2015-12-10 12:27:45 +00:00
|
|
|
|
2015-01-30 01:10:49 +00:00
|
|
|
module.exports = NativeModules;
|