aboutsummaryrefslogtreecommitdiff
path: root/src/deep.cpp
blob: 82f121454d2c69f68e7e3cf2b780919b95b89ef2 (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
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
/*
 * DEEP.CPP                         Copyright (c) 2024, Benoit Germain
 *
 * Deep userdata support, separate in its own source file to help integration
 * without enforcing a Lanes dependency
 */

/*
===============================================================================

Copyright (C) 2002-10 Asko Kauppi <akauppi@gmail.com>
              2011-24 Benoit Germain <bnt.germain@gmail.com>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

===============================================================================
*/
#include "_pch.hpp"
#include "deep.hpp"

#include "tools.hpp"

/*-- Metatable copying --*/

/*---=== Deep userdata ===---*/

/*
 * 'registry[REGKEY]' is a two-way lookup table for 'factory's and those type's
 * metatables:
 *
 *   metatable   ->  factory
 *   factory      ->  metatable
 */
// xxh64 of string "kDeepFactoryMetatableBijectionRegKey" generated at https://www.pelock.com/products/hash-calculator
static constexpr RegistryUniqueKey kDeepFactoryMetatableBijectionRegKey{ 0xB4E223E56BF0BCD6ull };

/*
 * The deep proxy cache is a weak valued table listing all deep UD proxies indexed by the deep UD that they are proxying
 * xxh64 of string "kDeepProxyCacheRegKey" generated at https://www.pelock.com/products/hash-calculator
 */
static constexpr RegistryUniqueKey kDeepProxyCacheRegKey{ 0xEBCD49AE1A3DD35Eull };

// #################################################################################################
// #################################################################################################
namespace {
    // #############################################################################################
    // #############################################################################################

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

    // Pops the key (metatable or factory) off the stack, and replaces with the deep lookup value (factory/metatable/nil).
    static void LookupDeep(lua_State* const L_, LuaType const expectedType_)
    {
        STACK_GROW(L_, 1);
        STACK_CHECK_START_REL(L_, 1);                                                              // L_: a
        kDeepFactoryMetatableBijectionRegKey.pushValue(L_);                                        // L_: a {}
        if (!lua_isnil(L_, -1)) {
            lua_insert(L_, -2);                                                                    // L_: {} a
            lua_rawget(L_, -2);                                                                    // L_: {} b
        }
        lua_remove(L_, -2);                                                                        // L_: a|b
        LuaType const _type{ luaG_type(L_, kIdxTop) };
        if (_type != LuaType::NIL && _type != expectedType_) {
            raise_luaL_error(L_, "INTERNAL ERROR: Unexpected value.");
        }
        STACK_CHECK(L_, 1);
    }

    // #############################################################################################
    // #############################################################################################
} // namespace
// #################################################################################################
// #################################################################################################

// #################################################################################################
// #################################################################################################
// ####################################### DeepFactory #############################################
// #################################################################################################
// #################################################################################################

/*
    * void= mt.__gc( proxy_ud )
    *
    * End of life for a proxy object; reduce the deep reference count and clean it up if reaches 0.
    *
    */
[[nodiscard]]
int DeepFactory::DeepGC(lua_State* const L_)
{
    DeepPrelude* const* const _proxy{ luaG_tofulluserdata<DeepPrelude*>(L_, StackIndex{ 1 }) };
    DeepPrelude* const _p{ *_proxy };

    // can work without a universe if creating a deep userdata from some external C module when Lanes isn't loaded
    // in that case, we are not multithreaded and locking isn't necessary anyway
    bool const _isLastRef{ _p->refcount.fetch_sub(1, std::memory_order_relaxed) == 1 };

    if (_isLastRef) {
        // retrieve wrapped __gc, if any
        lua_pushvalue(L_, lua_upvalueindex(1));                                                    // L_: self __gc?
        if (!lua_isnil(L_, -1)) {
            lua_insert(L_, -2);                                                                    // L_: __gc self
            lua_call(L_, 1, 0);                                                                    // L_:
        } else {
            // need an empty stack in case we are GC_ing from a Keeper, so that empty stack checks aren't triggered
            lua_pop(L_, 2);                                                                        // L_:
        }
        DeleteDeepObject(L_, _p);
    }
    return 0;
}

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

// NEVER call deleteDeepObjectInternal by itself, ALWAYS go through DeleteDeepObject()
void DeepFactory::DeleteDeepObject(lua_State* const L_, DeepPrelude* const o_)
{
    STACK_CHECK_START_REL(L_, 0);
    o_->factory.deepObjectCount.fetch_sub(1, std::memory_order_relaxed);
    o_->factory.deleteDeepObjectInternal(L_, o_);
    STACK_CHECK(L_, 0);
}

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

bool DeepFactory::IsDeepUserdata(lua_State* const L_, StackIndex const idx_)
{
    return LookupFactory(L_, idx_, LookupMode::LaneBody) != nullptr;
}

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

// Return the registered factory for 'index' (deep userdata proxy),  or nullptr if 'index' is not a deep userdata proxy.
DeepFactory* DeepFactory::LookupFactory(lua_State* const L_, StackIndex const index_, LookupMode const mode_)
{
    // when looking inside a keeper, we are 100% sure the object is a deep userdata
    if (mode_ == LookupMode::FromKeeper) {
        DeepPrelude* const _proxy{ *luaG_tofulluserdata<DeepPrelude*>(L_, index_) };
        // we can (and must) cast and fetch the internally stored factory
        return &_proxy->factory;
    } else {
        // essentially we are making sure that the metatable of the object we want to copy is stored in our metatable/factory database
        // it is the only way to ensure that the userdata is indeed a deep userdata!
        // of course, we could just trust the caller, but we won't
        STACK_GROW(L_, 1);
        STACK_CHECK_START_REL(L_, 0);

        if (!lua_getmetatable(L_, index_)) {                                                       // L_: deep ... metatable?
            return nullptr; // no metatable: can't be a deep userdata object!
        }

        // replace metatable with the factory pointer, if it is actually a deep userdata
        LookupDeep(L_, LuaType::LIGHTUSERDATA);                                                    // L_: deep ... factory|nil

        DeepFactory* const _ret{ luaG_tolightuserdata<DeepFactory>(L_, kIdxTop) }; // nullptr if not a userdata
        lua_pop(L_, 1);
        STACK_CHECK(L_, 0);
        return _ret;
    }
}

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

/*
 * Push a proxy userdata on the stack.
 * raises an error in case of problem (error cannot happen with mode_ == LookupMode::ToKeeper)
 *
 * Initializes necessary structures if it's the first time 'factory' is being used in
 * this Lua state (metatable, registring it). Otherwise, increments the reference count.
 */
void DeepFactory::PushDeepProxy(DestState const L_, DeepPrelude* const prelude_, UserValueCount const nuv_, LookupMode const mode_, lua_State* const errL_)
{
    STACK_CHECK_START_REL(L_, 0);
    kDeepProxyCacheRegKey.getSubTableMode(L_, "v");                                                // L_: DPC

    // Check if a proxy already exists
    lua_pushlightuserdata(L_, prelude_);                                                           // L_: DPC deep
    if (luaG_rawget(L_, StackIndex{ -2 }) != LuaType::NIL) {                                       // L_: DPC proxy
        lua_remove(L_, -2);                                                                        // L_: proxy
        STACK_CHECK(L_, 1);
        return;
    } else {
        lua_pop(L_, 1);                                                                            // L_: DPC
    }
    STACK_CHECK(L_, 1);

    STACK_GROW(L_, 7);

    // a new full userdata, fitted with the specified number of uservalue slots (always 1 for Lua < 5.4)
    DeepPrelude** const _proxy{ luaG_newuserdatauv<DeepPrelude*>(L_, nuv_) };                      // L_: DPC proxy
    LUA_ASSERT(L_, _proxy);
    *_proxy = prelude_;
    prelude_->refcount.fetch_add(1, std::memory_order_relaxed); // one more proxy pointing to this deep data

    // Get/create metatable for 'factory' (in this state)
    DeepFactory& _factory = prelude_->factory;
    lua_pushlightuserdata(L_, std::bit_cast<void*>(&_factory));                                    // L_: DPC proxy _factory
    LookupDeep(L_, LuaType::TABLE);                                                                // L_: DPC proxy metatable|nil

    if (lua_isnil(L_, -1)) { // No metatable yet.
        lua_pop(L_, 1);                                                                            // L_: DPC proxy
        StackIndex const _oldtop{ lua_gettop(L_) };
        // 1 - make one and register it
        if (mode_ != LookupMode::ToKeeper) {
            _factory.createMetatable(L_);                                                          // L_: DPC proxy metatable
            if (lua_gettop(L_) - _oldtop != 1 || !lua_istable(L_, -1)) {
                // factory didn't push exactly 1 value, or the value it pushed is not a table: ERROR!
                raise_luaL_error(errL_, "Bad DeepFactory::createMetatable overload: unexpected pushed value");
            }
            // if the metatable contains a __gc, we will call it from our own
            std::ignore = luaG_rawgetfield(L_, kIdxTop, "__gc");                                   // L_: DPC proxy metatable __gc
        } else {
            // keepers need a minimal metatable that only contains our own __gc
            lua_createtable(L_, 0, 1);                                                             // L_: DPC proxy metatable
            lua_pushnil(L_);                                                                       // L_: DPC proxy metatable nil
        }
        if (lua_isnil(L_, -1)) {
            // Add our own '__gc' method
            lua_pop(L_, 1);                                                                        // L_: DPC proxy metatable
            lua_pushcfunction(L_, DeepGC);                                                         // L_: DPC proxy metatable DeepGC
        } else {
            // Add our own '__gc' method wrapping the original
            lua_pushcclosure(L_, DeepGC, 1);                                                       // L_: DPC proxy metatable DeepGC
        }
        lua_setfield(L_, -2, "__gc");                                                              // L_: DPC proxy metatable

        // Memorize for later rounds
        _factory.storeDeepLookup(L_);

        // 2 - cause the target state to require the module that exported the factory
        if (std::string_view const _modname{ _factory.moduleName() }; !_modname.empty()) { // we actually got a module name
            // L.registry._LOADED exists without having registered the 'package' library.
            lua_getglobal(L_, "require");                                                          // L_: DPC proxy metatable require()
            // check that the module is already loaded (or being loaded, we are happy either way)
            if (!lua_isfunction(L_, -1)) { // a module name, but no require() function :-(
                raise_luaL_error(errL_, "lanes receiving deep userdata should register the 'package' library");
            }

            luaG_pushstring(L_, _modname);                                                         // L_: DPC proxy metatable require() "module"
            if (luaG_getfield(L_, kIdxRegistry, LUA_LOADED_TABLE) != LuaType::TABLE) {             // L_: DPC proxy metatable require() "module" _R._LOADED
                // no L.registry._LOADED; can this ever happen?
                lua_pop(L_, 6);                                                                    // L_:
                raise_luaL_error(errL_, "unexpected error while requiring a module identified by DeepFactory::moduleName");
            }

            lua_pushvalue(L_, -2);                                                                 // L_: DPC proxy metatable require() "module" _R._LOADED "module"
            lua_rawget(L_, -2);                                                                    // L_: DPC proxy metatable require() "module" _R._LOADED module
            int const _alreadyloaded{ lua_toboolean(L_, -1) };
            if (!_alreadyloaded) { // not loaded
                lua_pop(L_, 2);                                                                    // L_: DPC proxy metatable require() "module"
                // require "modname". in case of error, raise it in errL_
                if (L_ == errL_) {
                    lua_call(L_, 1, 0);                                                            // L_: DPC proxy metatable module
                } else {
                    LuaError const _require_result{ lua_pcall(L_, 1, 0, 0) };                      // L_: DPC proxy metatable error?
                    if (_require_result != LuaError::OK) {
                        // failed, raise the error in the proper state
                        raise_luaL_error(errL_, luaG_tostring(L_, kIdxTop));
                    }
                }
            } else { // already loaded, we are happy
                lua_pop(L_, 4);                                                                    // L_: DPC proxy metatable
            }
        }
    }
    STACK_CHECK(L_, 3);                                                                            // L_: DPC proxy metatable
    LUA_ASSERT(L_, luaG_type(L_, StackIndex{ -2 }) == LuaType::USERDATA);
    LUA_ASSERT(L_, lua_istable(L_, -1));
    lua_setmetatable(L_, -2);                                                                      // L_: DPC proxy

    // If we're here, we obviously had to create a new proxy, so cache it.
    lua_pushlightuserdata(L_, prelude_);                                                           // L_: DPC proxy deep
    lua_pushvalue(L_, -2);                                                                         // L_: DPC proxy deep proxy
    lua_rawset(L_, -4);                                                                            // L_: DPC proxy
    lua_remove(L_, -2);                                                                            // L_: proxy
    LUA_ASSERT(L_, luaG_type(L_, kIdxTop) == LuaType::USERDATA);
    STACK_CHECK(L_, 1);
}

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

/*
 * Create a deep userdata
 *
 *   proxy_ud= deep_userdata( [...] )
 *
 * Creates a deep userdata entry of the type defined by the factory.
 * Parameters found on the stack are left as is and passed on to DeepFactory::newDeepObjectInternal.
 *
 * Reference counting and true userdata proxying are taken care of for the actual data type.
 *
 * Types using the deep userdata system (and only those!) can be passed between
 * separate Lua states via 'luaG_inter_move()'.
 *
 * Returns: 'proxy' userdata for accessing the deep data via 'DeepFactory::toDeep()'
 */
void DeepFactory::pushDeepUserdata(DestState const L_, UserValueCount const nuv_) const
{
    STACK_GROW(L_, 1);
    STACK_CHECK_START_REL(L_, 0);
    StackIndex const _oldtop{ lua_gettop(L_) };
    DeepPrelude* const _prelude{ newDeepObjectInternal(L_) };
    if (_prelude == nullptr) {
        raise_luaL_error(L_, "DeepFactory::newDeepObjectInternal failed to create deep userdata (out of memory)");
    }
    deepObjectCount.fetch_add(1, std::memory_order_relaxed);

    if (_prelude->magic != kDeepVersion) {
        // just in case, don't leak the newly allocated deep userdata object
        DeleteDeepObject(L_, _prelude);
        raise_luaL_error(L_, "Bad Deep Factory: kDeepVersion is incorrect, rebuild your implementation with the latest deep implementation");
    }

    LUA_ASSERT(L_, _prelude->refcount.load(std::memory_order_relaxed) == 0); // 'DeepFactory::PushDeepProxy' will lift it to 1
    LUA_ASSERT(L_, &_prelude->factory == this);

    if (lua_gettop(L_) - _oldtop != 0) {
        // just in case, don't leak the newly allocated deep userdata object
        DeleteDeepObject(L_, _prelude);
        raise_luaL_error(L_, "Bad DeepFactory::newDeepObjectInternal overload: should not push anything on the stack");
    }

    DeepFactory::PushDeepProxy(L_, _prelude, nuv_, LookupMode::LaneBody, L_);                      // proxy
    STACK_CHECK(L_, 1);
}

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

/*
 * Sets up [-1]<->[-2] two-way lookups, and ensures the lookup table exists.
 * Pops the both values off the stack.
 */
void DeepFactory::storeDeepLookup(lua_State* const L_) const
{
    // the deep metatable is at the top of the stack                                               // L_: mt
    STACK_GROW(L_, 3);
    STACK_CHECK_START_REL(L_, 0);                                                                  // L_: mt
    std::ignore = kDeepFactoryMetatableBijectionRegKey.getSubTable(L_, NArr{ 0 }, NRec{ 0 });      // L_: mt {}
    lua_pushvalue(L_, -2);                                                                         // L_: mt {} mt
    lua_pushlightuserdata(L_, std::bit_cast<void*>(this));                                         // L_: mt {} mt factory
    lua_rawset(L_, -3);                                                                            // L_: mt {}
    STACK_CHECK(L_, 1);

    lua_pushlightuserdata(L_, std::bit_cast<void*>(this));                                         // L_: mt {} factory
    lua_pushvalue(L_, -3);                                                                         // L_: mt {} factory mt
    lua_rawset(L_, -3);                                                                            // L_: mt {}
    STACK_CHECK(L_, 1);

    lua_pop(L_, 1);                                                                                // L_: mt
    STACK_CHECK(L_, 0);
}

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

/*
 * Access deep userdata through a proxy.
 *
 * Reference count is not changed, and access to the deep userdata is not
 * serialized. It is the module's responsibility to prevent conflicting usage.
 */
DeepPrelude* DeepFactory::toDeep(lua_State* const L_, StackIndex const index_) const
{
    STACK_CHECK_START_REL(L_, 0);
    // ensure it is actually a deep userdata we created
    if (LookupFactory(L_, index_, LookupMode::LaneBody) != this) {
        return nullptr; // no metatable, or wrong kind
    }
    STACK_CHECK(L_, 0);

    DeepPrelude** const _proxy{ luaG_tofulluserdata<DeepPrelude*>(L_, index_) };
    return *_proxy;
}

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

void DeepPrelude::push(lua_State* const L_) const
{
    STACK_CHECK_START_REL(L_, 0);
    kDeepProxyCacheRegKey.getSubTableMode(L_, "v");                                                // L_: DPC
    lua_pushlightuserdata(L_, const_cast<DeepPrelude*>(this));                                     // L_: DPC this
    lua_rawget(L_, -2);                                                                            // L_: DPC deep
    lua_remove(L_, -2);                                                                            // L_: deep
    STACK_CHECK(L_, 1);
}