aboutsummaryrefslogtreecommitdiff
path: root/src/deep.h
blob: 298342746fe73fa296c8840ebb9e10f3e4c97ed9 (plain)
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
#pragma once

/*
 * public 'deep' API to be used by external modules if they want to implement Lanes-aware userdata
 * said modules will have to link against lanes (it is not really possible to separate the 'deep userdata' implementation from the rest of Lanes)
 */

#ifdef __cplusplus
extern "C" {
#endif // __cplusplus
#include "lua.h"
#ifdef __cplusplus
}
#endif // __cplusplus

#include "lanesconf.h"
#include "uniquekey.h"

#include <atomic>

// forwards
struct Universe;

enum LookupMode
{
    eLM_LaneBody, // send the lane body directly from the source to the destination lane
    eLM_ToKeeper, // send a function from a lane to a keeper state
    eLM_FromKeeper // send a function from a keeper state to a lane
};

enum DeepOp
{
    eDO_new,
    eDO_delete,
    eDO_metatable,
    eDO_module,
};

using luaG_IdFunction = void*(*)( lua_State* L, DeepOp op_);

// ################################################################################################

// xxh64 of string "DEEP_VERSION_3" generated at https://www.pelock.com/products/hash-calculator
static constexpr UniqueKey DEEP_VERSION{ 0xB2CC0FD9C0AE9674ull };

// should be used as header for deep userdata
// a deep userdata is a full userdata that stores a single pointer to the actual DeepPrelude-derived object
struct DeepPrelude
{
    UniqueKey const magic{ DEEP_VERSION };
    // when stored in a keeper state, the full userdata doesn't have a metatable, so we need direct access to the idfunc
    luaG_IdFunction idfunc { nullptr };
    // data is destroyed when refcount is 0
    std::atomic<int> m_refcount{ 0 };
};

char const* push_deep_proxy(lua_State* L, DeepPrelude* prelude, int nuv_, LookupMode mode_);
void free_deep_prelude( lua_State* L, DeepPrelude* prelude_);

LANES_API int luaG_newdeepuserdata( lua_State* L, luaG_IdFunction idfunc, int nuv_);
LANES_API DeepPrelude* luaG_todeep(lua_State* L, luaG_IdFunction idfunc, int index);