Skip to content

Commit 32c8744

Browse files
authored
Merge pull request #3 from firefly-zero/stash
Stash: load_stash, save_stash, BEFORE_EXIT
2 parents f9c7073 + 4bf3a11 commit 32c8744

3 files changed

Lines changed: 42 additions & 2 deletions

File tree

src/firefly.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,34 @@ bool is_online(Peers peers, Peer peer)
250250
return ((peers.online >> peer) & 1) != 0;
251251
}
252252

253+
/// @brief Save the given Stash.
254+
///
255+
/// @details When called, the stash for the given peer will be stored in RAM.
256+
/// Calling load_stash() for the same peer will return that stash.
257+
/// On exit, the runtime will persist the stash in FS.
258+
/// Next time the app starts, calling load_stash() will restore the stash
259+
/// saved earlier.
260+
void save_stash(Peer p, Stash s)
261+
{
262+
_ffb_save_stash(p, (int)s.head, s.size);
263+
}
264+
265+
/// @brief Load Stash saved earlier (in this or previous run) by save_stash.
266+
///
267+
/// @details The buffer should be big enough to fit the stash.
268+
/// If it's not, the stash will be truncated.
269+
/// If there is no stash or it's empty, nil is returned.
270+
///
271+
/// If the given buffer is nil, a new buffer will be allocated
272+
/// big enough to fit the biggest allowed stash. At the moment, it is 80 bytes.
273+
Stash load_stash(Peer p, Buffer s)
274+
{
275+
Stash res;
276+
res.size = _ffb_load_stash(p, (int)s.head, s.size);
277+
res.head = s.head;
278+
return res;
279+
}
280+
253281
// -- MISC -- //
254282

255283
/// @brief Write a debug message.

src/firefly.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
/// @brief Mark a "cheat" callback function.
2020
#define CHEAT __attribute__((export_name("cheat")))
2121

22+
/// @brief Mark a "before_exit" callback function.
23+
#define BEFORE_EXIT __attribute__((export_name("BEFORE_EXIT")))
24+
2225
/// @brief Screen width.
2326
#define WIDTH 240
2427
/// @brief Screen height.
@@ -152,6 +155,7 @@ typedef struct Buffer Buffer;
152155
typedef struct Buffer File;
153156
typedef struct Buffer Image;
154157
typedef struct Buffer Canvas;
158+
typedef struct Buffer Stash;
155159

156160
/// @brief A subregion of an Image.
157161
struct SubImage
@@ -378,6 +382,8 @@ void remove_file(char *path);
378382
Peer get_me();
379383
Peers get_peers();
380384
bool is_online(Peers peers, Peer peer);
385+
void save_stash(Peer p, Stash s);
386+
Stash load_stash(Peer p, Buffer s);
381387

382388
void log_debug(char *msg);
383389
void log_error(char *msg);

src/firefly_bindings.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,18 @@ void _ffb_remove_file(uintptr_t pathPtr, uintptr_t pathLen);
8181

8282
// -- NET -- //
8383

84-
WASM_IMPORT("misc", "get_me")
84+
WASM_IMPORT("net", "get_me")
8585
int32_t _ffb_get_me();
8686

87-
WASM_IMPORT("misc", "get_peers")
87+
WASM_IMPORT("net", "get_peers")
8888
int32_t _ffb_get_peers();
8989

90+
WASM_IMPORT("net", "save_stash")
91+
_ffb_save_stash(uintptr_t peerID, uintptr_t bufPtr, uintptr_t bufLen);
92+
93+
WASM_IMPORT("net", "load_stash")
94+
int32_t _ffb_load_stash(uintptr_t peerID, uintptr_t bufPtr, uintptr_t bufLen);
95+
9096
// -- STATS -- //
9197

9298
WASM_IMPORT("misc", "add_progress")

0 commit comments

Comments
 (0)