From da2ea2601b3af564ae5460cbd35307b3ea9591d6 Mon Sep 17 00:00:00 2001 From: Alex Dvornikov Date: Thu, 21 Sep 2017 08:34:41 -0700 Subject: [PATCH] Allow "nativeRequire" to accept "bundleId" and "moduleId" values Differential Revision: D5850960 fbshipit-source-id: 4d7aa0ed7542181861649a2089d90c3cf98723e9 --- ReactCommon/cxxreact/JSCExecutor.cpp | 11 +---------- ReactCommon/cxxreact/JSCUtils.cpp | 28 ++++++++++++++++++++++++++++ ReactCommon/cxxreact/JSCUtils.h | 8 ++++++++ 3 files changed, 37 insertions(+), 10 deletions(-) diff --git a/ReactCommon/cxxreact/JSCExecutor.cpp b/ReactCommon/cxxreact/JSCExecutor.cpp index 70fd599f564..4a63f5cd8ac 100644 --- a/ReactCommon/cxxreact/JSCExecutor.cpp +++ b/ReactCommon/cxxreact/JSCExecutor.cpp @@ -574,16 +574,7 @@ JSValueRef JSCExecutor::getNativeModule(JSObjectRef object, JSStringRef property JSValueRef JSCExecutor::nativeRequire( size_t argumentCount, const JSValueRef arguments[]) { - if (argumentCount != 1) { - throw std::invalid_argument("Got wrong number of args"); - } - - double moduleId = Value(m_context, arguments[0]).asNumber(); - if (moduleId < 0) { - throw std::invalid_argument(folly::to("Received invalid module ID: ", - Value(m_context, arguments[0]).toString().str())); - } - + uint32_t moduleId = parseNativeRequireParameters(m_context, arguments, argumentCount).second; ReactMarker::logMarker(ReactMarker::NATIVE_REQUIRE_START); loadModule(moduleId); ReactMarker::logMarker(ReactMarker::NATIVE_REQUIRE_STOP); diff --git a/ReactCommon/cxxreact/JSCUtils.cpp b/ReactCommon/cxxreact/JSCUtils.cpp index 6a3239bb3b5..96ede8f6ae7 100644 --- a/ReactCommon/cxxreact/JSCUtils.cpp +++ b/ReactCommon/cxxreact/JSCUtils.cpp @@ -13,5 +13,33 @@ String jsStringFromBigString(JSContextRef ctx, const JSBigString& bigstr) { } } +std::pair parseNativeRequireParameters( + const JSGlobalContextRef& context, + const JSValueRef arguments[], + size_t argumentCount) { + double moduleId = 0, bundleId = 0; + + if (argumentCount == 1) { + moduleId = Value(context, arguments[0]).asNumber(); + } else if (argumentCount == 2) { + moduleId = Value(context, arguments[0]).asNumber(); + bundleId = Value(context, arguments[1]).asNumber(); + } else { + throw std::invalid_argument("Got wrong number of args"); + } + + if (moduleId < 0) { + throw std::invalid_argument(folly::to("Received invalid module ID: ", + Value(context, arguments[0]).toString().str())); + } + + if (bundleId < 0) { + throw std::invalid_argument(folly::to("Received invalid bundle ID: ", + Value(context, arguments[1]).toString().str())); + } + + return std::make_pair(static_cast(bundleId), static_cast(moduleId)); +} + } } diff --git a/ReactCommon/cxxreact/JSCUtils.h b/ReactCommon/cxxreact/JSCUtils.h index dfa1df71594..ebf83a679ef 100644 --- a/ReactCommon/cxxreact/JSCUtils.h +++ b/ReactCommon/cxxreact/JSCUtils.h @@ -11,5 +11,13 @@ namespace react { String jsStringFromBigString(JSContextRef ctx, const JSBigString& bigstr); +/** + * Parses "nativeRequire" parameters + * and returns pair of "bundle id" & "module id" values + */ +std::pair parseNativeRequireParameters(const JSGlobalContextRef& context, + const JSValueRef arguments[], + size_t argumentCount); + } }