diff --git a/ReactAndroid/src/androidTest/java/com/facebook/react/testing/ReactTestHelper.java b/ReactAndroid/src/androidTest/java/com/facebook/react/testing/ReactTestHelper.java index a36743b7a..5d4ebb2d1 100644 --- a/ReactAndroid/src/androidTest/java/com/facebook/react/testing/ReactTestHelper.java +++ b/ReactAndroid/src/androidTest/java/com/facebook/react/testing/ReactTestHelper.java @@ -23,7 +23,7 @@ import com.facebook.react.bridge.CatalystInstance; import com.facebook.react.bridge.CatalystInstanceImpl; import com.facebook.react.bridge.JSBundleLoader; import com.facebook.react.bridge.JSCJavaScriptExecutor; -import com.facebook.react.bridge.JavaScriptModulesConfig; +import com.facebook.react.bridge.JavaScriptModuleRegistry; import com.facebook.react.bridge.NativeModule; import com.facebook.react.bridge.NativeModuleCallExceptionHandler; import com.facebook.react.bridge.NativeModuleRegistry; @@ -38,8 +38,8 @@ public class ReactTestHelper { private @Nullable Context mContext; private final NativeModuleRegistry.Builder mNativeModuleRegistryBuilder = new NativeModuleRegistry.Builder(); - private final JavaScriptModulesConfig.Builder mJSModulesConfigBuilder = - new JavaScriptModulesConfig.Builder(); + private final JavaScriptModuleRegistry.Builder mJSModuleRegistryBuilder = + new JavaScriptModuleRegistry.Builder(); @Override public ReactInstanceEasyBuilder setContext(Context context) { @@ -55,7 +55,7 @@ public class ReactTestHelper { @Override public ReactInstanceEasyBuilder addJSModule(Class moduleInterfaceClass) { - mJSModulesConfigBuilder.add(moduleInterfaceClass); + mJSModuleRegistryBuilder.add(moduleInterfaceClass); return this; } @@ -65,7 +65,7 @@ public class ReactTestHelper { .setReactQueueConfigurationSpec(ReactQueueConfigurationSpec.createDefault()) .setJSExecutor(new JSCJavaScriptExecutor(new WritableNativeMap())) .setRegistry(mNativeModuleRegistryBuilder.build()) - .setJSModulesConfig(mJSModulesConfigBuilder.build()) + .setJSModuleRegistry(mJSModuleRegistryBuilder.build()) .setJSBundleLoader(JSBundleLoader.createFileLoader( mContext, "assets://AndroidTestBundle.js")) diff --git a/ReactAndroid/src/main/java/com/facebook/react/ReactInstanceManagerImpl.java b/ReactAndroid/src/main/java/com/facebook/react/ReactInstanceManagerImpl.java index 3febc2913..bfaf7e6ba 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/ReactInstanceManagerImpl.java +++ b/ReactAndroid/src/main/java/com/facebook/react/ReactInstanceManagerImpl.java @@ -37,7 +37,7 @@ import com.facebook.react.bridge.JSCJavaScriptExecutor; import com.facebook.react.bridge.JavaJSExecutor; import com.facebook.react.bridge.JavaScriptExecutor; import com.facebook.react.bridge.JavaScriptModule; -import com.facebook.react.bridge.JavaScriptModulesConfig; +import com.facebook.react.bridge.JavaScriptModuleRegistry; import com.facebook.react.bridge.NativeModule; import com.facebook.react.bridge.NativeModuleCallExceptionHandler; import com.facebook.react.bridge.NativeModuleRegistry; @@ -759,7 +759,7 @@ import static com.facebook.react.bridge.ReactMarkerConstants.RUN_JS_BUNDLE_START // CREATE_REACT_CONTEXT_END is in JSCExecutor.cpp mSourceUrl = jsBundleLoader.getSourceUrl(); NativeModuleRegistry.Builder nativeRegistryBuilder = new NativeModuleRegistry.Builder(); - JavaScriptModulesConfig.Builder jsModulesBuilder = new JavaScriptModulesConfig.Builder(); + JavaScriptModuleRegistry.Builder jsModulesBuilder = new JavaScriptModuleRegistry.Builder(); final ReactApplicationContext reactContext = new ReactApplicationContext(mApplicationContext); if (mUseDeveloperSupport) { @@ -801,15 +801,6 @@ import static com.facebook.react.bridge.ReactMarkerConstants.RUN_JS_BUNDLE_START ReactMarker.logMarker(BUILD_NATIVE_MODULE_REGISTRY_END); } - ReactMarker.logMarker(BUILD_JS_MODULE_CONFIG_START); - Systrace.beginSection(Systrace.TRACE_TAG_REACT_JAVA_BRIDGE, "buildJSModuleConfig"); - JavaScriptModulesConfig javaScriptModulesConfig; - try { - javaScriptModulesConfig = jsModulesBuilder.build(); - } finally { - Systrace.endSection(Systrace.TRACE_TAG_REACT_JAVA_BRIDGE); - ReactMarker.logMarker(BUILD_JS_MODULE_CONFIG_END); - } NativeModuleCallExceptionHandler exceptionHandler = mNativeModuleCallExceptionHandler != null ? mNativeModuleCallExceptionHandler @@ -818,7 +809,7 @@ import static com.facebook.react.bridge.ReactMarkerConstants.RUN_JS_BUNDLE_START .setReactQueueConfigurationSpec(ReactQueueConfigurationSpec.createDefault()) .setJSExecutor(jsExecutor) .setRegistry(nativeModuleRegistry) - .setJSModulesConfig(javaScriptModulesConfig) + .setJSModuleRegistry(jsModulesBuilder.build()) .setJSBundleLoader(jsBundleLoader) .setNativeModuleCallExceptionHandler(exceptionHandler); @@ -876,7 +867,7 @@ import static com.facebook.react.bridge.ReactMarkerConstants.RUN_JS_BUNDLE_START ReactPackage reactPackage, ReactApplicationContext reactContext, NativeModuleRegistry.Builder nativeRegistryBuilder, - JavaScriptModulesConfig.Builder jsModulesBuilder) { + JavaScriptModuleRegistry.Builder jsModulesBuilder) { for (NativeModule nativeModule : reactPackage.createNativeModules(reactContext)) { nativeRegistryBuilder.add(nativeModule); } diff --git a/ReactAndroid/src/main/java/com/facebook/react/bridge/CatalystInstanceImpl.java b/ReactAndroid/src/main/java/com/facebook/react/bridge/CatalystInstanceImpl.java index d858c743d..f13ef8658 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/bridge/CatalystInstanceImpl.java +++ b/ReactAndroid/src/main/java/com/facebook/react/bridge/CatalystInstanceImpl.java @@ -72,7 +72,7 @@ public class CatalystInstanceImpl implements CatalystInstance { final ReactQueueConfigurationSpec ReactQueueConfigurationSpec, final JavaScriptExecutor jsExecutor, final NativeModuleRegistry registry, - final JavaScriptModulesConfig jsModulesConfig, + final JavaScriptModuleRegistry jsModuleRegistry, final JSBundleLoader jsBundleLoader, NativeModuleCallExceptionHandler nativeModuleCallExceptionHandler) { FLog.d(ReactConstants.TAG, "Initializing React Bridge."); @@ -81,7 +81,7 @@ public class CatalystInstanceImpl implements CatalystInstance { new NativeExceptionHandler()); mBridgeIdleListeners = new CopyOnWriteArrayList<>(); mJavaRegistry = registry; - mJSModuleRegistry = new JavaScriptModuleRegistry(CatalystInstanceImpl.this, jsModulesConfig); + mJSModuleRegistry = jsModuleRegistry; mJSBundleLoader = jsBundleLoader; mNativeModuleCallExceptionHandler = nativeModuleCallExceptionHandler; mTraceListener = new JSProfilerTraceListener(); @@ -93,7 +93,7 @@ public class CatalystInstanceImpl implements CatalystInstance { public ReactBridge call() throws Exception { Systrace.beginSection(Systrace.TRACE_TAG_REACT_JAVA_BRIDGE, "initializeBridge"); try { - return initializeBridge(jsExecutor, jsModulesConfig); + return initializeBridge(jsExecutor); } finally { Systrace.endSection(Systrace.TRACE_TAG_REACT_JAVA_BRIDGE); } @@ -104,9 +104,7 @@ public class CatalystInstanceImpl implements CatalystInstance { } } - private ReactBridge initializeBridge( - JavaScriptExecutor jsExecutor, - JavaScriptModulesConfig jsModulesConfig) { + private ReactBridge initializeBridge(JavaScriptExecutor jsExecutor) { mReactQueueConfiguration.getJSQueueThread().assertIsOnThread(); Assertions.assertCondition(mBridge == null, "initializeBridge should be called once"); @@ -126,7 +124,7 @@ public class CatalystInstanceImpl implements CatalystInstance { try { bridge.setGlobalVariable( "__fbBatchedBridgeConfig", - buildModulesConfigJSONProperty(mJavaRegistry, jsModulesConfig)); + buildModulesConfigJSONProperty(mJavaRegistry)); bridge.setGlobalVariable( "__RCTProfileIsProfiling", Systrace.isTracing(Systrace.TRACE_TAG_REACT_APPS) ? "true" : "false"); @@ -280,7 +278,8 @@ public class CatalystInstanceImpl implements CatalystInstance { @Override public T getJSModule(ExecutorToken executorToken, Class jsInterface) { - return Assertions.assertNotNull(mJSModuleRegistry).getJavaScriptModule(executorToken, jsInterface); + return Assertions.assertNotNull(mJSModuleRegistry) + .getJavaScriptModule(this, executorToken, jsInterface); } @Override @@ -350,17 +349,13 @@ public class CatalystInstanceImpl implements CatalystInstance { mBridge.setGlobalVariable(propName, jsonValue); } - private String buildModulesConfigJSONProperty( - NativeModuleRegistry nativeModuleRegistry, - JavaScriptModulesConfig jsModulesConfig) { + private String buildModulesConfigJSONProperty(NativeModuleRegistry nativeModuleRegistry) { StringWriter stringWriter = new StringWriter(); JsonWriter writer = new JsonWriter(stringWriter); try { writer.beginObject(); writer.name("remoteModuleConfig"); nativeModuleRegistry.writeModuleDescriptions(writer); - writer.name("localModulesConfig"); - jsModulesConfig.writeModuleDescriptions(writer); writer.endObject(); return stringWriter.toString(); } catch (IOException ioe) { @@ -504,7 +499,7 @@ public class CatalystInstanceImpl implements CatalystInstance { private @Nullable ReactQueueConfigurationSpec mReactQueueConfigurationSpec; private @Nullable JSBundleLoader mJSBundleLoader; private @Nullable NativeModuleRegistry mRegistry; - private @Nullable JavaScriptModulesConfig mJSModulesConfig; + private @Nullable JavaScriptModuleRegistry mJSModuleRegistry; private @Nullable JavaScriptExecutor mJSExecutor; private @Nullable NativeModuleCallExceptionHandler mNativeModuleCallExceptionHandler; @@ -519,8 +514,8 @@ public class CatalystInstanceImpl implements CatalystInstance { return this; } - public Builder setJSModulesConfig(JavaScriptModulesConfig jsModulesConfig) { - mJSModulesConfig = jsModulesConfig; + public Builder setJSModuleRegistry(JavaScriptModuleRegistry jsModuleRegistry) { + mJSModuleRegistry = jsModuleRegistry; return this; } @@ -545,7 +540,7 @@ public class CatalystInstanceImpl implements CatalystInstance { Assertions.assertNotNull(mReactQueueConfigurationSpec), Assertions.assertNotNull(mJSExecutor), Assertions.assertNotNull(mRegistry), - Assertions.assertNotNull(mJSModulesConfig), + Assertions.assertNotNull(mJSModuleRegistry), Assertions.assertNotNull(mJSBundleLoader), Assertions.assertNotNull(mNativeModuleCallExceptionHandler)); } diff --git a/ReactAndroid/src/main/java/com/facebook/react/bridge/JavaScriptModuleRegistry.java b/ReactAndroid/src/main/java/com/facebook/react/bridge/JavaScriptModuleRegistry.java index 77e972ca3..466afcccf 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/bridge/JavaScriptModuleRegistry.java +++ b/ReactAndroid/src/main/java/com/facebook/react/bridge/JavaScriptModuleRegistry.java @@ -15,7 +15,9 @@ import java.lang.ref.WeakReference; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; +import java.util.ArrayList; import java.util.HashMap; +import java.util.List; import java.util.WeakHashMap; import com.facebook.common.logging.FLog; @@ -29,23 +31,21 @@ import com.facebook.react.common.ReactConstants; * JavaScript. */ public class JavaScriptModuleRegistry { - - private final CatalystInstance mCatalystInstance; private final WeakHashMap, JavaScriptModule>> mModuleInstances; private final HashMap, JavaScriptModuleRegistration> mModuleRegistrations; - public JavaScriptModuleRegistry( - CatalystInstance instance, - JavaScriptModulesConfig config) { - mCatalystInstance = instance; + public JavaScriptModuleRegistry(List config) { mModuleInstances = new WeakHashMap<>(); mModuleRegistrations = new HashMap<>(); - for (JavaScriptModuleRegistration registration : config.getModuleDefinitions()) { + for (JavaScriptModuleRegistration registration : config) { mModuleRegistrations.put(registration.getModuleInterface(), registration); } } - public synchronized T getJavaScriptModule(ExecutorToken executorToken, Class moduleInterface) { + public synchronized T getJavaScriptModule( + CatalystInstance instance, + ExecutorToken executorToken, + Class moduleInterface) { HashMap, JavaScriptModule> instancesForContext = mModuleInstances.get(executorToken); if (instancesForContext == null) { @@ -65,11 +65,25 @@ public class JavaScriptModuleRegistry { JavaScriptModule interfaceProxy = (JavaScriptModule) Proxy.newProxyInstance( moduleInterface.getClassLoader(), new Class[]{moduleInterface}, - new JavaScriptModuleInvocationHandler(executorToken, mCatalystInstance, registration)); + new JavaScriptModuleInvocationHandler(executorToken, instance, registration)); instancesForContext.put(moduleInterface, interfaceProxy); return (T) interfaceProxy; } + public static class Builder { + private List mModules = + new ArrayList(); + + public Builder add(Class moduleInterfaceClass) { + mModules.add(new JavaScriptModuleRegistration(moduleInterfaceClass)); + return this; + } + + public JavaScriptModuleRegistry build() { + return new JavaScriptModuleRegistry(mModules); + } + } + private static class JavaScriptModuleInvocationHandler implements InvocationHandler { private final WeakReference mExecutorToken; diff --git a/ReactAndroid/src/main/java/com/facebook/react/bridge/JavaScriptModulesConfig.java b/ReactAndroid/src/main/java/com/facebook/react/bridge/JavaScriptModulesConfig.java deleted file mode 100644 index 42e5f37ad..000000000 --- a/ReactAndroid/src/main/java/com/facebook/react/bridge/JavaScriptModulesConfig.java +++ /dev/null @@ -1,71 +0,0 @@ -/** - * Copyright (c) 2015-present, Facebook, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - */ - -package com.facebook.react.bridge; - -import java.io.IOException; -import java.lang.reflect.Method; -import java.util.ArrayList; -import java.util.List; - -/** - * Class stores configuration of javascript modules that can be used across the bridge - */ -public class JavaScriptModulesConfig { - - private final List mModules; - - public JavaScriptModulesConfig(List modules) { - mModules = modules; - } - - public List getModuleDefinitions() { - return mModules; - } - - public void writeModuleDescriptions(JsonWriter writer) throws IOException { - writer.beginObject(); - for (JavaScriptModuleRegistration registration : mModules) { - writer.name(registration.getName()).beginObject(); - appendJSModuleToJSONObject(writer, registration); - writer.endObject(); - } - writer.endObject(); - } - - private void appendJSModuleToJSONObject( - JsonWriter writer, - JavaScriptModuleRegistration registration) throws IOException { - writer.name("moduleID").value(registration.getName()); - writer.name("methods").beginObject(); - for (Method method : registration.getMethods()) { - writer.name(method.getName()).beginObject(); - writer.name("methodID").value(method.getName()); - writer.endObject(); - } - writer.endObject(); - if (registration.getModuleInterface().isAnnotationPresent(SupportsWebWorkers.class)) { - writer.name("supportsWebWorkers").value(true); - } - } - - public static class Builder { - private List mModules = - new ArrayList(); - - public Builder add(Class moduleInterfaceClass) { - mModules.add(new JavaScriptModuleRegistration(moduleInterfaceClass)); - return this; - } - - public JavaScriptModulesConfig build() { - return new JavaScriptModulesConfig(mModules); - } - } -} diff --git a/ReactAndroid/src/test/java/com/facebook/react/bridge/JavaScriptModuleConfigTest.java b/ReactAndroid/src/test/java/com/facebook/react/bridge/JavaScriptModuleConfigTest.java deleted file mode 100644 index 616a240d6..000000000 --- a/ReactAndroid/src/test/java/com/facebook/react/bridge/JavaScriptModuleConfigTest.java +++ /dev/null @@ -1,95 +0,0 @@ -/** - * Copyright (c) 2015-present, Facebook, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - */ - -package com.facebook.react.bridge; - -import java.io.IOException; -import java.io.StringWriter; - -import com.fasterxml.jackson.databind.JsonNode; -import com.fasterxml.jackson.databind.ObjectMapper; -import org.junit.Test; - -import static org.fest.assertions.api.Assertions.assertThat; - -public class JavaScriptModuleConfigTest { - - private static interface SomeModule extends JavaScriptModule { - public void stringMethod(String arg); - public void intMethod(int arg); - } - - private static interface OtherModule extends JavaScriptModule { - public void method(String arg1, int arg2); - } - - @Test - public void testModuleWithMethods() throws Exception { - JavaScriptModulesConfig jsModulesConfig = new JavaScriptModulesConfig.Builder() - .add(SomeModule.class) - .build(); - - String json = getModuleDescriptions(jsModulesConfig); - JsonNode node = parse(json); - assertThat(node).hasSize(1); - - JsonNode module = node.fields().next().getValue(); - assertThat(module).isNotNull(); - - JsonNode methods = module.get("methods"); - assertThat(methods) - .isNotNull() - .hasSize(2); - - JsonNode intMethodNode = methods.get("intMethod"); - assertThat(intMethodNode).isNotNull(); - assertThat(intMethodNode.get("methodID").asText()).isEqualTo("intMethod"); - - JsonNode stringMethod = methods.get("stringMethod"); - assertThat(stringMethod).isNotNull(); - assertThat(stringMethod.get("methodID").asText()).isEqualTo("stringMethod"); - } - - @Test - public void testMultipleModules() throws Exception { - JavaScriptModulesConfig jsModulesConfig = new JavaScriptModulesConfig.Builder() - .add(OtherModule.class) - .add(SomeModule.class) - .build(); - - String json = getModuleDescriptions(jsModulesConfig); - JsonNode node = parse(json); - assertThat(node).hasSize(2); - - JsonNode someModuleNode = node.get("SomeModule"); - assertThat(someModuleNode).isNotNull(); - String someModuleID = someModuleNode.get("moduleID").asText(); - - JsonNode otherModuleNode = node.get("OtherModule"); - assertThat(otherModuleNode).isNotNull(); - String otherModuleID = otherModuleNode.get("moduleID").asText(); - assertThat(otherModuleID) - .isNotEqualTo(someModuleID); - } - - private static String getModuleDescriptions(JavaScriptModulesConfig jsModulesConfig) - throws IOException { - StringWriter stringWriter = new StringWriter(); - JsonWriter writer = new JsonWriter(stringWriter); - jsModulesConfig.writeModuleDescriptions(writer); - writer.close(); - return stringWriter.getBuffer().toString(); - } - - private JsonNode parse(String json) throws Exception { - ObjectMapper mapper = new ObjectMapper(); - return mapper.readTree(json); - } - -} diff --git a/ReactCommon/bridge/Instance.cpp b/ReactCommon/bridge/Instance.cpp index 3982916ff..8f0c81553 100644 --- a/ReactCommon/bridge/Instance.cpp +++ b/ReactCommon/bridge/Instance.cpp @@ -67,8 +67,7 @@ void Instance::initializeBridge( std::shared_ptr jsef, std::shared_ptr jsQueue, std::unique_ptr nativeQueue, - std::shared_ptr moduleRegistry, - folly::dynamic jsModuleDescriptions) { + std::shared_ptr moduleRegistry) { callback_ = std::move(callback); nativeQueue_ = std::move(nativeQueue); jsQueue_ = jsQueue; @@ -94,8 +93,7 @@ void Instance::initializeBridge( folly::dynamic config = folly::dynamic::object - ("remoteModuleConfig", std::move(nativeModuleDescriptions)) - ("localModulesConfig", std::move(jsModuleDescriptions)); + ("remoteModuleConfig", std::move(nativeModuleDescriptions)); #ifdef WITH_FBSYSTRACE FbSystraceSection t(TRACE_TAG_REACT_CXX_BRIDGE, "setGlobalVariable"); diff --git a/ReactCommon/bridge/Instance.h b/ReactCommon/bridge/Instance.h index 7d8b57d29..5b0fa9d46 100644 --- a/ReactCommon/bridge/Instance.h +++ b/ReactCommon/bridge/Instance.h @@ -32,8 +32,7 @@ class Instance { std::shared_ptr jsef, std::shared_ptr jsQueue, std::unique_ptr nativeQueue, - std::shared_ptr moduleRegistry, - folly::dynamic jsModuleDescriptions); + std::shared_ptr moduleRegistry); void loadScriptFromString(const std::string& string, const std::string& sourceURL); void loadScriptFromFile(const std::string& filename, const std::string& sourceURL); void loadUnbundle(