2016-05-04 02:29:58 +00:00
|
|
|
// Copyright 2004-present Facebook. All Rights Reserved.
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <functional>
|
|
|
|
#include <memory>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
2016-07-11 13:52:06 +00:00
|
|
|
#include <sys/mman.h>
|
|
|
|
|
2016-05-18 19:46:01 +00:00
|
|
|
#include <folly/dynamic.h>
|
2016-05-04 02:29:58 +00:00
|
|
|
|
2016-11-01 18:38:43 +00:00
|
|
|
#include "JSModulesUnbundle.h"
|
|
|
|
|
2016-05-04 02:29:58 +00:00
|
|
|
namespace facebook {
|
|
|
|
namespace react {
|
|
|
|
|
2016-07-14 22:23:53 +00:00
|
|
|
#define UNPACKED_JS_SOURCE_PATH_SUFFIX "/bundle.js"
|
|
|
|
#define UNPACKED_META_PATH_SUFFIX "/bundle.meta"
|
2016-09-29 16:08:28 +00:00
|
|
|
#define UNPACKED_BYTECODE_SUFFIX "/bundle.bytecode"
|
2016-07-14 22:23:53 +00:00
|
|
|
|
|
|
|
enum {
|
|
|
|
UNPACKED_JS_SOURCE = (1 << 0),
|
2016-08-04 22:42:56 +00:00
|
|
|
UNPACKED_BC_CACHE = (1 << 1),
|
2016-09-29 16:08:28 +00:00
|
|
|
UNPACKED_BYTECODE = (1 << 2),
|
2016-07-14 22:23:53 +00:00
|
|
|
};
|
|
|
|
|
2016-05-04 02:29:58 +00:00
|
|
|
class JSExecutor;
|
2016-10-03 12:07:41 +00:00
|
|
|
class JSModulesUnbundle;
|
2016-05-04 02:29:58 +00:00
|
|
|
class MessageQueueThread;
|
2016-10-03 12:07:41 +00:00
|
|
|
class ModuleRegistry;
|
2016-05-04 02:29:58 +00:00
|
|
|
|
2016-05-18 19:46:01 +00:00
|
|
|
struct MethodCallResult {
|
|
|
|
folly::dynamic result;
|
|
|
|
bool isUndefined;
|
|
|
|
};
|
|
|
|
|
|
|
|
// This interface describes the delegate interface required by
|
|
|
|
// Executor implementations to call from JS into native code.
|
|
|
|
class ExecutorDelegate {
|
|
|
|
public:
|
|
|
|
virtual ~ExecutorDelegate() {}
|
|
|
|
|
|
|
|
virtual void registerExecutor(std::unique_ptr<JSExecutor> executor,
|
|
|
|
std::shared_ptr<MessageQueueThread> queue) = 0;
|
|
|
|
virtual std::unique_ptr<JSExecutor> unregisterExecutor(JSExecutor& executor) = 0;
|
|
|
|
|
2016-10-03 12:07:41 +00:00
|
|
|
virtual std::shared_ptr<ModuleRegistry> getModuleRegistry() = 0;
|
|
|
|
|
2016-05-18 19:46:01 +00:00
|
|
|
virtual void callNativeModules(
|
2016-09-19 11:43:09 +00:00
|
|
|
JSExecutor& executor, folly::dynamic&& calls, bool isEndOfBatch) = 0;
|
2016-05-18 19:46:01 +00:00
|
|
|
virtual MethodCallResult callSerializableNativeHook(
|
|
|
|
JSExecutor& executor, unsigned int moduleId, unsigned int methodId, folly::dynamic&& args) = 0;
|
|
|
|
};
|
|
|
|
|
2016-05-04 02:29:58 +00:00
|
|
|
class JSExecutorFactory {
|
|
|
|
public:
|
|
|
|
virtual std::unique_ptr<JSExecutor> createJSExecutor(
|
2016-05-18 19:46:01 +00:00
|
|
|
std::shared_ptr<ExecutorDelegate> delegate,
|
|
|
|
std::shared_ptr<MessageQueueThread> jsQueue) = 0;
|
|
|
|
virtual ~JSExecutorFactory() {}
|
2016-05-04 02:29:58 +00:00
|
|
|
};
|
|
|
|
|
2016-05-14 00:15:06 +00:00
|
|
|
// JSExecutor functions sometimes take large strings, on the order of
|
|
|
|
// megabytes. Copying these can be expensive. Introducing a
|
|
|
|
// move-only, non-CopyConstructible type will let the compiler ensure
|
|
|
|
// that no copies occur. folly::MoveWrapper should be used when a
|
|
|
|
// large string needs to be curried into a std::function<>, which must
|
|
|
|
// by CopyConstructible.
|
|
|
|
|
|
|
|
class JSBigString {
|
|
|
|
public:
|
|
|
|
JSBigString() = default;
|
|
|
|
|
|
|
|
// Not copyable
|
|
|
|
JSBigString(const JSBigString&) = delete;
|
|
|
|
JSBigString& operator=(const JSBigString&) = delete;
|
|
|
|
|
|
|
|
virtual ~JSBigString() {}
|
|
|
|
|
|
|
|
virtual bool isAscii() const = 0;
|
|
|
|
virtual const char* c_str() const = 0;
|
|
|
|
virtual size_t size() const = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Concrete JSBigString implementation which holds a std::string
|
|
|
|
// instance.
|
|
|
|
class JSBigStdString : public JSBigString {
|
|
|
|
public:
|
|
|
|
JSBigStdString(std::string str, bool isAscii=false)
|
|
|
|
: m_isAscii(isAscii)
|
|
|
|
, m_str(std::move(str)) {}
|
|
|
|
|
|
|
|
bool isAscii() const override {
|
|
|
|
return m_isAscii;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char* c_str() const override {
|
|
|
|
return m_str.c_str();
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t size() const override {
|
|
|
|
return m_str.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
bool m_isAscii;
|
|
|
|
std::string m_str;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Concrete JSBigString implementation which holds a heap-allocated
|
|
|
|
// buffer, and provides an accessor for writing to it. This can be
|
|
|
|
// used to construct a JSBigString in place, such as by reading from a
|
|
|
|
// file.
|
|
|
|
class JSBigBufferString : public facebook::react::JSBigString {
|
|
|
|
public:
|
|
|
|
JSBigBufferString(size_t size)
|
2016-06-30 23:41:27 +00:00
|
|
|
: m_data(new char[size + 1])
|
|
|
|
, m_size(size) {
|
|
|
|
// Guarantee nul-termination. The caller is responsible for
|
|
|
|
// filling in the rest of m_data.
|
|
|
|
m_data[m_size] = '\0';
|
|
|
|
}
|
2016-05-14 00:15:06 +00:00
|
|
|
|
|
|
|
~JSBigBufferString() {
|
|
|
|
delete[] m_data;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isAscii() const override {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char* c_str() const override {
|
|
|
|
return m_data;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t size() const override {
|
|
|
|
return m_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
char* data() {
|
|
|
|
return m_data;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
char* m_data;
|
|
|
|
size_t m_size;
|
|
|
|
};
|
|
|
|
|
2016-07-11 13:52:06 +00:00
|
|
|
class JSBigMmapString : public JSBigString {
|
|
|
|
public:
|
|
|
|
enum class Encoding {
|
|
|
|
Unknown,
|
|
|
|
Ascii,
|
|
|
|
Utf8,
|
|
|
|
Utf16,
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
JSBigMmapString(int fd, size_t size, const uint8_t sha1[20], Encoding encoding) :
|
|
|
|
m_fd(fd),
|
|
|
|
m_size(size),
|
|
|
|
m_encoding(encoding),
|
|
|
|
m_str(nullptr)
|
|
|
|
{
|
|
|
|
memcpy(m_hash, sha1, 20);
|
|
|
|
}
|
|
|
|
|
|
|
|
~JSBigMmapString() {
|
|
|
|
if (m_str) {
|
|
|
|
CHECK(munmap((void *)m_str, m_size) != -1);
|
|
|
|
}
|
|
|
|
close(m_fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isAscii() const override {
|
|
|
|
return m_encoding == Encoding::Ascii;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char* c_str() const override {
|
|
|
|
if (!m_str) {
|
|
|
|
m_str = (const char *)mmap(0, m_size, PROT_READ, MAP_SHARED, m_fd, 0);
|
|
|
|
CHECK(m_str != MAP_FAILED);
|
|
|
|
}
|
|
|
|
return m_str;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t size() const override {
|
|
|
|
return m_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
int fd() const {
|
|
|
|
return m_fd;
|
|
|
|
}
|
|
|
|
|
|
|
|
const uint8_t* hash() const {
|
|
|
|
return m_hash;
|
|
|
|
}
|
|
|
|
|
|
|
|
Encoding encoding() const {
|
|
|
|
return m_encoding;
|
|
|
|
}
|
|
|
|
|
2016-07-14 22:23:53 +00:00
|
|
|
static std::unique_ptr<const JSBigMmapString> fromOptimizedBundle(const std::string& bundlePath);
|
|
|
|
|
2016-07-11 13:52:06 +00:00
|
|
|
private:
|
|
|
|
int m_fd;
|
|
|
|
size_t m_size;
|
|
|
|
uint8_t m_hash[20];
|
|
|
|
Encoding m_encoding;
|
|
|
|
mutable const char *m_str;
|
|
|
|
};
|
|
|
|
|
2016-05-04 02:29:58 +00:00
|
|
|
class JSExecutor {
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* Execute an application script bundle in the JS context.
|
|
|
|
*/
|
2016-05-14 00:15:06 +00:00
|
|
|
virtual void loadApplicationScript(std::unique_ptr<const JSBigString> script,
|
|
|
|
std::string sourceURL) = 0;
|
2016-05-04 02:29:58 +00:00
|
|
|
|
2016-07-14 22:23:53 +00:00
|
|
|
/**
|
|
|
|
* Execute an application script optimized bundle in the JS context.
|
|
|
|
*/
|
|
|
|
virtual void loadApplicationScript(std::string bundlePath, std::string source, int flags);
|
|
|
|
|
2016-05-04 02:29:58 +00:00
|
|
|
/**
|
|
|
|
* Add an application "unbundle" file
|
|
|
|
*/
|
|
|
|
virtual void setJSModulesUnbundle(std::unique_ptr<JSModulesUnbundle> bundle) = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Executes BatchedBridge.callFunctionReturnFlushedQueue with the module ID,
|
|
|
|
* method ID and optional additional arguments in JS. The executor is responsible
|
|
|
|
* for using Bridge->callNativeModules to invoke any necessary native modules methods.
|
|
|
|
*/
|
|
|
|
virtual void callFunction(const std::string& moduleId, const std::string& methodId, const folly::dynamic& arguments) = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Executes BatchedBridge.invokeCallbackAndReturnFlushedQueue with the cbID,
|
|
|
|
* and optional additional arguments in JS and returns the next queue. The executor
|
|
|
|
* is responsible for using Bridge->callNativeModules to invoke any necessary
|
|
|
|
* native modules methods.
|
|
|
|
*/
|
|
|
|
virtual void invokeCallback(const double callbackId, const folly::dynamic& arguments) = 0;
|
|
|
|
|
2016-05-14 00:15:06 +00:00
|
|
|
virtual void setGlobalVariable(std::string propName,
|
|
|
|
std::unique_ptr<const JSBigString> jsonValue) = 0;
|
2016-05-04 02:29:58 +00:00
|
|
|
virtual void* getJavaScriptContext() {
|
|
|
|
return nullptr;
|
2016-05-18 19:46:01 +00:00
|
|
|
}
|
2016-05-04 02:29:58 +00:00
|
|
|
virtual bool supportsProfiling() {
|
|
|
|
return false;
|
2016-05-18 19:46:01 +00:00
|
|
|
}
|
|
|
|
virtual void startProfiler(const std::string &titleString) {}
|
|
|
|
virtual void stopProfiler(const std::string &titleString, const std::string &filename) {}
|
2016-05-26 18:07:41 +00:00
|
|
|
virtual void handleMemoryPressureUiHidden() {}
|
2016-05-18 19:46:01 +00:00
|
|
|
virtual void handleMemoryPressureModerate() {}
|
2016-05-04 02:29:58 +00:00
|
|
|
virtual void handleMemoryPressureCritical() {
|
|
|
|
handleMemoryPressureModerate();
|
2016-05-18 19:46:01 +00:00
|
|
|
}
|
|
|
|
virtual void destroy() {}
|
|
|
|
virtual ~JSExecutor() {}
|
2016-05-04 02:29:58 +00:00
|
|
|
};
|
|
|
|
|
2016-07-14 22:23:53 +00:00
|
|
|
std::unique_ptr<const JSBigMmapString> readJSBundle(const std::string& path);
|
|
|
|
|
2016-05-04 02:29:58 +00:00
|
|
|
} }
|