Skip to content

Commit

Permalink
[android] Add method to load a script from string instead of file
Browse files Browse the repository at this point in the history
Loading directly from a string instead of a file provides more flexibility for bundle sources other than disk.
  • Loading branch information
ide committed Feb 21, 2018
1 parent 789ba76 commit e2b34f7
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -218,13 +218,29 @@ public void registerSegment(int segmentId, String path) {

/* package */ void loadScriptFromFile(String fileName, String sourceURL, boolean loadSynchronously) {
mSourceURL = sourceURL;
jniLoadScriptFromFile(fileName, sourceURL, loadSynchronously);

try {
final String contents = (String) Class.forName("host.exp.exponent.ReactNativeStaticHelpers")
.getMethod("getBundleSourceForPath", String.class)
.invoke(null, fileName);
if (contents == null) {
Log.d("CatalystInstanceImpl", "Loading script from file");
jniLoadScriptFromFile(fileName, sourceURL, loadSynchronously);
} else {
Log.d("CatalystInstanceImpl", "Loading script from string. Length: " + contents.length());
jniLoadScriptFromString(contents, sourceURL, loadSynchronously);
}
} catch (Exception e) {
Log.d("CatalystInstanceImpl", "Loading script from file");
jniLoadScriptFromFile(fileName, sourceURL, loadSynchronously);
}
}

private native void jniSetSourceURL(String sourceURL);
private native void jniRegisterSegment(int segmentId, String path);
private native void jniLoadScriptFromAssets(AssetManager assetManager, String assetURL, boolean loadSynchronously);
private native void jniLoadScriptFromFile(String fileName, String sourceURL, boolean loadSynchronously);
private native void jniLoadScriptFromString(String script, String mSourceURL, boolean loadSynchronously);

@Override
public void runJSBundle() {
Expand Down
9 changes: 9 additions & 0 deletions ReactAndroid/src/main/jni/react/jni/CatalystInstanceImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ void CatalystInstanceImpl::registerNatives() {
makeNativeMethod("jniRegisterSegment", CatalystInstanceImpl::jniRegisterSegment),
makeNativeMethod("jniLoadScriptFromAssets", CatalystInstanceImpl::jniLoadScriptFromAssets),
makeNativeMethod("jniLoadScriptFromFile", CatalystInstanceImpl::jniLoadScriptFromFile),
makeNativeMethod("jniLoadScriptFromString", CatalystInstanceImpl::jniLoadScriptFromString),
makeNativeMethod("jniCallJSFunction", CatalystInstanceImpl::jniCallJSFunction),
makeNativeMethod("jniCallJSCallback", CatalystInstanceImpl::jniCallJSCallback),
makeNativeMethod("setGlobalVariable", CatalystInstanceImpl::setGlobalVariable),
Expand Down Expand Up @@ -195,6 +196,14 @@ void CatalystInstanceImpl::jniLoadScriptFromAssets(
}
}

void CatalystInstanceImpl::jniLoadScriptFromString(
const std::string& scriptStdString,
const std::string& sourceURL,
bool loadSynchronously) {
std::unique_ptr<const JSBigStdString> script = folly::make_unique<const JSBigStdString>(scriptStdString);
instance_->loadScriptFromString(std::move(script), sourceURL, loadSynchronously);
}

void CatalystInstanceImpl::jniLoadScriptFromFile(const std::string& fileName,
const std::string& sourceURL,
bool loadSynchronously) {
Expand Down
1 change: 1 addition & 0 deletions ReactAndroid/src/main/jni/react/jni/CatalystInstanceImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ class CatalystInstanceImpl : public jni::HybridClass<CatalystInstanceImpl> {

void jniLoadScriptFromAssets(jni::alias_ref<JAssetManager::javaobject> assetManager, const std::string& assetURL, bool loadSynchronously);
void jniLoadScriptFromFile(const std::string& fileName, const std::string& sourceURL, bool loadSynchronously);
void jniLoadScriptFromString(const std::string& fileName, const std::string& sourceURL, bool loadSynchronously);
void jniCallJSFunction(std::string module, std::string method, NativeArray* arguments);
void jniCallJSCallback(jint callbackId, NativeArray* arguments);
void setGlobalVariable(std::string propName,
Expand Down

0 comments on commit e2b34f7

Please sign in to comment.