mirror of
https://github.com/zhigang1992/react-native.git
synced 2026-05-16 10:39:50 +08:00
Add new FileSourceProvider
Summary: Add a new interface to JSC that allows loading a file lazily from disk, i.e. using mmap, instead of loading the whole file upfront and copying into the VM. Reviewed By: michalgr Differential Revision: D3534042 fbshipit-source-id: 98b193cc7b7e33248073e2556ea94ce3391507c7
This commit is contained in:
committed by
Facebook Github Bot 9
parent
e5650560c0
commit
5d06918d07
@@ -7,6 +7,8 @@
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include <sys/mman.h>
|
||||
|
||||
#include <folly/dynamic.h>
|
||||
|
||||
#include "JSModulesUnbundle.h"
|
||||
@@ -134,6 +136,68 @@ private:
|
||||
size_t m_size;
|
||||
};
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
private:
|
||||
int m_fd;
|
||||
size_t m_size;
|
||||
uint8_t m_hash[20];
|
||||
Encoding m_encoding;
|
||||
mutable const char *m_str;
|
||||
};
|
||||
|
||||
class JSExecutor {
|
||||
public:
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user