Repository navigation
Expand file tree
/
Copy pathCodeCache.h
More file actions
123 lines (105 loc) · 5.34 KB
/
Copy pathCodeCache.h
File metadata and controls
123 lines (105 loc) · 5.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#ifndef NATIVESCRIPT_COMMON_LOADER_CODE_CACHE_H_
#define NATIVESCRIPT_COMMON_LOADER_CODE_CACHE_H_
#include <cstddef>
#include <cstdint>
#include <memory>
#include <optional>
#include <string>
#include <string_view>
#include "ns/AppFiles.h"
#include "ns/LoaderConfig.h"
// The on-disk V8 code cache: where a source's cache lives, whether it is
// fresh, and reading and writing it. V8-free; the deferred creation queue is
// in ns/loader/CodeCacheQueue.h.
//
// A cache file is a header followed by V8's cached data. The header holds a
// magic number, the format version, the kind, the module format, the cache
// key, the source's size and freshness token as platform::Stat reported them
// before the source was read, and the payload length. A cache is fresh when
// all of them match; V8 validates its own version and flags when it consumes
// the payload.
namespace nativescript::common::loader {
// V8 rejects a cache of the other kind, and one file can be compiled both
// ways (require() and import), so each kind has a file of its own.
enum class CodeCacheKind : uint32_t {
kClassicScript = 1,
kEsModule = 2,
};
// How import takes a source, recorded in its kEsModule cache file. A .js
// file that is CommonJS gets a file with no payload: a marker that spares the
// next import the detection compile.
enum class ModuleFormat : uint32_t {
kNone = 0, // a classic script's cache
kCommonJs = 1,
kEsModule = 2,
};
// One source's cache file and the source state it has to describe.
struct CodeCacheEntry {
// Absolute: codeCacheDir + "/" + CodeCacheFileName(key, kind).
std::string file;
// The logical path of the source.
std::string sourcePath;
// CodeCacheKey(sourcePath, appRoot); stored in the header, so two keys
// whose names collide read as a miss, never as each other's cache.
std::string key;
CodeCacheKind kind = CodeCacheKind::kEsModule;
// kNone for kClassicScript; kEsModule or kCommonJs for kEsModule.
ModuleFormat format = ModuleFormat::kEsModule;
// The source's Stat, taken before the source was read.
FileInfo source;
};
// The source's path relative to `appRoot` when it is inside it, which keeps
// names stable when the app moves (an iOS update changes the bundle path);
// otherwise the path itself. A relative key never starts with '/', so the two
// forms cannot coincide.
std::string CodeCacheKey(std::string_view logicalPath,
std::string_view appRoot);
// 16 lowercase hex digits of the 64-bit FNV-1a hash of `key`, then ".cache"
// for a classic script or ".mcache" for an ES module.
std::string CodeCacheFileName(std::string_view key, CodeCacheKind kind);
// The entry for the source at `logicalPath` as `source` describes it, whether
// or not a cache file exists yet; nullopt when caching is off: the loader
// config has codeCacheEnabled false or no codeCacheDir, `source` is not a
// file, or it has no freshness token. The format is kEsModule for kEsModule
// and kNone for kClassicScript. Any thread.
std::optional<CodeCacheEntry> CodeCacheEntryFor(std::string_view logicalPath,
CodeCacheKind kind,
const FileInfo& source,
const LoaderConfig& config);
// Whether entry.file holds a cache for exactly entry.source, in
// entry.format. Reads the header only, never the payload or the source. Any
// thread.
bool HasFreshCodeCache(const CodeCacheEntry& entry);
// The format entry.file records when it is fresh for entry.source apart from
// its format; nullopt otherwise. Reads the header only. Any thread.
std::optional<ModuleFormat> FreshModuleFormat(const CodeCacheEntry& entry);
struct CodeCacheBytes {
// Allocated with new[], so v8::ScriptCompiler::CachedData::BufferOwned can
// adopt it.
std::unique_ptr<uint8_t[]> data;
int length = 0;
};
// The payload, all of it, or nullopt when the file is missing, unreadable,
// truncated, a kCommonJs marker or written for another source. The header is
// checked again: a newer file may have replaced the one HasFreshCodeCache saw.
// Any thread.
std::optional<CodeCacheBytes> LoadCodeCache(const CodeCacheEntry& entry);
// Writes the cache under a temporary name in the same directory and renames
// it into place, creating the directory if needed. A kCommonJs entry takes no
// payload (null, 0); any other takes one. Readers see the old file
// or the whole new one, never a partial file. False on any failure, with
// nothing left behind. Any thread.
bool WriteCodeCache(const CodeCacheEntry& entry, const uint8_t* payload,
size_t length);
// Whether platform::Stat still reports `decided` for `logicalPath`: the same
// size and freshness token. A cache chosen from `decided` describes bytes
// read after it only when this holds after the read, because V8 checks a
// cache against the source's length alone. Any thread.
bool SourceUnchanged(std::string_view logicalPath, const FileInfo& decided);
// Hands the payload to the core's serial writer thread, which writes it with
// WriteCodeCache unless the source changed since entry.source. Writes inline
// when no thread could be started. Any thread.
void PostCodeCacheWrite(CodeCacheEntry entry,
std::unique_ptr<uint8_t[]> payload, size_t length);
} // namespace nativescript::common::loader
#endif // NATIVESCRIPT_COMMON_LOADER_CODE_CACHE_H_