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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
|
#pragma once
/// @file
/// WasmStore: a Store implementation that delegates every async operation to
/// the Go host via a single synchronous WASM import function.
///
/// Also declares lix_store_call, the raw WASM import, so other translation
/// units (e.g. file-system.cc) can call it without redeclaring the symbol.
#ifdef __EMSCRIPTEN__
extern "C" __attribute__((import_module("lix_store"), import_name("call"))) int32_t
lix_store_call(const char * req_ptr, int32_t req_len, char * resp_ptr, int32_t resp_max_len);
#else
extern "C" int32_t
lix_store_call(const char * req_ptr, int32_t req_len, char * resp_ptr, int32_t resp_max_len);
#endif
///
/// The import function (declared extern "C" in wasm-store.cc):
///
/// int32_t lix_store_call(
/// const char * req_ptr, int32_t req_len,
/// char * resp_ptr, int32_t resp_max_len
/// );
///
/// Returns bytes written to resp_ptr, or negative on hard failure.
/// Requests and responses use the lix wire codec (see gonix/codec/codec.go).
#include "lix/libstore/build-result.hh"
#include "lix/libstore/gc-store.hh"
#include "lix/libstore/log-store.hh"
#include "lix/libstore/store-api.hh"
#include "lix/libutil/result.hh"
#include <kj/async.h>
#include <string>
#include <string_view>
#include <vector>
namespace nix {
struct WasmStoreConfig final : StoreConfig
{
using StoreConfig::StoreConfig;
const std::string name() override
{
return "WASM Host Store";
}
std::string doc() override
{
return "Store backed by the Go WASM host via lix_store_call.";
}
};
struct WasmStore final : public virtual Store, public GcStore, public LogStore
{
WasmStoreConfig config_;
WasmStoreConfig & config() override
{
return config_;
}
const WasmStoreConfig & config() const override
{
return config_;
}
WasmStore(const std::string & scheme, const std::string & uri, WasmStoreConfig config);
explicit WasmStore(WasmStoreConfig config);
std::string getUri() override
{
return "wasm://";
}
static std::set<std::string> uriSchemes()
{
return {"wasm"};
}
// -----------------------------------------------------------------------
// Store — path operations (host-delegated)
// -----------------------------------------------------------------------
kj::Promise<Result<bool>>
isValidPathUncached(const StorePath & path, const Activity * context = nullptr) override;
kj::Promise<Result<std::shared_ptr<const ValidPathInfo>>>
queryPathInfoUncached(const StorePath & path, const Activity * context) override;
kj::Promise<Result<std::optional<StorePath>>>
queryPathFromHashPart(const std::string & hashPart) override;
kj::Promise<Result<StorePathSet>>
queryValidPaths(const StorePathSet & paths, SubstituteFlag maybeSubstitute = NoSubstitute) override;
kj::Promise<Result<StorePathSet>> queryAllValidPaths() override;
kj::Promise<Result<void>> computeFSClosure(
const StorePathSet & paths,
StorePathSet & out,
bool flipDirection = false,
bool includeOutputs = false,
bool includeDerivers = false
) override;
kj::Promise<Result<void>> ensurePath(const StorePath & path) override;
kj::Promise<Result<StorePath>> addTextToStore(
std::string_view name,
std::string_view s,
const StorePathSet & references,
RepairFlag repair = NoRepair
) override;
kj::Promise<Result<void>> addToStore(
const ValidPathInfo & info,
AsyncInputStream & source,
RepairFlag repair = NoRepair,
CheckSigsFlag checkSigs = CheckSigs,
const Activity * context = nullptr
) override;
kj::Promise<Result<void>> buildPaths(
const std::vector<DerivedPath> & paths,
BuildMode buildMode = bmNormal,
std::shared_ptr<Store> evalStore = nullptr
) override;
kj::Promise<Result<std::vector<KeyedBuildResult>>> buildPathsWithResults(
const std::vector<DerivedPath> & paths,
BuildMode buildMode = bmNormal,
std::shared_ptr<Store> evalStore = nullptr
) override;
kj::Promise<Result<BuildResult>> buildDerivation(
const StorePath & drvPath, const BasicDerivation & drv, BuildMode buildMode = bmNormal
) override;
kj::Promise<Result<void>> addTempRoot(const StorePath & path) override;
kj::Promise<Result<std::map<std::string, StorePath>>>
queryDerivationOutputMap(const StorePath & path, Store * evalStore = nullptr) override;
kj::Promise<Result<std::map<std::string, StorePath>>>
queryStaticDerivationOutputMap(const StorePath & path);
kj::Promise<Result<std::optional<TrustedFlag>>> isTrustedClient() override;
kj::Promise<Result<void>>
querySubstitutablePathInfos(const StorePathCAMap & paths, SubstitutablePathInfos & infos) override;
ref<FSAccessor> getFSAccessor() override;
kj::Promise<Result<box_ptr<AsyncInputStream>>>
narFromPath(const StorePath & path, const Activity * context = nullptr) override;
// -----------------------------------------------------------------------
// GcStore
// -----------------------------------------------------------------------
kj::Promise<Result<Roots>> findRoots(bool censor) override;
kj::Promise<Result<void>> collectGarbage(const GCOptions & options, GCResults & results) override;
// -----------------------------------------------------------------------
// LogStore
kj::Promise<Result<std::optional<std::string>>> getBuildLogExact(const StorePath & path) override;
kj::Promise<Result<void>> addBuildLog(const StorePath & path, std::string_view log) override;
// -----------------------------------------------------------------------
// Decoder cursor (public so static helpers in .cc can use it)
// -----------------------------------------------------------------------
struct Cursor
{
std::string_view data;
size_t pos = 0;
bool done() const
{
return pos >= data.size();
}
char peek() const
{
return data[pos];
}
char take()
{
return data[pos++];
}
size_t readLen();
std::string readScalar();
uint64_t readUint64();
bool readBool();
StorePathSet readStorePathSet(const Store & store);
void skipValue();
};
private:
// -----------------------------------------------------------------------
// Encoder helpers
// -----------------------------------------------------------------------
static std::string encScalar(std::string_view s);
static std::string encUint64(uint64_t v);
static std::string encBool(bool b);
static std::string encStorePathSet(const StorePathSet & ps, const Store & store);
static std::string encMessage(std::string_view inner);
std::vector<char> call(const std::string & encoded);
};
void registerWasmStore();
} // namespace nix
|