Commit graph

332 commits

Author SHA1 Message Date
Edoardo Lolletti
4a8aeeccef Apply suggestions 2025-05-30 23:09:29 +02:00
Edoardo Lolletti
436cc5aa99 Cleanup calling of lua_gc
The function like luaL_unref should never raise any error, so it's safe to be called outside a protected lua context
2025-05-30 23:09:29 +02:00
Edoardo Lolletti
6ba666b5ee Don't use pushcfunction/pcall to call luaL_unref
Lua guarantees that luaL_unref will never raise any error, and in case the only in this block of code could come from lua_checkstack, but we already weren't checking for its return value before
2025-05-30 23:09:29 +02:00
Edoardo Lolletti
c990686e2e Remove duel::release_script_group
It was now an empty function
2025-05-30 23:09:28 +02:00
Edoardo Lolletti
b71b5f2ebe Run a single gc step when collection on group creation and full gc step when resetting the field 2025-05-30 23:09:27 +02:00
Edoardo Lolletti
4ab7ffbb02 Update comments 2025-05-30 23:09:27 +02:00
Edoardo Lolletti
6e5398f563 Remove remnants of explicit group lifetime management
Rely entirely on lua, don't use `deleted` lua object to mark groups as deleted, fixes crashes under linux
2025-05-30 23:09:25 +02:00
Edoardo Lolletti
fb7088e6a6 Add more stack checking and call gc functions in a safe lua environment 2025-05-30 23:09:22 +02:00
Edoardo Lolletti
50a2b9ba22 Add initial draft of lua managed group lifetime 2025-05-30 23:09: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