Commit graph

328 commits

Author SHA1 Message Date
Edoardo Lolletti
17be53bbe4 add workaround for bugged luaL_checkstack in lua 5.4 and earlier 2026-05-15 20:20:50 +02:00
Edoardo Lolletti
400a541984 interpreter::pushobject: Use duel pointer from lua state rather than object being pushed i
Mitigates issue caused by moving around deleted effect objects, otherwise the push_weak_ref function would've been called on a garbage lua state pointer
2026-01-23 16:22:14 +01:00
Edoardo Lolletti
e2f5d6de3f Use a lua table to keep track of already loaded scripts
No longer use the handrolled partial hashmap
2025-07-12 22:28:56 +02:00
Edoardo Lolletti
0fe394f34d Use std::memcpy 2025-06-13 19:47:35 +02:00
Edoardo Lolletti
4ab85de875
Move Group's lifetime management to Lua (#166)
No longer manage a Group's lifetime explicitly in native code, but instead leverage Lua's garbage collector to handle it's lifetime.
In this change, Groups will be stored internally in the core inside a weak table, so that they will still be reference-able when needed, but if no reference were to exist in Lua code, they will be subject to garbage collection.
Whenever a Group is used by native code, its wrapped by a owned_lua template class, this is akin to std::shared_ptr--It reference counts the usages of that group internally in the core, including managing it in the Lua registry index when in use, so that it won't be collected.
With this approach, the exponential memory usage growth caused by long running procedures due to the creation hundredths of thousands of groups per iteration, is mitigated, since the temporary groups will be reaped more frequently, as opposed to the old approach of them being cleared only after the topmost Lua function had finished executing.
For now a single GC step is triggered whenever the total number of groups is more than 2048--an arbitrary value that seems to work well enough, but which can be changed/optimized after proper profiling.
A major upgrade with this approach is the removal of the concept of Groups "kept alive" and having to "explicitly delete" them to make them outlive a Lua function execution, since they will now follow Lua's variables lifetime properly.
2025-05-30 23:20:22 +02:00
Edoardo Lolletti
8104f31d69 Check at runtime for the lua library to be doing proper stack unwinding 2025-05-16 11:37:35 +02:00
Edoardo Lolletti
4e278a05ef Explicitly use lua_newuserdatauv with lua 5.4+ with 0 userdata value slots
In lua 5.4 and later, it's now possible to specify the number of userdata value slots, including 0, unlike in earlier versions where the slots were always fixed at 1.
Since they're unused in our case, use the full function to have 0 slots available and save some space for each userdata object
2025-04-25 12:05:00 +02:00
Edoardo Lolletti
8413cbec04 Ensure there's enough stack space before pushing herror handler function 2025-03-08 18:03:07 +01:00
Edoardo Lolletti
0fdc25fd74 Update lua stacktrace generation
Rather than calculate the stacktrace after the pcall, register an error handler in lua that will print it the moment the error is raised, thus leading to more accurate and meaningful traces
2025-03-04 23:56:25 +01:00
Edoardo Lolletti
76ac85d3c0 Update copyright year on changed files and LICENSE 2025-02-23 13:56:35 +01:00
Edoardo Lolletti
160d65db09 Update ensure_luaL_stack macro to work better with MSVC
Use a double expansion to workaround the tokenizer issue in MSVC compilers, allowing to properly call lua functions defined as macros
2025-02-23 13:48:04 +01:00
Edoardo Lolletti
695a31041a Mark ocgcore_lua_api_check with gnu's used attribute
Prevents its optimization when building with lto
2024-04-16 21:47:59 +02:00
Edoardo Lolletti
b47594cecc Use luaL_loadbufferx directly 2024-04-13 19:03:48 +02:00
Edoardo Lolletti
a388095e1f Wrap luaL calls so that the lua stack has always at least 5 elements
Functions from the auxiliary library, assume the stack has always at least 5 empty slots, add macro ensure_luaL_stack to wrap every such call so that the stack is checked for enough free space beforehand. This will make mandatory to use the latest version of visual studio 2017 as minimum for windows (it was already being required for c++17 support regardless), as it needs the conformant c++ preprocessor option to be used.
2024-04-13 10:36:47 +02:00
Edoardo Lolletti
787bd2d7e1 Minor cleanups in interpreter.cpp 2024-04-13 10:36:17 +02:00
Edoardo Lolletti
4a2e89f6c8 Fix stacktrace printing for coroutines 2024-02-02 23:19:00 +01:00
Edoardo Lolletti
44b0f30214 More minor c++17 updates 2024-01-05 16:53:38 +01:00
Edoardo Lolletti
9790b552a0 Address various warnings raised by clang's -Weverything
They were mostly extra breaks/extra semicolons
2024-01-05 15:50:25 +01:00
Edoardo Lolletti
ff41aec90a Update header inclusion 2024-01-05 13:03:46 +01:00
Edoardo Lolletti
da45f91460 Update copyright for 2024 2024-01-03 01:21:08 +01:00
Edoardo Lolletti
6dc321de75 Make LuaParamType an enum class 2023-12-08 19:55:50 +01:00
Edoardo Lolletti
cb5516a931 Rename get_card_or_group and lua_tostring_or_empty
Use the lua_get prefix for consistency with the other higher level lua getters
2023-12-03 11:34:38 +01:00
Edoardo Lolletti
a14af28a42 Don't use lua_resumec macro but declare an overload with lua 5.3 2023-11-27 19:52:26 +01:00
Edoardo Lolletti
954b493b90 Always disable "clooectgarbage" 2023-11-26 10:23:34 +01:00
Edoardo Lolletti
30d9343181 Assert that LUA_EXTRASPACE is big enough to store a pointer to duel
Also only copy exactly the size of the pointer rather than data equal to LUA_EXTRASPACE, as that could be more than a pointer, thus leaving to an out of bounds read
2023-05-07 19:42:38 +02:00
Edoardo Lolletti
e73fb449a8 Silence downcast warnings 2023-02-07 16:43:27 +01:00
Edoardo Lolletti
c9b3889ee8 Remove reintepret_cast 2022-11-02 13:01:42 +01:00
Edoardo Lolletti
a64cadf247 Add a __tostring method to card objects
Due to the way each card object is designed, the __tostring method has to be put in each cXXXXXX table, and cannot be put in the Card table as lua will do a rawget on the metatable to get the metamethods. Use a "wrapper" function as default tostring method to handle when that method is called directly from the metatable itself and not from a card object, that will load a defined Debug.CardToString function if present, otherwise emulates the same tostring behaviour lua uses
2022-10-20 19:07:56 +02:00
Edoardo Lolletti
f7487cf988 Comment a bit interpreter::load_card_scrip 2022-10-20 18:49:03 +02:00
Edoardo Lolletti
da0ac8fe46 Use lua_xmove when pushing PARAM_TYPE_INDEX in a coroutine
Instead of passing the value through the registry index
2022-10-20 18:47:16 +02:00
Edoardo Lolletti
457bc55b5c Use luaL_checkstack on all the library function that return an arbitrary number of values
The given lua stack to a c function has enough room to push few return values without issues, so all the functions that return a fixed amount of values (usually 1 or 2) weren't touched, all the others that return an unknown amount are now properly calling checkstack to make the lua api allocate the space needed for all the return values.
2022-10-06 13:11:11 +02:00
Edoardo Lolletti
703d8e1e60 Properly return COROUTINE_ERROR from interpreter::call_coroutine
It was returning the result of ``ret_fail`` that actually returned false (thus 0) rather than COROUTINE_ERROR (2)
2022-10-06 13:06:44 +02:00
Edoardo Lolletti
80a426e82a Make lua print api check messages through the core rather than asserting 2022-10-05 19:55:53 +02:00
Edoardo Lolletti
76f8bd4ad1 Fix coroutine not being unreferenced if the object to run wasn't a function 2022-10-03 22:16:36 +02:00
Edoardo Lolletti
45fed8aed1 Fix coroutines not being cleared when a recursive event trigger error is raised 2022-10-03 18:22:03 +02:00
Edoardo Lolletti
44ca205d31 interperer: Remove deepen and flatten, add `call_lua' instead
Remove the need to manually need to call those 2 functions to manage the call depth and no action counters, now in places where this is needed (so where lua_pcall is used) call_lua will be called, that handles everything. Also remove the redoundant calls to flatten/deepen that were called in functions that ended up calling other functions that did the pcall (and woudl also call those functions as well).
Fixes also another issue where deepen was not called immediately before the actual lua_pcall call but had some other lua functions inbetween that could have raised an error and made it skip the corresponding flatten call
2022-10-03 18:21:34 +02:00
Edoardo Lolletti
00e27da0c5 Remove double interpreter::deepen call in interpreter::get_operation_value 2022-10-03 18:17:48 +02:00
Edoardo Lolletti
eb82fa3c57 Minor syntax/readibility updates 2022-09-17 12:46:51 +02:00
Edoardo Lolletti
4a872ad768 Don't disable `load` under safe mode
It's now being used by the scripting library and in any case it doesn't really make anything safer, if someone could write a malicious eval call, that same person could write the malicious code in the script directly
2022-09-15 16:47:05 +02:00
Edoardo Lolletti
1a3ac27bd9
Add new OCG_DuelOptions parameter to make the core not load "unsafe" libraries (#126)
Add new OCG_DuelOptions parameter to make the core not load "unsafe" libraries
2022-09-03 22:43:40 +02:00
Edoardo Lolletti
e874def808 check_matching: remove no unneeded check
All the call sites now will have already checked the validity of the findex parameter
2022-06-11 13:05:40 +02:00
Edoardo Lolletti
ce7e7eee06 Revert "Mark for deletion groups set as labels in an effect when it's destroyed"
This reverts commit a85826a7d7.
2022-03-25 18:17:01 +01:00
Edoardo Lolletti
a85826a7d7 Mark for deletion groups set as labels in an effect when it's destroyed 2022-03-24 18:19:53 +01:00
Edoardo Lolletti
4da35cb7f1 Use preincrement operator instead of postincrement in the codebase
Semantically the proper operator to use for oneline increments is the preincrement one, the postincrement is the one to be used only in teh cases where the previous value is required.
2022-03-05 13:56:43 +01:00
Edoardo Lolletti
84a9a5bf8c Minor update 2022-03-05 00:32:39 +01:00
Edoardo Lolletti
53da389070 Update interpreter::add_param
Make it a templated function taking the parameter type at compile time, this allows performing more sanity checks, and fixes the situations with ambiguity of 0 with nullptr, also fixing some isntances where such behaviour was present.
2022-03-05 00:20:05 +01:00
Edoardo Lolletti
e77ec04678 Return integer values as lua_Integer from the various get_value functions
Removes ambiguity when the core needs a full unsigned 32 bit integer that could be altered when returned as signed integer since lua_Integer is an int64.
2022-01-08 20:04:19 +01:00
Edoardo Lolletti
6fe72bab09 Remove all NULL usage
Use nullptr
2021-12-13 14:07:51 +01:00
Edoardo Lolletti
7651500e56 Always set back current_state after a call to lua_resume
Fixes an issue where the core would keep using a yielded state to call functions on, thing that is forbidden by the lua api and caused assertions with lua built with LUA_APICHECK
2021-12-12 20:14:38 +01:00
Edoardo Lolletti
8bc15ddcfd Use luaL_checkstack when pushing values
Fixes assetions when using LUA_APICHECK
2021-12-12 19:35:58 +01:00