Skip to content
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions ReactAndroid/src/main/jni/react/jni/CatalystInstanceImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,8 @@ void CatalystInstanceImpl::jniLoadScriptFromAssets(
sourceURL,
loadSynchronously);
return;
} else if (Instance::isIndexedRAMBundle(&script)) {
instance_->loadRAMBundleFromString(std::move(script), sourceURL);
} else {
instance_->loadScriptFromString(std::move(script), sourceURL, loadSynchronously);
}
Expand Down
18 changes: 18 additions & 0 deletions ReactCommon/cxxreact/Instance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,24 @@ bool Instance::isIndexedRAMBundle(const char *sourcePath) {
return parseTypeFromHeader(header) == ScriptTag::RAMBundle;
}

bool Instance::isIndexedRAMBundle(std::unique_ptr<const JSBigString>* script) {
BundleHeader header;
strncpy(reinterpret_cast<char *>(&header), script->get()->c_str(), sizeof(header));

return parseTypeFromHeader(header) == ScriptTag::RAMBundle;
}

void Instance::loadRAMBundleFromString(std::unique_ptr<const JSBigString> script, const std::string& sourceURL) {
auto bundle = folly::make_unique<JSIndexedRAMBundle>(std::move(script));
auto startupScript = bundle->getStartupCode();
auto registry = RAMBundleRegistry::singleBundleRegistry(std::move(bundle));
loadRAMBundle(
std::move(registry),
std::move(startupScript),
sourceURL,
true);
}

void Instance::loadRAMBundleFromFile(const std::string& sourcePath,
const std::string& sourceURL,
bool loadSynchronously) {
Expand Down
2 changes: 2 additions & 0 deletions ReactCommon/cxxreact/Instance.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ class RN_EXPORT Instance {
void loadScriptFromString(std::unique_ptr<const JSBigString> string,
std::string sourceURL, bool loadSynchronously);
static bool isIndexedRAMBundle(const char *sourcePath);
static bool isIndexedRAMBundle(std::unique_ptr<const JSBigString>* string);
void loadRAMBundleFromString(std::unique_ptr<const JSBigString> script, const std::string& sourceURL);
void loadRAMBundleFromFile(const std::string& sourcePath,
const std::string& sourceURL,
bool loadSynchronously);
Expand Down
37 changes: 28 additions & 9 deletions ReactCommon/cxxreact/JSIndexedRAMBundle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
#include "JSIndexedRAMBundle.h"

#include <glog/logging.h>

#include <fstream>
#include <sstream>
#include <folly/Memory.h>

namespace facebook {
Expand All @@ -18,14 +19,32 @@ std::function<std::unique_ptr<JSModulesUnbundle>(std::string)> JSIndexedRAMBundl
};
}

JSIndexedRAMBundle::JSIndexedRAMBundle(const char *sourcePath) :
m_bundle (sourcePath, std::ios_base::in) {
JSIndexedRAMBundle::JSIndexedRAMBundle(const char *sourcePath) {
m_bundle = std::make_unique<std::ifstream>(
std::ifstream(sourcePath, std::ifstream::binary));
if (!m_bundle) {
throw std::ios_base::failure(
folly::to<std::string>("Bundle ", sourcePath,
"cannot be opened: ", m_bundle.rdstate()));
"cannot be opened: ", m_bundle->rdstate()));
}
init();
}

JSIndexedRAMBundle::JSIndexedRAMBundle(std::unique_ptr<const JSBigString> script) {
// tmpStream is needed because m_bundle is std::istream type
// which has no member 'write'
std::unique_ptr<std::stringstream> tmpStream = std::make_unique<std::stringstream>(
std::stringstream());
tmpStream->write(script->c_str(), script->size());
m_bundle = std::move(tmpStream);
if (!m_bundle) {
throw std::ios_base::failure(
folly::to<std::string>("Bundle from string cannot be opened: ", m_bundle->rdstate()));
}
init();
}

void JSIndexedRAMBundle::init() {
// read in magic header, number of entries, and length of the startup section
uint32_t header[3];
static_assert(
Expand Down Expand Up @@ -78,12 +97,12 @@ std::string JSIndexedRAMBundle::getModuleCode(const uint32_t id) const {
}

void JSIndexedRAMBundle::readBundle(char *buffer, const std::streamsize bytes) const {
if (!m_bundle.read(buffer, bytes)) {
if (m_bundle.rdstate() & std::ios::eofbit) {
if (!m_bundle->read(buffer, bytes)) {
if (m_bundle->rdstate() & std::ios::eofbit) {
throw std::ios_base::failure("Unexpected end of RAM Bundle file");
}
throw std::ios_base::failure(
folly::to<std::string>("Error reading RAM Bundle: ", m_bundle.rdstate()));
folly::to<std::string>("Error reading RAM Bundle: ", m_bundle->rdstate()));
}
}

Expand All @@ -92,9 +111,9 @@ void JSIndexedRAMBundle::readBundle(
const std::streamsize bytes,
const std::ifstream::pos_type position) const {

if (!m_bundle.seekg(position)) {
if (!m_bundle->seekg(position)) {
throw std::ios_base::failure(
folly::to<std::string>("Error reading RAM Bundle: ", m_bundle.rdstate()));
folly::to<std::string>("Error reading RAM Bundle: ", m_bundle->rdstate()));
}
readBundle(buffer, bytes);
}
Expand Down
8 changes: 5 additions & 3 deletions ReactCommon/cxxreact/JSIndexedRAMBundle.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

#pragma once

#include <fstream>
#include <istream>
#include <memory>

#include <cxxreact/JSBigString.h>
Expand All @@ -24,6 +24,7 @@ class RN_EXPORT JSIndexedRAMBundle : public JSModulesUnbundle {

// Throws std::runtime_error on failure.
JSIndexedRAMBundle(const char *sourceURL);
JSIndexedRAMBundle(std::unique_ptr<const JSBigString> script);

// Throws std::runtime_error on failure.
std::unique_ptr<const JSBigString> getStartupCode();
Expand Down Expand Up @@ -51,14 +52,15 @@ class RN_EXPORT JSIndexedRAMBundle : public JSModulesUnbundle {
}
};

void init();
std::string getModuleCode(const uint32_t id) const;
void readBundle(char *buffer, const std::streamsize bytes) const;
void readBundle(
char *buffer, const
std::streamsize bytes,
const std::ifstream::pos_type position) const;
const std::istream::pos_type position) const;

mutable std::ifstream m_bundle;
mutable std::unique_ptr<std::istream> m_bundle;
ModuleTable m_table;
size_t m_baseOffset;
std::unique_ptr<JSBigBufferString> m_startupCode;
Expand Down